A.2.7 Register Extensions: The REX Prefix
The Register Extensions, or REX for short, prefix is the means of
accessing extended registers on the x86-64 architecture. REX is
considered an instruction prefix, but is required to be after all other
prefixes and thus immediately before the first instruction opcode
itself. So overall, REX can be thought of as an "Opcode Prefix" instead.
The REX prefix itself is indicated by a value of 0x4X, where X is one of
16 different combinations of the actual REX flags.
The REX prefix flags consist of four 1-bit extensions fields. These
flags are found in the lower nibble of the actual REX prefix opcode.
Below is the list of REX prefix flags, from high bit to low bit.
REX.W: When set, this flag indicates the use of a 64-bit operand, as
opposed to the default of using 32-bit operands as found in 32-bit
Protected Mode.
REX.R: When set, this flag extends the "reg" (spare) field of the ModR/M
byte. Overall, this raises the amount of addressable registers in this
field from 8 to 16.
REX.X: When set, this flag extends the "index" field of the SIB byte.
Overall, this raises the amount of addressable registers in this field
from 8 to 16.
REX.B: When set, this flag extends the "r/m" field of the ModR/M byte.
This flag can also represent an extension to the opcode register "(/r)"
field. The determination of which is used varies depending on which
instruction is used. Overall, this raises the amount of addressable
registers in these fields from 8 to 16.
Internal use of the REX prefix by the processor is consistent, yet non-
trivial. Most instructions use the REX prefix as indicated by the above
flags. Some instructions require the REX prefix to be present even if
the flags are empty. Some instructions default to a 64-bit operand and
require the REX prefix only for actual register extensions, and thus
ignores the REX.W field completely.
At any rate, NASM is designed to handle, and fully supports, the REX
prefix internally. Please read the appropriate processor documentation
for further information on the REX prefix.
You may have noticed that opcodes 0x40 through 0x4F are actually opcodes
for the INC/DEC instructions for each General Purpose Register. This is,
of course, correct... for legacy x86. While in long mode, opcodes 0x40
through 0x4F are reserved for use as the REX prefix. The other opcode
forms of the INC/DEC instructions are used instead.