The Netwide Assembler: NASM =========================== Chapter 1: Introduction ----------------------- 1.1 What Is NASM? The Netwide Assembler, NASM, is an 80x86 and x86-64 assembler designed for portability and modularity. It supports a range of object file formats, including Linux and `*BSD' `a.out', `ELF', `COFF', `Mach-O', Microsoft 16-bit `OBJ', `Win32' and `Win64'. It will also output plain binary files. Its syntax is designed to be simple and easy to understand, similar to Intel's but less complex. It supports all currently known x86 architectural extensions, and has strong support for macros. 1.1.1 Why Yet Another Assembler? The Netwide Assembler grew out of an idea on `comp.lang.asm.x86' (or possibly `alt.lang.asm' - I forget which), which was essentially that there didn't seem to be a good _free_ x86-series assembler around, and that maybe someone ought to write one. (*) `a86' is good, but not free, and in particular you don't get any 32-bit capability until you pay. It's DOS only, too. (*) `gas' is free, and ports over to DOS and Unix, but it's not very good, since it's designed to be a back end to `gcc', which always feeds it correct code. So its error checking is minimal. Also, its syntax is horrible, from the point of view of anyone trying to actually _write_ anything in it. Plus you can't write 16-bit code in it (properly.) (*) `as86' is specific to Minix and Linux, and (my version at least) doesn't seem to have much (or any) documentation. (*) `MASM' isn't very good, and it's (was) expensive, and it runs only under DOS. (*) `TASM' is better, but still strives for MASM compatibility, which means millions of directives and tons of red tape. And its syntax is essentially MASM's, with the contradictions and quirks that entails (although it sorts out some of those by means of Ideal mode.) It's expensive too. And it's DOS-only. So here, for your coding pleasure, is NASM. At present it's still in prototype stage - we don't promise that it can outperform any of these assemblers. But please, _please_ send us bug reports, fixes, helpful information, and anything else you can get your hands on (and thanks to the many people who've done this already! You all know who you are), and we'll improve it out of all recognition. Again. 1.1.2 License Conditions Please see the file `LICENSE', supplied as part of any NASM distribution archive, for the license conditions under which you may use NASM. NASM is now under the so-called 2-clause BSD license, also known as the simplified BSD license. Copyright 1996-2011 the NASM Authors - All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: (*) Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. (*) Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 1.2 Contact Information The current version of NASM (since about 0.98.08) is maintained by a team of developers, accessible through the `nasm-devel' mailing list (see below for the link). If you want to report a bug, please read section 12.2 first. NASM has a website at `http://www.nasm.us/'. If it's not there, google for us! New releases, release candidates, and daily development snapshots of NASM are available from the official web site. Announcements are posted to `comp.lang.asm.x86', and to the web site `http://www.freshmeat.net/'. If you want information about the current development status, please subscribe to the `nasm-devel' email list; see link from the website. 1.3 Installation 1.3.1 Installing NASM under MS-DOS or Windows Once you've obtained the appropriate archive for NASM, `nasm-XXX-dos.zip' or `nasm-XXX-win32.zip' (where `XXX' denotes the version number of NASM contained in the archive), unpack it into its own directory (for example `c:\nasm'). The archive will contain a set of executable files: the NASM executable file `nasm.exe', the NDISASM executable file `ndisasm.exe', and possibly additional utilities to handle the RDOFF file format. The only file NASM needs to run is its own executable, so copy `nasm.exe' to a directory on your PATH, or alternatively edit `autoexec.bat' to add the `nasm' directory to your `PATH' (to do that under Windows XP, go to Start > Control Panel > System > Advanced > Environment Variables; these instructions may work under other versions of Windows as well.) That's it - NASM is installed. You don't need the nasm directory to be present to run NASM (unless you've added it to your `PATH'), so you can delete it if you need to save space; however, you may want to keep the documentation or test programs. If you've downloaded the DOS source archive, `nasm-XXX.zip', the `nasm' directory will also contain the full NASM source code, and a selection of Makefiles you can (hopefully) use to rebuild your copy of NASM from scratch. See the file `INSTALL' in the source archive. Note that a number of files are generated from other files by Perl scripts. Although the NASM source distribution includes these generated files, you will need to rebuild them (and hence, will need a Perl interpreter) if you change insns.dat, standard.mac or the documentation. It is possible future source distributions may not include these files at all. Ports of Perl for a variety of platforms, including DOS and Windows, are available from www.cpan.org. 1.3.2 Installing NASM under Unix Once you've obtained the Unix source archive for NASM, `nasm-XXX.tar.gz' (where `XXX' denotes the version number of NASM contained in the archive), unpack it into a directory such as `/usr/local/src'. The archive, when unpacked, will create its own subdirectory `nasm-XXX'. NASM is an auto-configuring package: once you've unpacked it, `cd' to the directory it's been unpacked into and type `./configure'. This shell script will find the best C compiler to use for building NASM and set up Makefiles accordingly. Once NASM has auto-configured, you can type `make' to build the `nasm' and `ndisasm' binaries, and then `make install' to install them in `/usr/local/bin' and install the man pages `nasm.1' and `ndisasm.1' in `/usr/local/man/man1'. Alternatively, you can give options such as `--prefix' to the configure script (see the file `INSTALL' for more details), or install the programs yourself. NASM also comes with a set of utilities for handling the `RDOFF' custom object-file format, which are in the `rdoff' subdirectory of the NASM archive. You can build these with `make rdf' and install them with `make rdf_install', if you want them. Chapter 2: Running NASM ----------------------- 2.1 NASM Command-Line Syntax To assemble a file, you issue a command of the form nasm -f [-o ] For example, nasm -f elf myfile.asm will assemble `myfile.asm' into an `ELF' object file `myfile.o'. And nasm -f bin myfile.asm -o myfile.com will assemble `myfile.asm' into a raw binary file `myfile.com'. To produce a listing file, with the hex codes output from NASM displayed on the left of the original sources, use the `-l' 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 `-hf', 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 `a.out' or `ELF', 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 `ELF', and you should use the option `-f elf' 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 `a.out', and you should use `-f aout' instead (Linux `a.out' 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. 2.1.1 The `-o' Option: Specifying the Output File Name NASM 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 (`obj', `win32' and `win64'), it will remove the `.asm' extension (or whatever extension you like to use - NASM doesn't care) from your source file name and substitute `.obj'. For Unix object file formats (`aout', `as86', `coff', `elf32', `elf64', `elfx32', `ieee', `macho32' and `macho64') it will substitute `.o'. For `dbg', `rdf', `ith' and `srec', it will use `.dbg', `.rdf', `.ith' and `.srec', respectively, and for the `bin' format it will simply remove the extension, so that `myfile.asm' produces the output file `myfile'. 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 `nasm.out' as the output file name instead. For situations in which this behaviour is unacceptable, NASM provides the `-o' command-line option, which allows you to specify your desired output file name. You invoke `-o' 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. 2.1.2 The `-f' Option: Specifying the Output File Format If you do not supply the `-f' option to NASM, it will choose an output file format for you itself. In the distribution versions of NASM, the default is always `bin'; if you've compiled your own copy of NASM, you can redefine `OF_DEFAULT' at compile time and choose what you want the default to be. Like `-o', the intervening space between `-f' and the output file format is optional; so `-f elf' and `-felf' are both valid. A complete list of the available output file formats can be given by issuing the command `nasm -hf'. 2.1.3 The `-l' Option: Generating a Listing File If you supply the `-l' 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 `[list -]', and turn it back on with `[list +]', (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. 2.1.4 The `-M' Option: Generate Makefile Dependencies This 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 2.1.5 The `-MG' Option: Generate Makefile Dependencies This option can be used to generate makefile dependencies on stdout. This differs from the `-M' 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. 2.1.6 The `-MF' Option: Set Makefile Dependency File This option can be used with the `-M' or `-MG' options to send the output to a file, rather than to stdout. For example: nasm -M -MF myfile.dep myfile.asm 2.1.7 The `-MD' Option: Assemble and Generate Dependencies The `-MD' option acts as the combination of the `-M' and `-MF' options (i.e. a filename has to be specified.) However, unlike the `-M' or `-MG' options, `-MD' 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 2.1.8 The `-MT' Option: Dependency Target Name The `-MT' 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 `-o' option. 2.1.9 The `-MQ' Option: Dependency Target Name (Quoted) The `-MQ' option acts as the `-MT' 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. 2.1.10 The `-MP' Option: Emit phony targets When used with any of the dependency generation options, the `-MP' 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. 2.1.11 The `-F' Option: Selecting a Debug Information Format This 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 `-g', see section 2.1.12, to enable output. Versions 2.03.01 and later automatically enable `-g' if `-F' is specified. A complete list of the available debug file formats for an output format can be seen by issuing the command `nasm -f -y'. Not all output formats currently support debugging output. See section 2.1.26. This should not be confused with the `-f dbg' 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. 2.1.12 The `-g' Option: Enabling Debug Information. This option can be used to generate debugging information in the specified format. See section 2.1.11. Using `-g' without `-F' 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, `-g' is _silently ignored_. 2.1.13 The `-X' Option: Selecting an Error Reporting Format This 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 `-Xvc' option and the `-Xgnu' option. The GNU format is the default and looks like this: filename.asm:65: error: specific error message where `filename.asm' is the name of the source file in which the error was detected, `65' is the source file line number on which the error was detected, `error' is the severity of the error (this could be `warning'), and `specific error message' is a more detailed text message which should help pinpoint the exact problem. The other format, specified by `-Xvc' 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 `Visual C++' output format, section 7.5. 2.1.14 The `-Z' Option: Send Errors to a File Under `MS-DOS' 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 `stderr', this can make it hard to capture the errors if (for example) you want to load them into an editor. NASM therefore provides the `-Z' 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 `-E', but it was changed since `-E' is an option conventionally used for preprocessing only, with disastrous results. See section 2.1.20. 2.1.15 The `-s' Option: Send Errors to `stdout' The `-s' option redirects error messages to `stdout' rather than `stderr', so it can be redirected under `MS-DOS'. To assemble the file `myfile.asm' and pipe its output to the `more' program, you can type: nasm -s -f obj myfile.asm | more See also the `-Z' option, section 2.1.14. 2.1.16 The `-i' Option: Include File Search Directories When NASM sees the `%include' or `%pathsearch' 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 `-i' 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 `-i' 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 `-i' 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 `-ifoo' will cause `%include "bar.i"' to search for the file `foobar.i'...) If you want to define a _standard_ include search path, similar to `/usr/include' on Unix systems, you should place one or more `-i' directives in the `NASMENV' environment variable (see section 2.1.28). For Makefile compatibility with many C compilers, this option can also be specified as `-I'. 2.1.17 The `-p' Option: Pre-Include a File NASM allows you to specify files to be _pre-included_ into your source file, by the use of the `-p' option. So running nasm myfile.asm -p myinc.inc is equivalent to running `nasm myfile.asm' and placing the directive `%include "myinc.inc"' at the start of the file. For consistency with the `-I', `-D' and `-U' options, this option can also be specified as `-P'. 2.1.18 The `-d' Option: Pre-Define a Macro Just as the `-p' option gives an alternative to placing `%include' directives at the start of a source file, the `-d' option gives an alternative to placing a `%define' 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 `-dFOO' is equivalent to coding `%define FOO'. This form of the directive may be useful for selecting assembly-time options which are then tested using `%ifdef', for example `-dDEBUG'. For Makefile compatibility with many C compilers, this option can also be specified as `-D'. 2.1.19 The `-u' Option: Undefine a Macro The `-u' option undefines a macro that would otherwise have been pre-defined, either automatically or by a `-p' or `-d' option specified earlier on the command lines. For example, the following command line: nasm myfile.asm -dFOO=100 -uFOO would result in `FOO' _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 `-U'. 2.1.20 The `-E' Option: Preprocess Only NASM allows the preprocessor to be run on its own, up to a point. Using the `-E' 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 `-o' 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 `-e'. `-E' in older versions of NASM was the equivalent of the current `-Z' option, section 2.1.14. 2.1.21 The `-a' Option: Don't Preprocess At All If 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 `-a' option, requiring no argument, instructs NASM to replace its powerful preprocessor with a stub preprocessor which does nothing. 2.1.22 The `-O' Option: Specifying Multipass Optimization Using the `-O' 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 `-Ox' mode is recommended for most uses, and is the default since NASM 2.09. Note that this is a capital `O', and is different from a small `o', which is used to specify the output file name. See section 2.1.1. 2.1.23 The `-t' Option: Enable TASM Compatibility Mode NASM includes a limited form of compatibility with Borland's `TASM'. When NASM's `-t' option is used, the following changes are made: (*) local labels may be prefixed with `@@' instead of `.' (*) size override is supported within brackets. In TASM compatible mode, a size override inside square brackets changes the size of the operand, and not the address type of the operand as it does in NASM syntax. E.g. `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. (*) unprefixed forms of some directives supported (`arg', `elif', `else', `endif', `if', `ifdef', `ifdifi', `ifndef', `include', `local') 2.1.24 The `-w' and `-W' Options: Enable or Disable Assembly Warnings NASM 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 `-w' command-line option, which enables or disables certain classes of assembly warning. Such warning classes are described by a name, for example `orphan-labels'; you can enable warnings of this class by the command-line option `-w+orphan-labels' and disable it by `-w-orphan-labels'. 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. (*) `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 `[warning +warning-name]', disabled with `[warning -warning-name]' or reset to their original value with `[warning *warning-name]'. No "user form" (without the brackets) exists. Since version 2.00, NASM has also supported the gcc-like syntax `-Wwarning' and `-Wno-warning' instead of `-w+warning' and `-w-warning', respectively. 2.1.25 The `-v' Option: Display Version Info Typing `NASM -v' 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. 2.1.26 The `-y' Option: Display Available Debug Info Formats Typing `nasm -f