Skip to content
Tutorials Center
My learning

Topics

Lesson 26 of 40 ยท Tools and Toolchain

Assemblers, Syntax Flavors and Object Files

How assembly text becomes a program: NASM vs GAS, Intel vs AT&T syntax, sections, symbols, directives, and the assemble-and-link pipeline, with demos.

An assembler is the program that turns assembly text into machine code. It is much simpler than a compiler: it translates instructions one to one, works out the numeric address of every label, and packs the result into an object file. A linker then combines object files into a runnable program. Knowing this pipeline matters even if you rarely write assembly by hand, because you will meet its output (.o files, symbol names, sections) whenever you debug native code.

There is also a practical wrinkle: x86 assembly has two common spellings, Intel syntax and AT&T syntax, and several assemblers (NASM, GAS, MASM, and others) with different directives. In this lesson you learn how to tell them apart, what the common sections and directives mean, and how an assembler resolves labels. The real tool commands are read-only; the TryIts are small stand-ins that do the same jobs on toy input.

The pipeline

flowchart LR
  S["hello.asm<br/>(source text)"] -->|assembler: nasm or as| O["hello.o<br/>(object file)"]
  O -->|linker: ld| E["hello<br/>(executable)"]
  L["other .o files<br/>and libraries"] --> E

The assembler translates one source file to one object file; the linker joins objects and libraries into an executable.

On Linux, with the GNU tools, building a program looks like this:

Terminal
# NASM source (Intel syntax)
nasm -f elf64 hello.asm -o hello.o
ld hello.o -o hello

# GNU assembler source (often AT&T syntax)
as hello.s -o hello.o
ld hello.o -o hello

# Or let gcc drive both steps
gcc -c hello.s -o hello.o
gcc hello.s -o hello

The -f elf64 option tells NASM which object format to produce. On Windows the format would be win64, and on macOS macho64. The assembler and linker do not run the program; they only build the file. Running it is a separate step (./hello).

Two syntaxes, one instruction set

The CPU does not care how you spell an instruction; the same bytes come out either way. But the spelling looks quite different.

Feature Intel syntax (NASM, MASM, LLVM option) AT&T syntax (GAS default)
Operand order destination first: mov eax, ebx source first: movl %ebx, %eax
Registers plain: rax prefixed: %rax
Immediates plain: 5 prefixed: $5
Operand size from the register, or dword ptr suffix on mnemonic: movb, movw, movl, movq
Memory [rbx+rcx*4+8] 8(%rbx,%rcx,4)
Comments ; # (or /* */)

The two spellings of the same load:

ASM
; Intel
mov eax, [rbx+rcx*4+8]

# AT&T
movl 8(%rbx,%rcx,4), %eax

The biggest trap is operand order. Intel reads like an assignment (dest = src), while AT&T reads left to right as "move this to there". If your mov seems to do the opposite of what you meant, check which syntax you are reading. GCC emits AT&T by default; gcc -S -masm=intel emits Intel syntax, and objdump -d -M intel disassembles in Intel syntax. This tutorial uses Intel syntax for x86 (and ARM, which has only one syntax in practice).

The next demo converts a few simple Intel-syntax instructions to AT&T, following exactly the rules in the table. It is a toy (it handles only the shapes shown), but it makes the pattern clear.

JS Intel to AT&T converter

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Sections, labels, symbols and directives

An assembly file is more than instructions. Directives (also called pseudo-instructions) are commands to the assembler itself; they produce no instructions but shape the output. They differ between assemblers, but the ideas are the same.

Sections group the output by purpose:

Section Holds Typical permissions
.text machine code read, execute
.data initialized writable data, such as a global int x = 5; read, write
.rodata constants such as string literals read
.bss zero-initialized data; takes no space in the file, only a size read, write

You switch sections with section .data in NASM or .section .data (or just .data) in GAS. The .bss trick is neat: a 1 MB zeroed array costs nothing in the file, because the loader just reserves and zeroes the memory when the program starts.

A label is a name followed by a colon, such as loop: or msg:. It marks a location. When the assembler finishes, every label has a numeric address (within its section). The set of labels and their values is the symbol table. Labels can be local (visible only in the file) or global (exported, so other object files and the linker can see them). The entry point of a Linux program is the global symbol _start, or main when you link with the C runtime.

Common directives:

Purpose NASM GAS
Export a symbol global _start .globl _start
Define bytes db 1, 2, 3 .byte 1, 2, 3
Define a string db "hi", 10 .ascii "hi\n"
Define 32-bit words dd 100 .long 100
Reserve zeroed space resb 64 .zero 64 or .skip 64
Align align 16 .align 16
Constant len equ 14 .set len, 14

