toc and preface

26
8/3/2019 Toc and Preface http://slidepdf.com/reader/full/toc-and-preface 1/26 Assembly Language for Intel ® -Based Computers Fifth Edition KIP R. IRVINE Florida International University School of Computing and Information Sciences Pearson Education, Inc. Upper Saddle River, NJ 07458

Upload: waqar-akram-abbasi

Post on 07-Apr-2018

219 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Toc and Preface

8/3/2019 Toc and Preface

http://slidepdf.com/reader/full/toc-and-preface 1/26

Assembly Language forIntel®

-Based Computers

Fifth Edition

KIP R. IRVINE

Florida International UniversitySchool of Computing and Information Sciences

Pearson Education, Inc.

Upper Saddle River, NJ 07458

Page 2: Toc and Preface

8/3/2019 Toc and Preface

http://slidepdf.com/reader/full/toc-and-preface 2/26

Library of Congress Cataloging-in-Publication Data

Irvine, Kip R.

Assembly language for intel-based computers/Kip R. Irvine.—5th ed.p. cm.Includes index.ISBN 0-13-238310-1

1. IBM Personal Computer—Programming. 2. Assembler language (Computer program language) I. Title.QA76.8.I77 2006005.265—dc22 2006012500

Vice President and Editorial Director, ECS: Marcia J. HortonExecutive Editor: Tracy DunkelbergerAssociate Editor: Carole SnyderEditorial Assistant: Christianna LeeExecutive Managing Editor: Vince O’BrienManaging Editor: Camille TrentacosteProduction Editor: Karen EttingerDirector of Creative Services: Paul BelfantiCreative Director: Juan LopezManaging Editor, AV Management and Production:  Patricia Burns

Art Editor: Gregory DullesManufacturing Manager, ESM: Alexis Heydt-LongManufacturing Buyer: Lisa McDowellExecutive Marketing Manager: Robin O’BrienMarketing Assistant: Mack Patterson

All rights reserved. No part of this book may be reproduced, in any form or by any means, without permission in writingfrom the publisher.

Pearson Prentice Hall® is a trademark of Pearson Education, Inc.

The author and publisher of this book have used their best efforts in preparing this book. These efforts include thedevelopment, research, and testing of the theories and programs to determine their effectiveness. The author and publisher make no warranty of any kind, expressed or implied, with regard to these programs or the documentation contained inthis book. The author and publisher shall not be liable in any event for incidental or consequential damages in connectionwith, or arising out of, the furnishing, performance, or use of these programs.

Printed in the United States of AmericaAll other trademarks or product names are the property of their respective owners.

TRADEMARK INFORMATION

IA-32, Pentium, and Intel 386 are trademarks of Intel Corporation.TASM and Turbo Debugger are trademarks of Borland International.Microsoft Assembler (MASM),Windows NT, Windows Me, Windows 95, Windows 98, Windows 2000, Windows XP,

MS-Windows, PowerPoint, Win32, DEBUG, WinDbg, MS-DOS, Visual Studio, Visual C++, and CodeView areregistered trademarks of Microsoft Corporation.

Autocad is a trademark of Autodesk.PartitionMagic is a trademark of Symantec.

10 9 8 7 6 5 4 3 2 1

ISBN 0-13-238310-1Pearson Education Ltd., LondonPearson Education Australia Pty. Ltd., SydneyPearson Education Singapore, Pte. Ltd.Pearson Education North Asia Ltd., Hong KongPearson Education Canada, Inc., TorontoPearson Educación de Mexico, S.A. de C.V.Pearson Education—Japan, TokyoPearson Education—Malaysia, Pte. Ltd.Pearson Education, Inc., Upper Saddle River, New Jersey

© 2007, 2003, 1999 Pearson Education, Inc.Pearson Prentice HallPearson Education, Inc.Upper Saddle River, New Jersey 07458

Page 3: Toc and Preface

8/3/2019 Toc and Preface

http://slidepdf.com/reader/full/toc-and-preface 3/26

To Jack and Candy Irvine

Page 4: Toc and Preface

8/3/2019 Toc and Preface

http://slidepdf.com/reader/full/toc-and-preface 4/26

Page 5: Toc and Preface

8/3/2019 Toc and Preface

http://slidepdf.com/reader/full/toc-and-preface 5/26

v

Contents

Preface xxiii

1 Basic Concepts 11.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 71.2.1 History of PC Assemblers 9

1.2.2 Section Review 9

1.3 Data Representation 91.3.1 Binary Numbers 10

1.3.2 Binary Addition 11

1.3.3 Integer Storage Sizes 12

1.3.4 Hexadecimal Integers 13

1.3.5 Signed Integers 14

1.3.6 Character Storage 16

1.3.7 Section Review 18

1.4 Boolean Operations 201.4.1 Truth Tables for Boolean Functions 22

1.4.2 Section Review 23

1.5 Chapter Summary 23

2 IA-32 Processor Architecture 252.1 General Concepts 25

2.1.1 Basic Microcomputer Design 26

2.1.2 Instruction Execution Cycle 27

2.1.3 Reading from Memory 30

2.1.4 How Programs Run 31

2.1.5 Section Review 32

2.2 IA-32 Processor Architecture 332.2.1 Modes of Operation 33

2.2.2 Basic Execution Environment 34

Page 6: Toc and Preface

8/3/2019 Toc and Preface

