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

IT training assembly language for x86 processors (7th ed ) irvine 2014 03 21

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 (11.5 MB, 873 trang )


ASCII CONTROL CHARACTERS
The following list shows the ASCII codes generated when a control key combination is pressed.
The mnemonics and descriptions refer to ASCII functions used for screen and printer formatting
and data communications.
ASCII
Code*

Ctrl-

Mnemonic

00

Description

ASCII
Code*

Ctrl-

Mnemonic

NUL

Null character

10

Ctrl-P


DLE

Data link escape

Description
Device control 1

01

Ctrl-A

SOH

Start of header

11

Ctrl-Q

DC1

02

Ctrl-B

STX

Start of text

12


Ctrl-R

DC2

Device control 2

03

Ctrl-C

ETX

End of text

13

Ctrl-S

DC3

Device control 3

04

Ctrl-D

EOT

End of transmission


14

Ctrl-T

DC4

Device control 4

05

Ctrl-E

ENQ

Enquiry

15

Ctrl-U

NAK

Negative acknowledge

06

Ctrl-F

ACK


Acknowledge

16

Ctrl-V

SYN

Synchronous idle

07

Ctrl-G

BEL

Bell

17

Ctrl-W

ETB

End transmission block

08

Ctrl-H


BS

Backspace

18

Ctrl-X

CAN

Cancel

09

Ctrl-I

HT

Horizontal tab

19

Ctrl-Y

EM

0A

Ctrl-J


LF

Line feed

1A

Ctrl-Z

SUB

End of medium
Substitute

0B

Ctrl-K

VT

Vertical tab

1B

Ctrl-I

ESC

Escape


0C

Ctrl-L

FF

Form feed

1C

Ctrl-\

FS

File separator

0D

Ctrl-M

CR

Carriage return

1D

Ctrl-]

GS


Group separator

0E

Ctrl-N

SO

Shift out

1E

Ctrl- ^

RS

Record separator

0F

Ctrl-O

SI

Shift in

1F

Ctrl-†


US

Unit separator

* ASCII codes are in hexadecimal.

† ASCII code 1Fh is Ctrl-Hyphen (-).

ALT-KEY COMBINATIONS
The following hexadecimal scan codes are produced by holding down
the ALT key and pressing each character:
Key
1

Scan Code
78

Key
A

Scan Code

Key

1E

N

Scan Code
31


2

79

B

30

O

18

3

7A

C

2E

P

19

4

7B

D


20

Q

10

5

7C

E

12

R

13

6

7D

F

21

S

1F


7

7E

G

22

T

14

8

7F

H

23

U

16

9

80

I


17

V

2F

0

81

J

24

W

11

Ϫ

82

K

25

X

2D


ϭ

83

L

26

Y

15

M

32

Z

2C


KEYBOARD SCAN CODES
The following keyboard scan codes may be retrieved either by calling INT 16h or by calling
INT 21h for keyboard input a second time (the first keyboard read returns 0). All codes are in
hexadecimal:
FUNCTION KEYS
Key

Normal


With
Shift

With
Ctrl

With Alt

F1

3B

54

5E

68

F2

3C

55

5F

69

F3


3D

56

60

6A

F4

3E

57

61

6B

F5

3F

58

62

6C

F6


40

59

63

6D

F7

41

5A

64

6E

F8

42

5B

65

6F

F9


43

5C

66

70

F10

44

5D

67

71

F11

85

87

89

8B

F12


86

88

8A

8C

Key

Alone

With
Ctrl Key

Home

47

77

End

4F

75

PgUp


49

84

PgDn

51

76

PrtSc

37

72

Left arrow

4B

73

Rt arrow

4D

74

Up arrow


48

8D

Dn arrow

50

91

Ins

52

92

Del

53

93

Back tab

0F

94

Gray +


4E

90

Gray −

4A

8E


Assembly Language for
x86 Processors
Seventh Edition

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

Boston Columbus Indianapolis New York San Francisco Upper Saddle River
Amsterdam Cape Town Dubai London Madrid Milan Munich Paris Montreal Toronto
Delhi Mexico City São Paulo Sydney Hong Kong Seoul Singapore Taipei Tokyo


Vice President and Editorial Director, ECS: Marcia Horton
Executive Editor: Tracy Johnson
Executive Marketing Manager: Tim Galligan
Marketing Assistant: Jon Bryant
Program Management Team Lead: Scott Disanno
Program Manager: Clare Romeo

Project Manager: Greg Dulles
Senior Operations Specialist: Nick Sklitsis
Operations Specialist: Linda Sager
Permissions Project Manager: Karen Sanatar
Full-Service Project Management: Pavithra Jayapaul, Jouve
Printer/Binder: Courier/Westford
Typeface: Times
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 © 2015, 2011, 2007, 2003 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., 1951Assembly language for x86 processors / Kip R. Irvine, Florida International University,
School of Computing and Information Sciences. — Seventh Edition.
pages cm
ISBN-13: 978-0-13-376940-1

ISBN-10: 0-13-376940-2
1. IBM microcomputers–Programming. 2. X86 assembly language (Computer program
language) I. Title.
QA76.8.I77 2014
005.265—dc23
2013046432

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


To Jack and Candy Irvine


This page intentionally left blank


Contents
Preface

xxiii

1

Basic Concepts

1.1

Welcome to Assembly Language

1.1.1
1.1.2
1.1.3

1.2

7

Section Review 9

Data Representation 9
1.3.1
1.3.2
1.3.3
1.3.4
1.3.5
1.3.6
1.3.7
1.3.8
1.3.9

1.4

1

Questions You Might Ask 3
Assembly Language Applications 6
Section Review 6

Virtual Machine Concept

1.2.1

1.3

1

Binary Integers 10
Binary Addition 12
Integer Storage Sizes 13
Hexadecimal Integers 13
Hexadecimal Addition 15
Signed Binary Integers 16
Binary Subtraction 18
Character Storage 19
Section Review 21

Boolean Expressions 22
1.4.1
1.4.2

Truth Tables for Boolean Functions 24
Section Review 26

1.5

Chapter Summary 26

1.6

Key Terms 27


1.7

Review Questions and Exercises 28
1.7.1
1.7.2

Short Answer 28
Algorithm Workbench 30

2

x86 Processor Architecture

2.1

General Concepts 33
2.1.1
2.1.2

Basic Microcomputer Design 33
Instruction Execution Cycle 34

v

32


vi


Contents

2.1.3
2.1.4
2.1.5

2.2

32-Bit x86 Processors 37
2.2.1
2.2.2
2.2.3
2.2.4

2.3

64-Bit Operation Modes 43
Basic 64-Bit Execution Environment 43

Components of a Typical x86 Computer 44
2.4.1
2.4.2
2.4.3

2.5

Modes of Operation 37
Basic Execution Environment 38
x86 Memory Management 41
Section Review 42


64-Bit x86-64 Processors 42
2.3.1
2.3.2

2.4

Reading from Memory 36
Loading and Executing a Program 36
Section Review 37

Motherboard 44
Memory 46
Section Review 46

Input–Output System 47
2.5.1
2.5.2

Levels of I/O Access 47
Section Review 49

2.6

Chapter Summary 50

2.7

Key Terms 51


2.8

Review Questions 52

3

Assembly Language Fundamentals

3.1

Basic Language Elements 54
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

First Assembly Language Program 54
Integer Literals 55
Constant Integer Expressions 56
Real Number Literals 57
Character Literals 57

String Literals 58
Reserved Words 58
Identifiers 58
Directives 59
Instructions 60
Section Review 63

Example: Adding and Subtracting Integers 63
3.2.1
3.2.2
3.2.3
3.2.4

The AddTwo Program 63
Running and Debugging the AddTwo Program 65
Program Template 70
Section Review 70

53


Contents

3.3

vii

Assembling, Linking, and Running Programs 71
3.3.1
3.3.2

3.3.3

3.4

Defining Data 74
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.4.13

3.5

The Assemble-Link-Execute Cycle 71
Listing File 71
Section Review 73
Intrinsic Data Types 74
Data Definition Statement 74
Adding a Variable to the AddTwo Program 75
Defining BYTE and SBYTE Data 76
Defining WORD and SWORD Data 78
Defining DWORD and SDWORD Data 79

Defining QWORD Data 79
Defining Packed BCD (TBYTE) Data 80
Defining Floating-Point Types 81
A Program That Adds Variables 81
Little-Endian Order 82
Declaring Uninitialized Data 83
Section Review 83

Symbolic Constants 84
3.5.1
3.5.2
3.5.3
3.5.4
3.5.5

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

3.6

64-Bit Programming 88

3.7

Chapter Summary 90

3.8


Key Terms 91
3.8.1
3.8.2

3.9

Terms 91
Instructions, Operators, and Directives 92

Review Questions and Exercises 92
3.9.1
3.9.2

Short Answer 92
Algorithm Workbench 93

3.10 Programming Exercises

94

4

Data Transfers, Addressing, and
Arithmetic 95

4.1

Data Transfer Instructions 96
4.1.1

4.1.2
4.1.3

Introduction 96
Operand Types 96
Direct Memory Operands 96


viii

Contents

4.1.4
4.1.5
4.1.6
4.1.7
4.1.8
4.1.9
4.1.10

4.2

Addition and Subtraction 105
4.2.1
4.2.2
4.2.3
4.2.4
4.2.5
4.2.6
4.2.7

4.2.8

4.3

Indirect Operands 117
Arrays 118
Indexed Operands 119
Pointers 121
Section Review 122

JMP and LOOP Instructions 123
4.5.1
4.5.2
4.5.3
4.5.4
4.5.5
4.5.6

4.6

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

Indirect Addressing 117

4.4.1
4.4.2
4.4.3
4.4.4
4.4.5

4.5

INC and DEC Instructions 105
ADD Instruction 105
SUB Instruction 106
NEG Instruction 106
Implementing Arithmetic Expressions 106
Flags Affected by Addition and Subtraction 107
Example Program (AddSubTest) 111
Section Review 112

Data-Related Operators and Directives 112
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

MOV Instruction 98

Zero/Sign Extension of Integers 99
LAHF and SAHF Instructions 101
XCHG Instruction 102
Direct-Offset Operands 102
Example Program (Moves) 103
Section Review 104

JMP Instruction 123
LOOP Instruction 124
Displaying an Array in the Visual Studio Debugger 125
Summing an Integer Array 126
Copying a String 127
Section Review 128

64-Bit Programming 128
4.6.1
4.6.2
4.6.3
4.6.4

MOV Instruction 128
64-Bit Version of SumArray 130
Addition and Subtraction 130
Section Review 131


Contents

ix


4.7

Chapter Summary 132

4.8

Key Terms 133
4.8.1
4.8.2

4.9

Terms 133
Instructions, Operators, and Directives 133

Review Questions and Exercises 134
4.9.1
4.9.2

Short Answer 134
Algorithm Workbench 136

4.10 Programming Exercises 137

5

Procedures

5.1


Stack Operations 140
5.1.1
5.1.2
5.1.3

5.2

Background Information 154
Section Review 155

The Irvine32 Library 155
5.4.1
5.4.2
5.4.3
5.4.4
5.4.5

5.5

PROC Directive 145
CALL and RET Instructions 147
Nested Procedure Calls 148
Passing Register Arguments to Procedures 150
Example: Summing an Integer Array 150
Saving and Restoring Registers 152
Section Review 153

Linking to an External Library 153
5.3.1
5.3.2


5.4

Runtime Stack (32-bit mode) 140
PUSH and POP Instructions 142
Section Review 145

Defining and Using Procedures 145
5.2.1
5.2.2
5.2.3
5.2.4
5.2.5
5.2.6
5.2.7

5.3

139

Motivation for Creating the Library 155
Overview 157
Individual Procedure Descriptions 158
Library Test Programs 170
Section Review 178

64-Bit Assembly Programming 178
5.5.1
5.5.2
5.5.3

5.5.4

The Irvine64 Library 178
Calling 64-Bit Subroutines 179
The x64 Calling Convention 179
Sample Program that Calls a Procedure 180

5.6

Chapter Summary 182

5.7

Key Terms 183
5.7.1
5.7.2

Terms 183
Instructions, Operators, and Directives 183


x

5.8

Contents

Review Questions and Exercises 183
5.8.1
5.8.2


Short Answer 183
Algorithm Workbench 186

5.9

Programming Exercises 187

6

Conditional Processing

6.1

Conditional Branching 190

6.2

Boolean and Comparison Instructions 190
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.2.11


6.3

Block-Structured IF Statements 210
Compound Expressions 213
WHILE Loops 214
Table-Driven Selection 216
Section Review 219

Application: Finite-State Machines 219
6.6.1
6.6.2
6.6.3

6.7

LOOPZ and LOOPE Instructions 209
LOOPNZ and LOOPNE Instructions 209
Section Review 210

Conditional Structures 210
6.5.1
6.5.2
6.5.3
6.5.4
6.5.5

6.6

Conditional Structures 199

Jcond Instruction 200
Types of Conditional Jump Instructions 201
Conditional Jump Applications 204
Section Review 208

Conditional Loop Instructions 209
6.4.1
6.4.2
6.4.3

6.5

The CPU Status Flags 191
AND Instruction 191
OR Instruction 192
Bit-Mapped Sets 194
XOR Instruction 195
NOT Instruction 196
TEST Instruction 196
CMP Instruction 197
Setting and Clearing Individual CPU Flags 198
Boolean Instructions in 64-Bit Mode 199
Section Review 199

Conditional Jumps 199
6.3.1
6.3.2
6.3.3
6.3.4
6.3.5


6.4

189

Validating an Input String 219
Validating a Signed Integer 220
Section Review 224

Conditional Control Flow Directives 225
6.7.1
6.7.2
6.7.3
6.7.4

Creating IF Statements 226
Signed and Unsigned Comparisons 227
Compound Expressions 228
Creating Loops with .REPEAT and .WHILE 231


Contents

xi

6.8

Chapter Summary 232

6.9


Key Terms 233
6.9.1
6.9.2

Terms 233
Instructions, Operators, and Directives 234

6.10 Review Questions and Exercises 234
6.10.1 Short Answer 234
6.10.2 Algorithm Workbench 236

6.11 Programming Exercises 237
6.11.1 Suggestions for Testing Your Code 237
6.11.2 Exercise Descriptions 238

7

Integer Arithmetic

7.1

Shift and Rotate Instructions 243
7.1.1
7.1.2
7.1.3
7.1.4
7.1.5
7.1.6
7.1.7

7.1.8
7.1.9
7.1.10

7.2

Shifting Multiple Doublewords 252
Binary Multiplication 253
Displaying Binary Bits 254
Extracting File Date Fields 254
Section Review 255

Multiplication and Division Instructions 255
7.3.1
7.3.2
7.3.3
7.3.4
7.3.5
7.3.6
7.3.7

7.4

Logical Shifts and Arithmetic Shifts 243
SHL Instruction 244
SHR Instruction 245
SAL and SAR Instructions 246
ROL Instruction 247
ROR Instruction 247
RCL and RCR Instructions 248

Signed Overflow 249
SHLD/SHRD Instructions 249
Section Review 251

Shift and Rotate Applications 251
7.2.1
7.2.2
7.2.3
7.2.4
7.2.5

7.3

242

MUL Instruction 255
IMUL Instruction 257
Measuring Program Execution Times 260
DIV Instruction 262
Signed Integer Division 264
Implementing Arithmetic Expressions 267
Section Review 269

Extended Addition and Subtraction 269
7.4.1
7.4.2
7.4.3
7.4.4

ADC Instruction 269

Extended Addition Example 270
SBB Instruction 272
Section Review 272


xii

7.5

Contents

ASCII and Unpacked Decimal Arithmetic 273
7.5.1
7.5.2
7.5.3
7.5.4
7.5.5

7.6

AAA Instruction
AAS Instruction
AAM Instruction
AAD Instruction
Section Review

274
276
276
276

277

Packed Decimal Arithmetic 277
7.6.1
7.6.2
7.6.3

DAA Instruction 277
DAS Instruction 279
Section Review 279

7.7

Chapter Summary 279

7.8

Key Terms 280
7.8.1
7.8.2

7.9

Terms 280
Instructions, Operators, and Directives 280

Review Questions and Exercises 281
7.9.1
7.9.2


Short Answer 281
Algorithm Workbench 282

7.10 Programming Exercises 284

8

Advanced Procedures

8.1

Introduction 287

8.2

Stack Frames 287
8.2.1
8.2.2
8.2.3
8.2.4
8.2.5
8.2.6
8.2.7
8.2.8
8.2.9
8.2.10
8.2.11

8.3


Stack Parameters 288
Disadvantages of Register Parameters 288
Accessing Stack Parameters 290
32-Bit Calling Conventions 293
Local Variables 295
Reference Parameters 297
LEA Instruction 298
ENTER and LEAVE Instructions 298
LOCAL Directive 300
The Microsoft x64 Calling Convention 301
Section Review 302

Recursion 302
8.3.1
8.3.2
8.3.3

8.4

286

Recursively Calculating a Sum 303
Calculating a Factorial 304
Section Review 311

INVOKE, ADDR, PROC, and PROTO 311
8.4.1
8.4.2
8.4.3
8.4.4


INVOKE Directive 311
ADDR Operator 312
PROC Directive 313
PROTO Directive 316


Contents

xiii

8.4.5
8.4.6
8.4.7
8.4.8
8.4.9

8.5

Creating Multimodule Programs 323
8.5.1
8.5.2
8.5.3
8.5.4
8.5.5
8.5.6
8.5.7

8.6


Hiding and Exporting Procedure Names 323
Calling External Procedures 324
Using Variables and Symbols across Module Boundaries 325
Example: ArraySum Program 326
Creating the Modules Using Extern 326
Creating the Modules Using INVOKE and PROTO 330
Section Review 333

Advanced Use of Parameters (Optional Topic) 333
8.6.1
8.6.2
8.6.3
8.6.4

8.7

Parameter Classifications 319
Example: Exchanging Two Integers 320
Debugging Tips 321
WriteStackFrame Procedure 322
Section Review 323

Stack Affected by the USES Operator 333
Passing 8-Bit and 16-Bit Arguments on the Stack 335
Passing 64-Bit Arguments 336
Non-Doubleword Local Variables 337

Java Bytecodes (Optional Topic) 339
8.7.1
8.7.2

8.7.3
8.7.4

Java Virtual Machine 339
Instruction Set 340
Java Disassembly Examples 341
Example: Conditional Branch 344

8.8

Chapter Summary 346

8.9

Key Terms 347
8.9.1
8.9.2

Terms 347
Instructions, Operators, and Directives 348

8.10 Review Questions and Exercises 348
8.10.1 Short Answer 348
8.10.2 Algorithm Workbench 348

8.11 Programming Exercises

349

9


Strings and Arrays

9.1

Introduction 352

9.2

String Primitive Instructions 353
9.2.1
9.2.2
9.2.3
9.2.4
9.2.5
9.2.6

352

MOVSB, MOVSW, and MOVSD 354
CMPSB, CMPSW, and CMPSD 355
SCASB, SCASW, and SCASD 356
STOSB, STOSW, and STOSD 356
LODSB, LODSW, and LODSD 356
Section Review 357


xiv

9.3


Contents

Selected String Procedures 357
9.3.1
9.3.2
9.3.3
9.3.4
9.3.5
9.3.6
9.3.7
9.3.8

9.4

Two-Dimensional Arrays 368
9.4.1
9.4.2
9.4.3
9.4.4
9.4.5

9.5

Str_compare Procedure 358
Str_length Procedure 359
Str_copy Procedure 359
Str_trim Procedure 360
Str_ucase Procedure 363
String Library Demo Program 364

String Procedures in the Irvine64 Library 365
Section Review 368
Ordering of Rows and Columns 368
Base-Index Operands 369
Base-Index-Displacement Operands 371
Base-Index Operands in 64-Bit Mode 372
Section Review 373

Searching and Sorting Integer Arrays 373
9.5.1
9.5.2
9.5.3

Bubble Sort 373
Binary Search 375
Section Review 382

9.6

Java Bytecodes: String Processing (Optional Topic) 382

9.7

Chapter Summary 383

9.8

Key Terms and Instructions 384

9.9


Review Questions and Exercises 384
9.9.1
9.9.2

Short Answer 384
Algorithm Workbench 385

9.10 Programming Exercises 386

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

Defining Structures 391
Declaring Structure Variables 393
Referencing Structure Variables 394
Example: Displaying the System Time 397
Structures Containing Structures 399
Example: Drunkard’s Walk 399
Declaring and Using Unions 403
Section Review 405


10.2 Macros 405
10.2.1 Overview 405
10.2.2 Defining Macros 406
10.2.3 Invoking Macros 407


Contents

xv

10.2.4
10.2.5
10.2.6
10.2.7

Additional Macro Features 408
Using the Book’s Macro Library (32-bit mode only) 412
Example Program: Wrappers 419
Section Review 420

10.3 Conditional-Assembly Directives 420
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


Checking for Missing Arguments 421
Default Argument Initializers 422
Boolean Expressions 423
IF, ELSE, and ENDIF Directives 423
The IFIDN and IFIDNI Directives 424
Example: Summing a Matrix Row 425
Special Operators 428
Macro Functions 431
Section Review 433

10.4 Defining Repeat Blocks 433
10.4.1
10.4.2
10.4.3
10.4.4
10.4.5
10.4.6

WHILE Directive 433
REPEAT Directive 434
FOR Directive 434
FORC Directive 435
Example: Linked List 436
Section Review 437

10.5 Chapter Summary 438
10.6 Key Terms 439
10.6.1 Terms 439
10.6.2 Operators and Directives 439


10.7 Review Questions and Exercises 440
10.7.1 Short Answer 440
10.7.2 Algorithm Workbench 440

10.8 Programming Exercises 442

11 MS-Windows Programming 445
11.1 Win32 Console Programming 445
11.1.1
11.1.2
11.1.3
11.1.4
11.1.5
11.1.6
11.1.7
11.1.8
11.1.9
11.1.10

Background Information 446
Win32 Console Functions 450
Displaying a Message Box 452
Console Input 455
Console Output 461
Reading and Writing Files 463
File I/O in the Irvine32 Library 468
Testing the File I/O Procedures 470
Console Window Manipulation 473
Controlling the Cursor 476



xvi

Contents

11.1.11
11.1.12
11.1.13
11.1.14

Controlling the Text Color 477
Time and Date Functions 479
Using the 64-Bit Windows API 482
Section Review 484

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

Necessary Structures 484
The MessageBox Function 486
The WinMain Procedure 486
The WinProc Procedure 487
The ErrorHandler Procedure 488

Program Listing 488
Section Review 492

11.3 Dynamic Memory Allocation 492
11.3.1
11.3.2

HeapTest Programs 496
Section Review 499

11.4 x86 Memory Management 499
11.4.1
11.4.2
11.4.3

Linear Addresses 500
Page Translation 503
Section Review 505

11.5 Chapter Summary 505
11.6 Key Terms 507
11.7 Review Questions and Exercises 507
11.7.1
11.7.2

Short Answer 507
Algorithm Workbench 508

11.8 Programming Exercises 509


12 Floating-Point Processing and Instruction
Encoding

511

12.1 Floating-Point Binary Representation 511
12.1.1
12.1.2
12.1.3
12.1.4
12.1.5
12.1.6

IEEE Binary Floating-Point Representation 512
The Exponent 514
Normalized Binary Floating-Point Numbers 514
Creating the IEEE Representation 514
Converting Decimal Fractions to Binary Reals 516
Section Review 518

12.2 Floating-Point Unit 518
12.2.1
12.2.2
12.2.3
12.2.4

FPU Register Stack 519
Rounding 521
Floating-Point Exceptions 523
Floating-Point Instruction Set 523



Contents

xvii

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 526
Comparing Floating-Point Values 530
Reading and Writing Floating-Point Values 533
Exception Synchronization 534
Code Examples 535
Mixed-Mode Arithmetic 537
Masking and Unmasking Exceptions 538
Section Review 539

12.3 x86 Instruction Encoding 539
12.3.1
12.3.2
12.3.3
12.3.4
12.3.5

12.3.6
12.3.7

Instruction Format 540
Single-Byte Instructions 541
Move Immediate to Register 541
Register-Mode Instructions 542
Processor Operand-Size Prefix 543
Memory-Mode Instructions 544
Section Review 547

12.4 Chapter Summary 547
12.5 Key Terms 549
12.6 Review Questions and Exercises 549
12.6.1
12.6.2

Short Answer 549
Algorithm Workbench 550

12.7 Programming Exercises 551

13 High-Level Language Interface 555
13.1 Introduction 555
13.1.1
13.1.2
13.1.3
13.1.4

General Conventions 556

.MODEL Directive 557
Examining Compiler-Generated Code 559
Section Review 564

13.2 Inline Assembly Code 564
13.2.1
13.2.2
13.2.3

__asm Directive in Visual C++ 564
File Encryption Example 566
Section Review 569

13.3 Linking 32-Bit Assembly Language Code to C/C++ 570
13.3.1
13.3.2
13.3.3
13.3.4
13.3.5
13.3.6

IndexOf Example 570
Calling C and C++ Functions 574
Multiplication Table Example 576
Calling C Library Functions 579
Directory Listing Program 582
Section Review 583


xviii


Contents

13.4 Chapter Summary 583
13.5 Key Terms 584
13.6 Review Questions 584
13.7 Programming Exercises 585
Chapters are available from the Companion Web site

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

Memory Organization 14.2
Redirecting Input-Output 14.3
Software Interrupts 14.4
INT Instruction 14.5
Coding for 16-Bit Programs 14.6
Section Review 14.7

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

14.2.5

Selected Output Functions 14.9
Hello World Program Example 14.11
Selected Input Functions 14.12
Date/Time Functions 14.16
Section Review 14.20

14.3 Standard MS-DOS File I/O Services 14.20
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

Create or Open File (716Ch) 14.22
Close File Handle (3Eh) 14.23
Move File Pointer (42h) 14.23
Get File Creation Date and Time 14.24
Selected Library Procedures 14.24
Example: Read and Copy a Text File 14.25
Reading the MS-DOS Command Tail 14.27
Example: Creating a Binary File 14.30
Section Review 14.33

14.4 Chapter Summary 14.33

14.5 Programming Exercises 14.35

15 Disk Fundamentals 15.1
15.1 Disk Storage Systems 15.1
15.1.1
15.1.2
15.1.3

Tracks, Cylinders, and Sectors 15.2
Disk Partitions (Volumes) 15.4
Section Review 15.4


Contents

xix

15.2 File Systems 15.5
15.2.1
15.2.2
15.2.3
15.2.4
15.2.5
15.2.6

FAT12 15.6
FAT16 15.6
FAT32 15.6
NTFS 15.7
Primary Disk Areas 15.7

Section Review 15.8

15.3 Disk Directory 15.9
15.3.1
15.3.2
15.3.3
15.3.4

MS-DOS Directory Structure 15.10
Long Filenames in MS-Windows 15.12
File Allocation Table (FAT) 15.14
Section Review 15.14

15.4 Reading and Writing Disk Sectors 15.15
15.4.1
15.4.2

Sector Display Program 15.16
Section Review 15.19

15.5 System-Level File Functions 15.20
15.5.1
15.5.2
15.5.3
15.5.4
15.5.5
15.5.6
15.5.7

Get Disk Free Space (7303h) 15.20

Create Subdirectory (39h) 15.23
Remove Subdirectory (3Ah) 15.23
Set Current Directory (3Bh) 15.23
Get Current Directory (47h) 15.24
Get and Set File Attributes (7143h) 15.24
Section Review 15.25

15.6 Chapter Summary 15.25
15.7 Programming Exercises 15.26

16 BIOS-Level Programming 16.1
16.1 Introduction 16.1
16.1.1

BIOS Data Area 16.2

16.2 Keyboard Input with INT 16h 16.3
16.2.1
16.2.2
16.2.3

How the Keyboard Works 16.3
INT 16h Functions 16.4
Section Review 16.8

16.3 VIDEO Programming with INT 10h 16.8
16.3.1
16.3.2
16.3.3
16.3.4

16.3.5

Basic Background 16.8
Controlling the Color 16.10
INT 10h Video Functions 16.12
Library Procedure Examples 16.22
Section Review 16.23


xx

Contents

16.4 Drawing Graphics Using INT 10h 16.23
16.4.1
16.4.2
16.4.3
16.4.4
16.4.5

INT 10h Pixel-Related Functions 16.24
DrawLine Program 16.25
Cartesian Coordinates Program 16.27
Converting Cartesian Coordinates to Screen Coordinates 16.29
Section Review 16.30

16.5 Memory-Mapped Graphics 16.30
16.5.1
16.5.2
16.5.3


Mode 13h: 320 X 200, 256 Colors 16.30
Memory-Mapped Graphics Program 16.32
Section Review 16.34

16.6 Mouse Programming 16.35
16.6.1
16.6.2
16.6.3

Mouse INT 33h Functions 16.35
Mouse Tracking Program 16.40
Section Review 16.44

16.7 Chapter Summary 16.45
16.8 Programming Exercises 16.46

17 Expert MS-DOS Programming 17.1
17.1 Introduction 17.1
17.2 Defining Segments 17.2
17.2.1
17.2.2
17.2.3
17.2.4
17.2.5

Simplified Segment Directives 17.2
Explicit Segment Definitions 17.4
Segment Overrides 17.7
Combining Segments 17.7

Section Review 17.9

17.3 Runtime Program Structure 17.9
17.3.1
17.3.2
17.3.3
17.3.4

Program Segment Prefix 17.10
COM Programs 17.10
EXE Programs 17.11
Section Review 17.13

17.4 Interrupt Handling 17.13
17.4.1
17.4.2
17.4.3
17.4.4
17.4.5
17.4.6

Hardware Interrupts 17.14
Interrupt Control Instructions 17.16
Writing a Custom Interrupt Handler 17.16
Terminate and Stay Resident Programs 17.19
Application: The No_Reset Program 17.19
Section Review 17.23

17.5 Hardware Control Using I/O Ports 17.23
17.5.1

17.5.2

Input–Output Ports 17.24
PC Sound Program 17.24

17.6 Chapter Summary 17.26


Contents

xxi

Appendix A
Appendix B
Appendix C

MASM Reference 587
The x86 Instruction Set 609
Answers to Section Review
Questions 644

Appendices are available from the Companion Web site

Appendix D
Appendix E
Index

664

BIOS and MS-DOS Interrupts D.1

Answers to Review Questions
(Chapters 14–17) E.1


This page intentionally left blank


×