bnez MIPS 指令的编码是什么? [英] what would be the encoding of a bnez MIPS instruction?
问题描述
如果 Loop 的地址是 0x00012344,最后一条指令的编码是什么?
循环:添加 $a0, $0, $t0原 $v0, $0, 4系统调用添加 $t0, $t0, -1bnez $t0, 循环
这最后一条指令相当于:
bne $t0, $0, 循环
但是,我将如何用机器代码编写它?
这就是我得到的:
000101 01000 00000 iiii iiii iiiiii
我不知道该怎么办,但因为我在这里.Loop 的地址是 0x00012344,但是它是一个 17 位的二进制数.我只有 16 位
MIPS 中的分支使用偏移量(左移 2 个位置),而不是绝对地址.
<块引用>BNE -- 不等于分支
说明:
如果两个寄存器不相等则分支
操作:
if $s != $t advance_pc (offset <<2));else Advance_pc (4);
语法:
bne $s, $t, 抵消
编码:
0001 01ss ssst tttt iiii iiii iiii iiii
所以 BNE
指令的 Loop
偏移量应该放在 i
部分,而不是它的绝对地址.
EDIT:编辑问题代码:
循环:addu $a0, $0, $t0原 $v0, $0, 4系统调用添加 $t0, $t0, -1bnez $t0, 循环
由于所有 MIPS 指令都是 32 位宽(4 个字节),我们可以轻松计算偏移量:
Loop
地址与 BNEZ 指令相距 -4 条指令,因此相距 -4 x 4 = -16 字节.- 但是我们需要再添加一个字,因为在 MIPS PC 已经增加了 4 并且当 BNEZ 执行时,PC 已经指向下一条指令(参见 MIPS 中的延迟槽).所以最终的偏移量是 -20 = -(0x14) = 0xFFFEC.
- 我们右移 2 个位置(除以 4)以获得字(而不是字节)的偏移量:0xFFFEC >> 2 = 0xFFFB
也可以直接按字数计算:
Loop
地址是来自 BNEZ 指令的 -4 条指令- 额外的 -1,因为 PC 已经递增:-5
- -5 = -(0x5) = 0xFFFB
所以我们得到了 i
部分的 0xFFFB;所以整个指令是0x1500FFFB
.
顺便说一下,您可以使用任何 MIPS 汇编器或 模拟器 来检查这一点.>
(火星模拟器截图)
What would be the encoding of this last instruction if the address of the Loop is 0x00012344?
Loop:
addu $a0, $0, $t0
ori $v0, $0, 4
syscall
addi $t0, $t0, -1
bnez $t0, Loop
this last instruction is equivalent to:
bne $t0, $0, Loop
however, How would I write this in machine code?
this is how for I got:
000101 01000 00000 iiiii iiiii iiiiii
I dont know what to but for the i's here. The address of Loop is 0x00012344 however that is a 17 bit binary number. I only have 16 bits
Branch in MIPS works with offsets (shifted left 2 positions), not absolute addresses.
BNE -- Branch on not equal
Description:
Branches if the two registers are not equal
Operation:
if $s != $t advance_pc (offset << 2)); else advance_pc (4);
Syntax:
bne $s, $t, offset
Encoding:
0001 01ss ssst tttt iiii iiii iiii iiii
So it's Loop
offset from the BNE
instruction that should go on the i
part, not its absolute address.
EDIT: with edited question code:
Loop:
addu $a0, $0, $t0
ori $v0, $0, 4
syscall
addi $t0, $t0, -1
bnez $t0, Loop
Since all MIPS instructions are 32-bit wide (4 bytes), we can calculate offset easily:
Loop
address is -4 instructions from BNEZ instruction, so it's -4 x 4 = -16 bytes away.- But we need to add one more word because in MIPS PC was already incremented by 4 and when BNEZ is executing, PC is already pointing to next instruction (see delay slot in MIPS). So final offset is -20 = -(0x14) = 0xFFFEC.
- We shift right 2 positions (divide by 4) to get this offset in words (instead of bytes): 0xFFFEC >> 2 = 0xFFFB
You can also do it by counting by words directly:
Loop
address is -4 instructions from BNEZ instruction- Additional -1 because PC already incremented: -5
- -5 = -(0x5) = 0xFFFB
So we got 0xFFFB for i
part; so whole instruction is 0x1500FFFB
.
By the way you can check this with any MIPS assembler or simulator.
(Screenshot from MARS simulator)
这篇关于bnez MIPS 指令的编码是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!