Tải bản đầy đủ (.pdf) (747 trang)

assembly language for x86 processors (6th ed ) irvine 2010 03 07

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (1.83 MB, 747 trang )


Assembly Language for
x86 Processors
Sixth Edition

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

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


Vice President and Editorial Director, ECS: Marcia J. Horton
Editor-in-Chief: Michael Hirsch
Executive Editor: Tracy Dunkelberger
Assistant Editor: Melinda Haggerty
Editorial Assistant: Allison Michael
Vice President, Production: Vince O’Brien
Senior Managing Editor: Scott Disanno
Production Liaison: Jane Bonnell
Production Editor: Maheswari PonSaravanan, TexTech International
Senior Operations Supervisor: Alan Fischer
Marketing Manager: Erin Davis
Marketing Assistant: Mack Patterson
Art Director: Kenny Beck
Cover Designer: Laura C. Ierardi
Cover Image: Color enhanced x-ray of nautilus shell / Bert Myers / Science Photo Library
Art Editor: Greg Dulles
Media Editor: Daniel Sandin


Media Project Manager: Danielle Leone
Composition/Full-Service Project Management: TexTech International
IA-32, Pentium, i486, Intel64, Celeron, and Intel 386 are trademarks of Intel Corporation. Athlon, Phenom, and Opteron
are trademarks of Advanced Micro Devices. TASM and Turbo Debugger are trademarks of Borland International.
Microsoft Assembler (MASM), Windows Vista, Windows 7, 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 are registered trademarks of Microsoft Corporation. Autocad is a trademark of Autodesk. Java is a
trademark of Sun Microsystems. PartitionMagic is a trademark of Symantec. All other trademarks or product names are
the property of their respective owners.
Copyright © 2011, 2007, 2003, 1999 by Pearson Education, Inc., Upper Saddle River, New Jersey 07458. All rights
reserved. Manufactured in the United States of America. This publication is protected by Copyright and permissions
should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system, or transmission in
any form or by any means, electronic, mechanical, photocopying, recording, or likewise. To obtain permission(s) to use
materials from this work, please submit a written request to Pearson Higher Education, Permissions Department, 1 Lake
Street, Upper Saddle River, NJ 07458.
Previously published as Assembly Language for Intel-Based Computers.
The author and publisher of this book have used their best efforts in preparing this book. These efforts include the
development, 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 in this book. The author and publisher shall not be liable in any event for incidental or consequential damages in
connection with, or arising out of, the furnishing, performance, or use of these programs.
Library of Congress Cataloging-in-Publication Data
Irvine, Kip R.
Assembly language for x86 processors / Kip R. Irvine. -- 6th ed.
p. cm.
Rev. ed. of: Assembly language for intel-based computers, 5th ed., 2007.
ISBN 0-13-602212-X (alk. paper)
1. IBM microcomputers--Programming. 2. Assembler language (Computer program
language) I. Irvine, Kip R. - Assembly language for intel-based computers. II. Title.
QA76.8.I77 2011
005.265--dc22

2009049014

10 9 8 7 6 5 4 3 2 1
ISBN-13: 978-0-13-602212-1
ISBN-10:
0-13-602212-X


To Jack and Candy Irvine


This page intentionally left blank


Contents
Preface

xix

1

Basic Concepts

1.1

Welcome to Assembly Language
1.1.1
1.1.2
1.1.3


1.2
1.3

1.4

22

Truth Tables for Boolean Functions 24
Section Review 26

1.5

Chapter Summary

1.6

Exercises
1.6.1
1.6.2

9

Binary Integers 9
Binary Addition 11
Integer Storage Sizes 12
Hexadecimal Integers 13
Signed Integers 15
Character Storage 17
Section Review 19


Boolean Operations
1.4.1
1.4.2

7

Section Review 9

Data Representation
1.3.1
1.3.2
1.3.3
1.3.4
1.3.5
1.3.6
1.3.7

1

Good Questions to Ask 2
Assembly Language Applications 5
Section Review 6

Virtual Machine Concept
1.2.1

1

26


27

Programming Tasks 27
Nonprogramming Tasks 27

2

x86 Processor Architecture

