X86 Assembly/Data Transfer

Some of the most important and most frequently used instructions are those that move data. Without them, there would be no way for registers or memory to even have anything in them to operate on.

Move
stands for move. Despite its name the  instruction copies the  operand into the  operand. After the operation both operands contain the same contents.

Modified flags

 * No FLAGS are modified by this instruction

Data swap
stands for exchange. The  instruction swaps the  operand with the  operand. It is like doing three  operations: except that no register needs to be reserved for temporary storage.
 * 1) from  to a temporary (another register),
 * 2) then from  to , and finally
 * 3) from the temporary storage to  ,

This exchange pattern of three consecutive  instructions can be detected by the DFU present in some architectures, which will trigger special treatment. The opcode for  is shorter though.

Operands
Any combination of register or memory operands, except that at most one operand may be a memory operand. You cannot exchange two memory blocks.

Modified Flags
None.

 Example 

Application
If one of the operands is a memory address, then the operation has an implicit  prefix, that is, the exchange operation is atomic. This can have a large performance penalty.

However, on some platforms exchanging two (non-partial) registers will trigger the register renamer. The register renamer is a unit in that merely renames registers, so no data actually have to be moved. This is super fast (branded as “zero-latency”). Renaming registers could be useful since
 * some instructions either require certain operands to be located in specific register, but data will be needed later on,
 * or encoding some opcodes is shorter if one of the operands is the accumulator register.

The   instruction is used for changing the Byte order (LE ↔ BE) of 16-bit values, because the   instruction is only available for 32-, and 64-bit values. You do so by addressing partial registers, e. g..

It is also worth noting that the common  (no operation) instruction, , is the opcode for.

Data swap based on comparison
stands for compare and exchange. Exchange is misleading as no data are actually exchanged.

The  instruction has one implicit operand: the  / /  depending on the size of.


 * 1) The instruction compares  to  / /.
 * 2) If they are equal,  becomes  . (   =   )
 * 3) Otherwise,  / /  becomes.

Unlike  there is no implicit   prefix, and if the instruction is required to be atomic,   has to be prefixed.

Operands
has to be a register. may be either a register or memory operand.

Modified flags

 * ≔   = ( | | ) [depending on  ’s size]
 * ,,  ,  ,  are altered, too.

Application
The following example shows how to use the  instruction to create a spin lock which will be used to protect the result variable. The last thread to grab the spin lock will get to set the final value of result :

Move with zero extend
stands for move with zero extension. Like the regular  the   instruction copies data from the  operand to the  operand, but the remaining bits in  that are not provided by  are filled with zeros. This instruction is useful for copying a small, unsigned value to a bigger register.

Operands
has to be a register, and can be either another register or a memory operand. For this operation to make sense has to be larger than.

Modified flags
There are none.

Move with sign extend
stands for move with sign extension. The  instruction copies the  operand in the  operand and pads the remaining bits not provided by  with the sign bit (the MSB) of.

This instruction is useful for copying a signed small value to a bigger register.

Operands
accepts the same operands as.

Modified Flags
does not modify any flags, either.

Move String
Move byte.

The  instruction copies one byte from the memory location specified in   to the location specified in. If the direction flag is cleared, then  and   are incremented after the operation. Otherwise, if the direction flag is set, then the pointers are decremented. In that case the copy would happen in the reverse direction, starting at the highest address and moving toward lower addresses until  is zero.

Operands
There are no explicit operands, but
 * determines the number of iterations,
 * specifies the source address,
 * the destination address, and
 * DF is used to determine the direction (it can be altered by the  and   instruction).

Modified flags
No flags are modified by this instruction.

Example
Move word

The  instruction copies one word (two bytes) from the location specified in   to the location specified in. It basically does the same thing as, except with words instead of bytes.

 Operands 

None.

 Modified flags 


 * No FLAGS are modified by this instruction

 Example 

Load Effective Address
stands for load effective address. The  instruction calculates the address of the  operand and loads it into the  operand.

Operands

 * Immediate
 * Register
 * Memory
 * Register

Modified flags

 * No FLAGS are modified by this instruction

Note
Load Effective Address calculates its  operand in the same way as the   instruction does, but rather than loading the contents of that address into the   operand, it loads the address itself.

can be used not only for calculating addresses, but also general-purpose unsigned integer arithmetic (with the caveat and possible advantage that FLAGS are unmodified). This can be quite powerful, since the  operand can take up to 4 parameters: base register, index register, scalar multiplier and displacement, e.g.   (Intel syntax) or   (GAS syntax). The scalar multiplier is limited to constant values 1, 2, 4, or 8 for byte, word, double word or quad word offsets respectively. This by itself allows for multiplication of a general register by constant values 2, 3, 4, 5, 8 and 9, as shown below (using NASM syntax):

Conditional Move
stands for conditional move. It behaves like  but the execution depends on various flags. There are following instruction available:

Operands
has to be a register. can be either a register or memory operand.

Application
The  instruction can be used to eliminate branches, thus usage of   instruction avoids branch mispredictions. However, the  instructions needs to be used wisely: the dependency chain will become longer.

General
General purpose byte or word transfer instructions:
 * copy byte or word from specified source to specified destination
 * copy byte or word from specified source to specified destination


 * copy specified word to top of stack.
 * copy specified word to top of stack.


 * copy word from top of stack to specified location
 * copy word from top of stack to specified location


 * copy all registers to stack
 * copy all registers to stack


 * copy words from stack to all registers
 * copy words from stack to all registers


 * Exchange bytes or exchange words
 * Exchange bytes or exchange words


 * translate a byte in  using a table in memory
 * translate a byte in  using a table in memory

Input/Output
These are I/O port transfer instructions:
 * copy a byte or word from specific port to accumulator
 * copy a byte or word from specific port to accumulator


 * copy a byte or word from accumulator to specific port
 * copy a byte or word from accumulator to specific port

Address Transfer Instruction
Special address transfer Instructions:
 * load effective address of operand into specified register
 * load effective address of operand into specified register


 * load DS register and other specified register from memory
 * load DS register and other specified register from memory


 * load ES register and other specified register from memory
 * load ES register and other specified register from memory

Flags
Flag transfer instructions:
 * load  with the low byte of flag register
 * load  with the low byte of flag register


 * stores  register to low byte of flag register
 * stores  register to low byte of flag register


 * copy flag register to top of stack
 * copy flag register to top of stack


 * copy top of stack word to flag register
 * copy top of stack word to flag register