Compiling an assembly program with NASM can be done on Linux or Windows, as NASM is available for both platforms. An NASM assembler will turn your low-level coding, using mnemonics, into machine language that can be understood by the processor. This article will not teach you to program with NASM, but to create an executable command for Linux and Windows from NASM source code.
Netwide Assembler (NASM) is an assembler and dissembler for the Intel x86 architecture and is commonly used to create 16-bit, 32-bit (IA-32), and 64-bit (x86-64) programs.
How to compile an assembly program with NASM for Linux?
Creating the source file
- You can use any text editor, such as Gedit, KWrite, or XEmacs, to do so. When you save your file, give it the extension .asm.
Assembling the source file
- For this step, you will need NASM software installed on your machine.
- If you're running Debian or Ubuntu, simply type the command:
sudo apt-get install nasm
- If you have another Linux distribution, you must use your distribution's package manager (e.g. Urpmi, Yum, Emerge) or download NASM from the official site.
- Use the following command line to assemble your source file:
nasm -f elf test.asm
- In the example, the saved .asm file is called test.asm. This will create a file named test.o in the current directory.
N.B. This file is not executable. It is still an object file.
Creating the executable
- Now that we have our object file, named test.o, we must create our executable.
- Your program may begin with a procedure called _start. This means that your program has its own point of entry, without the use of the main function. However, you'll need to use the "l" to create your executable:
ld test.o -o test
- Alternatively, your program may begin with a procedure called main. You will need to use gcc to create your executable:
gcc test.o -o test
- Now, your executable is created, tested, and located in the current directory.
- To run the program called test, just type this command:
. / test
How to compile an assembly program with NASM for Windows?
- The main function is not available under Windows and must be replaced by WinMain.
- If your entry point is _start or main, it should be changed to _WinMain @ 16. Also, change the ret at the end of the procedure to ret 16:
section .text global _WinMain@16 _WinMain@16: mov eax, 0 ret 16
Installing the software
- You must first install NASM. Keep an archive somewhere, as it will be used later.
- The most difficult step will be installing MinGW, which is a free development environment for Windows:
- Start by choosing the latest version of MingGW from their website. Run the installer, but don't update at this point. Leave all options selected by default, and wait for it to install.
- Now you need to insert NASM in the development environment MinGW. Unpack the NASM archive. You should get a folder containing, among other things, a file named nasm.exe. Copy this file into the directory C: \ MinGW \ bin.
Creating a source file
- Like Linux, there is no need to use a specific publisher to create a source file for NASM. You can use Notepad. But take note that it tends to add the .txt extension to files it creates. To remove any ambiguity, it is recommend that you view the extensions of your files.
- In any event, avoid word processors, such as Word or WordPad.
- If you wish, you can also use an editor that uses NASM syntax, such a NasmEdit IDE.
- Make sure your save your source file with the .asm extension.
Assembling the source file
- Open the Command window by going to Start > Run and typing cmd.exe
- Using the command cd, go to the folder containing your source file. Once you are in this directory, assemble your source file (test.asm) with this command:
nasm -f win32 test.asm -o test.o
- You have now created an object file. The next step will be to turn it into an executable file.
Creation and execution of the program
- From your Command window, type the final command to create the executable:
ld test.o -o test.exe
Need more help with Linux? Check out our Forum!
- Nasm compile
- Compile nasm
- Nasm compiler
- Ubuntu - CodeBlocks does not compile
- Change variable name in compiled .exe
- How to install tar.gz in Ubuntu: using terminal > Guide
- VBA compile Error: Do without Loop [solved] > Forum - Excel
- Search in Vim editor: Linux, for words
- Ubuntu remove password: for sudo, for user, for root
- How to disable avahi-daemon: Linux, Ubuntu
- Debian add printer: command line, install cups
- Change password Linux: script, terminal, Samba
- Debian change language: to English, command
- Linux read file line by line: for loop
- Vim autocomplete: C++, command
- Linux display file in reverse order
- How to install htop Linux: Ubtunu, Mint
- Restart network interface: Linux, Ubuntu
- How to create commands in Linux?
- Customize Wine on Linux: download, install, locate theme
- How to resize partition Linux: Gparted, command line
- How to remove the content of a directory: Linux
- How to recover lost admin password in CUPS?
- What is Daemon Log: in Linux
- How to split a file in Linux: split, merge
- How to convert DOS to UNIX: Linux, command
- Adding space in mount point: to FSTAB
- Delete lines in Linux: SED, commands
- Restoring Linux: from backup, using Tar
- How to uninstall package: in Linux