2.1

General Concepts
2.1.1
2.1.2
2.1.3
2.1.4
2.1.5

29

Basic Microcomputer Design 30
Instruction Execution Cycle 31
Reading from Memory 33
How Programs Run 34
Section Review 35

v

29



vi

2.2

Contents

x86 Architecture Details
2.2.1
2.2.2
2.2.3
2.2.4
2.2.5

2.3

x86 Memory Management
2.3.1
2.3.2
2.3.3

2.4

43

Real-Address Mode 43
Protected Mode 45
Section Review 47

Components of a Typical x86 Computer

2.4.1
2.4.2
2.4.3
2.4.4
2.4.5

2.5

36

Modes of Operation 36
Basic Execution Environment 36
Floating-Point Unit 39
Overview of Intel Microprocessors 39
Section Review 42

Input-Output System
2.5.1
2.5.2

48

Motherboard 48
Video Output 50
Memory 50
Input-Output Ports and Device Interfaces 50
Section Review 52

52


Levels of I/O Access 52
Section Review 55

2.6

Chapter Summary

55

2.7

Chapter Exercises

57

3

Assembly Language Fundamentals

3.1

Basic Elements of Assembly Language
3.1.1
3.1.2
3.1.3
3.1.4
3.1.5
3.1.6
3.1.7
3.1.8

3.1.9
3.1.10
3.1.11

3.2

Example: Adding and Subtracting Integers
3.2.1
3.2.2
3.2.3

3.3

58

Integer Constants 59
Integer Expressions 60
Real Number Constants 61
Character Constants 61
String Constants 61
Reserved Words 62
Identifiers 62
Directives 62
Instructions 63
The NOP (No Operation) Instruction 65
Section Review 66

66

Alternative Version of AddSub 69

Program Template 70
Section Review 70

Assembling, Linking, and Running Programs
3.3.1
3.3.2

The Assemble-Link-Execute Cycle 71
Section Review 77

71

58


Contents

3.4

vii

Defining Data
3.4.1
3.4.2
3.4.3
3.4.4
3.4.5
3.4.6
3.4.7
3.4.8

3.4.9
3.4.10
3.4.11
3.4.12

3.5

Symbolic Constants
3.5.1
3.5.2
3.5.3
3.5.4
3.5.5

3.6

77

Intrinsic Data Types 77
Data Definition Statement 77
Defining BYTE and SBYTE Data 78
Defining WORD and SWORD Data 80
Defining DWORD and SDWORD Data 81
Defining QWORD Data 81
Defining Packed Binary Coded Decimal (TBYTE) Data 82
Defining Real Number Data 83
Little Endian Order 83
Adding Variables to the AddSub Program 84
Declaring Uninitialized Data 85
Section Review 85


86

Equal-Sign Directive 86
Calculating the Sizes of Arrays and Strings 87
EQU Directive 88
TEXTEQU Directive 89
Section Review 90

Real-Address Mode Programming (Optional)
3.6.1

Basic Changes 90

3.7

Chapter Summary

3.8

Programming Exercises

4

Data Transfers, Addressing,
and Arithmetic 94

4.1

Data Transfer Instructions 94

4.1.1
4.1.2
4.1.3
4.1.4
4.1.5
4.1.6
4.1.7
4.1.8
4.1.9
4.1.10

4.2

91

Introduction 94
Operand Types 95
Direct Memory Operands 96
MOV Instruction 96
Zero/Sign Extension of Integers 98
LAHF and SAHF Instructions 100
XCHG Instruction 100
Direct-Offset Operands 101
Example Program (Moves) 102
Section Review 103

Addition and Subtraction
4.2.1
4.2.2
4.2.3

4.2.4

92

104

INC and DEC Instructions 104
ADD Instruction 104
SUB Instruction 105
NEG Instruction 105

90


viii

Contents

4.2.5
4.2.6
4.2.7
4.2.8

4.3

Data-Related Operators and Directives
4.3.1
4.3.2
4.3.3
4.3.4

4.3.5
4.3.6
4.3.7
4.3.8

4.4

