Docsity

MIPS Assembly Programming: Instructions and Control Structures - Prof. James D. Teresco, Study notes of Computer Architecture and Organization

Toggle sidebar
bg1
Computer Science 2500
Computer Organization
Rensselaer Polytechnic Institute
Spring 2009
Topic Notes: MIPS Programming
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:
•add $a, $b, $c
•sub $a, $b, $c
•addi $a, $b, n
•lw $a, n($b)
•sw $a, n($b)
•sll $a, $b, n
•srl $a, $b, n
•and $a, $b, $c
•andi $a, $b, n
•or $a, $b, $c
•ori $a, $b, n
•nor $a, $b, $c
•beq $a, $b, L
•bne $a, $b, L
•j L
Control Structures in MIPS Assembly
Suppose we want to implement a while loop in MIPS assembly. Consider the high-level language
code:
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download MIPS Assembly Programming: Instructions and Control Structures - Prof. James D. Teresco and more Study notes Computer Architecture and Organization in PDF only on Docsity!

Computer Science 2500

Computer Organization

Rensselaer Polytechnic Institute Spring 2009

Topic Notes: MIPS Programming

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:

  • add $a, $b, $c
  • sub $a, $b, $c
  • addi $a, $b, n
  • lw $a, n($b)
  • sw $a, n($b)
  • sll $a, $b, n
  • srl $a, $b, n
  • and $a, $b, $c
  • andi $a, $b, n
  • or $a, $b, $c
  • ori $a, $b, n
  • nor $a, $b, $c
  • beq $a, $b, L
  • bne $a, $b, L
  • j L

Control Structures in MIPS Assembly

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:

  • We need to multiply i by 4 to get the correct offset, since we’re assuming a is an array of word-sized values.
  • We might be tempted to write

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:

MIPS Subroutines and Programs

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.

Special Registers and Instructions

Recall that there were several registers reserved to help support subroutine calls:

  • $a0-$a4: argument registers – a place for the caller to place values to send to the subroutine.
  • $v0, $v1: return value registers – a place for subroutines to return values to the caller.
  • $ra: return address register – where to resume executing the caller when the subroutine is finished.

We also have a couple of special jump instructions:

  • Jump and Link :

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

  • Jump to Register :

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.

Registers and the Stack

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:

  • $t0-$t7 are always available for use by a subroutine - if a subroutine calls another subroutine, it must assume that the called subroutine will modify $t0-$t7. - if this is a problem for the calling subroutine, it should save any values it has in $t0- $t7 to memory and restore them after the subroutine call.
  • $s0-$s7 should be unchanged by a subroutine call - if a subroutine calls another subroutine, it can expect its values in $s0-$s7 to remain upon return. - if the called subroutine wishes to make use of $s0-$s7, it should save the caller’s values in any of these registers it will use in memory, then restore them before return.

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.

A Recursive Example

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:

make space for 2 words on the stack

addi $sp, $sp, -

save $ra and $a0 on the stack

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

x >= 1, just return 1

addi $v0, $zero, 1 # return value is 1

we could restore $a0 and $ra but we know they haven’t

changed when we take this quick exit, so let’s not

but we still need to put $sp back the way we found it

addi $sp, $sp, 8 jr $ra # return to caller

here, x>=1 so we need to make a recursive call

L1: addi $a0, $a0, -1 # get x-1 for the parameter jal factorial # recursive call, will put answer in $v

We now want to set up for the multiply, but we destroyed $a

above, but have it on the stack, so let’s load it

lw $a0, 4($sp) add $a1, $v0, $zero # put factorial(x-1) result into $a jal multiply # multiply $a0*$a1, put result in $v

$v0 is where we want our answer, so no work there

but multiply could have changed $a0 and did change $ra

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

Complete Programs and SPIM

We will use a simulator called SPIM to execute MIPS programs.

  • SPIM reads MIPS assembly language programs
  • SPIM simulates the execution of each instruction
  • SPIM displays values of registers and memory
  • SPIM allows breakpoints and step-by-step execution
  • SPIM provides primitive I/O to allow interactive processing

Three SPIM versions are available:

  • spim – the command-line version
  • xspim – the X11 version (Macs, Unix workstations)
  • PCspim – Windows version

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:

  • Execution starts at 0x00400000 by default, which is the code that sets up the call to main.
  • After a few steps, main gets invoked with a jal call.
  • When we get to main, we find that we are executing a lui instruction rather than the expected lw. Why? - The lw instruction requires a base register and offset: lw $t0, offset($base) - The assembler allows us to write lw $t0, num1 (which constitues a different ad- dressing mode) and inserts appropriate instructions to perform a load from a labelled memory location. - It computes the appropriate address, loads it into the reserved assembler register $at, then issues a lw instruction in the machine’s memory addressing mode.

Another simple example:

See: /cs/terescoj/shared/cs2500/examples/spim-simple/hello.c

A few things to note:

  • We can define a string constant as part of our data segment with an .asciiz directive.
  • The la is a pseudoinstruction that directs the assembler to load the given register with the address of the given label.
  • A syscall with $v0 set to 4 is the print string operation, and will print the null- terminated string starting at the address in $a0.

Next, we look at a complete version of the factorial program.

See: /cs/terescoj/shared/cs2500/examples/spim-factorial/factorial.s

  • The factorial subroutine is identical to what we looked at last time.
  • We fill in the missing multiply routine with the one you did for the lecture assignment, changed to use $a0 and $a1 as the parameters and to put the answer into $v0.
  • The main subroutine will use two “s” registers and will call subroutines, so we begin by pushing $s0, $s1, and $ra onto the stack. Note that SPIM initialized $sp for us appro- priately.
  • We print a prompt string with the print string syscall and then read in an int from the keyboard with the read int syscall. Note: a complete list of syscall codes is in Figure B.9.1.
  • After the call to factorial, we use a series of syscalls to print the answer.
  • Finally, we pop the stack and return from main.

MIPS Pseudoinstructions

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.

  • The pseudoinstructions map to one or more MIPS machine instructions.
  • These exist for convenience of the programmer (human or compiler).

Here are a few common MIPS pseudoinstructions:

  • move $a $b – move (copy) contents of register $b to register $a. This is assembled into add $s0, $s1, $zero.
  • blt $a, $b, L – branch on less than – if $a is less than $b, branch to label L. This is assembled into two machine instructions:

slt $at, $a, $b bne $at, L

Note the use of the assembler reserved register $at.

  • li – load immediate
  • la – load address
  • sgt, sle, sge – set on ...
  • bge, bgt, ble, blt – conditional branches