What are variables in assembly language?

Assembly language directives are used to allocate space in the memory for variables. Different directives are used to allocate space of different sizes.

Directives

We list down the directives provided by the assembler for the Intel X86 processors, Netwide Assembler (NASM) below:

  • DB – Define Byte is used to allocate only 1 byte.
  • DW – Define Word allocates 2 bytes.
  • DD – Define Doubleword is used to allocate 4 bytes.
  • DW – Define Quadword allocates 8 bytes.
  • DT – Define Tenword is used to allocate 10 bytes.

The directives listed above are typically used to allocate space for initialized data and are used in the data section. To reserve memory for uninitialized data, the following directives are used:

  • RESB – Reserves 1 byte.
  • RESW – Reserves 2 bytes or a Word.
  • RESD – Reserves 4 bytes or a Doubleword.
  • RESQ – Reserves 8 bytes or a Quadword.
  • REST – Reserves 10 bytes or a Tenword.

These directives are typically used in the bss section.

Example

The code below shows how directives may be used in an assembly language program to allocate and reserve space for initialized and uninitialized variables, respectively:

section .text
global _start
_start:
mov eax, 0x4 ;4 is the unique system call number of the write system call
mov ebx, 1 ;1 is the file descriptor for the stdout stream
mov ecx, fir_message ;message is the string that needs to be printed on the console
mov edx, fir_length ;length of message
int 0x80 ;interrupt to run the kernel
mov eax, 0x4 ;same steps as above to print sec_message
mov ebx, 1
mov ecx, sec_message
mov edx, sec_length
int 0x80
mov edx, "A" ;sets value of edx to "A"
mov [uninit], edx ;copies value in edx and moves it into the uninitialised variable
mov eax, 0x4 ;prints current value of uninit
mov ecx, uninit
mov edx, 1
int 0x80
mov eax, 0x1 ;1 is the unique system call number of the exit system call
mov ebx, 0 ;argument to exit system call
int 0x80 ;interrupt to run the kernel and exit gracefully
section .data
fir_message db "Welcome to Edpresso!" , 0xA
fir_length equ $-fir_message
sec_message db "The variable in the bss section is now initialised to: "
sec_length equ $-sec_message
section .bss
uninit resb 1 ;declares uninitialised variable

We declare and initialize two strings in the data section of our program. In the bss section, an uninitialized variable of size 1 byte is declared and later initialized to “A” in the code section. The write system call is used to write data to the stdout stream and print it onto the console.

The Netwide Assembler (NASM) can be used to assemble this program. Upon successful execution, it gives the following output:

Welcome to Edpresso!
The variable in the bss section is now initialised to: A

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved