MIPS program jr $ra instructions and stack handling

user2206366 picture user2206366 · Sep 23, 2015 · Viewed 18.7k times · Source

I am new to MIPS programming and have been struggling to understand MIPS program and how does it flow. Can anyone please help me out to understand it. Below is the code. My doubt is in RTN function where $ra is returned where the execution supposed to return after jr $ra and what will be stored after sw $ra, 8($sp) to stack because when this statement first executes what will be the value of $ra ? Is it some garbage value or we need to assume some value in order to understand the program?

f: addi $sp, $sp, -12
sw $ra, 8($sp)
sw $s0, 4($sp)
sw $a0, 0($sp)
bgt $a0, $0, L1
add $v0, $0, $0
j RTN
L1: addi $t0, $0, 1
bne $t0, $a0, L2
add $v0, $0, $t0
j RTN
L2: subi $a0, $a0,1
jal f
add $s0, $v0, $0
sub $a0, $a0,1
jal f
add $v0, $v0, $s0
RTN: lw $a0, 0($sp)
lw $s0, 4($sp)
lw $ra, 8($sp)
addi $sp, $sp, 12
jr $ra

Answer

Peter Cordes picture Peter Cordes · Oct 15, 2019

On function entry, ra holds the return address where our caller wants us to jump when we're done.

The function preamble saves it to the stack because the function body uses jal to make function calls. jal overwrites ra so we need to save/restore our own return address around that.

f: addi $sp, $sp, -12    ; adjust stack pointer to accommodate stack frame (subtract 12 bytes)
sw $ra, 8($sp)           ; save a copy of our return address
...

When the function is complete we can restore the things we saved, then use the standard jr $ra to return, setting PC = $ra.

...
lw $ra, 8($sp)           ; restore saved return address
addi $sp, $sp, 12        ; restore stack pointer (dispose of stack frame)
jr $ra                   ; jump to return address

(Some phrasing and code formatting copied from @PaulR's answer which seems to explain it incorrectly, so I felt all its text needed a rewrite outside of the code blocks.)