lipseudo

li

pseudo
RISC-V Assembly Reference

lyszt.net covers li and other RISC-V instructions in more detail.

Description

li loads a constant number into a register. The name stands for load immediate, and an immediate is a fixed value written directly in the code. The next line stores 42 in t0.

li t0, 42

Most programs start by placing known numbers into registers, including counters, limits and service codes.

The word load comes from a hardware limit. A RISC-V instruction is 32 bits long and must carry the operation, the registers and the constant together. One instruction cannot hold a full 32-bit constant. Small constants fit and large ones do not.

li works around that limit. It is a pseudo-instruction, a name the assembler replaces with whatever real instructions the value requires. A small value needs one instruction. A large value needs two, one that sets the upper portion and one that adds the lower portion, so the next line works for a number of any size.

li t1, 0x10010000

The simulator displays the real instructions that li expands into.

The RISC-V manual describes lui as the instruction for building 32-bit constants. It places a 20-bit value in the upper bits of the destination register and fills the lowest 12 bits with zeros.

li and la load different things. li loads a number you typed and la loads the address that a label stands for. The example loads a small value and a large one, and the assembler handles both the same way.

Expands to

addi rd, zero, imm          # small constants
lui rd, hi  +  addi rd, rd, lo   # large constants

The assembler replaces this pseudo-instruction with the real instruction or instructions listed above.

Example

li t0, 42
li t1, 0x10010000