Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1) Consider the following MIPS loop: Loop: slt $t2, $0, $t1 beq $t2, $0, Done su

ID: 3903820 • Letter: 1

Question

1) Consider the following MIPS loop:

            Loop:   slt        $t2, $0, $t1

                        beq      $t2, $0, Done

                        subi      $t1, $t1, 1

                        addi     $s2, $s2, 4

                        j           Loop

            Done:

A)   Assume that the register $t1 is initialized to the value 5. What is the value in register $s2 assuming $s2 is initially zero?

B)   For each of the loops above, write the equivalent C code routine. Assume that the register $s1, $s2, $t1, and $t2 are integers a, b, i, and temp, respectively.

C)   For the loops written in MIPS assembly above, assume that the register $t1 is initialized to the value 15. How many MIPS instructions are executed?

Explanation / Answer

1) A) The slt instruction sets t2 to 1 if $0 (0) < t1 or sets t2 to 0 if 0 >=t1.

beq(branch if equal) instruction jumps to Done statement when t2 equals 0.

subi subtracts immediate 1 from t1 in every iteration

addi adds immediate 4 to orevious s2 value.

j loop jumps back to instruction labelled Loop.

Considering above facts, the code snippet will start with t1 = 5 and run till t1 becomes 0. In the first iteration, slt will have t2=1, then beq is false, t1 becomes 4 ans s2 becomes 0+4 = 4. Similarly in every alteration the value change is shown below.

In 6th iteration t1=0, so t2=0 and the beq statement is executed and control is moved to Done statement and further execution proceeds. So the output in s2 = 20

B) Equivalent C code snippet is s2 =b, t1 = i. loop runs 5 times untill t1 is greater than zero.

void calculate()

{

int b =0,i;

for(i=5;i>0;)

{

i--; //Doing this here instead as it is done in the beginning of the loop above and not end

b = b +4;

}

}

C) Fot t1 = 15, loop will run untill t1 equals 0. Every iteration has 5 MIPS instructions. It'll execute 15*5 = 75 instructions. Also when t1 becomes 0, slt and beq are executed which will be false and it'll jump to Done statement. so 2 more instructions. Total number of instructions executed is 77.

Iteration (beginning values) t1 t2 s2 1 5 1 4 2 4 1 8 3 3 1 12 4 2 1 16 5 1 1 20