OFFSET Operator 112
ALIGN Directive 113
PTR Operator 114
TYPE Operator 115
LENGTHOF Operator 115
SIZEOF Operator 116
LABEL Directive 116
Section Review 117

Indirect Addressing
4.4.1
4.4.2
4.4.3
4.4.4
4.4.5

4.5

Implementing Arithmetic Expressions 106
Flags Affected by Addition and Subtraction 106
Example Program (AddSub3) 110
Section Review 111


117

Indirect Operands 118
Arrays 119
Indexed Operands 120
Pointers 121
Section Review 123

JMP and LOOP Instructions 124
4.5.1
4.5.2
4.5.3
4.5.4
4.5.5

JMP Instruction 124
LOOP Instruction 124
Summing an Integer Array 126
Copying a String 126
Section Review 127

4.6

Chapter Summary

4.7

Programming Exercises 129

5


Procedures

5.1

Introduction

5.2

Linking to an External Library 132
5.2.1
5.2.2

5.3

132

132

Background Information 133
Section Review 134

The Book’s Link Library
5.3.1
5.3.2
5.3.3
5.3.4

5.4


128

Stack Operations
5.4.1
5.4.2
5.4.3

134

Overview 136
Individual Procedure Descriptions 137
Library Test Programs 149
Section Review 157

157

Runtime Stack 158
PUSH and POP Instructions 160
Section Review 162

112


Contents

5.5

ix

Defining and Using Procedures 163

5.5.1
5.5.2
5.5.3
5.5.4
5.5.5
5.5.6

5.6

PROC Directive 163
CALL and RET Instructions 165
Example: Summing an Integer Array 168
Flowcharts 169
Saving and Restoring Registers 170
Section Review 171

Program Design Using Procedures
5.6.1
5.6.2
5.6.3

172

Integer Summation Program (Design) 173
Integer Summation Implementation 175
Section Review 177

5.7

Chapter Summary


5.8

Programming Exercises

6

Conditional Processing

6.1

Introduction

6.2

Boolean and Comparison Instructions
6.2.1
6.2.2
6.2.3
6.2.4
6.2.5
6.2.6
6.2.7
6.2.8
6.2.9
6.2.10

6.3

6.4


190

Conditional Structures 190
Jcond Instruction 191
Types of Conditional Jump Instructions 192
Conditional Jump Applications 195
Section Review 199

200

LOOPZ and LOOPE Instructions 200
LOOPNZ and LOOPNE Instructions 201
Section Review 201

Conditional Structures
6.5.1
6.5.2
6.5.3
6.5.4
6.5.5

180
181

The CPU Flags 182
AND Instruction 182
OR Instruction 183
Bit-Mapped Sets 184
XOR Instruction 186

NOT Instruction 187
TEST Instruction 187
CMP Instruction 188
Setting and Clearing Individual CPU Flags 189
Section Review 189

Conditional Loop Instructions
6.4.1
6.4.2
6.4.3

6.5

178

180

Conditional Jumps
6.3.1
6.3.2
6.3.3
6.3.4
6.3.5

177

202

Block-Structured IF Statements 202
Compound Expressions 204

WHILE Loops 206
Table-Driven Selection 208
Section Review 210


x

6.6

Contents

Application: Finite-State Machines
6.6.1
6.6.2
6.6.3

6.7

Conditional Control Flow Directives
6.7.1
6.7.2
6.7.3
6.7.4

Chapter Summary

6.9

Programming Exercises


7

Integer Arithmetic

7.1

Introduction

7.2

Shift and Rotate Instructions 230
7.2.1
7.2.2
7.2.3
7.2.4
7.2.5
7.2.6
7.2.7
7.2.8
7.2.9
7.2.10

225

229

229

Logical Shifts and Arithmetic Shifts 230
SHL Instruction 231

SHR Instruction 232
SAL and SAR Instructions 233
ROL Instruction 234
ROR Instruction 235
RCL and RCR Instructions 235
Signed Overflow 236
SHLD/SHRD Instructions 236
Section Review 238
Shifting Multiple Doublewords 240
Binary Multiplication 241
Displaying Binary Bits 242
Isolating MS-DOS File Date Fields 242
Section Review 243

