preface xix basic concepts 1

21
v Contents Preface xix 1 Basic Concepts 1 1.1 Welcome to Assembly Language 1 1.1.1 Good Questions to Ask 2 1.1.2 Assembly Language Applications 5 1.1.3 Section Review 6 1.2 Virtual Machine Concept 7 1.2.1 Section Review 9 1.3 Data Representation 9 1.3.1 Binary Integers 9 1.3.2 Binary Addition 11 1.3.3 Integer Storage Sizes 12 1.3.4 Hexadecimal Integers 13 1.3.5 Signed Integers 15 1.3.6 Character Storage 17 1.3.7 Section Review 19 1.4 Boolean Operations 22 1.4.1 Truth Tables for Boolean Functions 24 1.4.2 Section Review 26 1.5 Chapter Summary 26 1.6 Exercises 27 1.6.1 Programming Tasks 27 1.6.2 Non-Programming Tasks 27 2 IA-32 Processor Architecture 29 2.1 General Concepts 29 2.1.1 Basic Microcomputer Design 30 2.1.2 Instruction Execution Cycle 31 2.1.3 Reading from Memory 33 2.1.4 How Programs Run 34 2.1.5 Section Review 35 86028 00 i-xxviii FM r0 vs.fm Page v Friday, December 4, 2009 7:30 PM

Upload: others

Post on 11-Jun-2022

1 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Preface xix Basic Concepts 1

v

Contents

Preface xix

1

Basic Concepts 1

1.1 Welcome to Assembly Language 1

1.1.1 Good Questions to Ask 21.1.2 Assembly Language Applications 51.1.3 Section Review 6

1.2 Virtual Machine Concept 7

1.2.1 Section Review 9

1.3 Data Representation 9

1.3.1 Binary Integers 91.3.2 Binary Addition 111.3.3 Integer Storage Sizes 121.3.4 Hexadecimal Integers 131.3.5 Signed Integers 151.3.6 Character Storage 171.3.7 Section Review 19

1.4 Boolean Operations 22

1.4.1 Truth Tables for Boolean Functions 241.4.2 Section Review 26

1.5 Chapter Summary 26

1.6 Exercises 27

1.6.1 Programming Tasks 271.6.2 Non-Programming Tasks 27

2

IA-32 Processor Architecture 29

2.1 General Concepts 29

2.1.1 Basic Microcomputer Design 302.1.2 Instruction Execution Cycle 312.1.3 Reading from Memory 332.1.4 How Programs Run 342.1.5 Section Review 35

86028 00 i-xxviii FM r0 vs.fm Page v Friday, December 4, 2009 7:30 PM

Kip
Text Box
Kip
Text Box
Page 2: Preface xix Basic Concepts 1

vi Contents

2.2 x86 Processor Architecture 36

2.2.1 Modes of Operation 362.2.2 Basic Execution Environment 362.2.3 Floating-Point Unit 392.2.4 Overview of Intel Microprocessors 392.2.5 Section Review 42

2.3 x86 Memory Management 43

2.3.1 Real-Address Mode 432.3.2 Protected Mode 452.3.3 Section Review 47

2.4 Components of a Typical x86 Computer 48

2.4.1 Motherboard 482.4.2 Video Output 502.4.3 Memory 502.4.4 Input-Output Ports and Device Interfaces 502.4.5 Section Review 52

2.5 Input-Output System 52

2.5.1 Levels of I/O Access 522.5.2 Section Review 55

2.6 Chapter Summary 55

2.7 Chapter Exercises 57

3

Assembly Language Fundamentals 58

3.1 Basic Elements of Assembly Language 58

3.1.1 Integer Constants 593.1.2 Integer Expressions 603.1.3 Real Number Constants 613.1.4 Character Constants 613.1.5 String Constants 613.1.6 Reserved Words 623.1.7 Identifiers 623.1.8 Directives 623.1.9 Instructions 633.1.10 The NOP (No Operation) Instruction 653.1.11 Section Review 66

3.2 Example: Adding and Subtracting Integers 66

3.2.1 Alternative Version of AddSub 693.2.2 Program Template 703.2.3 Section Review 70

3.3 Assembling, Linking, and Running Programs 71

3.3.1 The Assemble-Link-Execute Cycle 713.3.2 Section Review 77

86028 00 i-xxviii FM r0 vs.fm Page vi Friday, December 4, 2009 7:30 PM

Page 3: Preface xix Basic Concepts 1

Contents vii

3.4 Defining Data 77

3.4.1 Intrinsic Data Types 773.4.2 Data Definition Statement 773.4.3 Defining BYTE and SBYTE Data 783.4.4 Defining WORD and SWORD Data 803.4.5 Defining DWORD and SDWORD Data 813.4.6 Defining QWORD Data 813.4.7 Defining Packed Binary Coded Decimal (TBYTE) Data 823.4.8 Defining Real Number Data 833.4.9 Little Endian Order 833.4.10 Adding Variables to the AddSub Program 843.4.11 Declaring Uninitialized Data 853.4.12 Section Review 85

3.5 Symbolic Constants 86

3.5.1 Equal-Sign Directive 863.5.2 Calculating the Sizes of Arrays and Strings 873.5.3 EQU Directive 883.5.4 TEXTEQU Directive 893.5.5 Section Review 90

3.6 Real-Address Mode Programming (Optional) 90

3.6.1 Basic Changes 90

3.7 Chapter Summary 91

3.8 Programming Exercises 92

4

Data Transfers, Addressing,and Arithmetic 94

4.1 Data Transfer Instructions 94

4.1.1 Introduction 944.1.2 Operand Types 954.1.3 Direct Memory Operands 964.1.4 MOV Instruction 964.1.5 Zero/Sign Extension of Integers 984.1.6 LAHF and SAHF Instructions 1004.1.7 XCHG Instruction 1004.1.8 Direct-Offset Operands 1014.1.9 Example Program (Moves) 1024.1.10 Section Review 103

4.2 Addition and Subtraction 104

4.2.1 INC and DEC Instructions 1044.2.2 ADD Instruction 1044.2.3 SUB Instruction 1054.2.4 NEG Instruction 105

86028 00 i-xxviii FM r0 vs.fm Page vii Friday, December 4, 2009 7:30 PM

Page 4: Preface xix Basic Concepts 1

viii Contents

4.2.5 Implementing Arithmetic Expressions 1064.2.6 Flags Affected by Addition and Subtraction 1064.2.7 Example Program (AddSub3) 1104.2.8 Section Review 111

4.3 Data-Related Operators and Directives 112

4.3.1 OFFSET Operator 1124.3.2 ALIGN Directive 1134.3.3 PTR Operator 1144.3.4 TYPE Operator 1154.3.5 LENGTHOF Operator 1154.3.6 SIZEOF Operator 1164.3.7 LABEL Directive 1164.3.8 Section Review 117

4.4 Indirect Addressing 117

4.4.1 Indirect Operands 1184.4.2 Arrays 1194.4.3 Indexed Operands 1204.4.4 Pointers 1214.4.5 Section Review 123

4.5 JMP and LOOP Instructions 124

4.5.1 JMP Instruction 1244.5.2 LOOP Instruction 1244.5.3 Summing an Integer Array 1264.5.4 Copying a String 1264.5.5 Section Review 127

4.6 Chapter Summary 128

4.7 Programming Exercises 129

5

Procedures 132

5.1 Introduction 132

5.2 Linking to an External Library 132

5.2.1 Background Information 1335.2.2 Section Review 134

5.3 The Book’s Link Library 134

5.3.1 Overview 1365.3.2 Individual Procedure Descriptions 1375.3.3 Library Test Programs 1495.3.4 Section Review 157

5.4 Stack Operations 157

5.4.1 Runtime Stack 1585.4.2 PUSH and POP Instructions 1605.4.3 Section Review 162

86028 00 i-xxviii FM r0 vs.fm Page viii Friday, December 4, 2009 7:30 PM

Page 5: Preface xix Basic Concepts 1

Contents ix

5.5 Defining and Using Procedures 163

5.5.1 PROC Directive 1635.5.2 CALL and RET Instructions 1655.5.3 Example: Summing an Integer Array 1685.5.4 Flowcharts 1695.5.5 Saving and Restoring Registers 1705.5.6 Section Review 171

5.6 Program Design Using Procedures 172

5.6.1 Integer Summation Program (Design) 1735.6.2 Integer Summation Implementation 1755.6.3 Section Review 177

5.7 Chapter Summary 177

5.8 Programming Exercises 178

6

Conditional Processing 180

6.1 Introduction 180

6.2 Boolean and Comparison Instructions 181

6.2.1 The CPU Flags 1826.2.2 AND Instruction 1826.2.3 OR Instruction 1836.2.4 Bit-Mapped Sets 1846.2.5 XOR Instruction 1866.2.6 NOT Instruction 1876.2.7 TEST Instruction 1876.2.8 CMP Instruction 1886.2.9 Setting and Clearing Individual CPU Flags 1896.2.10 Section Review 189

6.3 Conditional Jumps 190

6.3.1 Conditional Structures 1906.3.2 Jcond Instruction 1916.3.3 Types of Conditional Jump Instructions 1926.3.4 Conditional Jump Applications 1956.3.5 Section Review 199

6.4 Conditional Loop Instructions 200

6.4.1 LOOPZ and LOOPE Instructions 2006.4.2 LOOPNZ and LOOPNE Instructions 2016.4.3 Section Review 201

6.5 Conditional Structures 202

6.5.1 Block-Structured IF Statements 2026.5.2 Compound Expressions 2046.5.3 WHILE Loops 2066.5.4 Table-Driven Selection 2086.5.5 Section Review 210

86028 00 i-xxviii FM r0 vs.fm Page ix Friday, December 4, 2009 7:30 PM

Page 6: Preface xix Basic Concepts 1

x Contents

6.6 Application: Finite-State Machines 211

6.6.1 Validating an Input String 2116.6.2 Validating a Signed Integer 2126.6.3 Section Review 216

6.7 Conditional Control Flow Directives 217

6.7.1 Creating IF Statements 2186.7.2 Signed and Unsigned Comparisons 2196.7.3 Compound Expressions 2206.7.4 .REPEAT and .WHILE Directives 223

6.8 Chapter Summary 224

6.9 Programming Exercises 225

7

Integer Arithmetic 229

7.1 Introduction 229

7.2 Shift and Rotate Instructions 230

7.2.1 Logical Shifts and Arithmetic Shifts 2307.2.2 SHL Instruction 2317.2.3 SHR Instruction 2327.2.4 SAL and SAR Instructions 2337.2.5 ROL Instruction 2347.2.6 ROR Instruction 2357.2.7 RCL and RCR Instructions 2357.2.8 Signed Overflow 2367.2.9 SHLD/SHRD Instructions 2367.2.10 Section Review 238

7.3 Shift and Rotate Applications 239

7.3.1 Shifting Multiple Doublewords 2407.3.2 Binary Multiplication 2417.3.3 Displaying Binary Bits 2427.3.4 Isolating MS-DOS File Date Fields 2427.3.5 Section Review 243

7.4 Multiplication and Division Instructions 243

7.4.1 MUL Instruction 2437.4.2 IMUL Instruction 2457.4.3 Measuring Program Execution Times 2477.4.4 DIV Instruction 2497.4.5 Signed Integer Division 2507.4.6 Implementing Arithmetic Expressions 2537.4.7 Section Review 255

7.5 Extended Addition and Subtraction 256

7.5.1 ADC Instruction 2567.5.2 Extended Addition Example 257

86028 00 i-xxviii FM r0 vs.fm Page x Friday, December 4, 2009 7:30 PM

Page 7: Preface xix Basic Concepts 1

Contents xi

7.5.3 SBB Instruction 2587.5.4 Section Review 259

7.6 ASCII and Unpacked Decimal Arithmetic 260

7.6.1 AAA Instruction 2617.6.2 AAS Instruction 2627.6.3 AAM Instruction 2637.6.4 AAD Instruction 2637.6.5 Section Review 264

7.7 Packed Decimal Arithmetic 264

7.7.1 DAA Instruction 2647.7.2 DAS Instruction 2667.7.3 Section Review 266

7.8 Chapter Summary 266

7.9 Programming Exercises 267

8

Advanced Procedures 270

8.1 Introduction 270

8.2 Stack Frames 271

8.2.1 Stack Parameters 2728.2.2 Accessing Stack Parameters 2738.2.3 Local Variables 2818.2.4 ENTER and LEAVE Instructions 2858.2.5 LOCAL Directive 2868.2.6 Section Review 289

8.3 Recursion 290

8.3.1 Recursively Calculating a Sum 2918.3.2 Calculating a Factorial 2928.3.3 Section Review 298

8.4 INVOKE, ADDR, PROC, and PROTO 299

8.4.1 INVOKE Directive 2998.4.2 ADDR Operator 3008.4.3 PROC Directive 3018.4.4 PROTO Directive 3048.4.5 Parameter Classifications 3078.4.6 Example: Exchanging Two Integers 3078.4.7 Debugging Tips 3088.4.8 WriteStackFrame Procedure 3098.4.9 Section Review 310

8.5 Creating Multimodule Programs 311

8.5.1 Hiding and Exporting Procedure Names 3118.5.2 Calling External Procedures 312

86028 00 i-xxviii FM r0 vs.fm Page xi Friday, December 4, 2009 7:30 PM

Page 8: Preface xix Basic Concepts 1

xii Contents

8.5.3 Using Variables and Symbols Across Module Boundaries 3138.5.4 Example: ArraySum Program 3148.5.5 Creating the Modules Using Extern 3148.5.6 Creating the Modules Using INVOKE and PROTO 3188.5.7 Section Review 321

8.6 Java Bytecodes 321

8.6.1 Java Virtual Machine 3218.6.2 Instruction Set 3228.6.3 Java Disassembly Examples 323

8.7 Chapter Summary 328

8.8 Programming Exercises 329

9

Strings and Arrays 332

9.1 Introduction 332

9.2 String Primitive Instructions 333

9.2.1 MOVSB, MOVSW, and MOVSD 3349.2.2 CMPSB, CMPSW, and CMPSD 3359.2.3 SCASB, SCASW, and SCASD 3369.2.4 STOSB, STOSW, and STOSD 3369.2.5 LODSB, LODSW, and LODSD 3379.2.6 Section Review 337

9.3 Selected String Procedures 338

9.3.1 Str_compare Procedure 3389.3.2 Str_length Procedure 3399.3.3 Str_copy Procedure 3409.3.4 Str_trim Procedure 3409.3.5 Str_ucase Procedure 3439.3.6 String Library Demo Program 3449.3.7 Section Review 346

9.4 Two-Dimensional Arrays 346

