Load 32-bit constant to register in MIPS

lucasKo picture lucasKo · Oct 31, 2012 · Viewed 19.1k times · Source

I was confused about this part while I study MIPS.

The textbook written by Professor John L. Hennessy say if we get some big constant to load, we should

lui $s0, upper(big)
ori $s0, $s0, lower(big)

But why don't we just do

addi $s0, $zero, big

Since the registers are 32-bit, this is more strightforward, isn't it?

Answer

Paul R picture Paul R · Oct 31, 2012

The immediate argument passed to addi is only 16 bits. To load a 32-bit immediate value that is outside the range of a 16-bit value you need to do it in two goes, as in the example from your text book.

(Anticipating a further question, the reason there is no load immediate or add immediate instruction which takes a 32-bit immediate value is because the MIPS ISA uses fixed size 32-bit instructions, so there are always < 32 bits available for any instruction arguments - this is very common in RISC architectures.)