Nasmsyntax

PeterJohnson edited this page Jun 24, 2011 · 1 revision

NASM Syntax

NASM has a simplified syntax designed to let the user code with minimum overhead. In its simplest form, a NASM program needs nothing more than the assembly-language instructions; no assembler directives are necessary. NASM also makes some improvements to the Intel instruction syntax, removing ambiguity and improving consistency. The overall goal is to allow the user, as far as possible, to predict the machine-level opcode of each instruction without referring to the surrounding labels, directives, or instructions.

Source Line

Like most assemblers, NASM supports the standard four-part source line:

label:          instruction operands                ;comment

Pseudo-Instructions

The following pseudo-instructions cause the assembler to emit data into the output stream:

  • DB – Write bytes (8 bits)
  • DW – Write words (16 bits)
  • DD – Write double words (32 bits)
  • DQ – Write 64-bit floating-point constant
  • DT – Write 80-bit floating-point constant
  • INCBIN – Write the data found in the specified binary data file

These pseudo-instructions cause the assembler to reserve space in the uninitialized data section:

  • RESB - Reserve bytes (8 bits)
  • RESW – Reserve words (16 bits)
  • RESD – Reserve double words (32 bits)
  • RESQ – Reserve space for 64-bit floating-point constants
  • REST – Reserve space for 80-bit floating-point constants

NASM supports the EQU pseudo-instruction, which allows the user to create names for numeric constants.

Finally, NASM offers the TIMES prefix for all instructions and pseudo-instructions. The TIMES prefix causes NASM to repeat the associated instruction a specified number of times.

Labels

Any NASM instruction or pseudo-instruction may have a corresponding label. In NASM, labels are numeric constants equal to the instruction's location in memory. They are virtually identical to constants defined with EQU, and may be used in the same way. This is a key difference from other assemblers, where labels contain additional information about the type of the pointed-to data. By treating labels as simple numeric constants, NASM vastly simplifies its instruction syntax, as discussed in the next section.

NASM also supports a local-label mechanism. This system prevents naming conflicts by allowing labels of the same name to exist in different contexts. Label names beginning with a dot (.) become "local" to the last non-local label NASM sees. The label remains valid until the next non-local label comes along, at which point NASM resets the list of local labels. This allows the user to re-use short label names such as ".else" or ".loop" in multiple places without naming conflicts.

To support macros, NASM supports a third kind of label beginning with "..@". These labels have non-local scope, but do not reset the list of local labels.

Addressing

NASM has a simple rule for dealing with memory addressing in instructions: Everything in square brackets is a reference to a memory address, while everything outside of square brackets is a constant. For example:

data:           dd      1234
                mov     ebx, data
                mov     eax, [data]

When this code finishes executing, the register ebx will contain the memory address of the data, while eax will contain the contents of that memory address, namely the number 1234. This is different from other assemblers such as MASM, where the first instruction might act like the second instruction depending on the type of label. NASM's syntax is much more consistent in this regard.

Doc

See [http://www.tortall.net/projects/yasm/manual/html/nasm.html Part I, NASM Syntax] of the Yasm User Manual. It's also available in PDF, see Yasm documentation.