Skip to content

12 Jul 2026 1 min read

Writing a Tiny Stack VM in C

A walkthrough of a minimal bytecode virtual machine, from opcode design to a working REPL.

Every systems programmer eventually writes a tiny virtual machine. This one is about 300 lines of C and supports a handful of opcodes: push, add, sub, mul, and print.

The instruction set

We keep the opcode table small on purpose.

OpcodeMeaningStack effect
PUSHpush a literal() -> (a)
ADDpop two, add(a, b) -> (a+b)
PRINTpop and print(a) -> ()

Note

The stack is a fixed-size array of int64_t. No bounds checking yet — that’s a good exercise for the reader.

The core loop

typedef enum { OP_PUSH, OP_ADD, OP_SUB, OP_MUL, OP_PRINT, OP_HALT } OpCode;

void vm_run(VM *vm) {
    for (;;) {
        uint8_t op = vm->code[vm->ip++];
        switch (op) {
            case OP_PUSH:
                vm_push(vm, vm->code[vm->ip++]);
                break;
            case OP_ADD: {
                int64_t b = vm_pop(vm);
                int64_t a = vm_pop(vm);
                vm_push(vm, a + b);
                break;
            }
            case OP_PRINT:
                printf("%lld\n", vm_pop(vm));