NASM 0.94
[nasm.git] / nasm.doc
blob0613e18e176506af41335f6dc4d27b3b041ffc4a
1                      The Netwide Assembler, NASM
2                      ===========================
4 Introduction
5 ============
7 The Netwide Assembler grew out of an idea on comp.lang.asm.x86 (or
8 possibly alt.lang.asm, I forget which), which was essentially that
9 there didn't seem to be a good free x86-series assembler around, and
10 that maybe someone ought to write one.
12 - A86 is good, but not free, and in particular you don't get any
13   32-bit capability until you pay. It's DOS only, too.
15 - GAS is free, and ports over DOS/Unix, but it's not very good,
16   since it's designed to be a back end to gcc, which always feeds it
17   correct code. So its error checking is minimal. Also its syntax is
18   horrible, from the point of view of anyone trying to actually
19   _write_ anything in it. Plus you can't write 16-bit code in it
20   (properly).
22 - AS86 is Linux specific, and (my version at least) doesn't seem to
23   have much (or any) documentation.
25 - MASM isn't very good. And it's expensive. And it runs only under
26   DOS.
28 - TASM is better, but still strives for MASM compatibility, which
29   means millions of directives and tons of red tape. And its syntax
30   is essentially MASM's, with the contradictions and quirks that
31   entails (although it sorts out some of those by means of Ideal
32   mode). It's expensive too. And it's DOS only.
34 So here, for your coding pleasure, is NASM. At present it's still in
35 prototype stage - we don't promise that it can outperform any of
36 these assemblers. But please, _please_ send us bug reports, fixes,
37 helpful information, and anything else you can get your hands on
38 (and thanks to the many people who've done this already! You all
39 know who you are), and we'll improve it out of all recognition.
40 Again.
42 Please see the file `Licence' for the legalese.
44 Getting Started: Installation
45 =============================
47 NASM is distributed in source form, in what we hope is totally
48 ANSI-compliant C. It uses no non-portable code at all, that we know
49 of. It ought to compile without change on any system you care to try
50 it on. We also supply a pre-compiled 16-bit DOS binary.
52 To install it, edit the Makefile to describe your C compiler, and
53 type `make'. Then copy the binary to somewhere on your path. That's
54 all - NASM relies on no files other than its own executable.
55 Although if you're on a Unix system, you may also want to install
56 the NASM manpage (`nasm.1'). You may also want to install the binary
57 and manpage for the Netwide Disassembler, NDISASM (also see
58 `ndisasm.doc').
60 Running NASM
61 ============
63 To assemble a file, you issue a command of the form
65         nasm -f <format> <filename> [-o <output>]
67 For example,
69         nasm -f elf myfile.asm
71 will assemble `myfile.asm' into an ELF object file `myfile.o'. And
73         nasm -f bin myfile.asm -o myfile.com
75 will assemble `myfile.asm' into a raw binary program `myfile.com'.
77 To get usage instructions from NASM, try typing `nasm -h'. This will
78 also list the available output file formats, and what they are.
80 If you use Linux but aren't sure whether your system is a.out or
81 ELF, type `file /usr/bin/nasm' or wherever you put the NASM binary.
82 If it says something like
84 /usr/bin/nasm: ELF 32-bit LSB executable i386 (386 and up) Version 1
86 then your system is ELF, and you should use `-f elf' when you want
87 NASM to produce Linux object files. If it says
89 /usr/bin/nasm: Linux/i386 demand-paged executable (QMAGIC)
91 or something similar, your system is a.out, and you should use `-f
92 aout' instead.
94 Like Unix compilers and assemblers, NASM is silent unless it goes
95 wrong: you won't see any output at all, unless it gives error
96 messages.
98 Writing Programs with NASM
99 ==========================
101 Each line of a NASM source file should contain some combination of
102 the four fields
104 LABEL:  INSTRUCTION     OPERANDS        ; COMMENT
106 `LABEL' defines a label pointing to that point in the source. There
107 are no restrictions on white space: labels may have white space
108 before them, or not, as you please. The colon after the label is
109 also optional.
111 Valid characters in labels are letters, numbers, `_', `$', `#', `@',
112 `~', `?', and `.'. The only characters which may be used as the
113 _first_ character of an identifier are letters, `_' and `?', and
114 (with special meaning: see `Local Labels') `.'. An identifier may
115 also be prefixed with a $ sign to indicate that it is intended to be
116 read as an identifier and not a reserved word; thus, if some other
117 module you are linking with defines a symbol `eax', you can refer to
118 `$eax' in NASM code to distinguish it from the register name.
120 `INSTRUCTION' can be any machine opcode (Pentium and P6 opcodes, FPU
121 opcodes, MMX opcodes and even undocumented opcodes are all
122 supported). The instruction may be prefixed by LOCK, REP, REPE/REPZ
123 or REPNE/REPNZ, in the usual way. Explicit address-size and operand-
124 size prefixes A16, A32, O16 and O32 are provided - one example of
125 their use is given in the `Unusual Instruction Sizes' section below.
126 You can also use a segment register as a prefix: coding `es mov
127 [bx],ax' is equivalent to coding `mov [es:bx],ax'. We recommend the
128 latter syntax, since it is consistent with other syntactic features
129 of the language, but for instructions such as `lodsb' there isn't
130 anywhere to put a segment override except as a prefix. This is why
131 we support it.
133 The `INSTRUCTION' field may also contain some pseudo-opcodes: see
134 the section on pseudo-opcodes for details.
136 `OPERANDS' can be nonexistent, or huge, depending on the
137 instruction, of course. When operands are registers, they are given
138 simply as register names: `eax', `ss', `di' for example. NASM does
139 _not_ use the GAS syntax, in which register names are prefixed by a
140 `%' sign. Operands may also be effective addresses, or they may be
141 constants or expressions. See the separate sections on these for
142 details.
144 `COMMENT' is anything after the first semicolon on the line,
145 excluding semicolons inside quoted strings.
147 Of course, all these fields are optional: the presence or absence of
148 the OPERANDS field is required by the nature of the INSTRUCTION
149 field, but any line may contain a LABEL or not, may contain an
150 INSTRUCTION or not, and may contain a COMMENT or not, independently
151 of each other.
153 Lines may also contain nothing but a directive: see `Assembler
154 Directives' below for details.
156 NASM can currently not handle any line longer than 1024 characters.
157 This may be fixed in a future release.
159 Floating Point Instructions
160 ===========================
162 NASM has support for assembling FPU opcodes. However, its syntax is
163 not necessarily the same as anyone else's.
165 NASM uses the notation `st0', `st1', etc. to denote the FPU stack
166 registers. NASM also accepts a wide range of single-operand and
167 two-operand forms of the instructions. For people who wish to use
168 the single-operand form exclusively (this is in fact the `canonical'
169 form from NASM's point of view, in that it is the form produced by
170 the Netwide Disassembler), there is a TO keyword which makes
171 available the opcodes which cannot be so easily accessed by one
172 operand. Hence:
174         fadd st1        ; this sets st0 := st0 + st1
175         fadd st0,st1    ; so does this
176         fadd st1,st0    ; this sets st1 := st1 + st0
177         fadd to st1     ; so does this
179 It's also worth noting that the FPU instructions that reference
180 memory must use the prefixes DWORD, QWORD or TWORD to indicate what
181 size of memory operand they refer to.
183 NASM, in keeping with our policy of not trying to second-guess the
184 programmer, will _never_ automatically insert WAIT instructions into
185 your code stream. You must code WAIT yourself before _any_
186 instruction that needs it. (Of course, on 286 processors or above,
187 it isn't needed anyway...)
189 NASM supports specification of floating point constants by means of
190 `dd' (single precision), `dq' (double precision) and `dt' (extended
191 precision). Floating-point _arithmetic_ is not done, due to
192 portability constraints (not all platforms on which NASM can be run
193 support the same floating point types), but simple constants can be
194 specified. For example:
196 gamma   dq 0.5772156649         ; Euler's constant
198 Pseudo-Opcodes
199 ==============
201 Pseudo-opcodes are not real x86 machine opcodes, but are used in the
202 instruction field anyway because that's the most convenient place to
203 put them. The current pseudo-opcodes are DB, DW, DD, DQ and DT,
204 their uninitialised counterparts RESB, RESW, RESD, RESQ and REST,
205 the INCBIN command, the EQU command, and the TIMES prefix.
207 DB, DW, DD, DQ and DT work as you would expect: they can each take
208 an arbitrary number of operands, and when assembled, they generate
209 nothing but those operands. All three of them can take string
210 constants as operands. See the `Constants' section for details about
211 string constants.
213 RESB, RESW, RESD, RESQ and REST are designed to be used in the BSS
214 section of a module: they declare _uninitialised_ storage space.
215 Each takes a single operand, which is the number of bytes, words or
216 doublewords to reserve. We do not support the MASM/TASM syntax of
217 reserving uninitialised space by writing `DW ?' or similar: this is
218 what we do instead. (But see `Critical Expressions' for a caveat on
219 the nature of the operand.)
221 (An aside: if you want to be able to write `DW ?' and have something
222 vaguely useful happen, you can always code `? EQU 0'...)
224 INCBIN is borrowed from the old Amiga assembler Devpac: it includes
225 a binary file verbatim into the output file. This can be handy for
226 (for example) including graphics and sound data directly into a game
227 executable file. It can be called in one of these three ways:
229     INCBIN "file.dat"               ; include the whole file
230     INCBIN "file.dat",1024          ; skip the first 1024 bytes
231     INCBIN "file.dat",1024,512      ; skip the first 1024, and
232                                     ; actually include at most 512
234 EQU defines a symbol to a specified value: when EQU is used, the
235 LABEL field must be present. The action of EQU is to define the
236 given label name to the value of its (only) operand. This definition
237 is absolute, and cannot change later. So, for example,
239 message db 'hello, world'
240 msglen  equ $-message
242 defines `msglen' to be the constant 12. `msglen' may not then be
243 redefined later. This is not a preprocessor definition either: the
244 value of `msglen' is evaluated _once_, using the value of `$' (see
245 the section `Expressions' for details of `$') at the point of
246 definition, rather than being evaluated wherever it is referenced
247 and using the value of `$' at the point of reference. Note that the
248 caveat in `Critical Expressions' applies to EQU too, at the moment.
250 Finally, the TIMES prefix causes the instruction to be assembled
251 multiple times. This is partly NASM's equivalent of the DUP syntax
252 supported by MASM-compatible assemblers, in that one can do
254 zerobuf: times 64 db 0
256 or similar, but TIMES is more versatile than that. TIMES takes not
257 just a numeric constant, but a numeric _expression_, so one can do
258 things like
260 buffer: db 'hello, world'
261         times 64-$+buffer db ' '
263 which will store exactly enough spaces to make the total length of
264 `buffer' up to 64. (See the section `Critical Expressions' for a
265 caveat on the use of TIMES.) Finally, TIMES can be applied to
266 ordinary opcodes, so you can code trivial unrolled loops in it:
268         times 100 movsb
270 Note that there is no effective difference between `times 100 resb
271 1' and `resb 100', except that the latter will be assembled about
272 100 times faster due to the internal structure of the assembler.
274 Effective Addresses
275 ===================
277 NASM's addressing scheme is very simple, although it can involve
278 more typing than other assemblers. Where other assemblers
279 distinguish between a _variable_ (label declared without a colon)
280 and a _label_ (declared with a colon), and use different means of
281 addressing the two, NASM is totally consistent.
283 To refer to the contents of a memory location, square brackets are
284 required. This applies to simple variables, computed offsets,
285 segment overrides, effective addresses - _everything_. E.g.:
287 wordvar dw 123
288         mov ax,[wordvar]
289         mov ax,[wordvar+1]
290         mov ax,[es:wordvar+bx]
292 NASM does _not_ support the various strange syntaxes used by MASM
293 and others, such as
295         mov ax,wordvar  ; this is legal, but means something else
296         mov ax,es:wordvar[bx]   ; not even slightly legal
297         es mov ax,wordvar[1]    ; the prefix is OK, but not the rest
299 If no square brackets are used, NASM interprets label references to
300 mean the address of the label. Hence there is no need for MASM's
301 OFFSET keyword, but
303         mov ax,wordvar
305 loads AX with the _address_ of the variable `wordvar'.
307 More complicated effective addresses are handled by enclosing them
308 within square brackets as before:
310         mov eax,[ebp+2*edi+offset]
311         mov ax,[bx+di+8]
313 NASM will cope with some fairly strange effective addresses, if you
314 try it: provided your effective address expression evaluates
315 _algebraically_ to something that the instruction set supports, it
316 will be able to assemble it. For example,
318         mov eax,[ebx*5]         ; actually assembles to [ebx+ebx*4]
319         mov ax,[bx-si+2*si]     ; actually assembles to [bx+si]
321 will both work.
323 There is an ambiguity in the instruction set, which allows two forms
324 of 32-bit effective address with equivalent meaning:
326         mov eax,[2*eax+0]
327         mov eax,[eax+eax]
329 These two expressions clearly refer to the same address. The
330 difference is that the first one, if assembled `as is', requires a
331 four-byte offset to be stored as part of the instruction, so it
332 takes up more space. NASM will generate the second (smaller) form
333 for both of the above instructions, in an effort to save space.
334 There is not, currently, any means for forcing NASM to generate the
335 larger form of the instruction.
337 Mixing 16 and 32 Bit Code: Unusual Instruction Sizes
338 ====================================================
340 A number of assemblers seem to have trouble assembling instructions
341 that use a different operand or address size from the one they are
342 expecting; as86 is a good example, even though the Linux kernel boot
343 process (which is assembled using as86) needs several such
344 instructions and as86 can't do them.
346 Instructions such as `mov eax,2' in 16-bit mode are easy, of course,
347 and NASM can do them just as well as any other assembler. The
348 difficult instructions are things like far jumps.
350 Suppose you are in a 16-bit segment, in protected mode, and you want
351 to execute a far jump to a point in a 32-bit segment. You need to
352 code a 32-bit far jump in a 16-bit segment; not many assemblers I
353 know of will easily support this. NASM can, by means of the `word'
354 and `dword' specifiers. So you can code
356         call 1234h:5678h        ; this uses the default segment size
357         call word 1234h:5678h   ; this is guaranteed to be 16-bit
358         call dword 1234h:56789ABCh ; and this is guaranteed 32-bit
360 and NASM will generate correct code for them.
362 Similarly, if you are coding in a 16-bit code segment, but trying to
363 access memory in a 32-bit data segment, your effective addresses
364 will want to be 32-bit. Of course as soon as you specify an
365 effective address containing a 32-bit register, like `[eax]', the
366 addressing is forced to be 32-bit anyway. But if you try to specify
367 a simple offset, such as `[label]' or `[0x10000]', you will get the
368 default address size, which in this case will be wrong. However,
369 NASM allows you to code `[dword 0x10000]' to force a 32-bit address
370 size, or conversely `[word wlabel]' to force 16 bits.
372 Be careful not to confuse `word' and `dword' _inside_ the square
373 brackets with _outside_: consider the instruction
375         mov word [dword 0x123456],0x7890
377 which moves 16 bits of data to an address specified by a 32-bit
378 offset. There is no contradiction between the `word' and `dword' in
379 this instruction, since they modify different aspects of the
380 functionality. Or, even more confusingly,
382         call dword far [fs:word 0x4321]
384 which takes an address specified by a 16-bit offset, and extracts a
385 48-bit DWORD FAR pointer from it to call.
387 Using this effective-address syntax, the `dword' or `word' override
388 may come before or after the segment override if any: NASM isn't
389 fussy. Hence:
391         mov ax,[fs:dword 0x123456]
392         mov ax,[dword fs:0x123456]
394 are equivalent forms, and generate the same code.
396 The LOOP instruction comes in strange sizes, too: in a 16-bit
397 segment it uses CX as its count register by default, and in a 32-bit
398 segment it uses ECX. But it's possible to do either one in the other
399 segment, and NASM will cope by letting you specify the count
400 register as a second operand:
402         loop label              ; uses CX or ECX depending on mode
403         loop label,cx           ; always uses CX
404         loop label,ecx          ; always uses ECX
406 Finally, the string instructions LODSB, STOSB, MOVSB, CMPSB, SCASB,
407 INSB, and OUTSB can all have strange address sizes: typically, in a
408 16-bit segment they read from [DS:SI] and write to [ES:DI], and in a
409 32-bit segment they read from [DS:ESI] and write to [ES:EDI].
410 However, this can be changed by the use of the explicit address-size
411 prefixes `a16' and `a32'. These prefixes generate null code if used
412 in the same size segment as they specify, but generate an 0x67
413 prefix otherwise. Hence `a16' generates no code in a 16-bit segment,
414 but 0x67 in a 32-bit one, and vice versa. So `a16 lodsb' will always
415 generate code to read a byte from [DS:SI], no matter what the size
416 of the segment. There are also explicit operand-size override
417 prefixes, `o16' and `o32', which will optionally generate 0x66
418 bytes, but these are provided for completeness and should never have
419 to be used. (Note that NASM does not support the LODS, STOS, MOVS
420 etc. forms of the string instructions.)
422 Constants
423 =========
425 NASM can accept three kinds of constant: _numeric_, _character_ and
426 _string_ constants.
428 Numeric constants are simply numbers. NASM supports a variety of
429 syntaxes for expressing numbers in strange bases: you can do any of
431         100     ; this is decimal
432         0x100   ; hex
433         100h    ; hex as well
434         $100    ; hex again
435         100q    ; octal
436         100b    ; binary
438 NASM does not support A86's syntax of treating anything with a
439 leading zero as hex, nor does it support the C syntax of treating
440 anything with a leading zero as octal. Leading zeros make no
441 difference to NASM. (Except that, as usual, if you have a hex
442 constant beginning with a letter, and you want to use the trailing-H
443 syntax to represent it, you have to use a leading zero so that NASM
444 will recognise it as a number instead of a label.)
446 The `x' in `0x100', and the trailing `h', `q' and `b', may all be
447 upper case if you want.
449 Character constants consist of up to four characters enclosed in
450 single or double quotes. No escape character is defined for
451 including the quote character itself: if you want to declare a
452 character constant containing a double quote, enclose it in single
453 quotes, and vice versa.
455 Character constants' values are worked out in terms of a
456 little-endian computer: if you code
458         mov eax,'abcd'
460 then if you were to examine the binary output from NASM, it would
461 contain the visible string `abcd', which of course means that the
462 actual value loaded into EAX would be 0x64636261, not 0x61626364.
464 String constants are like character constants, only more so: if a
465 character constant appearing as operand to a DB, DW or DD is longer
466 than the word size involved (1, 2 or 4 respectively), it will be
467 treated as a string constant instead, which is to say the
468 concatenation of separate character constants.
470 For example,
472         db 'hello, world'
474 declares a twelve-character string constant. And
476         dd 'dontpanic'
478 (a string constant) is equivalent to writing
480         dd 'dont','pani','c'
482 (three character constants), so that what actually gets assembled is
483 equivalent to
485         db 'dontpanic',0,0,0
487 (It's worth noting that one of the reasons for the reversal of
488 character constants is so that the instruction `dw "ab"' has the
489 same meaning whether "ab" is treated as a character constant or a
490 string constant. Hence there is less confusion.)
492 Expressions
493 ===========
495 Expressions in NASM can be formed of the following operators: `|'
496 (bitwise OR), `^' (bitwise XOR), `&' (bitwise AND), `<<' and `>>'
497 (logical bit shifts), `+', `-', `*' (ordinary addition, subtraction
498 and multiplication), `/', `%' (unsigned division and modulo), `//',
499 `%%' (signed division and modulo), `~' (bitwise NOT), and the
500 operators SEG and WRT (see `SEG and WRT' below).
502 The order of precedence is:
504 |                       lowest
507 << >>
508 binary + and -
509 * / % // %%
510 unary + and -, ~, SEG   highest
512 As usual, operators within a precedence level associate to the left
513 (i.e. `2-3-4' evaluates the same way as `(2-3)-4').
515 A form of algebra is done by NASM when evaluating expressions: I
516 have already stated that an effective address expression such as
517 `[EAX*6-EAX]' will be recognised by NASM as algebraically equivalent
518 to `[EAX*4+EAX]', and assembled as such. In addition, algebra can be
519 done on labels as well: `label2*2-label1' is an acceptable way to
520 define an address as far beyond `label2' as `label1' is before it.
521 (In less algebraically capable assemblers, one might have to write
522 that as `label2 + (label2-label1)', where the value of every
523 sub-expression is either a valid address or a constant. NASM can of
524 course cope with that version as well.)
526 Expressions may also contain the special token `$', known as a Here
527 token, which always evaluates to the address of the current assembly
528 point. (That is, the address of the assembly point _before_ the
529 current instruction gets assembled.) The special token `$$'
530 evaluates to the address of the beginning of the current section;
531 this can be used for alignment, as shown below:
533         times ($$-$) & 3 nop    ; pad with NOPs to 4-byte boundary
535 Note that this technique aligns to a four-byte boundary with respect
536 to the beginning of the _segment_; if you can't guarantee that the
537 segment itself begins on a four-byte boundary, this alignment is
538 useless or worse. Be sure you know what kind of alignment you can
539 guarantee to get out of your linker before you start trying to use
540 TIMES to align to page boundaries. (Of course, the OBJ file format
541 can happily cope with page alignment, provided you specify that
542 segment attribute.)
544 SEG and WRT
545 ===========
547 NASM contains the capability for its object file formats (currently,
548 only `obj' makes use of this) to permit programs to directly refer
549 to the segment-base values of their segments. This is achieved
550 either by the object format defining the segment names as symbols
551 (`obj' does this), or by the use of the SEG operator.
553 SEG is a unary prefix operator which, when applied to a symbol
554 defined in a segment, will yield the segment base value of that
555 segment. (In `obj' format, symbols defined in segments which are
556 grouped are considered to be primarily a member of the _group_, not
557 the segment, and the return value of SEG reflects this.)
559 SEG may be used for far pointers: it is guaranteed that for any
560 symbol `sym', using the offset `sym' from the segment base `SEG sym'
561 yields a correct pointer to the symbol. Hence you can code a far
562 call by means of
564         CALL SEG routine:routine
566 or store a far pointer in a data segment by
568         DW routine, SEG routine
570 For convenience, NASM supports the forms
572         CALL FAR routine
573         JMP FAR routine
575 as direct synonyms for the canonical syntax
577         CALL SEG routine:routine
578         JMP SEG routine:routine
580 No alternative syntax for
582         DW routine, SEG routine
584 is supported.
586 Simply referring to `sym', for some symbol, will return the offset
587 of `sym' from its _preferred_ segment base (as returned from `SEG
588 sym'); sometimes, you may want to obtain the offset of `sym' from
589 some _other_ segment base. (E.g. the offset of `sym' from the base
590 of the segment it's in, where normally you'd get the offset from a
591 group base). This is accomplished using the WRT (With Reference To)
592 keyword: if `sym' is defined in segment `seg' but you want its
593 offset relative to the beginning of segment `seg2', you can do
595         mov ax,sym WRT seg2
597 The right-hand operand to WRT must be a segment-base value. You can
598 also do `sym WRT SEG sym2' if you need to.
600 Critical Expressions
601 ====================
603 NASM is a two-pass assembler: it goes over the input once to
604 determine the location of all the symbols, then once more to
605 actually generate the output code. Most expressions are
606 non-critical, in that if they contain a forward reference and hence
607 their correct value is unknown during the first pass, it doesn't
608 matter. However, arguments to RESB, RESW and RESD, and the argument
609 to the TIMES prefix, can actually affect the _size_ of the generated
610 code, and so it is critical that the expression can be evaluated
611 correctly on the first pass. So in these situations, expressions may
612 not contain forward references. This prevents NASM from having to
613 sort out a mess such as
615         times (label-$) db 0
616 label:  db 'where am I?'
618 in which the TIMES argument could equally legally evaluate to
619 _anything_, or perhaps even worse,
621         times (label-$+1) db 0
622 label:  db 'NOW where am I?'
624 in which any value for the TIMES argument is by definition invalid.
626 Since NASM is a two-pass assembler, this criticality condition also
627 applies to the argument to EQU. Suppose, if this were not the case,
628 we were to have the setup
630   mov ax,a
631 a equ b
634 On pass one, `a' cannot be defined properly, since `b' is not known
635 yet. On pass two, `b' is known, so line two can define `a' properly.
636 Unfortunately, line 1 needed `a' to be defined properly, so this
637 code will not assemble using only two passes.
639 There's a related issue: in an effective address such as
640 `[eax+offset]', the value of `offset' can be stored as either 1 or 4
641 bytes. NASM will use the one-byte form if it knows it can, to save
642 space, but will therefore be fooled by the following:
644   mov eax,[ebx+offset]
645 offset equ 10
647 In this case, although `offset' is a small value and could easily
648 fit into the one-byte form of the instruction, when NASM sees the
649 instruction in the first pass it doesn't know what `offset' is, and
650 for all it knows `offset' could be a symbol requiring relocation. So
651 it will allocate the full four bytes for the value of `offset'. This
652 can be solved by defining `offset' before it's used.
654 Local Labels
655 ============
657 NASM takes its local label scheme mainly from the old Amiga
658 assembler Devpac: a local label is one that begins with a period.
659 The `localness' comes from the fact that local labels are associated
660 with the previous non-local label, so that you may declare the same
661 local label twice if a non-local one intervenes. Hence:
663 label1  ; some code
664 .loop   ; some more code
665         jne .loop
666         ret
667 label2  ; some code
668 .loop   ; some more code
669         jne .loop
670         ret
672 In the above code, each `jne' instruction jumps to the line of code
673 before it, since the `.loop' labels are distinct from each other.
675 NASM, however, introduces an extra capability not present in Devpac,
676 which is that the local labels are actually _defined_ in terms of
677 their associated non-local label. So if you really have to, you can
678 write
680 label3  ; some more code
681         ; and some more
682         jmp label1.loop
684 So although local labels are _usually_ local, it is possible to
685 reference them from anywhere in your program, if you really have to.
687 Assembler Directives
688 ====================
690 Assembler directives appear on a line by themselves (apart from a
691 comment). They come in two forms: user-level directives and
692 primitive directives. Primitive directives are enclosed in square
693 brackets (no white space may appear before the opening square
694 bracket, although white space and a comment may come after the
695 closing bracket), and were the only form of directive supported by
696 earlier versions of NASM. User-level directives look the same, only
697 without the square brackets, and are the more modern form. (They are
698 implemented as macros expanding to primitive directives.) There is a
699 distinction in functionality, which is explained below in the
700 section on structures.
702 Some directives are universal: they may be used in any situation,
703 and do not change their syntax. The universal directives are listed
704 below.
706 `BITS 16' or `BITS 32' switches NASM into 16-bit or 32-bit mode.
707 (This is equivalent to USE16 and USE32 segments, in TASM or MASM.)
708 In 32-bit mode, instructions are prefixed with 0x66 or 0x67 prefixes
709 when they use 16-bit data or addresses; in 16-bit mode, the reverse
710 happens. NASM's default depends on the object format; the defaults
711 are documented with the formats. (See `obj', in particular, for some
712 unusual behaviour.)
714 `SECTION name' or `SEGMENT name' changes which section the code you
715 write will be assembled into. Acceptable section names vary between
716 output formats, but most formats (indeed, all formats at the moment)
717 support the names `.text', `.data' and `.bss'. Note that `.bss' is
718 an uninitialised data section, and so you will receive a warning
719 from NASM if you try to assemble any code or data in it. The only
720 thing you can do in `.bss' without triggering a warning is to use
721 RESB, RESW and RESD. That's what they're for.
723 `ABSOLUTE address' can be considered a different form of `SECTION',
724 in that it must be overridden using a SECTION directive once you
725 have finished using it. It is used to assemble notional code at an
726 absolute offset address; of course, you can't actually assemble
727 _code_ there, since no object file format is capable of putting the
728 code in place, but you can use RESB, RESW and RESD, and you can
729 define labels. Hence you could, for example, define a C-like data
730 structure by means of
732                 absolute 0
733         stLong  resd 1
734         stWord  resw 1
735         stByte1 resb 1
736         stByte2 resb 1
737         st_size:
738                 segment .text
740 and then carry on coding. This defines `stLong' to be zero, `stWord'
741 to be 4, `stByte1' to be 6, `stByte2' to be 7 and `st_size' to be 8.
742 So this has defined a data structure. The STRUC directive provides a
743 nicer way to do this: see below.
745 `EXTERN symbol' defines a symbol as being `external', in the C
746 sense: `EXTERN' states that the symbol is _not_ declared in this
747 module, but is declared elsewhere, and that you wish to _reference_
748 it in this module.
750 `GLOBAL symbol' defines a symbol as being global, in the sense that
751 it is exported from this module and other modules may reference it.
752 All symbols are local, unless declared as global. Note that the
753 `GLOBAL' directive must appear before the definition of the symbol
754 it refers to.
756 `COMMON symbol size' defines a symbol as being common: it is
757 declared to have the given size, and it is merged at link time with
758 any declarations of the same symbol in other modules. This is not
759 _fully_ supported in the `obj' file format: see the section on `obj'
760 for details.
762 `STRUC structure' begins the definition of a data structure, and
763 `ENDSTRUC' ends it. The structure shown above may be defined,
764 exactly equivalently, using STRUC as follows:
766                 struc st
767         stLong  resd 1
768         stWord  resw 1
769         stByte1 resb 1
770         stByte2 resb 1
771                 endstruc
773 Notice that this code still defines the symbol `st_size' to be the
774 size of the structure. The `_size' suffix is automatically appended
775 to the structure name. Notice also that the assembler takes care of
776 remembering which section you were assembling in (whereas in the
777 version using `ABSOLUTE' it was up to the programmer to sort that
778 out).
780 This is where user-level directives differ from primitives: the
781 `SECTION' (and `SEGMENT') user-level directives don't just call the
782 primitive versions, but they also `%define' the special preprocessor
783 symbol `__SECT__' to be the primitive directive that specifies the
784 current section. So the `ENDSTRUC' directive can remember what
785 section the assembly was directed to before the structure definition
786 began. For this reason, there is no primitive version of STRUC or
787 ENDSTRUC - they are implemented in terms of ABSOLUTE and SECTION.
788 This also means that if you use STRUC before explicitly announcing a
789 target section, you should explicitly announce one after ENDSTRUC.
791 The primitive directive [INCLUDE filename] (or the equivalent form
792 [INC filename]) is supported as a synonym for the preprocessor-
793 oriented `%include' form, but only temporarily: this usage will be
794 phased out in the next version of NASM.
796 Directives may also be specific to the output file format. At
797 present, the `bin' and `obj' formats define extra directives, which
798 are specified below.
800 The Preprocessor
801 ================
803 NASM contains a full-featured macro preprocessor, which supports
804 conditional assembly, multi-level file inclusion, two forms of macro
805 (single-line and multi-line), and a `context stack' mechanism for
806 extra macro power. Preprocessor directives all begin with a `%'
807 sign.
809 Single-line macros
810 ------------------
812 Single-line macros are defined in a similar way to C, using the
813 `%define' command. Hence you can do:
815    %define ctrl 0x1F &
816    %define param(a,b) ((a)+(a)*(b))
817      mov byte [param(2,ebx)], ctrl 'D'
819 which will expand to
821      mov byte [(2)+(2)*(ebx)], 0x1F & 'D'
823 When the expansion of a single-line macro contains tokens which
824 invoke another macro, the expansion is performed at invocation time,
825 not at definition time. Thus the code
827    %define a(x) 1+b(x)
828    %define b(x) 2*x
829      mov ax,a(8)
831 will evaluate in the expected way to `mov ax,1+2*8', even though the
832 macro `b' wasn't defined at the time of definition of `a'.
834 Macros defined with `%define' are case sensitive: after `%define foo
835 bar', only `foo' will expand to bar: `Foo' or `FOO' will not. By
836 using `%idefine' instead of `%define' (the `i' stands for
837 `insensitive') you can define all the case variants of a macro at
838 once, so that `%idefine foo bar' would cause `foo', `Foo' and `FOO'
839 all to expand to `bar'.
841 There is a mechanism which detects when a macro call has occurred as
842 a result of a previous expansion of the same macro, to guard against
843 circular references and infinite loops. If this happens, the
844 preprocessor will report an error.
846 Single-line macros with parameters can be overloaded: it is possible
847 to define two or more single-line macros with the same name, each
848 taking a different number of parameters, and the macro processor
849 will be able to distinguish between them. However, a parameterless
850 single-line macro excludes the possibility of any macro of the same
851 name _with_ parameters, and vice versa (though single-line macros
852 may be redefined, keeping the same number of parameters, without
853 error).
855 Multiple-line macros
856 --------------------
858 These are defined using `%macro' and `%endmacro', so that simple things
859 like this can be done:
861    %macro prologue 0
862      push ebp
863      mov ebp,esp
864    %endmacro
866 This defines `prologue' to be a multi-line macro, taking no
867 parameters, which expands to the two lines of code given.
869 Similarly to single-line macros, multi-line macros are case-
870 sensitive, unless you define them using `%imacro' instead of
871 `%macro'.
873 The `0' on the `%macro' line indicates that the macro `prologue'
874 expects no parameters. Macros can be overloaded: if two macros are
875 defined with the same name but different numbers of parameters, they
876 will be treated as separate. Multi-line macros may not be redefined.
878 Macros taking parameters can be written using `%1', `%2' and so on
879 to reference the parameters. So this code
881    %macro movs 2
882      push %2
883      pop %1
884    %endmacro
885      movs ds,cs
887 will define a macro `movs' to perform an effective MOV operation
888 from segment to segment register. The macro call given would of
889 course expand to `push cs' followed by `pop ds'.
891 You can define a label inside a macro in such a way as to make it
892 unique to that macro call (so that repeated calls to the same macro
893 won't produce multiple labels with the same name), by prefixing it
894 with `%%'. So:
896    %macro retz
897      jnz %%skip
898      ret
899    %%skip:
900    %endmacro
902 This defines a different label in place of `%%skip' every time it's
903 called. (Of course the above code could have easily been coded using
904 `jnz $+3', but not in more complex cases...) The actual label
905 defined would be `macro.2345.skip', where 2345 is replaced by some
906 number that changes with each macro call. Users are warned to avoid
907 defining labels of this shape themselves.
909 Sometimes you want a macro to be able to accept arbitrarily many
910 parameters and lump them into one. This can be done using the `+'
911 modifier on the `%macro' line:
913    %macro fputs 2+
914    [section .data] ; this is done as a primitive to avoid
915                    ; disturbing the __SECT__ define
916    %%str db %2
917    %%end:
918    __SECT__        ; this expands to a whole [section xxx] primitive
919      mov dx,%%str
920      mov cx,%%end-%%str
921      mov bx,%1
922      call writefile
923    %endmacro
924      fputs [filehandle], "hi there", 13, 10
926 This declares `pstring' to be a macro that accepts _at least two_
927 parameters, and all parameters after the first one are lumped
928 together as part of the last specified one (in this case %2). So in
929 the macro call, `%1' expands to `[filehandle]' while `%2' expands to
930 the whole remainder of the line: `"hi there", 13, 10'. Note also the
931 switching of sections in the middle of this macro expansion, to
932 ensure separation of data and code.
934 There is an alternative mechanism for putting commas in macro
935 parameters: instead of specifying the large-parameter-ness at macro
936 definition time, you can specify it at macro call time, by the use
937 of braces to surround a parameter which you want to contain commas.
940    %macro table_entry 2
941    %%start:
942      db %1
943      times 32-($-%%start) db 0
944      db %2
945      times 64-($-%%start) db 0
946    %endmacro
947      table_entry 'foo','bar'
948      table_entry 'megafoo', { 27,'[1mBAR!',27,'[m' }
950 will expand to, effectively (actually, there will be labels present,
951 but these have been omitted for clarity), the following:
953      db 'foo'
954      times 32-3 db 0
955      db 'bar'
956      times 64-35 db 0
957      db 'megafoo'
958      times 32-7 db 0
959      db 27,'[1mBAR!',27,'[m'
960      times 64-46 db 0
962 Macro parameter expansions can be concatenated on to other tokens,
963 so that you can do this:
965    %macro keytab_entry 2
966    keypos%1 equ $-keytab
967      db %2
968    %endmacro
969    keytab:
970      keytab_entry F1,128+1
971      keytab_entry F2,128+2
972      keytab_entry Return,13
974 which will define labels called `keyposF1', `keyposF2' and
975 `keyposReturn'. You can similarly do concatenations on the other
976 end, such as `%1foo'. If you need to concatenate a digit on to the
977 end of a macro parameter expansion, you can do this by enclosing the
978 parameter number in braces: `%{1}' is always a valid synonym for
979 `%1', and has the advantage that it can be legitimately prepended to
980 a digit, as in `%{1}2', and cause no confusion with `%{12}'.
981 Macro-specific labels and defines can be concatenated similarly:
982 `%{%foo}bar' will succeed where `%%foobar' would cause confusion.
983 (As it happens, `%%foobar' would work anyway, due to the format of
984 macro-specific labels, but for clarity, `%{%foo}bar' is recommended
985 if you _really_ want to do anything this perverse...)
987 The parameter handling has a special case: it can treat a macro
988 parameter specially if it's thought to contain a condition code. The
989 reference `%+1' is identical to `%1' except that it will perform an
990 initial sanity check to see if the parameter in question is a
991 condition code; more usefully, the reference `%-1' will produce the
992 _opposite_ condition code to the one specified in the parameter.
993 This allows for things such as a conditional-MOV macro to be
994 defined:
996    %macro movc 3
997      j%-1 %%skip
998      mov %2,%3
999    %%skip:
1000    %endmacro
1001      movc ae,ax,bx
1003 which will expand to something like
1005      jnae macro.1234.skip
1006      mov ax,bx
1007    macro.1234.skip:
1009 Note that `%+1' will allow CXZ or ECXZ to be passed as condition
1010 codes, but `%-1' will of course be unable to invert them.
1012 Parameters can also be defaulted: you can define a macro which, for
1013 example, said
1015    %macro strange 1-3 bx,3
1016      < some expansion text >
1017    %endmacro
1019 This macro takes between 1 and 3 parameters (inclusive); if
1020 parameter 2 is not specified it defaults to BX, and if parameter 3
1021 is not specified it defaults to 3. So the calls
1023      strange dx,si,di
1024      strange dx,si
1025      strange dx
1027 would be equivalent to
1029      strange dx,si,di
1030      strange dx,si,3
1031      strange dx,bx,3
1033 Defaults may be omitted, in which case they are taken to be blank.
1035 `%endm' is a valid synonym for `%endmacro'.
1037 Conditional Assembly
1038 --------------------
1040 Similarly to the C preprocessor, the commands `%ifdef' and `%endif'
1041 may be used to bracket a section of code, which will then only be
1042 assembled if at least one of the identifiers following `%ifdef' is
1043 defined as a single-line macro. The command `%ifndef' has opposite
1044 sense to `%ifdef', and `%else' can be placed between the `%if' and
1045 the `%endif' to work as expected. Since there is no analogue to C's
1046 `#if', there is no precise `elif' directive, but `%elifdef' and
1047 `%elifndef' work as expected.
1049 There is another family of `%if' constructs: `%ifctx', `%ifnctx',
1050 `%elifctx' and `%elifnctx', which operate on the context stack
1051 (described below).
1053 File Inclusion
1054 --------------
1056 You can include a file using the `%include' directive. Included
1057 files are only searched for in the current directory: there isn't
1058 (yet - if there's demand for it it could be arranged) any default
1059 search path for standard include files.
1061 This, again, works like C: `%include' is used to include a file. Of
1062 course it's quite likely you'd want to do the normal sort of thing
1063 inside the file:
1065    %ifndef MY_MACROS_FILE
1066    %define MY_MACROS_FILE
1067      < go and define some macros >
1068    %endif
1070 and then elsewhere
1072    %include "my-macros-file"
1073      < some code making use of the macros >
1075 so that it doesn't matter if the file accidentally gets included
1076 more than once.
1078 The Context Stack
1079 -----------------
1081 This is a feature which adds a whole extra level of power to NASM's
1082 macro capability. The context stack is an internal object within the
1083 preprocessor, which holds a stack of `contexts'. Each context has a
1084 name - just an identifier-type token - and can also have labels and
1085 `%define' macros associated with it. Other macros can manipulate the
1086 context stack: this is where the power comes in.
1088 To start with: the preprocessor command `%push' will create a new
1089 context with the given name, and push it on to the top of the stack.
1090 `%pop', taking no arguments, pops the top context off the stack and
1091 destroys it. `%repl' renames the top context without destroying any
1092 associated labels or macros, so it's distinct from doing `%pop'
1093 followed by `%push'. Finally, `%ifctx' and `%ifnctx' invoke
1094 conditional assembly based on the name of the top context. (The
1095 alternative forms `%elifctx' and `%elifnctx' are also available.)
1097 As well as the `%%foo' syntax to define labels specific to a macro
1098 call, there is also the syntax `%$foo' to define a label specific to
1099 the context currently on top of the stack. `%$$foo' can be used to
1100 refer to the context below that, or `%$$$foo' below that, and so on.
1102 This lot allows the definition of macro combinations that enclose
1103 other code, such as the following big example:
1105    %macro if 1
1106      %push if
1107      j%-1 %$ifnot
1108    %endmacro
1109    %macro else 0
1110      %ifctx if
1111        %repl else
1112        jmp %$ifend
1113        %$ifnot:
1114      %else
1115        %error "expected `if' before `else'"
1116      %endif
1117    %endmacro
1118    %macro endif 0
1119      %ifctx if
1120        %$ifnot:
1121        %pop
1122      %elifctx else
1123        %$ifend:
1124        %pop
1125      %else
1126        %error "expected `if' or `else' before `endif'"
1127      %endif
1128    %endmacro
1130 This will cope with a large `if/endif' construct _or_ an
1131 `if/else/endif', without flinching. So you can code:
1133      cmp ax,bx
1134      if ae
1135        cmp bx,cx
1136        if ae
1137          mov ax,cx
1138        else
1139          mov ax,bx
1140        endif
1141      else
1142        cmp ax,cx
1143        if ae
1144          mov ax,cx
1145        endif
1146      endif
1148 which will place the smallest out of AX, BX and CX into AX. Note the
1149 use of `%repl' to change the current context from `if' to `else'
1150 without disturbing the associated labels `%$ifend' and `%$ifnot';
1151 also note that the stack mechanism allows handling of nested IF
1152 statements without a hitch, and that conditional assembly is used in
1153 the `endif' macro in order to cope with the two possible forms with
1154 and without an `else'. Note also the directive `%error', which
1155 allows the user to report errors on improper invocation of a macro
1156 and so can catch unmatched `endif's at preprocess time.
1158 Output Formats
1159 ==============
1161 The current output formats supported are `bin', `aout', `coff',
1162 `elf', `as86', `obj', `win32', `rdf', and the debug pseudo-format
1163 `dbg'.
1165 `bin': flat-form binary
1166 -----------------------
1168 This is at present the only output format that generates instantly
1169 runnable code: all the others produce object files that need linking
1170 before they become executable.
1172 `bin' output files contain no red tape at all: they simply contain
1173 the binary representation of the exact code you wrote.
1175 The `bin' format supports a format-specific directive, which is ORG.
1176 `ORG addr' declares that your code should be assembled as if it were
1177 to be loaded into memory at the address `addr'. So a DOS .COM file
1178 should state `ORG 0x100', and a DOS .SYS file should state `ORG 0'.
1179 There should be _one_ ORG directive, at most, in an assembly file:
1180 NASM does not support the use of ORG to jump around inside an object
1181 file, like MASM does (see the `Bugs' section for a demonstration of
1182 the use of MASM's form of ORG to do something that NASM's won't do.)
1184 Like almost all formats (not `obj'), the `bin' format defines the
1185 section names `.text', `.data' and `.bss'. The layout is that
1186 `.text' comes first in the output file, followed by `.data', and
1187 notionally followed by `.bss'. So if you declare a BSS section in a
1188 flat binary file, references to the BSS section will refer to space
1189 past the end of the actual file. The `.data' and `.bss' sections are
1190 considered to be aligned on four-byte boundaries: this is achieved
1191 by inserting padding zero bytes between the end of the text section
1192 and the start of the data, if there is data present. Of course if no
1193 SECTION directives are present, everything will go into `.text', and
1194 you will get nothing in the output except the code you wrote.
1196 `bin' silently ignores GLOBAL directives, and will also not complain
1197 at EXTERN ones. You only get an error if you actually _reference_ an
1198 external symbol.
1200 Using the `bin' format, the default output filename is `filename'
1201 for inputs of `filename.asm'. If there is no extension to be
1202 removed, output will be placed in `nasm.out' and a warning will be
1203 generated.
1205 `bin' defaults to 16-bit assembly mode.
1207 `aout' and `elf': Linux object files
1208 ------------------------------------
1210 These two object formats are the ones used under Linux. They have no
1211 format-specific directives, and their default output filename is
1212 `filename.o'.
1214 `aout' defines the three standard sections `.text', `.data' and
1215 `.bss'. `elf' also, defines these three, but in addition it can
1216 support user-defined section names, which can be declared along with
1217 section attributes like this:
1219         section foo align=32 exec
1220         section bar write nobits
1222 The available options are:
1224 - A section can be `progbits' (the default) or `nobits'. `nobits'
1225   sections are BSS: their contents are not stored in the object
1226   file, and the only thing you can sensibly do in one is RESB.
1227   `progbits' are normal sections.
1229 - A section can be `exec' (indicating that it contains executable
1230   code), or `noexec' (the default).
1232 - A section can be `write' (indicating that it should be writable
1233   when linked), or `nowrite' (the default).
1235 - A section can be `alloc' (indicating that its contents should be
1236   loaded into program VM at load time; the default) or `noalloc'
1237   (for storing comments and things that don't form part of the
1238   loaded program).
1240 - You can specify a power of two for the section alignment by
1241   writing `align=64' or similar.
1243 The attributes of the default sections `.text', `.data' and `.bss'
1244 can also be redefined from their defaults. The NASM defaults are:
1246 section .text align=16 alloc exec nowrite progbits
1247 section .data align=4 alloc write noexec progbits
1248 section .bss align=4 alloc write noexec nobits
1250 ELF is a much more featureful object-file format than a.out: in
1251 particular it has enough features to support the writing of position
1252 independent code by means of a global offset table, and position
1253 independent shared libraries by means of a procedure linkage table.
1254 Unfortunately NASM, as yet, does not support these extensions, and
1255 so NASM cannot be used to write shared library code under ELF. NASM
1256 also does not support the capability, in ELF, for specifying precise
1257 alignment constraints on common variables.
1259 Both `aout' and `elf' default to 32-bit assembly mode.
1261 `coff' and `win32': Common Object File Format
1262 ---------------------------------------------
1264 The `coff' format generates standard Unix COFF object files, which
1265 can be fed to (for example) the DJGPP linker. Its default output
1266 filename, like the other Unix formats, is `filename.o'.
1268 The `win32' format generates Microsoft Win32 (Windows 95 or
1269 Intel-platform Windows NT) object files, which nominally use the
1270 COFF standard, but in fact are not compatible. Its default output
1271 filename is `filename.obj'.
1273 `coff' and `win32' are not quite compatible formats, due to the fact
1274 that Microsoft's interpretation of the term `relative relocation'
1275 does not seem to be the same as the interpretation used by anyone
1276 else. It is therefore more correct to state that Win32 uses a
1277 _variant_ of COFF. The object files will not therefore produce
1278 correct output when fed to each other's linkers. (I've tried it!)
1280 In addition to this subtle incompatibility, Win32 also defines
1281 extensions to basic COFF, such as a mechanism for importing symbols
1282 from dynamic-link libraries at load time. NASM may eventually
1283 support this extension in the form of a format-specific directive.
1284 However, as yet, it does not. Neither the `coff' nor `win32' output
1285 formats have any specific directives.
1287 The Microsoft linker also has a small blind spot: it cannot
1288 correctly relocate a relative CALL or JMP to an absolute address.
1289 Hence all PC-relative CALLs or JMPs, when using the `win32' format,
1290 must have targets which are relative to sections, or to external
1291 symbols. You can't do
1292         call 0x123456
1293 _even_ if you happen to know that there is executable code at that
1294 address. The linker simply won't get the reference right; so in the
1295 interests of not generating incorrect code, NASM will not allow this
1296 form of reference to be written to a Win32 object file. (Standard
1297 COFF, or at least the DJGPP linker, seems to be able to cope with
1298 this contingency. Although that may be due to the executable having
1299 a zero load address...)
1301 Note also that Borland Win32 compilers reportedly do not use this
1302 object file format: while Borland linkers will output Win32-COFF
1303 type executables, their object format is the same as the old DOS OBJ
1304 format. So if you are using a Borland compiler, don't use the
1305 `win32' object format, just use `obj' and declare all your segments
1306 as `USE32'.
1308 Both `coff' and `win32' support, in addition to the three standard
1309 section names `.text', `.data' and `.bss', the ability to define
1310 your own sections. Currently (this may change in the future) you can
1311 provide the options `text' (or `code'), `data' or `bss' to determine
1312 the type of section. Win32 also allows `info', which is an
1313 informational section type used by Microsoft C compilers to store
1314 linker directives. So you can do:
1316     section .mysect code    ; defines an extra code section
1318 or maybe, in Win32,
1320     section .drectve info   ; defines an MS-compatible directive section
1321     db '-defaultlib:LIBC -defaultlib:OLDNAMES '
1323 to pass directives to the MS linker.
1325 Both `coff' and `win32' default to 32-bit assembly mode.
1327 `obj': Microsoft 16-bit Object Module Format
1328 --------------------------------------------
1330 The `obj' format generates 16-bit Microsoft object files, suitable
1331 for feeding to 16-bit versions of Microsoft C, and probably
1332 TLINK as well (although that hasn't been tested). The Use32
1333 extensions are supported.
1335 `obj' defines no special segment names: you can call segments what
1336 you like. Unlike the other formats, too, segment names are actually
1337 defined as symbols, so you can write
1339         segment CODE
1340         mov ax,CODE
1342 and get the _segment_ address of the segment, suitable for loading
1343 into a segment register.
1345 Segments can be declared with attributes:
1347         SEGMENT CODE PRIVATE ALIGN=16 CLASS=CODE OVERLAY=OVL2 USE16
1349 You can specify segments to be PRIVATE, PUBLIC, COMMON or STACK;
1350 their alignment may be any power of two from 1 to 256 (although only
1351 1, 2, 4, 16 and 256 are really supported, so anything else gets
1352 rounded up to the next highest one of those); their class and
1353 overlay names may be specified. You may also specify segments to be
1354 USE16 or USE32. The defaults are PUBLIC ALIGN=1, no class, no
1355 alignment, USE16.
1357 You can also specify that a segment is _absolute_ at a certain
1358 segment address:
1360         SEGMENT SCREEN ABSOLUTE=0xB800
1362 The ABSOLUTE and ALIGN keywords are mutually exclusive.
1364 The format-specific directive GROUP allows segment grouping: `GROUP
1365 DGROUP DATA BSS' defines the group DGROUP to contain segments DATA
1366 and BSS.
1368 Segments are defined as part of their group by default: if variable
1369 `var' is declared in segment `data', which is part of group
1370 `dgroup', then the expression `SEG var' is equivalent to the
1371 expression `dgroup', and the expression `var' evaluates to the
1372 offset of the variable `var' relative to the beginning of the group
1373 `dgroup'. You must use the expression `var WRT data' to get the
1374 offset of the variable `var' relative to the beginning of its
1375 _segment_.
1377 NASM allows a segment to be part of more than one group (like A86,
1378 and unlike TASM), but will generate a warning (unlike A86!).
1379 References to the symbols in that segment will be resolved relative
1380 to the _first_ group it is defined in.
1382 The directive `UPPERCASE' causes all symbol, segment and group names
1383 output to the object file to be uppercased. The actual _assembly_ is
1384 still case sensitive.
1386 To avoid getting tangled up in NASM's local label mechanism, segment
1387 and group names have leading periods stripped when they are defined.
1388 Thus, the directive `SEGMENT .text' will define a segment called
1389 `text', which will clash with any other symbol called `text', and
1390 you will _not_ be able to reference the segment base as `.text', but
1391 only as `text'.
1393 Common variables in OBJ files can be `near' or `far': currently,
1394 NASM has a horribly grotty way to support that, which is that if you
1395 specify the common variable's size as negative, it will be near, and
1396 otherwise it will be far. The support isn't perfect: if you declare
1397 a far common variable both in a NASM assembly module and in a C
1398 program, you may well find the linker reports "mismatch in
1399 array-size" or some such. The reason for this is that far common
1400 variables are defined by means of _two_ size constants, which are
1401 multiplied to give the real size. Apparently the Microsoft linker
1402 (at least) likes both constants, not merely their product, to match
1403 up. This may be fixed in a future release.
1405 If the module you're writing is intended to contain the program
1406 entry point, you can declare this by defining the special label
1407 `..start' at the start point, either as a label or by EQU (although
1408 of course the normal caveats about EQU dependency still apply).
1410 `obj' has an unusual handling of assembly modes: instead of having a
1411 global default for the whole file, there is a separate default for
1412 each segment. Thus, each SEGMENT directive carries an implicit BITS
1413 directive with it, which switches to 16-bit or 32-bit mode depending
1414 on whether the segment is a Use16 or Use32 segment. If you want to
1415 place 32-bit code in a Use16 segment, you can use an explicit `BITS
1416 32' override, but if you switch temporarily away from that segment,
1417 you will have to repeat the override after coming back to it.
1419 `as86': Linux as86 (bin86-0.3)
1420 ------------------------------
1422 This output format attempts to replicate the format used to pass
1423 data between the Linux x86 assembler and linker, as86 and ld86. Its
1424 default file name, yet again, is `filename.o'. Its default
1425 segment-size attribute is 16 bits.
1427 `rdf': Relocatable Dynamic Object File Format
1428 ---------------------------------------------
1430 RDOFF was designed initially to test the object-file production
1431 interface to NASM. It soon became apparent that it could be enhanced
1432 for use in serious applications due to its simplicity; code to load
1433 and execute an RDOFF object module is very simple. It also contains
1434 enhancements to allow it to be linked with a dynamic link library at
1435 either run- or load- time, depending on how complex you wish to make
1436 your loader.
1438 The `rdoff' directory in the NASM distribution archive contains
1439 source for an RDF linker and loader to run under Linux.
1441 `rdf' has a default segment-size attribute of 32 bits.
1443 Debugging format: `dbg'
1444 -----------------------
1446 This output format is not built into NASM by default: it's for
1447 debugging purposes. It produces a debug dump of everything that the
1448 NASM assembly module feeds to the output driver, for the benefit of
1449 people trying to write their own output drivers.
1451 Bugs
1452 ====
1454 Apart from the missing features (correct OBJ COMMON support, ELF
1455 alignment, ELF PIC support, etc.), there are no _known_ bugs.
1456 However, any you find, with patches if possible, should be sent to
1457 <jules@dcs.warwick.ac.uk> or <anakin@pobox.com>, and we'll try to
1458 fix them.
1460 Beware of Pentium-specific instructions: Intel have provided a macro
1461 file for MASM, to implement the eight or nine new Pentium opcodes as
1462 MASM macros. NASM does not generate the same code for the CMPXCHG8B
1463 instruction as these macros do: this is due to a bug in the _macro_,
1464 not in NASM. The macro works by generating an SIDT instruction (if I
1465 remember rightly), which has almost exactly the right form, then
1466 using ORG to back up a bit and do a DB over the top of one of the
1467 opcode bytes. The trouble is that Intel overlooked (or MASM syntax
1468 didn't let them allow for) the possibility that the SIDT instruction
1469 may contain an 0x66 or 0x67 operand or address size prefix. If this
1470 happens, the ORG will back up by the wrong amount, and the macro
1471 will generate incorrect code. NASM gets it right. This, also, is not
1472 a bug in NASM, so please don't report it as one. (Also please note
1473 that the ORG directive in NASM doesn't work this way, and so you
1474 can't do equivalent tricks with it...)
1476 That's All Folks!
1477 =================
1479 Enjoy using NASM! Please feel free to send me comments, or
1480 constructive criticism, or bug fixes, or requests, or general chat.
1482 Contributions are also welcome: if anyone knows anything about any
1483 other object file formats I should support, please feel free to send
1484 me documentation and some short example files (in my experience,
1485 documentation is useless without at _least_ one example), or even to
1486 write me an output module. OS/2 object files, in particular, spring
1487 to mind. I don't have OS/2, though.
1489 Please keep flames to a minimum: I have had some very angry e-mails
1490 in the past, condemning me for writing a useless assembler, that
1491 output in no useful format (at the time, that was true), generated
1492 incorrect code (several typos in the instruction table, since fixed)
1493 and took up too much memory and disk space (the price you pay for
1494 total portability, it seems). All these were criticisms I was happy
1495 to hear, but I didn't appreciate the flames that went with them.
1496 NASM _is_ still a prototype, and you use it at your own risk. I
1497 _think_ it works, and if it doesn't then I want to know about it,
1498 but I don't guarantee anything. So don't flame me, please. Blame,
1499 but don't flame.
1501 - Simon Tatham <anakin@pobox.com>, 21-Nov-96