MidTerm Assignment
MidTerm Assignment
Consider the following MIPS loop for the following tree questions (0, 1, 2):
LOOP: slt $t2, $0, $t1
beq $t2, $0, DONE
addi $t1, $t1, -1
addi $s2, $s2, 2
j LOOP
DONE:
0. Assume that the register $t1 is initialized to the value 10. What is the value in register $s2 assuming
$s2 is initially zero?
1. For each of the loops above, write the equivalent C code routine. Assume that the registers $s1, $s2,
$t1, and $t2 are integers A, B, i, and temp, respectively.
2. For the loops written in MIPS assembly above, assume that the register $t1 is initialized to the value
N. How many MIPS instructions are executed?
3. Translate the following C code to MIPS assembly code. Use a minimum number of instructions.
Assume that the values of a, b, i, and j are in registers $s0, $s1, $t0, and $t1, respectively. Also, assume
that register $s2 holds the base address of the array D.
4. Translate the following loop into C. Assume that the C-level integer i is held in register $t1, $s2 holds
the C-level integer called result, and $s0 holds the base address of the integer emArray.
addi $t1, $0, $0
LOOP: lw $s1, 0($s0)
add $s2, $s2, $s1
addi $s0, $s0, 4
addi $t1, $t1, 1
slti $t2, $t1, 100
bne $t2, $s0, LOOP
5. Implement the following C code in MIPS assembly. What is thetotal number of MIPS instructions
needed to execute the function?
int fib(int n){
if (n==0)
return 0;
else if (n == 1)
return 1;
else
return fib(n-1) + fib(n-2);
6. Assume $t0 holds the value 0x00101000. What is the value of $t2 after the following instructions?
slt $t2, $0, $t0
bne $t2, $0, ELSE
j DONE
ELSE: addi $t2, $t2, 2
DONE:
9. For the register values shown above, what is the value of $t2 for the following sequence of
instructions?