Intel汇编语言程序设计(第5版)

Intel汇编语言程序设计(第5版)"

作者:KipR.Irvine
ISBN:9787302197935
定价:¥69
字数:千字
页数:
出版时间:2009.05.01
开本:
版次:1-1
装帧:
出版社:清华大学出版社
简介

本书全面细致地讲述了汇编语言程序设计的各个方面。从微处理器体系结构、工作机制到指令集;从最基本的编译器链接器的使用到高级过程、结构和宏的使用;从用纯汇编编写程序到用VC++、BC++等最新编译器与汇编的混合接口编程;从16位实模式下BIOS、DOS实模式文本及图形程序设计到32位保护模式的Windows程序设计;从磁盘基础知识到Intel指令编码、浮点运算等相关知识都做了深入而细致的讲解。

本书内容广博,但又讲解得非常深入,是汇编语言课程的很好教材。

【教辅材料】

PPT,习题答案,测试题库,示例代码

【本书特点】

1. 本书组织结构灵活,教师可根据需要按不同的顺序、不同的深度进行授课。

2. 以32位保护模式编程为默认模型,使读者更容易理解和接受。

3. 为32位和16位程序设计给出了扩展的链接库,包括用于简化输入/输出、数字处理、磁盘与文件处理,以及字符串处理等的40多个函数。

4. 作者为每章提供了测试题库,方便教师准备考试。 

5. 以自上而下的方式探究和解析程序的设计思想和过程。

6. 作者提供了内容丰富的教学课件。 

7. 每小节后面都给出了复习题,有助于学生掌握所学知识。

8. 为教师提供了复习题和编程练习题答案。 

9. 提供了本书配套的网站:http://www.prenhall.com/irvine/。

前言

PREfaCE

Assembly Language for intel-Based Computers, Fifth Edition, teaches assembly language 

programs suage for intel-Based Computers, Fifth Edition, teaches assembly language 

program.,,.

ining and architecture for intel lA-32 processors. It is an aDproDriate text for the following tvDes of

s and architecture for intel lA-32 processors. It is an appropriate text for the following types of

college courses:

. AssemblV Language Programming

y Language Programming

. Fundamentals of Computer Systems

. Fundamentals of Computer Architecture

Students use intel or AMD processors and program with Microsoft Macro Assembler (MASM)

8.0, running on any of the following MS-Windows platforms: Windows 95, 98, Millenium, NT, 2000,

and XP.

Although this book was originally designed as a programming textbook for college students, it has

.if this book was originally designed as a programming textbook for college students, it has

evolved over the last 15 years into much more. Many uniVersities use the book for their introductory

J J y

computer architecture courses. As a testament to its popularity, the foufth edition was tfanslated into

..

KOrean, Chinese, French, Russian, and Polish.

Emphasl'S of TOPiCS This edition includes topics that lead naturally into subsequent courses in

computer architecture, operating systems, and compiler writing:

4,

. Virtual machine concept

. Elementary boolean operations

; Doolean operations

. Instruction execution cVcle

y de

. Memory access and handshaking

J 5

. Interrupts and polling

. Pipelining and superscalar concepts

. Hardware-based I/O

. Floating-point binary representation

Other topics relate specifically to intel lA-32 architecture:

'

. lA-32 protected memory and paging

.

. MemorV segmentation in real-address mode

J segmentation in real-address mode

. 16-bit intemipt handling

. MS-DOS and BIOS sVstem calls (interrupts)

y item calls (interrupts)

. lA-32 Floating-Point Unit architecture and programming

. lA-32 Instruction encoding

Ceftain examDles Dresented in the book lend themselves to courses that occur later in a comDUter

pies presented in the book lend themselves to courses that occur later in a computer

..,

science curriculum:

. Searching and sorting algorithms

. High-level language structures

.h-level language structures

. Finite-state machines

. Code optimization examples

'

.

XIX

U PREfACE

imprOVementS in the Rfth Edition A number of improvements and new information have been

added in this edition, listed in the following table by chapter number f

Still a Programming Book This book is still focused on its original mission: 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 computCr works. Our premise is that students retain knowledge better when

theory is combined with experience. In an engineering course, students constrict 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 MOds and Protsctsd MOds This edition emphasizes 32-bit protected mode, but it still has

three 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

ruwhng 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 protCcted mode and real mode programming. Most differences

are abstracted by the book's parallel link libraries for real mode and protected mode programming.

Link LibrarieS We supply two versions of the link library that students use for basic input-output,

simulations, timing, and other useful stuff. The 32-bit version (lrvine32.lib) runs in protected mode,

sending its output to the Win32 console. The 16-bit version (lrvine16.lib) runs in real-address mode.

Full source code for the libraries is supplied on the book's Web site. The link libraries are available

only for convenience, not to prevent stlldeflts from learning how to program input-output themselves.

Stlldents are encouraged to create their own libraries.

PREfACE fbi

Includsd Software and EX8mPI8s All the example programs were tested with Microsoft Macro

Assembler Version 8.0. The 32-bit C++ applications in Chapter 12 were tested with Microsoft Visual

C++ .NET. The real-address mode programs in Chapter 12 (linking to C++) were assembled with

programs in Chapter 12 (linking to C++) were assembled with

Borland Turbo Assembler (TASM).

W8b Sits informatl'On Updates and corrections to this book may be found at the book's Web site,

