1

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...

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
Lechius
  • 189
  • 7
  • 1
    `SignImm` is the **Sign**ed **Imm**ediate encoded into the instruction word. – fuz Oct 16 '22 at 21:15
  • @fuz I know that is the 16 bit imm that is located in the i type instruction word, but I would like to know, what is that used for – Lechius Oct 16 '22 at 21:19
  • The immediate is what gives the location of the branch target as an offset from the current address in words. – fuz Oct 16 '22 at 21:26
  • I understand, but I have tried to calculate the difference distance in an example and it doesn't correspond to the SignImm, this is why I would like it to see it in an example please, thanks – Lechius Oct 16 '22 at 21:29
  • If you post the example you tried to work out, I can try and help you find out what the problem is. – fuz Oct 16 '22 at 21:35
  • @fuz I have edited the post with an example, check it – Lechius Oct 16 '22 at 21:41
  • I don't understand your calculations. The distance is `BTA-PC`. 59 is what you get when you subtract 4 from that and shift right by 2 (which is what is encoded in the instruction word). – fuz Oct 16 '22 at 21:47
  • So what is 59 representing then? I don't understand what is representing that 59... – Lechius Oct 16 '22 at 21:52
  • 1
    59 is what is stored as an immediate in the instruction word. – fuz Oct 16 '22 at 21:55

1 Answers1

1

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.

0x0040000C + 4 bytes + 0x3B dwords  

0x0040000C + 4 bytes + 0xEC bytes  

0x0040000C + 0x000000F0 bytes

0x004000FC
Sep Roland
  • 33,889
  • 7
  • 43
  • 76