Skip to main content
← All posts

Part 5: Assembly Data Allocation: DB, DW, DD, DQ, EQU & NASM Directives

Md. Harun-Or-RoshidNov 5, 20263 min readArticle
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:

DirectiveNameSize (Bytes / Bits)Typical Data RepresentedExample Code
DBDefine Byte1 byte (8 bits)ASCII character, byte flags, booleans, int8is_active db 1 msg db "Draftsbook", 0
DWDefine Word2 bytes (16 bits)UTF-16 chars, short integers (int16), port numbersport dw 8080 val dw 0xFFFF
DDDefine Doubleword4 bytes (32 bits)Standard int/long, single-precision IEEE floattotal dd 100000 pi dd 3.14159
DQDefine Quadword8 bytes (64 bits)64-bit pointers, size_t, int64, double-precision floatptr 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.

Share:
Share on X
Share on LinkedIn
Share on Facebook
Share on Reddit
Share on WhatsApp
Share on Telegram
Copy link

Related reading

Assembly Data Types: DB, DW, DD, DQ & EQU | Draftsbook · Draftsbook