跳过 mips32 中的下一条指令
Skipping the next instruction in mips32
假设我想在 MIPS 中开发一个伪指令,在我调用这个跳过指令后跳过下一条指令说
skip $s0
.
我想也许我可以在 $s0
上使用 $jr
,但我需要更改 $s0
的地址。
我该如何解决这个问题?
I want to develop a psuedoinstruction in MIPS that skips the next instruction
更简单的是使用一个总是被验证的"branch"。
beqc [=10=],[=10=],2
如果 $0==$0(即总是), 将用 PC+(2*4)
替换 PC
并跳过下一条指令。
这是一个常用于处理 if-then-else 的技巧
if(a1)
a2=3;
else
a3=4;
beqc $a1, [=12=], else
addi $a2, [=12=], 3
beqc [=12=], [=12=], 2 ; go to end of if then else
else: addi $a3, [=12=], 4
# end of if-then else
beqc
是 mips64-v6 中引入的无延迟分支(以及许多其他具有零延迟槽的 branch/jump)。
使用旧版本的 mips ISA,无法跳过下一条指令,因为 所有 分支都执行以下指令。跳过第二个next指令,思路是一样的。
beq [=13=],[=13=],2 ; delayed branch. execute next instruction and if test
; is true (ie always) go to pc+4+2*4
add [=13=], [=13=], [=13=] ; aka nop (because of the delay slot)
xxx $a1, $a2, $a3 ; this instruction will be skipped
yyy $t1, $t2, $t3 ; and this instruction will be executed
假设我想在 MIPS 中开发一个伪指令,在我调用这个跳过指令后跳过下一条指令说
skip $s0
.
我想也许我可以在 $s0
上使用 $jr
,但我需要更改 $s0
的地址。
我该如何解决这个问题?
I want to develop a psuedoinstruction in MIPS that skips the next instruction
更简单的是使用一个总是被验证的"branch"。
beqc [=10=],[=10=],2
如果 $0==$0(即总是), 将用 PC+(2*4)
替换 PC
并跳过下一条指令。
这是一个常用于处理 if-then-else 的技巧
if(a1)
a2=3;
else
a3=4;
beqc $a1, [=12=], else
addi $a2, [=12=], 3
beqc [=12=], [=12=], 2 ; go to end of if then else
else: addi $a3, [=12=], 4
# end of if-then else
beqc
是 mips64-v6 中引入的无延迟分支(以及许多其他具有零延迟槽的 branch/jump)。
使用旧版本的 mips ISA,无法跳过下一条指令,因为 所有 分支都执行以下指令。跳过第二个next指令,思路是一样的。
beq [=13=],[=13=],2 ; delayed branch. execute next instruction and if test
; is true (ie always) go to pc+4+2*4
add [=13=], [=13=], [=13=] ; aka nop (because of the delay slot)
xxx $a1, $a2, $a3 ; this instruction will be skipped
yyy $t1, $t2, $t3 ; and this instruction will be executed