9.4.1 Ordering of Rows and Columns 3469.4.2 Base-Index Operands 3479.4.3 Base-Index-Displacement Operands 3499.4.4 Section Review 350

9.5 Searching and Sorting Integer Arrays 350

9.5.1 Bubble Sort 3509.5.2 Binary Search 3529.5.3 Section Review 359

9.6 Java Bytecodes: String Processing 359

9.7 Chapter Summary 360

9.8 Programming Exercises 361

86028 00 i-xxviii FM r0 vs.fm Page xii Friday, December 4, 2009 7:30 PM

Page 9: Preface xix Basic Concepts 1

Contents xiii

10

Structures and Macros 366

10.1 Structures 366

10.1.1 Defining Structures 36710.1.2 Declaring Structure Variables 36810.1.3 Referencing Structure Variables 37010.1.4 Example: Displaying the System Time 37210.1.5 Structures Containing Structures 37510.1.6 Example: Drunkard’s Walk 37510.1.7 Declaring and Using Unions 37810.1.8 Section Review 381

10.2 Macros 382

10.2.1 Overview 38210.2.2 Defining Macros 38210.2.3 Invoking Macros 38310.2.4 Additional Macro Features 38410.2.5 Using the Book’s Macro Library 38810.2.6 Example Program: Wrappers 39410.2.7 Section Review 395

10.3 Conditional-Assembly Directives 396

10.3.1 Checking for Missing Arguments 39710.3.2 Default Argument Initializers 39810.3.3 Boolean Expressions 39910.3.4 IF, ELSE, and ENDIF Directives 39910.3.5 The IFIDN and IFIDNI Directives 40010.3.6 Example: Summing a Matrix Row 40110.3.7 Special Operators 40410.3.8 Macro Functions 40710.3.9 Section Review 409

10.4 Defining Repeat Blocks 410

10.4.1 WHILE Directive 41010.4.2 REPEAT Directive 41010.4.3 FOR Directive 41110.4.4 FORC Directive 41210.4.5 Example: Linked List 41210.4.6 Section Review 414

10.5 Chapter Summary 415

10.6 Programming Exercises 416

11

MS-Windows Programming 419

11.1 Win32 Console Programming 419

11.1.1 Background Information 42011.1.2 Win32 Console Functions 42411.1.3 Displaying a Message Box 426

86028 00 i-xxviii FM r0 vs.fm Page xiii Friday, December 4, 2009 7:30 PM

Page 10: Preface xix Basic Concepts 1

xiv Contents

11.1.4 Console Input 42911.1.5 Console Output 43511.1.6 Reading and Writing Files 43711.1.7 File I/O in the Irvine32 Library 44211.1.8 Testing the File I/O Procedures 44411.1.9 Console Window Manipulation 44711.1.10 Controlling the Cursor 45011.1.11 Controlling the Text Color 45111.1.12 Time and Date Functions 45311.1.13 Section Review 456

11.2 Writing a Graphical Windows Application 457

11.2.1 Necessary Structures 45811.2.2 The MessageBox Function 45911.2.3 The WinMain Procedure 46011.2.4 The WinProc Procedure 46011.2.5 The ErrorHandler Procedure 46111.2.6 Program Listing 46111.2.7 Section Review 465

11.3 Dynamic Memory Allocation 466

11.3.1 HeapTest Programs 46911.3.2 Section Review 473

11.4 x86 Memory Management 473

11.4.1 Linear Addresses 47311.4.2 Page Translation 47711.4.3 Section Review 479

11.5 Chapter Summary 479

11.6 Programming Exercises 481

12

Floating-Point Processing and Instruction Encoding 483

12.1 Floating-Point Binary Representation 483

12.1.1 IEEE Binary Floating-Point Representation 48412.1.2 The Exponent 48512.1.3 Normalized Binary Floating-Point Numbers 48612.1.4 Creating the IEEE Representation 48612.1.5 Converting Decimal Fractions to Binary Reals 48812.1.6 Section Review 490

12.2 Floating-Point Unit 490

12.2.1 FPU Register Stack 49112.2.2 Rounding 49312.2.3 Floating-Point Exceptions 49512.2.4 Floating-Point Instruction Set 495

86028 00 i-xxviii FM r0 vs.fm Page xiv Friday, December 4, 2009 7:30 PM

Page 11: Preface xix Basic Concepts 1

Contents xv

12.2.5 Arithmetic Instructions 49812.2.6 Comparing Floating-Point Values 50212.2.7 Reading and Writing Floating-Point Values 50412.2.8 Exception Synchronization 50612.2.9 Code Examples 50712.2.10 Mixed-Mode Arithmetic 50812.2.11 Masking and Unmasking Exceptions 50912.2.12 Section Review 511

12.3 x86 Instruction Encoding 512

12.3.1 Instruction Format 51212.3.2 Single-Byte Instructions 51312.3.3 Move Immediate to Register 51412.3.4 Register-Mode Instructions 51412.3.5 Processor Operand-Size Prefix 51512.3.6 Memory-Mode Instructions 51612.3.7 Section Review 519