http://slidepdf.com/reader/full/toc-and-preface 6/26

vi Contents

2.2.3 Floating-Point Unit 36

2.2.4 Intel Microprocessor History 37

2.2.5 Section Review 38

2.3 IA-32 Memory Management 392.3.1 Real-Address Mode 39

2.3.2 Protected Mode 41

2.3.3 Section Review 43

2.4 Components of an IA-32 Microcomputer 432.4.1 Motherboard 43

2.4.2 Video Output 44

2.4.3 Memory 45

2.4.4 Input-Output Ports and Device Interfaces 45

2.4.5 Section Review 46

2.5 Input-Output System 462.5.1 How It All Works 46

2.5.2 Section Review 48

2.6 Chapter Summary 49

3 Assembly Language Fundamentals 513.1 Basic Elements of Assembly Language 51

3.1.1 Integer Constants 52

3.1.2 Integer Expressions 52

3.1.3 Real Number Constants 53

3.1.4 Character Constants 54

3.1.5 String Constants 54

3.1.6 Reserved Words 54

3.1.7 Identifiers 54

3.1.8 Directives 55

3.1.9 Instructions 55

3.1.10 The NOP (No Operation) Instruction 57

3.1.11 Section Review 58

3.2 Example: Adding Three Integers 583.2.1 Alternative Version of AddSub 60

3.2.2 Program Template 61

3.2.3 Section Review 61

3.3 Assembling, Linking, and Running Programs 623.3.1 The Assemble-Link-Execute Cycle 62

3.3.2 Section Review 64

3.4 Defining Data 643.4.1 Intrinsic Data Types 64

3.4.2 Data Definition Statement 64

3.4.3 Defining BYTE and SBYTE Data 66

Page 7: Toc and Preface

8/3/2019 Toc and Preface

http://slidepdf.com/reader/full/toc-and-preface 7/26

Contents vii

3.4.4 Defining WORD and SWORD Data 67

3.4.5 Defining DWORD and SDWORD Data 68

3.4.6 Defining QWORD Data 693.4.7 Defining TBYTE Data 69

3.4.8 Defining Real Number Data 69

3.4.9 Little Endian Order 69

3.4.10 Adding Variables to the AddSub Program 70

3.4.11 Declaring Uninitialized Data 71

3.4.12 Section Review 71

3.5 Symbolic Constants 723.5.1 Equal-Sign Directive 72

3.5.2 Calculating the Sizes of Arrays and Strings 73

3.5.3 EQU Directive 74

3.5.4 TEXTEQU Directive 74

3.5.5 Section Review 75

3.6 Real-Address Mode Programming (Optional) 753.6.1 Basic Changes 75

3.7 Chapter Summary 76

3.8 Programming Exercises 77

4 Data Transfers, Addressing,and Arithmetic 794.1 Data Transfer Instructions 79

4.1.1 Introduction 79

4.1.2 Operand Types 804.1.3 Direct Memory Operands 80

4.1.4 MOV Instruction 81

4.1.5 Zero/Sign Extension of Integers 82

4.1.6 LAHF and SAHF Instructions 84

4.1.7 XCHG Instruction 84

4.1.8 Direct-Offset Operands 84

4.1.9 Example Program (Moves) 85

4.1.10 Section Review 86

4.2 Addition and Subtraction 874.2.1 INC and DEC Instructions 87

4.2.2 ADD Instruction 87

4.2.3 SUB Instruction 88

4.2.4 NEG Instruction 884.2.5 Implementing Arithmetic Expressions 89

4.2.6 Flags Affected by Addition and Subtraction 89

4.2.7 Example Program (AddSub3) 92

4.2.8 Section Review 93

Page 8: Toc and Preface

8/3/2019 Toc and Preface

http://slidepdf.com/reader/full/toc-and-preface 8/26

viii Contents

4.3 Data-Related Operators and Directives 944.3.1 OFFSET Operator 94

4.3.2 ALIGN Directive 954.3.3 PTR Operator 95

4.3.4 TYPE Operator 96

4.3.5 LENGTHOF Operator 97

4.3.6 SIZEOF Operator 97

4.3.7 LABEL Directive 97

4.3.8 Section Review 98

4.4 Indirect Addressing 994.4.1 Indirect Operands 99

4.4.2 Arrays 100

4.4.3 Indexed Operands 101

4.4.4 Pointers 102

4.4.5 Section Review 103

4.5 JMP and LOOP Instructions 1044.5.1 JMP Instruction 104

4.5.2 LOOP Instruction 105

4.5.3 Summing an Integer Array 106

4.5.4 Copying a String 106

4.5.5 Section Review 107

4.6 Chapter Summary 108

4.7 Programming Exercises 109

5 Procedures 1115.1 Introduction 111

5.2 Linking to an External Library 1115.2.1 Background Information 112

5.2.2 Section Review 112

5.3 The Book’s Link Library 1135.3.1 Overview 113

5.3.2 Individual Procedure Descriptions 115

5.3.3 Library Test Programs 125

5.3.4 Section Review 129

5.4 Stack Operations 1295.4.1 Runtime Stack 129

5.4.2 PUSH and POP Instructions 131

5.4.3 Section Review 134

5.5 Defining and Using Procedures 1345.5.1 PROC Directive 134

5.5.2 CALL and RET Instructions 136

Page 9: Toc and Preface

8/3/2019 Toc and Preface

