What Is RISC-V, and How Is an Instruction Encoded?

RISC-V is an instruction set architecture: the contract between software and a processor, listing the instructions, registers and how each instruction is encoded in bits. Unlike x86 or Arm it is an open standard that anyone can implement without a license. Its base integer set, RV32I, has 32 registers and fixed 32-bit instructions in six formats, and every instruction can be decoded by looking at a 7-bit opcode and a few function fields.

What you'll learn

  • What RISC-V is, and what the base ISA and extensions are
  • The 32 registers and their ABI names
  • The six instruction formats: R, I, S, B, U and J
  • How to encode an instruction by hand, checked against the GNU assembler

Conventions and assumptions

RV32I, version 2.1 of the base integer instruction set in the RISC-V Unprivileged ISA specification. Encodings in this post were checked against GNU as (riscv32-linux-gnu-as -march=rv32i).

Base ISA and extensions

RISC-V is modular. A processor implements one base, such as RV32I (32-bit registers) or RV64I (64-bit), plus any standard extensions it needs, named by letters: M for multiply and divide, A for atomics, F and D for single and double precision floating point, C for compressed 16-bit instructions and V for vectors. RV32IMAC, for example, is a common microcontroller choice. The base on its own is enough to run compiled C: about 40 instructions for arithmetic, logic, shifts, comparisons, loads, stores, branches and jumps.

The 32 registers

RegisterABI nameUse
x0zeroAlways reads 0; writes are ignored
x1raReturn address
x2spStack pointer
x5 to x7, x28 to x31t0 to t6Temporaries
x8, x9, x18 to x27s0 to s11Saved across calls
x10 to x17a0 to a7Function arguments and return values

x0 being hardwired to zero removes the need for many separate instructions. A move is addi rd, rs, 0; a no-op is addi x0, x0, 0; comparing with zero uses x0 as an operand.

The six formats

FormatUsed forFields, bit 31 to bit 0
RRegister-register ALU ops (add, sub, and)funct7, rs2, rs1, funct3, rd, opcode
IImmediate ALU ops, loads, jalrimm[11:0], rs1, funct3, rd, opcode
SStoresimm[11:5], rs2, rs1, funct3, imm[4:0], opcode
BConditional branchesimm[12,10:5], rs2, rs1, funct3, imm[4:1,11], opcode
Ului, auipcimm[31:12], rd, opcode
Jjalimm[20,10:1,11,19:12], rd, opcode
RISC-V R-type instruction fieldsA 32-bit R-type instruction, from bit 31 down to bit 0: funct7 (7 bits), rs2 (5), rs1 (5), funct3 (3), rd (5) and opcode (7).funct77 bitsrs25 bitsrs15 bitsf33rd5 bitsopcode7 bits
The R-type layout, drawn to scale (f3 is funct3): register fields sit in the same bit positions in every format, which keeps decoding simple.

Two design choices stand out. rs1, rs2 and rd sit in the same bit positions in every format that has them, so the register file can be read while the rest of the instruction is still being decoded. And the immediate's sign bit is always bit 31, so sign extension can start straight away. The price is that S, B and J split their immediates across the word.

Worked example: encoding addi x1, x0, 5

  • addi is I-type, with opcode 0010011 (0x13) and funct3 000.
  • rd is x1: 00001. rs1 is x0: 00000. The immediate is 5: 000000000101 in 12 bits.
  • Lay the fields out from bit 31 down: imm[11:0] = 000000000101, rs1 = 00000, funct3 = 000, rd = 00001, opcode = 0010011.
  • Grouped in fours: 0000 0000 0101 0000 0000 0000 1001 0011, which is 0x00500093.
  • GNU as produces the same word, and the encoder tool on this site shows each field.

A store, for contrast

sw ra, 12(sp) stores x1 at sp + 12. S-type has no rd, so the five bits where rd would be hold imm[4:0] instead, and imm[11:5] goes in the funct7 position. 12 is 0000000 01100, so imm[11:5] = 0000000 and imm[4:0] = 01100. With rs2 = x1, rs1 = x2, funct3 = 010 and opcode 0100011 the word is 0x00112623.

Common mistakes

  • Treating branch and jump offsets as instruction counts. They are byte offsets from the branch's own address, and always even.
  • Expecting lui to take a full 32-bit value. It takes the upper 20 bits; addi adds the lower 12, and because addi sign-extends, the upper part sometimes needs adding 1.
  • Writing pseudo-instructions (li, mv, ret) and looking for their own opcode. The assembler expands them into base instructions.
  • Forgetting that writes to x0 are discarded.

Check your understanding

  • Q1. Which format does lw use? Answer: I-type, with the offset in imm[11:0] and the base register in rs1.
  • Q2. What does addi x0, x0, 0 do? Answer: nothing. It is the canonical no-op, since writes to x0 are ignored.
  • Q3. Why is the sign bit of every immediate in bit 31? Answer: so sign extension can begin before the format is known, which shortens the decode path.

Related

Sources

Last verified

Encodings checked against GNU as (riscv32-linux-gnu-as -march=rv32i) on 14 November 2026; register conventions and formats checked against the RV32I chapter of the RISC-V Unprivileged ISA specification.