12.4 Chapter Summary 520

12.5 Programming Exercises 521

13 High-Level Language Interface 52513.1 Introduction 525

13.1.1 General Conventions 52613.1.2 .MODEL Directive 52713.1.3 Section Review 529

13.2 Inline Assembly Code 52913.2.1 __asm Directive in Microsoft Visual C++ 52913.2.2 File Encryption Example 53213.2.3 Section Review 535

13.3 Linking to C/C++ in Protected Mode 53513.3.1 Using Assembly Language to Optimize C++ Code 53613.3.2 Calling C and C++ Functions 54213.3.3 Multiplication Table Example 54413.3.4 Calling C Library Functions 54713.3.5 Directory Listing Program 55013.3.6 Section Review 552

13.4 Linking to C/C++ in Real-Address Mode 55213.4.1 Linking to Borland C++ 55313.4.2 ReadSector Example 55413.4.3 Example: Large Random Integers 55813.4.4 Section Review 559

13.5 Chapter Summary 560

13.6 Programming Exercises 561

86028 00 i-xxviii FM r0 vs.fm Page xv Friday, December 4, 2009 7:30 PM

Page 12: Preface xix Basic Concepts 1

xvi Contents

14 16-Bit MS-DOS Programming 56214.1 MS-DOS and the IBM-PC 562

14.1.1 Memory Organization 56314.1.2 Redirecting Input-Output 56414.1.3 Software Interrupts 56514.1.4 INT Instruction 56514.1.5 Coding for 16-Bit Programs 56714.1.6 Section Review 568

14.2 MS-DOS Function Calls (INT 21h) 56814.2.1 Selected Output Functions 57014.2.2 Hello World Program Example 57214.2.3 Selected Input Functions 57314.2.4 Date/Time Functions 57714.2.5 Section Review 581

14.3 Standard MS-DOS File I/O Services 58114.3.1 Create or Open File (716Ch) 58314.3.2 Close File Handle (3Eh) 58414.3.3 Move File Pointer (42h) 58414.3.4 Get File Creation Date and Time 58514.3.5 Selected Library Procedures 58514.3.6 Example: Read and Copy a Text File 58614.3.7 Reading the MS-DOS Command Tail 58814.3.8 Example: Creating a Binary File 59114.3.9 Section Review 594

14.4 Chapter Summary 594

14.5 Chapter Exercises 596

15 Disk Fundamentals15.1 Disk Storage Systems

15.1.1 Tracks, Cylinders, and Sectors15.1.2 Disk Partitions (Volumes)15.1.3 Section Review

15.2 File Systems15.2.1 FAT1215.2.2 FAT1615.2.3 FAT3215.2.4 NTFS15.2.5 Primary Disk Areas15.2.6 Section Review

15.3 Disk Directory15.3.1 MS-DOS Directory Structure15.3.2 Long Filenames in MS-Windows

86028 00 i-xxviii FM r0 vs.fm Page xvi Friday, December 4, 2009 7:30 PM

Page 13: Preface xix Basic Concepts 1

Contents xvii

15.3.3 File Allocation Table (FAT)15.3.4 Section Review

15.4 Reading and Writing Disk Sectors (7305h)15.4.1 Sector Display Program15.4.2 Section Review

15.5 System-Level File Functions15.5.1 Get Disk Free Space (7303h)15.5.2 Create Subdirectory (39h)15.5.3 Remove Subdirectory (3Ah)15.5.4 Set Current Directory (3Bh)15.5.5 Get Current Directory (47h)15.5.6 Get and Set File Attributes (7143h)15.5.7 Section Review

15.6 Chapter Summary

15.7 Programming Exercises

16 BIOS-Level Programming16.1 Introduction

16.1.1 BIOS Data Area

16.2 Keyboard Input with INT 16h16.2.1 How the Keyboard Works16.2.2 INT 16h Functions16.2.3 Section Review

16.3 VIDEO Programming with INT 10h16.3.1 Basic Background16.3.2 Controlling the Color16.3.3 INT 10h Video Functions16.3.4 Library Procedure Examples16.3.5 Section Review

16.4 Drawing Graphics Using INT 10h16.4.1 INT 10h Pixel-Related Functions16.4.2 DrawLine Program16.4.3 Cartesian Coordinates Program16.4.4 Converting Cartesian Coordinates to Screen Coordinates16.4.5 Section Review

16.5 Memory-Mapped Graphics16.5.1 Mode 13h: 320 � 200, 256 Colors16.5.2 Memory-Mapped Graphics Program16.5.3 Section Review

16.6 Mouse Programming16.6.1 Mouse INT 33h Functions

86028 00 i-xxviii FM r0 vs.fm Page xvii Friday, December 4, 2009 7:30 PM

Page 14: Preface xix Basic Concepts 1

xviii Contents

16.6.2 Mouse Tracking Program16.6.3 Section Review

16.7 Chapter Summary

