---
title: Machine code and instruction encoding
heading: How every RISC-V instruction fits in 32 bits
eyebrow: C8 · Lesson 4 · Machine code and instruction encoding
xp: 40
---

::: hook
The processor never sees `addi t0, zero, 10`. It sees 0x00A00293, thirty-two bits fetched from memory, and its decoder has to pull out of them which operation to do, which registers to use and what constant to add, with nothing but wiring. RISC-V makes that easy by using only six layouts, and by keeping the register fields in the same bit positions in all of them, so the register file can be read before the decoder has even finished. Learning the layouts turns a hex dump back into a program, and shows why immediates come in the sizes they do.
:::

::: play
- The program loads two constants, adds and subtracts them, and stores the results. Step it and watch each instruction execute.
- Each line below is one 32-bit word in memory. `addi t0, zero, 10` is 0x00A00293: the 10 sits in the top 12 bits, register 5 (t0) in bits 7 to 11, and 0x13 (an I-type arithmetic operation) in the bottom 7.
:::
:::

```sim cpu
.data
results: .word 0, 0
.text
    addi t0, zero, 10   # 0x00A00293
    addi t1, zero, 3    # 0x00300313
    add  t2, t0, t1     # 0x006283B3
    sub  t3, t0, t1     # 0x40628E33
    la   t4, results
    sw   t2, 0(t4)
    sw   t3, 4(t4)
    ebreak
show: t0, t1, t2, t3
hex: t4
memory: 0x1000..0x1008
speed: 2
```

::: predict
## An I-type instruction holds its constant in a 12-bit field, sign-extended. What is the largest value `addi` can add in one instruction?

- [ ] 4095
- [x] 2047
- [ ] 32,767
- [ ] 1,048,575

::: reveal
2047. Twelve bits in two's complement cover −2048 to 2047. A larger constant takes two instructions: `lui` puts the top 20 bits in place, and an `addi` adds the low 12; the assembler's `li` pseudo-instruction picks the pair for you.
:::
:::

::: nameit
## The six formats

Every instruction is 32 bits, with the **opcode** in bits 6:0. The fields, from bit 31 down:

| Format | Fields | Used for |
|---|---|---|
| R | funct7 · rs2 · rs1 · funct3 · rd · opcode | register-register: `add`, `sub`, `and`, `slt` |
| I | imm[11:0] · rs1 · funct3 · rd · opcode | immediates, loads, `jalr`: `addi`, `lw` |
| S | imm[11:5] · rs2 · rs1 · funct3 · imm[4:0] · opcode | stores: `sw` |
| B | imm[12, 10:5] · rs2 · rs1 · funct3 · imm[4:1, 11] · opcode | branches: `beq`, `blt` |
| U | imm[31:12] · rd · opcode | `lui`, `auipc` |
| J | imm[20, 10:1, 11, 19:12] · rd · opcode | `jal` |

with rd in bits 11:7, rs1 in 19:15, rs2 in 24:20 and funct3 in 14:12 wherever they appear. Registers are 5-bit numbers (x0 to x31); t0 is x5, a0 is x10, sp is x2.

**Opcodes worth knowing:** 0x33 R-type arithmetic, 0x13 I-type arithmetic, 0x03 loads, 0x23 stores, 0x63 branches, 0x37 `lui`, 0x6F `jal`. **funct3** and **funct7** pick the operation within an opcode: `add` and `sub` differ only in funct7 (0x00 against 0x20).

## Encoding by hand

Shift each field to its position and add: for I-type,

$$\text{word} = \text{imm} \cdot 2^{20} + \text{rs1} \cdot 2^{15} + \text{funct3} \cdot 2^{12} + \text{rd} \cdot 2^7 + \text{opcode}$$

with a negative immediate written in 12-bit two's complement. Branch and jump offsets count in halfwords and are scrambled across their fields so that the sign bit is always bit 31 and most bits line up with the S and U formats, which keeps the hardware's immediate generator small.

## Large constants

`lui rd, imm20` sets rd to imm20 × 4096 (the upper 20 bits). Because `addi` sign-extends, a constant whose bit 11 is 1 needs the upper part rounded up by one: `li a0, 0x12345678` becomes `lui a0, 0x12345` then `addi a0, a0, 0x678`, but 0x12345FFF needs `lui a0, 0x12346` and `addi a0, a0, -1`.
:::

::: example
## Encode `addi t0, zero, 10` and `add a0, a1, a2`.

::: solution
1. **The `addi` fields.** imm = 10, rs1 = 0, funct3 = 0, rd = 5, opcode 0x13.
2. **Assemble the `addi` word.**
   $$\begin{aligned} &10 \cdot 2^{20} + 5 \cdot 2^7 + \text{0x13} \\ &= \text{0x00A00293} \end{aligned}$$
3. **The `add` fields.** funct7 = 0, rs2 = 12, rs1 = 11, funct3 = 0, rd = 10, opcode 0x33, which make 0x00C58533.
4. **Change it to `sub`.** Changing funct7 to 0x20 makes it `sub`: 0x40C58533.

**Answer:** `addi t0, zero, 10` is 0x00A00293 and `add a0, a1, a2` is 0x00C58533 (0x40C58533 as `sub`).
:::
:::

::: example
## Decode 0x00812503.

::: solution
1. **Opcode.** Bits 6:0 are 0x03: a load.
2. **Destination and width.** rd (bits 11:7) = 10, which is a0, and funct3 (bits 14:12) = 2: a word.
3. **Base and offset.** rs1 (bits 19:15) = 2, which is sp, and the immediate (bits 31:20) = 8.

**Answer:** 0x00812503 is `lw a0, 8(sp)`.
:::
:::

::: example
## Encode `beq t0, t1, +16`, a branch 16 bytes ahead.

::: solution
1. **Where B-type puts its immediate.** Bit 12 goes to bit 31, bits 10:5 to bits 30:25, bits 4:1 to bits 11:8, and bit 11 to bit 7; bit 0 is always 0 and isn't stored.
2. **The immediate for +16.** Only imm[4] is set, so bits 11:8 hold 1000.
3. **The other fields.** rs2 = t1 = 6 at bits 24:20, rs1 = t0 = 5 at bits 19:15, funct3 = 000 and opcode 0x63.
4. **Assemble the word.**
   $$\begin{aligned} &6 \cdot 2^{20} + 5 \cdot 2^{15} \\ &+ 8 \cdot 2^8 + \text{0x63} \\ &= \text{0x00628863} \end{aligned}$$
5. **Why the scrambling.** It keeps the register fields in the same places as in R-type and S-type, which simplifies the decoder at the cost of an odd-looking immediate.

**Answer:** `beq t0, t1, +16` is 0x00628863.
:::
:::

::: check
given: rd = 5 | 10 | 18; imm = 7 | 100 | 1000
answer: imm*2^20 + rd*2^7 + 19
base: 16
width: 8

## Encode `addi x{rd}, x0, {imm}` as eight hex digits.

::: right
Right. imm in bits 31:20, rd in 11:7, opcode 0x13.
:::

::: misses
- [imm*2^20 + rd*2^7] Include the opcode, 0x13, in the bottom 7 bits.
- [any] Not yet. $\text{imm} \cdot 2^{20} + \text{rd} \cdot 2^7 + 0x13$.
:::
:::

::: check
given: rd = 1 | 10 | 31; rs1 = 2 | 11; rs2 = 3 | 12
answer: rs2*2^20 + rs1*2^15 + rd*2^7 + 51
base: 16
width: 8

## Encode `add x{rd}, x{rs1}, x{rs2}` (R-type, opcode 0x33, funct3 and funct7 zero).

::: right
Right. rs2 in 24:20, rs1 in 19:15, rd in 11:7, opcode 0x33.
:::

::: misses
- [rs1*2^20 + rs2*2^15 + rd*2^7 + 51] rs1 goes in bits 19:15 and rs2 in 24:20.
- [any] Not yet. Place each field and add.
:::
:::

::: check
given: w = 2111123 | 8463619 | 10486419 | 13002547
answer: floor(w/128) - 32*floor(w/4096)
tolerance: 0

## What destination register number (rd) does the instruction {w:hex8} write?

::: right
Right. Bits 11:7: {answer}.
:::

::: misses
- [w - 128*floor(w/128)] That's the opcode, bits 6:0.
- [any] Not yet. Shift right by 7 and keep 5 bits.
:::
:::

::: check
given: w = 2111123 | 8463619 | 10486419 | 13002547
answer: w - 128*floor(w/128)
base: 16
width: 2

## What is the opcode of {w:hex8}, in hex?

::: right
Right. The low 7 bits.
:::

::: misses
- [any] Not yet. Keep bits 6:0.
:::
:::

::: check
given: b = 12 | 20 | 32
answer: 2^(b - 1) - 1
tolerance: 0

## A field holds a {b}-bit two's-complement immediate. What is the largest value it can hold?

::: right
Right. $2^{b-1} - 1$ = {answer}.
:::

::: misses
- [2^b - 1] The immediate is signed.
- [any] Not yet. Half the range is negative.
:::
:::

::: check
given: up = 74565 | 4096 | 1000; lo = 100 | 1500 | 2047
answer: up*4096 + lo
tolerance: 0

## `lui a0, {up}` is followed by `addi a0, a0, {lo}`. What value does a0 hold?

::: right
Right. upper × 4096 + lower = {answer}.
:::

::: misses
- [up + lo] `lui` shifts its immediate up 12 bits.
- [any] Not yet. `lui` loads imm × 4096.
:::
:::

::: check
kind: parts
given: rd = 5 | 10 | 18; rs = 2 | 6 | 11; imm = 4 | 100 | 2000

## Encode `addi x{rd}, x{rs}, {imm}` (I-type: imm in bits 31:20, rs1 in 19:15, funct3 000, rd in 11:7, opcode 0x13).

::: part
answer: imm*2^20 + rs*2^15 + rd*2^7 + 19
tolerance: 0
base: 16
width: 8

### What's the 32-bit encoding?

::: misses
- [any] Shift each field into place and add.
:::
:::

::: part
answer: floor(imm/16)
tolerance: 0

### What does bits 31:24 of that word hold, as a number? (They're the top 8 bits of the 12-bit immediate.)

::: misses
- [any] The immediate's top 8 bits: imm ÷ 16, rounded down.
:::
:::
:::

::: check
kind: derivation
vars: o = 0..127, d = 0..31, f = 0..7, s = 0..31, t = 0..31, g = 0..127

## An R-type instruction has opcode o, rd = d, funct3 = f, rs1 = s, rs2 = t and funct7 = g.

::: step
answer: g*2^25 + t*2^20 + s*2^15 + f*2^12 + d*2^7 + o

### What's its encoding, as a number?
:::

::: step
answer: floor((g*2^25 + t*2^20 + s*2^15 + f*2^12 + d*2^7 + o)/2^15) - 32*floor((g*2^25 + t*2^20 + s*2^15 + f*2^12 + d*2^7 + o)/2^20)

### How does a decoder recover rs1 from the word w? (Write it in terms of the fields' sum.)
:::

::: right
Right. Every field is a shift and a mask away.
:::
:::

::: explain
## Why does RISC-V keep rs1, rs2 and rd in the same bit positions in every format?

::: model
The register file has to be read before the instruction can execute, and the sooner it starts, the shorter the critical path. If the register numbers always sit in the same bits, the hardware can wire those bits straight to the register file's address inputs and start reading as soon as the instruction arrives, in parallel with decoding the opcode to find out what kind of instruction it is. Different layouts would need multiplexers chosen by the decoded opcode in front of the register file, adding delay to every instruction. The immediates are the parts that move around, and they're assembled off the critical path.
:::
:::

::: practice

::: check
given: r = 1 | 5 | 10 | 31
answer: r*2^7
base: 16
width: 8

## What is rd = x{r} shifted into its field (bits 11:7), as a 32-bit word?

::: misses
- [any] $r \cdot 2^7$.
:::
:::

::: check
given: n = 5 | 7 | 12
answer: 2^n
tolerance: 0

## How many different values does an {n}-bit field hold?

::: misses
- [any] $2^n$.
:::
:::

::: check
given: imm = -1 | -16 | -2048
answer: imm + 4096
base: 16
width: 3

## Write the 12-bit immediate field for {imm} in hex.

::: misses
- [any] Two's complement in 12 bits: add 4096.
:::
:::
:::
