A simple stack-based virtual machine implemented in C. This project is designed to help you understand how virtual machines work at a low level, including instruction fetching, decoding, and execution.
- Stack-Based Design: Uses a stack for arithmetic and data manipulation.
- Basic Instruction Set: Supports instructions like
PSH,POP,ADD,SUB,MUL,DIV,SET,LWD,STO, andHLT. - File-Based Programs: Reads programs from
.asifiles (Assembly Instruction). - Registers: Includes general-purpose registers (
A,B,C,D,E,F) and special-purpose registers (IP,SP). - Memory System: Supports storing and loading values from memory addresses.
| Instruction | Description | Example |
|---|---|---|
PSH <val> |
Push a value onto the stack | PSH 10 |
POP |
Pop the top value from the stack | POP |
ADD |
Add the top two stack values | ADD |
SUB |
Subtract the top two stack values | SUB |
MUL <val> |
Multiply the top stack value by <val> |
MUL 2 |
DIV <val> |
Divide the top stack value by <val> |
DIV 2 |
SET <reg>, <val> |
Set a register to a value | SET A, 10 |
LWD <reg> |
Load value from a register onto the stack | LWD A |
STO <addr> |
Store the top stack value at memory address <addr> |
STO 0 |
HLT |
Halt the program | HLT |
- A C compiler (e.g.,
clang,gcc). - GNU Make (for building the project).
-
Clone the repository:
git clone https://github.com/iliasreg/vmi.git cd vmi -
Build the project using
make:make
-
Run the virtual machine:
./vm
Programs are written in .asi files. Each line contains an instruction. Comments start with ;.
; Add 5 and 10 and print the result
PSH 5 ; Push 5 to the stack
PSH 10 ; Push 10 to the stack
ADD ; Pop 5 and 10, push 15
POP ; Print the result (15)
HLT ; Stop the program
- Save the program in a file (e.g.,
test.asi). - Run the VM with the program:
./vm test.asi
Popped Value is: 15
vmi/
├── vscode/ // VS Code configuration files
├── helpers/ // Helper functions and utilities
│ ├── helpers.c // Implementation of helper functions
│ └── helpers.h // Header for helper functions
├── readers/ // File reading and lexing
│ ├── file_lexer.c // Implementation of file lexer
│ └── file_lexer.h // Header for file lexer
├── src/ // Source code
│ ├── main.c // Entry point (minimal code)
│ └── vm/ // Virtual machine core logic
│ ├── vm.c // Implementation of VM instructions
│ └── vm.h // Header for VM logic
├── .gitignore // Git ignore file
├── code.asi // Example assembly program
├── Makefile // Build system
└── README.md // Project documentation
- Instruction Set: Added
STO,LWD,MUL,DIV, andSUBinstructions. - Example Program: Updated to demonstrate
STOandLWD. - Project Structure: Added
file_lexer.handfile_lexer.cto reflect the file reading system. - Future Improvements: Added plans for control flow and debugging tools.
Let me know if you’d like further tweaks or additions! 🚀
Contributions are welcome! If you'd like to contribute:
- Fork the repository.
- Create a new branch for your feature:
git checkout -b feature/your-feature. - Commit your changes:
git commit -m "Add your feature". - Push to the branch:
git push origin feature/your-feature. - Submit a pull request.
This project is licensed under the MIT License. See LICENSE for details.