Compiling an assembly program with NASM
can be done on Linux or Windows as NASM is available for both platforms. The Netwide Assembler or the NASM is a popular assembler which is used for programming assembly code with x86 Intel processor architecture. Unlike high level programming languages, an assembly language code comprises only of mnemonics and symbols. It is possible to create an executable from NASM code
to run on Linux or Windows. Running the executable on Linux and Windows
requires a simple command line procedure which is specific to each operating system after compiling an assembly program with the NASM
on Linux or Windows.
Compiling an assembly program with Nasm
(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. It is available on multiple operating systems like Linux
or Windows, for example.
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 for Linux and Windows from NASM source code.
Step 1. Create a source file
You can use any text editor to create your source file
for NASM such as Gedit, KWrite, or XEmacs.
When you save your file, give it the extension .asm
Step 2. Assemble 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:
- If you have another Linux distribution, you must use your distribution's package manager (eg 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. Note: This file is not executable - it is still an object file.
Step 3. Creating the executable
Now that we have our object file named test.o we must create our executable.
Two cases are presented here:
- Your program begins 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:
- Your program begins with a procedure called "main". You will need to use gcc to create your executable:
- Our executable is created, it is tested and is in the current directory.
Step 4. Program Execution
- To run the program called "test", just type this command:
Under the windows the convention is not the same. 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" and change the "ret" at the end of the procedure to "ret 16":
mov eax, 0
Step 1. Install the necessary software
You must first install NASM. Go to the http://ccm.net/download/download 1025 nasm.
Keep an archive somewhere, it will be used later.
The most difficult step will be installing MinGW which is a free development environment for Windows:
Now we'll 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
Step 2. Create 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 beware - 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 case, avoid word processors such as Word or WordPad.
If you wish, you can also use an editor that uses NASM syntax, such NasmEdit IDE (free).
Make sure your save your source file with the .asm
Step 3. Assemble 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:
You have now created an object file. The next step will turn it into an executable file.
Step 4. Creation and execution of the program
From your Command window, type the final command to create the executable:
ld test.o -o test.exe
Published by deri58
. - Latest update by Paul Berentzen