BNE -- Branch on not equal
___________________________________________________________________________
|Description: | Branches if the two registers are not equal |
|_____________|_____________________________________________________________|
|Operation: | if $s != $t advance_pc (offset << 2)); else advance_pc (4); |
|_____________|_____________________________________________________________|
|Syntax: | bne $s, $t, offset |
|_____________|_____________________________________________________________|
|Encoding: | 0001 01ss ssst tttt iiii iiii iiii iiii |
|_____________|_____________________________________________________________|
For the first bne the offset is 1, so 1 << 2 = 4 so you increment the program counter by four bytes. Since the word size is 32 bits the pc is incremented by one instruction
.text
la $a0, opOne
lw $a0, 0($a0)
la $a1, opTwo
lw $a1, 0($a1)
add $t0, $zero, $a0
addi $t1, $zero, 1
bne $t1, $a1, loopend
addi $t1, $t1, 1 # increment by 1 insruction
loopend: # to here (well the next instruction)
For the second bne the offset is b1111111111111011, sign extended its -5 so -5 << 2 = -20 so you increment the program counter by - 20 bytes or decrement it by 20 bytes. Since the word size is 32 bits the pc is decremented by five instructions
.text
la $a0, opOne
lw $a0, 0($a0)
loopend: #
la $a1, opTwo # 5 to here
lw $a1, 0($a1) # 4 ^
add $t0, $zero, $a0 # 3 |
addi $t1, $zero, 1 # 2 |
bne $t1, $a1, loopend # 1 |
addi $t1, $t1, 1 # decrement by 5 instructions