Why do we Sign Extend in load word instruction?

user379888 picture user379888 · Sep 18, 2011 · Viewed 25.1k times · Source

I am learning MIPS 32 bit. I wanted to ask that why do we Sign Extend the 16 bit offset (in Single Cycle Datapath) before sending it to the ALU in case of Store Word?

Answer

Jonna picture Jonna · Oct 21, 2011

I am not sure if it's helpful for you now, but I am posting it anyway.

Let us consider in a very very general sense, an array of instructions in C++ i.e. A[0],A[1],A[2] ..... The "figurative" distance between any two instructions is 1 UNIT.

Lets take this analogy to MIPS. In MIPS, figuratively every instruction is separated by "1 UNIT", however, 1 UNIT = 4 Bytes in MIPS. Every instruction is 4 Bytes long and this is why when moving from instruction to instruction the PC is incremented by 4 i.e. PC+4. So that way the gap between instruction i and instruction i+2 is "figuratively" 2 but actually 2*4=8 i.e. PC+4+4

Coming back to offsets that are specified in Branch instructions, the offset represents the "figurative" distance from the next instruction(the instruction following the Branch). So to get the "real" distance, the offset is to be multiplied by 4. This is the reason we are instructed to "sign-extend" the offset by 2 bits to the 'LEFT', because, left shifting any binary value by n bits results in multiplying that value by 2^n. In our case 2^2 = 4

So the actual target address of a branch instruction is PC+4+4*Offset.

Hope this helps.