http: //www. asmi rvi ne. corn, including additional programming projects for instructors to assign

at the ends of chapters. If for some reason you cannot access this site, information about the book and

piers. If for some reason you cannot access this site, information about the book and

a link to its current Web site can be found at www. prenhall. corn by searching for the book title or for

the author name "Kip lrvine."

Overall Goals

The following goals of this book are designed to broaden the student's interest and knowledge in 

top. 1

ics related to assembly language:

; language:

. Intel lA-32 processor architecture and programming

. Real-address mode and protected mode programming

. AssemblV language directiVes, macros, operators, and program structure

y language directiVes, macros, operators, and program structure

. Programming methodology, showing how to use assembly language to create system-level 

soft.ramming 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

J a o programs, the operating systCm, and other aPPlication programs

One of our goals is to help students approach programming problems with a machine-level mind

set. It is impoftant to think of the CPU as an interactiVe tool, and to learn to monitor its operation as

directly as Dossible. A debugger is a programmer's best friend, not only for catching errors, but as an

J 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 Dresented in the text. Reference materials, such as guides to

J presented in the text. Reference materials, such as guides to

MS-DOS interrupts and instruction mnemonics, are available at the end of the book.

RequiTed BaCkground The reader should already be able to program confidently in at least one

other programming language, preferably 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 

engineer.

lug courses.

Features

COmPI8ts Program LiStingS A companion CD-ROM contains all the source code from the 

examples in this book. Additional listings are available on the book's Web page. An extensive link library is

supplied with the book, containing more than 30 procedures that simplify user input-output, numeric

.,.', n 1, 11.'.

processing, disk and file handling, and string handling. In the beginning stages of the course, students

can use this library to enhance their programs. Latef, they can create their own procedures and add

them to the librarV'

y.

Programming LOgiC TWo chapters emphasize boolean logic and bit-level manipulation. A 

con. 

scions 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.

..

all PREfACE

HardWare and OOeratl'na SyStsm ConceotS The first two chanters introduce basic hardware

peratl'ng Spetsm 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.

StFUctUred Proaramml'na Approach BeZinning with Chapter 5, procedures and functional 

decomsramming 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.

DI'Sk Storaae ConceDtS Students learn the fundamental principles behind the disk storage system

He ConceptS Students learn the fundamental principles behind the disk storage system

on MS-Windows--based sVstems from hardware and software Doints of view.

y items from hardware and software points of view.

Creatl'na LI'nk LI'braries Students are free to add their own Drocedures to the book's link library

d Link LI'brarieS Students are free to add their own procedures to the book's link library

and create new libraries. TheV learn to use a toolbox approach to programming and to write code that

J learn to use a toolbox approach to programming and to write code that

.

is useful in more than one program.

MaCros and StFUctUres A chapter is devoted to creating structures, unions, and macros, which are

esential in assemblV language and systems programming. Conditional macros with advanced 

operas language and systems programming. Conditional macros with advanced 

operators serve to make the macros more professional.

professional.

IRtsrfscina tO High-Level Lanauaaes A chanter is devoted to interfacinZ assembly lanZuaZe to C

s ic 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 lacely to find jobs programming in high-level

J

languages. They can ie~ to optimize their code and see examples of how C++ compilers optimize code.

InstFUctional Al'dS All the program listings are available on disk and on the Web. Instructors are 

provided a test baal, answers to review questions, solutions to programming exercises, and a Microsoft

.

Powerpoint slide presentation for each chapter.

. 

Summary of ChaDters

y of Chapters

Chapters I to & contain a basic foundation 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. Chapter 10 was split into two parts

J

for this graph because no other chapter depends on one's knowing how to create macros:

.laph because no other chapter depends on one's knowing how to create macros:

@ U

Mughs 

4

9

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

representation.

2. lA-32 Processor Architecture: Basic microcomputer design, instruction execution cycle, lA-32

.

t.

processor architecture, lA-32 memory management, components of a microcomputef, and the

.

input-output system.

3. Assembly LanZuage Fundamentals: Introduction to assembly language, liaring and debugging,

J Language Fundamentals: Introduction to assembly language, liaring and debugging,

and defininZ constants and variables.

5

PREfACE XXiii

4. Data Transfers, Addressing, and Arithmetic: Simple data transfer and arithmetic instructions,

assemble-link-execute cVcle, operators, directiVes, expressions, JMP and LOOP instructions, and

J, operators, directiVes, expressions, JMP and LOOP instructions, and

indirect addressing.

5.

5. Procedures: Linking to an external library, description of the book's link library, stack 

opera..

nons, defining and using procedures, flowchins, 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 ASCll and packed decimal arithmetic.

8. Advanced Procedures: Stack parameters, local variables, advanced PROC and INVOKE 

direc..

tives, and recursion.

9. Strings and Arrays: String primitiVes, manipulating arrays of characters and integers, 

twodimensional arraVs, sorting, and searching.

y o, hcf ting, and searching.

10. Structllres and Macros: Stmctures, 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. High-Level Language interface: Parameter passing conventions, inline assembly code, and linking

assemb1V language modules to C and C++ programs.

y language modules to C and C++ programs.

13. 16-Bit MS-DOS Programming: Calling MS-DOS interrupts for console and file input-output.

14. Disk Fundamentals: Disk storage systems, sectors, clusters, directories, file allocation tables, handling

MS-DOS error codes, and driVe and directory m~ulation.

