title: What are the advantages of a frame pointer?
body: <p>We're studying the MIPS assembler (I guess this question can apply to assembly in general though), and the teacher introduced us to the <strong>frame pointer</strong>.</p>
  
  <p>If I have a function prologue, I used to do directly the <strong>stack pointer</strong>:</p>
  
  <pre><code>addiu $sp, $sp, -8   ; alloc 2 words in the stack
  sw $s0, 4($sp)       ; save caller function $s0 value in the stack
  sw $ra, ($sp)        ; save the return address for the callee function
  </code></pre>
  
  <p>And in the function epilogue:</p>
  
  <pre><code>move $v0, $0         ; set 0 as return value
  lw $s0, 4($sp)       ; pick up caller $s0 value from the stack
  lw $ra, ($sp)        ; pick up return address to return to the caller
  addiu $sp, $sp, 8    ; dealloc the stack words I used
  jr $ra               ; return back to caller
  </code></pre>
  
  <p>The teacher said that using frame pointer is useful for us humans when we write functions in assembly:</p>
  
  <pre><code>addiu $sp, $sp, -12  ; alloc 3 words in the stack
  sw $fp, 8($sp)       ; save caller frame pointer in the stack
  addiu $fp, $sp, 8    ; set $fp to the uppermost address of the activation frame
  sw $ra, -4($fp)      ; saving like the first example, but relative 
  sw $s0, -8($fp)      ; to the frame pointer
  </code></pre>
  
  <p>The teacher also said that sometimes the stack pointer goes on allocating other space and refering to the activation frame within the function is harder since we need to pay attention.
  Using frame pointer we'll have a static pointer to the activation frame.</p>
  
  <p>Yes, but will I <strong>ever</strong> need to use the activation within the function, since it just contains the saved data of the caller function?</p>
  
  <p>I think it makes just things harder to implement. Is there a real practical example where the frame pointer is a great advantage to the programmer?</p>