NASM Compilation

m To get command line help, type:
nasm -h
m To compile into an ELF object file .o, type:
nasm -f elf myfile.asm
m To create a listing file, type:
nasm -f elf myfile.asm -l myfile.lst
m To send errors to a file, type:
nasm -E myfile.err -f elf myfile.asm
m To include other search paths such as /usr/include, type:
nasm -I/usr/include -f elf myfile.asm
m To include other files in a source file, use:
%include "myinc.inc"
m To define constants, use either of the equivalent forms:
-dFOO=100 on the compile command line.
%define FOO 100 in the source file.

NASM is case-sensitive.
NASM Syntax
m In order to refer to the contents of a memory location, use square brackets.
m In order to refer to the address of a variable, leave them out, e.g.,


No need for the OFFSET directive.

m NASM does not support the hybrid syntaxes such as:


m NASM does NOT remember variable types:


NASM Syntax

m NASM does NOT remember variable types
Therefore, un-typed operations are not supported, e.g.
LODS, MOVS, STOS, SCAS, CMPS, INS, and OUTS.

You must use instead:
LODSB, MOVSW, and SCASD, etc.

m NASM does not support ASSUME.
It will not keep track of what values you choose to put in your segment registers.

m NASM does not support memory models.
The programmer is responsible for coding CALL FAR instructions where necessary when calling external functions.


seg returns the segment base of procedure proc.

NASM Syntax

m NASM does not support memory models.
The programmer has to keep track of which functions are supposed to be called with a far call and which with a near call, and is responsible for putting the correct form of RET instruction (RETN or RETF).

m NASM uses the names st0, st1, etc. to refer to floating point registers.

m NASM's declaration syntax for un-initialized storage is different.


m Macros and directives work differently than they do in MASM.

NASM Syntax

NASM source line:


The ':' is optional, which can cause problems if, for example, you misspell an instruction, e.g. lodab instead of lodsb.

Use -w+orphan-labels as a command line option to the compiler to identify these!

Valid characters in labels are letters, numbers, _, $, #, @, ~, ., and ?.
Identifier valid starting characters include letters, . , _ and ?.

Instruction prefixes supported:
n LOCK
n REP,
n REPE/REPZ
n REPNE/REPNZ

NASM Syntax

Floating point instructions can take on two-operand forms or a single operand form:


Almost any float-point instruction that references memory must use one of the prefixes DWORD, QWORD or TWORD to indicate what size of memory operand it refers to.

Storage directives:
DB, DW, DD, DQ and DT are used for initialized data only.
RESB, RESW, RESD, RESQ and REST are used for uninitialized.


NASM Syntax

EQU defines a symbol to a constant:


Address mode examples:


Constants:
Suffixes H, Q and B are used hex, octal and binary. 0x also works for hex.


NASM Syntax

The SEG operator returns the preferred segment base of a symbol:


Will load ES:EBX with a valid pointer to symbol.
(Probably won't need unless you are writing 16-bit code which has multiple segments).

To declare a far pointer to a data item in a data segment:


Local Labels begin with a '.' and are associated with previous non-local label.


NASM Syntax

Single-line Macros:


Can be used as:


Which expands to:


Note that expansion occurs at invocation time, not at definition time, e.g.


Used as:


Expands to:


NASM Syntax

Overloading macros is allowed.


Undefining macros:


Multi-line Macros:


And use as:


Expands to:


NASM Syntax

Conditional assembly:
Given the macro (21h is a DOS interrupt):


And the call:


Using the command-line option -dDEBUG, expands the macro otherwise it is left out (similar to C).
Note that "I'm here", 13, 10 is substituted in for %2 in the above code.

NASM Syntax

Structure definitions:


mytype_size is also defined as the total size, and is 39 in this example.
To declare instances:


To reference, you must use:


The align (and alignb) directive can be used to align the data.
NASM Examples

Hello World (using ld):


To produce hello.o object file:
nasm -f elf hello.asm
To produce hello ELF executable:
ld -s -o hello hello.o
NASM Examples

Hello World (using gcc):


To produce hello.o object file:
nasm -f elf hello.asm
To produce hello ELF executable:
gcc -o hello hello.o