15. BIOS-Level Programming: Keyboard input, video text, graphics, and mouse programming.

16. ExDert MS-DOS Programming: Custom-designed segments, runtime program structure, and

pert MS-DOS Programming: Custom-designed segments, runtime program structure, and

Interrupt handling. Hardware control using I/O ports.

17. Floating-Point Processing and instruction Encoding: Floating-point binary representation and

floatinZ-DOint arithmetic. Learning to program the lA-32 Floating-Point Unit. Understanding the

o point arithmetic. Learning to program the lA-32 Floating-Point Unit. Understanding the

encoding of lA-32 machine instructions.

5

Appendix A: MASM Reference

Appendix B: The lA-32 Instruction Set

Appendix C: BIOS and MS-DOS Interrupts

Appendix D: Answers to Review Questions

Reference Materials

W8b Site The author maintains an actiVe Web site at www.asmirvine.com.

H8lP the Help file (in Windows Help Format) by Gerald Cahill of Antelope Valley College. 

Documents the book's link libraries, as well as Win32 data structures.

Assembly Language Workbook An interactive workbook is included on the book's Web site, 

cover'

. 1. I

lug such important topics as number conversions, addressing modes, register usage, Debug 

program.'

minZ and floating-point binary numbers. The content pages are HTML documents, making it easy

b, and floating-point binary numbers. The content pages are HTML documents, making it easy

for students and instructors to add their own customized content. This workbook is also available on

the book's Web site.

Debugging Tools Tutorials on using Microsoft CodeView, Microsoft Visual Studio, and Microsoft

Windows Debugger (WinDbg).

BIOS and MS-DOS Im6rruntS Appendix C contains a brief listing of the most-often-used INT 10h

ptS Appendix C contains a brief listing of the most-often-used INT 10h

(video), INT 16h (keyboard), and INT Zlh (MS-DOS) functions.

Instfucaon Set Appendix B lists most nonprivileged instructions for the lA-32 processor family.

. D r

XXIV PREfACE

For each instruction, we describe its effect, show its syntax, and show which flags are affected.

Powerpoint Presentoaons A complete set of Microsoft Powerpoint presentations written by the author.

Acknowledgments

Special thanks are due to Tracy Dunkleberger, Executive Editor for Computer Science at Prentice

Hall, who provided friendly, helpful guidance during the writing of the fifth edition. Karen Ettinger

did a terrific job as production editof, constantly keeping track of numerous minute details. Camille

,oh as production editof, constantly keeping track of numerous minute details. Camille

Trentacoste was the book's managing editor.

Fifth Edition

I offer mV special thanks and gratitude to the following professors who boosted my morale, gave me

J peelal thanks and gratitude to the following professors who boosted my morale, gave me

great pedagogical tips, and tirelessly examined the entire book. They have been a huge influence on

the development of this book, in some cases across multiple editions:

. Gerald Cahill, Antelope Valley College

. James Brink, Pacific Lutheran University

. William Barrett, San Jose State UniVersity

Many thanks to Scott Blackledse and John Tavior, both professional programmers, who proofread

J s Jlor, both professional programmers, who proofread

most of the manuscript and flagged numerous errors. Several people reviewed individual chapters:

. Jerry JoVce, Keene State College

J J, Keene State College

. Tianzheng Wu, Mount Mercy College

. Ron Davis, Kennedy-King College

. David Topham, Ohlone College

. Harvev Nice. Depaul UniVersitV

J, Depaul UniVersity

FOurth Edition

The following people were tremendously helpful in creating the fourth edition.

o people were tremendously helpful in creating the fourth edition.

. Gerald Cahill, Antelope Valley College

. James Brink, Pacific Lutheran University

. Maria Kolatis, County College of Moms

. Tom JoVce, Chief Engineer at Premier Heart, LLC

.

. Jeff WOthke, Purdue Calumet University

. Tim DowneV, Florida international University

y, florida international University

The followinZ individuals Drovided valuable Droofreading help in the fourth edition f

o individuals provided valuable proofreading help in the fourth edition f

. Andres Altamirano, Miami

. Courtnev Amor, Los Angeles

y Amor, Los Angeles

. Scott Blackledge, Platform Solutions, Inc.

. Ronald Davis, Kennedy-King College

. Ata Elahi, Southern Connecticut State UniversitV

. Jose Gonzalez, Miami

. LeroV Highsmith, Southern Connecticut State UniVersity

.

. Sand lqbal, Faran institute of Technology

,id lqbal, Faran institute of Technology

. Charles Jones, Maryville College

. Vincent KaVes, Mount St. Mary College

J.

. Eric Kobrin, Miami

. Pablo Maurin, Miami

. BarrV Meaker, Design Engineer, Boeing Corporation

y bleaker, Design Engineer, Boeing Corporation

. Ian Merkel, Miami

. Sylvia Miner, Miami

PREfACE XXV

. M. Nawaz, OPSTEC College of Computer Science

. Kam Ng, Chinese University of Hong Kong

. Hien Nguyen, Miami

. Ernie Philipp, Northern Virginia Community College

.

. BoVd SteDhens, UGMO Research, LLC

J phens, UGMO Research, LLC

. John Taylor, England

J 5 england

. ZacharV TaVlor, Columbia ColleZe

, laylor, Columbia College

. Virginia Welsh, Community College of Baltimore County

.inia Welsh, Community College of Baltimore County

