I would like to know what is SignImm in the following formula:
BTA = Branch Target Address = PC(old) + 4 + (SignImm << 2)
I have read that it is the address distance between the old PC + 4 and the new target address, but I have not seen it in a clear example. Could anyone explain me in an example please? I appreciate all the help, I am trying to learn assembly :)
0x00400000 addi $s0, $0, 4 # $s0 = 0 + 4 = 4
0x00400004 addi $s1, $0, 1 # $s1 = 0 + 1 = 1
0x00400008 sll $s1, $s1, 2 # $s1 = 1 << 2 = 4
0x0040000C bne $s0, $s1, target
0x00400010 addi $s1, $s1, 1 # $s1 = 4 + 1 = 5
0x00400014 sub $s1, $s1, $s0 # $s1 = 5 – 4 = 1
. . . . . .
0x004000FC target:
add $s1, $s1, $s0
BTA = PC + 4 + (SignImm << 2)
SignImm = (BTA-PC-4) >> 2
SignImm = (0x004000FC-0x0040000C-4) >> 2
SignImm = (0x000000F0-4) >> 2
SignImm = (0x000000EC) >> 2
SignImm = 0x0000003B = 59
So with this example 59 should be the distance between old PC + 4 and the target, but I have added 59 to 0x00400010 and it is not 0x004000FC...
All of your instructions reside at an address that is divisible by 4.
If the branch distance were to be encoded as a true byte-distance then that number would have its 2 low bits zero all the time. This is wasteful. The encoding therefore uses the dword-distance, thereby enabling branches that reach 4x farther.