C8 · Lesson 53 · Project
Decoding and running machine code in Python
An emulator is a program that behaves like a processor: it fetches machine-code words, decodes them and carries out each one on registers and memory kept in ordinary variables. Emulators run old game consoles, test firmware before the hardware exists, and check new chip designs instruction by instruction. The board in this course is one, written in JavaScript. In this project you'll write your own in Python, for a subset of RV32I big enough to run loops, arrays and function calls, and run real machine code on it: the words below were produced by the course's assembler from the programs shown.
- This program adds 1 to 10. Step it and note the machine-code word beside each instruction: those words, and nothing else, are what your emulator will be given.
- The
bneat address 0x14 is 0xFE629CE3. Its immediate, scattered across the word, decodes to −8: back two instructions, toloopat 0x0C. - When it reaches
ebreak, a0 = 55. Your emulator must get the same.
Predict
The branch word 0xFE629CE3 has its sign bit (bit 31) set. What does that tell you before decoding anything else?
The offset is negative. Every RISC-V format keeps the immediate's sign in bit 31, so a decoder can sign-extend before it has sorted out where the other immediate bits live. Backward branches are almost always loops.
Name it
What the emulator keeps
x, a list of 32 registers holding signed 32-bit values, with x[0] always 0 (skip any write to it).pc, starting at 0. Instructions arewords[pc // 4].mem, a dictionary from byte address to a 32-bit word, for word loads and stores only.
The cycle
Fetch words[pc // 4]. Stop at ebreak (0x00100073). Otherwise decode, execute, write rd if the instruction has a result and rd isn't 0, and set pc to pc + 4 or the jump target.
Decoding
| Field | Bits | Python |
|---|---|---|
| opcode | 6:0 | w & 0x7f |
| rd | 11:7 | (w >> 7) & 31 |
| funct3 | 14:12 | (w >> 12) & 7 |
| rs1 | 19:15 | (w >> 15) & 31 |
| rs2 | 24:20 | (w >> 20) & 31 |
| funct7 | 31:25 | w >> 25 |
Immediates, sign-extended from their top bit:
| Opcode | Format | Immediate |
|---|---|---|
| 0x13, 0x03, 0x67 | I | bits 31:20 (12 bits) |
| 0x23 | S | bits 31:25 then 11:7 (12 bits) |
| 0x63 | B | bit 31 → 12, bit 7 → 11, bits 30:25 → 10:5, bits 11:8 → 4:1, bit 0 is 0 (13 bits) |
| 0x37 | U | bits 31:12 in place, low 12 bits 0 |
| 0x6F | J | bit 31 → 20, bits 19:12 in place, bit 20 → 11, bits 30:21 → 10:1 (21 bits) |
To sign-extend an n-bit value v: if bit n − 1 is set, subtract .
The subset
R-type (0x33): add, sub, xor, or, and, slt. I-type (0x13): addi, xori, ori, andi, slti, slli, srli. lui (0x37), lw (0x03, funct3 2), sw (0x23, funct3 2), branches (0x63: beq 0, bne 1, blt 4, bge 5), jal (0x6F) and jalr (0x67). Wrap every result to 32 bits.
Worked example
Decode 0x00812503.
Show the solution
- Opcode. 0x03: a load.
- Registers and width. rd = 10 (a0), funct3 = 2 (a word), rs1 = 2 (sp).
- The immediate. The I immediate is bits 31:20 = 8.
Answer 0x00812503 is lw a0, 8(sp).
Worked example
Decode the branch 0xFE629CE3's immediate.
Show the solution
- Bit 31. Bit 31 = 1 gives imm[12], worth 4096.
- Bit 7. Bit 7 = 1 gives imm[11], worth 2048.
- Bits 30:25. 111111 gives imm[10:5], worth 2016.
- Bits 11:8. 1100 gives imm[4:1], worth 24, and imm[0] is always 0.
- Add them.
- Sign-extend. The top bit of the 13-bit value is set, so it means
Answer −8, a branch from the bne at 0x14 back to loop at 0x0C.
Check
What sign-extended I immediate does the addi word 80000013 hold?
Check
The emulator's pc is 0040. Which index of the words list holds the instruction to fetch?
Check
Write decode(w), returning a dictionary with keys op, rd, f3, rs1, rs2, f7 and imm, where imm is the sign-extended immediate for the I, S, B, U and J formats (opcodes above) and 0 for R-type.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Write run(words, limit=10000) for registers only: the R-type and I-type arithmetic instructions, lui and the four branches, stopping at ebreak and returning the list of 32 registers. (Include your decode, or decode inside run.)
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Write run(words, mem, limit=10000) for the whole subset: add lw, sw (word-aligned, with mem a dictionary from byte address to word, missing addresses reading as 0), jal and jalr. Return the registers; mem is changed in place.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Explain why
How does jal followed later by jalr zero, 0(ra) make a function call and return work, in terms of what the emulator does?
jal ra, square does two things in one instruction: it writes pc + 4, the address of the instruction after the call, into ra, and it sets the pc to the function's address. The function runs, and its last instruction, jalr zero, 0(ra), sets the pc to the value in ra (plus 0), while writing its own return address into x0, where it's thrown away. So the pc comes back to just after the call. Because the return address lives in a register, a function that calls another must save ra first, or the second jal overwrites it.
Practice
Keep practicing
5 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.