The Netwide Assembler: NASM

This manual documents NASM, the Netwide Assembler: an assembler targetting the Intel x86 series of processors, with portable source.

Chapter 1: Introduction
Section 1.1: What Is NASM?
Section 1.1.1: Why Yet Another Assembler?
Section 1.1.2: License Conditions
Section 1.2: Contact Information
Section 1.3: Installation
Section 1.3.1: Installing NASM under MS-DOS or Windows
Section 1.3.2: Installing NASM under Unix

Chapter 2: Running NASM
Section 2.1: NASM Command-Line Syntax
Section 2.1.1: The -o Option: Specifying the Output File Name
Section 2.1.2: The -f Option: Specifying the Output File Format
Section 2.1.3: The -l Option: Generating a Listing File
Section 2.1.4: The -M Option: Generate Makefile Dependencies
Section 2.1.5: The -MG Option: Generate Makefile Dependencies
Section 2.1.6: The -MF Option: Set Makefile Dependency File
Section 2.1.7: The -MD Option: Assemble and Generate Dependencies
Section 2.1.8: The -MT Option: Dependency Target Name
Section 2.1.9: The -MQ Option: Dependency Target Name (Quoted)
Section 2.1.10: The -MP Option: Emit phony targets
Section 2.1.11: The -F Option: Selecting a Debug Information Format
Section 2.1.12: The -g Option: Enabling Debug Information.
Section 2.1.13: The -X Option: Selecting an Error Reporting Format
Section 2.1.14: The -Z Option: Send Errors to a File
Section 2.1.15: The -s Option: Send Errors to stdout
Section 2.1.16: The -i Option: Include File Search Directories
Section 2.1.17: The -p Option: Pre-Include a File
Section 2.1.18: The -d Option: Pre-Define a Macro
Section 2.1.19: The -u Option: Undefine a Macro
Section 2.1.20: The -E Option: Preprocess Only
Section 2.1.21: The -a Option: Don't Preprocess At All
Section 2.1.22: The -O Option: Specifying Multipass Optimization
Section 2.1.23: The -t Option: Enable TASM Compatibility Mode
Section 2.1.24: The -w and -W Options: Enable or Disable Assembly Warnings
Section 2.1.25: The -v Option: Display Version Info
Section 2.1.26: The -y Option: Display Available Debug Info Formats
Section 2.1.27: The --prefix and --postfix Options.
Section 2.1.28: The NASMENV Environment Variable
Section 2.2: Quick Start for MASM Users
Section 2.2.1: NASM Is Case-Sensitive
Section 2.2.2: NASM Requires Square Brackets For Memory References
Section 2.2.3: NASM Doesn't Store Variable Types
Section 2.2.4: NASM Doesn't ASSUME
Section 2.2.5: NASM Doesn't Support Memory Models
Section 2.2.6: Floating-Point Differences
Section 2.2.7: Other Differences

Chapter 3: The NASM Language
Section 3.1: Layout of a NASM Source Line
Section 3.2: Pseudo-Instructions
Section 3.2.1: DB and Friends: Declaring Initialized Data
Section 3.2.2: RESB and Friends: Declaring Uninitialized Data
Section 3.2.3: INCBIN: Including External Binary Files
Section 3.2.4: EQU: Defining Constants
Section 3.2.5: TIMES: Repeating Instructions or Data
Section 3.3: Effective Addresses
Section 3.4: Constants
Section 3.4.1: Numeric Constants
Section 3.4.2: Character Strings
Section 3.4.3: Character Constants
Section 3.4.4: String Constants
Section 3.4.5: Unicode Strings
Section 3.4.6: Floating-Point Constants
Section 3.4.7: Packed BCD Constants
Section 3.5: Expressions
Section 3.5.1: |: Bitwise OR Operator
Section 3.5.2: ^: Bitwise XOR Operator
Section 3.5.3: &: Bitwise AND Operator
Section 3.5.4: << and >>: Bit Shift Operators
Section 3.5.5: + and -: Addition and Subtraction Operators
Section 3.5.6: *, /, //, % and %%: Multiplication and Division
Section 3.5.7: Unary Operators
Section 3.6: SEG and WRT
Section 3.7: STRICT: Inhibiting Optimization
Section 3.8: Critical Expressions
Section 3.9: Local Labels

