Chris Low-level systems developer
← Back to all posts

The Load That Sign-Extended

ISA

The claim is the RV64I load contract: lw sign-extends the 32-bit value to 64 bits while lwu zero-extends it. The run executes both loads on the same word and reads back 0xffffffff80000001 and 0x0000000080000001, proving the extension half of each instruction by register readback.

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 extension 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 load 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 two fixed loads with readback, not a computation proved against an independent oracle. The == consequence is shown as the habit the contract forces, not as a verified algorithm.

A 32-bit word sits in a scratch buffer holding 0x80000001, its top bit set. Two loads read that same word, one instruction apart, on the same hart. The UART printed 0xffffffff80000001 for the first and 0x80000001 for the second. Same word, same address, two different registers, two different values. [1]

The difference is the extension. lw loads the 32-bit value and sign-extends it to 64 bits: bit 31 is copied into every bit from 32 through 63. lwu loads the same 32 bits and zero-extends: the upper half is zeros. The manual states both in one breath, and the run shows them side by side. [1] [2]

The two loads, reduced to their results:

lw  t0, 0(s0)   # t0 = 0xffffffff80000001 : bit 31 copied upward
lwu t1, 0(s0)   # t1 = 0x0000000080000001 : bits 63..32 are zero
Blueprint-style diagram of the lw/lwu extension behavior: a 32-bit memory word holding 0x80000001 with bit 31 highlighted as the sign bit; below, the 64-bit result of lw t0, 0(s0) with bits 63 through 32 all ones, and of lwu t1, 0(s0) with bits 63 through 32 all zeros; at the bottom, t0 == 0x80000001 marked FALSE and t1 == 0x80000001 marked TRUE
One word, two loads. lw fills the upper 32 bits with copies of bit 31; lwu fills them with zeros.
Diagram: hand-built by Chris Dillinger for this post

The run behind it. A bare-metal M-mode program on QEMU 8.2.2 stored 0x80000001 with sw, then issued the lw and the lwu back to back and read both registers off the UART: lw -> 0xffffffff80000001, lwu -> 0x80000001, the same as 0x0000000080000001. The disassembly shows the real instructions: sw at 0x80000204, lw at 0x80000206, lwu at 0x80000208. Four checks, zero mismatches: both exact values, plus the comparison consequence below. [1]

The bench habit. A 32-bit status word is usually a bit pattern, not a signed number, so the sign-extended upper half is garbage you never asked for. The classic failure is silent: t0 == 0x80000001 is false after the lw and true after the lwu, and nothing in the code warns you. The habit: when the value is 32 bits of flags, reach for lwu (or ld when it is really 64 bits), and never trust a bare == on a register you did not fill yourself. [2]

Sources

  1. 01
    src/load-sign-ext: module PROOF.md and the firsthand runChris Dillinger

    The firsthand QEMU run behind the post: sw stored 0x80000001 to the scratch word; lw s1,0(a0) returned 0xffffffff80000001; lwu s0,0(a0) returned 0x0000000080000001; the lw result fails == against the 32-bit pattern while the lwu result matches. Four checks, zero mismatches, QEMU 8.2.2.

  2. 02
    RISC-V ISA Manual, unprivileged architecture (RV64I loads)RISC-V International

    The load contract, read from the pinned release: 'The LW instruction loads a 32-bit value from memory and sign-extends this to 64 bits before storing it in register rd for RV64I. The LWU instruction, on the other hand, zero-extends the 32-bit value from memory for RV64I.'

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