Lesson 2.1 - Intro + x86-x64 Assembly
Lesson 2.1 - Intro + x86-x64 Assembly
1:
x86 + x64 Assembly
CSC448/548-CYEN404 – REVERSE ENGINEERING
•Example:
Address Opcode Instructions
00A92D7C B8 00000080 MOV EAX,80000000h
00A92D81 B9 02000000 MOV ECX,2
00A92D86 F7E1 MUL ECX
In memory:
00A92D7C B8 00 00 00 80 B9 02 00 00 00 F7 E1
x86: INSTRUCTION SET BASICS (2)
•x86 has two syntax notations:
• Intel • AT&T
mov ecx, AABBCCDDh movl $0xAABBCCDD, %ecx
mov ecx,[eax] movl (%eax), %ecx
mov ecx, eax movl %eax, %ecx
•Differences:
• AT&T prefixes registers with %, immediates with $. Intel does not.
• AT&T adds prefix to indicate operation width. Intel does not.
• AT&T puts source operand then destination. Intel reverses the order.
x86: DATA MOVEMENT (1)
•x86 data movement methods:
• Register to register.
• Register to memory.
• Immediate to register.
• Immediate to memory.
• Memory to register.
• Memory to memory.
•Memory to memory is not supported by RISC architectures.
ARM:
01: 1B 68 LDR R3, [R3] ; read the value at address R3
02: 5A 1C ADDS R2, R3, #1 ; add 1 to it
03: 1A 60 STR R2, [R3] ; write updated value back to address R3
x86:
01: FF 00 inc dword ptr [eax] ; directly increment value at address EAX
x86: DATA MOVEMENT (2)
•MOV instruction:
• Data to registers.
01: BE 3F 00 0F 00 mov esi, 0F003h ; set ESI = 0xF003
02: 8B F1 mov esi, ecx ; set ESI = ECX
• Data to/from memory.
• Method: [base register + offset]
Example 1: simple data moves
Assembly Pseudo C
01: C7 00 01 00 00+ mov dword ptr [eax], 1 01: *eax = 1;
02: 8B 08 mov ecx, [eax] 02: ecx = *eax;
03: 89 18 mov [eax], ebx 03: *eax = ebx;
04: 89 46 34 mov [esi+34h], eax 04: *(esi+34) = eax;
05: 8B 46 34 mov eax, [esi+34h] 05: eax = *(esi+34);
06: 8B 14 01 mov edx, [ecx+eax] 06: edx = *(ecx+eax);
x86: DATA MOVEMENT (3)
•MOV instruction (cont.):
• Data to/from memory.
• Method: [base register + offset]
Example 2: access structure members at location computed at runtime
Pseudo C
struct ExampleStruct {
int integer;
Assembly
char character;
; Assuming struct starts at ebp-0xC
float floating;
01: C7 45 F4 0A 00 00 00 mov dword [ebp-0xC], 10
};
02: C6 45 F8 41 mov byte [ebp-0x8], ‘A‘
03: C7 45 FC CD CC 04 40 mov dword [ebp-0x4], 0x4044CCCD
int main() {
04: 8A 45 F8 mov al, [ebp-0x8]
struct ExampleStruct myStruct = {10, 'A', 3.14f};
05: C7 45 FC AE 47 E1 3F mov dword [ebp-0x4], 0x3FE147AE
char value_char = myStruct.character;
06: 81 25 F4 FF FF FF 00 and dword [ebp-0xC], 0
myStruct.floating = 2.718f;
myStruct.integer = 0;
return 0;
}
x86: DATA MOVEMENT (4)
•MOV instruction (cont.):
• Data to/from memory.
• Method: [base + index * scale]
Example: access array-type objects
Assembly
Pseudo Java
01: B9 00 00 00 00 mov ecx, 0
public class ArrayAccess {
02: BE 00 00 00 00 mov esi, array
public static void main(String[] args) {
loop_start:
int[] array = {10, 20, 30};
03: 83 F9 03 cmp ecx, 3
for (int i = 0; i < 3; i++) {
04: 7D 0D jge loop_end
int value = array[i];
05: 8B 04 8E mov eax, [esi + ecx * 4]
}
06: 41 inc ecx
}
07: EB F4 jmp loop_start
}
loop_end:
x86: DATA MOVEMENT (5)
•MOVS(B/W/D) instructions:
• Move 1/2/4-byte data between memory addresses.
• EDI = destination / ESI = source.
• Usage: string/memory copy function when the length is known at compile time.
• Example: string & memcpy
Assembly
_start:
Pseudo C
01: Bf, source mov esi, source
int main() {
02: B7, destination mov edi, destination
char source[] = "Hello, Assembly!";
copy_loop:
char destination[20];
03: A4 movsb
memcpy(destination, source, strlen(source) + 1);
04: A8, 00 test al, al
return 0;
05: 74, 06 jz end_copy
}
06: EB, F9 jmp copy_loop
end_copy:
x86: ARITHMETIC OPERATIONS (1)
•x86 fundamental arithmetic / bit-level operations:
• ADD, SUB, INC, DEC, AND, OR, XOR, NOT, SHL, SHR.
• Examples:
01: 83 C4 14 add esp, 14h ; esp = esp + 0x14
02: 2B C8 sub ecx, eax ; ecx = ecx – eax
03: 83 EC 0C sub esp, 0Ch ; esp = esp - 0xC
04: 41 inc ecx ; ecx = ecx + 1
05: 4F dec edi ; edi = edi – 1
06: 83 C8 FF or eax, 0FFFFFFFFh ; eax = eax | 0xFFFFFFFF
07: 83 E1 07 and ecx, 7 ; ecx = ecx & 7
08: 33 C0 xor eax, eax ; eax = eax ^ eax
09: F7 D7 not edi ; edi = ~edi
10: C0 E1 04 shl cl, 4 ; cl = cl << 4
11: D1 E9 shr ecx, 1 ; ecx = ecx >> 1
• Example:
; initial ESP = 0xb20000
01: B8 AA AA AA AA mov eax, 0AAAAAAAAh
02: BB BB BB BB BB mov ebx, 0BBBBBBBBh
03: B9 CC CC CC CC mov ecx, 0CCCCCCCCh
04: BA DD DD DD DD mov edx, 0DDDDDDDDh
05: 50 push eax ; address 0xb1fffc will contain the value 0xAAAAAAAA
; ESP will be 0xb1fffc (=0xb20000-4)
06: 53 push ebx ; address 0xb1fff8 will contain the value 0xBBBBBBBB
; ESP will be 0xb1fff8 (=0xb1fffc-4)
07: 5E pop esi ; ESI will contain the value 0xBBBBBBBB
; ESP will be 0xb1fffc ; (=0xb1fff8+4)
08: 5F pop edi ; EDI will contain the value 0xAAAAAAAA
; ESP will be 0xb20000 ; (=0xb1fffc+4)
x86: STACK OPERATIONS (2)
•x86 uses stack & CALL/RET operations to implement function calls.
• CALL perfoms two operations:
• Pushes return address to the stack.
• Address immediately after CALL instruction.
• Changes EIP to the call destination.
• Transfers control to the call target & begins execution.
• RET pops address stored on stack into EIP & transfers control to it.
• Calling convention - set of rules dictating how function calls work at the machine level.
• Defined by Application Binary Interface (ABI) for a particular system.
CDECL STDCALL FASTCALL
Pushed on the stack from right-to- Same as CDECL except that the callee First two parameters are passed in
Parameters left. Caller must clean up the stack must clean the stack. ECX and EDX. The rest are on the
after the call. stack.