the functions (procedures) in MIPS

Sneimeh picture Sneimeh · Nov 17, 2010 · Viewed 67.3k times · Source

I'm new in MIPS language and I don't understand how the functions (procedures) in the MIPS assembly language work. Here are but I will specify my problem :

  1. What does:

    • jal
    • jr
    • $ra

    mean in mips language and the important thing

  2. How can we use them when we want to create a function or (procedure)?

Answer

Mihai Scurtu picture Mihai Scurtu · Nov 18, 2010

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.