简体   繁体   English

解码BNE MIPS指令

[英]Decoding BNE MIPS Instruction

I am running the following MIPS code in MARS simulator: 我在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.

In machine code, the bne instruction is the following: 00010101011000001111111111111001 . 在机器代码中, bne指令如下: 00010101011000001111111111111001 In this case, the immediate is: 1111111111111001 which equals to: 0xFFF9 . 在这种情况下,立即数是: 1111111111111001 ,等于: 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. MIPS将采用该方法,将其向左移2(乘以4)并将其程序计数器移至该数字。 However, 0xFFF9 multiplied by 4 is 0x3FFE4 . 但是, 0xFFF9乘以4就是0x3FFE4 How is that possible? 那怎么可能? The program counter at SLL should be 0x18 and not 0x3FFE4 . SLL处的程序计数器应为0x18而不是0x3FFE4 What am I missing here? 我在这里想念什么?

Thank you, 谢谢,

There are 2 things to note here: 这里有两件事要注意:

  1. The immediate is in 2's complement; 立即数以2的补数表示; since the sign bit is 1, this is a NEGATIVE value 由于符号位为1,因此为负值
  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). 在MIPS上,分支的目标表示为偏移量(该值的平方乘以负值,因为循环位于bne之前)。 The value gets multiplied by 4 b/c the instructions are all 4-bytes in size 该值乘以4 b / c,指令的大小均为4个字节

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM