HOME Acknowledgement Chapter 1 - Basics of Computer Organization Chapter 2 - Introduction to NASM Chapter 3 - Basic I/O in NASM Chapter 4 - Introduction to Programming in NASM Chapter 5 - Integer Handling Chapter 6 - Subprograms Chapter 7 - Arrays and Strings Chapter 8 - Floating Point Operations
National Institute of Technology, Calicut Creative Commons License

NASM Manual NITC

Chapter 4 - Introduction to Programming in NASM


Now that we have gone through all the basics required for creating a program, let's begin. Like all programming lessons, we will first learn how to create a Hello World program. Programming in NASM is easy if you understand how each construct is used and implemented. A transition from C like language can be frustrating at first, but let's take it step by step. We will learn to program by translating code snippets in C language to assembly code.

Hello World Program

As discussed earlier, the whole program is divided into 3 sections, namely code section (section .text), section for uninitalised variables (section .bss) and section for initialised variables (section .text).
section .text is the place from where the execution starts in NASM program, analogous to the main() function in C-Programming.
  1. Let's store the string "Hello World" into a variable named str. We are going to print that string as the output
  2. Pseudo Code:
    char str[13] = "Hello World";
    NASM Code:
    NB: Using equ we declare constants, ie. their value won’t change during execution. $-string will return the length of string variables in bytes (ie. number of characters)
  3. Now we need to print this variable onto the screen. For that we use the write system call.
  4. Pseudo Code:
    printf("%s",str);
    NASM Code:
  5. Now since we have printed the text, let's exit the program. We use the exit system call for the same
    Pseudo Code:
    return 0;
    NASM Code:
  6. section .text is the place from where the execution starts in NASM program, analogous to the main() function in C-Programming. So let's put the print and exit parts into the section.
  7. The final program looks like this: