Every 32-bit RISC-V base integer instruction (RV32I) belongs to one of six core formats: R-type, I-type, S-type, B-type, U-type, or J-type. Understanding how these formats slice up a 32-bit instruction word is essential for assembling assembly code into machine hex, decoding raw binary dumps, or building pipelined datapath control units in computer architecture courses (such as UC Berkeley CS 61C or MIT 6.1910).
This guide walks through each of the six RV32I formats with bit-level field definitions, design rationales, and concrete worked examples computed directly with the site's instruction encoder library.
RV32I Architecture Principles
The RISC-V instruction set architecture (ISA) was designed with three hardware priorities:
- Fixed instruction length: Every RV32I instruction is exactly 32 bits wide, aligned on a 4-byte memory boundary (bits [1:0] are always
11for standard base instructions). - Fixed register specifier positions: The source registers
rs1(bits [19:15]) andrs2(bits [24:20]), as well as the destination registerrd(bits [11:7]), always appear in the identical bit slots whenever present. This allows register file reads to proceed in parallel with opcode decoding, eliminating decode-stage multiplexers and shortening critical clock paths. - Fixed sign-bit position: In every format that contains an immediate field (I, S, B, U, and J), the most significant bit of the immediate is wired directly to bit [31] of the instruction. The hardware sign-extension tree always draws from bit [31] without multiplexing.
Format Overview
The table below summarizes the six formats and their bit allocations:
| Format | Bits [31:25] (7) | Bits [24:20] (5) | Bits [19:15] (5) | Bits [14:12] (3) | Bits [11:7] (5) | Bits [6:0] (7) | Purpose |
|---|---|---|---|---|---|---|---|
| R | funct7 | rs2 | rs1 | funct3 | rd | opcode | Register-register ALU ops |
| I | imm[11:0] | rs1 | funct3 | rd | opcode | Immediate ALU, loads, jalr | |
| S | imm[11:5] | rs2 | rs1 | funct3 | imm[4:0] | opcode | Memory stores (sw, sh, sb) |
| B | imm[12|10:5] | rs2 | rs1 | funct3 | imm[4:1|11] | opcode | Conditional branches |
| U | imm[31:12] | rd | opcode | Upper immediate (lui, auipc) | |||
| J | imm[20|10:1|11|19:12] | rd | opcode | Unconditional jump (jal) |
1. R-Type (Register-Register)
R-type instructions operate purely on processor registers: two source registers (rs1 and rs2) supply input operands to the ALU, and the calculated result is written back to destination register rd.
Bit Layout
[31:25] funct7 (7 bits)
[24:20] rs2 (5 bits)
[19:15] rs1 (5 bits)
[14:12] funct3 (3 bits)
[11:7] rd (5 bits)
[6:0] opcode (7 bits) = 0110011 (0x33)
- opcode (bits [6:0]): Standard value
0110011(0x33) identifies RV32I integer register-register operations. - rd (bits [11:7]): 5-bit destination register index (x0 through x31).
- funct3 (bits [14:12]): 3-bit sub-operation selector (e.g.,
000for add/sub,111for and,010for slt). - rs1 (bits [19:15]): 5-bit first source register index.
- rs2 (bits [24:20]): 5-bit second source register index.
- funct7 (bits [31:25]): 7-bit function modifier (
0000000for add,0100000for sub or arithmetic shift).
Worked Example: add x1, x2, x3
Add the contents of x2 (stack pointer sp) and x3 (global pointer gp), storing the sum into x1 (return address ra):
| Field | Bits | Binary | Value | Description |
|---|---|---|---|---|
| funct7 | [31:25] | 0000000 |
0x00 | Standard addition operation variant |
| rs2 | [24:20] | 00011 |
3 (gp) | Second source register x3 |
| rs1 | [19:15] | 00010 |
2 (sp) | First source register x2 |
| funct3 | [14:12] | 000 |
0x0 | Addition ALU selector |
| rd | [11:7] | 00001 |
1 (ra) | Destination register x1 |
| opcode | [6:0] | 0110011 |
0x33 | R-type integer ALU opcode |
Combining the fields:
Binary: 0000000 00011 00010 000 00001 0110011
Grouped: 0000 0000 0011 0001 0000 0000 1011 0011
Hex: 0x003100B3
Notice how sub x1, x2, x3 would differ in only a single bit: funct7 would become 0100000 (bit 30 = 1), yielding hex 0x403100B3.
2. I-Type (Immediate & Loads)
I-type instructions encode an operation between one register operand (rs1) and a 12-bit signed immediate constant. They are used for immediate arithmetic (addi, andi, slti), memory loads (lw, lh, lb), and indirect jumps (jalr).
Bit Layout
[31:20] imm[11:0] (12 bits)
[19:15] rs1 (5 bits)
[14:12] funct3 (3 bits)
[11:7] rd (5 bits)
[6:0] opcode (7 bits)
The 12-bit immediate field imm[11:0] is sign-extended in hardware to 32 bits, allowing values in the range [-2048, +2047].
Worked Example: addi x5, x4, 5
Add the immediate constant +5 to register x4 (tp), writing the result into x5 (t0):
| Field | Bits | Binary | Value | Description |
|---|---|---|---|---|
| imm[11:0] | [31:20] | 000000000101 |
5 | 12-bit signed immediate constant |
| rs1 | [19:15] | 00100 |
4 (tp) | Source register x4 |
| funct3 | [14:12] | 000 |
0x0 | addi operation variant |
| rd | [11:7] | 00101 |
5 (t0) | Destination register x5 |
| opcode | [6:0] | 0010011 |
0x13 | I-type integer ALU opcode |
Combining the fields:
Binary: 000000000101 00100 000 00101 0010011
Grouped: 0000 0000 0101 0010 0000 0010 1001 0011
Hex: 0x00520293
Negative Immediates and Sign Extension
When an immediate is negative, its two's complement sign bit is 1. For example, addi sp, sp, -16 (stack frame allocation):
-16in 12-bit two's complement is1111 1111 0000(0xFF0).rs1=00010(sp),rd=00010(sp),funct3=000,opcode=0010011.- Assembled binary:
111111110000 00010 000 00010 0010011->0xFF010113.
Shift Instructions: Specialized I-Type
For immediate shifts (slli, srli, srai), the shift amount only requires 5 bits (0 to 31). The 12-bit immediate is therefore divided:
- Bits [24:20] contain the 5-bit shift amount (
shamt). - Bits [31:25] function as an auxiliary
funct7field (0000000for logical shifts,0100000for arithmetic right shiftsrai).
3. S-Type (Store)
S-type instructions store data from register rs2 into a memory location computed as rs1 + offset. Because store instructions do not write to a destination register, there is no rd field.
Why Is the Immediate Split?
Instead of placing all 12 immediate bits in bits [31:20] (which would force rs2 to move), RISC-V splits the immediate:
- Upper 7 bits:
imm[11:5]occupy bits [31:25]. - Lower 5 bits:
imm[4:0]occupy bits [11:7] (whererdis located in R-type and I-type).
This clever choice ensures that rs1 ([19:15]) and rs2 ([24:20]) remain in the exact same physical bit positions as in R-type instructions. The hardware register read ports connect directly to those bit lanes without needing an extra input multiplexer.
Bit Layout
[31:25] imm[11:5] (7 bits)
[24:20] rs2 (5 bits) - Source data to store
[19:15] rs1 (5 bits) - Base address register
[14:12] funct3 (3 bits) - Store width: 000=sb, 001=sh, 010=sw
[11:7] imm[4:0] (5 bits)
[6:0] opcode (7 bits) = 0100011 (0x23)
Worked Example: sw x1, 12(x2)
Store the 32-bit word in register x1 (ra) to the memory address at x2 + 12 (sp + 12):
- Immediate value: 12 =
0000 0000 1100in 12-bit binary. imm[11:5]=0000000(upper 7 bits).imm[4:0]=01100(lower 5 bits, decimal 12).rs2=00001(x1, the register holding the word to be written).rs1=00010(x2, base stack pointer).funct3=010(32-bit word store).opcode=0100011(0x23).
| Field | Bits | Binary | Value | Description |
|---|---|---|---|---|
| imm[11:5] | [31:25] | 0000000 |
0 | Upper 7 bits of store offset |
| rs2 | [24:20] | 00001 |
1 (ra) | Data register to store |
| rs1 | [19:15] | 00010 |
2 (sp) | Base address register |
| funct3 | [14:12] | 010 |
0x2 | Store word (sw) width |
| imm[4:0] | [11:7] | 01100 |
12 | Lower 5 bits of store offset |
| opcode | [6:0] | 0100011 |
0x23 | S-type store opcode |
Combining the fields:
Binary: 0000000 00001 00010 010 01100 0100011
Grouped: 0000 0000 0001 0001 0010 0110 0010 0011
Hex: 0x00112623
4. B-Type (Conditional Branch)
B-type instructions compare two registers (rs1 and rs2) and, if the condition is met, perform a program-counter-relative jump (PC = PC + offset).
Bit Layout and Scrambling
Instructions in RV32I must be aligned to even byte addresses (2-byte multiples for compressed C extension, 4-byte for base RV32I). Therefore, the lowest bit of any branch target offset (imm[0]) is always 0 and is not stored in the instruction word. This gives a 13-bit signed offset imm[12:0] with a jump reach of +/- 4 KB.
To maximize hardware reuse with the S-type layout, the bits of the immediate are positioned as follows:
[31] imm[12] (1 bit) - Sign bit remains at bit 31
[30:25] imm[10:5] (6 bits) - Aligned with S-type imm[10:5]
[24:20] rs2 (5 bits) - Comparison source register 2
[19:15] rs1 (5 bits) - Comparison source register 1
[14:12] funct3 (3 bits) - Branch condition (beq, bne, blt, bge, bltu, bgeu)
[11:8] imm[4:1] (4 bits) - Aligned with S-type imm[4:1]
[7] imm[11] (1 bit) - Sits in bit 7
[6:0] opcode (7 bits) = 1100011 (0x63)
Notice the alignment beauty: bits [30:25] map imm[10:5], and bits [11:8] map imm[4:1], perfectly matching S-type bit positions. Only imm[11] sits in a dedicated slot (bit 7) while the sign bit stays firmly at bit [31].
Worked Example: beq x1, x2, 16
Branch to PC + 16 if register x1 (ra) equals x2 (sp):
- Branch offset: +16 bytes =
0b0000000010000(13-bit binary). imm[12]= 0imm[11]= 0imm[10:5]=000000imm[4:1]=1000(since offset is 16 = 2⁴, bits [4:1] are1000)imm[0]= 0 (omitted from machine code)
Assemble the fields into instruction word slots:
- Bits [31:25] (
imm[12|10:5]):0+000000=0000000 - Bits [24:20] (
rs2):00010(x2) - Bits [19:15] (
rs1):00001(x1) - Bits [14:12] (
funct3):000(beq) - Bits [11:7] (
imm[4:1|11]):1000+0=10000 - Bits [6:0] (
opcode):1100011(0x63)
| Field | Bits | Binary | Value | Description |
|---|---|---|---|---|
| imm[12|10:5] | [31:25] | 0000000 |
0 | Sign bit (0) and offset bits 10:5 |
| rs2 | [24:20] | 00010 |
2 (sp) | Source comparison register 2 |
| rs1 | [19:15] | 00001 |
1 (ra) | Source comparison register 1 |
| funct3 | [14:12] | 000 |
0x0 | beq equality condition |
| imm[4:1|11] | [11:7] | 10000 |
16 | Offset bits 4:1 (1000) and bit 11 (0) |
| opcode | [6:0] | 1100011 |
0x63 | B-type conditional branch opcode |
Combining the fields:
Binary: 0000000 00010 00001 000 10000 1100011
Grouped: 0000 0000 0010 0000 1000 1000 0110 0011
Hex: 0x00208863
5. U-Type (Upper Immediate)
U-type instructions provide a way to load large 20-bit constants into the upper 20 bits of a register (rd), setting the lower 12 bits to zero. They are used by lui (Load Upper Immediate) and auipc (Add Upper Immediate to PC).
Bit Layout
[31:12] imm[31:12] (20 bits)
[11:7] rd (5 bits)
[6:0] opcode (7 bits) - 0110111 (0x37) for lui, 0010111 (0x17) for auipc
Worked Example: lui x5, 0x12345
Load the 20-bit hex value 0x12345 into register x5 (t0), producing value 0x12345000:
| Field | Bits | Binary | Value | Description |
|---|---|---|---|---|
| imm[31:12] | [31:12] | 00010010001101000101 |
0x12345 | 20-bit upper immediate |
| rd | [11:7] | 00101 |
5 (t0) | Destination register x5 |
| opcode | [6:0] | 0110111 |
0x37 | U-type LUI opcode |
Combining the fields:
Binary: 00010010001101000101 00101 0110111
Grouped: 0001 0010 0011 0100 0101 0010 1011 0111
Hex: 0x123452B7
Constructing 32-Bit Constants (LUI + ADDI)
Because RV32I instructions are only 32 bits wide, loading an arbitrary 32-bit constant requires two instructions:
lui rd, upper20loads the upper 20 bits.addi rd, rd, lower12adds the lower 12 bits.
Important catch: The addi instruction sign-extends its 12-bit immediate. If bit 11 of the constant is 1 (meaning the lower 12 bits are negative when interpreted as signed), addi will subtract from the upper bits. To compensate, the assembler adds 1 to the upper 20 bits in the lui instruction whenever bit 11 of the constant is set.
6. J-Type (Unconditional Jump)
J-type is used solely for the jal (Jump and Link) instruction. It stores the return address PC + 4 into rd and jumps to PC + offset.
Bit Layout and Scrambling
Like B-type branches, jump targets must be 2-byte aligned, so bit 0 is implicitly 0 and omitted. The instruction encodes a 21-bit signed offset imm[20:0], allowing jumps of +/- 1 MB.
To mirror the U-type layout (occupying bits [31:12] above rd), the offset bits are scrambled:
[31] imm[20] (1 bit) - Sign bit stays at bit 31
[30:21] imm[10:1] (10 bits)
[20] imm[11] (1 bit)
[19:12] imm[19:12] (8 bits)
[11:7] rd (5 bits) - Link register (ra = x1 for standard function calls)
[6:0] opcode (7 bits) = 1101111 (0x6F)
Worked Example: jal x1, 2048
Call a function at offset +2048 bytes, saving the return address into x1 (ra):
- Offset: +2048 =
0b000000000100000000000(21-bit binary with bit 11 = 1, all other bits 0). imm[20]= 0imm[19:12]=00000000imm[11]= 1imm[10:1]=0000000000imm[0]= 0 (omitted)
Assemble the scrambled bits into instruction slot [31:12]:
- Bit [31] (
imm[20]):0 - Bits [30:21] (
imm[10:1]):0000000000 - Bit [20] (
imm[11]):1 - Bits [19:12] (
imm[19:12]):00000000 - Upper 20 bits:
00000000000100000000 - Bits [11:7] (
rd):00001(x1) - Bits [6:0] (
opcode):1101111(0x6F)
| Field | Bits | Binary | Value | Description |
|---|---|---|---|---|
| imm[20|10:1|11|19:12] | [31:12] | 00000000000100000000 |
2048 | Scrambled 21-bit jump offset |
| rd | [11:7] | 00001 |
1 (ra) | Link register to receive PC + 4 |
| opcode | [6:0] | 1101111 |
0x6F | J-type unconditional jump opcode |
Combining the fields:
Binary: 00000000000100000000 00001 1101111
Grouped: 0000 0000 0001 0000 0000 0000 1110 1111
Hex: 0x001000EF
Summary Reference Table
The table below provides a quick lookup for every format, showing the exact bit positions and textbook examples verified by the encoder:
| Format | Opcode Hex | Key Identifying Fields | Assembly Example | Encoded Hex Word |
|---|---|---|---|---|
| R-Type | 0x33 |
funct7=[31:25], rs2=[24:20], rs1=[19:15], funct3=[14:12], rd=[11:7] | add x1, x2, x3 |
0x003100B3 |
| I-Type | 0x13, 0x03, 0x67 |
imm[11:0]=[31:20], rs1=[19:15], funct3=[14:12], rd=[11:7] | addi x5, x4, 5 |
0x00520293 |
| S-Type | 0x23 |
imm[11:5]=[31:25], rs2=[24:20], rs1=[19:15], imm[4:0]=[11:7] | sw x1, 12(x2) |
0x00112623 |
| B-Type | 0x63 |
imm[12|10:5]=[31:25], rs2=[24:20], rs1=[19:15], imm[4:1|11]=[11:7] | beq x1, x2, 16 |
0x00208863 |
| U-Type | 0x37, 0x17 |
imm[31:12]=[31:12], rd=[11:7] | lui x5, 0x12345 |
0x123452B7 |
| J-Type | 0x6F |
imm[20|10:1|11|19:12]=[31:12], rd=[11:7] | jal x1, 2048 |
0x001000EF |
Five Common Exam Pitfalls
- Forgetting that branch and jump offsets omit bit 0: In B-type and J-type instructions, the immediate value is a byte offset, not an instruction count. Because instructions must be aligned, bit 0 is always 0 and omitted from the machine code. If a question asks for a branch to
PC + 8, the encoded immediate is 8, but its bit 0 is dropped during placement. - Mixing up
rs1andrs2in store instructions: Insw rs2, offset(rs1), the register written to memory isrs2, while the base address register isrs1. Beginners frequently swap them becausers2appears first in the assembly syntax. - Overlooking negative immediate sign extension:
addi,lw, andswuse 12-bit signed offsets. Values above 2047 wrap into negative numbers. For example,0xFFFis -1, not 4095. - Scrambling immediate bits incorrectly on paper: When assembling B-type instructions manually, remember that bit 11 goes into bit 7 of the machine word, while bits [4:1] go into bits [11:8].
- Confusing ABI names and numeric registers: Remember that
zeroisx0,raisx1,spisx2,t0–t2arex5–x7,s0/fpisx8, anda0–a7arex10–x17.
Verify Encodings Online
You can assemble any RV32I instruction or decode raw hex words step by step with the interactive RISC-V instruction encoder. It highlights every bit field, shows ABI and numeric register translations, and flags syntax errors instantly. For cache and memory layout topics, see the cache mapping guide.
Use the Binary Calculator to practice the accompanying binary arithmetic.