Menu
Coddy logo textTech

The .data Section

Part of the Fundamentals section of Coddy's Assembly journey — lesson 23 of 45.

In Chapter 2, you loaded values from memory and stored values to memory. But where did that memory come from? Who created num1, num2, and result?

The answer is the .data section. This is where you create your own data in memory.

What is the .data section?

The .data section is a part of your assembly program where you define data that will be stored in memory before your program runs.

Syntax:

section .data
    ; Your data definitions go here

Why you need it:

Without .data sectionWith .data section
You have no data to work withYou can create numbers, strings, and variables
Memory addresses are unknownYou can use labels like myNumber or message
You cannot test your codeYou can load and store data you created

A simple example:

section .data
    age db 25           ; Create a byte called "age" with value 25
    price dw 1000       ; Create a word called "price" with value 1000
    name db 'Bob'       ; Create a string called "name" with letters B, o, b

Where it goes in your files:

Remember the two-file pattern from Chapter 0:

FileContent
data.asmsection .data with your data definitions
main.asm%include "data.asm" followed by section .text with your code

What happens when your program runs:

  1. The operating system loads your program into memory
  2. It reads the .data section and places your data at specific memory addresses
  1. Your labels (age, price, name) become addresses
  2. Your code can now access these addresses using [age], [price], [name]

Important rules:

RuleExplanation
One .data section per programPut all your data in one place
Labels must be uniqueYou cannot have two age labels
Data is created before your code runsIt is ready to use when _start begins

In the next lessons, you will learn how to define different types of data using db, dw, dd, and dq

challenge icon

Challenge

Write the code in the data.asm file to:

  1. Create the .data section
  2. Create a byte called count with the value 5

Try it yourself

%include "data.asm"

section .text
    global _start

_start:
    ; Load the value from count into al
    mov al, [count]
    
    ; Save it for printing
    mov r12, rax
    
    ; Convert to ASCII and print (0-9 only)
    add al, 48
    mov [digit], al
    
    mov rax, 1
    mov rdi, 1
    mov rsi, digit
    mov rdx, 1
    syscall
    
    ; Exit
    mov rax, 60
    mov rdi, 0
    syscall

section .bss
    digit resb 1
quiz iconTest yourself

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

All lessons in Fundamentals