0

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?)

Rob
  • 14,746
  • 28
  • 47
  • 65
Mike
  • 1
  • 1
  • This *is* the part that restores ESP and EBP (like `leave`), in a function that moved the stack pointer to make room for locals. You know AT&T syntax is `op src, dst` right? – Peter Cordes Mar 09 '20 at 02:22
  • Oh shoot I totally forgot that. Sorry, I get it now. Thank you. – Mike Mar 09 '20 at 03:04

0 Answers0