. Robert WOrkman, Southern Connecticut State University

J

. Tianzheng Wu, Mount Mercy College

& flu, Mount Mercy College

. Matthew Zukoski, Lehigh University

目录

Preface xxiii

1 sasic Concepts 1

1'1 W6Icome to Assembly Lanauaae 1

y Language 1

1. 1 .l Good Questions to Ask 2

1. 1 .2 Assembly Language Applications 5

J Language Applications 5

1.l.3 Section Review 6

1.2 Virtual Machine Concept 7

l .2.1 HistorV of PC Assemblers 9

y of PC Assemblers 9

l.2.2 Section Review 9

1.3 Data Representation 9

1 .3. 1 BinarV Numbers 10

J 1

l.3.2 BinarV Addition 11

J Addition 11

1 .3.3 Integer Storage Sizes 12

1 .3.4 Hexadecimal integers 13

l.3.5 Signed integers 14

.lied integers 14

l .3.6 Character Storage 16

l.3.7 Section Review 18

1'4 soolean Operations 20

1 .4.1 Truth Tables for Boolean Functions 22

l.4.2 Section Review 23

1'5 Chapter Summary 23

2 lA-32 Processor Architecture 25

2'1 General Concepts 25

2.1. 1 Basic Microcomputer Design 26

2.1.2 Instruction Execution Cycle 27

j

2.l.3 Reading from Memory 30

2.1 .4 How Programs Run 31

slams Run 31

2.1.5 Section Review 32

2.2 lA-32 Processor Architecture 33

2.2.1 Modes of Operation 33

2.2.2 Basic Execution Environment 34

v

.

yi

CONTENTS

2'2.3 Floating-Point Unit 36

o Point Unit 36

2'2'4 Intel MicroDrocessor History 37

processor History 37

2'2'5 Section Review 38

2'3 lA-32 Memory Management 39

2'3.1 Real-Address Mode 39

2'3'2 Protected Mode 41

2'3'3 Section Review 43

2'4 Components of an lA-32 MicrocomDuter 43

ponents of an lA-32 Microcomputer 43

2'4'1 Motherboard 43

2'4'2 Video Output 44

put 44

2'4'3 Memory 45

7,J

2'4.4 Input-Output Ports and Device interfaces 45

2'4'5 Section Review 46

2'5 Input-Output System 46

2'5'l How it All Works 46

2'5'2 Section Review 48

2'6 Chapter Summary 49

3 Assembly Language Fundamentals 51

3'1 Basic Elements of Assembly Language 51

3' 1. 1 Integer Constants 52

u

3'1.2 Integer Expressions 52

& pressions 52

3' l.3 Real Number Constants 53

3'l'4 Character Constants 54

3' l'5 String Constants 54

5

3'1.6 Reserved Words 54

3'l '7 Identifiers 54

3' I .8 Directives 55

3' I .9 Instructions 55

3' l.10 The NOP (No Operation) Instruction 57

3' 1. I I Section Review 58

3'2 Example: AddingThree Integers 58

3'2'l Alternative Version of AddSub 60

3'2.2 Program TemDlate 61

cram femplate 61

3'2'3 Section Review 61

3'3 Assemblina, Linkina, and Runnina Proarams 62

al Linking, and Running Programs 62

3'3.1 The Assemble-Link-Execute Cycle 62

3'3'2 Section Review 64

3'4 Defining Data 64

3'4'1 Intrinsic Data TVves 64

apes 64

3'4.2 Data Definition Statement 64

3'4.3 Defining BYTE and SBYTE Data 66

u

CONTENTS Vii

3.4.4 Defining WORD and SWORD Data 67

o

3.4.5 Defining DWORD and SDWORD Data 68

o DWORD and SDWORD Data 68

3.4.6 Defining OWORD Data 69

o 

3.4.7 DefininZ TBYTE Data 69

b IBYTE Data 69

3.4.8 Defining Real Number Data 69

o

3.4.9 Little Endian Order 69

3.4.10 Adding Variables to the AddSub Program 70

5 o

3.4. I I Declaring Uninitialized Data 71

o

3.4.12 Section Review 71

3'5 Symbolic Constants 72

ymbolic Constants 72

3.5.1 Equal-Sign Directive 72

3.5.2 Calculating the Sizes of Arrays and Strings 73

& the Sizes of Arrays and Strings 73

3.5.3 EQU Directive 74

3.5.4 TEXTEQU Directive 74

3.5.5 Section Review 75

3.6 Real-Address Mode Proarammina (Optional) 75

srammlng (Optional) 75

3.6.1 Basic ChanZes 75

o

3'7 Chapter Summary 76

3.8 Proqrammina Exercises 77

cramming Exercises 77

4 Data Transfers, Addressing,

and Arithmetic 79

4.1 DataTFansfer Instructions 79

4.l. I Introduction 79

4.l.2 Operand Types 80

4.l.3 Direct MemorV Operands 80

J perands 80

4. I .4 MOV Instruction 81

4. l.5 Zero/Sign Extension of integers 82

4.l.6 LAHF and SAHF Instructions 84

4.l.7 XCHG InstrUction 84

4.l.8 Direct-Offset Operands 84

4.l.9 Example Program (Moves) 85

4. 1.10 Section Review 86

4.2 Addition and Subtraction 87

4'2.1 INC and DEC Instructions 87

4.2.2 ADD Instruction 87

