jr指令(MIPS)为什么不执行我期望的代码?

问题描述 投票:0回答:1

这是我的MIPS汇编代码:

#         Assembly                    Description                      Address
main:         addi $2, $0, 5            # intialize $2 = 5             00       
              addi $3, $0, 12           # intialize $3 = 12            04       
              addi $7, $3, -9           # intialize $7 = 3             08       
              addi $8, $0, 44           # should init $8 = 44          0c      
              or   $4, $7, $2           # $4 = (3 OR 5) = 7            10       
              and  $5, $3, $4           # $5 = (12 AND 7) = 4          14       
              add  $5, $5, $4           # $5 = 4 + 7 = 11              18       
              beq  $5, $7, end          # shouldn’t be taken           1c       
              slt $4, $3, $4            # $4 = 12 < 7 = 0              20       
              beq $4, $0, around        # shouldn’t be taken           24       
              addi $5, $0, 0            # shouldn’t be taken           28       
around:       slt $4, $7, $2            # $4 = 3 < 5 = 1               2c       
              add $7, $4, $5            # $7 = 1 + 11 = 12             30       
              sub $7, $7, $2            # $7 = 12 – 5 = 7              34       
              srl $7, $7, 2             # $7 = 7>>2 = 1                38       
              jr $8                     # jump to [8] = 44             3c       
              addi $2, $0, 1            # shouldn’t be taken           40       
              sw $7, 68($3)             # [80] = 1                     44       
              lw $2, 80($0)             # $2 = [80] = 1                48       
              j end                     # should be taken              4c       
              addi $2, $0, 1            # shouldn’t happen             50       
end:          sw $2, 84($0)             # write mem[84] = 1            54       

在我添加jr $ 8和addi $ 2,$ 0,1指令之前,它一直很好,所以地址3c和40上的指令。我无法说出它为什么导致程序失败。我认为jr指令将转到$ 8寄存器中的地址,我在第4条指令中将其指定为44,在此我初始化$ 8。想法是jr指令将使其跳转到地址44,然后执行sw指令,因此将省略jr之后的addi指令。

为什么它不按照我的意愿去做?我在MIPS火星上运行它,逐步执行时似乎并没有到达sw指令...相反,它进入了寄存器编号14 ...?

有人可以解释一下我对jr指令的误解吗?

assembly mips instructions machine-code
1个回答
0
投票

您好,我无法发表评论。看来您找到了一种解决方案。不知道是否可以在此特定练习中从内存中添加变量,但是可以,立即数据限制为16位。我通常要做的是在.data中声明一个变量,例如0x100000来代表地址,然后将其加载到寄存器中以访问该内存地址。我倾向于将其用作置换,但是我不明白为什么它也不能作为您的问题的解决方案。

© www.soinside.com 2019 - 2024. All rights reserved.