3rd Edition - Dandelon.com · 1 Understanding the Programming Environment 13 The 25-Cent Tour of...
Transcript of 3rd Edition - Dandelon.com · 1 Understanding the Programming Environment 13 The 25-Cent Tour of...
3rd Edition
Allen L. Wyatt, Sr.
PROGRAMMINGS E R I E S
TABLE OF CONTENTS
V
Introduction 1Why Use Assembly Language? 2What You Should Know 4What Is Covered in This Book 6What Is Not Covered in This Book 8Summary 9
Parti: The Fundamentals of AssemblyLanguage
1 Understanding the Programming Environment 13The 25-Cent Tour of Memory 14The Stack 20Registers and Flags 22
The Segment Registers 24Additional Registers 25
80286 Register^ 2580386 and 80486 Registers 25
Interrupts 26BIOS Services 26DOS Services 27Device Drivers 27Summary 28
2 An Overview of Assembly Language 29Conceptual Guide to Assembly Language 29Assembly Language Structure 31The Assembly Language Instruction Set 32Assembly Language Directives 37Data Storage in Memory 40
Equates ..40Data-Allocation Directives 41
Addressing 44Register Addressing ^. AAImmediate Addressing' 45Direct Addressing 45Indirect Addressing 46Segment Assumptions 48
Looping and Branching 48Segments and Classes 49
vii
Using Assembly Language, 3rd Edition
Subroutines and Procedures 52The Assembly Language Subroutine Skeleton ..' 53Summary , 55
3 Choosing and Using Your Assembler 57What an Assembler Does 57The Leading Contenders 58
MASMfrom Microsoft 67QuickAssembler from Microsoft 68Turbo Assembler from Borland 69
The Files Involved '...71The ASM Source File 71The OBJ Output File ..̂ 73The LST Listing File : 73
More about Assembler Directives 78Conditional Assembly 79Macro Operations 80
Summary 83
4 Choosing and Using Your Linker 85What the Linker Does 86Choosing Your Linker 86
LINK from Microsoft 87TLINK from Borland 87
The Files Involved 88The Input File (OBJ) 89The Executable File (EXE) 90The List File (MAP) 90The Temporary File (VM.TMP) 92
Using Linker Options 92Display Help Information (/? or/h) 95Enable 32-Bit Processing (/3) 97Align Segment Data (JKistze) 97Operate Linker in Batch Mode (/BA) 97Do Not Ignore Case Differencesin Exports and Imports (/C) 97Do Not Ignore Case Differences (/c or/NOI) 98Create Debugger-compatible File (/COor/V) 98Set Allocation Space (/CP-.para) 98Warn about Duplicate LIB Symbols (/d) 98Use MS-DOS Segment Ordering (/DO) 99Place DGROUP at High End (/DS) 99Pack EXE File (/E) 99Ignore Extended Dictionary (/e or/NOE) 100Translate FAR Calls where Possible (/F) 100List Linker Options (/HE) 100
viii
Table of Contents
Load Program in High Memory (/HI) 100Initialize All Segments (/i) 100Prepare for Incremental Linking (/INC) 101Display Progress of Linking Process (/INF) 101Include Line Numbers in List File (/I or/LI) 101Specify Library Search Path (flpath) 101Include Global Symbol Table in List File (/m) 101Do Not Use Default Libraries (/n or /NOD:name) 102Do Not Translate FAR Calls (/NOF) 102Do Not Associate Groups (/NOG) 102Suppress Copyright Message (/NOL) 102Use MS-DOS Segment Ordering without Nulls (/NON) 102Do Not Pack Code Segments (/NOP) 103Create Overlays (/o) '. 103Set Overlay-Loader Interrupt Number (/O-.int) 103Pack Contiguous Code Segments (/P=size or /PACKC -.size) 103Pack Contiguous Code Segments (/PACKD-.size) 104Pad Code Segments (/FADO.size) ! 104Pad Data Segments (/PADD-.size) 104Pause for Disk Change (/PAU) 104Specify Application Type (/PM-.type) ~. 104Produce QuickLibrary QLB File (/Q) 105Generate Detailed Segment Map (/s) 105Specify MaximunriTSegments (/SE-.seg) 105Override Stack Size (/ST-.size) 105Produce Binary Image File (A) 106Specify Executable File Type (/Td? or fTwP) 106Issue Fixup Warnings (/W) 106Do Not Generate Any Listing File (/x) 106Use Memory Swapping (/ye and/yx) 107
Summary 107
Using Microsoft's Assembly Language Products 109A Sample Program 110Using MASM 6.0 112
Using MASM 6.0 in Programmer's Workbench 112Using MASM 6.0 from the Command Line 115
Assembler Options 116Display Help Information (/?) 119Assemble as a COM File (/AT ) „. 119Specify Alternate Linker (/^filename) ..;.T. 120Assemble Only (/c) . 120Do Not Ignore Case Differences of User Identifiers (/Cp) 120Convert All User Identifiers to Uppercase (/Cu) 120Do Not Ignore Case Differencesin Public and Extern Symbols (/Cx) 120
rx
Using Assembly Language, 3rd Edition
Define an Identifier (/Dname=texf) 121Output First-Pass Listing to STDOUT (/Ep) 121Override Stack Size (/F size) 121Create Bound Executable File (/Fbfilename) 122Specify Executable File (/Fefilename) 122Create a Source Listing File (j¥\filename) 122Create a Map Listing File (/Fmfilename) 122Create an Object File (JFofilename) 122Generate Math-Emulator Fixups (/Fpi) 123Create a Source Browser File (/Frfilename) 123Create an Extended Source Browser File (/FRfilename) 123Use FORTRAN/Pascal/BASIC Callingand Naming Conventions (/Gc) 123Use C Calling and Naming Conventions (/Gd) 124Limit Significant External Symbol Characters (/Hmax) 124Extended ML Help Information (/help) 124Override Default Include-File Path (/{pathname) 124Pass Options to the Linker (/link) 125Do Not Display Copyright Message (/nologo) 125Generate Complete Listings (/Sa) 125Generate Optional First-Pass Listings (/Sf) 125Generate Listing of Assembler-Generated Code (/Sg) 126Set Line Width for Listings (/Slwidth) 126Exclude Symbol Table from Listings (/Sn) 126Set Page Depth for Listings (/Splines) 126Set Listing Subtitle (/Sstext) 126Set Listing Title (/Sttexf) 127Include False Conditionals in Listings (/Sx) 127Assemble File without ASM Extension (/Tafilename) 127Enable Virtual Memory (/VM) 127Set Warning Level 0 (/w) 128Set Warning Level (/Wtevel) 128Treat Warnings as Errors (/WX) 128Ignore the INCLUDE Environment Variable (/X) 128Include Line Numbers in Object File (/Zd) 129Make AU Symbols Public (/Zf) 129Create CodeView-compatible File (/Zi) 129Force Compatibility with MASM 5.1 (/Zm) 129Pack Structures to Byte Boundary (/Zpboundary) 129Check Syntax Only (/Zs) : 130
Compatibility with MASM 5.1 Options... ff. .130Change from Alphabetic Segment Order (/A) 131Set Buffer Size (/Bblocks) 131Check Purity of 286/386 Code (/P) '. 132Order Segments by Occurrence in Source Code (/S) 132Show Extra Assembly Statistics (/V) 132Display Error Lines (/Z) '. 132
Table of Contents
Using ML through a Batch File 132Using ML with Error Codes 134
Meanwhile, Back at the Program 135Linking TEST.OBJ and SUMS.ASM 136
Using LINK Interactively 137Using the LINK Command Line 138Using the LINK Response File 139
Testing the Finished TEST.EXE 140Summary 142
Using Borland's Assembly Language Products 143A Sample Program 143Using TASM 3.0 145Assembler Options 147
Display Help Information (/? or/H) 149Alphabetic Segment Ordering (/A) 149Include Cross-References in the Listing File (/C) 149Define an Identifier (/Dname[=textJ) 150Emulate Floating-Point Instructions (/E) 151Override Default Include-File Path {[{pathname) 151Jam Directive into Effect (/Jname) 151Set Hash Table Capacity (/KHsize) 152Create a Listing File (/L) 152Create an Expanded Listing File (/LA) 152Set Number of Passes (/Mpasses) 152Make Case of Names Significant (/ML) 152Make All Names Uppercase (/MU) 153Set Maximum Valid Length for Symbols (/MVlength) 153Make Case of Global Symbols Significant (/MX) 153Exclude Tables from the Listing File (/N) 153Generate Overlay Code (/O) 153Generate Overlay Code with Phar-Lap-Style Fixups (/OP) 154Check Purity of 286/386 Code (/P) 154Suppress OBJ Records Not Needed for Linking (/Q) 154Create Code for Numeric Coprocessors (/R) 154Order Segments by Occurrence in Source Code (/S) 155Suppress Assembly Completion Messages (/T) .155Set Assembler Emulation Version (JUversion) '. 155Control Warning Messages (/W, /WO, /Wl,/W2, /W-xxx, and /W+xxx) „ 156List False Conditional Statements (/X) ...f.. 156Display Source Lines with Errors (/Z) .....157Include Line Numbers in Object File (/ZD) 157Add Full Debugging Data to OBJ File (/ZI) 157Add No Debugging Data to OBJ File (/ZN) •„..„ 157
Using TASM through a Batch File 157
X I
Using Assembly Language, 3rd Edit ion
Meanwhile, Back at the Program 159Linking TEST.OBJ and SUMS.ASM ::•„. 160
Using the TLINK Command Line 160Using the TLINK Response File 161
testing the Finished TEST.EXE 162Summary 164
Debugging Assembly Language Subroutines 165Debugging as an Art Form 165The Two General Classes of Tools 166
Software Debuggers 167Hardware Debuggers ." 167
Some Software Debuggers Compared 168Built-in, High-Level Language Debuggers 168DEBUG, the Standard 169
Display Help Screen (?) 171Assemble (A) 172Compare (C) 173Dump (D) 173Enter (E) 174Fill(F) 175Go(G) 175Hexadecimal Arithmetic (H) 176Input (I) 176Load(L) 177Move(M) 177Name(N) 177Output (O) 178Proceed (P) • 178Quit(Q) 179Register (R) 179Search (S) ". 179Trace (T) 180Unassemble (U) 180Write (W) 181Allocate Expanded Memory (XA) ...181Deallocate Expanded Memory (XD) 182Map Expanded Memory (XM) 182Expanded Memory Status (XS) 182
CodeView from Microsoft -•. 184Turbo Debugger from Borland 186
The General Techniques 187Using DEBUG 187
Loading DEBUG by Itself 187Loading DEBUG with a File 189DebuggingTEST.EXE 190
Xll
Table of Contents
Using CodeView 198Using Turbo Debugger 204
Summary 205
8 Using Additional Tools 207Working with Makefiles 207
What Does MAKE Do? 208Microsoft's Program-Maintenance Utility 209
Inference Rules 210Command Modifiers 211Macro Definitions 211NMAKE Directives 213Invoking NMAKE ...215
Borland's Version of MAKE 217Creating and Using a Makefile with Microsoft NMAKE 220
Working with Profilers 223Using the Microsoft Source Profiler 223
Counting Analysis 224Timing Analysis 228
Using Borland's Turbo Profiler 231Summary 236
9 Developing Libraries 237The Advantages of Libraries 237Library-Maintenance Programs 238
LIB from Microsoft 238TUB from Borland 239
Creating a Library 240Using LIB 240Using TLIB '. 243
Using a Library ....• 244Linking with a Library 244Creating and Using Library Response Files 246
Maintaining a Library 248Making Changes to a Library '. .' 248Managing a Library 250
Summary 251
Fart II: Mixed Language Programmingwith Assembly Language
10 Interfacing Subroutines 255Passing Parameters 255
How Parameters Are Placed on the Stack 256
xiii
Using Assembly Language, 3rd Edition
How Parameters Are Formatted 256Accessing Parameters on the Stack ., 257
Returning Values 260Returning Values through the Stack 260Returning Values through a Register 261Returning Values through Memory 261
Other Considerations 262Summary 263
11 Interfacing with BASIC 265Working with GW-BASIC or BASICA 266
Interfacing Short Subroutines 266Interfacing Longer Subroutines 273
Working with QBasic 275Compiled BASIC 278Summary 281
12 Interfacing with Pascal 283General Interfacing Guidelines 283
Inline Routines 284External Procedures 284The Interrupt Attribute 284
Interfacing Subroutines without Variable Passing 285Interfacing Subroutines with Variable Passing 292Passing Variable Pointers 295Using Function Subprograms 302Summary 305
13 Interfacing with C and C++ 307Memory Models 308General Interface Guidelines 309Simpler Interfacing 311Interfacing Subroutines with No Variable Passing 312
Using QuickAssembler with QuickC 314Using Inline ASM Coding with Borland C or C + + Products .... 314
Interfacing Subroutines with Variable Passing 315Passing Variable Pointers 318
Returning Data via Pointers 318Using Pointers to Process Strings 323
Summary rT. 325
14 Interfacing with dBASE III+ and FoxPro 327General Interfacing Guidelines 328Interfacing Subroutines without Variable Passing 329
Turning Off the Cursor 330Turning On the Cursor 331
xiv
Table of Contents
Interfacing Subroutines with Variable Passing 332Saving and Restoring the Screen 333Interfacing for dBASE IV 336
Assembling and Linking the BIN Files 336Using the BIN Files 337Summary 340
15 Interfacing with Clipper 341General Interfacing Guidelines 342Interfacing Subroutines 342
Interfacing Subroutines without Variable Passing 343Interfacing Subroutines with Variable Passing 346Interfacing Subroutines that Return Values 349
The Extend System Functions 351Summary 353
Part III: Applying Assembly LanguageTechniques
16 Working with Different Processors 357Detecting the CPU Type 357Detecting the Presence of an NPX 363Summary 365
17 Video Memory 367Differences between Display Adapters 369
The Monochrome Display Adapter (MDA) 369The Hercules Graphics Adapter (HGA) 372The Color Graphics Adapter (CGA) 372The Enhanced Graphics Adapter (EGA) 374The Multi-Color Graphics Array (MCGA) 376The Video Graphics Array (VGA) 377Super VGA Cards (SVGA) 377
Determining the Type of Display Installed 378Displaying a Character in Video Memory 382Displaying an ASCII String 384Using the Attribute Byte 386Creating Text-Based Graphics Routines <- 394Using Window Techniques: Saving and Restoring Windows 400Summary 411
18 Accessing Hardware Ports 413The IN and OUT Assembly Language Instructions 414The I/O Port Map 416
xv
Using Assembly Language, 3rd Edition
Some Significant Hardware Ports 418The 8259 Interrupt Controller 418The 8253 Timer 420The 8255A Programmable Peripheral Interface (PPI) 421
Controlling the Keyboard 424Controlling the Speaker 428
Video Controller Ports 431The Monochrome Adapter 432The Color/Graphics Adapter 433The EGA and VGA Adapters j , 436
Printer Ports 439Asynchronous Communications Ports 441
Summary 445
19 Working with Disk Drives 447DOS Functions for Disk Access 447Specifying the Disk or Media Type 448The Disk Base Table ." ....449Formatting a Track 450A Formatting Routine 452The Controlling Program 467Summary 469
f
Part IV: Reference20 Accessing BIOS Services 473
The BIOS Service Categories 474The BIOS Services 475
21 The DOS Services 575The DOS Service Categories 576The DOS Services 582
22 Processor Instruction Set 727Programming Models 728
8086/8088 72880286 72880386 <. : 72980486 , 7298087 73080287 73080387 '. 730
xvi
Table of Contents
Instruction Sets !...731Data-Transfer Instructions 731Arithmetic Instructions 732Numeric Transcendental Instructions 733Numeric Constant Instructions 733Bit-Manipulation Instructions 733Numeric Comparison Instructions 734String-Manipulation Instructions 734Control-Transfer Instructions 734Flag-and Processor-Control Instructions 735The Complete Instruction Set 735
Specialized Registers: Flags, Control, and Status 745The Flags Register 746The Control Word 751The Status Word 752
Detailed Instruction Information ; 758
23 Assembler Directives 871Directive" Categories 872
Processor Specification 872Global Control 873Simplified Segment Control 873Complete Segment Control 873Procedures and"Code Labels 874Scope and Visibility 874Data Allocation 874Complex Data Types 874Control Flow 875Macros and Repeat Blocks 875Conditional Assembly 875Conditional Error 875Listing Control 876String Control 876Miscellaneous ". 877
Detailed Directive Information 877
A The ASCII Character Set 1027
B Keyboard Interpretation Tables 1031BIOS Keyboard Codes > 1031Keyboard Controller Codes 1039
Glossary 1043
Index 1053
XVll