Docsity

Artificial Intelegence, Study notes of Computer Science

Partial preview of the text

Download Artificial Intelegence and more Study notes Computer Science in PDF only on Docsity!

Downloaded From: www.bsit.zxq.net

IT Books, Notes & Assignments visit: www

Assembly Language for

x86 Processors

Sixth Edition

KIP R. IRVINE

Florida International University

School of Computing and Information Sciences

Upper Saddle River Boston Columbus San Francisco New York Indianapolis London Toronto Sydney Singapore Tokyo Montreal Dubai Madrid Hong Kong Mexico City Munich Paris Amsterdam Cape Town

Downloaded From: www.bsit.zxq.net

To Jack and Candy Irvine

Downloaded From: www.bsit.zxq.net

This page intentionally left blank

Downloaded From: www.bsit.zxq.net

Chapters are available from the Companion Web site

15 Disk Fundamentals 16 BIOS-Level Programming

  • 1 Basic Concepts Preface xix
  • 1.1 Welcome to Assembly Language
    • 1.1.1 Good Questions to Ask
    • 1.1.2 Assembly Language Applications
    • 1.1.3 Section Review
  • 1.2 Virtual Machine Concept
    • 1.2.1 Section Review
  • 1.3 Data Representation
    • 1.3.1 Binary Integers
    • 1.3.2 Binary Addition
    • 1.3.3 Integer Storage Sizes
    • 1.3.4 Hexadecimal Integers
    • 1.3.5 Signed Integers
    • 1.3.6 Character Storage
    • 1.3.7 Section Review
  • 1.4 Boolean Operations
    • 1.4.1 Truth Tables for Boolean Functions
    • 1.4.2 Section Review
  • 1.5 Chapter Summary
  • 1.6 Exercises
    • 1.6.1 Programming Tasks
    • 1.6.2 Nonprogramming Tasks
  • 2 x86 Processor Architecture
  • 2.1 General Concepts
    • 2.1.1 Basic Microcomputer Design
    • 2.1.2 Instruction Execution Cycle
    • 2.1.3 Reading from Memory
    • 2.1.4 How Programs Run
    • 2.1.5 Section Review
  • 2.2 x86 Architecture Details vi Contents
    • 2.2.1 Modes of Operation
    • 2.2.2 Basic Execution Environment
    • 2.2.3 Floating-Point Unit
    • 2.2.4 Overview of Intel Microprocessors
    • 2.2.5 Section Review
  • 2.3 x86 Memory Management
    • 2.3.1 Real-Address Mode
    • 2.3.2 Protected Mode
    • 2.3.3 Section Review
  • 2.4 Components of a Typical x86 Computer
    • 2.4.1 Motherboard
    • 2.4.2 Video Output
    • 2.4.3 Memory
    • 2.4.4 Input-Output Ports and Device Interfaces
    • 2.4.5 Section Review
  • 2.5 Input-Output System
    • 2.5.1 Levels of I/O Access
    • 2.5.2 Section Review
  • 2.6 Chapter Summary
  • 2.7 Chapter Exercises
  • 3 Assembly Language Fundamentals
  • 3.1 Basic Elements of Assembly Language
    • 3.1.1 Integer Constants
    • 3.1.2 Integer Expressions
    • 3.1.3 Real Number Constants
    • 3.1.4 Character Constants
    • 3.1.5 String Constants
    • 3.1.6 Reserved Words
    • 3.1.7 Identifiers
    • 3.1.8 Directives
    • 3.1.9 Instructions
    • 3.1.10 The NOP (No Operation) Instruction
    • 3.1.11 Section Review
  • 3.2 Example: Adding and Subtracting Integers
    • 3.2.1 Alternative Version of AddSub
    • 3.2.2 Program Template
    • 3.2.3 Section Review
  • 3.3 Assembling, Linking, and Running Programs
    • 3.3.1 The Assemble-Link-Execute Cycle
    • 3.3.2 Section Review
  • 3.4 Defining Data Contents vii
    • 3.4.1 Intrinsic Data Types
    • 3.4.2 Data Definition Statement
    • 3.4.3 Defining BYTE and SBYTE Data
    • 3.4.4 Defining WORD and SWORD Data
    • 3.4.5 Defining DWORD and SDWORD Data
    • 3.4.6 Defining QWORD Data
    • 3.4.7 Defining Packed Binary Coded Decimal (TBYTE) Data
    • 3.4.8 Defining Real Number Data
    • 3.4.9 Little Endian Order
    • 3.4.10 Adding Variables to the AddSub Program
    • 3.4.11 Declaring Uninitialized Data
    • 3.4.12 Section Review
  • 3.5 Symbolic Constants
    • 3.5.1 Equal-Sign Directive
    • 3.5.2 Calculating the Sizes of Arrays and Strings
    • 3.5.3 EQU Directive
    • 3.5.4 TEXTEQU Directive
    • 3.5.5 Section Review
  • 3.6 Real-Address Mode Programming (Optional)
    • 3.6.1 Basic Changes
  • 3.7 Chapter Summary
  • 3.8 Programming Exercises
    • and Arithmetic 4 Data Transfers, Addressing,
  • 4.1 Data Transfer Instructions
    • 4.1.1 Introduction
    • 4.1.2 Operand Types
    • 4.1.3 Direct Memory Operands
    • 4.1.4 MOV Instruction
    • 4.1.5 Zero/Sign Extension of Integers
    • 4.1.6 LAHF and SAHF Instructions
    • 4.1.7 XCHG Instruction
    • 4.1.8 Direct-Offset Operands
    • 4.1.9 Example Program (Moves)
    • 4.1.10 Section Review
  • 4.2 Addition and Subtraction
    • 4.2.1 INC and DEC Instructions
    • 4.2.2 ADD Instruction
    • 4.2.3 SUB Instruction
    • 4.2.4 NEG Instruction
    • 4.2.5 Implementing Arithmetic Expressions viii Contents
    • 4.2.6 Flags Affected by Addition and Subtraction
    • 4.2.7 Example Program (AddSub3)
    • 4.2.8 Section Review
  • 4.3 Data-Related Operators and Directives
    • 4.3.1 OFFSET Operator
    • 4.3.2 ALIGN Directive
    • 4.3.3 PTR Operator
    • 4.3.4 TYPE Operator
    • 4.3.5 LENGTHOF Operator
    • 4.3.6 SIZEOF Operator
    • 4.3.7 LABEL Directive
    • 4.3.8 Section Review
  • 4.4 Indirect Addressing
    • 4.4.1 Indirect Operands
    • 4.4.2 Arrays
    • 4.4.3 Indexed Operands
    • 4.4.4 Pointers
    • 4.4.5 Section Review
  • 4.5 JMP and LOOP Instructions
    • 4.5.1 JMP Instruction
    • 4.5.2 LOOP Instruction
    • 4.5.3 Summing an Integer Array
    • 4.5.4 Copying a String
    • 4.5.5 Section Review
  • 4.6 Chapter Summary
  • 4.7 Programming Exercises
  • 5 Procedures
  • 5.1 Introduction
  • 5.2 Linking to an External Library
    • 5.2.1 Background Information
    • 5.2.2 Section Review
  • 5.3 The Book’s Link Library
    • 5.3.1 Overview
    • 5.3.2 Individual Procedure Descriptions
    • 5.3.3 Library Test Programs
    • 5.3.4 Section Review
  • 5.4 Stack Operations
    • 5.4.1 Runtime Stack
    • 5.4.2 PUSH and POP Instructions
    • 5.4.3 Section Review
  • 5.5 Defining and Using Procedures Contents ix
    • 5.5.1 PROC Directive
    • 5.5.2 CALL and RET Instructions
    • 5.5.3 Example: Summing an Integer Array
    • 5.5.4 Flowcharts
    • 5.5.5 Saving and Restoring Registers
    • 5.5.6 Section Review
  • 5.6 Program Design Using Procedures
    • 5.6.1 Integer Summation Program (Design)
    • 5.6.2 Integer Summation Implementation
    • 5.6.3 Section Review
  • 5.7 Chapter Summary
  • 5.8 Programming Exercises
  • 6 Conditional Processing
  • 6.1 Introduction
  • 6.2 Boolean and Comparison Instructions
    • 6.2.1 The CPU Flags
    • 6.2.2 AND Instruction
    • 6.2.3 OR Instruction
    • 6.2.4 Bit-Mapped Sets
    • 6.2.5 XOR Instruction
    • 6.2.6 NOT Instruction
    • 6.2.7 TEST Instruction
    • 6.2.8 CMP Instruction
    • 6.2.9 Setting and Clearing Individual CPU Flags
    • 6.2.10 Section Review
  • 6.3 Conditional Jumps
    • 6.3.1 Conditional Structures
    • 6.3.2 J cond Instruction
    • 6.3.3 Types of Conditional Jump Instructions
    • 6.3.4 Conditional Jump Applications
    • 6.3.5 Section Review
  • 6.4 Conditional Loop Instructions
    • 6.4.1 LOOPZ and LOOPE Instructions
    • 6.4.2 LOOPNZ and LOOPNE Instructions
    • 6.4.3 Section Review
  • 6.5 Conditional Structures
    • 6.5.1 Block-Structured IF Statements
    • 6.5.2 Compound Expressions
    • 6.5.3 WHILE Loops
    • 6.5.4 Table-Driven Selection
    • 6.5.5 Section Review
  • 6.6 Application: Finite-State Machines x Contents
    • 6.6.1 Validating an Input String
    • 6.6.2 Validating a Signed Integer
    • 6.6.3 Section Review
  • 6.7 Conditional Control Flow Directives
    • 6.7.1 Creating IF Statements
    • 6.7.2 Signed and Unsigned Comparisons
    • 6.7.3 Compound Expressions
    • 6.7.4 Creating Loops with .REPEAT and .WHILE
  • 6.8 Chapter Summary
  • 6.9 Programming Exercises
  • 7 Integer Arithmetic
  • 7.1 Introduction
  • 7.2 Shift and Rotate Instructions
    • 7.2.1 Logical Shifts and Arithmetic Shifts
    • 7.2.2 SHL Instruction
    • 7.2.3 SHR Instruction
    • 7.2.4 SAL and SAR Instructions
    • 7.2.5 ROL Instruction
    • 7.2.6 ROR Instruction
    • 7.2.7 RCL and RCR Instructions
    • 7.2.8 Signed Overflow
    • 7.2.9 SHLD/SHRD Instructions
    • 7.2.10 Section Review
  • 7.3 Shift and Rotate Applications
    • 7.3.1 Shifting Multiple Doublewords
    • 7.3.2 Binary Multiplication
    • 7.3.3 Displaying Binary Bits
    • 7.3.4 Isolating MS-DOS File Date Fields
    • 7.3.5 Section Review
  • 7.4 Multiplication and Division Instructions
    • 7.4.1 MUL Instruction
    • 7.4.2 IMUL Instruction
    • 7.4.3 Measuring Program Execution Times
    • 7.4.4 DIV Instruction
    • 7.4.5 Signed Integer Division
    • 7.4.6 Implementing Arithmetic Expressions
    • 7.4.7 Section Review
  • 7.5 Extended Addition and Subtraction
    • 7.5.1 ADC Instruction
    • 7.5.2 Extended Addition Example
    • 7.5.3 SBB Instruction Contents xi
    • 7.5.4 Section Review
  • 7.6 ASCII and Unpacked Decimal Arithmetic
    • 7.6.1 AAA Instruction
    • 7.6.2 AAS Instruction
    • 7.6.3 AAM Instruction
    • 7.6.4 AAD Instruction
    • 7.6.5 Section Review
  • 7.7 Packed Decimal Arithmetic
    • 7.7.1 DAA Instruction
    • 7.7.2 DAS Instruction
    • 7.7.3 Section Review
  • 7.8 Chapter Summary
  • 7.9 Programming Exercises
  • 8 Advanced Procedures
  • 8.1 Introduction
  • 8.2 Stack Frames
    • 8.2.1 Stack Parameters
    • 8.2.2 Accessing Stack Parameters
    • 8.2.3 Local Variables
    • 8.2.4 ENTER and LEAVE Instructions
    • 8.2.5 LOCAL Directive
    • 8.2.6 Section Review
  • 8.3 Recursion
    • 8.3.1 Recursively Calculating a Sum
    • 8.3.2 Calculating a Factorial
    • 8.3.3 Section Review
  • 8.4 INVOKE, ADDR, PROC, and PROTO
    • 8.4.1 INVOKE Directive
    • 8.4.2 ADDR Operator
    • 8.4.3 PROC Directive
    • 8.4.4 PROTO Directive
    • 8.4.5 Parameter Classifications
    • 8.4.6 Example: Exchanging Two Integers
    • 8.4.7 Debugging Tips
    • 8.4.8 WriteStackFrame Procedure
    • 8.4.9 Section Review
  • 8.5 Creating Multimodule Programs
    • 8.5.1 Hiding and Exporting Procedure Names
    • 8.5.2 Calling External Procedures
    • 8.5.3 Using Variables and Symbols across Module Boundaries xii Contents
    • 8.5.4 Example: ArraySum Program
    • 8.5.5 Creating the Modules Using Extern
    • 8.5.6 Creating the Modules Using INVOKE and PROTO
    • 8.5.7 Section Review
  • 8.6 Java Bytecodes
    • 8.6.1 Java Virtual Machine
    • 8.6.2 Instruction Set
    • 8.6.3 Java Disassembly Examples
  • 8.7 Chapter Summary
  • 8.8 Programming Exercises
  • 9 Strings and Arrays
  • 9.1 Introduction
  • 9.2 String Primitive Instructions
    • 9.2.1 MOVSB, MOVSW, and MOVSD
    • 9.2.2 CMPSB, CMPSW, and CMPSD
    • 9.2.3 SCASB, SCASW, and SCASD
    • 9.2.4 STOSB, STOSW, and STOSD
    • 9.2.5 LODSB, LODSW, and LODSD
    • 9.2.6 Section Review
  • 9.3 Selected String Procedures
    • 9.3.1 Str_compare Procedure
    • 9.3.2 Str_length Procedure
    • 9.3.3 Str_copy Procedure
    • 9.3.4 Str_trim Procedure
    • 9.3.5 Str_ucase Procedure
    • 9.3.6 String Library Demo Program
    • 9.3.7 Section Review
  • 9.4 Two-Dimensional Arrays
    • 9.4.1 Ordering of Rows and Columns
    • 9.4.2 Base-Index Operands
    • 9.4.3 Base-Index-Displacement Operands
    • 9.4.4 Section Review
  • 9.5 Searching and Sorting Integer Arrays
    • 9.5.1 Bubble Sort
    • 9.5.2 Binary Search
    • 9.5.3 Section Review
  • 9.6 Java Bytecodes: String Processing
  • 9.7 Chapter Summary
  • 9.8 Programming Exercises
  • 10 Structures and Macros Contents xiii
  • 10.1 Structures
    • 10.1.1 Defining Structures
    • 10.1.2 Declaring Structure Variables
    • 10.1.3 Referencing Structure Variables
    • 10.1.4 Example: Displaying the System Time
    • 10.1.5 Structures Containing Structures
    • 10.1.6 Example: Drunkard’s Walk
    • 10.1.7 Declaring and Using Unions
    • 10.1.8 Section Review
  • 10.2 Macros
    • 10.2.1 Overview
    • 10.2.2 Defining Macros
    • 10.2.3 Invoking Macros
    • 10.2.4 Additional Macro Features
    • 10.2.5 Using the Book’s Macro Library
    • 10.2.6 Example Program: Wrappers
    • 10.2.7 Section Review
  • 10.3 Conditional-Assembly Directives
    • 10.3.1 Checking for Missing Arguments
    • 10.3.2 Default Argument Initializers
    • 10.3.3 Boolean Expressions
    • 10.3.4 IF, ELSE, and ENDIF Directives
    • 10.3.5 The IFIDN and IFIDNI Directives
    • 10.3.6 Example: Summing a Matrix Row
    • 10.3.7 Special Operators
    • 10.3.8 Macro Functions
    • 10.3.9 Section Review
  • 10.4 Defining Repeat Blocks
    • 10.4.1 WHILE Directive
    • 10.4.2 REPEAT Directive
    • 10.4.3 FOR Directive
    • 10.4.4 FORC Directive
    • 10.4.5 Example: Linked List
    • 10.4.6 Section Review
  • 10.5 Chapter Summary
  • 10.6 Programming Exercises
  • 11 MS-Windows Programming
  • 11.1 Win32 Console Programming
    • 11.1.1 Background Information
    • 11.1.2 Win32 Console Functions
    • 11.1.3 Displaying a Message Box
    • 11.1.4 Console Input xiv Contents
    • 11.1.5 Console Output
    • 11.1.6 Reading and Writing Files
    • 11.1.7 File I/O in the Irvine32 Library
    • 11.1.8 Testing the File I/O Procedures
    • 11.1.9 Console Window Manipulation
    • 11.1.10 Controlling the Cursor
    • 11.1.11 Controlling the Text Color
    • 11.1.12 Time and Date Functions
    • 11.1.13 Section Review
  • 11.2 Writing a Graphical Windows Application
    • 11.2.1 Necessary Structures
    • 11.2.2 The MessageBox Function
    • 11.2.3 The WinMain Procedure
    • 11.2.4 The WinProc Procedure
    • 11.2.5 The ErrorHandler Procedure
    • 11.2.6 Program Listing
    • 11.2.7 Section Review
  • 11.3 Dynamic Memory Allocation
    • 11.3.1 HeapTest Programs
    • 11.3.2 Section Review
  • 11.4 x86 Memory Management
    • 11.4.1 Linear Addresses
    • 11.4.2 Page Translation
    • 11.4.3 Section Review
  • 11.5 Chapter Summary
  • 11.6 Programming Exercises
    • Encoding 12 Floating-Point Processing and Instruction
  • 12.1 Floating-Point Binary Representation
    • 12.1.1 IEEE Binary Floating-Point Representation
    • 12.1.2 The Exponent
    • 12.1.3 Normalized Binary Floating-Point Numbers
    • 12.1.4 Creating the IEEE Representation
    • 12.1.5 Converting Decimal Fractions to Binary Reals
    • 12.1.6 Section Review
  • 12.2 Floating-Point Unit
    • 12.2.1 FPU Register Stack
    • 12.2.2 Rounding
    • 12.2.3 Floating-Point Exceptions
    • 12.2.4 Floating-Point Instruction Set
    • 12.2.5 Arithmetic Instructions Contents xv
    • 12.2.6 Comparing Floating-Point Values
    • 12.2.7 Reading and Writing Floating-Point Values
    • 12.2.8 Exception Synchronization
    • 12.2.9 Code Examples
    • 12.2.10 Mixed-Mode Arithmetic
    • 12.2.11 Masking and Unmasking Exceptions
    • 12.2.12 Section Review
  • 12.3 x86 Instruction Encoding
    • 12.3.1 Instruction Format
    • 12.3.2 Single-Byte Instructions
    • 12.3.3 Move Immediate to Register
    • 12.3.4 Register-Mode Instructions
    • 12.3.5 Processor Operand-Size Prefix
    • 12.3.6 Memory-Mode Instructions
    • 12.3.7 Section Review
  • 12.4 Chapter Summary
  • 12.5 Programming Exercises
  • 13 High-Level Language Interface
  • 13.1 Introduction
    • 13.1.1 General Conventions
    • 13.1.2 .MODEL Directive
    • 13.1.3 Section Review
  • 13.2 Inline Assembly Code
    • 13.2.1 __asm Directive in Microsoft Visual C++
    • 13.2.2 File Encryption Example
    • 13.2.3 Section Review
  • 13.3 Linking to C/C++ in Protected Mode
    • 13.3.1 Using Assembly Language to Optimize C++ Code
    • 13.3.2 Calling C and C++ Functions
    • 13.3.3 Multiplication Table Example
    • 13.3.4 Calling C Library Functions
    • 13.3.5 Directory Listing Program
    • 13.3.6 Section Review
  • 13.4 Linking to C/C++ in Real-Address Mode
    • 13.4.1 Linking to Borland C++
    • 13.4.2 ReadSector Example
    • 13.4.3 Example: Large Random Integers
    • 13.4.4 Section Review
  • 13.5 Chapter Summary
  • 13.6 Programming Exercises
  • 14 16-Bit MS-DOS Programming xvi Contents
  • 14.1 MS-DOS and the IBM-PC
    • 14.1.1 Memory Organization
    • 14.1.2 Redirecting Input-Output
    • 14.1.3 Software Interrupts
    • 14.1.4 INT Instruction
    • 14.1.5 Coding for 16-Bit Programs
    • 14.1.6 Section Review
  • 14.2 MS-DOS Function Calls (INT 21h)
    • 14.2.1 Selected Output Functions
    • 14.2.2 Hello World Program Example
    • 14.2.3 Selected Input Functions
    • 14.2.4 Date/Time Functions
    • 14.2.5 Section Review
  • 14.3 Standard MS-DOS File I/O Services
    • 14.3.1 Create or Open File (716Ch)
    • 14.3.2 Close File Handle (3Eh)
    • 14.3.3 Move File Pointer (42h)
    • 14.3.4 Get File Creation Date and Time
    • 14.3.5 Selected Library Procedures
    • 14.3.6 Example: Read and Copy a Text File
    • 14.3.7 Reading the MS-DOS Command Tail
    • 14.3.8 Example: Creating a Binary File
    • 14.3.9 Section Review
  • 14.4 Chapter Summary
  • 14.5 Programming Exercises
  • Appendix A MASM Reference 17 Expert MS-DOS Programming
  • Appendix B The x86 Instruction Set
  • Appendix C Answers to Review Questions

