解码 BNE MIPS 指令

发布于 2024-12-19 01:52:20 字数 1336 浏览 2 评论 0原文

我在 MARS 模拟器中运行以下 MIPS 代码:

add $t0, $zero, $zero        # i = 0
        add $t4, $zero, $zero        # initialize the sum to zero
        add $t5, $zero, $zero        # initialize temporary register to zero

        la $a0, array                 # load address of array
        la $a1, array_size            # load address of array_size
        lw $a1, 0($a1)                # load value of array_size variable

loop:
        sll  $t1, $t0, 2              # t1 = (i * 4)
        add $t2, $a0, $t1             # t2 contains address of array[i]
        sw $t0, 0($t2)                # array[i] = i

        addi $t0, $t0, 1              # i = i+1
        add $t4, $t4, $t0             # sum($t4) = ($t4 + array[i])

        slt $t3, $t0, $a1             # $t3 = ( i < array_size)
        bne $t3, $zero, loop          # if ( i < array_size ) then loop

        add $t5, $a0, $zero           # save contents of $a0 in temporary reg $t5
        nop                           # done.

在机器代码中,bne 指令如下:00010101011000001111111111111001。在本例中,立即数为:1111111111111001,等于:0xFFF9。 MIPS 会将其向左位移 2(乘以 4),并将其程序计数器设为该数字。但是,0xFFF9 乘以 4 为 0x3FFE4。这怎么可能? SLL 处的程序计数器应为 0x18,而不是 0x3FFE4。我在这里缺少什么?

谢谢你,

I am running the following MIPS code in MARS simulator:

add $t0, $zero, $zero        # i = 0
        add $t4, $zero, $zero        # initialize the sum to zero
        add $t5, $zero, $zero        # initialize temporary register to zero

        la $a0, array                 # load address of array
        la $a1, array_size            # load address of array_size
        lw $a1, 0($a1)                # load value of array_size variable

loop:
        sll  $t1, $t0, 2              # t1 = (i * 4)
        add $t2, $a0, $t1             # t2 contains address of array[i]
        sw $t0, 0($t2)                # array[i] = i

        addi $t0, $t0, 1              # i = i+1
        add $t4, $t4, $t0             # sum($t4) = ($t4 + array[i])

        slt $t3, $t0, $a1             # $t3 = ( i < array_size)
        bne $t3, $zero, loop          # if ( i < array_size ) then loop

        add $t5, $a0, $zero           # save contents of $a0 in temporary reg $t5
        nop                           # done.

In machine code, the bne instruction is the following: 00010101011000001111111111111001. In this case, the immediate is: 1111111111111001 which equals to: 0xFFF9. MIPS will take that, bit shift it to the left by 2 (multiplies it by 4) and takes its program counter to that number. However, 0xFFF9 multiplied by 4 is 0x3FFE4. How is that possible? The program counter at SLL should be 0x18 and not 0x3FFE4. What am I missing here?

Thank you,

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

很酷又爱笑 2024-12-26 01:52:20

这里有两点需要注意:

  1. 立即数是 2 的补码;由于符号位为 1,因此这是一个负值。
  2. 在 MIPS 上,分支的目标表示为偏移量(它与负值平方,因为循环位于 bne 之前)。该值乘以 4 b/c 指令的大小均为 4 字节

There are 2 things to note here:

  1. The immediate is in 2's complement; since the sign bit is 1, this is a NEGATIVE value
  2. On the MIPS, the target of the branch is expressed as an offset (which squares w/ the value being negative, since loop comes before the bne). The value gets multiplied by 4 b/c the instructions are all 4-bytes in size
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文