Learning x86 with NASM — Working with Data and Stack Memory

Scott Cosentino
4 min readJun 1, 2022

In this article, we will create a program that stores simple numeric data using the .data section. We will first look at the different methods of declaring data. Once we have our data declared, we will look at how it can be loaded into a register for use.

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

Declaring Data in NASM x86

To declare data, we need to provide three pieces of information in the .data section of the program:

1. The variable name used to reference the data

2. The format of the data

3. The initial value of the data

For example, the code below shows how data can be declared in a program.

In this code, we have declared a variable named num. The type of this variable is DW, which stands for declare word. A word is an allocation of 16 bits of memory. The initial value of the variable is 0. To summarize, this code declares 16 bits of space in memory, initializes it to 0, and associates it with the name num.

Before we look at using the variable, let’s discuss a bit about the different data types that exist in NASM x86. We’ve seen that a word is declared using DW, giving us 16 bits of space. Below is a table showing the common data types and their sizes.

It is important to pick the data type that best fits the data size you are working with. If a variable will only ever need 8 bits, you should use a byte instead of using a larger size. This allows you to save on memory, which is a precious resource in the world of assembly programming. Now that you understand the different data types, we can start to look at using the variables in our programs.

Using Data in x86

To understand how data is stored and referenced in x86, let’s consider a simple example program.

This program declares a double word with a value of 5, referenced by the name num. We then set up the exit system call, with the status code set to…