LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
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
  Search this Thread
Old 03-11-2015, 11:24 AM   #1
stf92
Senior Member
 
Registered: Apr 2007
Location: Buenos Aires.
Distribution: Slackware
Posts: 4,442

Rep: Reputation: 76
as, the GNU assembler.


Somewhere in a source I wrote MOV DX,[BP+2], preceded by MOV BP,SP. At runtime, I get Segmentation fault, and it is just when executing said instruction. I tried to define a stack section, thus:

.SECTION JOHN aw [aw sorrounded by doubl quotes]

but don't know the directive to reserve space. What do you think about the segentation fault? [BP+2] is an implicit reference to the stack segment, that is, the instruction is equivalent to MOV DX,SS:[BP+2], that is, the OS seems to complain I'm directly accessing the stack segment.

Last edited by stf92; 03-11-2015 at 11:30 AM.
 
Old 03-11-2015, 11:38 AM   #2
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
You are using 16 bit addressing. I assume your program is running in 32 bit (or even 64 bit) mode.

I believe the x86 instruction set has the right overrides to actually use 16 bit addressing in 32 bit mode. But that means you are addressing memory that is not likely to be mapped and so it should seg fault.

Or maybe, you have the assembler in a strange mode so it assembles code for 16 bit mode. So you may be executing in 32-bit mode machine language that as assembled from asm only for 16-bit mode. Again, a seg fault would not be a surprising result.

Last edited by johnsfine; 03-11-2015 at 11:40 AM.
 
Old 03-11-2015, 12:34 PM   #3
stf92
Senior Member
 
Registered: Apr 2007
Location: Buenos Aires.
Distribution: Slackware
Posts: 4,442

Original Poster
Rep: Reputation: 76
I can't send the assembler listing here, but it clearly uses prefixes in this way (I say this Reading the listing): It prefixes my instructions, with don't use extended registers, with 0x66. It sure means the D flag, in the processor is low, at runtime, and so the following holds: Oper.size prefix 66H = yes, and adrs size prefix 67H =high mean effective oper.size = 32, effective adrs size = 16. This from the Intel manuals for family 6.

I think it would be convenient to have the mnemonics to specify either of the two prefixes.

EDIT: I attach the listing.
Attached Files
File Type: txt f02.txt (2.7 KB, 21 views)

Last edited by stf92; 03-11-2015 at 01:01 PM.
 
Old 03-11-2015, 01:26 PM   #4
genss
Member
 
Registered: Nov 2013
Posts: 741

Rep: Reputation: Disabled
stack grows from the top of virtual memory downwards
so in 32bit mode it is far above 66h
 
Old 03-11-2015, 03:15 PM   #5
stf92
Senior Member
 
Registered: Apr 2007
Location: Buenos Aires.
Distribution: Slackware
Posts: 4,442

Original Poster
Rep: Reputation: 76
If only you could tell me how to set gdb to show the registers each time it halts would be a great thing. Among them, sp would be seen to decrement by either two, four or eight with each push.
 
Old 03-11-2015, 04:10 PM   #6
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,781

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
Quote:
Originally Posted by stf92 View Post
If only you could tell me how to set gdb to show the registers each time it halts would be a great thing.
Auto Display
 
1 members found this post helpful.
Old 03-11-2015, 06:18 PM   #7
stf92
Senior Member
 
Registered: Apr 2007
Location: Buenos Aires.
Distribution: Slackware
Posts: 4,442

Original Poster
Rep: Reputation: 76
Thanks.
 
Old 03-12-2015, 04:16 AM   #8
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,863
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
This mixture of 16- and 32-bit code is bound to fail. If you are absolutely sure you cannot write this in C then use 32-bit Assembly everywhere.
 
Old 03-12-2015, 11:28 AM   #9
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
Why would you expect what you did to NOT seg fault??

Why would you expect the 16-bit sp register to contain a valid address?

You have pretty much confirmed that your 16-bit asm code was assembled with as believing it would run in 32-bit mode.

You can use 16-bit operands and even 16-bit addresses in 32-bit mode. But any 16-bit address you use must point to a valid 32-bit location or it will seg fault.

You don't seem to have done anything to cause the address that sp points at to be valid within your process's address space.

Quote:
Originally Posted by stf92 View Post
It prefixes my instructions, with don't use extended registers, with 0x66. It sure means the D flag, in the processor is low, at runtime, and so the following holds: Oper.size prefix 66H = yes, and adrs size prefix 67H =high mean effective oper.size = 32, effective adrs size = 16.
 
Old 03-13-2015, 03:05 PM   #10
stf92
Senior Member
 
Registered: Apr 2007
Location: Buenos Aires.
Distribution: Slackware
Posts: 4,442

Original Poster
Rep: Reputation: 76
i'm attaching a modified source which has been fully debugged. You could have begun by telling I'd should use ESP and EBP instead of sp and bp. That was all the problema.
 
Old 03-13-2015, 04:54 PM   #11
genss
Member
 
Registered: Nov 2013
Posts: 741

Rep: Reputation: Disabled
yes, sp is the bottom 16 bits of esp

https://www.tortall.net/projects/yas...registers.html

note that mov ax, bx keeps the upper 16 bits of eax intact,
while mov eax, ebx clears the upper 32 bits of rax
 
Old 03-13-2015, 06:00 PM   #12
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
Quote:
Originally Posted by stf92 View Post
You could have begun by telling I'd should use ESP and EBP instead of sp and bp. That was all the problema.
It was hard to guess what you were trying to do.

You could have given a bit more overview and/or context for what you were doing.
 
Old 03-13-2015, 07:37 PM   #13
stf92
Senior Member
 
Registered: Apr 2007
Location: Buenos Aires.
Distribution: Slackware
Posts: 4,442

Original Poster
Rep: Reputation: 76
aLL right. But now I'm told that by poping the first three ítems from the stack at program initializaiton I get the first invocation argument. I did that and then initialized sp as before. However, now I get segmentation fault. Where is the fetching of the program name, argumentes and its count well documented?
 
Old 03-13-2015, 09:33 PM   #14
genss
Member
 
Registered: Nov 2013
Posts: 741

Rep: Reputation: Disabled
http://asm.sourceforge.net/articles/startup.html
http://www.tldp.org/LDP/LGNET/issue84/hawk.html
etc.

"When all else fails, read the source"
 
1 members found this post helpful.
Old 03-14-2015, 03:37 PM   #15
stf92
Senior Member
 
Registered: Apr 2007
Location: Buenos Aires.
Distribution: Slackware
Posts: 4,442

Original Poster
Rep: Reputation: 76
Here's the finished program. It must be invoked as

f11_elf <four-digit hex uppercase number>

The number should be less than 8 and the result will be printed in hex. Example: f11_elf 0005.
Attached Files
File Type: txt F11_ELF.TXT (2.3 KB, 8 views)
File Type: txt F11_SOURCE.TXT (2.4 KB, 15 views)

Last edited by stf92; 03-14-2015 at 03:44 PM.
 
  


Reply



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
GNU Assembler (GAS/"as"): How to emit 8 bit relative branches? tedn Linux - Software 4 03-15-2012 10:16 PM
gnu assembler abhinav87 Programming 2 02-15-2011 10:45 PM
Native x86_64 Assembler for GNU/Linux? jhwilliams Linux - Software 1 06-13-2007 05:00 PM
gas - GNU Assembler - Source Platform yitzle Linux - Software 2 02-24-2007 08:32 PM
assembler usr Programming 2 11-15-2003 05:15 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration