Re: Any explanations
- From: "Rod Pemberton" <spamtrap@xxxxxxxxxx>
- Date: Sun, 27 Aug 2006 11:46:24 -0400
"John" <spamtrap@xxxxxxxxxx> wrote in message
news:1156649393.433513.187070@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
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
movl %esp, %ebp
subl $8, %esp
andl $-16, %esp
call __alloca
call ___main
leave
xorl %eax, %eax
ret
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
.
- Prev by Date: Re: Could not open hla.hw
- Next by Date: Re: Could not open hla.hw
- Previous by thread: Re: Any explanations
- Next by thread: Re: Any explanations
- Index(es):
Relevant Pages
|