Consider the following MIPS code: LOOP: slt $t2, $zero, $t1 beq $t2, $zero, DONE
ID: 3878749 • Letter: C
Question
Consider the following MIPS code: LOOP: slt $t2, $zero, $t1 beq $t2, $zero, DONE subi $t1, $t1, 1 addi $s2, $s2, 3 j LOOP DONE: a) Assume that register $t1 is initialized to the value 10 (in base 10), and $s2 is initialized to zero. What is the value of $s2 after executing the code segment above? b) Convert the MIPS instructions above to C code. Assume that registers $s2 and $t1 contain the variables B and i, respectively. c) How many MIPS instructions would be executed if the register $t1 was initialized to the value N?Explanation / Answer
4.a.)
Value of $t1=10
Value of $s2=0
slt : set less than i.e. if i>0 then t2=1 else t2=0
#first pass of loop:
t1=10
10 is greater than zero hence t2=1
since t2!=0
t1=t1-1 and s2=s2+3 >>>> t1=9, s2=0+3=3
##jump back to loop
#second pass of loop:
9>0 therefore t2=1
since t2!=0
t1=8, s2=6
##jump back to loop
#third pass of loop:
8>0 therefore t2=1
since t2!=0
t1=7, s2=9
##jump back to loop
#fourth pass of loop:
7>0 therefore t2=1
since t2!=0
t1=6, s2=12
##jump back to loop
#fifth pass of loop:
6>0 therefore t2=1
since t2!=0
t1=5, s2=15
##jump back to loop
#sixth pass of loop:
5>0 therefore t2=1
since t2!=0
t1=4, s2=18
##jump back to loop
#seventh pass of loop:
4>0 therefore t2=1
since t2!=0
t1=3, s2=21
##jump back to loop
#eighth pass of loop:
3>0 therefore t2=1
since t2!=0
t1=2, s2=24
##jump back to loop
#ninth pass of loop:
2>0 therefore t2=1
since t2!=0
t1=1, s2=27
##jump back to loop
#tenth pass of loop:
1>0 therefore t2=1
Since t2!=0
t1=0, s2=30
##jump back to loop
#eleventh pass of loop:
t1 is not greater than zero therefore t2=0
Condition t2==0 is true and control branches to label DONE and loop terminates
Therefore value of $s2 after executuing the code segment is 30………………..answer
b.)
C CODE:
int i=10,B=0;
while(i>0)
{
i--;
B+=3;
}
c.)
For one pass of loop 5 MIPS instructions are executed.
While(t2!=0) i.e., while the condition is true and control does not branch to DONE label , all these 5 instructions will be executed.
When t2 becomes zero, i.e., when i=0 then first two MIPS instructions are executed.
For $t1=N
$t1 becomes zero in Nth pass of loop i.e., N*5 instructions executed
In (N+1)th pass, $t1=0 and therefore $t2=0,
Condition false and loop terminates>>>>two instructions executed in this pass.
TOTAL INSTRUCTIONS: 5*N+2 ==>> 5N+2…………answer
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.