ToolSphere

RISC-V Instruction Encoder

Computer science

Encode RV32I assembly instructions to binary and hex or decode machine code back to assembly with R, I, S, B, U, and J instruction field breakdowns.

32-Bit Machine Code (Hex)

0x00520293

I-Type (Immediate ALU / Load / JALR)
Assembly (ABI Registers)

addi t0, tp, 5

Assembly (Numeric Registers)

addi x5, x4, 5

32-Bit Binary (Grouped by instruction fields)Bits [31:0]

000000000101 00100 000 00101 0010011

Instruction Format Fields

imm[11:0][31:20]000000000101val: 5 (0x005)
rs1[19:15]00100val: 4
funct3[14:12]000val: 0 (0x0)
rd[11:7]00101val: 5
opcode[6:0]0010011val: 19 (0x13)
Detailed field breakdown table
FieldBitsBinaryValueDescription
imm[11:0][31:20]0000000001015 (0x005)12-bit signed immediate offset / constant (5)
rs1[19:15]001004Source / base register: x4 (tp)
funct3[14:12]0000 (0x0)addi operation variant
rd[11:7]001015Destination register: x5 (t0)
opcode[6:0]001001119 (0x13)I-type opcode (0x13)

Add immediate: rd = rs1 + imm. Uses sign-extended immediate 5 (0x005).

Assemble or Decode Instruction

Accepts numeric registers (x0–x31) or ABI names (zero, ra, sp, gp, tp, t0–t6, s0–s11, a0–a7), plus memory offsets like 8(sp) or -4(x2).

RV32I Instruction Format Reference

R-Type (Arithmetic/Logic)

funct7[31:25] rs2[24:20] rs1[19:15] funct3[14:12] rd[11:7] opcode[6:0]

add, sub, sll, slt, sltu, xor, srl, sra, or, and (opcode 0x33)

I-Type (Immediate / Load)

imm[11:0][31:20] rs1[19:15] funct3[14:12] rd[11:7] opcode[6:0]

addi, slti, xori, ori, andi, slli, srli, srai, lb, lh, lw, lbu, lhu, jalr

S-Type (Stores)

imm[11:5][31:25] rs2[24:20] rs1[19:15] funct3[14:12] imm[4:0][11:7] opcode[6:0]

sb, sh, sw (opcode 0x23). Split immediate preserves register locations.

B-Type (Branches)

imm[12|10:5] rs2 rs1 funct3 imm[4:1|11] opcode[6:0]

beq, bne, blt, bge, bltu, bgeu (opcode 0x63). 13-bit signed offset.

U-Type (Upper Immediate)

imm[31:12] rd[11:7] opcode[6:0]

lui (load upper immediate), auipc (add upper immediate to PC)

J-Type (Jump and Link)

imm[20|10:1|11|19:12] rd[11:7] opcode[6:0]

jal rd, offset (opcode 0x6F). 21-bit signed target offset.

On this browser only

Give feedback

Did this tool help?

Tell us what worked, what didn’t, or what you’d like next.

Opens Google Forms in a new tab with this tool’s name filled in. No sign-in or email required. Feedback is optional.

How to use RISC-V Instruction Encoder

  1. 1Choose whether to assemble an assembly instruction into machine code or decode existing 32-bit hex or binary back into assembly.
  2. 2For assembly, enter an instruction such as addi x5, x4, 5, lw a0, 8(sp), or beq t0, t1, 16 using numeric or standard ABI register names.
  3. 3For machine code decoding, enter 8 hex digits (e.g. 0x00520293) or a 32-character binary bitstring.
  4. 4Click any quick example chip to instantly load and inspect canonical R, I, S, B, U, or J format textbook instructions.
  5. 5Read the 32-bit hex code, grouped binary representation, instruction format category, and field-by-field breakdown table.
  6. 6Use Copy Hex or Copy Binary to copy machine representations directly into your assembler, simulator, or lab report.

