Next Chapter | Previous Chapter | Contents | Index
NASM is a portable assembler, designed to be able to compile on any ANSI
C-supporting platform and produce output to run on a variety of Intel x86
operating systems. For this reason, it has a large number of available
output formats, selected using the 
As stated in section 2.1.1,
NASM chooses a default name for your output file based on the input file
name and the chosen output format. This will be generated by removing the
extension (
bin The 
The 
Using the 
ORG The 
For example, the following code will generate the longword
        org     0x100 
        dd      label 
label:
Unlike the 
bin SECTION The 
section .data align=16
switches to the section 
The parameter to 
Multisection The 
progbits nobits progbits .bss nobits align= start= vstart= follows= <section> vfollows= <section> org start vstart align= align=(1 << ALIGN_SHIFT) ALIGN_SHIFT SECTION .text ORG ORG 0 .bss progbits start= vstart= follows= vfollows= section.<secname>.start Map files can be generated in 
obj The 
The 
If your source file contains code before specifying an explicit
When you define a segment in an 
segment data 
dvar:   dw      1234 
segment code 
function: 
        mov     ax,data         ; get segment address of data 
        mov     ds,ax           ; and move it into DS 
        inc     word [dvar]     ; now this reference will work 
        ret
The 
extern  foo 
      mov   ax,seg foo            ; get preferred segment of foo 
      mov   ds,ax 
      mov   ax,data               ; a different segment 
      mov   es,ax 
      mov   ax,[ds:foo]           ; this accesses `foo' 
      mov   [es:foo wrt data],bx  ; so does this
obj SEGMENT The 
segment code private align=16
defines the segment 
The available qualifiers are:
PRIVATE PUBLIC COMMON STACK PRIVATE PUBLIC STACK COMMON ALIGN CLASS CLASS=CODE OVERLAY CLASS USE16 USE32 FLAT FLAT obj SEGMENT SCREEN ABSOLUTE=0xB800 ABSOLUTE ALIGN NASM's default segment attributes are 
GROUP The 
segment data 
        ; some data 
segment bss 
        ; some uninitialised data 
group dgroup data bss
which will define a group called 
If you just refer to 
NASM will allow a segment to be part of more than one group, but will generate a warning if you do this. Variables declared in a segment which is part of more than one group will default to being relative to the first group that was defined to contain the segment.
A group does not have to contain any segments; you can still make
UPPERCASE Although NASM itself is case sensitive, some OMF linkers are not;
therefore it can be useful for NASM to output single-case object files. The
IMPORT The 
The 
    import  WSAStartup wsock32.dll
A third optional parameter gives the name by which the symbol is known in the library you are importing it from, in case this is not the same as the name you wish the symbol to be known by to your code once you have imported it. For example:
    import  asyncsel wsock32.dll WSAAsyncSelect
EXPORT The 
Further parameters can be given to define attributes of the exported symbol. These parameters, like the second, are separated by white space. If further parameters are given, the external name must also be specified, even if it is the same as the internal name. The available attributes are:
resident nodata parm=NNN NNN For example:
    export  myfunc 
    export  myfunc TheRealMoreFormalLookingFunctionName 
    export  myfunc myfunc 1234  ; export by ordinal 
    export  myfunc myfunc resident parm=23 nodata
..start 
obj EXTERN If you declare an external symbol with the directive
    extern  foo
then references such as 
        mov     ax,seg foo      ; get preferred segment base 
        mov     es,ax           ; move it into ES 
        mov     ax,[es:foo]     ; and use offset `foo' from it
This is a little unwieldy, particularly if you know that an external is
going to be accessible from a given segment or group, say
        mov     ax,[foo wrt dgroup]
However, having to type this every time you want to access
    extern  foo:wrt dgroup
This form causes NASM to pretend that the preferred segment base of
This default-
obj COMMON The 
common nearvar 2:near ; `nearvar' is a near common common farvar 10:far ; and `farvar' is far
Far common variables may be greater in size than 64Kb, and so the OMF specification says that they are declared as a number of elements of a given size. So a 10-byte far common variable could be declared as ten one-byte elements, five two-byte elements, two five-byte elements or one ten-byte element.
Some 
common c_5by2 10:far 5 ; two five-byte elements common c_2by5 10:far 2 ; five two-byte elements
If no element size is specified, the default is 1. Also, the
common c_5by2 10:5 ; two five-byte elements common c_2by5 10:2 ; five two-byte elements
In addition to these extensions, the 
common foo 10:wrt dgroup common bar 16:far 2:wrt data common baz 24:wrt data:6
win32 The 
Note that although Microsoft say that Win32 object files follow the
win32 SECTION Like the 
The available qualifiers are:
code text data bss code data bss rdata info info .drectve align= obj The defaults assumed by NASM if you do not specify the above qualifiers are:
section .text code align=16 section .data data align=4 section .rdata rdata align=8 section .bss bss align=4
Any other section name is treated by default like
coff The 
The 
macho The 
elf The 
elf SECTION Like the 
The available qualifiers are:
alloc noalloc exec noexec write nowrite progbits nobits align= obj The defaults assumed by NASM if you do not specify the above qualifiers are:
section .text progbits alloc exec nowrite align=16 section .rodata progbits alloc noexec nowrite align=4 section .data progbits alloc noexec write align=4 section .bss nobits alloc noexec write align=4 section other progbits alloc noexec nowrite align=1
(Any section name other than 
elf WRT The 
Since 
wrt ..gotpc _GLOBAL_OFFSET_TABLE_ $$ wrt ..gotoff wrt ..got wrt ..plt CALL JMP wrt ..sym A fuller explanation of how to use these relocation types to write shared libraries entirely in NASM is given in section 8.2.
elf GLOBAL 
You can specify whether a global variable is a function or a data object
by suffixing the name with a colon and the word
global hashlookup:function, hashtable:data
exports the global symbol 
You can also specify the size of the data associated with the symbol, as a numeric expression (which may involve labels, and even forward references) after the type specifier. Like this:
global  hashtable:data (hashtable.end - hashtable) 
hashtable: 
        db this,that,theother  ; some data here 
.end:
This makes NASM automatically calculate the length of the table and
place that information into the 
Declaring the type and size of global symbols is necessary when writing shared library code. For more information, see section 8.2.4.
elf COMMON 
common dwordarray 128:4
This declares the total size of the array to be 128 bytes, and requires that it be aligned on a 4-byte boundary.
The 
aout a.out The 
aoutb a.out The 
as86 as86 The Minix/Linux 16-bit assembler 
NASM supports this format, just in case it is useful, as
rdf The 
The Unix NASM archive, and the DOS archive which includes sources, both
contain an 
LIBRARY 
    library  mylib.rdl
MODULE Special 
    module  mymodname
Note that when you statically link modules and tell linker to strip the
symbols from output file, all module names will be stripped too. To avoid
it, you should start module names with 
    module  $kernel.core
rdf GLOBAL 
Suffixing the name with a colon and the word
    global  sys_open:export
To specify that exported symbol is a procedure (function), you add the
word 
    global  sys_open:export proc
Similarly, to specify exported data object, add the word
    global  kernel_ticks:export data
rdf EXTERN By default the 
    library $libc 
    extern  _open:import 
    extern  _printf:import proc 
    extern  _errno:import data
Here the directive 
dbg The 
The 
For simple files, one can easily use the 
nasm -f dbg filename.asm
which will generate a diagnostic file called
nasm -e -f rdf -o rdfprog.i rdfprog.asm nasm -a -f dbg rdfprog.i
This preprocesses 
This workaround will still typically not work for programs intended for