Compiling an assembly program
can be done on Linux or Windows, as NASM is available for both platforms. 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.
An assemble 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.
Compiling an Assembly Program with NASM for Linux
Creating the Source File
You can use any text editor, such as Gedit
, 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.
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
Compiling an Assembly Program with NASM for Windows
function is not available under Windows and must be replaced by WinMain
If your entry point is _start
, it should be changed to _WinMain @ 16
. Also, change the ret
at the end of the procedure to ret 16
mov eax, 0
Installing the Software
You must first install http://ccm.net/download/download 1025 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 A-Z
. 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
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
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
Image: © Alexandr Pakhnyushchyy - Shutterstock.com