Skip to main content

Hello World (first programs) · Intermediate · 16 min

Hello World in WebAssembly text format with wabt and Wasmtime

By Padding · Updated 18 Nov 2026
Ubuntu 26.04 · wabt 1.0.36 · Wasmtime 50.0.2

You will write a WebAssembly module by hand in the text format, assemble it with wat2wasm, print Hello World through WASI in Wasmtime 50, and call an exported add function from both Wasmtime and Node.js.

On this page

Almost nobody writes WebAssembly by hand. People write Rust, C, C++ or Go and let a compiler produce a .wasm file. But every one of those files can be turned into a readable text form, called WAT (WebAssembly Text), and you can write that text yourself. Doing it once shows you what WebAssembly actually is, which makes the compiled output, the error messages and the browser APIs much less mysterious later.

Four facts shape everything you write in this tutorial:

  • WebAssembly is a stack machine. Instructions push values onto a stack and pop them off. i32.add takes the top two numbers and pushes their sum. There are no registers to name.
  • There are only numbers. The core types are 32- and 64-bit integers and floats (i32, i64, f32, f64), plus a few reference types. There is no string type.
  • Memory is one flat array of bytes, called linear memory. A "string" is just some bytes at an address you choose, and you pass around the address and the length.
  • A module can do nothing on its own. It cannot print, read files or open sockets. Everything outside the module comes in as an import that the host (a browser, Node.js, Wasmtime) provides.

To print Hello World, then, you place the text bytes in memory and call an imported function that writes bytes to standard output. That function comes from WASI, the WebAssembly System Interface. You use two tools:

  • wabt, the WebAssembly Binary Toolkit. Its wat2wasm assembles text into a binary .wasm file and wasm2wat turns a binary back into text. Ubuntu 26.04 ships wabt 1.0.36; the upstream project is ahead of that, but 1.0.36 handles everything here.
  • Wasmtime 50, a standalone WebAssembly runtime from the Bytecode Alliance. It compiles .wasm to native machine code and runs it outside any browser.

You work in a throwaway folder, ~/hello-wasm-demo, and delete it at the end.

Step 1 · Install wabt and Wasmtime

Install wabt from Ubuntu's packages, along with the tools the Wasmtime installer needs:

Bash
sudo apt update
sudo apt install -y wabt curl ca-certificates xz-utils
wat2wasm --version

Wasmtime is not in Ubuntu's packages at a current version, so use the official installer script. Download it first rather than piping it straight into a shell, so you can read it if you want to. Pass --version so you get exactly the release this page was written for:

Bash
cd "$(mktemp -d)"
curl -fsSLo install-wasmtime.sh https://wasmtime.dev/install.sh
bash install-wasmtime.sh --version v50.0.2
export PATH="$HOME/.wasmtime/bin:$PATH"
wasmtime --version

The installer puts a single wasmtime binary in ~/.wasmtime/bin and adds that folder to your shell profile for future terminals. The export line makes it work in this terminal right away. Nothing goes into system folders, and apt will not update it: to upgrade, run the installer again with a newer --version.

Check it worked

wat2wasm --version prints 1.0.36. wasmtime --version prints wasmtime 50.0.2 followed by a build hash and date in brackets.

Step 2 · Write hello.wat

Create the demo folder and the module:

Bash
mkdir -p ~/hello-wasm-demo
cd ~/hello-wasm-demo
cat > hello.wat <<'WAT'
(module
  ;; Ask the host for WASI's fd_write function.
  (import "wasi_snapshot_preview1" "fd_write"
    (func $fd_write (param i32 i32 i32 i32) (result i32)))

  ;; One page (64 KiB) of linear memory, exported so WASI can read it.
  (memory (export "memory") 1)

  ;; Put the 14 bytes of the message at address 8.
  (data (i32.const 8) "Hello, World!\n")

  ;; The function Wasmtime calls when the program starts.
  (func (export "_start")
    ;; Build an iovec at address 0: where the bytes are, and how many.
    (i32.store (i32.const 0) (i32.const 8))
    (i32.store (i32.const 4) (i32.const 14))

    ;; fd_write(fd, iovec address, iovec count, where to put bytes written)
    (call $fd_write
      (i32.const 1)
      (i32.const 0)
      (i32.const 1)
      (i32.const 20))
    drop))
WAT

