So... I'm compiling into assembler, with gcc -S -O2 -m32:
void h(int y){int x; x=y+1; f(y); f(2); }
And it gives me the following:
.file "sample.c"
.text
.p2align 4,,15
.globl h
.type h, @function
h:
pushl %ebp
movl %esp, %ebp
subl $24, %esp
movl 8(%ebp), %eax
movl %eax, (%esp)
call f
movl $2, 8(%ebp)
leave
jmp f
.size h, .-h
.ident "GCC: (GNU) 4.4.3 20100127 (Red Hat 4.4.3-4)"
.section .note.GNU-stack,"",@progbits
Now I know what pushl and movel: they store the current frame pointer onto the stack and then set the value of the frame pointer register to the value of the Stack Pointer.
- But I have no idea what the subl $24, %esp is. I understood that it moves the stack pointer down by 24 bytes. Correct?
- What is immed by the way?
- Why does movl 8(%ebp), %eax use 8? Is it 8 bytes? Is this to accommodate for return value + argument y to h? Or am I completely off here. So this means look back 8 bytes from the stack pointer?
- What does movl $2, 8(%ebp) do? It copies contant 2 to the location 8 bytes before the frame pointer. Did the frame pointer change when we called f? If yes - then 8(%ebp) points to the argument location for f.
- What does leave do? How can it "remove" a stack frame? I mean you cant just remove a piece of memory. In the doc it says it does mov(esp, ebp), pop ebp.
Thanks!