Wasm Hello World (wat2wasm) — WebAssembly

WAT is the human-readable text form of Wasm: (module (func $name (param i32) (result i32) ...) (export "name" (func $name))), with stack instructions such as…

In the first lesson, stayPrice showed up as 47 hexadecimal numbers inside a Uint8Array. Nobody writes bytes like that by hand. People write WAT (WebAssembly Text Format), the human-readable text form of Wasm. Then wat2wasm, one of the tools in wabt (the WebAssembly Binary Toolkit), assembles that text into a .wasm file full of bytes.

The same thing happens at Mist Homestay's front desk. Mrs. Sri writes a guest's booking in a notebook: "Terrace Room, 2 nights". The receptionist then copies it into the booking system's form, whose boxes come in a fixed, numbered order: the kind of guest in box 1, the list of rooms in box 3, the names that may be called in box 7, the details in box 10. The content is exactly the same, only the shape differs. The notebook entry is WAT, the form is .wasm, and the receptionist is wat2wasm.

WAT: text for a stack machine

This is Mist Homestay's price-of-a-stay function, price.wat:

;; price.wat: the price of a stay at Mist Homestay
(module
  (func $stayPrice (param $nights i32) (param $pricePerNight i32) (result i32)
    local.get $nights         ;; stack: [nights]
    local.get $pricePerNight  ;; stack: [nights, pricePerNight]
    i32.mul)                  ;; stack: [nights × pricePerNight]
  (export "stayPrice" (func $stayPrice)))

Wasm has only four number types: i32, i64 (32- and 64-bit integers), f32, f64 (floating point). Instructions always name their type: i32.mul is not the same as f64.mul. One more stack example, (2 + 3) × 4:

Instruction Stack afterwards
i32.const 2 [2]
i32.const 3 [2, 3]
i32.add [5]
i32.const 4 [5, 4]
i32.mul [20]

Assembling with wat2wasm

wabt is released on GitHub (WebAssembly/wabt) for macOS, Linux and Windows. This lesson uses release 1.0.42 (September 2026), unpacked into a folder of its own. brew install wabt and apt install wabt exist too, but their version can be older.

