Master the fundamental concepts of cpu design through this focused micro-challenge.
You have read the whole brief, and the concepts above stay free on every task. Writing and running the code needs a plan.
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 worksA stack is a LIFO region of RAM accessed via a dedicated stack pointer (SP). PUSH decrements SP (on descending stacks) and stores a value; POP loads the value and increments SP. x86, ARM, and RISC-V all rely on stacks for function calls and local variables.
cLoading…
For example, pushing 0x42 then popping should return 0x42 and restore SP to its pre-push value.
For this exercise, you will add PUSH and POP opcodes that manipulate SP and memory. This task asks you to prepare the call stack that CALL/RET and subroutine local variables depend on in the next exercise.
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 hardware stack: a stack-pointer register SP plus PUSH and POP instructions over a small memory region. The stack is full-descending, as on ARM and x86: it grows toward lower addresses, and SP points at the most recently pushed item. Detect overflow and underflow instead of silently corrupting memory.
Registers R0: R3 (8-bit, start at 0). Stack memory is 8 bytes, addresses 0-7. SP starts at 8 (empty stack).
| Instruction | Effect |
|---|---|
MOV Rd, imm | Rd = imm (0..255) |
ADD Rd, Rs / SUB Rd, Rs | Rd = Rd ± Rs (mod 256) |
PUSH Rs | if SP == 0: overflow. Else SP = SP - 1; mem[SP] = Rs |
POP Rd | if SP == 8: underflow. Else Rd = mem[SP]; SP = SP + 1 |
JMP label | PC = label (labels as name: at the start of a line) |
OUT Rd | print Rd in decimal on its own line |
HALT | stop |
OUT lines, plus one line after every successful PUSH/POP:
cLoading…
The program ends with one of:
cLoading…
pc is the index of the faulting instruction (0-based, labels and blank lines don't count). You can assume the programs are well-formed and every jump target exists.
Input:
cLoading…
Output:
cLoading…
SP is a register and the stack lives in a memory array; don't use a separate C stack structure with its own length field.SP or memory.Hidden tests cover reversing values through the stack (LIFO order), an underflow on an empty stack, and an overflow caused by a loop that keeps pushing.