ISA & Assembly

To build the constant 0x12345FFF in RISC-V with lui and addi, which pair is correct?

  1. Alui t0, 0x12346 followed by addi t0, t0, -1
  2. Blui t0, 0x12345 followed by addi t0, t0, 0xFFF
  3. Clui t0, 0x12345 followed by addi t0, t0, -1
Show answer and explanation

Answer: A. lui t0, 0x12346 followed by addi t0, t0, -1

The addi immediate is sign-extended, so when the low 12 bits are 0xFFF (top bit set) it is added as -1. The upper part built by lui is therefore made one larger, 0x12346, and adding -1 to 0x12346000 yields 0x12345FFF. This correction, adding 1 to the upper part whenever bit 11 of the low part is set, is done automatically by assemblers and linkers when li or %hi/%lo is used.

  • A. Correct. 0x12346000 - 1 = 0x12345FFF.
  • B. 0xFFF is outside the signed 12-bit range, so most assemblers reject it. Taken as the 12-bit value -1, it gives 0x12344FFF.
  • C. That gives 0x12345000 - 1 = 0x12344FFF.