What encoding an instruction means
The text addi a0, zero, 5 is for people. The processor only sees bits, so the ISA defines a template: the low 7 bits (opcode) give the group and, with it, the format; the other fields say which registers are involved and which constant (immediate) to use. RV32I has six formats (R, I, S, B, U and J) and all of them keep rs1, rs2 and rd in the same positions.
Worked example: 0x00500513
- Hexadecimal → binary. Each hex digit is 4 bits: 0x00500513 = 0000 0000 0101 0000 0000 0101 0001 0011.
- opcode = bits 6–0. 0010011: the OP-IMM group (operation with an immediate). This fixes the format: I.
- rd = bits 11–7. 01010 = 10 → x10, called a0 in the ABI. It is the destination register.
- funct3 = bits 14–12. 000: within OP-IMM, 000 is addi.
- rs1 = bits 19–15. 00000 = x0 (zero), the register that always holds 0.
- imm = bits 31–20. 000000000101 = 5. Bit 31 is 0, so the value is positive.
- Instruction. addi a0, zero, 5: a0 = 0 + 5. It is the encoding of the li a0, 5 pseudo-instruction.
The same breakdown, computed by the site’s decoder:
| Field | Bits | Binary | Meaning |
|---|---|---|---|
| imm[11:0] | [31:20] | 000000000101 | piece of the immediate |
| rs1 | [19:15] | 00000 | register x0 (zero) |
| funct3 | [14:12] | 000 | selects the operation within the group |
| rd | [11:7] | 01010 | register x10 (a0) |
| opcode | [6:0] | 0010011 | group OP-IMM |
| imm | 5 (0x5) | rebuilt immediate (signed) | |
| Instruction | addi a0, zero, 5 | ||
Notice the order of work: opcode first, because until you know the format you do not know how to read bits 31–7. In the I format those bits are a 12-bit immediate; in the R format they would be funct7 and rs2.