http://www.phatcode.net/res/223/files/html/toc.html The Art of ASSEMBLY LANGUAGE PROGRAMMING Forward Why Would Anyone Learn This Stuff? 1. What's Wrong With Assembly Language 2. What's Right With Assembly Language? 3. Organization of This Text and Pedagogical Concerns 4. Obtaining Program Source Listings and Other Materials in This Text Section One: Machine Organization CHAPTER ONE: DATA REPRESENTATION 1.0 - Chapter Overview 1.7 - Signed and Unsigned Numbers 1.1 - Numbering Systems 1.1.1 - A Review of the Decimal 1.8 - Sign and Zero Extension System 1.9 - Shifts and Rotates 1.1.2 - The Binary Numbering 1.10 - Bit Fields and Packed Data System 1.11 - The ASCII Character Set 1.1.3 - Binary Formats 1.12 Summary 1.2 - Data Organization 1.13 Laboratory Exercises 1.2.1 - Bits 1.13.1 Installing the Software 1.2.2 - Nibbles 1.13.2 Data Conversion Exercises 1.2.3 - Bytes 1.13.3 Logical Operations 1.2.4 - Words Exercises 1.2.5 - Double Words 1.13.4 Sign and Zero Extension 1.3 - The Hexadecimal Numbering Exercises System 1.13.5 Packed Data Exercises 1.4 - Arithmetic Operations on 1.14 Questions Binary and Hexadecimal Numbers 1.15 Programming Projects 1.5 - Logical Operations on Bits 1.6 - Logical Operations on Binary Numbers and Bit Strings CHAPTER TWO: BOOLEAN ALGEBRA 2.0 - Chapter Overview 2.7 - Okay, What Does It Have To 2.1 - Boolean Algebra Do With Programming, Then? 2.2 - Boolean Functions and Truth 2.8 - Generic Boolean Functions Tables 2.9 Laboratory Exercises 2.3 - Algebraic Manipulation of 2.9.1 Truth Tables and Logic Boolean Expressions Equations Exercises 2.4 - Canonical Forms 2.9.2 Canonical Logic Equations 2.5 - Simplification of Boolean Exercises Functions 2.9.3 Optimization Exercises 2.6 - What Does This Have To Do 2.9.4 Logic Evaluation Exercises With Computers, Anyway? 2.10 Programming Projects 2.6.1 - Correspondence Between 2.11 Summary Electronic Circuits and Boolean 2.12 Questions Functions 2.6.2 - Combinatorial Circuits 2.6.3 - Sequential and Clocked Logic CHAPTER THREE: SYSTEM ORGANIZATION 3.0 - Chapter Overview 3.3.9 - The Differences Between 3.1 - The Basic System Components the x86 Processors 3.1.1 - The System Bus 3.3.10 - The 886 Processor 3.1.1.1 - The Data Bus 3.3.11 - The 8286 Processor 3.1.1.2 - The Address Bus 3.3.12 - The 8486 Processor 3.1.1.3 - The Control Bus 3.3.12.1 - The 8486 Pipeline 3.1.2 - The Memory Subsystem 3.3.12.2 - Stalls in a Pipeline 3.1.3 - The I/O Subsystem 3.3.12.3 - Cache, the Prefetch 3.2 - System Timing Queue, and the 8486 3.2.1 - The System Clock 3.3.12.4 - Hazards on the 8486 3.2.2 - Memory Access and the 3.3.13 - The 8686 Processor System Clock 3.4 - I/O (Input/Output) 3.2.3 - Wait States 3.5 - Interrupts and Polled I/O 3.2.4 - Cache Memory 3.6 Laboratory Exercises 3.3 - The 886, 8286, 8486, and 3.6.1 The SIMx86 Program - Some 8686 "Hypothetical" Processors Simple x86 Programs 3.3.1 - CPU Registers 3.6.2 Simple I/O-Mapped Input/ 3.3.2 - The Arithmetic & Logical Output Operations Unit 3.6.3 Memory Mapped I/O 3.3.3 - The Bus Interface Unit 3.6.4 DMA Exercises 3.3.4 - The Control Unit and 3.6.5 Interrupt Driven I/O Instruction Sets Exercises 3.3.5 - The x86 Instruction Set 3.6.6 Machine Language 3.3.6 - Addressing Modes on the Programming & Instruction x86 Encoding Exercises 3.3.7 - Encoding x86 Instructions 3.6.7 Self Modifying Code 3.3.8 - Step-by-Step Instruction Exercises Execution 3.7 Programming Projects 3.8 Summary 3.9 Questions CHAPTER FOUR: MEMORY LAYOUT AND ACCESS 4.0 - Chapter Overview 4.9 Laboratory Exercises 4.1 - The 80x86 CPUs:A 4.9.1 The UCR Standard Library Programmer's View for 80x86 Assembly Language 4.1.1 - 8086 General Purpose Programmers Registers 4.9.2 Editing Your Source Files 4.1.2 - 8086 Segment Registers 4.9.3 The SHELL.ASM File 4.1.3 - 8086 Special Purpose 4.9.4 Assembling Your Code with Registers MASM 4.1.4 - 80286 Registers 4.9.5 Debuggers and CodeView' 4.1.5 - 80386/80486 Registers 4.9.5.1 A Quick Look at CodeView 4.2 - 80x86 Physical Memory 4.9.5.2 The Source Window Organization 4.9.5.3 The Memory Window 4.3 - Segments on the 80x86 4.9.5.4 The Register Window 4.4 - Normalized Addresses on the 4.9.5.5 The Command Window 80x86 4.9.5.6 The Output Menu Item 4.5 - Segment Registers on the 4.9.5.7 The CodeView Command 80x86 Window 4.6 - The 80x86 Addressing Modes 4.9.5.7.1 The Radix Command (N) 4.6.1 - 8086 Register Addressing 4.9.5.7.2 The Assemble Command Modes 4.9.5.7.3 The Compare Memory 4.6.2 - 8086 Memory Addressing Command Modes 4.9.5.7.4 The Dump Memory Command 4.6.2.1 - The Displacement Only 4.9.5.7.5 The Enter Command Addressing Mode 4.9.5.7.6 The Fill Memory Command 4.6.2.2 - The Register Indirect 4.9.5.7.7 The Move Memory Command Addressing Modes 4.9.5.7.8 The Input Command 4.6.2.3 - Indexed Addressing 4.9.5.7.9 The Output Command Modes 4.9.5.7.10 The Quit Command 4.6.2.4 - Based Indexed 4.9.5.7.11 The Register Command Addressing Modes 4.9.5.7.12 The Unassemble Command 4.6.2.5 - Based Indexed Plus 4.9.5.8 CodeView Function Keys Displacement Addressing Mode 4.9.5.9 Some Comments on CodeView 4.6.2.6 - MASM Syntax for 8086 Addresses Memory Addressing Modes 4.9.5.10 A Wrap on CodeView 4.6.2.7 - An Easy Way to Remember 4.9.6 Laboratory Tasks the 8086 Memory Addressing Modes 4.10 Programming Projects 4.6.2.8 - Some Final Comments 4.11 Summary About 8086 Addressing Modes 4.12 Questions 4.6.3 - 80386 Register Addressing Modes 4.6.4 - 80386 Memory Addressing Modes 4.6.4.1 - Register Indirect Addressing Modes 4.6.4.2 - 80386 Indexed, Base/ Indexed, and Base/Indexed/Disp Addressing Modes 4.6.4.3 - 80386 Scaled Indexed Addressing Modes 4.6.4.4 - Some Final Notes About the 80386 Memory Addressing Modes 4.7 - The 80x86 MOV Instruction 4.8 - Some Final Comments on the MOV Instructions Section Two: Basic Assembly Language CHAPTER FIVE: VARIABLES AND DATA STRUCTURES 5.0 - Chapter Overview 5.6.2.3 - Allocating Storage for 5.1 - Some Additional Multidimensional Arrays Instructions: LEA, LES, ADD, and 5.6.2.4 - Accessing MUL Multidimensional Array Elements 5.2 - Declaring Variables in an in Assembly Language Assembly Language Program 5.6.3 - Structures 5.3 - Declaring and Accessing 5.6.4 - Arrays of Structures and Scalar Variables Arrays/Structures as Structure 5.3.1 - Declaring and using BYTE Fields Variables 5.6.5 - Pointers to Structures 5.3.2 - Declaring and using WORD 5.7 - Sample Programs Variables 5.7.1 - Simple Variable 5.3.3 - Declaring and using DWORD Declarations Variables 5.7.2 - Using Pointer Variables 5.3.4 - Declaring and using 5.7.3 - Single Dimension Array FWORD, QWORD, and TBYTE Variables Access 5.3.5 - Declaring Floating Point 5.7.4 - Multidimensional Array Variables with REAL4, REAL8, and Access REAL10 5.7.5 - Simple Structure Access 5.4 - Creating Your Own Type 5.7.6 - Arrays of Structures Names with TYPEDEF 5.7.7 - Structures and Arrays as 5.5 - Pointer Data Types Fields of Another Structure 5.6 - Composite Data Types 5.7.8 - Pointers to Structures 5.6.1 - Arrays and Arrays of Structure 5.6.1.1 - Declaring Arrays in 5.8 Laboratory Exercises Your Data Segment 5.9 Programming Projects 5.6.1.2 - Accessing Elements of a 5.10 Summary Single Dimension Array 5.11 Questions 5.6.2 - Multidimensional Arrays 5.6.2.1 - Row Major Ordering 5.6.2.2 - Column Major Ordering CHAPTER SIX: THE 80x86 INSTRUCTION SET 6.0 - Chapter Overview 6.6.3.3 - ROL 6.1 - The Processor Status 6.6.3.4 - ROR Register (Flags) 6.6.4 - The Bit Operations 6.2 - Instruction Encodings 6.6.4.1 - TEST 6.3 - Data Movement Instructions 6.6.4.2 - The Bit Test 6.3.1 - The MOV Instruction Instructions: BT, BTS, BTR, and 6.3.2 - The XCHG Instruction BTC 6.3.3 - The LDS, LES, LFS, LGS, 6.6.4.3 - Bit Scanning: BSF and and LSS Instructions BSR 6.3.4 - The LEA Instruction 6.6.5 - The "Set on Condition" 6.3.5 - The PUSH and POP Instructions Instructions 6.7 - I/O Instructions 6.3.6 - The LAHF and SAHF 6.8 - String Instructions Instructions 6.9 - Program Flow Control 6.4 - Conversions Instructions 6.4.1 - The MOVZX, MOVSX, CBW, 6.9.1 - Unconditional Jumps CWD, CWDE, and CDQ Instructions 6.9.2 - The CALL and RET 6.4.2 - The BSWAP Instruction Instructions 6.4.3 - The XLAT Instruction 6.9.3 - The INT, INTO, BOUND, and 6.5 - Arithmetic Instructions IRET Instructions 6.5.1 - The Addition 6.9.4 - The Conditional Jump Instructions: ADD, ADC, INC, Instructions XADD, AAA, and DAA 6.9.5 - The JCXZ/JECXZ 6.5.1.1 - The ADD and ADC Instructions Instructions 6.9.6 - The LOOP Instruction 6.5.1.2 - The INC Instruction 6.9.7 - The LOOPE/LOOPZ 6.5.1.3 - The XADD Instruction Instruction 6.5.1.4 - The AAA and DAA 6.9.8 - The LOOPNE/LOOPNZ Instructions Instruction 6.5.2 - The Subtraction 6.10 - Miscellaneous Instructions Instructions: SUB, SBB, DEC, AAS, 6.11 - Sample Programs and DAS 6.11.1 - Simple Arithmetic I 6.5.3 - The CMP Instruction 6.11.2 - Simple Arithmetic II 6.5.4 - The CMPXCHG, and 6.11.3 - Logical Operations CMPXCHG8B Instructions 6.11.4 - Shift and Rotate 6.5.5 - The NEG Instruction Operations 6.5.6 - The Multiplication 6.11.5 - Bit Operations and SETcc Instructions: MUL, IMUL, and AAM Instructions 6.5.7 - The Division 6.11.6 - String Operations Instructions: DIV, IDIV, and AAD 6.11.7 - Conditional Jumps 6.6 - Logical, Shift, Rotate and 6.11.8 - CALL and INT Bit Instructions Instructions 6.6.1 - The Logical Instructions: 6.11.9 - Conditional Jumps I AND, OR, XOR, and NOT 6.11.10 - Conditional Jump 6.6.2 - The Shift Instructions: Instructions II SHL/SAL, SHR, SAR, SHLD, and SHRD 6.12 Laboratory Exercises 6.6.2.1 - SHL/SAL 6.12.1 The IBM/L System 6.6.2.2 - SAR 6.12.2 IBM/L Exercises 6.6.2.3 - SHR 6.13 Programming Projects 6.6.2.4 - The SHLD and SHRD 6.14 Summary Instructions 6.15 Questions 6.6.3 - The Rotate Instructions: RCL, RCR, ROL, and ROR 6.6.3.1 - RCL 6.6.3.2 - RCR CHAPTER SEVEN: THE UCR STANDARD LIBRARY 7.0 - Chapter Overview 7.1.11 - Plus more! 7.1 - An Introduction to the UCR 7.2 - Sample Programs Standard Library 7.2.1 - Stripped SHELL.ASM File 7.1.1 - Memory Management 7.2.2 - Numeric I/O Routines: MEMINIT, MALLOC, and 7.3 Laboratory Exercises FREE 7.3.1 Obtaining the UCR Standard 7.1.2 - The Standard Input Library Routines: GETC, GETS, GETSM 7.3.2 Unpacking the Standard 7.1.3 - The Standard Output Library Routines: PUTC, PUTCR, PUTS, 7.3.3 Using the Standard Library PUTH, PUTI, PRINT, and PRINTF 7.3.4 The Standard Library 7.1.4 - Formatted Output Documentation Files Routines: Putisize, Putusize, 7.4 Programming Projects Putlsize, and Putulsize 7.5 Summary 7.1.5 - Output Field Size 7.6 Questions Routines: Isize, Usize, and Lsize 7.1.6 - Conversion Routines: ATOx, and xTOA 7.1.7 - Routines that Test Characters for Set Membership 7.1.8 - Character Conversion Routines: ToUpper, ToLower 7.1.9 - Random Number Generation: Random, Randomize 7.1.10 - Constants, Macros, and other Miscellany CHAPTER EIGHT: MASM: DIRECTIVES & PSEUDO-OPCODES 8.0 - Chapter Overview 8.13.5 - IFIDN, IFDIF, IFIDNI, 8.1 - Assembly Language and IFDIFI Statements 8.14 - Macros 8.2 - The Location Counter 8.14.1 - Procedural Macros 8.3 - Symbols 8.14.2 - Macros vs. 80x86 8.4 - Literal Constants Procedures 8.4.1 - Integer Constants 8.14.3 - The LOCAL Directive 8.4.2 - String Constants 8.14.4 - The EXITM Directive 8.4.3 - Real Constants 8.14.5 - Macro Parameter 8.4.4 - Text Constants Expansion and Macro Operators 8.5 - Declaring Manifest 8.14.6 - A Sample Macro to Constants Using Equates Implement For Loops 8.6 - Processor Directives 8.14.7 - Macro Functions 8.7 - Procedures 8.14.8 - Predefined Macros, Macro 8.8 - Segments Functions, and Symbols 8.8.1 - Segment Names 8.14.9 - Macros vs. Text Equates 8.8.2 - Segment Loading Order 8.14.10 - Macros: Good and Bad 8.8.3 - Segment Operands News 8.8.3.1 - The ALIGN Type 8.15 - Repeat Operations 8.8.3.2 - The COMBINE Type 8.16 - The FOR and FORC Macro 8.8.4 - The CLASS Type Operations 8.8.5 - The Read-only Operand 8.17 - The WHILE Macro Operation 8.8.6 - The USE16, USE32, and 8.18 - Macro Parameters FLAT Options 8.19 - Controlling the Listing 8.8.7 - Typical Segment 8.19.1 - The ECHO and %OUT Definitions Directives 8.8.8 - Why You Would Want to 8.19.2 - The TITLE Directive Control the Loading Order 8.19.3 - The SUBTTL Directive 8.8.9 - Segment Prefixes 8.19.4 - The PAGE Directive 8.8.10 - Controlling Segments 8.19.5 - The .LIST, .NOLIST, and with the ASSUME Directive .XLIST Directives 8.8.11 - Combining Segments: The 8.19.6 - Other Listing Directives GROUP Directive 8.20 - Managing Large Programs 8.8.12 - Why Even Bother With 8.20.1 - The INCLUDE Directive Segments? 8.20.2 - The PUBLIC, EXTERN, and 8.9 - The END Directive EXTRN Directives 8.10 - Variables 8.20.3 - The EXTERNDEF Directive 8.11 - Label Types 8.21 - Make Files 8.11.1 - How to Give a Symbol a 8.22 - Sample Program Particular Type 8.22.1 - EX8.MAK 8.11.2 - Label Values 8.22.2 - Matrix.A 8.11.3 - Type Conflicts 8.22.3 - EX8.ASM 8.12 - Address Expressions 8.22.4 - GETI.ASM 8.12.1 - Symbol Types and 8.22.5 - GetArray.ASM Addressing Modes 8.22.6 - XProduct.ASM 8.12.2 - Arithmetic and Logical 8.23 Laboratory Exercises Operators 8.23.1 Near vs. Far Procedures 8.12.3 - Coercion 8.23.2 Data Alignment Exercises 8.12.4 - Type Operators 8.23.3 Equate Exercise 8.12.5 - Operator Precedence 8.23.4 IFDEF Exercise 8.13 - Conditional Assembly 8.23.5 Make File Exercise 8.13.1 - IF Directive 8.24 Programming Projects 8.13.2 - IFE directive 8.25 Summary 8.13.3 - IFDEF and IFNDEF 8.26 Questions 8.13.4 - IFB, IFNB CHAPTER NINE: ARITHMETIC AND LOGICAL OPERATIONS 9.0 - Chapter Overview 9.5.6 - TEST Operations 9.1 - Arithmetic Expressions 9.5.7 - Testing Signs with the 9.1.1 - Simple Assignments XOR Instruction 9.1.2 - Simple Expressions 9.6 - Masking Operations 9.1.3 - Complex Expressions 9.6.1 - Masking Operations with 9.1.4 - Commutative Operators the AND Instruction 9.2 - Logical (Boolean) 9.6.2 - Masking Operations with Expressions the OR Instruction 9.3 - Multiprecision Operations 9.7 - Packing and Unpacking Data 9.3.1 - Multiprecision Addition Types Operations 9.8 - Tables 9.3.2 - Multiprecision 9.8.1 - Function Computation via Subtraction Operations Table Look Up 9.3.3 - Extended Precision 9.8.2 - Domain Conditioning Comparisons 9.8.3 - Generating Tables 9.3.4 - Extended Precision 9.9 - Sample Programs Multiplication 9.9.1 - Converting Arithmetic 9.3.5 - Extended Precision Expressions to Assembly Language Division 9.9.2 - Boolean Operations 9.3.6 - Extended Precision NEG Example Operations 9.9.3 - 64-bit Integer I/O 9.3.7 - Extended Precision AND 9.9.4 - Packing and Unpacking Operations Date Data Types 9.3.8 - Extended Precision OR 9.10 Laboratory Exercises Operations 9.10.1 Debugging Programs with 9.3.9 - Extended Precision XOR CodeView Operations 9.10.2 Debugging Strategies 9.3.10 - Extended Precision NOT 9.10.2.1 Locating Infinite Loops Operations 9.10.2.2 Incorrect Computations 9.3.11 - Extended Precision Shift 9.10.2.3 Illegal Instructions/ Operations Infinite Loops Part II 9.3.12 - Extended Precision 9.10.3 Debug Exercise I: Using Rotate Operations CodeView to Find Bugs in a 9.4 - Operating on Different Calculation Sized Operands 9.10.4 Software Delay Loop 9.5 - Machine and Arithmetic Exercises Idioms 9.11 Programming Projects 9.5.1 - Multiplying Without MUL 9.12 Summary and IMUL 9.13 Questions 9.5.2 - Division Without DIV and IDIV 9.5.3 - Using AND to Compute Remainders 9.5.4 - Implementing Modulo-n Counters with AND 9.5.5 - Testing an Extended Precision Value for 0FFFF..FFh CHAPTER TEN: CONTROL STRUCTURES 10.0 - Chapter Overview 10.8.5 - Induction Variables 10.1 - Introduction to Decisions 10.8.6 - Other Performance 10.2 - IF..THEN..ELSE Sequences Improvements 10.3 - CASE Statements 10.9 - Nested Statements 10.4 - State Machines and 10.10 - Timing Delay Loops Indirect Jumps 10.11 - Sample Program 10.5 - Spaghetti Code 10.12 Laboratory Exercises 10.6 - Loops 10.12.1 The Physics of Sound 10.6.1 - While Loops 10.12.2 The Fundamentals of Music 10.6.2 - Repeat..Until Loops 10.12.3 The Physics of Music 10.6.3 - LOOP..ENDLOOP Loops 10.12.4 The 8253/8254 Timer Chip 10.6.4 - FOR Loops 10.12.5 Programming the Timer 10.7 - Register Usage and Loops Chip to Produce Musical Tones 10.8 - Performance Improvements 10.12.6 Putting it All Together 10.8.1 - Moving the Termination 10.12.7 Amazing Grace Exercise Condition to the End of a Loop 10.13 Programming Projects 10.8.2 - Executing the Loop 10.14 Summary Backwards 10.15 Questions 10.8.3 - Loop Invariant Computations 10.8.4 - Unraveling Loops CHAPTER ELEVEN: PROCEDURES AND FUNCTIONS 11.0 - Chapter Overview 11.5.10 - Passing Parameters in 11.1 - Procedures the Code Stream 11.2 - Near and Far Procedures 11.5.11 - Passing Parameters via 11.2.1 - Forcing NEAR or FAR a Parameter Block CALLs and Returns 11.6 - Function Results 11.2.2 - Nested Procedures 11.6.1 - Returning Function 11.3 - Functions Results in a Register 11.4 - Saving the State of the 11.6.2 - Returning Function Machine Results on the Stack 11.5 - Parameters 11.6.3 - Returning Function 11.5.1 - Pass by Value Results in Memory Locations 11.5.2 - Pass by Reference 11.7 - Side Effects 11.5.3 - Pass by Value-Returned 11.8 - Local Variable Storage 11.5.4 - Pass by Result 11.9 - Recursion 11.5.5 - Pass by Name 11.10 - Sample Program 11.5.6 - Pass by Lazy-Evaluation 11.11 Laboratory Exercises 11.5.7 - Passing Parameters in 11.11.1 Ex11_1.cpp Registers 11.11.2 Ex11_1.asm 11.5.8 - Passing Parameters in 11.11.3 EX11_1a.asm Global Variables 11.12 Programming Projects 11.5.9 - Passing Parameters on 11.13 Summary the Stack 11.14 Questions Section Three: Intermediate Level Assembly Language Programming CHAPTER TWELVE: PROCEDURES: ADVANCED TOPICS 12.0 - Chapter Overview 12.3.4 - Passing Lazy Evaluation 12.1 - Lexical Nesting, Static Parameters as Parameters Links, and Displays 12.3.5 - Parameter Passing 12.1.1 - Scope Summary 12.1.2 - Unit Activation, Address 12.4 - Passing Procedures as Binding, and Variable Lifetime Parameters 12.1.3 - Static Links 12.5 - Iterators 12.1.4 - Accessing Non-Local 12.5.1 - Implementing Iterators Variables Using Static Links Using In-Line Expansion 12.1.5 - The Display 12.5.2 - Implementing Iterators 12.1.6 - The 80286 ENTER and with Resume Frames LEAVE Instructions 12.6 - Sample Programs 12.2 - Passing Variables at 12.6.1 - An Example of an Different Lex Levels as Iterator Parameters. 12.6.2 - Another Iterator Example 12.2.1 - Passing Parameters by 12.7 Laboratory Exercises Value in a Block Structured 12.7.1 Iterator Exercise Language 12.7.2 The 80x86 Enter and Leave 12.2.2 - Passing Parameters by Instructions Reference, Result, and 12.7.3 Parameter Passing Value-Result in a Block Exercises Structured Language 12.8 Programming Projects 12.2.3 - Passing Parameters by 12.9 Summary Name and Lazy-Evaluation in a 12.10 Questions Block Structured Language 12.3 - Passing Parameters as Parameters to Another Procedure 12.3.1 - Passing Reference Parameters to Other Procedures 12.3.2 - Passing Value-Result and Result Parameters as Parameters 12.3.3 - Passing Name Parameters to Other Procedures CHAPTER THIRTEEN: MS-DOS, PC-BIOS AND FILE I/O 13.0 - Chapter Overview 13.3.7.2 - Terminate, but Stay 13.1 - The IBM PC BIOS Resident 13.2 - An Introduction to the 13.3.7.3 - Execute a Program BIOS' Services 13.3.8 - MS-DOS "New" Filing 13.2.1 - INT 5- Print Screen Calls 13.2.2 - INT 10h - Video Services 13.3.8.1 - Open File 13.2.3 - INT 11h - Equipment 13.3.8.2 - Create File Installed 13.3.8.3 - Close File 13.2.4 - INT 12h - Memory 13.3.8.4 - Read From a File Available 13.3.8.5 - Write to a File 13.2.5 - INT 13h - Low Level Disk 13.3.8.6 - Seek (Move File Services Pointer) 13.2.6 - INT 14h - Serial I/O 13.3.8.7 - Set Disk Transfer 13.2.6.1 - AH=0: Serial Port Address (DTA) Initialization 13.3.8.8 - Find First File 13.2.6.2 - AH=1: Transmit a 13.3.8.9 - Find Next File Character to the Serial Port 13.3.8.10 - Delete File 13.2.6.3 - AH=2: Receive a 13.3.8.11 - Rename File Character from the Serial Port 13.3.8.12 - Change/Get File 13.2.6.4 - AH=3: Serial Port Attributes Status 13.3.8.13 - Get/Set File Date and 13.2.7 - INT 15h - Miscellaneous Time Services 13.3.8.14 - Other DOS Calls 13.2.8 - INT 16h - Keyboard 13.3.9 - File I/O Examples Services 13.3.9.1 - Example #1: A Hex Dump 13.2.8.1 - AH=0: Read a Key From Utility the Keyboard 13.3.9.2 - Example #2: Upper Case 13.2.8.2 - AH=1: See if a Key is Conversion Available at the Keyboard 13.3.10 - Blocked File I/O 13.2.8.3 - AH=2: Return Keyboard 13.3.11 - The Program Segment Shift Key Status Prefix (PSP) 13.2.9 - INT 17h - Printer 13.3.12 - Accessing Command Line Services Parameters 13.2.9.1 - AH=0: Print a 13.3.13 - ARGC and ARGV Character 13.4 - UCR Standard Library File 13.2.9.2 - AH=1: Initialize I/O Routines Printer 13.4.1 - Fopen 13.2.9.3 - AH=2: Return Printer 13.4.2 - Fcreate Status 13.4.3 - Fclose 13.2.10 - INT 18h - Run BASIC 13.4.4 - Fflush 13.2.11 - INT 19h - Reboot 13.4.5 - Fgetc Computer 13.4.6 - Fread 13.2.12 - INT 1Ah - Real Time 13.4.7 - Fputc Clock 13.4.8 - Fwrite 13.2.12.1 - AH=0: Read the Real 13.4.9 - Redirecting I/O Through Time Clock the StdLib File I/O Routines 13.2.12.2 - AH=1: Setting the 13.4.10 - A File I/O Example Real Time Clock 13.5 - Sample Program 13.3 - An Introduction to MS-DOS' 13.6 Laboratory Exercises 13.3.1 - MS-DOS Calling Sequence 13.7 Programming Projects 13.3.2 - MS-DOS Character 13.8 Summary Oriented Functions 13.9 Questions 13.3.3 - MS-DOS Drive Commands 13.3.4 - MS-DOS "Obsolete" Filing Calls 13.3.5 - MS-DOS Date and Time Functions 13.3.6 - MS-DOS Memory Management Functions 13.3.6.1 - Allocate Memory 13.3.6.2 - Deallocate Memory 13.3.6.3 - Modify Memory Allocation 13.3.6.4 - Advanced Memory Management Functions 13.3.7 - MS-DOS Process Control Functions 13.3.7.1 - Terminate Program Execution CHAPTER FOURTEEN: FLOATING POINT ARITHMETIC 14.0 - Chapter Overview 14.4.6.10 - The FABS Instruction 14.1 - The Mathematics of 14.4.6.11 - The FCHS Instruction Floating Point Arithmetic 14.4.7 - Comparison Instructions 14.2 - IEEE Floating Point 14.4.7.1 - The FCOM, FCOMP, and Formats FCOMPP Instructions 14.3 - The UCR Standard Library 14.4.7.2 - The FUCOM, FUCOMP, and Floating Point Routines FUCOMPP Instructions 14.3.1 - Load and Store Routines 14.4.7.3 - The FTST Instruction 14.3.2 - Integer/Floating Point 14.4.7.4 - The FXAM Instruction Conversion 14.4.8 - Constant Instructions 14.3.3 - Floating Point 14.4.9 - Transcendental Arithmetic Instructions 14.3.4 - Float/Text Conversion 14.4.9.1 - The F2XM1 Instruction and Printff 14.4.9.2 - The FSIN, FCOS, and 14.4 - The 80x87 Floating Point FSINCOS Instructions Coprocessors 14.4.9.3 - The FPTAN Instruction 14.4.1 - FPU Registers 14.4.9.4 - The FPATAN Instruction 14.4.1.1 - The FPU Data Registers 14.4.9.5 - The FYL2X and FYL2XP1 14.4.1.2 - The FPU Control Instructions Register 14.4.10 - Miscellaneous 14.4.1.3 - The FPU Status instructions Register 14.4.10.1 - The FINIT and FNINIT 14.4.2 - FPU Data Types Instructions 14.4.3 - The FPU Instruction Set 14.4.10.2 - The FWAIT Instruction 14.4.4 - FPU Data Movement 14.4.10.3 - The FLDCW and FSTCW Instructions Instructions 14.4.4.1 - The FLD Instruction 14.4.10.4 - The FCLEX and FNCLEX 14.4.4.2 - The FST and FSTP Instructions Instructions 14.4.10.5 - The FLDENV, FSTENV, 14.4.4.3 - The FXCH Instruction and FNSTENV Instructions 14.4.5 - Conversions 14.4.10.6 - The FSAVE, FNSAVE, 14.4.5.1 - The FILD Instruction and FRSTOR Instructions 14.4.5.2 - The FIST and FISTP 14.4.10.7 - The FSTSW and FNSTSW Instructions Instructions 14.4.5.3 - The FBLD and FBSTP 14.4.10.8 - The FINCSTP and Instructions FDECSTP Instructions 14.4.6 - Arithmetic Instructions 14.4.10.9 - The FNOP Instruction 14.4.6.1 - The FADD and FADDP 14.4.10.10 - The FFREE Instructions Instruction 14.4.6.2 - The FSUB, FSUBP, 14.4.11 - Integer Operations FSUBR, and FSUBRP Instructions 14.5 - Sample Program: Additional 14.4.6.3 - The FMUL and FMULP Trigonometric Functions Instructions 14.6 Laboratory Exercises 14.4.6.4 - The FDIV, FDIVP, 14.6.1 FPU vs StdLib Accuracy FDIVR, and FDIVRP Instructions 14.7 Programming Projects 14.4.6.5 - The FSQRT Instruction 14.8 Summary 14.4.6.6 - The FSCALE Instruction 14.9 Questions 14.4.6.7 - The FPREM and FPREM1 Instructions 14.4.6.8 - The FRNDINT Instruction 14.4.6.9 - The FXTRACT Instruction CHAPTER FIFTEEN: STRINGS AND CHARACTER SETS 15.0 - Chapter Overview 15.4.5 - Strcpy, Strcpyl, Strdup, 15.1 - The 80x86 String Strdupl Instructions 15.4.6 - Strdel, Strdelm 15.1.1 - How the String 15.4.7 - Strins, Strinsl, Instructions Operate Strinsm, Strinsml 15.1.2 - The REP/REPE/REPZ and 15.4.8 - Strlen REPNZ/REPNE Prefixes 15.4.9 - Strlwr, Strlwrm, Strupr, 15.1.3 - The Direction Flag Struprm 15.1.4 - The MOVS Instruction 15.4.10 - Strrev, Strrevm 15.1.5 - The CMPS Instruction 15.4.11 - Strset, Strsetm 15.1.6 - The SCAS Instruction 15.4.12 - Strspan, Strspanl, 15.1.7 - The STOS Instruction Strcspan, Strcspanl 15.1.8 - The LODS Instruction 15.4.13 - Strstr, Strstrl 15.1.9 - Building Complex String 15.4.14 - Strtrim, Strtrimm Functions from LODS and STOS 15.4.15 - Other String Routines 15.1.10 - Prefixes and the String in the UCR Standard Library Instructions 15.5 - The Character Set Routines 15.2 - Character Strings in the UCR Standard Library 15.2.1 - Types of Strings 15.6 - Using the String 15.2.2 - String Assignment Instructions on Other Data Types 15.2.3 - String Comparison 15.6.1 - Multi-precision Integer 15.3 - Character String Functions Strings 15.3.1 - Substr 15.6.2 - Dealing with Whole 15.3.2 - Index Arrays and Records 15.3.3 - Repeat 15.7 - Sample Programs 15.3.4 - Insert 15.7.1 - Find.asm 15.3.5 - Delete 15.7.2 - StrDemo.asm 15.3.6 - Concatenation 15.7.3 - Fcmp.asm 15.4 - String Functions in the 15.8 Laboratory Exercises UCR Standard Library 15.8.1 MOVS Performance Exercise 15.4.1 - StrBDel, StrBDelm #1 15.4.2 - Strcat, Strcatl, 15.8.2 MOVS Performance Exercise Strcatm, Strcatml #2 15.4.3 - Strchr 15.8.3 Memory Performance 15.4.4 - Strcmp, Strcmpl, Exercise Stricmp, Stricmpl 15.8.4 The Performance of Length-Prefixed vs. Zero-Terminated Strings 15.9 Programming Projects 15.10 Summary 15.11 Questions CHAPTER SIXTEEN: PATTERN MATCHING 16.1 - An Introduction to Formal 16.3.11 - MatchToPat Language (Automata) Theory 16.3.12 - EOS 16.1.1 - Machines vs. Languages 16.3.13 - ARB 16.1.2 - Regular Languages 16.3.14 - ARBNUM 16.1.2.1 - Regular Expressions 16.3.15 - Skip 16.1.2.2 - Nondeterministic 16.3.16 - Pos Finite State Automata (NFAs) 16.3.17 - RPos 16.1.2.3 - Converting Regular 16.3.18 - GotoPos Expressions to NFAs 16.3.19 - RGotoPos 16.1.2.4 - Converting an NFA to 16.3.20 - SL_Match2 Assembly Language 16.4 - Designing Your Own Pattern 16.1.2.5 - Deterministic Finite Matching Routines State Automata (DFAs) 16.5 - Extracting Substrings from 16.1.2.6 - Converting a DFA to Matched Patterns Assembly Language 16.6 - Semantic Rules and Actions 16.1.3 - Context Free Languages 16.7 - Constructing Patterns for 16.1.4 - Eliminating Left the MATCH Routine Recursion and Left Factoring CFGs 16.8 - Some Sample Pattern 16.1.5 - Converting REs to CFGs Matching Applications 16.1.6 - Converting CFGs to 16.8.1 - Converting Written Assembly Language Numbers to Integers 16.1.7 - Some Final Comments on 16.8.2 - Processing Dates CFGs 16.8.3 - Evaluating Arithmetic 16.1.8 - Beyond Context Free Expressions Languages 16.8.4 - A Tiny Assembler 16.2 - The UCR Standard Library 16.8.5 - The "MADVENTURE" Game Pattern Matching Routines 16.9 - Laboratory Exercises 16.3 - The Standard Library 16.9.1 - Checking for Stack Pattern Matching Functions Overflow (Infinite Loops) 16.3.1 - Spancset 16.9.2 - Printing Diagnostic 16.3.2 - Brkcset Messages from a Pattern 16.3.3 - Anycset 16.10 Programming Projects 16.3.4 - Notanycset 16.11 Summary 16.3.5 - MatchStr 16.12 Questions 16.3.6 - MatchiStr 16.3.7 - MatchToStr 16.3.8 - MatchChar 16.3.9 - MatchToChar 16.3.10 - MatchChars Section Four: Advanced Assembly Language Programming CHAPTER SEVENTEEN: INTERRUPTS, TRAPS AND EXEPTIONS 17.1 - 80x86 Interrupt Structure 17.4.5 - The Parallel Port and Interrupt Service Routines Interrupts (INT 0Dh and INT 0Fh) (ISRs) 17.4.6 - The Diskette and Hard 17.2 - Traps Drive Interrupts (INT 0Eh and INT 17.3 - Exceptions 76h) 17.3.1 - Divide Error Exception 17.4.7 - The Real-Time Clock (INT 0) Interrupt (INT 70h) 17.3.2 - Single Step (Trace) 17.4.8 - The FPU Interrupt (INT Exception (INT 1) 75h) 17.3.3 - Breakpoint Exception 17.4.9 - Nonmaskable Interrupts (INT 3) (INT 2) 17.3.4 - Overflow Exception (INT 17.4.10 - Other Interrupts 4/INTO) 17.5 - Chaining Interrupt Service 17.3.5 - Bounds Exception (INT 5/ Routines BOUND) 17.6 - Reentrancy Problems 17.3.6 - Invalid Opcode Exception 17.7 - The Efficiency of an (INT 6) Interrupt Driven System 17.3.7 - Coprocessor Not 17.7.1 - Interrupt Driven I/O vs. Available (INT 7) Polling 17.4 - Hardware Interrupts 17.7.2 - Interrupt Service Time 17.4.1 - The 8259A Programmable 17.7.3 - Interrupt Latency Interrupt Controller (PIC) 17.7.4 - Prioritized Interrupts 17.4.2 - The Timer Interrupt (INT 17.8 - Debugging ISRs 8) 17.9 Summary 17.4.3 - The Keyboard Interrupt (INT 9) 17.4.4 - The Serial Port Interrupts (INT 0Bh and INT 0Ch) CHAPTER EIGHTEEN: RESIDENT PROGRAMS 18.1 - DOS Memory Usage and TSRs 18.5 - Installing a TSR 18.2 - Active vs. Passive TSRs 18.6 - Removing a TSR 18.3 - Reentrancy 18.7 - Other DOS Related Issues 18.3.1 - Reentrancy Problems with 18.8 - A Keyboard Monitor TSR DOS 18.9 - Semiresident Programs 18.3.2 - Reentrancy Problems with 18.10 Summary BIOS 18.3.3 - Reentrancy Problems with Other Code 18.4 - The Multiplex Interrupt (INT 2Fh) CHAPTER NINETEEN: PROCESSES, COROUTINES AND CONCURRENCY 19.1 - DOS Processes 19.3.1 - AMAZE.ASM 19.1.1 - Child Processes in DOS 19.3.2 - 32-bit Coroutines 19.1.1.1 - Load and Execute 19.4 - Multitasking 19.1.1.2 - Load Program 19.4.1 - Lightweight and 19.1.1.3 - Loading Overlays HeavyWeight Processes 19.1.1.4 - Terminating a Process 19.4.2 - The UCR Standard Library 19.1.1.5 - Obtaining the Child Processes Package Process Return Code 19.4.3 - Problems with 19.1.2 - Exception Handling in Multitasking DOS: The Break Handler 19.4.4 - A Sample Program with 19.1.3 - Exception Handling in Threads DOS: The Critical Error Handler 19.5 - Synchronization 19.1.4 - Exception Handling in 19.5.1 - Atomic Operations, Test DOS: Traps & Set, and Busy-Waiting 19.1.5 - Redirection of I/O for 19.5.2 - Semaphores Child Processes 19.5.3 - The UCR Standard Library 19.2 - Shared Memory Semaphore Support 19.2.1 - Static Shared Memory 19.5.4 - Using Semaphores to 19.2.2 - Dynamic Shared Memory Protect Critical Regions 19.3 - Coroutines 19.5.5 - Using Semaphores for Barrier Synchronization 19.6 - Deadlock 19.7 Summary Section Five: The PC'S I/O PORTS CHAPTER TWENTY: THE PC KEYBOARD 20.1 - Keyboard Basics 20.7 - Simulating Keystrokes 20.2 - The Keyboard Hardware 20.7.1 - Stuffing Characters in Interface the Type Ahead Buffer 20.3 - The Keyboard DOS Interface 20.7.2 - Using the 80x86 Trace 20.4 - The Keyboard BIOS Flag to Simulate IN AL, 60H Interface Instructions 20.5 - The Keyboard Interrupt 20.7.3 - Using the 8042 Service Routine Microcontroller to Simulate 20.6 - Patching into the INT 9 Keystrokes Interrupt Service Routine 20.8 Summary CHAPTER TWENTY ONE: THE PC PARALLEL PORTS 21.1 - Basic Parallel Port 21.3.2 - Printing via BIOS Information 21.3.3 - An INT 17h Interrupt 21.2 - The Parallel Port Hardware Service Routine 21.3 - Controlling a Printer 21.4 - Inter-Computer Through the Parallel Port Communications on the Parallel 21.3.1 - Printing via DOS Port 21.5 Summary CHAPTER TWENTY TWO: THE PC SERIAL PORTS 22.1 - The 8250 Serial 22.1.7 - The Line Status Register Communications Chip (LSR) 22.1.1 - The Data Register 22.1.8 - The Modem Status (Transmit/Receive Register) Register (MSR) 22.1.2 - The Interrupt Enable 22.1.9 - The Auxiliary Input Register (IER) Register 22.1.3 - The Baud Rate Divisor 22.2 - The UCR Standard Library 22.1.4 - The Interrupt Serial Communications Support Identification Register (IIR) Routines 22.1.5 - The Line Control 22.3 - Programming the 8250 Register (Examples from the Standard 22.1.6 - The Modem Control Library) Register 22.4 Summary CHAPTER TWENTY THREE: THE PC VIDEO DISPLAY 23.1 - Memory Mapped Video 23.2 - The Video Attribute Byte 23.3 - Programming the Text Display 23.4 Summary CHAPTER TWENTY FOUR: THE PC GAME ADAPTER 24.1 - Typical Game Devices 24.5.7 - TestPotCalibration 24.2 - The Game Adapter Hardware 24.5.8 - ReadRaw 24.3 - Using BIOS' Game I/O 24.5.9 - ReadSwitch Functions 24.5.10 - Read16Sw 24.4 - Writing Your Own Game I/O 24.5.11 - Remove Routines 24.5.12 - TestPresence 24.5 - The Standard Game Device 24.5.13 - An SGDI Driver for the Interface (SGDI) Standard Game Adapter Card 24.5.1 - Application Programmer's 24.6 - An SGDI Driver for the CH Interface (API) Products' Flight Stick Pro' 24.5.2 - Read4Sw 24.7 - Patching Existing Games 24.5.3 - Read4Pots: 24.8 Summary 24.5.4 - ReadPot 24.5.5 - Read4: 24.5.6 - CalibratePot Section Six: Optimization CHAPTER TWENTY FIVE: OPTIMIZING YOUR PROGRAMS 25.1 - When to Optimize, When Not 25.4 - The Three Types of to Optimize Optimization 25.2 - How Do You Find the Slow 25.5 - Improving the Code in Your Programs? Implementation of an Algorithm 25.3 - Is Optimization Necessary? 25.6 Summary Section Seven: Appendixes APPENDIX A: ASCII/IBM CHARACTER SET APPENDIX B: ANNOTATED BIBLIOGRAPHY APPENDIX C: KEYBOARD SCAN CODES APPENDIX D: INSTRUCTION SET REFERENCE Section D1 Section D3 Section D2 Section D4 --------------------------------------------------------------------- Copyright 1996 by Randall Hyde All rights reserved. The Art of Redesigned 10/2000 with "MS Assembly Language Programming FrontPage 98" using 17" monitor 30 SEP 1996 1024x768 (c) 2000 BIRCOM Entertainment'95