I don’t understand how is the branching address is calculated in in MIPS.
Here is big picture what happens to program counter(PC): PC = PC + 4 + ((signed-extended offset)<<2)
I see the reason why the offset number is signed-extended before being added to PC, it is just 16 bit value, that needs to be turned into 32 bit value, but why do we need to left double shift signed-extended number ???
(Also,<<2 means that we multiply the number by 4)
While we on the subject, I will extend the question:
When we offset from the immediate value (register), why we don’t do <<2 ?
Thanks !
The important thing to know is that the instructions are four-byte aligned. So, the two least significant address bits do not need to be stored, and this allows 16-bit displacement to give a 2^18-byte range.
Additionally, you may notice that displacements for data are not shifted, because data need to be byte-addressable.