Re: Any explanations



John schrieb:

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

What is actually going on? Why subtract 8 from esp (is that the stack
pointer?)
why this magical -16?

$ 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

.



Relevant Pages

  • Re: Indirect Jump
    ... order to generate a the indirect jump instruction, ... andl $-16, %esp ... movl %esp, %ebp ... testl %eax, %eax ...
    (alt.lang.asm)
  • my my code not compil MinGW-3.1.0
    ... pushl %ebp ... movl %esp, %ebp ... movl %eax, -484 ...
    (comp.lang.asm.x86)
  • Re: Any explanations
    ... movl %esp, %ebp ... xorl %eax, %eax ... can only assume that alloca() or the secondary mainconsumes a stack ...
    (comp.lang.asm.x86)
  • Re: passing floating point value to function
    ... pushl %eax ... which means it will push value stored in eax to stack, ... subl $4, %esp ... movl $.LC0, ...
    (alt.lang.asm)
  • GCC, function call, pass arguments via push
    ... of stack to pass arguments to another function, and will use push just ... movl %esp, %ebp ... movl, ... movl, %eax ...
    (comp.os.linux.development.apps)