LinuxCommandLibrary

nasm

Assemble assembly language code into object files

TLDR

Assemble source.asm into a binary file source, in the (default) raw binary format

$ nasm [source.asm]
copy

Assemble source.asm into a binary file output_file, in the specified format
$ nasm -f [format] [source.asm] -o [output_file]
copy

List valid output formats (along with basic nasm help)
$ nasm -hf
copy

Assemble and generate an assembly listing file
$ nasm -l [list_file] [source.asm]
copy

Add a directory (must be written with trailing slash) to the include file search path before assembling
$ nasm -i [path/to/include_dir/] [source.asm]
copy

SYNOPSIS

nasm [options] filename [-o outputfile] [-l listfile] [-f format]

PARAMETERS

filename
    The input assembly source file.

-o outputfile
    Specifies the output file name. If not specified, the assembler may attempt to determine a default name based on the input file name or the selected output format.

-l listfile
    Generates a listing file containing the assembled code alongside the source code.

-f format
    Specifies the output object file format. Common formats include bin (raw binary), elf32, elf64, coff, macho32, macho64, win32, win64, and aout. Use nasm -hf for a complete list.

-I path
    Adds a directory to the include file search path.

-D macro[=value]
    Defines a preprocessor macro.

-U macro
    Undefines a preprocessor macro.

-E
    Preprocess only (don't assemble).

-M
    Generate Makefile dependencies.

-w+warning
    Enable a specific warning.

-w-warning
    Disable a specific warning.

-g
    Enable debugging information output (e.g., DWARF).

DESCRIPTION

NASM (Netwide Assembler) is a free and open-source assembler for the x86 architecture. It's designed for portability and modularity, supporting a wide range of object file formats, including those used by Linux, Windows, and other operating systems. NASM excels at assembling small, highly optimized code snippets for bootloaders, device drivers, system programming, and performance-critical sections of larger applications.

It features a simple and powerful macro system, allowing for code reuse and abstraction. NASM supports multiple assembly syntaxes and preprocessor directives. It's considered highly customizable and often preferred by developers needing fine-grained control over the generated machine code. Unlike some assemblers, NASM's primary focus is to generate efficient machine code, so it provides fewer features for direct linking or high-level debugging.

NASM is frequently used in the context of security research and exploit development due to its ability to create position-independent code (PIC) and control instruction encoding.

CAVEATS

NASM does not perform linking. The resulting object files must be linked using a separate linker (e.g., ld). It supports limited high-level debugging functionality compared to compiler toolchains like GCC or Clang.

NASM SYNTAX

NASM uses the Intel syntax, which means the destination operand comes before the source operand in instructions (e.g., `mov eax, ebx`). It's important to be aware of this, especially if you're accustomed to AT&T syntax used by GNU assembler.

SECTIONS

Assembly code is typically organized into sections, such as `.text` (for executable code), `.data` (for initialized data), and `.bss` (for uninitialized data). These sections are important for linking and loading the program.

MACROS

NASM's macro system provides a powerful way to define reusable code snippets. Macros can take arguments and perform complex operations based on those arguments.

HISTORY

NASM was initially developed in the late 1990s by Julian Smart and Simon Tatham. Its primary goal was to create a portable, modular, and free x86 assembler. It quickly gained popularity due to its ease of use, support for multiple platforms, and powerful macro system. It is under active development to this day and continues to adapt to the evolving x86 instruction set architectures.

SEE ALSO

ld(1), objdump(1)

Copied to clipboard