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)
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
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.
How is the jump value calculated?
add $a0, $a0, $a0
? why don't usesll $a0, $a0, 3
– phuclv