Here is the Linux x86-64 "hello" program in both flavors. It assembles with the commands above; the instructions inside are explained in System Calls. Both versions rely on the build being non-position-independent, which is what plain ld produces.

ASM
; hello.asm  (NASM, Intel syntax)
section .data
msg:  db "Hello, world!", 10
len:  equ $ - msg            ; $ means "current address"

section .text
global _start
_start:
  mov eax, 1                 ; write
  mov edi, 1                 ; fd 1 = stdout
  mov esi, msg               ; address of the text
  mov edx, len
  syscall
  mov eax, 60                ; exit
  xor edi, edi
  syscall
ASM
# hello.s  (GNU as, AT&T syntax)
  .section .data
msg:  .ascii "Hello, world!\n"
  .set len, . - msg          # "." means "current address"

  .section .text
  .globl _start
_start:
  movl $1, %eax
  movl $1, %edi
  movl $msg, %esi
  movl $len, %edx
  syscall
  movl $60, %eax
  xorl %edi, %edi
  syscall

Notice how len is computed as "address now minus address of msg", which is the string length. The assembler does this arithmetic itself; it ends up as a plain constant in the instruction.

How an assembler resolves labels

Why can you write jmp done before done: even exists in the file? Because most assemblers work in two passes.

  1. Pass one reads every line, works out how many bytes each instruction or directive will occupy, and assigns each label an address. Nothing is emitted yet.
  2. Pass two goes through the lines again and emits bytes, now substituting the label addresses it learned in pass one.

References to symbols defined in other files cannot be solved by the assembler at all. It leaves a hole and a relocation entry in the object file ("patch this spot with the address of printf"), and the linker fills them in once it has decided where everything goes.

The demo below is a two-pass assembler for an invented 3-instruction machine with .text, .data and .bss sections. The opcode bytes are made up, but the symbol table, address assignment and forward reference handling work the way real assemblers do.

JS A two-pass toy assembler

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Look at jmp done: when pass one reaches it, done is still unknown, but its address is known by pass two, so the jump gets the right target. The .bss section appears in the symbol table (buf has an address) but contributes no bytes to the output.

Object files: ELF and PE

The output of an assembler is an object file, and the final program is an executable file. Both are structured containers, and the format depends on the operating system.

  • ELF (Executable and Linkable Format) is used on Linux, BSD and many other Unix-like systems. It starts with the four bytes 7f 45 4c 46 (the characters ELF after a 0x7F byte), followed by a header giving the architecture, entry point and the locations of its tables.
  • PE (Portable Executable) is used on Windows (.exe and .dll). It starts with MZ, a legacy DOS header, which points to a PE header.
  • Mach-O is used on macOS and iOS.

An ELF file contains sections (for the linker's view) and segments (for the loader's view: which bytes to map into memory and with which permissions), a symbol table, and relocation entries. You can look inside with standard tools:

Terminal
readelf -h hello          # the ELF header: class, machine, entry point
readelf -S hello.o        # sections: .text, .data, .bss, .symtab ...
nm hello.o                # symbols with their types (T = text, D = data, U = undefined)
objdump -d -M intel hello # disassemble the code in Intel syntax

An undefined symbol (U in nm) is exactly the "hole" mentioned earlier: the object file uses something that another object file or library has to supply.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. In AT&T syntax, which instruction copies the value 5 into eax?

    AT&T puts the source first, prefixes immediates with $ and registers with %, and uses a size suffix such as l for 32 bits.
  2. 2. Where does a zero-initialized global array of 1 MB live, and what does it cost in the executable file?

    .bss describes memory that the loader zero-fills at start-up, so the file only stores how big it is.
  3. 3. Why does an assembler need two passes?

    Pass one assigns every label an address; pass two emits the bytes using those addresses, so a jump to a later label works.
  4. 4. What does U mean for a symbol in nm output?

    An undefined symbol is a reference to something defined elsewhere; the linker resolves it using a relocation.

Summary

  • An assembler translates text into an object file; a linker combines objects and libraries into an executable.
  • Intel syntax is destination-first and plain; AT&T syntax is source-first with % registers, $ immediates and size suffixes.
  • Sections separate code (.text), writable data (.data), constants (.rodata) and zeroed data (.bss, which takes no file space).
  • Labels become symbols with addresses; global symbols are visible to the linker, and references to other files become relocations.
  • Assemblers use two passes so forward references work.
  • ELF (Linux), PE (Windows) and Mach-O (macOS) are the container formats; readelf, nm and objdump let you inspect them.

Next: Inline Assembly in C, where you embed a few instructions inside a C program.