in problem 2.26 2.26 Consider the following MIPS loop: LOOP: s1t $t2, $0. $t1 be
ID: 3588606 • Letter: I
Question
in problem 2.26 2.26 Consider the following MIPS loop: LOOP: s1t $t2, $0. $t1 beq t2. $0 DONE subi $t1. $t1. 1 addi $s2, $s2. 2 j L00P 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
1) The value is being incremented by 2 every time an iteration takes place like the following:
LOOP: slt $t2, $0, $t1 # 0 < 10: here, $t2 = 1
beq $t2, $0, DONE # 1 != 0: go to next instruction
sub $t1, $t1, 1 # $t1 = 10 – 1 = 9
add $s2, $s2, 2 # $s2 = 0 + 2 = 2
j LOOP # iteration again
DONE:
LOOP: slt $t2, $0, $t1 # 0 < 9: here, $t2 = 1
beq $t2, $0, DONE # 1 != 0: go to next instruction
sub $t1, $t1, 1 # $t1 = 9 – 1 = 8
add $s2, $s2, 2 # $s2 = 2 + 2 = 4
j LOOP # iteration again
DONE:
LOOP: slt $t2, $0, $t1 # 0 < 8: here, $t2 = 1
beq $t2, $0, DONE # 1 != 0: go to next instruction
sub $t1, $t1, 1 # $t1 = 8 – 1 = 7
add $s2, $s2, 2 # $s2 = 4 + 2 = 6
j LOOP # iteration again
DONE:
LOOP: slt $t2, $0, $t1 # 0 < 7: here, $t2 = 1
beq $t2, $0, DONE # 1 != 0: go to next instruction
sub $t1, $t1, 1 # $t1 = 7 – 1 = 6
add $s2, $s2, 2 # $s2 = 6 + 2 = 8
j LOOP # iteration again
DONE:
LOOP: slt $t2, $0, $t1 # 0 < 6: here, $t2 = 1
beq $t2, $0, DONE # 1 != 0: go to next instruction
sub $t1, $t1, 1 # $t1 = 6 – 1 = 5
add $s2, $s2, 2 # $s2 = 8 + 2 = 10
j LOOP # iteration again
DONE:
LOOP: slt $t2, $0, $t1 # 0 < 5: here, $t2 = 1
beq $t2, $0, DONE # 1 != 0: go to next instruction
sub $t1, $t1, 1 # $t1 = 5 – 1 = 4
add $s2, $s2, 2 # $s2 = 10 + 2 = 12
j LOOP # iteration again
DONE:
LOOP: slt $t2, $0, $t1 # 0 < 4: here, $t2 = 1
beq $t2, $0, DONE # 1 != 0: go to next instruction
sub $t1, $t1, 1 # $t1 = 5 – 1 = 4
add $s2, $s2, 2 # $s2 = 12 + 2 = 14
j LOOP # iteration again
DONE:
LOOP: slt $t2, $0, $t1 # 0 < 3: here, $t2 = 1
beq $t2, $0, DONE # 1 != 0: go to next instruction
sub $t1, $t1, 1 # $t1 = 4 – 1 = 3
add $s2, $s2, 2 # $s2 = 14 + 2 = 16
j LOOP # iteration again
DONE:
LOOP: slt $t2, $0, $t1 # 0 < 2: here, $t2 = 1
beq $t2, $0, DONE # 1 != 0: go to next instruction
sub $t1, $t1, 1 # $t1 = 3 – 1 = 2
add $s2, $s2, 2 # $s2 = 16 + 2 = 18
j LOOP # iteration again
DONE:
LOOP: slt $t2, $0, $t1 # 0 < 1: here, $t2 = 1
beq $t2, $0, DONE # 1 != 0: go to next instruction
sub $t1, $t1, 1 # $t1 = 2 – 1 = 1
add $s2, $s2, 2 # $s2 = 18 + 2 = 20
j LOOP # iteration again
DONE:
So, 20 is the answer.
2) while(i > 0) {
i = 1 - 1 // Decrement i by one during each iteration of the loop (sub $t1, $t1, 1)
B =B + 2 //Increment B by 2 each time(addi $s2, $s2, 2)
} // exit loop
3) For any value of N > 0, (N full Loops + 2 lines ) of MIPS instructions must be executed: each full loop contains 5 MIPS instructions. Therefore, if the register $t1 is initialized to the value N, atoatl of (5 * N) + 2 MIPS instructions are executed.
THANK YOU
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.