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

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

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

Assembly Language: IA-32 Instructions

Sungkyunkwan University

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

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

The x86 Architecture

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

Machine-Level Programming II: Arithmetic & Control. Complete Memory Addressing Modes

Machine-Level Programming II: Control and Arithmetic

Machine Level Programming II: Arithmetic &Control

Machine-Level Programming II: Arithmetic & Control /18-243: Introduction to Computer Systems 6th Lecture, 5 June 2012

CS241 Computer Organization Spring Addresses & Pointers

COMPUTER ENGINEERING DEPARTMENT

Credits to Randy Bryant & Dave O Hallaron

Program Exploitation Intro

ASSEMBLY II: CONTROL FLOW. Jo, Heeseung

Assembly II: Control Flow. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

The Hardware/Software Interface CSE351 Spring 2013

CS241 Computer Organization Spring 2015 IA

Process Layout and Function Calls

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.

UMBC. A register, an immediate or a memory address holding the values on. Stores a symbolic name for the memory location that it represents.

CSE2421 FINAL EXAM SPRING Name KEY. Instructions: Signature

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

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

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

Second Part of the Course

Module 3 Instruction Set Architecture (ISA)

Towards the Hardware"

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

CSE351 Spring 2018, Midterm Exam April 27, 2018

SPRING TERM BM 310E MICROPROCESSORS LABORATORY PRELIMINARY STUDY

Reverse Engineering II: The Basics

CS 31: Intro to Systems ISAs and Assembly. Martin Gagné Swarthmore College February 7, 2017

Reverse Engineering II: The Basics

Lecture (08) x86 programming 7

Basic Pentium Instructions. October 18

Lab 3. The Art of Assembly Language (II)

Machine-Level Programming II: Control Flow

Machine Programming 2: Control flow

Turning C into Object Code Code in files p1.c p2.c Compile with command: gcc -O p1.c p2.c -o p Use optimizations (-O) Put resulting binary in file p

Binghamton University. CS-220 Spring x86 Assembler. Computer Systems: Sections

An Introduction to x86 ASM

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

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

Introduction to IA-32. Jo, Heeseung

Basic Assembly Instructions

INTRODUCTION TO IA-32. Jo, Heeseung

ADDRESSING MODES. Operands specify the data to be used by an instruction

16.317: Microprocessor Systems Design I Fall 2014

Complex Instruction Set Computer (CISC)

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

16.317: Microprocessor Systems Design I Spring 2014

Practical Malware Analysis

CPS104 Recitation: Assembly Programming

Week /8086 Microprocessor Programming

x86 Assembly Tutorial COS 318: Fall 2017

CS61 Section Solutions 3

Q1: Multiple choice / 20 Q2: Data transfers and memory addressing

Assembly level Programming. 198:211 Computer Architecture. (recall) Von Neumann Architecture. Simplified hardware view. Lecture 10 Fall 2012

Assembly Language: Part 2

Assembly Language Each statement in an assembly language program consists of four parts or fields.

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

Introduction to 8086 Assembly

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

Lecture 2 Assembly Language

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

Chapter 3 Machine-Level Programming II Control Flow

Machine- Level Programming II: Arithme6c & Control

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

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

Princeton University Computer Science 217: Introduction to Programming Systems. Assembly Language: Part 2

Do not turn the page until 5:10.

Lab 6: Conditional Processing

22 Assembly Language for Intel-Based Computers, 4th Edition. 3. Each edge is a transition from one state to another, caused by some input.

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

Process Layout, Function Calls, and the Heap

Machine-Level Programming Introduction

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

Addressing Modes on the x86

CSE 351 Midterm Exam

Questions about last homework? (Would more feedback be useful?) New reading assignment up: due next Monday

Condition Codes The course that gives CMU its Zip! Machine-Level Programming II Control Flow Sept. 13, 2001 Topics

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2016 Lecture 12

