5

Why do we shift by 2 the sign extended 16bit constant in branching instruction in MIPS? I am confused with this idea. What good does this shifting brings to the sign extended 16 bit constant. Here is the picture:

enter image description here

Regards

Alfred
  • 1,543
  • 7
  • 33
  • 45

2 Answers2

13

MIPS instructions are 32 bits = 4 bytes, so the branch offset is specified as a multiple of 4, i.e. a branch offset of 1 = 4 bytes. This enables a much larger range of branch offsets than if the offset were specified in bytes (as there would then be two redundant bits). Shifting left by 2 is the same as multiplying by 4, of course.

Paul R
  • 208,748
  • 37
  • 389
  • 560
  • 1
    Sorry but What do you mean by Branch offset( or 16 bit constant(2 bytes)) of 1=4 bytes ? – Alfred Dec 07 '12 at 10:26
  • 6
    The branch instruction is a *relative branch* - it needs to know how many instructions *relative to the current PC* that it needs to jump. This is called a *branch offset*. To calculate the new PC address you multiply this 16 bit offset by 4, add it to the current PC, and then that becomes the new PC. – Paul R Dec 07 '12 at 10:29
4

Every binary that is shifted two times to the left is multiple of 4. So by Shifting the immediate two times to the left and adding it to the next instruction address the next instruction address would be obtained.