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

Similar documents
Basic Execution Environment

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

3.1 DATA MOVEMENT INSTRUCTIONS 45

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

Module 3 Instruction Set Architecture (ISA)

The x86 Architecture

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

Introduction to IA-32. Jo, Heeseung

INTRODUCTION TO IA-32. Jo, Heeseung

6/20/2011. Introduction. Chapter Objectives Upon completion of this chapter, you will be able to:

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

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

Complex Instruction Set Computer (CISC)

Assembler Programming. Lecture 2

Reverse Engineering II: The Basics

Intel 8086: Instruction Set

x86 Assembly Tutorial COS 318: Fall 2017

Addressing Modes on the x86

Chapter 3: Addressing Modes

Hardware and Software Architecture. Chapter 2

The Microprocessor and its Architecture

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

Code segment Stack segment

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

6/17/2011. Introduction. Chapter Objectives Upon completion of this chapter, you will be able to:

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

Chapter 2: The Microprocessor and its Architecture

Computer Architecture and Assembly Language. Practical Session 3

Reverse Engineering II: The Basics

IA32 Intel 32-bit Architecture

SYSC3601 Microprocessor Systems. Unit 2: The Intel 8086 Architecture and Programming Model

MICROPROCESSOR MICROPROCESSOR ARCHITECTURE. Prof. P. C. Patil UOP S.E.COMP (SEM-II)

SPRING TERM BM 310E MICROPROCESSORS LABORATORY PRELIMINARY STUDY

Lecture (02) The Microprocessor and Its Architecture By: Dr. Ahmed ElShafee

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

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

EEM336 Microprocessors I. The Microprocessor and Its Architecture

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

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

EEM336 Microprocessors I. Addressing Modes

CHAPTER 3 BASIC EXECUTION ENVIRONMENT

iapx Systems Electronic Computers M

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

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

Assembly Language Lab # 9

Signed number Arithmetic. Negative number is represented as

EC-333 Microprocessor and Interfacing Techniques

T Reverse Engineering Malware: Static Analysis I

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

x86 architecture et similia

The Instruction Set. Chapter 5

System calls and assembler

Computer Processors. Part 2. Components of a Processor. Execution Unit The ALU. Execution Unit. The Brains of the Box. Processors. Execution Unit (EU)

Interfacing Compiler and Hardware. Computer Systems Architecture. Processor Types And Instruction Sets. What Instructions Should A Processor Offer?

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

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

MICROPROCESSOR MICROPROCESSOR ARCHITECTURE. Prof. P. C. Patil UOP S.E.COMP (SEM-II)

CS241 Computer Organization Spring 2015 IA

Program Exploitation Intro

CS401 Assembly Language Solved Subjective MAY 03,2012 From Midterm Papers. MC

8086 INTERNAL ARCHITECTURE

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

SRI VENKATESWARA COLLEGE OF ENGINEERING AND TECHNOLOGY DEPARTMENT OF ECE EC6504 MICROPROCESSOR AND MICROCONTROLLER (REGULATION 2013)

Tutorial 10 Protection Cont.

appendix b From LC-3 to x86

Practical Malware Analysis

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2016 Lecture 12

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

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

Intel386 TM DX MICROPROCESSOR SPECIFICATION UPDATE

Chapter 11. Addressing Modes

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

Microkernel Construction

Intel386 DX PROCESSOR SPECIFICATION UPDATE

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

EC-333 Microprocessor and Interfacing Techniques

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

Lab 2: Introduction to Assembly Language Programming

UNIT 2 PROCESSORS ORGANIZATION CONT.

Microkernel Construction

16.317: Microprocessor Systems Design I Fall 2013

EEM336 Microprocessors I. Data Movement Instructions

IA-32 Architecture. CS 4440/7440 Malware Analysis and Defense

Exceptions. user mode. software should run in supervisor mode Certain features/privileges il are only allowed to code running in supervisor mode

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2018 Lecture 4

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

Summer 2003 Lecture 4 06/14/03

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

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

MOV Move INSTRUCTION SET REFERENCE, A-M. Description. Opcode Instruction 64-Bit Mode. Compat/ Leg Mode

icroprocessor istory of Microprocessor ntel 8086:

A4 Sample Solution Ch3

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

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

MICROPROCESSOR ARCHITECTURE

Ethical Hacking. Assembly Language Tutorial

Week /8086 Microprocessor Programming I

PROTECTION CHAPTER 4 PROTECTION

We will first study the basic instructions for doing multiplications and divisions

CMSC Lecture 03. UMBC, CMSC313, Richard Chang

Transcription:

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

Assembler Syntax Everything looks like this: label: instruction dest,src instruction label Comments: comment $ This is a comment commend $ ; Single line comment Immediate values: 123, 0123,0x123, 123h, m, \n Slide 2

