<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Divyam's blog]]></title><description><![CDATA[Divyam's blog]]></description><link>https://blog.divyam.dev</link><generator>RSS for Node</generator><lastBuildDate>Tue, 14 Apr 2026 02:20:43 GMT</lastBuildDate><atom:link href="https://blog.divyam.dev/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[Hello WebAssembly: Unleashing High-Performance Web Magic]]></title><description><![CDATA[In the ever-evolving landscape of web development, a new enchantment has emerged - WebAssembly (often abbreviated as wasm). It promises to bring high-performance magic to the web, allowing developers to weave powerful spells with their code. In this ...]]></description><link>https://blog.divyam.dev/hello-webassembly-c</link><guid isPermaLink="true">https://blog.divyam.dev/hello-webassembly-c</guid><category><![CDATA[WebAssembly]]></category><category><![CDATA[C]]></category><category><![CDATA[C++]]></category><category><![CDATA[webdev]]></category><dc:creator><![CDATA[Divyam Ahuja]]></dc:creator><pubDate>Fri, 06 Oct 2023 07:45:43 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1696578232058/5b4d701c-f3f3-46b2-ad7c-932e61aa2052.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>In the ever-evolving landscape of web development, a new enchantment has emerged - WebAssembly (often abbreviated as wasm). It promises to bring high-performance magic to the web, allowing developers to weave powerful spells with their code. In this blog post, we will embark on a mystical journey into the realm of WebAssembly by creating a "Hello, WebAssembly!" incantation using the language of C.</p>
<h2 id="heading-unraveling-webassembly"><strong>Unraveling WebAssembly</strong></h2>
<p>Before we dive into the world of WebAssembly and C, let's take a moment to understand what makes WebAssembly so magical.</p>
<p><strong>WebAssembly</strong>, in essence, is a binary instruction format designed to be executed at near-native speed in web browsers. It's not tied to any particular programming language, making it a versatile choice for web developers. Here's what makes WebAssembly magical:</p>
<ul>
<li><p><strong>Freedom of Language</strong>: It is not confined to a single language. WebAssembly opens the door to multiple programming languages, from C and C++ to Rust and beyond.</p>
</li>
<li><p><strong>Cross-Platform</strong>: WebAssembly is a nomad of the digital world; it can run on various devices and operating systems, making it accessible to all. WebAssembly System Interface (WASI) is an extension to WASM, standardizing the Cross-Platform Compatibility aspect of WebAssembly.</p>
</li>
<li><p><strong>Safety</strong>: WebAsembly runs in a secure sandboxed environment. So safety++.</p>
</li>
<li><p><strong>Speed</strong>: WebAssembly code runs blazing fast, rivaling native applications. This means you can craft web experiences that are not just responsive but lightning quick.</p>
</li>
</ul>
<h2 id="heading-preparing-your-cauldron"><strong>Preparing Your Cauldron</strong></h2>
<p>Before we begin our magical journey, you'll need to prepare your development cauldron. Here's what you'll need:</p>
<ul>
<li><p><strong>A potent browser</strong>: A modern browser like Chrome, Firefox, Safari, or Edge which is capable of interpreting and running WebAssembly.</p>
</li>
<li><p><strong>Script Scrollcrafting Quill (Text Editor)</strong>: Choose your favorite text editor or code editor. Popular options include Visual Studio Code, Sublime Text, Atom, Notepad or Notepad++.</p>
</li>
<li><p><strong>The Magical Fire of Phoenix (Compiler)</strong>: To transmute your C code into WebAssembly, you'll require a compiler. In this adventure, we'll employ Emscripten, a trusted tool of many a web sorcerer.</p>
</li>
</ul>
<h2 id="heading-setting-up-emscripten">Setting up Emscripten</h2>
<p>Before we craft our "Hello, WebAssembly!" spell, let's set up Emscripten, the essential tool for this magical journey.</p>
<h3 id="heading-step-1-acquiring-emscripten">Step 1. Acquiring Emscripten</h3>
<p>Emscripten is like the philosopher's stone of WebAssembly. To obtain it, visit the official Emscripten website at <a target="_blank" href="https://emscripten.org/">https://emscripten.org/</a> and follow the installation instructions specific to the operating system. Emscripten's powers are available for both Windows and Unix-like systems, including Linux and macOS.</p>
<h3 id="heading-step-2-conjuring-the-path"><strong>Step 2: Conjuring the Path</strong></h3>
<p>Once Emscripten is installed, you'll need to add its magical incantations to your environment. This ensures that the spells (commands) can summon their powers from any directory.</p>
<p>For Unix-like systems (Linux and macOS), one can add the following line to your shell profile (e.g., <code>.bashrc</code>, <code>.zshrc</code>, or <code>.profile</code>):</p>
<pre><code class="lang-bash"><span class="hljs-built_in">source</span> <span class="hljs-string">"/path/to/emsdk/emsdk_env.sh"</span>
</code></pre>
<p>For Windows, one can use the <code>emsdk_env.bat</code> batch script:</p>
<pre><code class="lang-powershell"><span class="hljs-string">"path\to\emsdk\emsdk_env.bat"</span>
</code></pre>
<h3 id="heading-step-3-verification"><strong>Step 3: Verification</strong></h3>
<p>To ensure that your cauldron is now imbued with Emscripten's magic, open a new terminal window and type:</p>
<pre><code class="lang-bash">emcc --version
</code></pre>
<h2 id="heading-crafting-the-hello-world-incantation"><strong>Crafting the "Hello, World!" Incantation</strong></h2>
<p>Now that your cauldron is prepared and Emscripten is at your side, it's time to weave the "Hello, WebAssembly!" spell in C and convert it into WebAssembly.</p>
<h3 id="heading-step-1-crafting-the-spell-c-code"><strong>Step 1: Crafting the Spell (C Code)</strong></h3>
<p>Begin your magical journey by crafting a spell in the form of C code. Create a new file named <code>hello.c</code> and inscribe the following incantation:</p>
<pre><code class="lang-c"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;stdio.h&gt;</span></span>