Chapter 4: The NASM Preprocessor
Section 4.1: Single-Line Macros
Section 4.1.1: The Normal Way: %define
Section 4.1.2: Resolving %define: %xdefine
Section 4.1.3: Macro Indirection: %[...]
Section 4.1.4: Concatenating Single Line Macro Tokens: %+
Section 4.1.5: The Macro Name Itself: %? and %??
Section 4.1.6: Undefining Single-Line Macros: %undef
Section 4.1.7: Preprocessor Variables: %assign
Section 4.1.8: Defining Strings: %defstr
Section 4.1.9: Defining Tokens: %deftok
Section 4.2: String Manipulation in Macros
Section 4.2.1: Concatenating Strings: %strcat
Section 4.2.2: String Length: %strlen
Section 4.2.3: Extracting Substrings: %substr
Section 4.3: Multi-Line Macros: %macro
Section 4.3.1: Overloading Multi-Line Macros
Section 4.3.2: Macro-Local Labels
Section 4.3.3: Greedy Macro Parameters
Section 4.3.4: Macro Parameters Range
Section 4.3.5: Default Macro Parameters
Section 4.3.6: %0: Macro Parameter Counter
Section 4.3.7: %00: Label Preceeding Macro
Section 4.3.8: %rotate: Rotating Macro Parameters
Section 4.3.9: Concatenating Macro Parameters
Section 4.3.10: Condition Codes as Macro Parameters
Section 4.3.11: Disabling Listing Expansion
Section 4.3.12: Undefining Multi-Line Macros: %unmacro
Section 4.4: Conditional Assembly
Section 4.4.1: %ifdef: Testing Single-Line Macro Existence
Section 4.4.2: %ifmacro: Testing Multi-Line Macro Existence
Section 4.4.3: %ifctx: Testing the Context Stack
Section 4.4.4: %if: Testing Arbitrary Numeric Expressions
Section 4.4.5: %ifidn and %ifidni: Testing Exact Text Identity
Section 4.4.6: %ifid, %ifnum, %ifstr: Testing Token Types
Section 4.4.7: %iftoken: Test for a Single Token
Section 4.4.8: %ifempty: Test for Empty Expansion
Section 4.4.9: %ifenv: Test If Environment Variable Exists
Section 4.5: Preprocessor Loops: %rep
Section 4.6: Source Files and Dependencies
Section 4.6.1: %include: Including Other Files
Section 4.6.2: %pathsearch: Search the Include Path
Section 4.6.3: %depend: Add Dependent Files
Section 4.6.4: %use: Include Standard Macro Package
Section 4.7: The Context Stack
Section 4.7.1: %push and %pop: Creating and Removing Contexts
Section 4.7.2: Context-Local Labels
Section 4.7.3: Context-Local Single-Line Macros
Section 4.7.4: Context Fall-Through Lookup
Section 4.7.5: %repl: Renaming a Context
Section 4.7.6: Example Use of the Context Stack: Block IFs
Section 4.8: Stack Relative Preprocessor Directives
Section 4.8.1: %arg Directive
Section 4.8.2: %stacksize Directive
Section 4.8.3: %local Directive
Section 4.9: Reporting User-Defined Errors: %error, %warning, %fatal
Section 4.10: Other Preprocessor Directives
Section 4.10.1: %line Directive
Section 4.10.2: %!<env>: Read an environment variable.
Section 4.11: Comment Blocks: %comment
Section 4.12: Standard Macros
Section 4.12.1: NASM Version Macros
Section 4.12.2: __NASM_VERSION_ID__: NASM Version ID
Section 4.12.3: __NASM_VER__: NASM Version string
Section 4.12.4: __FILE__ and __LINE__: File Name and Line Number
Section 4.12.5: __BITS__: Current BITS Mode
Section 4.12.6: __OUTPUT_FORMAT__: Current Output Format
Section 4.12.7: Assembly Date and Time Macros
Section 4.12.8: __USE_package__: Package Include Test
Section 4.12.9: __PASS__: Assembly Pass
Section 4.12.10: STRUC and ENDSTRUC: Declaring Structure Data Types
Section 4.12.11: ISTRUC, AT and IEND: Declaring Instances of Structures
Section 4.12.12: ALIGN and ALIGNB: Data Alignment
Section 4.12.13: SECTALIGN: Section Alignment

Chapter 5: Standard Macro Packages
Section 5.1: altreg: Alternate Register Names
Section 5.2: smartalign: Smart ALIGN Macro
Section 5.3: fp: Floating-point macros
Section 5.4: ifunc: Integer functions
Section 5.4.1: Integer logarithms

