CS2100 Computer Organisation Semester 1 2019/2020: Tutorial 3: Advance MIPS
CS2100 Computer Organisation Semester 1 2019/2020: Tutorial 3: Advance MIPS
Semester 1 2019/2020
Week of 9th September 2019
Tutorial 3: Advance MIPS
1 | P a g e
2. [Code Efficiency] Below is a sample C code fragment to calculate the N = floor(log2M), given
M.
int N = 0, M;
//Code to get M from user not shown
while (M > 1) {
M = M / 2; //Alternative: M = M >> 1;
N++; //i.e. N = N + 1;
}
//N = floor(log2 M)
Ms.Clove argues that the program will execute faster if the statement “M = M / 2;” is
replaced with “M = M >> 1;”. Let’s discuss whether she has a point.
a. Translate “M = M / 2;” into MIPS equivalent, you may need to look up instruction
to do integer division (also discussed in tutorial 2). You can assume the value of M is
in register $t1 initially.
b. Translate “M= M >> 1;” into MIPS equivalent. Similarly, you can assume value of M
is in register $t1 at the start.
c. Do you think Ms.Clove is right? Look for additional information to support your
argument.
2 | P a g e
3. [Memory & Branch] Your friend Alko just learned binary search in CS1020 and could not
wait to impress you. As a friendly gesture, show Alko that you can do the same, but in MIPS!
Complete the following MIPS code. To simplify your tasks, some instructions have already
been written for you, so you only need to fill in the missing parts in [ ]. Please translate as
close as possible to the original code given in the comment column. You can assume
registers $s0 to $s5 are properly initialized before the code below.
Variable Mappings Comments
address of array[] $s0
target $s1 // value to look for in array
low $s2 // lower bound of the subarray
high $s3 // upper bound of the subarray
mid $s4 // middle index of the subarray
ans $s5 // index of the target if found, ‐1 otherwise. Initialized to ‐1.
loop:
slt $t9, $s3, $s2 #while (low <= high) {
bne $t9, $zero, end
add $s4, $s2, $s3
# mid = (low + high)/ 2
[ ]
sll $t0, $s4, 2 # t0 = mid*4
add $t0, $s0, $t0 # t0 = &array[mid] in bytes
[ ] # t1 = array[mid]
slt $t9, $s1, $t1
# if (target < array[mid])
beq $t9, $zero, bigger
addi $s3, $s4, -1
# high = mid – 1
j loopEnd
bigger:
[ ] # else if (target > array[mid])
[ ]
addi $s2, $s4, 1
# low = mid + 1
l j loopEnd
# else {
equal:
ans = mid
add $s5, $s4, $zero
break
[ ]
# }
loopEnd:
#} //end of while-loop
[ ]
end:
3 | P a g e
4. (Memory instruction and HLL) Take a look at the following MIPS program and the memory
content. For simplicity, all values (memory address and content) are in decimal.
#s1 is initialized to 0 Address Content
#t0 is initialized to 112 100 120
104 132
loop: 108 128
beq $t0, $zero, exit 112 108
lw $t1, 0($t0) 116 124
add $s1, $s1, $t1 120 116
lw $t0, 4($t0) 124 104
j loop
128 100
exit:
132 136
136 112
Suppose we execute the code for 3 iterations, answer the following:
a. Give the value of register $s1 at the end of the 3rd iteration.
b. If we want to terminate the loop at the 4th check of "beq", some memory contents need
to be modified. Give the address(es) and the modified content(s) to achieve this. You
should find the minimum changes required.
c. (Challenging) What do you think the code is doing? Give a high level code fragment that
works in similar way.
4 | P a g e