AA
aadityansha06/VM-in-C
virtual machine in C
LC-3 Virtual Machine
A simple virtual machine implementation in C that simulates the LC-3 (Little Computer 3) architecture.
Overview
This project implements a functional LC-3 VM that can load and execute LC-3 machine code. It includes a complete instruction set implementation and debugging utilities.
Features
- 16-bit instruction set architecture
- 8 general-purpose registers (R0-R7)
- Program Counter (PC) and Condition Register (COND)
- Memory-mapped I/O
- Support for all LC-3 instructions:
- Arithmetic: ADD, AND, NOT
- Memory: LD, LDI, LDR, LEA, ST, STI, STR
- Control: BR, JSR, JMP, RTI, TRAP
- Debug utilities for viewing memory and registers
Files
vm.c- Main VM implementationvm_debug.c- Debug utility functionsvm_debug.h- Debug function declarationsprog.c- Example program generator
Compilation
Compile the VM:
gcc -Wall -std=c11 vm.c vm_debug.c -o vmCompile the example program generator:
gcc -Wall -std=c11 prog.c -o simple_programUsage
1. Generate a program file:
./simple_programThis creates simple_program.obj.
2. Run the VM:
./vm simple_program.objOutput
The VM will display:
- Memory state after program load
- Memory state after execution
- Final register values
Architecture
The VM simulates:
- Memory: 65,536 addressable 16-bit locations
- Registers: 8 general purpose + PC + COND
- Instruction Format: 16-bit instructions with 4-bit opcode