1. Consider the following MIPS loop: Loop: slt $t2, $zero, $t1 beq $t2, $zero, D
ID: 3878500 • Letter: 1
Question
1. Consider the following MIPS loop: Loop: slt $t2, $zero, $t1 beq $t2, $zero, Done addi $t1, $t1, -1 addi $s2, $s2, 2 j Loop Done: a. Assume that the register $t1 is initialized to the value 10. What is the value in register $s2 assuming $s2 is initially zero? b. For the loop above, write the equivalent C code routine. Assume that the registers $s1, $s2, $t1, and $t2 are integers A, B, i, and temp, respectively For the loop written in MIPS assembly above, assume that the register $t1 is nitialized to the value N. How many MIPS instructions are executed? c.Explanation / Answer
Let's see the code line by line
Loop: slt $t2, $zero, $t1 #If $zero(always has zero) is less than value in $t1, then $t2 is set to 1, else, it is set to 0
beq $t2, $zero, $Done #if $t2 is equal to $zero then jump to Done
addi $t1, $t1, -1 #Add -1 to $t1 or Decrement $t1 by 1
addi $s2, $s2, 2 #increment $s2 by 2
j Loop #go to loop again
Done:
a. $t1 = 10 and $s2 = 0
The condition for looping when translated to C is while($t1 >= 0) which means the loop is executed 11 times and $s2 is added by 2, 11 times. So, the final value in $s2 is 22
b.
i = 10;
B = 0;
Loop:
if (0 < i)
temp = 1;
else
temp = 0;
if (temp == 0)
goto Done;
i--;
B +=2;
goto Loop;
Done:
c.
For a value of N, the loop is repeated N+1 times and an additional 2 instructions are repeated to exit the loop. In one loop there are 5 instructions, so the total number of instructions are: 5*(N+1) + 2
Hope this helps. Do Upvote! :)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.