Compiling and Running Assembly Code Using NASM & WSL on Windows

Compiling and Running Assembly Code Using NASM and WSL in VS Code on Windows wsl installation

In this blog post, we will discuss how to compile and run assembly code using NASM in VS Code on Windows WSL.

What is WSL?

Windows Subsystem for Linux (WSL) is a Windows in-built feature that lets developers run a GNU/Linux environment -- including most command-line tools, utilities, and applications -- directly on Windows, unmodified, without the burden of a traditional virtual machine or dual-boot setup.

Steps to Compile and Run Assembly Code

  1. Install WSL (Windows Subsystem for Linux):
    1. On your Desktop, press [Win]+[R]
    2. Type "PowerShell" and press [Ctrl]+[Shift]+[Enter]
    3. powershell
    4. Follow the instructions to install WSL
      1. Type this command and press Enter.
        
        $	wsl --install
                        
        wsl installation
      2. Select [Yes] when prompted.
        wsl installation
    5. Restart your computer.
    6. After restart, this should appear [if it doesn't then open Ubuntu using search]
      1. wsl installation
      2. wsl installation
    7. Setup your user-account.
      1. user account setup
      2. user account setup
      3. user account setup
      4. user account setup
  2. Install NASM:
    1. Close any previously opened window
    2. Search for Ubuntu and open it
      ubuntu search
    3. Type this command and press Enter.
      
      $	sudo apt update
                  
    4. Enter your password when prompted.
    5. Type this command and press Enter.
      
      $	sudo apt install nasm
                  
      nasm install
    6. Also install this package
      
      $   sudo apt install gcc
                  
  3. Write Your Assembly Code:
    1. It's time to write your assembly code and test if it works on your machine. [WSL is a CLI so we'll use `nano` instead of `gedit`]
    2. Create a text file with a `.asm` extension (e.g., `hello.asm`).
      creating hello.asm
    3. Use nano editor to write your assembly code.
      1. inside nano editor
      2. Here's the sample code:
        
        section .data
                message db 'Hello, world!', 13, 10
        
        section .text
                global _start
        
        _start:
                ; Print the message to stdout
                mov eax, 4          ; system call number for sys_write
                mov ebx, 1          ; file descriptor for stdout
                mov ecx, message    ; address of the message
                mov edx, 15         ; length of the message
                int 0x80            ; call kernel
        
                ; Exit the program
                mov eax, 1          ; system call number for sys_exit
                xor ebx, ebx        ; return code 0
                int 0x80            ; call kernel
            

        you can also try these other samples [get them from here and here].

      3. Copy and paste the sample code
        unsaved sample code
      4. Press [Ctrl]+[O] and then [Enter] to save the file.
        saved sample code
      5. Press [Ctrl]+[X] to exit nano editor.
  4. Assemble the Code:
    1. Navigate to the directory containing your assembly file.
    2. Use the following command to assemble the code:
    3. 
      $	nasm -f elf64 hello.asm -o hello.o
          
      assembled code
  5. Link the Object File:
    • Use this command to link the object file.
    • 
      $	ld hello.o -o hello
              
      linked object file
  6. Run the file:
    • Use this command
    • 
      $	./hello
              
      run file
  7. Exiting WSL
    • Use this command
    • 
      $	exit
              

VS Code Setup

If you are using VS Code, you can use the following steps to set up WSL in it.

  1. Before proceeding, make sure you have followed the steps to install WSL and have successfully compiled and run assembly code in WSL terminal.
  2. Install these 3 extensions from VS Code Extensions Marketplace: extensions
  3. Open the VS Code Settings [Ctrl] + [,]
  4. Click on Open Settings (JSON) in top right corner.
    open settings json
  5. Add the following code inside the main curly braces along with other field value pairs to the settings.json file above other field value pairs:
    
    
    "code-runner.executorMapByFileExtension": {
    
      ".asm": "nasm -f elf64 $fileName && ld $fileNameWithoutExt.o -o $fileNameWithoutExt && ./$fileNameWithoutExt",
    },
    "code-runner.fileDirectoryAsCwd": true,
    "code-runner.runInTerminal": true,
    "code-runner.saveFileBeforeRun": true,
    
            
    code runner wsl installation
  6. Now close these settings, and click on Open a Remote Window in the bottom left corner.
    open remote window
  7. Click on "Connect to WSL" [and optionally select the distro you want to use.]
    connect to wsl
  8. Wait for the WSL environment to be set up. [This is a one-time process]
    1. WSL will start opening in a new window.
      wsl opening
    2. WSL Server will start downloading
      wsl downloading
    3. After download and installation is complete, a welcome page will open in a new window.
      wsl welcome page
  9. In Extensions, in Code-Runner, click on "Install in WSL" button.
    code runner wsl installation
  10. In Explorer,
    1. Click on the Open Folder button.
      open folder button
    2. Select "code" folder.
      code folder
    3. Now click on OK.
      ok button
  11. When a pop-up appears, currently its safe to click on "Yes, I trust the authors"
    code runner wsl installation
  12. Now again in Explorer,
    1. Click on the "hello.asm" file
      hello.asm
    2. The code will be visible in its full glory. You can also try with other code samples too.
      sample code
  13. and then click on Run button in top right corner.
    run file
  14. You should see the output in the terminal. Try with other code samples too.
    run file
  15. Exiting WSL in VS Code
    1. Click on the WSL: Ubuntu in bottom left corner.
      run file
    2. Click on "Close Remote Connection".
      run file

    Conclusion

    Compiling and running assembly code in VS Code on Windows can be a bit challenging, but it is a rewarding experience. With a little practice, you will be able to write and debug assembly code like a pro.

    In this blog post, we have discussed how to compile and run assembly code using NASM in VS Code on Windows.

    Additional Resources

    WSL Uninstallation / Modification Process

    • Removing a specific distro
      1. Open Powershell in Administrator Mode [like in WSL installation process]
      2. Type this command and press Enter
        
        $	wsl --list
                    
      3. Select the distro you want to remove
      4. Type this command and press Enter
        
        $	wsl --unregister [distro name]
                    
    • To install a specific distro
      1. You can install multiple distros simultaneously, you do not need to remove the previous one to install a new one
      2. Open Powershell in Administrator Mode [like in WSL installation process]
      3. To list the available distro, type this command and press Enter
        
        $	wsl --list --online
                    
      4. Select the distro you want to install
      5. Type this command and press Enter
        
        $	wsl --install [distro name]
                    
    • To remove all distros
      1. Open Powershell in Administrator Mode [like in WSL installation process]
      2. Type this command and press Enter
        
        $	wsl --unregister --all
                    
      3. Type this command and press Enter
      4. 
        $	wsl --uninstall
                

    I hope this blog post has been helpful. If you have any questions, please feel free to leave a comment below.

Comments

Popular posts from this blog

Compiling and Running C/C++ code in VS Code in Windows 11