EECE416 :Microcomputer Fundamentals and Design. X86 Assembly Programming Part 1. Dr. Charles Kim

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

Chapter 3 (Part a) Assembly Language Fundamentals

Assembly Language Programming

Assembly Language. Lecture 2 - x86 Processor Architecture. Ahmed Sallam

Assembly Language. Lecture 2 x86 Processor Architecture

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

Machine and Assembly Language Principles

Basic Execution Environment

Lab 2: Introduction to Assembly Language Programming

Introduction to IA-32. Jo, Heeseung

INTRODUCTION TO IA-32. Jo, Heeseung

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

Practical Malware Analysis

Complex Instruction Set Computer (CISC)

Chapter 3: Assembly Language Fundamentals. Cristina G. Rivera

Assembly Language for Intel-Based Computers, 4 th Edition. Chapter 3: Assembly Language Fundamentals

Chapter Overview. Assembly Language for Intel-Based Computers, 4 th Edition. Chapter 3: Assembly Language Fundamentals.

Assembly Language for Intel-Based Computers, 5 th Edition. Kip Irvine. Chapter 3: Assembly Language Fundamentals

The Instruction Set. Chapter 5

The Microprocessor and its Architecture

Module 3 Instruction Set Architecture (ISA)

Assembly Language for Intel-Based Computers, 5 th Edition

EEM336 Microprocessors I. The Microprocessor and Its Architecture

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

Hardware and Software Architecture. Chapter 2

Program Exploitation Intro

Computer Architecture Dr. Charles Kim Howard University

Assembly Language for Intel-Based Computers, 4 th Edition. Chapter 2: IA-32 Processor Architecture Included elements of the IA-64 bit

Computer Organization and Assembly Language. Lab Session 3

Computer System Architecture

W4118: PC Hardware and x86. Junfeng Yang

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

Ethical Hacking. Assembly Language Tutorial

Assembly Language for Intel-Based Computers, 5 th Edition. Chapter 3: Assembly Language Fundamentals

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

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

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

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

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

Chapter 2: The Microprocessor and its Architecture

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

x86 Assembly Tutorial COS 318: Fall 2017

Assembly Language Programming: Procedures. EECE416 uc. Charles Kim Howard University. Fall

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

For your convenience Apress has placed some of the front matter material after the index. Please use the Bookmarks and Contents at a Glance links to

Assembly Language. Lecture 3 Assembly Fundamentals

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

CMSC Lecture 03. UMBC, CMSC313, Richard Chang

Memory Models. Registers

Load Effective Address Part I Written By: Vandad Nahavandi Pour Web-site:

Reverse Engineering II: The Basics

Assembly Fundamentals

Chapter 3: Addressing Modes

Representation of Information

MICROPROCESSOR TECHNOLOGY

Computer Architecture Dr. Charles Kim Howard University

Assembly Language Fundamentals

3.1 DATA MOVEMENT INSTRUCTIONS 45

Systems Architecture I

Chapter 2. lw $s1,100($s2) $s1 = Memory[$s2+100] sw $s1,100($s2) Memory[$s2+100] = $s1

IA-32 Architecture COE 205. Computer Organization and Assembly Language. Computer Engineering Department

The x86 Architecture

Real instruction set architectures. Part 2: a representative sample

Computer Organization (II) IA-32 Processor Architecture. Pu-Jen Cheng

CPU. IBM PC and compatible Memory Structure

Writing 32-Bit Applications

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

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

Chapter 4: Data Movement Instructions. 4 1 MOV Revisited

VARDHAMAN COLLEGE OF ENGINEERING (AUTONOMOUS) Shamshabad, Hyderabad

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

ORG ; TWO. Assembly Language Programming

Islamic University Gaza Engineering Faculty Department of Computer Engineering ECOM 2125: Assembly Language LAB. Lab # 10. Advanced Procedures

Assembly Language for Intel-Based Computers, 4 th Edition. Chapter 2: IA-32 Processor Architecture. Chapter Overview.

17. Instruction Sets: Characteristics and Functions

EC-333 Microprocessor and Interfacing Techniques

Chapter 11. Addressing Modes

IA-32 Architecture. Computer Organization and Assembly Languages Yung-Yu Chuang 2005/10/6. with slides by Kip Irvine and Keith Van Rhein

Machine Code and Assemblers November 6

Computer Organization & Assembly Language Programming

Microprocessor. By Mrs. R.P.Chaudhari Mrs.P.S.Patil

Advanced Microprocessors

Addressing Modes on the x86

An Introduction to x86 ASM

Introduction to Assembly Language

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

CSIS1120A. 10. Instruction Set & Addressing Mode. CSIS1120A 10. Instruction Set & Addressing Mode 1

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

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

CC411: Introduction To Microprocessors

Assembly Language for Intel-Based Computers, 4 th Edition. Kip R. Irvine. Chapter 2: IA-32 Processor Architecture

16-Bit Intel Processor Architecture

Lab 4: Basic Instructions and Addressing Modes

x86 architecture et similia

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. Addressing Modes

1. Introduction to Assembly Language

Marking Scheme. Examination Paper. Module: Microprocessors (630313)

Reverse Engineering II: The Basics

IA32 Intel 32-bit Architecture

Transcription:

EECE416 :Microcomputer Fundamentals and Design X86 Assembly Programming Part 1 Dr. Charles Kim Department of Electrical and Computer Engineering Howard University www.mwftr.com 1

Multiple Address Access Issues Reminder: a single address locates only a byte of memory 8-bit processor Accesses one address with a byte data 16-bit processor Accesses two address spaces (Even and Odd) at a single execution with 2-byte (or Word ) data Where do we store each of the 2 bytes to each of the 2 address spaces? 4

Big-Endian vs. Little-Endian Big-Endian: Words are stored with the lower 8- bits ( Lower Byte ) in the higher of the two storage locations ( Addresses ): Motorola Big guy ( Upper Byte ) ends at lower address Little- Endian: Lower-order byte stored in the lowest address: Intel 80x86 family Little guys ( Lower Byte ) ends at lower address 5

Endianness Endian or Endian-Architecture how multi-byte data is represented by a computer system and is dictated by the CPU architecture of the system Not all computer systems are designed with the same endian architecture Issues with software and interface 6

Conversion Endian-Neutral Approaches Byte Swap Network I/O Macro Endian Neutral : allowing the code to be ported easily between processors of different Endian-architectures, and without rewriting any code. Endian-neutral software is developed by identifying system memory and external data interfaces, and using Endian-neutral coding practices to implement the interfaces. 7

HOMEWORK Technical Report on Endian-Neutral Approaches What? Why? How? 2-3 pages; 1 margin all sides; 11 pt; Times New Roman; No cover page (Title and your name at the top, and start in the first page); single space; single column; again the importance of the first paragraph. No figure, no photo, text only. Submission Due: Hardcopy + Softcopy by 5:00pm Date: Tuesday Oct 6 8

First x86 Family member: 8086 ( 8088). 1978 Cf. 4004 8080 8085 8086 16-bit registers, external data bus 20-bit addressing ( 1MB address space) Segmentation : 64KB Why? Internal 16-bit register cannot hold 20-bit address x86 Architecture 9

Protected Mode (from i286) X86 Modes of Operation Native State of the Processor in which all instructions and features are available Programs a re given separate memory areas ( Segments ), and the processor prevents programs from referencing memory outside their assigned segments. Real Address Mode A few extra features available for this mode by which direct access to system memory and hardware devices is possible (Windows 98, and MS-DOS) Programs running in real-address mode can cause the OS crash Virtual-8086 Mode While in Protected Mode, processor can directly execute real-address mode software such as MS-DOS programs in a safe multitasking environment [Windows XP] System Management Mode Power Management and System Security Customized by manufacturers for a particular system setup 10

Real Address Mode x86 Memory Management 1 MB Memory can be accessed 0x000000 0x0FFFFF [Details to come] Processor runs one program at a time Application programs are allowed to access any memory location including that is linked directly to system hardware MS-DOS and Windows95 and 98 Protected Mode [Details to come] Access to a total of 4GB Memory Multiple programs run at the same time own reserved memory area Windows and Linux 11

x86 Memory Management Real Addr Mode Memory Segmentization (for 8086 with 20 Addr lines) 1 MB Memory can be accessed 0x000000 0x0FFFFF But 16-bit register cannot hold the 20-bit address Solution: Segmentize the 1MB memory space into multiple (16 exactly) areas (segments) Accessing by Segment (expressed by first 4 hex digits only) + Offset (max. offset = 0xFFFF) 12

x86 Memory Management Real Addr Mode 20-bit address calculation 16-bit segment value 16-bit offset value Memory address = (16-bit Seg value) *0x10 + (16-bit Offset) 16-bit segment value is placed in one of the segment registers: CS: Code seg register DS: data seg register SS: Stack seg register ES, FS, GS, etc 13

x86 Memory Management Protected Mode Powerful native mode In Microsoft Assembler (MASM) Flat (Segment) Mode is appropriate for protected mode programming Just 1 32-bit address register is enough (for up to 4 GB Memory space) Actual address location calculation is done in the background 14

80286 x86 Architecture Protected Mode first introduced Segment register contents as selector or pointer 80386 24-bit base address 16MB memory size 32-bit registers for operands and addressing( 4GB space) Lower half of 32 bits is equivalent to 16 bits of earlier generations [Backward (upward) compatibility with 16-bit registers] Some new instructions was added (like bit manipulation) Max 4GB segmentation of physical space New Parallel Processing Stages introduced: Bus Interface Unit, Code Prefetch Unit, Instruction Decode Unit, Execution Unit, Segment Unit, Paging Unit 15