http://slidepdf.com/reader/full/toc-and-preface 9/26

Contents ix

5.5.3 Example: Summing an Integer Array 139

5.5.4 Flowcharts 140

5.5.5 Saving and Restoring Registers 1405.5.6 Section Review 142

5.6 Program Design Using Procedures 1435.6.1 Integer Summation Program (Design) 143

5.6.2 Integer Summation Implementation 145

5.6.3 Section Review 147

5.7 Chapter Summary 147

5.8 Programming Exercises 148

6 Conditional Processing 150

6.1 Introduction 1506.2 Boolean and Comparison Instructions 151

6.2.1 The CPU Flags 151

6.2.2 AND Instruction 152

6.2.3 OR Instruction 153

6.2.4 XOR Instruction 154

6.2.5 NOT Instruction 155

6.2.6 TEST Instruction 155

6.2.7 CMP Instruction 156

6.2.8 Setting and Clearing Individual CPU Flags 157

6.2.9 Section Review 157

6.3 Conditional Jumps 158

6.3.1 Conditional Structures 1586.3.2 Jcond Instruction 158

6.3.3 Types of Conditional Jump Instructions 159

6.3.4 Conditional Jump Applications 163

6.3.5 Bit Testing Instructions (Optional) 167

6.3.6 Section Review 168

6.4 Conditional Loop Instructions 1696.4.1 LOOPZ and LOOPE Instructions 169

6.4.2 LOOPNZ and LOOPNE Instructions 169

6.4.3 Section Review 170

6.5 Conditional Structures 1706.5.1 Block-Structured IF Statements 170

6.5.2 Compound Expressions 1736.5.3 WHILE Loops 174

6.5.4 Table-Driven Selection 177

6.5.5 Section Review 178

6.6 Application: Finite-State Machines 1796.6.1 Validating an Input String 180

Page 10: Toc and Preface

8/3/2019 Toc and Preface

http://slidepdf.com/reader/full/toc-and-preface 10/26

x Contents

6.6.2 Validating a Signed Integer 180

6.6.3 Section Review 183

6.7 Decision Directives 1846.7.1 Signed and Unsigned Comparisons 185

6.7.2 Compound Expressions 186

6.7.3 .REPEAT and .WHILE Directives 188

6.8 Chapter Summary 189

6.9 Programming Exercises 190

7 Integer Arithmetic 1937.1 Introduction 193

7.2 Shift and Rotate Instructions 1947.2.1 Logical Shifts and Arithmetic Shifts 194

7.2.2 SHL Instruction 195

7.2.3 SHR Instruction 196

7.2.4 SAL and SAR Instructions 196

7.2.5 ROL Instruction 197

7.2.6 ROR Instruction 198

7.2.7 RCL and RCR Instructions 198

7.2.8 Signed Overflow 199

7.2.9 SHLD/SHRD Instructions 199

7.2.10 Section Review 200

7.3 Shift and Rotate Applications 2017.3.1 Shifting Multiple Doublewords 201

7.3.2 Binary Multiplication 2027.3.3 Displaying Binary Bits 202

7.3.4 Isolating MS-DOS File Date Fields 203

7.3.5 Section Review 203

7.4 Multiplication and Division Operations 2047.4.1 MUL Instruction 204

7.4.2 IMUL Instruction 205

7.4.3 Benchmarking Multiplication Operations 207

7.4.4 DIV Instruction 208

7.4.5 Signed Integer Division 209

7.4.6 Implementing Arithmetic Expressions 211

7.4.7 Section Review 212

7.5 Extended Addition and Subtraction 2137.5.1 ADC Instruction 213

7.5.2 Extended Addition Example 213

7.5.3 SBB Instruction 214

7.5.4 Section Review 215

Page 11: Toc and Preface

8/3/2019 Toc and Preface

http://slidepdf.com/reader/full/toc-and-preface 11/26

Contents xi

7.6 ASCII and Unpacked Decimal Arithmetic 2157.6.1 AAA Instruction 216

7.6.2 AAS Instruction 2187.6.3 AAM Instruction 218

7.6.4 AAD Instruction 218

7.6.5 Section Review 219

7.7 Packed Decimal Arithmetic 2197.7.1 DAA Instruction 219

7.7.2 DAS Instruction 220

7.7.3 Section Review 220

7.8 Chapter Summary 221

7.9 Programming Exercises 222

8 Advanced Procedures 2248.1 Introduction 224

8.2 Stack Frames 2258.2.1 Stack Parameters 225

8.2.2 Local Variables 233

8.2.3 ENTER and LEAVE Instructions 236

8.2.4 LOCAL Directive 237

8.2.5 WriteStackFrame Procedure 240

8.2.6 Section Review 241

8.3 Recursion 2428.3.1 Recursively Calculating a Sum 243

8.3.2 Calculating a Factorial 2438.3.3 Section Review 245

8.4 .MODEL Directive 2468.4.1 Language Specifiers 247

8.4.2 Section Review 248

8.5 INVOKE, ADDR, PROC, and PROTO (Optional) 2488.5.1 INVOKE Directive 248

8.5.2 ADDR Operator 249

8.5.3 PROC Directive 250

8.5.4 PROTO Directive 253

8.5.5 Parameter Classifications 255

8.5.6 Example: Exchanging Two Integers 256

8.5.7 Debugging Tips 2568.5.8 Section Review 257

