Mark As Completed Discussion

Now that we have compiled our C++ code into a WebAssembly module, it's time to understand and interpret the generated Wasm output. Wasm output is a binary code format, different from higher level languages like JavaScript or C++. This compact and portable format leads to quicker applications initialization and improved performance, both crucial for web applications.

After compiling our simple C++ program using Emscripten, the Wasm output will be a .wasm file. This binary file may not be human-readable, but there are tools like Wabt's wasm2wat that allow us to convert it into a more readable format called WebAssembly Text format (wat).

The wasm2wat tool would convert our C++ program's Wasm output into something like the following:

SNIPPET
1(module
2(type (;0;) (func))
3(func (;0;) (type 0)
4  (i32.store offset=4
5    (i32.const 0)
6    (i32.const 1024))
7  (call $main)
8  (drop
9    (call $main))
10  return)
11)

This representation starts with a (module ...), indicating that this is a WebAssembly module. It includes a (type (;0;) (func)), defining the type of functions that can be included in this module, and then it defines a function (func ...) with a type 0.

Important to note is that WebAssembly is a stack-based virtual machine. This means that operations are performed by pushing operands onto a stack and popping operands from the top of the stack when fetching results.

This sort of understanding is not only valuable, but required to effectively leverage the benefits of WebAssembly and deal with performance optimization and debugging in real-world scenarios. However, remember you would primarily interact with the Wasm output through JavaScript or an HTML page, making the non-human readability far less of a hindrance in practice.

CPP
OUTPUT
:001 > Cmd/Ctrl-Enter to run, Cmd/Ctrl-/ to comment