LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices

Reply
 
LinkBack Search this Thread
Old 07-01-2004, 05:50 AM   #1
jrtayloriv
Member
 
Registered: Jun 2004
Location: Inland NW, US
Distribution: Ubuntu
Posts: 363
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).

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
 
Old 07-05-2004, 12:52 AM   #2
jinksys
Member
 
Registered: Aug 2003
Location: 63123
Distribution: OpenSuSE/Ubuntu
Posts: 417

Rep: Reputation: 31
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.
 
  


Reply


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
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
yast segmentation fault, system freezing - nvidia driver at fault? BaltikaTroika Suse/Novell 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


All times are GMT -5. The time now is 03:56 AM.

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