<html><head><title>NASM Manual</title></head>
<body><h1 align=center>The Netwide Assembler: NASM</h1>
<p align=center><a href="nasmdoca.html" rel='nofollow' onclick='return false;'>Previous Chapter</a> |
<a href="nasmdoc0.html" rel='nofollow' onclick='return false;'>Contents</a> |
<a href="nasmdoci.html" rel='nofollow' onclick='return false;'>Index</a>
<h2><a name="appendix-B" rel='nofollow' onclick='return false;'>Appendix B: x86 Instruction Reference</a></h2>
<p>This appendix provides a complete list of the machine instructions which
NASM will assemble, and a short description of the function of each one.
<p>It is not intended to be exhaustive documentation on the fine details of
the instructions' function, such as which exceptions they can trigger: for
such documentation, you should go to Intel's Web site,
<a href="http://developer.intel.com/design/Pentium4/manuals/" rel='nofollow' onclick='return false;'><code><nobr>http://developer.intel.com/design/Pentium4/manuals/</nobr></code></a>.
<p>Instead, this appendix is intended primarily to provide documentation on
the way the instructions may be used within NASM. For example, looking up
<code><nobr>LOOP</nobr></code> will tell you that NASM allows
<code><nobr>CX</nobr></code> or <code><nobr>ECX</nobr></code> to be
specified as an optional second argument to the
<code><nobr>LOOP</nobr></code> instruction, to enforce which of the two
possible counter registers should be used if the default is not the one
desired.
<p>The instructions are not quite listed in alphabetical order, since
groups of instructions with similar functions are lumped together in the
same entry. Most of them don't move very far from their alphabetic position
because of this.
<h3><a name="section-B.1" rel='nofollow' onclick='return false;'>B.1 Key to Operand Specifications</a></h3>
<p>The instruction descriptions in this appendix specify their operands
using the following notation:
<ul>
<li>Registers: <code><nobr>reg8</nobr></code> denotes an 8-bit general
purpose register, <code><nobr>reg16</nobr></code> denotes a 16-bit general
purpose register, and <code><nobr>reg32</nobr></code> a 32-bit one.
<code><nobr>fpureg</nobr></code> denotes one of the eight FPU stack
registers, <code><nobr>mmxreg</nobr></code> denotes one of the eight 64-bit
MMX registers, and <code><nobr>segreg</nobr></code> denotes a segment
register. In addition, some registers (such as
<code><nobr>AL</nobr></code>, <code><nobr>DX</nobr></code> or
<code><nobr>ECX</nobr></code>) may be specified explicitly.
<li>Immediate operands: <code><nobr>imm</nobr></code> denotes a generic
immediate operand. <code><nobr>imm8</nobr></code>,
<code><nobr>imm16</nobr></code> and <code><nobr>imm32</nobr></code> are
used when the operand is intended to be a specific size. For some of these
instructions, NASM needs an explicit specifier: for example,
<code><nobr>ADD ESP,16</nobr></code> could be interpreted as either
<code><nobr>ADD r/m32,imm32</nobr></code> or
<code><nobr>ADD r/m32,imm8</nobr></code>. NASM chooses the former by
default, and so you must specify <code><nobr>ADD ESP,BYTE 16</nobr></code>
for the latter.
<li>Memory references: <code><nobr>mem</nobr></code> denotes a generic
memory reference; <code><nobr>mem8</nobr></code>,
<code><nobr>mem16</nobr></code>, <code><nobr>mem32</nobr></code>,
<code><nobr>mem64</nobr></code> and <code><nobr>mem80</nobr></code> are
used when the operand needs to be a specific size. Again, a specifier is
needed in some cases: <code><nobr>DEC [address]</nobr></code> is ambiguous
and will be rejected by NASM. You must specify
<code><nobr>DEC BYTE [address]</nobr></code>,
<code><nobr>DEC WORD [address]</nobr></code> or
<code><nobr>DEC DWORD [address]</nobr></code> instead.
<li>Restricted memory references: one form of the
<code><nobr>MOV</nobr></code> instruction allows a memory address to be
specified <em>without</em> allowing the normal range of register
combinations and effective address processing. This is denoted by
<code><nobr>memoffs8</nobr></code>, <code><nobr>memoffs16</nobr></code> and
<code><nobr>memoffs32</nobr></code>.
<li>Register or memory choices: many instructions can accept either a
register <em>or</em> a memory reference as an operand.
<code><nobr>r/m8</nobr></code> is a shorthand for
<code><nobr>reg8/mem8</nobr></code>; similarly
<code><nobr>r/m16</nobr></code> and <code><nobr>r/m32</nobr></code>.
<code><nobr>r/m64</nobr></code> is MMX-related, and is a shorthand for
<code><nobr>mmxreg/mem64</nobr></code>.
</ul>
<h3><a name="section-B.2" rel='nofollow' onclick='return false;'>B.2 Key to Opcode Descriptions</a></h3>
<p>This appendix also provides the opcodes which NASM will generate for
each form of each instruction. The opcodes are listed in the following way:
<ul>
<li>A hex number, such as <code><nobr>3F</nobr></code>, indicates a fixed
byte containing that number.
<li>A hex number followed by <code><nobr>+r</nobr></code>, such as
<code><nobr>C8+r</nobr></code>, indicates that one of the operands to the
instruction is a register, and the `register value' of that register should
be added to the hex number to produce the generated byte. For example, EDX
has register value 2, so the code <code><nobr>C8+r</nobr></code>, when the
register operand is EDX, generates the hex byte
<code><nobr>CA</nobr></code>. Register values for specific registers are
given in <a href="#section-B.2.1" rel='nofollow' onclick='return false;'>section B.2.1</a>.
<li>A hex number followed by <code><nobr>+cc</nobr></code>, such as
<code><nobr>40+cc</nobr></code>, indicates that the instruction name has a
condition code suffix, and the numeric representation of the condition code
should be added to the hex number to produce the generated byte. For
example, the code <code><nobr>40+cc</nobr></code>, when the instruction
contains the <code><nobr>NE</nobr></code> condition, generates the hex byte
<code><nobr>45</nobr></code>. Condition codes and their numeric
representations are given in <a href="#section-B.2.2" rel='nofollow' onclick='return false;'>section B.2.2</a>.
<li>A slash followed by a digit, such as <code><nobr>/2</nobr></code>,
indicates that one of the operands to the instruction is a memory address
or register (denoted <code><nobr>mem</nobr></code> or
<code><nobr>r/m</nobr></code>, with an optional size). This is to be
encoded as an effective address, with a ModR/M byte, an optional SIB byte,
and an optional displacement, and the spare (register) field of the ModR/M
byte should be the digit given (which will be from 0 to 7, so it fits in
three bits). The encoding of effective addresses is given in
<a href="#section-B.2.5" rel='nofollow' onclick='return false;'>section B.2.5</a>.
<li>The code <code><nobr>/r</nobr></code> combines the above two: it
indicates that one of the operands is a memory address or
<code><nobr>r/m</nobr></code>, and another is a register, and that an
effective address should be generated with the spare (register) field in
the ModR/M byte being equal to the `register value' of the register
operand. The encoding of effective addresses is given in
<a href="#section-B.2.5" rel='nofollow' onclick='return false;'>section B.2.5</a>; register values are given in
<a href="#section-B.2.1" rel='nofollow' onclick='return false;'>section B.2.1</a>.
<li>The codes <code><nobr>ib</nobr></code>, <code><nobr>iw</nobr></code>
and <code><nobr>id</nobr></code> indicate that one of the operands to the
instruction is an immediate value, and that this is to be encoded as a
byte, little-endian word or little-endian doubleword respectively.
<li>The codes <code><nobr>rb</nobr></code>, <code><nobr>rw</nobr></code>
and <code><nobr>rd</nobr></code> indicate that one of the operands to the
instruction is an immediate value, and that the <em>difference</em> between
this value and the address of the end of the instruction is to be encoded
as a byte, word or doubleword respectively. Where the form
<code><nobr>rw/rd</nobr></code> appears, it indicates that either
<code><nobr>rw</nobr></code> or <code><nobr>rd</nobr></code> should be used
according to whether assembly is being performed in
<code><nobr>BITS 16</nobr></code> or <code><nobr>BITS 32</nobr></code>
state respectively.
<li>The codes <code><nobr>ow</nobr></code> and <code><nobr>od</nobr></code>
indicate that one of the operands to the instru