dq - Define Quad Word
Part of the Fundamentals section of Coddy's Assembly journey — lesson 27 of 45.
dd creates 4 bytes of data. But sometimes you need even larger numbers or memory addresses. For that, you use dq — Define Quad Word.
A quad word in x86 assembly is 8 bytes (64 bits).
Syntax:
label dq valueExamples:
section .data
myNumber dq 10000000000 ; 8 bytes, value 10 billion
bigNum dq 123456789012 ; Very large number
address dq msg ; Memory address of label "msg"Comparison table:
| Directive | Size | Range | Register to use |
|---|---|---|---|
db | 1 byte | 0 to 255 | al |
dw | 2 bytes | 0 to 65,535 | ax |
dd | 4 bytes | 0 to 4,294,967,295 | eax |
dq | 8 bytes | Very large (2⁶⁴-1) | rax |
How to use a quad word in your code:
section .data
myNumber dq 5000000000
section .text
mov rax, [myNumber] ; Load value into rax
add rax, 1000000000 ; rax becomes 6 billion
mov [myNumber], rax ; Store backSpecial use: storing addresses
dq is also used to store memory addresses (pointers):
section .data
message db 'Hello', 0
pointer dq message ; Stores the address of messageImportant rule:
| Rule | Explanation |
|---|---|
Use rax to load or store | rax is the 64-bit register |
Challenge
EasyCreate a quad word called universe_age with the value 13800000000 in the .data section.
Try it yourself
%include "data.asm"
section .data
msg db 'universe_age = '
msg_len equ $ - msg
newline db 0xa
section .bss
buffer resb 64
section .text
global _start
_start:
; Load the quad word value into rax
mov rax, [universe_age]
; Convert rax to decimal string
mov rsi, buffer
add rsi, 63 ; Go to end of buffer
mov byte [rsi], 0 ; Null terminator
mov rcx, 10 ; Divisor for decimal conversion
convert_loop:
xor rdx, rdx ; Clear rdx for division
div rcx ; Divide rax by 10, rax = quotient, rdx = remainder
add dl, '0' ; Convert remainder to ASCII
dec rsi
mov [rsi], dl ; Store digit
cmp rax, 0
jnz convert_loop
; Save digit pointer and length BEFORE they get clobbered
mov r8, rsi ; r8 = pointer to first digit
mov r9, buffer
add r9, 63
sub r9, rsi ; r9 = number of digits
; Print message
mov rax, 1
mov rdi, 1
mov rsi, msg
mov rdx, msg_len
syscall
; Print the number
mov rax, 1
mov rdi, 1
mov rsi, r8
mov rdx, r9
syscall
; Print newline
mov rax, 1
mov rdi, 1
mov rsi, newline
mov rdx, 1
syscall
; Exit
mov rax, 60
mov rdi, 0
syscall
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Fundamentals
1The Machine
What is CPUMemoryRegisters vs MemoryHow instructions workTwo-File PatternThe x86-64 Architecture4Data Definition
The .data Sectiondb - Define Bytedw - Define Worddd - Define Double Worddq - Define Quad WordStrings with dbThe $ SymbolRecap: Build data.asm