Strings with db
Part of the Fundamentals section of Coddy's Assembly journey — lesson 28 of 45.
You already know that db defines a single byte. But db can also define a string — multiple bytes in a row.
Syntax:
label db 'text here'Example:
section .data
message db 'Hello'This creates 5 bytes in memory: 'H', 'e', 'l', 'l', 'o'
What happens in memory:
| Address | Byte | ASCII value |
|---|---|---|
| message | 'H' | 72 |
| message+1 | 'e' | 101 |
| message+2 | 'l' | 108 |
| message+3 | 'l' | 108 |
| message+4 | 'o' | 111 |
Adding a newline:
To print a string and move to the next line, add 0x0a (newline character):
section .data
message db 'Hello', 0x0aNow the string has 6 bytes: 'H','e','l','l','o', newline
Multiple strings:
section .data
hello db 'Hello', 0x0a
goodbye db 'Goodbye', 0x0aWhy this matters:
Strings are the most common data you will use in assembly. The write syscall (which you will learn in Chapter 5) prints strings by reading bytes from memory until it reaches the length you give it.
Important note:
Strings are just bytes. The CPU does not know or care that 'H' is a letter. It is just the number 72. The write syscall interprets those numbers as characters when printing.
Challenge
EasyCreate a string called greeting with the value 'Hi' followed by a newline (0x0a) in the .data section.
Try it yourself
%include "data.asm"
section .text
global _start
_start:
; Print the string using write syscall
mov rax, 1
mov rdi, 1
mov rsi, greeting
mov rdx, 3 ; 'H', 'i', newline = 3 bytes
syscall
; Exit
mov rax, 60
mov rdi, 0
syscallThis 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 Architecture