Occasionally I use assembler to squeeze processing power from my programs.
A cheap solution was C inline assembler sections, but VS2019 doesn’t support them into a 64-bits program. Other compilers use other more or less complex solutions (IBM assembler, …).
This forced me to consider having & link .asm files in my projects. I can split them with 32 and 64 bits code and compile them when needed. Then I tried to use MASM included in Visual Studio… but I found it really complex to use, and quite poorly documented.
So I started to consider using NASM, an open-source assembler compiler, simpler and much better documented.
Download and Installation
The first step is to download and install the following needed pieces.
- NASM ‘compiler’ from its Home Site.
- VSNASM an open-source project to integrate it into Visual Studio from GitHub.
then install the first file (for example into “c:\nasm” folder) and copy VSNASM files in the same folder.
The last thing is to define an environmental variable NASMPATH with the installation folder with the final ‘\‘ (“c:\nasm\” in our example).
NOTE1: I warmly suggest installing it, into the ‘program files‘ folder to keep them safer.
A problem might arise if you have to compile your project on 2 different PCs installed with 2 different regional settings/languages because ‘program files’ folder translated names are different.
In these cases, my advice is to create a junction for a common folder name (for example ‘C:\NASM‘, ‘C:\_utils\NASM‘, …), and then use this folder to import the files in the project (see below).
NOTE2: I also warmly suggest editing ‘nasm.xml‘ to change the DisplayName to “NASM – Netwide Assembler” into the following nodes at the end of the file:
Setting up Visual Studio
Start Visual Studio and:
- open ‘Tools‘->’Options…‘ window from the main menu.
- go to ‘Projects and Solutions‘ -> ‘VC++ Project Settings‘.
- set ‘Build Customization Search Path‘ with your NASM installation folder.
- confirm and exit.
Although it is not mandatory, I suggest also installing ‘AsmDude‘ extension for syntax highlighting into .asm files.
Once installed, it can be configured for Nasm Intel Syntax highlighting from VS options.
VS Project Setup
The last step is to enable .asm support into your project.
- open your project.
- right-click each project using .asm files.
- select ‘Build Dependencies‘->’Build Customizations‘.
- check ‘nasm‘ option.
Then for each .asm file, you have to:
- open the file ‘Properties‘
- set into ‘General‘->’Item Type‘ to ‘Netwide Assembler‘ (or whatever you set, “NASM – Netwide Assembler” in my case).
- enable the debug information for the debug profile from the specific NASM options.
NOTE: from this last property window, we can also set if to compile the file or not in 32/64 bits target platforms.
After all these steps, we can start with something like:
bits 32 section .data <put your data here> section .text <put your function here>