简体   繁体   English

分支机器码

[英]Machine code for branching

I'm a little confused as to how a branching instruction translates to machine code. 对于分支指令如何转换为机器代码,我有些困惑。 I read in my book that the branch if equal instruction is a B-Type instruction, which is formatted like so: 我在书中读到,如果相等指令的分支是B型指令,其格式如下:

opcode reg1 reg2 address + offset 6 5 5 16 操作码reg1 reg2地址+偏移量6 5 5 16

In my textbook, there is a problem where I am shown a program, which I'm told is loaded at 0x4321ABC8 and looks like this: 在我的教科书中,有一个问题告诉我一个程序,该程序被告知加载在0x4321ABC8上,看起来像这样:

L1:   sw   $t0,3000($t1)

  addi $s1,$t1,-6

  beq  $t0,$t1,L1

As you can see, the 3rd line of code is branch if equal. 如您所见,代码的第三行相等。

I am trying to convert this program into binary (or hex). 我正在尝试将此程序转换为二进制(或十六进制)。 On the branch if equal instruction, I don't understand exactly what it is I'm supposed to enter into the address + offset field. 在分支上,如果指令相等,我不知道该输入地址+偏移量字段的确切含义。

First I thought the answer would be... 首先我以为答案会是...

base address (0x4321ABC8), plus 4 times the number of instructions from base which is where the program counter would be (4 * 2 = 8), 基址(0x4321ABC8),加上基数指令数的4倍,即程序计数器所在的位置(4 * 2 = 8),
Then minus four for the offset, which would be the original base address, 0x4321ABC8. 然后减去四个偏移量,该偏移量将是原始基址0x4321ABC8。

The problem with this is that 0x4321ABC8 is a number too big to fit into the 16 bit offset space of the instruction. 问题在于0x4321ABC8的数字太大,无法放入指令的16位偏移空间。

So then I thought that the answer would be to ONLY include the offset (which would be -8), however if that were the case, I don't know why the problem in the book would bother to tell me that the program is loaded at 0x4321ABC8. 因此,我认为答案将是仅包含偏移量(即-8),但是如果是这种情况,我不知道为什么书中的问题会麻烦告诉我程序已加载在0x4321ABC8。

Any help would be greatly appreciated! 任何帮助将不胜感激!

The offset is in fact encoded as number of words (or instructions, if you like), so you don't need to multiply by the word size (4 bytes). 实际上,偏移量被编码为单词数(如果需要,也可以是指令),因此您不需要乘以单词大小(4个字节)。 Also, this offset is relative to the already-updated PC , that is the address of the following instruction. 同样,此偏移量是相对于已经更新的PC ,即以下指令的地址。 So, your offset is -3 . 因此,您的偏移量是-3

That isn't the whole story though. 但这还不是全部。 MIPS does have branch delay slots but they are normally hidden from the programmer. MIPS确实具有分支延迟槽,但通常对程序员而言是隐藏的。 The assembler may try to fill these if it can do so without changing the meaning of your code. 汇编程序可以在不更改代码含义的情况下尝试填充它们。 If you feed your example into a real-world assembler (in this case, gas ) it will move the addi $s1,$t1,-6 into the delay slot of the branch because the result of that instruction is not used in the condition: 如果将示例提供给实际的汇编器(在本例中为gas ),它将把addi $s1,$t1,-6移入分支的延迟槽,因为该指令的结果未在条件中使用:

00000000 <L1>:
   0:   ad280bb8        sw      t0,3000(t1)
   4:   1109fffe        beq     t0,t1,0 <L1>
   8:   2131fffa        addi    s1,t1,-6

In this swapped order, the encoded offset is -2 , as you can also see from the fffe value. 按照此交换顺序,编码偏移量为-2 ,您也可以从fffe值中看到。

PS: If you are using SPIM be aware it has known off-by-one problem with offset encoding. PS:如果您使用的是SPIM注意,它存在偏移编码的一对一问题。 That is it uses the address of the current instruction instead of the next. 那就是它使用当前指令的地址而不是下一条指令。

PS #2: Just because they give you some extra information, it doesn't mean you have to use it. PS#2:仅仅因为它们为您提供了一些额外的信息,并不意味着您必须使用它。 That's a common trick to confuse poor students ;) 这是使可怜的学生迷惑的常见技巧;)

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

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