Chris Low-level systems developer
← Back to all posts

The Branch That Inverts the Address Map

ISA

The claim is the RV64I branch contract: blt compares its operands as signed two's-complement integers and bltu as unsigned. The run issues both branches on the same register pairs and reads back opposite taken/not-taken verdicts, proving the signedness lives in the instruction.

MICROARCH

No claim depends on how any core implements anything. No cache, pipeline, or timing behavior is asserted or measured; QEMU serves as a functional executor only, and the branch contract is complete from the instruction semantics and the run.

FIRMWARE

No firmware service takes part in the claim. The binary runs in M-mode on bare QEMU with no SBI or firmware calls; the behavior under test is each branch instruction's own contract.

KERNEL

No kernel is involved at any step. The test binary runs on bare QEMU with no operating system, and no system interface appears in the argument.

ALGORITHM

The claim is one fixed instruction semantic exercised as fixed branch pairs with readback, not a computation proved against an independent oracle. The range-check habit is shown as the consequence the contract forces, not as a verified algorithm.

Two addresses sit in registers: 0x8000000000000000 and 0x0000000000001000. Two branch instructions compare the same pair, one after the other, on the same hart. The UART printed the verdicts: blt took the branch, bltu fell through. Same registers, same order, opposite answers. [1]

The difference is the signedness of the compare, and it lives in the instruction, not the data. blt reads its operands as two’s-complement signed integers: 0x8000...0000 is the most negative 64-bit value, which is less than 0x1000, so the branch is taken. bltu reads the same bits as unsigned: 0x8000...0000 is the larger address, so the branch is not taken. One letter in the mnemonic flips the meaning of every bit pattern with the top bit set. [1] [2]

The two branches, reduced to their verdicts:

blt  s0, s1, taken   # signed:   0x8000000000000000 < 0x1000, branch taken
bltu s0, s1, taken   # unsigned: 0x8000000000000000 > 0x1000, falls through
Blueprint-style diagram of the blt/bltu signedness split: a vertical 64-bit address map with 0x0000000000001000 near the bottom, 0x8000000000000000 at the bit-63 midpoint, and 0xFFFFFFFFFFFFF000 at the top; beside it, the blt box reading 0x8000000000000000 as negative 9,223,372,036,854,775,808 with verdict BRANCH TAKEN, and the bltu box reading it as positive 9,223,372,036,854,775,808 with verdict FALLS THROUGH
Same two registers, opposite verdicts. blt reads the pair as signed and takes the branch; bltu reads it as unsigned and falls through.
Diagram: hand-built by Chris Dillinger for this post

The run behind it. A bare-metal M-mode program on QEMU 8.2.2 issued both branches on three register pairs: (0x8000000000000000, 0x1000), the reverse, and a high-half address 0xfffffffffffff000 against 0x1000. The disassembly shows the real instructions: blt at 0x80000292, bltu at 0x800002ca, the high-half pair at 0x80000304 and 0x80000320. Six checks, zero mismatches. The high-half pair is the one that matters: blt said 0xfffffffffffff000 was below 0x1000 (taken), bltu said it was above (not taken). [1]

The bench habit. Address ordering is unsigned, always: a range check addr < LIMIT has to use the unsigned compare. In C that means the unsigned type; the compiler listing for this run showed the same fork one level down, slt for the long version and sltu for the unsigned long version. The trap is the signed cast: the moment an address becomes a signed long, a bounds check inverts the map above bit 63, and a kernel-space address passes a "below the limit" test it should fail. The habit: if the value is an address, the compare is unsigned, no exceptions. [2]

Sources

  1. 01
    src/blt-bltu-addr: module PROOF.md and the firsthand runChris Dillinger

    The firsthand QEMU run behind the post: blt and bltu issued on (0x8000000000000000, 0x1000), the reverse pair, and (0xfffffffffffff000, 0x1000); blt took the branch wherever the signed reading is less, bltu wherever the unsigned reading is less. Six checks, zero mismatches, QEMU 8.2.2. The compiler listing shows slt for the long compare and sltu for the unsigned long compare.

  2. 02
    RISC-V ISA Manual, unprivileged architecture (branch instructions)RISC-V International

    The branch contract, read from the pinned release: 'BLT and BLTU take the branch if rs1 is less than rs2, using signed and unsigned comparison respectively.' The manual itself blesses the applied habit: 'Signed array bounds may be checked with a single BLTU instruction, since any negative index will compare greater than any nonnegative bound.'

Related work: kernel and driver correctness work, fixed price. .

Choose how to get in touch

The project brief is filled in for you.

Compose in Gmail Use my email app