What are the two main sections in an x86 assembly program and what does each contain?
.text holds the executable code (functions and instructions); .data holds initialized global/static variables.
* The main assembler sections: .text holds executable code, .data holds initialized read/write globals, and .rodata holds read-only constants. *
An assembly file has no functions or variables in the C sense — it has labeled regions of bytes placed into named sections, and the linker decides where each section ends up in the final program's address space. A section directive like .text or .data simply tells the assembler "everything below here belongs in this section, until I say otherwise."
| Section | Purpose | Typical content |
|---|---|---|
.text |
Executable code (usually read-only at runtime) | Function bodies, instructions |
.data |
Initialized read/write global data | Strings, constants, global variables |
.text
.align 16
.globl main
main:
movl $1, %eax
...
ret
.data
.align 16
hstr:
.ascii "Hello, world!\n"
A label (like main: or hstr:) is just a human-readable alias for the address where the next byte lands — in .text it names a function or jump target, in .data it names a variable.
Tip: Read-only constants and jump tables often go in a third section, .rodata (read-only data), so the OS can mark those pages non-writable.
Go deeper:
x86 assembly language (Wikipedia) — the reference on x86 sections, syntax and directives.
Using as — the GNU Assembler manual — the assembler manual — section directives .text/.data/.rodata and how they work.