MIPS循环输出

MIPS loop output

我们有关于 MIPS32 架构的作业要做,但我正在努力解决一些问题。

例如说R2(register n°2) = 0xD0000000。我们有以下代码:

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

而问题是执行后R3的值是多少。 这是我所做的:

(伪代码):

     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 

从这里我有点卡住了,因为 R2 将永远是 00..00(SLL 右边只有一个零)。 那么我必须明白这是一个无限循环吗?但我很确定这不是真的,我所做的事情有问题。

有人可以帮我吗?

谢谢!

这行代码 Bcl: BGEZ R2, Suit 的意思是: if R2 >= 0 then jump to Suit 但是在你的 trace sheet 即使条件可以跳转,你还是去了下一行 ( ADDI R3, R3, 1) .

例如在您的第一部分中:

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 = 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

如您所见,ADDI R3, R3, 1 行将永远不会执行,直到 Bcl: BGEZ R2, Suit 出错。