I was wondering how to find the branch location of an instruction such as
bne $s1, $s0, label_name
. From what I understand the 16-bit immediate value is a relative offset that should be added to the PC to branch.
What I want to know is how the branch address is calculated as I am finding multiple, contradicting answers for the same thing.
What I have found so far:
- Increment current PC by four.
- Shift immediate value (16-bit) left by two (multiply by four).
- Sign extend immediate value to 32-bits.
- Add the PC and immediate value.
Is this correct? And if so, how would I sign extend this value in C?