HW 6 - MIPS Assembly
Due at 10:30pm on Friday, October 16, 2015

Update: This due date for this assignment was extended to October 18th

Collaboration
This assignment must be completed individually.
Presentation
Number problems in order and present code in a fixed-width font including comments that make each instruction’s purpose clear.
Submission
Send your answers to me in an email with the subject [CSC 211.01] Assignment 6.

Problem 1

Show the machine code (in binary and hex) for the following MIPS code segment.

lw $s0, 2($s7)
andi $t0, $s0, 0x8
srl $t0, $t0, 3

Problem 2

Translate the following C code into MIPS assembly. Assume that values of the variables a, b, c, and d are held in registers $s0, $s1, $s2, and $s3, respectively.

if (a == b) {
  d = c;
} else if (a == c) {
  d = b;
} else {
  d = a;
}

Problem 3

Translate the following C code into MIPS assembly. Assume that values of the integer variables n, i, and sum are held in registers $s0, $s1, and $s2, respectively.

sum = 0;
for (i=1; i < n; i++) {
  sum += i;
}

Problem 4

Translate the following C code into MIPS assembly. Assume that the base address of array A is held in register $s7 and that values for the variables n, i, and sum are held in registers $s0, $s1 and $s2.

sum = 0;
for (i=0; i < n; i++) {
  sum += A[i];
}

Problem 5

Translate the following C function into MIPS assembly.

int arraysum(int array[], int size) { 
  int i;
  int sum = 0;
  for (i=0; i < size; i++) {
    sum += array[i];
  }
  return sum;
}

Problem 6

Translate the following C function call into assembly. Assume $s5 contains the address of the array numbers, $s6 contains the value n, and the value total will be stored in $s7.

total = arraysum(numbers, n);

Problem 7

Translate the following recursive C function into MIPS assembly.

int termial(int n) {
  if (n < 1) {
    return 0;
  } else {
    return n + termial(n - 1);
  }
}

Acknowledgements

This derivative work of Janet Davis and Marge Coahran, used under the Creative Commons Attribution-Noncommercial-Share Alike 3.0 United States License was developed in collaboration with Jerod Weinman.