Condition Codes. Lecture 4B Machine-Level Programming II: Control Flow. Setting Condition Codes (cont.) Setting Condition Codes (cont.

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

Intel Instruction Set (gas)

Machine- Level Programming II: Arithme c & Control

COMPUTER ENGINEERING DEPARTMENT

16.317: Microprocessor Systems Design I Fall 2013

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

Assembly II: Control Flow. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

CISC 360. Machine-Level Programming II: Control Flow Sep 17, class06

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

SOEN228, Winter Revision 1.2 Date: October 25,

Assembly I: Basic Operations. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

Assembly Programmer s View Lecture 4A Machine-Level Programming I: Introduction

Where We Are. Optimizations. Assembly code. generation. Lexical, Syntax, and Semantic Analysis IR Generation. Low-level IR code.

Registers. Ray Seyfarth. September 8, Bit Intel Assembly Language c 2011 Ray Seyfarth

Transcription:

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

Arithmetic Instructions (1)! Two Operand Instructions" ADD Dest, Src Dest = Dest + Src SUB Dest, Src Dest = Dest - Src MUL Dest, Src Dest = Dest * Src SAL Dest, Src Dest = Dest << Src SAR Dest, Src Dest = Dest >> Src Arithmetic SHR Dest, Src Dest = Dest >> Src Logical XOR Dest, Src Dest = Dest ^ Src AND Dest, Src Dest = Dest & Src OR Dest, Src Dest = Dest Src Arithmetic Instructions (2)! One Operand Instructions" INC Dest Dest = Dest + 1 DEC Dest Dest = Dest 1 NEG Dest Dest = -Dest NOT Dest Dest = ~Dest 2

Compare and Test Instructions! CMP Dest, Src Compute Dest - Src without setting Dest TEST Dest, Src Compute Dest & Src without setting Dest Jump Instructions! Jump depending on the result of the previous arithmetic instruction:" Jump Description jmp je jne js jns jg jge jl jle ja jb Unconditional Equal / Zero Not Equal / Not Zero Negative Nonnegative Greater (Signed) Greater or Equal (Signed) Less (Signed) Less or Equal (Signed) Above (unsigned) Below (unsigned) 3

Loading and Storing Data! IA-32 Architecture! CPU! Registers! EIP! EAX EBX ECX Addresses" Data" EDX Instructions" ESI EDI ESP EBP Memory! Object Code" Program Data" Stack! Condition Codes! CF ZF SF OF EFLAGS! EIP!!Instruction Pointer! ESP, EBP!Reserved for special use! EAX!!Always contains return value! 4

IA-32 General Purpose Registers! 31 15 8 7 0 AH AL BH BL CH CL DH DL SI DI General-purpose registers 16-bit 32-bit AX EAX BX EBX CX ECX DX EDX ESI EDI Byte Order in Multi-Byte Entities! Intel is a little endian architecture" Least significant byte of multi-byte entity is stored at lowest memory address" Little end goes first " The int 5 at address 1000:" Some other systems use big endian" Most significant byte of multi-byte entity is stored at lowest memory address" Big end goes first " The int 5 at address 1000:" 1000 1001 1002 1003 1000 1001 1002 1003 00000101 00000000 00000000 00000000 00000000 00000000 00000000 00000101 5

Little Endian Example! int main(void) { int i=0x003377ff, j; unsigned char *p = (unsigned char *) &i; for (j=0; j<4; j++) printf("byte %d: %x\n", j, p[j]); } Output on a little-endian machine Byte 0: ff! Byte 1: 77! Byte 2: 33! Byte 3: 0! C Example: One-Byte Data! Global char variable i is in AL, the lower byte of the A register. char i; if (i > 5) { i++; else i--; } CMP AL, 5 JLE else INC AL jmp endif else: DEC AL endif: 6

C Example: Four-Byte Data! Global int variable i is in EAX, the full 32 bits of the A register. int i; if (i > 5) { i++; else i--; } CMP EAX, 5 JLE else INC EAX JMP endif else: DEC EAX endif: Loading and Storing Data! Processors have many ways to access data" Known as addressing modes " Two simple ways seen in previous examples" Addressing Modes" Register" Immediate" Direct Memory" Base Memory" (Base or Index) plus Displacement Memory" (Base and Index) plus Displacement Memory" 7

Register Addressing! Registers embedded in the instruction" Examples" XOR EAX, EAX MOV EBX, ECX INC BH Immediate Addressing! Data embedded in the instruction" Examples" ADD EAX, 3 MOV AX, -40 8

Direct Memory Addressing! Memory address embedded in the instruction" Examples" MOV AL, [2000] Read the byte from memory address 2000" Load the byte value in the register AL" Note that 2000 refers to the constant value 2000 [2000] refers to the memory location at address 2000" (Indirect) Base Memory Addressing! 9

Indirect Memory Addressing! Load or store from a previously-computed address" Register with the base address is in the instruction" Examples" MOV AX, [BX] (register addressing)" CMP DL, [BX+8] (base+displacement addressing)" MOV EAX,[EBX+ESI*4+8] (base+index+displacement)" Indexed Addressing Example! int a[20]; int i, sum=0; for (i=0; i<20; i++) sum += a[i]; EAX: i EBX: sum ECX: address of a[0] sumloop: global variable MOV EAX, 0 MOV EBX, 0 MOV ECX, OFFSET FLAT:a ADD EBX, [ECX+EAX*4] INC EAX CMP EAX, 20 jne sumloop 26 10

LEA: Load Effective Address! LEA Dest, Src" Src is address mode expression" Set Dest to address denoted by expression" Example" LEA EAX, [EBX+4*ESI] " Load into EAX the value EBX+4*ESI Compare to" MOV EAX, [EBX+4*ESI] " Load into EAX the value stored in memory at address EBX+4*ESI Using LEA for Arithmetic Expressions! int arith (int x, int y, int z) { int t1 = x+y; int t2 = z+t1; int t3 = x+4; int t4 = y * 48; int t5 = t3 + t4; int rval = t2 * t5; return rval; } arith: PUSH EBP MOV EBP,ESP MOV ECX, DWORD PTR [EBP+8] MOV EDX, DWORD PTR [EBP+12] LEA EAX, [EDX+EDX*2] SAL EDX, 4 LEA EAX, [ECX+4+EAX] ADD EDX, ECX ADD EDX, DWORD PTR [EBP+16] IMUL EAX,EDX POP EBP RET Setup! Body! Finish! 11

Understanding arith! int arith (int x, int y, int z) { int t1 = x+y; int t2 = z+t1; int t3 = x+4; int t4 = y * 48; int t5 = t3 + t4; int rval = t2 * t5; return rval; } x is at address ebp+8 y is at address ebp+12 z is at address ebp+16 To be explained in next lecture MOV ECX, DWORD PTR [EBP+8] ; ECX = MOV EDX, DWORD PTR [EBP+12] ; EDX = LEA EAX, [EDX+EDX*2] ; EAX = SAL EAX, 4 ; EAX = LEA EAX, [ECX+4+EAX] ; EAX = ADD EDX, ECX ; EDX = ADD EDX, DWORD PTR [EBP+16] ; EDX = IMUL EAX,EDX ; EAX = Data Access Methods: Summary! Immediate addressing: data stored in the instruction itself" MOV ECX, 10! Register addressing: data stored in a register" MOV ECX, EAX! Direct addressing: address stored in instruction" MOV ECX, [200]! Indirect addressing: address stored in a register" MOV ECX, [EAX]! MOV ECX, [EAX+4]! MOV ECX, [EAX + ESI*4 + 12]! 12

Data Transfer Instructions (Chapter 5)! MOV Dest, Src General move instruction" EBX 17 PUSH Src PUSH EBX ESP # equivalent instructions" SUB ESP, 4 MOV [ESP], EBX ESP! 17 POP Dest POP ECX # equivalent instructions MOV ECX, [ESP] ADD ESP, 4 ESP 44 ESP ECX 44 13