nasm-to-wasm installation

I have not supported other architectures yet with the exception of amd64. In the future, I promise I will configure my apt repo soon.

nasm-to-wasm dependencies:

  1. Python3
  2. WebAssembly Binary Toolkit

Install nasm-to-wasm:

For Debian-based Linux:

Add a repository into Advanced Package Tool, the update and install nasm-to-wasm:

Bash

Copy

          curl -fsSL https://who5673.github.io/coc-nasm-web/deb/repo/coc-nasm-website.gpg | sudo gpg --dearmor -o /usr/share/keyrings/coc-nasm-web.gpg
echo "deb [signed-by=/usr/share/keyrings/coc-nasm-web.gpg arch=amd64] https://who5673.github.io/coc-nasm-web/deb/repo $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/coc-nasm-web.list
sudo apt update && sudo apt install nasm-to-wasm
        

If the codename is not supported, use stable as an alternative (not recommend if lsb_release -cs returns a supported codename):

Bash

Copy

          echo "deb [signed-by=/usr/share/keyrings/coc-nasm-website.gpg arch=amd64] https://who5673.github.io/coc-nasm-web/deb/repo stable main" | sudo tee /etc/apt/sources.list.d/coc-nasm-web.list
        

For other Operating System:

Step 1: Go to this page in coc-nasm-web github repo.

Step 2: In the website, click the download button like the image below:

The nasm-to-wasm downloaded will be in ~/Downloads like this:

Step 3: Run the script using Python Interpreter:

Bash

Copy

          # For Windows, run this script with python3:
python3 nasm-to-wasm.py

# For Unix-based system, provide the script with the permission of executing, then run directly the script:
chmod +x nasm-to-wasm
./nasm-to-wasm
        

Establish your first Netwide Assembler (NASM) program and then compile it into WASM:

This compiler is not as good as the real NASM one. Therefore, many commands cannot be supported because of the difference between WebAssembly Text and Netwide Assembler architectures.

Step 1: Take down Netwide Assembler script:

Example filename: main.nasm

NASM

Copy

          segment .text
  global sum

sum:
  mov rax, rdi
  add rax, rsi
  ret
        

Step 2: Choose one of 2 options below:

Option 1: Compile into WebAssembly Binary directly:

Bash

Copy

          nasm-to-wasm -i main.nasm -o main.wasm
        

Option 2: Compile into WebAssembly Text Format (WAT), then use wat2wasm to compile the WAT script:

(This is how the nasm-to-wasm compiler works)

Bash

Copy

          nasm-to-wasm -i main.nasm -o main.wat
wat2wasm main.wat -o main.wasm
        

You will also receive a JavaScript file like this main.js:

JavaScript

Copy

          export async function loadWasm() {
  // Load WebAssembly normal functions:
  const response = await fetch("main.wasm");
  const bytes = await response.arrayBuffer();
  const result = await WebAssembly.instantiate(bytes);
  return result.instance;
}

export default async function nasmToWasmInit() {
  const instance = await loadWasm();
  globalThis.sum = instance.exports.sum

  return instance;
};
        

Step 3: Test the script inside a HTML file:

Example file: index.html

HTML

Copy

          <!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Netwide Document<title>
    <!--I suggest finding an image file and link it into HTML as a favicon in order to clear the favicon.ico error in the page, for example:-->
    <link rel="icon" type="image/png" href="example_image.png">
  </head>
  <body>
    <script type="module" src="frontend.js"><script> <!--Example script-->
  </body>
</html>
        

JavaScript

Copy

          import nasmToWasmInit from "./main.js";

await nasmToWasmInit();   // Load the function from the WebAssembly Binary asynchorously
console.log(sum(5, 3));   // Must return 8