Elmasry
Elmasry

Reputation: 132

MIPS Jump instruction encoding: why left shifted, and why keep the high 4 bits of PC?

In the Jump instruction,

  1. why do we shift 26-bit address to 28-bit?
  2. why do we add the leftmost 4-bit from PC to the 28-bit?

Upvotes: 4

Views: 3482

Answers (1)

Martin Rosenau
Martin Rosenau

Reputation: 18493

Why do we shift 26-bit address to 28-bit?

When we shift the address by 2 bits, the argument (address) of the jump instruction can be in the range 0...2^28-1.

If we didn't shift the address, it could only be in the range 0...2^26-1.

This means we could only use 1/4 of the address space.

On the other hand, the apperent benefit of not shifting the address would not really be a benefit:

Not shifting the address would allow using addresses which are not divisible by 4. However, because instructions are always located at addresses which are divisible by 4, a jump instruction to an address not divisible by 4 makes no sense.

By the way: Other CPUs (e.g. MC68000) indeed use 16-bit "branch" (jump) instructions where the low bit always has to be zero - and therefore more memory could be addressed if the CPU shifted the address by one.

why do we add the leftmost 4-bit from PC to the 28-bit?

The PC register is 32 bits wide and the jump instruction contains only 26 bits. So we have to take 6 bits from somewhere else:

The low 2 bits of the PC register are always zero, so we still have to think about the "left" 4 bits.

If we would always set the left 4 bits to zero, we could only jump to some code located inside the first 256 Megabytes of memory.

If we simply don't modify the the left 4 bits of the PC, we can jump to some code which is located in the same 256 Megabytes range as the jump instruction itself.

Let's think about a for() or while() loop:

At the end of such a loop there is a jump instruction to the start of the loop.

Let's assume that the program is not neccessarily located in the first 256 Megabytes of memory.

What is more likely:

That the start of the loop is in the same 256-Megabytes-range as the end of the loop (the jump instruction) or that the start of the loop is inside the first 256 Megabytes of memory?

Upvotes: 5

Related Questions