Page 1 of 1

Some extra ASM

Posted: Sat May 06, 2006 11:00 pm
by azcoder
I am follwing a the little ASM tutorial on osdev.net:
http://www.osdev.net/howtos/1/index.html

But my gcc is giving me some extra asm.

test.c:
int main()
{
}

ndisasm -b 32 test.bin yields:
00000000 55 push ebp
00000001 89E5 mov ebp,esp
00000003 83EC08 sub esp,byte +0x8
00000006 83E4F0 and esp,byte -0x10
00000009 B800000000 mov eax,0x0
0000000E 29C4 sub esp,eax
00000010 C9 leave
00000011 C3 ret

Does anyone know why the extra stuff betwee mov ebp,esp and leave?

Thanks for any feedback.....

Re: Some extra ASM

Posted: Sat May 06, 2006 11:00 pm
by Daedalus
I'm not sure, but I think that's just code to give you access to any parameters.

Re: Some extra ASM

Posted: Sun May 07, 2006 11:00 pm
by JAAman
push ebp
mov ebp, esp
is sufficient to give access to parameters

sub esp, 0x08
not sure why this is -- it appears to be reserving some space on the stack, but im not sure why...

this is aligning the stack:

and esp, 0xFFFF FFF0 (-16 in two's complement, sign extended to 32bits)
this will adjust the stack to the nearest lower 16byte alignment (because it is always rounded down, there will never be any overwrite of a previous stack frame)



mov eax, 0
sub esp,eax

my guess is that this is to reserve space on the stack for local variables, since you dont have any local variables, it is using 0 (and not reserving any space at all)