<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span> </span>{
    <span class="hljs-built_in">printf</span>(<span class="hljs-string">"Hello, WebAssembly!\n"</span>);
    <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;
}
</code></pre>
<p>This enchanting code will make the console echo "Hello, WebAssembly!" as if it were a whisper from the web itself.</p>
<h3 id="heading-step-2-the-transmutation"><strong>Step 2: The Transmutation</strong></h3>
<p>With your incantation ready, it's time to invoke the compiler's magic. Navigate to the directory containing <code>hello.c</code> in your terminal and issue the command:</p>
<pre><code class="lang-bash">emcc hello.c -o hello.html
</code></pre>
<p>This command will instruct Emscripten to transmute <code>hello.c</code> into a scroll known as <code>hello.html</code>, a creation that contains both WebAssembly and an HTML incantation.</p>
<h3 id="heading-step-3-witnessing-the-enchantment"><strong>Step 3: Witnessing the Enchantment</strong></h3>
<p>Now, with bated breath, open the <code>hello.html</code> scroll in your browser. There, on the canvas of the digital realm, "Hello, WebAssembly!" shall appear, as if summoned from the depths of the web.</p>
<h2 id="heading-the-adventure-continues"><strong>The Adventure Continues</strong></h2>
<p>Congratulations! You've successfully embarked on a magical journey into the world of WebAssembly using the mystical language of C. But this is just the beginning of your adventure. You can delve deeper into the art of WebAssembly, explore its interactions with JavaScript, and wield various programming languages as your magical tools.</p>
<p>WebAssembly holds the promise of high-speed, secure, and versatile web development. As you continue to explore this enchanting realm, you'll discover endless possibilities for crafting web experiences that are not just powerful but truly magical. So, embrace the magic of WebAssembly, keep exploring, and let your web spells flourish!</p>
]]></content:encoded></item></channel></rss>