Read it from the top. Text after ;; is a comment.

  • (module ...) wraps everything. A .wasm file holds exactly one module.
  • (import "wasi_snapshot_preview1" "fd_write" ...) asks the host for a function called fd_write from the namespace wasi_snapshot_preview1, which is the name of WASI preview 1. The (func $fd_write (param i32 i32 i32 i32) (result i32)) part declares its type: four 32-bit integers in, one out. $fd_write is a name you use inside this file; the binary stores functions by number.
  • (memory (export "memory") 1) creates linear memory one page long. A page is 64 KiB. WASI functions read your memory, so WASI preview 1 requires the module to export it under the name memory.
  • (data (i32.const 8) "Hello, World!\n") copies the message bytes into memory at address 8 when the module starts. \n is one byte, a newline, so the message is 14 bytes long and fills addresses 8 to 21.
  • (func (export "_start") ...) defines a function and exports it as _start. Wasmtime calls the export named _start to run a WASI program, the way a C program starts at main.
  • fd_write does not take a pointer and a length directly. It takes a list of "iovecs", each a pair of 32-bit numbers: the address of some bytes and how many there are. The two i32.store lines build one iovec at address 0. The first writes the number 8 (where the message starts) into bytes 0 to 3, the second writes 14 (its length) into bytes 4 to 7.
  • (call $fd_write ...) passes four numbers: 1 is file descriptor 1, standard output; 0 is the address of the iovec list; 1 says the list holds one iovec; 20 is an address where fd_write stores how many bytes it wrote. Address 20 is free because the iovec ends at 8 and you never read it back.
  • fd_write returns an error code, which lands on the stack. A function with no result must end with an empty stack, so drop throws the code away. A real program would check it.

The nested brackets are a convenience called folded form. (i32.store (i32.const 0) (i32.const 8)) means the same as the flat sequence i32.const 0, i32.const 8, i32.store: push the address, push the value, then store pops both.

Check it worked

cat hello.wat shows the module. Count the characters in Hello, World! and add one for \n: you should get 14, the number in the second i32.store.

Step 3 · Assemble it and run it

Turn the text into a binary module, then look at its size and its first four bytes:

Bash
wat2wasm hello.wat -o hello.wasm
wc -c hello.wasm
head -c 4 hello.wasm | od -c

The whole program is a little over a hundred bytes. Every .wasm file starts with the same four bytes, a zero byte then the letters asm, which is how tools recognise the format. Small binaries are one reason WebAssembly suits the web: on a prepaid mobile data plan or a slow connection, a compact module downloads faster than the same logic in many other forms.

Now run it:

Bash
wasmtime hello.wasm

wasmtime hello.wasm is short for wasmtime run hello.wasm. Wasmtime compiles the module to native code for your processor, supplies the WASI imports, and calls _start.

Check it worked

The terminal prints Hello, World! on its own line. od -c shows \0 a s m on its first line.

Step 4 · Round-trip the binary back to text

Disassemble the binary with wasm2wat:

Bash
wasm2wat hello.wasm

You get your module back, but not in your words. The output looks something like this:

