nasm.txt 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. nasm(1)
  2. =======
  3. :doctype: manpage
  4. :man source: NASM
  5. :man manual: The Netwide Assembler Project
  6. NAME
  7. ----
  8. nasm - the Netwide Assembler, a portable 80x86 assembler
  9. SYNOPSIS
  10. --------
  11. *nasm* [*-@* response file] [*-f* format] [*-o* outfile] [*-l* listfile] ['options'...] filename
  12. DESCRIPTION
  13. -----------
  14. The *nasm* command assembles the file 'filename' and directs output to the file
  15. 'outfile' if specified. If 'outfile' is not specified, *nasm* will derive a default
  16. output file name from the name of its input file, usually by appending `.o' or
  17. `.obj', or by removing all extensions for a raw binary file. Failing that, the
  18. output file name will be `nasm.out'.
  19. OPTIONS
  20. -------
  21. *-@* 'filename'::
  22. Causes *nasm* to process options from filename as if they were included on
  23. the command line.
  24. *-a*::
  25. Causes *nasm* to assemble the given input file without first applying the
  26. macro preprocessor.
  27. *-D*|*-d* 'macro[=value]'::
  28. Pre-defines a single-line macro.
  29. *-E*|*-e*::
  30. Causes *nasm* to preprocess the given input file, and write the output to
  31. 'stdout' (or the specified output file name), and not actually assemble
  32. anything.
  33. *-f* 'format'::
  34. Specifies the output file format. To see a list of valid output formats,
  35. use the *-hf* option.
  36. *-F* 'format'::
  37. Specifies the debug information format. To see a list of valid output
  38. formats, use the *-y* option (for example *-felf -y*).
  39. *-g*::
  40. Causes *nasm* to generate debug information.
  41. *-g*'format'::
  42. Equivalent to **-g -F**__ format__.
  43. *-h*::
  44. Causes *nasm* to exit immediately, after giving a summary of its
  45. invocation options.
  46. *-hf*::
  47. Same as *-h* , but also lists all valid output formats.
  48. *-I*|*-i* 'directory'::
  49. Adds a directory to the search path for include files. The directory
  50. specification must include the trailing slash, as it will be directly
  51. prepended to the name of the include file.
  52. *-l* 'listfile'::
  53. Causes an assembly listing to be directed to the given file, in which
  54. the original source is displayed on the right hand side (plus the source
  55. for included files and the expansions of multi-line macros) and the
  56. generated code is shown in hex on the left.
  57. *-M*::
  58. Causes *nasm* to output Makefile-style dependencies to stdout; normal
  59. output is suppressed.
  60. *-MG* 'file'::
  61. Same as *-M* but assumes that missing Makefile dependecies are generated
  62. and added to dependency list without a prefix.
  63. *-MF* 'file'::
  64. Output Makefile-style dependencies to the specified file.
  65. *-MD* 'file'::
  66. Same as a combination of *-M* and *-MF* options.
  67. *-MT* 'file'::
  68. Override the default name of the dependency target dependency target name.
  69. This is normally the same as the output filename, specified by
  70. the *-o* option.
  71. *-MQ* 'file'::
  72. The same as *-MT* except it tries to quote characters that have special
  73. meaning in Makefile syntax. This is not foolproof, as not all characters
  74. with special meaning are quotable in Make.
  75. *-MP*::
  76. Emit phony target.
  77. *-O* 'number'::
  78. Optimize branch offsets.
  79. * *-O0*: No optimization
  80. * *-O1*: Minimal optimization
  81. * *-Ox*: Multipass optimization (default)
  82. *-o* 'outfile'::
  83. Specifies a precise name for the output file, overriding *nasm*'s default
  84. means of determining it.
  85. *-P*|*-p* 'file'::
  86. Specifies a file to be pre-included, before the main source file
  87. starts to be processed.
  88. *-s*::
  89. Causes *nasm* to send its error messages and/or help text to stdout
  90. instead of stderr.
  91. *-t*::
  92. Causes *nasm* to assemble in SciTech TASM compatible mode.
  93. *-U*|*-u* 'macro'::
  94. Undefines a single-line macro.
  95. *-v*::
  96. Causes *nasm* to exit immediately, after displaying its version number.
  97. *-W[no-]foo'::
  98. Causes *nasm* to enable or disable certain classes of warning messages,
  99. in gcc-like style, for example *-Worphan-labels* or *-Wno-orphan-labels*.
  100. *-w*'[+-]foo'::
  101. Causes *nasm* to enable or disable certain classes of warning messages,
  102. for example *-w+orphan-labels* or *-w-macro-params*.
  103. *-X* 'format'::
  104. Specifies error reporting format (gnu or vc).
  105. *-y*::
  106. Causes *nasm* to list supported debug formats.
  107. *-Z* 'filename'::
  108. Causes *nasm* to redirect error messages to 'filename'. This option exists
  109. to support operating systems on which stderr is not easily redirected.
  110. --prefix::
  111. --postfix::
  112. Prepend or append (respectively) the given argument to all global or
  113. extern variables.
  114. SYNTAX
  115. ------
  116. This man page does not fully describe the syntax of *nasm*'s assembly language,
  117. but does give a summary of the differences from other assemblers.
  118. 'Registers' have no leading `%' sign, unlike *gas*, and floating-point stack
  119. registers are referred to as 'st0', 'st1', and so on.
  120. 'Floating-point instructions' may use either the single-operand form or the
  121. double. A 'TO' keyword is provided; thus, one could either write
  122. fadd st0,st1
  123. fadd st1,st0
  124. or one could use the alternative single-operand forms
  125. fadd st1
  126. fadd to st1
  127. 'Uninitialised storage' is reserved using the 'RESB', 'RESW', 'RESD', 'RESQ',
  128. 'REST' and 'RESO' pseudo-opcodes, each taking one parameter which gives the
  129. number of bytes, words, doublewords, quadwords or ten-byte words to reserve.
  130. 'Repetition' of data items is not done by the 'DUP' keyword as seen in DOS
  131. assemblers, but by the use of the 'TIMES' prefix, like this:
  132. message: times 3 db 'abc'
  133. times 64-$+message db 0
  134. which defines the string `abcabcabc`, followed by the right number of zero
  135. bytes to make the total length up to 64 bytes.
  136. 'Symbol references' are always understood to be immediate (i.e. the address
  137. of the symbol), unless square brackets are used, in which case the contents
  138. of the memory location are used. Thus:
  139. mov ax,wordvar
  140. loads AX with the address of the variable `wordvar`, whereas
  141. mov ax,[wordvar]
  142. mov ax,[wordvar+1]
  143. mov ax,[es:wordvar+bx]
  144. all refer to the 'contents' of memory locations. The syntaxes
  145. mov ax,es:wordvar[bx]
  146. es mov ax,wordvar[1]
  147. are not legal at all, although the use of a segment register name as an instruction
  148. prefix is valid, and can be used with instructions such as 'LODSB' which can't
  149. be overridden any other way.
  150. 'Constants' may be expressed numerically in most formats: a trailing H, Q or
  151. B denotes hex, octal or binary respectively, and a leading `0x' or `$' denotes
  152. hex as well. Leading zeros are not treated specially at all. Character constants
  153. may be enclosed in single or double quotes; there is no escape character. The
  154. ordering is little-endian (reversed), so that the character constant ''abcd''
  155. denotes 0x64636261 and not 0x61626364.
  156. Local labels begin with a period, and their `locality' is granted by the assembler
  157. prepending the name of the previous non-local symbol. Thus declaring a label
  158. `.loop' after a label `label' has actually defined a symbol called `label.loop'.
  159. DIRECTIVES
  160. ----------
  161. 'SECTION' 'name' or 'SEGMENT' 'name' causes *nasm* to direct all following code
  162. to the named section. Section names vary with output file format, although most
  163. formats support the names '.text', '.data' and '.bss'. (The exception is the
  164. 'obj' format, in which all segments are user-definable.)
  165. 'ABSOLUTE' 'address' causes *nasm* to position its notional assembly point at
  166. an absolute address: so no code or data may be generated, but you can use 'RESB',
  167. 'RESW' and 'RESD' to move the assembly point further on, and you can define labels.
  168. So this directive may be used to define data structures. When you have finished
  169. doing absolute assembly, you must issue another 'SECTION' directive to return to
  170. normal assembly.
  171. 'BITS' '16', 'BITS' '32' or 'BITS' '64' switches the default processor mode for
  172. which *nasm* is generating code: it is equivalent to 'USE16' or 'USE32' in DOS
  173. assemblers.
  174. 'EXTERN' 'symbol' and 'GLOBAL' 'symbol' import and export symbol definitions,
  175. respectively, from and to other modules. Note that the 'GLOBAL' directive must
  176. appear before the definition of the symbol it refers to.
  177. 'STRUC' 'strucname' and 'ENDSTRUC', when used to bracket a number of 'RESB',
  178. 'RESW' or similar instructions, define a data structure. In addition to
  179. defining the offsets of the structure members, the construct also defines a symbol
  180. for the size of the structure, which is simply the structure name with 'size'
  181. tacked on to the end.
  182. FORMAT-SPECIFIC DIRECTIVES
  183. --------------------------
  184. 'ORG' 'address' is used by the 'bin' flat-form binary output format, and
  185. specifies the address at which the output code will eventually be loaded.
  186. 'GROUP' 'grpname' 'seg1' 'seg2'... is used by the obj (Microsoft 16-bit)
  187. output format, and defines segment groups. This format also uses 'UPPERCASE',
  188. which directs that all segment, group and symbol names output to the object
  189. file should be in uppercase. Note that the actual assembly is still case
  190. sensitive.
  191. 'LIBRARY' 'libname' is used by the 'rdf' output format, and causes a
  192. dependency record to be written to the output file which indicates that
  193. the program requires a certain library in order to run.
  194. MACRO PREPROCESSOR
  195. ------------------
  196. Single-line macros are defined using the '%define' or '%idefine' commands, in
  197. a similar fashion to the C preprocessor. They can be overloaded with respect
  198. to number of parameters, although defining a macro with no parameters prevents
  199. the definition of any macro with the same name taking parameters, and vice versa.
  200. '%define' defines macros whose names match case-sensitively, whereas '%idefine'
  201. defines case-insensitive macros.
  202. Multi-line macros are defined using '%macro' and '%imacro' (the distinction is the
  203. same as that between '%define' and '%idefine'), whose syntax is as follows
  204. %macro name minprm[-maxprm][+][.nolist] [defaults]
  205. <some lines of macro expansion text>
  206. %endmacro
  207. Again, these macros may be overloaded. The trailing plus sign indicates that
  208. any parameters after the last one get subsumed, with their separating commas,
  209. into the last parameter. The 'defaults' part can be used to specify defaults for
  210. unspecified macro parameters after 'minparam'. '%endm' is a valid synonym for
  211. '%endmacro'.
  212. To refer to the macro parameters within a macro expansion, you use '%1', '%2' and
  213. so on. You can also enforce that a macro parameter should contain a condition
  214. code by using '%+1', and you can invert the condition code by using '%-1'. You can also
  215. define a label specific to a macro invocation by prefixing it with a double `%' sign.
  216. Files can be included using the '%include' directive, which works like C.
  217. The preprocessor has a `context stack', which may be used by one macro to store
  218. information that a later one will retrieve. You can push a context on the stack
  219. using '%push', remove one using '%pop', and change the name of the top context (without
  220. disturbing any associated definitions) using '%repl'. Labels and '%define' macros
  221. specific to the top context may be defined by prefixing their names with %$,
  222. and things specific to the next context down with %$$, and so on.
  223. Conditional assembly is done by means of '%ifdef', '%ifndef', '%else' and '%endif'
  224. as in C. (Except that '%ifdef' can accept several putative macro names, and
  225. will evaluate TRUE if any of them is defined.) In addition, the directives
  226. '%ifctx' and '%ifnctx' can be used to condition on the name of the top context
  227. on the context stack. The obvious set of `else-if' directives, '%elifdef',
  228. '%elifndef', '%elifctx' and '%elifnctx' are also supported.
  229. BUGS
  230. ----
  231. Please report bugs through the bug tracker function at http://nasm.us.
  232. SEE ALSO
  233. --------
  234. *as*(1), *ld*(1).