Page 1 of 1

Value assignment to esp assigns a different one?

Posted: Fri Mar 21, 2014 7:31 pm
by ScropTheOSAdventurer
Ok. I am using qemu, linked with GDB (plus some debugging symbols). In my _start function, here is my code (disassembled by gdb for the sake of comparing the stack_top address with the funky value I get later):

Code: Select all

   0x001001c0 <+0>:	mov    0x10900d,%esp
   0x001001c6 <+6>:	call   0x1001a8 <Alo_Main>
   0x001001cb <+11>:	cli    
   0x001001cc <+12>:	hlt    
   0x001001cd <+13>:	jmp    0x1001cd <_start+13> 
Now, as I step through the code with gdb, immediately after I execute the 0x001001c0 instruction (which sets up the stack obviously), here is what I get for esp when I run "info registers" in gdb:
esp 0x458d8da0 0x458d8da0
---------------------------------------------------------

For comparison, here is the value of esp BEFORE the instruction:
esp 0x7ff00 0x7ff00

How on earth am I getting this wild value immediately after setting the stack to 0x10900d?

Any help would be appreciated.

Re: Value assignment to esp assigns a different one?

Posted: Fri Mar 21, 2014 9:20 pm
by bluemoon
I'm not native to GAS syntax but do you by any chance loading esp with value at memory 0x10900d instead of the constant $0x10900d ?

Re: Value assignment to esp assigns a different one?

Posted: Fri Mar 21, 2014 9:56 pm
by ScropTheOSAdventurer
I am not familiar with it either (I wrote the assembly originally in Intel syntax; it merely disassembled into the syntax you saw), however, I can give you the original source if you like :):

Code: Select all

 .intel_syntax noprefix 
 
.set ALIGN,    1<<0            
.set MEMINFO,  1<<1            
.set FLAGS,    ALIGN | MEMINFO  
.set MAGIC,    0x1BADB002       
.set CHECKSUM, -(MAGIC + FLAGS) 

.section .multiboot
.align 4
.long MAGIC
.long FLAGS
.long CHECKSUM
 
 
#Here is the stack part: 

.section .TheStack, "aw", @nobits 
stack_bottom: 
.skip 32768 # 32 kilobytes. 
stack_top: 


# Here is our entry point! 

.section .text 
.global _start 
.type _start, @function 
_start: 
	mov esp, stack_top #here is the assembly's equivalent of the disassembled   "mov    0x10900d,%esp" instruction
	call Alo_Main 
	
	cli 
	hlt 
	.InfiniteLoop: 
		jmp .InfiniteLoop 
		
		
.size _start, . - _start  


So, in any case, it should have pointed to the address of stack_top. Maybe it is just an emulator bug. I'll keep probing and seeing what I can come up with, and check and see what "mov 0x10900d, %esp" is in AT&T syntax.

Re: Value assignment to esp assigns a different one?

Posted: Fri Mar 21, 2014 9:57 pm
by thepowersgang
I assume you used GAS to compile your assembly. You need to prefix literals with '$', otherwise they're treated as addresses.

Re: Value assignment to esp assigns a different one?

Posted: Fri Mar 21, 2014 10:01 pm
by ScropTheOSAdventurer
@thepower: I guess you see that I did intel syntax. Does stack_top and stack_bottom need to have some special thing to them? Time to take a trip to google.....

UPDATE: I decided to disassemble it into intel syntax, and here we get this instruction:

Code: Select all

 
mov    esp,DWORD PTR ds:0x10900d


Any ideas as to why it is assembling to this?

Re: Value assignment to esp assigns a different one?

Posted: Fri Mar 21, 2014 10:14 pm
by sortie
That is wrongly adapted GAS assembly taken from bare bones. Use use Nasm assembly from the linked addon tutorial. Your code loads the value at the new stack into esp, rather than the new stack into esp.

Re: Value assignment to esp assigns a different one?

Posted: Fri Mar 21, 2014 10:18 pm
by ScropTheOSAdventurer
I should've figured. Thanks sortie! But then, how do I get the actual address of stack_top in intel syntax?

UPDATE:
@sortie: I looked at the nasm code for pointing to the stack_top label, and it is exactly the same code I used, so I am confused.

Re: Value assignment to esp assigns a different one?

Posted: Sat Mar 22, 2014 1:06 am
by zhiayang
ScropTheOSAdventurer wrote:I should've figured. Thanks sortie! But then, how do I get the actual address of stack_top in intel syntax?

UPDATE:
@sortie: I looked at the nasm code for pointing to the stack_top label, and it is exactly the same code I used, so I am confused.

Not familiar with intel syntax, but since you're using GAS, have you tried prefixing your labels with '$' to get their address? eg.

Code: Select all

Label:
.asciz "this is a string"


mov $Label, %rsi
either way, without using the '$' prefix GAS treats what you put as an address, ie. it takes the value at the address. For your example,

Code: Select all

mov esp, stack_top
would really be

Code: Select all

mov esp, [stack_top]
, which is really not what you want.


Note that I could be completely wrong and that GAS actually does intel syntax properly. In that case take this as a small lesson on how AT&T syntax works (:

Also, I personally recommend using GAS syntax anyway, it's more intuitive and you should already have an assembler from your binutils toolchain.


EDIT: I didn't really answer your question right.
IIRC for intel syntax, the address of literals is simply the name of the literal, in your case 'stack_top', which is why I suspect GAS is doing something a little wrong.


EDIT EDIT:
By looking at your disassembly, I see 'dword ptr ds:', which unequivocally means 'the value at the address of'.
I don't really suggest using intel syntax with an assembler designed for AT&T syntax.

The simplest options here would be:
1. Learn AT&T syntax, then convert your code.
2. Use NASM to assemble your code.

Re: Value assignment to esp assigns a different one?

Posted: Sat Mar 22, 2014 8:15 am
by ScropTheOSAdventurer
I switched to GAS syntax when it came to setting up the stack, and I inspected the stack and registers in gdb, and everything is working well! Thanks for all your help!