This page intentionally left blank

Downloaded From: www.bsit.zxq.net

xix

Preface

Assembly Language for x86 Processors, Sixth Edition , teaches assembly language programming and architecture for Intel and AMD processors. It is an appropriate text for the following types of college courses:

- Assembly Language Programming - Fundamentals of Computer Systems - Fundamentals of Computer Architecture Students use Intel or AMD processors and program with Microsoft Macro Assembler (MASM), running on Windows 98, XP, Vista, and Windows 7. Although this book was originally designed as a programming textbook for college students, it serves as an effective supplement to computer architecture courses. As a testament to its popularity, previous editions have been translated into Spanish, Korean, Chinese, French, Russian, and Polish. Emphasis of Topics This edition includes topics that lead naturally into subsequent courses in computer architecture, operating systems, and compiler writing: - Virtual machine concept - Instruction set architecture - Elementary Boolean operations - Instruction execution cycle - Memory access and handshaking - Interrupts and polling - Hardware-based I/O - Floating-point binary representation Other topics relate specially to Intel and AMD architecture: - Protected memory and paging - Memory segmentation in real-address mode - 16-bit interrupt handling - MS-DOS and BIOS system calls (interrupts) - Floating-point unit architecture and programming - Instruction encoding Certain examples presented in the book lend themselves to courses that occur later in a computer science curriculum: - Searching and sorting algorithms - High-level language structures

Downloaded From: www.bsit.zxq.net

For More BS-IT Books, Notes & Assignments visit: www.bsit.zxq.net