1 The Netwide Assembler, NASM
2 ===========================
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.
21 - AS86 is Linux specific, and (my version at least) doesn't seem to
22 have much (or any) documentation.
24 - MASM isn't very good. And it's expensive. And it runs only under
27 - TASM is better, but still strives for MASM compatibility, which
28 means millions of directives and tons of red tape. And its syntax
29 is essentially MASM's, with the contradictions and quirks that
30 entails (although it sorts out some of those by means of Ideal
31 mode). It's expensive too. And it's DOS only.
33 So here, for your coding pleasure, is NASM. At present it's still in
34 prototype stage - we don't promise that it can outperform any of
35 these assemblers. But please, _please_ send us bug reports and fixes
36 and anything else you can get your hands on, and we'll improve it
37 out of all recognition. Again.
39 Please see the file `Licence' for the legalese.
41 Getting Started: Installation
42 =============================
44 NASM is distributed in source form, in what we hope is totally
45 ANSI-compliant C. It uses no non-portable code at all, that we know
46 of. It ought to compile without change on any system you care to try
47 it on. We also supply a pre-compiled 16-bit DOS binary.
49 To install it, edit the Makefile to describe your C compiler, and
50 type `make'. Then copy the binary to somewhere on your path. That's
51 all - NASM relies on no files other than its own executable.
52 Although if you're on a Unix system, you may also want to install
53 the NASM manpage (`nasm.1'). You may also want to install the binary
54 and manpage for the Netwide Disassembler, NDISASM (also see
60 To assemble a file, you issue a command of the form
62 nasm -f <format> <filename> [-o <output>]
66 nasm -f elf myfile.asm
68 will assemble `myfile.asm' into an ELF object file `myfile.o'. And
70 nasm -f bin myfile.asm -o myfile.com
72 will assemble `myfile.asm' into a raw binary program `myfile.com'.
74 To get usage instructions from NASM, try typing `nasm -h'. This will
75 also list the available output file formats, and what they are.
77 If you use Linux but aren't sure whether your system is a.out or
78 ELF, type `file /usr/bin/nasm' or wherever you put the NASM binary.
79 If it says something like
81 /usr/bin/nasm: ELF 32-bit LSB executable i386 (386 and up) Version 1
83 then your system is ELF, and you should use `-f elf' when you want
84 NASM to produce Linux object files. If it says
86 /usr/bin/nasm: Linux/i386 demand-paged executable (QMAGIC)
88 or something similar, your system is a.out, and you should use `-f
91 Like Unix compilers and assemblers, NASM is silent unless it goes
92 wrong: you won't see any output at all, unless it gives error
95 Writing Programs with NASM
96 ==========================
98 Each line of a NASM source file should contain some combination of
101 LABEL: INSTRUCTION OPERANDS ; COMMENT
103 `LABEL' defines a label pointing to that point in the source. There
104 are no restrictions on white space: labels may have white space
105 before them, or not, as you please. The colon after the label is
108 Valid characters in labels are letters, numbers, `_', `$', `#', `@',
109 `~', `?', and `.'. The only characters which may be used as the
110 _first_ character of an identifier are letters, `_' and `?', and
111 (with special meaning: see `Local Labels') `.'. An identifier may
112 also be prefixed with a $ sign to indicate that it is intended to be
113 read as an identifier and not a reserved word; thus, if some other
114 module you are linking with defines a symbol `eax', you can refer to
115 `$eax' in NASM code to distinguish it from the register name.
117 `INSTRUCTION' can be any machine opcode (Pentium and P6 opcodes, FPU
118 opcodes, MMX opcodes and even undocumented opcodes are all
119 supported). The instruction may be prefixed by LOCK, REP, REPE/REPZ
120 or REPNE/REPNZ, in the usual way. Explicit address-size and operand-
121 size prefixes A16, A32, O16 and O32 are provided - one example of
122 their use is given in the `Unusual Instruction Sizes' section below.
123 You can also use a segment register as a prefix: coding `es mov
124 [bx],ax' is equivalent to coding `mov [es:bx],ax'. We recommend the
125 latter syntax, since it is consistent with other syntactic features
126 of the language, but for instructions such as `lodsb' there isn't
127 anywhere to put a segment override except as a prefix. This is why
130 The `INSTRUCTION' field may also contain some pseudo-opcodes: see
131 the section on pseudo-opcodes for details.
133 `OPERANDS' can be nonexistent, or huge, depending on the
134 instruction, of course. When operands are registers, they are given
135 simply as register names: `eax', `ss', `di' for example. NASM does
136 _not_ use the GAS syntax, in which register names are prefixed by a
137 `%' sign. Operands may also be effective addresses, or they may be
138 constants or expressions. See the separate sections on these for
141 `COMMENT' is anything after the first semicolon on the line,
142 excluding semicolons inside quoted strings.
144 Of course, all these fields are optional: the presence or absence of
145 the OPERANDS field is required by the nature of the INSTRUCTION
146 field, but any line may contain a LABEL or not, may contain an
147 INSTRUCTION or not, and may contain a COMMENT or not, independently
150 Lines may also contain nothing but a directive: see `Assembler
151 Directives' below for details.
153 NASM can currently not handle any line longer than 1024 characters.
154 This may be fixed in a future release.
156 Floating Point Instructions
157 ===========================
159 NASM has support for assembling FPU opcodes. However, its syntax is
160 not necessarily the same as anyone else's.
162 NASM uses the notation `st0', `st1', etc. to denote the FPU stack
163 registers. NASM also accepts a wide range of single-operand and
164 two-operand forms of the instructions. For people who wish to use
165 the single-operand form exclusively (this is in fact the `canonical'
166 form from NASM's point of view, in that it is the form produced by
167 the Netwide Disassembler), there is a TO keyword which makes
168 available the opcodes which cannot be so easily accessed by one
171 fadd st1 ; this sets st0 := st0 + st1
172 fadd st0,st1 ; so does this
173 fadd st1,st0 ; this sets st1 := st1 + st0
174 fadd to st1 ; so does this
176 It's also worth noting that the FPU instructions that reference
177 memory must use the prefixes DWORD, QWORD or TWORD to indicate what
178 size of memory operand they refer to.
180 NASM, in keeping with our policy of not trying to second-guess the
181 programmer, will _never_ automatically insert WAIT instructions into
182 your code stream. You must code WAIT yourself before _any_
183 instruction that needs it. (Of course, on 286 processors or above,
184 it isn't needed anyway...)
186 NASM supports specification of floating point constants by means of
187 `dd' (single precision), `dq' (double precision) and `dt' (extended
188 precision). Floating-point _arithmetic_ is not done, due to
189 portability constraints (not all platforms on which NASM can be run
190 support the same floating point types), but simple constants can be
191 specified. For example:
193 gamma dq 0.5772156649 ; Euler's constant
198 Pseudo-opcodes are not real x86 machine opcodes, but are used in the
199 instruction field anyway because that's the most convenient place to
200 put them. The current pseudo-opcodes are DB, DW and DD, their
201 uninitialised counterparts RESB, RESW and RESD, the EQU command, and
204 DB, DW and DD work as you would expect: they can each take an
205 arbitrary number of operands, and when assembled, they generate
206 nothing but those operands. All three of them can take string
207 constants as operands, which no other instruction can currently do.
208 See the `Constants' section for details about string constants.
210 RESB, RESW and RESD are designed to be used in the BSS section of a
211 module: they declare _uninitialised_ storage space. Each takes a
212 single operand, which is the number of bytes, words or doublewords
213 to reserve. We do not support the MASM/TASM syntax of reserving
214 uninitialised space by writing `DW ?' or similar: this is what we do
215 instead. (But see `Critical Expressions' for a caveat on the nature
218 (An aside: if you want to be able to write `DW ?' and have something
219 vaguely useful happen, you can always code `? EQU 0'...)
221 EQU defines a symbol to a specified value: when EQU is used, the
222 LABEL field must be present. The action of EQU is to define the
223 given label name to the value of its (only) operand. This definition
224 is absolute, and cannot change later. So, for example,
226 message db 'hello, world'
229 defines `msglen' to be the constant 12. `msglen' may not then be
230 redefined later. This is not a preprocessor definition either: the
231 value of `msglen' is evaluated _once_, using the value of `$' (see
232 the section `Expressions' for details of `$') at the point of
233 definition, rather than being evaluated wherever it is referenced
234 and using the value of `$' at the point of reference. Note that the
235 caveat in `Critical Expressions' applies to EQU too, at the moment.
237 Finally, the TIMES prefix causes the instruction to be assembled
238 multiple times. This is partly NASM's equivalent of the DUP syntax
239 supported by MASM-compatible assemblers, in that one can do
241 zerobuf: times 64 db 0
243 or similar, but TIMES is more versatile than that. TIMES takes not
244 just a numeric constant, but a numeric _expression_, so one can do
247 buffer: db 'hello, world'
248 times 64-$+buffer db ' '
250 which will store exactly enough spaces to make the total length of
251 `buffer' up to 64. (See the section `Critical Expressions' for a
252 caveat on the use of TIMES.) Finally, TIMES can be applied to
253 ordinary opcodes, so you can code trivial unrolled loops in it:
257 Note that there is no effective difference between `times 100 resb
263 NASM's addressing scheme is very simple, although it can involve
264 more typing than other assemblers. Where other assemblers
265 distinguish between a _variable_ (label declared without a colon)
266 and a _label_ (declared with a colon), and use different means of
267 addressing the two, NASM is totally consistent.
269 To refer to the contents of a memory location, square brackets are
270 required. This applies to simple variables, computed offsets,
271 segment overrides, effective addresses - _everything_. E.g.:
276 mov ax,[es:wordvar+bx]
278 NASM does _not_ support the various strange syntaxes used by MASM
281 mov ax,wordvar ; this is legal, but means something else
282 mov ax,es:wordvar[bx] ; not even slightly legal
283 es mov ax,wordvar[1] ; the prefix is OK, but not the rest
285 If no square brackets are used, NASM interprets label references to
286 mean the address of the label. Hence there is no need for MASM's
291 loads AX with the _address_ of the variable `wordvar'.
293 More complicated effective addresses are handled by enclosing them
294 within square brackets as before:
296 mov eax,[ebp+2*edi+offset]
299 NASM will cope with some fairly strange effective addresses, if you
300 try it: provided your effective address expression evaluates
301 _algebraically_ to something that the instruction set supports, it
302 will be able to assemble it. For example,
304 mov eax,[ebx*5] ; actually assembles to [ebx+ebx*4]
305 mov ax,[bx-si+2*si] ; actually assembles to [bx+si]
309 There is an ambiguity in the instruction set, which allows two forms
310 of 32-bit effective address with equivalent meaning:
315 These two expressions clearly refer to the same address. The
316 difference is that the first one, if assembled `as is', requires a
317 four-byte offset to be stored as part of the instruction, so it
318 takes up more space. NASM will generate the second (smaller) form
319 for both of the above instructions, in an effort to save space.
320 There is not, currently, any means for forcing NASM to generate the
321 larger form of the instruction.
323 Mixing 16 and 32 Bit Code: Unusual Instruction Sizes
324 ====================================================
326 A number of assemblers seem to have trouble assembling instructions
327 that use a different operand or address size from the one they are
328 expecting; as86 is a good example, even though the Linux kernel boot
329 process (which is assembled using as86) needs several such
330 instructions and as86 can't do them.
332 Instructions such as `mov eax,2' in 16-bit mode are easy, of course,
333 and NASM can do them just as well as any other assembler. The
334 difficult instructions are things like far jumps.
336 Suppose you are in a 16-bit segment, in protected mode, and you want
337 to execute a far jump to a point in a 32-bit segment. You need to
338 code a 32-bit far jump in a 16-bit segment; not many assemblers I
339 know of will easily support this. NASM can, by means of the `word'
340 and `dword' specifiers. So you can code
342 call 1234h:5678h ; this uses the default segment size
343 call word 1234h:5678h ; this is guaranteed to be 16-bit
344 call dword 1234h:56789ABCh ; and this is guaranteed 32-bit
346 and NASM will generate correct code for them.
348 Similarly, if you are coding in a 16-bit code segment, but trying to
349 access memory in a 32-bit data segment, your effective addresses
350 will want to be 32-bit. Of course as soon as you specify an
351 effective address containing a 32-bit register, like `[eax]', the
352 addressing is forced to be 32-bit anyway. But if you try to specify
353 a simple offset, such as `[label]' or `[0x10000]', you will get the
354 default address size, which in this case will be wrong. However,
355 NASM allows you to code `[dword 0x10000]' to force a 32-bit address
356 size, or conversely `[word wlabel]' to force 16 bits.
358 Be careful not to confuse `word' and `dword' _inside_ the square
359 brackets with _outside_: consider the instruction
361 mov word [dword 0x123456],0x7890
363 which moves 16 bits of data to an address specified by a 32-bit
364 offset. There is no contradiction between the `word' and `dword' in
365 this instruction, since they modify different aspects of the
366 functionality. Or, even more confusingly,
368 call dword far [fs:word 0x4321]
370 which takes an address specified by a 16-bit offset, and extracts a
371 48-bit DWORD FAR pointer from it to call.
373 Using this effective-address syntax, the `dword' or `word' override
374 may come before or after the segment override if any: NASM isn't
377 mov ax,[fs:dword 0x123456]
378 mov ax,[dword fs:0x123456]
380 are equivalent forms, and generate the same code.
382 The LOOP instruction comes in strange sizes, too: in a 16-bit
383 segment it uses CX as its count register by default, and in a 32-bit
384 segment it uses ECX. But it's possible to do either one in the other
385 segment, and NASM will cope by letting you specify the count
386 register as a second operand:
388 loop label ; uses CX or ECX depending on mode
389 loop label,cx ; always uses CX
390 loop label,ecx ; always uses ECX
392 Finally, the string instructions LODSB, STOSB, MOVSB, CMPSB, SCASB,
393 INSB, and OUTSB can all have strange address sizes: typically, in a
394 16-bit segment they read from [DS:SI] and write to [ES:DI], and in a
395 32-bit segment they read from [DS:ESI] and write to [ES:EDI].
396 However, this can be changed by the use of the explicit address-size
397 prefixes `a16' and `a32'. These prefixes generate null code if used
398 in the same size segment as they specify, but generate an 0x67
399 prefix otherwise. Hence `a16' generates no code in a 16-bit segment,
400 but 0x67 in a 32-bit one, and vice versa. So `a16 lodsb' will always
401 generate code to read a byte from [DS:SI], no matter what the size
402 of the segment. There are also explicit operand-size override
403 prefixes, `o16' and `o32', which will optionally generate 0x66
404 bytes, but these are provided for completeness and should never have
410 NASM can accept three kinds of constant: _numeric_, _character_ and
413 Numeric constants are simply numbers. NASM supports a variety of
414 syntaxes for expressing numbers in strange bases: you can do any of
416 100 ; this is decimal
423 NASM does not support A86's syntax of treating anything with a
424 leading zero as hex, nor does it support the C syntax of treating
425 anything with a leading zero as octal. Leading zeros make no
426 difference to NASM. (Except that, as usual, if you have a hex
427 constant beginning with a letter, and you want to use the trailing-H
428 syntax to represent it, you have to use a leading zero so that NASM
429 will recognise it as a number instead of a label.)
431 The `x' in `0x100', and the trailing `h', `q' and `b', may all be
432 upper case if you want.
434 Character constants consist of up to four characters enclosed in
435 single or double quotes. No escape character is defined for
436 including the quote character itself: if you want to declare a
437 character constant containing a double quote, enclose it in single
438 quotes, and vice versa.
440 Character constants' values are worked out in terms of a
441 little-endian computer: if you code
445 then if you were to examine the binary output from NASM, it would
446 contain the visible string `abcd', which of course means that the
447 actual value loaded into EAX would be 0x64636261, not 0x61626364.
449 String constants are like character constants, only more so: if a
450 character constant appearing as operand to a DB, DW or DD is longer
451 than the word size involved (1, 2 or 4 respectively), it will be
452 treated as a string constant instead, which is to say the
453 concatenation of separate character constants.
459 declares a twelve-character string constant. And
463 (a string constant) is equivalent to writing
467 (three character constants), so that what actually gets assembled is
472 (It's worth noting that one of the reasons for the reversal of
473 character constants is so that the instruction `dw "ab"' has the
474 same meaning whether "ab" is treated as a character constant or a
475 string constant. Hence there is less confusion.)
480 Expressions in NASM can be formed of the following operators: `|'
481 (bitwise OR), `^' (bitwise XOR), `&' (bitwise AND), `<<' and `>>'
482 (logical bit shifts), `+', `-', `*' (ordinary addition, subtraction
483 and multiplication), `/', `%' (unsigned division and modulo), `//',
484 `%%' (signed division and modulo), `~' (bitwise NOT), and the
485 operators SEG and WRT (see `SEG and WRT' below).
487 The order of precedence is:
495 unary + and -, ~, SEG highest
497 As usual, operators within a precedence level associate to the left
498 (i.e. `2-3-4' evaluates the same way as `(2-3)-4').
500 A form of algebra is done by NASM when evaluating expressions: I
501 have already stated that an effective address expression such as
502 `[EAX*6-EAX]' will be recognised by NASM as algebraically equivalent
503 to `[EAX*4+EAX]', and assembled as such. In addition, algebra can be
504 done on labels as well: `label2*2-label1' is an acceptable way to
505 define an address as far beyond `label2' as `label1' is before it.
506 (In less algebraically capable assemblers, one might have to write
507 that as `label2 + (label2-label1)', where the value of every
508 sub-expression is either a valid address or a constant. NASM can of
509 course cope with that version as well.)
511 Expressions may also contain the special token `$', known as a Here
512 token, which always evaluates to the address of the current assembly
513 point. (That is, the address of the assembly point _before_ the
514 current instruction gets assembled.) The special token `$$'
515 evaluates to the address of the beginning of the current section;
516 this can be used for alignment, as shown below:
518 times ($$-$) & 3 nop ; pad with NOPs to 4-byte boundary
523 NASM contains the capability for its object file formats (currently,
524 only `obj' makes use of this) to permit programs to directly refer
525 to the segment-base values of their segments. This is achieved
526 either by the object format defining the segment names as symbols
527 (`obj' does this), or by the use of the SEG operator.
529 SEG is a unary prefix operator which, when applied to a symbol
530 defined in a segment, will yield the segment base value of that
531 segment. (In `obj' format, symbols defined in segments which are
532 grouped are considered to be primarily a member of the _group_, not
533 the segment, and the return value of SEG reflects this.)
535 SEG may be used for far pointers: it is guaranteed that for any
536 symbol `sym', using the offset `sym' from the segment base `SEG sym'
537 yields a correct pointer to the symbol. Hence you can code a far
540 CALL SEG routine:routine
542 or store a far pointer in a data segment by
544 DW routine, SEG routine
546 For convenience, NASM supports the forms
551 as direct synonyms for the canonical syntax
553 CALL SEG routine:routine
554 JMP SEG routine:routine
556 No alternative syntax for
558 DW routine, SEG routine
562 Simply referring to `sym', for some symbol, will return the offset
563 of `sym' from its _preferred_ segment base (as returned from `SEG
564 sym'); sometimes, you may want to obtain the offset of `sym' from
565 some _other_ segment base. (E.g. the offset of `sym' from the base
566 of the segment it's in, where normally you'd get the offset from a
567 group base). This is accomplished using the WRT (With Reference To)
568 keyword: if `sym' is defined in segment `seg' but you want its
569 offset relative to the beginning of segment `seg2', you can do
573 The right-hand operand to WRT must be a segment-base value. You can
574 also do `sym WRT SEG sym2' if you need to.
579 NASM is a two-pass assembler: it goes over the input once to
580 determine the location of all the symbols, then once more to
581 actually generate the output code. Most expressions are
582 non-critical, in that if they contain a forward reference and hence
583 their correct value is unknown during the first pass, it doesn't
584 matter. However, arguments to RESB, RESW and RESD, and the argument
585 to the TIMES prefix, can actually affect the _size_ of the generated
586 code, and so it is critical that the expression can be evaluated
587 correctly on the first pass. So in these situations, expressions may
588 not contain forward references. This prevents NASM from having to
589 sort out a mess such as
592 label: db 'where am I?'
594 in which the TIMES argument could equally legally evaluate to
595 _anything_, or perhaps even worse,
597 times (label-$+1) db 0
598 label: db 'NOW where am I?'
600 in which any value for the TIMES argument is by definition invalid.
602 Since NASM is a two-pass assembler, this criticality condition also
603 applies to the argument to EQU. Suppose, if this were not the case,
604 we were to have the setup
610 On pass one, `a' cannot be defined properly, since `b' is not known
611 yet. On pass two, `b' is known, so line two can define `a' properly.
612 Unfortunately, line 1 needed `a' to be defined properly, so this
613 code will not assemble using only two passes.
618 NASM takes its local label scheme mainly from the old Amiga
619 assembler Devpac: a local label is one that begins with a period.
620 The `localness' comes from the fact that local labels are associated
621 with the previous non-local label, so that you may declare the same
622 local label twice if a non-local one intervenes. Hence:
625 .loop ; some more code
629 .loop ; some more code
633 In the above code, each `jne' instruction jumps to the line of code
634 before it, since the `.loop' labels are distinct from each other.
636 NASM, however, introduces an extra capability not present in Devpac,
637 which is that the local labels are actually _defined_ in terms of
638 their associated non-local label. So if you really have to, you can
641 label3 ; some more code
645 So although local labels are _usually_ local, it is possible to
646 reference them from anywhere in your program, if you really have to.
651 Assembler directives appear on a line by themselves (apart from a
652 comment), and must be enclosed in square brackets. No white space
653 may appear before the opening square bracket, although white space
654 and a comment may come after the closing bracket.
656 Some directives are universal: they may be used in any situation,
657 and do not change their syntax. The universal directives are listed
660 [BITS 16] or [BITS 32] switches NASM into 16-bit or 32-bit mode.
661 (This is equivalent to USE16 and USE32 segments, in TASM or MASM.)
662 In 32-bit mode, instructions are prefixed with 0x66 or 0x67 prefixes
663 when they use 16-bit data or addresses; in 16-bit mode, the reverse
664 happens. NASM's default depends on the object format; the defaults
665 are documented with the formats. (See `obj', in particular, for some
668 [INCLUDE filename] or [INC filename] includes another source file
669 into the current one. At present, only one level of inclusion is
672 [SECTION name] or [SEGMENT name] changes which section the code you
673 write will be assembled into. Acceptable section names vary between
674 output formats, but most formats (indeed, all formats at the moment)
675 support the names `.text', `.data' and `.bss'. Note that `.bss' is
676 an uninitialised data section, and so you will receive a warning
677 from NASM if you try to assemble any code or data in it. The only
678 thing you can do in `.bss' without triggering a warning is use RESB,
679 RESW and RESD. That's what they're for.
681 [ABSOLUTE address] can be considered a different form of [SECTION],
682 in that it must be overridden using a SECTION directive once you
683 have finished using it. It is used to assemble notional code at an
684 absolute offset address; of course, you can't actually assemble
685 _code_ there, since no object file format is capable of putting the
686 code in place, but you can use RESB, RESW and RESD, and you can
687 define labels. Hence you could, for example, define a C-like data
688 structure by means of
698 and then carry on coding. This defines `stLong' to be zero, `stWord'
699 to be 4, `stByte1' to be 6, `stByte2' to be 7 and `st_size' to be 8.
700 So this has defined a data structure.
702 [EXTERN symbol] defines a symbol as being `external', in the C
703 sense: `EXTERN' states that the symbol is _not_ declared in this
704 module, but is declared elsewhere, and that you wish to _reference_
707 [GLOBAL symbol] defines a symbol as being global, in the sense that
708 it is exported from this module and other modules may reference it.
709 All symbols are local, unless declared as global. Note that the
710 `GLOBAL' directive must appear before the definition of the symbol
713 [COMMON symbol size] defines a symbol as being common: it is
714 declared to have the given size, and it is merged at link time with
715 any declarations of the same symbol in other modules. This is not
716 _fully_ supported in the `obj' file format: see the section on `obj'
719 Directives may also be specific to the output file format. At
720 present, the `bin' and `obj' formats define extra directives, which
726 The current output formats supported are `bin', `aout', `coff',
729 `bin': flat-form binary
730 -----------------------
732 This is at present the only output format that generates instantly
733 runnable code: all the others produce object files that need linking
734 before they become executable.
736 `bin' output files contain no red tape at all: they simply contain
737 the binary representation of the exact code you wrote.
739 The `bin' format supports a format-specific directive, which is ORG.
740 [ORG addr] declares that your code should be assembled as if it were
741 to be loaded into memory at the address `addr'. So a DOS .COM file
742 should state [ORG 0x100], and a DOS .SYS file should state [ORG 0].
743 There should be _one_ ORG directive, at most, in an assembly file:
744 NASM does not support the use of ORG to jump around inside an object
745 file, like MASM does (see the `Bugs' section for a use of the ORG
746 directive not supported by NASM).
748 Like all formats, the `bin' format defines the section names
749 `.text', `.data' and `.bss'. The layout is that `.text' comes first
750 in the output file, followed by `.data', and notionally followed by
751 `.bss'. So if you declare a BSS section in a flat binary file,
752 references to the BSS section will refer to space past the end of
753 the actual file. The `.data' and `.bss' sections are considered to
754 be aligned on four-byte boundaries: this is achieved by inserting
755 padding zero bytes between the end of the text section and the start
756 of the data, if there is data present. Of course if no [SECTION]
757 directives are present, everything will go into `.text', and you
758 will get nothing in the output except the code you wrote.
760 `bin' silently ignores GLOBAL directives, and will also not complain
761 at EXTERN ones. You only get an error if you actually _reference_ an
764 Using the `bin' format, the default output filename is `filename'
765 for inputs of `filename.asm'. If there is no extension to be
766 removed, output will be placed in `nasm.out' and a warning will be
769 `bin' defaults to 16-bit assembly mode.
771 `aout' and `elf': Linux object files
772 ------------------------------------
774 These two object formats are the ones used under Linux. They have no
775 format-specific directives, and their default output filename is
778 ELF is a much more featureful object-file format than a.out: in
779 particular it has enough features to support the writing of position
780 independent code by means of a global offset table, and position
781 independent shared libraries by means of a procedure linkage table.
782 Unfortunately NASM, as yet, does not support these extensions, and
783 so NASM cannot be used to write shared library code under ELF. NASM
784 also does not support the capability, in ELF, for specifying precise
785 alignment constraints on common variables.
787 Both `aout' and `elf' default to 32-bit assembly mode.
789 `coff' and `win32': Common Object File Format
790 ---------------------------------------------
792 The `coff' format generates standard Unix COFF object files, which
793 can be fed to (for example) the DJGPP linker. Its default output
794 filename, like the other Unix formats, is `filename.o'.
796 The `win32' format generates Win32 (Windows 95 or Intel-platform
797 Windows NT) object files, which nominally use the COFF standard, but
798 in fact are not compatible. Its default output filename is
801 `coff' and `win32' are not quite compatible formats, due to the fact
802 that Microsoft's interpretation of the term `relative relocation'
803 does not seem to be the same as the interpretation used by anyone
804 else. It is therefore more correct to state that Win32 uses a
805 _variant_ of COFF. The object files will not therefore produce
806 correct output when fed to each other's linkers.
808 In addition to this subtle incompatibility, Win32 also defines
809 extensions to basic COFF, such as a mechanism for importing symbols
810 from dynamic-link libraries at load time. NASM may eventually
811 support this extension in the form of a format-specific directive.
812 However, as yet, it does not. Neither the `coff' nor `win32' output
813 formats have any specific directives.
815 The Microsoft linker also has a small blind spot: it cannot
816 correctly relocate a relative CALL or JMP to an absolute address.
817 Hence all PC-relative CALLs or JMPs, when using the `win32' format,
818 must have targets which are relative to sections, or to external
819 symbols. You can't do
821 _even_ if you happen to know that there is executable code at that
822 address. The linker simply won't get the reference right; so in the
823 interests of not generating incorrect code, NASM will not allow this
824 form of reference to be written to a Win32 object file. (Standard
825 COFF, or at least the DJGPP linker, seems to be able to cope with
826 this contingency. Although that may be due to the executable having
827 a zero load address.)
829 Both `coff' and `win32' default to 32-bit assembly mode.
831 `obj': Microsoft 16-bit Object Module Format
832 --------------------------------------------
834 The `obj' format generates 16-bit Microsoft object files, suitable
835 for feeding to 16-bit versions of Microsoft C, and probably
836 TLINK as well (although that hasn't been tested). The Use32
837 extensions are supported.
839 `obj' defines no special segment names: you can call segments what
840 you like. Unlike the other formats, too, segment names are actually
841 defined as symbols, so you can write
846 and get the _segment_ address of the segment, suitable for loading
847 into a segment register.
849 Segments can be declared with attributes:
851 [SEGMENT CODE PRIVATE ALIGN=16 CLASS=CODE OVERLAY=OVL2 USE16]
853 You can specify segments to be PRIVATE, PUBLIC, COMMON or STACK;
854 their alignment may be any power of two from 1 to 256 (although only
855 1, 2, 4, 16 and 256 are really supported, so anything else gets
856 rounded up to the next highest one of those); their class and
857 overlay names may be specified. You may also specify segments to be
858 USE16 or USE32. The defaults are PUBLIC ALIGN=1, no class, no
861 You can also specify that a segment is _absolute_ at a certain
864 [SEGMENT SCREEN ABSOLUTE=0xB800]
866 This is an alternative to the ALIGN keyword.
868 The format-specific directive GROUP allows segment grouping: [GROUP
869 DGROUP DATA BSS] defines the group DGROUP to contain segments DATA
872 Segments are defined as part of their group by default: if `var' is
873 declared in segment `data', which is part of group `dgroup', then
874 `SEG var' returns `dgroup', and `var' signifies the offset of `var'
875 relative to the beginning of `dgroup'. You must use `var WRT data'
876 to get the offset of `var' relative to the beginning of its
879 NASM allows a segment to be in two groups, but will generate a
880 warning. References to the symbols in that segment will be resolved
881 relative to the _first_ group it is defined in.
883 The directive [UPPERCASE] causes all symbol, segment and group names
884 output to the object file to be uppercased. The actual _assembly_ is
885 still case sensitive.
887 Common variables in OBJ files can be `near' or `far': currently,
888 NASM has a horribly grotty way to support that, which is that if you
889 specify the common variable's size as negative, it will be near, and
890 otherwise it will be far. The support isn't perfect: if you declare
891 a far common variable both in a NASM assembly module and in a C
892 program, you may well find the linker reports "mismatch in
893 array-size" or some such. The reason for this is that far common
894 variables are defined by means of _two_ size constants, which are
895 multiplied to give the real size. Apparently the Microsoft linker
896 (at least) likes both constants, not merely their product, to match
897 up. This may be fixed in a future release.
899 If the module you're writing is intended to contain the program
900 entry point, you can declare this by defining the special label
901 `..start' at the start point, either as a label or by EQU (although
902 of course the normal caveats about EQU dependency still apply).
904 `obj' has an unusual handling of assembly modes: instead of having a
905 global default for the whole file, there is a separate default for
906 each segment. Thus, each [SEGMENT] directive carries an implicit
907 [BITS] directive with it, which switches to 16-bit or 32-bit mode
908 depending on whether the segment is a Use16 or Use32 segment. If you
909 want to place 32-bit code in a Use16 segment, you can use an
910 explicit [BITS 32] override, but if you switch temporarily away from
911 that segment, you will have to repeat the override after coming back
914 `as86': Linux as86 (bin86-0.3)
915 ------------------------------
917 This output format replicates the format used to pass data between
918 the Linux x86 assembler and linker, as86 and ld86. Its default file
919 name, yet again, is `filename.o'. Its default segment-size attribute
922 `rdf': Relocatable Dynamic Object File Format
923 ---------------------------------------------
925 RDOFF was designed initially to test the object-file production
926 interface to NASM. It soon became apparent that it could be enhanced
927 for use in serious applications due to its simplicity; code to load
928 and execute an RDOFF object module is very simple. It also contains
929 enhancements to allow it to be linked with a dynamic link library at
930 either run- or load- time, depending on how complex you wish to make
933 The `rdoff' directory in the NASM distribution archive contains
934 source for an RDF linker and loader to run under Linux.
936 `rdf' has a default segment-size attribute of 32 bits.
938 Debugging format: `dbg'
939 -----------------------
941 This output format is not built into NASM by default: it's for
942 debugging purposes. It produces a debug dump of everything that the
943 NASM assembly module feeds to the output driver, for the benefit of
944 people trying to write their own output drivers.
949 Apart from the missing features (correct OBJ COMMON support, ELF
950 alignment, ELF PIC support, etc.), there are no _known_ bugs.
951 However, any you find, with patches if possible, should be sent to
952 <jules@dcs.warwick.ac.uk> or <anakin@pobox.com>, and we'll try to
955 Beware of Pentium-specific instructions: Intel have provided a macro
956 file for MASM, to implement the eight or nine new Pentium opcodes as
957 MASM macros. NASM does not generate the same code for the CMPXCHG8B
958 instruction as these macros do: this is due to a bug in the _macro_,
959 not in NASM. The macro works by generating an SIDT instruction (if I
960 remember rightly), which has almost exactly the right form, then
961 using ORG to back up a bit and do a DB over the top of one of the
962 opcode bytes. The trouble is that Intel overlooked (or were unable
963 to allow for) the possibility that the SIDT instruction may contain
964 an 0x66 or 0x67 operand or address size prefix. If this happens, the
965 ORG will back up by the wrong amount, and the macro will generate
966 incorrect code. NASM gets it right. This, also, is not a bug in
967 NASM, so please don't report it as one. (Also please note that the
968 ORG directive in NASM doesn't work this way, and so you can't do
969 equivalent tricks with it...)
974 Enjoy using NASM! Please feel free to send me comments, or
975 constructive criticism, or bug fixes, or requests, or general chat.
977 Contributions are also welcome: if anyone knows anything about any
978 other object file formats I should support, please feel free to send
979 me documentation and some short example files (in my experience,
980 documentation is useless without at _least_ one example), or even to
981 write me an output module. OS/2 object files, in particular, spring
982 to mind. I don't have OS/2, though.
984 Please keep flames to a minimum: I have had some very angry e-mails
985 in the past, condemning me for writing a useless assembler, that
986 output in no useful format (at the time, that was true), generated
987 incorrect code (several typos in the instruction table, since fixed)
988 and took up too much memory and disk space (the price you pay for
989 total portability, it seems). All these were criticisms I was happy
990 to hear, but I didn't appreciate the flames that went with them.
991 NASM _is_ still a prototype, and you use it at your own risk. I
992 _think_ it works, and if it doesn't then I want to know about it,
993 but I don't guarantee anything. So don't flame me, please. Blame,
996 - Simon Tatham <anakin@pobox.com>, 21-Nov-96