8.6 Creating Multimodule Programs 2588.6.1 Hiding and Exporting Procedure Names 258

8.6.2 Calling External Procedures 258

Page 12: Toc and Preface

8/3/2019 Toc and Preface

http://slidepdf.com/reader/full/toc-and-preface 12/26

xii Contents

8.6.3 Using Variables and Symbols Across Module Boundaries 259

8.6.4 Example: ArraySum Program 260

8.6.5 Creating the Modules Using Extern 2618.6.6 Creating the Modules Using INVOKE and PROTO 264

8.6.7 Section Review 266

8.7 Chapter Summary 267

8.8 Programming Exercises 268

9 Strings and Arrays 2699.1 Introduction 269

9.2 String Primitive Instructions 2709.2.1 MOVSB, MOVSW, and MOVSD 271

9.2.2 CMPSB, CMPSW, and CMPSD 2729.2.3 SCASB, SCASW, and SCASD 274

9.2.4 STOSB, STOSW, and STOSD 274

9.2.5 LODSB, LODSW, and LODSD 275

9.2.6 Section Review 275

9.3 Selected String Procedures 2769.3.1 Str_compare Procedure 276

9.3.2 Str_length Procedure 277

9.3.3 Str_copy Procedure 278

9.3.4 Str_trim Procedure 278

9.3.5 Str_ucase Procedure 279

9.3.6 String Library Demo Program 280

9.3.7 Section Review 282

9.4 Two-Dimensional Arrays 2829.4.1 Ordering of Rows and Columns 282

9.4.2 Base-Index Operands 283

9.4.3 Base-Index-Displacement Operands 285

9.4.4 Section Review 285

9.5 Searching and Sorting Integer Arrays 2859.5.1 Bubble Sort 286

9.5.2 Binary Search 287

9.5.3 Section Review 293

9.6 Chapter Summary 294

9.7 Programming Exercises 295

10 Structures and Macros 29910.1 Structures 299

10.1.1 Defining Structures 300

Page 13: Toc and Preface

8/3/2019 Toc and Preface

http://slidepdf.com/reader/full/toc-and-preface 13/26

Contents xiii

10.1.2 Declaring Structure Variables 301

10.1.3 Referencing Structure Variables 302

10.1.4 Example: Displaying the System Time 30510.1.5 Structures Containing Structures 307

10.1.6 Example: Drunkard’s Walk 307

10.1.7 Declaring and Using Unions 310

10.1.8 Section Review 312

10.2 Macros 31310.2.1 Overview 313

10.2.2 Defining Macros 313

10.2.3 Invoking Macros 314

10.2.4 Additional Macro Features 315

10.2.5 Using the Book’s Macro Library 318

10.2.6 Example Program: Wrappers 324

10.2.7 Section Review 325

10.3 Conditional-Assembly Directives 32610.3.1 Checking for Missing Arguments 326

10.3.2 Default Argument Initializers 328

10.3.3 Boolean Expressions 328

10.3.4 IF, ELSE, and ENDIF Directives 328

10.3.5 The IFIDN and IFIDNI Directives 329

10.3.6 Example: Summing a Matrix Row 330

10.3.7 Special Operators 333

10.3.8 Macro Functions 336

10.3.9 Section Review 337

10.4 Defining Repeat Blocks 33810.4.1 WHILE Directive 338

10.4.2 REPEAT Directive 338

10.4.3 FOR Directive 339

10.4.4 FORC Directive 340

10.4.5 Example: Linked List 340

10.4.6 Section Review 342

10.5 Chapter Summary 342

10.6 Programming Exercises 343

11 MS-Windows Programming 34611.1 Win32 Console Programming 346

11.1.1 Background Information 34711.1.2 Win32 Console Functions 350

11.1.3 Displaying a Message Box 352

11.1.4 Console Input 354

11.1.5 Console Output 360

11.1.6 Reading and Writing Files 361

Page 14: Toc and Preface

8/3/2019 Toc and Preface

http://slidepdf.com/reader/full/toc-and-preface 14/26

xiv Contents

11.1.7 File I/O in the Irvine32 Library 365

11.1.8 Testing the File I/O Procedures 367

11.1.9 Console Window Manipulation 37011.1.10 Controlling the Cursor 373

11.1.11 Controlling the Text Color 373

11.1.12 Time and Date Functions 375

11.1.13 Section Review 379

11.2 Writing a Graphical Windows Application 37911.2.1 Necessary Structures 380

11.2.2 The MessageBox Function 381

11.2.3 The WinMain Procedure 382

11.2.4 The WinProc Procedure 382

11.2.5 The ErrorHandler Procedure 383

11.2.6 Program Listing 383

11.2.7 Section Review 386

11.3 Dynamic Memory Allocation 38711.3.1 HeapTest Programs 390

11.3.2 Section Review 393

11.4 IA-32 Memory Management 39311.4.1 Linear Addresses 394

11.4.2 Page Translation 397

11.4.3 Section Review 398

11.5 Chapter Summary 399

11.6 Programming Exercises 400

12 High-Level Language Interface 40212.1 Introduction 402

12.1.1 General Conventions 402

12.1.2 Section Review 403

12.2 Inline Assembly Code 40412.2.1 __asm Directive in Microsoft Visual C++ 404

12.2.2 File Encryption Example 406

12.2.3 Section Review 409

