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.

  • A. Correct. 0x12346000 - 1 = 0x12345FFF.
  • B. The 12-bit value 0xFFF is sign-extended and added as -1.
  • C. That gives 0x12345000 - 1 = 0x12344FFF.
Practice this topic Back to topic

More ISA & Assembly questions

  1. What does the RISC-V auipc instruction (add upper immediate to PC) do?
  2. On RISC-V RV32I, what 32-bit value is in t0 after "addi t0, zero, -1"?
  3. 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?
  4. How do RISC-V and x86 differ in their mechanism for conditional branches?
  5. What does the RISC-V instruction "slt t0, a0, a1" do?