asseivibly lanquaqe for

14
AssEivibly LANquAqE foR x86 PROCESSORS Sixth Edition KIP R. IRVINE Florida International University School of Computing and Information Sciences PEARSON Boston Columbus Indianapolis New York San Francisco Upper Saddle River Amsterdam Cape Town Dubai London Madrid Milan Munich Paris Montreal Toronto Delhi Mexico City Sao Paulo Sydney Hong Kong Seoul Singapore Taipei Tokyo

Upload: others

Post on 11-Jun-2022

0 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: AssEivibly LANquAqE foR

AssEivibly LANquAqE foR

x86 PROCESSORS

Sixth Edition

KIP R. IRVINE Florida International University School of Computing and Information Sciences

PEARSON

Boston Columbus Indianapolis New York San Francisco Upper Saddle River Amsterdam Cape Town Dubai London Madrid Milan Munich Paris Montreal Toronto

Delhi Mexico City Sao Paulo Sydney Hong Kong Seoul Singapore Taipei Tokyo

Page 2: AssEivibly LANquAqE foR

CONTENTS • •

Preface 19

1 Basic Concepts 29 1.1 Welcome to Assembly Language 29

1.1.1 Good Questions to Ask 30 1.1.2 Assembly Language Applications 33 1.1.3 Section Review 34

1.2 Virtual Machine Concept 35 1.2.1 Section Review 37

1.3 Data Representation 37 1.3.1 Binary Integers 37 1.3.2 Binary Addition 39 1.3.3 Integer Storage Sizes 40 1.3.4 Hexadecimal Integers 41 1.3.5 Signed Integers 43 1.3.6 Character Storage 45 1.3.7 Section Review 47

1.4 Boolean Operations 50 1.4.1 Truth Tables for Boolean Functions 52 1.4.2 Section Review 54

1.5 Chapter Summary 54

1.6 Exercises 55 1.6.1 Programming Tasks 55 1.6.2 Nonprogramming Tasks 55

2 x86 Processor Architecture 57 2.1 General Concepts 57

2.1.1 Basic Microcomputer Design 58 2.1.2 Instruction Execution Cycle 59 2.1.3 Reading from Memory 61 2.1.4 How Programs Run 62 2.1.5 Section Review 63

5

Page 3: AssEivibly LANquAqE foR

2.2 x86 Architecture Details 64 2.2.1 Modes of Operation 64 2.2.2 Basic Execution Environment 64 2.2.3 Floating-Point Unit 67 2.2.4 Overview of Intel Microprocessors 67 2.2.5 Section Review 70

2.3 x86 Memory Management 71 2.3.1 Real-Address Mode 71 2.3.2 Protected Mode 73 2.3.3 Section Review 75

2.4 Components of a Typical x86 Computer 76 2.4.1 Motherboard 76 2.4.2 Video Output 78 2.4.3 Memory 78 2.4.4 Input-Output Ports and Device Interfaces 78 2.4.5 Section Review 80

2.5 Input-Output System 80 2.5.1 Levels of I/O Access 80 2.5.2 Section Review 83

2.6 Chapter Summary 83

2.7 Chapter Exercises 85

3 Assembly Language Fundamentals 3.1 Basic Elements of Assembly Language 86

3.1.1 Integer Constants 87 3.1.2 Integer Expressions 88 3.1.3 Real Number Constants 89 3.1.4 Character Constants 89 3.1.5 String Constants 89 3.1.6 Reserved Words 90 3.1.7 Identifiers 90 3.1.8 Directives 90 3.1.9 Instructions 91 3.1.10 The NOP (No Operation) Instruction 93 3.1.11 Section Review 94

3.2 Example: Adding and Subtracting Integers 94 3.2.1 Alternative Version of AddSub 97 3.2.2 Program Template 98 3.2.3 Section Review 98

3.3 Assembling, Linking, and Running Programs 99 3.3.1 The Assemble-Link-Execute Cycle 99 3.3.2 Section Review 105

Page 4: AssEivibly LANquAqE foR

CONTENTS 7

• •

3.4 Defining Data 105 3.4.1 Intrinsic Data Types 105 3.4.2 Data Definition Statement 105 3.4.3 Defining BYTE and SBYTE Data 106 3.4.4 Defining WORD and SWORD Data 108 3.4.5 Defining DWORD and SDWORD Data 109 3.4.6 Defining QWORD Data 109 3.4.7 Defining Packed Binary Coded Decimal (TBYTE) Data 110 3.4.8 Defining Real Number Data 111 3.4.9 Little Endian Order 111 3.4.10 Adding Variables to the AddSub Program 112 3.4.11 Declaring Uninitialized Data 113 3.4.12 Section Review 113

3.5 Symbolic Constants 114 3.5.1 Equal-Sign Directive 114 3.5.2 Calculating the Sizes of Arrays and Strings 115 3.5.3 EQU Directive 116 3.5.4 TEXTEQU Directive 117 3.5.5 Section Review 118

3.6 Real-Address Mode Programming (Optional) 118

3.6.1 Basic Changes 118

3.7 Chapter Summary 119

3.8 Programming Exercises 120

4 Data Transfers, Addressing, and Arithmetic 122

4.1 Data Transfer Instructions 122 4.1.1 Introduction 122 4.1.2 Operand Types 123 4.1.3 Direct Memory Operands 124 4.1.4 MOV Instruction 124 4.1.5 Zero/Sign Extension of Integers 126 4.1.6 LAHF and SAHF Instructions 128 4.1.7 XCHG Instruction 128 4.1.8 Direct-Offset Operands 129 4.1.9 Example Program (Moves) 130 4.1.10 Section Review 131

4.2 Addition and Subtraction 132 4.2.1 INC and DEC Instructions 132 4.2.2 ADD Instruction 132 4.2.3 SUB Instruction 133 4.2.4 NEG Instruction 133

Page 5: AssEivibly LANquAqE foR

4.2.5 Implementing Arithmetic Expressions 134 4.2.6 Flags Affected by Addition and Subtraction 4.2.7 Example Program (AddSub3) 138 4.2.8 Section Review 139

4.3 Data-Related Operators and Directives 4.3.1 OFFSET Operator 140 4.3.2 ALIGN Directive 141 4.3.3 PTR Operator 142 4.3.4 TYPE Operator 143 4.3.5 LENGTHOF Operator 143 4.3.6 SIZEOF Operator 144 4.3.7 LABEL Directive 144 4.3.8 Section Review 145

4.4 Indirect Addressing 145 4.4.1 Indirect Operands 146 4.4.2 Arrays 147 4.4.3 Indexed Operands 148 4.4.4 Pointers 149 4.4.5 Section Review 151

4.5 JMP and LOOP Instructions 152 4.5.1 JMP Instruction 152 4.5.2 LOOP Instruction 152 4.5.3 Summing an Integer Array 154 4.5.4 Copying a String 154 4.5.5 Section Review 155

4.6 Chapter Summary 156

4.7 Programming Exercises 157

5 Procedures 160 5.1 Introduction 160

5.2 Linking to an External Library 160 5.2.1 Background Information 161 5.2.2 Section Review 162

5.3 The Book's Link Library 162 5.3.1 Overview 164 5.3.2 Individual Procedure Descriptions 165 5.3.3 Library Test Programs 177 5.3.4 Section Review 185

5.4 Stack Operations 185 5.4.1 Runtime Stack 186 5.4.2 PUSH and POP Instructions 188 5.4.3 Section Review 190

Page 6: AssEivibly LANquAqE foR

CONTENTS 9

• : • 5.5 Defining and Using Procedures 191

5.5.1 PROC Directive 191 5.5.2 CALL and RET Instructions 193 5.5.3 Example: Summing an Integer Array 196 5.5.4 Flowcharts 197 5.5.5 Saving and Restoring Registers 198 5.5.6 Section Review 199

5.6 Program Design Using Procedures 200 5.6.1 Integer Summation Program (Design) 201 5.6.2 Integer Summation Implementation 203 5.6.3 Section Review 205

5.7 Chapter Summary 205

5.8 Programming Exercises 206

6 Conditional Processing 208 6.1 Introduction 208

6.2 Boolean and Comparison Instructions 209 6.2.1 The CPU Flags 210 6.2.2 AND Instruction 210 6.2.3 OR Instruction 211 6.2.4 Bit-Mapped Sets 212 6.2.5 XOR Instruction 214 6.2.6 NOT Instruction 215 6.2.7 TEST Instruction 215 6.2.8 CMP Instruction 216 6.2.9 Setting and Clearing Individual CPU Flags 217 6.2.10 Section Review 217

6.3 Conditional Jumps 218 6.3.1 Conditional Structures 218 6.3.2 Icond Instruction 219 6.3.3 Types of Conditional Jump Instructions 220 6.3.4 Conditional Jump Applications 223 6.3.5 Section Review 227