12.3 Linking to C/C++ in Protected Mode 40912.3.1 Using Assembly Language to Optimize C++ Code 410

12.3.2 Calling C and C++ Functions 415

12.3.3 Multiplication Table Example 41612.3.4 Calling C Library Functions 419

12.3.5 Directory Listing Program 422

12.3.6 Section Review 423

12.4 Linking to C/C++ in Real-Address Mode 42312.4.1 Linking to Borland C++ 424

Page 15: Toc and Preface

8/3/2019 Toc and Preface

http://slidepdf.com/reader/full/toc-and-preface 15/26

Contents xv

12.4.2 ReadSector Example 425

12.4.3 Example: Large Random Integers 428

12.4.4 Section Review 430

12.5 Chapter Summary 430

12.6 Programming Exercises 431

13 16-Bit MS-DOS Programming 43213.1 MS-DOS and the IBM-PC 432

13.1.1 Memory Organization 433

13.1.2 Redirecting Input-Output 434

13.1.3 Software Interrupts 435

13.1.4 INT Instruction 435

13.1.5 Coding for 16-Bit Programs 436

13.1.6 Section Review 437

13.2 MS-DOS Function Calls (INT 21h) 43813.2.1 Selected Output Functions 439

13.2.2 Hello World Program Example 441

13.2.3 Selected Input Functions 442

13.2.4 Date/Time Functions 446

13.2.5 Section Review 449

13.3 Standard MS-DOS File I/O Services 44913.3.1 Create or Open File (716Ch) 451

13.3.2 Close File Handle (3Eh) 452

13.3.3 Move File Pointer (42h) 452

13.3.4 Get File Creation Date and Time 453

13.3.5 Selected Library Procedures 453

13.3.6 Example: Read and Copy a Text File 454

13.3.7 Reading the MS-DOS Command Tail 456

13.3.8 Example: Creating a Binary File 458

13.3.9 Section Review 461

13.4 Chapter Summary 461

13.5 Chapter Exercises 463

14 Disk Fundamentals 46414.1 Disk Storage Systems 464

14.1.1 Tracks, Cylinders, and Sectors 46514.1.2 Disk Partitions (Volumes) 466

14.1.3 Section Review 468

14.2 File Systems 46814.2.1 FAT12 469

Page 16: Toc and Preface

8/3/2019 Toc and Preface

http://slidepdf.com/reader/full/toc-and-preface 16/26

xvi Contents

14.2.2 FAT16 469

14.2.3 FAT32 469

14.2.4 NTFS 47014.2.5 Primary Disk Areas 470

14.2.6 Section Review 471

14.3 Disk Directory 47214.3.1 MS-DOS Directory Structure 473

14.3.2 Long Filenames in MS-Windows 475

14.3.3 File Allocation Table (FAT) 476

14.3.4 Section Review 477

14.4 Reading and Writing Disk Sectors (7305h) 47714.4.1 Sector Display Program 478

14.4.2 Section Review 482

14.5 System-Level File Functions 48214.5.1 Get Disk Free Space (7303h) 483

14.5.2 Create Subdirectory (39h) 485

14.5.3 Remove Subdirectory (3Ah) 486

14.5.4 Set Current Directory (3Bh) 486

14.5.5 Get Current Directory (47h) 486

14.5.6 Get and Set File Attributes (7143h) 486

14.5.7 Section Review 487

14.6 Chapter Summary 487

14.7 Programming Exercises 488

15 BIOS-Level Programming 49015.1 Introduction 49015.1.1 BIOS Data Area 491

15.2 Keyboard Input with INT 16h 49215.2.1 How the Keyboard Works 492

15.2.2 INT 16h Functions 493

15.2.3 Section Review 497

15.3 VIDEO Programming with INT 10h 49815.3.1 Basic Background 498

15.3.2 Controlling the Color 499

15.3.3 INT 10h Video Functions 501

15.3.4 Library Procedure Examples 511

15.3.5 Section Review 512

15.4 Drawing Graphics Using INT 10h 51215.4.1 INT 10h Pixel-Related Functions 513

15.4.2 DrawLine Program 514

Page 17: Toc and Preface

8/3/2019 Toc and Preface

http://slidepdf.com/reader/full/toc-and-preface 17/26

Contents xvii

15.4.3 Cartesian Coordinates Program 515

15.4.4 Converting Cartesian Coordinates to Screen Coordinates 517

15.4.5 Section Review 518

15.5 Memory-Mapped Graphics 51915.5.1 Mode 13h: 320 200, 256 Colors 519

15.5.2 Memory-Mapped Graphics Program 520

15.5.3 Section Review 523

15.6 Mouse Programming 52315.6.1 Mouse INT 33h Functions 523

15.6.2 Mouse Tracking Program 528

15.6.3 Section Review 532

15.7 Chapter Summary 533

15.8 Chapter Exercises 534

16 Expert MS-DOS Programming 53616.1 Introduction 536

16.2 Defining Segments 53716.2.1 Simplified Segment Directives 537

16.2.2 Explicit Segment Definitions 538

16.2.3 Segment Overrides 542

16.2.4 Combining Segments 542

16.2.5 Section Review 543

16.3 Runtime Program Structure 544

16.3.1 Program Segment Prefix 54416.3.2 COM Programs 545

16.3.3 EXE Programs 546

16.3.4 Section Review 547

