dd - Define Double Word
Part of the Fundamentals section of Coddy's Assembly journey — lesson 26 of 45.
dw creates 2 bytes of data. But sometimes you need even larger numbers. For that, you use dd — Define Double Word.
A double word in x86 assembly is 4 bytes (32 bits).
Syntax:
label dd valueExamples:
section .data
myNumber dd 50000 ; 4 bytes, value 50000
bigNum dd 4000000 ; 4 bytes, value 4 million
max32 dd 4294967295 ; Maximum 32-bit valueComparison 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 |
How to use a double word in your code:
section .data
myNumber dd 50000
section .text
mov eax, [myNumber] ; Load value into eax
add eax, 10000 ; eax becomes 60000
mov [myNumber], eax ; Store backImportant rule:
| Rule | Explanation |
|---|---|
Use eax to load or store | eax is the 32-bit register |
Challenge
EasyCreate a double word called distance with the value 125000 in the .data section.
Try it yourself
%include "data.asm"
section .data
msg db 'distance = '
msg_len equ $ - msg
newline db 0xa
section .bss
buffer resb 32
section .text
global _start
_start:
; Load the double word value into eax
mov eax, [distance]
; Convert eax to decimal string
mov rsi, buffer
add rsi, 31 ; Go to end of buffer
mov byte [rsi], 0 ; Null terminator
mov ecx, 10 ; Divisor for decimal conversion
convert_loop:
xor edx, edx ; Clear edx for division
div ecx ; Divide eax by 10, eax = quotient, edx = remainder
add dl, '0' ; Convert remainder to ASCII
dec rsi
mov [rsi], dl ; Store digit
cmp eax, 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, 31
sub r9, rsi ; r9 = number of digits
; Print message
mov rax, 1
mov rdi, 1
mov rdx, msg_len
mov rsi, msg
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