Re: Any explanations



"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.

.



Relevant Pages

  • Re: Newbie question...
    ... something like push allign - pop allign ... @1: dec esp ... push eax ...
    (alt.lang.asm)
  • Re: Dangerous convertion?
    ... performed only once by the compiler, not every time the program runs. ... extern struct foo *global_f; ... movl %esp, %ebp ... movl global_f, %eax ...
    (comp.lang.c)
  • Re: How do I delete a folder through code?
    ... Here;s some data I got on the oldest, klunkiest compiler I have available ... 0001c 33 c0 xor eax, ... 00024 c3 ret 0 ...
    (microsoft.public.vc.mfc)
  • Re: Newbie question...
    ... and esp, -16 ... push DWORD PTR [ecx-4] ... push eax ...
    (alt.lang.asm)
  • Re: misc, OT: C compiler, x86-64 or not x86-64...
    ... or partly branch my lower compiler and make an x86-64 version as well?... ... push ebp; mov ebp, esp ... push dword HhDMmIPiboWKIGVIFi ... lea eax, ...
    (alt.lang.asm)