The Toolchain: nasm, ld, objdump
Turning an assembly file into a running program takes two steps — assemble and link — and a third tool, a disassembler, lets you look back inside the result. The playgrounds on this site do these steps for you on a remote Linux machine, but it is worth knowing exactly what happens.
Step 1: assemble with NASM
NASM reads your .asm text file and writes an object file: machine code plus a table of symbols (labels) and relocations (places where an address must be filled in later).
nasm -f elf64 -g hello.asm -o hello.o| Option | Meaning |
|---|---|
-f elf64 |
output format: 64-bit ELF, the Linux object format (win64 and macho64 exist for other systems) |
-g |
include debug information for gdb |
-o hello.o |
the output file name |
-l hello.lst |
also write a listing: every source line next to its address and machine-code bytes |
A listing is the quickest way to see what your instructions turn into:
1 section .text
2 global _start
3 _start:
4 00000000 B83C000000 mov eax, 60
5 00000005 31FF xor edi, edi
6 00000007 0F05 syscallStep 2: link with ld
The linker combines one or more object files into an executable. It decides the final addresses of every section, fills in the relocations, and records where execution starts: by default at the symbol _start, which is why every program declares global _start.
ld hello.o -o hello
./hello
echo $? # show the exit status of the last commandPrograms in this course do not use the C library, so plain ld is all you need. When you want printf and friends, you link with gcc instead (see Linking with C).
Step 3: look inside with objdump
objdump disassembles an executable back into instructions. -M intel selects Intel syntax (what NASM uses) instead of the AT&T default:
objdump -d -M intel hello0000000000401000 <_start>:
401000: b8 3c 00 00 00 mov eax,0x3c
401005: 31 ff xor edi,edi
401007: 0f 05 syscallThe left column is the address (ld places code at 0x401000 by default), then the bytes, then the instruction. Other useful tools: readelf -a (headers and sections), nm (symbols) and strace ./hello (shows every system call a program makes).
What can go wrong
Problems are reported at different stages, and recognising the stage helps you fix them. A typo in an instruction is found by the assembler, which prints the line number:
section .text
global _start
_start:
mvo eax, 60 ; typo!
xor edi, edi
syscall<source>:4: error: parser: instruction expected
Using an operand combination that doesn't exist is also an assembler error. x86 has no instruction that moves memory to memory, and both operands of mov must have the same size:
section .data
a: dq 1
b: dq 2
section .text
global _start
_start:
mov [a], [b] ; two memory operands: not encodable
mov eax, rbx ; 32-bit and 64-bit operands mixed
mov eax, 60
xor edi, edi
syscall<source>:8: error: invalid combination of opcode and operands
<source>:9: error: invalid combination of opcode and operands
A missing label is reported by NASM too — unless you declared it extern, a promise that another object file defines it. Then the linker is the one that complains:
section .text
global _start
extern helper ; "defined somewhere else"
_start:
call helper
mov eax, 60
xor edi, edi
syscall$ nasm -f elf64 prog.asm -o prog.o # assembles without complaint
$ ld prog.o -o prog
ld: prog.o: in function `_start':
prog.asm:(.text+0x1): undefined reference to `helper'Finally, runtime errors are not reported by any tool: the program assembles and links fine, then crashes or misbehaves. The kernel stops a program that touches memory it may not use with a signal such as SIGSEGV, and the only way to find out why is to reason about the code or use a debugger (Debugging with gdb).
A complete workflow
nasm -f elf64 -g -l prog.lst prog.asm -o prog.o # assemble (+ listing)
ld prog.o -o prog # link
./prog; echo "exit status: $?" # run
objdump -d -M intel prog | less # inspect
gdb ./prog # debugA small Makefile or shell script with these lines saves a lot of typing when you work locally.
Test yourself
Which tool decides the final addresses of your code and data?
- nasm
- ld (the linker)
- objdump
- the CPU
What does objdump -d -M intel prog show?
- The source file
- The disassembled machine code in Intel syntax
- The program's output
- The exit status
mov [a], [b] fails to assemble. Why?
aandbare reserved names- x86 instructions can't have two memory operands
movonly works with registers- Square brackets are not allowed with mov