Overview of Basic Execution Set of resources for Executing instructions and for Storing code, data, and state information Resources: 8 General data registers 6 Segment registers Status and control registers Holding the following items (for all): Operands for logical and arithmetic operations Operands for address calculations Memory pointers 16

General-Purpose Data Registers E??: Extended or 32-bit??X: 16-bit Primaries EAX (accumulator for operands and results data) EBX (Pointer to data in Segment) ECX (Counter) EDX (for I/O pointer) Secondaries EBP (base pointer to data on the stack in DS segment) ESI (Source pointer) EDI (data pointer) for string instructions ESP (Stack pointer)holds the stack pointer (restricted use) ESP points to the top item on the stack and the EBP points to the "previous" top of the stack before the function was called. 17

32-bit register EFLAG Register Initial state: 00000002H Contains a group of status flags (S), a control flag (C), and a group of system flags (X)

Status Flags

Control Flag (DF) DF (Direction Flag) The direction flag controls the string instructions (MOVS, CMPS, SCAS, LODS, and STOS). DF=1 string instructions to auto-decrement (that is, to process strings from high addresses to low addresses). DF=0 string instructions to auto-increment (process strings from low addresses to high addresses). STD Set DF flag CLD Clear DF flag

System Flags

MASM Screen Capture 22

Notational Conventions Little Endian Machine Bit and Byte Oder Smaller address at the bottom of figure Address increases toward top Bit positions numbered from right to left the bytes of a word are numbered starting from the least significant byte 23

How does a simple code look? 24

Assembly Language Fundamentals Integer Constants [{+/-}] digits [radix] Radix H hexadecimal q/o Octal D Decimal {Default} b Binary Example: *Note: Hex constant beginning with a letter must have a leading 0 25

Assembly Language Fundamentals Character Constants A single character enclosed in single or double quotes A d MASM stores the value in memory as the character s binary ASCII code String Constants A sequence of characters (including spaces) enclosed in single or double quotes ABC Good night, Gracie Say Good night, Gracie 26

Assembly Language Fundamentals Directives A command embedded in the source code that is recognized and acted upon by the assembler Directives can define variables, macros, and procedures Directives can assign names to memory segments Directives perform many other housekeeping tasks Directives do not execute at runtime Directives are case insensitive in MASM Defining Segments (or Program Sections).data ; the area of program containing variables.code ; the area of a program containing executable instructions.stack 100h ; the area of a program holding the runtime stack (with its size) 27

Assembly Language Fundamentals Instruction: A statement that becomes executable when a program is assembled Instrcutions are translated by the assembler into machine language bytes, which are loaded and executed by the CPU at runtime Instruction Format Label, Instructional mnemonic, argument1, argument2, argument3 Label: Identifier (followed by a colon) Mnemonic: a reserved name for a class of instruction opcodes which have the same function Operands (arguments): The operands argument1, argument2, and argument3 are optional. 0 to 3 operands 2 types of form: literals (i.e., numer) or identifiers for data items. When two operands are present in an arithmetic or logical instruction the right operand is the source (src) and the left operand, the destination (dst). Example: Count DWORD 100 ; data label LOADREG: MOV EAX, SUBTOTAL ;code label mnemonic dst, src 28

Assembly Language Fundamentals Instruction Mnemonic: a short word that identifies an instruction Example Example 29

Assembly Language Fundamentals Comments Single Line Comments (;) ; This line is a comment Block Comments: Begin with the COMMENT directive and a user-specified symbol 30

Assembly Language Fundamentals NOP (No Operation) Instruction Takes up 1 byte of program storage Does not do any work Use when to align code to even-addressed boundaries (which, in x86, loads data more quickly.) 31

Assembly Language Fundamentals - Examples Adding and Subtracting Integers TITLE directive Line comment INCLUDE directive.code directive PROC directive start CALL a procedure Exit --- halt to program (Not a MASM keyword, but of Irvine32.inc) ENDP directive end END directive last line to be assembled Output (DumpRegs) 32

Program Template 33

Assembling, Linking, and Running Programs Step 1: Create an ASCII text file named source file using a text editor Step 2: Assemble the source file. The assembler reads the source file and produces an object file, a machine language translation of the program. Optionally, it produced a listing file. {VS: BUILD SOLUTION} Step 3: The linker reads the object file and checks to see if the program contains any calls to procedures in a link library. The linker combines them with the object file, and produces the executable file. Step 4: OS loader utility reads the executable file into memory and branches the CPU to the program s starting address, and the program begins to execute. {VS: START W/ or W/O DEBUGGING} 34

Source Program 35

Listing File 36

Listing File 37

Listing File 38

Web Page for Instruction and Link Library 39