Share your knowledge at the LQ Wiki.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 07-01-2004, 05:50 AM   #1
Registered: Jun 2004
Location: Inland NW, US
Distribution: Ubuntu
Posts: 365
Blog Entries: 1

Rep: Reputation: 44
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 ESI 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).



;Displays a text string both normally and in reverse

[SECTION .text]

extern puts
global 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

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

[SECTION .data]

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

[SECTION .bss]

MessageReversed resb 40
Old 07-05-2004, 12:52 AM   #2
Registered: Aug 2003
Location: 63123
Distribution: OpenSuSE/Ubuntu
Posts: 419

Rep: Reputation: 35
Your problem is here:
revstr: ;reverse 'message' and store in 'messagereversed' 
 lds si, [Message] 
 les di, [MessageReversed] 
 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.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
yast segmentation fault, system freezing - nvidia driver at fault? BaltikaTroika SUSE / openSUSE 2 12-02-2005 09:34 AM
x86 assembly programming in Linux XsuX Programming 9 12-01-2004 09:45 AM
Segmentation Fault (What is that?) jlacroix Fedora 6 11-17-2003 08:32 PM
segmentation fault Linh Programming 6 11-03-2003 08:25 AM
x86 assembly: error message mandrake_linux Programming 1 06-12-2001 09:00 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:35 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration