C84 of 55
0 XP
  1. Hook
  2. Play
  3. Predict
  4. Name it
  5. Check
  6. Explain

C8 · Lesson 4 · Machine code and instruction encoding

How every RISC-V instruction fits in 32 bits

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.

  • 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.

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?

Name it

The six formats

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

FormatFieldsUsed for
Rfunct7 · rs2 · rs1 · funct3 · rd · opcoderegister-register: add, sub, and, slt
Iimm[11:0] · rs1 · funct3 · rd · opcodeimmediates, loads, jalr: addi, lw
Simm[11:5] · rs2 · rs1 · funct3 · imm[4:0] · opcodestores: sw
Bimm[12, 10:5] · rs2 · rs1 · funct3 · imm[4:1, 11] · opcodebranches: beq, blt
Uimm[31:12] · rd · opcodelui, auipc
Jimm[20, 10:1, 11, 19:12] · rd · opcodejal

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,

word=imm⋅220+rs1⋅215+funct3⋅212+rd⋅27+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.

Worked example

Encode addi t0, zero, 10 and add a0, a1, a2.

Show the solution
  1. The addi fields. imm = 10, rs1 = 0, funct3 = 0, rd = 5, opcode 0x13.
  2. Assemble the addi word. 10⋅220+5⋅27+0x13=0x00A00293
  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).

Worked example

Decode 0x00812503.

Show the 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).

Worked example

Encode beq t0, t1, +16, a branch 16 bytes ahead.

Show the 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. 6⋅220+5⋅215+8⋅28+0x63=0x00628863
  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

Encode addi x10, x0, 7 as eight hex digits.

Check

Encode add x10, x2, x12 (R-type, opcode 0x33, funct3 and funct7 zero).

Check

What destination register number (rd) does the instruction 00812503 write?

Check

What is the opcode of 00812503, in hex?

Check

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

Check

lui a0, 4096 is followed by addi a0, a0, 100. What value does a0 hold?

Check

Encode addi x10, x2, 2000 (I-type: imm in bits 31:20, rs1 in 19:15, funct3 000, rd in 11:7, opcode 0x13).

(a)

What's the 32-bit encoding?

(b)

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

Check

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

Step 1

What's its encoding, as a number?

Type it the way you would write it, like 3sin(2πft) or 3*sin(2*pi*f*t). Angles can be in radians or carry a degree sign, like 45°.

Explain why

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

Practice

Keep practicing

10 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.