db - Define Byte
Part of the Fundamentals section of Coddy's Assembly journey — lesson 24 of 45.
The most basic way to create data in memory is db — Define Byte. It creates one byte (8 bits) of data.
Syntax:
label db valuelabel— the name you give to this memory addressdb— Define Byte directivevalue— the number or character to store (0 to 255)
Examples:
section .data
age db 25 ; Creates a byte called "age" containing 25
grade db 'A' ; Creates a byte called "grade" containing 65 (ASCII for 'A')
zero db 0 ; Creates a byte called "zero" containing 0What you can store in one byte:
| Type | Range | Example |
|---|---|---|
| Small number | 0 to 255 | db 100 |
| Character | 'A' to 'z' | db 'X' |
| ASCII code | 0 to 255 | db 65 (same as 'A') |
Multiple bytes on one line:
You can define several bytes in a row using commas:
section .data
numbers db 5, 10, 15, 20 ; Creates 4 bytes: 5, 10, 15, 20
letters db 'A', 'B', 'C' ; Creates 3 bytes: 65, 66, 67How to use a byte in your code:
section .data
myByte db 42
section .text
mov al, [myByte] ; Load the value 42 into al
add al, 1 ; al becomes 43
mov [myByte], al ; Store 43 back into myByteImportant rule:
| Rule | Explanation |
|---|---|
| Value must fit in 1 byte | 0 to 255 (or a single character) |
Use al to load or store | al is the 8-bit register for bytes |
Common mistake:
; Wrong: trying to store a number too large for a byte
section .data
error db 300 ; 300 is too big (max 255)Challenge
EasyCreate a byte called score with the value 15 in the .data section.
Write the data.asm file with the correct .data section and the db directive.
Try it yourself
%include "data.asm"
section .text
global _start
_start:
mov al, [score]
mov r12, rax
add al, 48
mov [digit], al
mov rax, 1
mov rdi, 1
mov rsi, digit
mov rdx, 1
syscall
mov rax, 60
mov rdi, 0
syscall
section .bss
digit resb 1This 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