28

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?

  • 1
    What research have you already done - and what is your familiarity with the terms? What system is this for (different systems have different terminology)? – user40980 Apr 8 '13 at 21:07
  • I just want to get familiarized with frame pointer(mips assembly). I don't understand how it helps, why do we need it? Thanks much – qwaserdftyghuijkiii Apr 8 '13 at 21:09
43

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:

  • I don't know what is meant by "registers were at a premium in those days - register based parameters where unconventional" but parameters can also be put on the stack when the # of registers is limited. If you have 35 parameters for example (i believe MIPS has 32 regs.) then you would need to put the final 3 parameters on the stack Also more specific to implementation, the FP would be at the address in memory where the return address is. – Jonathan Mar 9 '15 at 5:29
  • 1
    @Jonathan MIPS Of those 32 registers, $0 was 0. $at was reserved, $v0 and $v1 were for return value of function calls, $a0 - $a3 were function parameters, $t0 - $t9 were temporary (caller preserves), $s0 - $s7 were saved, and $gp, $sp, $fp, and $ra were used by the system itself. All in all, there were only 24 general purpose registers and some of those incurred overhead if you wanted to use. You generally only used the $t registers (10). Compare this with the Itanium which has 128 registers. Calling a function in MIPS with 35 parameters, you would likely put them all on the stack. – user40980 Mar 9 '15 at 13:59
  • @Jonathan I'll also point to this comment: "AMD Bulldozer: 96 physical GPRs, Intel Sandy Bridge: 160 physical GPRs, Intel Haswell: 168 physical GPRs". Things get funny with multiple core machines. SPARC had 160 registers. I also point you to register window that went into some of the design philosophy of MIPS with half as many registers as its contemporaries. – user40980 Mar 9 '15 at 15:37
  • So, this is the same thing as the conventional use of the %ebp ‘base pointer’ register in the x86 calling-convention? (I'm new to all this, but it sounds like the same basic thing as using sth like MOV 8(%ebp), %eax or such?) – ELLIOTTCABLE Jun 6 '16 at 23:25
  • The line Realize that the frame pointer will need to be stored and restored with subroutine calls that modify it. is not quite clear what that means – zadane Mar 27 '17 at 17:25

protected by gnat May 18 at 23:23

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged or ask your own question.