You are previewing The Art of Assembly Language, 2nd Edition.
O'Reilly logo
The Art of Assembly Language, 2nd Edition

Book Description

The Art of Assembly Language teaches programmers how to understand assembly language and how to use it to write powerful, efficient code. Using the proven High Level Assembler (HLA) as its primary teaching tool, The Art of Assembly Language leverages your knowledge of high level programming languages to make it easier for you to quickly grasp basic assembly concepts.

Table of Contents

  1. The Art of Assembly Language
    1. PRAISE FOR THE FIRST EDITION OF THE ART OF ASSEMBLY LANGUAGE
    2. ACKNOWLEDGMENTS
          1. First Edition
          2. Second Edition
    3. 1. HELLO, WORLD OF ASSEMBLY LANGUAGE
      1. 1.1 The Anatomy of an HLA Program
      2. 1.2 Running Your First HLA Program
      3. 1.3 Some Basic HLA Data Declarations
      4. 1.4 Boolean Values
      5. 1.5 Character Values
      6. 1.6 An Introduction to the Intel 80x86 CPU Family
      7. 1.7 The Memory Subsystem
      8. 1.8 Some Basic Machine Instructions
      9. 1.9 Some Basic HLA Control Structures
        1. 1.9.1 Boolean Expressions in HLA Statements
        2. 1.9.2 The HLA if..then..elseif..else..endif Statement
        3. 1.9.3 Conjunction, Disjunction, and Negation in Boolean Expressions
        4. 1.9.4 The while..endwhile Statement
        5. 1.9.5 The for..endfor Statement
        6. 1.9.6 The repeat..until Statement
        7. 1.9.7 The break and breakif Statements
        8. 1.9.8 The forever..endfor Statement
        9. 1.9.9 The try..exception..endtry Statement
      10. 1.10 Introduction to the HLA Standard Library
        1. 1.10.1 Predefined Constants in the stdio Module
        2. 1.10.2 Standard In and Standard Out
        3. 1.10.3 The stdout.newln Routine
        4. 1.10.4 The stdout.putiX Routines
        5. 1.10.5 The stdout.putiXSize Routines
        6. 1.10.6 The stdout.put Routine
        7. 1.10.7 The stdin.getc Routine
        8. 1.10.8 The stdin.geti X Routines
        9. 1.10.9 The stdin.readLn and stdin.flushInput Routines
        10. 1.10.10 The stdin.get Routine
      11. 1.11 Additional Details About try..endtry
        1. 1.11.1 Nesting try..endtry Statements
        2. 1.11.2 The unprotected Clause in a try..endtry Statement
        3. 1.11.3 The anyexception Clause in a try..endtry Statement
        4. 1.11.4 Registers and the try..endtry Statement
      12. 1.12 High-Level Assembly Language vs. Low-Level Assembly Language
      13. 1.13 For More Information
    4. 2. DATA REPRESENTATION
      1. 2.1 Numbering Systems
        1. 2.1.1 A Review of the Decimal System
        2. 2.1.2 The Binary Numbering System
        3. 2.1.3 Binary Formats
      2. 2.2 The Hexadecimal Numbering System
      3. 2.3 Data Organization
        1. 2.3.1 Bits
        2. 2.3.2 Nibbles
        3. 2.3.3 Bytes
        4. 2.3.4 Words
        5. 2.3.5 Double Words
        6. 2.3.6 Quad Words and Long Words
      4. 2.4 Arithmetic Operations on Binary and Hexadecimal Numbers
      5. 2.5 A Note About Numbers vs. Representation
      6. 2.6 Logical Operations on Bits
      7. 2.7 Logical Operations on Binary Numbers and Bit Strings
      8. 2.8 Signed and Unsigned Numbers
      9. 2.9 Sign Extension, Zero Extension, Contraction, and Saturation
      10. 2.10 Shifts and Rotates
      11. 2.11 Bit Fields and Packed Data
      12. 2.12 An Introduction to Floating-Point Arithmetic
        1. 2.12.1 IEEE Floating-Point Formats
        2. 2.12.2 HLA Support for Floating-Point Values
      13. 2.13 Binary-Coded Decimal Representation
      14. 2.14 Characters
        1. 2.14.1 The ASCII Character Encoding
        2. 2.14.2 HLA Support for ASCII Characters
      15. 2.15 The Unicode Character Set
      16. 2.16 For More Information
    5. 3. MEMORY ACCESS AND ORGANIZATION
      1. 3.1 The 80x86 Addressing Modes
        1. 3.1.1 80x86 Register Addressing Modes
        2. 3.1.2 80x86 32-Bit Memory Addressing Modes
          1. 3.1.2.1 The Displacement-Only Addressing Mode
          2. 3.1.2.2 The Register-Indirect Addressing Modes
          3. 3.1.2.3 Indexed Addressing Modes
          4. 3.1.2.4 Variations on the Indexed Addressing Mode
          5. 3.1.2.5 Scaled-Indexed Addressing Modes
          6. 3.1.2.6 Addressing Mode Wrap-up
      2. 3.2 Runtime Memory Organization
        1. 3.2.1 The code Section
        2. 3.2.2 The static Section
        3. 3.2.3 The readonly Data Section
        4. 3.2.4 The storage Section
        5. 3.2.5 The @nostorage Attribute
        6. 3.2.6 The var Section
        7. 3.2.7 Organization of Declaration Sections Within Your Programs
      3. 3.3 How HLA Allocates Memory for Variables
      4. 3.4 HLA Support for Data Alignment
      5. 3.5 Address Expressions
      6. 3.6 Type Coercion
      7. 3.7 Register Type Coercion
      8. 3.8 The stack Segment and the push and pop Instructions
        1. 3.8.1 The Basic push Instruction
        2. 3.8.2 The Basic pop Instruction
        3. 3.8.3 Preserving Registers with the push and pop Instructions
      9. 3.9 The Stack Is a LIFO Data Structure
        1. 3.9.1 Other push and pop Instructions
        2. 3.9.2 Removing Data from the Stack Without Popping It
      10. 3.10 Accessing Data You've Pushed onto the Stack Without Popping It
      11. 3.11 Dynamic Memory Allocation and the Heap Segment
      12. 3.12 The inc and dec Instructions
      13. 3.13 Obtaining the Address of a Memory Object
      14. 3.14 For More Information
    6. 4. CONSTANTS, VARIABLES, AND DATA TYPES
      1. 4.1 Some Additional Instructions: intmul, bound, into
      2. 4.2 HLA Constant and Value Declarations
        1. 4.2.1 Constant Types
        2. 4.2.2 String and Character Literal Constants
        3. 4.2.3 String and Text Constants in the const Section
        4. 4.2.4 Constant Expressions
        5. 4.2.5 Multiple const Sections and Their Order in an HLA Program
        6. 4.2.6 The HLA val Section
        7. 4.2.7 Modifying val Objects at Arbitrary Points in Your Programs
      3. 4.3 The HLA Type Section
      4. 4.4 enum and HLA Enumerated Data Types
      5. 4.5 Pointer Data Types
        1. 4.5.1 Using Pointers in Assembly Language
        2. 4.5.2 Declaring Pointers in HLA
        3. 4.5.3 Pointer Constants and Pointer Constant Expressions
        4. 4.5.4 Pointer Variables and Dynamic Memory Allocation
        5. 4.5.5 Common Pointer Problems
      6. 4.6 Composite Data Types
      7. 4.7 Character Strings
      8. 4.8 HLA Strings
      9. 4.9 Accessing the Characters Within a String
      10. 4.10 The HLA String Module and Other String-Related Routines
      11. 4.11 In-Memory Conversions
      12. 4.12 Character Sets
      13. 4.13 Character Set Implementation in HLA
      14. 4.14 HLA Character Set Constants and Character Set Expressions
      15. 4.15 Character Set Support in the HLA Standard Library
      16. 4.16 Using Character Sets in Your HLA Programs
      17. 4.17 Arrays
      18. 4.18 Declaring Arrays in Your HLA Programs
      19. 4.19 HLA Array Constants
      20. 4.20 Accessing Elements of a Single-Dimensional Array
      21. 4.21 Sorting an Array of Values
      22. 4.22 Multidimensional Arrays
        1. 4.22.1 Row-Major Ordering
        2. 4.22.2 Column-Major Ordering
      23. 4.23 Allocating Storage for Multidimensional Arrays
      24. 4.24 Accessing Multidimensional Array Elements in Assembly Language
      25. 4.25 Records
      26. 4.26 Record Constants
      27. 4.27 Arrays of Records
      28. 4.28 Arrays/Records as Record Fields
      29. 4.29 Aligning Fields Within a Record
      30. 4.30 Pointers to Records
      31. 4.31 Unions
      32. 4.32 Anonymous Unions
      33. 4.33 Variant Types
      34. 4.34 Namespaces
      35. 4.35 Dynamic Arrays in Assembly Language
      36. 4.36 For More Information
    7. 5. PROCEDURES AND UNITS
      1. 5.1 Procedures
      2. 5.2 Saving the State of the Machine
      3. 5.3 Prematurely Returning from a Procedure
      4. 5.4 Local Variables
      5. 5.5 Other Local and Global Symbol Types
      6. 5.6 Parameters
        1. 5.6.1 Pass by Value
        2. 5.6.2 Pass by Reference
      7. 5.7 Functions and Function Results
        1. 5.7.1 Returning Function Results
        2. 5.7.2 Instruction Composition in HLA
        3. 5.7.3 The HLA @returns Option in Procedures
      8. 5.8 Recursion
      9. 5.9 Forward Procedures
      10. 5.10 HLA v2.0 Procedure Declarations
      11. 5.11 Low-Level Procedures and the call Instruction
      12. 5.12 Procedures and the Stack
      13. 5.13 Activation Records
      14. 5.14 The Standard Entry Sequence
      15. 5.15 The Standard Exit Sequence
      16. 5.16 Low-Level Implementation of Automatic (Local) Variables
      17. 5.17 Low-Level Parameter Implementation
        1. 5.17.1 Passing Parameters in Registers
        2. 5.17.2 Passing Parameters in the Code Stream
        3. 5.17.3 Passing Parameters on the Stack
          1. 5.17.3.1 Accessing Value Parameters on the Stack
          2. 5.17.3.2 Passing Value Parameters on the Stack
          3. 5.17.3.3 Accessing Reference Parameters on the Stack
          4. 5.17.3.4 Passing Reference Parameters on the Stack
          5. 5.17.3.5 Passing Formal Parameters as Actual Parameters
          6. 5.17.3.6 HLA Hybrid Parameter-Passing Facilities
          7. 5.17.3.7 Mixing Register and Stack-Based Parameters
      18. 5.18 Procedure Pointers
      19. 5.19 Procedural Parameters
      20. 5.20 Untyped Reference Parameters
      21. 5.21 Managing Large Programs
      22. 5.22 The #include Directive
      23. 5.23 Ignoring Duplicate #include Operations
      24. 5.24 Units and the external Directive
        1. 5.24.1 Behavior of the external Directive
        2. 5.24.2 Header Files in HLA
      25. 5.25 Namespace Pollution
      26. 5.26 For More Information
    8. 6. ARITHMETIC
      1. 6.1 80x86 Integer Arithmetic Instructions
        1. 6.1.1 The mul and imul Instructions
        2. 6.1.2 The div and idiv Instructions
        3. 6.1.3 The cmp Instruction
        4. 6.1.4 The setcc Instructions
        5. 6.1.5 The test Instruction
      2. 6.2 Arithmetic Expressions
        1. 6.2.1 Simple Assignments
        2. 6.2.2 Simple Expressions
        3. 6.2.3 Complex Expressions
        4. 6.2.4 Commutative Operators
      3. 6.3 Logical (Boolean) Expressions
      4. 6.4 Machine and Arithmetic Idioms
        1. 6.4.1 Multiplying without mul, imul, or intmul
        2. 6.4.2 Division Without div or idiv
        3. 6.4.3 Implementing Modulo-N Counters with and
      5. 6.5 Floating-Point Arithmetic
        1. 6.5.1 FPU Registers
          1. 6.5.1.1 FPU Data Registers
          2. 6.5.1.2 The FPU Control Register
          3. 6.5.1.3 The FPU Status Register
        2. 6.5.2 FPU Data Types
        3. 6.5.3 The FPU Instruction Set
        4. 6.5.4 FPU Data Movement Instructions
          1. 6.5.4.1 The fld Instruction
          2. 6.5.4.2 The fst and fstp Instructions
          3. 6.5.4.3 The fxch Instruction
        5. 6.5.5 Conversions
          1. 6.5.5.1 The fild Instruction
          2. 6.5.5.2 The fist, fistp, and fisttp Instructions
          3. 6.5.5.3 The fbld and fbstp Instructions
        6. 6.5.6 Arithmetic Instructions
          1. 6.5.6.1 The fadd and faddp Instructions
          2. 6.5.6.2 The fsub, fsubp, fsubr, and fsurpb Instructions
          3. 6.5.6.3 The fmul and fmulp Instructions
          4. 6.5.6.4 The fdiv, fdivp, fdivr, and fdivrp Instructions
          5. 6.5.6.5 The fsqrt Instruction
          6. 6.5.6.6 The fprem and fprem1 Instructions
          7. 6.5.6.7 The frndint Instruction
          8. 6.5.6.8 The fabs Instruction
          9. 6.5.6.9 The fchs Instruction
        7. 6.5.7 Comparison Instructions
          1. 6.5.7.1 The fcom, fcomp, and fcompp Instructions
          2. 6.5.7.2 The fcomi and fcomip Instructions
          3. 6.5.7.3 The ftst Instruction
        8. 6.5.8 Constant Instructions
        9. 6.5.9 Transcendental Instructions
          1. 6.5.9.1 The f2xm1 Instruction
          2. 6.5.9.2 The fsin, fcos, and fsincos Instructions
          3. 6.5.9.3 The fptan Instruction
          4. 6.5.9.4 The fpatan Instruction
          5. 6.5.9.5 The fyl2x Instruction
          6. 6.5.9.6 The fyl2xp1 Instruction
        10. 6.5.10 Miscellaneous Instructions
          1. 6.5.10.1 The finit and fninit Instructions
          2. 6.5.10.2 The fldcw and fstcw Instructions
          3. 6.5.10.3 The fclex and fnclex Instructions
          4. 6.5.10.4 The fstsw and fnstsw Instructions
        11. 6.5.11 Integer Operations
      6. 6.6 Converting Floating-Point Expressions to Assembly Language
        1. 6.6.1 Converting Arithmetic Expressions to Postfix Notation
        2. 6.6.2 Converting Postfix Notation to Assembly Language
      7. 6.7 HLA Standard Library Support for Floating-Point Arithmetic
      8. 6.8 For More Information
    9. 7. LOW-LEVEL CONTROL STRUCTURES
      1. 7.1 Low-Level Control Structures
      2. 7.2 Statement Labels
      3. 7.3 Unconditional Transfer of Control (jmp)
      4. 7.4 The Conditional Jump Instructions
      5. 7.5 "Medium-Level" Control Structures: jt and jf
      6. 7.6 Implementing Common Control Structures in Assembly Language
      7. 7.7 Introduction to Decisions
        1. 7.7.1 if..then..else Sequences
        2. 7.7.2 Translating HLA if Statements into Pure Assembly Language
          1. if( flag_specification ) then stmts endif;
          2. if( register ) then stmts endif;
          3. if( !register ) then stmts endif;
          4. if( boolean_variable ) then stmts endif;
          5. if( !boolean_variable ) then stmts endif;
          6. if( mem_reg relop mem_reg_const ) then stmts endif;
          7. if( reg/mem in LowConst..HiConst ) then stmts endif;
          8. if( reg/mem not in LowConst..HiConst ) then stmts endif;
        3. 7.7.3 Implementing Complex if Statements Using Complete Boolean Evaluation
        4. 7.7.4 Short-Circuit Boolean Evaluation
        5. 7.7.5 Short-Circuit vs. Complete Boolean Evaluation
        6. 7.7.6 Efficient Implementation of if Statements in Assembly Language
          1. 7.7.6.1 Know Your Data!
          2. 7.7.6.2 Rearranging Expressions
          3. 7.7.6.3 Destructuring Your Code
          4. 7.7.6.4 Calculation Rather Than Branching
        7. 7.7.7 switch/case Statements
      8. 7.8 State Machines and Indirect Jumps
      9. 7.9 Spaghetti Code
      10. 7.10 Loops
        1. 7.10.1 while Loops
        2. 7.10.2 repeat..until Loops
        3. 7.10.3 forever..endfor Loops
        4. 7.10.4 for Loops
        5. 7.10.5 The break and continue Statements
          1. forever..continue..endfor
          2. while..continue..endwhile
          3. for..continue..endfor
          4. repeat..continue..until
        6. 7.10.6 Register Usage and Loops
      11. 7.11 Performance Improvements
        1. 7.11.1 Moving the Termination Condition to the End of a Loop
        2. 7.11.2 Executing the Loop Backwards
        3. 7.11.3 Loop-Invariant Computations
        4. 7.11.4 Unraveling Loops
        5. 7.11.5 Induction Variables
      12. 7.12 Hybrid Control Structures in HLA
      13. 7.13 For More Information
    10. 8. ADVANCED ARITHMETIC
      1. 8.1 Multiprecision Operations
        1. 8.1.1 HLA Standard Library Support for Extended-Precision Operations
        2. 8.1.2 Multiprecision Addition Operations
        3. 8.1.3 Multiprecision Subtraction Operations
        4. 8.1.4 Extended-Precision Comparisons
        5. 8.1.5 Extended-Precision Multiplication
        6. 8.1.6 Extended-Precision Division
        7. 8.1.7 Extended-Precision neg Operations
        8. 8.1.8 Extended-Precision and Operations
        9. 8.1.9 Extended-Precision or Operations
        10. 8.1.10 Extended-Precision xor Operations
        11. 8.1.11 Extended-Precision not Operations
        12. 8.1.12 Extended-Precision Shift Operations
        13. 8.1.13 Extended-Precision Rotate Operations
        14. 8.1.14 Extended-Precision I/O
          1. 8.1.14.1 Extended-Precision Hexadecimal Output
          2. 8.1.14.2 Extended-Precision Unsigned Decimal Output
          3. 8.1.14.3 Extended-Precision Signed Decimal Output
          4. 8.1.14.4 Extended-Precision Formatted Output
          5. 8.1.14.5 Extended-Precision Input Routines
          6. 8.1.14.6 Extended-Precision Hexadecimal Input
          7. 8.1.14.7 Extended-Precision Unsigned Decimal Input
          8. 8.1.14.8 Extended-Precision Signed Decimal Input
      2. 8.2 Operating on Different-Size Operands
      3. 8.3 Decimal Arithmetic
        1. 8.3.1 Literal BCD Constants
        2. 8.3.2 The 80x86 daa and das Instructions
        3. 8.3.3 The 80x86 aaa, aas, aam, and aad Instructions
        4. 8.3.4 Packed Decimal Arithmetic Using the FPU
      4. 8.4 Tables
        1. 8.4.1 Function Computation via Table Lookup
        2. 8.4.2 Domain Conditioning
        3. 8.4.3 Generating Tables
        4. 8.4.4 Table Lookup Performance
      5. 8.5 For More Information
    11. 9. MACROS AND THE HLA COMPILE-TIME LANGUAGE
      1. 9.1 Introduction to the Compile-Time Language (CTL)
      2. 9.2 The #print and #error Statements
      3. 9.3 Compile-Time Constants and Variables
      4. 9.4 Compile-Time Expressions and Operators
      5. 9.5 Compile-Time Functions
        1. 9.5.1 Type-Conversion Compile-Time Functions
        2. 9.5.2 Numeric Compile-Time Functions
        3. 9.5.3 Character-Classification Compile-Time Functions
        4. 9.5.4 Compile-Time String Functions
        5. 9.5.5 Compile-Time Symbol Information
        6. 9.5.6 Miscellaneous Compile-Time Functions
        7. 9.5.7 Compile-Time Type Conversions of Text Objects
      6. 9.6 Conditional Compilation (Compile-Time Decisions)
      7. 9.7 Repetitive Compilation (Compile-Time Loops)
      8. 9.8 Macros (Compile-Time Procedures)
        1. 9.8.1 Standard Macros
        2. 9.8.2 Macro Parameters
          1. 9.8.2.1 Standard Macro Parameter Expansion
          2. 9.8.2.2 Macros with a Variable Number of Parameters
          3. 9.8.2.3 Required vs. Optional Macro Parameters
        3. 9.8.3 Local Symbols in a Macro
        4. 9.8.4 Macros as Compile-Time Procedures
        5. 9.8.5 Simulating Function Overloading with Macros
      9. 9.9 Writing Compile-Time "Programs"
        1. 9.9.1 Constructing Data Tables at Compile Time
        2. 9.9.2 Unrolling Loops
      10. 9.10 Using Macros in Different Source Files
      11. 9.11 For More Information
    12. 10. BIT MANIPULATION
      1. 10.1 What Is Bit Data, Anyway?
      2. 10.2 Instructions That Manipulate Bits
      3. 10.3 The Carry Flag as a Bit Accumulator
      4. 10.4 Packing and Unpacking Bit Strings
      5. 10.5 Coalescing Bit Sets and Distributing Bit Strings
      6. 10.6 Packed Arrays of Bit Strings
      7. 10.7 Searching for a Bit
      8. 10.8 Counting Bits
      9. 10.9 Reversing a Bit String
      10. 10.10 Merging Bit Strings
      11. 10.11 Extracting Bit Strings
      12. 10.12 Searching for a Bit Pattern
      13. 10.13 The HLA Standard Library Bits Module
      14. 10.14 For More Information
    13. 11. THE STRING INSTRUCTIONS
      1. 11.1 The 80x86 String Instructions
        1. 11.1.1 How the String Instructions Operate
        2. 11.1.2 The rep/repe/repz and repnz/repne Prefixes
        3. 11.1.3 The Direction Flag
        4. 11.1.4 The movs Instruction
        5. 11.1.5 The cmps Instruction
        6. 11.1.6 The scas Instruction
        7. 11.1.7 The stos Instruction
        8. 11.1.8 The lods Instruction
        9. 11.1.9 Building Complex String Functions from lods and stos
      2. 11.2 Performance of the 80x86 String Instructions
      3. 11.3 For More Information
    14. 12. CLASSES AND OBJECTS
      1. 12.1 General Principles
      2. 12.2 Classes in HLA
      3. 12.3 Objects
      4. 12.4 Inheritance
      5. 12.5 Overriding
      6. 12.6 Virtual Methods vs. Static Procedures
      7. 12.7 Writing Class Methods and Procedures
      8. 12.8 Object Implementation
        1. 12.8.1 Virtual Method Tables
        2. 12.8.2 Object Representation with Inheritance
      9. 12.9 Constructors and Object Initialization
        1. 12.9.1 Dynamic Object Allocation Within the Constructor
        2. 12.9.2 Constructors and Inheritance
        3. 12.9.3 Constructor Parameters and Procedure Overloading
      10. 12.10 Destructors
      11. 12.11 HLA's _initialize_ and _finalize_ Strings
      12. 12.12 Abstract Methods
      13. 12.13 Runtime Type Information
      14. 12.14 Calling Base Class Methods
      15. 12.15 For More Information
    15. A. ASCII CHARACTER SET
    16. Index
    17. About the Author
    18. COLOPHON
    19. UPDATES