We're studying the MIPS assembler (I guess this question can apply to assembly in general though), and the teacher introduced us to the frame pointer.
If I have a function prologue, I used to do directly the stack pointer:
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
And in the function epilogue:
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
The teacher said that using frame pointer is useful for us humans when we write functions in assembly:
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
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.
Yes, but will I ever need to use the activation within the function, since it just contains the saved data of the caller function?
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?