6.4 Conditional Loop Instructions 228 6.4.1 LOOPZ and LOOPE Instructions 228 6.4.2 LOOPNZ and LOOPNE Instructions 229 6.4.3 Section Review 229

6.5 Conditional Structures 230 6.5.1 Block-Structured IF Statements 230 6.5.2 Compound Expressions 232 6.5.3 WHILE Loops 234 6.5.4 Table-Driven Selection 236 6.5.5 Section Review 238

Page 7: AssEivibly LANquAqE foR

6.6 Application: Finite-State Machines 239 6.6.1 Validating an Input String 239 6.6.2 Validating a Signed Integer 240 6.6.3 Section Review 244

6.7 Conditional Control Flow Directives 245 6.7.1 Creating IF Statements 246 6.7.2 Signed and Unsigned Comparisons 247 6.7.3 Compound Expressions 248 6.7.4 Creating Loops with .REPEAT and .WHILE 251

6.8 Chapter Summary 252

6.9 Programming Exercises 253

7 Integer Arithmetic 257 7.1 Introduction 257

7.2 Shift and Rotate Instructions 258 7.2.1 Logical Shifts and Arithmetic Shifts 258 7.2.2 SHL Instruction 259 7.2.3 SHR Instruction 260 7.2.4 SAL and SAR Instructions 261 7.2.5 ROL Instruction 262 7.2.6 ROR Instruction 263 7.2.7 RCL and RCR Instructions 263 7.2.8 Signed Overflow 264 7.2.9 SHLD/SHRD Instructions 264 7.2.10 Section Review 266

7.3 Shift and Rotate Applications 267 7.3.1 Shifting Multiple Doublewords 268 7.3.2 Binary Multiplication 269 7.3.3 Displaying Binary Bits 270 7.3.4 Isolating MS-DOS File Date Fields 270 7.3.5 Section Review 271

7.4 Multiplication and Division Instructions 271 7.4.1 MUL Instruction 271 7.4.2 IMUL Instruction 273 7.4.3 Measuring Program Execution Times 275 7.4.4 DIV Instruction 277 7.4.5 Signed Integer Division 278 7.4.6 Implementing Arithmetic Expressions 281 7.4.7 Section Review 283

7.5 Extended Addition and Subtraction 284 7.5.1 ADC Instruction 284 7.5.2 Extended Addition Example 285

Page 8: AssEivibly LANquAqE foR

CONTENTS 11

7.5.3 SBB Instruction 286 7.5.4 Section Review 287

7.6 ASCII and Unpacked Decimal Arithmetic 288 7.6.1 AAA Instruction 289 7.6.2 AAS Instruction 290 7.6.3 AAM Instruction 291 7.6.4 AAD Instruction 291 7.6.5 Section Review 292

7.7 Packed Decimal Arithmetic 292 7.7.1 DAA Instruction 292 7.7.2 DAS Instruction 294 7.7.3 Section Review 294

7.8 Chapter Summary 294

7.9 Programming Exercises 295

8 Advanced Procedures 298 8.1 Introduction 298

8.2 Stack Frames 299 8.2.1 Stack Parameters 300 8.2.2 Accessing Stack Parameters 301 8.2.3 Local Variables 309 8.2.4 ENTER and LEAVE Instructions 313 8.2.5 LOCAL Directive 314 8.2.6 Section Review 317

8.3 Recursion 318 8.3.1 Recursively Calculating a Sum 319 8.3.2 Calculating a Factorial 320 8.3.3 Section Review 326

8.4 INVOKE, ADDR, PROC, and PROTO 327 8.4.1 INVOKE Directive 327 8.4.2 ADDR Operator 328 8.4.3 PROC Directive 329 8.4.4 PROTO Directive 332 8.4.5 Parameter Classifications 335 8.4.6 Example: Exchanging Two Integers 335 8.4.7 Debugging Tips 336 8.4.8 WriteStackFrame Procedure 337 8.4.9 Section Review 338

8.5 Creating Multimodule Programs 339 8.5.1 Hiding and Exporting Procedure Names 339 8.5.2 Calling External Procedures 340

Page 9: AssEivibly LANquAqE foR

8.5.3 Using Variables and Symbols across Module Boundaries 8.5.4 Example: ArraySum Program 342 8.5.5 Creating the Modules Using Extern 342 8.5.6 Creating the Modules Using INVOKE and PROTO 346 8.5.7 Section Review 349