Chapter 6: Assembler Directives
Section 6.1: BITS: Specifying Target Processor Mode
Section 6.1.1: USE16 & USE32: Aliases for BITS
Section 6.2: DEFAULT: Change the assembler defaults
Section 6.2.1: REL & ABS: RIP-relative addressing
Section 6.2.2: BND & NOBND: BND prefix
Section 6.3: SECTION or SEGMENT: Changing and Defining Sections
Section 6.3.1: The __SECT__ Macro
Section 6.4: ABSOLUTE: Defining Absolute Labels
Section 6.5: EXTERN: Importing Symbols from Other Modules
Section 6.6: GLOBAL: Exporting Symbols to Other Modules
Section 6.7: COMMON: Defining Common Data Areas
Section 6.8: CPU: Defining CPU Dependencies
Section 6.9: FLOAT: Handling of floating-point constants

Chapter 7: Output Formats
Section 7.1: bin: Flat-Form Binary Output
Section 7.1.1: ORG: Binary File Program Origin
Section 7.1.2: bin Extensions to the SECTION Directive
Section 7.1.3: Multisection Support for the bin Format
Section 7.1.4: Map Files
Section 7.2: ith: Intel Hex Output
Section 7.3: srec: Motorola S-Records Output
Section 7.4: obj: Microsoft OMF Object Files
Section 7.4.1: obj Extensions to the SEGMENT Directive
Section 7.4.2: GROUP: Defining Groups of Segments
Section 7.4.3: UPPERCASE: Disabling Case Sensitivity in Output
Section 7.4.4: IMPORT: Importing DLL Symbols
Section 7.4.5: EXPORT: Exporting DLL Symbols
Section 7.4.6: ..start: Defining the Program Entry Point
Section 7.4.7: obj Extensions to the EXTERN Directive
Section 7.4.8: obj Extensions to the COMMON Directive
Section 7.5: win32: Microsoft Win32 Object Files
Section 7.5.1: win32 Extensions to the SECTION Directive
Section 7.5.2: win32: Safe Structured Exception Handling
Section 7.6: win64: Microsoft Win64 Object Files
Section 7.6.1: win64: Writing Position-Independent Code
Section 7.6.2: win64: Structured Exception Handling
Section 7.7: coff: Common Object File Format
Section 7.8: macho32 and macho64: Mach Object File Format
Section 7.9: elf32, elf64, elfx32: Executable and Linkable Format Object Files
Section 7.9.1: ELF specific directive osabi
Section 7.9.2: elf Extensions to the SECTION Directive
Section 7.9.3: Position-Independent Code: elf Special Symbols and WRT
Section 7.9.4: Thread Local Storage: elf Special Symbols and WRT
Section 7.9.5: elf Extensions to the GLOBAL Directive
Section 7.9.6: elf Extensions to the COMMON Directive
Section 7.9.7: 16-bit code and ELF
Section 7.9.8: Debug formats and ELF
Section 7.10: aout: Linux a.out Object Files
Section 7.11: aoutb: NetBSD/FreeBSD/OpenBSD a.out Object Files
Section 7.12: as86: Minix/Linux as86 Object Files
Section 7.13: rdf: Relocatable Dynamic Object File Format
Section 7.13.1: Requiring a Library: The LIBRARY Directive
Section 7.13.2: Specifying a Module Name: The MODULE Directive
Section 7.13.3: rdf Extensions to the GLOBAL Directive
Section 7.13.4: rdf Extensions to the EXTERN Directive
Section 7.14: dbg: Debugging Format

Chapter 8: Writing 16-bit Code (DOS, Windows 3/3.1)
Section 8.1: Producing .EXE Files
Section 8.1.1: Using the obj Format To Generate .EXE Files
Section 8.1.2: Using the bin Format To Generate .EXE Files
Section 8.2: Producing .COM Files
Section 8.2.1: Using the bin Format To Generate .COM Files
Section 8.2.2: Using the obj Format To Generate .COM Files
Section 8.3: Producing .SYS Files
Section 8.4: Interfacing to 16-bit C Programs
Section 8.4.1: External Symbol Names
Section 8.4.2: Memory Models
Section 8.4.3: Function Definitions and Function Calls
Section 8.4.4: Accessing Data Items
Section 8.4.5: c16.mac: Helper Macros for the 16-bit C Interface
Section 8.5: Interfacing to Borland Pascal Programs
Section 8.5.1: The Pascal Calling Convention
Section 8.5.2: Borland Pascal Segment Name Restrictions
Section 8.5.3: Using c16.mac With Pascal Programs

