C8 · Lesson 7 · Instruction encoding lab
Encoding and decoding RISC-V instructions in Python
An assembler turns beq t0, t1, loop into 32 bits, and a processor's decoder, an emulator or a debugger turns those bits back into fields. All of them are a few shifts and masks. In this lab you'll write the pieces in Python: sign extension, I-type and B-type encoders, a field decoder and a small disassembler, each checked against the words this course's own assembler produces.
- The memory window shows the program as it sits in memory: one 32-bit word per instruction, starting at address 0.
- Step through. Each word is an instruction from lesson 4's formats: an I-type
addi, an R-typesub, an S-typesw, an I-typelw, and two B-type branches: one forward 8 bytes to leave the loop, one back 8 bytes to repeat it until t2 reaches 10. - Pick a word and split it by hand: the low 7 bits are the opcode, and the rest falls into the fields of its format.
Predict
A B-type instruction can branch ±4 KiB in steps of 2 bytes. How many immediate bits does the instruction actually store?
- The offset is a 13-bit signed number (bits 12 to 0), but bit 0 is always 0, because instructions sit on 2-byte boundaries, so only bits 12 to 1 are stored. They're scattered across bits 31, 30 to 25, 11 to 8 and 7 of the word, so that the register fields stay in the same places as in the other formats.
Name it
Fields with shifts and masks
To read a field of w bits starting at bit p: (word >> p) & ((1 << w) - 1). To place one: (value & mask) << p, combined with |.
| Format | 31:25 | 24:20 | 19:15 | 14:12 | 11:7 | 6:0 |
|---|---|---|---|---|---|---|
| R | funct7 | rs2 | rs1 | funct3 | rd | opcode |
| I | imm[11:5] | imm[4:0] | rs1 | funct3 | rd | opcode |
| S | imm[11:5] | rs2 | rs1 | funct3 | imm[4:0] | opcode |
| B | imm[12], imm[10:5] | rs2 | rs1 | funct3 | imm[4:1], imm[11] | opcode |
Sign extension
A field of b bits holding a two's-complement number is negative when its top bit is set, and then its value is the unsigned value minus :
Python's integers never overflow, so masking a negative number with & 0xFFF gives exactly its 12-bit two's-complement pattern.
Worked example
Encode sw a0, 8(sp) by hand.
Show the solution
- Split the immediate. S-type splits 8 into imm[11:5] = 0 and imm[4:0] = 8.
- The other fields. rs2 = a0 = 10, rs1 = sp = 2, funct3 = 010 for a word, opcode 0x23.
- Assemble the word.
- Check. That's the word the assembler gave in the program above.
Answer sw a0, 8(sp) is 0x00A12423.
Worked example
Decode 0xFE628EE3.
Show the solution
- Opcode and funct3. Opcode 0x63 is a branch, and funct3 = 000 makes it
beq. - Registers. rs1 = bits 19:15 = 5 (t0), and rs2 = bits 24:20 = 6 (t1).
- Gather the immediate's bits. Bit 31 = 1 gives imm[12], bits 30:25 = 111111 give imm[10:5], bits 11:8 = 1110 give imm[4:1], and bit 7 = 1 gives imm[11].
- Put them in order. Together, 1 1111111 1110 0 = 0x1FFC.
- Sign-extend from 13 bits.
Answer beq t0, t1, -4, a branch to the previous instruction.
Check
What's 2783 (a 12-bit field) sign-extended?
Check
Write sign_extend(value, bits): the low bits bits of value read as a two's-complement number.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Write encode_i(imm, rs1, funct3, rd, opcode): the 32-bit I-type word. imm may be negative (−2048 to 2047).
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Write decode(word): a dict with the fields opcode, rd, funct3, rs1, rs2, funct7, and imm_i (bits 31:20 sign-extended).
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Write encode_b(offset, rs2, rs1, funct3): a branch word (opcode 0x63) for an even byte offset from −4096 to 4094.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Write disassemble(word) for add, sub, addi, lw, sw and beq, returning text like add x10, x11, x12, addi x5, x0, 10, lw x10, 8(x2), sw x10, 8(x2) or beq x5, x6, -4. Anything else gives unknown. sign_extend is provided.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Explain why
Why does RISC-V scatter the bits of a branch offset around the instruction word instead of storing them in order?
The scattering keeps everything else in fixed places. rs1, rs2 and funct3 sit at the same bit positions in every format that has them, so the register file can start reading before the processor even knows what kind of instruction it has. The immediate bits are arranged so that each bit of the offset comes from as few different instruction bits as possible across the formats: bit 31 is always the sign, and most other immediate bits line up between the I, S and B layouts. That makes the immediate generator a handful of multiplexers. The cost falls on software (assemblers, disassemblers and anyone decoding by hand), which is cheap compared with hardware on the critical path.
Practice
Keep practicing
One kind of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.