4.2.3 SUB InstrUction 88

4'2.4 NEG InstrUction 88

4.2.5 ImplementingArithmetic Expressions 89

4

4.2.6 Flags Affected by Addition and Subtraction 89

so Affected by Addition and Subtraction 89

4.2.7 Example Program (AddSub3) 92

4.2.8 Section Review 93

... .ONTENTS

Vlll CONTENTS

4.3 Data-Related Operators and Directives 94

4.3.1 OFFSET Operator 94

4.3.2 ALIGN Directive 95

4.3.3 PTR Operator 95

4.3.4 TYPE Operator 96

4.3.5 LENGTHOF Operator 97

4.3.6 SIZEOF Operator 97

4.3.7 LABEL Directive 97

4.3.8 Section Review 98

4.4 Indirect Addressing 99

4.4.1 Indirect Operands 99

4.4.2 Arrays 100

4.4.3 Indexed Operands 101

4.4.4 Pointers 102

4.4.5 Section Review 103

4.5 JMP and LOOP Instructions 104

4.5.1 JMP Instruction 104

4.5.2 LOOP Instruction 105

4.5.3 Summing an integerArray 106

4.5.4 Copying a String 106

4.5.5 Section Review 107

4.6 Chapter Summary 108

4.7 Programming Exercises 109

5 Procedures 111

5.1 Introduction 111

5.2 Linking to an External Library 111

5.2.1 Background information 1 12

5.2.2 Section Review 1 12

5.3 The Book's Link Library 113

5.3.1 Overview 113

5.3.2 Individual Procedure Descriptions 115

5.3.3 Library Test Programs 125

5.3.4 Section Review 129

5.4 Stack Operations 129

5.4.1 Runtime Stack 129

5.4.2 PUSH and POP Instructions 131

5.4.3 Section Review 134

5.5 Denning and Using Procedures 134

5.5.1 PROC DirectiVe 134

5.5.2 CALL and RET Instructions 136

CONTENTS iX

5.5.3 Example: Summing an integer Array 139

5.5.4 Flowchafts 140

5'5'5 SavinZ and RestorinZ ReZisters 140

o and Restoring Registers 140

5.5.6 Section Review 142

5'6 Program Desian Usina Procedures 143

"ram Design Using Procedures 143

5.6.1 Integer Summation Program (Design) 143

5.6.2 Integer Summation Implementation 145

air summation Implementation 145

5'6.3 Section Review 147

5'7 Chapter Summary 147

5'8 Proarammina Exercises 148

cramming Exercises 148

6 Conditional Processing 150

6'1 Introduction 150

6.2 Boolean and Comparison Instructions 151

6.2.1 The CPU Flags 151

6.2.2 AND Instruction 152

6'2'3 OR InstrUction 153

6.2.4 XOR Instruction 154

6'2.5 NOT Instruction 155

6.2.6 TEST Instruction 155

6'2'7 CMP InstrUction 156

6'2.8 SettinZ and ClearinZ Individual CPU FlaZs 157

o and Clearing individual CPU Flags 157

6'2'9 Section Review 157

6'3 Conditional JumDs 158

ps 158

6'3. I Conditional Structures 158

6'3'2 Jcond instruction 158

6'3'3 TVpes of Conditional JumD Instructions 159

J pos of Conditional Jump instructions 159

6'3.4 Conditional Jump Applications 163

6.3.5 Bit TestinZ Instructions (Optional) 167

o Instructions (Optional) 167

6.3.6 Section Review 168

6'4 Conditional Loop Instructions 169

6.4.1 LOOPZ and LOOPE Instructions 169

6.4.2 LOOPNZ and LOOPNE Instructions 169

6'4.3 Section Review 170

6'5 Conditional Structures 170

6.5.1 Block-Structured iF Statements 170

6'5.2 Compound Expressions 173

6'5'3 WHILE Loops 174

6'5'4 Table-Driven Selection 177

6'5'5 Section Review 178

6'6 Application: Finite-State Machines 179

6'6'l Validating an input String 180

X CONTENTS

6.6.2 Validating a Signed integer 180

6.6.3 Section Review 183

6.7 Decision Directives 184

6.7.1 Signed and Unsigned Comparisons 185

6.7.2 Compound Expressions 186

6.7.3 .REPEAT and .WHILE DirectiVes 188

6'8 Chapter Summary 189

6.9 Programming Exercises 190

7 Integer Arithmetic 193

7.1 Introduction 193

7.2 Shift and Rotate Instructions 194

7.2.1 Logical Shifts and Arithmetic Shifts 194

7.2.2 SHL Instruction 195

7.2.3 SHR Instruction 196

7.2.4 SAL and SAR Instructions 196

7.2.5 ROL Instfllction 197

7.2.6 ROR Instruction 198

7.2.7 RCL and RCR Instructions 198

7.2.8 Signed Overflow 199

7.2.9 SHLD/SHRD Instructions 199

7.2.10 Section Review 200

7'3 Shift and Rotate Applications 201

7.3.1 Shifting Multiple Doublewords 201

7.3.2 BinarV Multiplication 202

' y idultiplication 202

7.3.3 Displaying Binary Bits 202

7.3.4 Isolating MS-DOS File Date Fields 203

7.3.5 Section Review 203

7.4 Multiplication and Division Operations 204

7.4.1 MUL Instfuction 204

7.4.2 IMUL Instttiction 205

