Stupid Simple WebAssembly
In this blog post, we’ll introduce you to WebAssembly. We’ll start with a history lesson on the Assembly language, which was created to make programming easier and boost productivity. Then we’ll move on to WebAssemby, a descendent of Assembly, and cover what it is, the problems it solves and how it does that.
Decades ago, if you wanted to program a computer, you had to write binary code. We called it machine code because it is the native language of any computer (maybe this will change with the rise of quantum computing, but for now, every computer works on binary code). This machine code can run directly on a computer without a compiler, transpiler or other high-level actions.
Writing machine code and debugging it are not easy tasks, so computer scientists realized they needed a higher-level language to boost productivity and make computer programming easier. From that need, the Assembly language was born. This language is easier to write and understand than machine code because instead of 0s and 1s, it uses instructions closer to human language, like add, mul, div, mov, etc.
To translate the code written in Assembly language, you need an assembler and a linker. However, there is still a 1-to-1 relationship between the Assembly language and the machine code, which means that one assembly instruction will be translated to exactly one instruction in machine code. This is not true in high-level languages like C, C++, C#, etc. Because the Assembly language is very close to machine code, it is also very fast (faster than any high-level language). High-level languages need to be compiled. As code complexity grows, it will be harder and harder for compilers to generate efficient and optimized machine codes, which means lower performance. So, if you need high performance, the ideal choice is the Assembly language.
Compared to Assembly, high-level languages have other advantages, like increased productivity, easier maintenance and portability, but they cannot compete with the Assembly language’s performance.
Now the question is, “What is WebAssembly?” We could say that WebAssembly is the assembly language for web apps. That would only be partially true because WebAssembly is a byte code, and Assembly is a language converted into byte code. What WebAssembly is trying to create is language that runs as fast as possible, close to Assembly speeds, but on the web (hence the naming).
If we want to define WebAssembly as simply as possible, it is binary code compiled for browsers.
How WebAssembly Works
In high-level languages like C, C++, C#, etc., we write the code and it runs on multiple processors, which means that we have portability. But how did we solve the problem of portability? We added a layer of abstraction, a “virtual processor” (the interpreter), which interprets the intermediate bytecode and turns it into a bytecode compatible with a specific processor. Before or after the interpreter, we could add an optimization step, but we would like to keep things simple for now.
In Web Assembly, the idea is the same but for different browsers, not processors.
This way, we can write code in any high-level language (that can be compiled in .wasm, a file extension for WebAssembly binary code) and it will run in the browser. That means CPU-intensive algorithms can be written in C and run in the browser, solving the problem of portability and dramatically increasing its performance.
Usually, we have the following structure when working with WebAssembly:
If you would like to learn more about how WebAssembly works, here is a great article about “The anatomy of WebAssembly.”
Advantages of WebAssembly
In my opinion, the most important advantages WebAssembly brings with it are portability and flexibility.
To run an application on a device, it must be compatible with its processor’s architecture and the device’s operating system. Without portability, you have to compile the same code for every processor architecture and operating system combination. With WebAssembly, there is only one compilation step, and it runs on all modern browsers.
This article introduced WebAssembly, why it was created, what problems it solves, and how it solves those problems.
Want to learn more about WebAssembly? Join our Summer is Open session on Kubewarden, an open source policy engine that leverages the power of WebAssembly. Live session is Tuesday, August 10.