Menu
Coddy logo textTech

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:

AddressByteASCII 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', 0x0a

Now the string has 6 bytes: 'H','e','l','l','o', newline

Multiple strings:

section .data
    hello db 'Hello', 0x0a
    goodbye db 'Goodbye', 0x0a

Why 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 icon

Challenge

Easy

Create 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
    syscall
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Fundamentals