Question

I have this code:

      addi $t1, $zero, $zero
LOOP: lw $s1,0($s0)
      add $s2, $s2, $s1
      addi $s0, $s0, 4
      addi $t1, $t1, 1
      slti $t2, $t1, 100
      bne $t2, $s0, LOOP

The question is what is the total number of instructions executed.

Assuming the s0=0

What I think is is the loop terminate when t2 == s0 t2 is one for any t1<100 and s0 increment by 4 in every cycle. So does that makes the loop infinite?

Was it helpful?

Solution

What I think is is the loop terminate when t2 == s0.

Correct.

t2 is one for any t1<100.

Correct.

s0 increment by 4 in every cycle.

Correct.

So does that makes the loop infinite?

Not necessarily.

If lw $s1,0($s0) tries to access memory that is protected, your program will be terminated by the OS.

Also, if any of the adding instructions result in an overflow, your program will probably be terminated by the OS as well. Only unsigned add instructions (addu and the like) produce no exceptions/traps.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top