Rust · own instruction set · one file
Mirage assembles plain text into bytecode and runs it on a small stack machine. Its own instruction set, a text assembler, and a runner, with a value stack, a call stack, local variables, and no panics on bad input.
-
-
-
assembled and run in your browser, a port of the same assembler and stack machine below, not a server call
Push values, do arithmetic, branch on comparisons, call and return from labeled routines, store variables in local slots, print results. The whole pipeline, assembler and executor, fits in a handful of small files.
Labels, integer literals, and comments, parsed in two passes so forward and backward jumps both resolve.
Stack underflow, division by zero, an unknown opcode, an undefined label, all typed errors, never a panic.
A fixed 9 bytes per instruction after a short header, simple enough to disassemble by indexing straight into the stream.
| Instruction | Effect |
|---|---|
PUSH n | push integer literal |
POP / DUP | discard or duplicate the top value |
ADD SUB MUL DIV MOD NEG | arithmetic on the top of stack |
EQ LT GT | comparisons, push 1 or 0 |
LOAD n / STORE n | read or write local slot n |
JMP JZ JNZ | unconditional and conditional jumps |
CALL / RET | call a labeled routine and return |
PRINT / HALT | print the top value, stop execution |
Sum 1 through 10 on the value stack, using two local slots as accumulator and counter.
loop:
LOAD 1
PUSH 10
GT
JNZ done
LOAD 0
LOAD 1
ADD
STORE 0
LOAD 1
PUSH 1
ADD
STORE 1
JMP loop
done:
LOAD 0
PRINT
HALT
Assemble and run in one step, or produce a bytecode file and disassemble it back to readable instructions.
mirage run program.asm mirage asm program.asm -o program.mbc mirage disasm program.mbc