Chapter Five - Variables and Data Structures

5.0 - Chapter Overview
5.1 - Some Additional Instructions: LEA, LES, ADD, and MUL
5.2 - Declaring Variables in an Assembly Language Program
5.3 - Declaring and Accessing Scalar Variables
5.3.1 - Declaring and using BYTE Variables
5.3.2 - Declaring and using WORD Variables
5.3.3 - Declaring and using DWORD Variables
5.3.4 - Declaring and using FWORD, QWORD, and TBYTE Variables
5.3.5 - Declaring Floating Point Variables with REAL4, REAL8, and REAL10
5.4 - Creating Your Own Type Names with TYPEDEF
5.5 - Pointer Data Types
5.6 - Composite Data Types
5.6.1 - Arrays
5.6.1.1 - Declaring Arrays in Your Data Segment
5.6.1.2 - Accessing Elements of a Single Dimension Array
5.6.2 - Multidimensional Arrays
5.6.2.1 - Row Major Ordering
5.6.2.2 - Column Major Ordering
5.6.2.3 - Allocating Storage for Multidimensional Arrays
5.6.2.4 - Accessing Multidimensional Array Elements in Assembly Language
5.6.3 - Structures
5.6.4 - Arrays of Structures and Arrays/Structures as Structure Fields
5.6.5 - Pointers to Structures
5.7 - Sample Programs
5.7.1 - Simple Variable Declarations
5.7.2 - Using Pointer Variables
5.7.3 - Single Dimension Array Access
5.7.4 - Multidimensional Array Access
5.7.5 - Simple Structure Access
5.7.6 - Arrays of Structures
5.7.7 - Structures and Arrays as Fields of Another Structure
5.7.8 - Pointers to Structures and Arrays of Structure


5.8 Laboratory Exercises
5.9 Programming Projects
5.10 Summary
5.11 Questions


Chapter Six - The 80x86 Instruction Set
6.0 - Chapter Overview
6.1 - The Processor Status Register (Flags)
6.2 - Instruction Encodings
6.3 - Data Movement Instructions
6.3.1 - The MOV Instruction
6.3.2 - The XCHG Instruction
6.3.3 - The LDS, LES, LFS, LGS, and LSS Instructions
6.3.4 - The LEA Instruction
6.3.5 - The PUSH and POP Instructions
6.3.6 - The LAHF and SAHF Instructions
6.4 - Conversions
6.4.1 - The MOVZX, MOVSX, CBW, CWD, CWDE, and CDQ Instructions
6.4.2 - The BSWAP Instruction
6.4.3 - The XLAT Instruction
6.5 - Arithmetic Instructions
6.5.1 - The Addition Instructions: ADD, ADC, INC, XADD, AAA, and DAA
6.5.1.1 - The ADD and ADC Instructions
6.5.1.2 - The INC Instruction
6.5.1.3 - The XADD Instruction
6.5.1.4 - The AAA and DAA Instructions
6.5.2 - The Subtraction Instructions: SUB, SBB, DEC, AAS, and DAS
6.5.3 - The CMP Instruction
6.5.4 - The CMPXCHG, and CMPXCHG8B Instructions
6.5.5 - The NEG Instruction
6.5.6 - The Multiplication Instructions: MUL, IMUL, and AAM
6.5.7 - The Division Instructions: DIV, IDIV, and AAD
6.6 - Logical, Shift, Rotate and Bit Instructions
6.6.1 - The Logical Instructions: AND, OR, XOR, and NOT
6.6.2 - The Shift Instructions: SHL/SAL, SHR, SAR, SHLD, and SHRD
6.6.2.1 - SHL/SAL
6.6.2.2 - SAR
6.6.2.3 - SHR
6.6.2.4 - The SHLD and SHRD Instructions
6.6.3 - The Rotate Instructions: RCL, RCR, ROL, and ROR
6.6.3.1 - RCL
6.6.3.2 - RCR
6.6.3.3 - ROL
6.6.3.4 - ROR
6.6.4 - The Bit Operations
6.6.4.1 - TEST
6.6.4.2 - The Bit Test Instructions: BT, BTS, BTR, and BTC
6.6.4.3 - Bit Scanning: BSF and BSR
6.6.5 - The "Set on Condition" Instructions
6.7 - I/O Instructions
6.8 - String Instructions
6.9 - Program Flow Control Instructions
6.9.1 - Unconditional Jumps
6.9.2 - The CALL and RET Instructions
6.9.3 - The INT, INTO, BOUND, and IRET Instructions
6.9.4 - The Conditional Jump Instructions
6.9.5 - The JCXZ/JECXZ Instructions
6.9.6 - The LOOP Instruction
6.9.7 - The LOOPE/LOOPZ Instruction
6.9.8 - The LOOPNE/LOOPNZ Instruction
6.10 - Miscellaneous Instructions
6.11 - Sample Programs
6.11.1 - Simple Arithmetic I
6.11.2 - Simple Arithmetic II
6.11.3 - Logical Operations
6.11.4 - Shift and Rotate Operations
6.11.5 - Bit Operations and SETcc Instructions
6.11.6 - String Operations
6.11.7 - Conditional Jumps
6.11.8 - CALL and INT Instructions
6.11.9 - Conditional Jumps I
6.11.10 - Conditional Jump Instructions II


