ISA & Assembly

In RISC-V, what is the usual way to put a 32-bit constant that does not fit in a 12-bit immediate (e.g. 0x12345678) into a register?

  1. AUse a single addi with the whole 32-bit immediate
  2. BSet the upper 20 bits with lui, then add the lower 12 bits with addi
  3. CConstants cannot be used, so the value must always be read from input
Show answer and explanation

Answer: B. Set the upper 20 bits with lui, then add the lower 12 bits with addi

lui (load upper immediate) places a 20-bit immediate in bits 31-12 of a register and clears the low 12 bits. Following it with addi to add the low 12 bits produces any 32-bit constant. Writing "li a0, 0x12345678" in assembly makes the assembler expand it into these two instructions as needed. In fixed-length ISAs, building large constants in pieces is the norm.

  • A. A 32-bit immediate cannot fit in a 32-bit instruction.
  • B. Correct. The li pseudo-instruction expands to this pair.
  • C. Two instructions build it, or it can be loaded from memory.
Practice this topic Back to topic

More ISA & Assembly questions

  1. To build the constant 0x12345FFF in RISC-V with lui and addi, which pair is correct?
  2. What does the RISC-V auipc instruction (add upper immediate to PC) do?
  3. On RISC-V RV32I, what 32-bit value is in t0 after "addi t0, zero, -1"?
  4. A byte in memory holds 0x80. Which pair correctly gives the 32-bit register value after loading it with RISC-V lb and with lbu?
  5. How do RISC-V and x86 differ in their mechanism for conditional branches?