the functions (procedures) in MIPS

Firstly, you might want to check this quick MIPS reference. It really helped me.

Secondly, to explain jal, jr and $ra. What jal <label> does is jump to the label label and store the program counter (think of it as the address of the current instruction) in the $ra register. Now, when you want to return from label to where you initially were, you just use jr $ra.

Here’s an example:

.text
main:
li $t0, 1
jal procedure # call procedure
li $v0, 10
syscall

procedure:
li $t0, 3
jr $ra # return

You will notice when running this in a SPIM emulator that the value left in $t0 is 3, the one loaded in the so-called procedure.

Hope this helps.

Leave a Comment