Data Movement

Data must be moved into a CPU register from RAM in order to be operated upon. Once the calculations are completed, the result may be copied from the register and placed into a variable. This basic data movement operation is performed with the move instruction.

1 Mov

The general form of the move instruction is:

Intel Syntax:

The destination operand comes before the source operand.

mov <dest>, <src>

// example, to copy the contents of register ebx into register eax.
mov eax, ebx 

AT&T Syntax:

The source operand comes before the destination operand. Additionally, register names are prefixed with a percent sign (%).

mov <src>, <dest>

// example, to copy the contents of register ebx into register eax.
mov %ebx, %eax

The source operand is copied from the source operand into the destination operand. The value of the source operand is unchanged. The destination and source operand must be of the same size.

Example:

Let's consider some examples to illustrate the usage of the mov instruction:

; Example 1: Copying an immediate value to a variable
mov dword [dValue], 27    ; dValue = 27

; Example 2: Copying a byte-sized value from a memory location to another
mov al, byte [bNum]       ; Move the byte from memory location bNum to AL register
mov byte [bAns], al       ; Copy the contents of AL register to memory location bAns

; Example 3: Copying a word-sized value from a memory location to another
mov ax, word [wNum]       ; Move the word from memory location wNum to AX register
mov word [wAns], ax       ; Copy the contents of AX register to memory location wAns

; Example 4: Copying a double-word-sized value from a memory location to another
mov eax, dword [dNum]     ; Move the double-word from memory location dNum to EAX register
mov dword [dAns], eax     ; Copy the contents of EAX register to memory location dAns

; Example 5: Copying a quadword-sized value from a memory location to another
mov rax, qword [qNum]     ; Move the quadword from memory location qNum to RAX register
mov qword [qAns], rax     ; Copy the contents of RAX register to memory location qAns

Address and Values

The only way to access memory is with the brackets ([]). Omitting the brackets will not access memory and instead obtain the address of the item. For example:

mov rax, qword [var1]  ; value of var1 in rax
mov rax, var1          ; address of var1 in rax

Since omitting the brackets is not an error, the assembler will not generate error message or warnings. This can lead to confusion.

In addition, the address of a variable can be obtained with the load effective address, or lea instruction.

2 Load Effective Address

The lea (Load Effective Address) instruction is used to load the effective address of a memory operand into a register, rather than the actual value stored at that memory address. The instruction calculates and loads the offset or address of the memory operand into the specified register. The lea instruction is commonly used for various purposes, including address calculations, pointer arithmetic, and accessing data structures.

Syntax:

Intel Syntax:

LEA destination, source
  • destination: The register where the effective address will be loaded.
  • source: The memory operand whose effective address will be calculated. This can be immediate value, a memory location, or a label representing a memory address.

AT&T Syntax:

In this, the source operand comes before the destination operand. Additionally, register names are prefixed with a percent sign (%).

# Load the effective address of the array into the SI register
lea array, %SI

Examples:

; Example 1: Loading the effective address of a memory location into a register
LEA SI, array      ; Load the effective address of the array into SI register

; Example 2: Using LEA for address calculations and accessing array elements
LEA DI, [array+2]  ; Calculate the effective address of array[2] and load it into DI register

; Example 3: Performing pointer arithmetic
LEA BX, [SI+4]     ; Add 4 to the address in SI and load the result into BX register

; Example 4: Accessing data structures (e.g., accessing a structure field)
LEA DX, [struct_ptr+field_offset]  ; Load the address of a field within a structure into DX register

3 XCHG (Exchange)

Exchange the contents of two operands. It is commonly used for swapping values between registers or memory locations. The xchg instruction is particularly useful in scenarios where a temporary storage location isn't available or when a direct exchange of values is desired.

Syntax:

Intel Syntax:

xchg operand1, operand2

AT&T Syntax:

xchg operand2, operand1

Examples:

XCHG AX, BX     ; Exchange the contents of registers AX and BX
XCHG [mem1], [mem2]  ; Exchange the contents of two memory locations
# Exchange the contents of registers %ax and %bx
xchg %ax, %bx

4 Push and Pop

They are fundamental for managing the stack, a region of memory used for storing data and return addresses during subroutine calls.

  1. Push Instructions:
    1. The push instruction is used to push a value onto the top of the stack
    2. It decrements the stack pointer (RSP in intel syntax, %rsp in AT&T syntax) and then stores the value at the memory location pointed to by the stack pointer.
  2. Pop Instructions:
    1. The pop instruction is used to pop a value from the top of the stack.
    2. It retrieves the value from the memory location pointed to by the stack pointer and then increments the stack pointer.

Example:

PUSH AX      ; Pushes the contents of register AX onto the stack
PUSH DWORD [variable]   ; Pushes the value at memory location 'variable' onto the stack

POP BX       ; Pops the top value from the stack into register BX
POP DWORD [variable]   ; Pops the top value from the stack into memory location 'variable'