0
votes

Extremely new to MIPs assembly language -- I haven't been able to find a link that specifically tells you what a function is distinguished as in MIPS, examples of how we use jal, jr, etc..

It's very difficult to find out how these actions work -- My text book for this section of the course is very broad and assumes I already know the language and just outlines a code (under the assumption that I know exactly what the code means)

Hence, the book isn't helping me so I'm trying to practice on my own..

Anywho, in my class we wrote a simple array code which supposedly has a "function" in it.

My question is -- how do I know what is the "function" Below is a code we did in class -- it's supposed to output an array from 1 to 10.. I understand where it increments and such (addi) and why it's a 1 and 4 but I don't understand what $ra, jal , jr mean... but I'm assuming that these have something to do with what a "function" is

#Load an array and display

    .data

    .align 2
a1:     .space 40

    .text
    .globl main
main:
    la $s0, a1
    jal loadArray
    la $s0, a1# <- return here after function return
    jal displayArray
    # <- return here after 2nd function
exit:
    li $v0, 10
    syscall

displayArray:
    #$ra = the address of line 14
    li $t0, 1   #int i = 0
    displayLoop: blt $t0, 11, display
        #else
        jr $ra
    display:
        lw $t0, ($s0)   # a1[i] = i;

    li $v0, 1
    move $a0, $t0
    syscall

    #incremment i and array pointer
    addi $t0, $t0, 1
    addi $s0, $s0, 4

    j displayLoop

loadArray:
    #$ra = the address of line 14
    li $t0, 1   #int i = 0
    loadLoop: blt $t0, 11, load
        #else
        jr $ra
    load:
        sw $t0, ($s0)   # a1[i] = i;

        #incremment i and array pointer
        addi $t0, $t0, 1
        addi $s0, $s0, 4

        j loadLoop

So this is the sample code but the only difference between this code and a usual code with just labels is the jump register and jump and link... Does that distinguish what is a function???

Sorry for the long wall of text -- TL;DR In MIPS, how do you know what is a "function" I know what a label is (obv.) but not a function...

This isn't as straight forward to me as it is in cpp or java lol.

1

1 Answers

2
votes

A function (or subroutine) is really just any piece of code that you can call (with jal) and expect to return to you (with jr $ra). It is also generally expected to keep your stack and registers intact across the call.

The term function is used more often by high-level languages like C, whereas subroutine is more common at the assembly-language level.

jal means "Jump And Link" - I would expect this to be covered in your textbook. This instruction 1) stores the return address (the address of the instruction after the jal) in $ra, the link register (or return address register) and then 2) jumps to the target address. This is how you call a function.

When the function is finished, it returns to the caller by jumping to the address in the return address register, with the jr $ra instruction (Jump to Register $ra).

Your comments imply that you already understand this:

jal loadArray
la $s0, a1# <- return here after function return
jal displayArray
# <- return here after 2nd function

So loadArray can be considered a function, as can displayArray.

Read more: