lui
lyszt.net covers lui and other RISC-V instructions in more detail.
Encoding format
Description
lui assembles large constants. A RISC-V instruction occupies 32 bits, and those bits carry the operation, its registers, and any constant. A normal instruction can hold roughly 12 bits of constant, which covers small numbers but falls short of a full 32-bit value. Several instructions supply the pieces of a large constant.
lui (load upper immediate) supplies the top piece. It takes a 20-bit constant and writes it into the upper 20 bits of the destination register, leaving the low 12 bits at zero. The form appears below.
lui rd, value
A full constant needs two instructions. lui sets the high 20 bits, and addi adds the low 12. The li pseudo-instruction performs this work for you, so hand-written lui is rarely needed for ordinary numbers.
The RISC-V manual states that the assembly syntax of lui and auipc omits the lower 12 bits of the constant, because those bits are always zero. It also states that a lui followed by jalr can reach any 32-bit absolute address: lui loads the upper 20 bits of the target address and jalr adds the lower bits.
lui appears directly when loading round addresses whose low bits are zero, such as the fixed addresses of hardware devices, where a single lui supplies the whole value. Its close relative is auipc, which builds an address relative to the current location.
Example
lui t0, 0x10 # t0 = 0x10000 (0x10 << 12)
