Writing WebAssembly "from scratch" with WAT

NicolasBrondinBernard

Author
@NicolasBrondinBernard

Compiling to WebAssembly is nice, but writing it by hand is better?

Article published on 31/01/2022, last updated on 10/08/2026

In a previous article we talked about the concept of Web Assembly, its nature and its usefulness (see the article).

WASM is generally compiled from another language, one that's rather low-level (C++, Rust, Go,...), but did you know that it's also possible to write Web Assembly by hand (from scratch)?

Web Assembly Text Format (WAT)

WAT is a textual representation of the WASM binary format, which allows for two things:

  • Writing WASM directly, without compiling it from another language
  • Understanding and analyzing how an existing WASM module works

Of course, in theory, nothing stops you from writing a module in web assembly in binary format, but let's admit it, the task is pretty unbearable:

00 61 73 6d 01 00 00 00 
01 07 01 60 02 7f 7f 01 
7f 03 02 01 00 07 0c 01 
08 6d 75 6c 74 69 70 6c 
79 00 00 0a 09 01 07 00 
20 00 20 01 6c 0b

Above, a module exposing a simple function to multiply two 32-bit integers.

And if you ever feel like trying the experience, here's the complete documentation of the specification: https://webassembly.github.io/spec/core/index.html

But if, like most mortals, you don't spend your Saturday nights eating hexadecimal, the WAT text format will surely be much more suitable:

(module
  (func $multiply (param $lhs i32) (param $rhs i32) (result i32)
    get_local $lhs
    get_local $rhs
    i32.mul)
  (export "multiply" (func $multiply))
)

Above, the same module, but in WAT format

Every structure of the Web Assembly grammar has been brought into a language that is readable, short, and relatively easy to write, so you can find all the documentation here.

Once your module is written in WAT, all you'll need to do is compile it into WASM with tools like this one:


Rick Mason sur Unsplash

Finished reading this article?
Our complete courses
Take it to the next level with our courses!

Complete courses, exercises and certificates to really learn programming!

4.8 average rating

Comments (0)

to leave a comment

No comments yet