Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Load 32-bit constant to register in MIPS

Tags:

assembly

mips

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?

like image 806
lucasKo Avatar asked Oct 31 '12 15:10

lucasKo


People also ask

How do you load a 32-bit constant value to the register ARM?

Load 32-bit immediate values to a register using LDR Rd, =const. The LDR Rd,=const pseudo-instruction generates the most efficient single instruction to load any 32-bit number. You can use this pseudo-instruction to generate constants that are out of range of the MOV and MVN instructions.

Are MIPS registers 32-bit?

On the MIPS, a register holds 32 bits. There are many registers in the processor, but only some of them are visible in assembly language. The others are used by the processor in carrying out its operations.


2 Answers

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

like image 133
Paul R Avatar answered Oct 11 '22 04:10

Paul R


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.

like image 21
yaman Avatar answered Oct 11 '22 04:10

yaman