0

I understand when I translate C code into MIPS language, the address needs to be multiplied by 4. i.e ) int x = A[1] => lw $t1, 4*1 (address of A)

But I don't know why it is also the case for branch target address.

From this Q&A How to Calculate Jump Target Address and Branch Target Address?
I read that offset needs to be word-aligned since PC address is so. But then, isn't the offset address needed to be divided by 4, since PC is word-aligned, and offset address is byte-aligned ?

I think I have some misconception here.

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
Js Park
  • 1
  • 2

1 Answers1

2

MIPS instruction are all 4 bytes long — and they are always located on even word boundaries.  As such, this applies to all instruction addresses — branch source as well as branch target — and hence all branch offsets have the low 2 bits as being 0, because they are the difference between two word-aligned instruction addresses.

It would be wasteful to encode those always 0 bits in the instruction, so they are omitted.

Rather than encoding bits known to always be zero into the instruction field, those 2 bits are repurposed to allow for a larger branch displacement immediate.  This is why there is a /4 or *4 depending on whether encoding or decoding.

(In fact, the PC register will always hold zeros for the low two bits, so the hardware doesn't even need a 32 bit register for the PC, a 30 bit register would suffice, and +1 instead of +4 to increment — if it was me that's how I'd try to setup the hardware internally.)

Erik Eidt
  • 23,049
  • 2
  • 29
  • 53
  • thanks for Answer! So, the multiplication is just the sake of reappearing two 0's...? by shift left? – Js Park Mar 31 '21 at 17:37
  • Yes, exactly, those 2 always zero bits. It might be by left shift, but since the shift amount is constant, it could be done merely by wiring, or, as another approach, but not needing them at all. – Erik Eidt Mar 31 '21 at 17:39
  • then why didn't my textbook and the Q&A link above explain in that way? it doesn't make sense to me why it is called 'word aligned' – Js Park Mar 31 '21 at 17:41
  • They're just showing the raw mechanics / formulas, rather properly motivating why this is done or why it works. That link did try to explain it but it didn't start the explanation at the beginning of the story; it started explaining the underlying formulas, and diverged into the reasoning behind it. – Erik Eidt Mar 31 '21 at 17:42
  • 1
    Word aligned is what we call addresses whose low 2 bits are zero, aka evenly divisible by 4. – Erik Eidt Mar 31 '21 at 17:43
  • I got it Thanks!! Erik. It really helped a lot!! it's past the midnight here right now I really appreciate your answer – Js Park Mar 31 '21 at 17:46