16.8 Chapter Exercises

17 Expert MS-DOS Programming17.1 Introduction

17.2 Defining Segments17.2.1 Simplified Segment Directives17.2.2 Explicit Segment Definitions17.2.3 Segment Overrides17.2.4 Combining Segments17.2.5 Section Review

17.3 Runtime Program Structure17.3.1 Program Segment Prefix17.3.2 COM Programs17.3.3 EXE Programs17.3.4 Section Review

17.4 Interrupt Handling17.4.1 Hardware Interrupts17.4.2 Interrupt Control Instructions17.4.3 Writing a Custom Interrupt Handler17.4.4 Terminate and Stay Resident Programs17.4.5 Application: The No_Reset Program17.4.6 Section Review

17.5 Hardware Control Using I/O Ports17.5.1 Input-Output Ports17.5.2 PC Sound Program

17.6 Chapter Summary

Appendix A MASM Reference 598Appendix B The IA-32 Instruction Set 620Appendix C Answers to Review Questions 655Appendix D BIOS and MS-DOS InterruptsIndex 000

86028 00 i-xxviii FM r0 vs.fm Page xviii Friday, December 4, 2009 7:30 PM

Page 15: Preface xix Basic Concepts 1

xix

Preface

Assembly Language for x86 Processors, Sixth Edition, teaches assembly language programmingand architecture for Intel and AMD processors. It is an appropriate text for the following typesof 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, and Vista. Although this book was originally designedas a programming textbook for college students, it serves as an effective supplement to computerarchitecture courses. As a testament to its popularity, previous editions have been translated intoSpanish, Korean, Chinese, French, Russian, and Polish.

Emphasis of Topics This edition includes topics that lead naturally into subsequent coursesin 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 specically 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 computerscience curriculum:

• Searching and sorting algorithms• High-level language structures

86028 00 i-xxviii FM r0 vs.fm Page xix Friday, December 4, 2009 7:30 PM

Page 16: Preface xix Basic Concepts 1

xx Preface

• Finite-state machines• Code optimization examples

Improvements in the Sixth EditionChapters 1–8 have major revisions to the text explanations and examples.

• We have added numerous step-by-step descriptions of sample programs, particularly inChapters 1–8.

• Many new illustrations have been inserted into the chapters to improve student comprehen-sion of concepts and details.

• Java Bytecodes The Java Virtual Machine (JVM) provides an excellent real-life example of astack-oriented architecture. It provides an excellent contrast to x86 architecture. Therefore, inChapters 8 and 9, the author explains the basic operation of Java bytecodes with short illus-trative examples. Numerous short examples are shown in disassembled bytecode format, fol-lowed by detailed step-by-step explanations.

• Many programming exercises have been replaced and added to the first 8 chapters.• Tutorial videos by the author are available on the Pearson Web site to explain worked-out examples.

This book is still focused on its primary goal, to teach students how to write and debug programsat the machine level. It will never replace a complete book on computer architecture, but it doesgive students the first-hand experience of writing software in an environment that teaches themhow a computer works. Our premise is that students retain knowledge better when theory iscombined with experience. In an engineering course, students construct prototypes; in a com-puter architecture course, students should write machine-level programs. In both cases, theyhave a memorable experience that gives them the confidence to work in any OS/machine-ori-ented environment.

Real Mode and Protected Mode This edition emphasizes 32-bit protected mode, but it stillhas three electronic chapters devoted to real-mode programming. For example, there is an entirechapter on BIOS programming for the keyboard, video display (including graphics), and mouse.Another chapter covers MS-DOS programming using interrupts (system calls). Students canbenefit from programming directly to hardware and the BIOS.

The examples in the first half of the book is nearly all presented as 32-bit text-oriented appli-cations running in protected mode using the flat memory model. This approach is wonderfullysimple because it avoids the complications of segment-offset addressing. Specially marked para-graphs and popup boxes point out occasional differences between protected mode and real-modeprogramming. Most differences are abstracted by the book’s parallel link libraries for real modeand protected mode programming.

Link Libraries We supply two versions of the link library that students use for basic input-output, simulations, timing, and other useful stuff. The 32-bit version (Irvine32.lib) runs in pro-tected mode, sending its output to the Win32 console. The 16-bit version (Irvine16.lib) runs inreal-address mode. Full source code for the libraries is supplied on the book’s Web site. Thelink libraries are available only for convenience, not to prevent students from learning how toprogram input-output themselves. Students are encouraged to create their own libraries.

86028 00 i-xxviii FM r0 vs.fm Page xx Friday, December 4, 2009 7:30 PM

Page 17: Preface xix Basic Concepts 1

Preface xxi

Included Software and Examples All the example programs were tested with MicrosoftMacro Assembler Version 10.0, running in Microsoft Visual Studio 2010. In addition, batch filesare supplied that permit students to assemble and run applications from the Windows commandprompt. The 32-bit C++ applications in Chapter 14 were tested with Microsoft Visual C++ .NET.

Web Site Information Updates and corrections to this book may be found at the book’s Website, www.asmirvine.com, including additional programming projects for instructors toassign at the ends of chapters. If for some reason you cannot access this site, information aboutthe book and a link to its current Web site can be found at www.pearsonhighered.com bysearching for the book’s title or author.

Overall Goals The following goals of this book are designed to broaden the student’s interest and knowledge intopics related to assembly language:

• Intel and AMD processor architecture and programming• Real-address mode and protected mode programming• Assembly language directives, macros, operators, and program structure• Programming methodology, showing how to use assembly language to create system-level

software tools and application programs• Computer hardware manipulation• Interaction between assembly language programs, the operating system, and other applica-

tion programs

One of our goals is to help students approach programming problems with a machine-levelmind set. It is important to think of the CPU as an interactive tool, and to learn to monitor itsoperation as directly as possible. A debugger is a programmer’s best friend, not only for catch-ing errors, but as an educational tool that teaches about the CPU and operating system. Weencourage students to look beneath the surface of high-level languages and to realize thatmost programming languages are designed to be portable and, therefore, independent of theirhost machines. In addition to the short examples, this book contains hundreds of ready-to-runprograms that demonstrate instructions or ideas as they are presented in the text. Referencematerials, such as guides to MS-DOS interrupts and instruction mnemonics, are available atthe end of the book.

Required Background The reader should already be able to program confidently in at leastone high-level programming language such as Python, Java, C, or C++. One chapter covers C++interfacing, so it is very helpful to have a compiler on hand. I have used this book in the class-room with majors in both computer science and management information systems, and it hasbeen used elsewhere in engineering courses.

Features Complete Program Listings The book’s Web site contains supplemental learning materials,study guides, and all the source code from the book’s examples. An extensive link library is

86028 00 i-xxviii FM r0 vs.fm Page xxi Friday, December 4, 2009 7:30 PM

Page 18: Preface xix Basic Concepts 1

xxii Preface

supplied with the book, containing more than 30 procedures that simplify user input-output,numeric processing, disk and file handling, and string handling. In the beginning stages of thecourse, students can use this library to enhance their programs. Later, they can create theirown procedures and add them to the library.

Programming Logic Two chapters emphasize Boolean logic and bit-level manipulation. Aconscious attempt is made to relate high-level programming logic to the low-level details of themachine. This approach helps students to create more efficient implementations and to betterunderstand how compilers generate object code.

Hardware and Operating System Concepts The first two chapters introduce basic hard-ware and data representation concepts, including binary numbers, CPU architecture, status flags,and memory mapping. A survey of the computer’s hardware and a historical perspective of theIntel processor family helps students to better understand their target computer system.

Structured Programming Approach Beginning with Chapter 5, procedures and functionaldecomposition are emphasized. Students are given more complex programming exercises,requiring them to focus on design before starting to write code.

Java Bytecodes and the Java Virtual Machine In Chapters 8 and 9, the author explains thebasic operation of Java bytecodes with short illustrative examples. Numerous short examples areshown in disassembled bytecode format, followed by detailed step-by-step explanations.

Disk Storage Concepts Students learn the fundamental principles behind the disk storagesystem on MS-Windows–based systems from hardware and software points of view.

Creating Link Libraries Students are free to add their own procedures to the book’s linklibrary and create new libraries. They learn to use a toolbox approach to programming and towrite code that is useful in more than one program.

Macros and Structures A chapter is devoted to creating structures, unions, and macros,which are essential in assembly language and systems programming. Conditional macros withadvanced operators serve to make the macros more professional.

Interfacing to High-Level Languages A chapter is devoted to interfacing assembly lan-guage to C and C++. This is an important job skill for students who are likely to find jobs pro-gramming in high-level languages. They can learn to optimize their code and see examples ofhow C++ compilers optimize code.

Instructional Aids All the program listings are available on disk and on the Web. Instructorsare provided a test bank, answers to review questions, solutions to programming exercises, and aMicrosoft PowerPoint slide presentation for each chapter.

Chapter DescriptionsChapters 1 to 8 contain core concepts of assembly language and should be covered in sequence.After that, you have a fair amount of freedom. The following chapter dependency graph showshow later chapters depend on knowledge gained from other chapters.

86028 00 i-xxviii FM r0 vs.fm Page xxii Friday, December 4, 2009 7:30 PM

Page 19: Preface xix Basic Concepts 1

Preface xxiii

1. Basic Concepts: Applications of assembly language, basic concepts, machine language, and datarepresentation.

2. x86 Processor Architecture: Basic microcomputer design, instruction execution cycle, x86processor architecture, x86 memory management, components of a microcomputer, and theinput-output system.

3. Assembly Language Fundamentals: Introduction to assembly language, linking anddebugging, and defining constants and variables.

4. Data Transfers, Addressing, and Arithmetic: Simple data transfer and arithmetic instruc-tions, assemble-link-execute cycle, operators, directives, expressions, JMP and LOOPinstructions, and indirect addressing.

5. Procedures: Linking to an external library, description of the book’s link library, stack oper-ations, defining and using procedures, owcharts, and top-down structured design.

6. Conditional Processing: Boolean and comparison instructions, conditional jumps andloops, high-level logic structures, and finite state machines.

7. Integer Arithmetic: Shift and rotate instructions with useful applications, multiplicationand division, extended addition and subtraction, and ASCII and packed decimal arithmetic.

8. Advanced Procedures: Stack parameters, local variables, advanced PROC and INVOKEdirectives, and recursion.

9. Strings and Arrays: String primitives, manipulating arrays of characters and integers, two-dimensional arrays, sorting, and searching.

10. Structures and Macros: Structures, macros, conditional assembly directives, and definingrepeat blocks.

11. MS-Windows Programming: Protected mode memory management concepts, using theMicrosoft Windows API to display text and colors, and dynamic memory allocation.

12. Floating-Point Processing and Instruction Encoding: Floating-point binary representa-tion and floating-point arithmetic. Learning to program the IA-32 floating-point unit. Under-standing the encoding of IA-32 machine instructions.

13. High-Level Language Interface: Parameter passing conventions, inline assembly code, andlinking assembly language modules to C and C++ programs.

14. 16-Bit MS-DOS Programming: Calling MS-DOS interrupts for console and file input-output.

• Appendix A: Microsoft Macro Assembler Reference• Appendix B: The x86 Instruction Set• Appendix C: Answers to Review Questions

1 through 9

10

11 12 13 14 16 17 18

15

86028 00 i-xxviii FM r0 vs.fm Page xxiii Friday, December 4, 2009 7:30 PM

Kip
Text Box
Kip
Text Box
Page 20: Preface xix Basic Concepts 1

xxiv Preface

The following chapters and appendices are supplied online at the Pearson Web site:

15. Disk Fundamentals: Disk storage systems, sectors, clusters, directories, file allocationtables, handling MS-DOS error codes, and drive and directory manipulation.

16. BIOS-Level Programming: Keyboard input, video text, graphics, and mouse programming.17. Expert MS-DOS Programming: Custom-designed segments, runtime program structure,

and Interrupt handling. Hardware control using I/O ports.

• Appendix D: BIOS and MS-DOS Interrupts

Reference Materials Web Site The author maintains an active Web site at www.asmirvine.com.

Help File Help file (in Windows Help Format) by Gerald Cahill of Antelope Valley College.Documents the book’s link libraries, as well as Win32 data structures.

Assembly Language Workbook An interactive workbook is included on the book’s Website, covering such important topics as number conversions, addressing modes, register usage,debug programming, and floating-point binary numbers. The content pages are HTML docu-ments, making it easy for students and instructors to add their own customized content. Thisworkbook is also available on the book’s Web site.

Debugging Tools Tutorials on using Microsoft CodeView, Microsoft Visual Studio, andMicrosoft Windows Debugger (WinDbg).

BIOS and MS-DOS Interrupts Appendix C contains a brief listing of the most-often-usedINT 10h (video), INT 16h (keyboard), and INT 21h (MS-DOS) functions.

Instruction Set Appendix B lists most nonprivileged instructions for the x86 processor family.For each instruction, we describe its effect, show its syntax, and show which flags are affected.

PowerPoint Presentations A complete set of Microsoft PowerPoint presentations written by theauthor.

AcknowledgmentsSpecial thanks are due to Tracy Dunkleberger, Executive Editor for Computer Science at Pren-tice Hall, who provided friendly, helpful guidance during the writing of the Fifth edition. xxxxdid a terrific job as production editor, constantly keeping track of numerous minute details. yyyywas the book’s managing editor. Melinda Haggerty was a great editorial assistant.

Sixth EditionSpecial thanks and gratitude are due to James Brink, formerly of Pacific Lutheran University,who tirelessly read the entire manuscript and offered excellent suggestions. Also, my thanks goto John Taylor, who has helped with the proofreading of both the Fifth and Sixth editions.In addition, I would like to thank the following reviewers:

86028 00 i-xxviii FM r0 vs.fm Page xxiv Friday, December 4, 2009 7:30 PM

Page 21: Preface xix Basic Concepts 1

Preface xxv

Fifth Edition I offer my special thanks and gratitude to the following professors who boosted my morale, gaveme great pedagogical tips, and tirelessly examined the entire book. They were a huge influenceon the development of the Fifth edition, in some cases across multiple editions:

• Gerald Cahill, Antelope Valley College• James Brink, Pacific Lutheran University• William Barrett, San Jose State University

Many thanks to Scott Blackledge and John Taylor, both professional programmers, who proofreadmost of the manuscript and flagged numerous errors. Several people reviewed individual chapters:

• Jerry Joyce, Keene State College• Tianzheng Wu, Mount Mercy College• Ron Davis, Kennedy-King College• David Topham, Ohlone College• Harvey Nice, DePaul University

86028 00 i-xxviii FM r0 vs.fm Page xxv Friday, December 4, 2009 7:30 PM