2.26 Consider the following MIPS loop: LOOP: slt $t2, $0, $t1 beq $t2, $0, DONE
ID: 3738007 • Letter: 2
Question
2.26 Consider the following MIPS loop: LOOP: slt $t2, $0, $t1 beq $t2, $0, DONE subi $t1, $t1. 1 addi $s2, $s2, 2 LOOP DONE: 2.26.1 [5] Assume that the register $t1 is initialized to the value 10. What is the value in register $s2 assuming $s2 is initially zero? 2.26.2 [5] 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.26.3 [5] 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?Explanation / Answer
2.26.1)
given :
$t1 = 10
$s2 = 0
Require :
$s2 = ?
Answer :-
slt $t2,$0,$t1 -> $t2 = 1 (if $0 < $t1 i.e 0 < 10)= true
beq $t0,$0,DONE -> false
subi $t1,$t1,1 -> $t1 = 9
addi $s2,$s2,2 -> $s2 = 0 + 2 = 2
So, here ,for every loop executed ; $t1 value is decremented by 1 and $s2 value is incremented by 2.And,this happens untill $t1 = 0
So,at the end of 10 loops (as $t1 = 0 );$s2 will be incremented by 2 for 10 times.Therefore, $s2 = 20
2.26.2)
Equivalent C code:-
while(1){
if(i>0){
temp=1;
}
else{
temp = 0;
}
if(temp == 0){
break;
}
i=i-1;
B=B+2;
}
2.26.3)
If we assume $t1 = N;
then for a successful loop ; no. of instructions executed = 5
And,for unsuccessful loop ; no. of instructions executed = 2
So,for $t1 = N ;
no of successful loops = N
no of unsuccesfful loops = 1
Therefore,
no of instructions executed = 5*N + 2*1 = (5*N) + 2
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.