Skip to content
BIASED

RISC-V instructions and their encoding

Block 1 of 5~2 min

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; x[r]x[r] is register rr; sext⁡\operatorname{sext} sign-extends; arithmetic is mod 2322^{32}. w[a:b]w[a:b] is bits aa down to bb.

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 t0t0 is huge. 216⋅216=2322^{16}\cdot 2^{16}=2^{32}: mul returns 0 while mulhu returns 1.

Answers are saved on the server and tied to this browser until you sign in.