The maintainer of this website has a Spotify Coding Playlist of their Lo-fi Hip Hop beats!

Introduction to WebAssembly Text

Tutorial

Introduction to WebAssembly Text

What is WebAssembly Text (WAT)?

If you write a program in C or Rust and then instruct the compiler to generate a WebAssembly[^1] file, the result will be a .wasm file containing the binary op codes for your program. This file contains the executable code and is therefore intended only to be machine-readable, not human-readable. However, as part of your development process, you may very well need to look inside this machine-readable file: and at this point you will need a set of tools that can translate the WebAssembly op codes back into a human-readable format.

This is where the WebAssembly Binary Toolkit (WABT) becomes a vital resource in your development tool bag. Once installed, you have a variety of tools that can work directly with not only WebAssembly binary files, but also their text representation.

Using the WABT disassembler called wasm2wat, you can transform a compiled WebAssembly module (a .wasm file) back into a file containing the plain text op codes used by that program (a .wat file). This plain text representation is known as WebAssembly Text (or WAT).

In addition to disassembling .wasm files to plain text .wat files, WABT also provides you with a tool called wat2wasm that does the reverse. It takes a plain text WebAssembly Text file and assembles into an executable .wasm file.

The object of this tutorial is to give an introduction to writing programs directly in WebAssembly Text format.

But WebAssembly is Just a Compilation Target, So Why Bother?

WebAssembly certainly is a compilation target, and it is certainly true that the majority of WebAssembly programs will be generated by compilers, not humans. However, these facts should not be used to conclude that there are therefore no cases in which it would be beneficial to write a WebAssembly Text program by hand.

Although it is a very low-level development process, writing WebAssembly Text programs by hand brings the benefits of being able to build a very small, very efficient program suitable for performing highly repetitive, CPU-intensive tasks.

This introduction to WebAssembly Text serves as the starting point for a subsequent tutorial that describes how to implement the CPU-intensive calculations needed to plot the Mandelbrot and Julia Sets.

Date Added: 1/11/22