In the "Programming from the ground up" book, I see the stack frame when calling function is somewhat like this
parameter 2 <- 12(%ebp)
parameter 1 <- 8(%ebp)
return address <- 4(%ebp)
ebp <- (%ebp)
local var 1 <- -4(%ebp)
local var 2 <- -8(%ebp) and (%esp)
It says that when the function return, it stores the return value to %eax
, then it restore the %ebp
and %esp
to the position before the function was called, then the ret
will pop whatever on top of the stack and point %eip
to that address (which is supposed to be the return address). But when I see the code, I kinda suspect the line
movl %ebp, %esp # This
popl %ebp # And this
ret
Why does it point the base pointer to the top of the stack? Isn't when popl
is executed, only one local variable will be gone (because that's where %esp
is pointing?)