TExcelTAD
Home › WebAssembly › Text vs Binary Format

Text vs Binary Format

WebAssembly has two official, equivalent representations:

Binary format (.wasm) Text format (.wat)
Purpose What engines load and run What humans read and write
Look Bytes: 00 61 73 6d 01 00 00 00 … S-expressions: (module (func …))
Size Very compact Several times larger
Produced by Compilers (clang, rustc, …) or wat2wasm Hand-written, or wasm2wat / browser DevTools

They map one-to-one: every construct in the binary has a text form and vice versa (apart from comments and names, which the binary can keep in an optional "name" custom section). Browser DevTools show Wasm functions in the text format when you step through them.

The tools

The WebAssembly Binary Toolkit (wabt) converts between the two:

bash
wat2wasm add.wat -o add.wasm     # text  -> binary
wasm2wat add.wasm -o add.wat     # binary -> text
wasm-objdump -x add.wasm         # list sections, imports and exports
wasm-objdump -d add.wasm         # disassemble the code section

The playground uses the same wabt library, compiled to JavaScript, to turn your text into bytes in the browser.

Anatomy of the text format

The text format is built from S-expressions: parenthesised lists whose first word says what they are.

wat · try it yourself
(module                                   ;; the whole module
  (import "console" "log"                 ;; an import with a two-level name...
    (func $log (param i32)))              ;; ...that is a function taking an i32

  (memory 1)                              ;; one page of linear memory

  (global $count (mut i32) (i32.const 0)) ;; a mutable global

  (func $tick                             ;; a function with a $name
    (global.set $count
      (i32.add (global.get $count) (i32.const 1))))

  (func (export "main")                   ;; exported under the name "main"
    call $tick
    call $tick
    call $tick
    (call $log (global.get $count))))
3

Things to notice:

  • ;; starts a line comment, (; … ;) is a block comment.
  • Names start with $ ($log, $tick). They are only for humans: in the binary everything is a numeric index. You can always write the index instead, e.g. call 1.
  • Instructions can be written flat (call $tick) or folded into S-expressions ((call $log (global.get $count))). Both produce exactly the same bytes. The next lessons use both.

Same code, written with indices

Here is a function that squares a number, written with names first and then with raw indices. They compile to identical binaries:

wat · try it yourself
(module
  (func $square (param $x i32) (result i32)
    (i32.mul (local.get $x) (local.get $x)))
  (func (export "main") (result i32)
    (call $square (i32.const 12))))
144
wat · try it yourself
(module
  (func (param i32) (result i32)       ;; function 0
    local.get 0
    local.get 0
    i32.mul)
  (func (export "main") (result i32)   ;; function 1
    i32.const 12
    call 0))
144

What the bytes look like

The binary for a tiny module that exports a function adding two i32s is only 41 bytes:

text
00 61 73 6d  01 00 00 00                   magic "\0asm", version 1
01 07 01 60 02 7f 7f 01 7f                 type section: (i32, i32) -> i32
03 02 01 00                                function section: function 0 has type 0
07 07 01 03 61 64 64 00 00                 export section: "add" = function 0
0a 09 01 07 00 20 00 20 01 6a 0b           code section: local.get 0, local.get 1, i32.add, end

You'll decode bytes like these yourself in the Binary Format lesson. For now, the takeaway is that the text you write is a faithful, readable spelling of these bytes.

Tip: When you're curious what a compiler produced, run wasm2wat (or open the module in your browser's DevTools Sources panel). Reading the text format is the best way to understand real-world Wasm.

Test yourself

What does wat2wasm do?

  1. Runs a module
  2. Converts the text format into the binary format
  3. Converts JavaScript to Wasm
  4. Optimises a binary

Are $names stored in the module's code?

  1. Yes, every call looks the name up
  2. No, the binary uses numeric indices; names can only be kept in an optional custom section
  3. Only exported names
  4. Only names of locals

Which is a comment in the text format?

  1. // note
  2. # note
  3. ;; note
  4. -- note