6.12 Laboratory Exercises
6.12.1 The IBM/L System
6.12.2 IBM/L Exercises
6.13 Programming Projects
6.14 Summary
6.15 Questions


Chapter Seven - The UCR Standard Library
7.0 - Chapter Overview
7.1 - An Introduction to the UCR Standard Library
7.1.1 - Memory Management Routines: MEMINIT, MALLOC, and FREE
7.1.2 - The Standard Input Routines: GETC, GETS, GETSM
7.1.3 - The Standard Output Routines: PUTC, PUTCR, PUTS, PUTH, PUTI, PRINT, and PRINTF
7.1.4 - Formatted Output Routines: Putisize, Putusize, Putlsize, and Putulsize
7.1.5 - Output Field Size 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
7.1.11 - Plus more!
7.2 - Sample Programs
7.2.1 - Stripped SHELL.ASM File
7.2.2 - Numeric I/O


7.3 Laboratory Exercises
7.3.1 Obtaining the UCR Standard Library
7.3.2 Unpacking the Standard Library
7.3.3 Using the Standard Library
7.3.4 The Standard Library Documentation Files
7.4 Programming Projects
7.5 Summary
7.6 Questions


8.0 - Chapter Overview
8.1 - Assembly Language Statements
8.2 - The Location Counter
8.3 - Symbols
8.4 - Literal Constants
8.4.1 - Integer Constants
8.4.2 - String Constants
8.4.3 - Real Constants
8.4.4 - Text Constants
8.5 - Declaring Manifest Constants Using Equates
8.6 - Processor Directives
8.7 - Procedures
8.8 - Segments
8.8.1 - Segment Names
8.8.2 - Segment Loading Order
8.8.3 - Segment Operands
8.8.3.1 - The ALIGN Type
8.8.3.2 - The COMBINE Type
8.8.4 - The CLASS Type
8.8.5 - The Read-only Operand
8.8.6 - The USE16, USE32, and FLAT Options
8.8.7 - Typical Segment Definitions
8.8.8 - Why You Would Want to Control the Loading Order
8.8.9 - Segment Prefixes
8.8.10 - Controlling Segments with the ASSUME Directive
8.8.11 - Combining Segments: The GROUP Directive
8.8.12 - Why Even Bother With Segments?
8.9 - The END Directive
8.10 - Variables
8.11 - Label Types
8.11.1 - How to Give a Symbol a Particular Type
8.11.2 - Label Values
8.11.3 - Type Conflicts
8.12 - Address Expressions
8.12.1 - Symbol Types and Addressing Modes
8.12.2 - Arithmetic and Logical Operators
8.12.3 - Coercion
8.12.4 - Type Operators
8.12.5 - Operator Precedence
8.13 - Conditional Assembly
8.13.1 - IF Directive
8.13.2 - IFE directive
8.13.3 - IFDEF and IFNDEF
8.13.4 - IFB, IFNB
8.13.5 - IFIDN, IFDIF, IFIDNI, and IFDIFI
8.14 - Macros
8.14.1 - Procedural Macros
8.14.2 - Macros vs. 80x86 Procedures
8.14.3 - The LOCAL Directive
8.14.4 - The EXITM Directive
8.14.5 - Macro Parameter Expansion and Macro Operators
8.14.6 - A Sample Macro to Implement For Loops
8.14.7 - Macro Functions
8.14.8 - Predefined Macros, Macro Functions, and Symbols
8.14.9 - Macros vs. Text Equates
8.14.10 - Macros: Good and Bad News
8.15 - Repeat Operations
8.16 - The FOR and FORC Macro Operations
8.17 - The WHILE Macro Operation
8.18 - Macro Parameters
8.19 - Controlling the Listing
8.19.1 - The ECHO and %OUT Directives
8.19.2 - The TITLE Directive
8.19.3 - The SUBTTL Directive
8.19.4 - The PAGE Directive
8.19.5 - The .LIST, .NOLIST, and .XLIST Directives
8.19.6 - Other Listing Directives
8.20 - Managing Large Programs
8.20.1 - The INCLUDE Directive
8.20.2 - The PUBLIC, EXTERN, and EXTRN Directives
8.20.3 - The EXTERNDEF Directive
8.21 - Make Files
8.22 - Sample Program
8.22.1 - EX8.MAK
8.22.2 - Matrix.A
8.22.3 - EX8.ASM
8.22.4 - GETI.ASM
8.22.5 - GetArray.ASM
8.22.6 - XProduct.ASM


8.23 Laboratory Exercises
8.23.1 Near vs. Far Procedures
8.23.2 Data Alignment Exercises
8.23.3 Equate Exercise
8.23.4 IFDEF Exercise
8.23.5 Make File Exercise
8.24 Programming Projects
8.25 Summary
8.26 Questions


