the functions (procedures) in MIPS
Asked Answered
L

4

20

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)?
Loree answered 17/11, 2010 at 16:1 Comment(1)
Is this a homework problem? The wikipedia article (en.wikipedia.org/wiki/MIPS_architecture#MIPS_assembly_language )explains the "jal" and "jr" instructions, perhaps it answers your question.Doran
R
24

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.

Reduce answered 18/11, 2010 at 11:27 Comment(1)
It looks like the reference sheet is no longer availableGlyptodont
B
11

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)
Bryantbryanty answered 6/4, 2011 at 8:36 Comment(0)
A
2

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

Airfield answered 18/11, 2010 at 13:20 Comment(0)
S
0
  • jal: aka jump and link against any function name will redirect you to the required function.
  • jr $ra: It returns the value from a function that was called.

main function:

   .data

            x: .word 3                        # initializing x to be 3
            y: .word 5                        # initializing y to be 5
            answer: .word 0                   # intialzing answer to be 0
            prompt: .asciiz "Add X to Y: "    # console prompt for final answer

   .text

   .globl main

   .ent main

    main:

             lw $a0, x                         # pass arguments to function $a0 - x, $a1 - y
             lw $a1, y
             jal adds                          # adds function is called
             sw $v0, answer                    # answer from the returned value

             la $a0, prompt                    # displaying prompt on the console
             li $v0, 4
             syscall

             lw $a0, answer                    # displaying final answer on the console
             li $v0, 1
             syscall

             li $v0, 10                        # end program
             syscall

    .end main

adds function:

    .globl adds

    .ent adds

    adds:                                      # adds function

              li $v0, 0                                          
              add $v0, $a0, $a1                # adding arguments to the callee
              jr $ra                           # return

   .end adds
Stoner answered 27/3, 2021 at 11:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.