跳转指令MIPS大会 [英] Jump instruction in MIPS Assembly

查看:352
本文介绍了跳转指令MIPS大会的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是一些MIPS汇编code我写来测试跳转指令:

Here is some MIPS assembly code I wrote to test the jump instruction:

addi $a0, $0, 1
j next
next:
j skip1
add $a0, $a0, $a0
skip1:
j skip2:
add $a0, $a0, $a0
add $a0, $a0, $a0
skip2:
j skip3
loop:
add $a0, $a0, $a0
add $a0, $a0, $a0
add $a0, $a0, $a0
skip3:
j loop

当我运行汇编程序,这里的结果是:

When I run the assembler, here's the result:

[0x000000]  0x20040001  # addi $a0, $zero, 1 ($a0 = 1)
[0x000004]  0x08000002  # j 0x0002 (jump to addr 0x0008)
[0x000008]  0x08000004  # j 0x0004 (jump to addr 0x0010)
[0x00000C]  0x00842020  # add $a0, $a0, $a0 ($a0 = $a0 + $a0)
[0x000010]  0x08000007  # j 0x0007 (jump to addr 0x001C)
[0x000014]  0x00842020  # add $a0, $a0, $a0 ($a0 = $a0 + $a0)
[0x000018]  0x00842020  # add $a0, $a0, $a0 ($a0 = $a0 + $a0)
[0x00001C]  0x0800000B  # j 0x000B (jump to addr 0x002C)
[0x000020]  0x00842020  # add $a0, $a0, $a0 ($a0 = $a0 + $a0)
[0x000024]  0x00842020  # add $a0, $a0, $a0 ($a0 = $a0 + $a0)
[0x000028]  0x00842020  # add $a0, $a0, $a0 ($a0 = $a0 + $a0)
[0x00002C]  0x08000008  # j 0x0008 (jump to addr 0x0020)

纵观机code为跳转指令,这是我所看到的:

Looking at the machine code for the jump instructions, this is what I see:

1st jump (just jumps to next instruction) 0x08000002
2nd jump (skips 1 instruction) 0x08000004
3rd jump (skips 2 instructions) 0x08000007
4th jump (skips 3 instructions) 0x0800000B
5th jump (skips 3 instructions backwards) 0x08000008

从看这些指令,它看起来像机器code有08跳转指令开始,并在年底的数字告诉跳转指令哪里去了。不过,我想不通这个数字是如何计算的。此外,有什么指示,我认为第五跳是向后跳。

From looking at these instructions, it looks like the machine code starts with a 08 for the jump instruction, and the number at the end tells the jump instruction where to go. However, I can not figure out how this number is calculated. Also, there is nothing to indicate to me that the 5th jump is a backwards jump.

跳价值是如何计算的?

推荐答案

刚刚寻找到一个参考手册关于运code编码的更多细节。

Just look into a reference manual for more details about the opcode encoding.

短版:在32位指令不能包含一个32位的跳转目标。运code使用6位,这让26位的指令。目标地址是通过取的指令的地址的前4位以下的Ĵ指令,则2个零比特附加到从跳转指令的操作数的26位构成。 (由于指令都是32位的,对齐是有用的,允许最后两个0的漏报。)

Short version: In a 32 bit instruction you can not include a 32-bit jump destination. The opcode uses 6 bits, which leaves 26 bits for the instruction. The target address is constructed by taking the first 4 bits of the address of the instruction following the j instruction, then 2 zero bits are appended to the 26 bits from the jump instruction operand. (As the instructions are 32 bits, alignment is useful and allows the omitting of the last two 0's.)

要落后跳跃:地址是绝对的,不是相对的,所以它只能依赖于跳转指令的地址,无论是向前迈进了一或向后跳

To the backward jump: The addresses are absolute, NOT relative, so it only depends on the address of the jump instruction, whether it is a forward or a backward jump.

修改:更详细的说明:
我们有地址的 X 的跳转指令的Ĵ的。让我们的 T 的再present的的跳跃操作数Ĵ的。的 T 的为26位宽。
下一个指令的地址的位模式的计算如下:

EDIT: More detailed description: We have at address x jump instruction j. Let t represent the jump operand of j. t is 26 bit wide. The bit pattern of address of the next instruction is computed as follows:

upper_6_bits_of(x+4),t,0,0

所以跳始终绝对的。有没有相对跳跃。当结果小于x那么它是一个落后的跳跃,当它大于它是一个向前跳跃(如果你想要的东西愚蠢的,你把它相等; - )。

So the jump is ALWAYS absolute. There are no relative jumps. when the result is smaller than x then it is a backward jump, when it is greater it is a forward jump (and if you want something stupid, you make it equal ;-).

因此​​,让我们看看你的榜样的第五跳:

So let's look at the 5th jump of your example:

跳转目标的前6位都是000000,因为跳转后面的指令的地址的高6位是000000

The first 6 bits of the jump target are: 000000, because the upper 6 bits of the address of the instruction behind the jump are 000000.

接下来26位是跳转指令的最低26比特,即00000000000000000000001000

The next 26 bits are the lowest 26 bits of the jump instruction, that is 00000000000000000000001000

最后2位:00,因为得了总是附加

The last 2 bits are: 00, because the got always appended.

我们一起:0000000000000000000000000000100000,这是十六进制20.而在该地址正是标签/指令,其中的流动应该继续

Together we have: 0000000000000000000000000000100000, which is hex 20. And at that address is exactly the label/instruction where the flow should continue.

这篇关于跳转指令MIPS大会的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