Chris Low-level systems developer
← Back to all posts

The Twelve Bits That Borrow From the Twenty

ISA

The claim is about the RISC-V instruction encoding itself: lui carries a 20-bit immediate into bits 31:12 and addi carries a 12-bit signed immediate into bits 11:0, and that signedness forces the assembler's rounding. The listings show the exact two encodings the split produces, read straight off objdump.

MICROARCH

No claim depends on how any core implements anything. No cache, pipeline, or timing behavior is asserted or measured anywhere in the argument; the rounding is an assembler decision read off the listings, before any hardware runs.

FIRMWARE

No boot code, machine-mode service, or privilege handoff takes part. The experiment is freestanding assembly with no runtime, and the argument never enters firmware.

KERNEL

No kernel is involved at any step. The listings come from assembling a bare function with no operating system, and no system interface appears in the argument.

ALGORITHM

The claim is a fixed assembler convention, not a computation. Nothing is proved against an independent oracle; the recombination arithmetic only checks the listings against themselves.

I asked the assembler for one constant this morning: li a0, 0x12345EEF, assembled for rv64g with the 13.2.0 toolchain. Two instructions came back: lui a0, 0x12346, then addiw a0, a0, -273. The constant's top twenty bits are 0x12345, not 0x12346, and its bottom twelve are 0xEEF, not -273. Neither half of the pair looks like the constant it builds. [1]

The split point is fixed by the encoding: lui carries twenty bits into positions 31:12, and addi carries twelve bits into positions 11:0. The catch is that addi's twelve bits are signed, -2048 to 2047, while the constant's low twelve bits are unsigned, 0 to 4095. When the low twelve reach 0x800, bit 11 set, no signed twelve-bit field can hold them: 0xEEF is 3823, past the 2047 limit. So the assembler rounds the high twenty up by one, 0x12345 becomes 0x12346, and writes the low part as the signed remainder, 0xEEF minus 0x1000, which is -273. Recombined: 0x12346000 minus 273 is 0x12345EEF. [2]

The edges confirm the rule. li a0, 0x7FF needs no lui at all: 2047 fits the signed immediate, so a single addi does the whole job. li a1, 0x800 is the smallest constant that needs the pair, and the rounding shows at its barest: lui a1, 0x1, then addiw a1, a1, -2048. The lui shows 0x1 for a constant whose top twenty bits are 0x0. And li a2, 0x12345678 needs no rounding: its low twelve bits are 0x678, bit 11 clear, so the pair is the naive split, lui a2, 0x12345 with addiw a2, a2, 1656. [1]

The two interesting pairs, reduced to the instructions:

# bit 11 of the low 12 clear: the naive split
    lui   a2, 0x12345
    addiw a2, a2, 1656          # 0x12345678
# bit 11 set: the lui rounds up, the low part goes negative
    lui   a3, 0x12346           # 0x12345 + 1
    addiw a3, a3, -273          # 0xEEF - 0x1000
Blueprint-style diagram of a 32-bit constant split into high 20 and low 12 bits, showing the lui rounding up by one when bit 11 is set and the addi carrying the signed remainder -273
One constant, two instructions. Bit 11 of the low twelve is set, so the lui carries 0x12346 instead of 0x12345, and the addi carries -273 instead of 0xEEF.
Diagram: hand-built by Chris Dillinger for this post

Two habits fall out of this. Reading a disassembly: when the addi after a lui is negative, the constant's true high twenty bits are one less than the lui shows; the value is the lui shifted left twelve plus the signed immediate, and treating the immediate as unsigned puts you a full 0x1000 off. Writing assembly by hand, in startup code or a trap handler: add one to the lui whenever the low twelve bits are 0x800 or more, because the naive pair does not assemble at all. I tried it: lui a0, 0x12345 followed by addi a0, a0, 0xEEF dies with "illegal operands", since 0xEEF cannot fit a signed twelve-bit field. The assembler forces the rounding; the only choice is whether you do it on purpose. [1]

Sources

  1. 01
    lab/163-lui-addi-rounding: the lui/addi listingsChris Dillinger

    The firsthand objdump listings from riscv64-unknown-elf-gcc 13.2.0 (-march=rv64g): 0x7FF as a single addi, 0x800 as lui 0x1 plus addiw -2048, 0x12345678 as the naive pair, 0x12345EEF as lui 0x12346 plus addiw -273, and the assembler's illegal-operands rejection of the naive hand-written pair.

  2. 02
    RISC-V ISA Manual, unprivileged architectureRISC-V International

    The I-type encoding: the 12-bit immediate is sign-extended, so addi's range is -2048..2047; the U-type encoding: lui places its 20-bit immediate in bits 31:12. Together they fix the split point and force the rounding the post demonstrates.

Stuck on a board or a crash? Fixed-price firmware work: crash triage $250/symptom, bare-metal bring-up from $500, C audit from $350. .

Choose how to get in touch

The project brief is filled in for you.

Compose in Gmail Use my email app