About RISC-V Instruction Encoder

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.

Guides and tutorials

Frequently asked questions

What is the difference between RV32I and extensions like RV64I or RV32M?

RV32I is the base 32-bit integer instruction set with 32-bit registers and address space. RV64I widens registers and addresses to 64 bits with new 64-bit integer instructions. Standard extensions add modular hardware capabilities: M adds integer multiplication and division, A adds atomic memory instructions, F adds single-precision floating point, D adds double-precision floating point, and C adds 16-bit compressed instructions.

Why are immediate bits scrambled in B-type and J-type instructions?

RISC-V scrambles the immediate bit positions so that the sign bit is always placed at bit 31 across every format (I, S, B, U, and J). Furthermore, immediate bits 10:1 in B-type and J-type align with bits 10:1 in S-type and I-type where possible. This simplifies hardware sign-extension multiplexers and minimizes wiring delay on critical processor paths.

Why are source and destination registers pinned to the same bit positions?

In pipelined processors, decoding an instruction and reading source registers from the dual-port register file occurs early in the pipeline. By keeping rs1 at bits [19:15] and rs2 at bits [24:20], the processor begins reading operand values immediately without waiting to decode the opcode or determine the instruction type.

How does two complement sign extension work for 12-bit immediates?

A 12-bit signed immediate occupies bits [11:0], where bit 11 is the sign bit. If bit 11 is 0, bits [31:12] are filled with zeros (positive value from 0 to 2047). If bit 11 is 1, bits [31:12] are filled with ones (negative value from -1 to -2048). For example, -16 is represented as 12-bit 0xFF0, which sign-extends to 32-bit 0xFFFFFFF0.

What happens when an immediate constant exceeds 12 bits?

Base integer instructions like addi only support 12-bit signed values (-2048 to +2047). For larger 32-bit constants, assemblers use a two-instruction sequence: lui loads the upper 20 bits into bits [31:12] of a register, followed by addi to add the low 12 bits. The pseudoinstruction li (load immediate) automatically expands to this sequence.

Why is register x0 hardwired to zero in RISC-V?

Register x0 always reads as zero, and any value written to x0 is discarded. This design eliminates dedicated hardware instructions for common operations: copying a register is addi rd, rs, 0; testing for zero is beq rs, x0, target; a no-op (nop) is addi x0, x0, 0; and unconditional register jumps discard the return address using jalr x0, 0(rs).

Why do branch and jump offsets omit bit 0?

All RISC-V instructions are at least 2-byte (halfword) aligned. Therefore, instruction target addresses are always even, meaning bit 0 of the byte offset is always 0. By not storing bit 0 in the 32-bit instruction word, RISC-V doubles the reachable branching and jumping range without increasing instruction word length.

What is the difference between jal and jalr?

jal (jump and link) is a J-type instruction that jumps to a PC-relative offset (+/- 1 MB) using a 21-bit immediate offset. jalr (jump and link register) is an I-type instruction that computes the target address by adding a 12-bit immediate to an arbitrary 32-bit base address held in a register (rs1). jalr enables dynamic procedure returns (ret), switch jump tables, and function pointers.

Can this tool decode 16-bit compressed (RVC) instructions?

This tool focuses on the standard RV32I 32-bit base integer instruction set taught in university computer architecture courses. Standard 32-bit instructions always have bits [1:0] set to 11. Instructions ending in 00, 01, or 10 belong to the optional 16-bit compressed extension (RVC).

Which courses and textbooks use this instruction encoding?

This encoding strictly matches the official RISC-V Unprivileged Architecture Specification and standard textbooks like Patterson & Hennessy Computer Organization and Design: RISC-V Edition. It is directly applicable to undergraduate courses such as UC Berkeley CS 61C, MIT 6.1910, Stanford EE 108, CMU 15-213 / 18-447, and similar computer architecture coursework.

Have a suggestion for RISC-V Instruction Encoder?Share your feedback ↗