Master the fundamental concepts of webassembly internals through this focused micro-challenge.
Three hints are available for this task, revealed one at a time inside the code workspace so you can struggle productively before seeing them.
Every task includes starter code, theory, and hidden tests so you can implement and verify locally in the browser.
How it worksWebAssembly is a portable binary ISA. LLVM's \`clang --target=wasm32\` plus \`wasm-ld\` turns C into a \`.wasm\` module browsers and standalone runtimes can load.
Pipeline:
C symbols are hidden by default. Export with \`attribute((visibility("default")))\` or a linker \`--export\` flag so JavaScript or WASI can call \`add\`.
For example:
\`\`\`c attribute((export_name("add"))) int add(int a, int b) { return a + b; } \`\`\`
compiles to \`i32.add\` on two parameters in the Wasm stack machine.
Binary starts with magic \`\\0asm\` and version bytes, then type, function, export, and code sections.
Inspect both WAT and the raw binary hex for the same module. Seeing `00 61 73 6D` at offset zero confirms you are looking at Wasm, not a mislabeled object file from a wrong target triple.
This exercise asks you to document the clang/wasm-ld pipeline for a simple \`add\` function. You will show C source, linker commands, and the resulting WAT so you can read what the compiler produced.
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.
What clang --target=wasm32 + wasm-ld produce for int add(int a, int b) { return a + b; } is a 41-byte module. Build the back half of that pipeline: an assembler that turns functions written in WebAssembly text instructions into the binary format. The encoder must handle:
Print the result as an annotated hex dump.
;; starts a comment.
cLoading…
Instructions:
unreachable 00, nop 01, return 0F, call NAME 10, drop 1A;local.get/set/tee 20/21/22 (index);i32.const 41 (int32), i64.const 42;i32.eqz 45, i32.eq 46, i32.ne 47, i32.lt_s 48, i32.gt_s 4A;i32.add 6A, i32.sub 6B, i32.mul 6C, i32.div_s 6D, i32.rem_s 6F;i32.and 71, i32.or 72, i32.shl 74;i64.add 7C, i64.mul 7E, i32.wrap_i64 A7, i64.extend_i32_s AC.Every body ends with end (0B). Value types are i32 7F, i64 7E, f32 7D and f64 7C.
60, the param count, the params, the result count and the results. Functions with equal signatures share a type.00 (func) and the function index.n type per run of equal consecutive types), the instructions and 0B.cLoading…
%04x:, the bytes as %02x, then ; COMMENT.section ID (NAME), N bytes);N type(s), N export(s), N bod(y|ies)). The function section's count and type indices share one line, N function(s);body 1 (main): 29 bytes, 2 local groups: 2 x i32, 1 x i64);call NAME (func I) for calls.missing end, then unknown calls (in function order), then N error(s), no module written.
line N: unknown instruction Xline N: bad immediate X for OP (locals must be 0-255, and i32 constants must fit in 32 bits)line N: OP needs one immediate / line N: OP takes no immediateline N: local I out of range in Fline N: bad func header word X, line N: at most one result, line N: bad local type Xline N: func inside func F, line N: instruction outside a funcfunc F: missing end, func F: call to unknown function Xno functions.Input:
cLoading…
Output:
cLoading…
Hidden tests cover three functions with a shared type, grouped locals, multi-byte positive and negative constants (624485 and -123456), -64 versus 64, calls, and every kind of error.