16.4 Interrupt Handling 54816.4.1 Hardware Interrupts 549

16.4.2 Interrupt Control Instructions 550

16.4.3 Writing a Custom Interrupt Handler 551

16.4.4 Terminate and Stay Resident Programs 553

16.4.5 Application: The No_Reset Program 554

16.4.6 Section Review 557

16.5 Hardware Control Using I/O Ports 558

16.5.1 Input-Output Ports 55816.5.2 PC Sound Program 558

16.6 Chapter Summary 560

Page 18: Toc and Preface

8/3/2019 Toc and Preface

http://slidepdf.com/reader/full/toc-and-preface 18/26

xviii Contents

17 Floating-Point Processing and Instruction

Encoding 56217.1 Floating-Point Binary Representation 562

17.1.1 IEEE Binary Floating-Point Representation 563

17.1.2 The Exponent 564

17.1.3 Normalized Binary Floating-Point Numbers 565

17.1.4 Creating the IEEE Representation 565

17.1.5 Converting Decimal Fractions to Binary Reals 567

17.1.6 Section Review 568

17.2 Floating-Point Unit 56917.2.1 FPU Register Stack 569

17.2.2 Rounding 571

17.2.3 Floating-Point Exceptions 573

17.2.4 Floating-Point Instruction Set 57317.2.5 Arithmetic Instructions 576

17.2.6 Comparing Floating-Point Values 579

17.2.7 Reading and Writing Floating-Point Values 582

17.2.8 Exception Synchronization 583

17.2.9 Code Examples 584

17.2.10 Mixed-Mode Arithmetic 585

17.2.11 Masking and Unmasking Exceptions 586

17.2.12 Section Review 587

17.3 Intel Instruction Encoding 58817.3.1 IA-32 Instruction Format 588

17.3.2 Single-Byte Instructions 589

17.3.3 Move Immediate to Register 590

17.3.4 Register-Mode Instructions 59117.3.5 IA-32 Processor Operand-Size Prefix 591

17.3.6 Memory-Mode Instructions 592

17.3.7 Section Review 595

17.4 Chapter Summary 596

17.5 Programming Exercises 597

Appendix A MASM Reference 600

Appendix B The IA-32 Instruction Set 619

Appendix C BIOS and MS-DOS Interrupts 650

Appendix D Answers to Review Questions 659

Index 705

Page 19: Toc and Preface

8/3/2019 Toc and Preface

http://slidepdf.com/reader/full/toc-and-preface 19/26

xix

Preface

 Assembly Language for Intel-Based Computers, Fifth Edition, teaches assembly language program-

ming and architecture for Intel IA-32 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)

8.0, running on any of the following MS-Windows platforms: Windows 95, 98, Millenium, NT, 2000,

and XP.

Although this book was originally designed as a programming textbook for college students, it has

evolved over the last 15 years into much more. Many universities use the book for their introductory

computer architecture courses. As a testament to its popularity, the fourth edition was translated into

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

• Elementary boolean operations

• Instruction execution cycle

• Memory access and handshaking

• Interrupts and polling

• Pipelining and superscalar concepts• Hardware-based I/O

• Floating-point binary representation

Other topics relate specifically to Intel IA-32 architecture:

• IA-32 protected memory and paging

• Memory segmentation in real-address mode

• 16-bit interrupt handling

• MS-DOS and BIOS system calls (interrupts)

• IA-32 Floating-Point Unit architecture and programming

• IA-32 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• Finite-state machines

• Code optimization examples

Page 20: Toc and Preface

8/3/2019 Toc and Preface

http://slidepdf.com/reader/full/toc-and-preface 20/26

xx Preface

Improvements in the Fifth Edition  A number of improvements and new information have been

added in this edition, listed in the following table by chapter number:

Still a Programming Book  This book is still focused on its original mission: to teach students how

to write and debug programs at the machine level. It will never replace a complete book on computer 

architecture, but it does give students the first-hand experience of writing software in an environment

that teaches them how a computer works. Our premise is that students retain knowledge better when

theory is combined with experience. In an engineering course, students construct prototypes; in acomputer architecture course, students should write machine-level programs. In both cases, they have a

memorable experience that gives them the confidence to work in any OS/machine-oriented environment.

Real Mode and Protected Mode  This edition emphasizes 32-bit protected mode, but it still has

three chapters devoted to real-mode programming. For example, there is an entire chapter on BIOS

programming for the keyboard, video display (including graphics), and mouse. Another chapter cov-

ers MS-DOS programming using interrupts (system calls). Students can benefit from programming

directly to hardware and the BIOS.

The examples in the first half of the book are nearly all presented as 32-bit text-oriented applications

running in protected mode using the flat memory model. This approach is wonderfully simple because it

avoids the complications of segment-offset addressing. Specially marked paragraphs and popup boxes

point out occasional differences between protected mode and real mode programming. Most differences

are abstracted by the book’s parallel link libraries for real mode and 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 protected mode,

sending its output to the Win32 console. The 16-bit version ( Irvine16.lib) runs in real-address mode.

Full source code for the libraries is supplied on the book’s Web site. The link libraries are available

only for convenience, not to prevent students from learning how to program input-output themselves.

Students are encouraged to create their own libraries.

Chapter Improvements

2 Improved explanation of the instruction execution cycle.

5 An expanded link library with additional subroutines to write rich user interfaces, calculate program