8.6 Java Bytecodes 349 8.6.1 Java Virtual Machine 349 8.6.2 Instruction Set 350 8.6.3 Java Disassembly Examples 351

8.7 Chapter Summary 356

8.8 Programming Exercises 357

9 Strings and Arrays 360 9.1 Introduction 360

9.2 String Primitive Instructions 361 9.2.1 MOVSB, MOVSW, and MOVSD 362 9.2.2 CMPSB, CMPSW, and CMPSD 363 9.2.3 SCASB, SCASW, and SCASD 364 9.2.4 STOSB, STOSW, and STOSD 364 9.2.5 LODSB, LODSW, and LODSD 365 9.2.6 Section Review 365

9.3 Selected String Procedures 366 9.3.1 Str_compare Procedure 366 9.3.2 Strjength Procedure 367 9.3.3 Str_copy Procedure 368 9.3.4 Str_trim Procedure 368 9.3.5 Str__ucase Procedure 371 9.3.6 String Library Demo Program 372 9.3.7 Section Review 374

9.4 Two-Dimensionai Arrays 374 9.4.1 Ordering of Rows and Columns 374 9.4.2 Base-Index Operands 375 9.4.3 Base-Index-Displacement Operands 377 9.4.4 Section Review 378

9.5 Searching and Sorting Integer Arrays 378 9.5.1 Bubble Sort 378 9.5.2 Binary Search 380 9.5.3 Section Review 387

9.6 Java Bytecodes: String Processing 387

9.7 Chapter Summary 388

9.8 Programming Exercises 389

Page 10: AssEivibly LANquAqE foR

CONTENTS 15

• •

10 Structures and Macros 394 10.1 Structures 394

10.1.1 Defining Structures 395 10.1.2 Declaring Structure Variables 396 10.1.3 Referencing Structure Variables 398 10.1.4 Example: Displaying the System Time 400 10.1.5 Structures Containing Structures 403 10.1.6 Example: Drunkard's Walk 403 10.1.7 Declaring and Using Unions 406 10.1.8 Section Review 409

10.2 Macros 410 10.2.1 Overview 410 10.2.2 Defining Macros 410 10.2.3 Invoking Macros 411 10.2.4 Additional Macro Features 412 10.2.5 Using the Book's Macro Library 416 10.2.6 Example Program: Wrappers 422 10.2.7 Section Review 423

10.3 Conditional-Assembly Directives 424 10.3.1 Checking for Missing Arguments 425 10.3.2 Default Argument Initializers 426 10.3.3 Boolean Expressions 427 10.3.4 IF, ELSE, and ENDIF Directives 427 10.3.5 The IFIDN and IFIDNI Directives 428 10.3.6 Example: Summing a Matrix Row 429 10.3.7 Special Operators 432 10.3.8 Macro Functions 435 10.3.9 Section Review 437

10.4 Defining Repeat Blocks 438 10.4.1 WHILE Directive 438 10.4.2 REPEAT Directive 438 10.4.3 FOR Directive 439 10.4.4 FORC Directive 440 10.4.5 Example: Linked List 440 10.4.6 Section Review 442

10.5 Chapter Summary 443

10.6 Programming Exercises 444

11 MS-Windows Programming 447 11.1 Win32 Console Programming 447

11.1.1 Background Information 448 11.1.2 Win32 Console Functions 452 11.1.3 Displaying a Message Box 454

Page 11: AssEivibly LANquAqE foR

14 CONTENTS

11.1.4 Console Input 457 11.1.5 Console Output 463 11.1.6 Reading and Writing Files 465 11.1.7 File I/O in the Irvine32 Library 470 11.1.8 Testing the File I/O Procedures 472 11.1.9 Console Window Manipulation 475 11.1.10 Controlling the Cursor 478 11.1.11 Controlling the Text Color 479 11.1.12 Time and Date Functions 481 11.1.13 Section Review 484

11.2 Writing a Graphical Windows Application 485 11.2.1 Necessary Structures 486 11.2.2 The MessageBox Function 487 11.2.3 The WinMain Procedure 488 11.2.4 The WinProc Procedure 488 11.2.5 The ErrorHandler Procedure 489 11.2.6 Program Listing 489 11.2.7 Section Review 493

11.3 Dynamic Memory Allocation 494 11.3.1 HeapTest Programs 497 11.3.2 Section Review 501

