Master the fundamental concepts of webassembly internals through this focused micro-challenge.
Wasm execution is specification-defined stack semantics. Your interpreter maintains an operand stack, locals array, and structured control frames for blocks and branches.
Core patterns:
Validation (next task) proves stack depth correct before you run. Execution pops operands, pushes results, and updates \`pc\` through the current function body.
For example, \`(param i32 i32) (result i32)\` with body \`local.get 0; local.get 1; i32.add\` leaves one \`i32\` on the stack.
\`\`\` stack: [] -> [a] -> [a,b] -> [a+b] \`\`\`
Structured control (`block`, `loop`, `if`) requires a control stack in addition to the operand stack. Branch depths index into that stack; getting depth wrong traps at runtime even after validation passes.
This exercise asks you to implement a Wasm interpreter for a curated opcode subset. You will execute validated function bodies with an explicit operand stack matching the spec's stack effects.
You will use the same mental model here when reading production interpreter source later in the track. Sketch one concrete input on paper, predict the outcome, then confirm with code. That discipline catches logic errors early and makes debugging far faster when you extend the implementation in follow-on tasks.
Write a C program that implements a Wasm stack machine interpreter for a subset of instructions and executes a simple function.
Requirements:
Three hints are available for this task, revealed one at a time inside the code workspace so you can struggle productively before seeing them.
All starter code and reference implementations are available for your local setup.
View on Github