nasm
Assemble assembly language code into object files
TLDR
Assemble source.asm into a binary file source, in the (default) raw binary format
Assemble source.asm into a binary file output_file, in the specified format
List valid output formats (along with basic nasm help)
Assemble and generate an assembly listing file
Add a directory (must be written with trailing slash) to the include file search path before assembling
SYNOPSIS
nasm [options] <input_file> [-o <output_file>]
To compile an assembly file:
nasm -f elf mycode.asm -o mycode.o
To link the object file (using ld):
ld -m elf_i386 mycode.o -o myprogram
PARAMETERS
-o <file>
Specifies the name of the output file. If omitted, NASM attempts to guess a name based on the input filename and selected format, or defaults to a.out.
-f <format>
Selects the output file format. Common formats include bin (flat binary), elf (ELF object file), a.out, coff, macho, win32, etc. The default is bin.
-l <file>
Generates a listing file, useful for debugging, showing source code, corresponding machine code, and symbol addresses.
-g
Produces debugging information in the output file, often in DWARF or STABS format, enabling symbolic debugging with tools like GDB.
-D <macro>[=<value>]
Defines a preprocessor macro. Similar to #define in C, allowing conditional assembly or constant definitions directly from the command line.
-U <macro>
Undefines a previously defined preprocessor macro, overriding any -D options or built-in definitions.
-I <directory>
Adds a directory to the include file search path, where NASM looks for files specified by the %include directive.
-M
Generates a Makefile-compatible dependency file to standard output, listing all source and include files.
-v
Prints the NASM version number and exits.
-s
Causes NASM to be silent, suppressing all output except critical error messages.
-w+<warning> | -w-<warning>
Enables or disables specific warnings by category (e.g., -w+macro-params). Use -w help to see all warning categories.
-Z <file>
Redirects error messages to the specified file instead of standard error.
-E
Only preprocesses the input file and prints the result to standard output, without performing assembly.
DESCRIPTION
NASM, the Netwide Assembler, is a popular open-source assembler primarily used for the Intel x86 and x64 instruction set architectures. It functions as a powerful tool for translating human-readable assembly language source code into machine code, which computers can directly execute. Unlike some other assemblers, NASM is designed to be highly portable, running on various operating systems including Linux, Windows, and macOS, and supports numerous output formats such as ELF, a.out, COFF, Mach-O, and flat binary files.
Its two-pass design efficiently handles forward references, making it robust for complex assembly projects. Programmers often choose NASM for low-level system programming tasks, including developing bootloaders, operating system kernels, device drivers, and performance-critical routines where direct hardware control and optimization are paramount. NASM distinguishes itself with its straightforward Intel-style syntax, offering a clean and predictable way to write assembly code, making it a favorite for both beginners and experienced assembly programmers seeking precise control over generated machine code.
CAVEATS
NASM primarily supports Intel-style syntax, which differs significantly from AT&T syntax used by GNU Assembler (as). This means code written for one assembler is generally not directly compatible with the other. Additionally, NASM is an assembler, not a linker; it produces object files which typically require a separate linking step with a linker like ld to create an executable program. It focuses on the x86/x64 instruction set and does not support other architectures directly.
<B>INTEL SYNTAX FOCUS</B>
NASM strictly adheres to Intel assembly syntax (e.g., 'MOV dest, src'), which is prevalent in many operating system development contexts and direct hardware interactions, distinguishing it from AT&T syntax used by GNU Assembler (as) where 'MOV src, dest' with register prefixes is common.
<B>TWO-PASS DESIGN</B>
NASM employs a two-pass assembly process. The first pass determines the size and position of all instructions and data, resolving forward references (symbols defined later in the code). The second pass then generates the actual machine code and fills in all addresses and values, ensuring efficient handling of complex assembly programs.
<B>MACRO PREPROCESSOR</B>
Beyond basic assembly, NASM includes a powerful built-in macro preprocessor, similar to C's preprocessor. It supports directives like %define, %include, %if, %ifdef, and %macro, allowing for code reusability, conditional assembly, and creation of complex, parameterized assembly routines, significantly enhancing code modularity and maintainability.
HISTORY
NASM was initially conceived and developed by Simon Tatham, with the first public release in 1996. The project's goal was to create a highly portable and flexible x86 assembler, overcoming limitations and quirks found in existing assemblers like MASM or TASM, especially regarding their reliance on specific operating systems. Its commitment to a clean, Intel-style syntax and support for numerous output formats quickly made it a popular choice, particularly in the open-source community for developing operating systems and low-level utilities. Over the years, maintenance and development have been taken over by various contributors, ensuring its continued relevance and adaptation to new x86 architecture features.