Directives

The content here is taken and/or edited from the NASM Documentation.

SEGMENT / SECTION

The SECTION directive (SEGMENT is an exactly equivalent synonym) changes which section of the output file the code you write will be assembled into. In some object file formats, the number and names of sections are fixed; in others, the user may make up as many as they wish. Hence SECTION may sometimes give an error message, or may define a new section, if you try to switch to a section that does not (yet) exist.

Here is a table on sections that exist:

SegmentUse
textread-only portion containing the executable instructions
codesame as text
dataread-write portion containing initialized static variables
rodataread-only portion containing initialized static variables
bssread-write portion containing statically-allocated, but not explicitly initialized variables
extraUsually to store DS data temporarily

EXTERN

EXTERN is similar to the C keyword extern: it is used to declare a symbol which is not defined anywhere in the module being assembled, but is assumed to be defined in some other module and needs to be referred to by this one.

extern printf

GLOBAL

GLOBAL is the other end of EXTERN: if one module declares a symbol as EXTERN and refers to it, then in order to prevent linker errors, some other module must actually define the symbol and declare it as GLOBAL.

global main

segment .text
main:
  xor eax, eax
  ret

COMMON

The COMMON directive is used to declare common variables. A common variable is much like a global variable declared in the uninitialized data section, so that

common  intvar  4

is similar in function to

global  intvar 
section .bss 
  intvar  resd    1

The difference is that if more than one module defines the same common variable, then at link time those variables will be merged, and references to intvar in all modules will point at the same piece of memory.

CPU

The CPU directive restricts assembly to those instructions which are available on the specified CPU.

OptionMeaning
CPU 8086Assemble only 8086 instruction set
CPU 186Assemble instructions up to the 80186 instruction set
CPU 286Assemble instructions up to the 286 instruction set
CPU 386Assemble instructions up to the 386 instruction set
CPU 486486 instruction set
CPU 586Pentium instruction set
CPU PENTIUMSame as 586
CPU 686P6 instruction set
CPU PPROSame as 686
CPU P2Same as 686
CPU P3Pentium III (Katmai) instruction sets
CPU KATMAISame as P3
CPU P4Pentium 4 (Willamette) instruction set
CPU WILLAMETTESame as P4
CPU PRESCOTTPrescott instruction set
CPU X64x86-64 (x64/AMD64/Intel 64) instruction set
CPU IA64IA64 CPU (in x86 mode) instruction set

All options are case insensitive. All instructions will be selected only if they apply to the selected CPU or lower. By default, all instructions are available.

FLOAT

By default, floating-point constants are rounded to nearest, and IEEE denormals are supported. The following options can be set to alter this behaviour:

OptionMeaning
FLOAT DAZFlush denormals to zero
FLOAT NODAZDo not flush denormals to zero (default)
FLOAT NEARRound to nearest (default)
FLOAT UPRound up (toward +Infinity)
FLOAT DOWNRound down (toward –Infinity)
FLOAT ZERORound toward zero
FLOAT DEFAULTRestore default settings

Blog Comments powered by Disqus.