nasm program example

Mastering NASM: Dive into a Step-by-Step Programming Example

By Matthew Martinez
AD

NASM, or Netwide Assembler, is a popular tool for writing x86-compatible assembly programs. It's widely used in the computer programming community due to its versatility and compatibility with a wide range of operating systems. But for beginners, understanding how to use NASM can be a daunting task. In this article, we'll explore a simple NASM program example to help you get started.

Assembly language is a low-level programming language that the compiler translates directly into object code, or machine code, that a central processing unit (CPU) can understand and execute. NASM is a powerful tool in this regard, providing a simple and efficient way to create x86 assembly programs.

Creating a Simple "Hello, World!" Program with NASM

One of the most common first programs for any new language is the classic "Hello, World!" program. It's a straightforward way to test the environment and verify that everything is set up correctly. Let's use NASM to create this program.

AD
NASM Blog | Evidence-Based Fitness Articles & Industry Insights
NASM Blog | Evidence-Based Fitness Articles & Industry Insights
Tips for Studying with NASM
Tips for Studying with NASM
an overview of nasa's satellite systems table
an overview of nasa's satellite systems table

For this example, we'll create a 64-bit Linux program. If you're using a different operating system or assembly environment, some of the details may vary, but the overall process will be the same.

Setting Up the NASM Environment

Before we start writing code, we need to make sure NASM is installed on our system. You can usually install it using your package manager. For example, on Ubuntu, you can use the following command:

a whiteboard with some writing on it
a whiteboard with some writing on it
Nasm Par Q Form
Nasm Par Q Form
2024 NASM CPT Final Exam Study Guide New Full Questions and Answers ( Included ) 100% Correct
2024 NASM CPT Final Exam Study Guide New Full Questions and Answers ( Included ) 100% Correct

sudo apt-get install nasm

Once NASM is installed, we can verify it with:

AD

nasm -v

NASM Personal Training Study Guide - The Fitnessista
NASM Personal Training Study Guide - The Fitnessista
the work out schedule is shown in this file, and it shows how to use it
the work out schedule is shown in this file, and it shows how to use it
NASM Chapter 14 Integrated Program Design and the Optimum Performance Training (OPT) Model
NASM Chapter 14 Integrated Program Design and the Optimum Performance Training (OPT) Model
NASM CNC Chapter 1: Introduction
NASM CNC Chapter 1: Introduction
Seriously!!! The ULTIMATE NASM Study Guide to Ace Your Exam
Seriously!!! The ULTIMATE NASM Study Guide to Ace Your Exam
All About NASM - Peanut Butter Fingers
All About NASM - Peanut Butter Fingers

The command above should display the NASM version if it's installed correctly.

Writing the "Hello, World!" Program

Now that NASM is set up, let's create our "Hello, World!" program. We'll save this code in a file called hello.asm.

AD

The assembly language code for our program might look something like this:

section .data
    msg db 'Hello, World!', 0xa

section .text
    global _start

_start:
    mov eax, 4
    mov ebx, 1
    mov ecx, msg
    mov edx, 13
    int 0x80

    mov eax, 1
    xor ebx, ebx
    int 0x80

Assembling and Running the Program

Now that we have our assembly code, we can assemble it into object code using NASM:

nasm -f elf64 hello.asm -o hello.o

AD

The above command tells NASM to assemble the 64-bit Linux object file hello.o from the hello.asm source file.

Next, we need to link the object file to create an executable. For this, we can use the GNU toolchain's ld linker:

ld hello.o -o hello

Finally, we can run our program with:

./hello

You should see the following output:

Hello, World!

Congratulations! You've just written and run your first NASM program. Keep practicing and exploring to learn more about NASM and assembly programming.

Remember, NASM might seem daunting at first, but with practice, you'll find it a powerful and versatile tool for writing x86 assembly programs.

AD