How to compile assembly code: using NASM in Linux, Windows
You can compile assembler programs using NASM on Linux or Windows, as NASM is available for both platforms. The NASM assembler will use mnemonics to convert low-level coding into machine language that the processor can understand. This article will not teach you how to program using NASM, but will teach you how to create executable commands for Linux and Windows from NASM source code.
Netwide Assembler (NASM) is an assembler and disassembler for the Intel x86 architecture, commonly used to create 16-bit, 32-bit (IA-32), and 64-bit (x86-64) programs.
How to compile assembly programs using NASM for Linux?
Creating Source Files
We can do this using any text editor such as Gedit, KWrite, or XEmacs. When you save the file, give it an .asm extension .
Compile source files
For this step, we need to install the NASM software on our computer.
If we are running Debian or Ubuntu, just type the following command:
$ sudo apt-get install nasm
If we have other Linux distributions, we must use the package manager of our distribution (such as Urppi, Yum, Emerge) or download NASM from the official website.
Use the following command line to compile the source file:
$ nasm -f elf test.asm
In the example, the saved .asm file is called test.asm . This will create a file called test.o in the current directory .
注意
The file is not executable. It is still an object file.
Creating an Executable
Now that we have our object file, called test.o , we have to create our executable file.
Our program may _start
start as a procedure named . This means that our program has its own entry point, without the need to use main
the function. However, we need to use "l" to create an executable file:
$ ld test.o -o test
Alternatively, our program can be main
started from a process called . We will need to use gcc
to create our executable:
$ gcc test.o -o test
Now our executable is created, tested, and located in the current directory.
Program Execution
To run a program named test , just type the following command:
$ ./test
How to compile assembly programs using NASM for Windows?
There is no function under Windows main
, and WinMain must be used instead.
If our entry point is _start
or main
, we should change it 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
Install the software
We must first install NASM. Save the installation package somewhere as we will need it later.
The most difficult step is to install MinGW , a free development environment for Windows:
Start by selecting the latest version of MingGW from their website. Run the installer, but do not update at this time. Leave all options selected by default, and wait for it to install.
Now you need to insert NASM in the development environment MinGW. Unzip the NASM archive. We should get a folder containing a file called nasm.exe . Copy this file to the directory C:\MinGW\bin .
Creating Source Files
As with Linux, you do not need to use a specific publisher to create source files for NASM. You can use Notepad. But be aware that it tends to add the .txt extension to the files it creates. To eliminate any ambiguity, it is recommended that you look at the file's extension.
At all costs, avoid using a word processor such as Word or WordPad.
If we prefer, we can also use an editor that uses NASM syntax, such as the NasmEdit IDE.
Make sure to save the source file with an .asm extension.
Compile source files
Open a Command window by going to Start > Run and typing cmd.exe
Using the command cd, go to the folder containing the source file. Once in this directory, assemble the source file (test.asm) using the following command:
> nasm -f win32 test.asm -o test.o
We have now created an object file. The next step will be to turn it into an executable file.
Program creation and execution
In the command window, type the final command to create the executable file:
> ld test.o -o test.exe
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
Restart PostgreSQL in Ubuntu 18.04
Publish Date:2025/04/09 Views:72 Category:PostgreSQL
-
This short article shows how to restart PostgreSQL in Ubuntu. Restart PostgreSQL Server in Ubuntu You can restart Postgres server in Ubuntu using the following command. Order: sudo service postgres restart Sometimes the above command does n
Issues to note when installing Apache on Linux
Publish Date:2025/04/08 Views:78 Category:OPERATING SYSTEM
-
As the most commonly used web server, Apache can be used in most computer operating systems. As a free and open source Unix-like operating system, Linux and Apache are a golden pair. This article will introduce the installation and use of A
How to decompress x.tar.xz format files under Linux
Publish Date:2025/04/08 Views:186 Category:OPERATING SYSTEM
-
A lot of software found today is in the tar.xz format, which is a lossless data compression file format that uses the LZMA compression algorithm. Like gzip and bzip2, it supports multiple file compression, but the convention is not to compr
Summary of vim common commands
Publish Date:2025/04/08 Views:115 Category:OPERATING SYSTEM
-
In Linux, the best editor should be vim. However, the complex commands behind vim's powerful functions also make us daunted. Of course, these commands do not need to be memorized by rote. As long as you practice using vim more, you can reme
Detailed explanation of command return value $? in Linux
Publish Date:2025/04/08 Views:58 Category:OPERATING SYSTEM
-
? is a special variable. This variable represents the return value of the previous command. That is to say, when we run certain commands, these commands will return a code after running. Generally, if the command is successfully run, the re
Common judgment formulas for Linux script shell
Publish Date:2025/04/08 Views:159 Category:OPERATING SYSTEM
-
In shell script programming, predicates are often used. There are two ways to use predicates, one is to use test, and the other is to use []. Let's take a look at how to use these two methods through two simple examples. Example 1 # test –
Shell script programming practice - specify a directory to delete files
Publish Date:2025/04/08 Views:98 Category:OPERATING SYSTEM
-
Usually, in Linux system we need to frequently delete some temporary files or junk files. If we delete them one by one manually, it will be quite troublesome. I have also been learning shell script programming recently, so I tried to write
Use of Linux command at - set time to execute command only once
Publish Date:2025/04/08 Views:158 Category:OPERATING SYSTEM
-
This article mainly involves a knowledge point, which is the atd service. Similar to this service is the crond service. The functions of these two services can be similar to the two functional functions of javascript. Those who have learned
Use of Linux command crontab - loop execution of set commands
Publish Date:2025/04/08 Views:170 Category:OPERATING SYSTEM
-
Compared with at , which executes a command only once, crontab, which we are going to talk about in this article, executes the set commands in a loop. Similarly, the use of crontab requires the support of the crond service. The service is s