ISA & Assembly
To build the constant 0x12345FFF in RISC-V with lui and addi, which pair is correct?
- Alui t0, 0x12346 followed by addi t0, t0, -1
- Blui t0, 0x12345 followed by addi t0, t0, 0xFFF
- 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.