在MIPS汇编奇怪的跳跃 [英] Strange jump in MIPS assembly
问题描述
我可能失去了一些东西真的很明显在这里,但我已经打算在这个遍地,我坚决卡住了。在下面的code, $ 8个
递增仅 $ 2!= $ 1,0
。我现在翻一番,三重检查和 BEQ
指令工作(例如,如果我改变垂耳为END2,它确实去那里)。
I'm probably missing something really obvious here, but I've been going over this over and over and I'm decidedly stuck. In the code below, $8
is incremented only if $2 != $0
. Now I double and triple checked and the beq
instruction works (for example if I change lop to end2, it does go there).
然而,由于某些原因, $ 8个
无论递增,即使执行分支
However, for some reason, $8
is incremented regardless, even if the branch is executed.
lop: beq $3, $0, end2
and $2, $3, $4
sll $3, $3, 1
beq $2, $0, lop
addi $8, $8, 1
j lop
我得承认我完全难住了。
I've got to admit I'm completely stumped.
推荐答案
(即和
后的第一个 BEQ
总是会被执行了。)
(The and
after the first beq
will always be executed, too.)
MIPS有明确的管道的危害;通过分支(或不)在作出决定时,以下指令已经发展得足够远通过指令流水线,这将是无论执行。这被称为延迟槽。
MIPS has explicit pipeline hazards; by the time the decision to branch (or not) is made, the following instruction has already progressed far enough through the instruction pipeline that it will be executed regardless. This is known as the "branch delay slot".
在某些情况下,你可以安排code利用这一优势;如果你不能(或不愿),你可以把一个 NOP
在接下来的指令。
In some cases you can arrange code to take advantage of this; if you can't (or don't want to), you can just put a nop
in the following instruction.
有些汇编器将重新排序code(或 NOP
填写)为您 - 例如气体
,GNU汇编,不,除非你告诉它不要以 .SET noreorder
指令。但你仍然需要拆卸反正时要意识到这一点。
Some assemblers will reorder code (or fill in the nop
) for you - e.g. gas
, the GNU assembler, does, unless you tell it not to with a .set noreorder
directive. But you still need to be aware of it when disassembling anyway.
如果你被汇编写code时不自动重排,我建议注释一些额外的缩进延迟插槽,使它脱颖而出:
If you're writing code without automatic reordering by the assembler, I recommend annotating the delay slot with some extra indentation to make it stand out:
lop: beq $3, $0, end2
nop
and $2, $3, $4
sll $3, $3, 1
beq $2, $0, lop
nop
addi $8, $8, 1
j lop
这篇关于在MIPS汇编奇怪的跳跃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!