Chapter 9: Writing 32-bit Code (Unix, Win32, DJGPP)
Section 9.1: Interfacing to 32-bit C Programs
Section 9.1.1: External Symbol Names
Section 9.1.2: Function Definitions and Function Calls
Section 9.1.3: Accessing Data Items
Section 9.1.4: c32.mac: Helper Macros for the 32-bit C Interface
Section 9.2: Writing NetBSD/FreeBSD/OpenBSD and Linux/ELF Shared Libraries
Section 9.2.1: Obtaining the Address of the GOT
Section 9.2.2: Finding Your Local Data Items
Section 9.2.3: Finding External and Common Data Items
Section 9.2.4: Exporting Symbols to the Library User
Section 9.2.5: Calling Procedures Outside the Library
Section 9.2.6: Generating the Library File

Chapter 10: Mixing 16 and 32 Bit Code
Section 10.1: Mixed-Size Jumps
Section 10.2: Addressing Between Different-Size Segments
Section 10.3: Other Mixed-Size Instructions

Chapter 11: Writing 64-bit Code (Unix, Win64)
Section 11.1: Register Names in 64-bit Mode
Section 11.2: Immediates and Displacements in 64-bit Mode
Section 11.3: Interfacing to 64-bit C Programs (Unix)
Section 11.4: Interfacing to 64-bit C Programs (Win64)

Chapter 12: Troubleshooting
Section 12.1: Common Problems
Section 12.1.1: NASM Generates Inefficient Code
Section 12.1.2: My Jumps are Out of Range
Section 12.1.3: ORG Doesn't Work
Section 12.1.4: TIMES Doesn't Work
Section 12.2: Bugs

Appendix A: Ndisasm
Section A.1: Introduction
Section A.2: Getting Started: Installation
Section A.3: Running NDISASM
Section A.3.1: COM Files: Specifying an Origin
Section A.3.2: Code Following Data: Synchronisation
Section A.3.3: Mixed Code and Data: Automatic (Intelligent) Synchronisation
Section A.3.4: Other Options
Section A.4: Bugs and Improvements

Appendix B: Instruction List
Section B.1: Introduction
Section B.1.1: Special instructions...
Section B.1.2: Conventional instructions
Section B.1.3: Katmai Streaming SIMD instructions (SSE -- a.k.a. KNI, XMM, MMX2)
Section B.1.4: Introduced in Deschutes but necessary for SSE support
Section B.1.5: XSAVE group (AVX and extended state)
Section B.1.6: Generic memory operations
Section B.1.7: New MMX instructions introduced in Katmai
Section B.1.8: AMD Enhanced 3DNow! (Athlon) instructions
Section B.1.9: Willamette SSE2 Cacheability Instructions
Section B.1.10: Willamette MMX instructions (SSE2 SIMD Integer Instructions)
Section B.1.11: Willamette Streaming SIMD instructions (SSE2)
Section B.1.12: Prescott New Instructions (SSE3)
Section B.1.13: VMX/SVM Instructions
Section B.1.14: Extended Page Tables VMX instructions
Section B.1.15: Tejas New Instructions (SSSE3)
Section B.1.16: AMD SSE4A
Section B.1.17: New instructions in Barcelona
Section B.1.18: Penryn New Instructions (SSE4.1)
Section B.1.19: Nehalem New Instructions (SSE4.2)
Section B.1.20: Intel SMX
Section B.1.21: Geode (Cyrix) 3DNow! additions
Section B.1.22: Intel new instructions in ???
Section B.1.23: Intel AES instructions
Section B.1.24: Intel AVX AES instructions
Section B.1.25: Intel AVX instructions
Section B.1.26: Intel Carry-Less Multiplication instructions (CLMUL)
Section B.1.27: Intel AVX Carry-Less Multiplication instructions (CLMUL)
Section B.1.28: Intel Fused Multiply-Add instructions (FMA)
Section B.1.29: Intel post-32 nm processor instructions
Section B.1.30: VIA (Centaur) security instructions
Section B.1.31: AMD Lightweight Profiling (LWP) instructions
Section B.1.32: AMD XOP and FMA4 instructions (SSE5)
Section B.1.33: Intel AVX2 instructions
Section B.1.34: Transactional Synchronization Extensions (TSX)
Section B.1.35: Intel BMI1 and BMI2 instructions, AMD TBM instructions
Section B.1.36: Intel AVX-512 instructions
Section B.1.37: Systematic names for the hinting nop instructions

