# Ch 15: A Virtual Machine

> Study guide to Crafting Interpreters chapter 15. The virtual machine: the instruction pointer, the main dispatch loop, a value stack, and arithmetic…

Updated October 20, 2026.

## Summary {#summary}

Crafting Interpreters chapter 15, A Virtual Machine: The virtual machine: the instruction pointer, the main dispatch loop, a value stack, and arithmetic instructions, with tracing of the stack as it runs. Budget about 5 hours; it is a reading chapter, with no tests to pass.

## Key facts {#key-facts}

- Week 13 of 145 on the cracked.guide path, in [Crafting Interpreters](https://cracked.guide/crafting-interpreters).
- Time budget: about 5 hours.
- Part of the book: clox (bytecode VM, C).
- No test suite for this chapter: you mark it done yourself.
- 4 challenges at the end of the chapter.
- Terms it teaches: [Bytecode VM](https://cracked.guide/glossary/bytecode-vm).

## Week 13 goals {#objectives}

By the end of week 13 you can:

- Implement the VM's dispatch loop with an instruction pointer and value stack.
- Trace the value stack as arithmetic instructions push and pop.
- Explain why a binary operator pops its right operand first.

## This week's steps {#steps}

- Read the chapter: Enable the execution trace and step through a few hand-built chunks.
- Write the code in your lox repo: Type it into clox/ (C) yourself as you read, and get it running.
- Do the challenges (1–4): Challenge 1 (write out bytecode for several expressions) is a quick and useful exercise.

## Why it matters {#why}

This loop is the beating heart of every bytecode interpreter. Understanding stack machines also helps you read JVM or WebAssembly bytecode.

## How to approach it {#approach}

Enable the execution trace and step through a few hand-built chunks. Notice how each instruction pushes and pops.

## Challenges {#challenges}

4 challenges at the end of the chapter.

Challenge 1 (write out bytecode for several expressions) is a quick and useful exercise. Challenge 3 (grow the stack dynamically) and challenge 4 (negate in place) are short C exercises worth doing.

## Common traps {#traps}

- Stack overflow with the fixed-size stack, since pushes are unchecked.
- Pop order for binary operators: the right operand is on top.

## Links {#links}

- [Read the chapter: Ch 15: A Virtual Machine](https://craftinginterpreters.com/a-virtual-machine.html)
- [Official repo: book source, both interpreters, test suite](https://github.com/munificent/craftinginterpreters)
- [Wikipedia: Stack machine](https://en.wikipedia.org/wiki/Stack_machine)

---

Source: https://cracked.guide/guide/ci-15
cracked.guide: A free, dated 145-week path to getting genuinely cracked at programming.
