LOGBOOK

HELP

1 / 46
Other keys: showSpace: good1-4: rate0: skip5: flag

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.

An x86 assembly program branching into three section boxes: .text for read-only executable code, .data for initialized read/write global data, and .rodata for read-only constants and jump tables.

* 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:

or press any other key

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.

Two object files feeding a central linker: one exports main via .globl so the linker matches it to another file's reference, while a symbol without .globl stays file-local.

* 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:

Illustration
Aurelio A. Heckert <aurium@gmail.com> · CC-BY-SA-2.0 · Wikimedia Commons
or press any other key