Appendix C: NASM Version History
Section C.1: NASM 2 Series
Section C.1.1: Version 2.11.05
Section C.1.2: Version 2.11.04
Section C.1.3: Version 2.11.03
Section C.1.4: Version 2.11.02
Section C.1.5: Version 2.11.01
Section C.1.6: Version 2.11
Section C.1.7: Version 2.10.09
Section C.1.8: Version 2.10.08
Section C.1.9: Version 2.10.07
Section C.1.10: Version 2.10.06
Section C.1.11: Version 2.10.05
Section C.1.12: Version 2.10.04
Section C.1.13: Version 2.10.03
Section C.1.14: Version 2.10.02
Section C.1.15: Version 2.10.01
Section C.1.16: Version 2.10
Section C.1.17: Version 2.09.10
Section C.1.18: Version 2.09.09
Section C.1.19: Version 2.09.08
Section C.1.20: Version 2.09.07
Section C.1.21: Version 2.09.06
Section C.1.22: Version 2.09.05
Section C.1.23: Version 2.09.04
Section C.1.24: Version 2.09.03
Section C.1.25: Version 2.09.02
Section C.1.26: Version 2.09.01
Section C.1.27: Version 2.09
Section C.1.28: Version 2.08.02
Section C.1.29: Version 2.08.01
Section C.1.30: Version 2.08
Section C.1.31: Version 2.07
Section C.1.32: Version 2.06
Section C.1.33: Version 2.05.01
Section C.1.34: Version 2.05
Section C.1.35: Version 2.04
Section C.1.36: Version 2.03.01
Section C.1.37: Version 2.03
Section C.1.38: Version 2.02
Section C.1.39: Version 2.01
Section C.1.40: Version 2.00
Section C.2: NASM 0.98 Series
Section C.2.1: Version 0.98.39
Section C.2.2: Version 0.98.38
Section C.2.3: Version 0.98.37
Section C.2.4: Version 0.98.36
Section C.2.5: Version 0.98.35
Section C.2.6: Version 0.98.34
Section C.2.7: Version 0.98.33
Section C.2.8: Version 0.98.32
Section C.2.9: Version 0.98.31
Section C.2.10: Version 0.98.30
Section C.2.11: Version 0.98.28
Section C.2.12: Version 0.98.26
Section C.2.13: Version 0.98.25alt
Section C.2.14: Version 0.98.25
Section C.2.15: Version 0.98.24p1
Section C.2.16: Version 0.98.24
Section C.2.17: Version 0.98.23
Section C.2.18: Version 0.98.22
Section C.2.19: Version 0.98.21
Section C.2.20: Version 0.98.20
Section C.2.21: Version 0.98.19
Section C.2.22: Version 0.98.18
Section C.2.23: Version 0.98.17
Section C.2.24: Version 0.98.16
Section C.2.25: Version 0.98.15
Section C.2.26: Version 0.98.14
Section C.2.27: Version 0.98.13
Section C.2.28: Version 0.98.12
Section C.2.29: Version 0.98.11
Section C.2.30: Version 0.98.10
Section C.2.31: Version 0.98.09
Section C.2.32: Version 0.98.08
Section C.2.33: Version 0.98.09b with John Coffman patches released 28-Oct-2001
Section C.2.34: Version 0.98.07 released 01/28/01
Section C.2.35: Version 0.98.06f released 01/18/01
Section C.2.36: Version 0.98.06e released 01/09/01
Section C.2.37: Version 0.98p1
Section C.2.38: Version 0.98bf (bug-fixed)
Section C.2.39: Version 0.98.03 with John Coffman's changes released 27-Jul-2000
Section C.2.40: Version 0.98.03
Section C.2.41: Version 0.98
Section C.2.42: Version 0.98p9
Section C.2.43: Version 0.98p8
Section C.2.44: Version 0.98p7
Section C.2.45: Version 0.98p6
Section C.2.46: Version 0.98p3.7
Section C.2.47: Version 0.98p3.6
Section C.2.48: Version 0.98p3.5
Section C.2.49: Version 0.98p3.4
Section C.2.50: Version 0.98p3.3
Section C.2.51: Version 0.98p3.2
Section C.2.52: Version 0.98p3-hpa
Section C.2.53: Version 0.98 pre-release 3
Section C.2.54: Version 0.98 pre-release 2
Section C.2.55: Version 0.98 pre-release 1
Section C.3: NASM 0.9 Series
Section C.3.1: Version 0.97 released December 1997
Section C.3.2: Version 0.96 released November 1997
Section C.3.3: Version 0.95 released July 1997
Section C.3.4: Version 0.94 released April 1997
Section C.3.5: Version 0.93 released January 1997
Section C.3.6: Version 0.92 released January 1997
Section C.3.7: Version 0.91 released November 1996
Section C.3.8: Version 0.90 released October 1996

Index