Part 5: Assembly Data Allocation: DB, DW, DD, DQ, EQU & NASM Directives
Lesson Overview & Data Management
Unlike high-level languages with abstract typing systems, assembly language models data purely as sequences of contiguous bytes. The developer bears full responsibility for declaring memory reservations, enforcing operand size constraints, managing Little-Endian byte order, and aligning data to hardware cache-line boundaries. In Part 5, we analyze NASM data allocation directives (DB, DW, DD, DQ), uninitialized reservations in .bss, compile-time constants (EQU), and preprocessor macro directives.
1. Initialized Data Directives: DB through DQ
In the .data and .rodata sections, data values are placed into the compiled ELF-64 binary image using the "Define" family of directives:
| Directive | Name | Size (Bytes / Bits) | Typical Data Represented | Example Code |
|---|---|---|---|---|
| DB | Define Byte | 1 byte (8 bits) | ASCII character, byte flags, booleans, int8 | is_active db 1 msg db "Draftsbook", 0 |
| DW | Define Word | 2 bytes (16 bits) | UTF-16 chars, short integers (int16), port numbers | port dw 8080 val dw 0xFFFF |
| DD | Define Doubleword | 4 bytes (32 bits) | Standard int/long, single-precision IEEE float | total dd 100000 pi dd 3.14159 |
| DQ | Define Quadword | 8 bytes (64 bits) | 64-bit pointers, size_t, int64, double-precision float | ptr dq 0x7FFFFFFF1000 big_num dq 10000000000 |
2. Little-Endian Architecture and Byte Ordering
All x86 and x86-64 processors are strictly Little-Endian. This means the least significant byte (LSB) is stored at the lowest numerical memory address, while the most significant byte (MSB) is stored at the highest address.
; Consider the 32-bit hexadecimal value: 0x12345678
value dd 0x12345678
; Physical layout in contiguous memory bytes:
Address + 0: 0x78 (Least Significant Byte)
Address + 1: 0x56
Address + 2: 0x34
Address + 3: 0x12 (Most Significant Byte)
3. Uninitialized Memory in .bss: RESB through RESQ
Declaring large buffers (such as a 10 MB network cache) in .data inflates your compiled binary on disk to 10 MB. In contrast, reserving space in the .bss section costs 0 bytes on disk. The operating system kernel zeroes out the pages on demand when the program starts.
section .bss
packet_buffer resb 65536 ; Reserve 64 KB byte buffer
audio_samples resw 1024 ; Reserve 1024 16-bit words
user_ids resd 500 ; Reserve 500 32-bit integers
hash_table resq 1000 ; Reserve 1000 64-bit pointers
4. Constants and Preprocessor Directives: EQU vs %define
EQU (Assembler Constant)
Evaluated by the assembler during pass 1. Immutable symbolic constant that produces no runtime memory footprint:
SYS_EXIT equ 60 BUFFER_SZ equ 4096
%define (Preprocessor Macro)
Textual macro substitution performed before assembly begins. Can accept parameters:
%define STDOUT 1 %define ALIGN_16(x) (((x)+15)&~15)
Summary & Next Step
Now that we can allocate and format memory, we are ready to perform computation. In Part 6, we explore x86-64 arithmetic operations: addition, subtraction, signed and unsigned multiplication and division, and how the ALU updates the RFLAGS status register.
Related reading
Part 11: Linux 64-Bit System Calls, NASM Macros & Dynamic Memory Allocation
Learn low-level systems programming: Linux 64-bit SYSCALL table, multi-argument NASM %macro directives, file I/O operations, and sys_mmap memory allocation.
By Md. Harun-Or-Roshid
Part 10: Stack Frames, Procedures & The System V AMD64 ABI in Assembly
Master assembly subroutines: CALL, RET, stack frames, RBP base pointers, 16-byte stack alignment, and the official System V AMD64 ABI calling convention.
By Md. Harun-Or-Roshid
Part 9: Assembly Arrays, String Primitives & Hardware Streaming (REP MOVSB)
Master memory buffers and strings in x86-64 assembly: arrays, strides, and hardware block primitives including MOVSB, LODSB, STOSB, SCASB, and REP.
By Md. Harun-Or-Roshid
Part 8: Conditional Branching, Jump Instructions & Fast Loops in Assembly
Learn how conditional jumps work in x86-64 assembly using CMP, TEST, JE, JNE, JG, JL, JA, JB, and write high-performance loops with branch prediction.
By Md. Harun-Or-Roshid