NASM(1)

๐Ÿ’– NASM(1) โ€” The Netwide Assembler Project ๐Ÿ’–

okay bestie, sit DOWN, we're learning the Netwide Assembler today and honestly? it's giving iconic. this is nasm, the portable 80x86 assembler, and this page is your group chat briefing. ๐Ÿซถ


โœจ NAME โœจ

nasm โ€” the Netwide Assembler, a portable 80x86 assembler ๐Ÿ’…


โœจ SYNOPSIS โœจ

nasm [-@ response file] [-f format] [-o outfile] [-l listfile]
[options...] filename

โœจ DESCRIPTION โœจ

so the nasm command takes your filename and assembles it, then yeets the output into outfile if you said so. no outfile? no problem, queen โ€” nasm will just make up a default output name based on your input file, usually by slapping .o or .obj on the end, or by stripping ALL the extensions if it's a raw binary file. and if even that fails? the output file is literally just nasm.out. default queen behavior. ๐Ÿ‘‘


โœจ OPTIONS โœจ

  • -@ filename โ€” makes nasm read options out of filename, like they were typed on the command line. cute lil shortcut, love that for us.
  • -a โ€” assemble the input file without running the macro preprocessor first. raw. unfiltered. no cap.
  • -D|-d macro[=value] โ€” pre-defines a single-line macro. yes, lowercase works too, they're twins. ๐Ÿ‘ฏ
  • -E|-e โ€” preprocess the input file and write the result to stdout (or wherever your output file name points), and then... not assemble anything. just vibes. it's the "look but don't touch" option.
  • -f format โ€” pick your output file format. wanna see the valid ones? hit up -hf, bestie.
  • -F format โ€” pick your debug information format. to see the valid ones, use the -y option (like, for example, -felf -y). yes really, that's the example. ๐Ÿ’€
  • -g โ€” generate debug information.
  • -gformat โ€” literally the same thing as -g -F format. two for one, slay.
  • -h โ€” nasm exits immediately after giving you a lil summary of its invocation options. bye girl ๐Ÿ’จ
  • -hf โ€” same as -h , but it ALSO lists all valid output formats. the deluxe version.
  • -I|-i directory โ€” adds a directory to the search path for include files. โš ๏ธ the directory spec must include the trailing slash, because it gets directly prepended to the name of the include file. no slash, no search, no mercy.
  • -l listfile โ€” sends an assembly listing to the given file. in that listing, the original source sits on the right hand side (plus the source for included files and the expansions of multi-line macros) and the generated code shows up in hex on the left. aesthetic AND informative. โœจ
  • -M โ€” outputs Makefile-style dependencies to stdout; normal output gets suppressed. we're in dependency mode now.
  • -MG file โ€” same as -M but it assumes that missing Makefile dependencies are generated and added to the dependency list without a prefix.
  • -MF file โ€” output Makefile-style dependencies to the specified file.
  • -MD file โ€” same as combining -M and -MF. it's the mashup.
  • -MT file โ€” override the default name of the dependency target dependency target name. (yes, the original page says "dependency target" twice, I'm not fixing it, that's how it is, we respect the text.) normally this is the same as the output filename you gave with -o.
  • -MQ file โ€” same as -MT except it tries to quote characters that have special meaning in Makefile syntax. ๐Ÿšจ not foolproof tho โ€” not all characters with special meaning are quotable in Make.
  • -MP โ€” emit phony target.
  • -O number โ€” optimize branch offsets. here are your levels:
    • -O0: No optimization
    • -O1: Minimal optimization
    • -Ox: Multipass optimization (default)
  • -o outfile โ€” give the output file a precise name, overriding nasm's whole default-determination situation. you're in charge now.
  • -P|-p file โ€” specifies a file to be pre-included, before the main source file starts to be processed. early bird gets the include.
  • -s โ€” sends error messages and/or help text to stdout instead of stderr. redirecting who? we go straight to stdout now.
  • -t โ€” assemble in SciTech TASM compatible mode. nostalgia core.
  • -U|-u macro โ€” undefines a single-line macro. un-define. un-slay. (jk, it's very useful.)
  • -v โ€” exits immediately after displaying its version number. just a quick hello. ๐ŸŒธ
  • -W[no-]foo โ€” enables or disables certain classes of warning messages, gcc-style. for example -Wlabel-orphan or -Wno-orphan-labels.
  • -w[+-]foo โ€” enables or disables certain classes of warning messages too, but with the other flavor of syntax. for example -w+label-orphan or -w-macro-params.
  • -X format โ€” specifies the error reporting format (gnu or vc).
  • -y โ€” lists supported debug formats. the lil menu. ๐Ÿ“‹
  • -Z filename โ€” redirects error messages to filename. this option exists to support operating systems on which stderr is not easily redirected. it's a workaround, and we love a workaround.
  • --prefix, --postfix โ€” prepend or append (respectively) the given argument to all global or extern variables. bookends for your symbols. ๐ŸŽ€

โœจ SYNTAX โœจ

okay so real talk: this man page does not fully describe the syntax of nasm's assembly language. what it does do is give you a summary of the differences from other assemblers. so if you're coming from somewhere else, this is your diff. ๐Ÿ‘€

Registers have no leading % sign, unlike gas, and floating-point stack registers are referred to as st0, st1, and so on. no percent, we're not doing that here.

Floating-point instructions may use either the single-operand form or the double. A TO keyword is provided; thus, one could either write

fadd st0,st1
fadd st1,st0

or one could use the alternative single-operand forms

fadd st1
fadd to st1

Uninitialised storage is reserved using the RESB, RESW, RESD, RESQ, REST and RESO pseudo-opcodes, each taking one parameter which gives the number of bytes, words, doublewords, quadwords or ten-byte words to reserve. one parameter, that's the whole job. ๐Ÿซถ

Repetition of data items is not done by the DUP keyword like in those DOS assemblers โ€” it's done with the TIMES prefix, like this:

message: times 3 db 'abc'
         times 64-$+message db 0

which defines the string abcabcabc, followed by the right number of zero bytes to make the total length up to 64 bytes. math but make it assembly. ๐Ÿงฎโœจ

Symbol references are always understood to be immediate (i.e. the address of the symbol), unless square brackets are used, in which case the contents of the memory location are used. so:

mov ax,wordvar

loads AX with the address of the variable wordvar, whereas

mov ax,[wordvar]
mov ax,[wordvar+1]
mov ax,[es:wordvar+bx]

all refer to the contents of memory locations. brackets = dereference, periodt. but the syntaxes

mov ax,es:wordvar[bx]
es mov ax,wordvar[1]

are not legal at all, bestie. not even a little. although the use of a segment register name as an instruction prefix is valid, and can be used with instructions such as LODSB which can't be overridden any other way. so there's your loophole, use it wisely. ๐Ÿ’…

Constants may be expressed numerically in most formats: a trailing H, Q or B denotes hex, octal or binary respectively, and a leading 0x or $ denotes hex as well. Leading zeros are not treated specially at all. Character constants may be enclosed in single or double quotes; there is no escape character. and here's the twist โ€” the ordering is little-endian (reversed), so that the character constant 'abcd' denotes 0x64636261 and not 0x61626364. yes it's backwards. yes that's intentional. ๐Ÿ˜ญ

Local labels begin with a period, and their 'locality' is granted by the assembler prepending the name of the previous non-local symbol. Thus declaring a label .loop after a label label has actually defined a symbol called label.loop. sneaky lil namespacing, iykyk. ๐Ÿฆ‹


โœจ DIRECTIVES โœจ

SECTION name or SEGMENT name causes nasm to direct all following code to the named section. Section names vary with output file format, although most formats support the names .text, .data and .bss. (The exception is the obj format, in which all segments are user-definable.) okay so obj is the wild child, noted.

ABSOLUTE address causes nasm to position its notional assembly point at an absolute address: so no code or data may be generated, but you can use RESB, RESW and RESD to move the assembly point further on, and you can define labels. So this directive may be used to define data structures. ๐Ÿ—๏ธ and when you have finished doing absolute assembly, you must issue another SECTION directive to return to normal assembly. don't forget, girl, you'll be stuck in absolute-land forever.

BITS 16, BITS 32 or BITS 64 switches the default processor mode for which nasm is generating code: it is equivalent to USE16 or USE32 in DOS assemblers. glow-up mode selector. ๐ŸŒˆ

EXTERN symbol and GLOBAL symbol import and export symbol definitions, respectively, from and to other modules. โš ๏ธ Note that the GLOBAL directive must appear before the definition of the symbol it refers to. order matters, no skipping.

STRUC strucname and ENDSTRUC, when used to bracket a number of RESB, RESW or similar instructions, define a data structure. In addition to defining the offsets of the structure members, the construct also defines a symbol for the size of the structure, which is simply the structure name with size tacked on to the end. free size symbol, we love a bonus. ๐ŸŽ


โœจ FORMAT-SPECIFIC DIRECTIVES โœจ

ORG address is used by the bin flat-form binary output format, and specifies the address at which the output code will eventually be loaded.

GROUP grpname seg1 seg2... is used by the obj (Microsoft 16-bit) output format, and defines segment groups. This format also uses UPPERCASE, which directs that all segment, group and symbol names output to the object file should be in uppercase. ๐Ÿšจ Note that the actual assembly is still case sensitive. so the output is shouting but the input still cares about your capital letters. tricky lil situation.

LIBRARY libname is used by the rdf output format, and causes a dependency record to be written to the output file which indicates that the program requires a certain library in order to run. ๐Ÿ“


part 1 done bestie, keep scrolling, there's more of this masterpiece ๐Ÿ’œ

๐Ÿงš MACRO PREPROCESSOR ๐Ÿ’–

Okay bestie, this is the part where NASM gets actually magical. โœจ

Single-line macros are defined using the %define or %idefine commands, and the vibe is very much like the C preprocessor. They can be overloaded with respect to number of parameters โ€” but heads up: defining a macro with no parameters blocks you from defining any macro with the same name that takes parameters, and vice versa. %define defines macros whose names match case-sensitively, whereas %idefine defines case-insensitive macros. ๐Ÿซถ

Multi-line macros are defined using %macro and %imacro (same distinction as between %define and %idefine), and the syntax goes like this:

%macro name minprm[-maxprm][+][.nolist] [defaults]
        <some lines of macro expansion text>
%endmacro

Again, these macros may be overloaded. The trailing plus sign means any parameters after the last one get subsumed โ€” with their separating commas โ€” into the last parameter. The defaults part can be used to specify defaults for unspecified macro parameters after minparam. And %endm is a totally valid synonym for %endmacro. ๐Ÿ’…


To refer to the macro parameters inside a macro expansion, you use %1, %2, and so on. You can also enforce that a macro parameter should contain a condition code by using %+1, and you can invert the condition code using %-1. Obsessed. ๐Ÿ˜Œ You can also define a label specific to a macro invocation by prefixing it with a double % sign.

Files can be included using the %include directive, which works like C. ๐ŸŒธ

The preprocessor has a โ€˜context stackโ€™, which may be used by one macro to store information that a later one will retrieve. The whole roster:

  • %push โ€” push a context on the stack
  • %pop โ€” remove one
  • %repl โ€” change the name of the top context (without disturbing any associated definitions)

Labels and %define macros specific to the top context may be defined by prefixing their names with %$, and things specific to the next context down with %$$, and so on. ๐ŸŒˆ

Conditional assembly is done by means of %ifdef, %ifndef, %else and %endif as in C. (Except that %ifdef can accept several putative macro names, and will evaluate TRUE if any of them is defined โ€” little overachiever. ๐Ÿ’€)

In addition, the directives %ifctx and %ifnctx can be used to condition on the name of the top context on the context stack. And the obvious set of โ€˜else-ifโ€™ directives โ€” %elifdef, %elifndef, %elifctx and %elifnctx โ€” is also supported. Periodt. ๐Ÿ‘‘


๐Ÿ› BUGS

Please report bugs through the bug tracker function at http://nasm.us. Not me begging you to file an issue โ€” okay yes, that's exactly what's happening. ๐Ÿ™ˆ


๐Ÿ‘€ SEE ALSO


And that's the whole page, bestie!! ๐ŸŽ€ This was NASM(1) straight from the NASM crew, dated 06/28/2026 โ€” go forth and assemble something iconic. ๐Ÿ’–โœจ