Output formats: bin, elf, elf64
The common formats
The -f flag selects the output format. elf64 is the usual target for Linux programs, elf32 for 32-bit objects, win64 for Windows, macho64 for macOS, and bin for a flat raw binary. The flag is not optional: without it NASM picks bin:
nasm -f elf64 -o prog.o prog.asm nasm -f bin -o boot.bin boot.asm
elf64 and elf32
ELF objects carry section and symbol tables, so the linker can place and rename them. Every lesson so far used elf64. The 32-bit format differs in register assignment for syscalls (int 0x80 with EAX and EBX).
bin: flat binary
bin emits exactly the bytes you assembled, with no headers. That is what a boot sector needs: 512 bytes ending in the 0xAA55 signature. The origin point is set with the bits and org directives, because there is no header to say where the code lives.
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
Which flag selects the NASM output format?
Answer
-f
-f names the format, as in -f elf64 or -f bin.
-
Which format should a Linux program that ld links use?
Answer
elf64
The GNU linker reads ELF objects, and elf64 is the 64-bit form.
-
What does
-f binproduce?Answer
Exactly the assembled bytes, with no headers
The flat form is what a boot sector or a raw image needs.
-
How large must a boot sector be, and how does it end?
Answer
512 bytes ending in 0xAA55
The BIOS reads one 512-byte sector and checks the signature in its last two bytes.
-
Why does a flat binary need
org?Answer
There is no header to say where the code is loaded
org tells the assembler the address the code will run at, so labels resolve.
-
Which directive selects 16-bit code in a boot sector?
Answer
bits 16
The CPU starts a boot sector in real mode, which is 16-bit.
-
What does an ELF object carry that a flat binary does not?
Answer
Section and symbol tables
The tables let the linker place sections and resolve symbols by name.
-
Which 32-bit Linux syscall interface does the elf32 format use?
Answer
int 0x80 with EAX and EBX
The 32-bit path enters through int 0x80 with the 32-bit registers.
-
What does NASM do when no -f flag is given?
Answer
It uses bin
The plain output format is bin, and a program that needs 64-bit code fails in its 16-bit default mode.
-
Which format is for a macOS object?
Answer
macho64
macOS uses Mach-O, and macho64 is its 64-bit object format.
The code on this page was run with NASM 3.01 on Alpine Linux 3.24.3 (x86_64) in a Docker container.
Article author: Arthur Isaev