Skip to content

About

this is 16bit virtual machine build with c++

Resources

Stars

1 star

Watchers

0 watching

Forks

Latest commit

 

History

2 Commits

Folders and files

Repository files navigation

Simple 16-Bit Virtual Machine

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.

Features

  • 16-bit Architecture: The VM uses 16-bit words for instructions, registers, and memory addressing.
  • 8 General-Purpose Registers: R0 through R7.
  • 64KB of Memory: The VM has a 65,536-word address space.
  • Stack: A basic stack implementation for PUSH and POP operations.
  • 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.

Instruction Set

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.

How to Build

This project uses CMake.

Prerequisites:

  • A C++ compiler (like GCC, Clang, or MSVC)
  • CMake (version 3.10 or higher)

Build Steps:

  1. Create a build directory:

    mkdir build
    cd build
  2. Configure the project:

    cmake ..
  3. Build the project:

    • On Windows (with Visual Studio):
      cmake --build . --config Release
    • On Linux/macOS:
      make

This will create three executables in the build/Release (or build) directory: vm, assembler, and vm_tests.

How to Run

  1. Write an assembly program (e.g., my_program.asm). You can use samples/loop_demo.asm as a starting point.

  2. Assemble the program using the assembler tool:

    # From the build directory
    ./assembler ../samples/loop_demo.asm loop_demo.bin

    This will create a binary file (loop_demo.bin) that the VM can execute.

  3. Run the program with the vm executable:

    ./vm loop_demo.bin

    For example, running the loop_demo.asm program will output:

    R2 = 3
    

How to Run Tests

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-failure

All tests should pass.

About

this is 16bit virtual machine build with c++

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages