內容簡介
本書全面細緻地講述了彙編語言程式設計的各個方面。從微處理器體系結構、工作機制到指令集;從最基本的編譯器連結器的使用到高級過程、結構和宏的使用;從用純彙編編寫程式到用VC++、BC++等最新編譯器與彙編的混合接口編程;從16位實模式下BIOS、DOS實模式文本及圖形程式設計到32位保護模式的Windows程式設計;從磁碟基礎知識到Intel指令編碼、浮點運算等相關知識都做了深入而細緻的講解。
本書內容廣博,但又講解得非常深入,是彙編語言課程的很好教材。
圖書目錄
CONTENTS
Preface xix
1 Basic Concepts 1
1.1 W6lcome to Assembly Language 1
1. 1. I Good Questions to Ask 2
1. 1.2 AssemblV Language Applications 5
J LanguageApplications 5
1. I .3 Section Review 6
1.2 Virtual Machine Concept 7
1.2.1 Section Review 9
1.3 Data Representation 9
l.3.1 Binary integers 9
y Integers 9
l.3.2 BinarV Addition I I
y Addition I I
l.3.3 Integer Storage Sizes 12
l.3.4 Hexadecimal integers 13
1.3.5 Signed integers 15
l.3.6 Character Storage 17
l.3.7 Section Review 19
1.4 Boolean Operations 22
l.4. 1 Truth Tables for Boolean Functions 24
1.4.2 Section Review 26
1'5 Chapter Summary 26
1.6 Exercises 27
l.6.1 Programming Tasks 27
l.6.2 Nonprogramming Tasks 27
2 x86 Processor Architecture 29
2.1 General Concepts 29
2.l. 1 Basic Microcomputer Design 30
2. I .2 Instruction Execution CVcle 31
ycle 31
2.1.3 Reading from Memory 33
2.1.4 How Programs Run 34
2.l.5 Section Review 35
v
. ac
VI CONTENTS
2.2 x86 Architecture Details 36
2.2.1 Modes of Operation 36
2.2.2 Basic Execution Environment 36
2.2.3 Floating-Point Unit 39
2.2.4 Overview of intel Microprocessors 39
2.2.5 Section Review 42
2.3 x86 Memory Management 43
2.3.1 Real-Address Mode 43
2.3.2 Protected Mode 45
2.3.3 Section Review 47
2.4 Components of aTypical x86 Computer 48
2.4.1 Motherboard 48
2.4.2 Video Output 50
2.4.3 MemorV 50
J
2.4.4 Input-Output Ports and Device interfaces 50
2.4.5 Section Review 52
2.5 Input-Output System 52
2.5.1 Levels of I/O Access 52
2.5.2 Section Review 55
2.6 Chapter Summary 55
2'7 Chapter Exercises 57
3 Assembly Language Fundamentals 58
3.1 Basic Elements of Assembly Language 58
3. 1. I Integer Constants 59
3.l.2 Integer Expressions 60
3. 1.3 Real Number Constants 61
3. I .4 Character Constants 61
3. l.5 String Constants 61
3.1.6 Reserved WOrds 62
3.l.7 Identifiers 62
3.l.8 Directives 62
3.1.9 Instructions 63
3. 1. 10 The NOP (No Operation) Instruction 65
3. 1. I I Section Review 66
3.2 Example: Adding and Subtracting Integers 66
3.2. I Alternative Version of AddSub 69
3.2.2 Program Template 70
3.2.3 Section Review 70
3.3 Assembling, Linking, and Running Programs 71
3.3.1 The Assemble-Link-Execute Cycle 71
3.3.2 Section Review 77
CONTENTS Vii
3.4 Defining Data 77
3.4.1 Intrinsic Data Types 77
apes 77
3.4.2 Data Definition Statement 77
3.4.3 Defining BYTE and SBYTE Data 78
3.4.4 Defining WORD and SWORD Data 80
3.4.5 Defining DWORD and SDWORD Data sl
3.4.6 Defining QWORD Data sl
3.4.7 Defining Packed Binary Coded Decimal (TBYTE) Data 82
3.4.8 Defining Real Number Data 83
3.4.9 Little Endian Order 83
3.4.10 Adding Vocables to the AddSub Program 84
3.4.1 1 Decledng Uninitialized Data 85
3.4.12 Section Review 85
3.5 Symbolic Constants 86
3.5.1 Equal-Sign Directive 86
i elf Directive 86
3.5.2 Calculating the Sizes of Arrays and Strings 87
3.5.3 EQU Directive 88
3.5.4 TEXTEQU Directive 89
3.5.5 Section Review 90
3.6 Real-Address Mode Programming (Optional) 90
3.6.1 Basic Changes 90
3.7 Chapter Summary 91
3.8 Programming Exercises 92
4 Data Transfers, Addressing,
. -.....
and Arithmetic 94
4.1 DataTFansfer Instructions 94
4. 1. I Introduction 94
4.l.2 Operand Types 95
4.l.3 Direct Memory ODerands 96
j perands 96
4.l.4 MOV Instruction 96
4. l.5 Zero/Sign Extension of integers 98
4.1.6 LAHF and SAHF Instructions 100
4.1.7 XCHG Instruction 100
4.1.8 Direct-Offset Operands 101
4.l.9 Example Program (Moves) 102
4.l.10 Section Review 103
4.2 Addition and Subtraction 104
4.2.1 INC and DEC Instructions 104
4.2.2 ADD Instruction 104
4.2.3 SUB InstrUction 105
4.2.4 NEG InstrUction 105
...
Vlll CONTENTS
4.2.5 ImplementingArithmetic Expressions 106
4.2.6 FlaZs Affected by Addition and Subtraction 106
o> Affected by Addition and Subtraction 106
4.2.7 Example Program (AddSub3) 1 10
4.2.8 Section Review I I I
4.3 Data-Related Operators and Directives 112
4.3.1 OFFSET Operator 112
4.3.2 ALIGN Directive 1 13
4.3.3 PTR Operator 114
4.3.4 TYPE Operator 115
perator 1 15
4.3.5 LENGTHOF Operator 115
4.3.6 SIZEOF Operator 116
4.3.7 LABEL Directive 116
4.3.8 Section Review 117
4.4 Indirect Addressina 117
9 1 17
4.4. 1 Indirect Operands 1 18
4.4.2 ArraVs 119
ys 119
4.4.3 Indexed Operands 120
4.4.4 Pointers 121
4.4.5 Section Review 123
4.5 JMP and LOOP Instructions 124
4.5.1 JMP Instruction 124
4.5.2 LOOP InstrUction 124
4.5.3 Summing an integerArray 126
4.5.4 Copying a String 126
4.5.5 Section Review 127
4'6 Chapter Summary 128
4.7 Proarammina Exercises 129
cramming Exercises 129
5 Procedures 132
5.1 Introduction 132
5.2 Linkina to an External Library 132
s to an External Library 132
5.2.1 Background information 133
5.2.2 Section Review 134
5.3 The Book's Link Library 134
y 134
5.3.1 Overview 136
5.3.2 Individual Procedure Descriptions 137
ptions 137
5.3.3 Library Test Programs 149
J iest Programs 149
5.3.4 Section Review 157
5'4 Stack Operations 157
5.4.1 Runtime Stack 158
5.4.2 PUSH and POP Instructions 160
5.4.3 Section Review 162
CoNTENTS iX
5.5 Defining and Using Procedures 163
5.5.1 PROC Directive 163
5.5.2 CALL and RET Instructions 165
5.5.3 Example: Summing an integerArray 168
pie: Summing an integer Array 168
5.5.4 Flowcharts 169
5.5.5 Saving and Restoring Registers 170
5.5.6 Section Review 171
5.6 Program Design Using Procedures 172
5.6.1 Integer Summation Program (Design) 173
5.6.2 Integer Summation Implementation 175
5.6.3 Section Review 177
5.7 Chapter Summary 177
5.8 Programming Exercises 178
6 Conditional Processing 180
6.1 Introduction 180
6.2 Boolean and Comparison instructions 181
6.2.1 The CPU Flags 182
6.2.2 AND InstrUction 182
6.2.3 OR InstrUction 183
6.2.4 Bit-Mapped Sets 184
6.2.5 XOR Instruction 186
6.2.6 NOT Instruction 187
6.2.7 TEST Instruction 187
6.2.8 CMP InstrUction 188
6.2.9 Setting and Clearing individual CPU Flags 189
6.2.10 Section Review 189
6.3 Conditional Jumps 190
6.3.1 Conditional Stttictures 190
6.3.2 Jcond instruction 191
6.3.3 Types of Conditional Jump instructions 192
ypes of Conditional Jump instructions 192
6.3.4 Conditional JumpApplications 195
6.3.5 Section Review 199
6'4 Conditional Loop Instructions 200
6.4.1 LOOPZ and LOOPE Instructions 200
6.4.2 LOOPNZ and LOOPNE Instructions 201
6.4.3 Section Review 201
6'5 Conditional Structures 202
6.5.1 Block-Structured iF Statements 202
6.5.2 Compound Expressions 204
pound Expressions 204
6.5.3 WHILE Loops 206
6.5.4 Table-Driven Selection 208
6.5.5 Section Review 210
X CONTENTS
6.6 Application: Finite-State Machines 211
6.6.1 Validating an input String ZI I
6.6.2 Validating a Signed integer 212
6.6.3 Section Review 216
6'7 Conditional Control Flow Directives 217
6.7. I Creating iF Statements 218
6.7.2 Signed and Unsigned Compedsons 219
6.7.3 Compound Expressions 220
6.7.4 Creating Loops with .REPEAT and .WHILE 223
6'8 Chapter Summary 224
6.9 Proarammina Exercises 225
cramming Exercises 225
7 IntegerArithmetic 229
7.1 Introduction 229
7'2 Shift and Rotate Instructions 230
7.2.1 Logical Shifts and Arithmetic Shifts 230
7.2.2 SHL Instruction 231
7.2.3 SHR InstrUction 232
7.2.4 SAL and SAR Instructions 233
7.2.5 ROL Instruction 234
7.2.6 ROR Instruction 235
7.2.7 RCL and RCR Instructions 235
7.2.8 Signed Overflow 236
7.2.9 SHLDISHRD Instructions 236
7.2.10 Section Review 238
7'3 Shift and Rotate Applications 239
7.3.1 Shifting Multiple Doublewords 240
7.3.2 Binary MultiDlication 241
j Multiplication 241
7.3.3 Displaying Binary Bits 242
7.3.4 Isolating MS-DOS File Date Fields 242
7.3.5 Section Review 243
7.4 Multiplication and Division Instructions 243
7.4.1 MUL InstrUction 243
7.4.2 IMUL InstrUction 245
7.4.3 Measuring Program Execution Times 247
7.4.4 DIV Instruction 249
7.4.5 Signed integer Division 250
7.4.6 ImplementingArithmetic Expressions 253
7.4.7 Section Review 255
7.5 Extended Addition and Subtraction 256
7.5.1 ADC Instruction 256
7.5.2 ExtendedAddition Example 257
CONTENTS Xi
7.5.3 SBB InstrUction 258
7.5.4 Section Review 259
7.6 ASCll and Unpacked Decimal Arithmetic 260
7.6.1 AAA InstrUction 261
7.6.2 AAS InstrUction 262
7.6.3 AAM Instruction 263
7.6.4 AAD InstrUction 263
7.6.5 Section Review 264
7.7 Packed Decimal Arithmetic 264
7.7.1 DAA Instruction 264
7.7.2 DAS Instruction 266
7.7.3 Section Review 266
7.8 Chapter Summary 266
7.9 Programming Exercises 267
8 Advanced Procedures 270
8.1 Introduction 270
8.2 Stack Frames 271
8.2.1 Stack Parameters 272
8.2.2 Accessing Stack Parameters 273
8.2.3 Local Vocables 281
8.2.4 ENTER and LEAVE Instructions 285
8.2.5 LOCAL Directive 286
8.2.6 Section Review 289
8.3 Recursion 290
8.3.1 Recursively Calculating a Sum 291
8.3.2 Calculating a Factorial 292
8.3.3 Section Review 298
8.4 INVOKE, ADDR, PROC, and PROTO 299
8.4.1 INVOKE Directive 299
8.4.2 ADDR Operator 300
8.4.3 PROC Directive 301
8.4.4 PROTO Directive 304
8.4.5 Parameter Classifications 307
8.4.6 Example: Exchanging Two integers 307
8.4.7 DebuggingTips 308
8.4.8 WriteStackFrame Procedure 309
8.4.9 Section Review 310
8.5 Creating Multimodule Programs 311
8.5. I Hiding and Exporting Procedure Names 31 1
8.5.2 Calling External Procedures 312
..
Xll CONTENTS
8.5.3 Using Variables and Symbols across Module Boundaries 313
8.5.4 Example: ArraySum Program 314
8.5.5 Creating the Modules Using Extem 314
8.5.6 Creating the Modules Using INVOKE and PROTO 318
8.5.7 Section Review 321
8.6 Java Bytecodes 321
8.6. I Java Virtual Machine 321
8.6.2 Instruction Set 322
8.6.3 Java DisassemblV Examples 323
J examples 323
8'7 Chapter Summary 328
8.8 Programmina Exercises 329
cramming Exercises 329
9 Strings and Arrays 332
9.1 Introduction 332
9'2 String Primitive Instructions 333
9.2.1 MOVSB, MOVSW, and MOVSD 334
9.2.2 CMPSB, CMPSW, and CMPSD 335
9.2.3 SCASB, SCASW, and SCASD 336
9.2.4 STOSB, STOSW, and STOSD 336
9.2.5 LODSB, LODSW, and LODSD 337
9.2.6 Section Review 337
9'3 Selected Strina Procedures 338
s Procedures 338
9.3.1 Sir comDare Procedure 338
-- pare Procedure 338
9.3.2 Sir length Procedure 339
~ o
9.3.3 Sir copy Procedure 340
-- py Procedure 340
9.3.4 Sir trim Procedure 340
9.3.5 Sir "case Procedure 343
9.3.6 String Library Demo Program 344
9.3.7 Section Review 346
9.4 Two-Dimensional Arrays 346
ys 346
9.4.1 Ordering of Rows and Columns 346
9.4.2 Base-Index Operands 347
9.4.3 Base-Index-Displacement Operands 349
9.4.4 Section Review 350
9'5 Searching and Sorting Integer Arrays 350
9.5.1 Bubble Sort 350
9.5.2 BinarV Search 352
J search 352
9.5.3 Section Review 359
9.6 Java Bytecodes: String Processing 359
9'7 Chapter Summary 360
9.8 Proarammina Exercises 361
cramming Exercises 361
CONTENTS Xiii
10 Structures and Macros 366
10.1 Structures 366
10.l.l Defining Structures 367
10.l.2 Decledng Structure Variables 368
10.1.3 Referencing Structure Variables 370
10.l.4 Example: Displaying the System Time 372
10.1.5 Structures Containing Structures 375
10.l.6 Example f Drunkard's Walk 375
10.l.7 Decledng and Using Unions 378
10.1.8 Section Review 381
10.2 Macros 382
10.2.1 Overview 382
10.2.2 Defining Macros 382
10.2.3 Involdng Macros 383
10.2.4 Additional Macro Features 384
10.2.5 Using the Book's Macro Library 388
10.2.6 ExamDle Program: Wrappers 394
pie Program: Wrappers 394
10.2.7 Section Review 395
10.3 Conditional-Assembly Directives 396
10.3.1 Checking for Missing Arguments 397
10.3.2 DefaultArgument initialliers 398
10.3.3 Boolean Expressions 399
10.3.4 IF, ELSE, and ENDIF Directives 399
10.3.5 The IFIDN and IFIDNI Directives 400
10.3.6 Example: Summing a Matrix Row 401
10.3.7 Special Operators 404
10.3.8 Macro Functions 407
10.3.9 Section Review 409
10.4 Defining Repeat Blocks 410
10.4.1 WHILE Directive 410
10.4.2 REPEAT Directive 410
10.4.3 FOR Directive 4if
10.4.4 FORC Directive 412
10.4.5 Example: Linked List 412
10.4.6 Section Review 414
10'5 Chapter Summary 415
10.6 Programming Exercises 416
11 MS-Windows Programming 419
11.1 Win32 Console Programming 419
l 1. 1. I Background information 420
l 1. l.2 Win32 Console Functions 424
l 1. 1.3 Displaying a Message Box 426
.
XIV CONTENTS
l 1. l.4 Console input 429
l 1. 1.5 Console Output 435
l 1. I .6 Reading and Writing Files 437
1 1. 1.7 File I/O in the lrvine32 LibrarV 442
, F42
l 1. I .8 Testing the File I/O Procedures 444
l 1. I .9 Console Window Manipulation 447
1 1. 1. 10 Controlling the Cursor 450
l 1. 1. I I Controlling the Text Color 451
l 1. 1. 12 Time and Date Functions 453
1 1. 1. 13 Section Review 456
11 .2 Writina a GraDhical Windows Apolication 457
s a Graphical Windows Application 457
l l.2.1 Necessary Structures 458
; structures 458
l 1.2.2 The MessageBox Function 459
11.2.3 The WinMain Procedure 460
l l.2.4 The Winproc Procedure 460
l I .2.5 The ErrorHandler Procedure 461
1 1.2.6 Program Listing 461
.ram Listing 461
l l.2.7 Section Review 465
11.3 Dynamic Memory Allocation 466
ynamic Memory Allocation 466
l l.3. I HeapTest Programs 469
l l.3.2 Section Review 473
11.4 x86 Memory Manaaement 473
y Management 473
l 1 .4. 1 Linear Addresses 473
l l.4.2 Page Translation 477
11.4.3 Section Review 479
11'5 Chapter Summary 479
11.6 Proqrammina Exercises 481
cramming Exercises 481
12 Floatinq-Point Processina and Instruction
u Point Processing and Instruction
Encodina 483
9 483
12.1 Floatina-Point Binary Reoresentation 483
a-Point Binary Representation 483
12. 1. I IEEE BinarV Floating-Point Representation 484
; floating-Point Representation 484
12. l.2 The Exponent 485
12. l.3 Normalized BinarV Floating-Point Numbers 486
y Floating-Point Numbers 486
12. I .4 Creating the IEEE Representation 486
a presentation 486
12. l.5 Converting Decimal Fractions to Binary Reals 488
a Decimal Fractions to Binary Reals 488
12.l.6 Section Review 490
12.2 Floating-Point Unit 490
s-Point Unit 490
12.2.1 FPU Register Stack 491
12.2.2 Rounding 493
12.2.3 Floating-Point Exceptions 495
12.2.4 Floating-Point instruction Set 495
CONTENTS XV
12.2.5 Arithmetic instructions 498
12.2.6 Comparing Floating-PointValues 502
12.2.7 Reading and Writing Floating-PointValues 504
12.2.8 Exception Synchronization 506
12.2.9 Code Examples 507
12.2.10 Mixed-ModeArithmetic 508
12.2. I I Masking and Unmasking Exceptions 509
12.2. 12 Section Review 51 1
12.3 x86 Instruction Encoding 512
12.3. I Instruction FOrmat 512
12.3.2 Single-Byte instructions 513
12.3.3 Move Immediate to Register 514
12.3.4 Register-Mode instructions 514
12.3.5 Processor Operand-Size Prefix 515
perand-Size Prefix 515
12.3.6 MemorV-Mode instructions 516
J
12.3.7 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.l.2 .MODEL Directive 527
13. l.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
yption Example 532
13.2.3 Section Review 535
13.3 Linking to CIC++ in Protected Mode 535
13.3. I Using Assembly Language to Optimize C++ Code 536
13.3.2 Calling C and C++ Functions 542
13.3.3 Multiplication Table Example 544
13.3.4 Calling C Library Functions 547
13.3.5 Directory Listing Program 550
; Listing Program 550
13.3.6 Section Review 552
13.4 Linking to CIC++ in Real-Address Mode 552
13.4.1 Linking to Borland C++ 553
13.4.2 ReadSector Example 554
13.4.3 Example: Large Random integers 558
13.4.4 Section Review 559
13.5 Chapter Summary 560
13.6 Programming Exercises 560
.
XVI CONTENTS
14 16-Bit MS-DOS Programming 562
14.1 MS-DOS and the 18M-PC 562
14. l.l MemorV OrZanization 563
y Organization 563
14.l.2 Redirecting input-Output 564
14.l.3 Software interrupts 565
14.l.4 INT Instruction 565
14.l.5 Coding for 16-Bit Programs 567
14.l.6 Section Review 568
14.2 MS-DOS Function Calls (INT 21h) 568
14.2.1 Selected Output Functions 570
14.2.2 Hello World Program Example 572
14.2.3 Selected input Functions 573
14.2.4 Date/Time Functions 577
14.2.5 Section Review 581
14.3 Standard MS-DOS File ilO Services 581
14.3.1 Create or Open File (716Ch) 583
pen File (716Ch) 583
14.3.2 Close File Handle (3Eh) 584
14.3.3 Move File Pointer (42h) 584
14.3.4 Get File Creation Date and Time 585
14.3.5 Selected Library Procedures 585
J
14.3.6 Example. Read and Copy a Text File 586
pie' Read and Copy a Text File 586
14.3.7 Reading the MS-DOS Command Tail 588
14.3.8 Example: Creating a Binary File 591
14.3.9 Section Review 594
14'4 Chapter Summary 594
14.5 Proaramminq Exercises 596
.ramming Exercises 596
Chapters are avail8bIS from the COmP8nion Web sitS
15 Disk Fundamentals
16 BIOS-Level Programming
17 Expert MS-DOS Programming
Appendix A MASM Reference 598
Appendix B The x86 Instruction Set 620
Appendix C Answers to Review Questions 655