This project was made around December, 2024 and is a simple virtual machine (VM) implemented in C. It provides a basic understanding of how virtual machines work by simulating a simplified computer architecture.
The VM has its own memory, registers, and a limited instruction set. It can load and execute a program written in its assembly-like language(yup had to re-invent assembly).
tankvm.h: Header file containing definitions for data structures and function prototypes.VMstruct: Represents the virtual machine, including:- Registers: 16-bit registers (
ax,bx,cx,dx,sp,ip) within thes_registerstruct. Opcodeenum: Defines the instruction set (e.g.,mov,nop,hlt).Instructionstruct: Represents a single instruction with an opcode and arguments.instrmap: A lookup table mapping opcodes to instruction sizes.
tankvm.c: Source file containing the implementation of the VM's functionality.virtualmachine(): Allocates and initializes a newVMstruct.exampleprogram(): Creates a sample program in VM's memory (hardcoded).execute(): The main execution loop that fetches, decodes, and executes instructions.execinstr(): Executes a single instruction based on its opcode.__mov(): Implements themovinstruction (move value into a register).map(): Returns the size of an instruction based on its opcode.error(): Handles errors like segmentation faults or halt instructions.- Utility functions:
copy(),zero(),printhex(). main(): Entry point that creates a VM, loads the example program, executes it, and prints memory contents.
Makefile: Defines the build process.
The VM supports the following instructions:
mov: Move a value into a register.nop: No operation.hlt: Halt the VM.
- Initialization: The
mainfunction creates aVMinstance usingvirtualmachine(). - Program Loading: The
exampleprogram()function creates a simple, hardcoded program in the VM's memory. - Execution: The
execute()function fetches instructions, decodes them, and executes them in a loop. - Termination: The program halts when a
hltinstruction is encountered.
To build the project, run:
makeThen go ahead and execute:
./tankvm- Limited instruction set.
- Hardcoded program.
- No input/output.
- Limited memory (65KB).
- Implement more instructions.
- Allow loading programs from a file.
- Add input/output capabilities.
- Improve error handling.