11.4 x86 Memory Management 501 11.4.1 Linear Addresses 501 11.4.2 Page Translation 505 11.4.3 Section Review 507

11.5 Chapter Summary 507

11.6 Programming Exercises 509

12 Floating-Point Processing and Instruction Encoding 511

12.1 Floating-Point Binary Representation 511 12.1.1 IEEE Binary Floating-Point Representation 512 12.1.2 The Exponent 513 12.1.3 Normalized Binary Floating-Point Numbers 514 12.1.4 Creating the IEEE Representation 514 12.1.5 Converting Decimal Fractions to Binary Reals 516 12.1.6 Section Review 518

12.2 Floating-Point Unit 518 12.2.1 FPU Register Stack 519 12.2.2 Rounding 521 12.2.3 Floating-Point Exceptions 523 12.2.4 Floating-Point Instruction Set 523

Page 12: AssEivibly LANquAqE foR

CONTENTS 1 5

• • 12.2.5 Arithmetic Instructions 526 12.2.6 Comparing Floating-Point Values 530 12.2.7 Reading and Writing Floating-Point Values 532 12.2.8 Exception Synchronization 534 12.2.9 Code Examples 535 12.2.10 Mixed-Mode Arithmetic 536 12.2.11 Masking and Unmasking Exceptions 537 12.2.12 Section Review 539

12.3 x86 Instruction Encoding 540 12.3.1 Instruction Format 540 12.3.2 Single-Byte Instructions 541 12.3.3 Move Immediate to Register 542 12.3.4 Register-Mode Instructions 542 12.3.5 Processor Operand-Size Prefix 543 12.3.6 Memory-Mode Instructions 544 12.3.7 Section Review 547

12.4 Chapter Summary 548

12.5 Programming Exercises 549

13 High-Level Language Interface 553 13.1 Introduction 553

13.1.1 General Conventions 554 13.1.2 .MODEL Directive 555 13.1.3 Section Review 557

13.2 Inline Assembly Code 557 13.2.1 asm Directive in Microsoft Visual C++ 557 13.2.2 File Encryption Example 560 13.2.3 Section Review 563

13.3 Linking to C/C++ in Protected Mode 563 13.3.1 Using Assembly Language to Optimize C++Code 564 13.3.2 Calling С and C++Functions 570 13.3.3 Multiplication Table Example 572 13.3.4 Calling С Library Functions 575 13.3.5 Directory Listing Program 578 13.3.6 Section Review 580

13.4 Linking to C/C++in Real-Address Mode 580 13.4.1 Linking to Borland C++ 581 13.4.2 ReadSector Example 582 13.4.3 Example: Large Random Integers 586 13.4.4 Section Review 587

13.5 Chapter Summary 588

13.6 Programming Exercises 588

Page 13: AssEivibly LANquAqE foR

16 CONTENTS

14.1 MS-DOS and the IBM-PC 590 14.1.1 Memory Organization 591 14.1.2 Redirecting Input-Output 592 14.1.3 Software Interrupts 593 14.1.4 INT Instruction 593 14.1.5 Coding for 16-Bit Programs 595 14.1.6 Section Review 596

14.2 MS-DOS Function Calls (INT 21 h) 596 14.2.1 Selected Output Functions 598 14.2.2 Hello World Program Example 600 14.2.3 Selected Input Functions 601 14.2.4 Date/Time Functions 605 14.2.5 Section Review 609

14.3 Standard MS-DOS File I/O Services 609 14.3.1 Create or Open File (716Ch) 611 14.3.2 Close File Handle (3Eh) 612 14.3.3 Move File Pointer (42h) 612 14.3.4 Get File Creation Date and Time 613 14.3.5 Selected Library Procedures 613 14.3.6 Example: Read and Copy a Text File 614 14.3.7 Reading the MS-DOS Command Tail 616 14.3.8 Example: Creating a Binary File 619 14.3.9 Section Review 622

14.4 Chapter Summary 622

14.5 Programming Exercises 624

Chapters are available from the Companion Web site

15 Disk Fundamentals

16 BIOS-Level Programming 17 Expert MS-DOS Programming Appendix A MASM Reference 626 Appendix В The x86 Instruction Set 648 Appendix С Answers to Review Questions 683

Page 14: AssEivibly LANquAqE foR

CONTENTS 17

Appendices are available from the Companion Web site

Appendix D BIOS and MS-DOS Interrupts Appendix E Answers to Review Questions

(Chapters 15-17)

Index 727