Search code examples
cpu-registersmachine-codestack-pointer

Why is Saved Frame Pointer present in a stack frame?


I read that the SFP is used to restore EBP to its previous value. Why does EBP needs to return to it's initial value?


Solution

  • Why does EBP needs to return to it's initial value?

    When a function call is made, the compiler typically, as the first thing for the function body, pushes the current EBP value on to the stack and sets the EBP (base pointer/frame pointer) to the current ESP (stack pointer, always points to the top of the stack). Then EBP is used to access local variables and arguments of the function.

    The value of EBP is restored when a function returns o that it can serve the function call of the previous function.