7.4.3 Benchmarking Multiplication Operations 207

7.4.4 DIV Instruction 208

7.4.5 Signed integer Division 209

7.4.6 Implementing Arithmetic Expressions ZI I

7.4.7 Section Review 212

7.5 Extended Addition and Subtraction 213

7.5. 1 ADC Instruction 213

7.5.2 Extended Addition Example 213

7.5.3 SBB Instruction 214

7.5.4 Section Review 215

CONTENTS Xi

XI

7'6 ASCll and Unpacked Decimal Arithmetic 215

7.6.1 AAA Instruction 216

7.6.2 AAS Instruction 218

7.6.3 AAM Instruction 218

7.6.4 AAD Instruction 218

7'6'5 Section Review 219

7'7 Packed Decimal Arithmetic 219

7.7.1 DAA InstrUction 219

7.7.2 DAS Instruction 220

7.7.3 Section Review 220

7'8 Chapter Summary 221

7.9 Proqrammina Exercises 222

cramming Exercises 222

8 Advanced Procedures 224

8.1 Introduction 224

8'2 Stack Frames 225

8'2'l Stack Parameters 225

8.2.2 LocalVariables 233

8.2.3 ENTER and LEAVE Instructions 236

8.2.4 LOCAL Directive 237

8.2.5 WriteStackFrame Procedure 240

8'2.6 Section Review 241

8.3 Recursion 242

8.3.1 Recursivelv Calculating a Sum 243

j o a Sum 243

8'3.2 Calculating a Factorial 243

8.3.3 Section Review 245

8'4 .MODEL Directive 246

8.4.1 Language Specifiers 247

suage specifiers 247

8'4.2 Section Review 248

8'5 INVOKE, ADDR, PROC, and PROTO (Optional) 248

8'5.1 INVOKE Directive 248

8.5.2 ADDR Operator 249

8'5'3 PROC Directive 250

8.5.4 PROTO Directive 253

8'5'5 Parameter Classifications 255

8.5.6 Example: Exchanging Two integers 256

8.5.7 DebuggingTips 256

.ging Yips 256

8'5.8 Section Review 257

8'6 Creatina Multimodule Proarams 258

s Multimodule Programs 258

8.6.1 Hiding and Exporting Procedure Names 258

s and Exporting Procedure Names 258

8'6.2 CallinZ External Procedures 258

o txtemal Procedures 258

.. CONTENTS

XI I

8.6.3 Using Variables and Symbols Across Module Boundaries 259

8.6.4 Example: ArraySum Program 260

8.6.5 Creating the Modules Using Extem 261

8.6.6 Creating the Modules Using INVOKE and PROTO 264

8.6.7 Section Review 266

8.7 Chapter Summary 267

8.8 Programming Exercises 268

9 Strings and Arrays 269

9.1 Introduction 269

9.2 String Primitive Instructions 270

9.2.1 MOVSB, MOVSW, and MOVSD 271

9.2.2 CMPSB, CMPSW, and CMPSD 272

9.2.3 SCASB, SCASW, and SCASD 274

9.2.4 STOSB, STOSW, and STOSD 274

9.2.5 LODSB, 1,ODSW, and 1.ODSD 275

9.2.6 Section Review 275

9.3 Selected String Procedures 276

9.3.1 Stf--compare Procedure 276

9.3.2 Stl'--length Procedure 277

9.3.3 Stf--copy Procedure 278

9.3.4 Sir trim Procedure 278

9.3.5 Sir "case Procedure 279

9.3.6 String Library Demo Program 280

9.3.7 Section Review 282

9.4 TWo-Dimensional Arrays 282

9.4.1 Ordering of Rows and Columns 282

9.4.2 Base-Index Operands 283

9.4.3 Base-index-Displacement Operands 285

9.4.4 Section Review 285

9.5 Searching and Sorting integer Arrays 285

9.5.1 Bubble Sort 286

9.5.2 Binary Search 287

9.5.3 Section Review 293

9'6 Chapter Summary 294

9.7 Programming Exercises 295

10 Structures and Macros 299

10'1 Structures 299

10. 1 .l Defining Structures 300

CONTENTS Xiii

10.l.2 Declaring Structure Variables 301

s structure Variables 301

10.1 .3 Referencing Structure Variables 302

10.1 .4 Example: Displaying the System Time 305

.

10.1 .5 Structures Containing Structures 307

s structures 307

10.1 .6 ExamDle. Drunkard's Walk 307

pie: Drunkard's Walk 307

10.1 .7 Declaring and Using Unions 310

10' I .8 Section Review 312

10'2 Macros 313

10'2.1 Overview 313

10.2.2 Defining Macros 313

o iAacros 313

10'2'3 Invoking Macros 314

s 'Aacros 314

10.2.4 Additional Macro Features 315

10.2.5 Using the Book's Macro Library 318

S Lhe Book's Macro Library 318

10'2.6 Example Program: Wrappers 324

J

10'2'7 Section Review 325

10'3 Conditional-Assembly Directives 326

10'3.1 Checking for Missing Arguments 326

s for Missing Arguments 326

10'3.2 Default Argument initialliers 328

10.3.3 Boolean ExDressions 328

pressions 328

10'3'4 IF, ELSE, and ENDIF Directives 328

10'3'5 The IFIDN and IFIDNI Directives 329

10.3.6 Example: Summing a Matrix Row 330

