_main:
; create stack frame
pushl %ebp
movl %esp, %ebp
; save one local variable
subl $8, %esp
; zero four rightmost bits of esp
andl $-16, %esp
; set eax to (0 + 15 + 15) / 2^4 * 2^4 = 16
movl $0, %eax
addl $15, %eax
addl $15, %eax
shrl $4, %eax
sall $4, %eax
; set local variable to eax (16)
movl %eax, -4(%ebp)
movl -4(%ebp), %eax
; call allocation and main
call __alloca
call ___main
; set eax to zero (return value)
movl $0, %eax
; fold stack frame and return to caller
leave
ret
I compiled a int main(){return 0;}
C code using gcc -S return_zero.c
(on Windows) and this is what I got (I removed the assembler directives and added explanation comment as much as I understood. Correct me if I'm wrong, please.).
I don't understand three things:
- Why does the compiler align esp to 16?
- Why is eax set to 16, why it is done in such a complicated way, and why is there a local variable also set to 16?
- What are __alloca and ___main?
I got somewhat unclear explanations to both #1 and #3 on the internet so I'd like if someone can answer more deeply and for #2 I haven't find any explanation so if someone can explain it'll be great.
If any further information is needed comment and I'll post it.
Thanks!