Memory Organization 4 Gigabytes of addressable memory Support for segmentation and paging Flat memory model is a special case of segmentation Slide 3

Data Types Bytes, words, doublewords, quadwords Little-endian Basically no alignment restrictions Recognized types Integer Ordinal BCD integer, packed BCD integer Near pointer, far pointer Bit field, bit string, byte string Floating point types Slide 4

Registers Sixteen registers for application programmers 8 general (32-bit) EEX, EBX, ECX, EDX, EBP, ESP, ESI, EDI 6 segment (16 bit): determine accessible code 2 status and control: state of processor All condition codes kept in EFLAG (32-bit) DF (of EFLAGS): controls string instructions EIP Offset in code segment Not directly accessible to programmer Slide 5

Non-32-Bit Registers Aliases provided for registers EBP, ESI, EDI, and ESP Can access bits 0-15 as BP, SI, DI, and SP EAX, EBX, ECX, and EDX Can access bits 0-15 as AX, BX, CX, and DX Can access bits 0-7 as AL, BL, CL, and DL Can access bits 8-15 as AH, BH, CH, DH These are carryovers from 80286, but are useful 31 16 15 8 7 0 EAX AH AX AL Slide 6

Stack Implementation Supported by three registers Stack segment (SS): current stack Stack pointer (ESP): offset to top of stack Stack frame-base pointer (EBP) Access data structures passed on the stack Standard to copy ESP to EBP on procedure entry Slide 7

Stack Segment 31 0 Bottom of Stack Stack Layout Subroutine Passed Variables EBP ESP Top of Stack Slide 8

Instruction Format Prefixes Segment override Address and operand size Repeat Lock Opcode and implicit operand(s) Register specifier Addressing mode specifier Scale, index, base (SIB) type Displacement Immediate operand (data) Slide 9

Instruction Examples XCHG EAX, EBX ADD [EBP+8][ESI*4], 17 MOV EAX, SS:[42H] Slide 10

Data Addressing Modes MOV EAX, 123h MOV EAX, NUMBER1 NUMBER1 is a variable (address) MOV EAX,EBX MOV EAX,[EBX] MOV EAX,[EBX+8] Operand is at EBX+8 (can also write 8[EBX]) MOV [EAX+ESI],EBX Base and index registers determine the address MOV EAX,[EBX+EDI+2] Base and index register plus immediate offset are added Immediate Direct Register Register Indirect Register Relative Base Indexed Base Index Relative Slide 11

Instruction Addressing Modes Offsets calculated from start of next instruction JMP LABEL1 Direct Processor adds offset LABEL1 to EIP JMP EBX Register Indirect JMP [EBX] Memory Indirect JMP FAR LABEL Direct Intersegment Allows control to pass to another segment Segment and offset addresses put in command JMP DWORD PTR [EBX] Indirect Intersegment Load words pointer to by EBX into ECS and EIP DWORD PTR indicates the double-word at [EBX] should be used Slide 12

Segmentation: What & Why What is a segment? Provides an independent memory spaces Holds code, data, or stack Has address space up to 4GB Visible to programmer Why segmentation? Provide separate memory spaces Segments can be individually controlled To access large amounts of memory Up to 16,383 segments of size up to 4GB = 64TB Slide 13

Segmented Addressing 15 Segment Selector 0 Operand 31 Offset Within Segment 0 Slide 14

Segment Registers Determine the segments a program can access Total of six CS: points to code segment CALL and JMP instructions DS: points to main data area Data instructions (MOV, etc.) SS: stack segment (often same as data segment) PUSH and POP instructions ES, FS, GS: special information Can specify which segment an instruction uses Example: MOV EAX, CS:[0] Slide 15

Segmentation: Historical Note We are discussing 80386 segmentation 80286 and earlier limited segment size to 64KB Compatibility with the 8080 (64KB address space) Allowed 16 bit addressing to continue while providing access to more memory Slide 16

How Segmentation Works Segment descriptor: address and size Physical address = offset + base address Pointer into a segment Segment selector: 16 bits to identify a segment Offset: 32 bit address with a segment CR0 register toggles between direct mapping to physical memory and using paging Slide 17

Do We Still Need Segmenting? Not required for memory access purposes Can be used to implement memory management Most operating systems use a flat-memory model Really just one large segment Slide 18

Data Movement Instructions MOVdest,src No segment register to segment register moves Use MOVS for memory to memory moves XCHG reg,reg Stack manipulation PUSH val, PUSHA POP val, POPA PUSHA stores ESP, but POPA does not restore it Slide 19

