A simple virtual machine (VM) that executes a custom bytecode instruction set. The BVM supports a variety of operations, including arithmetic, stack manipulation, flow control, and system calls. To program this vm see https://github.com/BlagojeBlagojevic/blang.
- Stack-based architecture
- Supports integer, float, and character operations
- Arithmetic operations (ADD, MUL, DIV, MOD, DEC, INC)
- Bitwise operations (SHR, SHL, OR, AND, BNOT)
- Stack manipulation (PUSH, POP, DUP, OVER, ROT, SWAP)
- Flow control (IF, JMP, JMPT, JMPF,)
- Memory operations (MEM, COPY, COPYSTACK, MEMSTACK)
- System calls (WRITE, READ, EXIT, SYSTEM, etc.) via
SYSCALLS
define - FFI(DRIVER)
- Input/output operations (PRINT, PRINTSTRING, PRINTSTACK)
- A C compiler (GCC, Clang, etc.)
-
Clone the repository:
git clone https://github.com/BlagojeBlagojevic/BVM.git cd BVM
-
Compile the project (enable syscalls with
-DSYSCALLS -DSYSTEM
if needed):gcc -o bvm main.c -DSYSCALLS -DSYSTEM
Execute a program from a text file:
./bvm program.txt
PUSH <value>
: Push an integer onto the stack.PUSHF <value>
: Push a float onto the stack.PUSHIP
: Push the current instruction pointer (IP) onto the stack.PUSHSP
: Push the stack pointer (SP) onto the stack.POP
: Remove the top value from the stack.DUP
: Duplicate the top stack value.OVER
: Copy the second stack item to the top.ROT
: Rotate the top three stack items.SWAP_NO
: Swap the top two stack values.SWAP <index>
: Swap the top value with the value atindex
.
ADD <type>
: Add top two values.type
: 0 (int), 1 (float), 2 (uint).MUL <type>
,DIV <type>
,DEC <type>
,MOD
: Multiply, divide, decrement, modulo.INC <value>
: Increment the top value byvalue
.
SHR
,SHL
: Shift right/left.OR
,AND
,BNOT
: Bitwise OR, AND, NOT.
IF <cond>
: Compare top two values.cond
: '>' (0), '<' (1), '==' (2). Push 1/0.JMP <address>
: Jump toaddress
.JMPT <address>
: Jump if top value is non-zero.JMPF <address>
: Jump if top value is zero.SETSP <index>
: Set stack pointer toindex
.RESTORE <new_sp>
: Restore SP to <new_sp> and IP from the stack..
COPY <index>
: Copy value atindex
to the top.MEM <index>
: Store top value atindex
.COPYSTACK <memaddres>
: Copy value from memaddres to a SP-1MEMSTACK <memaddres, value>
: Copy value to a mem addres
PRINT <type>
: Print top value.type
: 0 (int), 1 (float), 2 (char).PRINTSTRING
: Print null-terminated string from the stack.PRINTSTACK
: Dump the entire stack.
WRITE
: Write to a file descriptor (args: fd, data, size).READ
: Read from a file descriptor.EXIT
: Terminate the VM with a status code.SYSTEM
: Execute a shell command (requiresSYSTEM
define).
DRIVER
: Calls a device driver function specified by the driver number.
NOP
: No operation.HALT
: Pause execution (debugging).END
: Terminate the VM gracefully.
PUSH 10
PUSH 20
ADD 0
PRINT 0
PUSH 65
PRINT 2 # Prints 'A'
PUSH 0
PUSH 1
IF >
JMPT 7 # Jump to HALT if 1 > 0
PRINT 0
HALT
END
MIT License. See LICENSE for details.