Multiplication and Division Instructions
7.4.1
7.4.2
7.4.3
7.4.4
7.4.5
7.4.6
7.4.7

7.5

224

Shift and Rotate Applications 239
7.3.1
7.3.2

7.3.3
7.3.4
7.3.5

7.4

217

Creating IF Statements 218
Signed and Unsigned Comparisons 219
Compound Expressions 220
Creating Loops with .REPEAT and .WHILE 223

6.8

7.3

211

Validating an Input String 211
Validating a Signed Integer 212
Section Review 216

MUL Instruction 243
IMUL Instruction 245
Measuring Program Execution Times 247
DIV Instruction 249
Signed Integer Division 250
Implementing Arithmetic Expressions 253
Section Review 255


Extended Addition and Subtraction
7.5.1
7.5.2

ADC Instruction 256
Extended Addition Example 257

256

243


Contents

xi

7.5.3
7.5.4

7.6

ASCII and Unpacked Decimal Arithmetic
7.6.1
7.6.2
7.6.3
7.6.4
7.6.5

7.7


SBB Instruction 258
Section Review 259
AAA Instruction
AAS Instruction
AAM Instruction
AAD Instruction
Section Review

261
262
263
263
264

Packed Decimal Arithmetic 264
7.7.1
7.7.2
7.7.3

DAA Instruction 264
DAS Instruction 266
Section Review 266

7.8

Chapter Summary

7.9


Programming Exercises

8

Advanced Procedures

8.1

Introduction

8.2

Stack Frames
8.2.1
8.2.2
8.2.3
8.2.4
8.2.5
8.2.6

8.3

8.4

270

270
271

290


Recursively Calculating a Sum 291
Calculating a Factorial 292
Section Review 298

INVOKE, ADDR, PROC, and PROTO
8.4.1
8.4.2
8.4.3
8.4.4
8.4.5
8.4.6
8.4.7
8.4.8
8.4.9

8.5

267

Stack Parameters 272
Accessing Stack Parameters 273
Local Variables 281
ENTER and LEAVE Instructions 285
LOCAL Directive 286
Section Review 289

Recursion
8.3.1
8.3.2

8.3.3

266

Creating Multimodule Programs
8.5.1
8.5.2

299

INVOKE Directive 299
ADDR Operator 300
PROC Directive 301
PROTO Directive 304
Parameter Classifications 307
Example: Exchanging Two Integers 307
Debugging Tips 308
WriteStackFrame Procedure 309
Section Review 310

311

Hiding and Exporting Procedure Names 311
Calling External Procedures 312

260


xii


Contents

8.5.3
8.5.4
8.5.5
8.5.6
8.5.7

8.6

Using Variables and Symbols across Module Boundaries 313
Example: ArraySum Program 314
Creating the Modules Using Extern 314
Creating the Modules Using INVOKE and PROTO 318
Section Review 321

Java Bytecodes
8.6.1
8.6.2
8.6.3

321

Java Virtual Machine 321
Instruction Set 322
Java Disassembly Examples 323

8.7

Chapter Summary


8.8

Programming Exercises

9

Strings and Arrays

9.1

Introduction

9.2

String Primitive Instructions 333
9.2.1
9.2.2
9.2.3
9.2.4
9.2.5
9.2.6

9.3

332

332

MOVSB, MOVSW, and MOVSD 334

CMPSB, CMPSW, and CMPSD 335
SCASB, SCASW, and SCASD 336
STOSB, STOSW, and STOSD 336
LODSB, LODSW, and LODSD 337
Section Review 337
Str_compare Procedure 338
Str_length Procedure 339
Str_copy Procedure 340
Str_trim Procedure 340
Str_ucase Procedure 343
String Library Demo Program 344
Section Review 346

Two-Dimensional Arrays 346
9.4.1
9.4.2
9.4.3
9.4.4

9.5

329

Selected String Procedures 338
9.3.1
9.3.2
9.3.3
9.3.4
9.3.5
9.3.6

9.3.7

9.4

328

Ordering of Rows and Columns 346
Base-Index Operands 347
Base-Index-Displacement Operands 349
Section Review 350

Searching and Sorting Integer Arrays
9.5.1
9.5.2
9.5.3

