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

LUI Decode And Execute Animation

Uses the same rhythm as the ADDI page: machine-code fields, fixed-field identification, operand reads, instruction-specific execution, and ISA-visible state update.

rd
imm20
lui
,
Execution Context
31..12
11..7
6..0
00010010001101000101
imm[31:12]
00101
rd
0110111
opcode
Execution Data Path
instruction
0x123452B7
opcode
0110111 -> LUI
rd
t0(x5)
imm[31:12]
00010010001101000101 << 12 -> 0x12345000
upper load
0x12345000
x5
t0(x5) = 0x12345000
Current Step

Concept Step: receive the 32-bit instruction encoding

The machine code is split by the current instruction format; the animation starts from encoding/decode.

encoding: 0x123452B7
syntax : lui t0(x5), 0x12345
result : t0(x5) = 0x12345000

This animation shows ISA-visible decode and state changes, not any specific CPU pipeline, cache, prediction, or timing implementation.

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.