Re: Any explanations
- From: "Eman" <spamtrap@xxxxxxxxxx>
- Date: Sun, 27 Aug 2006 23:15:22 +0400
"Eman" <e!m!a!n@xxxxxxxxxxx> ???????/???????? ? ???????? ?????????:
"John" <spamtrap@xxxxxxxxxx> ???????/???????? ? ???????? ?????????:I would like to understand the following C++/C program in assembly
int main(void){
return 0;
}
In addition. Actually, to implement this main, the following code would be enough:
_main:
xor eax,eax ;or "sub eax,eax" or "and eax,0" or "mov eax,0"
ret
However, the compiler expects there would be done something useful
rather than simply "return 0;". That's why it provides some
initialization (as was also said by others: calling "__alloca" - typically that routine allocates some storage from the stack; and "___main" - this looks like main internal initialization routine).
_main:
pushl %ebp
movl $16, %eax
movl %esp, %ebp
subl $8, %esp
andl $-16, %esp
call __alloca
call ___main
leave
xorl %eax, %eax
ret
push ebp ; mov ebp,esp ;"standard" prologue
sub esp,8 ;create "stack frame" with two DWORDs
and esp,0FFFFFFF0h ;align stack pointer / 16
mov eax,16 ;argument for __alloca and/or __main
call __alloca ;this is __alloca it's doing something
call ___main ;this is ___main it's doing something
xor eax,eax ;the zero value for return (EAX:=0)
leave ;restore stack ("mov esp,ebp", "pop ebp")
ret
What is actually going on? Why subtract 8 from esp (is that the stack
pointer?)
Assured size of stack frame.
why this magical -16?
Stack pointer' alignment.
what happens when alloca and main are called?
It depends on startup / internal support code of the concrete
compiler version/os. I do not know, honestly. I'm sure someone
here might explain exactly what they do. If i'd need it, i'll
look in disassembly or in the CRT-sources (if available).
--
PS. Excuse me, if this message has appeared to be duplicated.
.
- Prev by Date: Re: Could not open hla.hw
- Next by Date: Get the FAQs
- Previous by thread: Re: Any explanations
- Index(es):
Relevant Pages
|