I need to encode the following beq
instruction:
...
start: addu $8, $9, $10
addiu $8, $8, 0x00FF7A01
beq $8, $0, start
...
I know that it is a J
format instruction so the division will be 6 bits to opcode and 6 to target address. beq
opcode is 000100
, so I already have 0001000100000000
, but I don't know how to find the 16 remaining bits, any help?
When
beq $8, $0, start
is getting executed, thePC
contains the address of next instruction. If the contents of$8
and$0
are not equal, there will be no jump, which means there will be no change in the value ofPC
.But if the contents of
$8
and$0
are same, you want to jump to the labelstart
. In other words, what you want is this - you want thePC
to contain the address of the instructionaddu $8, $9, $10
.Therefore, the difference between current value of PC and the value of PC in case of jump needs to be calculated.
Number of instructions to jump = 3 (but in negative direction since we want the PC to jump to a previous instruction).
Binary Equivalent of -3 = 2's complement of (
0000 0000 0000 0011
) which turns out to be1111 1111 1111 1101
Therefore, the 32-bit representation of this
beq
instruction will be:[6-bits opcode of beq][5-bits register $8][5-bits register $0][16-bits of relative-address]