Next Chapter | Previous Chapter | Contents | Index
To assemble a file, you issue a command of the form
nasm -f <format> <filename> [-o <output>]
For example,
nasm -f elf myfile.asm
will assemble
into an
object file
. And
nasm -f bin myfile.asm -o myfile.com
will assemble
into a raw binary
file
.
To produce a listing file, with the hex codes output from NASM displayed
on the left of the original sources, use the
option to give a listing file name, for example:
nasm -f coff myfile.asm -l myfile.lst
To get further usage instructions from NASM, try typing
nasm -h
As
, this will also list the available
output file formats, and what they are.
If you use Linux but aren't sure whether your system is
or
, type
file nasm
(in the directory in which you put the NASM binary when you installed it). If it says something like
nasm: ELF 32-bit LSB executable i386 (386 and up) Version 1
then your system is
, and you should use
the option
when you want NASM to produce
Linux object files. If it says
nasm: Linux/i386 demand-paged executable (QMAGIC)
or something similar, your system is
,
and you should use
instead (Linux
systems have long been obsolete, and are
rare these days.)
Like Unix compilers and assemblers, NASM is silent unless it goes wrong: you won't see any output at all, unless it gives error messages.
-o
Option: Specifying the Output File NameNASM will normally choose the name of your output file for you;
precisely how it does this is dependent on the object file format. For
Microsoft object file formats (
,
and
), it
will remove the
extension (or whatever
extension you like to use - NASM doesn't care) from your source file name
and substitute
. For Unix object file formats
(
,
,
,
,
,
,
,
and
) it will substitute
. For
,
,
and
, it will use
,
,
and
, respectively, and for the
format it will simply remove the extension,
so that
produces the output file
.
If the output file already exists, NASM will overwrite it, unless it has
the same name as the input file, in which case it will give a warning and
use
as the output file name instead.
For situations in which this behaviour is unacceptable, NASM provides
the
command-line option, which allows you to
specify your desired output file name. You invoke
by following it with the name you wish for the
output file, either with or without an intervening space. For example:
nasm -f bin program.asm -o program.com nasm -f bin driver.asm -odriver.sys
Note that this is a small o, and is different from a capital O , which is used to specify the number of optimisation passes required. See section 2.1.22.
-f
Option: Specifying the Output File FormatIf you do not supply the
option to NASM, it
will choose an output file format for you itself. In the distribution
versions of NASM, the default is always
; if
you've compiled your own copy of NASM, you can redefine
at compile time and choose what you
want the default to be.
Like
, the intervening space between
and the output file format is optional; so
and
are
both valid.
A complete list of the available output file formats can be given by
issuing the command
.
-l
Option: Generating a Listing FileIf you supply the
option to NASM, followed
(with the usual optional space) by a file name, NASM will generate a
source-listing file for you, in which addresses and generated code are
listed on the left, and the actual source code, with expansions of
multi-line macros (except those which specifically request no expansion in
source listings: see section
4.3.11) on the right. For example:
nasm -f elf myfile.asm -l myfile.lst
If a list file is selected, you may turn off listing for a section of
your source with
, and turn it back on
with
, (the default, obviously). There is
no "user form" (without the brackets). This can be used to list only
sections of interest, avoiding excessively long listings.
-M
Option: Generate Makefile DependenciesThis option can be used to generate makefile dependencies on stdout. This can be redirected to a file for further processing. For example:
nasm -M myfile.asm > myfile.dep
-MG
Option: Generate Makefile DependenciesThis option can be used to generate makefile dependencies on stdout.
This differs from the
option in that if a
nonexisting file is encountered, it is assumed to be a generated file and
is added to the dependency list without a prefix.
-MF
Option: Set Makefile Dependency FileThis option can be used with the
or
options to send the output to a file, rather
than to stdout. For example:
nasm -M -MF myfile.dep myfile.asm
-MD
Option: Assemble and Generate DependenciesThe
option acts as the combination of the
and
options
(i.e. a filename has to be specified.) However, unlike the
or
options,
does not inhibit the normal
operation of the assembler. Use this to automatically generate updated
dependencies with every assembly session. For example:
nasm -f elf -o myfile.o -MD myfile.dep myfile.asm
-MT
Option: Dependency Target NameThe
option can be used to override the
default name of the dependency target. This is normally the same as the
output filename, specified by the
option.
-MQ
Option: Dependency Target Name (Quoted)The
option acts as the
option, except it tries to quote characters
that have special meaning in Makefile syntax. This is not foolproof, as not
all characters with special meaning are quotable in Make. The default
output (if no
or
option is specified) is automatically quoted.
-MP
Option: Emit phony targetsWhen used with any of the dependency generation options, the
option causes NASM to emit a phony target
without dependencies for each header file. This prevents Make from
complaining if a header file has been removed.
-F
Option: Selecting a Debug Information FormatThis option is used to select the format of the debug information
emitted into the output file, to be used by a debugger (or will
be). Prior to version 2.03.01, the use of this switch did not
enable output of the selected debug info format. Use
, see section
2.1.12, to enable output. Versions 2.03.01 and later automatically
enable
if
is
specified.
A complete list of the available debug file formats for an output format
can be seen by issuing the command
. Not all output formats
currently support debugging output. See section
2.1.26.
This should not be confused with the
output format option which is not built into NASM by default. For
information on how to enable it when building from the sources, see
section 7.14.
-g
Option: Enabling Debug Information.This option can be used to generate debugging information in the
specified format. See section 2.1.11. Using
without
results
in emitting debug info in the default format, if any, for the selected
output format. If no debug information is currently implemented in the
selected output format,
is silently
ignored.
-X
Option: Selecting an Error Reporting FormatThis option can be used to select an error reporting format for any error messages that might be produced by NASM.
Currently, two error reporting formats may be selected. They are the
option and the
option. The GNU format is the default and
looks like this:
filename.asm:65: error: specific error message
where
is the name of the source
file in which the error was detected,
is the
source file line number on which the error was detected,
is the severity of the error (this could be
), and
is a more detailed text
message which should help pinpoint the exact problem.
The other format, specified by
is the
style used by Microsoft Visual C++ and some other programs. It looks like
this:
filename.asm(65) : error: specific error message
where the only difference is that the line number is in parentheses instead of being delimited by colons.
See also the
output format,
section 7.5.
-Z
Option: Send Errors to a FileUnder
it can be difficult (though there
are ways) to redirect the standard-error output of a program to a file.
Since NASM usually produces its warning and error messages on
, this can make it hard to capture the
errors if (for example) you want to load them into an editor.
NASM therefore provides the
option, taking
a filename argument which causes errors to be sent to the specified files
rather than standard error. Therefore you can redirect the errors into a
file by typing
nasm -Z myfile.err -f obj myfile.asm
In earlier versions of NASM, this option was called
, but it was changed since
is an option conventionally used for
preprocessing only, with disastrous results. See
section 2.1.20.
-s
Option: Send Errors to stdout
The
option redirects error messages to
rather than
, so it can be redirected under
. To assemble the file
and pipe its output to the
program, you can type:
nasm -s -f obj myfile.asm | more
See also the
option,
section 2.1.14.
-i
Option: Include File Search DirectoriesWhen NASM sees the
or
directive in a source file (see
section 4.6.1,
section 4.6.2 or
section 3.2.3), it will search
for the given file not only in the current directory, but also in any
directories specified on the command line by the use of the
option. Therefore you can include files from a
macro library, for example, by typing
nasm -ic:\macrolib\ -f obj myfile.asm
(As usual, a space between
and the path
name is allowed, and optional).
NASM, in the interests of complete source-code portability, does not
understand the file naming conventions of the OS it is running on; the
string you provide as an argument to the
option will be prepended exactly as written to the name of the include
file. Therefore the trailing backslash in the above example is necessary.
Under Unix, a trailing forward slash is similarly necessary.
(You can use this to your advantage, if you're really perverse, by
noting that the option
will cause
to search for the file
...)
If you want to define a standard include search path, similar
to
on Unix systems, you should place
one or more
directives in the
environment variable (see
section 2.1.28).
For Makefile compatibility with many C compilers, this option can also
be specified as
.
-p
Option: Pre-Include a FileNASM allows you to specify files to be pre-included into your
source file, by the use of the
option. So
running
nasm myfile.asm -p myinc.inc
is equivalent to running
and
placing the directive
at the
start of the file.
For consistency with the
,
and
options, this
option can also be specified as
.
-d
Option: Pre-Define a MacroJust as the
option gives an alternative to
placing
directives at the start of a
source file, the
option gives an alternative
to placing a
directive. You could code
nasm myfile.asm -dFOO=100
as an alternative to placing the directive
%define FOO 100
at the start of the file. You can miss off the macro value, as well: the
option
is equivalent to coding
. This form of the directive may be
useful for selecting assembly-time options which are then tested using
, for example
.
For Makefile compatibility with many C compilers, this option can also
be specified as
.
-u
Option: Undefine a MacroThe
option undefines a macro that would
otherwise have been pre-defined, either automatically or by a
or
option
specified earlier on the command lines.
For example, the following command line:
nasm myfile.asm -dFOO=100 -uFOO
would result in
not being a
predefined macro in the program. This is useful to override options
specified at a different point in a Makefile.
For Makefile compatibility with many C compilers, this option can also
be specified as
.
-E
Option: Preprocess OnlyNASM allows the preprocessor to be run on its own, up to a point. Using
the
option (which requires no arguments) will
cause NASM to preprocess its input file, expand all the macro references,
remove all the comments and preprocessor directives, and print the
resulting file on standard output (or save it to a file, if the
option is also used).
This option cannot be applied to programs which require the preprocessor to evaluate expressions which depend on the values of symbols: so code such as
%assign tablesize ($-tablestart)
will cause an error in preprocess-only mode.
For compatiblity with older version of NASM, this option can also be
written
.
in older
versions of NASM was the equivalent of the current
option, section
2.1.14.
-a
Option: Don't Preprocess At AllIf NASM is being used as the back end to a compiler, it might be
desirable to suppress preprocessing completely and assume the compiler has
already done it, to save time and increase compilation speeds. The
option, requiring no argument, instructs NASM
to replace its powerful preprocessor with a stub preprocessor which does
nothing.
-O
Option: Specifying Multipass OptimizationUsing the
option, you can tell NASM to
carry out different levels of optimization. The syntax is:
-O0
: No optimization. All operands take their
long forms, if a short form is not specified, except conditional jumps.
This is intended to match NASM 0.98 behavior.
-O1
: Minimal optimization. As above, but
immediate operands which will fit in a signed byte are optimized, unless
the long form is specified. Conditional jumps default to the long form
unless otherwise specified.
-Ox
(where x
is the
actual letter x
): Multipass optimization.
Minimize branch offsets and signed immediate bytes, overriding size
specification unless the strict
keyword has been
used (see section 3.7). For
compatibility with earlier releases, the letter x
may also be any number greater than one. This number has no effect on the
actual number of passes.
The
mode is recommended for most uses, and
is the default since NASM 2.09.
Note that this is a capital
, and is
different from a small
, which is used to
specify the output file name. See section
2.1.1.
-t
Option: Enable TASM Compatibility ModeNASM includes a limited form of compatibility with Borland's
. When NASM's
option is used, the following changes are made:
@@
instead
of .
mov eax,[DWORD val]
is valid syntax in TASM
compatibility mode. Note that you lose the ability to override the default
address type for the instruction.
arg
, elif
,
else
, endif
,
if
, ifdef
,
ifdifi
, ifndef
,
include
, local
)
-w
and -W
Options: Enable or Disable Assembly WarningsNASM can observe many conditions during the course of assembly which are worth mentioning to the user, but not a sufficiently severe error to justify NASM refusing to generate an output file. These conditions are reported like errors, but come up with the word `warning' before the message. Warnings do not prevent NASM from generating an output file and returning a success status to the operating system.
Some conditions are even less severe than that: they are only sometimes
worth mentioning to the user. Therefore NASM supports the
command-line option, which enables or disables
certain classes of assembly warning. Such warning classes are described by
a name, for example
; you can enable
warnings of this class by the command-line option
and disable it by
.
The suppressible warning classes are:
macro-params
covers warnings about multi-line
macros being invoked with the wrong number of parameters. This warning
class is enabled by default; see
section 4.3.1 for an example of
why you might want to disable it.
macro-selfref
warns if a macro references
itself. This warning class is disabled by default.
macro-defaults
warns when a macro has more
default parameters than optional parameters. This warning class is enabled
by default; see section 4.3.5 for
why you might want to disable it.
orphan-labels
covers warnings about source
lines which contain no instruction but define a label without a trailing
colon. NASM warns about this somewhat obscure condition by default; see
section 3.1 for more information.
number-overflow
covers warnings about numeric
constants which don't fit in 64 bits. This warning class is enabled by
default.
gnu-elf-extensions
warns if 8-bit or 16-bit
relocations are used in -f elf
format. The GNU
extensions allow this. This warning class is disabled by default.
float-overflow
warns about floating point
overflow. Enabled by default.
float-denorm
warns about floating point
denormals. Disabled by default.
float-underflow
warns about floating point
underflow. Disabled by default.
float-toolong
warns about too many digits in
floating-point numbers. Enabled by default.
user
controls
%warning
directives (see
section 4.9). Enabled by default.
lock
warns about
LOCK
prefixes on unlockable instructions. Enabled
by default.
hle
warns about invalid use of the HLE
XACQUIRE
or XRELEASE
prefixes. Enabled by default.
bnd
warns about ineffective use of the
BND
prefix when a relaxed form of jmp instruction
becomes jmp short form. Enabled by default.
error
causes warnings to be treated as
errors. Disabled by default.
all
is an alias for all suppressible
warning classes (not including error
). Thus,
-w+all
enables all available warnings.
In addition, you can set warning classes across sections. Warning
classes may be enabled with
, disabled with
or reset to their
original value with
. No
"user form" (without the brackets) exists.
Since version 2.00, NASM has also supported the gcc-like syntax
and
instead of
and
, respectively.
-v
Option: Display Version InfoTyping
will display the version of
NASM which you are using, and the date on which it was compiled.
You will need the version number if you report a bug.
For command-line compatibility with Yasm, the form
is also accepted for this option.
-y
Option: Display Available Debug Info FormatsTyping
will display
a list of the available debug info formats for the given output format. The
default format is indicated by an asterisk. For example:
nasm -f elf -y
valid debug formats for 'elf32' output format are ('*' denotes default): * stabs ELF32 (i386) stabs debug format for Linux dwarf elf32 (i386) dwarf debug format for Linux
--prefix
and --postfix
Options.The
and
options prepend or append
(respectively) the given argument to all
or
variables. E.g.
will prepend the underscore to all
global and external variables, as C sometimes (but not always) likes it.
NASMENV
Environment VariableIf you define an environment variable called
, the program will interpret it as a list
of extra command-line options, which are processed before the real command
line. You can use this to define standard search directories for include
files, by putting
options in the
variable.
The value of the variable is split up at white space, so that the value
will be treated as two separate
options. However, that means that the value
won't do what you might want,
because it will be split at the space and the NASM command-line processing
will get confused by the two nonsensical words
and
.
To get round this, NASM provides a feature whereby, if you begin the
environment variable with some character
that isn't a minus sign, then NASM will treat this character as the
separator character for options. So setting the
variable to the value
is equivalent to setting it to
, but
will work.
This environment variable was previously called
. This was changed with version 0.98.31.
If you're used to writing programs with MASM, or with TASM in
MASM-compatible (non-Ideal) mode, or with
,
this section attempts to outline the major differences between MASM's
syntax and NASM's. If you're not already used to MASM, it's probably worth
skipping this section.
One simple difference is that NASM is case-sensitive. It makes a
difference whether you call your label
,
or
. If you're
assembling to
or
files, you
can invoke the
directive (documented in
section 7.4) to ensure that all
symbols exported to other code modules are forced to be upper case; but
even then, within a single module, NASM will distinguish between
labels differing only in case.
NASM was designed with simplicity of syntax in mind. One of the design goals of NASM is that it should be possible, as far as is practical, for the user to look at a single line of NASM code and tell what opcode is generated by it. You can't do this in MASM: if you declare, for example,
foo equ 1 bar dw 2
then the two lines of code
mov ax,foo mov ax,bar
generate completely different opcodes, despite having identical-looking syntaxes.
NASM avoids this undesirable situation by having a much simpler syntax
for memory references. The rule is simply that any access to the
contents of a memory location requires square brackets around the
address, and any access to the address of a variable doesn't. So
an instruction of the form
will
always refer to a compile-time constant, whether it's an
or the address of a variable; and to access
the contents of the variable
, you
must code
.
This also means that NASM has no need for MASM's
keyword, since the MASM code
means exactly the same thing as
NASM's
. If you're trying to get large
amounts of MASM code to assemble sensibly under NASM, you can always code
to make the preprocessor treat
the
keyword as a no-op.
This issue is even more confusing in
,
where declaring a label with a trailing colon defines it to be a `label' as
opposed to a `variable' and causes
to adopt
NASM-style semantics; so in
,
has different behaviour depending on
whether
was declared as
(a label) or
(a word-size variable). NASM is very
simple by comparison: everything is a label.
NASM, in the interests of simplicity, also does not support the hybrid
syntaxes supported by MASM and its clones, such as
, where a memory reference is
denoted by one portion outside square brackets and another portion inside.
The correct syntax for the above is
. Likewise,
is wrong and
is right.
NASM, by design, chooses not to remember the types of variables you
declare. Whereas MASM will remember, on seeing
, that you declared
as a word-size variable, and will then be
able to fill in the ambiguity in the size of the instruction
, NASM will deliberately remember
nothing about the symbol
except where it
begins, and so you must explicitly code
.
For this reason, NASM doesn't support the
,
,
,
,
,
, or
instructions, but only supports the forms
such as
,
,
and
, which explicitly specify the size of
the components of the strings being manipulated.
ASSUME
As part of NASM's drive for simplicity, it also does not support the
directive. NASM will not keep track of
what values you choose to put in your segment registers, and will never
automatically generate a segment override prefix.
NASM also does not have any directives to support different 16-bit
memory models. The programmer has to keep track of which functions are
supposed to be called with a far call and which with a near call, and is
responsible for putting the correct form of
instruction (
or
; NASM accepts
itself as an alternate form for
); in
addition, the programmer is responsible for coding CALL FAR instructions
where necessary when calling external functions, and must also
keep track of which external variable definitions are far and which are
near.
NASM uses different names to refer to floating-point registers from
MASM: where MASM would call them
,
and so on, and
would call them simply
,
and so on, NASM
chooses to call them
,
etc.
As of version 0.96, NASM now treats the instructions with `nowait' forms in the same way as MASM-compatible assemblers. The idiosyncratic treatment employed by 0.95 and earlier was based on a misunderstanding by the authors.
For historical reasons, NASM uses the keyword
where MASM and compatible assemblers use
.
NASM does not declare uninitialized storage in the same way as MASM:
where a MASM programmer might use
, NASM requires
, intended to be read as `reserve 64
bytes'. For a limited amount of compatibility, since NASM treats
as a valid character in symbol names, you can
code
and then writing
will at least do something vaguely useful.
is still not a supported syntax, however.
In addition to all of this, macros and directives work completely differently to MASM. See chapter 4 and chapter 6 for further details.