the functions (procedures) in MIPS

76,025

Solution 1

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.

Solution 2

1.the first two are instructions,the third it's kind of special register

  • jal=jump and link (Address of following instruction put in $ra,and jump to target address)
  • jr=jump to specify register
  • $ra=return address

we often use the instruction like this ...

  • jr $ra (Copy $ra to program counter)

it means return(jump) to the address saved in $ra .

2.

Here's an example function (procedure) in C

int main(){
   x=addthem(a,b);
}
int addthem(int a, int b){
   return a+b;
}

function in MIPS

.text
main:    #assume value a is already in $t0, b in $t1
    add $a0,$0,$t0   # it's the same function as move the value
    add $a1,$0,$t1 
    jal addthem      # call procedure
    add $t3,$0,$v0   # move the return value from $v0 to where we want
    syscall

addthem:
    addi $sp,$sp,-4     # Moving Stack pointer
    sw $t0, 0($sp)      # Store previous value

    add $t0,$a0,$a1     # Procedure Body
    add $v0,$0,$t0      # Result

    lw $t0, 0($sp)      # Load previous value
    addi $sp,$sp,4      # Moving Stack pointer 
    jr $ra              # return (Copy $ra to PC)

Solution 3

You will want to read the System V Application Binary Interface, MIPS RISC Processor Supplement. This describes the conventions used for calling functions, in particular how the stack is managed and parameters are exchanged (there is no hardware stack in MIPS, everything is a matter of software conventions, and the ABI defines those conventions).

The document above assumes some basic knowledge of what MIPS instructions do, so you will also need the MIPS32 Architecture for Programmers, in particular volume II (instruction set), which describes the detailed effect of each instruction. But, do yourself a favor, download and read volume I (introduction) first.

The jal instruction is the "jump and link" opcode. It jumps at the target address (which is the address of the first opcode of the called procedure) while saving the current instruction pointer into the link register, which is register 31 (to be precise, it saves in register 31 the value x+8, where x is the address of the jal opcode itself).

Share:
76,025
Sneimeh
Author by

Sneimeh

There are only 10 types of people in the world: those who understand binary, and those who don't. A programmer is a person who fixed a problem you didn't know you had, in a way you don't understand. Studying Commpunication and Information Engineering at Hochschule Rhein-Waal (HSRW)

Updated on July 09, 2022

Comments

  • Sneimeh
    Sneimeh almost 2 years

    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)?