Custom VM & compiler sandbox – language reference, examples, and how-to. Updated: 2025-08-04
MXVM is a custom virtual machine, interpreter, and compiler project designed as a learning sandbox. It is not intended for production; instead it helps users explore and experiment with:
The language combines assembly-like instructions with higher-level constructs like functions, modules, and formatted I/O to create an accessible pedagogical VM environment.
cmake (version 3.15+ recommended)# Clone or go into your copy of the repo
cd MXVM-main/MXVM-main
mkdir build
cd build
cmake ..
make
This produces the compiler/interpreter binary (e.g., src/vm/mxvmc) and supporting modules.
# from inside the build directory, run an example like demo.mxvm
./src/vm/mxvmc ../mxvm_src/demo.mxvm
Replace demo.mxvm with other examples such as fibonacci.mxvm, Comments.mxvm, or your own programs.
The entry point is a program block. Objects are reusable units that encapsulate code, data, and expose functions. Syntax:
program Name { ... }
object Name { ... }
Objects are referenced via section object.
io, string).Supported types include:
| Type | Description |
|---|---|
int | Integer value (typically 64-bit) |
float | Floating point value |
string | Null-terminated character sequences. Can specify size, e.g., string name, 256. |
ptr | Pointer to buffer or memory region |
byte | 8-bit value |
export prefix | Marks data as externally visible from an object. |
Example:
section data {
int x = 0
string message = "Hello, MXVM!\n"
}
Define functions with the function name: syntax. Use call to invoke and ret to return. Labels serve as jump targets.
section code {
start:
call some_function
done
function some_function:
; work
ret
}
Detailed description of available instructions:
| Instruction | Operands | Description | Example |
|---|---|---|---|
mov | dest, src | Copy src into dest. | mov x, 1 |
load | dest, base, index, size | Load memory at base+index size bytes into dest. | load character, buffer, loop_index, 1 |
store | value, base, index, size | Store value into memory at base+index. | store zero_byte, buffer, file_size, 1 |
add | dest, value | Add value to dest. | add x, 1 |
sub | dest, value | Subtract value from dest. | sub x, 1 |
mul | dest, value | Multiply dest by value. | mul x, 2 |
div | dest, value | Divide dest by value. | div x, 2 |
mod | dest, value | Compute dest %= value. | mod accum, 2 |
or | dest, value | Bitwise OR. | or a, b |
and | dest, value | Bitwise AND. | and a, mask |
xor | dest, value | Bitwise XOR. | xor a, b |
not | dest | Bitwise NOT. | not flag |
neg | dest | Arithmetic negation. | neg x |
cmp | a, b | Compare a and b; sets flags for conditionals. | cmp x, 10 |
jmp | label | Unconditional jump. | jmp loop |
je | label | Jump if equal. | je done |
jne | label | Jump if not equal. | jne start |
jl | label | Jump if less than. | jl smaller |
jle | label | Jump if less or equal. | jle end |
jg | label | Jump if greater than. | jg bigger |
jge | label | Jump if greater or equal. | jge ok |
jz | label | Jump if zero. | jz zero_case |
jnz | label | Jump if not zero. | jnz nonzero |
print | format, args... | Formatted output. | print fmt, x |
getline | dest | Read line into buffer. | getline input |
alloc | ptr_var, count, size | Allocate memory. | alloc buffer,1,256 |
free | ptr | Free memory. | free buffer |
call | function | Invoke function. | call foo.init |
ret | none | Return from function. | ret |
invoke | external, args... | Call external runtime API. | invoke fopen, name, mode |
return | value | Set return value from invoke. | return size |
done | none | Program exit normally. | done |
exit | code | Exit with code. | exit 1 |
A simple program that increments a variable from 0 to 10, then decrements it back to 0, demonstrating loops, comparison, and conditional jumps.
program Demo {
section data {
int x = 0;
string fmt_str = "Hello World! Value of x is: %d\n";
string fmt_end = "Goodbye!\n";
}
section code {
mov x, 1
start:
add x, 1
print fmt_str, x
cmp x, 10
jne start
print fmt_end
loop:
sub x, 1
print fmt_str, x
cmp x, 0
jg loop
print fmt_end
stop:
done
}
}
Computes Fibonacci numbers up to n safely with range checking.
program Fibonacci {
section data {
int n = 30
int a = 0
int b = 1
int i = 2
string format = "%lld "
string newline = "\n"
}
section code {
cmp n, 0
jl out_of_range
print format, a
print format, b
loop:
cmp i, n
jge done_fib
add temp, a, b
mov a, b
mov b, temp
print format, b
add i, 1
jmp loop
done_fib:
print newline
done
}
}
Demonstrates external module usage, file I/O, buffer manipulation, and formatting with invoke, load, and store.
Key pattern: allocate a buffer, read file contents, and print bytes in hex.
Shows looping, formatted printing, and decrementing counters.
Minimal cheat sheet for writing MXVM programs:
// Program skeleton
program Name {
section module { io, string }
section object { other_object }
section data {
int counter = 0
string msg = "Hi\n"
}
section code {
mov counter, 5
start:
print msg, counter
done
}
}
Use labels and conditional jumps (jmp, je, etc.), call / ret for functions, and invoke to call external APIs.
Include built-in functionality by adding it to the module section. Example:
section module { io }
This enables functions like fopen, fread, fprintf, and formatted I/O.
cmp + conditional jumps with labels.function name: and invoke via call.invoke + return to communicate with C-style APIs.Watch comparisons feeding conditional jumps. Maintain stack discipline when using push / pop / stack_load / stack_store. Use clear format strings to trace state.