






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!







Rensselaer Polytechnic Institute Spring 2009
We spent some time looking at the MIPS Instruction Set Architecture. We will now consider how to program in MIPS assembly language.
Our arsenal of MIPS assembly instructions now include:
Suppose we want to implement a while loop in MIPS assembly. Consider the high-level language code:
while (a!=b) a += i;
where a is stored in register $s0, b in $s1 and the increment i is in $s2.
Our MIPS assembly code might look like this:
Loop: beq $s0, $s1, EndLoop # if a==b goto EndLoop add $s0, $s0, $s2 # a = a + i j Loop # goto Loop EndLoop: ... code after loop ...
Note that we use a beq instruction here as we want to branch in the case where we no longer wish to execute the contents of the loop.
Now consider a slightly more complicated loop:
while (a[i] == k) i += j;
We need to deal with an array access here. Suppose we have made the following register assign- ments:
The start of array a is in $s3, i is in $s0, k is in $s1, and j is in $s2. All are int values.
One way to implement this code would be:
LoopTop: sll $t0, $s0, 2 # t0 = i * 4 add $t0, $t0, $s3 # t0 = address of a[i] lw $t1, 0($t0) # t1 = contents of a[i] bne $t1, $s1, EndLoop # if a[i] != k, goto EndLoop add $s0, $s0, $s2 # i = i + j j LoopTop # jump back to the top of the loop EndLoop: ... code after loop ...
A few notes:
lw $t1, $t0($s3)
char a[11]; ... put something in a ... for (i=0; i<10; i++) { a[i+1] = a[i]; }
Assuming $s0 contains the address of a, here’s one way to write this:
add $s0, $zero, $zero # i= ForLoop: slti $t1, $s0, 10 # i<10? beq $t1, $zero, LoopEnd # if done, branch out add $t2, $s0, $s1 # t2 gets address of a[i] lb $t3, 0($t2) # t3 gets a[i] addi $t2, $t2, 1 # t2 gets address of a[i+1] sb $t3, 0($t2) # a[i+1] gets t addi $s0, $s0, 1 # i++ j ForLoop # back to check loop condition LoopEnd:
You are all familiar with function/method calls in high-level languages. In assembly language, we usually refer to these as subroutines.
The idea is the same as a function or method call – the program branches from its sequence of instructions to execute a chunk of code elsewhere, then returns to continue where it left off.
We’ll now look at how to write and call subroutines in MIPS assembly.
Recall that there were several registers reserved to help support subroutine calls:
We also have a couple of special jump instructions:
jal address
This instruction puts the address of the next instruction (PC+4) into register $ra, then jumps to the address. This is a J-format instruction, just like the standard jump instruction (j).
jr $a
Jumps to the address specified in the given register. This is an R-format instruction. Assuming the subroutine hasn’t changed the $ra register, this can be used to return from the subroutine.
We said previously that the “s” registers $s0-$s7 are the ones assigned to variables and the “t” registers $t0-$t7 are temporary values.
This becomes important when we start looking at subroutines. The accepted convention for register use by subroutines:
Since subroutines can be called by anyone, we don’t know which “s” registers, if any, are important to the caller. So we have to save these if we use them.
So where do we save values in memory when we need to save them? On the stack.
The stack is a section of memory dedicated to saving registers to manage subroutine calls.
bne $t0, $zero, LoopTop # branch if we didn’t just copy a null
lw $s1, 0($sp) # restore s lw $s2, 4($sp) # restore s addi $sp, $sp, 8 # restore sp jr $ra # return from subroute
Note that we could so something simpler here: save and restore $a0 and $a1 and use those in place of $s1 and $s2 throughout.
We will develop a MIPS assembly subroutine to implement the following C function:
int factorial (int x) { if (x<1) return 1; return x * factorial(x-1);
Since this subroutine calls another subroutine (itself) we need to save $ra and any temp registers we care about before making the recursive call.
We will assume a subroutine multiply exists and we will use that to do our multiplication to get extra practice with subroutines.
Here is some code for this:
factorial:
addi $sp, $sp, -
sw $a0, 4($sp) sw $ra, 0($sp)
slti $t0, $a0, 1 # is x < 1? beq $t0, $zero, L1 # if no, skip over if part
addi $v0, $zero, 1 # return value is 1
addi $sp, $sp, 8 jr $ra # return to caller
L1: addi $a0, $a0, -1 # get x-1 for the parameter jal factorial # recursive call, will put answer in $v
lw $a0, 4($sp) add $a1, $v0, $zero # put factorial(x-1) result into $a jal multiply # multiply $a0*$a1, put result in $v
lw $ra, 0($sp) # restore $ra lw $a0, 4($sp) # restore $a addi $sp, $sp, 8 # restore $sp jr $ra # return to caller
Trace through this with the call factorial(3).
We will use a simulator called SPIM to execute MIPS programs.
Three SPIM versions are available:
We will grade submissions with spim, but you are welcome to use any version you wish when developing MIPS programs.
Some things to notice when stepping through our program:
Another simple example:
See: /cs/terescoj/shared/cs2500/examples/spim-simple/hello.c
A few things to note:
Next, we look at a complete version of the factorial program.
See: /cs/terescoj/shared/cs2500/examples/spim-factorial/factorial.s
We have mentioned the idea of pseudoinstructions a few times. These are “instructions” that exist in MIPS assembly that don’t exist in machine language.
Here are a few common MIPS pseudoinstructions:
slt $at, $a, $b bne $at, L
Note the use of the assembler reserved register $at.