timings, generate pseudorandom integers, and parse integer strings. The documentation of the library

has greatly improved.

6 Improved explanation of conditional jump encoding and relative jump ranges.

7 Two-operand and three-operand IMUL instructions are added. Performance comparisons are shown

for differing approaches to integer multiplication.

8 Completely redesigned so that low-level details of stack frames (activation records) are explained first

before introducing MASM’s high-level INVOKE and PROC directives.

10 Improved documentation of the book’s macro library.

11 New topic: Dynamic memory allocation in MS-Windows applications. Improved coverage of file

handling and error reporting in MS-Windows applications.

12 Improved coverage of calling C and C++ functions from assembly language.

17 Introduction to the IA-32 floating-point instruction set. Floating-point data types. IA-32 Instruction

encoding and decoding.

Page 21: Toc and Preface

8/3/2019 Toc and Preface

http://slidepdf.com/reader/full/toc-and-preface 21/26

Preface xxi

Included Software and Examples  All the example programs were tested with Microsoft Macro

Assembler Version 8.0. The 32-bit C++ applications in Chapter 12 were tested with Microsoft Visual

C++ .NET. The real-address mode programs in Chapter 12 (linking to C++) were assembled withBorland Turbo Assembler (TASM).

Web Site Information  Updates and corrections to this book may be found at the book’s Web site,

http://www.asmirvine.com, including additional programming projects for instructors to assign

at the ends of chapters. If for some reason you cannot access this site, information about the book and

a link to its current Web site can be found at www.prenhall.comby searching for the book title or for 

the author name “Kip Irvine.”

Overall Goals

The following goals of this book are designed to broaden the student’s interest and knowledge in top-

ics related to assembly language:

• Intel IA-32 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 soft-

ware tools and application programs

• Computer hardware manipulation

• Interaction between assembly language programs, the operating system, and other application programs

One of our goals is to help students approach programming problems with a machine-level mind

set. It is important to think of the CPU as an interactive tool, and to learn to monitor its operation as

directly as possible. A debugger is a programmer’s best friend, not only for catching errors, but as an

educational tool that teaches about the CPU and operating system. We encourage students to look

beneath the surface of high-level languages and to realize that most programming languages are

designed to be portable and, therefore, independent of their host machines.

In addition to the short examples, this book contains hundreds of ready-to-run programs that dem-

onstrate instructions or ideas as they are presented in the text. Reference materials, such as guides toMS-DOS interrupts and instruction mnemonics, are available at the end of the book.

Required Background  The reader should already be able to program confidently in at least one

other programming language, preferably 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 classroom with majors in both

computer science and management information systems, and it has been used elsewhere in engineer-

ing courses.

Features

Complete Program Listings  A companion CD-ROM contains all the source code from the exam-

ples in this book. Additional listings are available on the book’s Web page. An extensive link library is

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 the course, students

can use this library to enhance their programs. Later, they can create their own procedures and add

them to the library.

Programming Logic  Two chapters emphasize boolean logic and bit-level manipulation. A con-

scious attempt is made to relate high-level programming logic to the low-level details of the machine.

This approach helps students to create more efficient implementations and to better understand how

compilers generate object code.

Page 22: Toc and Preface

8/3/2019 Toc and Preface

http://slidepdf.com/reader/full/toc-and-preface 22/26

xxii Preface

Hardware and Operating System Concepts  The first two chapters introduce basic hardware

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 the Intel processor familyhelps students to better understand their target computer system.

Structured Programming Approach  Beginning with Chapter 5, procedures and functional decom-

position are emphasized. Students are given more complex programming exercises, requiring them to

focus on design before starting to write code.

Disk Storage Concepts  Students learn the fundamental principles behind the disk storage system

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 link library

and create new libraries. They learn to use a toolbox approach to programming and to write code that

is useful in more than one program.

Macros and Structures  A chapter is devoted to creating structures, unions, and macros, which are

esential in assembly language and systems programming. Conditional macros with advanced opera-

tors serve to make the macros more professional.

Interfacing to High-Level Languages  A chapter is devoted to interfacing assembly language to C

and C++. This is an important job skill for students who are likely to find jobs programming in high-level

languages. They can learn to optimize their code and see examples of how C++ compilers optimize code.

Instructional Aids  All the program listings are available on disk and on the Web. Instructors are pro-

vided a test bank, answers to review questions, solutions to programming exercises, and a Microsoft

PowerPoint slide presentation for each chapter.

Summary of Chapters

Chapters 1 to 8 contain a basic foundation of assembly language and should be covered in sequence.

After that, you have a fair amount of freedom. The following chapter dependency graph shows how

later chapters depend on knowledge gained from other chapters. Chapter 10 was split into two parts

for this graph because no other chapter depends on one’s knowing how to create macros:

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

2. IA-32 Processor Architecture: Basic microcomputer design, instruction execution cycle, IA-32

processor architecture, IA-32 memory management, components of a microcomputer, and the

input-output system.

3. Assembly Language Fundamentals: Introduction to assembly language, linking and debugging,

and defining constants and variables.

111 through 8 9

13 14

15

16

17

10(structs)

1210

(macros)

Page 23: Toc and Preface

8/3/2019 Toc and Preface

http://slidepdf.com/reader/full/toc-and-preface 23/26

Preface xxiii

4. Data Transfers, Addressing, and Arithmetic: Simple data transfer and arithmetic instructions,

