Basic Instructions
Moving data and loading data into registers.
One of the most important instruction in assembly language is data movement. Its used to move data between addresses, moving data between registers and memory addresses, and loading immediate data into registers or memory addresses. The main Data Movement instructions are:
mov
Move or load data
mov rax
lea
Load address point to value
lea rax
xchg
Swap data
xchg rax
Moving data
If we want to load values 0 and 1 we can use the mov
instruction and mov 0
to rax
and move 1
to rbx
.
global _start
section .text
_start:
mov rax, 0
mov rbx, 1
Loading data
Using the mov
instruction we can also load immediate data. We can load the value of 1 into the rax register but since the size of the loaded data depends on the size of the register its not efficiënt to mov rax 1
into a 64-bit register rax
. A better solution is to use mov al, 1
which will place 1 into the 8--bit or 1 byte register.
global _start
section .text
_start:
mov al, 0
mov bl, 1
Address pointers
When an register or address points contains another address which points to the final value we call these pointer registers, like rsp
, rbp
and rip
.
$rsp : 0x00007fffffffe490 → 0x0000000000000001
$rip : 0x0000000000401000 → <_start+0> mov eax, 0x0
RSP register points to the top of the stack, 0x0000000000000001. RIP register points to the next instruction to be executed
Moving Pointer Values
The register rsp
(stack pointer) holds a memory address which points to a value stored in memory. It holds an address instead of actual data.
$rsp : 0x00007fffffffe490 → 0x0000000000000001
$rip : 0x0000000000401000 → <_start+0> mov eax, 0x0
In this case rsp
is 0x00007fffffffe490
where value 0x1
is stored. To move this value we will have to use [ ] w x86_64 assembly means "load value at address". If we want to move the value rsp
is pointing to we use square brackets like, mov rax, [rsp] which moves to final value.
Recap
mov rax, rsp
: Moves the address inrsp
torax
.mov rax, [rsp]
: Moves the value at the address inrsp
torax
global _start
section .text
_start:
mov rax, rsp ; gets the address
mov rax, [rsp] ; get the value at address
# First instruction, copied rsp into rax.
$rax : 0x00007fffffffe490 → 0x0000000000000001
$rsp : 0x00007fffffffe490 → 0x000000000000000
# Second instruction, copied value intro rax
─────────────────────────────────────────────────────────────────────────────────── code:x86:64 ────
→ 0x401003 <_start+3> mov rax, QWORD PTR [rsp]
───────────────────────────────────────────────────────────────────────────────────── registers
$rax : 0x1
$rsp : 0x00007fffffffe490 → 0x0000000000000001
We may need to set data size, like byte or qword but usually nasm will do this.
Loading value pointers
Using lea
instruction we can load a pointer address to a value. Lea or Load Effective Address is the opposite of moving pointers. If we need to load the address of a value instead of loading actual data. It will only load the address itself.
global _start
section .text
_start:
lea rax, [rsp+10]
mov rax, [rsp+10]
# First instruction
$rax : 0x00007fffffffe49a → 0x000000007fffffff
$rsp : 0x00007fffffffe490 → 0x0000000000000001
# Second instruction
$rax : 0x7fffffff
$rsp : 0x00007fffffffe490 → 0x0000000000000001
lea rax, [rsp+10]
loaded the address that is 10 addresses away fromrsp
.rsp
is0x00007fffffffe490
, sorsp + 10
is0x00007fffffffe49a
.mov rax, [rsp+10]
moved the value stored at0x00007fffffffe49a
which is0x7fffffff
into rax.
Example
What is the hex value of rax
at the end of the program after adding mov rax, rsp
.
global _start
section .text
_start:
mov rax, 1024 ; rax = 1024 (0x400)
mov rbx, 2048 ; rbx = 2048 (0x800)
xchg rax, rbx ; swaps values rax and rbx
push rbx ; rsp now points to where 1024 is stored
mov rax, rsp ; rax gets the ADDRESS (0x7fffffffdda8) of where 1024 is stored
rsp contains an ADDRESS (0x7fffffffdda8) and at that address, the VALUE stored is 1024 (0x400). To check the values of end of program:
gef➤ b _start
gef➤ r
# Stop after line mov rax, rsp
$rax : 0x00007fffffffdda8 → 0x0000000000000400
$rbx : 0x400
$rcx : 0x0
$rdx : 0x0
$rsp : 0x00007fffffffdda8 → 0x0000000000000400
# Get address and value
gef➤ x/x $rax
0x7fffffffdda8: 0x00000400
Last updated
Was this helpful?