Skip to main content

Intel Assembly

Registers

Purpose64 bit Register
Syscall number / Return valueRAX
Callee SavedRBX
1st argRDI
2nd argRSI
3rd argRDX
4th arg / loop counterRCX
5th argR8
6th argR9
Base stack pointerRBP
Current stack pointerRSP
Instruction PointerRIP

Instructions

Jumping

InstructionCondition
jz / jeD == 0
jnz / jneD != 0
jsD < 0
jnsD >= 0
jgD > S
jgeD >= S
jlD < S
jleD <= S

Flags

FlagBitName
CF (CY / NC)0Carry Flag
PF (PE / PO)2Parity Flag
AF (AC / NA)4Auxiliary Carry Flag
ZF (ZR / NZ)6Zero Flag, raised if zero
SF (NC / PL)7Sign Flag, raised if negative
TF8Trap Flag
IF (EL / DI)9Interrupt Flag
DF (DN / UP)10Direction Flag
OF (OV / NV)11Overflow Flag
IOPL12-13I/O Privilege Level
NT14Nested Task
RF16Resume Flag
VM17Virtual-x86 Mode
AC18Alignment Check / Access Control
VIF19Virtual Interrupt Flag
VIP20Virtual Interrupt Pending
ID21Identification Flag

Passing arguments

64 bit

In the C calling conversion
RDI, RSI, RDX, RCX, R8 and R9 registers and anything additional will be placed onto the stack.

Stack alignment

rsp should point to an adress that is a multiple of 16 (ends with 0)

This stack is aligned

0x00007fffffffdca0│+0x0000: 0x0000000000000000	 ← $rsp
0x00007fffffffdca8│+0x0008: 0x0000000000000000
0x00007fffffffdcb0│+0x0010: 0x0000000000000000

This stack is not aligned

0x00007fffffffdd78│+0x0000: 0x0000000000000000  ← $rsp
0x00007fffffffdd80│+0x0008: 0x0000000000000000
0x00007fffffffdd88│+0x0010: 0x0000000000000000

To align it, subtract from rsp to give it some room

sub rsp, 8
call someFunction
add rsp, 8

Assembly and Disassembly

# Assemble code
nasm -f elf64 program.s

# Link code
ld -o program program.o

# Link code with libc functions
ld -o program program.o -lc --dynamic-linker /lib64/ld-linux-x86-64.so.2

# Disassemble .text section
objdump -M intel -d program

# Disassemble .data section
objdump -sj .data program

# Show binary assembly code
objdump -M intel --no-show-raw-insn --no-addresses -d program