Value assignment to esp assigns a different one?

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
User avatar
ScropTheOSAdventurer
Member
Member
Posts: 86
Joined: Sun Aug 25, 2013 5:47 pm
Location: Nebraska, USA

Value assignment to esp assigns a different one?

Post 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.
"Procrastination is the art of keeping up with yesterday."
User avatar
bluemoon
Member
Member
Posts: 1761
Joined: Wed Dec 01, 2010 3:41 am
Location: Hong Kong

Re: Value assignment to esp assigns a different one?

Post 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 ?
User avatar
ScropTheOSAdventurer
Member
Member
Posts: 86
Joined: Sun Aug 25, 2013 5:47 pm
Location: Nebraska, USA

Re: Value assignment to esp assigns a different one?

Post 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.
Last edited by ScropTheOSAdventurer on Fri Mar 21, 2014 9:57 pm, edited 1 time in total.
"Procrastination is the art of keeping up with yesterday."
User avatar
thepowersgang
Member
Member
Posts: 734
Joined: Tue Dec 25, 2007 6:03 am
Libera.chat IRC: thePowersGang
Location: Perth, Western Australia
Contact:

Re: Value assignment to esp assigns a different one?

Post by thepowersgang »

I assume you used GAS to compile your assembly. You need to prefix literals with '$', otherwise they're treated as addresses.
Kernel Development, It's the brain surgery of programming.
Acess2 OS (c) | Tifflin OS (rust) | mrustc - Rust compiler
Currently Working on: mrustc
User avatar
ScropTheOSAdventurer
Member
Member
Posts: 86
Joined: Sun Aug 25, 2013 5:47 pm
Location: Nebraska, USA

Re: Value assignment to esp assigns a different one?

Post 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?
"Procrastination is the art of keeping up with yesterday."
User avatar
sortie
Member
Member
Posts: 931
Joined: Wed Mar 21, 2012 3:01 pm
Libera.chat IRC: sortie

Re: Value assignment to esp assigns a different one?

Post 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.
User avatar
ScropTheOSAdventurer
Member
Member
Posts: 86
Joined: Sun Aug 25, 2013 5:47 pm
Location: Nebraska, USA

Re: Value assignment to esp assigns a different one?

Post 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.
"Procrastination is the art of keeping up with yesterday."
User avatar
zhiayang
Member
Member
Posts: 368
Joined: Tue Dec 27, 2011 7:57 am
Libera.chat IRC: zhiayang

Re: Value assignment to esp assigns a different one?

Post 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.
User avatar
ScropTheOSAdventurer
Member
Member
Posts: 86
Joined: Sun Aug 25, 2013 5:47 pm
Location: Nebraska, USA

Re: Value assignment to esp assigns a different one?

Post 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!
"Procrastination is the art of keeping up with yesterday."
Post Reply