Home/Instructions/Load Upper Immediate
LUI

RISC-V LUI Instruction Details

Instruction ManualU-type

Load a 20-bit immediate into the upper 20 bits of rd, filling the lower 12 bits with zeros

Instruction Syntax

lui rd, imm
Operand Breakdown
Destination rd: register receiving the result.
Upper immediate: 20-bit value loaded into bits [31:12] of rd. Lower 12 bits are zero-filled.
RV32IImmediates & ConstantsAddress Computation

Instruction Encoding

31..12
imm[31:12]
11..7
rd
6..0
opcode

LUI uses opcode 0110111 (0x37). The 20-bit immediate is loaded into the upper 20 bits of rd, with lower 12 bits zero-filled.

Format: U-type
opcode: 0110111 (0x37)

Instruction Behavior

LUI (U-type, opcode=0110111) places the 20-bit U-immediate value into the destination register rd, filling the lowest 12 bits with zeros. Used to build 32-bit constants, typically paired with ADDI. When rd=x0, the encoding is reserved for HINT instructions.

Quick Understanding & Search Notes

LUI places the U-immediate into the upper portion of rd and clears the low 12 bits, commonly as the first step when forming larger constants.

The U-type immediate corresponds to the upper 20 bits of the result; the low 12 bits are zero.
On RV64I, the 32-bit LUI result is sign-extended to 64 bits.

Official Spec Notes

These notes are checked against the RISC-V Unprivileged ISA manual and summarize operation semantics, immediate ranges, and edge behavior.

Common Usage Scenarios

Immediates & Constants

Understand this scenario with real code like «lui x5, 0x12345 # x5 = 0x12345 << 12 = 0x12345000».

Pre-Use Checklist

Syntax Check
  • Confirm the current instruction format is U-type.
  • Confirm the operand order matches the example.
Semantic Check
  • Ensure the destination register usage is compatible with the calling convention.
  • Confirm this is not the lower-level form of a pseudo-instruction expansion.

Pitfalls / Common Confusions

Lower 12 bits are always zero, not displayed in assembly
When rd=x0, reserved for HINT instructions

FAQ

Can LUI load a full 32-bit constant by itself?

No. LUI sets the upper 20 bits and clears the low 12 bits; a full constant usually needs a following instruction such as ADDI.

What is the key difference between LUI and AUIPC?

LUI forms a value from the immediate alone; AUIPC adds the upper-immediate offset to the current PC.