int main(void){
return 0;
}
================g++ -O -S compile.cpp
.file "x.c"
.def ___main; .scl 2; .type 32; .endef
.file "x.c"
.def ___main; .scl 2; .type 32; .endef
.text
.align 2
.p2align 4,,15
.globl _main
.def _main; .scl 2; .type 32; .endef
_main:
pushl %ebp
movl $16, %eax
movl %esp, %ebp
subl $8, %esp
andl $-16, %esp
call __alloca
call ___main
leave
xorl %eax, %eax
ret
What is actually going on? Why subtract 8 from esp (is that the stack
pointer?)
why this magical -16? what happens when alloca and main are called?
What
is leave and xorl? Is there a explanation of something like this or a
tutorial on
the web that can help me understand this?
Thanks a lot,
--j
Possibly to reserve space for any exception objects.
> why this magical -16? what happens when alloca and main are called?
> What
Probably to make sure the stack pointer is correctly aligned, -16 is
0xfffffff0.
> is leave and xorl? Is there a explanation of something like this or a
> tutorial on
xorl %eax, %eax is a quick register to register techinque to zero eax,
which is used for the return value.
> the web that can help me understand this?
>
Have a look for gcc documentation on the web, where yoou will also find
x86 programming information.
--
Ian Collins.
$ is the actual programm-counter.
> what happens when alloca and main are called?
I´m not familar with C.
I guess alloca allocate some memory?
> What is leave and xorl?
Leave destroy the stackframe, where normaly the enter-instruction
build one. A stackframe is only used in highlevel-language like C,
to put some values on the stack for subroutines.
When xor used with destination and source the same register,
the register will be cleared. This instruction is smaller
then movl &0,%eax.
Dirk
In the wikipedia there is an example EXACTLY like yours.
GOTO
http://en.wikibooks.org/wiki/X86_Assembly/GAS_Syntax
John wrote:
> I would like to understand the following C++/C program in assembly
>
> int main(void){
> return 0;
> }
>
> ================g++ -O -S compile.cpp
>
> .file "x.c"
> .def ___main; .scl 2; .type 32; .endef
> .file "x.c"
> .def ___main; .scl 2; .type 32; .endef
> .text
> .align 2
> .p2align 4,,15
> .globl _main
> .def _main; .scl 2; .type 32; .endef
> _main:
> pushl %ebp
> movl $16, %eax
probably the argument to the 'alloca' call. Alloca allocates stack
space. Not sure why it is being called.
>
> movl %esp, %ebp
>
> subl $8, %esp
this is generally how local variables are allocated. The pushl ebp, move
esp, ebp, and subl 8, esp could be replaced by an enter instruction, but
that is a lot slower so most compilers do this explicitly. You haven't
declared local variables so this might have something to do with C++
exception handling.
>
> andl $-16, %esp
align the stack. Stack alignment is done by GCC because floating point
accesses are significantly faster when aligned.
>
> call __alloca
now allocate space on the stack. Not sure what is happening here. Could
be allocating something required for C++ exception use. or could be doing
alignment for the next call, or could be
allocating space for return data from the next call
>
> call ___main
this is probably a C++ stub that somehow executes the constructors for
file-scope objects.
>
>
> leave
restore ESP and EBP to what they were when the function entered
(same as movl %ebp, %esp, popl %ebp)
>
> xorl %eax, %eax
this is the code for the 'return 0' statement. By convention integers
are returned in EAX. This instruction sets EAX to 0.
>
> ret
>
> What is actually going on? Why subtract 8 from esp (is that the stack
> pointer?)
> why this magical -16? what happens when alloca and main are called?
> What
> is leave and xorl? Is there a explanation of something like this or a
> tutorial on
> the web that can help me understand this?
>
> Thanks a lot,
> --j
.
These two instructions (out of order) save and replace the current stack
pointer. They are equivalent the 'leave' instruction. These two when
combined with the 'subl $8, %esp' are the C function's prolog. The prolog
and epilog (below) create and destroy the stackframe, respectively.
pushl %ebp
movl %esp, %ebp
This allocates space for a 'long'. This is usually for passed arguments,
but main has none due to 'void'. Since there are no declaread variables, I
can only assume that alloca() or the secondary main() consumes a stack
argument.
subl $8, %esp
Again, you usually see this for a argument passed by a register. Perhaps it
is consumed by alloca() or the secondary main().
movl $16, %eax
I can only assume Mr. Collins statement about stack alignment is correct for
the following:
andl $-16, %esp
calls alloca() and a secondary main(). These are not present in C code.
They either have something to do with C++ or your program.
call __alloca
call ___main
This restores the saved stack pointer. It is equivalent to 'movl %ebp,
%esp; popl %ebp'. It is also the C functions epilog.
leave
This generates the zero and return for the return(0).
xorl %eax, %eax
ret
Rod Pemberton
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.