RISC-V instructions and their encoding
RV32I/M arithmetic and logic, six formats with fixed register fields, encoding `sub` and `addi` by hand, scrambled store/branch immediates, and `lui`+`addi` for any 32-bit constant.
Arithmetic, logic and shifts
The previous topic ran programs and treated instructions as mnemonics. The processor sees 32-bit words — 0x006283b3 for add t2, t0, t1, or the auipc/addi pair from la.
Notation. rd, rs1, rs2, imm; is register ; sign-extends; arithmetic is mod . is bits down to .
Baseline. Laying each instruction out as written fails twice: a 32-bit constant never fits beside an opcode, and if rs1 moves between formats the decoder cannot start the register-file read until it knows the instruction — that delay hits every op. RISC-V answers with fixed register field positions and a separate recipe for wide constants.
RV32I compute ops (register–register and register–immediate): add/addi, sub, and/andi, or/ori, xor/xori, shifts sll/slli, srl/srli, sra/srai, compares slt/slti, sltu/sltiu. Immediate is a sign-extended 12-bit constant (−2048…2047). No subi: use negative addi. Shift amount = low 5 bits (sll with amount 33 shifts by 1). No flags; no overflow traps. M adds mul (low 32 of the 64-bit product), mulh/mulhu/mulhsu, div/divu (toward zero), rem/remu. Pseudos: mv, neg, not=xori rd,rs,-1, seqz=sltiu rd,rs,1, snez=sltu rd,zero,rs.
Worked. t0=-20 (0xffffffec). srai by 2 → −5; srli → 0x3ffffffb (large positive); slli by 2 → −80. slti vs 1 yields 1; sltiu yields 0 because unsigned is huge. : mul returns 0 while mulhu returns 1.
Six formats
Key idea. Every RV32I instruction is one 32-bit word in one of six formats. Opcode, rd, rs1, rs2, funct3, funct7 sit in fixed bit positions whenever present, so register reads can start before the instruction is fully known. The immediate takes the leftover bits, with its sign always in bit 31.
| Format | Typical use |
|---|---|
| R | add, sub, sll, … |
| I | addi, loads, jalr |
| S | stores |
| B | conditional branches |
| U | lui, auipc |
| J | jal |
Opcode is always bits 6–0; funct3 bits 14–12; when present, rs1 is 19–15, rs2 24–20, rd 11–7.
Encoding by hand
Example 1. sub a0, a1, a2: funct7=0100000, rs2=12, rs1=11, funct3=000, rd=10, opcode=0110011 → 0x40c58533.
Example 2. addi sp, sp, -16: imm12=111111110000 (4080 = ), rs1=rd=2, funct3=000, opcode=0010011 → 0xff010113. Execution sign-extends to 0xfffffff0.
Example 3. lw a0, 8(sp): I-format load, funct3=010, imm=8, rs1=sp, rd=a0, opcode=0000011 → 0x00812503.
Scrambled immediates
S and B formats still need two source registers, so the immediate is split around rs2/rs1/funct3 to keep those fields fixed. Branches store a multiple of 2 (low bit implicit 0); range ±4 KiB. jal reaches ±1 MiB with its own scramble of a 20-bit offset. Decoding B/J by eye means unshuffling bits — bit 31 is still the sign, but bit 11 of a branch sits where S puts imm[4:0]’s top, and so on.
Example. bne t0, t1, -12 at the end of a four-instruction loop: offset −12 encoded in the scrambled B layout. The hardware reassembles the offset, sign-extends, and adds it to pc if the condition holds.
Loading constants
A 32-bit constant cannot fit beside an opcode in one word. Recipe: lui rd, hi writes the top 20 bits in positions 31–12; addi rd, rd, lo adds a signed 12-bit piece. If bit 11 of the desired low part is set, addi would subtract — so assemblers bump hi by 1 (sign adjustment). Example: loading 0x12345ABC needs lui of 0x12346 and addi of -1348 (0xABC with bit 11 set), not a naive split. auipc adds imm<<12 to PC — how la builds addresses without absolute relocation, and how call reaches far procedures.
Small constants use one addi rd, zero, imm (alias li) when they fit in −2048…2047. Combining lui+addi with the six formats is enough to express every RV32I instruction the assembler emits.
Why scramble immediates at all? Keeping rs1/rs2/rd glued to fixed bit slices lets the register-file ports wire to constant bit ranges in every format that uses them. The cost is human readability of hex dumps: 0x006283b3 is recognisably an R-type (op=0110011), but a branch’s offset bits are not contiguous in the word. Practice: given 0xff010113, read opcode 0010011 (OP-IMM), rd=sp, funct3=000 (addi), rs1=sp, imm12 with high bit set → −16. That single decode habit transfers to reading compiler output and to writing relocatable object dumps later in linking.
Unsigned vs signed compares show up again in address arithmetic: checking that a pointer p lies in [lo, hi) is often bgeu / bltu on the differences, because a wrapped unsigned distance catches both ends. Multiplication overflow detection uses mulh/mulhu beside mul: if the high half is not a pure sign- or zero-extension of the low half, the mathematical product did not fit in 32 bits.
Store immediates scramble the same way as branches but without the implicit low zero: sw t0, 12(sp) places imm[11:5] in bits 31–25 and imm[4:0] in bits 11–7 around rs2. Once you can encode addi/sub/lw/sw by hand, every other RV32I op is the same wiring with a different funct3/funct7.
Next: Branches, loops and memory access — changing pc and moving bytes between registers and memory.
Check your understanding
The same questions as in Pulse. Answers are shared between modes.
Answers are saved on the server and tied to this browser until you sign in.