Search code examples
assemblyx86stackstack-framestack-pointer

(x86) Is the value of ESP relative to EBP, or not?


I always used to think that SP was relative to BP, meaning that pushing and popping stuff on the stack would use the address BP - SP, with BP being the start of the stack, and SP being the current item on the top of the stack. However, after looking at the CDECL calling convention, that would not make sense, as SP is assigned to the value of BP. So, if I change BP, does that even affect SP? Is BP even used for push/pop instructions? Or does push/pop just work with SP? Is BP even needed?


Solution

  • So, if I change BP, does that even effect SP? Is BP even used for push/pop instructions?

    No, and no.

    BP/EBP/RBP is often used to hold the address of the current stack frame of a function. That is, an address relative to the function's arguments and local variables that will stay the same throughout the function (whereas the stack pointer might change temporarily).
    Instructions like PUSH / POP or CALL / RET will change SP/ESP/RSP but not BP/EBP/RBP.