本书在当前主流操作系统采用的32位地址环境中介绍了80x86汇编语言和计算机体系结构,使读者能够在汇编语言层面上了解传统意义上的体系结构。
·重点介绍了32位内存模型和32位指令
·强调体系结构,如寄存器、内存编址、硬件功能等
·阐述了如何在汇编语言中实现高级语言概念
·提前介绍了汇编语言编程以及Microsoft公司的WinDbg调试器
Preface vii
Chapter 1 Representing Data in a Computer 1
1.1 Binary and Hexadecimal Numbers 2
1.2 Character Codes 6
1.3 2's Complement Representation for Signed Integers 9
1.4 Addition and Subtraction of 2's Complement Numbers 15
1.5 Other Systems for Representing Numbers 21
Chapter Summary 25
Chapter 2 Parts of a Computer System 27
2.1 PC Hardware: Memory 28
2.2 PC Hardware: The CPU 30
2.3 PC Hardware: Input/Output Devices 36
2.4 PC Software 37
PC Software: The Operating System 37
PC Software: Text Editors 38
PC Software: Language Translators and the Linker 38
Chapter Summary 39
Chapter 3 Elements of Assembly Language 41
3.1 Assembly Language Statements 42
3.2 A Complete Example 45
3.3 How to Assemble, Link, and Run a Program 53
3.4 The Assembler Listing File 60
3.5 Constant Operands 68
3.6 Instruction Operands 73
3.7 Input/Output Using Macros Defined in IO.H 77
Chapter Summary 82
Chapter 4 Basic Instructions 85
4.1 Copying Data 86
4.2 Integer Addition and Subtraction Instructions 95
4.3 Multiplication Instructions 108
4.4 Division Instructions 118
4.5 Addition and Subtraction of Larger Numbers 130
4.6 Something Extra: Levels of Abstraction and Microcode 133
Chapter Summary 134
Chapter 5 Branching and Looping 137
5.1 Unconditional Jumps 138
5.2 Conditional Jumps, Compare Instructions,
and if Structures 144
5.3 Implementing Loop Structures 159
5.4 for Loops in Assembly Language 173
5.5 Arrays 180
5.6 Something Extra: Pipelining 189
Chapter Summary 190
Chapter 6 Procedures 193
6.1 The 80x86 Stack 194
6.2 Procedure Body, Call and Return 201
6.3 Parameters and Local Variables 211
6.4 Recursion 223
6.5 Other Architectures: Procedures Without a Stack 228
Chapter Summary 230
Chapter 7 String Operations 231
7.1 Using String Instructions 232
7.2 Repeat Prefixes and More String Instructions 239
7.3 Character Translation 254
7.4 Converting a 2's Complement Integer to an
ASCII String 259
7.5 Other Architectures: CISC versus RISC Designs 264
Chapter Summary 265
Chapter 8 Bit Manipulation 267
8.1 Logical Operations 268
8.2 Shift and Rotate Instructions 278
8.3 Converting an ASCII String to a 2's
Complement Integer 292
8.4 The Hardware Level--Logic Gates 298
Chapter Summary 299
Chapter 9 The Assembly Process 301
9.1 Two-Pass and One-Pass Assembly 302
9.2 80x86 Instruction Coding 307
9.3 Macro Definition and Expansion 319
9.4 Conditional Assembly 326
9.5 Macros in IO.H 333
Chapter Summary 337
Chapter 10 Floating-Point Arithmetic 339
10.1 80x86 Floating-Point Architecture 340
10.2 Programming with Floating-Point Instructions 359
10.3 Floating-Point Emulation 374
10.4 Floating-Point and In-line Assembly 384
Chapter Summary 386
Chapter 11 Decimal Arithmetic 387
11.1 Packed BCD Representations 388
11.2 Packed BCD Instructions 396
11.3 Unpacked BCD Representations and Instructions 404
11.4 Other Architectures: VAX Packed Decimal
Instructions 416
Chapter Summary 417
Chapter 12 Input/Output 419
12.1 Console I/O Using the Keme132 Library 420
12.2 Sequential Ffie I/O Using the Kernel32 Library 428
12.3 Lower-Level Input/Output 437
Chapter Summary 439
Appendix A Hexadecimal/ASCII Conversion 441
Appendix B Useful MS-DOS Commands 443
Appendix C MASM 6.11 Reserved Words 445
Appendix D 80x86 Instructions (by Mnemonic) 449
Appendix E 80x86 Instructions (by Opcode) 469
Index 489
文艺复兴以降, 源远流长的科学精神和逐步形成的学术规范, 使西方国家在自然科学的各 个领域取得了垄断性的优势, 也正是这样的传统, 使美国在信息技术发展的六十多年间名家辈 出. 独领风骚. 在商业化的进程中, 美国的产业界与教育界越来越紧密地结合, 计算机学科中 的许多泰山北斗同时身处科研和教学的最前线, 由此而产生的经典科学著作, 不仅擘划了研究 的范畴, 还揭橥了学术的源变, 既遵循学术规范, 又自有学者个性, 其价值并不会因年月的流 逝而减退.
近年, 在全球信息化大潮的推动下, 我国的计算机产业发展迅猛, 对专业人才的需求日益 迫切. 这对计算机教育界和出版界都既是机遇, 也是挑战, 而专业教材的建设在教育战略上显 得举足轻重. 在我国信息技术发展时间较短. 从业人员较少的现状下, 美国等发达国家在其计 算机科学发展的几十年间积淀的经典教材仍有许多值得借鉴之处. 因此, 引进一批国外优秀计 算机教材将对我国计算机教育事业的发展起积极的推动作用, 也是与世界接轨. 建设真正的世 界一流大学的必由之路.
机械工业出版社华章图文信息有限公司较早意识到"出版要为教育服务". 自1998年开始, 华章公司就将工作重点放在了遴选. 移译国外优秀教材上. 经过几年的不懈努力, 我们与 Prentice Hall, Addison-Wesley, McGraw-Hill, Morgan Kaufmann等世界著名出版公司建立了良 好的合作关系, 从它们现有的数百种教材中甄选出Tanenbaum, Stroustrup, Kernighan, Jim Gray等大师名家的一批经典作品, 以"计算机科学丛书"为总称出版, 供渎者学习. 研究及庋 藏. 大理石纹理的封面, 也正体现了这套丛书的品位和格调.
"计算机科学丛书"的出版工作得到了国内外学者的鼎力襄助, 国内的专家不仅提供了中肯 的选题指导, 还不辞劳苦地担任了翻译和审校的工作, 而原书的作者也相当关注其作品在中国 的传播, 有的还专减为其书的中译本作序. 迄今, "计算机科学丛书"已经出版了近百个品种, 这些书籍在读者中树立了良好的口碑, 并被许多高校采用为正式教材和参考书籍, 为进一步推 广与发展打下了坚实的基础.
随着学科建设的初步完善和教材改革的逐渐深化, 教育界对国外计算机教材的需求和应用 都步入一个新的阶段. 为此, 华章公司将加大引进教材的力度, 在"华章教育"的总规划之下 出版三个系列的计算机教材:除"计算机科学丛书"之外, 对影印版的教材, 则单独开辟出 "经典原版书库", 同时, 引进全美通行的教学辅导书"Schaum's Outlines"系列组成"全美经 典学习指导系列". 为了保证这三套丛书的权威性, 同时也为了更好地为学校和老师们服务, 华 章公司聘请了中国科学院. 北京大学. 清华大学. 国防科技大学. 复旦大学. 上海交通大学. 南京大学. 浙江大学. 中国科技大学. 哈尔滨工业大学. 西安交通大学. 中国人民大学. 北京 航空航天大学. 北京邮电大学. 中山大学. 解放军理工大学. 郑州大学. 湖北工学院. 中国国 家信息安全测评认证中心等国内重点大学和科研机构在计算机的各个领域的著名学者组成"专 家指导委员会", 为我们提供选题意见和出版监督.
这三套丛书是响应教育部提出的使用外版教材的号召, 为国内高校的计算机及相关专业的 教学度身订造的. 其中许多教材均已为M. I. T., Stanford, U. C. Berkeley, C. M. U. 等世界名牌 大学所采用. 不仅涵盖了程序设计. 数据结构. 操作系统. 计算机体系结构. 数据库. 编译原 理. 软件工程. 图形学. 通信与网络. 离散数学等国内大学计算机专业普遍开设的核心课程, 而且各具特色--有的出自语言设计者之手. 有的历经三十年而不衰. 有的已被全世界的几百所高校采用. 在这些圆熟通博的名师大作的指引之下, 读者必将在计算机科学的宫殿中由登堂而入室.
权威的作者. 经典的教材. 一流的译者. 严格的审校. 精细的编辑, 这些因素使我们的图 书有了质量的保证, 但我们的目标是尽善尽美, 而反馈的意见正是我们达到这一终极目标的重 要帮助. 教材的出版只是我们的后续服务的起点. 华章公司欢迎老师和读者对我们的工作提出 建议或给予指正, 我们的联系方法如下:
电子邮件:hzedu@hzbook. com
联系电话:(010)68995264
联系地址:北京市西城区百万庄南街1号
邮政编码:100037
Richard C. Detmer于1966年毕业于肯塔基大学, 后于威斯康星大学获硕士学位和博士学位, 现任中田纳西州立大学计算机科学系教授和系主任.
随书光盘内容包括:
Microsoft公司的MASM汇编程序. 全屏幕调试器WinDbg和链接器以及完整的源代码和作者自己编写的用于辅助I/O的软件.
A computer can be viewed from many different levels. Many people are interested only in using applications such as word processing or games. A computer programmer, however, often sees the computer as an instrument to create new applications software. A high-level language programmer's image of the computer is provided by the language compiler, which gives the impression that the computer stores object types like integer, real, and array of char in named memory locations, calculates values of expressions, calls procedures, executes while loops, and so forth.
However, an actual computer works at even lower levels. This book emphasizes the architectural level, that is, the level defined by the machine instructions that the processor can execute. Assembly-language instructions translate directly into machinelanguage instructions, so that when you write an assembly-language program, you gain an understanding of how the computer works at the machine-language level.
Although this book emphasizes the assembly-language/machine-language level
of computer operations, it also looks at other levels. For instance, it describes how highlevel language concepts such as if statements are realized at the machine level. It discusses some of the functions of the operating system. It briefly describes the logic gates that are used at the hardware level. It also looks at how assembly language is translated into machine language.
To program effectively at any level, programmers must understand certain fundamentsl principles at the machine level. These apply to most computer architectures. Introduction to 80x86 Assembly Language and Computer Architecture teaches these fundamental concepts:
·memory addressing, CPU registers and their uses
·representation of data in a computer in numeric formats and as character strings
·instructions to operate on 2's complement integers
·instructions to operate on individual bits
·instructions to handle strings of characters
·instructions for branching and looping
·coding of procedures: transfer of control, parameter passing, local variables, and preserving the environment for the calling program
The primary architecture covered is the Intel 80x86 CPU family used in many personal computers. However, almost every chapter includes information about other architectures, or about different computer levels. Programming in assembly language and studying related concepts in Introduction to 80x86 Assembly Language and Computer Architecture prepares the student to program effectively in any programming language, to pursue advanced studies in computer design and architecture, or to learn more about system details for specific computers.
Text Organization and Content
Much of the material in this book is based on my previous book, Fundamentals of Assembly Language Programming Using the IBM PC and Compatibles. While teaching this material through the years, I have increasingly come to the conclusion that an assembly language course is the best place to introduce computer architecture to most students. This book reflects a stronger emphasis on architecture than on programming. It also concentrates on general concepts as opposed to the details of a particular computer system.
The minimal prerequisite for my assembly language class is a good understand-ing of a structured high-level language. Chapters 3 through 6 and Chapter 8 form the core of my one-semester course. ! normally cover Chapters 1-8 thoroughly, Chapter 9 quickly, and then choose topics from Chapters 10-12 depending on time and resources avafiable. For instance, ! sometimes introduce floating-point operations via in-line assembly statements in a C++ program. Style and Pedagogy
The text primarily teaches by example. A complete assembly-language program is presented very early, in Chapter 3, and its components are carefully examined at a level that the student is able to understand. Subsequent chapters include many examples of assembly language code along with appropriate explanations of new or difficult concepts.
The text uses numerous figures and examples. Many series of "before" and
"after" examples are given for instructions. Examples are included that illustrate the use of a debugger. These examples give the student a stronger sense of what is happening inside the computer.
Exercises appear at the end of each section. Short-answer exercises reinforce understanding of the material just covered, and programming exercises offer an opportunity to apply the material to assembly-language programs.
Software Environment
The "standard" 80x86 assembler is Microsoft's Macro Assembler (MASM), version 6.11.
Although this assembler can produce' code for 32-bit fiat memory model programming appropriate to a Windows 95, Windows NT, or other 32-bit Microsoft operating system environment, the linker and debugger that come with this software package are not suitable for use in such an environment. This book comes with a CD containing the assembler program from MASM (ML), a more recent Microsoft linker, the 32-bit fullscreen debugger WinDbg (also from Microsoft), and necessary supporting files. This software package provides a good environment for producing and debugging console applications.
The CD included with the book also contains a package designed to simplify
input/output for the student, so that the emphasis remains on architecture rather than operating system detafis. This I/O package is used extensively through most of the book. Finally, the CD contains source code for each program that appears as a figure in the book.
Instructor's Support
Supplementary materials for this book include an Instructor's Guide that contains some
teaching tips and solutions to many exercises. In addition, the author can be contacted at rdetmer@mtsu.edu with questions or comments.
Acknowledgments
I would like to thank my students who suffered through preliminary versions of this text,
often getting materials that were duplicated "just in time." These students were very good at catching errors. I also want to thank Hong Ski Yuan, who used a preliminary version of this text in his assembly language class and who offered valuable feedback.
Many thanks to the following people who took the time to review the manuscript: Dennis Bouvier, University of Houston-Clear Lake; Barry Fagin, US Air Force Academy; Glynis Hamel, Worcester Polytechnic Institute; Dennis Fairclough, Utah Valley State College; Thomas Higginbotham, Southeastern Louisiana University; Clifford Nadler, Worcester Polytechnic Institute.
My wife, Carol, deserves credit for her understanding during the many hours that I ignored her and word-processed at my computer.
Richard C. Detmer