Scarica crash course di assembly e più Slide in PDF di Programmazione Assembler solo su Docsity!
x86 Crash Course
With a focus on Linux and a glance to x86 64
Mario Polino, Marcello Pogliani March 19, 2019
DEIB, Politecnico di Milano
Credits — contributors as of March 19, 2019
- Main contributor: Andrea Mambretti (wrote the first version)
- Mario Polino
- Marcello Pogliani
- Stefano Zanero
- Federico Maggi
The x86 ISA
- Born in 1978, 16-bit ISA (Intel 8086)
- Evolved to a 32-bit ISA (1985, Intel 80386)
- Evolved to a 64-bit ISA (2003, AMD Opteron)
- CISC design (e.g., string operations)
- Many legacy features (e.g, segmentation)
- We’ll see the basics of the “core” ISA
- There is also the floating point unit, processor-specific features, and extensions such as SIMD (MMX, SSE, SSE2) with their own instructions and registers^1
(^1) Complete reference: Intel Software Developer’s Manual, about 5,000 pages (https://software.intel.com/en-us/articles/intel-sdm)
IA-32: Registers
- General-purpose registers
- EAX, EBX, ECX, EDX
- ESI, EDI (source and destination index for string operations)
- EBP (base pointer)
- ESP (stack pointer)
- Instruction pointer: EIP
- No explicit access
- Modified by jmp, call, ret
- Read through the stack (saved IP)
- Program status and control: EFLAGS
- (segment registers)
IA-32: EFLAGS register
- 32-bits register, boolean flags
- Program status: overflow, sign, zero, auxiliary carry (BCD), parity, carry - Indicate the result of arithmetic instructions - Extremely important for control flow
- Program control: direction flag
- controls string instructions (auto-increment or auto-decrement)
- System: control operating-system operations
Fundamental data types
byte 8 bits word 2 bytes dword Doubleword, 4 bytes (32 bits) qword Quadword, 8 bytes (64 bits)
Assembly: Syntax
Two main syntaxes:
- Intel: default in most Windows programs (e.g., IDA)
- AT&T: default in most UNIX tools (e.g., gdb, objdump) Beware: The order of the operands is different We will use the Intel syntax
Assembly: Syntax
move the value 0 to EAX
Intel AT&T mov eax, 0h movl $0x0,%eax
move the value 0 to the address contained in EBX+
Intel AT&T mov [ebx+4h],0h movl $0x0,0x4(%ebx)
x86 Assembly and Machine Code
Instruction = opcode + operand Example
mov ebp, esp 89 E
mov bl, 10 B3 0A
mov bx, 10 66 BB 0A 00
mov ebx, 10 BB 0A 00 00 00
opcode (1-2 bytes) operands
mov r/m r ebp, esp
mov bl size prefix
mov ebx
Beware: in x86, instructions have variable length.
Basic instructions
- Data Transfer: mov, push, pop, xchg, lea
- Integer Arithmetic: add, sub, mul, imul, div, idiv, inc, dec
- Logical: and, or, not, xor
- Control Transfer: jmp, jne, call, ret
- and lots more...
Integer Arithmetics: add and sub
add destination, source sub destination, source dest ← dest + source dest ← dest − source
- Addressing: source: immediate, register, memory location destination: register or memory location (the destination has to be at least as large as the source)
- Negate a value: neg [op]
- Bitwise operations: and, or, xor, not work similarly Examples add esp, 44h add edx, cx add al, dh sub esp, 33h sub eax, ebx sub [eax], 1h
Integer Arithmetics: unsigned multiply (mul)
- mul source source: register or memory location
- dest ← implied op × source
- Implied operands according to the size of source
- First operand: AL, AX, or EAX
- Destination: AX, DX:AX, EDX:EAX (double the size of source)
- Signed multiply: imul Example
- mul ebx: EDX:EAX ← EAX * EBX
- most significant bits of the result in EDX
- least significant bits of the result in EAX
- mul cx: DX:AX ← AX * CX
- mul cl: AX ← AL * CL
Integer Arithmetics: cmp and test
cmp op1, op2 test op1, op Computes op1 − op2 Computes op1 & op
- Sets the flags (ZF,CF, OF,... )
- Discards the result Examples cmp eax, ebx cmp eax, 44BBCCDDh cmp al, dh cmp al, 44h cmp ax,FFFFh cmp [eax],4h
Control-Flow Instructions: unconditional jump jmp
Program Instruction jmp 0x080483ad 0x080483a3 Instruction never executed
0x080483a
0x080483a
0x080483ad Other Block Instruction
- jmp address or offset
- Unconditional jump: just set the EIP to address
- Can be also relative: increment or decrement EIP by an offset