MIPS loop output

60 views Asked by At

We have an homework to do about the MIPS32 architecture but I'm struggling with some questions.

For instance it's said that R2(register n°2) = 0xD0000000. And we have the following code :

ADDI  R3, R0, 0
ADDI  R4, R0, 31
Bcl:  BGEZ R2, Suit
      ADDI R3, R3, 1
Suit: SLL R2, R2, 1
      ADDI R4, R4, -1
      BGEZ R4, Bcl

And the question is what is the value of R3 after the execution. Here is what I did :

(pseudo code):

     R3 = 0
     R4 = 31
     
     R2 = 1101 00.. ..00 and it's greater than 0 so we go into the loop
     R2 = SLL(R2,1) = 1010 00.. ..00 
     R4 = R4 - 1
     R3 = R3 + 1 (R3 = 1)
     R4 = 30 >= 0 so we go to Bcl 

     R2 = 1010 00.. ..00 and it's greater than 0 so we go into the loop
     R2 = SLL(R2,1) = 0100 00.. ..00 
     R4 = R4 - 1
     R3 = R3 + 1 (R3 = 2)
     R4 = 29 >= 0 so we go to Bcl 

     R2 = 0100 00.. ..00 and it's greater than 0 so we go into the loop
     R2 = SLL(R2,1) = 1000 00.. ..00 
     R4 = R4 - 1
     R3 = R3 + 1 (R3 = 3)
     R4 = 28 >= 0 so we go to Bcl 

     R2 = 1000 00.. ..00 and it's greater than 0 so we go into the loop
     R2 = SLL(R2,1) = 0000 00.. ..00 
     R4 = R4 - 1
     R3 = R3 + 1 (R3 = 4)
     R4 = 27 >= 0 so we go to Bcl 

And from here i'm kinda stuck because R2 will be 00..00 for ever (the SLL only had a zero at the right). So do I have to understand it's a infinite loop ? But I'm quite sure it's not true and there is something wrong in what I did.

Can somebody help me ?

Thanks !

1

There are 1 answers

1
Hosseinreza On BEST ANSWER

This line of code Bcl: BGEZ R2, Suit means : if R2 >= 0 then jump to Suit but in your trace sheet even though the condition is okay to jump , you went for next line ( ADDI R3, R3, 1) .

For example in your first section :

R2 = 1101 00.. ..00 and it's greater than 0 so we go into the loop
R2 = SLL(R2,1) = 1010 00.. ..00 
R4 = R4 - 1
R3 = R3 + 1 (R3 = 1)
R4 = 30 >= 0 so we go to Bcl 

you have to change it to this :

R2 = 1101 00.. ..00 and it's greater than 0 so we jump to the Suit
R2 = SLL(R2,1) = 1010 00.. ..00 
R4 = R4 - 1
R4 = 30 >= 0 so we jump to Bcl

// I will continue one more section to show you 

R2 = 1010 00.. ..00 and it's greater than 0 so we jump to Suit
R2 = SLL(R2,1) = 0100 00.. ..00 
R4 = R4 - 1
R4 = 29 >= 0 so we go to Bcl

As you see the line ADDI R3, R3, 1 will never execute until Bcl: BGEZ R2, Suit become wrong.