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, 10is 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?
- Twelve bits in two's complement cover −2048 to 2047. A larger constant takes two instructions:
luiputs the top 20 bits in place, and anaddiadds the low 12; the assembler'slipseudo-instruction picks the pair for you.
Name it
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,
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
- The
addifields. imm = 10, rs1 = 0, funct3 = 0, rd = 5, opcode 0x13. - Assemble the
addiword. - The
addfields. funct7 = 0, rs2 = 12, rs1 = 11, funct3 = 0, rd = 10, opcode 0x33, which make 0x00C58533. - Change it to
sub. Changing funct7 to 0x20 makes itsub: 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
- Opcode. Bits 6:0 are 0x03: a load.
- Destination and width. rd (bits 11:7) = 10, which is a0, and funct3 (bits 14:12) = 2: a word.
- 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
- 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.
- The immediate for +16. Only imm[4] is set, so bits 11:8 hold 1000.
- The other fields. rs2 = t1 = 6 at bits 24:20, rs1 = t0 = 5 at bits 19:15, funct3 = 000 and opcode 0x63.
- Assemble the word.
- 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°.
Step 2
How does a decoder recover rs1 from the word w? (Write it in terms of the fields' sum.)
Explain why
Why does RISC-V keep rs1, rs2 and rd in the same bit positions in every format?
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
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.