RISC-V Instructions

Resources

Instruction Word

  • RISC-V instructions are 32 bits wide in the base ISA (RV32I / RV64I / RV128I).
  • Some extensions add compressed 16-bit instructions (C extension) or longer 48-/64-bit instructions (for special encodings), but 32-bit is the β€œnormal” form.

A 32-bit instruction is divided into fields, depending on the format.


πŸ“œ Common Instruction Formats

RISC-V has a handful of standard formats, each designed for a class of instructions:

1. R-Type (Register type)

Used for register-register arithmetic/logical operations.

[ funct7 ][ rs2 ][ rs1 ][ funct3 ][ rd ][ opcode ]
  31..25   24..20 19..15   14..12   11..7   6..0
  • opcode: 7 bits β†’ main instruction group (e.g., ALU, load/store, branch).
  • rd: destination register.
  • rs1, rs2: source registers.
  • funct3: 3 bits β†’ sub-operation (e.g., add vs sub vs xor).
  • funct7: 7 bits β†’ further distinguishes instructions (e.g., ADD vs SUB both use funct3=000, but funct7 differs).

πŸ‘‰ Example: add rd, rs1, rs2
opcode = 0110011 (0x33), funct3 = 000, funct7 = 0000000.

πŸ‘‰ Example: sub rd, rs1, rs2
Same except funct7 = 0100000.

2. I-Type (Immediate type)

Used for instructions with an immediate operand (loads, ALU-immediate, etc.).

[ imm[11:0] ][ rs1 ][ funct3 ][ rd ][ opcode ]
  31..20      19..15   14..12   11..7   6..0
  • imm: 12-bit signed immediate.
  • No rs2 or funct7 field here.

πŸ‘‰ Example: addi rd, rs1, imm.

3. S-Type (Store type)

Used for memory stores.

[ imm[11:5] ][ rs2 ][ rs1 ][ funct3 ][ imm[4:0] ][ opcode ]
  31..25      24..20 19..15   14..12     11..7     6..0
  • Immediate is split into high and low parts across the instruction word.

πŸ‘‰ Example: sw rs2, offset(rs1).

4. B-Type (Branch type)

Used for conditional branches.

[ imm[12|10:5] ][ rs2 ][ rs1 ][ funct3 ][ imm[4:1|11] ][ opcode ]
  31..25          24..20 19..15   14..12      11..7       6..0
  • Immediate bits are shuffled!
  • Offset is sign-extended and multiplied by 2 to get branch target.

πŸ‘‰ Example: beq rs1, rs2, offset.

5. U-Type (Upper immediate)

Used for long immediates (lui, auipc).

[ imm[31:12] ][ rd ][ opcode ]
  31..12       11..7   6..0
  • Immediate is placed in upper 20 bits, lower 12 bits are zero.

6. J-Type (Jump type)

Used for jal (jump and link).

[ imm[20|10:1|11|19:12] ][ rd ][ opcode ]
  31      30..21   20   19..12   11..7   6..0
  • Immediate is scrambled like B-Type.
  • Sign-extended, shifted left by 1.

🧩 General Rule: Opcode Determines the Format

Every RISC-V instruction has a 7-bit opcode field (bits 6..0).
This opcode tells you two things:

  1. Which format to use (R, I, S, B, U, J, etc.).
  2. Which instruction category it belongs to (arithmetic, load/store, branch, etc.).

Once you know the format, you know how to interpret the remaining bits (rd, rs1, rs2, funct3, funct7, immediate layout).


πŸ“œ Standard Opcode Map (base RV32I)

Here’s the key part of the opcode map (7 bits, binary):

Opcode (bin)HexFormatCategoryExample instruction
01100110x33RInteger register-registeradd, sub, xor
00100110x13IInteger register-immaddi, xori
00000110x03ILoadslb, lw
01000110x23SStoressb, sw
11000110x63BConditional branchesbeq, bne
11011110x6FJJump and linkjal
11001110x67IJump and link registerjalr
01101110x37ULoad upper immediatelui
00101110x17UAdd upper immediate to PCauipc
11100110x73ISystem instructionsecall, CSR ops
00010110x0Bcustom-0Reserved for custom ISAuser-defined instr.
01010110x2Bcustom-1Reserved for custom ISAβ€”
10110110x5Bcustom-2Reserved for custom ISA(your case)
11110110x7Bcustom-3Reserved for custom ISAβ€”

1. 7-bit Opcode Space

  • A 7-bit field has 128 possible values (0–127 / 0x00–0x7F).
  • In RISC-V, these 7 bits don’t uniquely identify an instruction by themselves.
  • Instead, they act as a primary opcode:
    • They tell you the instruction format (R, I, S, B, U, J).
    • They narrow down the category (ALU, load, store, branch, system…).

Then, inside that category, funct3 (3 bits) and sometimes funct7 (7 bits) are used to distinguish the actual instruction.

Leave a Reply

Your email address will not be published. Required fields are marked *