assemble-link-execute cycle, operators, directives, expressions, JMP and LOOP instructions, and

indirect addressing.5. Procedures: Linking to an external library, description of the book’s link library, stack opera-

tions, defining and using procedures, flowcharts, and top-down structured design.

6. Conditional Processing: Boolean and comparison instructions, conditional jumps and loops,

high-level logic structures, and finite state machines.

7. Integer Arithmetic: Shift and rotate instructions with useful applications, multiplication and

division, extended addition and subtraction, and ASCII and packed decimal arithmetic.

8. Advanced Procedures: Stack parameters, local variables, advanced PROC and INVOKE direc-

tives, 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 defining repeat blocks.

11. MS-Windows Programming: Protected mode memory management concepts, using the Microsoft

Windows API to display text and colors, and dynamic memory allocation.12. High-Level Language Interface: Parameter passing conventions, inline assembly code, and linking

assembly language modules to C and C++ programs.

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

14. Disk Fundamentals: Disk storage systems, sectors, clusters, directories, file allocation tables, handling

MS-DOS error codes, and drive and directory manipulation.

15. BIOS-Level Programming: Keyboard input, video text, graphics, and mouse programming.

16. Expert MS-DOS Programming: Custom-designed segments, runtime program structure, and

Interrupt handling. Hardware control using I/O ports.

17. Floating-Point Processing and Instruction Encoding: Floating-point binary representation and

floating-point arithmetic. Learning to program the IA-32 Floating-Point Unit. Understanding the

encoding of IA-32 machine instructions.

Appendix A: MASM Reference

Appendix B: The IA-32 Instruction Set

Appendix C: BIOS and MS-DOS Interrupts

Appendix D: Answers to Review Questions

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. Docu-

ments the book’s link libraries, as well as Win32 data structures.

 Assembly Language Workbook  An interactive workbook is included on the book’s Web site, cover-

ing such important topics as number conversions, addressing modes, register usage, Debug program-

ming, and floating-point binary numbers. The content pages are HTML documents, making it easy

for students and instructors to add their own customized content. This workbook is also available on

the book’s Web site.

 Debugging Tools Tutorials on using Microsoft CodeView, Microsoft Visual Studio, and Microsoft

Windows Debugger (WinDbg).

 BIOS and MS-DOS Interrupts Appendix C contains a brief listing of the most-often-used INT 10h

(video), INT 16h (keyboard), and INT 21h (MS-DOS) functions.

 Instruction Set  Appendix B lists most nonprivileged instructions for the IA-32 processor family.

Page 24: Toc and Preface

8/3/2019 Toc and Preface

http://slidepdf.com/reader/full/toc-and-preface 24/26

xxiv Preface

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 the author.

Acknowledgments

Special thanks are due to Tracy Dunkleberger, Executive Editor for Computer Science at Prentice

Hall, who provided friendly, helpful guidance during the writing of the fifth edition. Karen Ettinger 

did a terrific job as production editor, constantly keeping track of numerous minute details. Camille

Trentacoste was the book’s managing editor.

Fifth Edition 

I offer my special thanks and gratitude to the following professors who boosted my morale, gave me

great pedagogical tips, and tirelessly examined the entire book. They have been a huge influence on

the development of this book, in some cases across multiple editions:

• Gerald Cahill, Antelope Valley College

• James Brink, Pacific Lutheran University

• William Barrett, San Jose State UniversityMany thanks to Scott Blackledge and John Taylor, both professional programmers, who proofread

most 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

Fourth Edition 

The following people were tremendously helpful in creating the fourth edition:

• Gerald Cahill, Antelope Valley College

• James Brink, Pacific Lutheran University

• Maria Kolatis, County College of Morris

• Tom Joyce, Chief Engineer at Premier Heart, LLC

• Jeff Wothke, Purdue Calumet University

• Tim Downey, Florida International University

The following individuals provided valuable proofreading help in the fourth edition:

• Andres Altamirano, Miami

• Courtney Amor, Los Angeles

• Scott Blackledge, Platform Solutions, Inc.

• Ronald Davis, Kennedy-King College

• Ata Elahi, Southern Connecticut State University

• Jose Gonzalez, Miami

• Leroy Highsmith, Southern Connecticut State University

• Sajid Iqbal, Faran Institute of Technology

• Charles Jones, Maryville College

• Vincent Kayes, Mount St. Mary College

• Eric Kobrin, Miami

• Pablo Maurin, Miami

• Barry Meaker, Design Engineer, Boeing Corporation

• Ian Merkel, Miami

• Sylvia Miner, Miami

Page 25: Toc and Preface

8/3/2019 Toc and Preface

http://slidepdf.com/reader/full/toc-and-preface 25/26

Preface xxv

• M. Nawaz, OPSTEC College of Computer Science

• Kam Ng, Chinese University of Hong Kong

•Hien Nguyen, Miami

• Ernie Philipp, Northern Virginia Community College

• Boyd Stephens, UGMO Research, LLC

• John Taylor, England

• Zachary Taylor, Columbia College

• Virginia Welsh, Community College of Baltimore County

• Robert Workman, Southern Connecticut State University

• Tianzheng Wu, Mount Mercy College

• Matthew Zukoski, Lehigh University

Page 26: Toc and Preface

8/3/2019 Toc and Preface

http://slidepdf.com/reader/full/toc-and-preface 26/26