Registers and Immediates
Course: Computer Architectures
Type: Concept
Definition
A register is a CPU storage location. In RV32, a general-purpose register holds 32 bits.
An immediate is a constant encoded directly inside an instruction.
addi
addi rd, rs1, immmeans:
rd = rs1 + immThe immediate in addi is a signed 12-bit value:
-2048 ... +2047The 12-bit restriction applies to the immediate, not to the value already stored in rs1.
lui
lui rd, imm20places the 20-bit immediate into bits 31:12 and fills the lower 12 bits with zero.
Example:
lui x5, 0x12345produces:
x5 = 0x12345000Think:
lui imm20 = imm20 << 12Why li can become lui + addi
li is a pseudo-instruction.
For a small value:
li x5, 100can become:
addi x5, x0, 100For a larger value, the assembler may use:
lui x5, upper
addi x5, x5, lowerThe 0x800 case
For a signed 12-bit immediate:
0x7FF = +2047
0x800 = -2048
0xFFF = -1So:
li x5, 0x1800can be formed as:
lui x5, 0x2
addi x5, x5, -0x800because:
0x2000 - 0x0800 = 0x1800