$ wat2wasm --version
1.0.42
$ wat2wasm price.wat -o price.wasm
$ xxd price.wasm
00000000: 0061 736d 0100 0000 0107 0160 027f 7f01  .asm.......`....
00000010: 7f03 0201 0007 0d01 0973 7461 7950 7269  .........stayPri
00000020: 6365 0000 0a09 0107 0020 0020 016c 0b    ce....... . .l.

wat2wasm also checks the types before it writes anything. Forget the local.get $pricePerNight line and i32.mul finds only one number on the stack:

price.wat:5:5: error: type mismatch in i32.mul, expected [i32, i32] but got [i32]
    i32.mul)                  ;; stack: [nights × pricePerNight]
    ^^^^^^^

Taking the 47 bytes apart

A .wasm file always starts with 8 opening bytes, then numbered sections. Each section starts with its number and its size in bytes.

Offset Bytes Meaning
0x00 00 61 73 6d magic number: \0asm, marking a Wasm file
0x04 01 00 00 00 binary format version: 1
0x08 01 07 01 type section (1), 7 bytes, holding 1 function type
0x0b 60 02 7f 7f 01 7f function type (60): 2 parameters i32 i32 (7f = i32), 1 result i32
0x11 03 02 01 00 function section (3), 2 bytes: 1 function, using type number 0
0x15 07 0d 01 09 export section (7), 13 bytes: 1 export, its name is 9 bytes
0x19 73 74 61 79 50 72 69 63 65 stayPrice
0x22 00 00 export kind 00 = function, function number 0
0x24 0a 09 01 07 00 code section (10), 9 bytes: 1 function body 7 bytes long, 0 extra local variables
0x29 20 00 20 01 6c 0b local.get 0, local.get 1, i32.mul (6c), end (0b)

wasm-objdump -d price.wasm (also from wabt) shows that last part:

000028 func[0] <stayPrice>:
 000029: 20 00                      | local.get 0
 00002b: 20 01                      | local.get 1
 00002d: 6c                         | i32.mul
 00002e: 0b                         | end

The byte 6c at offset 0x2d is i32.mul. Guess before you read on: if that byte is changed to 6a, what does stayPrice(2, 3500) return?

It returns 3502. 6a is i32.add, so Wasm now adds instead of multiplying. Change it to 6b (i32.sub) and the result is -3498. The names $nights and $pricePerNight are not in these 47 bytes. Turn the file back with wasm2wat price.wasm and you get local.get 0 and local.get 1. To keep the names, assemble with wat2wasm --debug-names: the file becomes 96 bytes because a name section is added.

Loading it from JavaScript

When you already hold the bytes (in a Uint8Array, or from fetch and then arrayBuffer()), use WebAssembly.instantiate:

const bytes = new Uint8Array([0x00, 0x61, 0x73, 0x6d, /* ...the 47 bytes of price.wasm... */]);

WebAssembly.instantiate(bytes).then(({ module, instance }) => {
  const { stayPrice } = instance.exports;
  console.log(stayPrice(2, 3500)); // 7000
});

When the file is on a server, the most efficient way is instantiateStreaming. It compiles while the file is still downloading, then returns a Promise holding the module and the instance:

WebAssembly.instantiateStreaming(fetch('price.wasm'), imports).then(({ instance }) => {
  const { stayPrice } = instance.exports;
  console.log(stayPrice(2, 3500)); // 7000
});

Other options for loading, including a third way for Node only:

Way When to pick it
instantiateStreaming(fetch(...)) The default in the browser: the file is on a server, and it compiles while downloading.
instantiate(bytes) You already hold the bytes, or the server cannot send application/wasm.
import { stayPrice } from './price.wasm' A Node script on 24.5 or later (or 22.19 or later), with no flag. Browsers do not support this kind of import yet (MDN browser-compat-data 8.1.9).

In Node 24.16, this file prints 7000, plus the warning ExperimentalWarning: Importing WebAssembly module instances is an experimental feature and might change at any time:

// load.mjs, in the same folder as price.wasm
import { stayPrice } from './price.wasm';

console.log(stayPrice(2, 3500)); // 7000

⚠️ Why the function is missing or the answer is wrong

TL;DR: WAT is the human-readable text form of Wasm: (module (func $name (param i32) (result i32) ...) (export "name" (func $name))), with stack instructions such as local.get, i32.add, i32.mul. wat2wasm price.wat -o price.wasm (wabt 1.0.42) assembles it into 47 bytes: the magic 00 61 73 6d, the version 01 00 00 00, then the type (1), function (3), export (7) and code (10) sections, where 20 00 20 01 6c 0b means local.get 0, local.get 1, i32.mul, end. Change 6c to 6a and the result is 350002. The $ names are not stored unless you use --debug-names. From JavaScript, use WebAssembly.instantiate(bytes) for bytes you already have, or WebAssembly.instantiateStreaming(fetch("price.wasm"), imports) for a file from a server sent with Content-Type: application/wasm.

⚡ WAT: text for a stack machine

Write:

(module
  (func $stayPrice (param $nights i32) (param $pricePerNight i32) (result i32)
    local.get $nights         ;; stack: [nights]
    local.get $pricePerNight  ;; stack: [nights, pricePerNight]
    i32.mul)                  ;; stack: [nights × pricePerNight]
  (export "stayPrice" (func $stayPrice)))

Meaning: local.get puts a parameter's value on top of the stack, and i32.mul takes the top two numbers and multiplies them. The number left on the stack is what the function returns. Without (export ...), JavaScript cannot see the function.

⚡ Assemble it into 47 bytes

Write:

$ wat2wasm price.wat -o price.wasm

Meaning: wat2wasm checks the types first, then writes the bytes. The result is 47 bytes: the magic 00 61 73 6d, the version 01 00 00 00, then the type, function, export and code sections. The name $nights is not stored: in the binary file a parameter only has an index.

⚡ One byte changes the answer

Guess first: the byte 6c (i32.mul) at offset 0x2d is changed to 6a. What does stayPrice(2, 3500) return?

3502. 6a is i32.add, so Wasm now adds instead of multiplying.

Meaning: Every Wasm instruction is just one byte or a few. wasm-objdump -d price.wasm shows which bytes are which instruction.

⚡ Load the file from a server

Write:

WebAssembly.instantiateStreaming(fetch('price.wasm'), imports).then(({ instance }) => {
  const { stayPrice } = instance.exports;
  console.log(stayPrice(2, 3500)); // 7000
});

Meaning: instantiateStreaming compiles while the file is still downloading. The server has to send Content-Type: application/wasm. When you already hold the bytes, use WebAssembly.instantiate(bytes).

What you will learn