Re: frame pointer



Christian Christmann wrote:
Hi,

I'm learning MIPS assembly programming. In couple of examples
I went through, the authors save the "frame pointer" with

sw $fp 8($sp) # sp is stack pointer

onto the stack.
However, after returning from a routine accessed by a jump instruction
(like JAL label, "JAL" = jump&link return address), they restore the
stored values using $fp like

lw $ra -4($fp).

What is the general purpose of a frame pointer and why is in the
aforementioned examples not the stack pointer "$sp" used for
saving and restoring context values?

If you use $sp for addressing inside the stack frame, it's like trying
to hit a moving target. That makes it very difficult to write reliable
(and maintainable!) code. The frame pointer saves $sp at a predictable
point so that you have a predictable base to work with while still
allowing $sp to change as necessary.

As for why $fp is used instead of $sp to load the return address,
that's entirely up to the author of the code. If it's more convenient
to use $fp, all the better.

.



Relevant Pages

  • Re: stack pointer and frame pointer
    ... code break wrote: ... A stack pointer is an index into a given stack frame. ... A frame pointer represents a boundary of a stack frame. ... This is architecture stuff, not C stuff. ...
    (comp.lang.c)
  • frame pointer
    ... I'm learning MIPS assembly programming. ... What is the general purpose of a frame pointer and why is in the ... aforementioned examples not the stack pointer "$sp" used for ... saving and restoring context values? ...
    (alt.lang.asm)
  • Re: should program call stack grow upward or downwards?
    ... registers, such as the 88000, then growing upwards is more expensive ... than growing downwards. ... offsets relative to a frame pointer, but since the stack pointer ...
    (comp.arch)
  • trap number 12 :-(
    ... stack pointer = 0x10:0cxcdb0c918 ... frame pointer = 0x10:0cxdb0c91c ... current proces = ll (idle) ...
    (freebsd-newbies)