CPSC220: Introduction to Computer Architecture (Fall 2013)

Lab #2

Due at the start of class on Friday, 09/26/2013

Summary

This assignment focuses on basic MIPS assembly language programming. You should use the MARS simulator to code and test your work.

Reading

Study

Begin by familirizing yourself with the MARS simulator (and, of course, MIPS, since this is likely your first experience with it). Following are some example programs and a corresponding version in MIPS. Copy each MIPS program to a new file (the names should end in the extension ".asm". Assemble each one, and run them (preferably using the "step" button).

Example of an if/else :

int x = 5;
int y = 7;
int z = 0;
if (x < y) 
   z = x;
else
   z = y;

MIPS version:

    .globl main
    .globl true
    .globl done
.data
x:    .word 5
y:    .word 7
z:    .word
.text
    
main:    
    lw   $s0, x
    lw   $s1, y
    
    slt  $t0, $s0, $s1    #    $t0 = 1  if  $s0 < $s1
    bne $t0, $zero, true        
    
    sw   $s1, z
    j done
true:    
    sw   $s0, z
done:        
    li   $v0, 10
    syscall

Loop Example

int y = 125;
int x = 0;
while (y > 0) {
   x = x + 1;
   y = y / 2;
}

MIPS version

    .globl main
    .globl loop
    .globl end
.data
x:    .word 0
y:    .word 125
.text
    
main:    
    lw $t0, x
    lw $t1, y
    bltz $t1, end
loop:    
    div $t1, $t1, 2
    add $t0, $t0, 1
    bgtz $t1, loop
end:    
    
    li   $v0, 10
    syscall

Array example

int[] a = {0,0,0,0,0,0,0,0,0,0,0};
int i = 0;

while (i < a.length) {
  a[i] = i;
  i = i + 1;
}

MIPS version:

    .globl main
    .globl loop
    .globl end
.data
a:    .word 0 : 11
i:    .word 0
.text
    
main:    
    la $s0, a
    lw $s1, i
loop:    
    li $t0, 11         # a.length: there are other ways to do this;  Java stores
                       # this value as an extra element in the array itself
    bge $s1, $t0, end
    sll $t1, $s1, 2    # calculate (i*4) (the 4 comes from the fact that a is
                       # an array of 32-bit (i.e. 4 byte) words
    add $t1, $s0, $t1  # calculate location of a[i]
    
    sw $s1, 0($t1)     # a[i] = $s1 (i.e. a[i] = i)
    addi $s1, $s1, 1   # i = i + 1, but we don't need to write to memory yet
    j loop
end:    
    # If i was in scope outside the loop, we'd need to write the value of $s1
    # at this point.
    
    li   $v0, 10
    syscall


Your Job

Translate each of the following code fragments into equivalent MIPS assembly programs. For now, we'll ignore problems of input/output and system calls: just test your programs by hard-coding values in the initial memory setup. That, of course, requires some careful though on how to setup appropriate memory and registers in the first place!

  1. int y = 2012;
    int m = 2;
    int d = 29;
    
    int v = 0;
    
    if (y % 4 == 0 && (y%100 != 0 || y%400 == 0)) {
        v = 1;
    }
    
  2. int n = 17;
    int i = 0;
    
    while (n != 1) {
      if (n % 2 == 0) { 
        n = n / 2;      
      } else {
        n = 3 * n + 1;
      }
      i = i + 1;
    }
    
  3. int[] A = { 35, 2, 3 -2, -17, 8, 19, 23, 2 };
    int loc = -1;
    int n = -2;
    
    for (int i = 0; i < A.length; i ++){
      if (A[i] == n){
        loc = i;
        break;
      }
    }
    
  4. int[] A = { 35, 2, 3 -2, -17, 8, 19, 23, 2 };
    
    int i = 0;
    while (i < A.length){
      for (int j =0; j < A.length -1; j++){
        if (A[j] > A[j+1]){
          int k = A[j];
          A[j] = A[j+1];
          A[j+1] = k;
        }
      }
      i++;
    }
    

Turn in:

To hand in your files:

Copy your entire hw2 directory into your handin directory.