Interrupt structure and interrupt service routines isrs
The Art of Assembly Language
The Art of Assembly Language
(Brief Contents)
Thi d | t | t d | ith F | M k |
---|
Page ii
The Art of Assembly Language
Thi d | t | t d | ith F | M k |
---|
2.2 Boolean Functions and Truth Tables ................................................................................45
2.3 Algebraic Manipulation of Boolean Expressions .............................................................48
2.7 Okay, What Does It Have To Do With Programming, Then? ..........................................64
2.8 Generic Boolean Functions ..............................................................................................65
2.12 Questions .........................................................................................................................80
Chapter Three System Organization .............................................................................. 83
3.2.1 The System Clock ...................................................................................................92 3.2.2 Memory Access and the System Clock ...................................................................93 3.2.3 Wait States ...............................................................................................................95 3.2.4 Cache Memory ........................................................................................................96
3.3 The 886, 8286, 8486, and 8686 “Hypothetical” Processors ..............................................99
3.4 I/O (Input/Output) ........................................................................................................... 124
3.5 Interrupts and Polled I/O ................................................................................................. 126
3.9 Questions .......................................................................................................................... 142
Chapter Four Memory Layout and Access .................................................................. 145
4.3 Segments on the 80x86 ..................................................................................................... 151
4.4 Normalized Addresses on the 80x86 ................................................................................ 154
4.8 Some Final Comments on the MOV Instructions ............................................................. 169
4.9 Laboratory Exercises ......................................................................................................... 169
4.11 Summary ..........................................................................................................................188
4.12 Questions .........................................................................................................................190
5.1 Some Additional Instructions: LEA, LES, ADD, and MUL .................................................195
5.2 Declaring Variables in an Assembly Language Program ..................................................196
5.6 Composite Data Types ......................................................................................................206
5.6.1 Arrays ......................................................................................................................206 5.6.1.1 Declaring Arrays in Your Data Segment ...................................................... 207 5.6.1.2 Accessing Elements of a Single Dimension Array ........................................ 209 5.6.2 Multidimensional Arrays .........................................................................................210 5.6.2.1 Row Major Ordering ..................................................................................... 211
5.7.1 Simple Variable Declarations ................................................................................ 222 5.7.2 Using Pointer Variables ......................................................................................... 224 5.7.3 Single Dimension Array Access ............................................................................. 226 5.7.4 Multidimensional Array Access ............................................................................. 227 5.7.5 Simple Structure Access ......................................................................................... 229 5.7.6 Arrays of Structures ................................................................................................ 231 5.7.7 Structures and Arrays as Fields of Another Structure ............................................ 233 5.7.8 Pointers to Structures and Arrays of Structures ..................................................... 235
5.8 Laboratory Exercises ......................................................................................................... 237
6.0 Chapter Overview ............................................................................................................ 243
6.1 The Processor Status Register (Flags) ............................................................................... 244
6.4.1 The MOVZX, MOVSX, CBW, CWD, CWDE, and CDQ Instructions .................... 252 6.4.2 The BSWAP Instruction ......................................................................................... 254 6.4.3 The XLAT Instruction ............................................................................................. 255
6.5 Arithmetic Instructions ..................................................................................................... 255
6.6.2.1 SHL/SAL ........................................................................................................ 271 6.6.2.2 SAR ................................................................................................................ 272 6.6.2.3 SHR ................................................................................................................ 273 6.6.2.4 The SHLD and SHRD Instructions ................................................................ 274 6.6.3 The Rotate Instructions: RCL, RCR, ROL, and ROR ................................................276 6.6.3.1 RCL ................................................................................................................ 277 6.6.3.2 RCR ................................................................................................................ 277 6.6.3.3 ROL ............................................................................................................... 278 6.6.3.4 ROR ............................................................................................................... 278 6.6.4 The Bit Operations .................................................................................................279 6.6.4.1 TEST .............................................................................................................. 280 6.6.4.2 The Bit Test Instructions: BT, BTS, BTR, and BTC ...................................... 280 6.6.4.3 Bit Scanning: BSF and BSR ........................................................................... 281 6.6.5 The “Set on Condition” Instructions .......................................................................281
6.7 I/O Instructions .................................................................................................................284
6.11 Sample Programs .............................................................................................................303
6.11.1 Simple Arithmetic I ...............................................................................................303 6.11.2 Simple Arithmetic II ..............................................................................................305 6.11.3 Logical Operations ................................................................................................306 6.11.4 Shift and Rotate Operations .................................................................................308 6.11.5 Bit Operations and SETcc Instructions .................................................................310 6.11.6 String Operations ..................................................................................................312 6.11.7 Conditional Jumps ................................................................................................313 6.11.8 CALL and INT Instructions ...................................................................................315 6.11.9 Conditional Jumps I ..............................................................................................317 6.11.10 Conditional Jump Instructions II ........................................................................318
6.15 Questions .........................................................................................................................331
Chapter Seven The UCR Standard Library ................................................................... 333
The Art of Assembly Language
7.1.4 Formatted Output Routines: Putisize, Putusize, Putlsize, and Putulsize .............. 340 7.1.5 Output Field Size Routines: Isize, Usize, and Lsize .............................................. 340 7.1.6 Conversion Routines: ATOx, and xTOA ............................................................... 341 7.1.7 Routines that Test Characters for Set Membership ............................................... 342 7.1.8 Character Conversion Routines: ToUpper, ToLower ............................................ 343 7.1.9 Random Number Generation: Random, Randomize ............................................ 343 7.1.10 Constants, Macros, and other Miscellany ............................................................ 344 7.1.11 Plus more! ............................................................................................................ 344
7.4 Programming Projects ...................................................................................................... 351
7.5 Summary ........................................................................................................................... 351
8.2 The Location Counter ...................................................................................................... 357
8.3 Symbols ............................................................................................................................. 358
8.7 Procedures ........................................................................................................................ 365
8.8 Segments ........................................................................................................................... 366
8.11 Label Types ......................................................................................................................385
8.11.1 How to Give a Symbol a Particular Type .............................................................385 8.11.2 Label Values ..........................................................................................................386 8.11.3 Type Conflicts .......................................................................................................386
8.14 Macros ..............................................................................................................................400
8.14.1 Procedural Macros ................................................................................................400 8.14.2 Macros vs. 80x86 Procedures ...............................................................................404 8.14.3 The LOCAL Directive ............................................................................................406 8.14.4 The EXITM Directive ............................................................................................406 8.14.5 Macro Parameter Expansion and Macro Operators .............................................407 8.14.6 A Sample Macro to Implement For Loops ...........................................................409 8.14.7 Macro Functions ...................................................................................................413 8.14.8 Predefined Macros, Macro Functions, and Symbols ............................................414 8.14.9 Macros vs. Text Equates .......................................................................................418 8.14.10 Macros: Good and Bad News .............................................................................419
8.19 Controlling the Listing .....................................................................................................424
8.19.1 The ECHO and %OUT Directives ........................................................................424 8.19.2 The TITLE Directive ..............................................................................................424 8.19.3 The SUBTTL Directive ..........................................................................................424 8.19.4 The PAGE Directive ..............................................................................................424 8.19.5 The .LIST, .NOLIST, and .XLIST Directives ..........................................................425 8.19.6 Other Listing Directives ........................................................................................425
8.22.1 EX8.MAK ...............................................................................................................432 8.22.2 Matrix.A .................................................................................................................432 8.22.3 EX8.ASM ...............................................................................................................433 8.22.4 GETI.ASM ..............................................................................................................442
Page x
8.24 Programming Projects .................................................................................................... 453
8.25 Summary ......................................................................................................................... 453
9.1.1 Simple Assignments ............................................................................................... 460 9.1.2 Simple Expressions ................................................................................................ 460 9.1.3 Complex Expressions ............................................................................................ 462 9.1.4 Commutative Operators ........................................................................................ 466
9.2 Logical (Boolean) Expressions ......................................................................................... 467
9.5.1 Multiplying Without MUL and IMUL ..................................................................... 487 9.5.2 Division Without DIV and IDIV ............................................................................ 488 9.5.3 Using AND to Compute Remainders ..................................................................... 488 9.5.4 Implementing Modulo-n Counters with AND ....................................................... 489 9.5.5 Testing an Extended Precision Value for 0FFFF..FFh ........................................... 489 9.5.6 TEST Operations ................................................................................................... 489 9.5.7 Testing Signs with the XOR Instruction ................................................................ 490
9.6 Masking Operations ......................................................................................................... 490
Page xi
9.8.3 Generating Tables ..................................................................................................497
9.11 Programming Projects .....................................................................................................516
9.12 Summary ..........................................................................................................................516
10.2 IF..THEN..ELSE Sequences ..............................................................................................522
10.3 CASE Statements ..............................................................................................................525
10.7 Register Usage and Loops ...............................................................................................534
10.8 Performance Improvements ............................................................................................535
10.12 Laboratory Exercises .....................................................................................................552
10.12.1 The Physics of Sound .........................................................................................552 10.12.2 The Fundamentals of Music ...............................................................................553 10.12.3 The Physics of Music ..........................................................................................554 10.12.4 The 8253/8254 Timer Chip .................................................................................555 10.12.5 Programming the Timer Chip to Produce Musical Tones .................................555 10.12.6 Putting it All Together .........................................................................................556
12.1.2 Unit Activation, Address Binding, and Variable Lifetime ....................................642 12.1.3 Static Links ............................................................................................................642 12.1.4 Accessing Non-Local Variables Using Static Links ...............................................647 12.1.5 The Display ...........................................................................................................648 12.1.6 The 80286 ENTER and LEAVE Instructions ..........................................................650
12.2 Passing Variables at Different Lex Levels as Parameters. ...............................................652
12.5 Iterators ............................................................................................................................663
12.5.1 Implementing Iterators Using In-Line Expansion ................................................664 12.5.2 Implementing Iterators with Resume Frames ......................................................666
12.8 Programming Projects .....................................................................................................695
12.9 Summary ..........................................................................................................................697
13.2 An Introduction to the BIOS’ Services ............................................................................701
13.2.1 INT 5- Print Screen ...............................................................................................702 13.2.2 INT 10h - Video Services ......................................................................................702 13.2.3 INT 11h - Equipment Installed .............................................................................704 13.2.4 INT 12h - Memory Available ................................................................................704 13.2.5 INT 13h - Low Level Disk Services .......................................................................704 13.2.6 INT 14h - Serial I/O ..............................................................................................706 13.2.6.1 AH=0: Serial Port Initialization ................................................................... 706 13.2.6.2 AH=1: Transmit a Character to the Serial Port ........................................... 707 13.2.6.3 AH=2: Receive a Character from the Serial Port ........................................ 707 13.2.6.4 AH=3: Serial Port Status ............................................................................. 707 13.2.7 INT 15h - Miscellaneous Services .........................................................................708 13.2.8 INT 16h - Keyboard Services ................................................................................708 13.2.8.1 AH=0: Read a Key From the Keyboard ...................................................... 709 13.2.8.2 AH=1: See if a Key is Available at the Keyboard ...................................... 709 13.2.8.3 AH=2: Return Keyboard Shift Key Status ................................................... 710 13.2.9 INT 17h - Printer Services .....................................................................................710
13.3.1 MS-DOS Calling Sequence ................................................................................... 714 13.3.2 MS-DOS Character Oriented Functions ............................................................... 714 13.3.3 MS-DOS Drive Commands .................................................................................. 716 13.3.4 MS-DOS “Obsolete” Filing Calls .......................................................................... 717 13.3.5 MS-DOS Date and Time Functions ...................................................................... 718 13.3.6 MS-DOS Memory Management Functions .......................................................... 718 13.3.6.1 Allocate Memory ........................................................................................ 719 13.3.6.2 Deallocate Memory .................................................................................... 719 13.3.6.3 Modify Memory Allocation ........................................................................ 719 13.3.6.4 Advanced Memory Management Functions .............................................. 720 13.3.7 MS-DOS Process Control Functions .................................................................... 721 13.3.7.1 Terminate Program Execution ................................................................... 721 13.3.7.2 Terminate, but Stay Resident ..................................................................... 721 13.3.7.3 Execute a Program ..................................................................................... 722 13.3.8 MS-DOS “New” Filing Calls ................................................................................. 725 13.3.8.1 Open File .................................................................................................... 725 13.3.8.2 Create File .................................................................................................. 726 13.3.8.3 Close File .................................................................................................... 727 13.3.8.4 Read From a File ........................................................................................ 727 13.3.8.5 Write to a File ............................................................................................. 728 13.3.8.6 Seek (Move File Pointer) ............................................................................ 728 13.3.8.7 Set Disk Transfer Address (DTA) ............................................................... 729 13.3.8.8 Find First File .............................................................................................. 729 13.3.8.9 Find Next File ............................................................................................. 730 13.3.8.10 Delete File ................................................................................................ 730 13.3.8.11 Rename File .............................................................................................. 730 13.3.8.12 Change/Get File Attributes ..................................................................... 731 13.3.8.13 Get/Set File Date and Time ...................................................................... 731 13.3.8.14 Other DOS Calls ....................................................................................... 732 13.3.9 File I/O Examples ................................................................................................ 734 13.3.9.1 Example #1: A Hex Dump Utility .............................................................. 734 13.3.9.2 Example #2: Upper Case Conversion ........................................................ 735 13.3.10 Blocked File I/O ................................................................................................ 737 13.3.11 The Program Segment Prefix (PSP) ................................................................... 739 13.3.12 Accessing Command Line Parameters ............................................................... 742 13.3.13 ARGC and ARGV ................................................................................................ 750
13.4 UCR Standard Library File I/O Routines ........................................................................ 751
13.6 Laboratory Exercises .......................................................................................................763
13.7 Programming Projects .....................................................................................................768
14.1 The Mathematics of Floating Point Arithmetic ...............................................................771
14.2 IEEE Floating Point Formats ............................................................................................774
Page xvi
The Art of Assembly Language
14.7 Programming Projects .................................................................................................... 814
14.8 Summary ......................................................................................................................... 814
15.1.1 How the String Instructions Operate ................................................................... 819 15.1.2 The REP/REPE/REPZ and REPNZ/REPNE Prefixes ............................................. 820 15.1.3 The Direction Flag ............................................................................................... 821 15.1.4 The MOVS Instruction ......................................................................................... 822 15.1.5 The CMPS Instruction .......................................................................................... 826 15.1.6 The SCAS Instruction ........................................................................................... 828 15.1.7 The STOS Instruction ........................................................................................... 828 15.1.8 The LODS Instruction .......................................................................................... 829 15.1.9 Building Complex String Functions from LODS and STOS ................................ 830 15.1.10 Prefixes and the String Instructions ................................................................... 830
15.2 Character Strings ............................................................................................................. 831
Page xvii
15.4.1 StrBDel, StrBDelm ................................................................................................846 15.4.2 Strcat, Strcatl, Strcatm, Strcatml ............................................................................847 15.4.3 Strchr .....................................................................................................................848 15.4.4 Strcmp, Strcmpl, Stricmp, Stricmpl .......................................................................848 15.4.5 Strcpy, Strcpyl, Strdup, Strdupl .............................................................................849 15.4.6 Strdel, Strdelm .......................................................................................................850 15.4.7 Strins, Strinsl, Strinsm, Strinsml ............................................................................851 15.4.8 Strlen .....................................................................................................................852 15.4.9 Strlwr, Strlwrm, Strupr, Struprm ...........................................................................852 15.4.10 Strrev, Strrevm .....................................................................................................853 15.4.11 Strset, Strsetm ......................................................................................................853 15.4.12 Strspan, Strspanl, Strcspan, Strcspanl .................................................................854 15.4.13 Strstr, Strstrl .........................................................................................................855 15.4.14 Strtrim, Strtrimm ..................................................................................................855 15.4.15 Other String Routines in the UCR Standard Library ...........................................856
15.7.1 Find.asm ................................................................................................................860 15.7.2 StrDemo.asm .........................................................................................................862 15.7.3 Fcmp.asm ..............................................................................................................865
15.8 Laboratory Exercises .......................................................................................................868
Chapter 16 Pattern Matching ....................................................................................... 883
16.1 An Introduction to Formal Language (Automata) Theory .............................................883
The Art of Assembly Language
16.3.1 Spancset ............................................................................................................... 914 16.3.2 Brkcset .................................................................................................................. 915 16.3.3 Anycset ................................................................................................................. 915 16.3.4 Notanycset ........................................................................................................... 916 16.3.5 MatchStr ................................................................................................................ 916 16.3.6 MatchiStr ............................................................................................................... 916 16.3.7 MatchToStr ........................................................................................................... 917 16.3.8 MatchChar ............................................................................................................ 917 16.3.9 MatchToChar ........................................................................................................ 918 16.3.10 MatchChars ......................................................................................................... 918 16.3.11 MatchToPat ........................................................................................................ 918 16.3.12 EOS ..................................................................................................................... 919 16.3.13 ARB ..................................................................................................................... 919 16.3.14 ARBNUM ............................................................................................................ 920 16.3.15 Skip ..................................................................................................................... 920 16.3.16 Pos ...................................................................................................................... 921 16.3.17 RPos .................................................................................................................... 921 16.3.18 GotoPos .............................................................................................................. 921 16.3.19 RGotoPos ........................................................................................................... 922 16.3.20 SL_Match2 .......................................................................................................... 922
16.8 Some Sample Pattern Matching Applications ................................................................ 935
16.8.1 Converting Written Numbers to Integers ............................................................ 935 16.8.2 Processing Dates .................................................................................................. 941 16.8.3 Evaluating Arithmetic Expressions ...................................................................... 948 16.8.4 A Tiny Assembler ................................................................................................. 953 16.8.5 The “MADVENTURE” Game ................................................................................ 963
16.12 Questions ...................................................................................................................... 991
Section Four: ............................................................................................................... 993
17.3 Exceptions ...................................................................................................................... 1000
17.3.1 Divide Error Exception (INT 0) ........................................................................... 1000 17.3.2 Single Step (Trace) Exception (INT 1) ................................................................ 1000 17.3.3 Breakpoint Exception (INT 3) ............................................................................. 1001 17.3.4 Overflow Exception (INT 4/INTO) ..................................................................... 1001 17.3.5 Bounds Exception (INT 5/BOUND) ................................................................... 1001 17.3.6 Invalid Opcode Exception (INT 6) ...................................................................... 1004
17.5 Chaining Interrupt Service Routines ...............................................................................1010
17.6 Reentrancy Problems ......................................................................................................1012
Chapter 18 Resident Programs ................................................................................... 1025
18.1 DOS Memory Usage and TSRs ........................................................................................1025
18.5 Installing a TSR ................................................................................................................1035
18.6 Removing a TSR ...............................................................................................................1037
Chapter 19 Processes, Coroutines, and Concurrency ............................................... 1065
19.1 DOS Processes .................................................................................................................1065