This collection contains a set of exercises designed to help understand how a simple CPU works.
The tasks are ordered by difficulty and focus on different core concepts:
- Registers & data movement
- Arithmetic & logic operations
- Control flow (jumps)
- Stack usage
- Function calls
- Memory access
Swap the values of register A and B.
Example: A = 5 B = 3 → A = 3 → B = 5
Hint: You can use the stack or a temporary register.
Add two numbers stored in registers A and B. Store the result in A.
Shift the value in register A to the left and then to the right.
Goal:
Understand how SHL and SHR affect values.
Count from 1 to N using a loop.
Goal:
Use CMP, JZ, JNZ, or similar jump instructions.
Compute the sum from 1 to N using a loop (not the mathematical formula).
Example: N = 5 → Result = 15
Concepts:
- Looping
- Accumulation
Compare two values (A and B) and store the larger one in A.
Swap A and B using only stack operations (PUSH, POP).
Push two constants onto the stack, pop them into registers, and add them.
Example: PUSH 5 PUSH 3 POP A POP B ADD A,B
Write a function that adds 2 to register A.
Example: CALL addTwo
Pass a value via the stack to a function that doubles it.
Concepts:
- Parameter passing
- Stack usage
Write a function that multiplies two values without using a dedicated multiply instruction.
Pass the two input values via the stack.
The function should compute the result (e.g., using repeated addition), push the result onto the stack, and return.
Concepts:
- Parameter passing via stack
- Stack usage (PUSH/POP)
- Function calls (CALL/RET)
- Loop-based multiplication (repeated addition)
Compute the factorial of N using a loop.
Example: 5! = 120
Generate the Fibonacci sequence up to N.
Example: f0 = 0, f1=1, f2=1, 2, 3, 5, 8, ...
Use multiple function calls (CALL/RET) and verify correct return behavior.
Load a value from a memory address into a register.
Example: Memory[100] = 5 → A = 5
Concepts:
- Memory addressing
- LOAD instruction
Store a value from a register into memory.
Example: A = 42 → Memory[101] = 42
Concepts:
- STORE instruction
- Data persistence
Load a value from one memory address and store it into another.
Example: Memory[100] → Memory[101]
Load a value from memory and add it to a register.
Example: A = 3 Memory[100] = 5 → A = 8
Read two values from memory, add them, and store the result back in memory.
Example: Memory[100] = 4 Memory[101] = 6 → Memory[102] = 10
Load N from memory, compute the sum from 1 to N, and store the result back in memory.
Concepts:
- Memory + loops + arithmetic combined
- Detect stack underflow/overflow
- Implement a small stack-based calculator
- Optimize code for minimal register usage
These exercises are designed to help you understand:
- How data moves inside a CPU
- How limited registers affect program design
- How control flow works at a low level
- How stacks enable function calls
Enjoy exploring the inner workings of a CPU 🚀