Learning x86 with NASM — Your First Program

Scott Cosentino
4 min readMay 31, 2022

In this article, we will create a simple x86 assembly program using the NASM assembler on Linux. Our program will trigger an exit system call. The exit system call with quit our program, setting a status code on exit. Let’s start by building the basic structure of our x86 program.

The code for this article can be found at: https://github.com/scprogramming/Learning-x86-with-NASM/tree/main/first%20program

Structuring NASM x86 Programs

Every x86 program in NASM will contain a few key components required to get the program up and running. The first two components are known as sections. We will declare two sections in our program named .data and .text, as shown below.

The .data section is used to declare variables used by the program at runtime. For our program, the data section will be empty since no variables are required. The .text section contains the code for our program. When we first create a program in x86, we need to start by adding an entry point to the .text section.

_start is a label that indicates a location in the program, in this case, the program’s start. The line global _start exports the label, making it accessible to programs external to ours. This allows the operating system to find the start of the program to execute the code correctly. With the structure of the program setup, we can now start to implement our system call to exit.

Interrupts and Moves

An interrupt is a request for the processor to stop the currently executing code to execute some event. Interrupts are typically used to reference external resources, such as system calls or hardware. In this case, we are looking to run a system call. A system call is a method of requesting a service from the operating system’s kernel. The kernel handles many different tasks for us, such as process management and hardware communication. In this case, we are trying to get our program to exit, which requires the help of the process manager of…

--

--