LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   x86 Assembly - segmentation fault? (https://www.linuxquestions.org/questions/programming-9/x86-assembly-segmentation-fault-199832/)

jrtayloriv 07-01-2004 05:50 AM

x86 Assembly - segmentation fault?
 
Howdy folks:

Just began exploring x86 assembly the other day and I'm having a few problems with a practice program that I am trying to write that displays a string and then displays it again backwards.

First, the code below assembles and links fine. But I am getting a segmentation fault at the lds and les instructions in the revstr procedure. Can anyone see what is causing this?

Second, I thought that placing the variable name within brackets retrieved the data itself not the address. movsb needs the addresses of the two strings that it will operate on, but when I try to copy the addresses into DS:SI and ES:DI I get an assembler error that says:

reversestring.asm:42: error: invalid combination of opcode and operands

The documentation that I have on how to use movsb shows it with brackets around the variables. What does this mean, because I know that it isn't attempting to move the variables values into the registers, because the values have no use here.

lds si, [Message]
les di, [MessageReversed]

It seems that it would make more sense to have it

lds si, Message
les di, MessageReversed

Or am I misunderstanding the lds and les instructions? Perhaps someone could attempt to give me a better explanation of these instructions than the one that I have. I tried reading the Intel Instruction Set Reference entry on lds/les and as anyone who has been there knows, the manual is not exactly targeted towards a novice assembly programmer.


Anyways, below is the source. Any assistance would be greatly appreciated. Even better than an answer would be a pointer in the right direction (as far as reading topics that would assist me in finding the answer).

Thanks,
JT

*****************************************************************************

;ReverseString
;Displays a text string both normally and in reverse


[SECTION .text]

extern puts
global main

main:
push ebp
mov ebp, esp
push ebx
push esi
push edi

push dword Message
call puts
add esp, 4

call revstr

push dword MessageReversed
call puts
add esp, 4

pop edi
pop esi
pop ebx
mov esp, ebp
pop ebp
ret

revstr: ;reverse 'message' and store in 'messagereversed'
lds si, [Message]
les di, [MessageReversed]
std
mov cx, 40
rep movsb
ret


[SECTION .data]

Message DB "This is a sentence.", 10, 0

[SECTION .bss]

MessageReversed resb 40

jinksys 07-05-2004 12:52 AM

Your problem is here:
Code:

revstr: ;reverse 'message' and store in 'messagereversed'
 lds si, [Message]
 les di, [MessageReversed]
 std
 mov cx, 40
 rep movsb

STD tells movsb to decrement memory addresses when moving bytes, and since si is set to the beginning of Message, it reads before Message and segfaults. BTW, [Message] loads the value of Message, not the address.


All times are GMT -5. The time now is 02:34 PM.