An Introduction to x86 ASM

Similar documents
Basic Assembly Instructions

X86 Addressing Modes Chapter 3" Review: Instructions to Recognize"

Program Exploitation Intro

Introduction to 8086 Assembly

LABORATORY WORK NO. 7 FLOW CONTROL INSTRUCTIONS

Intel Instruction Set (gas)

Practical Malware Analysis

EXPERIMENT WRITE UP. LEARNING OBJECTIVES: 1. Get hands on experience with Assembly Language Programming 2. Write and debug programs in TASM/MASM

Lab 6: Conditional Processing

CS61 Section Solutions 3

administrivia today start assembly probably won t finish all these slides Assignment 4 due tomorrow any questions?

143A: Principles of Operating Systems. Lecture 5: Calling conventions. Anton Burtsev January, 2017

Lecture 15 Intel Manual, Vol. 1, Chapter 3. Fri, Mar 6, Hampden-Sydney College. The x86 Architecture. Robb T. Koether. Overview of the x86

Introduction to IA-32. Jo, Heeseung

CS412/CS413. Introduction to Compilers Tim Teitelbaum. Lecture 21: Generating Pentium Code 10 March 08

INTRODUCTION TO IA-32. Jo, Heeseung

Complex Instruction Set Computer (CISC)

CSC 2400: Computer Systems. Towards the Hardware: Machine-Level Representation of Programs

The x86 Architecture

complement) Multiply Unsigned: MUL (all operands are nonnegative) AX = BH * AL IMUL BH IMUL CX (DX,AX) = CX * AX Arithmetic MUL DWORD PTR [0x10]

Inline Assembler. Willi-Hans Steeb and Yorick Hardy. International School for Scientific Computing

Conditional Processing

CSE P 501 Compilers. x86 Lite for Compiler Writers Hal Perkins Autumn /25/ Hal Perkins & UW CSE J-1

CNIT 127: Exploit Development. Ch 1: Before you begin. Updated

CSC 8400: Computer Systems. Machine-Level Representation of Programs

Assembly Language: IA-32 Instructions

Jump instructions. Unconditional jumps Direct jump. do not change flags. jmp label

Lab 3. The Art of Assembly Language (II)

Process Layout and Function Calls

mith College Computer Science CSC231 Assembly Week #9 Spring 2017 Dominique Thiébaut

Dr. Ramesh K. Karne Department of Computer and Information Sciences, Towson University, Towson, MD /12/2014 Slide 1

Function Calls COS 217. Reading: Chapter 4 of Programming From the Ground Up (available online from the course Web site)

Branching and Looping

EECE.3170: Microprocessor Systems Design I Summer 2017 Homework 4 Solution

BAHAR DÖNEMİ MİKROİŞLEMCİLER LAB4 FÖYÜ

W4118: PC Hardware and x86. Junfeng Yang

The Instruction Set. Chapter 5

CS 31: Intro to Systems ISAs and Assembly. Kevin Webb Swarthmore College February 9, 2016

How Software Executes

Introduction to Reverse Engineering. Alan Padilla, Ricardo Alanis, Stephen Ballenger, Luke Castro, Jake Rawlins

SOEN228, Winter Revision 1.2 Date: October 25,

CS 31: Intro to Systems ISAs and Assembly. Kevin Webb Swarthmore College September 25, 2018

Basic Execution Environment

3.1 DATA MOVEMENT INSTRUCTIONS 45

Digital Forensics Lecture 3 - Reverse Engineering

143A: Principles of Operating Systems. Lecture 4: Calling conventions. Anton Burtsev October, 2017

238P: Operating Systems. Lecture 3: Calling conventions. Anton Burtsev October, 2018

x86 Assembly Tutorial COS 318: Fall 2017

Winter Compiler Construction T11 Activation records + Introduction to x86 assembly. Today. Tips for PA4. Today:

Machine Programming 2: Control flow

Low-Level Essentials for Understanding Security Problems Aurélien Francillon

Branching and Looping

Module 3 Instruction Set Architecture (ISA)

x64 Cheat Sheet Fall 2014

ICS143A: Principles of Operating Systems. Midterm recap, sample questions. Anton Burtsev February, 2017

Second Part of the Course

Reverse Engineering II: The Basics

Reverse Engineering Low Level Software. CS5375 Software Reverse Engineering Dr. Jaime C. Acosta

We can study computer architectures by starting with the basic building blocks. Adders, decoders, multiplexors, flip-flops, registers,...

Q1: Multiple choice / 20 Q2: Protected mode memory accesses

Ex: Write a piece of code that transfers a block of 256 bytes stored at locations starting at 34000H to locations starting at 36000H. Ans.

CPS104 Recitation: Assembly Programming

Branching and Looping

