opflags: Rework opflags bits with OP_ macros
[nasm.git] / nasm.1
blobbca0cd5795e5e38beb48a32ff5481b174864c62b
1 .TH NASM 1 "The Netwide Assembler Project"
2 .SH NAME
3 nasm \- the Netwide Assembler, a portable 80x86 assembler
4 .SH SYNOPSIS
5 .B nasm
7 .B \-@
8 response file
9 ] [
10 .B \-f
11 format
12 ] [
13 .B \-o
14 outfile
15 ] [
16 .B \-l
17 listfile
18 ] [
19 .IR options ...
20 ] filename
21 .br
22 .B nasm \-h
23 .br
24 .B nasm \-v
25 .SH DESCRIPTION
26 The
27 .B nasm
28 command assembles the file
29 .I filename
30 and directs output to the file
31 .I outfile
32 if specified. If
33 .I outfile
34 is not specified,
35 .B nasm
36 will derive a default output file name from the name of its input
37 file, usually by appending `.o' or `.obj', or by removing all
38 extensions for a raw binary file. Failing that, the output file name
39 will be `nasm.out'.
40 .SS OPTIONS
41 .TP
42 .BI \-@ " filename"
43 Causes
44 .B nasm
45 to process options from 
46 .I filename
47 as if they were included on the command line.
48 .TP
49 .B \-a
50 Causes
51 .B nasm
52 to assemble the given input file without first applying the macro
53 preprocessor.
54 .TP
55 .BI \-D " macro[=value]"
56 Pre-defines a single-line macro.
57 .TP
58 .BI \-d " macro[=value]"
59 Same as the
60 .B \-D
61 option.
62 .TP
63 .B \-e
64 Causes
65 .B nasm
66 to preprocess the given input file, and write the output to
67 .I stdout
68 (or the specified output file name), and not actually assemble
69 anything.
70 .TP
71 .BI \-f " format"
72 Specifies the output file format. To see a list of valid output
73 formats, use the
74 .B -hf
75 option.
76 .TP
77 .B \-g
78 Causes
79 .B nasm
80 to generate debug information in selected format
81 .TP
82 .B \-h
83 Causes
84 .B nasm
85 to exit immediately, after giving a summary of its invocation
86 options.
87 .TP
88 .B \-hf
89 Same as
90 .B -h
91 , but also lists all valid output formats.
92 .TP
93 .BI \-I " directory"
94 Adds a directory to the search path for include files. The directory
95 specification must include the trailing slash, as it will be
96 directly prepended to the name of the include file.
97 .TP
98 .BI \-i " directory"
99 Same as the
100 .B \-I
101 option.
103 .BI \-l " listfile"
104 Causes an assembly listing to be directed to the given file, in
105 which the original source is displayed on the right hand side (plus
106 the source for included files and the expansions of multi-line
107 macros) and the generated code is shown in hex on the left.
109 .B \-M
110 Causes
111 .B nasm
112 to output Makefile-style dependencies to stdout; normal output is
113 suppressed.
115 .BI \-MG " file"
116 Same as
117 .B \-M
118 but assumes that missing Makefile dependecies are generated and added
119 to dependency list without a prefix.
121 .BI \-MF " file"
122 Output Makefile-style dependencies to the specified file.
124 .BI \-MD " file"
125 Same as a combination of
126 .B \-M
128 .B \-MF
129 options.
131 .BI \-MT " file"
132 Override the default name of the dependency target
133 dependency target name. This is normally the same
134 as the output filename, specified by the
135 .B \-o
136 option.
138 .BI \-MQ " file"
139 The same as
140 .B \-MT
141 except it tries to quote characters that have special
142 meaning in Makefile syntax. This is not foolproof,
143 as not all characters with special meaning are quotable
144 in Make.
146 .BI \-MP
147 Emit phony target
149 .BI \-O " number"
150 Optimize branch offsets.
152 .B \-O0
153 :No optimization
155 .B \-O1
156 :Minimal optimization
158 .B \-Ox
159 :Multipass optimization (default)
161 .BI \-o " outfile"
162 Specifies a precise name for the output file, overriding
163 .BR nasm 's
164 default means of determining it.
166 .BI \-P " file"
167 Specifies a file to be pre-included, before the main source file
168 starts to be processed.
170 .BI \-p " file"
171 Same as the
172 .B \-P
173 option.
175 .BI \-r
176 Causes
177 .B nasm
178 to exit immediately, after displaying its version number.
179 .I (obsolete)
181 .B \-s
182 Causes
183 .B nasm
184 to send its error messages and/or help text to
185 .I stdout
186 instead of
187 .IR stderr .
189 .B \-t
190 Causes
191 .B nasm
192 to assemble in SciTech TASM compatible mode
194 .BI \-U " macro"
195 Undefines a single-line macro.
197 .BI \-u " macro"
198 Same as the
199 .B \-U
200 option.
202 .BI \-v
203 Causes
204 .B nasm
205 to exit immediately, after displaying its version number.
207 .BI \-w [+-]foo
208 Causes
209 .B nasm
210 to enable or disable certain classes of warning messages, for
211 example
212 .B \-w+orphan-labels
214 .B \-w-macro-params
216 .BI \-X " format"
217 specifies error reporting format (gnu or vc).
219 .BI \-Z " filename"
220 Causes
221 .B nasm
222 to redirect error messages to
223 .IR filename .
224 This option exists to support operating systems on which stderr is not
225 easily redirected.
227 .BI \-\-prefix
229 .BI \-\-postfix
230 Prepend or append (respectively) the given argument to all
231 global or extern variables.
234 .SS SYNTAX
235 This man page does not fully describe the syntax of
236 .BR nasm 's
237 assembly language, but does give a summary of the differences from
238 other assemblers.
240 .I Registers
241 have no leading `%' sign, unlike
242 .BR gas ,
243 and floating-point stack registers are referred to as
244 .IR st0 ,
245 .IR st1 ,
246 and so on.
248 .I Floating-point instructions
249 may use either the single-operand form or the double. A
250 .I TO
251 keyword is provided; thus, one could either write
253 .ti +15n
254 fadd st0,st1
256 .ti +15n
257 fadd st1,st0
259 or one could use the alternative single-operand forms
261 .ti +15n
262 fadd st1
264 .ti +15n
265 fadd to st1
267 .I Uninitialised storage
268 is reserved using the
269 .IR RESB ,
270 .IR RESW ,
271 .IR RESD ,
272 .IR RESQ ,
273 .I REST
275 .I RESO
276 pseudo-opcodes, each taking one parameter which gives the number of
277 bytes, words, doublewords, quadwords or ten-byte words to reserve.
279 .I Repetition
280 of data items is not done by the
281 .I DUP
282 keyword as seen in DOS assemblers, but by the use of the
283 .I TIMES
284 prefix, like this:
286 .ti +6n
287 .ta 9n
288 message:        times 3 db 'abc'
290 .ti +15n
291 times 64-$+message db 0
293 which defines the string `abcabcabc', followed by the right number
294 of zero bytes to make the total length up to 64 bytes.
296 .I Symbol references
297 are always understood to be immediate (i.e. the address of the
298 symbol), unless square brackets are used, in which case the contents
299 of the memory location are used. Thus:
301 .ti +15n
302 mov ax,wordvar
304 loads AX with the address of the variable `wordvar', whereas
306 .ti +15n
307 mov ax,[wordvar]
309 .ti +15n
310 mov ax,[wordvar+1]
312 .ti +15n
313 mov ax,[es:wordvar+bx]
315 all refer to the
316 .I contents
317 of memory locations. The syntaxes
319 .ti +15n
320 mov ax,es:wordvar[bx]
322 .ti +15n
323 es mov ax,wordvar[1]
325 are not legal at all, although the use of a segment register name as
326 an instruction prefix is valid, and can be used with instructions
327 such as
328 .I LODSB
329 which can't be overridden any other way.
331 .I Constants
332 may be expressed numerically in most formats: a trailing H, Q or B
333 denotes hex, octal or binary respectively, and a leading `0x' or `$'
334 denotes hex as well. Leading zeros are not treated specially at all.
335 Character constants may be enclosed in single or double quotes;
336 there is no escape character. The ordering is little-endian
337 (reversed), so that the character constant
338 .I 'abcd'
339 denotes 0x64636261 and not 0x61626364.
341 .I Local labels
342 begin with a period, and their `locality' is granted by the
343 assembler prepending the name of the previous non-local symbol. Thus
344 declaring a label `.loop' after a label `label' has actually defined
345 a symbol called `label.loop'.
346 .SS DIRECTIVES
347 .I SECTION name
349 .I SEGMENT name
350 causes
351 .B nasm
352 to direct all following code to the named section. Section names
353 vary with output file format, although most formats support the
354 names
355 .IR .text ,
356 .I .data
358 .IR .bss .
359 (The exception is the
360 .I obj
361 format, in which all segments are user-definable.)
363 .I ABSOLUTE address
364 causes
365 .B nasm
366 to position its notional assembly point at an absolute address: so
367 no code or data may be generated, but you can use
368 .IR RESB ,
369 .I RESW
371 .I RESD
372 to move the assembly point further on, and you can define labels. So
373 this directive may be used to define data structures. When you have
374 finished doing absolute assembly, you must issue another
375 .I SECTION
376 directive to return to normal assembly.
378 .I BITS 16,
379 .I BITS 32
381 .I BITS 64
382 switches the default processor mode for which
383 .B nasm
384 is generating code: it is equivalent to
385 .I USE16
387 .I USE32
388 in DOS assemblers.
390 .I EXTERN symbol
392 .I GLOBAL symbol
393 import and export symbol definitions, respectively, from and to
394 other modules. Note that the
395 .I GLOBAL
396 directive must appear before the definition of the symbol it refers
399 .I STRUC strucname
401 .IR ENDSTRUC ,
402 when used to bracket a number of
403 .IR RESB ,
404 .I RESW
405 or similar instructions, define a data structure. In addition to
406 defining the offsets of the structure members, the construct also
407 defines a symbol for the size of the structure, which is simply the
408 structure name with
409 .I _size
410 tacked on to the end.
411 .SS FORMAT-SPECIFIC DIRECTIVES
412 .I ORG address
413 is used by the
414 .I bin
415 flat-form binary output format, and specifies the address at which
416 the output code will eventually be loaded.
418 .I GROUP grpname seg1 seg2...
419 is used by the
420 .I obj
421 (Microsoft 16-bit) output format, and defines segment groups. This
422 format also uses
423 .IR UPPERCASE ,
424 which directs that all segment, group and symbol names output to the
425 object file should be in uppercase. Note that the actual assembly is
426 still case sensitive.
428 .I LIBRARY libname
429 is used by the
430 .I rdf
431 output format, and causes a dependency record to be written to the
432 output file which indicates that the program requires a certain
433 library in order to run.
434 .SS MACRO PREPROCESSOR
435 Single-line macros are defined using the
436 .I %define
438 .I %idefine
439 commands, in a similar fashion to the C preprocessor. They can be
440 overloaded with respect to number of parameters, although defining a
441 macro with no parameters prevents the definition of any macro with
442 the same name taking parameters, and vice versa.
443 .I %define
444 defines macros whose names match case-sensitively, whereas
445 .I %idefine
446 defines case-insensitive macros.
448 Multi-line macros are defined using
449 .I %macro
451 .I %imacro
452 (the distinction is the same as that between
453 .I %define
455 .IR %idefine ),
456 whose syntax is as follows:
458 .ti +6n
459 %macro
460 .I name
461 .IR minprm [- maxprm "][+][.nolist] [" defaults ]
463 .ti +15n
464 <some lines of macro expansion text>
466 .ti +6n
467 %endmacro
469 Again, these macros may be overloaded. The trailing plus sign
470 indicates that any parameters after the last one get subsumed, with
471 their separating commas, into the last parameter. The
472 .I defaults
473 part can be used to specify defaults for unspecified macro
474 parameters after
475 .IR minparam .
476 .I %endm
477 is a valid synonym for
478 .IR %endmacro .
480 To refer to the macro parameters within a macro expansion, you use
481 .IR %1 ,
482 .I %2
483 and so on. You can also enforce that a macro parameter should
484 contain a condition code by using
485 .IR %+1 ,
486 and you can invert the condition code by using
487 .IR %-1 .
488 You can also define a label specific to a macro invocation by
489 prefixing it with a double % sign.
491 Files can be included using the
492 .I %include
493 directive, which works like C.
495 The preprocessor has a `context stack', which may be used by one
496 macro to store information that a later one will retrieve. You can
497 push a context on the stack using
498 .IR %push ,
499 remove one using
500 .IR %pop ,
501 and change the name of the top context (without disturbing any
502 associated definitions) using
503 .IR %repl .
504 Labels and
505 .I %define
506 macros specific to the top context may be defined by prefixing their
507 names with %$, and things specific to the next context down with
508 %$$, and so on.
510 Conditional assembly is done by means of
511 .IR %ifdef ,
512 .IR %ifndef ,
513 .I %else
515 .I %endif
516 as in C. (Except that
517 .I %ifdef
518 can accept several putative macro names, and will evaluate TRUE if
519 any of them is defined.) In addition, the directives
520 .I %ifctx
522 .I %ifnctx
523 can be used to condition on the name of the top context on the
524 context stack. The obvious set of `else-if' directives,
525 .IR %elifdef ,
526 .IR %elifndef ,
527 .IR %elifctx
529 .IR %elifnctx
530 are also supported.
531 .SH BUGS
532 Please report bugs through the bug tracker function at http://nasm.sourceforge.org.
533 .SH SEE ALSO
534 .BR as "(" 1 "),"
535 .BR ld "(" 1 ")."