简体   繁体   English

将Mips程序集转换为机器代码:BNE

[英]Translating Mips assembly to machine code: BNE

Given this code: 给出以下代码:

[0x00000000]  arraycopy: lw $t0, 0($a0)
[0x00000004]             addi $a0, $a0,4
[0x00000008]             addi $a1, $a1,4
[0x0000000C]             sw $t0, -4($a1)
[0x00000010]             bne $t0,$0,arraycopy
[0x00000014]             Nop ( means no operation )

Now I'm interested to translate the bne command line to machine code: 现在,我有兴趣将bne命令行转换为机器代码:

What I got: 0001 0100 0000 1000 - .... 我得到的是:0001 0100 0000 1000-....

Now what will be the rest of the command? 现在剩下的命令是什么?

**Update: I keep getting that the Offset value is: 1111 1111 1110 1100 which is -20 But the correct answer should be : 0xfffb which is -5 **更新:我不断得到偏移值是: 1111 1111 1110 1100 ,它是-20,但是正确的答案应该是: 0xfffb ,它是-5

Any Idea why?** 知道为什么吗?**

Because MIPS instructions must be aligned to 4 byte boundaries, the offset within a branch instruction can be encoded [and is encoded] as a word offset and not a byte offset. 因为MIPS指令必须对齐到4个字节的边界,分支指令中的偏移量可以被编码[和编码]作为一个的偏移,而不是一个字节偏移量。 This increases the reachable range of the branch by 4x [a good thing]. 这将分支的可达范围增加了4倍[一件好事]。 So, for an signed encoded offset of 16 bits, you get a signed byte offset of 18 bits 因此,对于16位的带符号编码偏移量,您将获得18位的带符号字节偏移量

Thus, for a given byte offset, it is encoded in the branch by shifting it right by 2 [ie the lower two bits must always be zero, so nothing gets lost]. 因此,对于给定的字节偏移,通过将其右移2来在分支中进行编码(即,低两位必须始终为零,因此不会丢失任何内容)。 When the instruction is executed, the hardware will take the offset and restore it to a byte offset by shifting it left by 2. 执行该指令时,硬件将获取偏移量并将其左移2位,将其恢复为字节偏移量。

The byte offset is calculated not from the address of the branch itself (0x00000010) but from the address of the instruction following (0x00000014). 字节偏移的计算不是根据分支本身的地址(0x00000010),而是根据后续指令的地址(0x00000014)。

So, the byte offset is -0x00000014 (decimal -20) --> 0xFFFFFFEC. 因此,字节偏移量为-0x00000014(十进制-20)-> 0xFFFFFFEC。 Shifting this right 2 bits [dividing by 4] produces 0xFFFFFFFB (decimal -5). 向右移2位(除以4)将产生0xFFFFFFFB(十进制-5)。

Since the encoded offset is only a signed 16 bits, we end up with 0xFFFB 由于编码的偏移量只有16位有符号,因此我们以0xFFFB结尾

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

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