ebp
is used as a frame pointer in Intel processors (assuming you're using a calling convention that uses frames).
It provides a known point of reference for locating passed-in parameters (on one side) and local variables (on the other) no matter what you do with the stack pointer while your function is active.
The sequence:
push %ebp ; save callers frame pointer
mov %esp,%ebp ; create a new frame pointer
sub $N,%esp ; make space for locals
saves the frame pointer for the previous stack frame (the caller), loads up a new frame pointer, then adjusts the stack to hold things for the current "stack level".
Since parameters would have been pushed before setting up the frame, they can be accessed with [bp+N]
where N
is a suitable offset.
Similarly, because locals are created "under" the frame pointer, they can be accessed with [bp-N]
.
The leave
instruction is a single one which undoes that stack frame. You used to have to do it manually but Intel introduced a faster way of getting it done. It's functionally equivalent to:
mov %ebp, %esp ; restore the old stack pointer
pop %ebp ; and frame pointer
(the old, manual way).
Answering the questions one by one in case I've missed something:
To start a new frame. See above.
It isn't. esp
is copied to ebp
. This is AT&T notation (the %reg
is a dead giveaway) where (among other thing) source and destination operands are swapped relative to Intel notation.
See answer to (2) above. You're subtracting 4 from esp
, not the other way around.
It's a parameter being passed to the function at 0x80482f0
. It's not being loaded into esp
but into the memory pointed at by esp
. In other words, it's being pushed on the stack. Since the function being called is puts
(see (5) below), it will be the address of the string you want puts
ed.
The function name in the <>
after the address. It's calling the puts
function (probably the one in the standard library though that's not guaranteed). For a description of what the PLT is, see here.
I've already explained leave
above as unwinding the current stack frame before exiting. The ret
simply returns from the current function. If the current functtion is main
, it's going back to the C startup code.