Master the fundamental concepts of cpu design through this focused micro-challenge.
Three hints are available for this task, revealed one at a time inside the code workspace so you can struggle productively before seeing them.
Every task includes starter code, theory, and hidden tests so you can implement and verify locally in the browser.
How it worksYour emulator already fetches and decodes instructions. Now the execute stage must dispatch arithmetic and logic opcodes to an ALU module that reads two register operands and writes back a result plus flags.
rs and rt from the register fileopcoderd; update Z/N/C/V flagscLoading…
Teaching ISAs often pack register indices into the instruction byte. For example, bits 4-3 might select destination, bits 2-1 source A, bits 0 source B.
For this exercise, you will connect decode output to ALU operations for ADD, SUB, AND, OR, and NOT. This task asks you to close the loop from instruction bits to numeric results, the same execute-stage wiring QEMU's interpreter performs for each guest instruction.
Keep the relevant datasheet, ISA manual, or architecture textbook chapter open while you implement. When your output disagrees with the reference trace on the same program, the bug is usually a mis-decoded opcode, a stale register read, or a flag bit left unchanged after arithmetic.
For this exercise, you will use those habits while implementing the requirement in the starter code. Microarchitectural product names change across CPU generations, but the control ideas (fetch, bypass, cache lines, vector lanes) stay stable enough to debug from first principles.
Give the toy CPU a real ALU and a flags register. Programs are now written in assembly text. Your emulator first assembles the whole program, then executes it. Every arithmetic or logic instruction goes through one alu(op, a, b) function that returns the result and updates the Z, C and N flags.
Four 8-bit registers R0: R3, all 0 at start. Flags Z, C, N, all 0 at start.
| Instruction | Effect | Flags |
|---|---|---|
MOV Rd, imm | Rd = imm (0..255, decimal or 0x hex) | unchanged |
ADD Rd, Rs | Rd = Rd + Rs | Z, N; C = carry out of bit 7 |
SUB Rd, Rs | Rd = Rd - Rs | Z, N; C = 1 if no borrow (Rd >= Rs before the operation) |
AND / OR / XOR Rd, Rs | bitwise | Z, N; C = 0 |
NOT Rd | Rd = ~Rd | Z, N; C = 0 |
HALT | stop | - |
Z = result is 0; N = bit 7 of the result. Results are kept to 8 bits. Execution also stops after the last line.
One instruction per line; mnemonics and register names are upper case, operands are separated by a comma and optional spaces. Blank lines are ignored.
One line per executed instruction, echoing it in canonical form (OP Rd, Rs / OP Rd, imm with a decimal immediate / NOT Rd / HALT):
cLoading…
Then a final line R0=a R1=b R2=c R3=d Z=z C=c N=n.
If any line fails to assemble, execute nothing and print only error line L: MESSAGE for the first bad line (L counts all lines from 1, including blank ones). The possible messages are unknown instruction WORD, bad operands and immediate out of range.
Input:
cLoading…
Output:
cLoading…
Hidden tests cover a borrow in SUB (C=0), an ADD that carries out to exactly 0, operands with no space after the comma, a blank line, instructions after HALT that must not run, and a program whose first bad line (counting a blank line) is an unknown instruction.