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, imm

means:

rd = rs1 + imm

The immediate in addi is a signed 12-bit value:

-2048 ... +2047

The 12-bit restriction applies to the immediate, not to the value already stored in rs1.

lui

lui rd, imm20

places the 20-bit immediate into bits 31:12 and fills the lower 12 bits with zero.

Example:

lui x5, 0x12345

produces:

x5 = 0x12345000

Think:

lui imm20 = imm20 << 12

Why li can become lui + addi

li is a pseudo-instruction.

For a small value:

li x5, 100

can become:

addi x5, x0, 100

For a larger value, the assembler may use:

lui  x5, upper
addi x5, x5, lower

The 0x800 case

For a signed 12-bit immediate:

0x7FF = +2047
0x800 = -2048
0xFFF = -1

So:

li x5, 0x1800

can be formed as:

lui  x5, 0x2
addi x5, x5, -0x800

because:

0x2000 - 0x0800 = 0x1800