A simple virtual machine (VM) that executes a custom bytecode instruction set. The BVM supports a variety of operations, including arithmetic, stack manipulation, and flow control.
- Stack-based architecture
- Supports integer, float, and pointer operations
- Basic arithmetic operations (ADD, MUL, DIV, DEC, INC)
- Stack operations (PUSH, POP, DUP, SWAP)
- Flow control operations (IF, JMP, JMPT, SETSP)
- Input/output operations (PRINT)
- Instruction set includes NOP and HALT
- Simple disassembler and program loader
- A C compiler (GCC, Clang, etc.)
-
Clone the repository:
https://github.com/BlagojeBlagojevic/BVM.git cd bvm
-
Compile the project:
gcc -o bvm main.c bvm.c
To run the VM with a program:
./bvm program.txt
program.txt
should contain the bytecode instructions.
The VM supports the following instructions:
PUSH
: Push an integer onto the stack.PUSHF
: Push a float onto the stack.PUSHIP
: Push the instruction pointer onto the stack.POP
: Pop the top value from the stack.PRINT
: Print the top value on the stack.ADD
: Add the top two values on the stack.MUL
: Multiply the top two values on the stack.DEC
: Decrement the top value on the stack.DIV
: Divide the top two values on the stack.DUP
: Duplicate the top value on the stack.IF
: Perform a comparison and push the result.JMP
: Jump to a specific instruction.JMPT
: Jump to a specific instruction if the top value on the stack is true.SETSP
: Set the stack pointer.COPY
: Copy a value to a specific stack position.SWAP
: Swap the top value with a specific stack position.SWAPF
: Swap the top float value with a specific stack position.NOP
: No operation.HALT
: Halt the VM.INC
: Increment the top value on the stack.NEWLINE
: No operation (for readability).END
: End the program.
initBVM()
: Initialize the BVM.executeInstruction(Bvm *bvm)
: Execute a single instruction.loop(Bvm *bvm)
: Run the instruction execution loop.textToProgram(const char* name, Instruction *instruction)
: Load a program from a text file.dissasembler(Bvm *bvm)
: Disassemble the loaded program (not implemented).programToBin(const char* name, Instruction *instruction, u64 numOfInstruction)
: Save a program to a binary file.binToProgram(const char* name, Instruction *instruction)
: Load a program from a binary file.
Example of a simple program in program.txt
:
PUSH 10
PUSH 20
ADD 0
PRINT 0
END
This program pushes two integers onto the stack, adds them, prints the result, and ends the execution.
This project is licensed under the MIT License - see the LICENSE file for details.
Feel free to submit issues, fork the repository and send pull requests!