10'3'7 Special Operators 333

10'3'8 Macro Functions 336

10'3.9 Section Review 337

10'4 Definina ReDeat Blocks 338

s Repeat Blocks 338

10'4'l WHILE Directive 338

10.4.2 REPEAT Directive 338

10'4.3 FOR Directive 339

10.4.4 FORC Directive 340

10'4.5 Example: Linked List 340

pie: Linked List 340

10'4.6 Section Review 342

10'5 Chapter Summary 342

10'6 Proarammina Exercises 343

cramming Exercises 343

11 MS-Windows Proarammina 346

"ramming 346

11'1 Win32 Console Proqrammina 346

cramming 346

l 1. 1. 1 Background information 347

.round information 347

l l' l.2 Win32 Console Functions 350

11.1 .3 Displaying a Message Box 352

.

l 1' l'4 Console Input 354

put 354

l I 'l.5 Console Output 360

l 1. I .6 Reading and Writing Files 361

. CONTENTS

XIV CONTENTS

l 1. 1 .7 File I/O in the lrvine32 Library 365

l 1. I .8 Testing the File I/O Procedures 367

1 1. 1.9 Console Window Manipulation 370

11. 1 .10 Controlling the Cursor 373

l 1. 1. 1 1 Controlling the Text Color 373

1 1. 1. 12 Time and Date Functions 375

l 1. 1. 13 Section Review 379

11.2 Writing a Graphical Windows Application 379

l 1.2.1 Necessary Structures 380

l 1.2.2 The MessageBox Function 381

l 1.2.3 The WinMain Procedure 382

l 1.2.4 The Winproc Procedure 382

l 1.2.5 The ErrorHandler Procedure 383

l 1.2.6 Program Listing 383

l 1.2.7 Section Review 386

11.3 Dynamic Memory Allocation 387

1 l.3.1 HeapTest Programs 390

11.3.2 Section Review 393

11.4 lA-32 Memory Management 393

l l.4.1 Linear Addresses 394

l 1.4.2 Page Translation 397

11.4.3 Section Review 398

11'5 Chapter Summary 399

11.6 Programming Exercises 400

12 High-Level Language interface 402

12.1 Introduction 402

12. 1. 1 General Conventions 402

12.1.2 Section Review 403

12.2 Inline Assembly Code 404

12.2.1 asm DirectiVe in Microsoft Visual C++ 404

12.2.2 File Encryption Example 406

12.2.3 SectionReview 409

12.3 Linking to C/C++ in Protected Mode 409

12.3.1 Using Assembly Language to Optimize C++ Code 410

12.3.2 Calling C and C++ Functions 415

12.3.3 MultiplicationTable Example 416

12.3.4 Calling C Library Functions 419

12.3.5 Directory Listing Program 422

J Listing Program 422

12.3.6 Section Review 423

12.4 Linking to C/C++ in Real-Address Mode 423

12.4.1 Linking to Borland C++ 424

CONTENTS XV

12.4.2 ReadSector Example 425

12.4.3 Example: Large Random integers 428

'

12'4.4 Section Review 430

12'5 Chapter Summary 430

12'6 Programming Exercises 431

13 16-Bit MS-DOS Programming 432

13.1 MS-DOS and the IBM-PC 432

13. I .l Memory Organization 433

j &anlzation 433

13.1.2 Redirecting InDut-Output 434

o Input-Output 434

13'l.3 Software interrupts 435

13'l.4 INT Instruction 435

13. l.5 Coding for 16-Bit Programs 436

s tor 16-Bit Programs 436

13'l.6 Section Review 437

13'2 MS-DOS Function Calls (INT 21h) 438

13.2.1 Selected Output Functions 439

13.2.2 Hello WOrld Program Example 441

13'2.3 Selected input Functions 442

13.2.4 Datemme Functions 446

13'2.5 Section Review 449

13'3 Standard MS-DOS File I/O Services 449

13.3. 1 Create or Open File (716Ch) 451

13'3.2 Close File Handle (3Eh) 452

13.3.3 Move File Pointer (42h) 452

13'3.4 Get File Creation Date and Time 453

13'3.5 Selected Library Procedures 453

J

13.3.6 Example: Read and Copy a Text File 454

.'

13.3.7 ReadinZ the MS-DOS Command Tail 456

o

13.3.8 Example: Creating a Binary File 458

13.3.9 Section Review 461

13'4 Chapter Summary 461

13'5 Chapter Exercises 463

14 Disk Fundamentals 464

14'1 Disk Storaae Systems 464

sc Systems 464

14'l'l Tracks, Cylinders, and Sectors 465

14.l.2 Disk Partitions (Volumes) 466

14.1.3 Section Review 468

14.2 File Systems 468

ystems 468

14.2.1 FAT12 469

. r

XVI CONTENTS

14.2.2 FAT16 469

14.2.3 FAT32 469

14.2.4 NTFS 470

14.2.5 PrimarV DiskAreas 470

j.

14.2.6 Section Review 471

14.3 Disk Directory 472

14.3.1 MS-DOS Directory Structure 473

14.3.2 Long Filenames in MS-Windows 475

14.3.3 FileAllocation Table (FAT) 476

14.3.4 Section Review 477

14.4 Reading and Writing Disk Sectors (7305h) 477

14.4.1 Sector Display Program 478

14.4.2 Section Review 482

14.5 System-Level File Functions 482

14.5.1 Get Disk Free Space (7303h) 483

14.5.2 Create Subdirectory (39h) 485

14.5.3 Remove Subdirectory (3Ah) 486

14.5.4 Set Current Directory (3Bh) 486

14.5.5 Get Current Directory (47h) 486

y (47h) 486

14.5.6 Get and Set File Attributes (7143h) 486

14.5.7 Section Review 487

14'6 Chapter Summary 487

14.7 Programming ExerciseS 488

15 BIOS-Level Programming 490

15.1 Introduction 490

15.1.1 BIOS Data Area 491

15.2 Keyboard Input with INT 16h 492

15.2. 1 How the Keyboard Works 492

J

15.2.2 INT 16h Functions 493

15.2.3 Section Review 497

15.3 VIDEO Programming with INT 10h 498

15.3.1 Basic Background 498

.found 498

15.3.2 Controlling the Color 499

15.3.3 INT 10hVideo Functions 501

15.3.4 LibrarV Procedure Examples sl 1

J Procedure Examples sl 1

15.3.5 Section Review 512

15.4 Drawing Graphics Using INT 10h 512

15.4.1 INT 10h Pixel-Related Functions 513

15.4.2 DrawLine Program 514

CONTENTS xvii

15.4.3 Cartesian Coordinates Program 515

o

15.4(4 Converting Cartesian Coordinates to Screen Coordinates 517

6

15.4.5 Section Review 518

15.5 Memory-Mapped GraDhics 519

y-Mapped Graphics 519

15.5.1 Mode 13h: 320 X 200, 256 Colors 519

15.5.2 MemorV-Mapped Graohics Program 520

J idapped Graphics Program 520

15.5.3 Section Review 523

15.6 Mouse Proarammina 523

srammlng 523

15.6.1 Mouse INT 33h Functions 523

15.6.2 Mouse Tracking Program 528

o Program 528

15.6.3 SectionReview 532

15'7 Chapter Summary 533

15'8 Chapter Exercises 534

16 Expert MS-DOS Programming 536

16.1 Introduction 536

16.2 Defining Segments 537

16.2.1 Simplified Segment DirectiVes 537

16.2.2 Explicit Segment Definitions 538

16.2.3 Segment Overrides 542

16.2.4 CombininZ Segments 542

o begments 542

16.2.5 Section Review 543

16.3 Runtime Program Structure 544

16.3.1 Program Segment Prefix 544

16.3.2 COM Programs 545

Slams 545

16.3.3 EXE Programs 546

16.3.4 Section Review 547

16.4 Interrupt Handlina 548

pt Handling 548

16.4. I Hardware interrupts 549

16.4.2 Interrupt Control instructions 550

16.4.3 Writing a Custom interrupt Handler 551

o

16.4.4 Terminate and StaV Resident ProZrams 553

, Resident Programs 553

16.4.5 Application: The No--Reset Program 554

16.4.6 Section Review 557

16.5 Hardware ContrOl Using l/O Ports 558

16.5.1 Input-Output Pofts 558

put~Output Ports 558

16.5.2 PC Sound Program 558

16'6 Chapter Summary 560

...

xvill CONTENTS

17 Floating-Point Processing and instruction

Encodina 562 9 and instruction

9 O62

17.1 Floatina-Point Binary Reoresentation 562

s-Point Binary Representation 562

17.1.1 IEEE BinarV Floating-Point Representation 563

y floating-Point Representation 563

17.1.2 The Exponent 564

ponent 564

17.1.3 Normalized Binary FloatinZ-Point Numbers 565

J floating-Point Numbers 565

17.l.4 Creating the IEEE Representation 565

17.1.5 ConvertinZ Decimal Fractions to Binary Reals 567

o Decimal Fractions to Binary Reals 567

17.1.6 Section Review 568

17.2 Floating-Point Unit 569

17.2.1 FPU ReZister Stack 569

o.

17.2.2 Rounding 571

5 3 71

17.2.3 Floating-Point Exceptions 573

& Point Exceptions 573

17.2.4 Floating-Point instruction Set 573

& Point instruction Set 573

17.2.5 Arithmetic instructions 576

17.2.6 Comparing Floating-PointValues 579

17.2.7 Reading and Writing Floating-Point Values 582

a and Writing Floating-Point Values 582

17.2.8 Exception Synchronization 583

17.2.9 Code Examples 584

17.2.10 Mixed-ModeArithmetic 585

17.2.if Masking and Unmasking Exceptions 586

s and Unmasking Exceptions 586

17.2.12 Section Review 587

17.3 Intel Instruction Encoding 588

17.3.1 lA-32 InstrUction Format 588

17.3.2 Single-Byte instfllctions 589

&ie-Byte instfllctions 589

17.3.3 Move Immediate to Register 590

ulster 590

17.3.4 Register-Mode instructions 591

17.3.5 lA-32 Processor Operand-Size Prefix 591

17.3.6 Memory-Mode instfllctions 592

J .bode instructions 592

17.3.7 Section Review 595

17'4 Chapter Summary 596

17.5 Proarammina Exercises 597

cramming Exercises 597

Appendix A MASM Reference 600

Appendix B The lA-32 Instruction Set 619

Appendix C BIOS and MS-DOS Interrupts 650

Appendix D Answers to Review Questions 659

作者简介

编辑推荐

作者寄语

电子资料

www.luweidong.cn

下一个