I don't understand how to translate a label. Anyone can give me a help
suppose we have the following code:
loop:
add $t2,$t2,$t1
addi $t2,$t2,4
sw $t2,4($s0)
bne $t2,20,loop
jr $ra
How translate to binary bne $t2,10,loop ?
A few things to note.
The branch offset is always calculated from the address of the branch + 4 (i.e. PC+4), so it is relative to the address of the jr instruction.
Since mips instructions must be aligned to a word [four byte boundary], the rightmost two bits of an instruction address will be [must always be] zero.
The mips architecture takes advantage of that by encoding a branch offset as a word offset (i.e. the byte offset is shifted right by 2). This extends the range of a branch instruction 16 bit immediate encoding to 18 bits.
So, here is the listing:
00: loop:
00: add $t2,$t2,$t1
04: addi $t2,$t2,4
08: sw $t2,4($s0)
0C: bne $t2,20,loop
10: jr $ra
The jr address is 0x10, so the byte offset for loop would be -0x10 and the encoded offset would be -0x04 or 0xFFFC and the bne would be xxxxFFFC
But ... There is a problem with that. This particular bne uses an immediate value for the second argument. This makes the bne a pseudo-op and not a simple bne instruction.
So, the actual sequence becomes:
00: loop:
00: add $t2,$t2,$t1
04: addi $t2,$t2,4
08: sw $t2,4($s0)
0C: addi $at,$zero,20
10: bne $at,$t2,loop
14: jr $ra
Note that the bne becomes a two instruction sequence: addi and bne
The jr address is now 0x14, so the byte offset for loop would be -0x14 and the encoded offset would be -0x05 or 0xFFFB and the bne would be xxxxFFFB
The bne assembler format is:
bne s,t,label
The bne encoding is:
0001 01ss ssst tttt iiii iiii iiii iiii
So, the s register is $at or $1 --> 00001
The t register is $t2 or $10 --> 01010
So, now we have:
0001 01ss ssst tttt iiii iiii iiii iiii
00 0010 1010
0001 0100 0010 1010 iiii iiii iiii iiii
In hex, this is 142Ayyyy. We already know that yyyy is FFFB, so the final hex value is: 142AFFFB
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With