Shifting the Sign extended constant in MIPS
Asked Answered
P

2

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

Programmer answered 7/12, 2012 at 10:19 Comment(0)
W
14

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.

Wallpaper answered 7/12, 2012 at 10:23 Comment(2)
Sorry but What do you mean by Branch offset( or 16 bit constant(2 bytes)) of 1=4 bytes ?Programmer
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.Wallpaper
G
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.

Guardianship answered 5/5, 2018 at 15:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.