This project demonstrates a recursive factorial calculation implemented in both C and assembly language, running on a custom 16-bit RISC CPU emulator. The assignment showcases:
- C recursive function with a main driver program
- Executable memory layout in the virtual CPU
- Function call mechanism (CALL/RET instructions)
- Recursion execution with stack frame management
## Quick Start
### 1. Build the Tools
bash
make clean
make all
This creates:
build/assembler- Assembles .asm files to binarybuild/emulator- Executes binary programs on virtual CPU
gcc -o build/factorial_c programs/factorial.c
./build/factorial_c# Assemble
./build/assembler programs/factorial.asm /tmp/factorial.bin
# Execute
./build/emulator /tmp/factorial.binOutput:
=== Starting Execution ===
F(5) = 120
=== Execution Complete ===
Instructions executed: 90
./build/emulator /tmp/factorial.bin -dThis shows:
- Each instruction executed
- Register values after each step
- Stack pointer changes during recursion
- Memory addresses and program counter
./demo.shThis presents a step-by-step walkthrough of:
- C program with recursion
- Assembly translation
- Memory layout diagram
- Function call mechanism
- Recursion trace with stack visualization
- Actual execution trace
- R0: Return value / Accumulator
- R1: Function argument (n)
- R2: Temporary storage
- R3-R7: General purpose
- SP: Stack pointer (hardware managed)
- PC: Program counter (hardware managed)
- 0x0000-0x7FFF: Code section (32KB)
- 0x8000-0xEFFF: Data section (28KB)
- 0xF000-0xF0FF: Memory-mapped I/O (256 bytes)
- 0xF100-0xFFFF: Stack (3.75KB, grows downward)
- MOVI: Move immediate to register
- MOV: Move register to register
- PUSH/POP: Stack operations
- CALL/RET: Function calls
- ADD/SUB/MUL/DIV: Arithmetic
- CMPI: Compare immediate
- JZ/JNZ: Conditional jumps
- STORE: Memory-mapped I/O output
- HALT: Stop execution