A minimal virtual machine created for didactic purposes.
The specification is in the VM source.
There is also an assembler written in AWK, supporting labels and some additional directives, see an example.
The virtual machine is simple, but quite expressive - it can interpret Brainfuck, for example. Even recursion can be done with a little magic.
A universal machine can emulate itself, i.e., it can run (short) MiniVM programs.
The 240-byte file size limit is just enough for a "Code 39" (ISO/IEC 16388:2023) barcode generator (at most 15 characters):
It can display the bifurcation diagram of the logistic map, despite the lack of floating point numbers (or multiplication):
A little PostScript program generates punch cards for any program you write (see an example).
There is a disassembler written by an AI - but it can be done even in the machine itself: a quine example prints its own (disasembled) source code.
Finally, there is a JavaScript version that runs in your browser (also vibe-coded).
For a not-so-minimal version, consider (for example) the following:
Consider using a larger cell size (16 or 32 bit). To keep it concise, an instruction and the associated parameter can be encoded in one cell (the opcode in the high bits, for example). Using 4 bits for the instructions, you can index 2^(16-4) = 4096 cells, and there is also space to create 6 new instructions! (Similarly for the 32 bit version.)
| 16-bit version | 32-bit version | |
|---|---|---|
| Layout | 4 bit opcode + 12 bit data | 6 bit opcode + 26 bit data |
| Memory | 4K (4096) cells = 8 KB | 64M cells = 256 MB |
| Instructions | 16 | 64 |
For new instructions, some recommendations are:
-
Direct memory read (
SET's pair):LOAD -
Jumps, e.g.
JMP,JZ, also indirect jumpJTV(jump-to-value) -
Arithmetics, e.g.
MUL,DIV,MOD -
Bit manipulations, e.g.
NAND,AND,OR,XOR,LSH,RSH
In the 32-bit version direct/indirect versions of all instructions can be added.