NASM — the Netwide Assembler
Total Page:16
File Type:pdf, Size:1020Kb
NASM — The Netwide Assembler version 2.12.02 © 1996−2016 The NASM Development Team — All Rights Reserved This document is redistributable under the license given in the file "LICENSE" distributed in the NASM archive. Contents Chapter 1: Introduction . .16 1.1 What Is NASM? . .16 1.1.1 Why Yet Another Assembler?. .16 1.1.2 License Conditions . .16 1.2 Contact Information . .17 1.3 Installation. .17 1.3.1 Installing NASM under MS−DOS or Windows . .17 1.3.2 Installing NASM under Unix . .18 Chapter 2: Running NASM . .19 2.1 NASM Command−Line Syntax . .19 2.1.1 The −o Option: Specifying the Output File Name . .19 2.1.2 The −f Option: Specifying the Output File Format . .20 2.1.3 The −l Option: Generating a Listing File . .20 2.1.4 The −M Option: Generate Makefile Dependencies . .20 2.1.5 The −MG Option: Generate Makefile Dependencies . .20 2.1.6 The −MF Option: Set Makefile Dependency File . .20 2.1.7 The −MD Option: Assemble and Generate Dependencies. .21 2.1.8 The −MT Option: Dependency Target Name. .21 2.1.9 The −MQ Option: Dependency Target Name (Quoted) . .21 2.1.10 The −MP Option: Emit phony targets. .21 2.1.11 The −F Option: Selecting a Debug Information Format . .21 2.1.12 The −g Option: Enabling Debug Information. .21 2.1.13 The −X Option: Selecting an Error Reporting Format. .21 2.1.14 The −Z Option: Send Errors to a File. .22 2.1.15 The −s Option: Send Errors to stdout ............................22 2.1.16 The −i Option: Include File Search Directories . .22 2.1.17 The −p Option: Pre−Include a File. .23 2.1.18 The −d Option: Pre−Define a Macro. .23 2.1.19 The −u Option: Undefine a Macro . .23 3 2.1.20 The −E Option: Preprocess Only. .23 2.1.21 The −a Option: Don’t Preprocess At All . .24 2.1.22 The −O Option: Specifying Multipass Optimization . .24 2.1.23 The −t Option: Enable TASM Compatibility Mode . .24 2.1.24 The −w and −W Options: Enable or Disable Assembly Warnings . .24 2.1.25 The −v Option: Display Version Info . .26 2.1.26 The −y Option: Display Available Debug Info Formats . .26 2.1.27 The −−prefix and −−postfix Options. .26 2.1.28 The NASMENV Environment Variable . .26 2.2 Quick Start for MASM Users . .26 2.2.1 NASM Is Case−Sensitive . .26 2.2.2 NASM Requires Square Brackets For Memory References . .27 2.2.3 NASM Doesn’t Store Variable Types . .27 2.2.4 NASM Doesn’t ASSUME ....................................27 2.2.5 NASM Doesn’t Support Memory Models . .28 2.2.6 Floating−Point Differences . .28 2.2.7 Other Differences . .28 Chapter 3: The NASM Language . .29 3.1 Layout of a NASM Source Line . .29 3.2 Pseudo−Instructions . .30 3.2.1 DB and Friends: Declaring Initialized Data . .30 3.2.2 RESB and Friends: Declaring Uninitialized Data . .30 3.2.3 INCBIN: Including External Binary Files . .31 3.2.4 EQU: Defining Constants . .31 3.2.5 TIMES: Repeating Instructions or Data . .31 3.3 Effective Addresses . .32 3.4 Constants . .33 3.4.1 Numeric Constants . .33 3.4.2 Character Strings . .34 3.4.3 Character Constants. .35 3.4.4 String Constants . .35 3.4.5 Unicode Strings. .35 3.4.6 Floating−Point Constants . .35 4 3.4.7 Packed BCD Constants . .37 3.5 Expressions . .37 3.5.1 |: Bitwise OR Operator. .37 3.5.2 ^: Bitwise XOR Operator . .37 3.5.3 &: Bitwise AND Operator . .37 3.5.4 << and >>: Bit Shift Operators . .37 3.5.5 + and −: Addition and Subtraction Operators . .37 3.5.6 *, /, //, % and %%: Multiplication and Division. .38 3.5.7 Unary Operators . .38 3.6 SEG and WRT ............................................38 3.7 STRICT: Inhibiting Optimization . .39 3.8 Critical Expressions . .39 3.9 Local Labels. .39 Chapter 4: The NASM Preprocessor . .41 4.1 Single−Line Macros . .41 4.1.1 The Normal Way: %define ..................................41 4.1.2 Resolving %define: %xdefine ...............................42 4.1.3 Macro Indirection: %[...] ..................................43 4.1.4 Concatenating Single Line Macro Tokens: %+..........................43 4.1.5 The Macro Name Itself: %? and %?? ..............................44 4.1.6 Undefining Single−Line Macros: %undef ...........................44 4.1.7 Preprocessor Variables: %assign ...............................44 4.1.8 Defining Strings: %defstr ..................................45 4.1.9 Defining Tokens: %deftok ..................................45 4.2 String Manipulation in Macros . .45 4.2.1 Concatenating Strings: %strcat ................................45 4.2.2 String Length: %strlen ....................................46 4.2.3 Extracting Substrings: %substr ................................46 4.3 Multi−Line Macros: %macro ....................................46 4.3.1 Overloading Multi−Line Macros . .47 4.3.2 Macro−Local Labels . .48 4.3.3 Greedy Macro Parameters. .48 4.3.4 Macro Parameters Range . .49 5 4.3.5 Default Macro Parameters . .50 4.3.6 %0: Macro Parameter Counter. .51 4.3.7 %00: Label Preceeding Macro . .51 4.3.8 %rotate: Rotating Macro Parameters . .51 4.3.9 Concatenating Macro Parameters . .52 4.3.10 Condition Codes as Macro Parameters . .53 4.3.11 Disabling Listing Expansion . .53 4.3.12 Undefining Multi−Line Macros: %unmacro..........................53 4.4 Conditional Assembly . .54 4.4.1 %ifdef: Testing Single−Line Macro Existence. .54 4.4.2 %ifmacro: Testing Multi−Line Macro Existence . .55 4.4.3 %ifctx: Testing the Context Stack. .55 4.4.4 %if: Testing Arbitrary Numeric Expressions . .55 4.4.5 %ifidn and %ifidni: Testing Exact Text Identity . .56 4.4.6 %ifid, %ifnum, %ifstr: Testing Token Types . ..