tags:

views:

69

answers:

1

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.

  1. But I have no idea what the subl $24, %esp is. I understood that it moves the stack pointer down by 24 bytes. Correct?
  2. What is immed by the way?
  3. 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?
  4. 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.
  5. 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!

+3  A: 

The compiler is reserving space on the stack for locals and whatever other needs it might have. I'm not sure offhand why it's reserving 24 bytes (it doesn't seem to need or use it all).

When calling function f(), instead of using a push instruction to put the parameter on the stack, it uses a simple movl to the last location it reserved:

movl    8(%ebp), %eax    ; get the value of `y` passed in to `h()`
movl    %eax, (%esp)     ; put that value on the stack for call to `f()`

A more interesting (in my opinion) thing happening here is how the compiler is handling the call to f(2):

movl    $2, 8(%ebp)      ; store 2 in the `y` argument passed to `h()`
                         ;     since `h()` won't be using `y` anymore
leave                    ; get rid of the stackframe for `h()`
jmp f                    ; jump to `f()` instead of calling it - it'll return
                         ;     directly to whatever called `h()`

To answer your question, "immed by the way?" - that is what the instruction reference uses to indicate that the value is encoded in the instruction opcode instead of coming somewhere else like a register or memory location.

Michael Burr
why is it subtracting? see update to my question...
drozzy
subtracting from the `esp` register essentially 'reserves' space on the stack. It's as if you've pushed 24 bytes worth of stuff (6 32-bit values) onto the stack - the difference being that you can't rely on what those 'pushed' values now on the stack are - all you know is that you can use that memory without worrying that a subsequent push will overwrite it.
Michael Burr
It subtracts because the stack grows 'downwards' in memory. 'immed' is an immediate value, much like the $24 in the line you cite.
mocj
Also note that the compiler has made two optimizations: The assignment to x is removed because the value is not used. The second call to f is turned into a jmp-instruction because it does not need to return back to the h function when the call is complete.
Ville Krumlinde
So I update my question with so more... questions. Sorry.
drozzy
Subtracting just moves the stack pointer down, essentially "pushing" things on the stack. Then this creates room in the stack for the local variables to be accessed as offsets from the frame pointer.
ghills
To answer your question 3 - it moves 8 bytes from the frame pointer to get the first parameter to the function. You are right, it is 8 because there are 4 bytes for the old frame pointer that is saved on the stack, and 4 bytes for the return address.The instruction is used to get the parameter into a register.
ghills
OOh, I see. So 4 bytes moves it just before the return address, and another 4 bytes moves it just before the argument y. Thanks!
drozzy
Added another question, about 8(%ebp)
drozzy