关于我们
精品教材 更多
规划教材 更多
|
IBM PC汇编语言程序设计(第5版)
本书主要介绍用汇编语言进行程序设计,先从硬件和软件着手,然后介绍编程所需的各种指令,并给出丰富的范例。全书分为七大部分:PC机硬件、软件基础,汇编语言基础,屏幕与键盘操作,数据操作,高级输入/输出,特别主题,以及关于BIOS数据区、中断与端口、运算符与指令、PC指令系统等的参考资料。
本书既可作为计算机及相关专业汇编语言课程的教材,也可作为开发人员的参考用书。要掌握本书内容,不仅要认真阅读本书的每个章节,还应使用本书提供的范例并尝试运行这些范例,并认真完成每章后面的练习。
pREfaCE
ac k Yhe heart of a personal computer is a microprocessor, which handles the computer's re. . quirements for arithmetic, logic, and control. The microprocessor had its origin in the 1960s, when research designers devised the integrated circuit (IC) by combining various electronic components into a single component on a silicon "chip." In the early 1970s intel ponents into a single component on a silicon "chip." In the early 1970s intel . introduced the 8008 chip, which ushered in the first generation of microprocessors. .. By 1974 the 8008 had evolved into the 8080. a nonular second-generation microJ, a popular second-generation micro. processor with general-purpose use. In 1978 Intel produced the third-generation 8086 ,.,'.. processor, which represented a significant advance in its design. The 8088, a variation of the 8086, provided a slightly simpler design and compatibility with then-current inpuUout. put devices. The 8088 was selected by IBM in 1981 for its forthcoming personal computed Enhanced versions of the 8086 include the 80286, 80386, 80486, Pentium, and other more advanced Pentium and Celeron models, each of which provides additional processing '. power Each familV of processors has its own unique set of instructions that are used to diy of processors has its own unique set of instructions that are used to direct its operations, such as accept input from a keyboard, display data on a screen, and per'. form arithmetic. This set of instructions (the machine language) is too complex and obscure for use in the development of programs. Software suppliers provide an assembly language for the processor family that represents the various instructions in more understandable . symbolic code. LEVELS OF PROGRAMMING The levels of programming languages are the following f . . 11 birin i. jffochine language consists of the individual instructions that the processor executes I .' one at a time; these are embedded in the operating system and in the low-level perating system and in the low-level ',. readV-onlV fortions of the machine architecture. j J portions of the machine architecture. .. Pace~ ac xii Preface ., I I rsvv all) lanqltaee is desiZI Low-level assembly language is designed for a specific family of processors; the symbolic instructions directly relate to machine language instructions one-for-one and are ,,,. 1. 1 assembled into machine lanZllocre. assembled into machine language. . bios-level fang'lrsqes such as C. C++, and Visual BASIC were designed to eliminate High-level languages such as C, C++, and Visual BASIC were designed to eliminate - -. -.. the technicalities of a reticular commuter; statements compiled in a high-level lanpndicular computer; statements compiled in a high-level fan. - 1' 1,.. guage typically generate many low-level instructions. ADVANTAGES OF ASSEMBLY LANGUAGE A knowledge and use of assembly language includes these advantages: . Shows how DroZrams interface with the operating system, the processor, and BIOS. programs interface with the operating system, the processor, and BIOS. . q,, 1. 1. 1 1. 1 1 1. . shows how data is reDresented 'and stored in memorV and on external devices. presented and stored in memory and on external devices. . Clannes how the Drocessor accesses and executes instructions and how instructions processor accesses and executes instructions and how instructions 1 1 o 1 rim laal access and process data. . Clarifies how a program accesses external devices. As well, reasons for using assembly language include the following: . A riroarqrn written in assembl\l languaZe rca n program written in assembly language requires considerably less memory and ..,..'., 1 1 1 execution time than one written in a high-level language. . Assembl\l language Zives a DroZrqmmer the ability HssemblV language gives a programmer the ability to perform highly technical tasks J. buage gives a programmer the ability to perform highly technical tasks - -, 1,. rt~'. that would be difficult, if not impossible, in a high-level language. . A ithouqh most software sDecialists develoD new aDnlications in hiZh-l although most software specialists develop new applications in high-level languages, .if most software specialists develop new applications in high-level languages, ,.'.. 1.... 1. all T which are easier to write and maintain, a common practice is to recode in assembly language those sections that are time-critical. n. 1 . resident Drograms (that reside in memory while other programs execute) and programs (that reside in memory while other programs execute) and ... interrupt service routines (that handle input and output) are almost always developed in assembly language. pod in assembly language. The following material is required for learning PC assembly language: . Access to an IBM Dersonal comDuter (an\l model) or eauivalent compatible. access to an IBM personal computer (any model) or equivalent compatible. . A cODa! of the Windows 95/98 or DOS oDeratinZ s\lstem and familiaritV with its use. H copy of the Windows 95/98 or DOS operating system and familiarity with its use. It is much easier to learn the intricacies of assembly language while working within ,. 1.,. 1., ~ ac R 1 1. k.' o afl acid' l,r simDlo afl a relatively simDle oDerating system like DOS rather than within the Windows enviy dimple operating system like DOS rather than within the Windows enviX v T... ronment. within DOS, you can freely experiment and can later step up to the Windows environment. . A nririx! of an assembler translator DroZrqm. Common suDnliers include Microsoft, H copy of an assembler translator program. Common suppliers include Microsoft, py of an assembler translator program. Common suppliers include Microsoft, Borland, and SLR Systems. The following are not required for learning assembly language: n.,, 1 c., if I k k 1.. 1. . Prior knowledZe of a Drogra~ng language, although such knowledge may help a progra~ng language, although such knowledge may help . 1., you grasp some programming concepts more readily. n.11'cl...m 1.,1. 11 If k. . Prior knowledge of electronics or circuitfV. This book Drovides all the information l nor knowledge of electronics or circuitry. This book provides all the information ,, n rib h DC's architecture that im'1 recall about the PC's architecture that Von require for programming in assembly language. ; luire for programming in assembly language. The Approach ioTa he xiii FOCUS OF THIS BOOK To assist readers in learning assembly language programming, this book first covers the . 1 simpler aspects of the hardware and the language and then introduces instructions as they ' 1 1. are needed. As well, the text emphasizes claritV in Drogram examDles. Thus the examDles phasizes clarity in program examples. Thus the examples use those instructions and approaches that are the eastest to understand, even though a pro' fessional programmer would often solve similar problems with more sODhisticated--but programmer would often solve similar problems with more sophisticated--but less clear-ode. ac 1... Fhe programs also omit macro instructions (explained in ChaDter ZI); although proprograms also omit macro instructions (explained in Chapter ZI); although professional programmers use macros extensively, their appearance in a book of this nature programmers use macros extensively, their appearance in a book of this nature would interfere with learning the principles of the language. Once you have learned these .. 1, 1' principles, you can then adopt the techniques of the professional. THE APPROACH TO TAKE This book can act as both a tutorial and a reference. To make the most effective use of your .. investment in a PC and software, work through each chapter carefully and reread any material that is not immediatelV clear. Use the nroZram examDles and Zet them to execute (or y clear. Use the program examples and get them to execute (or "run") on your computer. Also, be sure to work through the exercises at the end of each chapter. ac G-st eig k' L Yhe first eight chapters furnish the foundation material for assembly language. Af.ilt chapters furnish the foundation material for assembly language. Af. ter studying these chapters, you can proceed with Chapters 9, I l, 12, 14, 15, 16, 20, 21, ding these chapters, you can proceed with Chapters 9, I l, 12, 14, 15, 16, 20, 21, or 22. Chapters 24 through 26 are intended as references. Chapters related to each other .. are: o,''' . 8 through 10 (screen and he'lboard operations) .if 10 (screen and keyboard operations) ' rye 1 ry,/.,..' . 1 7 and 13 (arithmetic operations) ., perations) , . 1 6 through 19 (disk nrocessinZI J 6 through 19 (disk processing) .if 19 (disk processing) ry rye ry ac . ), qnd,2 tR,lhrir. -- and 23 (subprograms and overlays) On completing this book, you will be able to: ' T T,,,', rt', . Understand the hardware of the personal comDUteL personal computed T T,''. . Understand machine-languaZe code and hexadecimal format. buage code and hexadecimal format. X T 1,,. 1 1. . Understand the steps involved in assemblinZ linkinZ and executinZ a DroZrql ps involved in assembling, linking, and executing a program. ' ac T..,, 1',,' 1,', . Write programs in assembly language to handle the keyboard and screen, perform . ... riwifh~oti k arithmetic, convert between ASCll and binarV formats, perform table searches and j iormats, perform table searches and . sorts, and handle disk input and output. I put. ~'...,. . brace machine execution as an aid in DroZrqm debuZqir trace machine execution as an aid in program debugging. program debugging. Ic T. . Write Vour own macro instructions to facilitate faster codinZ your own macro instructions to facilitate faster coding. . Link separately assembled programs into one executable program. l i programs into one executable program. Learning assembly language and getting your programs to work is an exciting and challenging experience. For the time and effort invested, the rewards are sure to be great. - rs. r xiv Preface NOTES ON THE FIFTH EDITION This fifth edition reflects a considerable number of enhancements to the previous edition, . 1,., n, 1. including the following f b the following f . More features of the intel Pentium Drocessors .Acre features of the intel Pentium processors ' x,,. . More DroZram examDles and exercises iAore Drogram examples and exercises program examples and exercises ~,..,... . earlier introduction to intemiDt ODerations barner introduction to intemipt operations . Considerable reorganization and revision of explanations throughout the text . More material on Drotected mode, passinZ Darameters. the use of the stack. address.Aore material on protected mode, passing parameters, the use of the stack, address...'. "NT 10H functions.' '",, lug modes, video systems and INT 10H functions, array handling, subprograms, and o Inodes, video systems and INT 10H functions, array handling, subprograms, and pofts n.,,,,..,.,, n, 1 . Revised and additional auestions at the end of each chaDteL ,uestions at the end of each chapter Users of the fourth edition should note that the contents of Chapter 7 (.COM Pro\,,,.,.,.,..., grams) has been combined in this edition with Chapter 5. Also, Chapter ZI has been dis,.,.,.,,, 1.., persed in this way f the material on mouse handling to its own chapter (15) and the rest of the chapter (ports, string I/O, and sound) combined with BIOS and program intemipts in Chapter 24. Note to the Student/Reader: Check out the web site for answers to selected questions, programs from the book for downloading, and questions for trying out. The address is ,, 1 I,, www.prenhall.condabel Note to the instructor; An instructors' CD with software, additional problems, and solutions is available for adopters of the book. ACknowIedaments sments The author is grateful for the assistance and cooperation of all those who contributed suggestions for, reviews of, and corrections to earlier editions.
CONTENTS
PREr^cE - yi xI cart A FundamentalS of PC Hardware and SOfware 1 l BASIC FE^rURES OF PC HARDw^RE 1 Introduction 1 Bits and BVtes 1 ytes 1 ac o: Number SI foe Binary Number System 3 j I yotem 3 Hexadecimal Representation 6 presentation 6 ASCll Code 7 PC ComDonents 8 ponents 8 Internal Memory 10 j 10 Rocrmporits and AddressinZ 1, begments and Addressing 12 .Inents and Addressing 12 Registers 13 .lsters 13 Hardware interrupts 18 KeV Points ] 9 y Points 19 Review Questions and Exercises 19 2 INSTRUCTION ADDREssiNG AND EXECUTION 22 Introduction 22 Features of an Operating System 22 perating System 22 . Contents IV al ATal O O-,7 she BIOS Boot Process 23 Ic q1TRr DWrn T l,4 she System Program Loader 24 totem Program Loader 24 ac aam l,< foe Stack 25 Instruction Execution and Addressing 27 Instruction Operands 29 Protected Mode 29 Key Points 30 J Review Questions and Exercises 30 3 EXAMINING COMPUTER MEMORY AND EXECUTING INSTRUCTIONS 32 Introduction 32 Using the DEBUG Program 32 Viewing Memory Locations 34 Machine Language Example i: Using Immediate Data 36 Machine Language Example II: Using Defined Data 40 An Assembly Language Program 44 Using the INT Instruction 45 Using the PTR Operator 47 KeV Points 48 J Points 48 Review Questions and Exercises 49 Part B FundamentalS of ASSembly Language 51 4 REQUIREMENTS FOR CODING IN ASSEMBle LANGU^oE 51 Introduction 51 Assembly Language Features 52 Conventional Segment Directives 58 Qimrilifipod aeon simplified Segment Directives 62 Initializing for Protected Mode 64 Defining Types of Data 65 Equate Directives 70 Key Points 72 y Points 72 Review Questions and Exercises 73 5 ASSEMBLING, LINKING, AND EXECUTING PROGRAMS 74 Introduction 74 Preparing a Program for Assembling and Execution 74 m acal hi R 1 fwo-Pass Assembler sl Linking an Object Program sl Executing a Program 83 ac foe Cross-Reference Listing 84 Error Diagnostics 84 ale hi T. foe Assembler Location Counter 85 Writing .COM Programs 86 Contents v Key Points 89 y Points 89 Review Questions and Exercises 90 6 SYMBOLIC INSTRUCTIONS AND ADDRESSING 92 Introduction 92 ac q- 1 1. T. foe SVmbolic instruction Set--An Overview 92 Jlnbolic instruction Set--An Overview 92 Data Transfer instructions 95 Basic Arithmetic instructions 97 Repetitive Move Operations 98 '. ac TNT Instfuction 100 foe INT Instruction 100 Addressing Modes 100 e,dodes 100 ac q foe Segment Override Prefix 104 .Inent Override Prefix 104 Near and Far Addresses 105 Aligning Data Addresses 105 .mug Data Addresses 105 Key Points ] 06 j POints 106 Review Questions and Exercises 106 7 PROGRAM LOGIC AND CONTROL 109 Introduction 109 Rhoft, Near, and Far Addresses 1 10 short, Near, and Far Addresses 1 10 al T' -n r. foe JMP Instruction 1 1 1 ac T aam T. foe LOOP Instruction 1 13 ac al ac o. foe Flags Register 114 ba Register 114 ac foe CMP Instruction 1 15 Conditional Jump instructions 116 I Calling Procedures 119 b Procedures 1 19 ac aam foe Effect of Program Execution on the Stack 121 brain Execution on the Stack 121 Boolean Operations 125 shifting aide 1 aam shifting Bits 128 b Bits 128 Rotating Bits 131 o Bits 131 Organizing a Program 133 .antiing a Program 133 Key Points 134 j I olnts 134 Review Questions and Exercises 135 Part C Video and K6yboard Operations 137 8 INTRODUCTION TO VIDEO AND KEYBo^RD PROCESSING 137 Introduction 137 q Features 138 screen Features 138 qrrttiria the Cursor 138 betting the Cursor 138 o Lhe Cursor 138 Clearing the Screen 139 g the Screen 139 INT ZIH Function 09H for Screen DisplaV 139 play 139 INT 21H Function OAH for KeVboard InDut 141 J Doard input 141 INT 21H Function 02H for Screen Display 147 - aam riforitc yi contents File Handles 148 INT ZIH Function 40H for Screen Display 148 INT ZIH Function 3FH for Keyboard input 149 KeV Points 151 j I Olnts 151 Review Questions and Exercises 151 - - 9 VIDEO SySTEMS 153 Introduction 153 Components of a Video System 154 Video Modes 156 Attributes 156 BIOS INT IOH Operations 158 Using Graphics Mode 175 Direct Video Display 179 ASCll Characters for Boxes and Menus 182 Key Points 183 J Points 183 Review Questions and Exercises 184 1 0 KEYBo^RD OPER^rlONS 1 86 Introduction 186 BIOS KeVboard Data Areas 187 J board Data Areas 187 INT 21H for Keyboard input 188 INT 16H for Keyboard input 189 Extended Function Keys and Scan Codes 192 BIOS INT 09H and the Keyboard Buffer 197 Key Points 199 j I olnts 199 Review Questions and Exercises 201 Part D Data Manipulation 203 1 1 PROCESSING STRING D^r^ 203 Introduction 203 Features of String Operations 204 MOVSf Move String instruction 205 LODSt Load String instruction 207 ITOS: Store String instruction 207 o fOS: Store String instruction 207 Program f Using LODS and STOS to Edit Data 208 CMPSf Compare String instruction 209 ICAS. Scan StrinZ Instruction 213 .CAS. Scan String instruction 213 Alternative Coding for String instructions 214 Replicating a Pattern 215 KeV Points 215 J Points 215 Review Questions and Exercises 216 Contents vii 1 2 ARITHMETIC I' PROCESSING BINARY D^r^ 218 Introduction 218 Processing Unsigned and Signed Binary Data 218 b .lied and Signed Binary Data 218 Addition and Subtraction of Binary Data 220 j Data 220 Multiplying Binary Data 225 . Dividing BinarV Data 232 b Binary Data 232 al Numeric Data Processor 237 foe Numeric Data Processor 237 KeV Points 239 J Points 239 Review Questions and Exercises 239 13 ARITHMETIC 114 PROCESSING ASCll AND BCD DATA 241 Introduction 241 Data in Decimal Format 242 Processing ASCll Data 242 b ASCll Data 242 Processing Packed BCD Data 247 o Packed BCD Data 247 Converting ASCll Data to Binary Format 250 o ASCll Data to Binary Format 250 Converting Binary Data to ASCll Format 251 o Binary Data to ASCll Format 251 qhiftiricr orid Roundinl ofliftinR and Rounding a Product 252 o and Rounding a Product 252 Key Points 257 J Review Questions and Exercises 258 14 DEFINING AND PROCESSING TABLES 259 Introduction 259 Defining Tables 259 o fables 259 Direct Addressing of Table Entries 261 Roqrnhiricr q Table. searching a Table 264 b a fable 264 ac arT 'm /m 1' foe XLAT (Translate) Instruction 268 Q rartiri a Table aam borting Table Entries 271 o table Entries 271 Address Tables 274 Ti-, ri.. 1 ^ laal fwo-Dimensional Arrays 275 y3 275 Key Points 278 J I olnts 278 Review Questions and Exercises 278 PART E Advanced Input,Output 281 1 5 FACILITIES FOR USING THE MOUSE 281 Introduction 281 Basic Mouse Operations 283 Program: Displaying the Mouse Location 284 More Advanced Mouse Operations 287 perations 287 Program: Using the Mouse with a Menu 290 KeV Points 293 y Points 293 Review Questions and Exercises 294 ... nrintents viII contents 16 DISK STOR^oE I' ORGANIZ^rlON 295 Introduction 295 Characteristics of a Disk Storage Device 295 ac fbi FI q-, foe Disk System Area and Data Area 299 y alem Area and Data Area 299 al n n 1 7fin foe Boot Record 300 ac acid foe DirectorV 301 J ac ail 1 11. acal in she File Allocation Table 302 Processing Files on Disk 307 Key Points 308 y Points 308 Review Questions and Exercises 308 1 7 DISK STOR^oE II: WRITING AND READING FILES 310 Introduction 310 ASCllZ Strings 310 File Handles 311 Error Return Codes 31 1 File Pointers 311 Creating Disk Files 312 Reading Disk Files 314 Random Processing 318 KeV Points 327 j I olnts 327 Review Questions and Exercises 328 1 8 DISK STOR^oE Ill. INT 21 H FUNCTIONS FOR SUPPORTING DISKS AND FILES 330 Introduction 330 Operations Handling Disk Drives 331 Operations Handling the Directory and the FAT 341 Operations Handling Disk Files 343 KeV Points 349 J Points 349 Review Questions and Exercises 352 19 DISK STOR^oE IVy INT 13H DISK FUNCTIONS 353 Introduction 353 BIOS Status Byte 354 Basic INT 13H Disk Operations 354 Other INT 13H Disk Operations 358 Key Points 362 j I olnts 362 Review Questions and Exercises 363 20 FACILITIES FOR PRINTING 364 Introduction 364 Common Printer Control Characters 364 INT ZIH Function 40H: Print Characters 365 COntents ix IX qripni of Priritrvr Conl special Printer Control Characters 369 BIOS INT 17H Functions for Printing 373 Key Points 374 j I olnts 374 Review Questions and Exercises 375 Part F Special ropies 377 ZI DEFINING AND USING MACROS 377 Introduction 377 qirnrile Macro Definitions 378 dimple Macro Definitions 378 Using Parameters in Macros 379 b Parameters in Macros 379 Using Comments in Macros 380 6 Nested Macros 382 Macro Directives 383 KeV Points 391 j I Olnts 391 Review Questions and Exercises 392 22 LINKING TO SUBPROGR^MS 393 Introduction 393 ac gb foe SEGMENT Directive 394 Intrasegment Calls 395 Intersegment Calls 396 .Inent Calls 396 The EXTRN and PUBLIC Attributes 397 Using EXTRN and PUBLIC for an EntrV Point 398 b ac FRN and PUBLIC for an Entry Point 398 Defining the Code Segment as PUBLIC 400 b the Code Segment as PUBLIC 400 UsinZ SimDlified SeZment Directives 402 o simplified Segment Directives 402 Passing Parameters to a Subprogram 404 b Parameters to a Subprogram 404 The ENTER and LEAVE Instructions 406 Linking a C/C++ Program with an Assembly Language Program 408 b a C/C++ Program with an Assembly Language Program 408 Key Points 412 J Points 412 Review Questions and Exercises 412 23 PROGRAM Lo^DING AND OVERL^vS 414 Introduction 414 The Program Segment Prefix 414 The HiZh-Memory Area 418 .if-Memory Area 418 Memory Allocation Strategy 418 J allocation Strategy 418 The Program Loader 419 Allocating and Freeing Memory 424 b and Freeing Memory 424 Loading or Executing a Program Function 425 b o a Program Function 425 Program OverlaVs 428 cram Overlays 428 Resident Programs 432 Key Points 435 y Points 435 Review Questions and Exercises 436 x Contents Part G Reference Chapters 437 24 BIOS D^r^ AREAS, INTERRUPts, AND PORes 437 Introduction 437 ac P Yhe Boot Process 437 ac pTriq no+o 4 ~ 4 foe BIOS Data Area 438 Interrupt Services 442 BIOS Interrupts 443 pts 443 INT 21H Services 446 Ports 451 qttiriq Tnrilltloutl oning input/OutDUt 453 o Input/Output 453 Generating Sound 454 KeV Points 455 J Points 455 Review Questions and Exercises 456 25 OPER^rORS AND DIRECTIVES 458 Introduction 458 yi, aam. G 4 F R YVpe Specifiers 458 ape Specifiers 458 Operators 459 Directives 463 26 THE PC INSTRUCTION SET 479 Introduction 479 Register Notation 480 ac I I l-. A' I n1Tt 4 foe Addressing Mode Byte 480 o .bode Byte 480 ac Tab of~' 1. foe instruction Set 483 APPENDICES A CONVERSION BEtwEEN HEXADECIMAL AND DECIMAL NUMBERS 508 B ASCll CHAR^cTER CODES sl 1 C THE DEBUG PROGRAM 513 D RESERVED WORDS 520 E ASSEMBLING AND LINKING PROGRAMS 522 F KEYBo^RD Sc^N CODES AND ASCll CODES 528 INDEX 533
我要评论
|