Understanding the RISC-V RV32I base integer instruction set
RISC-V is an open, modular instruction set architecture (ISA) developed at the University of California, Berkeley. In modern US undergraduate computer science and computer engineering curricula, including courses like Berkeley CS 61C, MIT 6.1910, and Stanford EE 108, RISC-V has largely superseded MIPS and x86 as the primary pedagogical architecture. Its base integer variant, RV32I, defines a minimal, clean, 32-bit load-store architecture with 32 general-purpose registers (x0 to x31) and fewer than 40 core instructions.
Unlike variable-length CISC architectures like x86-64 where instructions range from 1 to 15 bytes, every RV32I instruction is exactly 32 bits (4 bytes) long and must be aligned on a 4-byte boundary in memory. Every instruction falls into one of six regular formats: R-type, I-type, S-type, B-type, U-type, or J-type.
The six core instruction formats: why register positions never change
A central engineering principle of RISC-V is simplicity of hardware decoding. In high-performance pipelined microarchitectures, control logic must decode instructions and read operands within a single clock cycle. RISC-V accomplishes this by pinning the source and destination register fields to the exact same bit positions across all formats:
- Destination register (rd): bits [11:7] in R, I, U, and J formats.
- Source register 1 (rs1): bits [19:15] in R, I, S, and B formats.
- Source register 2 (rs2): bits [24:20] in R, S, and B formats.
- Opcode: bits [6:0] across all formats.
Because rs1 and rs2 always reside in bits [19:15] and [24:20], a hardware processor can access the dual-port register file immediately, in parallel with decoding the opcode and function fields.
1. R-type: register-to-register arithmetic and logic
R-type instructions take two source registers (rs1, rs2) and write the computation result into destination register rd.
- Bit layout: funct7 [31:25] | rs2 [24:20] | rs1 [19:15] | funct3 [14:12] | rd [11:7] | opcode [6:0]
- Worked example: add x1, x2, x3
- Assembly: add x1, x2, x3 (adds sp and gp, stores to ra)
- rd = x1 (00001), rs1 = x2 (00010), rs2 = x3 (00011)
- funct7 = 0000000 (0x00), funct3 = 000 (0x0), opcode = 0110011 (0x33)
- Binary fields: 0000000 00011 00010 000 00001 0110011
- Hex machine code: 0x003100B3
2. I-type: immediate arithmetic, memory loads, and register jumps
I-type instructions operate on one register operand (rs1) and a 12-bit signed immediate constant (imm[11:0]), saving the result to rd. The immediate value is sign-extended to 32 bits, covering values from -2048 to +2047.
- Bit layout: imm[11:0] [31:20] | rs1 [19:15] | funct3 [14:12] | rd [11:7] | opcode [6:0]
- Worked example (ALU): addi x5, x4, 5
- Assembly: addi x5, x4, 5 (adds 5 to thread pointer tp, writes to temporary t0)
- imm = 5 (000000000101), rs1 = x4 (00100), funct3 = 000 (0x0), rd = x5 (00101), opcode = 0010011 (0x13)
- Binary fields: 000000000101 00100 000 00101 0010011
- Hex machine code: 0x00520293
- Worked example (Load): lw x10, 8(x2)
- Assembly: lw a0, 8(sp) (loads 32-bit word from stack memory address sp + 8 into argument register a0)
- imm = 8 (000000001000), rs1 = x2 (00010), funct3 = 010 (0x2), rd = x10 (01010), opcode = 0000011 (0x03)
- Binary fields: 000000001000 00010 010 01010 0000011
- Hex machine code: 0x00812503
3. S-type: memory store instructions
Store instructions read the base address from rs1 and the data to store from rs2. Because stores produce no register result, they require no rd field. RISC-V splits the 12-bit immediate across bits [31:25] and [11:7] so that rs1 and rs2 remain in their fixed bit positions.
- Bit layout: imm[11:5] [31:25] | rs2 [24:20] | rs1 [19:15] | funct3 [14:12] | imm[4:0] [11:7] | opcode [6:0]
- Worked example: sw x1, 12(x2)
- Assembly: sw ra, 12(sp) (stores return address ra to memory address sp + 12)
- imm = 12 (0b000000001100): imm[11:5] = 0000000, imm[4:0] = 01100
- rs2 = x1 (00001), rs1 = x2 (00010), funct3 = 010 (0x2), opcode = 0100011 (0x23)
- Binary fields: 0000000 00001 00010 010 01100 0100011
- Hex machine code: 0x00112623
4. B-type: conditional branches and scrambled immediates
Branch instructions compare rs1 and rs2 and branch to a relative PC offset if the condition holds. Branches address halfword (2-byte) boundaries, so bit 0 is implicitly 0 and not stored in the machine word. The remaining 12 bits form a 13-bit signed offset (-4096 to +4094 bytes).
- Bit layout: imm[12] [31] | imm[10:5] [30:25] | rs2 [24:20] | rs1 [19:15] | funct3 [14:12] | imm[4:1] [11:8] | imm[11] [7] | opcode [6:0]
- Why are immediate bits scrambled? In hardware, keeping the sign bit at bit 31 across all formats (I, S, B, and J) enables instant sign-extension before instruction type decode completes.
- Worked example: beq x1, x2, 16
- Assembly: beq ra, sp, 16 (branches forward 16 bytes if ra equals sp)
- imm = 16 (0b0000000010000): imm[12] = 0, imm[10:5] = 000000, imm[4:1] = 1000, imm[11] = 0
- rs2 = x2 (00010), rs1 = x1 (00001), funct3 = 000 (0x0), opcode = 1100011 (0x63)
- Binary fields: 0000000 00010 00001 000 10000 1100011
- Hex machine code: 0x00208863
5. U-type: upper immediate constants
U-type instructions provide 20-bit immediate values to build large 32-bit constants or compute PC-relative addresses (lui and auipc).
- Bit layout: imm[31:12] [31:12] | rd [11:7] | opcode [6:0]
- Worked example: lui x5, 0x12345
- Assembly: lui t0, 0x12345 (loads upper 20 bits into t0, resulting in 0x12345000)
- imm[31:12] = 0x12345 (00010010001101000101), rd = x5 (00101), opcode = 0110111 (0x37)
- Binary fields: 00010010001101000101 00101 0110111
- Hex machine code: 0x123452B7
6. J-type: unconditional jump and link
J-type instructions (jal) perform procedure calls by saving the next sequential instruction address (PC + 4) into destination register rd (usually x1 / ra) and branching to a 21-bit signed PC-relative target offset (-1048576 to +1048574 bytes, or plus or minus 1 MB).
- Bit layout: imm[20] [31] | imm[10:1] [30:21] | imm[11] [20] | imm[19:12] [19:12] | rd [11:7] | opcode [6:0]
- Worked example: jal x1, 2048
- Assembly: jal ra, 2048 (calls subroutine 2048 bytes forward, saves return address in ra)
- imm = 2048 (bit 11 is 1): imm[20] = 0, imm[10:1] = 0, imm[11] = 1, imm[19:12] = 0
- rd = x1 (00001), opcode = 1101111 (0x6F)
- Binary fields: 00000000000100000000 00001 1101111
- Hex machine code: 0x001000EF
Standard register conventions and ABI names
While machine hardware only indexes registers as numbers 0 through 31, assembly programmers and compilers rely on standard Application Binary Interface (ABI) names:
- x0 (zero): Hardwired constant 0. Writes are discarded.
- x1 (ra): Return address for procedure calls, saved by caller.
- x2 (sp): Stack pointer, saved by callee.
- x3 (gp) and x4 (tp): Global pointer and thread pointer.
- x5–x7, x28–x31 (t0–t6): Temporaries that are not preserved across function calls.
- x8–x9, x18–x27 (s0–s11): Saved registers that must be preserved by callee. x8 doubles as the frame pointer (fp).
- x10–x17 (a0–a7): Function argument registers. a0 and a1 also carry return values.
Common pseudoinstructions
RISC-V assemblers provide convenient pseudoinstructions that map directly to base RV32I instructions:
- nop: Assembles to addi x0, x0, 0 (machine code 0x00000013).
- mv rd, rs: Copies a register, assembling to addi rd, rs, 0.
- not rd, rs: Bitwise inversion, assembling to xori rd, rs, -1.
- neg rd, rs: Two complement negation, assembling to sub rd, x0, rs.
- j offset: Unconditional jump, assembling to jal x0, offset.
- ret: Subroutine return, assembling to jalr x0, 0(ra) (machine code 0x00008067).
Related computer architecture tools
Instruction encoding links directly into memory systems and processor organization. To explore how memory addresses are broken down into tag, index, and block offset bits in processor caches, use the cache mapping calculator. For bitwise boolean operations and two complement arithmetic, use the binary calculator. To analyze gate-level digital building blocks like multiplexers and ALUs, use the logic gate simulator.