Text
(module
  (type (;0;) (func (param i32 i32 i32 i32) (result i32)))
  (type (;1;) (func))
  (import "wasi_snapshot_preview1" "fd_write" (func (;0;) (type 0)))
  (func (;1;) (type 1)
    i32.const 0
    i32.const 8
    i32.store
    ...

Three things changed. The comments are gone, because the binary has nowhere to keep them. The folded brackets became a flat list of instructions, which is how the binary really stores them: one instruction after another on a stack machine. And $fd_write became plain numbers, because function names are only a convenience of the text format. Function 0 is the import and function 1 is your _start. Function types got their own (type ...) entries, shared by every function with the same signature.

You can keep the names by asking wat2wasm to write a name section, an optional part of the binary meant for debuggers:

Bash
wat2wasm --debug-names hello.wat -o hello-named.wasm
wasm2wat hello-named.wasm | grep fd_write

This is how you read any .wasm file, including ones compiled from Rust or C, when you want to know what it imports and exports.

Check it worked

The plain wasm2wat output contains (export "_start" (func 1)) and the data line with Hello, World!\0a (\0a is the newline byte). The grep on the named file prints lines containing $fd_write.

Step 5 · Export an add function and call it with --invoke

WebAssembly's real job is usually computation called by a host, not printing. Write a second module with one exported function that adds two integers:

Bash
cd ~/hello-wasm-demo
cat > add.wat <<'WAT'
(module
  (func (export "add") (param $a i32) (param $b i32) (result i32)
    local.get $a
    local.get $b
    i32.add))
WAT
wat2wasm add.wat -o add.wasm

This time the body is written flat, so you can follow the stack:

  • local.get $a pushes the first parameter. The stack holds [a].
  • local.get $b pushes the second. The stack holds [a, b].
  • i32.add pops both and pushes their sum. The stack holds [a+b].
  • The function ends, and whatever is left on the stack is the result. Its type must match (result i32), and wat2wasm refuses the file if it does not.

This module imports nothing and has no _start, so there is nothing to run as a program. Ask Wasmtime to call add directly:

Bash
wasmtime run --invoke add add.wasm 2 3

Wasmtime options go before the file name; anything after the file name goes to the module. The Wasmtime documentation marks --invoke on core modules as unstable, so it may print a warning line saying so, and the exact output format may change in later releases. It is a debugging aid, not an interface to build scripts on.

Check it worked

The command prints 5.

Step 6 · Load the same module from Node.js

The same add.wasm runs in any WebAssembly host, including browsers and Node.js, through the JavaScript WebAssembly API. Install Node.js from Ubuntu's packages (version 22 in Ubuntu 26.04; any recent version works for this):

Bash
sudo apt install -y nodejs
node --version

If you already followed Install Node.js 26 LTS on Ubuntu, skip the install and use that Node instead. Then write a small script:

Bash
cd ~/hello-wasm-demo
cat > add.mjs <<'JS'
import { readFile } from "node:fs/promises";

const bytes = await readFile("add.wasm");
const { instance } = await WebAssembly.instantiate(bytes);
const { add } = instance.exports;

console.log("2 + 3 =", add(2, 3));
console.log("2147483647 + 1 =", add(2147483647, 1));
JS
node add.mjs

Line by line:

  • readFile("add.wasm") reads the binary into memory. The .mjs extension tells Node to treat the file as a JavaScript module, which allows await at the top level.
  • WebAssembly.instantiate(bytes) compiles the bytes and creates an instance. It returns both the compiled module and the instance; you only need the instance. A module with imports would take a second argument, an object supplying them, which is the JavaScript version of what Wasmtime did for fd_write.
  • instance.exports.add is your WebAssembly function wrapped as an ordinary JavaScript function. JavaScript numbers are converted to i32 on the way in and back on the way out.
  • The second call adds 1 to the largest 32-bit signed integer. WebAssembly integers wrap around instead of growing, so the result is the smallest 32-bit integer. In plain JavaScript, 2147483647 + 1 would give 2147483648.

Check it worked

Node prints 2 + 3 = 5 and then 2147483647 + 1 = -2147483648.

Step 7 · Recognise the errors beginners hit

Most first WAT files fail in one of a few ways.

Leaving a value on the stack. Delete the drop from hello.wat and wat2wasm refuses it, with an error something like type mismatch at end of function, expected [] but got [i32]. The validator checks the stack type of every function before anything runs. Try it on a copy:

Bash
cd ~/hello-wasm-demo
grep -v '^    drop))$' hello.wat > broken.wat
echo '  ))' >> broken.wat
wat2wasm broken.wat -o broken.wasm || echo "wat2wasm rejected broken.wat"

Not exporting memory. If you write (memory 1) without (export "memory"), the file assembles, but Wasmtime fails when fd_write is called, with an error about a missing memory export. WASI can only read bytes the module exports.

Getting the length wrong. Write 5 instead of 14 in the second i32.store and the program prints Hello with no newline. Nothing checks that your numbers describe your data; that is your job in WAT, and the compiler's job in Rust or C.

Using the wrong import name. wasi_snapshot_preview1 must be spelt exactly. A typo means Wasmtime has no such import to offer and refuses to start the module, with an error naming the unknown import.

Command not found. If a new terminal says wasmtime: command not found, open another terminal so the profile change takes effect, or run export PATH="$HOME/.wasmtime/bin:$PATH" again.

You may also see the names WASI preview 2 and components. Wasmtime 50 runs two kinds of binary: core modules like yours, which get the preview 1 interface, and components, a newer wrapper format that gets preview 2 and later. Compilers for Rust and other languages can produce either. For hand-written WAT, core modules with preview 1 are the simple path.

Check it worked

The wat2wasm broken.wat command prints an error that mentions a type mismatch, followed by wat2wasm rejected broken.wat.

Step 8 · Clean up

Delete the demo folder. If you do not want to keep Wasmtime, delete its folder as well and remove the two WASMTIME_HOME lines the installer added to your shell profile (~/.bashrc or ~/.profile):

Bash
rm -rf ~/hello-wasm-demo
ls ~ | grep hello-wasm-demo || echo "demo folder removed"

If you installed Node.js only for this tutorial, remove it too in the command below; leave nodejs off that line if you use Node elsewhere or followed the Node.js 26 tutorial. The downloaded install-wasmtime.sh sits in a temporary folder that your system clears on its own, so there is nothing to delete for it.

Bash
rm -rf ~/.wasmtime
sudo apt remove -y wabt nodejs

Check it worked

The command prints demo folder removed.

Next steps

Where WebAssembly is used: in browsers for heavy work such as image and video editing, games and CAD tools; in edge and serverless platforms that start a fresh sandboxed module for each request, because a module is quick to start and cannot see other requests; as a plugin format, where an application runs third-party code without trusting it; and as a sandbox for untrusted code generally, because a module can only touch what its host hands it.

In practice you will compile to WebAssembly from another language rather than write WAT. Good places to go next:

Need this done for you?

Padding does this work for growing companies, from the first diagnosis to a system that keeps working.

Platform Engineering

Get new tutorials

New tutorials, about twice a month.