UMBC. contain new IP while 4th and 5th bytes contain CS. CALL BX and CALL [BX] versions also exist. contain displacement added to IP.

Reverse Engineering II: The Basics

x86 assembly CS449 Fall 2017

History of the Intel 80x86

EXPERIMENT WRITE UP. LEARNING OBJECTIVES: 1. Get hands on experience with Assembly Language Programming 2. Write and debug programs in TASM/MASM

Chapter 11. Addressing Modes

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2016 Lecture 12

Instruction Set Architecture

Reverse Engineering II: Basics. Gergely Erdélyi Senior Antivirus Researcher

Machine-level Representation of Programs. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

Islamic University Gaza Engineering Faculty Department of Computer Engineering ECOM 2125: Assembly Language LAB. Lab # 7. Procedures and the Stack

Subprograms: Local Variables

CMSC 313 Lecture 07. Short vs Near Jumps Logical (bit manipulation) Instructions AND, OR, NOT, SHL, SHR, SAL, SAR, ROL, ROR, RCL, RCR

Assembly Language: Function Calls

Low Level Programming Lecture 2. International Faculty of Engineerig, Technical University of Łódź

UMBC. 1 (Feb. 9, 2002) seg_base + base + index. Systems Design & Programming 80x86 Assembly II CMPE 310. Base-Plus-Index addressing:

16.317: Microprocessor Systems Design I Fall 2014

Scott M. Lewandowski CS295-2: Advanced Topics in Debugging September 21, 1998

Assembly Language: Function Calls" Goals of this Lecture"

Sistemi Operativi. Lez. 16 Elementi del linguaggio Assembler AT&T

Sungkyunkwan University

mith College Computer Science CSC231 Assembly Week #11 Fall 2017 Dominique Thiébaut

Marking Scheme. Examination Paper Department of CE. Module: Microprocessors (630313)

from WRITE GREAT CODE Volume 2: Thinking Low-Level, Writing High-Level ONLINE APPENDIX A The Minimal 80x86 Instruction Set by Randall Hyde

Islamic University Gaza Engineering Faculty Department of Computer Engineering ECOM 2125: Assembly Language LAB. Lab # 8. Conditional Processing

Computer Architecture and Assembly Language. Practical Session 3

Assembly Language II: Addressing Modes & Control Flow

Compiler Construction D7011E

mith College Computer Science CSC231 Assembly Week #10 Fall 2017 Dominique Thiébaut

Assembly Language: Function Calls" Goals of this Lecture"

Assembly Language: Function Calls. Goals of this Lecture. Function Call Problems

System Programming and Computer Architecture (Fall 2009)

Addressing Modes on the x86

Machine Language CS 3330 Samira Khan

Y86 Processor State. Instruction Example. Encoding Registers. Lecture 7A. Computer Architecture I Instruction Set Architecture Assembly Language View

Process Layout, Function Calls, and the Heap

MODE (mod) FIELD CODES. mod MEMORY MODE: 8-BIT DISPLACEMENT MEMORY MODE: 16- OR 32- BIT DISPLACEMENT REGISTER MODE

Transcription:

An Introduction to x86 ASM Malware Analysis Seminar Meeting 1 Cody Cutler, Anton Burtsev

Registers General purpose EAX, EBX, ECX, EDX ESI, EDI (index registers, but used as general in 32-bit protected mode) Stack EBP, ESP Instruction pointer EIP Flags EFLAGS Segment CS, DS, SS, ES, FS, GS

Syntax General form: mnemonic operand(s) movl %eax, %ebx Operands (0-3) may refer: Registers Memory Immediate

Syntax (contd.) Register naming (AT&T is UNIX default) AT&T: %eax Intel: eax Source/Destination Ordering: Load EBX with the value of EAX AT&T: movl %eax, %ebx Intel: mov ebx, eax

Constant value/immediate value format Load EAX with the address of the "C" variable boo AT&T: movl $_boo, %eax Intel: mov eax, _boo Note that _ works for static (global) variables only Now let's load ebx with 0xd00d: AT&T: movl $0xd00d, %ebx Intel: mov ebx, d00dh

Operator size specification You don't want make GAS to guess this wrong AT&T: movw %ax, %bx Intel: mov bx, ax

Referencing memory 32bit protected mode addressing AT&T: immed32(basepointer,indexpointer,indexscale) Intel: [basepointer + indexpointer*indexscale + immed32] A global C variable AT&T: _booga Intel: [_booga] Addressing what a register points to: AT&T: (%eax) Intel: [eax]

Referencing memory (contd.) Addressing a variable offset by a value in a register: AT&T: _variable(%eax) Intel: [eax + _variable] Addressing a value in an array of integers (scaling by 4): AT&T: _array(,%eax,4) Intel: [eax*4 + array] Offsets with immediate value C code: *(p+1) where p is a char * AT&T: 1(%eax) where eax has the value of p Intel: [eax + 1]

