WebAssembly - "Hello World"

Hello, aspiring programmers! Today, we're going to embark on an exciting journey into the world of WebAssembly. Don't worry if you've never written a line of code before - we'll start from the very beginning and take it step by step. By the end of this tutorial, you'll have created your first WebAssembly program that displays the classic "Hello World" message. So, let's dive in!

WebAssembly - “Hello World”

What is WebAssembly?

Before we start coding, let's understand what WebAssembly is. WebAssembly, often abbreviated as Wasm, is a binary instruction format for a stack-based virtual machine. It's designed as a portable target for the compilation of high-level languages like C, C++, and Rust, enabling deployment on the web for client and server applications.

Think of WebAssembly as a way to run high-performance code in web browsers. It's like giving your web applications superpowers!

Setting Up Our Environment

To get started with WebAssembly, we need to set up our development environment. For this tutorial, we'll use a simple online tool called WasmFiddle (https://wasmldedle.net/). It allows us to write, compile, and run WebAssembly code right in our browser.

Our First WebAssembly Program

Step 1: Writing the C Code

Let's start by writing a simple C program that prints "Hello World". Don't worry if you're not familiar with C - I'll explain each line.

#include <stdio.h>

int main() {
    printf("Hello World!\n");
    return 0;
}

Let's break this down:

  1. #include <stdio.h>: This line tells the compiler to include the standard input/output library, which contains the printf function we'll use.
  2. int main(): This is the main function where our program starts executing.
  3. printf("Hello World!\n");: This line prints "Hello World!" to the console. The \n at the end adds a new line.
  4. return 0;: This indicates that our program has finished successfully.

Step 2: Compiling to WebAssembly

Now, let's compile this C code to WebAssembly. In WasmFiddle, you can simply paste the C code into the left panel and click "Build". The WebAssembly code will appear in the middle panel.

Step 3: Running the WebAssembly

To run our WebAssembly code, WasmFiddle provides a JavaScript panel on the right. Here's the code to run our WebAssembly:

Module.onRuntimeInitialized = function() {
    Module._main();
};

This code tells the browser to run our main function once the WebAssembly module is initialized.

Output

When you click "Run" in WasmFiddle, you should see "Hello World!" appear in the console output at the bottom of the page. Congratulations! You've just run your first WebAssembly program!

Understanding What Happened

Let's take a moment to understand what just happened:

  1. We wrote a simple C program.
  2. The C program was compiled into WebAssembly, a low-level language that can run in web browsers.
  3. We used JavaScript to load and run our WebAssembly code.
  4. The WebAssembly code executed our printf function, which displayed "Hello World!" in the console.

It's like we've taught our web browser a new language and then used that language to say hello to the world!

Why Use WebAssembly?

You might be wondering, "Why go through all this trouble when we could just use JavaScript?" Great question! WebAssembly has several advantages:

  1. Performance: WebAssembly can run at near-native speed, making it much faster than JavaScript for computationally intensive tasks.
  2. Language Choice: You can write code in languages like C, C++, or Rust, which might be more suitable for certain tasks or more familiar to some developers.
  3. Security: WebAssembly runs in a sandboxed environment, providing an extra layer of security.

Conclusion

Congratulations on writing and running your first WebAssembly program! We've only scratched the surface of what's possible with WebAssembly, but I hope this tutorial has sparked your interest in this powerful technology.

Remember, every expert was once a beginner. Keep practicing, stay curious, and don't be afraid to experiment. Who knows? The next revolutionary web application might be built with the WebAssembly skills you're developing right now!

In our next lesson, we'll explore more complex WebAssembly examples and learn how to integrate WebAssembly with HTML and CSS to create interactive web pages. Until then, happy coding!

Method Description
#include <stdio.h> Includes the standard input/output library
int main() Defines the main function where program execution begins
printf() Prints formatted output to the console
return 0; Indicates successful program completion
Module.onRuntimeInitialized JavaScript method to run code when WebAssembly module is ready
Module._main() Calls the main function in the WebAssembly module

Credits: Image by storyset