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

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 bne at address 0x14 is 0xFE629CE3. Its immediate, scattered across the word, decodes to −8: back two instructions, to loop at 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?

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 are words[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

FieldBitsPython
opcode6:0w & 0x7f
rd11:7(w >> 7) & 31
funct314:12(w >> 12) & 7
rs119:15(w >> 15) & 31
rs224:20(w >> 20) & 31
funct731:25w >> 25

Immediates, sign-extended from their top bit:

OpcodeFormatImmediate
0x13, 0x03, 0x67Ibits 31:20 (12 bits)
0x23Sbits 31:25 then 11:7 (12 bits)
0x63Bbit 31 → 12, bit 7 → 11, bits 30:25 → 10:5, bits 11:8 → 4:1, bit 0 is 0 (13 bits)
0x37Ubits 31:12 in place, low 12 bits 0
0x6FJbit 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 2n.

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
  1. Opcode. 0x03: a load.
  2. Registers and width. rd = 10 (a0), funct3 = 2 (a word), rs1 = 2 (sp).
  3. 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
  1. Bit 31. Bit 31 = 1 gives imm[12], worth 4096.
  2. Bit 7. Bit 7 = 1 gives imm[11], worth 2048.
  3. Bits 30:25. 111111 gives imm[10:5], worth 2016.
  4. Bits 11:8. 1100 gives imm[4:1], worth 24, and imm[0] is always 0.
  5. Add them. 4096+2048+2016+24=8184
  6. Sign-extend. The top bit of the 13-bit value is set, so it means 8184−8192=−8

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?

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.