Frame Pointer Explanation in Assembly and Stack

assemblystack

In MIPS assembly, there is a register for the stack pointer, and another register for the frame pointer. What is the frame pointer and what is its purpose? How does it differ from the stack pointer?

Best Answer

In MIPS assembly, the stack pointer points to the top of the stack. As you allocate space on the stack, the stack pointer ($sp) moves to point to the free memory.

When calling a subroutine in MIPS assembly (registers were at a premium in those days - register based parameters where unconventional), one writes the parameters to the stack and then advances the stack pointer.

When the method starts out, a parameter may be at an offset of 16($sp). However, as variables are placed on the stack, the stack pointer moves and the same parameter may now be located at 24($sp) instead. This can make the code a bit confusing.

The frame pointer ($fp) points to the start of the stack frame and does not move for the duration of the subroutine call. This points to the base of the stack frame, and the parameters that are passed in to the subroutine remain at a constant spot relative to the frame pointer.

Realize that the frame pointer will need to be stored and restored with subroutine calls that modify it.

Further reading:

Related Topic