Arithmetic Instructions ADD/ADC dest,src SUB/SUBB dest,src INC/DEC reg CMP dest,src Register unchanged; flags updated NEG val 3 +4 7 Slide 20

Arithmetic Instructions (continued) MUL/DIV val (AL/AX/EAX implicit) IMUL: 1, 2, 3 and 3 operand forms IDIV Operands for Division Operand Size (Divisor) Dividend Quotient Remainder Byte AX AL AH Word DX and AX AX DX Doubleword EDX and EAX EAX EDX Slide 21

Logical Instructions Boolean operations AND, OR, XOR, NOT Update flags and saves value in destination register Bit test and modify Instruction Effect on CF Flag Effect on Selected Bit BT (Bit Test) CF Flag Selected Bit No Effect BTS (Bit Test and Set) CF Flag Selected Bit Selected Bit 1 BTR (Bit Test and Reset) CF Flag Selected Bit Selected Bit 0 BTC (Bit Test and Complement) CF Flag Selected Bit Selected Bit -(Selected Bit) Slide 22

Logical Instructions (continued) Bit scan instructions Shift and rotate instructions Byte-set-on-condition instructions SETcc: sets byte to 0 or 1 based on condition code TEST instruction Logical AND without alteration to destination operand Slide 23

Function Calling Conventions cdecl Parameters passed on stack, right to left Caller maintains stack Used for variable arguments stdcall Parameters passed on stack, right to left Callee maintains stack fastcall First two 32-bit values passed in ECX & EDX Remaining params passed on stack, right to left Slide 24

A Sample Function ( cdecl) _x$ = 8 _y$ = 12 _z$ = 16?sample@@YAHHHH@Z PROC NEAR ; sample ; 5 : int sample(int x,int y,int z) { 00000 55 push ebp 00001 8b ec mov ebp, esp ; 6 : return x+y+z; 00003 8b 45 08 mov eax, DWORD PTR _x$[ebp] 00006 03 45 0c add eax, DWORD PTR _y$[ebp] 00009 03 45 10 add eax, DWORD PTR _z$[ebp] ; 7 : } 0000c 5d pop ebp 0000d c3 ret 0 Slide 25

A Sample Function ( stdcall) _x$ = 8 _y$ = 12 _z$ = 16?sample@@YGHHHH@Z PROC NEAR ; sample ; 5 : int sample(int x,int y,int z) { 00000 55 push ebp 00001 8b ec mov ebp, esp ; 6 : return x+y+z; 00003 8b 45 08 mov eax, DWORD PTR _x$[ebp] 00006 03 45 0c add eax, DWORD PTR _y$[ebp] 00009 03 45 10 add eax, DWORD PTR _z$[ebp] ; 7 : } 0000c 5d pop ebp 0000d c2 0c 00 ret 12 ; 0000000cH Slide 26

A Sample Function ( fastcall) _x$ = -4 _y$ = -8 _z$ = 8?sample@@YIHHHH@Z PROC NEAR ; sample ; 5 : int sample(int x,int y,int z) { 00000 55 push ebp 00001 8b ec mov ebp, esp 00003 83 ec 08 sub esp, 8 00006 89 55 f8 mov DWORD PTR _y$[ebp], edx 00009 89 4d fc mov DWORD PTR _x$[ebp], ecx ; 6 : return x+y+z; 0000c 8b 45 fc mov eax, DWORD PTR _x$[ebp] 0000f 03 45 f8 add eax, DWORD PTR _y$[ebp] 00012 03 45 08 add eax, DWORD PTR _z$[ebp] ; 7 : } 00015 8b e5 mov esp, ebp 00017 5d pop ebp 00018 c2 04 00 ret 4 Slide 27

Control Transfer Instructions Unconditional transfers CALL Pushes EIP onto stack RET arg Retrieves return address from stack Optional argument specifies adjustment to ESP JMP Conditional jumps Looping Slide 28

Miscellaneous Instructions LEA reg,memory: Load Effective Address Puts offset in register Useful for initializing ESI, EDI NOP 1 byte Advances EIP BSWAP: Byte Swap (Endian conversion) Bits 7 0 exchanged with 31 24 Bits 15 8 exchanged with 23 16 Speeds arithmetic Slide 29

Protected Mode System Instructions Not all useful to applications Not all protected from application Some functions provided Verification of pointer parameters Interrupt control Debugging System control Slide 30

Fault Types of Exceptions CS and EIP point to instruction that generated fault Instruction restart (processor registers restored) Trap CS and EIP: instruction after one that generated trap Transfers are reflected Abort No precise location of instruction causing exception Cannot restart program Hardware errors, illegal values in system tables Slide 31

Interrupt Tasks and Procedures Use stack to preserve processor state Interrupt pushes EFLAGS onto stack first Error codes can be pushed Returning from interrupt procedure Use IRET instead of RET Increments ESP extra 4 bytes and restores EFLAGS Slide 32