Chapter Nine - Arithmetic and Logical Operations
9.0 - Chapter Overview
9.1 - Arithmetic Expressions
9.1.1 - Simple Assignments
9.1.2 - Simple Expressions
9.1.3 - Complex Expressions
9.1.4 - Commutative Operators
9.2 - Logical (Boolean) Expressions
9.3 - Multiprecision Operations
9.3.1 - Multiprecision Addition Operations
9.3.2 - Multiprecision Subtraction Operations
9.3.3 - Extended Precision Comparisons
9.3.4 - Extended Precision Multiplication
9.3.5 - Extended Precision Division
9.3.6 - Extended Precision NEG Operations
9.3.7 - Extended Precision AND Operations
9.3.8 - Extended Precision OR Operations
9.3.9 - Extended Precision XOR Operations
9.3.10 - Extended Precision NOT Operations
9.3.11 - Extended Precision Shift Operations
9.3.12 - Extended Precision Rotate Operations
9.4 - Operating on Different Sized Operands
9.5 - Machine and Arithmetic Idioms
9.5.1 - Multiplying Without MUL and IMUL
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
9.5.6 - TEST Operations
9.5.7 - Testing Signs with the XOR Instruction
9.6 - Masking Operations
9.6.1 - Masking Operations with the AND Instruction
9.6.2 - Masking Operations with the OR Instruction
9.7 - Packing and Unpacking Data Types
9.8 - Tables
9.8.1 - Function Computation via Table Look Up
9.8.2 - Domain Conditioning
9.8.3 - Generating Tables
9.9 - Sample Programs
9.9.1 - Converting Arithmetic Expressions to Assembly Language
9.9.2 - Boolean Operations Example
9.9.3 - 64-bit Integer I/O
9.9.4 - Packing and Unpacking Date Data Types



9.10 Laboratory Exercises
9.10.1 Debugging Programs with CodeView
9.10.2 Debugging Strategies
9.10.2.1 Locating Infinite Loops
9.10.2.2 Incorrect Computations
9.10.2.3 Illegal Instructions/Infinite Loops Part II
9.10.3 Debug Exercise I: Using CodeView to Find Bugs in a Calculation
9.10.4 Software Delay Loop Exercises
9.11 Programming Projects
9.12 Summary
9.13 Questions


Chapter 10 - Control Structures
10.0 - Chapter Overview
10.1 - Introduction to Decisions
10.2 - IF..THEN..ELSE Sequences
10.3 - CASE Statements
10.4 - State Machines and Indirect Jumps
10.5 - Spaghetti Code
10.6 - Loops
10.6.1 - While Loops
10.6.2 - Repeat..Until Loops
10.6.3 - LOOP..ENDLOOP Loops
10.6.4 - FOR Loops
10.7 - Register Usage and Loops
10.8 - Performance Improvements
10.8.1 - Moving the Termination Condition to the End of a Loop
10.8.2 - Executing the Loop Backwards
10.8.3 - Loop Invariant Computations
10.8.4 - Unraveling Loops
10.8.5 - Induction Variables
10.8.6 - Other Performance Improvements
10.9 - Nested Statements
10.10 - Timing Delay Loops
10.11 - Sample Program



10.12 Laboratory Exercises
10.12.1 The Physics of Sound
10.12.2 The Fundamentals of Music
10.12.3 The Physics of Music
10.12.4 The 8253/8254 Timer Chip
10.12.5 Programming the Timer Chip to Produce Musical Tones
10.12.6 Putting it All Together
10.12.7 Amazing Grace Exercise
10.13 Programming Projects
10.14 Summary
10.15 Questions


Chapter Eleven - Procedures and Functions
11.0 - Chapter Overview
11.1 - Procedures
11.2 - Near and Far Procedures
11.2.1 - Forcing NEAR or FAR CALLs and Returns
11.2.2 - Nested Procedures
11.3 - Functions
11.4 - Saving the State of the Machine
11.5 - Parameters
11.5.1 - Pass by Value
11.5.2 - Pass by Reference
11.5.3 - Pass by Value-Returned
11.5.4 - Pass by Result
11.5.5 - Pass by Name
11.5.6 - Pass by Lazy-Evaluation
11.5.7 - Passing Parameters in Registers
11.5.8 - Passing Parameters in Global Variables
11.5.9 - Passing Parameters on the Stack
11.5.10 - Passing Parameters in the Code Stream
11.5.11 - Passing Parameters via a Parameter Block
11.6 - Function Results
11.6.1 - Returning Function Results in a Register
11.6.2 - Returning Function Results on the Stack
11.6.3 - Returning Function Results in Memory Locations
11.7 - Side Effects
11.8 - Local Variable Storage
11.9 - Recursion
11.10 - Sample Program

11.11 Laboratory Exercises
11.11.1 Ex11_1.cpp
11.11.2 Ex11_1.asm
11.11.3 EX11_1a.asm
11.12 Programming Projects
11.13 Summary
11.14 Questions