Question
What are the two main sections in an x86 assembly program and what does each contain?
Answer
.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.
Note saved — thanks!
Question
What does the .globl directive do in assembly, and what happens to a symbol without it?
Answer
.globl name exports a symbol so the linker can resolve references to it from other object files; without it the symbol is local to its own file.
* The .globl directive exports a symbol for the linker to match across files; a symbol without it stays file-local like C static. *
When you compile multiple .c/.s files separately, each becomes an object file. The linker then stitches them together by matching up symbol names. A symbol only participates in that matching if it's marked global — otherwise it's invisible outside its own file.
.globl main # main is visible to the linker (and to the C runtime)
main:
...
This is the assembly equivalent of external linkage in C. A C function is global by default; marking it static makes it file-local — exactly the difference between having .globl and omitting it.
Common gotcha: If main is missing .globl, the C startup code can't find your entry point and the linker reports an undefined-reference error for main.
Go deeper:
GNU Assembler (Wikipedia) — what GAS is and how its output feeds the linker.
Using as — the GNU Assembler manual — the .globl/.global directive and symbol visibility in the manual.
Note saved — thanks!