Stack Frame After an Exception Example shown with error code Unused Old SS Old ESP Old EFLAGS Old CS Old EIP Error Code Privilege Level Change ESP from TSS Old ESP New ESP Old EFLAGS Old EFLAGS Old CS Old EIP Error Code No Privilege Level Change Slide 33

Exceptions Return address adjustment Usually return address is offending instruction Debug exception: check DR6 Exception Conditions Code Name Code Name 0 Division by zero 10 Invalid TSS 1 Debug 11 Segment not present 3 Breakpoint 12 Stack exception 4 Overflow 13 General protection 5 Bounds check 14 Page fault 6 Invalid opcode 16 Floating point error 7 Device not available 17 Alignment check 8 Double fault 18 Machine check Slide 34

Interesting Exceptions 1: Debug Exception Fault or trap? Instruction address breakpoint: fault Data address breakpoint: trap General detect: fault Single-step: trap Task-switch breakpoint: trap No error code pushed Debug registers reveal condition Slide 35

Interesting Exceptions (continued) 3: Breakpoint INT 3: one byte long Saved CS and EIP point to byte following breakpoint Replace INT 3 with real opcode and decrement saved EIP 5: Bounds Check Operand exceeds specified limits Check signed array index 12: Stack Exception Limit violation Load SS with segment that is marked not present Always restartable Slide 36

Interesting Exceptions (continued) 13: General Protection All other protection violations Always a fault Pushes error code onto stack Null is pushed unless loading a descriptor caused exception 17: Alignment Check Access to unaligned operands Only generated in user-mode Slide 37

Debugging Support Overview Debug support for memory and I/O locations New for the Pentium architecture Main facility is debug registers Instruction and data breakpoints Based on breakpoint fields Slide 38

Debugging Support Debug interrupt vector Useful flags Trap bit of TSS (T-bit) Exception when switch to task with this bit set Resume flag (RF) No multiple exceptions for same instruction Trap flag (TF) Exception after each instruction Breakpoint instruction Interrupt vector for breakpoint exception Slide 39

Benefits No code patching Efficiency Debug Registers Works on ROM-based software Reads and writes of data handled Debug address registers (DR0 - DR3) Up to four breakpoints Debug control register (DR7) Form of memory or I/O access to trigger breakpoint Debug status register (DR6) Conditions in effect at time of exception Slide 40

Debug Exceptions Two interrupt vectors for debug exceptions Interrupt 1: Debug Exceptions Handler is debugger Flags in DR6 & DR7 tell what caused the exception Instruction breakpoints: faults Other exceptions: traps Interrupt 3: Breakpoint Instruction Execution of INT 3 (replaces first opcode) When exception handler is called return address is first byte of instruction following the INT 3 Slide 41

Interrupt 1 Instruction-breakpoint (fault) Use RF flag to restart execution (may fault again!) Data Memory and I/O Breakpoints (trap) Original data overwritten before exception generated Need to store data before setting breakpoint General-detect fault Emulators get full control to debug registers Single-step trap First instruction after TF is set does not generate exception Processor clears TF before executing handler INT instructions clear TF flag Task-switch trap Exception occurs before first instruction in new task Slide 42

Invoking a Debugger Two modes of invocation Separate task Context of current task I/O addresses Input of byte or word Output of byte, word, or doubleword Memory addresses Read or write of byte, word, or doubleword Write of byte, word, or doubleword Slide 43

Invoking a Debugger (continued) Execution Task switch Execution of breakpoint instruction Execution of any instruction Execution at a memory address Attempt to change debug register Slide 44

Optimization: Alignment Pentium: relaxed restrictions on alignment But same performance consequences as 80486 Code alignment on cache boundary 32-byte for Pentium; 16-byte for 80486 No substantial effect on Pentium Large benefit on 80486 Misaligned access in data cache costs 3 cycles 4 byte: on 4-byte boundary 2 byte: fully contained in aligned 4-byte word 8-byte: 8-byte boundary (double precision reals) Slide 45

Optimization: Register Usage Use EAX when possible Many instructions 1-byte shorter Use DS to access data segment One byte shorter than using other segments Use ESP to reference stack Load displacements into registers Slide 46

Optimization: Instruction Selection Integer Instruction Selection Use TEST to test for 0 ANDs operands (no register writing) Check zero condition flag Put address calculations into loads and stores Memory reference instructions have four operands Using base register (not index) saves one clock cycle Use XOR reg,reg to clear a register Watch out: sets condition codes Perform task switching in software Smaller processor state to save/restore Minimize segment register loads and far pointers Segment protection and the loading of segment registers are costly Slide 47