350

Bubble Sort 350
Binary Search 352
Section Review 359

9.6

Java Bytecodes: String Processing

9.7

Chapter Summary


9.8

Programming Exercises

360
361

359


Contents

xiii

10 Structures and Macros 366
10.1 Structures
10.1.1
10.1.2
10.1.3
10.1.4
10.1.5
10.1.6
10.1.7
10.1.8

10.2 Macros
10.2.1
10.2.2
10.2.3
10.2.4

10.2.5
10.2.6
10.2.7

366

Defining Structures 367
Declaring Structure Variables 368
Referencing Structure Variables 370
Example: Displaying the System Time 372
Structures Containing Structures 375
Example: Drunkard’s Walk 375
Declaring and Using Unions 378
Section Review 381

382

Overview 382
Defining Macros 382
Invoking Macros 383
Additional Macro Features 384
Using the Book’s Macro Library 388
Example Program: Wrappers 394
Section Review 395

10.3 Conditional-Assembly Directives
10.3.1
10.3.2
10.3.3
10.3.4

10.3.5
10.3.6
10.3.7
10.3.8
10.3.9

10.4 Defining Repeat Blocks
10.4.1
10.4.2
10.4.3
10.4.4
10.4.5
10.4.6

396

Checking for Missing Arguments 397
Default Argument Initializers 398
Boolean Expressions 399
IF, ELSE, and ENDIF Directives 399
The IFIDN and IFIDNI Directives 400
Example: Summing a Matrix Row 401
Special Operators 404
Macro Functions 407
Section Review 409

410

WHILE Directive 410
REPEAT Directive 410

FOR Directive 411
FORC Directive 412
Example: Linked List 412
Section Review 414

10.5 Chapter Summary

415

10.6 Programming Exercises 416

11 MS-Windows Programming 419
11.1 Win32 Console Programming
11.1.1 Background Information 420
11.1.2 Win32 Console Functions 424
11.1.3 Displaying a Message Box 426

419


xiv

Contents

11.1.4
11.1.5
11.1.6
11.1.7
11.1.8
11.1.9

11.1.10
11.1.11
11.1.12
11.1.13

Console Input 429
Console Output 435
Reading and Writing Files 437
File I/O in the Irvine32 Library 442
Testing the File I/O Procedures 444
Console Window Manipulation 447
Controlling the Cursor 450
Controlling the Text Color 451
Time and Date Functions 453
Section Review 456

11.2 Writing a Graphical Windows Application
11.2.1
11.2.2
11.2.3
11.2.4
11.2.5
11.2.6
11.2.7

457

Necessary Structures 458
The MessageBox Function 459
The WinMain Procedure 460

The WinProc Procedure 460
The ErrorHandler Procedure 461
Program Listing 461
Section Review 465

11.3 Dynamic Memory Allocation

466

11.3.1 HeapTest Programs 469
11.3.2 Section Review 473

11.4 x86 Memory Management

473

11.4.1 Linear Addresses 473
11.4.2 Page Translation 477
11.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
12.1.1
12.1.2
12.1.3
12.1.4
12.1.5
12.1.6

12.2 Floating-Point Unit
12.2.1
12.2.2
12.2.3
12.2.4

483

IEEE Binary Floating-Point Representation 484
The Exponent 485
Normalized Binary Floating-Point Numbers 486
Creating the IEEE Representation 486
Converting Decimal Fractions to Binary Reals 488
Section Review 490

490

FPU Register Stack 491
Rounding 493

Floating-Point Exceptions 495
Floating-Point Instruction Set 495


Contents

xv

12.2.5
12.2.6
12.2.7
12.2.8
12.2.9
12.2.10
12.2.11
12.2.12

Arithmetic Instructions 498
Comparing Floating-Point Values 502
Reading and Writing Floating-Point Values 504
Exception Synchronization 506
Code Examples 507
Mixed-Mode Arithmetic 508
Masking and Unmasking Exceptions 509
Section Review 511

12.3 x86 Instruction Encoding 512
12.3.1
12.3.2
12.3.3

12.3.4
12.3.5
12.3.6
12.3.7

Instruction Format 512
Single-Byte Instructions 513
Move Immediate to Register 514
Register-Mode Instructions 514
Processor Operand-Size Prefix 515
Memory-Mode Instructions 516
Section Review 519

12.4 Chapter Summary

520

12.5 Programming Exercises

521

13 High-Level Language Interface 525
13.1 Introduction

525

13.1.1 General Conventions 526
13.1.2 .MODEL Directive 527
13.1.3 Section Review 529


13.2 Inline Assembly Code

529

13.2.1 __asm Directive in Microsoft Visual C++ 529
13.2.2 File Encryption Example 532
13.2.3 Section Review 535

13.3 Linking to C/C++ in Protected Mode
13.3.1
13.3.2
13.3.3
13.3.4
13.3.5
13.3.6

535

Using Assembly Language to Optimize C++ Code 536
Calling C and C++ Functions 542
Multiplication Table Example 544
Calling C Library Functions 547
Directory Listing Program 550
Section Review 552

13.4 Linking to C/C++ in Real-Address Mode
13.4.1
13.4.2
13.4.3
13.4.4


Linking to Borland C++ 553
ReadSector Example 554
Example: Large Random Integers 558
Section Review 559

13.5 Chapter Summary

560

13.6 Programming Exercises

560

552


xvi

Contents

14 16-Bit MS-DOS Programming 562
14.1 MS-DOS and the IBM-PC 562
14.1.1
14.1.2
14.1.3
14.1.4
14.1.5
14.1.6


Memory Organization 563
Redirecting Input-Output 564
Software Interrupts 565
INT Instruction 565
Coding for 16-Bit Programs 567
Section Review 568

14.2 MS-DOS Function Calls (INT 21h)
14.2.1
14.2.2
14.2.3
14.2.4
14.2.5

568

Selected Output Functions 570
Hello World Program Example 572
Selected Input Functions 573
Date/Time Functions 577
Section Review 581

14.3 Standard MS-DOS File I/O Services
14.3.1
14.3.2
14.3.3
14.3.4
14.3.5
14.3.6
14.3.7

14.3.8
14.3.9

581

Create or Open File (716Ch) 583
Close File Handle (3Eh) 584
Move File Pointer (42h) 584
Get File Creation Date and Time 585
Selected Library Procedures 585
Example: Read and Copy a Text File 586
Reading the MS-DOS Command Tail 588
Example: Creating a Binary File 591
Section Review 594

14.4 Chapter Summary

594

14.5 Programming Exercises

596

Chapters are available from the Companion Web site

15 Disk Fundamentals
16 BIOS-Level Programming
17 Expert MS-DOS Programming
Appendix A
Appendix B

Appendix C

MASM Reference 598
The x86 Instruction Set 620
Answers to Review Questions

655


Contents

xvii

Appendices are available from the Companion Web site

Appendix D
Appendix E
Index

699

BIOS and MS-DOS Interrupts
Answers to Review Questions
(Chapters 15–17)


This page intentionally left blank


Preface

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

• Searching and sorting algorithms
• High-level language structures

xix


xx

Preface

• Finite-state machines
• Code optimization examples

What’s New in the Sixth Edition
In this revision, we have placed a strong emphasis on improving the descriptions of important
programming concepts and relevant program examples.
• We have added numerous step-by-step descriptions of sample programs, particularly in
Chapters 1–8.
• Many new illustrations have been inserted into the chapters to improve student comprehension of concepts and details.
• Java Bytecodes: The Java Virtual Machine (JVM) provides an excellent real-life example of
a stack-oriented architecture. It provides an excellent contrast to x86 architecture. Therefore,
in Chapters 8 and 9, the author explains the basic operation of Java bytecodes with short illustrative examples. Numerous short examples are shown in disassembled bytecode format, followed by detailed step-by-step explanations.
• Selected programming exercises have been replaced in the first 8 chapters. Programming
exercises are now assigned stars to indicate their difficulty. One star is the easiest, four stars
indicate the most difficult level.
• Tutorial videos by the author are available on the Companion Web site (www.pearsonhighered.com/
irvine) to explain worked-out programming exercises.
• The order of chapters in the second half of the book has been revised to form a more logical
sequence of topics, and selected chapters are supplied in electronic form for easy searching.
This book is still focused on its primary goal, 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 a computer 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 electronic 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 covers 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.


