10

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?

lucasKo
  • 175
  • 2
  • 3
  • 10

4 Answers4

10

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.)

phuclv
  • 37,963
  • 15
  • 156
  • 475
Paul R
  • 208,748
  • 37
  • 389
  • 560
  • 1
    Thank you! Because of the fixed length, the immediate format leaves only 16 bit for constant, other 16 bits for the operation and register code. – lucasKo Nov 01 '12 at 13:58
  • Another question regarding combining "lui", "ori" and "or" to load a large constant into a register. 1. Why would you do this rather than just computing the value with add-operations and save it into a register? 2. Also, how do you use a constant bigger than 26 bits? A j-type instruction takes only a 26 bit constant. – Energizer1 Dec 22 '19 at 17:32
6

Yes registers are 32-bits but how can you specify a 32-bit number in an instruction that is 32 bits? An instruction consists of opcode and data. So, you can't squeeze an opcode + 32-bit data in a single addi.

yaman
  • 759
  • 3
  • 17
1

Alternatively, using .data and .text

.data
   word32bits: .word 0xFFFFFFFF

.text
   lw $t0, word32bits # $t0 now contains your 32 bit word.
Rafael Nagel
  • 23
  • 1
  • 5
0

You can use literal pool if you need to load a lot of constants. Then each constant load costs only 1 load instruction. More information here and here

Community
  • 1
  • 1
phuclv
  • 37,963
  • 15
  • 156
  • 475