This project provides a simple 16-bit virtual machine (VM), a corresponding assembler, and a sample program. It's a demonstration of basic computer architecture concepts.
- 16-bit Architecture: The VM uses 16-bit words for instructions, registers, and memory addressing.
- 8 General-Purpose Registers:
R0throughR7. - 64KB of Memory: The VM has a 65,536-word address space.
- Stack: A basic stack implementation for
PUSHandPOPoperations. - Assembler: A tool to convert human-readable assembly files (
.asm) into machine-readable binary code for the VM. - Test Suite: A set of unit tests to verify the VM's functionality.
The VM supports the following instructions:
| Mnemonic | Operands | Description |
|---|---|---|
HALT |
Stops the VM's execution. | |
MOVI |
R, val |
Moves an immediate value (val) into a register (R). |
MOV |
R1, R2 |
Copies the value from register R2 to R1. |
ADD |
R1, R2, R3 |
Adds R2 and R3, stores the result in R1. |
SUB |
R1, R2, R3 |
Subtracts R3 from R2, stores the result in R1. Sets ZF. |
PUSH |
R |
Pushes the value of register R onto the stack. |
POP |
R |
Pops a value from the stack into register R. |
JMP |
addr |
Unconditionally jumps to the specified memory addr. |
CMP |
R1, R2 |
Compares R1 and R2, setting the Zero Flag (ZF) if equal. |
JZ |
addr |
Jumps to addr if the Zero Flag (ZF) is set. |
JNZ |
addr |
Jumps to addr if the Zero Flag (ZF) is not set. |
This project uses CMake.
Prerequisites:
- A C++ compiler (like GCC, Clang, or MSVC)
- CMake (version 3.10 or higher)
Build Steps:
-
Create a build directory:
mkdir build cd build -
Configure the project:
cmake ..
-
Build the project:
- On Windows (with Visual Studio):
cmake --build . --config Release - On Linux/macOS:
make
- On Windows (with Visual Studio):
This will create three executables in the build/Release (or build) directory: vm, assembler, and vm_tests.
-
Write an assembly program (e.g.,
my_program.asm). You can usesamples/loop_demo.asmas a starting point. -
Assemble the program using the
assemblertool:# From the build directory ./assembler ../samples/loop_demo.asm loop_demo.binThis will create a binary file (
loop_demo.bin) that the VM can execute. -
Run the program with the
vmexecutable:./vm loop_demo.bin
For example, running the
loop_demo.asmprogram will output:R2 = 3
To verify that the VM is working correctly, you can run the test suite using CTest.
From the build directory, run:
ctest -C Release --output-on-failureAll tests should pass.