Preface

xxi

Link Libraries We supply two versions of the link library that students use for basic inputoutput, 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 Companion 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.
Included Software and Examples All the example programs were tested with Microsoft
Macro Assembler Version 10.0, running in Microsoft Visual Studio 2010. In addition, batch files
are supplied that permit students to assemble and run applications from the Windows command
prompt. 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 Companion
Web site, including additional programming projects for instructors to assign at the ends of chapters.

Overall Goals
The following goals of this book are designed to broaden the student’s interest and knowledge in
topics 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 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 demonstrate instructions or ideas as they are presented in the text. Reference materials, such as
guides to MS-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 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 classroom with majors in both computer science and management information systems, and it has
been used elsewhere in engineering courses.

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


xxii


Preface

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
conscious 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.
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 family helps students to better understand their target computer system.
Structured Programming Approach Beginning with Chapter 5, procedures and functional
decomposition 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 the
basic operation of Java bytecodes with short illustrative examples. Numerous short examples are
shown in disassembled bytecode format, followed by detailed step-by-step explanations.
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 essential in assembly language and systems programming. Conditional macros with
advanced operators 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 the Web. Instructors are provided
a test bank, answers to review questions, solutions to programming exercises, and a Microsoft
PowerPoint slide presentation for each chapter.
VideoNotes VideoNotes are Pearson’s new visual tool designed to teach students key programming concepts and techniques. These short step-by-step videos demonstrate how to solve
problems from design through coding. VideoNotes allow for self-paced instruction with
easy navigation including the ability to select, play, rewind, fast-forward, and stop within each
VideoNote exercise. A note appears within the text to designate that a VideoNote is available.


Preface

xxiii

VideoNotes are free with the purchase of a new textbook. To purchase access to VideoNotes,
go to www.pearsonhighered.com/irvine and click on the VideoNotes under Student Resources.

Chapter Descriptions
Chapters 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 shows
how later chapters depend on knowledge gained from other chapters.
1 through 9

10

11

12

15


13

14

16

17

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

2. x86 Processor Architecture: Basic microcomputer design, instruction execution cycle, x86
processor architecture, x86 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.
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 operations, 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
directives, and recursion.
9. Strings and Arrays: String primitives, manipulating arrays of characters and integers, twodimensional 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. 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.


xxiv

Preface

13. High-Level Language Interface: Parameter passing conventions, inline assembly code, and
linking 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: MASM Reference
• Appendix B: The x86 Instruction Set
• Appendix C: Answers to Review Questions
The following chapters and appendices are supplied online at the Companion Web site:
15. Disk Fundamentals: Disk storage systems, sectors, clusters, directories, file allocation
tables, 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
• Appendix E: Answers to Review Questions (Chapters 15–17)

Instructor and Student Resources
Instructor Resource Materials
The following protected instructor material is available on the Companion Web site:
www.pearsonhighered.com/irvine

For username and password information, please contact your Pearson Representative.
• Lecture PowerPoint Slides

• Instructor Solutions Manual
Student Resource Materials
The student resource materials can be accessed through the publisher’s Web site located at
www.pearsonhighered.com/irvine. These resources include:
• VideoNotes
• Online Chapters and Appendices
• Chapter 15: Disk Fundamentals
• Chapter 16: BIOS-Level Programming
• Chapter 17: Expert MS-DOS Programming
• Appendix D: BIOS and MS-DOS Interrupts
• Appendix E: Answers to Review Questions (Chapters 15–17)
Students must use the access card located in the front of the book to register and access the online
chapters and VideoNotes. If there is no access card in the front of this textbook, students can purchase
access by going to www.pearsonhighered.com/irvine and selecting “purchase access to premium
content.” Instructors must also register on the site to access this material. Students will also find a link
to the author’s Web site. An access card is not required for the following materials:
• Getting Started, a comprehensive step-by-step tutorial that helps students customize Visual
Studio for assembly language programming.
• Supplementary articles on assembly language programming topics.


×