assemblystackstack-framestack-pointer

Doesn't the frame pointer make the stack pointer redundant?


As far as I understand it, the stack pointer points to the "free" memory on the stack, and "pushing" data on the stack writes to the location pointed by the stack pointer and increments/decrements it.

But isn't it possible to use offsets from the frame pointer to achieve the same thing, thus saving a register. The overhead from adding offsets to the frame pointer is pretty much the same as the overhead of incrementing and decrementing the stack pointer. The only advantage I see is accessing data from the "top" (or bottom) will be faster, as long as it is not a push or pop operation, e.g. just reading or writing to that address without incrementing/decrementing. But then again, such operations would take a single extra cycle using the frame pointer, and there will be one additional register for general purpose use.

It seems like only the frame pointer is really needed. And it even serves a lot more purpose than just modifying data in the current stack frame, such as to be used in debugging and for stack unwinding. Am I missing something?


Solution

  • Well, yes, and in fact common for 64-bit code generators. There are complications however that do not make it universally possible. A hard requirement is that the value of the stack pointer is known at compile time so the code generator can generate the offset reliably. This does not work when: