Assembly – Moving Data

Course notes from Vivek Ramachandran’s online class “x86/64 Assembly and Shellcoding on Linux

Common Data Moving Instructions


The mov keyword in Assembly allows for moving data within the Assembly protocol.  This command can be used to mov data:

  • Between registers
  • From Memory to a register and vice versa
  • Data to register
  • Data to memory

LEA (Load Effective Address)

This loads a pointer value into something… like the following example:

lea rax, [:my_data]

the label :my_data address is being loaded into the RAX register.

XCHG (exchange)

This swaps values, like so:

xchg register1, register2

xchg register1, memory address

64bit Concerns

When working in 64bit, you may have a situation where you load or move data into a 64 bit location (like RAX.)  The data that moves into the 64bit register, if less than 64bit (i.e. 32 bit) the remaining data space will be padded with 0’s.

Example Script Showing Each Move Type

From Vivek Ramachandran’s course on 64bit Assembly Language is this simple script to describe each of the data moves, as well as the 32bit data into a 64bit register.

; Filename: MovingData.nasm
; Author:  Vivek Ramachandran
; Website:
; Training: 
; Purpose: Mov instructions in 64-bit CPU

global _start			

section .text

	; mov immediate data to register 
	mov rax, 0xaaaaaaaabbbbbbbb
	mov eax, 0xaaaaaaaa
	mov rax, 0xaaaaaaaabbbbbbbb
	mov al, 0x11
	mov rax, 0xaaaaaaaabbbbbbbb
	mov ah, 0xcc
	mov rax, 0xaaaaaaaabbbbbbbb
	mov ax, 0xdddd

	; mov register to register 

	mov rbp, rax
	mov r10, rbp
	mov r11d, r10d
	mov r12w, r11w	
	mov r13b, r12b

	; mov from memory into register 

	mov rsi, [sample2]
	mov r14d, [sample]
	mov r15w, [sample]
	mov dil, [sample]

	; mov from register into memory 

	mov rax, [sample2]
	mov byte [sample], al	
	mov word [sample], ax
	mov dword [sample], eax
	mov qword [sample], rax

	; lea demo

	lea rax, [sample]
	lea rbx, [rax] 

	; xchg demo 
	mov rax, 0x1234567890abcdef
	mov rbx, 0x9999999999999999

	xchg rax, rbx
	; exit the program gracefully  

	mov rax, 0x3c
	mov rdi, 0		

section .data

sample:	db 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff, 0x11, 0x22
sample2: dq 0x1122334455667788
sample3: times 8 db 0x00

Posted in: ASM

Leave a Reply

Your email address will not be published. Required fields are marked *