0

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

例如,据说 R2(寄存器 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 右侧只有一个零)。那么我必须理解它是一个无限循环吗?但我很确定这不是真的,我所做的事情有问题。

有人可以帮助我吗?

谢谢 !

4

1 回答 1

0

这行代码的 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出错。

于 2020-10-13T13:00:27.830 回答