Reputation: 51
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
?
Upvotes: 5
Views: 5819
Reputation: 33601
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
Upvotes: 3
Reputation: 26655
Branch on not equal has the syntax bne rs,rt,label
where the first 6 digits is the opcode, the next 5 digits is the rs, the next 5 digits is the rt and the rest is the label of the branch target address:
BTA = PC + 4 + imm * 4
which is calculated by sign extending the immediate, multiplying by 4 and adding that and 4 to the program counter. For an explaination see this question
How to Calculate Jump Target Address and Branch Target Address?
The bne
format is immediate (I-type). The opcode is 5 (000101). For example, this machine code prints every third character in the alphabet.
00100100000100000000000000110000
00000000000100000010000000100001
00100100000000100000000000001011
00000000000000000000000000001100
00100010000100000000000000000011
00100100000010000000000001011101
00010110000010001111111111111010
00000000000000000000000000000000
00001000000100000000000000001000
00000000000000000000000000000000
It is 10 lines (10 instructions). Line 7 is a branch and it has machine code 00010110000010001111111111111010
. The first 6 numbers 000101
is the opcode 5. Then 5 + 5 bits of registers (in this example the registers are $16 and $8 and the rest is the immediate branch target address.
Similarly, your program
.text
loop:
add $t2,$t2,$t1
addi $t2,$t2,4
sw $t2,4($s0)
bne $t2,20,loop
jr $ra
translates to 7 lines the machine code:
00000001010010010101000000100000
00100001010010100000000000000100
10101110000010100000000000000100
00100000000000010000000000010100
00010100001010101111111111111011
00000011111000000000000000001000
The actual translation looks like this
Address Code Basic Source
0x00400000 0x01495020 add $10,$10,$9 3 add $t2,$t2,$t1
0x00400004 0x214a0004 addi $10,$10,0x00000004 addi $t2,$t2,4
0x00400008 0xae0a0004 sw $10,0x00000004($16)5 sw $t2,4($s0)
0x0040000c 0x20010014 addi $1,$0,0x00000014 6 bne $t2,20,loop
0x00400010 0x142afffb bne $1,$10,0xfffffffb
0x00400014 0x03e00008 jr $31 7 jr $ra
...where the second to last line is the bne
: 00010100001010101111111111111011
. The first 6 digits 000101
is the opcode, the next 5 (00001
) + 5 (01010
) digits are the registers and the rest (1111111111111011
) is the immediate value of the branch target address (in hexadecimal 1111111111111011
=FFFB
.
For more details see a MIPS manual.
Upvotes: 0