Question: Hey! I found this C++ code and MIPS translation on a computer science website while learning MIPS, and I had a few questions. C++ while
Hey! I found this C++ code and MIPS translation on a computer science website while learning MIPS, and I had a few questions.
C++
while (a[i] == k) i++;
MIPS
Loop:
sll $t1, $s3, 2 # $t1 = 4i
add $t1, $t1, $s6 # $t1 = address of a[i] lw $t0, 0($t1) # $t0 = a[i] bne $t0, $s5, Exit # exit loop if save[i] != k addi $s3, $s3, 1 # i++ j Loop
Exit: ...
Part A) If we assume that the first value where a[i] != k is when i = 10, what is the number of MIPS instructions executed by the loop if i starts at 0?
Part B) As shown in the above code, two branch/jump instructions are expected for each loop iteration (except the last one). I don't believe this procedure is rather convenient and optimal. Could you rewrite the above code so that only one branch/jump instruction is executed for each loop (I'm thinking this may involve repeating some of the code outside of the loop)? After making this adjustment, what are the total number of instructions executed now using the conditions in Part A)?
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