Referencing memory (contd.) Addressing a particular char in an array of 8-character records EAX holds the number of the record desired. EBX has the wanted char's offset within the record. AT&T: _array(%ebx,%eax,8) Intel: [ebx + eax*8 + _array]

Arithmetic Integers: Two's compliment: Reverse bits, then add one (throw away carry) Original value: 00111000 (+56) Reverse bits: 11000111 Add 1: 11001000 (-56) Rules of arithmetic are preserved 002C 44 + FFFF + ( - 1) 002B 43

Carry and overflow Overflow Set if the true result of the operation is too big to fit into the destination for signed arithmetic. Carry Set if there is a carry in the msb of an addition or a borrow in the msb of a subtraction. Can be used to detect overflow for unsigned arithmetic.

Extended precision arithmetic ADC operand1 = operand1 + carry flag + operand2 SBB operand1 = operand1 - carry flag operand2 Sum of 64-bit integers in EDX:EAX and EBX:ECX add eax, ecx ; add lower 32-bits adc edx, ebx ; add upper 32-bits and carry

Control structures Control structures decide what to do based on comparisons of data CMP instruction subtract operands set EFLAGS EFLAGS register ZF zero flag CF carry flag SF sign flag

Control structures (contd.) Unsigned: cmp vleft, vright <=> vleft vright vleft = vright: ZF (1), CF (0) vleft > vright: ZF (0), CF (0) no borrow vleft < vright: ZF (0), CF (1) borrow Signed: cmp vleft, vright <=> vleft vright vleft = vright: ZF (1), CF (0) vleft > vright: ZF (0), SF = CF vleft < vright: ZF (0), SF!= CF

Branch instructions JMP Short: One byte instruction! But jumps only 128 bytes up or down Near: Jump anywere in a segment 2-byte displacement: jump 32000 bytes 4-byte displacement: jump anywhere in 32-bit mode Far: Jump across segments

Examples if ( EAX == 0 ) EBX = 1; else EBX = 2; cmp eax, 0 ; set flags (ZF set if eax - 0 = 0) jz thenblock ; if ZF is set branch to thenblock mov ebx, 2 ; ELSE part of IF jmp next ; jump over THEN part of IF thenblock: mov ebx, 1 ; THEN part of IF next:

Comparison instructions JE branches if vleft = vright JNE branches if vleft!= vright JL, JNGE branches if vleft < vright JLE, JNG branches if vleft <= vright JG, JNLE branches if vleft > vright JGE, JNL branches if vleft >= vright

Loops LOOP Decrements ECX, if ECX!= 0 branches to label LOOPE, LOOPZ Decrements ECX (FLAGS register is not modified), if ECX!= 0 and ZF = 1, branches LOOPNE, LOOPNZ Decrements ECX (FLAGS unchanged), if ECX!= 0 and ZF = 0, branches

Loop example sum = 0; for ( i=10; i >0; i-- ) sum += i; mov eax, 0 ; eax is sum mov ecx, 10 ; ecx is i loop_start: add eax, ecx loop loop_start

Stack SS Specifies stack segment (usually same as data) ESP Contains the address of the data that would be removed from the stack PUSH/POP Insert/remove data on the stack Subtract/add 4 to ESP

Call/return CALL Makes an unconditional jump to a subprogram and pushes the address of the next instruction on the stack RET Pops off an address and jumps to that address

Calling conventions Goal: reentrant programs Conventions differ from compiler, optimizations, etc. Call/return are used for function invocations Parameters passed on the stack Pushed onto the stack before the CALL instruction

Stack bottom pointer Initially parameter is [ESP + 4] Later as the function pushes things on the stack it changes, e.g. [ESP + 8] Use dedicated register EBP

Prologue/epilogue subprogram_label: push ebp mov ebp, esp ; subprogram code pop ebp ret ; save original EBP value on stack ; new EBP = ESP ; restore original EBP value Example invocation push dword 1 call fun add esp, 4 ; pass 1 as parameter ; remove parameter from stack

Local variables Stored right after the saved EBP value in the stack Allocated by subtracting the number of bytes required from ESP subprogram_label: push ebp ; save original EBP value on stack mov ebp, esp ; new EBP = ESP sub esp, LOCAL_BYTES ; = # bytes needed by locals ; subprogram code mov esp, ebp ; deallocate locals pop ebp ; restore original EBP value ret

Enter/leave ENTER prologue code LEAVE Epilogue subprogram_label: enter LOCAL_BYTES, 0 ; subprogram code leave ret ; = # bytes needed by locals

Examples