home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft_Programmers_Library.7z / MPL / intel / 80386.txt < prev    next >
Encoding:
Text File  |  2013-11-08  |  869.4 KB  |  20,925 lines

  1.  INTEL 80386 PROGRAMMER'S REFERENCE MANUAL 1986
  2.  
  3.  Intel Corporation makes no warranty for the use of its products and
  4.  assumes no responsibility for any errors which may appear in this document
  5.  nor does it make a commitment to update the information contained herein.
  6.  
  7.  Intel retains the right to make changes to these specifications at any
  8.  time, without notice.
  9.  
  10.  Contact your local sales office to obtain the latest specifications before
  11.  placing your order.
  12.  
  13.  The following are trademarks of Intel Corporation and may only be used to
  14.  identify Intel Products:
  15.  
  16.  Above, BITBUS, COMMputer, CREDIT, Data Pipeline, FASTPATH, Genius, i, î,
  17.  ICE, iCEL, iCS, iDBP, iDIS, I²ICE, iLBX, im, iMDDX, iMMX, Inboard,
  18.  Insite, Intel, intel, intelBOS, Intel Certified, Intelevision,
  19.  inteligent Identifier, inteligent Programming, Intellec, Intellink,
  20.  iOSP, iPDS, iPSC, iRMK, iRMX, iSBC, iSBX, iSDM, iSXM, KEPROM, Library
  21.  Manager, MAPNET, MCS, Megachassis, MICROMAINFRAME, MULTIBUS, MULTICHANNEL,
  22.  MULTIMODULE, MultiSERVER, ONCE, OpenNET, OTP, PC BUBBLE, Plug-A-Bubble,
  23.  PROMPT, Promware, QUEST, QueX, Quick-Pulse Programming, Ripplemode, RMX/80,
  24.  RUPI, Seamless, SLD, SugarCube, SupportNET, UPI, and VLSiCEL, and the
  25.  combination of ICE, iCS, iRMX, iSBC, iSBX, iSXM, MCS, or UPI and a numerical
  26.  suffix, 4-SITE.
  27.  
  28.  MDS is an ordering code only and is not used as a product name or
  29.  trademark. MDS(R) is a registered trademark of Mohawk Data Sciences
  30.  Corporation.
  31.  
  32.  Additional copies of this manual or other Intel literature may be obtained
  33.  from:
  34.  
  35.  Intel Corporation
  36.  Literature Distribution
  37.  Mail Stop SC6-59
  38.  3065 Bowers Avenue
  39.  Santa Clara, CA 95051
  40.  
  41.  (c)INTEL CORPORATION 1987    CG-5/26/87
  42.  
  43.  
  44.  Customer Support
  45.  
  46.  ───────────────────────────────────────────────────────────────────────────
  47.  
  48.  Customer Support is Intel's complete support service that provides Intel
  49.  customers with hardware support, software support, customer training, and
  50.  consulting services. For more information contact your local sales offices.
  51.  
  52.  After a customer purchases any system hardware or software product,
  53.  service and support become major factors in determining whether that
  54.  product will continue to meet a customer's expectations. Such support
  55.  requires an international support organization and a breadth of programs
  56.  to meet a variety of customer needs. As you might expect, Intel's customer
  57.  support is quite extensive. It includes factory repair services and
  58.  worldwide field service offices providing hardware repair services,
  59.  software support services, customer training classes, and consulting
  60.  services.
  61.  
  62.  Hardware Support Services
  63.  
  64.  Intel is committed to providing an international service support package
  65.  through a wide variety of service offerings available from Intel Hardware
  66.  Support.
  67.  
  68.  Software Support Services
  69.  
  70.  Intel's software support consists of two levels of contracts. Standard
  71.  support includes TIPS (Technical Information Phone Service), updates and
  72.  subscription service (product-specific troubleshooting guides and COMMENTS
  73.  Magazine). Basic support includes updates and the subscription service.
  74.  Contracts are sold in environments which represent product groupings
  75.  (i.e., iRMX environment).
  76.  
  77.  Consulting Services
  78.  
  79.  Intel provides field systems engineering services for any phase of your
  80.  development or support effort. You can use our systems engineers in a
  81.  variety of ways ranging from assistance in using a new product, developing
  82.  an application, personalizing training, and customizing or tailoring an
  83.  Intel product to providing technical and management consulting. Systems
  84.  Engineers are well versed in technical areas such as microcommunications,
  85.  real-time applications, embedded microcontrollers, and network services.
  86.  You know your application needs; we know our products. Working together we
  87.  can help you get a successful product to market in the least possible time.
  88.  
  89.  Customer Training
  90.  
  91.  Intel offers a wide range of instructional programs covering various
  92.  aspects of system design and implementation. In just three to ten days a
  93.  limited number of individuals learn more in a single workshop than in
  94.  weeks of self-study. For optimum convenience, workshops are scheduled
  95.  regularly at Training Centers woridwide or we can take our workshops to
  96.  you for on-site instruction. Covering a wide variety of topics, Intel's
  97.  major course categories include: architecture and assembly language,
  98.  programming and operating systems, bitbus and LAN applications.
  99.  
  100.  Training Center Locations
  101.  
  102.  To obtain a complete catalog of our workshops, call the nearest Training
  103.  Center in your area.
  104.  
  105.  Boston                    (617) 692-1000
  106.  Chicago                   (312) 310-5700
  107.  San Francisco             (415) 940-7800
  108.  Washington D.C.           (301) 474-2878
  109.  Isreal                    (972) 349-491-099
  110.  Tokyo                     03-437-6611
  111.  Osaka (Call Tokyo)        03-437-6611
  112.  Toronto, Canada           (416) 675-2105
  113.  London                    (0793) 696-000
  114.  Munich                    (089) 5389-1
  115.  Paris                     (01) 687-22-21
  116.  Stockholm                 (468) 734-01-00
  117.  Milan                     39-2-82-44-071
  118.  Benelux (Rotterdam)       (10) 21-23-77
  119.  Copenhagen                (1) 198-033
  120.  Hong Kong                 5-215311-7
  121.  
  122.  
  123.  Table of Contents
  124.  
  125.  Chapter 1  Introduction to the 80386
  126.  
  127.  1.1  Organization of This Manual
  128.        1.1.1  Part I ── Applications Programming
  129.        1.1.2  Part II ── Systems Programming
  130.        1.1.3  Part III ── Compatibility
  131.        1.1.4  Part IV ── Instruction Set
  132.        1.1.5  Appendices
  133.  
  134.  1.2  Related Literature
  135.  1.3  Notational Conventions
  136.        1.3.1  Data-Structure Formats
  137.        1.3.2  Undefined Bits and Software Compatibility
  138.        1.3.3  Instruction Operands
  139.        1.3.4  Hexadecimal Numbers
  140.        1.3.5  Sub- and Super-Scripts
  141.  
  142.                       PART I    APPLICATIONS PROGRAMMING
  143.  
  144.  Chapter 2  Basic Programming Model
  145.  
  146.  2.1  Memory Organization and Segmentation
  147.        2.1.1  The"Flat" Model
  148.        2.1.2  The Segmented Model
  149.  
  150.  2.2  Data Types
  151.  2.3  Registers
  152.        2.3.1  General Registers
  153.        2.3.2  Segment Registers
  154.        2.3.3  Stack Implementation
  155.        2.3.4  Flags Register
  156.                2.3.4.1  Status Flags
  157.                2.3.4.2  Control Flag
  158.                2.3.4.3  Instruction Pointer
  159.  
  160.  2.4  Instruction Format
  161.  2.5  Operand Selection
  162.        2.5.1  Immediate Operands
  163.        2.5.2  Register Operands
  164.        2.5.3  Memory Operands
  165.                2.5.3.1  Segment Selection
  166.                2.5.3.2  Effective-Address Computation
  167.  
  168.  2.6  Interrupts and Exceptions
  169.  
  170.  Chapter 3  Applications Instruction Set
  171.  
  172.  3.1  Data Movement Instructions
  173.        3.1.1  General-Purpose Data Movement Instructions
  174.        3.1.2  Stack Manipulation Instructions
  175.        3.1.3  Type Conversion Instructions
  176.  
  177.  3.2  Binary Arithmetic Instructions
  178.        3.2.1  Addition and Subtraction Instructions
  179.        3.2.2  Comparison and Sign Change Instruction
  180.        3.2.3  Multiplication Instructions
  181.        3.2.4  Division Instructions
  182.  
  183.  3.3  Decimal Arithmetic Instructions
  184.        3.3.1  Packed BCD Adjustment Instructions
  185.        3.3.2  Unpacked BCD Adjustment Instructions
  186.  
  187.  3.4  Logical Instructions
  188.        3.4.1  Boolean Operation Instructions
  189.        3.4.2  Bit Test and Modify Instructions
  190.        3.4.3  Bit Scan Instructions
  191.        3.4.4  Shift and Rotate Instructions
  192.                3.4.4.1  Shift Instructions
  193.                3.4.4.2  Double-Shift Instructions
  194.                3.4.4.3  Rotate Instructions
  195.                3.4.4.4  Fast"bit-blt" Using Double Shift
  196.                           Instructions
  197.                3.4.4.5  Fast Bit-String Insert and Extract
  198.  
  199.        3.4.5  Byte-Set-On-Condition Instructions
  200.        3.4.6  Test Instruction
  201.  
  202.  3.5  Control Transfer Instructions
  203.        3.5.1  Unconditional Transfer Instructions
  204.                3.5.1.1  Jump Instruction
  205.                3.5.1.2  Call Instruction
  206.                3.5.1.3  Return and Return-From-Interrupt Instruction
  207.  
  208.        3.5.2  Conditional Transfer Instructions
  209.                3.5.2.1  Conditional Jump Instructions
  210.                3.5.2.2  Loop Instructions
  211.                3.5.2.3  Executing a Loop or Repeat Zero Times
  212.  
  213.        3.5.3  Software-Generated Interrupts
  214.  
  215.  3.6  String and Character Translation Instructions
  216.        3.6.1  Repeat Prefixes
  217.        3.6.2  Indexing and Direction Flag Control
  218.        3.6.3  String Instructions
  219.  
  220.  3.7  Instructions for Block-Structured Languages
  221.  3.8  Flag Control Instructions
  222.        3.8.1  Carry and Direction Flag Control Instructions
  223.        3.8.2  Flag Transfer Instructions
  224.  
  225.  3.9  Coprocessor Interface Instructions
  226.  3.10 Segment Register Instructions
  227.        3.10.1  Segment-Register Transfer Instructions
  228.        3.10.2  Far Control Transfer Instructions
  229.        3.10.3  Data Pointer Instructions
  230.  
  231.  3.11  Miscellaneous Instructions
  232.         3.11.1  Address Calculation Instruction
  233.         3.11.2  No-Operation Instruction
  234.         3.11.3  Translate Instruction
  235.  
  236.                         PART II    SYSTEMS PROGRAMMING
  237.  
  238.  Chapter 4  Systems Architecture
  239.  
  240.  4.1  Systems Registers
  241.        4.1.1  Systems Flags
  242.        4.1.2  Memory-Management Registers
  243.        4.1.3  Control Registers
  244.        4.1.4  Debug Register
  245.        4.1.5  Test Registers
  246.  
  247.  4.2  Systems Instructions
  248.  
  249.  Chapter 5  Memory Management
  250.  
  251.  5.1  Segment Translation
  252.        5.1.1  Descriptors
  253.        5.1.2  Descriptor Tables
  254.        5.1.3  Selectors
  255.        5.1.4  Segment Registers
  256.  
  257.  5.2  Page Translation
  258.        5.2.1  Page Frame
  259.        5.2.2  Linear Address
  260.        5.2.3  Page Tables
  261.        5.2.4  Page-Table Entries
  262.                5.2.4.1  Page Frame Address
  263.                5.2.4.2  Present Bit
  264.                5.2.4.3  Accessed and Dirty Bits
  265.                5.2.4.4  Read/Write and User/Supervisor Bits
  266.  
  267.        5.2.5  Page Translation Cache
  268.  
  269.  5.3  Combining Segment and Page Translation
  270.        5.3.1  "Flat" Architecture
  271.        5.3.2  Segments Spanning Several Pages
  272.        5.3.3  Pages Spanning Several Segments
  273.        5.3.4  Non-Aligned Page and Segment Boundaries
  274.        5.3.5  Aligned Page and Segment Boundaries
  275.        5.3.6  Page-Table per Segment
  276.  
  277.  Chapter 6  Protection
  278.  
  279.  6.1  Why Protection?
  280.  6.2  Overview of 80386 Protection Mechanisms
  281.  6.3  Segment-Level Protection
  282.        6.3.1  Descriptors Store Protection Parameters
  283.                6.3.1.1  Type Checking
  284.                6.3.1.2  Limit Checking
  285.                6.3.1.3  Privilege Levels
  286.  
  287.        6.3.2  Restricting Access to Data
  288.                6.3.2.1  Accessing Data in Code Segments
  289.  
  290.        6.3.3  Restricting Control Transfers
  291.        6.3.4  Gate Descriptors Guard Procedure Entry Points
  292.                6.3.4.1  Stack Switching
  293.                6.3.4.2  Returning from a Procedure
  294.  
  295.        6.3.5  Some Instructions are Reserved for Operating System
  296.                6.3.5.1  Privileged Instructions
  297.                6.3.5.2  Sensitive Instructions
  298.  
  299.        6.3.6  Instructions for Pointer Validation
  300.                6.3.6.1  Descriptor Validation
  301.                6.3.6.2  Pointer Integrity and RPL
  302.  
  303.  6.4  Page-Level Protection
  304.        6.4.1  Page-Table Entries Hold Protection Parameters
  305.                6.4.1.1  Restricting Addressable Domain
  306.                6.4.1.2  Type Checking
  307.  
  308.        6.4.2  Combining Protection of Both Levels of Page Tables
  309.        6.4.3  Overrides to Page Protection
  310.  
  311.  6.5  Combining Page and Segment Protection
  312.  
  313.  Chapter 7  Multitasking
  314.  
  315.  7.1  Task State Segment
  316.  7.2  TSS Descriptor
  317.  7.3  Task Register
  318.  7.4  Task Gate Descriptor
  319.  7.5  Task Switching
  320.  7.6  Task Linking
  321.        7.6.1  Busy Bit Prevents Loops
  322.        7.6.2  Modifying Task Linkages
  323.  
  324.  7.7  Task Address Space
  325.        7.7.1  Task Linear-to-Physical Space Mapping
  326.        7.7.2  Task Logical Address Space
  327.  
  328.  Chapter 8    Input/Output
  329.  
  330.  8.1  I/O Addressing
  331.        8.1.1  I/O Address Space
  332.        8.1.2  Memory-Mapped I/O
  333.  
  334.  8.2  I/O Instructions
  335.        8.2.1  Register I/O Instructions
  336.        8.2.2  Block I/O Instructions
  337.  
  338.  8.3  Protection and I/O
  339.        8.3.1  I/O Privilege Level
  340.        8.3.2  I/O Permission Bit Map
  341.  
  342.  Chapter 9  Exceptions and Interrupts
  343.  
  344.  9.1  Identifying Interrupts
  345.  9.2  Enabling and Disabling Interrupts
  346.        9.2.1  NMI Masks Further NMls
  347.        9.2.2  IF Masks INTR
  348.        9.2.3  RF Masks Debug Faults
  349.        9.2.4  MOV or POP to SS Masks Some Interrupts and Exceptions
  350.  
  351.  9.3  Priority Among Simultaneous Interrupts and Exceptions
  352.  9.4  Interrupt Descriptor Table
  353.  9.5  IDT Descriptors
  354.  9.6  Interrupt Tasks and Interrupt Procedures
  355.        9.6.1  Interrupt Procedures
  356.                9.6.1.1  Stack of Interrupt Procedure
  357.                9.6.1.2  Returning from an Interrupt Procedure
  358.                9.6.1.3  Flags Usage by Interrupt Procedure
  359.                9.6.1.4  Protection in Interrupt Procedures
  360.  
  361.        9.6.2  Interrupt Tasks
  362.  
  363.  9.7  Error Code
  364.  9.8  Exception Conditions
  365.        9.8.1  Interrupt 0 ── Divide Error
  366.        9.8.2  Interrupt 1 ── Debug Exceptions
  367.        9.8.3  Interrupt 3 ── Breakpoint
  368.        9.8.4  Interrupt 4 ── Overflow
  369.        9.8.5  Interrupt 5 ── Bounds Check
  370.        9.8.6  Interrupt 6 ── Invalid Opcode
  371.        9.8.7  Interrupt 7 ── Coprocessor Not Available
  372.        9.8.8  Interrupt 8 ── Double Fault
  373.        9.8.9  Interrupt 9 ── Coprocessor Segment Overrun
  374.        9.8.10 Interrupt 10 ── Invalid TSS
  375.        9.8.11 Interrupt 11 ── Segment Not Present
  376.        9.8.12 Interrupt 12 ── Stack Exception
  377.        9.8.13 Interrupt 13 ── General Protection Exception
  378.        9.8.14 Interrupt 14 ── Page Fault
  379.                9.8.14.1  Page Fault during Task Switch
  380.                9.8.14.2  Page Fault with Inconsistent Stack Pointer
  381.  
  382.        9.8.15 Interrupt 16 ── Coprocessor Error
  383.  
  384.  9.9  Exception Summary
  385.  
  386.  9.10  Error Code Summary
  387.  
  388.  Chapter 10  Initialization
  389.  
  390.  10.1  Processor State after Reset
  391.  10.2  Software Initialization for Real-Address Mode
  392.         10.2.1  Stack
  393.         10.2.2  Interrupt Table
  394.         10.2.3  First Instructions
  395.  
  396.  10.3  Switching to Protected Mode
  397.  10.4  Software Initialization for Protected Mode
  398.         10.4.1  Interrupt Descriptor Table
  399.         10.4.2  Stack
  400.         10.4.3  Global Descriptor Table
  401.         10.4.4  Page Tables
  402.         10.4.5  First Task
  403.  
  404.  10.5  Initialization Example
  405.  10.6  TLB Testing
  406.         10.6.1  Structure of the TLB
  407.         10.6.2  Test Registers
  408.         10.6.3  Test Operations
  409.  
  410.  Chapter 11  Coprocessing and Multiprocessing
  411.  
  412.  11.1  Coprocessing
  413.         11.1.1  Coprocessor Identification
  414.         11.1.2  ESC and WAIT Instructions
  415.         11.1.3  EM and MP Flags
  416.         11.1.4  The Task-Switched Flag
  417.         11.1.5  Coprocessor Exceptions
  418.                  11.1.5.1  Interrupt 7 ── Coprocessor Not Available
  419.                  11.1.5.2  Interrupt 9 ── Coprocessor Segment Overrun
  420.                  11.1.5.3  Interrupt 16 ── Coprocessor Error
  421.  
  422.  11.2  General Multiprocessing
  423.         11.2.1  LOCK and the LOCK# Signal
  424.         11.2.2  Automatic Locking
  425.         11.2.3  Cache Considerations
  426.  
  427.  Chapter 12  Debugging
  428.  
  429.  12.1  Debugging Features of the Architecture
  430.  12.2  Debug Registers
  431.         12.2.1  Debug Address Registers (DRO-DR3)
  432.         12.2.2  Debug Control Register (DR7)
  433.         12.2.3  Debug Status Register (DR6)
  434.         12.2.4  Breakpoint Field Recognition
  435.  
  436.  12.3  Debug Exceptions
  437.         12.3.1  Interrupt 1 ── Debug Exceptions
  438.                  12.3.1.1  Instruction Address Breakpoint
  439.                  12.3.1.2  Data Address Breakpoint
  440.                  12.3.1.3  General Detect Fault
  441.                  12.3.1.4  Single-Step Trap
  442.                  12.3.1.5  Task Switch Breakpoint
  443.  
  444.         12.3.2  Interrupt 3 ── Breakpoint Exception
  445.  
  446.                            PART III    COMPATIBILITY
  447.  
  448.  Chapter 13  Executing 80286 Protected-Mode Code
  449.  
  450.  13.1  80286 Code Executes as a Subset of the 80386
  451.  13.2  Two Ways to Execute 80286 Tasks
  452.  13.3  Differences from 80286
  453.         13.3.1  Wraparound of 80286 24-Bit Physical Address Space
  454.         13.3.2  Reserved Word of Descriptor
  455.         13.3.3  New Descriptor Type Codes
  456.         13.3.4  Restricted Semantics of LOCK
  457.         13.3.5  Additional Exceptions
  458.  
  459.  Chapter 14  80386 Real-Address Mode
  460.  
  461.  14.1  Physical Address Formation
  462.  14.2  Registers and Instructions
  463.  14.3  Interrupt and Exception Handling
  464.  14.4  Entering and Leaving Real-Address Mode
  465.         14.4.1  Switching to Protected Mode
  466.  
  467.  14.5  Switching Back to Real-Address Mode
  468.  14.6  Real-Address Mode Exceptions
  469.  14.7  Differences from 8086
  470.  14.8  Differences from 80286 Real-Address Mode
  471.         14.8.1  Bus Lock
  472.         14.8.2  Location of First Instruction
  473.         14.8.3  Initial Values of General Registers
  474.         14.8.4  MSW Initialization
  475.  
  476.  Chapter 15  Virtual 8088 Mode
  477.  
  478.  15.1  Executing 8086 Code
  479.         15.1.1  Registers and Instructions
  480.         15.1.2  Linear Address Formation
  481.  
  482.  15.2  Structure of a V86 Task
  483.         15.2.1  Using Paging for V86 Tasks
  484.         15.2.2  Protection within a V86 Task
  485.  
  486.  15.3  Entering and Leaving V86 Mode
  487.         15.3.1  Transitions Through Task Switches
  488.         15.3.2  Transitions Through Trap Gates and Interrupt Gates
  489.  
  490.  15.4  Additional Sensitive Instructions
  491.         15.4.1  Emulating 8086 Operating System Calls
  492.         15.4.2  Virtualizing the Interrupt-Enable Flag
  493.  
  494.  15.5  Virtual I/O
  495.         15.5.1  I/O-Mapped I/O
  496.         15.5.2  Memory-Mapped I/O
  497.         15.5.3  Special I/O Buffers
  498.  
  499.  15.6  Differences from 8086
  500.  15.7  Differences from 80286 Real-Address Mode
  501.  
  502.  Chapter 16  Mixing 16-Bit and 32-Bit Code
  503.  
  504.  16.1  How the 80386 Implements 16-Bit and 32-Bit Features
  505.  16.2  Mixing 32-Bit and 16-Bit Operations
  506.  16.3  Sharing Data Segments among Mixed Code Segments
  507.  16.4  Transferring Control among Mixed Code Segments
  508.         16.4.1  Size of Code-Segment Pointer
  509.         16.4.2  Stack Management for Control Transfers
  510.                  16.4.2.1  Controlling the Operand-Size for a CALL
  511.                  16.4.2.2  Changing Size of Call
  512.  
  513.         16.4.3  Interrupt Control Transfers
  514.         16.4.4  Parameter Translation
  515.         16.4.5  The Interface Procedure
  516.  
  517.                           PART IV    INSTRUCTION SET
  518.  
  519.  Chapter 17  80386 Instruction Set
  520.  
  521.  17.1  Operand-Size and Address-Size Attributes
  522.         17.1.1  Default Segment Attribute
  523.         17.1.2  Operand-Size and Address-Size Instruction Prefixes
  524.         17.1.3  Address-Size Attribute for Stack
  525.  
  526.  17.2  Instruction Format
  527.         17.2.1  ModR/M and SIB Bytes
  528.         17.2.2  How to Read the Instruction Set Pages
  529.                  17.2.2.1  Opcode
  530.                  17.2.2.2  Instruction
  531.                  17.2.2.3  Clocks
  532.                  17.2.2.4  Description
  533.                  17.2.2.5  Operation
  534.                  17.2.2.6  Description
  535.                  17.2.2.7  Flags Affected
  536.                  17.2.2.8  Protected Mode Exceptions
  537.                  17.2.2.9  Real Address Mode Exceptions
  538.                  17.2.2.10 Virtual-8086 Mode Exceptions
  539.  
  540.  Instruction Sets
  541.  
  542.  AAA
  543.  AAD
  544.  AAM
  545.  AAS
  546.  ADC
  547.  ADD
  548.  AND
  549.  ARPL
  550.  BOUND
  551.  BSF
  552.  BSR
  553.  BT
  554.  BTC
  555.  BTR
  556.  BTS
  557.  CALL
  558.  CBW/CWDE
  559.  CLC
  560.  CLD
  561.  CLI
  562.  CLTS
  563.  CMC
  564.  CMP
  565.  CMPS/CMPSB/CMPSW/CMPSD
  566.  CWD/CDQ
  567.  DAA
  568.  DAS
  569.  DEC
  570.  DIV
  571.  ENTER
  572.  HLT
  573.  IDIV
  574.  IMUL
  575.  IN
  576.  INC
  577.  INS/INSB/INSW/INSD
  578.  INT/INTO
  579.  IRET/IRETD
  580.  Jcc
  581.  JMP
  582.  LAHF
  583.  LAR
  584.  LEA
  585.  LEAVE
  586.  LGDT/LIDT
  587.  LGS/LSS/LDS/LES/LFS
  588.  LLDT
  589.  LMSW
  590.  LOCK
  591.  LODS/LODSB/LODSW/LODSD
  592.  LOOP/LOOPcond
  593.  LSL
  594.  LTR
  595.  MOV
  596.  MOV
  597.  MOVS/MOVSB/MOVSW/MOVSD
  598.  MOVSX
  599.  MOVZX
  600.  MUL
  601.  NEG
  602.  NOP
  603.  NOT
  604.  OR
  605.  OUT
  606.  OUTS/OUTSB/OUTSW/OUTSD
  607.  POP
  608.  POPA/POPAD
  609.  POPF/POPFD
  610.  PUSH
  611.  PUSHA/PUSHAD
  612.  PUSHF/PUSHFD
  613.  RCL/RCR/ROL/ROR
  614.  REP/REPE/REPZ/REPNE/REPNZ
  615.  RET
  616.  SAHF
  617.  SAL/SAR/SHL/SHR
  618.  SBB
  619.  SCAS/SCASB/SCASW/SCASD
  620.  SETcc
  621.  SGDT/SIDT
  622.  SHLD
  623.  SHRD
  624.  SLDT
  625.  SMSW
  626.  STC
  627.  STD
  628.  STI
  629.  STOS/STOSB/STOSW/STOSD
  630.  STR
  631.  SUB
  632.  TEST
  633.  VERR,VERW
  634.  WAIT
  635.  XCHG
  636.  XLAT/XLATB
  637.  XOR
  638.  
  639.  Appendix A  Opcode Map
  640.  
  641.  Appendix B  Complete Flag Cross-Reference
  642.  
  643.  Appendix C  Status Flag Summary
  644.  
  645.  Appendix D  Condition Codes
  646.  
  647.  
  648.  Figures
  649.  
  650.  1-1     Example Data Structure
  651.  
  652.  2-1     Two-Component Pointer
  653.  2-2     Fundamental Data Types
  654.  2-3     Bytes, Words, and Doublewords in Memory
  655.  2-4     80386 Data Types
  656.  2-5     80386 Applications Register Set
  657.  2-6     Use of Memory Segmentation
  658.  2-7     80386 Stack
  659.  2-8     EFLAGS Register
  660.  2-9     Instruction Pointer Register
  661.  2-10    Effective Address Computation
  662.  
  663.  3-1     PUSH
  664.  3-2     PUSHA
  665.  3-3     POP
  666.  3-4     POPA
  667.  3-5     Sign Extension
  668.  3-6     SAL and SHL
  669.  3-7     SHR
  670.  3-8     SAR
  671.  3-9     Using SAR to Simulate IDIV
  672.  3-10    Shift Left Double
  673.  3-11    Shift Right Double
  674.  3-12    ROL
  675.  3-13    ROR
  676.  3-14    RCL
  677.  3-15    RCR
  678.  3-16    Formal Definition of the ENTER Instruction
  679.  3-17    Variable Access in Nested Procedures
  680.  3-18    Stack Frame for MAIN at Level 1
  681.  3-19    Stack Frame for Prooedure A
  682.  3-20    Stack Frame for Procedure B at Level 3 Called from A
  683.  3-21    Stack Frame for Procedure C at Level 3 Called from B
  684.  3-22    LAHF and SAHF
  685.  3-23    Flag Format for PUSHF and POPF
  686.  
  687.  4-1     Systems Flags of EFLAGS Register
  688.  4-2     Control Registers
  689.  
  690.  5-1     Address Translation Overview
  691.  5-2     Segment Translation
  692.  5-3     General Segment-Descriptor Format
  693.  5-4     Format of Not-Present Descriptor
  694.  5-5     Descriptor Tables
  695.  5-6     Format of a Selector
  696.  5-7     Segment Registers
  697.  5-8     Format of a Linear Address
  698.  5-9     Page Translation
  699.  5-10    Format of a Page Table Entry
  700.  5-11    Invalid Page Table Entry
  701.  5-12    80386 Addressing Mechanism
  702.  5-13    Descriptor per Page Table
  703.  
  704.  6-1     Protection Fields of Segment Descriptors
  705.  6-2     Levels of Privilege
  706.  6-3     Privilege Check for Data Access
  707.  6-4     Privilege Check for Control Transfer without Gate
  708.  6-5     Format of 80386 Call Gate
  709.  6-6     Indirect Transfer via Call Gate
  710.  6-7     Privilege Check via Call Gate
  711.  6-8     Initial Stack Pointers of TSS
  712.  6-9     Stack Contents after an Interievel Call
  713.  6-10    Protection Fields of Page Table Entries
  714.  
  715.  7-1     80386 32-Bit Task State Segment
  716.  7-2     TSS Descriptor for 32-Bit TSS
  717.  7-3     Task Register
  718.  7-4     Task Gate Descriptor
  719.  7-5     Task Gate Indirectly Identifies Task
  720.  7-6     Partially-Overlapping Linear Spaces
  721.  
  722.  8-1     Memory-Mapped I/O
  723.  8-2     I/O Address Bit Map
  724.  
  725.  9-1     IDT Register and Table
  726.  9-2     Pseudo-Descriptor Format for LIDT and SIDT
  727.  9-3     80386 IDT Gate Descriptors
  728.  9-4     Interrupt Vectoring for Procedures
  729.  9-5     Stack Layout after Exception of Interrupt
  730.  9-6     Interrupt Vectoring for Tasks
  731.  9-7     Error Code Format
  732.  9-8     Page-Fault Error Code Format
  733.  9-9     CR2 Format
  734.  
  735.  10-1    Contents of EDX after RESET
  736.  10-2    Initial Contents of CRO
  737.  10-3    TLB Structure
  738.  10-4    Test Registers
  739.  
  740.  12-1    Debug Registers
  741.  
  742.  14-1    Real-Address Mode Address Formation
  743.  
  744.  15-1    V86 Mode Address Formation
  745.  15-2    Entering and Leaving an 8086 Program
  746.  15-3    PL 0 Stack after Interrupt in V86 Task
  747.  
  748.  16-1    Stack after Far 16-Bit and 32-Bit Calls
  749.  
  750.  17-1    80386 Instruction Format
  751.  17-2    ModR/M and SIB Byte Formats
  752.  17-3    Bit Offset for BIT[EAX, 21]
  753.  17-4    Memory Bit Indexing
  754.  
  755.  
  756.  Tables
  757.  
  758.  2-1      Default Segment Register Selection Rules
  759.  2-2      80386 Reserved Exceptions and Interrupts
  760.  
  761.  3-1      Bit Test and Modify Instructions
  762.  3-2      Interpretation of Conditional Transfers
  763.  
  764.  6-1      System and Gate Descriptor Types
  765.  6-2      Useful Combinations of E, G, and B Bits
  766.  6-3      Interievel Return Checks
  767.  6-4      Valid Descriptor Types for LSL
  768.  6-5      Combining Directory and Page Protection
  769.  
  770.  7-1      Checks Made during a Task Switch
  771.  7-2      Effect of Task Switch on BUSY, NT, and Back-Link
  772.  
  773.  9-1      Interrupt and Exception ID Assignments
  774.  9-2      Priority Among Simultaneous Interrupts and Exceptions
  775.  9-3      Double-Fault Detection Classes
  776.  9-4      Double-Fault Definition
  777.  9-5      Conditions That Invalidate the TSS
  778.  9-6      Exception Summary
  779.  9-7      Error-Code Summary
  780.  
  781.  10-1     Meaning of D, U, and W Bit Pairs
  782.  
  783.  12-1     Breakpeint Field Recognition Examples
  784.  12-2     Debug Exception Conditions
  785.  
  786.  14-1     80386 Real-Address Mode Exceptions
  787.  14-2     New 80386 Exceptions
  788.  
  789.  17-1     Effective Size Attributes
  790.  17-2     16-Bit Addressing Forms with the ModR/M Byte
  791.  17-3     32-Bit Addressing Forms with the ModR/M Byte
  792.  17-4     32-Bit Addressing Forms with the SIB Byte
  793.  17-5     Task Switch Times for Exceptions
  794.  17-6     80386 Exceptions
  795.  
  796.  
  797.  Chapter 1  Introduction to the 80386
  798.  
  799.  ────────────────────────────────────────────────────────────────────────────
  800.  
  801.  The 80386 is an advanced 32-bit microprocessor optimized for multitasking
  802.  operating systems and designed for applications needing very high
  803.  performance. The 32-bit registers and data paths support 32-bit addresses
  804.  and data types. The processor can address up to four gigabytes of physical
  805.  memory and 64 terabytes (2^(46) bytes) of virtual memory. The on-chip
  806.  memory-management facilities include address translation registers,
  807.  advanced multitasking hardware, a protection mechanism, and paged virtual
  808.  memory. Special debugging registers provide data and code breakpoints even
  809.  in ROM-based software.
  810.  
  811.  
  812.  1.1  Organization of This Manual
  813.  
  814.  This book presents the architecture of the 80386 in five parts:
  815.  
  816.     Part I      ── Applications Programming
  817.     Part II     ── Systems Programming
  818.     Part III    ── Compatibility
  819.     Part IV     ── Instruction Set
  820.     Appendices
  821.  
  822.  These divisions are determined in part by the architecture itself and in
  823.  part by the different ways the book will be used. As the following table
  824.  indicates, the latter two parts are intended as reference material for
  825.  programmers actually engaged in the process of developing software for the
  826.  80386. The first three parts are explanatory, showing the purpose of
  827.  architectural features, developing terminology and concepts, and describing
  828.  instructions as they relate to specific purposes or to specific
  829.  architectural features.
  830.  
  831.  Explanation                Part I   ── Applications Programming
  832.                             Part II  ── Systems Programming
  833.                             Part III ── Compatibility
  834.  
  835.  Reference                  Part IV  ── Instruction Set
  836.                             Appendices
  837.  
  838.  The first three parts follow the execution modes and protection features of
  839.  the 80386 CPU. The distinction between applications features and systems
  840.  features is determined by the protection mechanism of the 80386. One purpose
  841.  of protection is to prevent applications from interfering with the operating
  842.  system; therefore, the processor makes certain registers and instructions
  843.  inaccessible to applications programs. The features discussed in Part I are
  844.  those that are accessible to applications; the features in Part II are
  845.  available only to systems software that has been given special privileges or
  846.  in unprotected systems.
  847.  
  848.  The processing mode of the 80386 also determines the features that are
  849.  accessible. The 80386 has three processing modes:
  850.  
  851.     1.  Protected Mode.
  852.     2.  Real-Address Mode.
  853.     3.  Virtual 8086 Mode.
  854.  
  855.  Protected mode is the natural 32-bit environment of the 80386 processor. In
  856.  this mode all instructions and features are available.
  857.  
  858.  Real-address mode (often called just "real mode") is the mode of the
  859.  processor immediately after RESET. In real mode the 80386 appears to
  860.  programmers as a fast 8086 with some new instructions. Most applications of
  861.  the 80386 will use real mode for initialization only.
  862.  
  863.  Virtual 8086 mode (also called V86 mode) is a dynamic mode in the sense
  864.  that the processor can switch repeatedly and rapidly between V86 mode and
  865.  protected mode. The CPU enters V86 mode from protected mode to execute an
  866.  8086 program, then leaves V86 mode and enters protected mode to continue
  867.  executing a native 80386 program.
  868.  
  869.  The features that are available to applications programs in protected mode
  870.  and to all programs in V86 mode are the same. These features form the
  871.  content of Part I. The additional features that are available to systems
  872.  software in protected mode form Part II. Part III explains real-address
  873.  mode and V86 mode, as well as how to execute a mix of 32-bit and 16-bit
  874.  programs.
  875.  
  876.  Available in All Modes             Part I ── Applications Programming
  877.  
  878.  Available in Protected             Part II ── Systems Programming
  879.  Mode Only
  880.  
  881.  Compatibility Modes                Part III ── Compatibility
  882.  
  883.  
  884.  1.1.1  Part I ── Applications Programming
  885.  
  886.  This part presents those aspects of the architecture that are customarily
  887.  used by applications programmers.
  888.  
  889.  Chapter 2 ── Basic Programming Model: Introduces the models of memory
  890.  organization. Defines the data types. Presents the register set used by
  891.  applications. Introduces the stack. Explains string operations. Defines the
  892.  parts of an instruction. Explains addressing calculations. Introduces
  893.  interrupts and exceptions as they may apply to applications programming.
  894.  
  895.  Chapter 3 ── Application Instruction Set: Surveys the instructions commonly
  896.  used for applications programming. Considers instructions in functionally
  897.  related groups; for example, string instructions are considered in one
  898.  section, while control-transfer instructions are considered in another.
  899.  Explains the concepts behind the instructions. Details of individual
  900.  instructions are deferred until Part IV, the instruction-set reference.
  901.  
  902.  
  903.  1.1.2  Part II ── Systems Programming
  904.  
  905.  This part presents those aspects of the architecture that are customarily
  906.  used by programmers who write operating systems, device drivers, debuggers,
  907.  and other software that supports applications programs in the protected mode
  908.  of the 80386.
  909.  
  910.  Chapter 4 ── Systems Architecture: Surveys the features of the 80386 that
  911.  are used by systems programmers. Introduces the remaining registers and data
  912.  structures of the 80386 that were not discussed in Part I. Introduces the
  913.  systems-oriented instructions in the context of the registers and data
  914.  structures they support. Points to the chapter where each register, data
  915.  structure, and instruction is considered in more detail.
  916.  
  917.  Chapter 5 ── Memory Management: Presents details of the data structures,
  918.  registers, and instructions that support virtual memory and the concepts of
  919.  segmentation and paging. Explains how systems designers can choose a model
  920.  of memory organization ranging from completely linear ("flat") to fully
  921.  paged and segmented.
  922.  
  923.  Chapter 6 ── Protection: Expands on the memory management features of the
  924.  80386 to include protection as it applies to both segments and pages.
  925.  Explains the implementation of privilege rules, stack switching, pointer
  926.  validation, user and supervisor modes. Protection aspects of multitasking
  927.  are deferred until the following chapter.
  928.  
  929.  Chapter 7 ── Multitasking: Explains how the hardware of the 80386 supports
  930.  multitasking with context-switching operations and intertask protection.
  931.  
  932.  Chapter 8 ── Input/Output: Reveals the I/O features of the 80386, including
  933.  I/O instructions, protection as it relates to I/O, and the I/O permission
  934.  map.
  935.  
  936.  Chapter 9 ── Exceptions and Interrupts: Explains the basic interrupt
  937.  mechanisms of the 80386. Shows how interrupts and exceptions relate to
  938.  protection. Discusses all possible exceptions, listing causes and including
  939.  information needed to handle and recover from the exception.
  940.  
  941.  Chapter 10 ── Initialization: Defines the condition of the processor after
  942.  RESET or power-up. Explains how to set up registers, flags, and data
  943.  structures for either real-address mode or protected mode. Contains an
  944.  example of an initialization program.
  945.  
  946.  Chapter 11 ── Coprocessing and Multiprocessing: Explains the instructions
  947.  and flags that support a numerics coprocessor and multiple CPUs with shared
  948.  memory.
  949.  
  950.  Chapter 12 ── Debugging: Tells how to use the debugging registers of the
  951.  80386.
  952.  
  953.  
  954.  1.1.3  Part III ── Compatibility
  955.  
  956.  Other parts of the book treat the processor primarily as a 32-bit machine,
  957.  omitting for simplicity its facilities for 16-bit operations. Indeed, the
  958.  80386 is a 32-bit machine, but its design fully supports 16-bit operands and
  959.  addressing, too. This part completes the picture of the 80386 by explaining
  960.  the features of the architecture that support 16-bit programs and 16-bit
  961.  operations in 32-bit programs. All three processor modes are used to
  962.  execute 16-bit programs: protected mode can directly execute 16-bit 80286
  963.  protected mode programs, real mode executes 8086 programs and real-mode
  964.  80286 programs, and virtual 8086 mode executes 8086 programs in a
  965.  multitasking environment with other 80386 protected-mode programs. In
  966.  addition, 32-bit and 16-bit modules and individual 32-bit and 16-bit
  967.  operations can be mixed in protected mode.
  968.  
  969.  Chapter 13 ── Executing 80286 Protected-Mode Code: In its protected mode,
  970.  the 80386 can execute complete 80286 protected-mode systems, because 80286
  971.  capabilities are a subset of 80386 capabilities.
  972.  
  973.  Chapter 14 ── 80386 Real-Address Mode: Explains the real mode of the 80386
  974.  CPU. In this mode the 80386 appears as a fast real-mode 80286 or fast 8086
  975.  enhanced with additional instructions.
  976.  
  977.  Chapter 15 ── Virtual 8086 Mode: The 80386 can switch rapidly between its
  978.  protected mode and V86 mode, giving it the ability to multiprogram 8086
  979.  programs along with "native mode" 32-bit programs.
  980.  
  981.  Chapter 16 ── Mixing 16-Bit and 32-Bit Code: Even within a program or task,
  982.  the 80386 can mix 16-bit and 32-bit modules. Furthermore, any given module
  983.  can utilize both 16-bit and 32-bit operands and addresses.
  984.  
  985.  
  986.  1.1.4  Part IV ── Instruction Set
  987.  
  988.  Parts I, II, and III present overviews of the instructions as they relate
  989.  to specific aspects of the architecture, but this part presents the
  990.  instructions in alphabetical order, providing the detail needed by
  991.  assembly-language programmers and programmers of debuggers, compilers,
  992.  operating systems, etc. Instruction descriptions include algorithmic
  993.  description of operation, effect of flag settings, effect on flag settings,
  994.  effect of operand- or address-size attributes, effect of processor modes,
  995.  and possible exceptions.
  996.  
  997.  
  998.  1.1.5  Appendices
  999.  
  1000.  The appendices present tables of encodings and other details in a format
  1001.  designed for quick reference by assembly-language and systems programmers.
  1002.  
  1003.  
  1004.  1.2  Related Literature
  1005.  
  1006.  The following books contain additional material concerning the 80386
  1007.  microprocessor:
  1008.  
  1009.    ■  Introduction to the 80386, order number 231252
  1010.  
  1011.    ■  80386 Hardware Reference Manual, order number 231732
  1012.  
  1013.    ■  80386 System Software Writer's Guide, order number 231499
  1014.  
  1015.    ■  80386 High Performance 32-bit Microprocessor with Integrated Memory
  1016.       Management (Data Sheet), order number 231630
  1017.  
  1018.  
  1019.  1.3  Notational Conventions
  1020.  
  1021.  This manual uses special notations for data-structure formats, for symbolic
  1022.  representation of instructions, for hexadecimal numbers, and for super- and
  1023.  sub-scripts. Subscript characters are surrounded by {curly brackets}, for
  1024.  example 10{2} = 10 base 2. Superscript characters are preceeded by a caret
  1025.  and enclosed within (parentheses), for example 10^(3) = 10 to the third
  1026.  power. A review of these notations will make it easier to read the
  1027.  manual.
  1028.  
  1029.  1.3.1  Data-Structure Formats
  1030.  
  1031.  In illustrations of data structures in memory, smaller addresses appear at
  1032.  the lower-right part of the figure; addresses increase toward the left and
  1033.  upwards. Bit positions are numbered from right to left. Figure 1-1
  1034.  illustrates this convention.
  1035.  
  1036.  
  1037.  1.3.2  Undefined Bits and Software Compatibility
  1038.  
  1039.  In many register and memory layout descriptions, certain bits are marked as
  1040.  undefined. When bits are marked as undefined (as illustrated in Figure
  1041.  1-1), it is essential for compatibility with future processors that
  1042.  software treat these bits as undefined. Software should follow these
  1043.  guidelines in dealing with undefined bits:
  1044.  
  1045.    ■  Do not depend on the states of any undefined bits when testing the
  1046.       values of registers that contain such bits. Mask out the undefined bits
  1047.       before testing.
  1048.  
  1049.    ■  Do not depend on the states of any undefined bits when storing them in
  1050.       memory or in another register.
  1051.  
  1052.    ■  Do not depend on the ability to retain information written into any
  1053.       undefined bits.
  1054.  
  1055.    ■  When loading a register, always load the undefined bits as zeros or
  1056.       reload them with values previously stored from the same register.
  1057.  
  1058.  ────────────────────────────────────────────────────────────────────────────
  1059.  NOTE
  1060.    Depending upon the values of undefined register bits will make software
  1061.    dependent upon the unspecified manner in which the 80386 handles these
  1062.    bits. Depending upon undefined values risks making software incompatible
  1063.    with future processors that define usages for these bits. AVOID ANY
  1064.    SOFTWARE DEPENDENCE UPON THE STATE OF UNDEFINED 80386 REGISTER BITS.
  1065.  ────────────────────────────────────────────────────────────────────────────
  1066.  
  1067.  
  1068.  Figure 1-1.  Example Data Structure
  1069.  
  1070.   GREATEST                 DATA STRUCTURE
  1071.   ADDRESS
  1072.    31              23              15              7             0 ──BIT
  1073.   ╔═══════════════╪═══════════════╪═══════════════╪═══════════════╗   OFFSET
  1074.   ║                                                               ║28
  1075.   ╠═══════════════╪═══════════════╪═══════════════╪═══════════════╣
  1076.   ║                                                               ║24
  1077.   ╠═══════════════╪═══════════════╪═══════════════╪═══════════════╣
  1078.   ║                                                               ║20
  1079.   ╠═══════════════╪═══════════════╪═══════════════╪═══════════════╣
  1080.   ║                                                               ║16
  1081.   ╠═══════════════╪═══════════════╪═══════════════╪═══════════════╣
  1082.   ║                                                               ║12
  1083.   ╠═══════════════╪═══════════════╪═══════════════╪═══════════════╣
  1084.   ║                                                               ║8
  1085.   ╠═══════════════╪═══════════════╪═══════════════╪═══════════════╣
  1086.   ║                           UNDEFINED                           ║4
  1087.   ╠═══════════════╪═══════════════╪═══════════════╪═══════════════╣  SMALLEST
  1088.   ║    BYTE 3          BYTE 2          BYTE 1          BYTE 0     ║0 ADDRESS
  1089.   ╚═══════════════╪═══════════════╪═══════════════╪═══════════════╝
  1090.                                                      BYTE OFFSET───┘
  1091.  
  1092.  
  1093.  1.3.3  Instruction Operands
  1094.  
  1095.  When instructions are represented symbolically, a subset of the 80386
  1096.  Assembly Language is used. In this subset, an instruction has the following
  1097.  format:
  1098.  
  1099.  label: prefix mnemonic argument1, argument2, argument3
  1100.  
  1101.  where:
  1102.  
  1103.    ■  A label is an identifier that is followed by a colon.
  1104.  
  1105.    ■  A prefix is an optional reserved name for one of the instruction
  1106.       prefixes.
  1107.  
  1108.    ■  A mnemonic is a reserved name for a class of instruction opcodes that
  1109.       have the same function.
  1110.  
  1111.    ■  The operands argument1, argument2, and argument3 are optional. There
  1112.       may be from zero to three operands, depending on the opcode.  When
  1113.       present, they take the form of either literals or identifiers for data
  1114.       items.  Operand identifiers are either reserved names of registers or
  1115.       are assumed to be assigned to data items declared in another part of
  1116.       the program (which may not be shown in the example). When two operands
  1117.       are present in an instruction that modifies data, the right operand is
  1118.       the source and the left operand is the destination.
  1119.  
  1120.  For example:
  1121.  
  1122.  LOADREG: MOV EAX, SUBTOTAL
  1123.  
  1124.  In this example LOADREG is a label, MOV is the mnemonic identifier of an
  1125.  opcode, EAX is the destination operand, and SUBTOTAL is the source operand.
  1126.  
  1127.  1.3.4  Hexadecimal Numbers
  1128.  
  1129.  Base 16 numbers are represented by a string of hexadecimal digits followed
  1130.  by the character H.  A hexadecimal digit is a character from the set (0, 1,
  1131.  2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F).  In some cases, especially in
  1132.  examples of program syntax, a leading zero is added if the number would
  1133.  otherwise begin with one of the digits A-F.  For example, 0FH is equivalent
  1134.  to the decimal number 15.
  1135.  
  1136.  1.3.5  Sub- and Super-Scripts
  1137.  
  1138.  This manual uses special notation to represent sub- and super-script
  1139.  characters. Sub-script characters are surrounded by {curly brackets}, for
  1140.  example 10{2} = 10 base 2. Super-script characters are preceeded by a
  1141.  caret and enclosed within (parentheses), for example 10^(3) = 10 to the
  1142.  third power.
  1143.  
  1144.  
  1145.                       PART I    APPLICATIONS PROGRAMMING
  1146.  
  1147.  
  1148.  Chapter 2  Basic Programming Model
  1149.  
  1150.  ────────────────────────────────────────────────────────────────────────────
  1151.  
  1152.  This chapter describes the 80386 application programming environment as
  1153.  seen by assembly language programmers when the processor is executing in
  1154.  protected mode. The chapter introduces programmers to those features of the
  1155.  80386 architecture that directly affect the design and implementation of
  1156.  80386 applications programs. Other chapters discuss 80386 features that
  1157.  relate to systems programming or to compatibility with other processors of
  1158.  the 8086 family.
  1159.  
  1160.  The basic programming model consists of these aspects:
  1161.  
  1162.    ■  Memory organization and segmentation
  1163.    ■  Data types
  1164.    ■  Registers
  1165.    ■  Instruction format
  1166.    ■  Operand selection
  1167.    ■  Interrupts and exceptions
  1168.  
  1169.  Note that input/output is not included as part of the basic programming
  1170.  model. Systems designers may choose to make I/O instructions available to
  1171.  applications or may choose to reserve these functions for the operating
  1172.  system. For this reason, the I/O features of the 80386 are discussed in Part
  1173.  II.
  1174.  
  1175.  This chapter contains a section for each aspect of the architecture that is
  1176.  normally visible to applications.
  1177.  
  1178.  
  1179.  2.1  Memory Organization and Segmentation
  1180.  
  1181.  The physical memory of an 80386 system is organized as a sequence of 8-bit
  1182.  bytes. Each byte is assigned a unique address that ranges from zero to a
  1183.  maximum of 2^(32) -1 (4 gigabytes).
  1184.  
  1185.  80386 programs, however, are independent of the physical address space.
  1186.  This means that programs can be written without knowledge of how much
  1187.  physical memory is available and without knowledge of exactly where in
  1188.  physical memory the instructions and data are located.
  1189.  
  1190.  The model of memory organization seen by applications programmers is
  1191.  determined by systems-software designers. The architecture of the 80386
  1192.  gives designers the freedom to choose a model for each task. The model of
  1193.  memory organization can range between the following extremes:
  1194.  
  1195.    ■  A "flat" address space consisting of a single array of up to 4
  1196.       gigabytes.
  1197.  
  1198.    ■  A segmented address space consisting of a collection of up to 16,383
  1199.       linear address spaces of up to 4 gigabytes each.
  1200.  
  1201.  Both models can provide memory protection. Different tasks may employ
  1202.  different models of memory organization. The criteria that designers use to
  1203.  determine a memory organization model and the means that systems programmers
  1204.  use to implement that model are covered in Part II──Systems Programming.
  1205.  
  1206.  
  1207.  2.1.1  The "Flat" Model
  1208.  
  1209.  In a "flat" model of memory organization, the applications programmer sees
  1210.  a single array of up to 2^(32) bytes (4 gigabytes). While the physical
  1211.  memory can contain up to 4 gigabytes, it is usually much smaller; the
  1212.  processor maps the 4 gigabyte flat space onto the physical address space by
  1213.  the address translation mechanisms described in Chapter 5. Applications
  1214.  programmers do not need to know the details of the mapping.
  1215.  
  1216.  A pointer into this flat address space is a 32-bit ordinal number that may
  1217.  range from 0 to 2^(32) -1. Relocation of separately-compiled modules in this
  1218.  space must be performed by systems software (e.g., linkers, locators,
  1219.  binders, loaders).
  1220.  
  1221.  
  1222.  2.1.2  The Segmented Model
  1223.  
  1224.  In a segmented model of memory organization, the address space as viewed by
  1225.  an applications program (called the logical address space) is a much larger
  1226.  space of up to 2^(46) bytes (64 terabytes). The processor maps the 64
  1227.  terabyte logical address space onto the physical address space (up to 4
  1228.  gigabytes) by the address translation mechanisms described in Chapter 5.
  1229.  Applications programmers do not need to know the details of this mapping.
  1230.  
  1231.  Applications programmers view the logical address space of the 80386 as a
  1232.  collection of up to 16,383 one-dimensional subspaces, each with a specified
  1233.  length. Each of these linear subspaces is called a segment. A segment is a
  1234.  unit of contiguous address space. Segment sizes may range from one byte up
  1235.  to a maximum of 2^(32) bytes (4 gigabytes).
  1236.  
  1237.  A complete pointer in this address space consists of two parts (see Figure
  1238.  2-1):
  1239.  
  1240.    1.  A segment selector, which is a 16-bit field that identifies a
  1241.        segment.
  1242.  
  1243.    2.  An offset, which is a 32-bit ordinal that addresses to the byte level
  1244.        within a segment.
  1245.  
  1246.  During execution of a program, the processor associates with a segment
  1247.  selector the physical address of the beginning of the segment. Separately
  1248.  compiled modules can be relocated at run time by changing the base address
  1249.  of their segments. The size of a segment is variable; therefore, a segment
  1250.  can be exactly the size of the module it contains.
  1251.  
  1252.  
  1253.  2.2  Data Types
  1254.  
  1255.  Bytes, words, and doublewords are the fundamental data types (refer to
  1256.  Figure 2-2). A byte is eight contiguous bits starting at any logical
  1257.  address. The bits are numbered 0 through 7; bit zero is the least
  1258.  significant bit.
  1259.  
  1260.  A word is two contiguous bytes starting at any byte address. A word thus
  1261.  contains 16 bits. The bits of a word are numbered from 0 through 15; bit 0
  1262.  is the least significant bit. The byte containing bit 0 of the word is
  1263.  called the low byte; the byte containing bit 15 is called the high byte.
  1264.  
  1265.  Each byte within a word has its own address, and the smaller of the
  1266.  addresses is the address of the word. The byte at this lower address
  1267.  contains the eight least significant bits of the word, while the byte at the
  1268.  higher address contains the eight most significant bits.
  1269.  
  1270.  A doubleword is two contiguous words starting at any byte address. A
  1271.  doubleword thus contains 32 bits. The bits of a doubleword are numbered from
  1272.  0 through 31; bit 0 is the least significant bit. The word containing bit 0
  1273.  of the doubleword is called the low word; the word containing bit 31 is
  1274.  called the high word.
  1275.  
  1276.  Each byte within a doubleword has its own address, and the smallest of the
  1277.  addresses is the address of the doubleword. The byte at this lowest address
  1278.  contains the eight least significant bits of the doubleword, while the byte
  1279.  at the highest address contains the eight most significant bits. Figure 2-3
  1280.  illustrates the arrangement of bytes within words anddoublewords.
  1281.  
  1282.  Note that words need not be aligned at even-numbered addresses and
  1283.  doublewords need not be aligned at addresses evenly divisible by four. This
  1284.  allows maximum flexibility in data structures (e.g., records containing
  1285.  mixed byte, word, and doubleword items) and efficiency in memory
  1286.  utilization. When used in a configuration with a 32-bit bus, actual
  1287.  transfers of data between processor and memory take place in units of
  1288.  doublewords beginning at addresses evenly divisible by four; however, the
  1289.  processor converts requests for misaligned words or doublewords into the
  1290.  appropriate sequences of requests acceptable to the memory interface. Such
  1291.  misaligned data transfers reduce performance by requiring extra memory
  1292.  cycles. For maximum performance, data structures (including stacks) should
  1293.  be designed in such a way that, whenever possible, word operands are aligned
  1294.  at even addresses and doubleword operands are aligned at addresses evenly
  1295.  divisible by four. Due to instruction prefetching and queuing within the
  1296.  CPU, there is no requirement for instructions to be aligned on word or
  1297.  doubleword boundaries. (However, a slight increase in speed results if the
  1298.  target addresses of control transfers are evenly divisible by four.)
  1299.  
  1300.  Although bytes, words, and doublewords are the fundamental types of
  1301.  operands, the processor also supports additional interpretations of these
  1302.  operands. Depending on the instruction referring to the operand, the
  1303.  following additional data types are recognized:
  1304.  
  1305.  Integer:
  1306.  A signed binary numeric value contained in a 32-bit doubleword,16-bit word,
  1307.  or 8-bit byte. All operations assume a 2's complement representation. The
  1308.  sign bit is located in bit 7 in a byte, bit 15 in a word, and bit 31 in a
  1309.  doubleword. The sign bit has the value zero for positive integers and one
  1310.  for negative. Since the high-order bit is used for a sign, the range of an
  1311.  8-bit integer is -128 through +127; 16-bit integers may range from -32,768
  1312.  through +32,767; 32-bit integers may range from -2^(31) through +2^(31) -1.
  1313.  The value zero has a positive sign.
  1314.  
  1315.  Ordinal:
  1316.  An unsigned binary numeric value contained in a 32-bit doubleword,
  1317.  16-bit word, or 8-bit byte. All bits are considered in determining
  1318.  magnitude of the number. The value range of an 8-bit ordinal number
  1319.  is 0-255; 16 bits can represent values from 0 through 65,535; 32 bits
  1320.  can represent values from 0 through 2^(32) -1.
  1321.  
  1322.  Near Pointer:
  1323.  A 32-bit logical address. A near pointer is an offset within a segment.
  1324.  Near pointers are used in either a flat or a segmented model of memory
  1325.  organization.
  1326.  
  1327.  Far Pointer:
  1328.  A 48-bit logical address of two components: a 16-bit segment selector
  1329.  component and a 32-bit offset component. Far pointers are used by
  1330.  applications programmers only when systems designers choose a
  1331.  segmented memory organization.
  1332.  
  1333.  String:
  1334.  A contiguous sequence of bytes, words, or doublewords. A string may
  1335.  contain from zero bytes to 2^(32) -1 bytes (4 gigabytes).
  1336.  
  1337.  Bit field:
  1338.  A contiguous sequence of bits. A bit field may begin at any bit position
  1339.  of any byte and may contain up to 32 bits.
  1340.  
  1341.  Bit string:
  1342.  A contiguous sequence of bits. A bit string may begin at any bit position
  1343.  of any byte and may contain up to 2^(32) -1 bits.
  1344.  
  1345.  BCD:
  1346.  A byte (unpacked) representation of a decimal digit in the range0 through
  1347.  9. Unpacked decimal numbers are stored as unsigned byte quantities. One
  1348.  digit is stored in each byte. The magnitude of the number is determined from
  1349.  the low-order half-byte; hexadecimal values 0-9 are valid and are
  1350.  interpreted as decimal numbers. The high-order half-byte must be zero for
  1351.  multiplication and division; it may contain any value for addition and
  1352.  subtraction.
  1353.  
  1354.  Packed BCD:
  1355.  A byte (packed) representation of two decimal digits, each in the range
  1356.  0 through 9. One digit is stored in each half-byte. The digit in the
  1357.  high-order half-byte is the most significant. Values 0-9 are valid in each
  1358.  half-byte. The range of a packed decimal byte is 0-99.
  1359.  
  1360.  Figure 2-4 graphically summarizes the data types supported by the 80386.
  1361.  
  1362.  
  1363.  Figure 2-1.  Two-Component Pointer
  1364.  
  1365.                                                    
  1366.                                     ║               ║
  1367.                                     ╠═══════════════╣─┐
  1368.         32            0             ║               ║ │
  1369.        ╔═══════╪═══════╗   ╔═══╗    ╠═══════════════╣ │
  1370.        ║    OFFSET     ╟───╢ + ╟───║    OPERAND    ║ │
  1371.        ╚═══════╪═══════╝   ╚═══╝    ╠═══════════════╣ ├─ SELECTED SEGMENT
  1372.                                    ║               ║ │
  1373.             16    0          │      ║               ║ │
  1374.            ╔═══════╗         │      ║               ║ │
  1375.            ║SEGMENT╟─────────∙─────╠═══════════════╣─┘
  1376.            ╚═══════╝                ║               ║
  1377.                                     ║               ║
  1378.                                     ║               ║
  1379.                                                    
  1380.  
  1381.  
  1382.  Figure 2-2.  Fundamental Data Types
  1383.  
  1384.    7              0
  1385.   ╔═══════════════╗
  1386.   ║     BYTE      ║  BYTE
  1387.   ╚═══════════════╝
  1388.  
  1389.    15              7              0
  1390.   ╔═══════════════╤═══════════════╗
  1391.   ║   HIGH BYTE   │   LOW BYTE    ║  WORD
  1392.   ╚═══════════════╧═══════════════╝
  1393.      address n+1      address n
  1394.  
  1395.    31              23              15              7             0
  1396.   ╔═══════════════╪═══════════════╪═══════════════╪══════════════╗
  1397.   ║           HIGH WORD           │            LOW WORD          ║ DOUBLEWORD
  1398.   ╚═══════════════╪═══════════════╪═══════════════╪══════════════╝
  1399.      address n+3     address n+2     address n+1      address n
  1400.  
  1401.  
  1402.  Figure 2-3.  Bytes, Words, and Doublewords in Memory
  1403.  
  1404.                    MEMORY
  1405.         BYTE       VALUES
  1406.       ADDRESS   ╔══════════╗
  1407.                E║          ║
  1408.                 ╠══════════╣──┐
  1409.                D║    7A    ║  ├─ DOUBLE WORD AT ADDRESS A
  1410.                 ╠══════════╣─┐│  CONTAINS 7AFE0636
  1411.                C║    FE    ║ ││
  1412.                 ╠══════════╣ ├─ WORD AT ADDRESS B
  1413.                B║    06    ║ ││ CONTAINS FE06
  1414.                 ╠══════════╣─┘│
  1415.                A║    36    ║  │
  1416.                 ╠══════════╣══╡
  1417.                9║    1F    ║  ├─ WORD AT ADDRESS 9
  1418.                 ╠══════════╣──┘  CONTAINS IF
  1419.                8║          ║
  1420.                 ╠══════════╣──┐
  1421.                7║    23    ║  │
  1422.                 ╠══════════╣  ├─ WORD AT ADDRESS 6
  1423.                6║    OB    ║  │  CONTAINS 23OB
  1424.                 ╠══════════╣──┘
  1425.                5║          ║
  1426.                 ╠══════════╣
  1427.                4║          ║
  1428.                 ╠══════════╣──┐
  1429.                3║    74    ║  │
  1430.                 ╠══════════╣─┐├─ WORD AT ADDRESS 2
  1431.                2║    CB    ║ ││  CONTAINS 74CB
  1432.                 ╠══════════╣──┘
  1433.                1║    31    ║ ├── WORD AT ADDRESS 1
  1434.                 ╠══════════╣─┘   CONTAINS CB31
  1435.                0║          ║
  1436.                 ╚══════════╝
  1437.  
  1438.  
  1439.  Figure 2-4.  80386 Data Types
  1440.  
  1441.                                                             +1       0
  1442.            7       0              7       0            15 14    8 7      0
  1443.       BYTE ╔╤╤╤╤╤╤╤╗         BYTE ╔╤╤╤╤╤╤╤╗         WORD ╔╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╗
  1444.    INTEGER ║│  │   ║      ORDINAL ║   │   ║      INTEGER ║│  │   │   │   ║
  1445.            ╚╧══════╝              ╚═══════╝              ╚╧══════╧═══════╝
  1446.    SIGN BIT┘└──────┘              └───────┘      SIGN BIT┘└MSB           │
  1447.             MAGNITUDE             MAGNITUDE              └───────────────┘
  1448.                                                              MAGNITUDE
  1449.  
  1450.  
  1451.              +1       0                       +3     +2      +1       0
  1452.           15              0               31            16 15             0
  1453.      WORD ╔╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╗    DOUBLEWORD ╔╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╗
  1454.   ORDINAL ║│  │   │   │   ║       INTEGER ║│  │   │   │   │   │   │   │   ║
  1455.           ╚╧══════╧═══════╝               ╚╧══════╧═══════╧═══════╧═══════╝
  1456.           │               │       SIGN BIT┘└MSB                           │
  1457.           └───────────────┘               └───────────────────────────────┘
  1458.               MAGNITUDE                              MAGNITUDE
  1459.  
  1460.  
  1461.                             +3      +2      +1       0
  1462.                          31                              0
  1463.               DOUBLEWORD ╔╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╗
  1464.                  ORDINAL ║   │   │   │   │   │   │   │   ║
  1465.                          ╚═══════╧═══════╧═══════╧═══════╝
  1466.                          └───────────────────────────────┘
  1467.                                      MAGNITUDE
  1468.  
  1469.                             +N              +1       0
  1470.                          7       0       7      0 7      0
  1471.             BINARY CODED ╔╤╤╤╤╤╤╤╗       ╔╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╗
  1472.            DECIMAL (BCD) ║   │   ║    ║   │   │   │   ║
  1473.                          ╚═══════╝       ╚═══════╧═══════╝
  1474.                             BCD             BCD     BCD
  1475.                           DIGIT N         DIGIT 1  DIGIT 0
  1476.  
  1477.                             +N              +1       0
  1478.                          7       0       7      0 7      0
  1479.                   PACKED ╔╤╤╤╤╤╤╤╗       ╔╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╗
  1480.                      BCD ║   │   ║    ║   │   │   │   ║
  1481.                          ╚═══════╝       ╚═══════╧═══════╝
  1482.                          └───┘                       └───┘
  1483.                          MOST                        LEAST
  1484.                          SIGNIFICANT           SIGNIFICANT
  1485.                          DIGIT                       DIGIT
  1486.  
  1487.                             +N              +1       0
  1488.                          7       0       7      0 7      0
  1489.                     BYTE ╔╤╤╤╤╤╤╤╗       ╔╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╗
  1490.                   STRING ║   │   ║    ║   │   │   │   ║
  1491.                          ╚═══════╝       ╚═══════╧═══════╝
  1492.  
  1493.                                                -2 GIGABYTES
  1494.                  +2 GIGABYTES                           210
  1495.       BIT ╔╤╤╤╤════════════╤╤═══════  ════════════════╤╤╤╤╗
  1496.    STRING ║││││            ││                         ││││║
  1497.           ╚╧╧╧╧════════════╧╧════════  ═══════════════╧╧╧╧╝
  1498.                           BIT 0
  1499.  
  1500.                 +3      +2      +1       0
  1501.              31                              0
  1502.  NEAR 32-BIT ╔╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╗
  1503.      POINTER ║   │   │   │   │   │   │   │   ║
  1504.              ╚═══════╧═══════╧═══════╧═══════╝
  1505.              └───────────────────────────────┘
  1506.                               OFFSET
  1507.  
  1508.                +5      +4      +3      +2      +1       0
  1509.             48                                              0
  1510.  FAR 48-BIT ╔╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╗
  1511.     POINTER ║   │   │   │   │   │   │   │   │   │   │   │   ║
  1512.             ╚═══════╧═══════╧═══════╧═══════╧═══════╧═══════╝
  1513.             └───────────────┴───────────────────────────────┘
  1514.                  SELECTOR                 OFFSET
  1515.  
  1516.                    +5      +4      +3      +2      +1       0
  1517.      32-BIT ╔╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╗
  1518.   BIT FIELD ║   │   │   │   │   │   │   │   │   │   │   │   ║
  1519.             ╚═══════╧═══════╧═══════╧═══════╧═══════╧═══════╝
  1520.                     │───────── BIT FIELD ─────────│
  1521.                               1 TO 32 BITS
  1522.  
  1523.  
  1524.  2.3  Registers
  1525.  
  1526.  The 80386 contains a total of sixteen registers that are of interest to the
  1527.  applications programmer. As Figure 2-5 shows, these registers may be
  1528.  grouped into these basic categories:
  1529.  
  1530.    1.  General registers. These eight 32-bit general-purpose registers are
  1531.        used primarily to contain operands for arithmetic and logical
  1532.        operations.
  1533.  
  1534.    2.  Segment registers. These special-purpose registers permit systems
  1535.        software designers to choose either a flat or segmented model of
  1536.        memory organization. These six registers determine, at any given time,
  1537.        which segments of memory are currently addressable.
  1538.  
  1539.    3.  Status and instruction registers. These special-purpose registers are
  1540.        used to record and alter certain aspects of the 80386 processor state.
  1541.  
  1542.  
  1543.  2.3.1  General Registers
  1544.  
  1545.  The general registers of the 80386 are the 32-bit registers EAX, EBX, ECX,
  1546.  EDX, EBP, ESP, ESI, and EDI. These registers are used interchangeably to
  1547.  contain the operands of logical and arithmetic operations. They may also be
  1548.  used interchangeably for operands of address computations (except that ESP
  1549.  cannot be used as an index operand).
  1550.  
  1551.  As Figure 2-5 shows, the low-order word of each of these eight registers
  1552.  has a separate name and can be treated as a unit. This feature is useful for
  1553.  handling 16-bit data items and for compatibility with the 8086 and 80286
  1554.  processors. The word registers are named AX, BX, CX, DX, BP, SP, SI, and DI.
  1555.  
  1556.  Figure 2-5 also illustrates that each byte of the 16-bit registers AX, BX,
  1557.  CX, and DX has a separate name and can be treated as a unit. This feature is
  1558.  useful for handling characters and other 8-bit data items. The byte
  1559.  registers are named AH, BH, CH, and DH (high bytes); and AL, BL, CL, and DL
  1560.  (low bytes).
  1561.  
  1562.  All of the general-purpose registers are available for addressing
  1563.  calculations and for the results of most arithmetic and logical
  1564.  calculations; however, a few functions are dedicated to certain registers.
  1565.  By implicitly choosing registers for these functions, the 80386 architecture
  1566.  can encode instructions more compactly. The instructions that use specific
  1567.  registers include: double-precision multiply and divide, I/O, string
  1568.  instructions, translate, loop, variable shift and rotate, and stack
  1569.  operations.
  1570.  
  1571.  
  1572.  2.3.2  Segment Registers
  1573.  
  1574.  The segment registers of the 80386 give systems software designers the
  1575.  flexibility to choose among various models of memory organization.
  1576.  Implementation of memory models is the subject of Part II ── Systems
  1577.  Programming. Designers may choose a model in which applications programs do
  1578.  not need to modify segment registers, in which case applications programmers
  1579.  may skip this section.
  1580.  
  1581.  Complete programs generally consist of many different modules, each
  1582.  consisting of instructions and data. However, at any given time during
  1583.  program execution, only a small subset of a program's modules are actually
  1584.  in use. The 80386 architecture takes advantage of this by providing
  1585.  mechanisms to support direct access to the instructions and data of the
  1586.  current module's environment, with access to additional segments on demand.
  1587.  
  1588.  At any given instant, six segments of memory may be immediately accessible
  1589.  to an executing 80386 program. The segment registers CS, DS, SS, ES, FS, and
  1590.  GS are used to identify these six current segments. Each of these registers
  1591.  specifies a particular kind of segment, as characterized by the associated
  1592.  mnemonics ("code," "data," or "stack") shown in Figure 2-6. Each register
  1593.  uniquely determines one particular segment, from among the segments that
  1594.  make up the program, that is to be immediately accessible at highest speed.
  1595.  
  1596.  The segment containing the currently executing sequence of instructions is
  1597.  known as the current code segment; it is specified by means of the CS
  1598.  register. The 80386 fetches all instructions from this code segment, using
  1599.  as an offset the contents of the instruction pointer. CS is changed
  1600.  implicitly as the result of intersegment control-transfer instructions (for
  1601.  example, CALL and JMP), interrupts, and exceptions.
  1602.  
  1603.  Subroutine calls, parameters, and procedure activation records usually
  1604.  require that a region of memory be allocated for a stack. All stack
  1605.  operations use the SS register to locate the stack. Unlike CS, the SS
  1606.  register can be loaded explicitly, thereby permitting programmers to define
  1607.  stacks dynamically.
  1608.  
  1609.  The DS, ES, FS, and GS registers allow the specification of four data
  1610.  segments, each addressable by the currently executing program. Accessibility
  1611.  to four separate data areas helps programs efficiently access different
  1612.  types of data structures; for example, one data segment register can point
  1613.  to the data structures of the current module, another to the exported data
  1614.  of a higher-level module, another to a dynamically created data structure,
  1615.  and another to data shared with another task. An operand within a data
  1616.  segment is addressed by specifying its offset either directly in an
  1617.  instruction or indirectly via general registers.
  1618.  
  1619.  Depending on the structure of data (e.g., the way data is parceled into one
  1620.  or more segments), a program may require access to more than four data
  1621.  segments. To access additional segments, the DS, ES, FS, and GS registers
  1622.  can be changed under program control during the course of a program's
  1623.  execution. This simply requires that the program execute an instruction to
  1624.  load the appropriate segment register prior to executing instructions that
  1625.  access the data.
  1626.  
  1627.  The processor associates a base address with each segment selected by a
  1628.  segment register. To address an element within a segment, a 32-bit offset is
  1629.  added to the segment's base address. Once a segment is selected (by loading
  1630.  the segment selector into a segment register), a data manipulation
  1631.  instruction only needs to specify the offset. Simple rules define which
  1632.  segment register is used to form an address when only an offset is
  1633.  specified.
  1634.  
  1635.  
  1636.  Figure 2-5.  80386 Applications Register Set
  1637.  
  1638.                                GENERAL REGISTERS
  1639.  
  1640.    31                23                15                7               0
  1641.   ╔═════════════════╪═════════════════╬═════════════════╧═════════════════╗
  1642.   ║                                  EAX       AH       AX      AL        ║
  1643.   ╠═════════════════╪═════════════════╬═════════════════╩═════════════════╣
  1644.   ║                                  EDX       DH       DX      DL        ║
  1645.   ╠═════════════════╪═════════════════╬═════════════════╩═════════════════╣
  1646.   ║                                  ECX       CH       CX      CL        ║
  1647.   ╠═════════════════╪═════════════════╬═════════════════╩═════════════════╣
  1648.   ║                                  EBX       BH       BX      BL        ║
  1649.   ╠═════════════════╪═════════════════╬═════════════════╩═════════════════╣
  1650.   ║                                  EBP                BP                ║
  1651.   ╠═════════════════╪═════════════════╬═════════════════╪═════════════════╣
  1652.   ║                                  ESI                SI                ║
  1653.   ╠═════════════════╪═════════════════╬═════════════════╪═════════════════╣
  1654.   ║                                  EDI                DI                ║
  1655.   ╠═════════════════╪═════════════════╬═════════════════╪═════════════════╣
  1656.   ║                                  ESP                SP                ║
  1657.   ╚═════════════════╪═════════════════╬═════════════════╪═════════════════╝
  1658.  
  1659.  
  1660.                        15                7               0
  1661.                       ╔═════════════════╪═════════════════╗
  1662.                       ║         CS (CODE SEGMENT)         ║
  1663.                       ╟─────────────────┼─────────────────╢
  1664.                       ║         SS (STACK SEGMENT)        ║
  1665.      SEGMENT          ╟─────────────────┼─────────────────╢
  1666.      REGISTERS        ║         DS (DATA SEGMENT)         ║
  1667.                       ╟─────────────────┼─────────────────╢
  1668.                       ║         ES (DATA SEGMENT)         ║
  1669.                       ╟─────────────────┼─────────────────╢
  1670.                       ║         FS (DATA SEGMENT)         ║
  1671.                       ╟─────────────────┼─────────────────╢
  1672.                       ║         GS (DATA SEGMENT)         ║
  1673.                       ╚═════════════════╪═════════════════╝
  1674.  
  1675.  
  1676.                         STATUS AND INSTRUCTION REGISTERS
  1677.  
  1678.       31               23                15                7              0
  1679.     ╔═════════════════╪═════════════════╪═════════════════╪═════════════════╗
  1680.     ║                                EFLAGS                                 ║
  1681.     ╟───────────────────────────────────────────────────────────────────────╢
  1682.     ║                        EIP (INSTRUCTION POINTER)                      ║
  1683.     ╚═════════════════╪═════════════════╪═════════════════╪═════════════════╝
  1684.  
  1685.  
  1686.  Figure 2-6.  Use of Memory Segmentation
  1687.  
  1688.       ╔════════════════╗                                ╔════════════════╗
  1689.       ║     MODULE     ║                                ║     MODULE     ║
  1690.       ║       A        ║──┐                        ┌──║       A        ║
  1691.       ║      CODE      ║   │                        │   ║      DATA      ║
  1692.       ╚════════════════╝   │  ╔══════════════════╗  │   ╚════════════════╝
  1693.                            └──╢    CS (CODE)     ║  │
  1694.                               ╠══════════════════╣  │
  1695.       ╔════════════════╗   ┌──╢    SS (STACK)    ║  │   ╔════════════════╗
  1696.       ║                ║   │  ╠══════════════════╣  │   ║      DATA      ║
  1697.       ║     STACK      ║──┘  ║    DS (DATA)     ╟──┘┌─║   STRUCTURE    ║
  1698.       ║                ║      ╠══════════════════╣   │  ║       1        ║
  1699.       ╚════════════════╝      ║    ES (DATA)     ╟───┘  ╚════════════════╝
  1700.                               ╠══════════════════╣
  1701.                            ┌──╢    FS (DATA)     ║
  1702.       ╔════════════════╗   │  ╠══════════════════╣      ╔════════════════╗
  1703.       ║      DATA      ║   │  ║    GS (DATA)     ╟──┐   ║      DATA      ║
  1704.       ║   STRUCTURE    ║──┘  ╚══════════════════╝  └──║   STRUCTURE    ║
  1705.       ║       2        ║                                ║       3        ║
  1706.       ╚════════════════╝                                ╚════════════════╝
  1707.  
  1708.  
  1709.  2.3.3  Stack Implementation
  1710.  
  1711.  Stack operations are facilitated by three registers:
  1712.  
  1713.    1.  The stack segment (SS) register. Stacks are implemented in memory. A
  1714.        system may have a number of stacks that is limited only by the maximum
  1715.        number of segments. A stack may be up to 4 gigabytes long, the maximum
  1716.        length of a segment. One stack is directly addressable at a time──the
  1717.        one located by SS. This is the current stack, often referred to simply
  1718.        as "the" stack. SS is used automatically by the processor for all
  1719.        stack operations.
  1720.  
  1721.    2.  The stack pointer (ESP) register. ESP points to the top of the
  1722.        push-down stack (TOS). It is referenced implicitly by PUSH and POP
  1723.        operations, subroutine calls and returns, and interrupt operations.
  1724.        When an item is pushed onto the stack (see Figure 2-7), the processor
  1725.        decrements ESP, then writes the item at the new TOS. When an item is
  1726.        popped off the stack, the processor copies it from TOS, then
  1727.        increments ESP. In other words, the stack grows down in memory toward
  1728.        lesser addresses.
  1729.  
  1730.    3.  The stack-frame base pointer (EBP) register. The EBP is the best
  1731.        choice of register for accessing data structures, variables and
  1732.        dynamically allocated work space within the stack. EBP is often used
  1733.        to access elements on the stack relative to a fixed point on the stack
  1734.        rather than relative to the current TOS. It typically identifies the
  1735.        base address of the current stack frame established for the current
  1736.        procedure. When EBP is used as the base register in an offset
  1737.        calculation, the offset is calculated automatically in the current
  1738.        stack segment (i.e., the segment currently selected by SS). Because
  1739.        SS does not have to be explicitly specified, instruction encoding in
  1740.        such cases is more efficient. EBP can also be used to index into
  1741.        segments addressable via other segment registers.
  1742.  
  1743.  
  1744.  Figure 2-7.  80386 Stack
  1745.  
  1746.       31                         0
  1747.       ╔══════╪══════╪══════╪══════╗ ───────BOTTOM OF STACK
  1748.       ║                           ║       (INITIAL ESP VALUE)
  1749.       ╟══════╪══════╪══════╪══════╣
  1750.       ║                           ║
  1751.       ╠══════╪══════╪══════╪══════╣        
  1752.       ║                           ║        │POP
  1753.       ╠══════╪══════╪══════╪══════╣        │
  1754.       ║                           ║        │
  1755.       ╠══════╪══════╪══════╪══════╣        │      TOP OF     ╔═════════════╗
  1756.       ║                           ║ ──────┼─────────────────╢     ESP     ║
  1757.       ╠══════╪══════╪══════╪══════╣        │      STACK      ╚═════════════╝
  1758.       ║                           ║        │
  1759.       ║                           ║        │
  1760.       ║                           ║        │PUSH
  1761.       ║                           ║        
  1762.  
  1763.  
  1764.  2.3.4  Flags Register
  1765.  
  1766.  The flags register is a 32-bit register named EFLAGS. Figure 2-8 defines
  1767.  the bits within this register. The flags control certain operations and
  1768.  indicate the status of the 80386.
  1769.  
  1770.  The low-order 16 bits of EFLAGS is named FLAGS and can be treated as a
  1771.  unit. This feature is useful when executing 8086 and 80286 code, because
  1772.  this part of EFLAGS is identical to the FLAGS register of the 8086 and the
  1773.  80286.
  1774.  
  1775.  The flags may be considered in three groups: the status flags, the control
  1776.  flags, and the systems flags. Discussion of the systems flags is delayed
  1777.  until Part II.
  1778.  
  1779.  
  1780.  Figure 2-8.  EFLAGS Register
  1781.  
  1782.                                                16-BIT FLAGS REGISTER
  1783.                                                           A
  1784.                                       ┌───────────────────┴───────────────┐
  1785.    31                  23                  15               7            0
  1786.   ╔═══════════════════╪═══════════════╤═╤═╤═╤═╤═╪═╤═╤═╤═╤═╤═╤═╤═╤═╤═╤═╤═╤═╗
  1787.   ║                                   │V│R│ │N│ IO│O│D│I│T│S│Z│ │A│ │P│ │C║
  1788.   ║ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 │ │ │0│ │   │ │ │ │ │ │ │0│ │0│ │1│ ║
  1789.   ║                                   │M│F│ │T│ PL│F│F│F│F│F│F│ │F│ │F│ │F║
  1790.   ╚═══════════════════╪═══════════════╧╤╧╤╧═╧╤╧═╪═╧╤╧╤╧╤╧╤╧╤╧╤╧═╧╤╧═╧╤╧═╧╤╝
  1791.                                        │ │   │  │  │ │ │ │ │ │   │   │   │
  1792.         VIRTUAL 8086 MODE───X──────────┘ │   │  │  │ │ │ │ │ │   │   │   │
  1793.               RESUME FLAG───X────────────┘   │  │  │ │ │ │ │ │   │   │   │
  1794.          NESTED TASK FLAG───X────────────────┘  │  │ │ │ │ │ │   │   │   │
  1795.       I/O PRIVILEGE LEVEL───X───────────────────┘  │ │ │ │ │ │   │   │   │
  1796.                  OVERFLOW───S──────────────────────┘ │ │ │ │ │   │   │   │
  1797.            DIRECTION FLAG───C────────────────────────┘ │ │ │ │   │   │   │
  1798.          INTERRUPT ENABLE───X──────────────────────────┘ │ │ │   │   │   │
  1799.                 TRAP FLAG───S────────────────────────────┘ │ │   │   │   │
  1800.                 SIGN FLAG───S──────────────────────────────┘ │   │   │   │
  1801.                 ZERO FLAG───S────────────────────────────────┘   │   │   │
  1802.           AUXILIARY CARRY───S────────────────────────────────────┘   │   │
  1803.               PARITY FLAG───S────────────────────────────────────────┘   │
  1804.                CARRY FLAG───S────────────────────────────────────────────┘
  1805.  
  1806.            S = STATUS FLAG, C = CONTROL FLAG, X = SYSTEM FLAG
  1807.  
  1808.            NOTE: 0 OR 1 INDICATES INTEL RESERVED. DO NOT DEFINE
  1809.  
  1810.  
  1811.  2.3.4.1  Status Flags
  1812.  
  1813.  The status flags of the EFLAGS register allow the results of one
  1814.  instruction to influence later instructions. The arithmetic instructions use
  1815.  OF, SF, ZF, AF, PF, and CF. The SCAS (Scan String), CMPS (Compare String),
  1816.  and LOOP instructions use ZF to signal that their operations are complete.
  1817.  There are instructions to set, clear, and complement CF before execution of
  1818.  an arithmetic instruction. Refer to Appendix C for definition of each
  1819.  status flag.
  1820.  
  1821.  
  1822.  2.3.4.2  Control Flag
  1823.  
  1824.  The control flag DF of the EFLAGS register controls string instructions.
  1825.  
  1826.  DF (Direction Flag, bit 10)
  1827.  
  1828.    Setting DF causes string instructions to auto-decrement; that is, to
  1829.    process strings from high addresses to low addresses. Clearing DF causes
  1830.    string instructions to auto-increment, or to process strings from low
  1831.    addresses to high addresses.
  1832.  
  1833.  
  1834.  2.3.4.3  Instruction Pointer
  1835.  
  1836.  The instruction pointer register (EIP) contains the offset address,
  1837.  relative to the start of the current code segment, of the next sequential
  1838.  instruction to be executed. The instruction pointer is not directly visible
  1839.  to the programmer; it is controlled implicitly by control-transfer
  1840.  instructions, interrupts, and exceptions.
  1841.  
  1842.  As Figure 2-9 shows, the low-order 16 bits of EIP is named IP and can be
  1843.  used by the processor as a unit. This feature is useful when executing
  1844.  instructions designed for the 8086 and 80286 processors.
  1845.  
  1846.  
  1847.  Figure 2-9.  Instruction Pointer Register
  1848.  
  1849.                                                   16-BIT IP REGISTER
  1850.                                         ┌──────────────────┴────────────────┐
  1851.      31                23                15                7               0
  1852.     ╔═════════════════╪═════════════════╪═════════════════╪═════════════════╗
  1853.     ║                       EIP (INSTRUCTION POINTER)                       ║
  1854.     ╚═════════════════╪═════════════════╪═════════════════╪═════════════════╝
  1855.  
  1856.  
  1857.  2.4  Instruction Format
  1858.  
  1859.  The information encoded in an 80386 instruction includes a specification of
  1860.  the operation to be performed, the type of the operands to be manipulated,
  1861.  and the location of these operands. If an operand is located in memory, the
  1862.  instruction must also select, explicitly or implicitly, which of the
  1863.  currently addressable segments contains the operand.
  1864.  
  1865.  80386 instructions are composed of various elements and have various
  1866.  formats. The exact format of instructions is shown in Appendix B; the
  1867.  elements of instructions are described below. Of these instruction elements,
  1868.  only one, the opcode, is always present. The other elements may or may not
  1869.  be present, depending on the particular operation involved and on the
  1870.  location and type of the operands. The elements of an instruction, in order
  1871.  of occurrence are as follows:
  1872.  
  1873.    ■  Prefixes ── one or more bytes preceding an instruction that modify the
  1874.       operation of the instruction. The following types of prefixes can be
  1875.       used by applications programs:
  1876.  
  1877.       1.  Segment override ── explicitly specifies which segment register an
  1878.           instruction should use, thereby overriding the default
  1879.           segment-register selection used by the 80386 for that instruction.
  1880.  
  1881.       2.  Address size ── switches between 32-bit and 16-bit address
  1882.           generation.
  1883.  
  1884.       3.  Operand size ── switches between 32-bit and 16-bit operands.
  1885.  
  1886.       4.  Repeat ── used with a string instruction to cause the instruction
  1887.           to act on each element of the string.
  1888.  
  1889.    ■  Opcode ── specifies the operation performed by the instruction. Some
  1890.       operations have several different opcodes, each specifying a different
  1891.       variant of the operation.
  1892.  
  1893.    ■  Register specifier ── an instruction may specify one or two register
  1894.       operands. Register specifiers may occur either in the same byte as the
  1895.       opcode or in the same byte as the addressing-mode specifier.
  1896.  
  1897.    ■  Addressing-mode specifier ── when present, specifies whether an operand
  1898.       is a register or memory location; if in memory, specifies whether a
  1899.       displacement, a base register, an index register, and scaling are to be
  1900.       used.
  1901.  
  1902.    ■  SIB (scale, index, base) byte ── when the addressing-mode specifier
  1903.       indicates that an index register will be used to compute the address of
  1904.       an operand, an SIB byte is included in the instruction to encode the
  1905.       base register, the index register, and a scaling factor.
  1906.  
  1907.    ■  Displacement ── when the addressing-mode specifier indicates that a
  1908.       displacement will be used to compute the address of an operand, the
  1909.       displacement is encoded in the instruction. A displacement is a signed
  1910.       integer of 32, 16, or eight bits. The eight-bit form is used in the
  1911.       common case when the displacement is sufficiently small. The processor
  1912.       extends an eight-bit displacement to 16 or 32 bits, taking into
  1913.       account the sign.
  1914.  
  1915.    ■  Immediate operand ── when present, directly provides the value of an
  1916.       operand of the instruction. Immediate operands may be 8, 16, or 32 bits
  1917.       wide. In cases where an eight-bit immediate operand is combined in some
  1918.       way with a 16- or 32-bit operand, the processor automatically extends
  1919.       the size of the eight-bit operand, taking into account the sign.
  1920.  
  1921.  
  1922.  2.5  Operand Selection
  1923.  
  1924.  An instruction can act on zero or more operands, which are the data
  1925.  manipulated by the instruction. An example of a zero-operand instruction is
  1926.  NOP (no operation). An operand can be in any of these locations:
  1927.  
  1928.    ■  In the instruction itself (an immediate operand)
  1929.  
  1930.    ■  In a register (EAX, EBX, ECX, EDX, ESI, EDI, ESP, or EBP in the case
  1931.       of 32-bit operands; AX, BX, CX, DX, SI, DI, SP, or BP in the case of
  1932.       16-bit operands; AH, AL, BH, BL, CH, CL, DH, or DL in the case of 8-bit
  1933.       operands; the segment registers; or the EFLAGS register for flag
  1934.       operations)
  1935.  
  1936.    ■  In memory
  1937.  
  1938.    ■  At an I/O port
  1939.  
  1940.  Immediate operands and operands in registers can be accessed more rapidly
  1941.  than operands in memory since memory operands must be fetched from memory.
  1942.  Register operands are available in the CPU. Immediate operands are also
  1943.  available in the CPU, because they are prefetched as part of the
  1944.  instruction.
  1945.  
  1946.  Of the instructions that have operands, some specify operands implicitly;
  1947.  others specify operands explicitly; still others use a combination of
  1948.  implicit and explicit specification; for example:
  1949.  
  1950.  Implicit operand: AAM
  1951.  
  1952.    By definition, AAM (ASCII adjust for multiplication) operates on the
  1953.    contents of the AX register.
  1954.  
  1955.  Explicit operand: XCHG EAX, EBX
  1956.  
  1957.    The operands to be exchanged are encoded in the instruction after the
  1958.    opcode.
  1959.  
  1960.  Implicit and explicit operands: PUSH COUNTER
  1961.  
  1962.    The memory variable COUNTER (the explicit operand) is copied to the top of
  1963.    the stack (the implicit operand).
  1964.  
  1965.  Note that most instructions have implicit operands. All arithmetic
  1966.  instructions, for example, update the EFLAGS register.
  1967.  
  1968.  An 80386 instruction can explicitly reference one or two operands.
  1969.  Two-operand instructions, such as MOV, ADD, XOR, etc., generally overwrite
  1970.  one of the two participating operands with the result. A distinction can
  1971.  thus be made between the source operand (the one unaffected by the
  1972.  operation) and the destination operand (the one overwritten by the result).
  1973.  
  1974.  For most instructions, one of the two explicitly specified operands──either
  1975.  the source or the destination──can be either in a register or in memory.
  1976.  The other operand must be in a register or be an immediate source operand.
  1977.  Thus, the explicit two-operand instructions of the 80386 permit operations
  1978.  of the following kinds:
  1979.  
  1980.    ■  Register-to-register
  1981.    ■  Register-to-memory
  1982.    ■  Memory-to-register
  1983.    ■  Immediate-to-register
  1984.    ■  Immediate-to-memory
  1985.  
  1986.  Certain string instructions and stack manipulation instructions, however,
  1987.  transfer data from memory to memory. Both operands of some string
  1988.  instructions are in memory and are implicitly specified. Push and pop stack
  1989.  operations allow transfer between memory operands and the memory-based
  1990.  stack.
  1991.  
  1992.  
  1993.  2.5.1  Immediate Operands
  1994.  
  1995.  Certain instructions use data from the instruction itself as one (and
  1996.  sometimes two) of the operands. Such an operand is called an immediate
  1997.  operand. The operand may be 32-, 16-, or 8-bits long. For example:
  1998.  
  1999.  SHR PATTERN, 2
  2000.  
  2001.  One byte of the instruction holds the value 2, the number of bits by which
  2002.  to shift the variable PATTERN.
  2003.  
  2004.  TEST PATTERN, 0FFFF00FFH
  2005.  
  2006.  A doubleword of the instruction holds the mask that is used to test the
  2007.  variable PATTERN.
  2008.  
  2009.  
  2010.  2.5.2  Register Operands
  2011.  
  2012.  Operands may be located in one of the 32-bit general registers (EAX, EBX,
  2013.  ECX, EDX, ESI, EDI, ESP, or EBP), in one of the 16-bit general registers
  2014.  (AX, BX, CX, DX, SI, DI, SP, or BP), or in one of the 8-bit general
  2015.  registers (AH, BH, CH, DH, AL, BL, CL,or DL).
  2016.  
  2017.  The 80386 has instructions for referencing the segment registers (CS, DS,
  2018.  ES, SS, FS, GS). These instructions are used by applications programs only
  2019.  if systems designers have chosen a segmented memory model.
  2020.  
  2021.  The 80386 also has instructions for referring to the flag register. The
  2022.  flags may be stored on the stack and restored from the stack. Certain
  2023.  instructions change the commonly modified flags directly in the EFLAGS
  2024.  register. Other flags that are seldom modified can be modified indirectly
  2025.  via the flags image in the stack.
  2026.  
  2027.  
  2028.  2.5.3  Memory Operands
  2029.  
  2030.  Data-manipulation instructions that address operands in memory must specify
  2031.  (either directly or indirectly) the segment that contains the operand and
  2032.  the offset of the operand within the segment. However, for speed and compact
  2033.  instruction encoding, segment selectors are stored in the high speed segment
  2034.  registers. Therefore, data-manipulation instructions need to specify only
  2035.  the desired segment register and an offset in order to address a memory
  2036.  operand.
  2037.  
  2038.  An 80386 data-manipulation instruction that accesses memory uses one of the
  2039.  following methods for specifying the offset of a memory operand within its
  2040.  segment:
  2041.  
  2042.    1.  Most data-manipulation instructions that access memory contain a byte
  2043.        that explicitly specifies the addressing method for the operand. A
  2044.        byte, known as the modR/M byte, follows the opcode and specifies
  2045.        whether the operand is in a register or in memory. If the operand is
  2046.        in memory, the address is computed from a segment register and any of
  2047.        the following values: a base register, an index register, a scaling
  2048.        factor, a displacement. When an index register is used, the modR/M
  2049.        byte is also followed by another byte that identifies the index
  2050.        register and scaling factor. This addressing method is the
  2051.        mostflexible.
  2052.  
  2053.    2.  A few data-manipulation instructions implicitly use specialized
  2054.        addressing methods:
  2055.  
  2056.        ■   For a few short forms of MOV that implicitly use the EAX register,
  2057.            the offset of the operand is coded as a doubleword in the
  2058.            instruction. No base register, index register, or scaling factor
  2059.            are used.
  2060.  
  2061.        ■   String operations implicitly address memory via DS:ESI, (MOVS,
  2062.            CMPS, OUTS, LODS, SCAS) or via ES:EDI (MOVS, CMPS, INS, STOS).
  2063.  
  2064.        ■   Stack operations implicitly address operands via SS:ESP
  2065.            registers; e.g., PUSH, POP, PUSHA, PUSHAD, POPA, POPAD, PUSHF,
  2066.            PUSHFD, POPF, POPFD, CALL, RET, IRET, IRETD, exceptions, and
  2067.            interrupts.
  2068.  
  2069.  
  2070.  2.5.3.1  Segment Selection
  2071.  
  2072.  Data-manipulation instructions need not explicitly specify which segment
  2073.  register is used. For all of these instructions, specification of a segment
  2074.  register is optional. For all memory accesses, if a segment is not
  2075.  explicitly specified by the instruction, the processor automatically chooses
  2076.  a segment register according to the rules of Table 2-1. (If systems
  2077.  designers have chosen a flat model of memory organization, the segment
  2078.  registers and the rules that the processor uses in choosing them are not
  2079.  apparent to applications programs.)
  2080.  
  2081.  There is a close connection between the kind of memory reference and the
  2082.  segment in which that operand resides. As a rule, a memory reference implies
  2083.  the current data segment (i.e., the implicit segment selector is in DS).
  2084.  However, ESP and EBP are used to access items on the stack; therefore, when
  2085.  the ESP or EBP register is used as a base register, the current stack
  2086.  segment is implied (i.e., SS contains the selector).
  2087.  
  2088.  Special instruction prefix elements may be used to override the default
  2089.  segment selection. Segment-override prefixes allow an explicit segment
  2090.  selection. The 80386 has a segment-override prefix for each of the segment
  2091.  registers. Only in the following special cases is there an implied segment
  2092.  selection that a segment prefix cannot override:
  2093.  
  2094.    ■  The use of ES for destination strings in string instructions.
  2095.    ■  The use of SS in stack instructions.
  2096.    ■  The use of CS for instruction fetches.
  2097.  
  2098.  
  2099.  Table 2-1. Default Segment Register Selection Rules
  2100.  
  2101.  Memory Reference Needed  Segment     Implicit Segment Selection Rule
  2102.                           Register
  2103.                           Used
  2104.  
  2105.  Instructions             Code (CS)   Automatic with instruction prefetch
  2106.  Stack                    Stack (SS)  All stack pushes and pops. Any
  2107.                                       memory reference that uses ESP or
  2108.                                       EBP as a base register.
  2109.  Local Data               Data (DS)   All data references except when
  2110.                                       relative to stack or string
  2111.                                       destination.
  2112.  Destination Strings      Extra (ES)  Destination of string instructions.
  2113.  
  2114.  
  2115.  2.5.3.2  Effective-Address Computation
  2116.  
  2117.  The modR/M byte provides the most flexible of the addressing methods, and
  2118.  instructions that require a modR/M byte as the second byte of the
  2119.  instruction are the most common in the 80386 instruction set. For memory
  2120.  operands defined by modR/M, the offset within the desired segment is
  2121.  calculated by taking the sum of up to three components:
  2122.  
  2123.    ■  A displacement element in the instruction.
  2124.  
  2125.    ■  A base register.
  2126.  
  2127.    ■  An index register. The index register may be automatically multiplied
  2128.       by a scaling factor of 2, 4, or 8.
  2129.  
  2130.  The offset that results from adding these components is called an effective
  2131.  address. Each of these components of an effective address may have either a
  2132.  positive or negative value. If the sum of all the components exceeds 2^(32),
  2133.  the effective address is truncated to 32 bits.Figure 2-10 illustrates the
  2134.  full set of possibilities for modR/M addressing.
  2135.  
  2136.  The displacement component, because it is encoded in the instruction, is
  2137.  useful for fixed aspects of addressing; for example:
  2138.  
  2139.    ■  Location of simple scalar operands.
  2140.    ■  Beginning of a statically allocated array.
  2141.    ■  Offset of an item within a record.
  2142.  
  2143.  The base and index components have similar functions. Both utilize the same
  2144.  set of general registers. Both can be used for aspects of addressing that
  2145.  are determined dynamically; for example:
  2146.  
  2147.    ■  Location of procedure parameters and local variables in stack.
  2148.  
  2149.    ■  The beginning of one record among several occurrences of the same
  2150.       record type or in an array of records.
  2151.  
  2152.    ■  The beginning of one dimension of multiple dimension array.
  2153.  
  2154.    ■  The beginning of a dynamically allocated array.
  2155.  
  2156.  The uses of general registers as base or index components differ in the
  2157.  following respects:
  2158.  
  2159.    ■  ESP cannot be used as an index register.
  2160.  
  2161.    ■  When ESP or EBP is used as the base register, the default segment is
  2162.       the one selected by SS. In all other cases the default segment is DS.
  2163.  
  2164.  The scaling factor permits efficient indexing into an array in the common
  2165.  cases when array elements are 2, 4, or 8 bytes wide. The shifting of the
  2166.  index register is done by the processor at the time the address is evaluated
  2167.  with no performance loss. This eliminates the need for a separate shift or
  2168.  multiply instruction.
  2169.  
  2170.  The base, index, and displacement components may be used in any
  2171.  combination; any of these components may be null. A scale factor can be used
  2172.  only when an index is also used. Each possible combination is useful for
  2173.  data structures commonly used by programmers in high-level languages and
  2174.  assembly languages. Following are possible uses for some of the various
  2175.  combinations of address components.
  2176.  
  2177.  DISPLACEMENT
  2178.  
  2179.     The displacement alone indicates the offset of the operand. This
  2180.     combination is used to directly address a statically allocated scalar
  2181.     operand. An 8-bit, 16-bit, or 32-bit displacement can be used.
  2182.  
  2183.  BASE
  2184.  
  2185.     The offset of the operand is specified indirectly in one of the general
  2186.     registers, as for "based" variables.
  2187.  
  2188.  BASE + DISPLACEMENT
  2189.  
  2190.     A register and a displacement can be used together for two distinct
  2191.     purposes:
  2192.  
  2193.     1.  Index into static array when element size is not 2, 4, or 8 bytes.
  2194.         The displacement component encodes the offset of the beginning of
  2195.         the array. The register holds the results of a calculation to
  2196.         determine the offset of a specific element within the array.
  2197.  
  2198.     2.  Access item of a record. The displacement component locates an
  2199.         item within record. The base register selects one of several
  2200.         occurrences of record, thereby providing a compact encoding for
  2201.         this common function.
  2202.  
  2203.     An important special case of this combination, is to access parameters
  2204.     in the procedure activation record in the stack.  In this case, EBP is
  2205.     the best choice for the base register, because when EBP is used as a
  2206.     base register, the processor automatically uses the stack segment
  2207.     register (SS) to locate the operand, thereby providing a compact
  2208.     encoding for this common function.
  2209.  
  2210.  (INDEX * SCALE) + DISPLACEMENT
  2211.  
  2212.     This combination provides efficient indexing into a static array when
  2213.     the element size is 2, 4, or 8 bytes. The displacement addresses the
  2214.     beginning of the array, the index register holds the subscript of the
  2215.     desired array element, and the processor automatically converts the
  2216.     subscript into an index by applying the scaling factor.
  2217.  
  2218.  BASE + INDEX + DISPLACEMENT
  2219.  
  2220.     Two registers used together support either a two-dimensional array (the
  2221.     displacement determining the beginning of the array) or one of several
  2222.     instances of an array of records (the displacement indicating an item
  2223.     in the record).
  2224.  
  2225.  BASE + (INDEX * SCALE) + DISPLACEMENT
  2226.  
  2227.     This combination provides efficient indexing of a two-dimensional array
  2228.     when the elements of the array are 2, 4, or 8 bytes wide.
  2229.  
  2230.  
  2231.  Figure 2-10.  Effective Address Computation
  2232.  
  2233.        SEGMENT +    BASE   +    (INDEX * SCALE)  +     DISPLACEMENT
  2234.  
  2235.                   ┌     ┐
  2236.                   │ --- │     ┌     ┐     ┌   ┐
  2237.        ┌    ┐     │ EAX │     │ EAX │     │ 1 │
  2238.        │ CS │     │ ECX │     │ ECX │     │   │     ┌                     ┐
  2239.        │ SS │     │ EDX │     │ EDX │     │ 2 │     │     NO DISPLACEMENT │
  2240.       ─┤ DS ├─ + ─┤ EBX ├─ + ─┤ EBX ├─ * ─┤   ├─ + ─┤  8-BIT DISPLACEMENT ├─
  2241.        │ ES │     │ ESP │     │ --- │     │ 4 │     │ 32-BIT DISPLACEMENT │
  2242.        │ FS │     │ EBP │     │ EBP │     │   │     └                     ┘
  2243.        │ GS │     │ ESI │     │ ESI │     │ 6 │
  2244.        └    ┘     │ EDI │     │ EDI │     └   ┘
  2245.                   └     ┘     └     ┘
  2246.  
  2247.  
  2248.  2.6  Interrupts and Exceptions
  2249.  
  2250.  The 80386 has two mechanisms for interrupting program execution:
  2251.  
  2252.    1.  Exceptions are synchronous events that are the responses of the CPU
  2253.        to certain conditions detected during the execution of an instruction.
  2254.  
  2255.    2.  Interrupts are asynchronous events typically triggered by external
  2256.        devices needing attention.
  2257.  
  2258.  Interrupts and exceptions are alike in that both cause the processor to
  2259.  temporarily suspend its present program execution in order to execute a
  2260.  program of higher priority. The major distinction between these two kinds of
  2261.  interrupts is their origin. An exception is always reproducible by
  2262.  re-executing with the program and data that caused the exception, whereas an
  2263.  interrupt is generally independent of the currently executing program.
  2264.  
  2265.  Application programmers are not normally concerned with servicing
  2266.  interrupts. More information on interrupts for systems programmers may be
  2267.  found in Chapter 9. Certain exceptions, however, are of interest to
  2268.  applications programmers, and many operating systems give applications
  2269.  programs the opportunity to service these exceptions. However, the operating
  2270.  system itself defines the interface between the applications programs and
  2271.  the exception mechanism of the 80386.
  2272.  
  2273.  Table 2-2 highlights the exceptions that may be of interest to applications
  2274.  programmers.
  2275.  
  2276.    ■  A divide error exception results when the instruction DIV or IDIV is
  2277.       executed with a zero denominator or when the quotient is too large for
  2278.       the destination operand. (Refer to Chapter 3 for a discussion of DIV
  2279.       and IDIV.)
  2280.  
  2281.    ■  The debug exception may be reflected back to an applications program
  2282.       if it results from the trap flag (TF).
  2283.  
  2284.    ■  A breakpoint exception results when the instruction INT 3 is executed.
  2285.       This instruction is used by some debuggers to stop program execution at
  2286.       specific points.
  2287.  
  2288.    ■  An overflow exception results when the INTO instruction is executed
  2289.       and the OF (overflow) flag is set (after an arithmetic operation that
  2290.       set the OF flag). (Refer to Chapter 3 for a discussion of INTO).
  2291.  
  2292.    ■  A bounds check exception results when the BOUND instruction is
  2293.       executed and the array index it checks falls outside the bounds of the
  2294.       array. (Refer to Chapter 3 for a discussion of the BOUND instruction.)
  2295.  
  2296.    ■  Invalid opcodes may be used by some applications to extend the
  2297.       instruction set. In such a case, the invalid opcode exception presents
  2298.       an opportunity to emulate the opcode.
  2299.  
  2300.    ■  The "coprocessor not available" exception occurs if the program
  2301.       contains instructions for a coprocessor, but no coprocessor is present
  2302.       in the system.
  2303.  
  2304.    ■  A coprocessor error is generated when a coprocessor detects an illegal
  2305.       operation.
  2306.  
  2307.  The instruction INT generates an interrupt whenever it is executed; the
  2308.  processor treats this interrupt as an exception. The effects of this
  2309.  interrupt (and the effects of all other exceptions) are determined by
  2310.  exception handler routines provided by the application program or as part of
  2311.  the systems software (provided by systems programmers). The INT instruction
  2312.  itself is discussed in Chapter 3. Refer to Chapter 9 for a more complete
  2313.  description of exceptions.
  2314.  
  2315.  
  2316.  Table 2-2. 80386 Reserved Exceptions and Interrupts
  2317.  
  2318.  Vector Number      Description
  2319.  
  2320.  0                  Divide Error
  2321.  1                  Debug Exceptions
  2322.  2                  NMI Interrupt
  2323.  3                  Breakpoint
  2324.  4                  INTO Detected Overflow
  2325.  5                  BOUND Range Exceeded
  2326.  6                  Invalid Opcode
  2327.  7                  Coprocessor Not Available
  2328.  8                  Double Exception
  2329.  9                  Coprocessor Segment Overrun
  2330.  10                 Invalid Task State Segment
  2331.  11                 Segment Not Present
  2332.  12                 Stack Fault
  2333.  13                 General Protection
  2334.  14                 Page Fault
  2335.  15                 (reserved)
  2336.  16                 Coprocessor Error
  2337.  17-32              (reserved)
  2338.  
  2339.  
  2340.  Chapter 3  Applications Instruction Set
  2341.  
  2342.  ────────────────────────────────────────────────────────────────────────────
  2343.  
  2344.  This chapter presents an overview of the instructions which programmers can
  2345.  use to write application software for the 80386 executing in protected
  2346.  virtual-address mode. The instructions are grouped by categories of related
  2347.  functions.
  2348.  
  2349.  The instructions not discussed in this chapter are those that are normally
  2350.  used only by operating-system programmers. Part II describes the operation
  2351.  of these instructions.
  2352.  
  2353.  The descriptions in this chapter assume that the 80386 is operating in
  2354.  protected mode with 32-bit addressing in effect; however, all instructions
  2355.  discussed are also available when 16-bit addressing is in effect in
  2356.  protected mode, real mode, or virtual 8086 mode. For any differences of
  2357.  operation that exist in the various modes, refer to Chapter 13,
  2358.  Chapter 14, or Chapter 15.
  2359.  
  2360.  The instruction dictionary in Chapter 17 contains more detailed
  2361.  descriptions of all instructions, including encoding, operation, timing,
  2362.  effect on flags, and exceptions.
  2363.  
  2364.  
  2365.  3.1  Data Movement Instructions
  2366.  
  2367.  These instructions provide convenient methods for moving bytes, words, or
  2368.  doublewords of data between memory and the registers of the base
  2369.  architecture. They fall into the following classes:
  2370.  
  2371.    1.  General-purpose data movement instructions.
  2372.    2.  Stack manipulation instructions.
  2373.    3.  Type-conversion instructions.
  2374.  
  2375.  
  2376.  3.1.1  General-Purpose Data Movement Instructions
  2377.  
  2378.  MOV (Move) transfers a byte, word, or doubleword from the source operand to
  2379.  the destination operand. The MOV instruction is useful for transferring data
  2380.  along any of these paths:
  2381.  
  2382.    ■  To a register from memory
  2383.    ■  To memory from a register
  2384.    ■  Between general registers
  2385.    ■  Immediate data to a register
  2386.    ■  Immediate data to a memory
  2387.  
  2388.  The MOV instruction cannot move from memory to memory or from segment
  2389.  register to segment register are not allowed. Memory-to-memory moves can be
  2390.  performed, however, by the string move instruction MOVS.
  2391.  
  2392.  XCHG (Exchange) swaps the contents of two operands. This instruction takes
  2393.  the place of three MOV instructions. It does not require a temporary
  2394.  location to save the contents of one operand while load the other is being
  2395.  loaded. XCHG is especially useful for implementing semaphores or similar
  2396.  data structures for process synchronization.
  2397.  
  2398.  The XCHG instruction can swap two byte operands, two word operands, or two
  2399.  doubleword operands. The operands for the XCHG instruction may be two
  2400.  register operands, or a register operand with a memory operand. When used
  2401.  with a memory operand, XCHG automatically activates the LOCK signal. (Refer
  2402.  to Chapter 11 for more information on the bus lock.)
  2403.  
  2404.  
  2405.  3.1.2  Stack Manipulation Instructions
  2406.  
  2407.  PUSH (Push) decrements the stack pointer (ESP), then transfers the source
  2408.  operand to the top of stack indicated by ESP (see Figure 3-1). PUSH is
  2409.  often used to place parameters on the stack before calling a procedure; it
  2410.  is also the basic means of storing temporary variables on the stack. The
  2411.  PUSH instruction operates on memory operands, immediate operands, and
  2412.  register operands (including segment registers).
  2413.  
  2414.  PUSHA (Push All Registers) saves the contents of the eight general
  2415.  registers on the stack (see Figure 3-2). This instruction simplifies
  2416.  procedure calls by reducing the number of instructions required to retain
  2417.  the contents of the general registers for use in a procedure. The processor
  2418.  pushes the general registers on the stack in the following order: EAX, ECX,
  2419.  EDX, EBX, the initial value of ESP before EAX was pushed, EBP, ESI, and
  2420.  EDI. PUSHA is complemented by the POPA instruction.
  2421.  
  2422.  POP (Pop) transfers the word or doubleword at the current top of stack
  2423.  (indicated by ESP) to the destination operand, and then increments ESP to
  2424.  point to the new top of stack. See Figure 3-3. POP moves information from
  2425.  the stack to a general register, or to memory.
  2426.  
  2427.  POPA (Pop All Registers) restores the registers saved on the stack by
  2428.  PUSHA, except that it ignores the saved value of ESP. See Figure 3-4.
  2429.  
  2430.  
  2431.  Figure 3-1.  PUSH
  2432.  
  2433.         D  O        BEFORE PUSH                  AFTER PUSH
  2434.         I  F      31          0             31          0 
  2435.         R        ║               ║           ║               ║
  2436.         E  E     ╠═══════╪═══════╣           ╠═══════╪═══════╣
  2437.         C  X     ║▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒║           ║▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒║
  2438.         T  P     ╠═══════╪═══════╣           ╠═══════╪═══════╣
  2439.         I  A     ║▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒║           ║▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒║
  2440.         O  N     ╠═══════╪═══════╣──ESP     ╠═══════╪═══════╣
  2441.         N  S     ║               ║           ║    OPERAND    ║
  2442.            I     ╠═══════╪═══════╣           ╠═══════╪═══════╣──ESP
  2443.          │ O     ║               ║           ║               ║
  2444.          │ N     ╠═══════╪═══════╣           ╠═══════╪═══════╣
  2445.          │       ║               ║           ║               ║
  2446.                 ╠═══════╪═══════╣           ╠═══════╪═══════╣
  2447.                  ║               ║           ║               ║
  2448.                                                           
  2449.  
  2450.  
  2451.  Figure 3-2.  PUSHA
  2452.  
  2453.                     BEFORE PUSHA                AFTER PUSHA
  2454.                   31          0             31          0 
  2455.           D  O   ║               ║           ║               ║
  2456.           I  F   ╠═══════╪═══════╣           ╠═══════╪═══════╣
  2457.           R      ║▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒║           ║▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒║
  2458.           E  E   ╠═══════╪═══════╣           ╠═══════╪═══════╣
  2459.           C  X   ║▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒║           ║▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒║
  2460.           T  P   ╠═══════╪═══════╣──ESP     ╠═══════╪═══════╣
  2461.           I  A   ║               ║           ║      EAX      ║
  2462.           O  N   ╠═══════╪═══════╣           ╠═══════╪═══════╣
  2463.           N  S   ║               ║           ║      ECX      ║
  2464.              I   ╠═══════╪═══════╣           ╠═══════╪═══════╣
  2465.            │ O   ║               ║           ║      EDX      ║
  2466.            │ N   ╠═══════╪═══════╣           ╠═══════╪═══════╣
  2467.            │     ║               ║           ║      EBX      ║
  2468.                 ╠═══════╪═══════╣           ╠═══════╪═══════╣
  2469.                  ║               ║           ║    OLD ESP    ║
  2470.                  ╠═══════╪═══════╣           ╠═══════╪═══════╣
  2471.                  ║               ║           ║      EBP      ║
  2472.                  ╠═══════╪═══════╣           ╠═══════╪═══════╣
  2473.                  ║               ║           ║      ESI      ║
  2474.                  ╠═══════╪═══════╣           ╠═══════╪═══════╣
  2475.                  ║               ║           ║      EDI      ║
  2476.                  ╠═══════╪═══════╣           ╠═══════╪═══════╣──ESP
  2477.                  ║               ║           ║               ║
  2478.                  ╠═══════╪═══════╣           ╠═══════╪═══════╣
  2479.                  ║               ║           ║               ║
  2480.                                                           
  2481.  
  2482.  
  2483.  3.1.3  Type Conversion Instructions
  2484.  
  2485.  The type conversion instructions convert bytes into words, words into
  2486.  doublewords, and doublewords into 64-bit items (quad-words). These
  2487.  instructions are especially useful for converting signed integers, because
  2488.  they automatically fill the extra bits of the larger item with the value of
  2489.  the sign bit of the smaller item. This kind of conversion, illustrated by
  2490.  Figure 3-5, is called sign extension.
  2491.  
  2492.  There are two classes of type conversion instructions:
  2493.  
  2494.    1.  The forms CWD, CDQ, CBW, and CWDE which operate only on data in the
  2495.        EAX register.
  2496.  
  2497.    2.  The forms MOVSX and MOVZX, which permit one operand to be in any
  2498.        general register while permitting the other operand to be in memory or
  2499.        in a register.
  2500.  
  2501.  CWD (Convert Word to Doubleword) and CDQ (Convert Doubleword to Quad-Word)
  2502.  double the size of the source operand. CWD extends the sign of the
  2503.  word in register AX throughout register DX. CDQ extends the sign of the
  2504.  doubleword in EAX throughout EDX. CWD can be used to produce a doubleword
  2505.  dividend from a word before a word division, and CDQ can be used to produce
  2506.  a quad-word dividend from a doubleword before doubleword division.
  2507.  
  2508.  CBW (Convert Byte to Word) extends the sign of the byte in register AL
  2509.  throughout AX.
  2510.  
  2511.  CWDE (Convert Word to Doubleword Extended) extends the sign of the word in
  2512.  register AX throughout EAX.
  2513.  
  2514.  MOVSX (Move with Sign Extension) sign-extends an 8-bit value to a 16-bit
  2515.  value and a 8- or 16-bit value to 32-bit value.
  2516.  
  2517.  MOVZX (Move with Zero Extension) extends an 8-bit value to a 16-bit value
  2518.  and an 8- or 16-bit value to 32-bit value by inserting high-order zeros.
  2519.  
  2520.  
  2521.  Figure 3-3.  POP
  2522.  
  2523.         D  O         BEFORE POP                  AFTER POP
  2524.         I  F      31          0             31          0 
  2525.         R        ║               ║           ║               ║
  2526.         E  E     ╠═══════╪═══════╣           ╠═══════╪═══════╣
  2527.         C  X     ║▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒║           ║▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒║
  2528.         T  P     ╠═══════╪═══════╣           ╠═══════╪═══════╣
  2529.         I  A     ║▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒║           ║▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒║
  2530.         O  N     ╠═══════╪═══════╣           ╠═══════╪═══════╣──ESP
  2531.         N  S     ║    OPERAND    ║           ║               ║
  2532.            I     ╠═══════╪═══════╣──ESP     ╠═══════╪═══════╣
  2533.          │ O     ║               ║           ║               ║
  2534.          │ N     ╠═══════╪═══════╣           ╠═══════╪═══════╣
  2535.          │       ║               ║           ║               ║
  2536.                 ╠═══════╪═══════╣           ╠═══════╪═══════╣
  2537.                  ║               ║           ║               ║
  2538.                                                           
  2539.  
  2540.  
  2541.  Figure 3-4.  POPA
  2542.  
  2543.                     BEFORE POPA                  AFTER POPA
  2544.                   31          0             31          0 
  2545.           D  O   ║               ║           ║               ║
  2546.           I  F   ╠═══════╪═══════╣           ╠═══════╪═══════╣
  2547.           R      ║▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒║           ║▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒║
  2548.           E  E   ╠═══════╪═══════╣           ╠═══════╪═══════╣
  2549.           C  X   ║▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒║           ║▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒║
  2550.           T  P   ╠═══════╪═══════╣           ╠═══════╪═══════╣──ESP
  2551.           I  A   ║      EAX      ║           ║               ║
  2552.           O  N   ╠═══════╪═══════╣           ╠═══════╪═══════╣
  2553.           N  S   ║      ECX      ║           ║               ║
  2554.              I   ╠═══════╪═══════╣           ╠═══════╪═══════╣
  2555.            │ O   ║      EDX      ║           ║               ║
  2556.            │ N   ╠═══════╪═══════╣           ╠═══════╪═══════╣
  2557.            │     ║      EBX      ║           ║               ║
  2558.                 ╠═══════╪═══════╣           ╠═══════╪═══════╣
  2559.                  ║      ESP      ║           ║               ║
  2560.                  ╠═══════╪═══════╣           ╠═══════╪═══════╣
  2561.                  ║      EPB      ║           ║               ║
  2562.                  ╠═══════╪═══════╣           ╠═══════╪═══════╣
  2563.                  ║      ESI      ║           ║               ║
  2564.                  ╠═══════╪═══════╣           ╠═══════╪═══════╣
  2565.                  ║      EDI      ║           ║               ║
  2566.                  ╠═══════╪═══════╣──ESP     ╠═══════╪═══════╣
  2567.                  ║               ║           ║               ║
  2568.                  ╠═══════╪═══════╣           ╠═══════╪═══════╣
  2569.                  ║               ║           ║               ║
  2570.                                                           
  2571.  
  2572.  
  2573.  Figure 3-5.  Sign Extension
  2574.  
  2575.                                      15                7               0
  2576.                                      ╔═╦══════════════╪════════════════╗
  2577.       BEFORE SIGN EXTENSION─────────║S║ N N N N N N N N N N N N N N N ║
  2578.                                      ╚═╩══════════════╪════════════════╝
  2579.       AFTER SIGN EXTENSION──────┐
  2580.                                 │
  2581.      31               23             15              7              0
  2582.      ╔═╦═════════════╪═══════════════╪═══════════════╪═══════════════╗
  2583.      ║S║S S S S S S S S S S S S S S S S N N N N N N N N N N N N N N N║
  2584.      ╚═╩═════════════╪═══════════════╪═══════════════╪═══════════════╝
  2585.  
  2586.  
  2587.  3.2  Binary Arithmetic Instructions
  2588.  
  2589.  The arithmetic instructions of the 80386 processor simplify the
  2590.  manipulation of numeric data that is encoded in binary. Operations include
  2591.  the standard add, subtract, multiply, and divide as well as increment,
  2592.  decrement, compare, and change sign. Both signed and unsigned binary
  2593.  integers are supported. The binary arithmetic instructions may also be used
  2594.  as one step in the process of performing arithmetic on decimal integers.
  2595.  
  2596.  Many of the arithmetic instructions operate on both signed and unsigned
  2597.  integers. These instructions update the flags ZF, CF, SF, and OF in such a
  2598.  manner that subsequent instructions can interpret the results of the
  2599.  arithmetic as either signed or unsigned. CF contains information relevant to
  2600.  unsigned integers; SF and OF contain information relevant to signed
  2601.  integers. ZF is relevant to both signed and unsigned integers; ZF is set
  2602.  when all bits of the result are zero.
  2603.  
  2604.  If the integer is unsigned, CF may be tested after one of these arithmetic
  2605.  operations to determine whether the operation required a carry or borrow of
  2606.  a one-bit in the high-order position of the destination operand. CF is set
  2607.  if a one-bit was carried out of the high-order position (addition
  2608.  instructions ADD, ADC, AAA, and DAA) or if a one-bit was carried (i.e.
  2609.  borrowed) into the high-order bit (subtraction instructions SUB, SBB, AAS,
  2610.  DAS, CMP, and NEG).
  2611.  
  2612.  If the integer is signed, both SF and OF should be tested. SF always has
  2613.  the same value as the sign bit of the result. The most significant bit (MSB)
  2614.  of a signed integer is the bit next to the sign──bit 6 of a byte, bit 14 of
  2615.  a word, or bit 30 of a doubleword. OF is set in either of these cases:
  2616.  
  2617.    ■  A one-bit was carried out of the MSB into the sign bit but no one bit
  2618.       was carried out of the sign bit (addition instructions ADD, ADC, INC,
  2619.       AAA, and DAA). In other words, the result was greater than the greatest
  2620.       positive number that could be contained in the destination operand.
  2621.  
  2622.    ■  A one-bit was carried from the sign bit into the MSB but no one bit
  2623.       was carried into the sign bit (subtraction instructions SUB, SBB, DEC,
  2624.       AAS, DAS, CMP, and NEG). In other words, the result was smaller that
  2625.       the smallest negative number that could be contained in the destination
  2626.       operand.
  2627.  
  2628.  These status flags are tested by executing one of the two families of
  2629.  conditional instructions: Jcc (jump on condition cc) or SETcc (byte set on
  2630.  condition).
  2631.  
  2632.  
  2633.  3.2.1  Addition and Subtraction Instructions
  2634.  
  2635.  ADD (Add Integers) replaces the destination operand with the sum of the
  2636.  source and destination operands. Sets CF if overflow.
  2637.  
  2638.  ADC (Add Integers with Carry) sums the operands, adds one if CF is set, and
  2639.  replaces the destination operand with the result. If CF is cleared, ADC
  2640.  performs the same operation as the ADD instruction. An ADD followed by
  2641.  multiple ADC instructions can be used to add numbers longer than 32 bits.
  2642.  
  2643.  INC (Increment) adds one to the destination operand. INC does not affect
  2644.  CF. Use ADD with an immediate value of 1 if an increment that updates carry
  2645.  (CF) is needed.
  2646.  
  2647.  SUB (Subtract Integers) subtracts the source operand from the destination
  2648.  operand and replaces the destination operand with the result. If a borrow is
  2649.  required, the CF is set. The operands may be signed or unsigned bytes,
  2650.  words, or doublewords.
  2651.  
  2652.  SBB (Subtract Integers with Borrow) subtracts the source operand from the
  2653.  destination operand, subtracts 1 if CF is set, and returns the result to the
  2654.  destination operand. If CF is cleared, SBB performs the same operation as
  2655.  SUB. SUB followed by multiple SBB instructions may be used to subtract
  2656.  numbers longer than 32 bits. If CF is cleared, SBB performs the same
  2657.  operation as SUB.
  2658.  
  2659.  DEC (Decrement) subtracts 1 from the destination operand. DEC does not
  2660.  update CF. Use SUB with an immediate value of 1 to perform a decrement that
  2661.  affects carry.
  2662.  
  2663.  
  2664.  3.2.2  Comparison and Sign Change Instruction
  2665.  
  2666.  CMP (Compare) subtracts the source operand from the destination operand. It
  2667.  updates OF, SF, ZF, AF, PF, and CF but does not alter the source and
  2668.  destination operands. A subsequent Jcc or SETcc instruction can test the
  2669.  appropriate flags.
  2670.  
  2671.  NEG (Negate) subtracts a signed integer operand from zero. The effect of
  2672.  NEG is to reverse the sign of the operand from positive to negative or from
  2673.  negative to positive.
  2674.  
  2675.  
  2676.  3.2.3  Multiplication Instructions
  2677.  
  2678.  The 80386 has separate multiply instructions for unsigned and signed
  2679.  operands. MUL operates on unsigned numbers, while IMUL operates on signed
  2680.  integers as well as unsigned.
  2681.  
  2682.  MUL (Unsigned Integer Multiply) performs an unsigned multiplication of the
  2683.  source operand and the accumulator. If the source is a byte, the processor
  2684.  multiplies it by the contents of AL and returns the double-length result to
  2685.  AH and AL. If the source operand is a word, the processor multiplies it by
  2686.  the contents of AX and returns the double-length result to DX and AX. If the
  2687.  source operand is a doubleword, the processor multiplies it by the contents
  2688.  of EAX and returns the 64-bit result in EDX and EAX. MUL sets CF and OF
  2689.  when the upper half of the result is nonzero; otherwise, they are cleared.
  2690.  
  2691.  IMUL (Signed Integer Multiply) performs a signed multiplication operation.
  2692.  IMUL has three variations:
  2693.  
  2694.    1.  A one-operand form. The operand may be a byte, word, or doubleword
  2695.        located in memory or in a general register. This instruction uses EAX
  2696.        and EDX as implicit operands in the same way as the MUL instruction.
  2697.  
  2698.    2.  A two-operand form. One of the source operands may be in any general
  2699.        register while the other may be either in memory or in a general
  2700.        register. The product replaces the general-register operand.
  2701.  
  2702.    3.  A three-operand form; two are source and one is the destination
  2703.        operand. One of the source operands is an immediate value stored in
  2704.        the instruction; the second may be in memory or in any general
  2705.        register. The product may be stored in any general register. The
  2706.        immediate operand is treated as signed. If the immediate operand is a
  2707.        byte, the processor automatically sign-extends it to the size of the
  2708.        second operand before performing the multiplication.
  2709.  
  2710.  The three forms are similar in most respects:
  2711.  
  2712.    ■  The length of the product is calculated to twice the length of the
  2713.       operands.
  2714.  
  2715.    ■  The CF and OF flags are set when significant bits are carried into the
  2716.       high-order half of the result. CF and OF are cleared when the
  2717.       high-order half of the result is the sign-extension of the low-order
  2718.       half.
  2719.  
  2720.  However, forms 2 and 3 differ in that the product is truncated to the
  2721.  length of the operands before it is stored in the destination register.
  2722.  Because of this truncation, OF should be tested to ensure that no
  2723.  significant bits are lost. (For ways to test OF, refer to the INTO and PUSHF
  2724.  instructions.)
  2725.  
  2726.  Forms 2 and 3 of IMUL may also be used with unsigned operands because,
  2727.  whether the operands are signed or unsigned, the low-order half of the
  2728.  product is the same.
  2729.  
  2730.  
  2731.  3.2.4  Division Instructions
  2732.  
  2733.  The 80386 has separate division instructions for unsigned and signed
  2734.  operands. DIV operates on unsigned numbers, while IDIV operates on signed
  2735.  integers as well as unsigned. In either case, an exception (interrupt zero)
  2736.  occurs if the divisor is zero or if the quotient is too large for AL, AX, or
  2737.  EAX.
  2738.  
  2739.  DIV (Unsigned Integer Divide) performs an unsigned division of the
  2740.  accumulator by the source operand. The dividend (the accumulator) is twice
  2741.  the size of the divisor (the source operand); the quotient and remainder
  2742.  have the same size as the divisor, as the following table shows.
  2743.  
  2744.  Size of Source Operand
  2745.        (divisor)             Dividend       Quotient      Remainder
  2746.  
  2747.  Byte                        AX             AL            AH
  2748.  Word                        DX:AX          AX            DX
  2749.  Doubleword                  EDX:EAX        EAX           EDX
  2750.  
  2751.  Non-integral quotients are truncated to integers toward 0. The remainder is
  2752.  always less than the divisor. For unsigned byte division, the largest
  2753.  quotient is 255. For unsigned word division, the largest quotient is 65,535.
  2754.  For unsigned doubleword division the largest quotient is 2^(32) -1.
  2755.  
  2756.  IDIV (Signed Integer Divide) performs a signed division of the accumulator
  2757.  by the source operand. IDIV uses the same registers as the DIV instruction.
  2758.  
  2759.  For signed byte division, the maximum positive quotient is +127, and the
  2760.  minimum negative quotient is -128. For signed word division, the maximum
  2761.  positive quotient is +32,767, and the minimum negative quotient is -32,768.
  2762.  For signed doubleword division the maximum positive quotient is 2^(31) -1,
  2763.  the minimum negative quotient is -2^(31). Non-integral results are truncated
  2764.  towards 0. The remainder always has the same sign as the dividend and is
  2765.  less than the divisor in magnitude.
  2766.  
  2767.  
  2768.  3.3  Decimal Arithmetic Instructions
  2769.  
  2770.  Decimal arithmetic is performed by combining the binary arithmetic
  2771.  instructions (already discussed in the prior section) with the decimal
  2772.  arithmetic instructions. The decimal arithmetic instructions are used in one
  2773.  of the following ways:
  2774.  
  2775.    ■  To adjust the results of a previous binary arithmetic operation to
  2776.       produce a valid packed or unpacked decimal result.
  2777.  
  2778.    ■  To adjust the inputs to a subsequent binary arithmetic operation so
  2779.       that the operation will produce a valid packed or unpacked decimal
  2780.       result.
  2781.  
  2782.  These instructions operate only on the AL or AH registers. Most utilize the
  2783.  AF flag.
  2784.  
  2785.  
  2786.  3.3.1  Packed BCD Adjustment Instructions
  2787.  
  2788.  DAA (Decimal Adjust after Addition) adjusts the result of adding two valid
  2789.  packed decimal operands in AL. DAA must always follow the addition of two
  2790.  pairs of packed decimal numbers (one digit in each half-byte) to obtain a
  2791.  pair of valid packed decimal digits as results. The carry flag is set if
  2792.  carry was needed.
  2793.  
  2794.  DAS (Decimal Adjust after Subtraction) adjusts the result of subtracting
  2795.  two valid packed decimal operands in AL. DAS must always follow the
  2796.  subtraction of one pair of packed decimal numbers (one digit in each half-
  2797.  byte) from another to obtain a pair of valid packed decimal digits as
  2798.  results. The carry flag is set if a borrow was needed.
  2799.  
  2800.  
  2801.  3.3.2  Unpacked BCD Adjustment Instructions
  2802.  
  2803.  AAA (ASCII Adjust after Addition) changes the contents of register AL to a
  2804.  valid unpacked decimal number, and zeros the top 4 bits. AAA must always
  2805.  follow the addition of two unpacked decimal operands in AL. The carry flag
  2806.  is set and AH is incremented if a carry is necessary.
  2807.  
  2808.  AAS (ASCII Adjust after Subtraction) changes the contents of register AL to
  2809.  a valid unpacked decimal number, and zeros the top 4 bits. AAS must always
  2810.  follow the subtraction of one unpacked decimal operand from another in AL.
  2811.  The carry flag is set and AH decremented if a borrow is necessary.
  2812.  
  2813.  AAM (ASCII Adjust after Multiplication) corrects the result of a
  2814.  multiplication of two valid unpacked decimal numbers. AAM must always follow
  2815.  the multiplication of two decimal numbers to produce a valid decimal result.
  2816.  The high order digit is left in AH, the low order digit in AL.
  2817.  
  2818.  AAD (ASCII Adjust before Division) modifies the numerator in AH and AL to
  2819.  prepare for the division of two valid unpacked decimal operands so that the
  2820.  quotient produced by the division will be a valid unpacked decimal number.
  2821.  AH should contain the high-order digit and AL the low-order digit. This
  2822.  instruction adjusts the value and places the result in AL. AH will contain
  2823.  zero.
  2824.  
  2825.  
  2826.  3.4  Logical Instructions
  2827.  
  2828.  The group of logical instructions includes:
  2829.  
  2830.    ■  The Boolean operation instructions
  2831.    ■  Bit test and modify instructions
  2832.    ■  Bit scan instructions
  2833.    ■  Rotate and shift instructions
  2834.    ■  Byte set on condition
  2835.  
  2836.  
  2837.  3.4.1  Boolean Operation Instructions
  2838.  
  2839.  The logical operations are AND, OR, XOR, and NOT.
  2840.  
  2841.  NOT (Not) inverts the bits in the specified operand to form a one's
  2842.  complement of the operand. The NOT instruction is a unary operation that
  2843.  uses a single operand in a register or memory. NOT has no effect on the
  2844.  flags.
  2845.  
  2846.  The AND, OR, and XOR instructions perform the standard logical operations
  2847.  "and", "(inclusive) or", and "exclusive or". These instructions can use the
  2848.  following combinations of operands:
  2849.  
  2850.    ■  Two register operands
  2851.  
  2852.    ■  A general register operand with a memory operand
  2853.  
  2854.    ■  An immediate operand with either a general register operand or a
  2855.       memory operand.
  2856.  
  2857.  AND, OR, and XOR clear OF and CF, leave AF undefined, and update SF, ZF,
  2858.  and PF.
  2859.  
  2860.  
  2861.  3.4.2  Bit Test and Modify Instructions
  2862.  
  2863.  This group of instructions operates on a single bit which can be in memory
  2864.  or in a general register. The location of the bit is specified as an offset
  2865.  from the low-order end of the operand. The value of the offset either may be
  2866.  given by an immediate byte in the instruction or may be contained in a
  2867.  general register.
  2868.  
  2869.  These instructions first assign the value of the selected bit to CF, the
  2870.  carry flag. Then a new value is assigned to the selected bit, as determined
  2871.  by the operation. OF, SF, ZF, AF, PF are left in an undefined state. Table
  2872.  3-1 defines these instructions.
  2873.  
  2874.  
  2875.  Table 3-1. Bit Test and Modify Instructions
  2876.  
  2877.  Instruction                      Effect on CF            Effect on
  2878.                                                           Selected Bit
  2879.  
  2880.  Bit (Bit Test)                   CF  BIT                (none)
  2881.  BTS (Bit Test and Set)           CF  BIT                BIT  1
  2882.  BTR (Bit Test and Reset)         CF  BIT                BIT  0
  2883.  BTC (Bit Test and Complement)    CF  BIT                BIT  NOT(BIT)
  2884.  
  2885.  
  2886.  3.4.3  Bit Scan Instructions
  2887.  
  2888.  These instructions scan a word or doubleword for a one-bit and store the
  2889.  index of the first set bit into a register.  The bit string being scanned
  2890.  may be either in a register or in memory. The ZF flag is set if the entire
  2891.  word is zero (no set bits are found); ZF is cleared if a one-bit is found.
  2892.  If no set bit is found, the value of the destination register is undefined.
  2893.  
  2894.  BSF (Bit Scan Forward) scans from low-order to high-order (starting from
  2895.  bit index zero).
  2896.  
  2897.  BSR (Bit Scan Reverse) scans from high-order to low-order (starting from
  2898.  bit index 15 of a word or index 31 of a doubleword).
  2899.  
  2900.  
  2901.  3.4.4  Shift and Rotate Instructions
  2902.  
  2903.  The shift and rotate instructions reposition the bits within the specified
  2904.  operand.
  2905.  
  2906.  These instructions fall into the following classes:
  2907.  
  2908.    ■  Shift instructions
  2909.    ■  Double shift instructions
  2910.    ■  Rotate instructions
  2911.  
  2912.  
  2913.  3.4.4.1  Shift Instructions
  2914.  
  2915.  The bits in bytes, words, and doublewords may be shifted arithmetically or
  2916.  logically. Depending on the value of a specified count, bits can be shifted
  2917.  up to 31 places.
  2918.  
  2919.  A shift instruction can specify the count in one of three ways. One form of
  2920.  shift instruction implicitly specifies the count as a single shift. The
  2921.  second form specifies the count as an immediate value. The third form
  2922.  specifies the count as the value contained in CL. This last form allows the
  2923.  shift count to be a variable that the program supplies during execution.
  2924.  Only the low order 5 bits of CL are used.
  2925.  
  2926.  CF always contains the value of the last bit shifted out of the destination
  2927.  operand. In a single-bit shift, OF is set if the value of the high-order
  2928.  (sign) bit was changed by the operation. Otherwise, OF is cleared. Following
  2929.  a multibit shift, however, the content of OF is always undefined.
  2930.  
  2931.  The shift instructions provide a convenient way to accomplish division or
  2932.  multiplication by binary power. Note however that division of signed numbers
  2933.  by shifting right is not the same kind of division performed by the IDIV
  2934.  instruction.
  2935.  
  2936.  SAL (Shift Arithmetic Left) shifts the destination byte, word, or
  2937.  doubleword operand left by one or by the number of bits specified in the
  2938.  count operand (an immediate value or the value contained in CL). The
  2939.  processor shifts zeros in from the right (low-order) side of the operand as
  2940.  bits exit from the left (high-order) side. See Figure 3-6.
  2941.  
  2942.  SHL (Shift Logical Left) is a synonym for SAL (refer to SAL).
  2943.  
  2944.  SHR (Shift Logical Right) shifts the destination byte, word, or doubleword
  2945.  operand right by one or by the number of bits specified in the count operand
  2946.  (an immediate value or the value contained in CL). The processor shifts
  2947.  zeros in from the left side of the operand as bits exit from the right side.
  2948.  See Figure 3-7.
  2949.  
  2950.  SAR (Shift Arithmetic Right) shifts the destination byte, word, or
  2951.  doubleword operand to the right by one or by the number of bits specified in
  2952.  the count operand (an immediate value or the value contained in CL). The
  2953.  processor preserves the sign of the operand by shifting in zeros on the left
  2954.  (high-order) side if the value is positive or by shifting by ones if the
  2955.  value is negative. See Figure 3-8.
  2956.  
  2957.  Even though this instruction can be used to divide integers by a power of
  2958.  two, the type of division is not the same as that produced by the IDIV
  2959.  instruction. The quotient of IDIV is rounded toward zero, whereas the
  2960.  "quotient" of SAR is rounded toward negative infinity. This difference is
  2961.  apparent only for negative numbers. For example, when IDIV is used to divide
  2962.  -9 by 4, the result is -2 with a remainder of -1. If SAR is used to shift
  2963.  -9 right by two bits, the result is -3. The "remainder" of this kind of
  2964.  division is +3; however, the SAR instruction stores only the high-order bit
  2965.  of the remainder (in CF).
  2966.  
  2967.  The code sequence in Figure 3-9 produces the same result as IDIV for any M
  2968.  = 2^(N), where 0 < N < 32. This sequence takes about 12 to 18 clocks,
  2969.  depending on whether the jump is taken; if ECX contains M, the corresponding
  2970.  IDIV ECX instruction will take about 43 clocks.
  2971.  
  2972.  
  2973.  Figure 3-6.  SAL and SHL
  2974.  
  2975.                       OF   CF                OPERAND
  2976.  
  2977.         BEFORE SHL    X    X     10001000100010001000100010001111
  2978.         OR SAL
  2979.  
  2980.         AFTER SHL     1    1 ── 00010001000100010001000100011110 ── 0
  2981.         OR SAL BY 1
  2982.  
  2983.         AFTER SHL     X    0 ── 00100010001000100011110000000000 ── 0
  2984.         OR SAL BY 10
  2985.  
  2986.  SHL (WHICH HAS THE SYNONYM SAL) SHIFTS THE BITS IN THE REGISTER OR MEMORY
  2987.  OPERAND TO THE LEFT BY THE SPECIFIED NUMBER OF BIT POSITIONS. CF RECEIVES
  2988.  THE LAST BIT SHIFTED OUT OF THE LEFT OF THE OPERAND. SHL SHIFTS IN ZEROS
  2989.  TO FILL THE VACATED BIT LOCATIONS. THESE INSTRUCTIONS OPERATE ON BYTE,
  2990.  WORD, AND DOUBLEWORD OPERANDS.
  2991.  
  2992.  
  2993.  Figure 3-7.  SHR
  2994.  
  2995.                                        OPERAND                  CF
  2996.  
  2997.         BEFORE SHR         10001000100010001000100010001111     X
  2998.  
  2999.         AFTER SHR    0────01000100010001000100010001000111────1
  3000.         BY 1
  3001.  
  3002.         AFTER SHR    0────00000000001000100010001000100010────O
  3003.         BY 10
  3004.  
  3005.  SHR SHIFTS THE BITS OF THE REGISTER OR MEMORY OPERAND TO THE RIGHT BY THE
  3006.  SPECIFIED NUMBER OF BIT POSITIONS. CF RECEIVES THE LAST BIT SHIFTED OUT OF
  3007.  THE RIGHT OF THE OPERAND. SHR SHIFTS IN ZEROS TO FILL THE VACATED BIT
  3008.  LOCATIONS.
  3009.  
  3010.  
  3011.  Figure 3-8.  SAR
  3012.  
  3013.                                    POSITIVE OPERAND             CF
  3014.  
  3015.         BEFORE SAR         01000100010001000100010001000111     X
  3016.  
  3017.         AFTER SAR    0────00100010001000100010001000100011────1
  3018.         BY 1
  3019.  
  3020.                                    NEGATIVE OPERAND             CF
  3021.  
  3022.         BEFORE SAR         11000100010001000100010001000111     X
  3023.  
  3024.         AFTER SAR    0────11100010001000100010001000100011────1
  3025.         BY 1
  3026.  
  3027.  SAR PRESERVES THE SIGN OF THE REGISTER OR MEMORY OPERAND AS IT SHIFTS THE
  3028.  OPERAND TO THE RIGHT BY THE SPECIFIED NUMBER OF BIT POSITIONS. CF RECIEVES
  3029.  THE LAST BIT SHIFTED OUT OF THE RIGHT OF THE OPERAND.
  3030.  
  3031.  
  3032.  Figure 3-9.  Using SAR to Simulate IDIV
  3033.  
  3034.      ; assuming N is in ECX, and the dividend is in EAX
  3035.      ;                                               CLOCKS
  3036.      CMP     EAX, 0      ; to set sign flag          2
  3037.      JGE     NoAdjust    ; jump if sign is zero      3 or 9
  3038.      ADD     EAX, ECX    ;                           2
  3039.      DEC     EAX         ; EAX := EAX + (N-1)        2
  3040.  NoAdjust:
  3041.      SAR     EAX, CL     ;                           3
  3042.      ;                       TOTAL CLOCKS           12 or 18]
  3043.  
  3044.  
  3045.  3.4.4.2  Double-Shift Instructions
  3046.  
  3047.  These instructions provide the basic operations needed to implement
  3048.  operations on long unaligned bit strings. The double shifts operate either
  3049.  on word or doubleword operands, as follows:
  3050.  
  3051.    1.  Taking two word operands as input and producing a one-word output.
  3052.  
  3053.    2.  Taking two doubleword operands as input and producing a doubleword
  3054.        output.
  3055.  
  3056.  Of the two input operands, one may either be in a general register or in
  3057.  memory, while the other may only be in a general register. The results
  3058.  replace the memory or register operand. The number of bits to be shifted is
  3059.  specified either in the CL register or in an immediate byte of the
  3060.  instruction.
  3061.  
  3062.  Bits are shifted from the register operand into the memory or register
  3063.  operand. CF is set to the value of the last bit shifted out of the
  3064.  destination operand. SF, ZF, and PF are set according to the value of the
  3065.  result. OF and AF are left undefined.
  3066.  
  3067.  SHLD (Shift Left Double) shifts bits of the R/M field to the left, while
  3068.  shifting high-order bits from the Reg field into the R/M field on the right
  3069.  (see Figure 3-10). The result is stored back into the R/M operand. The Reg
  3070.  field is not modified.
  3071.  
  3072.  SHRD (Shift Right Double) shifts bits of the R/M field to the right, while
  3073.  shifting low-order bits from the Reg field into the R/M field on the left
  3074.  (see Figure 3-11). The result is stored back into the R/M operand. The Reg
  3075.  field is not modified.
  3076.  
  3077.  
  3078.  3.4.4.3  Rotate Instructions
  3079.  
  3080.  Rotate instructions allow bits in bytes, words, and doublewords to be
  3081.  rotated. Bits rotated out of an operand are not lost as in a shift, but are
  3082.  "circled" back into the other "end" of the operand.
  3083.  
  3084.  Rotates affect only the carry and overflow flags. CF may act as an
  3085.  extension of the operand in two of the rotate instructions, allowing a bit
  3086.  to be isolated and then tested by a conditional jump instruction (JC or
  3087.  JNC). CF always contains the value of the last bit rotated out, even if the
  3088.  instruction does not use this bit as an extension of the rotated operand.
  3089.  
  3090.  In single-bit rotates, OF is set if the operation changes the high-order
  3091.  (sign) bit of the destination operand. If the sign bit retains its original
  3092.  value, OF is cleared. On multibit rotates, the value of OF is always
  3093.  undefined.
  3094.  
  3095.  ROL (Rotate Left) rotates the byte, word, or doubleword destination operand
  3096.  left by one or by the number of bits specified in the count operand (an
  3097.  immediate value or the value contained in CL). For each rotation specified,
  3098.  the high-order bit that exits from the left of the operand returns at the
  3099.  right to become the new low-order bit of the operand. See Figure 3-12.
  3100.  
  3101.  ROR (Rotate Right) rotates the byte, word, or doubleword destination
  3102.  operand right by one or by the number of bits specified in the count operand
  3103.  (an immediate value or the value contained in CL). For each rotation
  3104.  specified, the low-order bit that exits from the right of the operand
  3105.  returns at the left to become the new high-order bit of the operand.
  3106.  See Figure 3-13.
  3107.  
  3108.  RCL (Rotate Through Carry Left) rotates bits in the byte, word, or
  3109.  doubleword destination operand left by one or by the number of bits
  3110.  specified in the count operand (an immediate value or the value contained in
  3111.  CL).
  3112.  
  3113.  This instruction differs from ROL in that it treats CF as a high-order
  3114.  one-bit extension of the destination operand. Each high-order bit that exits
  3115.  from the left side of the operand moves to CF before it returns to the
  3116.  operand as the low-order bit on the next rotation cycle. See Figure 3-14.
  3117.  
  3118.  RCR (Rotate Through Carry Right) rotates bits in the byte, word, or
  3119.  doubleword destination operand right by one or by the number of bits
  3120.  specified in the count operand (an immediate value or the value contained in
  3121.  CL).
  3122.  
  3123.  This instruction differs from ROR in that it treats CF as a low-order
  3124.  one-bit extension of the destination operand. Each low-order bit that exits
  3125.  from the right side of the operand moves to CF before it returns to the
  3126.  operand as the high-order bit on the next rotation cycle. See Figure 3-15.
  3127.  
  3128.  
  3129.  Figure 3-10.  Shift Left Double
  3130.  
  3131.                    31                   DESTINATION                 0
  3132.      ╔════╗       ╔══════════════════════════════════════════════════╗
  3133.      ║ CF ║──────╢                 MEMORY OF REGISTER               ║───┐
  3134.      ╚════╝       ╚══════════════════════════════════════════════════╝    │
  3135.               ┌───────────────────────────────────────────────────────────┘
  3136.               │    31                     SOURCE                    0
  3137.               │   ╔══════════════════════════════════════════════════╗
  3138.               └───╢                      REGISTER                    ║
  3139.                   ╚══════════════════════════════════════════════════╝
  3140.  
  3141.  
  3142.  Figure 3-11.  Shift Right Double
  3143.  
  3144.          31                     SOURCE                    0
  3145.         ╔══════════════════════════════════════════════════╗
  3146.         ║                      REGISTER                    ╟───┐
  3147.         ╚══════════════════════════════════════════════════╝   │
  3148.     ┌──────────────────────────────────────────────────────────┘
  3149.     │    31                   DESTINATION                 0
  3150.     │   ╔══════════════════════════════════════════════════╗        ╔════╗
  3151.     └──║                 MEMORY OF REGISTER               ╟───────║ CF ║
  3152.         ╚══════════════════════════════════════════════════╝        ╚════╝
  3153.  
  3154.  
  3155.  Figure 3-12.  ROL
  3156.  
  3157.                    31                   DESTINATION                 0
  3158.      ╔════╗       ╔══════════════════════════════════════════════════╗
  3159.      ║ CF ║───┬──╢                 MEMORY OF REGISTER               ║──┐
  3160.      ╚════╝    │  ╚══════════════════════════════════════════════════╝   │
  3161.                └─────────────────────────────────────────────────────────┘
  3162.  
  3163.  
  3164.  Figure 3-13.  ROR
  3165.  
  3166.       ┌──────────────────────────────────────────────────────────┐
  3167.       │    31                   DESTINATION                 0    │
  3168.       │   ╔══════════════════════════════════════════════════╗   │    ╔════╗
  3169.       └──║                 MEMORY OF REGISTER               ╟───┴───║ CF ║
  3170.           ╚══════════════════════════════════════════════════╝        ╚════╝
  3171.  
  3172.  
  3173.  Figure 3-14.  RCL
  3174.  
  3175.                    31                   DESTINATION                 0
  3176.      ╔════╗       ╔══════════════════════════════════════════════════╗
  3177.    ┌─╢ CF ║──────╢                 MEMORY OF REGISTER               ║──┐
  3178.    │ ╚════╝       ╚══════════════════════════════════════════════════╝   │
  3179.    └─────────────────────────────────────────────────────────────────────┘
  3180.  
  3181.  
  3182.  Figure 3-15.  RCR
  3183.  
  3184.     ┌──────────────────────────────────────────────────────────────────────┐
  3185.     │    31                   DESTINATION                 0                │
  3186.     │   ╔══════════════════════════════════════════════════╗        ╔════╗ │
  3187.     └──║                 MEMORY OF REGISTER               ╟───────║ CF ╟─┘
  3188.         ╚══════════════════════════════════════════════════╝        ╚════╝
  3189.  
  3190.  
  3191.  3.4.4.4  Fast "BIT BLT" Using Double Shift Instructions
  3192.  
  3193.  One purpose of the double shifts is to implement a bit string move, with
  3194.  arbitrary misalignment of the bit strings.  This is called a "bit blt" (BIT
  3195.  BLock Transfer.)  A simple example is to move a bit string from an arbitrary
  3196.  offset into a doubleword-aligned byte string.  A left-to-right string is
  3197.  moved 32 bits at a time if a double shift is used inside the move loop.
  3198.  
  3199.       MOV   ESI,ScrAddr
  3200.       MOV   EDI,DestAddr
  3201.       MOV   EBX,WordCnt
  3202.       MOV   CL,RelOffset      ; relative offset Dest-Src
  3203.       MOV   EDX,[ESI]         ; load first word of source
  3204.       ADD   ESI,4             ; bump source address
  3205.  BltLoop:
  3206.       LODS                    ; new low order part
  3207.       SHLD  EDX,EAX,CL        ; EDX overwritten with aligned stuff
  3208.       XCHG  EDX,EAS           ; Swap high/low order parts
  3209.       STOS                    ; Write out next aligned chunk
  3210.       DEC   EBX
  3211.       JA    BltLoop
  3212.  
  3213.  This loop is simple yet allows the data to be moved in 32-bit pieces for
  3214.  the highest possible performance. Without a double shift, the best that can
  3215.  be achieved is 16 bits per loop iteration by using a 32-bit shift and
  3216.  replacing the XCHG with a ROR by 16 to swap high and low order parts of
  3217.  registers. A more general loop than shown above would require some extra
  3218.  masking on the first doubleword moved (before the main loop), and on the
  3219.  last doubleword moved (after the main loop), but would have the same basic
  3220.  32-bits per loop iteration as the code above.
  3221.  
  3222.  
  3223.  3.4.4.5  Fast Bit-String Insert and Extract
  3224.  
  3225.  The double shift instructions also enable:
  3226.  
  3227.    ■  Fast insertion of a bit string from a register into an arbitrary bit
  3228.       location in a larger bit string in memory without disturbing the bits
  3229.       on either side of the inserted bits.
  3230.  
  3231.    ■  Fast extraction of a bits string into a register from an arbitrary bit
  3232.       location in a larger bit string in memory without disturbing the bits
  3233.       on either side of the extracted bits.
  3234.  
  3235.  The following coded examples illustrate bit insertion and extraction under
  3236.  variousconditions:
  3237.  
  3238.    1.  Bit String Insert into Memory (when bit string is 1-25 bits long,
  3239.        i.e., spans four bytes or less):
  3240.  
  3241.        ; Insert a right-justified bit string from register into
  3242.        ; memory bit string.
  3243.        ;
  3244.        ; Assumptions:
  3245.        ; 1) The base of the string array is dword aligned, and
  3246.        ; 2) the length of the bit string is an immediate value
  3247.        ;    but the bit offset is held in a register.
  3248.        ;
  3249.        ; Register ESI holds the right-justified bit string
  3250.        ; to be inserted.
  3251.        ; Register EDI holds the bit offset of the start of the
  3252.        ; substring.
  3253.        ; Registers EAX and ECX are also used by this
  3254.        ; "insert" operation.
  3255.        ;
  3256.        MOV   ECX,EDI      ; preserve original offset for later use
  3257.        SHR   EDI,3        ; signed divide offset by 8 (byte address)
  3258.        AND   CL,7H        ; isolate low three bits of offset in CL
  3259.        MOV   EAX,[EDI]strg_base      ; move string dword into EAX
  3260.        ROR   EAX,CL       ; right justify old bit field
  3261.        SHRD  EAX,ESI,length          ; bring in new bits
  3262.        ROL   EAX,length   ; right justify new bit field
  3263.        ROL   EAX,CL       ; bring to final position
  3264.        MOV   [EDI]strg_base,EAX      ; replace dword in memory
  3265.  
  3266.    2.  Bit String Insert into Memory (when bit string is 1-31 bits long, i.e.
  3267.        spans five bytes or less):
  3268.  
  3269.        ; Insert a right-justified bit string from register into
  3270.        ; memory bit string.
  3271.        ;
  3272.        ; Assumptions:
  3273.        ; 1) The base of the string array is dword aligned, and
  3274.        ; 2) the length of the bit string is an immediate value
  3275.        ;    but the bit offset is held in a register.
  3276.        ;
  3277.        ; Register ESI holds the right-justified bit string
  3278.        ; to be inserted.
  3279.        ; Register EDI holds the bit offset of the start of the
  3280.        ; substring.
  3281.        ; Registers EAX, EBX, ECX, and EDI are also used by
  3282.        ; this "insert" operation.
  3283.        ;
  3284.        MOV   ECX,EDI     ; temp storage for offset
  3285.        SHR   EDI,5       ; signed divide offset by 32 (dword address)
  3286.        SHL   EDI,2       ; multiply by 4 (in byte address format)
  3287.        AND   CL,1FH      ; isolate low five bits of offset in CL
  3288.        MOV   EAX,[EDI]strg_base      ; move low string dword into EAX
  3289.        MOV   EDX,[EDI]strg_base+4    ; other string dword into EDX
  3290.        MOV   EBX,EAX     ; temp storage for part of string     ┐ rotate
  3291.        SHRD  EAX,EDX,CL  ; double shift by offset within dword ├ EDX:EAX
  3292.        SHRD  EAX,EBX,CL  ; double shift by offset within dword ┘ right
  3293.        SHRD  EAX,ESI,length          ; bring in new bits
  3294.        ROL   EAX,length  ; right justify new bit field
  3295.        MOV   EBX,EAX     ; temp storage for part of string         ┐ rotate
  3296.        SHLD  EAX,EDX,CL  ; double shift back by offset within word ├ EDX:EAX
  3297.        SHLD  EDX,EBX,CL  ; double shift back by offset within word ┘ left
  3298.        MOV   [EDI]strg_base,EAX      ; replace dword in memory
  3299.        MOV   [EDI]strg_base+4,EDX    ; replace dword in memory
  3300.  
  3301.    3.  Bit String Insert into Memory (when bit string is exactly 32 bits
  3302.        long, i.e., spans five or four types of memory):
  3303.  
  3304.        ; Insert right-justified bit string from register into
  3305.        ; memory bit string.
  3306.        ;
  3307.        ; Assumptions:
  3308.        ; 1) The base of the string array is dword aligned, and
  3309.        ; 2) the length of the bit string is 32
  3310.        ;    but the bit offset is held in a register.
  3311.        ;
  3312.        ; Register ESI holds the 32-bit string to be inserted.
  3313.        ; Register EDI holds the bit offset of the start of the
  3314.        ; substring.
  3315.        ; Registers EAX, EBX, ECX, and EDI are also used by
  3316.        ; this "insert" operation.
  3317.        ;
  3318.        MOV   EDX,EDI     ; preserve original offset for later use
  3319.        SHR   EDI,5       ; signed divide offset by 32 (dword address)
  3320.        SHL   EDI,2       ; multiply by 4 (in byte address format)
  3321.        AND   CL,1FH      ; isolate low five bits of offset in CL
  3322.        MOV   EAX,[EDI]strg_base      ; move low string dword into EAX
  3323.        MOV   EDX,[EDI]strg_base+4    ; other string dword into EDX
  3324.        MOV   EBX,EAX     ; temp storage for part of string     ┐ rotate
  3325.        SHRD  EAX,EDX     ; double shift by offset within dword ├ EDX:EAX
  3326.        SHRD  EDX,EBX     ; double shift by offset within dword ┘ right
  3327.        MOV   EAX,ESI     ; move 32-bit bit field into position
  3328.        MOV   EBX,EAX     ; temp storage for part of string         ┐ rotate
  3329.        SHLD  EAX,EDX     ; double shift back by offset within word ├ EDX:EAX
  3330.        SHLD  EDX,EBX     ; double shift back by offset within word ┘ left
  3331.        MOV   [EDI]strg_base,EAX      ; replace dword in memory
  3332.        MOV   [EDI]strg_base,+4,EDX   ; replace dword in memory
  3333.  
  3334.    4.  Bit String Extract from Memory (when bit string is 1-25 bits long,
  3335.        i.e., spans four bytes or less):
  3336.  
  3337.        ; Extract a right-justified bit string from memory bit
  3338.        ; string into register
  3339.        ;
  3340.        ; Assumptions:
  3341.        ; 1) The base of the string array is dword aligned, and
  3342.        ; 2) the length of the bit string is an immediate value
  3343.        ;    but the bit offset is held in a register.
  3344.        ;
  3345.        ; Register EAX holds the right-justified, zero-padded
  3346.        ; bit string that was extracted.
  3347.        ; Register EDI holds the bit offset of the start of the
  3348.        ; substring.
  3349.        ; Registers EDI, and ECX are also used by this "extract."
  3350.        ;
  3351.        MOV  ECX,EDI      ; temp storage for offset
  3352.        SHR  EDI,3        ; signed divide offset by 8 (byte address)
  3353.        AND  CL,7H        ; isolate low three bits of offset
  3354.        MOV  EAX,[EDI]strg_base       ; move string dword into EAX
  3355.        SHR  EAX,CL       ; shift by offset within dword
  3356.        AND  EAX,mask     ; extracted bit field in EAX
  3357.  
  3358.    5.  Bit String Extract from Memory (when bit string is 1-32 bits long,
  3359.        i.e., spans five bytes or less):
  3360.  
  3361.        ; Extract a right-justified bit string from memory bit
  3362.        ; string into register.
  3363.        ;
  3364.        ; Assumptions:
  3365.        ; 1) The base of the string array is dword aligned, and
  3366.        ; 2) the length of the bit string is an immediate
  3367.        ;    value but the bit offset is held in a register.
  3368.        ;
  3369.        ; Register EAX holds the right-justified, zero-padded
  3370.        ; bit string that was extracted.
  3371.        ; Register EDI holds the bit offset of the start of the
  3372.        ; substring.
  3373.        ; Registers EAX, EBX, and ECX are also used by this "extract."
  3374.        MOV   ECX,EDI     ; temp storage for offset
  3375.        SHR   EDI,5       ; signed divide offset by 32 (dword address)
  3376.        SHL   EDI,2       ; multiply by 4 (in byte address format)
  3377.        AND   CL,1FH      ; isolate low five bits of offset in CL
  3378.        MOV   EAX,[EDI]strg_base      ; move low string dword into EAX
  3379.        MOV   EDX,[EDI]strg_base+4    ; other string dword into EDX
  3380.        SHRD  EAX,EDX,CL  ; double shift right by offset within dword
  3381.        AND   EAX,mask    ; extracted bit field in EAX
  3382.  
  3383.  
  3384.  3.4.5  Byte-Set-On-Condition Instructions
  3385.  
  3386.  This group of instructions sets a byte to zero or one depending on any of
  3387.  the 16 conditions defined by the status flags. The byte may be in memory or
  3388.  may be a one-byte general register. These instructions are especially useful
  3389.  for implementing Boolean expressions in high-level languages such as Pascal.
  3390.  
  3391.  SETcc (Set Byte on Condition cc) set a byte to one if condition cc is true;
  3392.  sets the byte to zero otherwise. Refer to Appendix D for a definition of
  3393.  the possible conditions.
  3394.  
  3395.  
  3396.  3.4.6  Test Instruction
  3397.  
  3398.  TEST (Test) performs the logical "and" of the two operands, clears OF and
  3399.  CF, leaves AF undefined, and updates SF, ZF, and PF. The flags can be tested
  3400.  by conditional control transfer instructions or by the byte-set-on-condition
  3401.  instructions. The operands may be doublewords, words, or bytes.
  3402.  
  3403.  The difference between TEST and AND is that TEST does not alter the
  3404.  destination operand. TEST differs from BT in that TEST is useful for testing
  3405.  the value of multiple bits in one operations, whereas BT tests a single bit.
  3406.  
  3407.  
  3408.  3.5  Control Transfer Instructions
  3409.  
  3410.  The 80386 provides both conditional and unconditional control transfer
  3411.  instructions to direct the flow of execution. Conditional control transfers
  3412.  depend on the results of operations that affect the flag register.
  3413.  Unconditional control transfers are always executed.
  3414.  
  3415.  
  3416.  3.5.1  Unconditional Transfer Instructions
  3417.  
  3418.  JMP, CALL, RET, INT and IRET instructions transfer control from one code
  3419.  segment location to another. These locations can be within the same code
  3420.  segment (near control transfers) or in different code segments (far control
  3421.  transfers). The variants of these instructions that transfer control to
  3422.  other segments are discussed in a later section of this chapter. If the
  3423.  model of memory organization used in a particular 80386 application does
  3424.  not make segments visible to applications programmers, intersegment control
  3425.  transfers will not be used.
  3426.  
  3427.  
  3428.  3.5.1.1  Jump Instruction
  3429.  
  3430.  JMP (Jump) unconditionally transfers control to the target location. JMP is
  3431.  a one-way transfer of execution; it does not save a return address on the
  3432.  stack.
  3433.  
  3434.  The JMP instruction always performs the same basic function of transferring
  3435.  control from the current location to a new location. Its implementation
  3436.  varies depending on whether the address is specified directly within the
  3437.  instruction or indirectly through a register or memory.
  3438.  
  3439.  A direct JMP instruction includes the destination address as part of the
  3440.  instruction. An indirect JMP instruction obtains the destination address
  3441.  indirectly through a register or a pointer variable.
  3442.  
  3443.  Direct near JMP. A direct JMP uses a relative displacement value contained
  3444.  in the instruction. The displacement is signed and the size of the
  3445.  displacement may be a byte, word, or doubleword. The processor forms an
  3446.  effective address by adding this relative displacement to the address
  3447.  contained in EIP. When the additions have been performed, EIP refers to the
  3448.  next instruction to be executed.
  3449.  
  3450.  Indirect near JMP. Indirect JMP instructions specify an absolute address in
  3451.  one of several ways:
  3452.  
  3453.    1.  The program can JMP to a location specified by a general register
  3454.        (any of EAX, EDX, ECX, EBX, EBP, ESI, or EDI). The processor moves
  3455.        this 32-bit value into EIP and resumes execution.
  3456.  
  3457.    2.  The processor can obtain the destination address from a memory
  3458.        operand specified in the instruction.
  3459.  
  3460.    3.  A register can modify the address of the memory pointer to select a
  3461.        destination address.
  3462.  
  3463.  
  3464.  3.5.1.2  Call Instruction
  3465.  
  3466.  CALL (Call Procedure) activates an out-of-line procedure, saving on the
  3467.  stack the address of the instruction following the CALL for later use by a
  3468.  RET (Return) instruction. CALL places the current value of EIP on the stack.
  3469.  The RET instruction in the called procedure uses this address to transfer
  3470.  control back to the calling program.
  3471.  
  3472.  CALL instructions, like JMP instructions have relative, direct, and
  3473.  indirect versions.
  3474.  
  3475.  Indirect CALL instructions specify an absolute address in one of these
  3476.  ways:
  3477.  
  3478.    1.  The program can CALL a location specified by a general register (any
  3479.        of EAX, EDX, ECX, EBX, EBP, ESI, or EDI). The processor moves this
  3480.        32-bit value into EIP.
  3481.  
  3482.    2.  The processor can obtain the destination address from a memory
  3483.        operand specified in the instruction.
  3484.  
  3485.  
  3486.  3.5.1.3  Return and Return-From-Interrupt Instruction
  3487.  
  3488.  RET (Return From Procedure) terminates the execution of a procedure and
  3489.  transfers control through a back-link on the stack to the program that
  3490.  originally invoked the procedure. RET restores the value of EIP that was
  3491.  saved on the stack by the previous CALL instruction.
  3492.  
  3493.  RET instructions may optionally specify an immediate operand. By adding
  3494.  this constant to the new top-of-stack pointer, RET effectively removes any
  3495.  arguments that the calling program pushed on the stack before the execution
  3496.  of the CALL instruction.
  3497.  
  3498.  IRET (Return From Interrupt) returns control to an interrupted procedure.
  3499.  IRET differs from RET in that it also pops the flags from the stack into the
  3500.  flags register. The flags are stored on the stack by the interrupt
  3501.  mechanism.
  3502.  
  3503.  
  3504.  3.5.2  Conditional Transfer Instructions
  3505.  
  3506.  The conditional transfer instructions are jumps that may or may not
  3507.  transfer control, depending on the state of the CPU flags when the
  3508.  instruction executes.
  3509.  
  3510.  
  3511.  3.5.2.1  Conditional Jump Instructions
  3512.  
  3513.  Table 3-2 shows the conditional transfer mnemonics and their
  3514.  interpretations. The conditional jumps that are listed as pairs are actually
  3515.  the same instruction. The assembler provides the alternate mnemonics for
  3516.  greater clarity within a program listing.
  3517.  
  3518.  Conditional jump instructions contain a displacement which is added to the
  3519.  EIP register if the condition is true. The displacement may be a byte, a
  3520.  word, or a doubleword. The displacement is signed; therefore, it can be used
  3521.  to jump forward or backward.
  3522.  
  3523.  
  3524.  Table 3-2. Interpretation of Conditional Transfers
  3525.  
  3526.  Unsigned Conditional Transfers
  3527.  
  3528.  Mnemonic         Condition Tested          "Jump If..."
  3529.  
  3530.  JA/JNBE           (CF or ZF) = 0           above/not below nor equal
  3531.  JAE/JNB           CF = 0                   above or equal/not below
  3532.  JB/JNAE           CF = 1                   below/not above nor equal
  3533.  JBE/JNA           (CF or ZF) = 1           below or equal/not above
  3534.  JC                CF = 1                   carry
  3535.  JE/JZ             ZF = 1                   equal/zero
  3536.  JNC               CF = 0                   not carry
  3537.  JNE/JNZ           ZF = 0                   not equal/not zero
  3538.  JNP/JPO           PF = 0                   not parity/parity odd
  3539.  JP/JPE            PF = 1                   parity/parity even
  3540.  
  3541.  Signed Conditional Transfers
  3542.  
  3543.  Mnemonic         Condition Tested          "Jump If..."
  3544.  JG/JNLE          ((SF xor OF) or ZF) = 0   greater/not less nor equal
  3545.  JGE/JNL          (SF xor OF) = 0           greater or equal/not less
  3546.  JL/JNGE          (SF xor OF) = 1           less/not greater nor equal
  3547.  JLE/JNG          ((SF xor OF) or ZF) = 1   less or equal/not greater
  3548.  JNO              OF = 0                    not overflow
  3549.  JNS              SF = 0                    not sign (positive, including 0)
  3550.  JO               OF = 1                    overflow
  3551.  JS               SF = 1                    sign (negative)
  3552.  
  3553.  
  3554.  3.5.2.2  Loop Instructions
  3555.  
  3556.  The loop instructions are conditional jumps that use a value placed in ECX
  3557.  to specify the number of repetitions of a software loop. All loop
  3558.  instructions automatically decrement ECX and terminate the loop when ECX=0.
  3559.  Four of the five loop instructions specify a condition involving ZF that
  3560.  terminates the loop before ECX reaches zero.
  3561.  
  3562.  LOOP (Loop While ECX Not Zero) is a conditional transfer that automatically
  3563.  decrements the ECX register before testing ECX for the branch condition. If
  3564.  ECX is non-zero, the program branches to the target label specified in the
  3565.  instruction. The LOOP instruction causes the repetition of a code section
  3566.  until the operation of the LOOP instruction decrements ECX to a value of
  3567.  zero. If LOOP finds ECX=0, control transfers to the instruction immediately
  3568.  following the LOOP instruction. If the value of ECX is initially zero, then
  3569.  the LOOP executes 2^(32) times.
  3570.  
  3571.  LOOPE (Loop While Equal) and LOOPZ (Loop While Zero) are synonyms for the
  3572.  same instruction. These instructions automatically decrement the ECX
  3573.  register before testing ECX and ZF for the branch conditions. If ECX is
  3574.  non-zero and ZF=1, the program branches to the target label specified in the
  3575.  instruction. If LOOPE or LOOPZ finds that ECX=0 or ZF=0, control transfers
  3576.  to the instruction immediately following the LOOPE or LOOPZ instruction.
  3577.  
  3578.  LOOPNE (Loop While Not Equal) and LOOPNZ (Loop While Not Zero) are synonyms
  3579.  for the same instruction. These instructions automatically decrement the ECX
  3580.  register before testing ECX and ZF for the branch conditions. If ECX is
  3581.  non-zero and ZF=0, the program branches to the target label specified in the
  3582.  instruction. If LOOPNE or LOOPNZ finds that ECX=0 or ZF=1, control transfers
  3583.  to the instruction immediately following the LOOPNE or LOOPNZ instruction.
  3584.  
  3585.  
  3586.  3.5.2.3  Executing a Loop or Repeat Zero Times
  3587.  
  3588.  JCXZ (Jump if ECX Zero) branches to the label specified in the instruction
  3589.  if it finds a value of zero in ECX. JCXZ is useful in combination with the
  3590.  LOOP instruction and with the string scan and compare instructions, all of
  3591.  which decrement ECX. Sometimes, it is desirable to design a loop that
  3592.  executes zero times if the count variable in ECX is initialized to zero.
  3593.  Because the LOOP instructions (and repeat prefixes) decrement ECX before
  3594.  they test it, a loop will execute 2^(32) times if the program enters the
  3595.  loop with a zero value in ECX. A programmer may conveniently overcome this
  3596.  problem with JCXZ, which enables the program to branch around the code
  3597.  within the loop if ECX is zero when JCXZ executes. When used with repeated
  3598.  string scan and compare instructions, JCXZ can determine whether the
  3599.  repetitions terminated due to zero in ECX or due to satisfaction of the
  3600.  scan or compare conditions.
  3601.  
  3602.  
  3603.  3.5.3  Software-Generated Interrupts
  3604.  
  3605.  The INT n, INTO, and BOUND instructions allow the programmer to specify a
  3606.  transfer to an interrupt service routine from within a program.
  3607.  
  3608.  INT n (Software Interrupt) activates the interrupt service routine that
  3609.  corresponds to the number coded within the instruction. The INT instruction
  3610.  may specify any interrupt type. Programmers may use this flexibility to
  3611.  implement multiple types of internal interrupts or to test the operation of
  3612.  interrupt service routines. (Interrupts 0-31 are reserved by Intel.) The
  3613.  interrupt service routine terminates with an IRET instruction that returns
  3614.  control to the instruction that follows INT.
  3615.  
  3616.  INTO (Interrupt on Overflow) invokes interrupt 4 if OF is set. Interrupt 4
  3617.  is reserved for this purpose. OF is set by several arithmetic, logical, and
  3618.  string instructions.
  3619.  
  3620.  BOUND (Detect Value Out of Range) verifies that the signed value contained
  3621.  in the specified register lies within specified limits. An interrupt (INT 5)
  3622.  occurs if the value contained in the register is less than the lower bound
  3623.  or greater than the upper bound.
  3624.  
  3625.  The BOUND instruction includes two operands. The first operand specifies
  3626.  the register being tested. The second operand contains the effective
  3627.  relative address of the two signed BOUND limit values. The BOUND instruction
  3628.  assumes that the upper limit and lower limit are in adjacent memory
  3629.  locations. These limit values cannot be register operands; if they are, an
  3630.  invalid opcode exception occurs.
  3631.  
  3632.  BOUND is useful for checking array bounds before using a new index value to
  3633.  access an element within the array. BOUND provides a simple way to check the
  3634.  value of an index register before the program overwrites information in a
  3635.  location beyond the limit of the array.
  3636.  
  3637.  The block of memory that specifies the lower and upper limits of an array
  3638.  might typically reside just before the array itself. This makes the array
  3639.  bounds accessible at a constant offset from the beginning of the array.
  3640.  Because the address of the array will already be present in a register, this
  3641.  practice avoids extra calculations to obtain the effective address of the
  3642.  array bounds.
  3643.  
  3644.  The upper and lower limit values may each be a word or a doubleword.
  3645.  
  3646.  
  3647.  3.6  String and Character Translation Instructions
  3648.  
  3649.  The instructions in this category operate on strings rather than on logical
  3650.  or numeric values. Refer also to the section on I/O for information about
  3651.  the string I/O instructions (also known as block I/O).
  3652.  
  3653.  The power of 80386 string operations derives from the following features of
  3654.  the architecture:
  3655.  
  3656.  1.  A set of primitive string operations
  3657.  
  3658.     MOVS   ── Move String
  3659.     CMPS   ── Compare string
  3660.     SCAS   ── Scan string
  3661.     LODS   ── Load string
  3662.     STOS   ── Store string
  3663.  
  3664.  2.  Indirect, indexed addressing, with automatic incrementing or
  3665.      decrementing of the indexes.
  3666.  
  3667.     Indexes:
  3668.  
  3669.        ESI    ── Source index register
  3670.        EDI    ── Destination index register
  3671.  
  3672.     Control flag:
  3673.  
  3674.        DF     ── Direction flag
  3675.  
  3676.     Control flag instructions:
  3677.  
  3678.        CLD    ── Clear direction flag instruction
  3679.        STD    ── Set direction flag instruction
  3680.  
  3681.  3.  Repeat prefixes
  3682.  
  3683.     REP          ── Repeat while ECX not xero
  3684.     REPE/REPZ    ── Repeat while equal or zero
  3685.     REPNE/REPNZ  ── Repeat while not equal or not zero
  3686.  
  3687.  The primitive string operations operate on one element of a string. A
  3688.  string element may be a byte, a word, or a doubleword. The string elements
  3689.  are addressed by the registers ESI and EDI. After every primitive operation
  3690.  ESI and/or EDI are automatically updated to point to the next element of the
  3691.  string. If the direction flag is zero, the index registers are incremented;
  3692.  if one, they are decremented. The amount of the increment or decrement is
  3693.  1, 2, or 4 depending on the size of the string element.
  3694.  
  3695.  
  3696.  3.6.1  Repeat Prefixes
  3697.  
  3698.  The repeat prefixes REP (Repeat While ECX Not Zero), REPE/REPZ (Repeat
  3699.  While Equal/Zero), and REPNE/REPNZ (Repeat While Not Equal/Not Zero) specify
  3700.  repeated operation of a string primitive. This form of iteration allows the
  3701.  CPU to process strings much faster than would be possible with a regular
  3702.  software loop.
  3703.  
  3704.  When a primitive string operation has a repeat prefix, the operation is
  3705.  executed repeatedly, each time using a different element of the string. The
  3706.  repetition terminates when one of the conditions specified by the prefix is
  3707.  satisfied.
  3708.  
  3709.  At each repetition of the primitive instruction, the string operation may
  3710.  be suspended temporarily in order to handle an exception or external
  3711.  interrupt. After the interruption, the string operation can be restarted
  3712.  again where it left off. This method of handling strings allows operations
  3713.  on strings of arbitrary length, without affecting interrupt response.
  3714.  
  3715.  All three prefixes causes the hardware to automatically repeat the
  3716.  associated string primitive until ECX=0. The differences among the repeat
  3717.  prefixes have to do with the second termination condition. REPE/REPZ and
  3718.  REPNE/REPNZ are used exclusively with the SCAS (Scan String) and CMPS
  3719.  (Compare String) primitives. When these prefixes are used, repetition of the
  3720.  next instruction depends on the zero flag (ZF) as well as the ECX register.
  3721.  ZF does not require initialization before execution of a repeated string
  3722.  instruction, because both SCAS and CMPS set ZF according to the results of
  3723.  the comparisons they make. The differences are summarized in the
  3724.  accompanying table.
  3725.  
  3726.  Prefix                      Termination         Termination
  3727.                              Condition 1         Condition 2
  3728.  
  3729.  REP                           ECX = 0             (none)
  3730.  REPE/REPZ                     ECX = 0             ZF = 0
  3731.  REPNE/REPNZ                   ECX = 0             ZF = 1
  3732.  
  3733.  
  3734.  3.6.2  Indexing and Direction Flag Control
  3735.  
  3736.  The addresses of the operands of string primitives are determined by the
  3737.  ESI and EDI registers. ESI points to source operands. By default, ESI refers
  3738.  to a location in the segment indicated by the DS segment register. A
  3739.  segment-override prefix may be used, however, to cause ESI to refer to CS,
  3740.  SS, ES, FS, or GS. EDI points to destination operands in the segment
  3741.  indicated by ES; no segment override is possible. The use of two different
  3742.  segment registers in one instruction allows movement of strings between
  3743.  different segments.
  3744.  
  3745.  This use of ESI and DSI has led to the descriptive names source index and
  3746.  destination index for the ESI and EDI registers, respectively. In all
  3747.  cases other than string instructions, however, the ESI and EDI registers may
  3748.  be used as general-purpose registers.
  3749.  
  3750.  When ESI and EDI are used in string primitives, they are automatically
  3751.  incremented or decremented after to operation. The direction flag determines
  3752.  whether they are incremented or decremented. The instruction CLD puts zero
  3753.  in DF, causing the index registers to be incremented; the instruction STD
  3754.  puts one in DF, causing the index registers to be decremented. Programmers
  3755.  should always put a known value in DF before using string instructions in a
  3756.  procedure.
  3757.  
  3758.  
  3759.  3.6.3  String Instructions
  3760.  
  3761.  MOVS (Move String) moves the string element pointed to by ESI to the
  3762.  location pointed to by EDI. MOVSB operates on byte elements, MOVSW operates
  3763.  on word elements, and MOVSD operates on doublewords. The destination segment
  3764.  register cannot be overridden by a segment override prefix, but the source
  3765.  segment register can be overridden.
  3766.  
  3767.  The MOVS instruction, when accompanied by the REP prefix, operates as a
  3768.  memory-to-memory block transfer. To set up for this operation, the program
  3769.  must initialize ECX and the register pairs ESI and EDI. ECX specifies the
  3770.  number of bytes, words, or doublewords in the block.
  3771.  
  3772.  If DF=0, the program must point ESI to the first element of the source
  3773.  string and point EDI to the destination address for the first element. If
  3774.  DF=1, the program must point these two registers to the last element of the
  3775.  source string and to the destination address for the last element,
  3776.  respectively.
  3777.  
  3778.  CMPS (Compare Strings) subtracts the destination string element (at ES:EDI)
  3779.  from the source string element (at ESI) and updates the flags AF, SF, PF, CF
  3780.  and OF. If the string elements are equal, ZF=1; otherwise, ZF=0. If DF=0,
  3781.  the processor increments the memory pointers (ESI and EDI) for the two
  3782.  strings. CMPSB compares bytes, CMPSW compares words, and CMPSD compares
  3783.  doublewords. The segment register used for the source address can be changed
  3784.  with a segment override prefix while the destination segment register
  3785.  cannot be overridden.
  3786.  
  3787.  SCAS (Scan String) subtracts the destination string element at ES:EDI from
  3788.  EAX, AX, or AL and updates the flags AF, SF, ZF, PF, CF and OF. If the
  3789.  values are equal, ZF=1; otherwise, ZF=0. If DF=0, the processor increments
  3790.  the memory pointer (EDI) for the string. SCASB scans bytes; SCASW scans
  3791.  words; SCASD scans doublewords. The destination segment register (ES) cannot
  3792.  be overridden.
  3793.  
  3794.  When either the REPE or REPNE prefix modifies either the SCAS or CMPS
  3795.  primitives, the processor compares the value of the current string element
  3796.  with the value in EAX for doubleword elements, in AX for word elements, or
  3797.  in AL for byte elements. Termination of the repeated operation depends on
  3798.  the resulting state of ZF as well as on the value in ECX.
  3799.  
  3800.  LODS (Load String) places the source string element at ESI into EAX for
  3801.  doubleword strings, into AX for word strings, or into AL for byte strings.
  3802.  LODS increments or decrements ESI according to DF.
  3803.  
  3804.  STOS (Store String) places the source string element from EAX, AX, or AL
  3805.  into the string at ES:DSI. STOS increments or decrements EDI according to
  3806.  DF.
  3807.  
  3808.  
  3809.  3.7  Instructions for Block-Structured Languages
  3810.  
  3811.  The instructions in this section provide machine-language support for
  3812.  functions normally found in high-level languages. These instructions include
  3813.  ENTER and LEAVE, which simplify the programming of procedures.
  3814.  
  3815.  ENTER (Enter Procedure) creates a stack frame that may be used to implement
  3816.  the scope rules of block-structured high-level languages. A LEAVE
  3817.  instruction at the end of a procedure complements an ENTER at the beginning
  3818.  of the procedure to simplify stack management and to control access to
  3819.  variables for nested procedures.
  3820.  
  3821.  The ENTER instruction includes two parameters. The first parameter
  3822.  specifies the number of bytes of dynamic storage to be allocated on the
  3823.  stack for the routine being entered. The second parameter corresponds to the
  3824.  lexical nesting level (0-31) of the routine. (Note that the lexical level
  3825.  has no relationship to either the protection privilege levels or to the I/O
  3826.  privilege level.)
  3827.  
  3828.  The specified lexical level determines how many sets of stack frame
  3829.  pointers the CPU copies into the new stack frame from the preceding frame.
  3830.  This list of stack frame pointers is sometimes called the display. The first
  3831.  word of the display is a pointer to the last stack frame. This pointer
  3832.  enables a LEAVE instruction to reverse the action of the previous ENTER
  3833.  instruction by effectively discarding the last stack frame.
  3834.  
  3835.     Example: ENTER 2048,3
  3836.  
  3837.     Allocates 2048 bytes of dynamic storage on the stack and sets up pointers
  3838.     to two previous stack frames in the stack frame that ENTER creates for
  3839.     this procedure.
  3840.  
  3841.  After ENTER creates the new display for a procedure, it allocates the
  3842.  dynamic storage space for that procedure by decrementing ESP by the number
  3843.  of bytes specified in the first parameter. This new value of ESP serves as a
  3844.  starting point for all PUSH and POP operations within that procedure.
  3845.  
  3846.  To enable a procedure to address its display, ENTER leaves EBP pointing to
  3847.  the beginning of the new stack frame. Data manipulation instructions that
  3848.  specify EBP as a base register implicitly address locations within the stack
  3849.  segment instead of the data segment.
  3850.  
  3851.  The ENTER instruction can be used in two ways: nested and non-nested. If
  3852.  the lexical level is 0, the non-nested form is used. Since the second
  3853.  operand is 0, ENTER pushes EBP, copies ESP to EBP and then subtracts the
  3854.  first operand from ESP. The nested form of ENTER occurs when the second
  3855.  parameter (lexical level) is not 0.
  3856.  
  3857.  Figure 3-16 gives the formal definition of ENTER.
  3858.  
  3859.  The main procedure (with other procedures nested within) operates at the
  3860.  highest lexical level, level 1. The first procedure it calls operates at the
  3861.  next deeper lexical level, level 2. A level 2 procedure can access the
  3862.  variables of the main program which are at fixed locations specified by the
  3863.  compiler. In the case of level 1, ENTER allocates only the requested
  3864.  dynamic storage on the stack because there is no previous display to copy.
  3865.  
  3866.  A program operating at a higher lexical level calling a program at a lower
  3867.  lexical level requires that the called procedure should have access to the
  3868.  variables of the calling program. ENTER provides this access through a
  3869.  display that provides addressability to the calling program's stack frame.
  3870.  
  3871.  A procedure calling another procedure at the same lexical level implies
  3872.  that they are parallel procedures and that the called procedure should not
  3873.  have access to the variables of the calling procedure. In this case, ENTER
  3874.  copies only that portion of the display from the calling procedure which
  3875.  refers to previously nested procedures operating at higher lexical levels.
  3876.  The new stack frame does not include the pointer for addressing the calling
  3877.  procedure's stack frame.
  3878.  
  3879.  ENTER treats a reentrant procedure as a procedure calling another procedure
  3880.  at the same lexical level. In this case, each succeeding iteration of the
  3881.  reentrant procedure can address only its own variables and the variables of
  3882.  the calling procedures at higher lexical levels. A reentrant procedure can
  3883.  always address its own variables; it does not require pointers to the stack
  3884.  frames of previous iterations.
  3885.  
  3886.  By copying only the stack frame pointers of procedures at higher lexical
  3887.  levels, ENTER makes sure that procedures access only those variables of
  3888.  higher lexical levels, not those at parallel lexical levels (see Figure
  3889.  3-17). Figures 3-18 through 3-21 demonstrate the actions of the ENTER
  3890.  instruction if the modules shown in Figure 3-17 were to call one another in
  3891.  alphabetic order.
  3892.  
  3893.  Block-structured high-level languages can use the lexical levels defined by
  3894.  ENTER to control access to the variables of previously nested procedures.
  3895.  Referring to Figure 3-17 for example, if PROCEDURE A calls PROCEDURE B
  3896.  which, in turn, calls PROCEDURE C, then PROCEDURE C will have access to the
  3897.  variables of MAIN and PROCEDURE A, but not PROCEDURE B because they operate
  3898.  at the same lexical level. Following is the complete definition of access to
  3899.  variables for Figure 3-17.
  3900.  
  3901.    1.  MAIN PROGRAM has variables at fixed locations.
  3902.  
  3903.    2.  PROCEDURE A can access only the fixed variables of MAIN.
  3904.  
  3905.    3.  PROCEDURE B can access only the variables of PROCEDURE A and MAIN.
  3906.        PROCEDURE B cannot access the variables of PROCEDURE C or PROCEDURE D.
  3907.  
  3908.    4.  PROCEDURE C can access only the variables of PROCEDURE A and MAIN.
  3909.        PROCEDURE C cannot access the variables of PROCEDURE B or PROCEDURE D.
  3910.  
  3911.    5.  PROCEDURE D can access the variables of PROCEDURE C, PROCEDURE A, and
  3912.        MAIN. PROCEDURE D cannot access the variables of PROCEDURE B.
  3913.  
  3914.  ENTER at the beginning of the MAIN PROGRAM creates dynamic storage space
  3915.  for MAIN but copies no pointers. The first and only word in the display
  3916.  points to itself because there is no previous value for LEAVE to return to
  3917.  EBP. See Figure 3-18.
  3918.  
  3919.  After MAIN calls PROCEDURE A, ENTER creates a new display for PROCEDURE A
  3920.  with the first word pointing to the previous value of EBP (BPM for LEAVE to
  3921.  return to the MAIN stack frame) and the second word pointing to the current
  3922.  value of EBP. Procedure A can access variables in MAIN since MAIN is at
  3923.  level 1. Therefore the base for the dynamic storage for MAIN is at [EBP-2].
  3924.  All dynamic variables for MAIN are at a fixed offset from this value. See
  3925.  Figure 3-19.
  3926.  
  3927.  After PROCEDURE A calls PROCEDURE B, ENTER creates a new display for
  3928.  PROCEDURE B with the first word pointing to the previous value of EBP, the
  3929.  second word pointing to the value of EBP for MAIN, and the third word
  3930.  pointing to the value of EBP for A and the last word pointing to the current
  3931.  EBP. B can access variables in A and MAIN by fetching from the display the
  3932.  base addresses of the respective dynamic storage areas. See Figure 3-20.
  3933.  
  3934.  After PROCEDURE B calls PROCEDURE C, ENTER creates a new display for
  3935.  PROCEDURE C with the first word pointing to the previous value of EBP, the
  3936.  second word pointing to the value of EBP for MAIN, and the third word
  3937.  pointing to the EBP value for A and the third word pointing to the current
  3938.  value of EBP. Because PROCEDURE B and PROCEDURE C have the same lexical
  3939.  level, PROCEDURE C is not allowed access to variables in B and therefore
  3940.  does not receive a pointer to the beginning of PROCEDURE B's stack frame.
  3941.  See Figure 3-21.
  3942.  
  3943.  LEAVE (Leave Procedure) reverses the action of the previous ENTER
  3944.  instruction. The LEAVE instruction does not include any operands. LEAVE
  3945.  copies EBP to ESP to release all stack space allocated to the procedure by
  3946.  the most recent ENTER instruction. Then LEAVE pops the old value of EBP from
  3947.  the stack. A subsequent RET instruction can then remove any arguments that
  3948.  were pushed on the stack by the calling program for use by the called
  3949.  procedure.
  3950.  
  3951.  
  3952.  Figure 3-16.  Formal Definition of the ENTER Instruction
  3953.  
  3954.  The formal definition of the ENTER instruction for all cases is given by the
  3955.  following listing. LEVEL denotes the value of the second operand.
  3956.  
  3957.  Push EBP
  3958.  Set a temporary value FRAME_PTR := ESP
  3959.  If LEVEL > 0 then
  3960.        Repeat (LEVEL-1) times:
  3961.            EBP :=EBP - 4
  3962.            Push the doubleword pointed to by EBP
  3963.        End repeat
  3964.        Push FRAME_PTR
  3965.  End if
  3966.  EBP := FRAME_PTR
  3967.  ESP := ESP - first operand.
  3968.  
  3969.  
  3970.  Figure 3-17.  Variable Access in Nested Procedures
  3971.  
  3972.        ╔════════════════════════════════════════════════════════════════╗
  3973.        ║                MAIN PROCEDURE (LEXICAL LEVEL 1)                ║
  3974.        ║   ╔════════════════════════════════════════════════════════╗   ║
  3975.        ║   ║              PROCEDURE A (LEXICAL LEVEL 2)             ║   ║
  3976.        ║   ║  ╔══════════════════════════════════════════════════╗  ║   ║
  3977.        ║   ║  ║           PROCEDURE B (LEXICAL LEVEL 3)          ║  ║   ║
  3978.        ║   ║  ╚══════════════════════════════════════════════════╝  ║   ║
  3979.        ║   ║                                                        ║   ║
  3980.        ║   ║  ╔══════════════════════════════════════════════════╗  ║   ║
  3981.        ║   ║  ║           PROCEDURE C (LEXICAL LEVEL 3)          ║  ║   ║
  3982.        ║   ║  ║  ╔════════════════════════════════════════════╗  ║  ║   ║
  3983.        ║   ║  ║  ║        PROCEDURE D (LEXICAL LEVEL 4)       ║  ║  ║   ║
  3984.        ║   ║  ║  ╚════════════════════════════════════════════╝  ║  ║   ║
  3985.        ║   ║  ║                                                  ║  ║   ║
  3986.        ║   ║  ╚══════════════════════════════════════════════════╝  ║   ║
  3987.        ║   ║                                                        ║   ║
  3988.        ║   ╚════════════════════════════════════════════════════════╝   ║
  3989.        ║                                                                ║
  3990.        ╚════════════════════════════════════════════════════════════════╝
  3991.  
  3992.  
  3993.  Figure 3-18.  Stack Frame for MAIN at Level 1
  3994.  
  3995.                                        31          0 
  3996.                  D  O                 ║               ║
  3997.                  I  F              ┌─ ╠═══════╪═══════╣
  3998.                  R                 │  ║    OLD ESP    ║
  3999.                  E  E     DISPLAY ─┤  ╠═══════╪═══════╣──EBP FOR
  4000.                  C  X              │  ║      EBPM    ║    MAIN
  4001.                  T  P              ╞═ ╠═══════╪═══════╣
  4002.                  I  A              │  ║               ║
  4003.                  O  N              │  ╠═══════╪═══════╣
  4004.                  N  S     DYNAMIC ─┤  ║               ║
  4005.                     I     STORAGE  │  ╠═══════╪═══════╣
  4006.                   │ O              │  ║               ║
  4007.                   │ N              └─ ╠═══════╪═══════╣──ESP
  4008.                   │                   ║               ║
  4009.                                                     
  4010.  
  4011.  
  4012.  Figure 3-19.  Stack Frame for Procedure A
  4013.  
  4014.                                        31          0 
  4015.                  D  O                 ║               ║
  4016.                  I  F                 ╠═══════╪═══════╣
  4017.                  R                    ║    OLD ESP    ║
  4018.                  E  E                 ╠═══════╪═══════╣
  4019.                  C  X                 ║      EBPM    ║
  4020.                  T  P                 ╠═══════╪═══════╣
  4021.                  I  A                 ║               ║
  4022.                  O  N                 ╠═══════╪═══════╣
  4023.                  N  S                 ║               ║
  4024.                     I                 ╠═══════╪═══════╣
  4025.                   │ O                 ║               ║
  4026.                   │ N              ┌─ ╠═══════╪═══════╣
  4027.                   │                │  ║      EBPM     ║
  4028.                                   │  ╠═══════╪═══════╣──EBP FOR A
  4029.                           DISPLAY ─┤  ║      EBPM     ║
  4030.                                    │  ╠═══════╪═══════╣
  4031.                                    │  ║      EBPA    ║
  4032.                                    ╞═ ╠═══════╪═══════╣
  4033.                                    │  ║               ║
  4034.                                    │  ╠═══════╪═══════╣
  4035.                           DYNAMIC ─┤  ║               ║
  4036.                           STORAGE  │  ╠═══════╪═══════╣
  4037.                                    │  ║               ║
  4038.                                    └─ ╠═══════╪═══════╣──ESP
  4039.                                       ║               ║
  4040.                                                      
  4041.  
  4042.  
  4043.  Figure 3-20.  Stack Frame for Procedure B at Level 3 Called from A
  4044.  
  4045.                                        31          0 
  4046.                  D  O                 ║               ║
  4047.                  I  F                 ╠═══════╪═══════╣
  4048.                  R                    ║    OLD ESP    ║
  4049.                  E  E                 ╠═══════╪═══════╣
  4050.                  C  X                 ║      EBPM    ║
  4051.                  T  P                 ╠═══════╪═══════╣
  4052.                  I  A                 ║               ║
  4053.                  O  N                 ╠═══════╪═══════╣
  4054.                  N  S                 ║               ║
  4055.                     I                 ╠═══════╪═══════╣
  4056.                   │ O                 ║               ║
  4057.                   │ N                 ╠═══════╪═══════╣
  4058.                   │                   ║      EBPM     ║
  4059.                                      ╠═══════╪═══════╣
  4060.                                       ║      EBPM     ║
  4061.                                       ╠═══════╪═══════╣
  4062.                                       ║      EBPA     ║
  4063.                                       ╠═══════╪═══════╣
  4064.                                       ║               ║
  4065.                                       ╠═══════╪═══════╣
  4066.                                       ║               ║
  4067.                                       ╠═══════╪═══════╣
  4068.                                       ║               ║
  4069.                                    ┌─ ╠═══════╪═══════╣
  4070.                                    │  ║      EBPA     ║
  4071.                                    │  ╠═══════╪═══════╣──EBP
  4072.                                    │  ║      EBPM     ║
  4073.                           DISPLAY ─┤  ╠═══════╪═══════╣
  4074.                                    │  ║      EBPA     ║
  4075.                                    │  ╠═══════╪═══════╣
  4076.                                    │  ║      EBPB    ║
  4077.                                    ╞═ ╠═══════╪═══════╣
  4078.                                    │  ║               ║
  4079.                                    │  ╠═══════╪═══════╣
  4080.                           DYNAMIC ─┤  ║               ║
  4081.                           STORAGE  │  ╠═══════╪═══════╣
  4082.                                    │  ║               ║
  4083.                                    └─ ╠═══════╪═══════╣──ESP
  4084.                                       ║               ║
  4085.                                                      
  4086.  
  4087.  
  4088.  Figure 3-21.  Stack Frame for Procedure C at Level 3 Called from B
  4089.  
  4090.                                        31          0 
  4091.                  D  O                 ║               ║
  4092.                  I  F                 ╠═══════╪═══════╣
  4093.                  R                    ║    OLD ESP    ║
  4094.                  E  E                 ╠═══════╪═══════╣
  4095.                  C  X                 ║      EBPM    ║
  4096.                  T  P                 ╠═══════╪═══════╣
  4097.                  I  A                 ║               ║
  4098.                  O  N                 ╠═══════╪═══════╣
  4099.                  N  S                 ║               ║
  4100.                     I                 ╠═══════╪═══════╣
  4101.                   │ O                 ║               ║
  4102.                   │ N                 ╠═══════╪═══════╣
  4103.                   │                   ║      EBPM     ║
  4104.                                      ╠═══════╪═══════╣
  4105.                                       ║      EBPM     ║
  4106.                                       ╠═══════╪═══════╣
  4107.                                       ║      EBPA    ║
  4108.                                       ╠═══════╪═══════╣
  4109.                                       ║               ║
  4110.                                       ╠═══════╪═══════╣
  4111.                                       ║               ║
  4112.                                       ╠═══════╪═══════╣
  4113.                                       ║               ║
  4114.                                    ┌─ ╠═══════╪═══════╣
  4115.                                    │  ║      EBPA     ║
  4116.                                    │  ╠═══════╪═══════╣──EBP
  4117.                                    │  ║      EBPM     ║
  4118.                           DISPLAY ─┤  ╠═══════╪═══════╣
  4119.                                    │  ║      EBPA     ║
  4120.                                    │  ╠═══════╪═══════╣
  4121.                                    │  ║      EBPB    ║
  4122.                                    ╞═ ╠═══════╪═══════╣
  4123.                                    │  ║               ║
  4124.                                    │  ╠═══════╪═══════╣
  4125.                           DYNAMIC ─┤  ║               ║
  4126.                           STORAGE  │  ╠═══════╪═══════╣
  4127.                                    │  ║               ║
  4128.                                    └─ ╠═══════╪═══════╣──ESP
  4129.                                       ║               ║
  4130.                                                      
  4131.  
  4132.  
  4133.  3.8  Flag Control Instructions
  4134.  
  4135.  The flag control instructions provide a method for directly changing the
  4136.  state of bits in the flag register.
  4137.  
  4138.  
  4139.  3.8.1  Carry and Direction Flag Control Instructions
  4140.  
  4141.  The carry flag instructions are useful in conjunction with
  4142.  rotate-with-carry instructions RCL and RCR. They can initialize the carry
  4143.  flag, CF, to a known state before execution of a rotate that moves the carry
  4144.  bit into one end of the rotated operand.
  4145.  
  4146.  The direction flag control instructions are specifically included to set or
  4147.  clear the direction flag, DF, which controls the left-to-right or
  4148.  right-to-left direction of string processing. If DF=0, the processor
  4149.  automatically increments the string index registers, ESI and EDI, after each
  4150.  execution of a string primitive. If DF=1, the processor decrements these
  4151.  index registers. Programmers should use one of these instructions before any
  4152.  procedure that uses string instructions to insure that DF is set properly.
  4153.  
  4154.  Flag Control Instruction                  Effect
  4155.  
  4156.  STC (Set Carry Flag)                      CF  1
  4157.  CLC (Clear Carry Flag)                    CF  0
  4158.  CMC (Complement Carry Flag)               CF  NOT (CF)
  4159.  CLD (Clear Direction Flag)                DF  0
  4160.  STD (Set Direction Flag)                  DF  1
  4161.  
  4162.  
  4163.  3.8.2  Flag Transfer Instructions
  4164.  
  4165.  Though specific instructions exist to alter CF and DF, there is no direct
  4166.  method of altering the other applications-oriented flags. The flag transfer
  4167.  instructions allow a program to alter the other flag bits with the bit
  4168.  manipulation instructions after transferring these flags to the stack or the
  4169.  AH register.
  4170.  
  4171.  The instructions LAHF and SAHF deal with five of the status flags, which
  4172.  are used primarily by the arithmetic and logical instructions.
  4173.  
  4174.  LAHF (Load AH from Flags) copies SF, ZF, AF, PF, and CF to AH bits 7, 6, 4,
  4175.  2, and 0, respectively (see Figure 3-22). The contents of the remaining bits
  4176.  (5, 3, and 1) are undefined. The flags remain unaffected.
  4177.  
  4178.  SAHF (Store AH into Flags) transfers bits 7, 6, 4, 2, and 0 from AH into
  4179.  SF, ZF, AF, PF, and CF, respectively (see Figure 3-22).
  4180.  
  4181.  The PUSHF and POPF instructions are not only useful for storing the flags
  4182.  in memory where they can be examined and modified but are also useful for
  4183.  preserving the state of the flags register while executing a procedure.
  4184.  
  4185.  PUSHF (Push Flags) decrements ESP by two and then transfers the low-order
  4186.  word of the flags register to the word at the top of stack pointed to by ESP
  4187.  (see Figure 3-23). The variant PUSHFD decrements ESP by four, then
  4188.  transfers both words of the extended flags register to the top of the stack
  4189.  pointed to by ESP (the VM and RF flags are not moved, however).
  4190.  
  4191.  POPF (Pop Flags) transfers specific bits from the word at the top of stack
  4192.  into the low-order byte of the flag register (see Figure 3-23), then
  4193.  increments ESP by two. The variant POPFD transfers specific bits from the
  4194.  doubleword at the top of the stack into the extended flags register (the RF
  4195.  and VM flags are not changed, however), then increments ESP by four.
  4196.  
  4197.  
  4198.  Figure 3-22.  LAHF and SAHF
  4199.  
  4200.                       7    6    5    4    3    2    1    0
  4201.                     ╔════╦════╦════╦════╦════╦════╦════╦════╗
  4202.                     ║ SF ║ ZF ║ UU ║ AF ║ UU ║ PF ║ UU ║ CF ║
  4203.                     ╚════╩════╩════╩════╩════╩════╩════╩════╝
  4204.  
  4205.       LAHF LOADS FIVE FLAGS FROM THE FLAG REGISTER INTO REGISTER AH. SAHF
  4206.       STORES THESE SAME FIVE FLAGS FROM AH INTO THE FLAG REGISTER. THE BIT
  4207.       POSITION OF EACH FLAG IS THE SAME IN AH AS IT IS IN THE FLAG REGISTER.
  4208.       THE REMAINING BITS (MARKED UU) ARE RESERVED; DO NOT DEFINE.
  4209.  
  4210.  
  4211.  3.9  Coprocessor Interface Instructions
  4212.  
  4213.  A numerics coprocessor (e.g., the 80387 or 80287) provides an extension to
  4214.  the instruction set of the base architecture. The coprocessor extends the
  4215.  instruction set of the base architecture to support high-precision integer
  4216.  and floating-point calculations. This extended instruction set includes
  4217.  arithmetic, comparison, transcendental, and data transfer instructions. The
  4218.  coprocessor also contains a set of useful constants to enhance the speed of
  4219.  numeric calculations.
  4220.  
  4221.  A program contains instructions for the coprocessor in line with the
  4222.  instructions for the CPU. The system executes these instructions in the same
  4223.  order as they appear in the instruction stream. The coprocessor operates
  4224.  concurrently with the CPU to provide maximum throughput for numeric
  4225.  calculations.
  4226.  
  4227.  The 80386 also has features to support emulation of the numerics
  4228.  coprocessor when the coprocessor is absent. The software emulation of the
  4229.  coprocessor is transparent to application software but requires more time
  4230.  for execution. Refer to Chapter 11 for more information on coprocessor
  4231.  emulation.
  4232.  
  4233.  ESC (Escape) is a 5-bit sequence that begins the opcodes that identify
  4234.  floating point numeric instructions. The ESC pattern tells the 80386 to send
  4235.  the opcode and addresses of operands to the numerics coprocessor. The
  4236.  numerics coprocessor uses the escape instructions to perform
  4237.  high-performance, high-precision floating point arithmetic that conforms to
  4238.  the IEEE floating point standard 754.
  4239.  
  4240.  WAIT (Wait) is an 80386 instruction that suspends program execution until
  4241.  the 80386 CPU detects that the BUSY pin is inactive. This condition
  4242.  indicates that the coprocessor has completed its processing task and that
  4243.  the CPU may obtain the results.
  4244.  
  4245.  
  4246.  Figure 3-23.  Flag Format for PUSHF and POPF
  4247.  
  4248.                                  PUSHFD/POPFD
  4249.       ┌───────────────────────────────┴────────────────────────────────┐
  4250.                                                    PUSHF/POPF
  4251.                                       ┌────────────────┴───────────────┐
  4252.        31              23               15                7           0
  4253.       ╔═══════════════╪═══════════╤═╤═╤═╤═╤════╤═╤═╤═╤═╤═╤═╤═╤═╤═╤═╤═╤═╗
  4254.       ║                           │V│R│ │N│ID  │O│D│I│T│S│Z│ │A│ │P│ │C║
  4255.       ║0 0 0 0 0 0 0 0 0 0 0 0 0 0│ │ │0│ │    │ │ │ │ │ │ │0│ │0│ │1│ ║
  4256.       ║                           │M│F│ │T│  PL│F│F│F│F│F│F│ │F│ │F│ │F║
  4257.       ╚═══════════════╪═══════════╧═╧═╧═╧═╧════╧═╧═╧═╧═╧═╧═╧═╧═╧═╧═╧═╧═╝
  4258.  
  4259.       BITS MARKED 0 AND 1 ARE RESERVED BY INTEL. DO NOT DEFINE.
  4260.  
  4261.     SYSTEMS FLAGS (INCLUDING THE IOPL FIELD, AND THE VM, RF, AND IF FLAGS)
  4262.     ARE PUSHED AND ARE VISIBLE TO APPLICATIONS PROGRAMS. HOWEVER, WHEN AN
  4263.     APPLICATIONS PROGRAM POPS THE FLAGS, THESE ITEMS ARE NOT CHANGED,
  4264.     REGARDLESS OF THE VALUES POPPED INTO THEM.
  4265.  
  4266.  
  4267.  3.10  Segment Register Instructions
  4268.  
  4269.  This category actually includes several distinct types of instructions.
  4270.  These various types are grouped together here because, if systems designers
  4271.  choose an unsegmented model of memory organization, none of these
  4272.  instructions is used by applications programmers. The instructions that deal
  4273.  with segment registers are:
  4274.  
  4275.  1.  Segment-register transfer instructions.
  4276.  
  4277.     MOV SegReg, ...
  4278.     MOV ..., SegReg
  4279.     PUSH SegReg
  4280.     POP SegReg
  4281.  
  4282.  2.  Control transfers to another executable segment.
  4283.  
  4284.     JMP far    ; direct and indirect
  4285.     CALL far
  4286.     RET far
  4287.  
  4288.  3.  Data pointer instructions.
  4289.  
  4290.     LDS
  4291.     LES
  4292.     LFS
  4293.     LGS
  4294.     LSS
  4295.  
  4296.  Note that the following interrupt-related instructions are different; all
  4297.  are capable of transferring control to another segment, but the use of
  4298.  segmentation is not apparent to the applications programmer.
  4299.  
  4300.  INT n
  4301.  INTO
  4302.  BOUND
  4303.  IRET
  4304.  
  4305.  
  4306.  3.10.1  Segment-Register Transfer Instructions
  4307.  
  4308.  The MOV, POP, and PUSH instructions also serve to load and store segment
  4309.  registers. These variants operate similarly to their general-register
  4310.  counterparts except that one operand can be a segment register. MOV cannot
  4311.  move segment register to a segment register. Neither POP nor MOV can place a
  4312.  value in the code-segment register CS; only the far control-transfer
  4313.  instructions can change CS.
  4314.  
  4315.  
  4316.  3.10.2  Far Control Transfer Instructions
  4317.  
  4318.  The far control-transfer instructions transfer control to a location in
  4319.  another segment by changing the content of the CS register.
  4320.  
  4321.  Direct far JMP. Direct JMP instructions that specify a target location
  4322.  outside the current code segment contain a far pointer. This pointer
  4323.  consists of a selector for the new code segment and an offset within the new
  4324.  segment.
  4325.  
  4326.  Indirect far JMP. Indirect JMP instructions that specify a target location
  4327.  outside the current code segment use a 48-bit variable to specify the far
  4328.  pointer.
  4329.  
  4330.  Far CALL. An intersegment CALL places both the value of EIP and CS on the
  4331.  stack.
  4332.  
  4333.  Far RET. An intersegment RET restores the values of both CS and EIP which
  4334.  were saved on the stack by the previous intersegment CALL instruction.
  4335.  
  4336.  
  4337.  3.10.3  Data Pointer Instructions
  4338.  
  4339.  The data pointer instructions load a pointer (consisting of a segment
  4340.  selector and an offset) to a segment register and a general register.
  4341.  
  4342.  LDS (Load Pointer Using DS) transfers a pointer variable from the source
  4343.  operand to DS and the destination register. The source operand must be a
  4344.  memory operand, and the destination operand must be a general register. DS
  4345.  receives the segment-selector of the pointer. The destination register
  4346.  receives the offset part of the pointer, which points to a specific location
  4347.  within the segment.
  4348.  
  4349.  Example: LDS ESI, STRING_X
  4350.  
  4351.  Loads DS with the selector identifying the segment pointed to by a
  4352.  STRING_X, and loads the offset of STRING_X into ESI.  Specifying ESI as the
  4353.  destination operand is a convenient way to prepare for a string operation on
  4354.  a source string that is not in the current data segment.
  4355.  
  4356.  LES (Load Pointer Using ES) operates identically to LDS except that ES
  4357.  receives the segment selector rather than DS.
  4358.  
  4359.  Example: LES EDI, DESTINATION_X
  4360.  
  4361.  Loads ES with the selector identifying the segment pointed to by
  4362.  DESTINATION_X, and loads the offset of DESTINATION_X into EDI. This
  4363.  instruction provides a convenient way to select a destination for a string
  4364.  operation if the desired location is not in the current extra segment.
  4365.  
  4366.  LFS (Load Pointer Using FS) operates identically to LDS except that FS
  4367.  receives the segment selector rather than DS.
  4368.  
  4369.  LGS (Load Pointer Using GS) operates identically to LDS except that GS
  4370.  receives the segment selector rather than DS.
  4371.  
  4372.  LSS (Load Pointer Using SS) operates identically to LDS except that SS
  4373.  receives the segment selector rather than DS.  This instruction is
  4374.  especially important, because it allows the two registers that identify the
  4375.  stack (SS:ESP) to be changed in one uninterruptible operation.  Unlike the
  4376.  other instructions which load SS, interrupts are not inhibited at the end
  4377.  of the LSS instruction.  The other instructions (e.g., POP SS) inhibit
  4378.  interrupts to permit the following instruction to load ESP, thereby forming
  4379.  an indivisible load of SS:ESP.  Since both SS and ESP can be loaded by LSS,
  4380.  there is no need to inhibit interrupts.
  4381.  
  4382.  
  4383.  3.11  Miscellaneous Instructions
  4384.  
  4385.  The following instructions do not fit in any of the previous categories,
  4386.  but are nonetheless useful.
  4387.  
  4388.  
  4389.  3.11.1  Address Calculation Instruction
  4390.  
  4391.  LEA (Load Effective Address) transfers the offset of the source operand
  4392.  (rather than its value) to the destination operand.  The source operand must
  4393.  be a memory operand, and the destination operand must be a general register.
  4394.  This instruction is especially useful for initializing registers before the
  4395.  execution of the string primitives (ESI, EDI) or the XLAT instruction (EBX).
  4396.  The LEA can perform any indexing or scaling that may be needed.
  4397.  
  4398.  Example: LEA EBX, EBCDIC_TABLE
  4399.  
  4400.  Causes the processor to place the address of the starting location of the
  4401.  table labeled EBCDIC_TABLE into EBX.
  4402.  
  4403.  
  4404.  3.11.2  No-Operation Instruction
  4405.  
  4406.  NOP (No Operation) occupies a byte of storage but affects nothing but the
  4407.  instruction pointer, EIP.
  4408.  
  4409.  
  4410.  3.11.3  Translate Instruction
  4411.  
  4412.  XLAT (Translate) replaced a byte in the AL register with a byte from a
  4413.  user-coded translation table. When XLAT is executed, AL should have the
  4414.  unsigned index to the table addressed by EBX. XLAT changes the contents of
  4415.  AL from table index to table entry. EBX is unchanged. The XLAT instruction
  4416.  is useful for translating from one coding system to another such as from
  4417.  ASCII to EBCDIC.  The translate table may be up to 256 bytes long.  The
  4418.  value placed in the AL register serves as an index to the location of the
  4419.  corresponding translation value.
  4420.  
  4421.  
  4422.                          PART II  SYSTEMS PROGRAMMING
  4423.  
  4424.  
  4425.  Chapter 4  Systems Architecture
  4426.  
  4427.  ────────────────────────────────────────────────────────────────────────────
  4428.  
  4429.  Many of the architectural features of the 80386 are used only by systems
  4430.  programmers. This chapter presents an overview of these aspects of the
  4431.  architecture.
  4432.  
  4433.  The systems-level features of the 80386 architecture include:
  4434.  
  4435.    Memory Management
  4436.    Protection
  4437.    Multitasking
  4438.    Input/Output
  4439.    Exceptions and Interrupts
  4440.    Initialization
  4441.    Coprocessing and Multiprocessing
  4442.    Debugging
  4443.  
  4444.  These features are implemented by registers and instructions, all of which
  4445.  are introduced in the following sections. The purpose of this chapter is not
  4446.  to explain each feature in detail, but rather to place the remaining
  4447.  chapters of Part II in perspective. Each mention in this chapter of a
  4448.  register or instruction is either accompanied by an explanation or a
  4449.  reference to a following chapter where detailed information can be obtained.
  4450.  
  4451.  
  4452.  4.1  Systems Registers
  4453.  
  4454.  The registers designed for use by systems programmers fall into these
  4455.  classes:
  4456.  
  4457.    EFLAGS
  4458.    Memory-Management Registers
  4459.    Control Registers
  4460.    Debug Registers
  4461.    Test Registers
  4462.  
  4463.  
  4464.  4.1.1  Systems Flags
  4465.  
  4466.  The systems flags of the EFLAGS register control I/O, maskable interrupts,
  4467.  debugging, task switching, and enabling of virtual 8086 execution in a
  4468.  protected, multitasking environment. These flags are highlighted in Figure
  4469.  4-1.
  4470.  
  4471.  IF (Interrupt-Enable Flag, bit 9)
  4472.  
  4473.     Setting IF allows the CPU to recognize external (maskable) interrupt
  4474.     requests. Clearing IF disables these interrupts. IF has no effect on
  4475.     either exceptions or nonmaskable external interrupts. Refer to Chapter
  4476.     9 for more details about interrupts.
  4477.  
  4478.  NT (Nested Task, bit 14)
  4479.  
  4480.     The processor uses the nested task flag to control chaining of
  4481.     interrupted and called tasks. NT influences the operation of the IRET
  4482.     instruction. Refer to Chapter 7 and Chapter 9 for more information on
  4483.     nested tasks.
  4484.  
  4485.  RF (Resume Flag, bit 16)
  4486.  
  4487.     The RF flag temporarily disables debug exceptions so that an instruction
  4488.     can be restarted after a debug exception without immediately causing
  4489.     another debug exception. Refer to Chapter 12 for details.
  4490.  
  4491.  TF (Trap Flag, bit 8)
  4492.  
  4493.     Setting TF puts the processor into single-step mode for debugging. In
  4494.     this mode, the CPU automatically generates an exception after each
  4495.     instruction, allowing a program to be inspected as it executes each
  4496.     instruction. Single-stepping is just one of several debugging features of
  4497.     the 80386. Refer to Chapter 12 for additional information.
  4498.  
  4499.  VM (Virtual 8086 Mode, bit 17)
  4500.  
  4501.     When set, the VM flag indicates that the task is executing an 8086
  4502.     program. Refer to Chapter 14 for a detailed discussion of how the 80386
  4503.     executes 8086 tasks in a protected, multitasking environment.
  4504.  
  4505.  
  4506.  Figure 4-1.  System Flags of EFLAGS Register
  4507.  
  4508.        31              23               15                7           0
  4509.       ╔═══════════════╪═══════════╤═╤═╤╪╤═╤════╤═╤═╤═╤═╤╪╤═╤═╤═╤═╤═╤═╤═╗
  4510.       ║▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒│V│R│▒│N│ID  │O│D│I│T│S│Z│▒│A│▒│P│▒│C║
  4511.       ║0 0 0 0 0 0 0 0 0 0 0 0 0 0│ │ │0│ │    │▒│▒│ │▒│▒│▒│0│▒│0│▒│1│▒║
  4512.       ║▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒│M│F│▒│T│  PL│F│F│F│F│F│F│▒│F│▒│F│▒│F║
  4513.       ╚═══════════════╪═══════════╧╤╧╤╧╪╧╤╧═╤══╧═╧═╧╤╧═╧╪╧═╧═╧═╧═╧═╧═╧═╝
  4514.                                    │ │   │  │       │
  4515.               VIRTUAL 8086 MODE────┘ │   │  │       │
  4516.                     RESUME FLAG──────┘   │  │       │
  4517.                NESTED TASK FLAG──────────┘  │       │
  4518.             I/O PRIVILEGE LEVEL─────────────┘       │
  4519.                INTERRUPT ENABLE─────────────────────┘
  4520.  
  4521.  ────────────────────────────────────────────────────────────────────────────
  4522.  NOTE
  4523.        0 OR 1 INDICATES INTEL RESERVED. DO NOT DEFINE.
  4524.  ────────────────────────────────────────────────────────────────────────────
  4525.  
  4526.  
  4527.  4.1.2  Memory-Management Registers
  4528.  
  4529.  Four registers of the 80386 locate the data structures that control
  4530.  segmented memory management:
  4531.  
  4532.  GDTR    Global Descriptor Table Register
  4533.  LDTR    Local Descriptor Table Register
  4534.  
  4535.     These registers point to the segment descriptor tables GDT and LDT.
  4536.     Refer to Chapter 5 for an explanation of addressing via descriptor
  4537.     tables.
  4538.  
  4539.  IDTR    Interrupt Descriptor Table Register
  4540.  
  4541.     This register points to a table of entry points for interrupt handlers
  4542.     (the IDT). Refer to Chapter 9 for details of the interrupt mechanism.
  4543.  
  4544.  TR      Task Register
  4545.  
  4546.     This register points to the information needed by the processor to define
  4547.     the current task. Refer to Chapter 7 for a description of the
  4548.     multitasking features of the 80386.
  4549.  
  4550.  
  4551.  4.1.3  Control Registers
  4552.  
  4553.  Figure 4-2 shows the format of the 80386 control registers CR0, CR2, and
  4554.  CR3. These registers are accessible to systems programmers only via variants
  4555.  of the MOV instruction, which allow them to be loaded from or stored in
  4556.  general registers; for example:
  4557.  
  4558.  MOV EAX, CR0
  4559.  MOV CR3, EBX
  4560.  
  4561.  CR0 contains system control flags, which control or indicate conditions
  4562.  that apply to the system as a whole, not to an individual task.
  4563.  
  4564.  EM (Emulation, bit 2)
  4565.  
  4566.     EM indicates whether coprocessor functions are to be emulated. Refer to
  4567.     Chapter 11 for details.
  4568.  
  4569.  ET (Extension Type, bit 4)
  4570.  
  4571.     ET indicates the type of coprocessor present in the system (80287 or
  4572.     80387). Refer to Chapter 11 and Chapter 10 for details.
  4573.  
  4574.  MP (Math Present, bit 1)
  4575.  
  4576.     MP controls the function of the WAIT instruction, which is used to
  4577.     coordinate a coprocessor. Refer to Chapter 11 for details.
  4578.  
  4579.  PE (Protection Enable, bit 0)
  4580.  
  4581.     Setting PE causes the processor to begin executing in protected mode.
  4582.     Resetting PE returns to real-address mode. Refer to Chapter 14 and
  4583.     Chapter 10 for more information on changing processor modes.
  4584.  
  4585.  PG (Paging, bit 31)
  4586.  
  4587.     PG indicates whether the processor uses page tables to translate linear
  4588.     addresses into physical addresses. Refer to Chapter 5 for a description
  4589.     of page translation; refer to Chapter 10 for a discussion of how to set
  4590.     PG.
  4591.  
  4592.  TS (Task Switched, bit 3)
  4593.  
  4594.     The processor sets TS with every task switch and tests TS when
  4595.     interpreting coprocessor instructions. Refer to Chapter 11 for details.
  4596.  
  4597.  CR2 is used for handling page faults when PG is set. The processor stores
  4598.  in CR2 the linear address that triggers the fault. Refer to Chapter 9 for a
  4599.  description of page-fault handling.
  4600.  
  4601.  CR3 is used when PG is set. CR3 enables the processor to locate the page
  4602.  table directory for the current task. Refer to Chapter 5 for a description
  4603.  of page tables and page translation.
  4604.  
  4605.  
  4606.  Figure 4-2.  Control Registers
  4607.  
  4608.   31                23                15                7               0
  4609.  ╔═════════════════╪═════════════════╪════════╦════════╪═════════════════╗
  4610.  ║                                            ║                          ║
  4611.  ║    PAGE DIRECTORY BASE REGISTER (PDBR)     ║         RESERVED         ║CR3
  4612.  ╟────────────────────────────────────────────╨──────────────────────────╢
  4613.  ║                                                                       ║
  4614.  ║                       PAGE FAULT LINEAR ADDRESS                       ║CR2
  4615.  ╟───────────────────────────────────────────────────────────────────────╢
  4616.  ║                                                                       ║
  4617.  ║                                RESERVED                               ║CR1
  4618.  ╟─┬───────────────────────────────────────────────────────────┬─┬─┬─┬─┬─╢
  4619.  ║P│                                                           │E│T│E│M│P║
  4620.  ║G│                              RESERVED                     │T│S│M│P│E║CR0
  4621.  ╚═╧═══════════════╪═════════════════╪═════════════════╪═══════╧═╧═╧═╧═╧═╝
  4622.  
  4623.  
  4624.  4.1.4  Debug Register
  4625.  
  4626.  The debug registers bring advanced debugging abilities to the 80386,
  4627.  including data breakpoints and the ability to set instruction breakpoints
  4628.  without modifying code segments. Refer to Chapter 12 for a complete
  4629.  description of formats and usage.
  4630.  
  4631.  
  4632.  4.1.5  Test Registers
  4633.  
  4634.  The test registers are not a standard part of the 80386 architecture. They
  4635.  are provided solely to enable confidence testing of the translation
  4636.  lookaside buffer (TLB), the cache used for storing information from page
  4637.  tables. Chapter 12 explains how to use these registers.
  4638.  
  4639.  
  4640.  4.2  Systems Instructions
  4641.  
  4642.  Systems instructions deal with such functions as:
  4643.  
  4644.    1.  Verification of pointer parameters (refer to Chapter 6):
  4645.  
  4646.      ARPL             ── Adjust RPL
  4647.      LAR              ── Load Access Rights
  4648.      LSL              ── Load Segment Limit
  4649.      VERR             ── Verify for Reading
  4650.      VERW             ── Verify for Writing
  4651.  
  4652.    2.  Addressing descriptor tables (refer to Chaper 5):
  4653.  
  4654.      LLDT             ── Load LDT Register
  4655.      SLDT             ── Store LDT Register
  4656.      LGDT             ── Load GDT Register
  4657.      SGDT             ── Store GDT Register
  4658.  
  4659.    3.  Multitasking (refer to Chapter 7):
  4660.  
  4661.      LTR              ── Load Task Register
  4662.      STR              ── Store Task Register
  4663.  
  4664.    4. Coprocessing and Multiprocessing (refer to Chapter 11):
  4665.  
  4666.      CLTS             ── Clear Task-Switched Flag
  4667.      ESC              ── Escape instructions
  4668.      WAIT             ── Wait until Coprocessor not Busy
  4669.      LOCK             ── Assert Bus-Lock Signal
  4670.  
  4671.    5.  Input and Output (refer to Chapter 8):
  4672.  
  4673.      IN               ── Input
  4674.      OUT              ── Output
  4675.      INS              ── Input String
  4676.      OUTS             ── Output String
  4677.  
  4678.    6.  Interrupt control (refer to Chapter 9):
  4679.  
  4680.      CLI              ── Clear Interrupt-Enable Flag
  4681.      STI              ── Set Interrupt-Enable Flag
  4682.      LIDT             ── Load IDT Register
  4683.      SIDT             ── Store IDT Register
  4684.  
  4685.    7.  Debugging (refer to Chapter 12):
  4686.  
  4687.      MOV              ── Move to and from debug registers
  4688.  
  4689.    8.  TLB testing (refer to Chapter 10):
  4690.  
  4691.      MOV              ── Move to and from test registers
  4692.  
  4693.    9.  System Control:
  4694.  
  4695.      SMSW             ── Set MSW
  4696.      LMSW             ── Load MSW
  4697.      HLT              ── Halt Processor
  4698.      MOV              ── Move to and from control registers
  4699.  
  4700.  The instructions SMSW and LMSW are provided for compatibility with the
  4701.  80286 processor.  80386 programs access the MSW in CR0 via variants of the
  4702.  MOV instruction.  HLT stops the processor until receipt of an INTR or RESET
  4703.  signal.
  4704.  
  4705.  In addition to the chapters cited above, detailed information about each of
  4706.  these instructions can be found in the instruction reference chapter,
  4707.  Chapter 17.
  4708.  
  4709.  
  4710.  Chapter 5  Memory Management
  4711.  
  4712.  ────────────────────────────────────────────────────────────────────────────
  4713.  
  4714.  The 80386 transforms logical addresses (i.e., addresses as viewed by
  4715.  programmers) into physical address (i.e., actual addresses in physical
  4716.  memory) in two steps:
  4717.  
  4718.    ■  Segment translation, in which a logical address (consisting of a
  4719.       segment selector and segment offset) are converted to a linear address.
  4720.  
  4721.    ■  Page translation, in which a linear address is converted to a physical
  4722.       address. This step is optional, at the discretion of systems-software
  4723.       designers.
  4724.  
  4725.  These translations are performed in a way that is not visible to
  4726.  applications programmers. Figure 5-1 illustrates the two translations at a
  4727.  high level of abstraction.
  4728.  
  4729.  Figure 5-1 and the remainder of this chapter present a simplified view of
  4730.  the 80386 addressing mechanism. In reality, the addressing mechanism also
  4731.  includes memory protection features. For the sake of simplicity, however,
  4732.  the subject of protection is taken up in another chapter, Chapter 6.
  4733.  
  4734.  
  4735.  Figure 5-1.  Address Translation Overview
  4736.  
  4737.               15           0      31                           0
  4738.      LOGICAL ╔═══════════════╗   ╔══════════════════════════════╗
  4739.      ADDRESS ║    SELECTOR   ║   ║            OFFSET            ║
  4740.              ╚═══════════════╝   ╚═══╤══════════════════════════╝
  4741.                                      
  4742.                       ╔══════════════════════════════╗
  4743.                       ║     SEGMENT TRANSLATION      ║
  4744.                       ╚══════════════╤═══════════════╝
  4745.                                   ╔══╧═╗       PAGING ENABLED
  4746.                                   ║PG ?╟────────────────────┐
  4747.                                   ╚══╤═╝                    │
  4748.                     31        PAGING  DISABLED       0     │
  4749.            LINEAR  ╔═══════════╦═══════════╦═══════════╗    │
  4750.            ADDRESS ║    DIR    ║   PAGE    ║  OFFSET   ║    │
  4751.                    ╚═══════════╩═════╤═════╩═══════════╝    │
  4752.                                                            │
  4753.                       ╔══════════════════════════════╗      │
  4754.                       ║       PAGE TRANSLATION       ║      │
  4755.                       ╚══════════════╤═══════════════╝      │
  4756.                                      │─────────────────────┘
  4757.                        31                          0
  4758.              PHYSICAL ╔══════════════════════════════╗
  4759.              ADDRESS  ║                              ║
  4760.                       ╚══════════════════════════════╝
  4761.  
  4762.  
  4763.  5.1  Segment Translation
  4764.  
  4765.  Figure 5-2 shows in more detail how the processor converts a logical
  4766.  address into a linear address.
  4767.  
  4768.  To perform this translation, the processor uses the following data
  4769.  structures:
  4770.  
  4771.    ■  Descriptors
  4772.    ■  Descriptor tables
  4773.    ■  Selectors
  4774.    ■  Segment Registers
  4775.  
  4776.  
  4777.  5.1.1  Descriptors
  4778.  
  4779.  The segment descriptor provides the processor with the data it needs to map
  4780.  a logical address into a linear address. Descriptors are created by
  4781.  compilers, linkers, loaders, or the operating system, not by applications
  4782.  programmers. Figure 5-3 illustrates the two general descriptor formats. All
  4783.  types of segment descriptors take one of these formats. Segment-descriptor
  4784.  fields are:
  4785.  
  4786.  BASE: Defines the location of the segment within the 4 gigabyte linear
  4787.  address space. The processor concatenates the three fragments of the base
  4788.  address to form a single 32-bit value.
  4789.  
  4790.  LIMIT: Defines the size of the segment. When the processor concatenates the
  4791.  two parts of the limit field, a 20-bit value results. The processor
  4792.  interprets the limit field in one of two ways, depending on the setting of
  4793.  the granularity bit:
  4794.  
  4795.    1.  In units of one byte, to define a limit of up to 1 megabyte.
  4796.  
  4797.    2.  In units of 4 Kilobytes, to define a limit of up to 4 gigabytes. The
  4798.        limit is shifted left by 12 bits when loaded, and low-order one-bits
  4799.        are inserted.
  4800.  
  4801.  Granularity bit: Specifies the units with which the LIMIT field is
  4802.  interpreted. When thebit is clear, the limit is interpreted in units of one
  4803.  byte; when set, the limit is interpreted in units of 4 Kilobytes.
  4804.  
  4805.  TYPE: Distinguishes between various kinds of descriptors.
  4806.  
  4807.  DPL (Descriptor Privilege Level): Used by the protection mechanism (refer
  4808.  to Chapter 6).
  4809.  
  4810.  Segment-Present bit: If this bit is zero, the descriptor is not valid for
  4811.  use in address transformation; the processor will signal an exception when a
  4812.  selector for the descriptor is loaded into a segment register. Figure 5-4
  4813.  shows the format of a descriptor when the present-bit is zero. The operating
  4814.  system is free to use the locations marked AVAILABLE. Operating systems that
  4815.  implement segment-based virtual memory clear the present bit in either of
  4816.  these cases:
  4817.  
  4818.    ■  When the linear space spanned by the segment is not mapped by the
  4819.       paging mechanism.
  4820.  
  4821.    ■  When the segment is not present in memory.
  4822.  
  4823.  Accessed bit: The processor sets this bit when the segment is accessed;
  4824.  i.e., a selector for the descriptor is loaded into a segment register or
  4825.  used by a selector test instruction. Operating systems that implement
  4826.  virtual memory at the segment level may, by periodically testing and
  4827.  clearing this bit, monitor frequency of segment usage.
  4828.  
  4829.  Creation and maintenance of descriptors is the responsibility of systems
  4830.  software, usually requiring the cooperation of compilers, program loaders or
  4831.  system builders, and therating system.
  4832.  
  4833.  
  4834.  Figure 5-2.  Segment Translation
  4835.  
  4836.             15              0    31                                   0
  4837.    LOGICAL ╔════════════════╗   ╔═════════════════════════════════════╗
  4838.    ADDRESS ║    SELECTOR    ║   ║                OFFSET               ║
  4839.            ╚═══╤═════════╤══╝   ╚═══════════════════╤═════════════════╝
  4840.         ┌──────┘                                   │
  4841.         │ DESCRIPTOR TABLE                          │
  4842.         │  ╔════════════╗                           │
  4843.         │  ║            ║                           │
  4844.         │  ║            ║                           │
  4845.         │  ║            ║                           │
  4846.         │  ║            ║                           │
  4847.         │  ╠════════════╣                           │
  4848.         │  ║  SEGMENT   ║ BASE          ╔═══╗       │
  4849.         └─║ DESCRIPTOR ╟──────────────║ + ║──────┘
  4850.            ╠════════════╣ ADDRESS       ╚═╤═╝
  4851.            ║            ║                 │
  4852.            ╚════════════╝                 │
  4853.                                           
  4854.                LINEAR  ╔════════════╦═══════════╦══════════════╗
  4855.                ADDRESS ║    DIR     ║   PAGE    ║    OFFSET    ║
  4856.                        ╚════════════╩═══════════╩══════════════╝
  4857.  
  4858.  
  4859.  Figure 5-3.  General Segment-Descriptor Format
  4860.  
  4861.           DESCRIPTORS USED FOR APPLICATIONS CODE AND DATA SEGMENTS
  4862.  
  4863.    31                23                15                7               0
  4864.   ╔═════════════════╪═╤═╤═╤═╤═════════╪═╤═════╤═╤═════╤═╪═════════════════╗
  4865.   ║                 │ │ │ │A│         │ │     │ │     │ │                 ║
  4866.   ║   BASE 31..24   │G│X│O│V│ LIMIT   │P│ DPL │1│ TYPE│A│  BASE 23..16    ║ 4
  4867.   ║                 │ │ │ │L│ 19..16  │ │     │ │     │ │                 ║
  4868.   ╟─────────────────┴─┴─┴─┴─┴─────────┼─┴─────┴─┴─────┴─┴─────────────────╢
  4869.   ║                                   │                                   ║
  4870.   ║        SEGMENT BASE 15..0         │       SEGMENT LIMIT 15..0         ║ 0
  4871.   ║                                   │                                   ║
  4872.   ╚═════════════════╪═════════════════╪═════════════════╪═════════════════╝
  4873.  
  4874.                  DESCRIPTORS USED FOR SPECIAL SYSTEM SEGMENTS
  4875.  
  4876.    31                23                15                7               0
  4877.   ╔═════════════════╪═╤═╤═╤═╤═════════╪═╤═════╤═╤═══════╪═════════════════╗
  4878.   ║                 │ │ │ │A│         │ │     │ │       │                 ║
  4879.   ║   BASE 31..24   │G│X│O│V│ LIMIT   │P│ DPL │0│  TYPE │  BASE 23..16    ║ 4
  4880.   ║                 │ │ │ │L│ 19..16  │ │     │ │       │                 ║
  4881.   ╟─────────────────┴─┴─┴─┴─┴─────────┼─┴─────┴─┴───────┴─────────────────╢
  4882.   ║                                   │                                   ║
  4883.   ║        SEGMENT BASE 15..0         │       SEGMENT LIMIT 15..0         ║ 0
  4884.   ║                                   │                                   ║
  4885.   ╚═════════════════╪═════════════════╪═════════════════╪═════════════════╝
  4886.  
  4887.             A      - ACCESSED
  4888.             AVL    - AVAILABLE FOR USE BY SYSTEMS PROGRAMMERS
  4889.             DPL    - DESCRIPTOR PRIVILEGE LEVEL
  4890.             G      - GRANULARITY
  4891.             P      - SEGMENT PRESENT
  4892.  
  4893.  
  4894.  5.1.2  Descriptor Tables
  4895.  
  4896.  Segment descriptors are stored in either of two kinds of descriptor table:
  4897.  
  4898.    ■  The global descriptor table (GDT)
  4899.    ■  A local descriptor table (LDT)
  4900.  
  4901.  A descriptor table is simply a memory array of 8-byte entries that contain
  4902.  descriptors, as Figure 5-5 shows. A descriptor table is variable in length
  4903.  and may contain up to 8192 (2^(13)) descriptors. The first entry of the GDT
  4904.  (INDEX=0) is not used by the processor, however.
  4905.  
  4906.  The processor locates the GDT and the current LDT in memory by means of the
  4907.  GDTR and LDTR registers. These registers store the base addresses of the
  4908.  tables in the linear address space and store the segment limits. The
  4909.  instructions LGDT and SGDT give access to the GDTR; the instructions LLDT
  4910.  and SLDT give access to the LDTR.
  4911.  
  4912.  
  4913.  Figure 5-4.  Format of Not-Present Descriptor
  4914.  
  4915.    31                23                15                7               0
  4916.   ╔═════════════════╪═════════════════╪═╤═════╤═╤═══════╪═════════════════╗
  4917.   ║                                   │ │     │ │       │                 ║
  4918.   ║            AVAILABLE              │O│ DPL │S│ TYPE  │   AVAILABLE     ║ 4
  4919.   ║                                   │ │     │ │       │                 ║
  4920.   ╟───────────────────────────────────┴─┴─────┴─┴───────┴─────────────────╢
  4921.   ║                                                                       ║
  4922.   ║                               AVAILABLE                               ║ 0
  4923.   ║                                                                       ║
  4924.   ╚═════════════════╪═════════════════╪═════════════════╪═════════════════╝
  4925.  
  4926.  
  4927.  Figure 5-5.  Descriptor Tables
  4928.  
  4929.        GLOBAL DESCRIPTOR TABLE                 LOCAL DESCRIPTOR TABLE
  4930.      ╔══════╤═════╤═════╤══════╗            ╔══════╤═════╤═════╤══════╗
  4931.      ║      │     │     │      ║            ║      │     │     │      ║
  4932.      ╟──────┴─────┼─────┴──────╢            ╟──────┴─────┼─────┴──────╢
  4933.      ║            │            ║ M          ║            │            ║ M
  4934.      ╚════════════╧════════════╝            ╚════════════╧════════════╝
  4935.      |                         |            |                         |
  4936.      |                         |            |                         |
  4937.      ╔══════╤═════╤═════╤══════╗            ╔══════╤═════╤═════╤══════╗
  4938.      ║      │     │     │      ║            ║      │     │     │      ║
  4939.      ╟──────┴─────┼─────┴──────╢            ╟──────┴─────┼─────┴──────╢
  4940.      ║            │            ║ N + 3      ║            │            ║ N + 3
  4941.      ╠══════╤═════╪═════╤══════╣            ╠══════╤═════╪═════╤══════╣
  4942.      ║      │     │     │      ║            ║      │     │     │      ║
  4943.      ╟──────┴─────┼─────┴──────╢            ╟──────┴─────┼─────┴──────╢
  4944.      ║            │            ║ N + 2      ║            │            ║ N + 2
  4945.      ╠══════╤═════╪═════╤══════╣            ╠══════╤═════╪═════╤══════╣
  4946.      ║      │     │     │      ║            ║      │     │     │      ║
  4947.      ╟──────┴─────┼─────┴──────╢            ╟──────┴─────┼─────┴──────╢
  4948.      ║            │            ║ N + 1      ║            │            ║ N + 1
  4949.      ╠══════╤═════╪═════╤══════╣            ╠══════╤═════╪═════╤══════╣
  4950.      ║      │     │     │      ║            ║      │     │     │      ║
  4951.      ╟──────┴─────┼─────┴──────╢            ╟──────┴─────┼─────┴──────╢
  4952.      ║            │            ║ N          ║            │            ║ N
  4953.      ╚════════════╧════════════╝            ╚════════════╧════════════╝
  4954.      |                         |            |                         |
  4955.      |                         |            |                         |
  4956.      ╔══════╤═════╤═════╤══════╗            ╔══════╤═════╤═════╤══════╗
  4957.      ║      │     │     │      ║            ║      │     │     │      ║
  4958.      ╟──────┴──(UNUSED)─┴──────╢            ╟──────┴─────┼─────┴──────╢
  4959.      ║            │            ║            ║            │            ║
  4960.      ╚════════════╧════════════╝            ╚════════════╧════════════╝
  4961.                                                                      
  4962.       ╔═════════════════════╗  │             ╔═════════════════════╗  │
  4963.       ║         GDTR        ╟──┘             ║         LDTR        ╟──┘
  4964.       ╚═════════════════════╝                ╚═════════════════════╝
  4965.  
  4966.  
  4967.  5.1.3  Selectors
  4968.  
  4969.  The selector portion of a logical address identifies a descriptor by
  4970.  specifying a descriptor table and indexing a descriptor within that table.
  4971.  Selectors may be visible to applications programs as a field within a
  4972.  pointer variable, but the values of selectors are usually assigned (fixed
  4973.  up) by linkers or linking loaders. Figure 5-6 shows the format of a
  4974.  selector.
  4975.  
  4976.  Index: Selects one of 8192 descriptors in a descriptor table. The processor
  4977.  simply multiplies this index value by 8 (the length of a descriptor), and
  4978.  adds the result to the base address of the descriptor table in order to
  4979.  access the appropriate segment descriptor in the table.
  4980.  
  4981.  Table Indicator: Specifies to which descriptor table the selector refers. A
  4982.  zero indicates the GDT; a one indicates the current LDT.
  4983.  
  4984.  Requested Privilege Level: Used by the protection mechanism. (Refer to
  4985.  Chapter 6.)
  4986.  
  4987.  Because the first entry of the GDT is not used by the processor, a selector
  4988.  that has an index of zero and a table indicator of zero (i.e., a selector
  4989.  that points to the first entry of the GDT), can be used as a null selector.
  4990.  The processor does not cause an exception when a segment register (other
  4991.  than CS or SS) is loaded with a null selector. It will, however, cause an
  4992.  exception when the segment register is used to access memory.  This feature
  4993.  is useful for initializing unused segment registers so as to trap accidental
  4994.  references.
  4995.  
  4996.  
  4997.  Figure 5-6.  Format of a Selector
  4998.  
  4999.                           15                      4 3   0
  5000.                          ╔═════════════════════════╤═╤═══╗
  5001.                          ║                         │T│   ║
  5002.                          ║           INDEX         │ │RPL║
  5003.                          ║                         │I│   ║
  5004.                          ╚═════════════════════════╧═╧═══╝
  5005.  
  5006.                           TI  - TABLE INDICATOR
  5007.                           RPL - REQUESTOR'S PRIVILEGE LEVEL
  5008.  
  5009.  
  5010.  Figure 5-7.  Segment Registers
  5011.  
  5012.               16-BIT VISIBLE
  5013.                  SELECTOR                 HIDDEN DESCRIPTOR
  5014.             ╔════════════════╦════════════════════════════════════════╗
  5015.          CS ║                ║                                        ║
  5016.             ╟────────────────╫────────────────────────────────────────╢
  5017.          SS ║                ║                                        ║
  5018.             ╟────────────────╫────────────────────────────────────────╢
  5019.          DS ║                ║                                        ║
  5020.             ╟────────────────╫────────────────────────────────────────╢
  5021.          ES ║                ║                                        ║
  5022.             ╟────────────────╫────────────────────────────────────────╢
  5023.          FS ║                ║                                        ║
  5024.             ╟────────────────╫────────────────────────────────────────╢
  5025.          GS ║                ║                                        ║
  5026.             ╚════════════════╩════════════════════════════════════════╝
  5027.  
  5028.  
  5029.  5.1.4  Segment Registers
  5030.  
  5031.  The 80386 stores information from descriptors in segment registers, thereby
  5032.  avoiding the need to consult a descriptor table every time it accesses
  5033.  memory.
  5034.  
  5035.  Every segment register has a "visible" portion and an "invisible" portion,
  5036.  as Figure 5-7 illustrates. The visible portions of these segment address
  5037.  registers are manipulated by programs as if they were simply 16-bit
  5038.  registers. The invisible portions are manipulated by the processor.
  5039.  
  5040.  The operations that load these registers are normal program instructions
  5041.  (previously described in Chapter 3). These instructions are of two classes:
  5042.  
  5043.    1.  Direct load instructions; for example, MOV, POP, LDS, LSS, LGS, LFS.
  5044.        These instructions explicitly reference the segment registers.
  5045.  
  5046.    2.  Implied load instructions; for example, far CALL and JMP. These
  5047.        instructions implicitly reference the CS register, and load it with a
  5048.        new value.
  5049.  
  5050.  Using these instructions, a program loads the visible part of the segment
  5051.  register with a 16-bit selector. The processor automatically fetches the
  5052.  base address, limit, type, and other information from a descriptor table and
  5053.  loads them into the invisible part of the segment register.
  5054.  
  5055.  Because most instructions refer to data in segments whose selectors have
  5056.  already been loaded into segment registers, the processor can add the
  5057.  segment-relative offset supplied by the instruction to the segment base
  5058.  address with no additional overhead.
  5059.  
  5060.  
  5061.  5.2  Page Translation
  5062.  
  5063.  In the second phase of address transformation, the 80386 transforms a
  5064.  linear address into a physical address. This phase of address transformation
  5065.  implements the basic features needed for page-oriented virtual-memory
  5066.  systems and page-level protection.
  5067.  
  5068.  The page-translation step is optional. Page translation is in effect only
  5069.  when the PG bit of CR0 is set. This bit is typically set by the operating
  5070.  system during software initialization. The PG bit must be set if the
  5071.  operating system is to implement multiple virtual 8086 tasks, page-oriented
  5072.  protection, or page-oriented virtual memory.
  5073.  
  5074.  
  5075.  5.2.1  Page Frame
  5076.  
  5077.  A page frame is a 4K-byte unit of contiguous addresses of physical memory.
  5078.  Pages begin onbyte boundaries and are fixed in size.
  5079.  
  5080.  
  5081.  5.2.2  Linear Address
  5082.  
  5083.  A linear address refers indirectly to a physical address by specifying a
  5084.  page table, a page within that table, and an offset within that page. Figure
  5085.  5-8 shows the format of a linear address.
  5086.  
  5087.  Figure 5-9 shows how the processor converts the DIR, PAGE, and OFFSET
  5088.  fields of a linear address into the physical address by consulting two
  5089.  levels of page tables. The addressing mechanism uses the DIR field as an
  5090.  index into a page directory, uses the PAGE field as an index into the page
  5091.  table determined by the page directory, and uses the OFFSET field to address
  5092.  a byte within the page determined by the page table.
  5093.  
  5094.  
  5095.  Figure 5-8.  Format of a Linear Address
  5096.  
  5097.        31                 22 21                 12 11                 0
  5098.       ╔═════════════════════╦═════════════════════╦════════════════════╗
  5099.       ║                     ║                     ║                    ║
  5100.       ║         DIR         ║        PAGE         ║       OFFSET       ║
  5101.       ║                     ║                     ║                    ║
  5102.       ╚═════════════════════╩═════════════════════╩════════════════════╝
  5103.  
  5104.  
  5105.  Figure 5-9.  Page Translation
  5106.  
  5107.                                                                PAGE FRAME
  5108.                ╔═══════════╦═══════════╦══════════╗         ╔═══════════════╗
  5109.                ║    DIR    ║   PAGE    ║  OFFSET  ║         ║               ║
  5110.                ╚═════╤═════╩═════╤═════╩═════╤════╝         ║               ║
  5111.                      │           │           │              ║               ║
  5112.        ┌─────────────┘           │           └─────────────║    PHYSICAL   ║
  5113.        │                         │                          ║    ADDRESS    ║
  5114.        │   PAGE DIRECTORY        │      PAGE TABLE          ║               ║
  5115.        │  ╔═══════════════╗      │   ╔═══════════════╗      ║               ║
  5116.        │  ║               ║      │   ║               ║      ╚═══════════════╝
  5117.        │  ║               ║      │   ╠═══════════════╣              
  5118.        │  ║               ║      └──║ PG TBL ENTRY  ╟──────────────┘
  5119.        │  ╠═══════════════╣          ╠═══════════════╣
  5120.        └─║   DIR ENTRY   ╟──┐       ║               ║
  5121.           ╠═══════════════╣  │       ║               ║
  5122.           ║               ║  │       ║               ║
  5123.           ╚═══════════════╝  │       ╚═══════════════╝
  5124.                             │               
  5125.  ╔═══════╗        │          └───────────────┘
  5126.  ║  CR3  ╟────────┘
  5127.  ╚═══════╝
  5128.  
  5129.  
  5130.  5.2.3  Page Tables
  5131.  
  5132.  A page table is simply an array of 32-bit page specifiers. A page table is
  5133.  itself a page, and therefore contains 4 Kilobytes of memory or at most 1K
  5134.  32-bit entries.
  5135.  
  5136.  Two levels of tables are used to address a page of memory. At the higher
  5137.  level is a page directory. The page directory addresses up to 1K page tables
  5138.  of the second level. A page table of the second level addresses up to 1K
  5139.  pages. All the tables addressed by one page directory, therefore, can
  5140.  address 1M pages (2^(20)). Because each page contains 4K bytes 2^(12)
  5141.  bytes), the tables of one page directory can span the entire physical
  5142.  address space of the 80386 (2^(20) times 2^(12) = 2^(32)).
  5143.  
  5144.  The physical address of the current page directory is stored in the CPU
  5145.  register CR3, also called the page directory base register (PDBR). Memory
  5146.  management software has the option of using one page directory for all
  5147.  tasks, one page directory for each task, or some combination of the two.
  5148.  Refer to Chapter 10 for information on initialization of CR3. Refer to
  5149.  Chapter 7 to see how CR3 can change for each task.
  5150.  
  5151.  
  5152.  5.2.4  Page-Table Entries
  5153.  
  5154.  Entries in either level of page tables have the same format. Figure 5-10
  5155.  illustrates this format.
  5156.  
  5157.  
  5158.  5.2.4.1  Page Frame Address
  5159.  
  5160.  The page frame address specifies the physical starting address of a page.
  5161.  Because pages are located on 4K boundaries, the low-order 12 bits are always
  5162.  zero. In a page directory, the page frame address is the address of a page
  5163.  table. In a second-level page table, the page frame address is the address
  5164.  of the page frame that contains the desired memory operand.
  5165.  
  5166.  
  5167.  5.2.4.2  Present Bit
  5168.  
  5169.  The Present bit indicates whether a page table entry can be used in address
  5170.  translation. P=1 indicates that the entry can be used.
  5171.  
  5172.  When P=0 in either level of page tables, the entry is not valid for address
  5173.  translation, and the rest of the entry is available for software use; none
  5174.  of the other bits in the entry is tested by the hardware. Figure 5-11
  5175.  illustrates the format of a page-table entry when P=0.
  5176.  
  5177.  If P=0 in either level of page tables when an attempt is made to use a
  5178.  page-table entry for address translation, the processor signals a page
  5179.  exception. In software systems that support paged virtual memory, the
  5180.  page-not-present exception handler can bring the required page into physical
  5181.  memory. The instruction that caused the exception can then be reexecuted.
  5182.  Refer to Chapter 9 for more information on exception handlers.
  5183.  
  5184.  Note that there is no present bit for the page directory itself. The page
  5185.  directory may be not-present while the associated task is suspended, but the
  5186.  operating system must ensure that the page directory indicated by the CR3
  5187.  image in the TSS is present in physical memory before the task is
  5188.  dispatched. Refer to Chapter 7 for an explanation of the TSS and task
  5189.  dispatching.
  5190.  
  5191.  
  5192.  Figure 5-10.  Format of a Page Table Entry
  5193.  
  5194.         31                                  12 11                      0
  5195.        ╔══════════════════════════════════════╤═══════╤═══╤═╤═╤═══╤═╤═╤═╗
  5196.        ║                                      │       │   │ │ │   │U│R│ ║
  5197.        ║      PAGE FRAME ADDRESS 31..12       │ AVAIL │0 0│D│A│0 0│/│/│P║
  5198.        ║                                      │       │   │ │ │   │S│W│ ║
  5199.        ╚══════════════════════════════════════╧═══════╧═══╧═╧═╧═══╧═╧═╧═╝
  5200.  
  5201.                  P      - PRESENT
  5202.                  R/W    - READ/WRITE
  5203.                  U/S    - USER/SUPERVISOR
  5204.                  D      - DIRTY
  5205.                  AVAIL  - AVAILABLE FOR SYSTEMS PROGRAMMER USE
  5206.  
  5207.                  NOTE: 0 INDICATES INTEL RESERVED. DO NOT DEFINE.
  5208.  
  5209.  
  5210.  Figure 5-11.  Invalid Page Table Entry
  5211.  
  5212.         31                                                           1 0
  5213.        ╔══════════════════════════════════════════════════════════════╤═╗
  5214.        ║                                                              │ ║
  5215.        ║                            AVAILABLE                         │0║
  5216.        ║                                                              │ ║
  5217.        ╚══════════════════════════════════════════════════════════════╧═╝
  5218.  
  5219.  
  5220.  5.2.4.3  Accessed and Dirty Bits
  5221.  
  5222.  These bits provide data about page usage in both levels of the page tables.
  5223.  With the exception of the dirty bit in a page directory entry, these bits
  5224.  are set by the hardware; however, the processor does not clear any of these
  5225.  bits.
  5226.  
  5227.  The processor sets the corresponding accessed bits in both levels of page
  5228.  tables to one before a read or write operation to a page.
  5229.  
  5230.  The processor sets the dirty bit in the second-level page table to one
  5231.  before a write to an address covered by that page table entry. The dirty bit
  5232.  in directory entries is undefined.
  5233.  
  5234.  An operating system that supports paged virtual memory can use these bits
  5235.  to determine what pages to eliminate from physical memory when the demand
  5236.  for memory exceeds the physical memory available. The operating system is
  5237.  responsible for testing and clearing these bits.
  5238.  
  5239.  Refer to Chapter 11 for how the 80386 coordinates updates to the accessed
  5240.  and dirty bits in multiprocessor systems.
  5241.  
  5242.  
  5243.  5.2.4.4  Read/Write and User/Supervisor Bits
  5244.  
  5245.  These bits are not used for address translation, but are used for
  5246.  page-level protection, which the processor performs at the same time as
  5247.  address translation. Refer to Chapter 6 where protection is discussed in
  5248.  detail.
  5249.  
  5250.  
  5251.  5.2.5  Page Translation Cache
  5252.  
  5253.  For greatest efficiency in address translation, the processor stores the
  5254.  most recently used page-table data in an on-chip cache. Only if the
  5255.  necessary paging information is not in the cache must both levels of page
  5256.  tables be referenced.
  5257.  
  5258.  The existence of the page-translation cache is invisible to applications
  5259.  programmers but not to systems programmers; operating-system programmers
  5260.  must flush the cache whenever the page tables are changed. The
  5261.  page-translation cache can be flushed by either of two methods:
  5262.  
  5263.    1.  By reloading CR3 with a MOV instruction; for example:
  5264.  
  5265.        MOV CR3, EAX
  5266.  
  5267.    2.  By performing a task switch to a TSS that has a different CR3 image
  5268.        than the current TSS. (Refer to Chapter 7 for more information on
  5269.        task switching.)
  5270.  
  5271.  
  5272.  5.3  Combining Segment and Page Translation
  5273.  
  5274.  Figure 5-12 combines Figure 5-2 and Figure 5-9 to summarize both phases
  5275.  of the transformation from a logical address to a physical address when
  5276.  paging is enabled. By appropriate choice of options and parameters to both
  5277.  phases, memory-management software can implement several different styles of
  5278.  memory management.
  5279.  
  5280.  
  5281.  5.3.1  "Flat" Architecture
  5282.  
  5283.  When the 80386 is used to execute software designed for architectures that
  5284.  don't have segments, it may be expedient to effectively "turn off" the
  5285.  segmentation features of the 80386. The 80386 does not have a mode that
  5286.  disables segmentation, but the same effect can be achieved by initially
  5287.  loading the segment registers with selectors for descriptors that encompass
  5288.  the entire 32-bit linear address space. Once loaded, the segment registers
  5289.  don't need to be changed. The 32-bit offsets used by 80386 instructions are
  5290.  adequate to address the entire linear-address space.
  5291.  
  5292.  
  5293.  5.3.2  Segments Spanning Several Pages
  5294.  
  5295.  The architecture of the 80386 permits segments to be larger or smaller than
  5296.  the size of a page (4 Kilobytes). For example, suppose a segment is used to
  5297.  address and protect a large data structure that spans 132 Kilobytes. In a
  5298.  software system that supports paged virtual memory, it is not necessary for
  5299.  the entire structure to be in physical memory at once. The structure is
  5300.  divided into 33 pages, any number of which may not be present. The
  5301.  applications programmer does not need to be aware that the virtual memory
  5302.  subsystem is paging the structure in this manner.
  5303.  
  5304.  
  5305.  Figure 5-12.  80306 Addressing Machanism
  5306.  
  5307.        16                0 32                                  0
  5308.      ╔════════════════════╦═════════════════════════════════════╗ LOGICAL
  5309.      ║      SELECTOR      ║                 OFFSET              ║ ADDRESS
  5310.      ╚════╤══════════╤════╩════════════════════╤════════════════╝
  5311.   ┌───────┘                                   │
  5312.   │   DESCRIPTOR TABLE                         │
  5313.   │  ╔═══════════════╗                         │
  5314.   │  ║               ║                         │
  5315.   │  ║               ║                         │
  5316.   │  ║               ║                         │
  5317.   │  ║               ║                         │
  5318.   │  ╠═══════════════╣                         │
  5319.   │  ║   SEGMENT     ║         ╔═══╗           │
  5320.   └─║  DESCRIPTOR   ╟────────║ + ║──────────┘
  5321.      ╠═══════════════╣         ╚═╤═╝
  5322.      ║               ║           │
  5323.      ╚═══════════════╝           │
  5324.                                                                PAGE FRAME
  5325.        LINEAR  ╔═══════════╦═══════════╦══════════╗         ╔═══════════════╗
  5326.        ADDRESS ║    DIR    ║   PAGE    ║  OFFSET  ║         ║               ║
  5327.                ╚═════╤═════╩═════╤═════╩═════╤════╝         ║               ║
  5328.                      │           │           │              ║               ║
  5329.        ┌─────────────┘           │           └─────────────║    PHYSICAL   ║
  5330.        │                         │                          ║    ADDRESS    ║
  5331.        │   PAGE DIRECTORY        │      PAGE TABLE          ║               ║
  5332.        │  ╔═══════════════╗      │   ╔═══════════════╗      ║               ║
  5333.        │  ║               ║      │   ║               ║      ║               ║
  5334.        │  ║               ║      │   ║               ║      ╚═══════════════╝
  5335.        │  ║               ║      │   ╠═══════════════╣              
  5336.        │  ║               ║      └──║ PG TBL ENTRY  ╟──────────────┘
  5337.        │  ╠═══════════════╣          ╠═══════════════╣
  5338.        └─║   DIR ENTRY   ╟──┐       ║               ║
  5339.           ╠═══════════════╣  │       ║               ║
  5340.           ║               ║  │       ║               ║
  5341.           ╚═══════════════╝  │       ╚═══════════════╝
  5342.                             │               
  5343.  ╔═══════╗        │          └───────────────┘
  5344.  ║  CR3  ╟────────┘
  5345.  ╚═══════╝
  5346.  
  5347.  
  5348.  5.3.3  Pages Spanning Several Segments
  5349.  
  5350.  On the other hand, segments may be smaller than the size of a page. For
  5351.  example, consider a small data structure such as a semaphore. Because of the
  5352.  protection and sharing provided by segments (refer to Chapter 6), it may be
  5353.  useful to create a separate segment for each semaphore. But, because a
  5354.  system may need many semaphores, it is not efficient to allocate a page for
  5355.  each. Therefore, it may be useful to cluster many related segments within a
  5356.  page.
  5357.  
  5358.  
  5359.  5.3.4  Non-Aligned Page and Segment Boundaries
  5360.  
  5361.  The architecture of the 80386 does not enforce any correspondence between
  5362.  the boundaries of pages and segments. It is perfectly permissible for a page
  5363.  to contain the end of one segment and the beginning of another. Likewise, a
  5364.  segment may contain the end of one page and the beginning of another.
  5365.  
  5366.  
  5367.  5.3.5  Aligned Page and Segment Boundaries
  5368.  
  5369.  Memory-management software may be simpler, however, if it enforces some
  5370.  correspondence between page and segment boundaries. For example, if segments
  5371.  are allocated only in units of one page, the logic for segment and page
  5372.  allocation can be combined. There is no need for logic to account for
  5373.  partially used pages.
  5374.  
  5375.  
  5376.  5.3.6  Page-Table per Segment
  5377.  
  5378.  An approach to space management that provides even further simplification
  5379.  of space-management software is to maintain a one-to-one correspondence
  5380.  between segment descriptors and page-directory entries, as Figure 5-13
  5381.  illustrates. Each descriptor has a base address in which the low-order 22
  5382.  bits are zero; in other words, the base address is mapped by the first entry
  5383.  of a page table. A segment may have any limit from 1 to 4 megabytes.
  5384.  Depending on the limit, the segment is contained in from 1 to 1K page
  5385.  frames. A task is thus limited to 1K segments (a sufficient number for many
  5386.  applications), each containing up to 4 Mbytes. The descriptor, the
  5387.  corresponding page-directory entry, and the corresponding page table can be
  5388.  allocated and deallocated simultaneously.
  5389.  
  5390.  
  5391.  Figure 5-13.  Descriptor per Page Table
  5392.  
  5393.                                                                PAGE FRAMES
  5394.                                                               ╔═══════════╗
  5395.           LDT          PAGE DIRECTORY       PAGE TABLES       ║           ║
  5396.      ╔══════════╗       ╔══════════╗        ╔══════════╗      ║           ║
  5397.      ║          ║       ║          ║        ║          ║   ┌─╚═══════════╝
  5398.      ╠══════════╣       ╠══════════╣        ╠══════════╣   │
  5399.      ║          ║       ║          ║        ║   PTE    ╟───┘  ╔═══════════╗
  5400.      ╠══════════╣       ╠══════════╣        ╠══════════╣      ║           ║
  5401.      ║          ║       ║          ║        ║   PTE    ╟───┐  ║           ║
  5402.      ╠══════════╣       ╠══════════╣        ╠══════════╣   └─╚═══════════╝
  5403.      ║          ║       ║          ║        ║   PTE    ╟───┐
  5404.      ╠══════════╣       ╠══════════╣   ┌───╚══════════╝   │  ╔═══════════╗
  5405.      ║DESCRIPTOR╟──────║   PDE    ╟───┘                   │  ║           ║
  5406.      ╠══════════╣       ╠══════════╣                       │  ║           ║
  5407.      ║DESCRIPTOR╟──────║   PDE    ╟───┐                   └─╚═══════════╝
  5408.      ╠══════════╣       ╠══════════╣   │    ╔══════════╗
  5409.      ║          ║       ║          ║   │    ║          ║      ╔═══════════╗
  5410.      ╠══════════╣       ╠══════════╣   │    ╠══════════╣      ║           ║
  5411.      ║          ║       ║          ║   │    ║          ║      ║           ║
  5412.      ╠══════════╣       ╠══════════╣   │    ╠══════════╣   ┌─╚═══════════╝
  5413.      ║          ║       ║          ║   │    ║   PTE    ╟───┘
  5414.      ╠══════════╣       ╠══════════╣   │    ╠══════════╣      ╔═══════════╗
  5415.      ║          ║       ║          ║   │    ║   PTE    ╟───┐  ║           ║
  5416.      ╚══════════╝       ╚══════════╝   └───╚══════════╝   │  ║           ║
  5417.          LDT           PAGE DIRECTORY       PAGE TABLES    └─╚═══════════╝
  5418.                                                                PAGE FRAMES
  5419.  
  5420.  
  5421.  Chapter 6  Protection
  5422.  
  5423.  ────────────────────────────────────────────────────────────────────────────
  5424.  
  5425.  6.1  Why Protection?
  5426.  
  5427.  The purpose of the protection features of the 80386 is to help detect and
  5428.  identify bugs. The 80386 supports sophisticated applications that may
  5429.  consist of hundreds or thousands of program modules. In such applications,
  5430.  the question is how bugs can be found and eliminated as quickly as possible
  5431.  and how their damage can be tightly confined. To help debug applications
  5432.  faster and make them more robust in production, the 80386 contains
  5433.  mechanisms to verify memory accesses and instruction execution for
  5434.  conformance to protection criteria. These mechanisms may be used or ignored,
  5435.  according to system design objectives.
  5436.  
  5437.  
  5438.  6.2  Overview of 80386 Protection Mechanisms
  5439.  
  5440.  Protection in the 80386 has five aspects:
  5441.  
  5442.    1.  Type checking
  5443.    2.  Limit checking
  5444.    3.  Restriction of addressable domain
  5445.    4.  Restriction of procedure entry points
  5446.    5.  Restriction of instruction set
  5447.  
  5448.  The protection hardware of the 80386 is an integral part of the memory
  5449.  management hardware. Protection applies both to segment translation and to
  5450.  page translation.
  5451.  
  5452.  Each reference to memory is checked by the hardware to verify that it
  5453.  satisfies the protection criteria. All these checks are made before the
  5454.  memory cycle is started; any violation prevents that cycle from starting and
  5455.  results in an exception. Since the checks are performed concurrently with
  5456.  address formation, there is no performance penalty.
  5457.  
  5458.  Invalid attempts to access memory result in an exception. Refer to
  5459.  Chapter 9 for an explanation of the exception mechanism. The present
  5460.  chapter defines the protection violations that lead to exceptions.
  5461.  
  5462.  The concept of "privilege" is central to several aspects of protection
  5463.  (numbers 3, 4, and 5 in the preceeding list). Applied to procedures,
  5464.  privilege is the degree to which the procedure can be trusted not to make a
  5465.  mistake that might affect other procedures or data. Applied to data,
  5466.  privilege is the degree of protection that a data structure should have
  5467.  from less trusted procedures.
  5468.  
  5469.  The concept of privilege applies both to segment protection and to page
  5470.  protection.
  5471.  
  5472.  
  5473.  6.3  Segment-Level Protection
  5474.  
  5475.  All five aspects of protection apply to segment translation:
  5476.  
  5477.    1.  Type checking
  5478.    2.  Limit checking
  5479.    3.  Restriction of addressable domain
  5480.    4.  Restriction of procedure entry points
  5481.    5.  Restriction of instruction set
  5482.  
  5483.  The segment is the unit of protection, and segment descriptors store
  5484.  protection parameters. Protection checks are performed automatically by the
  5485.  CPU when the selector of a segment descriptor is loaded into a segment
  5486.  register and with every segment access. Segment registers hold the
  5487.  protection parameters of the currently addressable segments.
  5488.  
  5489.  
  5490.  6.3.1  Descriptors Store Protection Parameters
  5491.  
  5492.  Figure 6-1 highlights the protection-related fields of segment descriptors.
  5493.  
  5494.  The protection parameters are placed in the descriptor by systems software
  5495.  at the time a descriptor is created. In general, applications programmers do
  5496.  not need to be concerned about protection parameters.
  5497.  
  5498.  When a program loads a selector into a segment register, the processor
  5499.  loads not only the base address of the segment but also protection
  5500.  information. Each segment register has bits in the invisible portion for
  5501.  storing base, limit, type, and privilege level; therefore, subsequent
  5502.  protection checks on the same segment do not consume additional clock
  5503.  cycles.
  5504.  
  5505.  
  5506.  Figure 6-1.  Protection Fields of Segment Descriptors
  5507.  
  5508.                             DATA SEGMENT DESCRIPTOR
  5509.  
  5510.    31                23                15                7               0
  5511.   ╔═════════════════╪═╤═╤═╤═╤═════════╪═╤═════╤═════════╪═════════════════╗
  5512.   ║▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒│▒│▒│▒│A│ LIMIT   │▒│     │  TYPE   │▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒║
  5513.   ║▒▒▒BASE 31..24▒▒▒│G│B│0│V│ 19..16  │P│ DPL │         │▒▒▒BASE 23..16▒▒▒║ 4
  5514.   ║▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒│▒│▒│▒│L│         │▒│     │1│0│E│W│A│▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒║
  5515.   ╟─────────────────┴─┴─┴─┴─┴─────────┼─┴─────┴─┴─┴─┴─┴─┴─────────────────╢
  5516.   ║▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒│                                   ║
  5517.   ║▒▒▒▒▒▒▒▒SEGMENT BASE 15..0▒▒▒▒▒▒▒▒▒│        SEGMENT LIMIT 15..0        ║ 0
  5518.   ║▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒│                                   ║
  5519.   ╚═════════════════╪═════════════════╪═════════════════╪═════════════════╝
  5520.  
  5521.                          EXECUTABLE SEGMENT DESCRIPTOR
  5522.  
  5523.    31                23                15                7               0
  5524.   ╔═════════════════╪═╤═╤═╤═╤═════════╪═╤═════╤═════════╪═════════════════╗
  5525.   ║▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒│▒│▒│▒│A│ LIMIT   │▒│     │  TYPE   │▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒║
  5526.   ║▒▒▒BASE 31..24▒▒▒│G│D│0│V│ 19..16  │P│ DPL │         │▒▒▒BASE 23..16▒▒▒║ 4
  5527.   ║▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒│▒│▒│▒│L│         │▒│     │1│0│C│R│A│▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒║
  5528.   ╟─────────────────┴─┴─┴─┴─┴─────────┼─┴─────┴─┴─┴─┴─┴─┴─────────────────╢
  5529.   ║▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒│                                   ║
  5530.   ║▒▒▒▒▒▒▒▒SEGMENT BASE 15..0▒▒▒▒▒▒▒▒▒│        SEGMENT LIMIT 15..0        ║ 0
  5531.   ║▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒│                                   ║
  5532.   ╚═════════════════╪═════════════════╪═════════════════╪═════════════════╝
  5533.  
  5534.                           SYSTEM SEGMENT DESCRIPTOR
  5535.  
  5536.    31                23                15                7               0
  5537.   ╔═════════════════╪═╤═╤═╤═╤═════════╪═╤═════╤═╤═══════╪═════════════════╗
  5538.   ║▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒│▒│▒│▒│A│ LIMIT   │▒│     │ │       │▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒║
  5539.   ║▒▒▒BASE 31..24▒▒▒│G│X│0│V│ 19..16  │P│ DPL │0│ TYPE  │▒▒▒BASE 23..16▒▒▒║ 4
  5540.   ║▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒│▒│▒│▒│L│         │▒│     │ │       │▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒║
  5541.   ╟─────────────────┴─┴─┴─┴─┴─────────┼─┴─────┴─┴───────┴─────────────────╢
  5542.   ║▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒│                                   ║
  5543.   ║▒▒▒▒▒▒▒▒SEGMENT BASE 15..0▒▒▒▒▒▒▒▒▒│       SEGMENT LIMIT 15..0         ║ 0
  5544.   ║▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒│                                   ║
  5545.   ╚═════════════════╪═════════════════╪═════════════════╪═════════════════╝
  5546.  
  5547.  
  5548.          A   - ACCESSED                              E   - EXPAND-DOWN
  5549.          AVL - AVAILABLE FOR PROGRAMMERS USE         G   - GRANULARITY
  5550.          B   - BIG                                   P   - SEGMENT PRESENT
  5551.          C   - CONFORMING                            R   - READABLE
  5552.          D   - DEFAULT                               W   - WRITABLE
  5553.          DPL - DESCRIPTOR PRIVILEGE LEVEL
  5554.  
  5555.  
  5556.  6.3.1.1  Type Checking
  5557.  
  5558.  The TYPE field of a descriptor has two functions:
  5559.  
  5560.    1.  It distinguishes among different descriptor formats.
  5561.    2.  It specifies the intended usage of a segment.
  5562.  
  5563.  Besides the descriptors for data and executable segments commonly used by
  5564.  applications programs, the 80386 has descriptors for special segments used
  5565.  by the operating system and for gates. Table 6-1 lists all the types defined
  5566.  for system segments and gates. Note that not all descriptors define
  5567.  segments; gate descriptors have a different purpose that is discussed later
  5568.  in this chapter.
  5569.  
  5570.  The type fields of data and executable segment descriptors include bits
  5571.  which further define the purpose of the segment (refer to Figure 6-1):
  5572.  
  5573.    ■  The writable bit in a data-segment descriptor specifies whether
  5574.       instructions can write into the segment.
  5575.  
  5576.    ■  The readable bit in an executable-segment descriptor specifies
  5577.       whether instructions are allowed to read from the segment (for example,
  5578.       to access constants that are stored with instructions). A readable,
  5579.       executable segment may be read in two ways:
  5580.  
  5581.    1.  Via the CS register, by using a CS override prefix.
  5582.  
  5583.    2.  By loading a selector of the descriptor into a data-segment register
  5584.        (DS, ES, FS,or GS).
  5585.  
  5586.  Type checking can be used to detect programming errors that would attempt
  5587.  to use segments in ways not intended by the programmer. The processor
  5588.  examines type information on two kinds of occasions:
  5589.  
  5590.    1.  When a selector of a descriptor is loaded into a segment register.
  5591.        Certain segment registers can contain only certain descriptor types;
  5592.        for example:
  5593.  
  5594.    ■  The CS register can be loaded only with a selector of an executable
  5595.       segment.
  5596.  
  5597.    ■  Selectors of executable segments that are not readable cannot be
  5598.       loaded into data-segment registers.
  5599.  
  5600.    ■  Only selectors of writable data segments can be loaded into SS.
  5601.  
  5602.    2.  When an instruction refers (implicitly or explicitly) to a segment
  5603.        register. Certain segments can be used by instructions only in certain
  5604.        predefined ways; for example:
  5605.  
  5606.    ■  No instruction may write into an executable segment.
  5607.  
  5608.    ■  No instruction may write into a data segment if the writable bit is
  5609.       not set.
  5610.  
  5611.    ■  No instruction may read an executable segment unless the readable bit
  5612.       is set.
  5613.  
  5614.  
  5615.  Table 6-1. System and Gate Descriptor Types
  5616.  
  5617.  Code      Type of Segment or Gate
  5618.  
  5619.    0       -reserved
  5620.    1       Available 286 TSS
  5621.    2       LDT
  5622.    3       Busy 286 TSS
  5623.    4       Call Gate
  5624.    5       Task Gate
  5625.    6       286 Interrupt Gate
  5626.    7       286 Trap Gate
  5627.    8       -reserved
  5628.    9       Available 386 TSS
  5629.    A       -reserved
  5630.    B       Busy 386 TSS
  5631.    C       386 Call Gate
  5632.    D       -reserved
  5633.    E       386 Interrupt Gate
  5634.    F       386 Trap Gate
  5635.  
  5636.  
  5637.  6.3.1.2  Limit Checking
  5638.  
  5639.  The limit field of a segment descriptor is used by the processor to prevent
  5640.  programs from addressing outside the segment. The processor's interpretation
  5641.  of the limit depends on the setting of the G (granularity) bit. For data
  5642.  segments, the processor's interpretation of the limit depends also on the
  5643.  E-bit (expansion-direction bit) and the B-bit (big bit) (refer to Table
  5644.  6-2).
  5645.  
  5646.  When G=0, the actual limit is the value of the 20-bit limit field as it
  5647.  appears in the descriptor. In this case, the limit may range from 0 to
  5648.  0FFFFFH (2^(20) - 1 or 1 megabyte). When G=1, the processor appends 12
  5649.  low-order one-bits to the value in the limit field. In this case the actual
  5650.  limit may range from 0FFFH (2^(12) - 1 or 4 kilobytes) to 0FFFFFFFFH(2^(32)
  5651.  - 1 or 4 gigabytes).
  5652.  
  5653.  For all types of segments except expand-down data segments, the value of
  5654.  the limit is one less than the size (expressed in bytes) of the segment. The
  5655.  processor causes a general-protection exception in any of these cases:
  5656.  
  5657.    ■  Attempt to access a memory byte at an address > limit.
  5658.    ■  Attempt to access a memory word at an address ≥limit.
  5659.    ■  Attempt to access a memory doubleword at an address ≥(limit-2).
  5660.  
  5661.  For expand-down data segments, the limit has the same function but is
  5662.  interpreted differently. In these cases the range of valid addresses is from
  5663.  limit + 1 to either 64K or 2^(32) - 1 (4 Gbytes) depending on the B-bit. An
  5664.  expand-down segment has maximum size when the limit is zero.
  5665.  
  5666.  The expand-down feature makes it possible to expand the size of a stack by
  5667.  copying it to a larger segment without needing also to update intrastack
  5668.  pointers.
  5669.  
  5670.  The limit field of descriptors for descriptor tables is used by the
  5671.  processor to prevent programs from selecting a table entry outside the
  5672.  descriptor table. The limit of a descriptor table identifies the last valid
  5673.  byte of the last descriptor in the table. Since each descriptor is eight
  5674.  bytes long, the limit value is N * 8 - 1 for a table that can contain up to
  5675.  N descriptors.
  5676.  
  5677.  Limit checking catches programming errors such as runaway subscripts and
  5678.  invalid pointer calculations. Such errors are detected when they occur, so
  5679.  that identification of the cause is easier. Without limit checking, such
  5680.  errors could corrupt other modules; the existence of such errors would not
  5681.  be discovered until later, when the corrupted module behaves incorrectly,
  5682.  and when identification of the cause is difficult.
  5683.  
  5684.  
  5685.  Table 6-2. Useful Combinations of E, G, and B Bits
  5686.  
  5687. ╓┌────────────────────────┌─────────┌─────────┌─────────┌────────────────────╖
  5688.  Case:                    1         2         3         4
  5689.  
  5690.  Expansion Direction      U         U         D         D
  5691.  G-bit                    0         1         0         1
  5692.  B-bit                    X         X         0         1
  5693.  Case:                    1         2         3         4
  5694. B-bit                    X         X         0         1
  5695.  
  5696.  Lower bound is:
  5697.       0                   X         X
  5698.       LIMIT+1                                 X
  5699.  shl(LIMIT,12,1)+1                                      X
  5700.  
  5701.  Upper bound is:
  5702.       LIMIT               X
  5703.       shl(LIMIT,12,1)               X
  5704.       64K-1                                   X
  5705.       4G-1                                              X
  5706.  
  5707.  Max seg size is:
  5708.       64K                 X
  5709.       64K-1                         X
  5710.       4G-4K                                   X
  5711.       4G                                                X
  5712.  
  5713.  Min seg size is:
  5714.  Case:                    1         2         3         4
  5715. Min seg size is:
  5716.       0                   X         X
  5717.       4K                                      X         X
  5718.  
  5719.  shl (X, 12, 1) = shift X left by 12 bits inserting one-bits on the right
  5720.  
  5721.  
  5722.  6.3.1.3  Privilege Levels
  5723.  
  5724.  The concept of privilege is implemented by assigning a value from zero to
  5725.  three to key objects recognized by the processor. This value is called the
  5726.  privilege level. The value zero represents the greatest privilege, the
  5727.  value three represents the least privilege. The following
  5728.  processor-recognized objects contain privilege levels:
  5729.  
  5730.    ■  Descriptors contain a field called the descriptor privilege level
  5731.       (DPL).
  5732.  
  5733.    ■  Selectors contain a field called the requestor's privilege level
  5734.       (RPL). The RPL is intended to represent the privilege level of
  5735.       the procedure that originates a selector.
  5736.  
  5737.    ■  An internal processor register records the current privilege level
  5738.       (CPL). Normally the CPL is equal to the DPL of the segment that
  5739.       the processor is currently executing. CPL changes as control is
  5740.       transferred to segments with differing DPLs.
  5741.  
  5742.  The processor automatically evaluates the right of a procedure to access
  5743.  another segment by comparing the CPL to one or more other privilege levels.
  5744.  The evaluation is performed at the time the selector of a descriptor is
  5745.  loaded into a segment register. The criteria used for evaluating access to
  5746.  data differs from that for evaluating transfers of control to executable
  5747.  segments; therefore, the two types of access are considered separately in
  5748.  the following sections.
  5749.  
  5750.  Figure 6-2 shows how these levels of privilege can be interpreted as rings
  5751.  of protection. The center is for the segments containing the most critical
  5752.  software, usually the kernel of the operating system. Outer rings are for
  5753.  the segments of less critical software.
  5754.  
  5755.  It is not necessary to use all four privilege levels. Existing software
  5756.  that was designed to use only one or two levels of privilege can simply
  5757.  ignore the other levels offered by the 80386. A one-level system should use
  5758.  privilege level zero; a two-level system should use privilege levels zero
  5759.  and three.
  5760.  
  5761.  
  5762.  Figure 6-2.  Levels of Privilege
  5763.  
  5764.                                       TASK C
  5765.                ┌───────────────────────────────────────────────────┐
  5766.                │ ╔═══════════════════════════════════════════════╗ │
  5767.                │ ║                 APPLICATIONS                  ║ │
  5768.                │ ║     ╔═══════════════════════════════════╗     ║ │
  5769.                │ ║     ║        CUSTOM EXTENSIONS          ║     ║ │
  5770.                │ ║     ║     ╔═══════════════════════╗     ║     ║ │
  5771.                │ ║     ║     ║    SYSTEM SERVICES    ║     ║     ║ │
  5772.                │ ║     ║     ║     ╔═══════════╗     ║     ║     ║ │
  5773.                │ ║     ║     ║     ║  KERNAL   ║     ║     ║     ║ │
  5774.                ╞═╟─────╫─────╫─────╫─────┬─────╫─────╫─────╫─────╢═╡
  5775.                │ ║     ║     ║     ║     │LEVEL║LEVEL║LEVEL║LEVEL║ │
  5776.                │ ║     ║     ║     ║     │  0  ║  1  ║  2  ║  3  ║ │
  5777.                │ ║     ║     ║     ╚═════╪═════╝     ║     ║     ║ │
  5778.                │ ║     ║     ║           │           ║     ║     ║ │
  5779.                │ ║     ║     ╚═══════════╪═══════════╝     ║     ║ │
  5780.                │ ║     ║                 │                 ║     ║ │
  5781.                │ ║     ╚═════════════════╪═════════════════╝     ║ │
  5782.                │ ║                       │                       ║ │
  5783.          TASK B┤ ╚═══════════════════════╧═══════════════════════╝ ├TASK A
  5784.                └────────────────────────┘ └────────────────────────┘
  5785.  
  5786.  
  5787.  6.3.2  Restricting Access to Data
  5788.  
  5789.  To address operands in memory, an 80386 program must load the selector of a
  5790.  data segment into a data-segment register (DS, ES, FS, GS, SS). The
  5791.  processor automatically evaluates access to a data segment by comparing
  5792.  privilege levels. The evaluation is performed at the time a selector for the
  5793.  descriptor of the target segment is loaded into the data-segment register.
  5794.  As Figure 6-3 shows, three different privilege levels enter into this type
  5795.  of privilege check:
  5796.  
  5797.    1.  The CPL (current privilege level).
  5798.  
  5799.    2.  The RPL (requestor's privilege level) of the selector used to specify
  5800.        the target segment.
  5801.  
  5802.    3.  The DPL of the descriptor of the target segment.
  5803.  
  5804.  Instructions may load a data-segment register (and subsequently use the
  5805.  target segment) only if the DPL of the target segment is numerically greater
  5806.  than or equal to the maximum of the CPL and the selector's RPL. In other
  5807.  words, a procedure can only access data that is at the same or less
  5808.  privileged level.
  5809.  
  5810.  The addressable domain of a task varies as CPL changes. When CPL is zero,
  5811.  data segments at all privilege levels are accessible; when CPL is one, only
  5812.  data segments at privilege levels one through three are accessible; when CPL
  5813.  is three, only data segments at privilege level three are accessible. This
  5814.  property of the 80386 can be used, for example, to prevent applications
  5815.  procedures from reading or changing tables of the operating system.
  5816.  
  5817.  
  5818.  Figure 6-3.  Privilege Check for Data Access
  5819.  
  5820.            16-BIT VISIBLE
  5821.               SELECTOR            INVISIBLE DESCRIPTOR
  5822.          ╔═══════════════╦═══════════════════╦═══╦═══════════╗
  5823.       CS ║               ║                   ║CPL║           ║
  5824.          ╚═══════════════╩═══════════════════╩═╤═╩═══════════╝
  5825.                                                │
  5826.      TARGET SEGMENT SELECTOR                   │        ╔═══════════╗
  5827.   ╔═══════════════════════╦═╦═══╗              └───────║ PRIVILEGE ║
  5828.   ║         INDEX         ║ ║RPL╟──────────────────────║ CHECK     ║
  5829.   ╚═══════════════════════╩═╩═══╝              ┌───────║ BY CPU    ║
  5830.                                                │        ╚═══════════╝
  5831.       DATA SEGMENT DESCRIPTOR              ┌───┘
  5832.                                            │
  5833.    31                23                15  │             7               0
  5834.   ╔═════════════════╪═╤═╤═╤═╤═════════╪═╤══╧══╤═════════╪═════════════════╗
  5835.   ║                 │ │ │ │A│ LIMIT   │ │     │  TYPE   │                 ║
  5836.   ║   BASE 31..24   │G│B│0│V│         │P│ DPL │         │   BASE 23..16   ║ 4
  5837.   ║                 │ │ │ │L│  19..16 │ │     │1│0│E│W│A│                 ║
  5838.   ╟─────────────────┴─┴─┴─┴─┴─────────┼─┴─────┴─┴─┴─┴─┴─┴─────────────────╢
  5839.   ║                                   │                                   ║
  5840.   ║        SEGMENT BASE 15..0         │        SEGMENT LIMIT 15..0        ║ 0
  5841.   ║                                   │                                   ║
  5842.   ╚═════════════════╪═════════════════╪═════════════════╪═════════════════╝
  5843.  
  5844.  
  5845.      CPL - CURRENT PRIVILEGE LEVEL
  5846.      RPL - REQUESTOR'S PRIVILEGE LEVEL
  5847.      DPL - DESCRIPTOR PRIVILEGE LEVEL
  5848.  
  5849.  
  5850.  6.3.2.1  Accessing Data in Code Segments
  5851.  
  5852.  Less common than the use of data segments is the use of code segments to
  5853.  store data. Code segments may legitimately hold constants; it is not
  5854.  possible to write to a segment described as a code segment. The following
  5855.  methods of accessing data in code segments are possible:
  5856.  
  5857.    1.  Load a data-segment register with a selector of a nonconforming,
  5858.        readable, executable segment.
  5859.  
  5860.    2.  Load a data-segment register with a selector of a conforming,
  5861.        readable, executable segment.
  5862.  
  5863.    3.  Use a CS override prefix to read a readable, executable segment whose
  5864.        selector is already loaded in the CS register.
  5865.  
  5866.  The same rules as for access to data segments apply to case 1. Case 2 is
  5867.  always valid because the privilege level of a segment whose conforming bit
  5868.  is set is effectively the same as CPL regardless of its DPL. Case 3 always
  5869.  valid because the DPL of the code segment in CS is, by definition, equal to
  5870.  CPL.
  5871.  
  5872.  
  5873.  6.3.3  Restricting Control Transfers
  5874.  
  5875.  With the 80386, control transfers are accomplished by the instructions JMP,
  5876.  CALL, RET, INT, and IRET, as well as by the exception and interrupt
  5877.  mechanisms. Exceptions and interrupts are special cases that Chapter 9
  5878.  covers. This chapter discusses only JMP, CALL, and RET instructions.
  5879.  
  5880.  The "near" forms of JMP, CALL, and RET transfer within the current code
  5881.  segment, and therefore are subject only to limit checking. The processor
  5882.  ensures that the destination of the JMP, CALL, or RET instruction does not
  5883.  exceed the limit of the current executable segment. This limit is cached in
  5884.  the CS register; therefore, protection checks for near transfers require no
  5885.  extra clock cycles.
  5886.  
  5887.  The operands of the "far" forms of JMP and CALL refer to other segments;
  5888.  therefore, the processor performs privilege checking. There are two ways a
  5889.  JMP or CALL can refer to another segment:
  5890.  
  5891.    1.  The operand selects the descriptor of another executable segment.
  5892.  
  5893.    2.  The operand selects a call gate descriptor. This gated form of
  5894.        transfer is discussed in a later section on call gates.
  5895.  
  5896.  As Figure 6-4 shows, two different privilege levels enter into a privilege
  5897.  check for a control transfer that does not use a call gate:
  5898.  
  5899.    1.  The CPL (current privilege level).
  5900.    2.  The DPL of the descriptor of the target segment.
  5901.  
  5902.  Normally the CPL is equal to the DPL of the segment that the processor is
  5903.  currently executing. CPL may, however, be greater than DPL if the conforming
  5904.  bit is set in the descriptor of the current executable segment. The
  5905.  processor keeps a record of the CPL cached in the CS register; this value
  5906.  can be different from the DPL in the descriptor of the code segment.
  5907.  
  5908.  The processor permits a JMP or CALL directly to another segment only if one
  5909.  of the following privilege rules is satisfied:
  5910.  
  5911.    ■  DPL of the target is equal to CPL.
  5912.  
  5913.    ■  The conforming bit of the target code-segment descriptor is set, and
  5914.       the DPL of the target is less than or equal to CPL.
  5915.  
  5916.  An executable segment whose descriptor has the conforming bit set is called
  5917.  a conforming segment. The conforming-segment mechanism permits sharing of
  5918.  procedures that may be called from various privilege levels but should
  5919.  execute at the privilege level of the calling procedure. Examples of such
  5920.  procedures include math libraries and some exception handlers. When control
  5921.  is transferred to a conforming segment, the CPL does not change. This is
  5922.  the only case when CPL may be unequal to the DPL of the current executable
  5923.  segment.
  5924.  
  5925.  Most code segments are not conforming. The basic rules of privilege above
  5926.  mean that, for nonconforming segments, control can be transferred without a
  5927.  gate only to executable segments at the same level of privilege. There is a
  5928.  need, however, to transfer control to (numerically) smaller privilege
  5929.  levels; this need is met by the CALL instruction when used with call-gate
  5930.  descriptors, which are explained in the next section. The JMP instruction
  5931.  may never transfer control to a nonconforming segment whose DPL does not
  5932.  equal CPL.
  5933.  
  5934.  
  5935.  Figure 6-4.  Privilege Check for Control Transfer without Gate
  5936.  
  5937.            16-BIT VISIBLE
  5938.               SELECTOR               INVISIBLE PART
  5939.          ╔═══════════════╦═══════════════════╦═══╦═══════════╗
  5940.          ║               ║                   ║CPL║           ║ CS
  5941.          ╚═══════════════╩═══════════════════╩═╤═╩═══════════╝
  5942.                                                │
  5943.                                                │        ╔═══════════╗
  5944.                                                └───────║ PRIVILEGE ║
  5945.                                            ┌───────────║ CHECK     ║
  5946.                                            │       ┌───║ BY CPU    ║
  5947.       CODE-SEGMENT DESCRIPTOR              │       │    ╚═══════════╝
  5948.                                            │       │
  5949.    31                23                15  │       │     7               0
  5950.   ╔═════════════════╪═╤═╤═╤═╤═════════╪═╤══╧══╤════╪════╪═════════════════╗
  5951.   ║                 │ │ │ │A│ LIMIT   │ │     │    │    │                 ║
  5952.   ║   BASE 31..24   │G│D│0│V│         │P│ DPL │    │    │   BASE 23..16   ║ 4
  5953.   ║                 │ │ │ │L│  19..16 │ │     │1│1│C│R│A│                 ║
  5954.   ╟─────────────────┴─┴─┴─┴─┴─────────┼─┴─────┴─┴─┴─┴─┴─┴─────────────────╢
  5955.   ║                                   │                                   ║
  5956.   ║        SEGMENT BASE 15..0         │        SEGMENT LIMIT 15..0        ║ 0
  5957.   ║                                   │                                   ║
  5958.   ╚═════════════════╪═════════════════╪═════════════════╪═════════════════╝
  5959.  
  5960.      CPL - CURRENT PRIVILEGE LEVEL
  5961.      DPL - DESCRIPTOR PRIVILEGE LEVEL
  5962.      C   - CONFORMING BIT
  5963.  
  5964.  
  5965.  6.3.4  Gate Descriptors Guard Procedure Entry Points
  5966.  
  5967.  To provide protection for control transfers among executable segments
  5968.  at different privilege levels, the 80386 uses gate descriptors. There are
  5969.  four kinds of gate descriptors:
  5970.  
  5971.    ■  Call gates
  5972.    ■  Trap gates
  5973.    ■  Interrupt gates
  5974.    ■  Task gates
  5975.  
  5976.  This chapter is concerned only with call gates. Task gates are used for
  5977.  task switching, and therefore are discussed in Chapter 7. Chapter 9
  5978.  explains how trap gates and interrupt gates are used by exceptions and
  5979.  interrupts. Figure 6-5 illustrates the format of a call gate. A call gate
  5980.  descriptor may reside in the GDT or in an LDT, but not in the IDT.
  5981.  
  5982.  A call gate has two primary functions:
  5983.  
  5984.    1.  To define an entry point of a procedure.
  5985.    2.  To specify the privilege level of the entry point.
  5986.  
  5987.  Call gate descriptors are used by call and jump instructions in the same
  5988.  manner as code segment descriptors. When the hardware recognizes that the
  5989.  destination selector refers to a gate descriptor, the operation of the
  5990.  instruction is expanded as determined by the contents of the call gate.
  5991.  
  5992.  The selector and offset fields of a gate form a pointer to the entry point
  5993.  of a procedure. A call gate guarantees that all transitions to another
  5994.  segment go to a valid entry point, rather than possibly into the middle of a
  5995.  procedure (or worse, into the middle of an instruction). The far pointer
  5996.  operand of the control transfer instruction does not point to the segment
  5997.  and offset of the target instruction; rather, the selector part of the
  5998.  pointer selects a gate, and the offset is not used. Figure 6-6 illustrates
  5999.  this style of addressing.
  6000.  
  6001.  As Figure 6-7 shows, four different privilege levels are used to check the
  6002.  validity of a control transfer via a call gate:
  6003.  
  6004.    1.  The CPL (current privilege level).
  6005.  
  6006.    2.  The RPL (requestor's privilege level) of the selector used to specify
  6007.        the call gate.
  6008.  
  6009.    3.  The DPL of the gate descriptor.
  6010.  
  6011.    4.  The DPL of the descriptor of the target executable segment.
  6012.  
  6013.  The DPL field of the gate descriptor determines what privilege levels can
  6014.  use the gate. One code segment can have several procedures that are intended
  6015.  for use by different privilege levels. For example, an operating system may
  6016.  have some services that are intended to be used by applications, whereas
  6017.  others may be intended only for use by other systems software.
  6018.  
  6019.  Gates can be used for control transfers to numerically smaller privilege
  6020.  levels or to the same privilege level (though they are not necessary for
  6021.  transfers to the same level). Only CALL instructions can use gates to
  6022.  transfer to smaller privilege levels. A gate may be used by a JMP
  6023.  instruction only to transfer to an executable segment with the same
  6024.  privilege level or to a conforming segment.
  6025.  
  6026.  For a JMP instruction to a nonconforming segment, both of the following
  6027.  privilege rules must be satisfied; otherwise, a general protection exception
  6028.  results.
  6029.  
  6030.     MAX (CPL,RPL) ≤ gate DPL
  6031.     target segment DPL = CPL
  6032.  
  6033.  For a CALL instruction (or for a JMP instruction to a conforming segment),
  6034.  both of the following privilege rules must be satisfied; otherwise, a
  6035.  general protection exception results.
  6036.  
  6037.     MAX (CPL,RPL) ≤ gate DPL
  6038.     target segment DPL ≤ CPL
  6039.  
  6040.  
  6041.  Figure 6-5.  Format of 80386 Call Gate
  6042.  
  6043.     31                23                15               7              0
  6044.    ╔═════════════════╪═════════════════╪═╤═════╤═════════╪═════╤═════════╗
  6045.    ║                                   │ │     │  TYPE   │     │  DWORD  ║
  6046.    ║           OFFSET 31..16           │P│ DPL │         │0 0 0│         ║ 4
  6047.    ║                                   │ │     │0 1 1 0 0│     │  COUNT  ║
  6048.    ╟───────────────────────────────────┼─┴─────┴─────────┴─────┴─────────╢
  6049.    ║                                   │                                 ║
  6050.    ║              SELECTOR             │           OFFSET 15..0          ║ 0
  6051.    ║                                   │                                 ║
  6052.    ╚═════════════════╪═════════════════╪═════════════════╪═══════════════╝
  6053.  
  6054.  
  6055.  Figure 6-6.  Indirect Transfer via Call Gate
  6056.  
  6057.           OPCODE                 OFFSET                   SELECTOR
  6058.         ╔═════════╦════════════════════════════════════╦═══════╦═╦═══╗
  6059.         ║  CALL   ║            (NOT USED)              ║ INDEX ║ ║RPL║
  6060.         ╚═════════╩════════════════════════════════════╩═══╤═══╩═╩═══╝
  6061.                                                            │
  6062.                        DESCRIPTOR TABLE                    │
  6063.                   ╔══════╤═════╤═════╤══════╗              │
  6064.                   ║      │     │     │      ║              │
  6065.                   ╟──────┴─────┼─────┴──────╢              │
  6066.                   ║            │            ║              │
  6067.                   ╚════════════╧════════════╝              │
  6068.                                                          │
  6069.                                                          │
  6070.                                                          │
  6071.                   ╔════════════╤═════╤══════╗              │
  6072.           GATE    ║   OFFSET   │ DPL │COUNT ║─────────────┘    EXECUTABLE
  6073.        DESCRIPTOR ╟────────────┼─────┴──────╢                     SEGMENT
  6074.            ┌──────╢  SELECTOR  │   OFFSET   ╟─────┐          ╔══════════════╗
  6075.            │      ╠══════╤═════╪═════╤══════╣     │          ║              ║
  6076.            │      ║      │     │     │      ║     │          ║              ║
  6077.            │      ╟──────┴─────┼─────┴──────╢     │          ║              ║
  6078.            │      ║            │            ║     │          ║              ║
  6079.            │      ╠══════╤═════╪═════╤══════╣     │          ║              ║
  6080.            │      ║      │     │     │      ║     └─────────║  PROCEDURE   ║
  6081.            │      ╟──────┴─────┼─────┴──────╢                ║              ║
  6082.            │      ║            │            ║                ║              ║
  6083.                  ╠══════╤═════╪═════╤══════╣                ║              ║
  6084.        EXECUTABLE ║ BASE │     │ DPL │ BASE ║                ║              ║
  6085.         SEGMENT   ╟──────┴─────┼─────┴──────╢     ┌─────────╚══════════════╝
  6086.        DESCRIPTOR ║    BASE    │            ╟─────┘
  6087.                   ╚════════════╧════════════╝
  6088.                                            
  6089.                                            
  6090.                                            
  6091.                   ╔══════╤═════╤═════╤══════╗
  6092.                   ║      │     │     │      ║
  6093.                   ╟──────┴─────┼─────┴──────╢
  6094.                   ║            │            ║
  6095.                   ╚════════════╧════════════╝
  6096.  
  6097.  
  6098.  Figure 6-7.  Privilege Check via Call Gate
  6099.  
  6100.                 16-BIT VISIBLE
  6101.                    SELECTOR            INVISIBLE DESCRIPTOR
  6102.               ╔═══════════════╦═══════════════════╦═══╦═══════════╗
  6103.            CS ║               ║                   ║CPL║           ║
  6104.               ╚═══════════════╩═══════════════════╩═╤═╩═══════════╝
  6105.                                                     │
  6106.                TARGET SELECTOR                      │        ╔═══════════╗
  6107.        ╔═══════════════════════╦═╦═══╗              └───────║ PRIVILEGE ║
  6108.        ║         INDEX         ║ ║RPL╟──────────────────────║ CHECK     ║
  6109.        ╚═══════════════════════╩═╩═══╝   ┌──────────────────║ BY        ║
  6110.                                          │               ┌──║ CPU       ║
  6111.                                   ┌──────┘               │   ╚═══════════╝
  6112.                                   │                      │
  6113.           GATE DESCRIPTOR                               │
  6114.      ╔═══════════════════════╦══════════╦═══════════╗    │
  6115.      ║        OFFSET         ║   DPL    ║   COUNT   ║    │
  6116.      ╠═══════════════════════╬══════════╩═══════════╣    │
  6117.      ║       SELECTOR        ║        OFFSET        ║    │
  6118.      ╚═══════════════════════╩══════════════════════╝    │
  6119.                                                          │
  6120.                                                          │
  6121.                            ╔═══════════╦═══════════╦═════╧════╦═══════════╗
  6122.                 EXECUTABLE ║   BASE    ║   LIMIT   ║    DPL   ║   BASE    ║
  6123.                  SEGMENT   ╠═══════════╩═══════════╬══════════╩═══════════╣
  6124.                 DESCRIPTOR ║          BASE         ║        LIMIT         ║
  6125.                            ╚═══════════════════════╩══════════════════════╝
  6126.  
  6127.              CPL     - CURRENT PRIVILEGE LEVEL
  6128.              RPL     - REQUESTOR'S PRIVILEGE LEVEL
  6129.              DPL     - DESCRIPTOR PRIVILEGE LEVEL
  6130.  
  6131.  
  6132.  6.3.4.1  Stack Switching
  6133.  
  6134.  If the destination code segment of the call gate is at a different
  6135.  privilege level than the CPL, an interlevel transfer is being requested.
  6136.  
  6137.  To maintain system integrity, each privilege level has a separate stack.
  6138.  These stacks assure sufficient stack space to process calls from less
  6139.  privileged levels. Without them, a trusted procedure would not work
  6140.  correctly if the calling procedure did not provide sufficient space on the
  6141.  caller's stack.
  6142.  
  6143.  The processor locates these stacks via the task state segment (see Figure
  6144.  6-8). Each task has a separate TSS, thereby permitting tasks to have
  6145.  separate stacks. Systems software is responsible for creating TSSs and
  6146.  placing correct stack pointers in them. The initial stack pointers in the
  6147.  TSS are strictly read-only values. The processor never changes them during
  6148.  the course of execution.
  6149.  
  6150.  When a call gate is used to change privilege levels, a new stack is
  6151.  selected by loading a pointer value from the Task State Segment (TSS). The
  6152.  processor uses the DPL of the target code segment (the new CPL) to index the
  6153.  initial stack pointer for PL 0, PL 1, or PL 2.
  6154.  
  6155.  The DPL of the new stack data segment must equal the new CPL; if it does
  6156.  not, a stack exception occurs. It is the responsibility of systems software
  6157.  to create stacks and stack-segment descriptors for all privilege levels that
  6158.  are used. Each stack must contain enough space to hold the old SS:ESP, the
  6159.  return address, and all parameters and local variables that may be required
  6160.  to process a call.
  6161.  
  6162.  As with intralevel calls, parameters for the subroutine are placed on the
  6163.  stack. To make privilege transitions transparent to the called procedure,
  6164.  the processor copies the parameters to the new stack. The count field of a
  6165.  call gate tells the processor how many doublewords (up to 31) to copy from
  6166.  the caller's stack to the new stack. If the count is zero, no parameters are
  6167.  copied.
  6168.  
  6169.  The processor performs the following stack-related steps in executing an
  6170.  interlevel CALL.
  6171.  
  6172.    1.  The new stack is checked to assure that it is large enough to hold
  6173.        the parameters and linkages; if it is not, a stack fault occurs with
  6174.        an error code of 0.
  6175.  
  6176.    2.  The old value of the stack registers SS:ESP is pushed onto the new
  6177.        stack as two doublewords.
  6178.  
  6179.    3.  The parameters are copied.
  6180.  
  6181.    4.  A pointer to the instruction after the CALL instruction (the former
  6182.        value of CS:EIP) is pushed onto the new stack. The final value of
  6183.        SS:ESP points to this return pointer on the new stack.
  6184.  
  6185.  Figure 6-9 illustrates the stack contents after a successful interlevel
  6186.  call.
  6187.  
  6188.  The TSS does not have a stack pointer for a privilege level 3 stack,
  6189.  because privilege level 3 cannot be called by any procedure at any other
  6190.  privilege level.
  6191.  
  6192.  Procedures that may be called from another privilege level and that require
  6193.  more than the 31 doublewords for parameters must use the saved SS:ESP link
  6194.  to access all parameters beyond the last doubleword copied.
  6195.  
  6196.  A call via a call gate does not check the values of the words copied onto
  6197.  the new stack. The called procedure should check each parameter for
  6198.  validity. A later section discusses how the ARPL, VERR, VERW, LSL, and LAR
  6199.  instructions can be used to check pointer values.
  6200.  
  6201.  
  6202.  Figure 6-8.  Initial Stack Pointers of TSS
  6203.  
  6204.                   31       23       15       7      0
  6205.                  ╔════════╪════════╪════════╪════════╗64
  6206.                                                     
  6207.                                                     
  6208.                                                     
  6209.                  ║                                   ║
  6210.                  ╠════════╪════════╪════════╪════════╣
  6211.                  ║              EFLAGS               ║24
  6212.                  ╠════════╪════════╪════════╪════════╣
  6213.                  ║     INSTRUCTION POINTER (EIP)     ║20
  6214.                  ╠════════╪════════╪════════╪════════╣
  6215.                  ║            CR3 (PDBR)             ║1C
  6216.                  ╠════════╪════════╦════════╪═════╦══╣  ─┐
  6217.                  ║00000000 00000000║       SS2    ║10║18 │
  6218.                  ╠════════╪════════╩════════╪═════╩══╣   │
  6219.                  ║               ESP2                ║14 │
  6220.                  ╠════════╪════════╦════════╪═════╦══╣   │
  6221.                  ║00000000 00000000║       SS1    ║01║10 │  INITIAL
  6222.                  ╠════════╪════════╩════════╪═════╩══╣   ├─ STACK
  6223.                  ║               ESP1                ║0C │  POINTERS
  6224.                  ╠════════╪════════╦════════╪═════╦══╣   │
  6225.                  ║00000000 00000000║       SS0    ║00║8  │
  6226.                  ╠════════╪════════╩════════╪═════╩══╣   │
  6227.                  ║               ESP0                ║4  │
  6228.                  ╠════════╪════════╦════════╪════════╣  ─┘
  6229.                  ║00000000 00000000║  TSS BACK LINK  ║0
  6230.                  ╚════════╪════════╩════════╪════════╝
  6231.  
  6232.  
  6233.  Figure 6-9.  Stack Contents after an Interlevel Call
  6234.  
  6235.                                              31            0     SS:ESP
  6236.                                             ╔═══════╪═══════╗──FROM TSS
  6237.                   31            0           ║▒▒▒▒▒▒▒│OLD SS ║
  6238.                  ╔═══════╪═══════╗          ╠═══════╪═══════╣
  6239.         D  O     ║               ║          ║    OLD ESP    ║
  6240.         I  F     ║               ║          ╠═══════╪═══════╣
  6241.         R        ║               ║          ║    PARM 3     ║
  6242.         E  E     ║               ║          ╠═══════╪═══════╣
  6243.         C  X     ║               ║          ║    PARM 2     ║
  6244.         T  P     ╠═══════╪═══════╣          ╠═══════╪═══════╣
  6245.         I  A     ║    PARM 3     ║          ║    PARM 1     ║
  6246.         O  N     ╠═══════╪═══════╣          ╠═══════╪═══════╣
  6247.         N  S     ║    PARM 2     ║          ║▒▒▒▒▒▒▒│OLD CS ║     NEW
  6248.            I     ╠═══════╪═══════╣  OLD     ╠═══════╪═══════╣    SS:ESP
  6249.          │ O     ║    PARM 1     ║  SS:ESP  ║    OLD EIP    ║      │
  6250.          │ N     ╠═══════╪═══════╣───┘     ╠═══════╪═══════╣─────┘
  6251.          │       ║               ║          ║               ║
  6252.                 ║               ║          ║               ║
  6253.                  ╚═══════╪═══════╝          ╚═══════╪═══════╝
  6254.                      OLD STACK                  NEW STACK
  6255.  
  6256.  
  6257.  6.3.4.2  Returning from a Procedure
  6258.  
  6259.  The "near" forms of the RET instruction transfer control within the current
  6260.  code segment and therefore are subject only to limit checking. The offset of
  6261.  the instruction following the corresponding CALL, is popped from the stack.
  6262.  The processor ensures that this offset does not exceed the limit of the
  6263.  current executable segment.
  6264.  
  6265.  The "far" form of the RET instruction pops the return pointer that was
  6266.  pushed onto the stack by a prior far CALL instruction. Under normal
  6267.  conditions, the return pointer is valid, because of its relation to the
  6268.  prior CALL or INT. Nevertheless, the processor performs privilege checking
  6269.  because of the possibility that the current procedure altered the pointer or
  6270.  failed to properly maintain the stack. The RPL of the CS selector popped
  6271.  off the stack by the return instruction identifies the privilege level of
  6272.  the calling procedure.
  6273.  
  6274.  An intersegment return instruction can change privilege levels, but only
  6275.  toward procedures of lesser privilege. When the RET instruction encounters a
  6276.  saved CS value whose RPL is numerically greater than the CPL, an interlevel
  6277.  return occurs. Such a return follows these steps:
  6278.  
  6279.    1.  The checks shown in Table 6-3 are made, and CS:EIP and SS:ESP are
  6280.        loaded with their former values that were saved on the stack.
  6281.  
  6282.    2.  The old SS:ESP (from the top of the current stack) value is adjusted
  6283.        by the number of bytes indicated in the RET instruction. The resulting
  6284.        ESP value is not compared to the limit of the stack segment. If ESP is
  6285.        beyond the limit, that fact is not recognized until the next stack
  6286.        operation. (The SS:ESP value of the returning procedure is not
  6287.        preserved; normally, this value is the same as that contained in the
  6288.        TSS.)
  6289.  
  6290.    3.  The contents of the DS, ES, FS, and GS segment registers are checked.
  6291.        If any of these registers refer to segments whose DPL is greater than
  6292.        the new CPL (excluding conforming code segments), the segment register
  6293.        is loaded with the null selector (INDEX = 0, TI = 0). The RET
  6294.        instruction itself does not signal exceptions in these cases;
  6295.        however, any subsequent memory reference that attempts to use a
  6296.        segment register that contains the null selector will cause a general
  6297.        protection exception. This prevents less privileged code from
  6298.        accessing more privileged segments using selectors left in the
  6299.        segment registers by the more privileged procedure.
  6300.  
  6301.  
  6302.  6.3.5  Some Instructions are Reserved for Operating System
  6303.  
  6304.  Instructions that have the power to affect the protection mechanism or to
  6305.  influence general system performance can only be executed by trusted
  6306.  procedures. The 80386 has two classes of such instructions:
  6307.  
  6308.    1.  Privileged instructions ── those used for system control.
  6309.  
  6310.    2.  Sensitive instructions ── those used for I/O and I/O related
  6311.        activities.
  6312.  
  6313.  
  6314.  Table 6-3. Interlevel Return Checks
  6315.  
  6316. ╓┌──────────────────────────────────────────┌───────────┌────────────────────╖
  6317.  Type of Check                              Exception  Error Code
  6318.  
  6319.  ESP is within current SS segment           SF          0
  6320.  ESP + 7 is within current SS segment       SF          0
  6321.  RPL of return CS is greater than CPL       GP          Return CS
  6322.  Return CS selector is not null             GP          Return CS
  6323.  Type of Check                              Exception  Error Code
  6324. Return CS selector is not null             GP          Return CS
  6325.  Return CS segment is within descriptor
  6326.  table limit                                GP          Return CS
  6327.  Return CS descriptor is a code segment     GP          Return CS
  6328.  Return CS segment is present               NP          Return CS
  6329.  DPL of return nonconforming code
  6330.  segment = RPL of return CS, or DPL of
  6331.  return conforming code segment ≤ RPL
  6332.  of return CS                               GP          Return CS
  6333.  ESP + N + 15 is within SS segment         SF          Return SS
  6334.  SS selector at ESP + N + 12 is not null    GP          Return SS
  6335.  SS selector at ESP + N + 12 is within
  6336.  descriptor table limit                     GP          Return SS
  6337.  SS descriptor is writable data segment     GP          Return SS
  6338.  SS segment is present                      SF          Return SS
  6339.  Saved SS segment DPL = RPL of saved
  6340.  CS                                         GP          Return SS
  6341.  Saved SS selector RPL = Saved SS
  6342.  segment DPL                                GP          Return SS
  6343.  
  6344.  
  6345.  6.3.5.1  Privileged Instructions
  6346.  
  6347.  The instructions that affect system data structures can only be executed
  6348.  when CPL is zero. If the CPU encounters one of these instructions when CPL
  6349.  is greater than zero, it signals a general protection exception. These
  6350.  instructions include:
  6351.  
  6352.     CLTS                 ── Clear Task─Switched Flag
  6353.     HLT                  ── Halt Processor
  6354.     LGDT                 ── Load GDL Register
  6355.     LIDT                 ── Load IDT Register
  6356.     LLDT                 ── Load LDT Register
  6357.     LMSW                 ── Load Machine Status Word
  6358.     LTR                  ── Load Task Register
  6359.     MOV to/from CRn      ── Move to Control Register n
  6360.     MOV to /from DRn     ── Move to Debug Register n
  6361.     MOV to/from TRn      ── Move to Test Register n
  6362.  
  6363.  
  6364.  6.3.5.2  Sensitive Instructions
  6365.  
  6366.  Instructions that deal with I/O need to be restricted but also need to be
  6367.  executed by procedures executing at privilege levels other than zero. The
  6368.  mechanisms for restriction of I/O operations are covered in detail in
  6369.  Chapter 8, "Input/Output".
  6370.  
  6371.  
  6372.  6.3.6  Instructions for Pointer Validation
  6373.  
  6374.  Pointer validation is an important part of locating programming errors.
  6375.  Pointer validation is necessary for maintaining isolation between the
  6376.  privilege levels. Pointer validation consists of the following steps:
  6377.  
  6378.    1.  Check if the supplier of the pointer is entitled to access the
  6379.        segment.
  6380.  
  6381.    2.  Check if the segment type is appropriate to its intended use.
  6382.  
  6383.    3.  Check if the pointer violates the segment limit.
  6384.  
  6385.  Although the 80386 processor automatically performs checks 2 and 3 during
  6386.  instruction execution, software must assist in performing the first check.
  6387.  The unprivileged instruction ARPL is provided for this purpose. Software can
  6388.  also explicitly perform steps 2 and 3 to check for potential violations
  6389.  (rather than waiting for an exception). The unprivileged instructions LAR,
  6390.  LSL, VERR, and VERW are provided for this purpose.
  6391.  
  6392.  LAR (Load Access Rights) is used to verify that a pointer refers to a
  6393.  segment of the proper privilege level and type. LAR has one operand──a
  6394.  selector for a descriptor whose access rights are to be examined. The
  6395.  descriptor must be visible at the privilege level which is the maximum of
  6396.  the CPL and the selector's RPL. If the descriptor is visible, LAR obtains a
  6397.  masked form of the second doubleword of the descriptor, masks this value
  6398.  with 00FxFF00H, stores the result into the specified 32-bit destination
  6399.  register, and sets the zero flag. (The x indicates that the corresponding
  6400.  four bits of the stored value are undefined.) Once loaded, the access-rights
  6401.  bits can be tested. All valid descriptor types can be tested by the LAR
  6402.  instruction. If the RPL or CPL is greater than DPL, or if the selector is
  6403.  outside the table limit, no access-rights value is returned, and the zero
  6404.  flag is cleared. Conforming code segments may be accessed from any privilege
  6405.  level.
  6406.  
  6407.  LSL (Load Segment Limit) allows software to test the limit of a descriptor.
  6408.  If the descriptor denoted by the given selector (in memory or a register) is
  6409.  visible at the CPL, LSL loads the specified 32-bit register with a 32-bit,
  6410.  byte granular, unscrambled limit that is calculated from fragmented limit
  6411.  fields and the G-bit of that descriptor. This can only be done for segments
  6412.  (data, code, task state, and local descriptor tables); gate descriptors are
  6413.  inaccessible. (Table 6-4 lists in detail which types are valid and which
  6414.  are not.) Interpreting the limit is a function of the segment type. For
  6415.  example, downward expandable data segments treat the limit differently than
  6416.  code segments do. For both LAR and LSL, the zero flag (ZF) is set if the
  6417.  loading was performed; otherwise, the ZF is cleared.
  6418.  
  6419.  
  6420.  Table 6-4. Valid Descriptor Types for LSL
  6421.  
  6422.      Type   Descriptor Type             Valid?
  6423.      Code
  6424.  
  6425.      0      (invalid)                   NO
  6426.      1      Available 286 TSS           YES
  6427.      2      LDT                         YES
  6428.      3      Busy 286 TSS                YES
  6429.      4      286 Call Gate               NO
  6430.      5      Task Gate                   NO
  6431.      6      286 Trap Gate               NO
  6432.      7      286 Interrupt Gate          NO
  6433.      8      (invalid)                   NO
  6434.      9      Available 386 TSS           YES
  6435.      A      (invalid)                   NO
  6436.      B      Busy 386 TSS                YES
  6437.      C      386 Call Gate               NO
  6438.      D      (invalid)                   NO
  6439.      E      386 Trap Gate               NO
  6440.      F      386 Interrupt Gate          NO
  6441.  
  6442.  
  6443.  6.3.6.1  Descriptor Validation
  6444.  
  6445.  The 80386 has two instructions, VERR and VERW, which determine whether a
  6446.  selector points to a segment that can be read or written at the current
  6447.  privilege level. Neither instruction causes a protection fault if the result
  6448.  is negative.
  6449.  
  6450.  VERR (Verify for Reading) verifies a segment for reading and loads ZF with
  6451.  1 if that segment is readable from the current privilege level. VERR checks
  6452.  that:
  6453.  
  6454.    ■  The selector points to a descriptor within the bounds of the GDT or
  6455.       LDT.
  6456.  
  6457.    ■  It denotes a code or data segment descriptor.
  6458.  
  6459.    ■  The segment is readable and of appropriate privilege level.
  6460.  
  6461.  The privilege check for data segments and nonconforming code segments is
  6462.  that the DPL must be numerically greater than or equal to both the CPL and
  6463.  the selector's RPL. Conforming segments are not checked for privilege level.
  6464.  
  6465.  VERW (Verify for Writing) provides the same capability as VERR for
  6466.  verifying writability. Like the VERR instruction, VERW loads ZF if the
  6467.  result of the writability check is positive. The instruction checks that the
  6468.  descriptor is within bounds, is a segment descriptor, is writable, and that
  6469.  its DPL is numerically greater or equal to both the CPL and the selector's
  6470.  RPL. Code segments are never writable, conforming or not.
  6471.  
  6472.  
  6473.  6.3.6.2  Pointer Integrity and RPL
  6474.  
  6475.  The Requestor's Privilege Level (RPL) feature can prevent inappropriate use
  6476.  of pointers that could corrupt the operation of more privileged code or data
  6477.  from a less privileged level.
  6478.  
  6479.  A common example is a file system procedure, FREAD (file_id, n_bytes,
  6480.  buffer_ptr). This hypothetical procedure reads data from a file into a
  6481.  buffer, overwriting whatever is there. Normally, FREAD would be available at
  6482.  the user level, supplying only pointers to the file system procedures and
  6483.  data located and operating at a privileged level. Normally, such a procedure
  6484.  prevents user-level procedures from directly changing the file tables.
  6485.  However, in the absence of a standard protocol for checking pointer
  6486.  validity, a user-level procedure could supply a pointer into the file tables
  6487.  in place of its buffer pointer, causing the FREAD procedure to corrupt them
  6488.  unwittingly.
  6489.  
  6490.  Use of RPL can avoid such problems. The RPL field allows a privilege
  6491.  attribute to be assigned to a selector. This privilege attribute would
  6492.  normally indicate the privilege level of the code which generated the
  6493.  selector. The 80386 processor automatically checks the RPL of any selector
  6494.  loaded into a segment register to determine whether the RPL allows access.
  6495.  
  6496.  To take advantage of the processor's checking of RPL, the called procedure
  6497.  need only ensure that all selectors passed to it have an RPL at least as
  6498.  high (numerically) as the original caller's CPL. This action guarantees that
  6499.  selectors are not more trusted than their supplier. If one of the selectors
  6500.  is used to access a segment that the caller would not be able to access
  6501.  directly, i.e., the RPL is numerically greater than the DPL, then a
  6502.  protection fault will result when that selector is loaded into a segment
  6503.  register.
  6504.  
  6505.  ARPL (Adjust Requestor's Privilege Level) adjusts the RPL field of a
  6506.  selector to become the larger of its original value and the value of the RPL
  6507.  field in a specified register. The latter is normally loaded from the image
  6508.  of the caller's CS register which is on the stack. If the adjustment changes
  6509.  the selector's RPL, ZF (the zero flag) is set; otherwise, ZF is cleared.
  6510.  
  6511.  
  6512.  6.4  Page-Level Protection
  6513.  
  6514.  Two kinds of protection are related to pages:
  6515.  
  6516.    1.  Restriction of addressable domain.
  6517.    2.  Type checking.
  6518.  
  6519.  
  6520.  6.4.1  Page-Table Entries Hold Protection Parameters
  6521.  
  6522.  Figure 6-10 highlights the fields of PDEs and PTEs that control access to
  6523.  pages.
  6524.  
  6525.  
  6526.  Figure 6-10.  Protection Fields of Page Table Entries
  6527.  
  6528.        31                                  12 11          7           0
  6529.       ╔══════════════════════════════════════╪═══════╤═══╤═╤═╤═══╤═╤═╤═╗
  6530.       ║▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒│▒▒▒▒▒▒▒│▒▒▒│▒│▒│▒▒▒│U│R│▒║
  6531.       ║▒▒▒▒▒▒PAGE FRAME ADDRESS 31..12▒▒▒▒▒▒▒│▒AVAIL▒│0▒0│D│A│0▒0│/│/│P║
  6532.       ║▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒│▒▒▒▒▒▒▒│▒▒▒│▒│▒│▒▒▒│S│W│▒║
  6533.       ╚══════════════════════════════════════╪═══════╧═══╧═╧═╧═══╧═╧═╧═╝
  6534.        R/W     - READ/WRITE
  6535.        U/S     - USER/SUPERVISOR
  6536.  
  6537.  
  6538.  6.4.1.1  Restricting Addressable Domain
  6539.  
  6540.  The concept of privilege for pages is implemented by assigning each page to
  6541.  one of two levels:
  6542.  
  6543.    1.  Supervisor level (U/S=0) ── for the operating system and other systems
  6544.        software and related data.
  6545.  
  6546.    2.  User level (U/S=1) ── for applications procedures and data.
  6547.  
  6548.  The current level (U or S) is related to CPL.  If CPL is 0, 1, or 2, the
  6549.  processor is executing at supervisor level. If CPL is 3, the processor is
  6550.  executing at user level.
  6551.  
  6552.  When the processor is executing at supervisor level, all pages are
  6553.  addressable, but, when the processor is executing at user level, only pages
  6554.  that belong to the user level are addressable.
  6555.  
  6556.  
  6557.  6.4.1.2  Type Checking
  6558.  
  6559.  At the level of page addressing, two types are defined:
  6560.  
  6561.    1.  Read-only access (R/W=0)
  6562.    2.  Read/write access (R/W=1)
  6563.  
  6564.  When the processor is executing at supervisor level, all pages are both
  6565.  readable and writable. When the processor is executing at user level, only
  6566.  pages that belong to user level and are marked for read/write access are
  6567.  writable; pages that belong to supervisor level are neither readable nor
  6568.  writable from user level.
  6569.  
  6570.  
  6571.  6.4.2  Combining Protection of Both Levels of Page Tables
  6572.  
  6573.  For any one page, the protection attributes of its page directory entry may
  6574.  differ from those of its page table entry. The 80386 computes the effective
  6575.  protection attributes for a page by examining the protection attributes in
  6576.  both the directory and the page table.  Table 6-5 shows the effective
  6577.  protection provided by the possible combinations of protection attributes.
  6578.  
  6579.  
  6580.  6.4.3  Overrides to Page Protection
  6581.  
  6582.  Certain accesses are checked as if they are privilege-level 0 references,
  6583.  even if CPL = 3:
  6584.  
  6585.    ■  LDT, GDT, TSS, IDT references.
  6586.    ■  Access to inner stack during ring-crossing CALL/INT.
  6587.  
  6588.  
  6589.  6.5  Combining Page and Segment Protection
  6590.  
  6591.  When paging is enabled, the 80386 first evaluates segment protection, then
  6592.  evaluates page protection. If the processor detects a protection violation
  6593.  at either the segment or the page level, the requested operation cannot
  6594.  proceed; a protection exception occurs instead.
  6595.  
  6596.  For example, it is possible to define a large data segment which has some
  6597.  subunits that are read-only and other subunits that are read-write.  In this
  6598.  case, the page directory (or page table) entries for the read-only subunits
  6599.  would have the U/S and R/W bits set to x0, indicating no write rights for
  6600.  all the pages described by that directory entry (or for individual pages).
  6601.  This technique might be used, for example, in a UNIX-like system to define
  6602.  a large data segment, part of which is read only (for shared data or ROMmed
  6603.  constants). This enables UNIX-like systems to define a "flat" data space as
  6604.  one large segment, use "flat" pointers to address within this "flat" space,
  6605.  yet be able to protect shared data, shared files mapped into the virtual
  6606.  space, and supervisor areas.
  6607.  
  6608.  
  6609.  Table 6-5. Combining Directory and Page Protection
  6610.  
  6611.  Page Directory Entry     Page Table Entry      Combined Protection
  6612.    U/S          R/W         U/S      R/W          U/S         R/W
  6613.  
  6614.    S-0          R-0         S-0      R-0           S           x
  6615.    S-0          R-0         S-0      W-1           S           x
  6616.    S-0          R-0         U-1      R-0           S           x
  6617.    S-0          R-0         U-1      W-1           S           x
  6618.    S-0          W-1         S-0      R-0           S           x
  6619.    S-0          W-1         S-0      W-1           S           x
  6620.    S-0          W-1         U-1      R-0           S           x
  6621.    S-0          W-1         U-1      W-1           S           x
  6622.    U-1          R-0         S-0      R-0           S           x
  6623.    U-1          R-0         S-0      W-1           S           x
  6624.    U-1          R-0         U-1      R-0           U           R
  6625.    U-1          R-0         U-1      W-1           U           R
  6626.    U-1          W-1         S-0      R-0           S           x
  6627.    U-1          W-1         S-0      W-1           S           x
  6628.    U-1          W-1         U-1      R-0           U           R
  6629.    U-1          W-1         U-1      W-1           U           W
  6630.  
  6631.  ───────────────────────────────────────────────────────────────────────────
  6632.  NOTE
  6633.    S ── Supervisor
  6634.    R ── Read only
  6635.    U ── User
  6636.    W ── Read and Write
  6637.    x indicates that when the combined U/S attribute is S, the R/W attribute
  6638.    is not checked.
  6639.  ───────────────────────────────────────────────────────────────────────────
  6640.  
  6641.  
  6642.  Chapter 7  Multitasking
  6643.  
  6644.  ────────────────────────────────────────────────────────────────────────────
  6645.  
  6646.  To provide efficient, protected multitasking, the 80386 employs several
  6647.  special data structures. It does not, however, use special instructions to
  6648.  control multitasking; instead, it interprets ordinary control-transfer
  6649.  instructions differently when they refer to the special data structures. The
  6650.  registers and data structures that support multitasking are:
  6651.  
  6652.    ■  Task state segment
  6653.    ■  Task state segment descriptor
  6654.    ■  Task register
  6655.    ■  Task gate descriptor
  6656.  
  6657.  With these structures the 80386 can rapidly switch execution from one task
  6658.  to another, saving the context of the original task so that the task can be
  6659.  restarted later. In addition to the simple task switch, the 80386 offers two
  6660.  other task-management features:
  6661.  
  6662.    1.  Interrupts and exceptions can cause task switches (if needed in the
  6663.        system design). The processor not only switches automatically to the
  6664.        task that handles the interrupt or exception, but it automatically
  6665.        switches back to the interrupted task when the interrupt or exception
  6666.        has been serviced. Interrupt tasks may interrupt lower-priority
  6667.        interrupt tasks to any depth.
  6668.  
  6669.    2.  With each switch to another task, the 80386 can also switch to
  6670.        another LDT and to another page directory. Thus each task can have a
  6671.        different logical-to-linear mapping and a different linear-to-physical
  6672.        mapping. This is yet another protection feature, because tasks can be
  6673.        isolated and prevented from interfering with one another.
  6674.  
  6675.  
  6676.  7.1  Task State Segment
  6677.  
  6678.  All the information the processor needs in order to manage a task is stored
  6679.  in a special type of segment, a task state segment (TSS). Figure 7-1 shows
  6680.  the format of a TSS for executing 80386 tasks. (Another format is used for
  6681.  executing 80286 tasks; refer to Chapter 13.)
  6682.  
  6683.  The fields of a TSS belong to two classes:
  6684.  
  6685.    1.  A dynamic set that the processor updates with each switch from the
  6686.        task. This set includes the fields that store:
  6687.  
  6688.        ■  The general registers (EAX, ECX, EDX, EBX, ESP, EBP, ESI, EDI).
  6689.  
  6690.        ■  The segment registers (ES, CS, SS, DS, FS, GS).
  6691.  
  6692.        ■  The flags register (EFLAGS).
  6693.  
  6694.        ■  The instruction pointer (EIP).
  6695.  
  6696.        ■  The selector of the TSS of the previously executing task (updated
  6697.           only when a return is expected).
  6698.  
  6699.    2.  A static set that the processor reads but does not change. This set
  6700.        includes the fields that store:
  6701.  
  6702.        ■  The selector of the task's LDT.
  6703.  
  6704.        ■  The register (PDBR) that contains the base address of the task's
  6705.           page directory (read only when paging is enabled).
  6706.  
  6707.        ■  Pointers to the stacks for privilege levels 0-2.
  6708.  
  6709.        ■  The T-bit (debug trap bit) which causes the processor to raise a
  6710.           debug exception when a task switch occurs. (Refer to Chapter 12
  6711.           for more information on debugging.)
  6712.  
  6713.        ■  The I/O map base (refer to Chapter 8 for more information on the
  6714.           use of the I/O map).
  6715.  
  6716.  Task state segments may reside anywhere in the linear space. The only case
  6717.  that requires caution is when the TSS spans a page boundary and the
  6718.  higher-addressed page is not present. In this case, the processor raises an
  6719.  exception if it encounters the not-present page while reading the TSS during
  6720.  a task switch. Such an exception can be avoided by either of two strategies:
  6721.  
  6722.    1.  By allocating the TSS so that it does not cross a page boundary.
  6723.  
  6724.    2.  By ensuring that both pages are either both present or both
  6725.        not-present at the time of a task switch. If both pages are
  6726.        not-present, then the page-fault handler must make both pages present
  6727.        before restarting the instruction that caused the task switch.
  6728.  
  6729.  
  6730.  Figure 7-1.  80386 32-Bit Task State Segment
  6731.  
  6732.        31              23              15              7             0
  6733.       ╔═══════════════╪═══════════════╬═══════════════╪═════════════╦═╗
  6734.       ║          I/O MAP BASE         ║ 0 0 0 0 0 0 0   0 0 0 0 0 0 ║T║64
  6735.       ╟───────────────┼───────────────╫───────────────┼─────────────╨─╢
  6736.       ║0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0║              LDT              ║60
  6737.       ╟───────────────┼───────────────╫───────────────┼───────────────╢
  6738.       ║0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0║              GS               ║5C
  6739.       ╟───────────────┼───────────────╫───────────────┼───────────────╢
  6740.       ║0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0║              FS               ║58
  6741.       ╟───────────────┼───────────────╫───────────────┼───────────────╢
  6742.       ║0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0║              DS               ║54
  6743.       ╟───────────────┼───────────────╫───────────────┼───────────────╢
  6744.       ║0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0║              SS               ║50
  6745.       ╟───────────────┼───────────────╫───────────────┼───────────────╢
  6746.       ║0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0║              CS               ║4C
  6747.       ╟───────────────┼───────────────╫───────────────┼───────────────╢
  6748.       ║0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0║              ES               ║48
  6749.       ╟───────────────┼───────────────╫───────────────┼───────────────╢
  6750.       ║                              EDI                              ║44
  6751.       ╟───────────────┼───────────────┼───────────────┼───────────────╢
  6752.       ║                              ESI                              ║40
  6753.       ╟───────────────┼───────────────┼───────────────┼───────────────╢
  6754.       ║                              EBP                              ║3C
  6755.       ╟───────────────┼───────────────┼───────────────┼───────────────╢
  6756.       ║                              ESP                              ║38
  6757.       ╟───────────────┼───────────────┼───────────────┼───────────────╢
  6758.       ║                              EBX                              ║34
  6759.       ╟───────────────┼───────────────┼───────────────┼───────────────╢
  6760.       ║                              EDX                              ║30
  6761.       ╠═══════════════╪═══════════════╪═══════════════╪═══════════════╣
  6762.       ║                              ECX                              ║2C
  6763.       ╟───────────────┼───────────────┼───────────────┼───────────────╢
  6764.       ║                              EAX                              ║28
  6765.       ╟───────────────┼───────────────┼───────────────┼───────────────╢
  6766.       ║                            EFLAGS                             ║24
  6767.       ╟───────────────┼───────────────┼───────────────┼───────────────╢
  6768.       ║                    INSTRUCTION POINTER (EIP)                  ║20
  6769.       ╟───────────────┼───────────────┼───────────────┼───────────────╢
  6770.       ║                          CR3  (PDPR)                          ║1C
  6771.       ╟───────────────┼───────────────╫───────────────┼───────────────╢
  6772.       ║0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0║              SS2              ║18
  6773.       ╟───────────────┼───────────────╫───────────────┼───────────────╢
  6774.       ║                             ESP2                              ║14
  6775.       ╟───────────────┼───────────────╫───────────────┼───────────────╢
  6776.       ║0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0║              SS1              ║10
  6777.       ╟───────────────┼───────────────╫───────────────┼───────────────╢
  6778.       ║                             ESP1                              ║0C
  6779.       ╟───────────────┼───────────────╫───────────────┼───────────────╢
  6780.       ║0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0║              SS0              ║8
  6781.       ╟───────────────┼───────────────╫───────────────┼───────────────╢
  6782.       ║                             ESP0                              ║4
  6783.       ╟───────────────┼───────────────╫───────────────┼───────────────╢
  6784.       ║0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0║   BACK LINK TO PREVIOUS TSS   ║0
  6785.       ╚═══════════════╪═══════════════╬═══════════════╪═══════════════╝
  6786.  
  6787.  ────────────────────────────────────────────────────────────────────────────
  6788.  NOTE
  6789.        0 MEANS INTEL RESERVED. DO NOT DEFINE.
  6790.  ────────────────────────────────────────────────────────────────────────────
  6791.  
  6792.  
  6793.  7.2  TSS Descriptor
  6794.  
  6795.  The task state segment, like all other segments, is defined by a
  6796.  descriptor. Figure 7-2 shows the format of a TSS descriptor.
  6797.  
  6798.  The B-bit in the type field indicates whether the task is busy. A type code
  6799.  of 9 indicates a non-busy task; a type code of 11 indicates a busy task.
  6800.  Tasks are not reentrant. The B-bit allows the processor to detect an attempt
  6801.  to switch to a task that is already busy.
  6802.  
  6803.  The BASE, LIMIT, and DPL fields and the G-bit and P-bit have functions
  6804.  similar to their counterparts in data-segment descriptors. The LIMIT field,
  6805.  however, must have a value equal to or greater than 103. An attempt to
  6806.  switch to a task whose TSS descriptor has a limit less that 103 causes an
  6807.  exception. A larger limit is permissible, and a larger limit is required if
  6808.  an I/O permission map is present. A larger limit may also be convenient for
  6809.  systems software if additional data is stored in the same segment as the
  6810.  TSS.
  6811.  
  6812.  A procedure that has access to a TSS descriptor can cause a task switch. In
  6813.  most systems the DPL fields of TSS descriptors should be set to zero, so
  6814.  that only trusted software has the right to perform task switching.
  6815.  
  6816.  Having access to a TSS-descriptor does not give a procedure the right to
  6817.  read or modify a TSS. Reading and modification can be accomplished only with
  6818.  another descriptor that redefines the TSS as a data segment. An attempt to
  6819.  load a TSS descriptor into any of the segment registers (CS, SS, DS, ES, FS,
  6820.  GS) causes an exception.
  6821.  
  6822.  TSS descriptors may reside only in the GDT. An attempt to identify a TSS
  6823.  with a selector that has TI=1 (indicating the current LDT) results in an
  6824.  exception.
  6825.  
  6826.  
  6827.  Figure 7-2.  TSS Descriptor for 32-bit TSS
  6828.  
  6829.    31                23                15                7               0
  6830.   ╔═════════════════╪═╤═╤═╤═╤═════════╪═╤═════╤═════════╪═════════════════╗
  6831.   ║                 │ │ │ │A│ LIMIT   │ │     │  TYPE   │                 ║
  6832.   ║   BASE 31..24   │G│0│0│V│         │P│ DPL │         │   BASE 23..16   ║ 4
  6833.   ║                 │ │ │ │L│  19..16 │ │     │0│1│0│B│1│                 ║
  6834.   ╟─────────────────┴─┴─┴─┴─┴─────────┼─┴─────┴─┴─┴─┴─┴─┴─────────────────╢
  6835.   ║                                   │                                   ║
  6836.   ║             BASE 15..0            │             LIMIT 15..0           ║ 0
  6837.   ║                                   │                                   ║
  6838.   ╚═════════════════╪═════════════════╪═════════════════╪═════════════════╝
  6839.  
  6840.  
  6841.  7.3  Task Register
  6842.  
  6843.  The task register (TR) identifies the currently executing task by pointing
  6844.  to the TSS. Figure 7-3 shows the path by which the processor accesses the
  6845.  current TSS.
  6846.  
  6847.  The task register has both a "visible" portion (i.e., can be read and
  6848.  changed by instructions) and an "invisible" portion (maintained by the
  6849.  processor to correspond to the visible portion; cannot be read by any
  6850.  instruction). The selector in the visible portion selects a TSS descriptor
  6851.  in the GDT. The processor uses the invisible portion to cache the base and
  6852.  limit values from the TSS descriptor. Holding the base and limit in a
  6853.  register makes execution of the task more efficient, because the processor
  6854.  does not need to repeatedly fetch these values from memory when it
  6855.  references the TSS of the current task.
  6856.  
  6857.  The instructions LTR and STR are used to modify and read the visible
  6858.  portion of the task register. Both instructions take one operand, a 16-bit
  6859.  selector located in memory or in a general register.
  6860.  
  6861.  LTR (Load task register) loads the visible portion of the task register
  6862.  with the selector operand, which must select a TSS descriptor in the GDT.
  6863.  LTR also loads the invisible portion with information from the TSS
  6864.  descriptor selected by the operand. LTR is a privileged instruction; it may
  6865.  be executed only when CPL is zero. LTR is generally used during system
  6866.  initialization to give an initial value to the task register; thereafter,
  6867.  the contents of TR are changed by task switch operations.
  6868.  
  6869.  STR (Store task register) stores the visible portion of the task register
  6870.  in a general register or memory word. STR is not privileged.
  6871.  
  6872.  
  6873.  Figure 7-3.  Task Register
  6874.  
  6875.                            ╔═════════════════════════╗
  6876.                            ║                         ║
  6877.                            ║                         ║
  6878.                            ║       TASK STATE        ║
  6879.                            ║        SEGMENT          ║─────────┐
  6880.                            ║                         ║          │
  6881.                            ║                         ║          │
  6882.                            ╚═════════════════════════╝          │
  6883.             16-BIT VISIBLE                                     │
  6884.                REGISTER                │   HIDDEN REGISTER      │
  6885.         ╔════════════════════╦═════════╧══════════╦═════════════╧══════╗
  6886.      TR ║      SELECTOR      ║      (BASE)        ║       (LIMT)       ║
  6887.         ╚═════════╤══════════╩════════════════════╩════════════════════╝
  6888.                   │                                         
  6889.                   │                    └─────────────────┐   │
  6890.                   │          GLOBAL DESCRIPTOR TABLE     │   │
  6891.                   │        ╒═════════════════════════╕   │   │
  6892.                   │        |     TSS DESCRIPTOR      |   │   │
  6893.                   │        ╔══════╦═════╦═════╦══════╗   │   │
  6894.                   │        ║      ║     ║     ║      ╟───┘   │
  6895.                   │        ╠══════╩═════╬═════╩══════╣       │
  6896.                   └───────║            ║            ╟───────┘
  6897.                            ╚════════════╩════════════╝
  6898.                            |                         |
  6899.                            ╘═════════════════════════╛
  6900.  
  6901.  
  6902.  7.4  Task Gate Descriptor
  6903.  
  6904.  A task gate descriptor provides an indirect, protected reference to a TSS.
  6905.  Figure 7-4 illustrates the format of a task gate.
  6906.  
  6907.  The SELECTOR field of a task gate must refer to a TSS descriptor. The value
  6908.  of the RPL in this selector is not used by the processor.
  6909.  
  6910.  The DPL field of a task gate controls the right to use the descriptor to
  6911.  cause a task switch. A procedure may not select a task gate descriptor
  6912.  unless the maximum of the selector's RPL and the CPL of the procedure is
  6913.  numerically less than or equal to the DPL of the descriptor. This constraint
  6914.  prevents untrusted procedures from causing a task switch. (Note that when a
  6915.  task gate is used, the DPL of the target TSS descriptor is not used for
  6916.  privilege checking.)
  6917.  
  6918.  A procedure that has access to a task gate has the power to cause a task
  6919.  switch, just as a procedure that has access to a TSS descriptor. The 80386
  6920.  has task gates in addition to TSS descriptors to satisfy three needs:
  6921.  
  6922.    1.  The need for a task to have a single busy bit. Because the busy-bit
  6923.        is stored in the TSS descriptor, each task should have only one such
  6924.        descriptor. There may, however, be several task gates that select the
  6925.        single TSS descriptor.
  6926.  
  6927.    2.  The need to provide selective access to tasks. Task gates fulfill
  6928.        this need, because they can reside in LDTs and can have a DPL that is
  6929.        different from the TSS descriptor's DPL. A procedure that does not
  6930.        have sufficient privilege to use the TSS descriptor in the GDT (which
  6931.        usually has a DPL of 0) can still switch to another task if it has
  6932.        access to a task gate for that task in its LDT. With task gates,
  6933.        systems software can limit the right to cause task switches to
  6934.        specific tasks.
  6935.  
  6936.    3.  The need for an interrupt or exception to cause a task switch. Task
  6937.        gates may also reside in the IDT, making it possible for interrupts
  6938.        and exceptions to cause task switching. When interrupt or exception
  6939.        vectors to an IDT entry that contains a task gate, the 80386 switches
  6940.        to the indicated task. Thus, all tasks in the system can benefit from
  6941.        the protection afforded by isolation from interrupt tasks.
  6942.  
  6943.  Figure 7-5 illustrates how both a task gate in an LDT and a task gate in
  6944.  the IDT can identify the same task.
  6945.  
  6946.  
  6947.  Figure 7-4.  Task Gate Descriptor
  6948.  
  6949.     31                23               15                7             0
  6950.    ╔═════════════════╪════════════════╪═╤═════╤═════════╪═════════════════╗
  6951.    ║▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒│ │     │         │▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒║
  6952.    ║▒▒▒▒▒▒▒▒▒▒▒▒(NOT USED)▒▒▒▒▒▒▒▒▒▒▒▒│P│ DPL │0 0 1 0 1│▒▒▒(NOT USED)▒▒▒▒║ 4
  6953.    ║▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒│ │     │         │▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒║
  6954.    ╟──────────────────────────────────┼─┴─────┴─────────┴─────────────────╢
  6955.    ║                                  │▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒║
  6956.    ║              SELECTOR            │▒▒▒▒▒▒▒▒▒▒▒▒(NOT USED)▒▒▒▒▒▒▒▒▒▒▒▒▒║ 0
  6957.    ║                                  │▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒║
  6958.    ╚═════════════════╪════════════════╪═════════════════╪═════════════════╝
  6959.  
  6960.  
  6961.  Figure 7-5.  Task Gate Indirectly Identifies Task
  6962.  
  6963.           LOCAL DESCRIPTOR TABLE              INTERRUPT DESCRIPTOR TABLE
  6964.         ╒═════════════════════════╕           ╒═════════════════════════╕
  6965.         |                         |           |                         |
  6966.         |        TASK GATE        |           |        TASK GATE        |
  6967.         ╔══════╤═════╤═════╤══════╗           ╔══════╤═════╤═════╤══════╗
  6968.         ║      │     │     │      ║           ║      │     │     │      ║
  6969.         ╟──────┴─────┼─────┴──────╢           ╟──────┴─────┼─────┴──────╢
  6970.      ┌──╢            │            ║        ┌──╢            │            ║
  6971.      │  ╚════════════╧════════════╝        │  ╚════════════╧════════════╝
  6972.      │  |                         |        │  |                         |
  6973.      │  |                         |        │  |                         |
  6974.      │  ╘═════════════════════════╛        │  ╘═════════════════════════╛
  6975.      └────────────────┐  ┌─────────────────┘
  6976.                       │  │    GLOBAL DESCRIPTOR TABLE
  6977.                       │  │  ╒═════════════════════════╕
  6978.                       │  │  |                         |
  6979.                       │  │  |     TASK DESCRIPTOR     |
  6980.                       │  │  ╔══════╤═════╤═════╤══════╗
  6981.                       │  │  ║      │     │     │      ║
  6982.                       │  └─╟──────┴─────┼─────┴──────╢
  6983.                       └────║            │            ╟──┐
  6984.                             ╚════════════╧════════════╝  │
  6985.                             |                         |  │
  6986.                             |                         |  │
  6987.                             ╘═════════════════════════╛  │
  6988.                                                          │
  6989.                             ╔═════════════════════════╗  │
  6990.                             ║                         ║  │
  6991.                             ║                         ║  │
  6992.                             ║                         ║  │
  6993.                             ║       TASK STATE        ║  │
  6994.                             ║         SEGMENT         ║  │
  6995.                             ║                         ║  │
  6996.                             ║                         ║  │
  6997.                             ║                         ║  │
  6998.                             ╚═════════════════════════╝─┘
  6999.  
  7000.  
  7001.  7.5  Task Switching
  7002.  
  7003.  The 80386 switches execution to another task in any of four cases:
  7004.  
  7005.    1.  The current task executes a JMP or CALL that refers to a TSS
  7006.        descriptor.
  7007.  
  7008.    2.  The current task executes a JMP or CALL that refers to a task gate.
  7009.  
  7010.    3.  An interrupt or exception vectors to a task gate in the IDT.
  7011.  
  7012.    4.  The current task executes an IRET when the NT flag is set.
  7013.  
  7014.  JMP, CALL, IRET, interrupts, and exceptions are all ordinary mechanisms of
  7015.  the 80386 that can be used in circumstances that do not require a task
  7016.  switch. Either the type of descriptor referenced or the NT (nested task) bit
  7017.  in the flag word distinguishes between the standard mechanism and the
  7018.  variant that causes a task switch.
  7019.  
  7020.  To cause a task switch, a JMP or CALL instruction can refer either to a TSS
  7021.  descriptor or to a task gate. The effect is the same in either case: the
  7022.  80386 switches to the indicated task.
  7023.  
  7024.  An exception or interrupt causes a task switch when it vectors to a task
  7025.  gate in the IDT. If it vectors to an interrupt or trap gate in the IDT, a
  7026.  task switch does not occur. Refer to Chapter 9 for more information on the
  7027.  interrupt mechanism.
  7028.  
  7029.  Whether invoked as a task or as a procedure of the interrupted task, an
  7030.  interrupt handler always returns control to the interrupted procedure in the
  7031.  interrupted task. If the NT flag is set, however, the handler is an
  7032.  interrupt task, and the IRET switches back to the interrupted task.
  7033.  
  7034.  A task switching operation involves these steps:
  7035.  
  7036.    1.  Checking that the current task is allowed to switch to the designated
  7037.        task. Data-access privilege rules apply in the case of JMP or CALL
  7038.        instructions. The DPL of the TSS descriptor or task gate must be less
  7039.        than or equal to the maximum of CPL and the RPL of the gate selector.
  7040.        Exceptions, interrupts, and IRETs are permitted to switch tasks
  7041.        regardless of the DPL of the target task gate or TSS descriptor.
  7042.  
  7043.    2.  Checking that the TSS descriptor of the new task is marked present
  7044.        and has a valid limit. Any errors up to this point occur in the
  7045.        context of the outgoing task. Errors are restartable and can be
  7046.        handled in a way that is transparent to applications procedures.
  7047.  
  7048.    3.  Saving the state of the current task. The processor finds the base
  7049.        address of the current TSS cached in the task register. It copies the
  7050.        registers into the current TSS (EAX, ECX, EDX, EBX, ESP, EBP, ESI,
  7051.        EDI, ES, CS, SS, DS, FS, GS, and the flag register). The EIP field of
  7052.        the TSS points to the instruction after the one that caused the task
  7053.        switch.
  7054.  
  7055.    4.  Loading the task register with the selector of the incoming task's
  7056.        TSS descriptor, marking the incoming task's TSS descriptor as busy,
  7057.        and setting the TS (task switched) bit of the MSW. The selector is
  7058.        either the operand of a control transfer instruction or is taken from
  7059.        a task gate.
  7060.  
  7061.    5.  Loading the incoming task's state from its TSS and resuming
  7062.        execution. The registers loaded are the LDT register; the flag
  7063.        register; the general registers EIP, EAX, ECX, EDX, EBX, ESP, EBP,
  7064.        ESI, EDI; the segment registers ES, CS, SS, DS, FS, and GS; and PDBR.
  7065.        Any errors detected in this step occur in the context of the incoming
  7066.        task. To an exception handler, it appears that the first instruction
  7067.        of the new task has not yet executed.
  7068.  
  7069.  Note that the state of the outgoing task is always saved when a task switch
  7070.  occurs. If execution of that task is resumed, it starts after the
  7071.  instruction that caused the task switch. The registers are restored to the
  7072.  values they held when the task stopped executing.
  7073.  
  7074.  Every task switch sets the TS (task switched) bit in the MSW (machine
  7075.  status word). The TS flag is useful to systems software when a coprocessor
  7076.  (such as a numerics coprocessor) is present. The TS bit signals that the
  7077.  context of the coprocessor may not correspond to the current 80386 task.
  7078.  Chapter 11 discusses the TS bit and coprocessors in more detail.
  7079.  
  7080.  Exception handlers that field task-switch exceptions in the incoming task
  7081.  (exceptions due to tests 4 thru 16 of Table 7-1) should be cautious about
  7082.  taking any action that might load the selector that caused the exception.
  7083.  Such an action will probably cause another exception, unless the exception
  7084.  handler first examines the selector and fixes any potential problem.
  7085.  
  7086.  The privilege level at which execution resumes in the incoming task is
  7087.  neither restricted nor affected by the privilege level at which the outgoing
  7088.  task was executing. Because the tasks are isolated by their separate address
  7089.  spaces and TSSs and because privilege rules can be used to prevent improper
  7090.  access to a TSS, no privilege rules are needed to constrain the relation
  7091.  between the CPLs of the tasks. The new task begins executing at the
  7092.  privilege level indicated by the RPL of the CS selector value that is loaded
  7093.  from the TSS.
  7094.  
  7095.  
  7096.  Table 7-1. Checks Made during a Task Switch
  7097.  
  7098. ╓┌────────┌──────────────────────────────────┌────────────┌──────────────────╖
  7099.  Test     Test Description                   Exception   Error Code Selects
  7100.  Test     Test Description                   Exception   Error Code Selects
  7101.  
  7102.    1      Incoming TSS descriptor is         NP           Incoming TSS
  7103.           present
  7104.    2      Incoming TSS descriptor is         GP           Incoming TSS
  7105.           marked not-busy
  7106.    3      Limit of incoming TSS is           TS           Incoming TSS
  7107.           greater than or equal to 103
  7108.  
  7109.               ── All register and selector values are loaded ──
  7110.  
  7111.    4      LDT selector of incoming           TS           Incoming TSS
  7112.           task is valid
  7113.    5      LDT of incoming task is            TS           Incoming TSS
  7114.           present
  7115.    6      CS selector is valid              TS           Code segment
  7116.    7      Code segment is present            NP           Code segment
  7117.    8      Code segment DPL matches           TS           Code segment
  7118.           CS RPL
  7119.    9      Stack segment is valid            GP           Stack segment
  7120.   10      Stack segment is present           SF           Stack segment
  7121.  Test     Test Description                   Exception   Error Code Selects
  7122.  10      Stack segment is present           SF           Stack segment
  7123.   11      Stack segment DPL = CPL            SF           Stack segment
  7124.   12      Stack-selector RPL = CPL           GP           Stack segment
  7125.   13      DS, ES, FS, GS selectors are       GP           Segment
  7126.           valid
  7127.  
  7128.   14      DS, ES, FS, GS segments            GP           Segment
  7129.           are readable
  7130.   15      DS, ES, FS, GS segments            NP           Segment
  7131.           are present
  7132.   16      DS, ES, FS, GS segment DPL         GP           Segment
  7133.           ≥ CPL (unless these are
  7134.           conforming segments)
  7135.  
  7136.  
  7137.  7.6  Task Linking
  7138.  
  7139.  The back-link field of the TSS and the NT (nested task) bit of the flag
  7140.  word together allow the 80386 to automatically return to a task that CALLed
  7141.  another task or was interrupted by another task. When a CALL instruction, an
  7142.  interrupt instruction, an external interrupt, or an exception causes a
  7143.  switch to a new task, the 80386 automatically fills the back-link of the new
  7144.  TSS with the selector of the outgoing task's TSS and, at the same time,
  7145.  sets the NT bit in the new task's flag register. The NT flag indicates
  7146.  whether the back-link field is valid. The new task releases control by
  7147.  executing an IRET instruction. When interpreting an IRET, the 80386 examines
  7148.  the NT flag. If NT is set, the 80386 switches back to the task selected by
  7149.  the back-link field. Table 7-2 summarizes the uses of these fields.
  7150.  
  7151.  
  7152.  Table 7-2. Effect of Task Switch on BUSY, NT, and Back-Link
  7153.  
  7154.  Affected Field      Effect of JMP      Effect of            Effect of
  7155.                      Instruction        CALL Instruction     IRET Instruction
  7156.  
  7157.  Busy bit of         Set, must be       Set, must be 0       Unchanged,
  7158.  incoming task       0 before           before               must be set
  7159.  
  7160.  Busy bit of         Cleared            Unchanged            Cleared
  7161.  outgoing task                          (already set)
  7162.  
  7163.  NT bit of           Cleared            Set                  Unchanged
  7164.  incoming task
  7165.  
  7166.  NT bit of           Unchanged          Unchanged            Cleared
  7167.  outgoing task
  7168.  
  7169.  Back-link of        Unchanged          Set to outgoing      Unchanged
  7170.  incoming task                          TSS selector
  7171.  
  7172.  Back-link of        Unchanged          Unchanged            Unchanged
  7173.  outgoing task
  7174.  
  7175.  
  7176.  7.6.1  Busy Bit Prevents Loops
  7177.  
  7178.  The B-bit (busy bit) of the TSS descriptor ensures the integrity of the
  7179.  back-link. A chain of back-links may grow to any length as interrupt tasks
  7180.  interrupt other interrupt tasks or as called tasks call other tasks. The
  7181.  busy bit ensures that the CPU can detect any attempt to create a loop. A
  7182.  loop would indicate an attempt to reenter a task that is already busy;
  7183.  however, the TSS is not a reentrable resource.
  7184.  
  7185.  The processor uses the busy bit as follows:
  7186.  
  7187.    1.  When switching to a task, the processor automatically sets the busy
  7188.        bit of the new task.
  7189.  
  7190.    2.  When switching from a task, the processor automatically clears the
  7191.        busy bit of the old task if that task is not to be placed on the
  7192.        back-link chain (i.e., the instruction causing the task switch is JMP
  7193.        or IRET). If the task is placed on the back-link chain, its busy bit
  7194.        remains set.
  7195.  
  7196.    3.  When switching to a task, the processor signals an exception if the
  7197.        busy bit of the new task is already set.
  7198.  
  7199.  By these actions, the processor prevents a task from switching to itself or
  7200.  to any task that is on a back-link chain, thereby preventing invalid reentry
  7201.  into a task.
  7202.  
  7203.  The busy bit is effective even in multiprocessor configurations, because
  7204.  the processor automatically asserts a bus lock when it sets or clears the
  7205.  busy bit. This action ensures that two processors do not invoke the same
  7206.  task at the same time. (Refer to Chapter 11 for more on multiprocessing.)
  7207.  
  7208.  
  7209.  7.6.2  Modifying Task Linkages
  7210.  
  7211.  Any modification of the linkage order of tasks should be accomplished only
  7212.  by software that can be trusted to correctly update the back-link and the
  7213.  busy-bit. Such changes may be needed to resume an interrupted task before
  7214.  the task that interrupted it. Trusted software that removes a task from the
  7215.  back-link chain must follow one of the following policies:
  7216.  
  7217.    1.  First change the back-link field in the TSS of the interrupting task,
  7218.        then clear the busy-bit in the TSS descriptor of the task removed from
  7219.        the list.
  7220.  
  7221.    2.  Ensure that no interrupts occur between updating the back-link chain
  7222.        and the busy bit.
  7223.  
  7224.  
  7225.  7.7  Task Address Space
  7226.  
  7227.  The LDT selector and PDBR fields of the TSS give software systems designers
  7228.  flexibility in utilization of segment and page mapping features of the
  7229.  80386. By appropriate choice of the segment and page mappings for each task,
  7230.  tasks may share address spaces, may have address spaces that are largely
  7231.  distinct from one another, or may have any degree of sharing between these
  7232.  two extremes.
  7233.  
  7234.  The ability for tasks to have distinct address spaces is an important
  7235.  aspect of 80386 protection. A module in one task cannot interfere with a
  7236.  module in another task if the modules do not have access to the same address
  7237.  spaces. The flexible memory management features of the 80386 allow systems
  7238.  designers to assign areas of shared address space to those modules of
  7239.  different tasks that are designed to cooperate with each other.
  7240.  
  7241.  
  7242.  7.7.1  Task Linear-to-Physical Space Mapping
  7243.  
  7244.  The choices for arranging the linear-to-physical mappings of tasks fall
  7245.  into two general classes:
  7246.  
  7247.    1.  One linear-to-physical mapping shared among all tasks.
  7248.  
  7249.        When paging is not enabled, this is the only possibility. Without page
  7250.        tables, all linear addresses map to the same physical addresses.
  7251.  
  7252.        When paging is enabled, this style of linear-to-physical mapping
  7253.        results from using one page directory for all tasks. The linear space
  7254.        utilized may exceed the physical space available if the operating
  7255.        system also implements page-level virtual memory.
  7256.  
  7257.    2.  Several partially overlapping linear-to-physical mappings.
  7258.  
  7259.        This style is implemented by using a different page directory for each
  7260.        task. Because the PDBR (page directory base register) is loaded from
  7261.        the TSS with each task switch, each task may have a different page
  7262.        directory.
  7263.  
  7264.  In theory, the linear address spaces of different tasks may map to
  7265.  completely distinct physical addresses. If the entries of different page
  7266.  directories point to different page tables and the page tables point to
  7267.  different pages of physical memory, then the tasks do not share any physical
  7268.  addresses.
  7269.  
  7270.  In practice, some portion of the linear address spaces of all tasks must
  7271.  map to the same physical addresses. The task state segments must lie in a
  7272.  common space so that the mapping of TSS addresses does not change while the
  7273.  processor is reading and updating the TSSs during a task switch. The linear
  7274.  space mapped by the GDT should also be mapped to a common physical space;
  7275.  otherwise, the purpose of the GDT is defeated. Figure 7-6 shows how the
  7276.  linear spaces of two tasks can overlap in the physical space by sharing
  7277.  page tables.
  7278.  
  7279.  
  7280.  7.7.2  Task Logical Address Space
  7281.  
  7282.  By itself, a common linear-to-physical space mapping does not enable
  7283.  sharing of data among tasks. To share data, tasks must also have a common
  7284.  logical-to-linear space mapping; i.e., they must also have access to
  7285.  descriptors that point into a shared linear address space. There are three
  7286.  ways to create common logical-to-physical address-space mappings:
  7287.  
  7288.    1.  Via the GDT. All tasks have access to the descriptors in the GDT. If
  7289.        those descriptors point into a linear-address space that is mapped to
  7290.        a common physical-address space for all tasks, then the tasks can
  7291.        share data and instructions.
  7292.  
  7293.    2.  By sharing LDTs. Two or more tasks can use the same LDT if the LDT
  7294.        selectors in their TSSs select the same LDT segment. Those
  7295.        LDT-resident descriptors that point into a linear space that is mapped
  7296.        to a common physical space permit the tasks to share physical memory.
  7297.        This method of sharing is more selective than sharing by the GDT; the
  7298.        sharing can be limited to specific tasks. Other tasks in the system
  7299.        may have different LDTs that do not give them access to the shared
  7300.        areas.
  7301.  
  7302.    3.  By descriptor aliases in LDTs. It is possible for certain descriptors
  7303.        of different LDTs to point to the same linear address space. If that
  7304.        linear address space is mapped to the same physical space by the page
  7305.        mapping of the tasks involved, these descriptors permit the tasks to
  7306.        share the common space. Such descriptors are commonly called
  7307.        "aliases". This method of sharing is even more selective than the
  7308.        prior two; other descriptors in the LDTs may point to distinct linear
  7309.        addresses or to linear addresses that are not shared.
  7310.  
  7311.  
  7312.  Figure 7-6. Partially-Overlapping Linear Spaces
  7313.  
  7314.           TSSs                                              PAGE FRAMES
  7315.                                                            ╔══════════╗
  7316.        TASK A TSS    PAGE DIRECTORIES     PAGE TABLES      ║  TASK A  ║
  7317.       ╔══════════╗     ╔═══════════╗     ╔═══════════╗  ┌─║   PAGE   ║
  7318.       ║          ║     ║           ║     ║           ║  │  ╚══════════╝
  7319.       ║          ║     ╠═══════════╣     ╠═══════════╣  │  ╔══════════╗
  7320.       ║          ║     ║           ║     ║    PTE    ╟──┘  ║  TASK A  ║
  7321.       ║          ║     ╠═══════════╣     ╠═══════════╣  ┌─║   PAGE   ║
  7322.       ║          ║     ║           ║     ║    PTE    ╟──┘  ╚══════════╝
  7323.       ╠══════════╣     ╠═══════════╣     ╠═══════════╣     ╔══════════╗
  7324.       ║   PDBR   ╟────║    PDE    ╟────║    PTE    ╟──┐  ║  TASK A  ║
  7325.       ╠══════════╣     ╠═══════════╣     ╚═══════════╝  └─║   PAGE   ║
  7326.       ║          ║     ║    PDE    ╟──┐    SHARED PT       ╚══════════╝
  7327.       ╚══════════╝     ╚═══════════╝  │  ╔═══════════╗     ╔══════════╗
  7328.                                       │  ║           ║     ║  SHARED  ║
  7329.                                       │  ╠═══════════╣  ┌─║   PAGE   ║
  7330.                                       │  ║           ║  │  ╚══════════╝
  7331.                                       │  ╠═══════════╣  │  ╔══════════╗
  7332.                                       │  ║    PTE    ╟──┘  ║  SHARED  ║
  7333.                                       │  ╠═══════════╣  ┌─║   PAGE   ║
  7334.                                       ├─║    PTE    ╟──┘  ╚══════════╝
  7335.        TASK B TSS                     │  ╚═══════════╝     ╔══════════╗
  7336.       ╔══════════╗     ╔═══════════╗  │                    ║  TASK B  ║
  7337.       ║          ║     ║           ║  │                ┌──║   PAGE   ║
  7338.       ║          ║     ╠═══════════╣  │  ╔═══════════╗ │   ╚══════════╝
  7339.       ║          ║     ║           ║  │  ║           ║ │   ╔══════════╗
  7340.       ║          ║     ╠═══════════╣  │  ╠═══════════╣ │   ║  TASK B  ║
  7341.       ║          ║     ║           ║  │  ║           ║ │ ┌║   PAGE   ║
  7342.       ╠══════════╣     ╠═══════════╣  │  ╠═══════════╣ │ │ ╚══════════╝
  7343.       ║   PDBR   ╟────║    PDE    ╟──┘  ║    PTE    ╟─┘ │  PAGE FRAMES
  7344.       ╠══════════╣     ╠═══════════╣     ╠═══════════╣   │
  7345.       ║          ║     ║    PDE    ╟────║    PTE    ╟───┘
  7346.       ╚══════════╝     ╚═══════════╝     ╚═══════════╝
  7347.           TSSs       PAGE DIRECTORIES     PAGE TABLES
  7348.  
  7349.  
  7350.  Chapter 8  Input/Output
  7351.  
  7352.  ────────────────────────────────────────────────────────────────────────────
  7353.  
  7354.  This chapter presents the I/O features of the 80386 from the following
  7355.  perspectives:
  7356.  
  7357.    ■  Methods of addressing I/O ports
  7358.  
  7359.    ■  Instructions that cause I/O operations
  7360.  
  7361.    ■  Protection as it applies to the use of I/O instructions and I/O port
  7362.       addresses.
  7363.  
  7364.  
  7365.  8.1  I/O Addressing
  7366.  
  7367.  The 80386 allows input/output to be performed in either of two ways:
  7368.  
  7369.    ■  By means of a separate I/O address space (using specific I/O
  7370.       instructions)
  7371.  
  7372.    ■  By means of memory-mapped I/O (using general-purpose operand
  7373.       manipulationinstructions).
  7374.  
  7375.  
  7376.  8.1.1  I/O Address Space
  7377.  
  7378.  The 80386 provides a separate I/O address space, distinct from physical
  7379.  memory, that can be used to address the input/output ports that are used for
  7380.  external 16 devices. The I/O address space consists of 2^(16) (64K)
  7381.  individually addressable 8-bit ports; any two consecutive 8-bit ports can be
  7382.  treated as a 16-bit port; and four consecutive 8-bit ports can be treated
  7383.  as a 32-bit port. Thus, the I/O address space can accommodate up to 64K
  7384.  8-bit ports, up to 32K 16-bit ports, or up to 16K 32-bit ports.
  7385.  
  7386.  The program can specify the address of the port in two ways. Using an
  7387.  immediate byte constant, the program can specify:
  7388.  
  7389.    ■  256 8-bit ports numbered 0 through 255.
  7390.    ■  128 16-bit ports numbered 0, 2, 4, . . . , 252, 254.
  7391.    ■  64 32-bit ports numbered 0, 4, 8, . . . , 248, 252.
  7392.  
  7393.  Using a value in DX, the program can specify:
  7394.  
  7395.    ■  8-bit ports numbered 0 through 65535
  7396.    ■  16-bit ports numbered 0, 2, 4, . . . , 65532, 65534
  7397.    ■  32-bit ports numbered 0, 4, 8, . . . , 65528, 65532
  7398.  
  7399.  The 80386 can transfer 32, 16, or 8 bits at a time to a device located in
  7400.  the I/O space. Like doublewords in memory, 32-bit ports should be aligned at
  7401.  addresses evenly divisible by four so that the 32 bits can be transferred in
  7402.  a single bus access. Like words in memory, 16-bit ports should be aligned at
  7403.  even-numbered addresses so that the 16 bits can be transferred in a single
  7404.  bus access. An 8-bit port may be located at either an even or odd address.
  7405.  
  7406.  The instructions IN and OUT move data between a register and a port in the
  7407.  I/O address space. The instructions INS and OUTS move strings of data
  7408.  between the memory address space and ports in the I/O address space.
  7409.  
  7410.  
  7411.  8.1.2  Memory-Mapped I/O
  7412.  
  7413.  I/O devices also may be placed in the 80386 memory address space. As long
  7414.  as the devices respond like memory components, they are indistinguishable to
  7415.  the processor.
  7416.  
  7417.  Memory-mapped I/O provides additional programming flexibility. Any
  7418.  instruction that references memory may be used to access an I/O port located
  7419.  in the memory space. For example, the MOV instruction can transfer data
  7420.  between any register and a port; and the AND, OR, and TEST instructions may
  7421.  be used to manipulate bits in the internal registers of a device (see Figure
  7422.  8-1). Memory-mapped I/O performed via the full instruction set maintains
  7423.  the full complement of addressing modes for selecting the desired I/O
  7424.  device (e.g., direct address, indirect address, base register, index
  7425.  register, scaling).
  7426.  
  7427.  Memory-mapped I/O, like any other memory reference, is subject to access
  7428.  protection and control when executing in protected mode. Refer to Chapter 6
  7429.  for a discussion of memory protection.
  7430.  
  7431.  
  7432.  8.2  I/O Instructions
  7433.  
  7434.  The I/O instructions of the 80386 provide access to the processor's I/O
  7435.  ports for the transfer of data to and from peripheral devices. These
  7436.  instructions have as one operand the address of a port in the I/O address
  7437.  space. There are two classes of I/O instruction:
  7438.  
  7439.    1.  Those that transfer a single item (byte, word, or doubleword) located
  7440.        in a register.
  7441.  
  7442.    2.  Those that transfer strings of items (strings of bytes, words, or
  7443.        doublewords) located in memory. These are known as "string I/O
  7444.        instructions" or "block I/O instructions".
  7445.  
  7446.  
  7447.  8.2.1  Register I/O Instructions
  7448.  
  7449.  The I/O instructions IN and OUT are provided to move data between I/O ports
  7450.  and the EAX (32-bit I/O), the AX (16-bit I/O), or AL (8-bit I/O) general
  7451.  registers. IN and OUT instructions address I/O ports either directly, with
  7452.  the address of one of up to 256 port addresses coded in the instruction, or
  7453.  indirectly via the DX register to one of up to 64K port addresses.
  7454.  
  7455.  IN (Input from Port) transfers a byte, word, or doubleword from an input
  7456.  port to AL, AX, or EAX. If a program specifies AL with the IN instruction,
  7457.  the processor transfers 8 bits from the selected port to AL. If a program
  7458.  specifies AX with the IN instruction, the processor transfers 16 bits from
  7459.  the port to AX. If a program specifies EAX with the IN instruction, the
  7460.  processor transfers 32 bits from the port to EAX.
  7461.  
  7462.  OUT (Output to Port) transfers a byte, word, or doubleword to an output
  7463.  port from AL, AX, or EAX. The program can specify the number of the port
  7464.  using the same methods as the IN instruction.
  7465.  
  7466.  
  7467.  Figure 8-1.  Memory-Mapped I/O
  7468.  
  7469.                  MEMORY
  7470.               ADDRESS SPACE                        I/O DEVICE 1
  7471.             ╔═══════════════╗                  ╔═══════════════════╗
  7472.             ║               ║                  ║ INTERNAL REGISTER ║
  7473.             ╟───────────────╢ ─ ─ ─ ─ ─ ─ ─ ─ ─║─╔═══════════════╗ ║
  7474.             ║               ║                  ║ ║               ║ ║
  7475.             ╟───────────────╢ ─ ─ ─ ─ ─ ─ ─ ─ ─║─╚═══════════════╝ ║
  7476.             ║               ║                  ╚═══════════════════╝
  7477.             ║               ║
  7478.             ║               ║
  7479.             ║               ║
  7480.             ║               ║                      I/O DEVICE 2
  7481.             ║               ║                  ╔═══════════════════╗
  7482.             ║               ║                  ║ INTERNAL REGISTER ║
  7483.             ╟───────────────╢ ─ ─ ─ ─ ─ ─ ─ ─ ─║─╔═══════════════╗ ║
  7484.             ║               ║                  ║ ║               ║ ║
  7485.             ╟───────────────╢ ─ ─ ─ ─ ─ ─ ─ ─ ─║─╚═══════════════╝ ║
  7486.             ║               ║                  ╚═══════════════════╝
  7487.             ╚═══════════════╝
  7488.  
  7489.  
  7490.  8.2.2  Block I/O Instructions
  7491.  
  7492.  The block (or string) I/O instructions INS and OUTS move blocks of data
  7493.  between I/O ports and memory space. Block I/O instructions use the DX
  7494.  register to specify the address of a port in the I/O address space. INS and
  7495.  OUTS use DX to specify:
  7496.  
  7497.    ■  8-bit ports numbered 0 through 65535
  7498.    ■  16-bit ports numbered 0, 2, 4, . . . , 65532, 65534
  7499.    ■  32-bit ports numbered 0, 4, 8, . . . , 65528, 65532
  7500.  
  7501.  Block I/O instructions use either SI or DI to designate the source or
  7502.  destination memory address. For each transfer, SI or DI are automatically
  7503.  either incremented or decremented as specified by the direction bit in the
  7504.  flags register.
  7505.  
  7506.  INS and OUTS, when used with repeat prefixes, cause block input or output
  7507.  operations. REP, the repeat prefix, modifies INS and OUTS to provide a means
  7508.  of transferring blocks of data between an I/O port and memory. These block
  7509.  I/O instructions are string primitives (refer also to Chapter 3 for more on
  7510.  string primitives). They simplify programming and increase the speed of data
  7511.  transfer by eliminating the need to use a separate LOOP instruction or an
  7512.  intermediate register to hold the data.
  7513.  
  7514.  The string I/O primitives can operate on byte strings, word strings, or
  7515.  doubleword strings. After each transfer, the memory address in ESI or EDI is
  7516.  updated by 1 for byte operands, by 2 for word operands, or by 4 for
  7517.  doubleword operands. The value in the direction flag (DF) determines whether
  7518.  the processor automatically increments ESI or EDI (DF=0) or whether it
  7519.  automatically decrements these registers (DF=1).
  7520.  
  7521.  INS (Input String from Port) transfers a byte or a word string element from
  7522.  an input port to memory. The mnemonics INSB, INSW, and INSD are variants
  7523.  that explicitly specify the size of the operand. If a program specifies
  7524.  INSB, the processor transfers 8 bits from the selected port to the memory
  7525.  location indicated by ES:EDI. If a program specifies INSW, the processor
  7526.  transfers 16 bits from the port to the memory location indicated by ES:EDI.
  7527.  If a program specifies INSD, the processor transfers 32 bits from the port
  7528.  to the memory location indicated by ES:EDI. The destination segment register
  7529.  choice (ES) cannot be changed for the INS instruction. Combined with the REP
  7530.  prefix, INS moves a block of information from an input port to a series of
  7531.  consecutive memory locations.
  7532.  
  7533.  OUTS (Output String to Port) transfers a byte, word, or doubleword string
  7534.  element to an output port from memory. The mnemonics OUTSB, OUTSW, and OUTSD
  7535.  are variants that explicitly specify the size of the operand. If a program
  7536.  specifies OUTSB, the processor transfers 8 bits from the memory location
  7537.  indicated by ES:EDI to the the selected port. If a program specifies OUTSW,
  7538.  the processor transfers 16 bits from the memory location indicated by ES:EDI
  7539.  to the the selected port. If a program specifies OUTSD, the processor
  7540.  transfers 32 bits from the memory location indicated by ES:EDI to the the
  7541.  selected port. Combined with the REP prefix, OUTS moves a block of
  7542.  information from a series of consecutive memory locations indicated by
  7543.  DS:ESI to an output port.
  7544.  
  7545.  
  7546.  8.3  Protection and I/O
  7547.  
  7548.  Two mechanisms provide protection for I/O functions:
  7549.  
  7550.    1.  The IOPL field in the EFLAGS register defines the right to use
  7551.        I/O-related instructions.
  7552.  
  7553.    2.  The I/O permission bit map of a 80386 TSS segment defines the right
  7554.        to use ports in the I/O address space.
  7555.  
  7556.  These mechanisms operate only in protected mode, including virtual 8086
  7557.  mode; they do not operate in real mode. In real mode, there is no protection
  7558.  of the I/O space; any procedure can execute I/O instructions, and any I/O
  7559.  port can be addressed by the I/O instructions.
  7560.  
  7561.  
  7562.  8.3.1  I/O Privilege Level
  7563.  
  7564.  Instructions that deal with I/O need to be restricted but also need to be
  7565.  executed by procedures executing at privilege levels other than zero. For
  7566.  this reason, the processor uses two bits of the flags register to store the
  7567.  I/O privilege level (IOPL). The IOPL defines the privilege level
  7568.  needed to execute I/O-related instructions.
  7569.  
  7570.  The following instructions can be executed only if CPL ≤ IOPL:
  7571.  
  7572.  IN      ── Input
  7573.  INS     ── Input String
  7574.  OUT     ── Output
  7575.  OUTS    ── Output String
  7576.  CLI     ── Clear Interrupt-Enable Flag
  7577.  STI     ── Set Interrupt-Enable
  7578.  
  7579.  These instructions are called "sensitive" instructions, because they are
  7580.  sensitive to IOPL.
  7581.  
  7582.  To use sensitive instructions, a procedure must execute at a privilege
  7583.  level at least as privileged as that specified by the IOPL (CPL ≤ IOPL). Any
  7584.  attempt by a less privileged procedure to use a sensitive instruction
  7585.  results in a general protection exception.
  7586.  
  7587.  Because each task has its own unique copy of the flags register, each task
  7588.  can have a different IOPL. A task whose primary function is to perform I/O
  7589.  (a device driver) can benefit from having an IOPL of three, thereby
  7590.  permitting all procedures of the task to performI/O. Other tasks typically
  7591.  have IOPL set to zero or one, reserving the right to perform I/O
  7592.  instructions for the most privileged procedures.
  7593.  
  7594.  A task can change IOPL only with the POPF instruction; however, such
  7595.  changes are privileged. No procedure may alter IOPL (the I/O privilege level
  7596.  in the flag register) unless the procedure is executing at privilege level
  7597.  0. An attempt by a less privileged procedure to alter IOPL does not result
  7598.  in an exception; IOPL simply remains unaltered.
  7599.  
  7600.  The POPF instruction may be used in addition to CLI and STI to alter the
  7601.  interrupt-enable flag (IF); however, changes to IF by POPF are
  7602.  IOPL-sensitive. A procedure may alter IF with a POPF instruction only when
  7603.  executing at a level that is at least as privileged as IOPL. An attempt by a
  7604.  less privileged procedure to alter IF in this manner does not result in an
  7605.  exception; IF simply remains unaltered.
  7606.  
  7607.  
  7608.  8.3.2  I/O Permission Bit Map
  7609.  
  7610.  The I/O instructions that directly refer to addresses in the processor's
  7611.  I/O space are IN, INS, OUT, OUTS. The 80386 has the ability to selectively
  7612.  trap references to specific I/O addresses. The structure that enables
  7613.  selective trapping is the I/O Permission Bit Map in the TSS segment (see
  7614.  Figure 8-2). The I/O permission map is a bit vector. The size of the map
  7615.  and its location in the TSS segment are variable. The processor locates the
  7616.  I/O permission map by means of the I/O map base field in the fixed portion
  7617.  of the TSS. The I/O map base field is 16 bits wide and contains the offset
  7618.  of the beginning of the I/O permission map. The upper limit of the I/O
  7619.  permission map is the same as the limit of the TSS segment.
  7620.  
  7621.  In protected mode, when it encounters an I/O instruction (IN, INS, OUT, or
  7622.  OUTS), the processor first checks whether CPL ≤ IOPL. If this condition is
  7623.  true, the I/O operation may proceed. If not true, the processor checks the
  7624.  I/O permission map. (In virtual 8086 mode, the processor consults the map
  7625.  without regard for IOPL. Refer to Chapter 15.)
  7626.  
  7627.  Each bit in the map corresponds to an I/O port byte address; for example,
  7628.  the bit for port 41 is found at I/O map base + 5, bit offset 1. The
  7629.  processor tests all the bits that correspond to the I/O addresses spanned by
  7630.  an I/O operation; for example, a doubleword operation tests four bits
  7631.  corresponding to four adjacent byte addresses. If any tested bit is set,
  7632.  the processor signals a general protection exception. If all the tested bits
  7633.  are zero, the I/O operation may proceed.
  7634.  
  7635.  It is not necessary for the I/O permission map to represent all the I/O
  7636.  addresses. I/O addresses not spanned by the map are treated as if they had
  7637.  one bits in the map. For example, if TSS limit is equal to I/O map base +
  7638.  31, the first 256 I/O ports are mapped; I/O operations on any port greater
  7639.  than 255 cause an exception.
  7640.  
  7641.  If I/O map base is greater than or equal to TSS limit, the TSS segment has
  7642.  no I/O permission map, and all I/O instructions in the 80386 program cause
  7643.  exceptions when CPL > IOPL.
  7644.  
  7645.  Because the I/O permission map is in the TSS segment, different tasks can
  7646.  have different maps. Thus, the operating system can allocate ports to a task
  7647.  by changing the I/O permission map in the task's TSS.
  7648.  
  7649.  
  7650.  Figure 8-2.  I/O Address Bit Map
  7651.  
  7652.                                      TSS SEGMEMT
  7653.  
  7654.                          31       23       15       7      0
  7655.                         ╔════════╪════════╪════════╪════════╗
  7656.                LIMIT───║                                   ║
  7657.                         ║ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ║
  7658.                                                            
  7659.                               I/O PERMISSION BIT MAP       
  7660.                                                            
  7661.                         ║ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ║
  7662.                   ┌────║                                   ║
  7663.                   │     ╟────────┼────────┼────────┼────────╢
  7664.                   │                                        
  7665.                   │                                        
  7666.                   │                                        
  7667.                   │     ╟────────┼────────┼────────┼────────╢
  7668.                   └─────╢  I/O MAP BASE   │uuuuuuuu uuuuuuuT║64
  7669.                         ╟────────┼────────┼────────┼────────╢
  7670.                         ║00000000 00000000│       LOT       ║60
  7671.                         ╟────────┼────────┼────────┼────────╢
  7672.                         ║00000000 00000000│       GS        ║5C
  7673.                         ╟────────┼────────┼────────┼────────╢
  7674.                         ║                                   ║58
  7675.                                                            
  7676.                                                            
  7677.                                                            
  7678.                         ║                                   ║4
  7679.                         ╟────────┼────────┼────────┼────────╢
  7680.                         ║00000000 00000000│  TSS BACK LINK  ║0
  7681.                         ╚════════╪════════╪════════╪════════╝
  7682.  
  7683.  
  7684.  Chapter 9  Exceptions and Interrupts
  7685.  
  7686.  ────────────────────────────────────────────────────────────────────────────
  7687.  
  7688.  Interrupts and exceptions are special kinds of control transfer; they work
  7689.  somewhat like unprogrammed CALLs. They alter the normal program flow to
  7690.  handle external events or to report errors or exceptional conditions. The
  7691.  difference between interrupts and exceptions is that interrupts are used to
  7692.  handle asynchronous events external to the processor, but exceptions handle
  7693.  conditions detected by the processor itself in the course of executing
  7694.  instructions.
  7695.  
  7696.  There are two sources for external interrupts and two sources for
  7697.  exceptions:
  7698.  
  7699.    1.  Interrupts
  7700.  
  7701.        ■  Maskable interrupts, which are signalled via the INTR pin.
  7702.  
  7703.        ■  Nonmaskable interrupts, which are signalled via the NMI
  7704.           (Non-Maskable Interrupt) pin.
  7705.  
  7706.    2.  Exceptions
  7707.  
  7708.        ■  Processor detected. These are further classified as faults, traps,
  7709.           and aborts.
  7710.  
  7711.        ■  Programmed. The instructions INTO, INT 3, INT n, and BOUND can
  7712.           trigger exceptions. These instructions are often called "software
  7713.           interrupts", but the processor handles them as exceptions.
  7714.  
  7715.  This chapter explains the features that the 80386 offers for controlling
  7716.  and responding to interrupts when it is executing in protected mode.
  7717.  
  7718.  
  7719.  9.1  Identifying Interrupts
  7720.  
  7721.  The processor associates an identifying number with each different type of
  7722.  interrupt or exception.
  7723.  
  7724.  The NMI and the exceptions recognized by the processor are assigned
  7725.  predetermined identifiers in the range 0 through 31. Not all of these
  7726.  numbers are currently used by the 80386; unassigned identifiers in this
  7727.  range are reserved by Intel for possible future expansion.
  7728.  
  7729.  The identifiers of the maskable interrupts are determined by external
  7730.  interrupt controllers (such as Intel's 8259A Programmable Interrupt
  7731.  Controller) and communicated to the processor during the processor's
  7732.  interrupt-acknowledge sequence. The numbers assigned by an 8259A PIC can be
  7733.  specified by software. Any numbers in the range 32 through 255 can be used.
  7734.  Table 9-1 shows the assignment of interrupt and exception identifiers.
  7735.  
  7736.  Exceptions are classified as faults, traps, or aborts depending on the way
  7737.  they are reported and whether restart of the instruction that caused the
  7738.  exception is supported.
  7739.  
  7740.  Faults  Faults are exceptions that are reported "before" the
  7741.          instruction causingthe exception. Faults are either detected before
  7742.          the instruction begins to execute, or during execution of the
  7743.          instruction. If detected during the instruction, the fault is
  7744.          reported with the machine restored to a state that permits the
  7745.          instruction to be restarted.
  7746.  
  7747.  Traps   A trap is an exception that is reported at the instruction
  7748.          boundary immediately after the instruction in which the
  7749.          exception was detected.
  7750.  
  7751.  Aborts  An abort is an exception that permits neither precise location
  7752.          of the instruction causing the exception nor restart of the program
  7753.          that caused the exception. Aborts are used to report severe errors,
  7754.          such as hardware errors and inconsistent or illegal values in system
  7755.          tables.
  7756.  
  7757.  
  7758.  Table 9-1. Interrupt and Exception ID Assignments
  7759.  
  7760.  Identifier   Description
  7761.  
  7762.  0            Divide error
  7763.  1            Debug exceptions
  7764.  2            Nonmaskable interrupt
  7765.  3            Breakpoint (one-byte INT 3 instruction)
  7766.  4            Overflow (INTO instruction)
  7767.  5            Bounds check (BOUND instruction)
  7768.  6            Invalid opcode
  7769.  7            Coprocessor not available
  7770.  8            Double fault
  7771.  9            (reserved)
  7772.  10           Invalid TSS
  7773.  11           Segment not present
  7774.  12           Stack exception
  7775.  13           General protection
  7776.  14           Page fault
  7777.  15           (reserved)
  7778.  16           Coprecessor error
  7779.  17-31        (reserved)
  7780.  32-255       Available for external interrupts via INTR pin
  7781.  
  7782.  
  7783.  9.2  Enabling and Disabling Interrupts
  7784.  
  7785.  The processor services interrupts and exceptions only between the end of
  7786.  one instruction and the beginning of the next. When the repeat prefix is
  7787.  used to repeat a string instruction, interrupts and exceptions may occur
  7788.  between repetitions. Thus, operations on long strings do not delay interrupt
  7789.  response.
  7790.  
  7791.  Certain conditions and flag settings cause the processor to inhibit certain
  7792.  interrupts and exceptions at instruction boundaries.
  7793.  
  7794.  
  7795.  9.2.1  NMI Masks Further NMIs
  7796.  
  7797.  While an NMI handler is executing, the processor ignores further interrupt
  7798.  signals at the NMI pin until the next IRET instruction is executed.
  7799.  
  7800.  
  7801.  9.2.2  IF Masks INTR
  7802.  
  7803.  The IF (interrupt-enable flag) controls the acceptance of external
  7804.  interrupts signalled via the INTR pin. When IF=0, INTR interrupts are
  7805.  inhibited; when IF=1, INTR interrupts are enabled. As with the other flag
  7806.  bits, the processor clears IF in response to a RESET signal. The
  7807.  instructions CLI and STI alter the setting of IF.
  7808.  
  7809.  CLI (Clear Interrupt-Enable Flag) and STI (Set Interrupt-Enable Flag)
  7810.  explicitly alter IF (bit 9 in the flag register). These instructions may be
  7811.  executed only if CPL ≤ IOPL. A protection exception occurs if they are
  7812.  executed when CPL > IOPL.
  7813.  
  7814.  The IF is also affected implicitly by the following operations:
  7815.  
  7816.    ■  The instruction PUSHF stores all flags, including IF, in the stack
  7817.       where they can be examined.
  7818.  
  7819.    ■  Task switches and the instructions POPF and IRET load the flags
  7820.       register; therefore, they can be used to modify IF.
  7821.  
  7822.    ■  Interrupts through interrupt gates automatically reset IF, disabling
  7823.       interrupts. (Interrupt gates are explained later in this chapter.)
  7824.  
  7825.  
  7826.  9.2.3  RF Masks Debug Faults
  7827.  
  7828.  The RF bit in EFLAGS controls the recognition of debug faults. This permits
  7829.  debug faults to be raised for a given instruction at most once, no matter
  7830.  how many times the instruction is restarted. (Refer to Chapter 12 for more
  7831.  information on debugging.)
  7832.  
  7833.  
  7834.  9.2.4  MOV or POP to SS Masks Some Interrupts and Exceptions
  7835.  
  7836.  Software that needs to change stack segments often uses a pair of
  7837.  instructions; for example:
  7838.  
  7839.    MOV SS, AX
  7840.    MOV ESP, StackTop
  7841.  
  7842.  If an interrupt or exception is processed after SS has been changed but
  7843.  before ESP has received the corresponding change, the two parts of the stack
  7844.  pointer SS:ESP are inconsistent for the duration of the interrupt handler or
  7845.  exception handler.
  7846.  
  7847.  To prevent this situation, the 80386, after both a MOV to SS and a POP to
  7848.  SS instruction, inhibits NMI, INTR, debug exceptions, and single-step traps
  7849.  at the instruction boundary following the instruction that changes SS. Some
  7850.  exceptions may still occur; namely, page fault and general protection fault.
  7851.  Always use the 80386 LSS instruction, and the problem will not occur.
  7852.  
  7853.  
  7854.  9.3  Priority Among Simultaneous Interrupts and Exceptions
  7855.  
  7856.  If more than one interrupt or exception is pending at an instruction
  7857.  boundary, the processor services one of them at a time. The priority among
  7858.  classes of interrupt and exception sources is shown in Table 9-2. The
  7859.  processor first services a pending interrupt or exception from the class
  7860.  that has the highest priority, transferring control to the first
  7861.  instruction of the interrupt handler. Lower priority exceptions are
  7862.  discarded; lower priority interrupts are held pending. Discarded exceptions
  7863.  will be rediscovered when the interrupt handler returns control to the point
  7864.  of interruption.
  7865.  
  7866.  
  7867.  9.4  Interrupt Descriptor Table
  7868.  
  7869.  The interrupt descriptor table (IDT) associates each interrupt or exception
  7870.  identifier with a descriptor for the instructions that service the
  7871.  associated event. Like the GDT and LDTs, the IDT is an array of 8-byte
  7872.  descriptors. Unlike the GDT and LDTs, the first entry of the IDT may contain
  7873.  a descriptor. To form an index into the IDT, the processor multiplies the
  7874.  interrupt or exception identifier by eight. Because there are only 256
  7875.  identifiers, the IDT need not contain more than 256 descriptors. It can
  7876.  contain fewer than 256 entries; entries are required only for interrupt
  7877.  identifiers that are actually used.
  7878.  
  7879.  The IDT may reside anywhere in physical memory. As Figure 9-1 shows, the
  7880.  processor locates the IDT by means of the IDT register (IDTR). The
  7881.  instructions LIDT and SIDT operate on the IDTR. Both instructions have one
  7882.  explicit operand: the address in memory of a 6-byte area. Figure 9-2 shows
  7883.  the format of this area.
  7884.  
  7885.  LIDT (Load IDT register) loads the IDT register with the linear base
  7886.  address and limit values contained in the memory operand.  This instruction
  7887.  can be executed only when the CPL is zero. It is normally used by the
  7888.  initialization logic of an operating system when creating an IDT.  An
  7889.  operating system may also use it to change from one IDT to another.
  7890.  
  7891.  SIDT (Store IDT register) copies the base and limit value stored in IDTR
  7892.  to a memory location. This instruction can be executed at any privilege
  7893.  level.
  7894.  
  7895.  
  7896.  Table 9-2. Priority Among Simultaneous Interrupts and Exceptions
  7897.  
  7898.  Priority   Class of Interrupt or Exception
  7899.  
  7900.  HIGHEST    Faults except debug faults
  7901.             Trap instructions INTO, INT n, INT 3
  7902.             Debug traps for this instruction
  7903.             Debug faults for next instruction
  7904.             NMI interrupt
  7905.  LOWEST     INTR interrupt
  7906.  
  7907.  
  7908.  Figure 9-1.  IDT Register and Table
  7909.  
  7910.                                                INTERRUPT DESCRIPTOR TABLE
  7911.                                                ╔══════╤═════╤═════╤══════╗
  7912.                                          ┌────║      │     │     │      ║
  7913.                                          │     ╟─ GATE FOR INTERRUPT #N ─╢
  7914.                                          │     ║      │     │     │      ║
  7915.                                          │     ╚══════╧═════╧═════╧══════╝
  7916.                                          │                              
  7917.                                          │                              
  7918.                                          │                              
  7919.                                          │     ╔══════╤═════╤═════╤══════╗
  7920.                                          │     ║      │     │     │      ║
  7921.                                          │     ╟─ GATE FOR INTERRUPT #2 ─╢
  7922.                                          │     ║      │     │     │      ║
  7923.                                          │     ╠══════╤═════╤═════╤══════╣
  7924.              IDT REGISTER                │     ║      │     │     │      ║
  7925.                                          │     ╟─ GATE FOR INTERRUPT #1 ─╢
  7926.                      15            0     │     ║      │     │     │      ║
  7927.                     ╔═══════════════╗    │     ╠══════╤═════╤═════╤══════╣
  7928.                     ║   IDT LIMIT   ╟────┘     ║      │     │     │      ║
  7929.    ╔════════════════╩═══════════════╣          ╟─ GATE FOR INTERRUPT #0 ─╢
  7930.    ║            IDT BASE            ╟─────────║      │     │     │      ║
  7931.    ╚════════════════════════════════╝          ╚══════╧═════╧═════╧══════╝
  7932.     31                             0
  7933.  
  7934.  
  7935.  Figure 9-2.  Pseudo-Descriptor Format for LIDT and SIDT
  7936.  
  7937.    31                23                15                7               0
  7938.   ╔═════════════════╪═════════════════╪═════════════════╪═════════════════╗
  7939.   ║                                 BASE                                  ║2
  7940.   ╚═════════════════╪═════════════════╦═════════════════╪═════════════════╣
  7941.                                       ║               LIMIT               ║0
  7942.                                       ╚═════════════════╪═════════════════╝
  7943.  
  7944.  
  7945.  9.5  IDT Descriptors
  7946.  
  7947.  The IDT may contain any of three kinds of descriptor:
  7948.  
  7949.     ■  Task gates
  7950.     ■  Interrupt gates
  7951.     ■  Trap gates
  7952.  
  7953.  Figure 9-3 illustrates the format of task gates and 80386 interrupt gates
  7954.  and trap gates. (The task gate in an IDT is the same as the task gate
  7955.  already discussed in Chapter 7.)
  7956.  
  7957.  
  7958.  Figure 9-3.  80306 IDT Gate Descriptors
  7959.  
  7960.                                  80386 TASK GATE
  7961.     31                23                15                7                0
  7962.    ╔═════════════════╪═════════════════╪═══╤═══╤═════════╪═════════════════╗
  7963.    ║▒▒▒▒▒▒▒▒▒▒▒▒▒(NOT USED)▒▒▒▒▒▒▒▒▒▒▒▒│ P │DPL│0 0 1 0 1│▒▒▒(NOT USED)▒▒▒▒║4
  7964.    ╟───────────────────────────────────┼───┴───┴─────────┴─────────────────╢
  7965.    ║             SELECTOR              │▒▒▒▒▒▒▒▒▒▒▒▒▒(NOT USED)▒▒▒▒▒▒▒▒▒▒▒▒║0
  7966.    ╚═════════════════╪═════════════════╪═════════════════╪═════════════════╝
  7967.  
  7968.                                  80386 INTERRUPT GATE
  7969.     31                23                15                7                0
  7970.    ╔═════════════════╪═════════════════╪═══╤═══╤═════════╪═════╪═══════════╗
  7971.    ║           OFFSET 31..16           │ P │DPL│0 1 1 1 0│0 0 0│(NOT USED) ║4
  7972.    ╟───────────────────────────────────┼───┴───┴─────────┴─────┴───────────╢
  7973.    ║             SELECTOR              │           OFFSET 15..0            ║0
  7974.    ╚═════════════════╪═════════════════╪═════════════════╪═════════════════╝
  7975.  
  7976.                                  80386 TRAP GATE
  7977.     31                23                15                7                0
  7978.    ╔═════════════════╪═════════════════╪═══╤═══╤═════════╪═════╪═══════════╗
  7979.    ║          OFFSET 31..16            │ P │DPL│0 1 1 1 1│0 0 0│(NOT USED) ║4
  7980.    ╟───────────────────────────────────┼───┴───┴─────────┴─────┴───────────╢
  7981.    ║             SELECTOR              │           OFFSET 15..0            ║0
  7982.    ╚═════════════════╪═════════════════╪═════════════════╪═════════════════╝
  7983.  
  7984.  
  7985.  9.6  Interrupt Tasks and Interrupt Procedures
  7986.  
  7987.  Just as a CALL instruction can call either a procedure or a task, so an
  7988.  interrupt or exception can "call" an interrupt handler that is either a
  7989.  procedure or a task. When responding to an interrupt or exception, the
  7990.  processor uses the interrupt or exception identifier to index a descriptor
  7991.  in the IDT. If the processor indexes to an interrupt gate or trap gate, it
  7992.  invokes the handler in a manner similar to a CALL to a call gate. If the
  7993.  processor finds a task gate, it causes a task switch in a manner similar to
  7994.  a CALL to a task gate.
  7995.  
  7996.  
  7997.  9.6.1  Interrupt Procedures
  7998.  
  7999.  An interrupt gate or trap gate points indirectly to a procedure which will
  8000.  execute in the context of the currently executing task as illustrated by
  8001.  Figure 9-4. The selector of the gate points to an executable-segment
  8002.  descriptor in either the GDT or the current LDT. The offset field of the
  8003.  gate points to the beginning of the interrupt or exception handling
  8004.  procedure.
  8005.  
  8006.  The 80386 invokes an interrupt or exception handling procedure in much the
  8007.  same manner as it CALLs a procedure; the differences are explained in the
  8008.  following sections.
  8009.  
  8010.  
  8011.  Figure 9-4.  Interrupt Vectoring for Procedures
  8012.  
  8013.                    IDT                                    EXECUTABLE SEGMENT
  8014.             ╔═══════════════╗                             ╔═══════════════╗
  8015.             ║               ║                       OFFSET║               ║
  8016.             ╠═══════════════╣  ┌─────────────────────────║ ENTRY POINT   ║
  8017.             ║               ║  │      LDT OR GDT          ║               ║
  8018.             ╠═══════════════╣  │   ╔═══════════════╗      ║               ║
  8019.             ║               ║  │   ║               ║      ║               ║
  8020.  INTERRUPT  ╠═══════════════╣  │   ╠═══════════════╣      ║               ║
  8021.     ID─────║ TRAP GATE OR  ╟──┘   ║               ║      ║               ║
  8022.             ║INTERRUPT GATE ╟──┐   ╠═══════════════╣      ║               ║
  8023.             ╠═══════════════╣  │   ║               ║      ║               ║
  8024.             ║               ║  │   ╠═══════════════╣      ║               ║
  8025.             ╠═══════════════╣  └──║   SEGMENT     ╟─┐    ║               ║
  8026.             ║               ║      ║  DESCRIPTOR   ║ │    ║               ║
  8027.             ╠═══════════════╣      ╠═══════════════╣ │    ║               ║
  8028.             ║               ║      ║               ║ │    ║               ║
  8029.             ╠═══════════════╣      ╠═══════════════╣ │    ║               ║
  8030.             ║               ║      ║               ║ │BASE║               ║
  8031.             ╚═══════════════╝      ╠═══════════════╣ └───╚═══════════════╝
  8032.                                    ║               ║
  8033.                                    ║               ║
  8034.                                    ║               ║
  8035.                                    ╚═══════════════╝
  8036.  
  8037.  
  8038.  9.6.1.1  Stack of Interrupt Procedure
  8039.  
  8040.  Just as with a control transfer due to a CALL instruction, a control
  8041.  transfer to an interrupt or exception handling procedure uses the stack to
  8042.  store the information needed for returning to the original procedure. As
  8043.  Figure 9-5 shows, an interrupt pushes the EFLAGS register onto the stack
  8044.  before the pointer to the interrupted instruction.
  8045.  
  8046.  Certain types of exceptions also cause an error code to be pushed on the
  8047.  stack. An exception handler can use the error code to help diagnose the
  8048.  exception.
  8049.  
  8050.  
  8051.  9.6.1.2  Returning from an Interrupt Procedure
  8052.  
  8053.  An interrupt procedure also differs from a normal procedure in the method
  8054.  of leaving the procedure. The IRET instruction is used to exit from an
  8055.  interrupt procedure. IRET is similar to RET except that IRET increments EIP
  8056.  by an extra four bytes (because of the flags on the stack) and moves the
  8057.  saved flags into the EFLAGS register. The IOPL field of EFLAGS is changed
  8058.  only if the CPL is zero. The IF flag is changed only if CPL ≤ IOPL.
  8059.  
  8060.  
  8061.  Figure 9-5.  Stack Layout after Exception of Interrupt
  8062.  
  8063.                             WITHOUT PRIVILEGE TRANSITION
  8064.  
  8065.        D  O      31          0                     31          0
  8066.        I  F    ╠═══════╦═══════╣                 ╠═══════╦═══════╣
  8067.        R       ║▒▒▒▒▒▒▒║▒▒▒▒▒▒▒║    OLD          ║▒▒▒▒▒▒▒║▒▒▒▒▒▒▒║    OLD
  8068.        E  E    ╠═══════╬═══════╣   SS:ESP        ╠═══════╬═══════╣   SS:ESP
  8069.        C  X    ║▒▒▒▒▒▒▒║▒▒▒▒▒▒▒║     │           ║▒▒▒▒▒▒▒║▒▒▒▒▒▒▒║     │
  8070.        T  P    ╠═══════╩═══════╣────┘           ╠═══════╩═══════╣────┘
  8071.        I  A    ║  OLD EFLAGS   ║                 ║  OLD EFLAGS   ║
  8072.        O  N    ╠═══════╦═══════╣                 ╠═══════╦═══════╣
  8073.        N  S    ║▒▒▒▒▒▒▒║OLD CS ║    NEW          ║▒▒▒▒▒▒▒║OLD CS ║
  8074.           I    ╠═══════╩═══════╣   SS:ESP        ╠═══════╩═══════╣
  8075.         │ O    ║    OLD EIP    ║     │           ║    OLD EIP    ║    NEW
  8076.         │ N    ╠═══════════════╣────┘           ╠═══════════════╣   SS:ESP
  8077.         │      ║               ║                 ║  ERROR CODE   ║     │
  8078.                                               ╠═══════════════╣────┘
  8079.                                                ║               ║
  8080.                               
  8081.                WITHOUT ERROR CODE                 WITH ERROR CODE
  8082.  
  8083.                               WITH PRIVILEGE TRANSITION
  8084.  
  8085.        D  O     31            0                     31          0
  8086.        I  F    ╔═══════╦═══════╗────┐           ╔═══════╦═══════╗────┐
  8087.        R       ║▒▒▒▒▒▒▒║OLD SS ║     │           ║▒▒▒▒▒▒▒║OLD SS ║     │
  8088.        E  E    ╠═══════╩═══════╣   SS:ESP        ╠═══════╩═══════╣   SS:ESP
  8089.        C  X    ║    OLD ESP    ║  FROM TSS       ║    OLD ESP    ║  FROM TSS
  8090.        T  P    ╠═══════════════╣                 ╠═══════════════╣
  8091.        I  A    ║  OLD EFLAGS   ║                 ║  OLD EFLAGS   ║
  8092.        O  N    ╠═══════╦═══════╣                 ╠═══════╦═══════╣
  8093.        N  S    ║▒▒▒▒▒▒▒║OLD CS ║    NEW          ║▒▒▒▒▒▒▒║OLD CS ║
  8094.           I    ╠═══════╩═══════╣   SS:EIP        ╠═══════╩═══════╣
  8095.         │ O    ║    OLD EIP    ║     │           ║    OLD EIP    ║    NEW
  8096.         │ N    ╠═══════════════╣────┘           ╠═══════════════╣   SS:ESP
  8097.         │      ║               ║                 ║  ERROR CODE   ║     │
  8098.                                               ╠═══════════════╣────┘
  8099.                                                ║               ║
  8100.                               
  8101.                WITHOUT ERROR CODE                 WITH ERROR CODE
  8102.  
  8103.  
  8104.  9.6.1.3  Flags Usage by Interrupt Procedure
  8105.  
  8106.  Interrupts that vector through either interrupt gates or trap gates cause
  8107.  TF (the trap flag) to be reset after the current value of TF is saved on the
  8108.  stack as part of EFLAGS. By this action the processor prevents debugging
  8109.  activity that uses single-stepping from affecting interrupt response. A
  8110.  subsequent IRET instruction restores TF to the value in the EFLAGS image on
  8111.  the stack.
  8112.  
  8113.  The difference between an interrupt gate and a trap gate is in the effect
  8114.  on IF (the interrupt-enable flag). An interrupt that vectors through an
  8115.  interrupt gate resets IF, thereby preventing other interrupts from
  8116.  interfering with the current interrupt handler. A subsequent IRET
  8117.  instruction restores IF to the value in the EFLAGS image on the stack. An
  8118.  interrupt through a trap gate does not change IF.
  8119.  
  8120.  
  8121.  9.6.1.4  Protection in Interrupt Procedures
  8122.  
  8123.  The privilege rule that governs interrupt procedures is similar to that for
  8124.  procedure calls: the CPU does not permit an interrupt to transfer control to
  8125.  a procedure in a segment of lesser privilege (numerically greater privilege
  8126.  level) than the current privilege level. An attempt to violate this rule
  8127.  results in a general protection exception.
  8128.  
  8129.  Because occurrence of interrupts is not generally predictable, this
  8130.  privilege rule effectively imposes restrictions on the privilege levels at
  8131.  which interrupt and exception handling procedures can execute. Either of the
  8132.  following strategies can be employed to ensure that the privilege rule is
  8133.  never violated.
  8134.  
  8135.    ■  Place the handler in a conforming segment. This strategy suits the
  8136.       handlers for certain exceptions (divide error, for example). Such a
  8137.       handler must use only the data available to it from the stack. If it
  8138.       needed data from a data segment, the data segment would have to have
  8139.       privilege level three, thereby making it unprotected.
  8140.  
  8141.    ■  Place the handler procedure in a privilege level zero segment.
  8142.  
  8143.  
  8144.  9.6.2  Interrupt Tasks
  8145.  
  8146.  A task gate in the IDT points indirectly to a task, as Figure 9-6
  8147.  illustrates. The selector of the gate points to a TSS descriptor in the GDT.
  8148.  
  8149.  When an interrupt or exception vectors to a task gate in the IDT, a task
  8150.  switch results. Handling an interrupt with a separate task offers two
  8151.  advantages:
  8152.  
  8153.    ■  The entire context is saved automatically.
  8154.  
  8155.    ■  The interrupt handler can be isolated from other tasks by giving it a
  8156.       separate address space, either via its LDT or via its page directory.
  8157.  
  8158.  The actions that the processor takes to perform a task switch are discussed
  8159.  in Chapter 7. The interrupt task returns to the interrupted task by
  8160.  executing an IRET instruction.
  8161.  
  8162.  If the task switch is caused by an exception that has an error code, the
  8163.  processor automatically pushes the error code onto the stack that
  8164.  corresponds to the privilege level of the first instruction to be executed
  8165.  in the interrupt task.
  8166.  
  8167.  When interrupt tasks are used in an operating system for the 80386, there
  8168.  are actually two schedulers: the software scheduler (part of the operating
  8169.  system) and the hardware scheduler (part of the processor's interrupt
  8170.  mechanism). The design of the software scheduler should account for the fact
  8171.  that the hardware scheduler may dispatch an interrupt task whenever
  8172.  interrupts are enabled.
  8173.  
  8174.  
  8175.  Figure 9-6.  Interrupt Vectoring for Tasks
  8176.  
  8177.              IDT                       GDT
  8178.       ╔════════════════╗        ╔════════════════╗
  8179.       ║                ║        ║                ║              TSS
  8180.       ╟────────────────╢        ╟────────────────╢       ╔════════════════╗
  8181.       ║                ║        ║                ║       ║                ║
  8182.       ╟────────────────╢        ╟────────────────╢       ║                ║
  8183.       ║                ║        ║                ║       ║                ║
  8184.       ╟────────────────╢        ╟────────────────╢       ║                ║
  8185.   ┌──║   TASK GATE    ╟───┐    ║                ║       ║                ║
  8186.   │   ╟────────────────╢   │    ╟────────────────╢       ║                ║
  8187.   │   ║                ║   └───║ TSS DESCRIPTOR ╟───┐   ║                ║
  8188.   │   ╟────────────────╢        ╟────────────────╢   │   ║                ║
  8189.   │   ║                ║        ║                ║   │   ║                ║
  8190.   │   ╟────────────────╢        ╟────────────────╢   └──╚════════════════╝
  8191.   │   ║                ║        ║                ║
  8192.   │   ╟────────────────╢        ╟────────────────╢
  8193.   │   ║                ║        ║                ║
  8194.   │   ╚════════════════╝        ╚════════════════╝
  8195.   │
  8196.   └─INTERRUPT ID
  8197.  
  8198.  
  8199.  9.7  Error Code
  8200.  
  8201.  With exceptions that relate to a specific segment, the processor pushes an
  8202.  error code onto the stack of the exception handler (whether procedure or
  8203.  task). The error code has the format shown in Figure 9-7. The format of the
  8204.  error code resembles that of a selector; however, instead of an RPL field,
  8205.  the error code contains two one-bit items:
  8206.  
  8207.    1.  The processor sets the EXT bit if an event external to the program
  8208.        caused the exception.
  8209.  
  8210.    2.  The processor sets the I-bit (IDT-bit) if the index portion of the
  8211.        error code refers to a gate descriptor in the IDT.
  8212.  
  8213.  If the I-bit is not set, the TI bit indicates whether the error code refers
  8214.  to the GDT (value 0) or to the LDT (value 1). The remaining 14 bits are the
  8215.  upper 14 bits of the segment selector involved. In some cases the error code
  8216.  on the stack is null, i.e., all bits in the low-order word are zero.
  8217.  
  8218.  
  8219.  Figure 9-7.  Error Code Format
  8220.  
  8221.         31              15                                         2 1 0
  8222.        ╔═══════════════╪════════════════╤═════════════════╪═══════╤═╤═╤═╗
  8223.        ║▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒│                         │T│ │E║
  8224.        ║▒▒▒▒▒▒▒▒▒▒▒UNDEFINED▒▒▒▒▒▒▒▒▒▒▒▒│     SELECTOR INDEX      │ │I│ ║
  8225.        ║▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒│                         │I│ │X║
  8226.        ╚═══════════════╪════════════════╧═════════════════╪═══════╧═╧═╧═╝
  8227.  
  8228.  
  8229.  9.8  Exception Conditions
  8230.  
  8231.  The following sections describe each of the possible exception conditions
  8232.  in detail. Each description classifies the exception as a fault, trap, or
  8233.  abort. This classification provides information needed by systems
  8234.  programmers for restarting the procedure in which the exception occurred:
  8235.  
  8236.  Faults   The CS and EIP values saved when a fault is reported point to the
  8237.           instruction causing the fault.
  8238.  
  8239.  Traps    The CS and EIP values stored when the trap is reported point to the
  8240.           instruction dynamically after the instruction causing the trap. If
  8241.           a trap is detected during an instruction that alters program flow,
  8242.           the reported values of CS and EIP reflect the alteration of program
  8243.           flow. For example, if a trap is detected in a JMP instruction, the
  8244.           CS and EIP values pushed onto the stack point to the target of the
  8245.           JMP, not to the instruction after the JMP.
  8246.  
  8247.  Aborts   An abort is an exception that permits neither precise location of
  8248.           the instruction causing the exception nor restart of the program
  8249.           that caused the exception. Aborts are used to report severe errors,
  8250.           such as hardware errors and inconsistent or illegal values in
  8251.           system tables.
  8252.  
  8253.  
  8254.  9.8.1  Interrupt 0 ── Divide Error
  8255.  
  8256.  The divide-error fault occurs during a DIV or an IDIV instruction when the
  8257.  divisor is zero.
  8258.  
  8259.  
  8260.  9.8.2  Interrupt 1 ── Debug Exceptions
  8261.  
  8262.  The processor triggers this interrupt for any of a number of conditions;
  8263.  whether the exception is a fault or a trap depends on the condition:
  8264.  
  8265.     ■  Instruction address breakpoint fault.
  8266.     ■  Data address breakpoint trap.
  8267.     ■  General detect fault.
  8268.     ■  Single-step trap.
  8269.     ■  Task-switch breakpoint trap.
  8270.  
  8271.  The processor does not push an error code for this exception. An exception
  8272.  handler can examine the debug registers to determine which condition caused
  8273.  the exception. Refer to Chapter 12 for more detailed information about
  8274.  debugging and the debug registers.
  8275.  
  8276.  
  8277.  9.8.3  Interrupt 3 ── Breakpoint
  8278.  
  8279.  The INT 3 instruction causes this trap. The INT 3 instruction is one byte
  8280.  long, which makes it easy to replace an opcode in an executable segment with
  8281.  the breakpoint opcode. The operating system or a debugging subsystem can use
  8282.  a data-segment alias for an executable segment to place an INT 3 anywhere it
  8283.  is convenient to arrest normal execution so that some sort of special
  8284.  processing can be performed. Debuggers typically use breakpoints as a way of
  8285.  displaying registers, variables, etc., at crucial points in a task.
  8286.  
  8287.  The saved CS:EIP value points to the byte following the breakpoint. If a
  8288.  debugger replaces a planted breakpoint with a valid opcode, it must subtract
  8289.  one from the saved EIP value before returning. Refer also to Chapter 12 for
  8290.  more information on debugging.
  8291.  
  8292.  
  8293.  9.8.4  Interrupt 4 ── Overflow
  8294.  
  8295.  This trap occurs when the processor encounters an INTO instruction and the
  8296.  OF (overflow) flag is set. Since signed arithmetic and unsigned arithmetic
  8297.  both use the same arithmetic instructions, the processor cannot determine
  8298.  which is intended and therefore does not cause overflow exceptions
  8299.  automatically. Instead it merely sets OF when the results, if interpreted as
  8300.  signed numbers, would be out of range. When doing arithmetic on signed
  8301.  operands, careful programmers and compilers either test OF directly or use
  8302.  the INTO instruction.
  8303.  
  8304.  
  8305.  9.8.5  Interrupt 5 ── Bounds Check
  8306.  
  8307.  This fault occurs when the processor, while executing a BOUND instruction,
  8308.  finds that the operand exceeds the specified limits. A program can use the
  8309.  BOUND instruction to check a signed array index against signed limits
  8310.  defined in a block of memory.
  8311.  
  8312.  
  8313.  9.8.6  Interrupt 6 ── Invalid Opcode
  8314.  
  8315.  This fault occurs when an invalid opcode is detected by the execution unit.
  8316.  (The exception is not detected until an attempt is made to execute the
  8317.  invalid opcode; i.e., prefetching an invalid opcode does not cause this
  8318.  exception.) No error code is pushed on the stack. The exception can be
  8319.  handled within the same task.
  8320.  
  8321.  This exception also occurs when the type of operand is invalid for the
  8322.  given opcode. Examples include an intersegment JMP referencing a register
  8323.  operand, or an LES instruction with a register source operand.
  8324.  
  8325.  
  8326.  9.8.7  Interrupt 7 ── Coprocessor Not Available
  8327.  
  8328.  This exception occurs in either of two conditions:
  8329.  
  8330.    ■  The processor encounters an ESC (escape) instruction, and the EM
  8331.       (emulate) bit ofCR0 (control register zero) is set.
  8332.  
  8333.    ■  The processor encounters either the WAIT instruction or an ESC
  8334.       instruction, and both the MP (monitor coprocessor) and TS (task
  8335.       switched) bits of CR0 are set.
  8336.  
  8337.  Refer to Chapter 11 for information about the coprocessor interface.
  8338.  
  8339.  
  8340.  9.8.8  Interrupt 8 ── Double Fault
  8341.  
  8342.  Normally, when the processor detects an exception while trying to invoke
  8343.  the handler for a prior exception, the two exceptions can be handled
  8344.  serially. If, however, the processor cannot handle them serially, it signals
  8345.  the double-fault exception instead. To determine when two faults are to be
  8346.  signalled as a double fault, the 80386 divides the exceptions into three
  8347.  classes: benign exceptions, contributory exceptions, and page faults. Table
  8348.  9-3 shows this classification.
  8349.  
  8350.  Table 9-4 shows which combinations of exceptions cause a double fault and
  8351.  which do not.
  8352.  
  8353.  The processor always pushes an error code onto the stack of the
  8354.  double-fault handler; however, the error code is always zero. The faulting
  8355.  instruction may not be restarted. If any other exception occurs while
  8356.  attempting to invoke the double-fault handler, the processor shuts down.
  8357.  
  8358.  
  8359.  Table 9-3. Double-Fault Detection Classes
  8360.  
  8361.  Class           ID          Description
  8362.  
  8363.                   1          Debug exceptions
  8364.                   2          NMI
  8365.                   3          Breakpoint
  8366.  Benign           4          Overflow
  8367.  Exceptions       5          Bounds check
  8368.                   6          Invalid opcode
  8369.                   7          Coprocessor not available
  8370.                  16          Coprocessor error
  8371.  
  8372.                   0          Divide error
  8373.                   9          Coprocessor Segment Overrun
  8374.  Contributory    10          Invalid TSS
  8375.  Exceptions      11          Segment not present
  8376.                  12          Stack exception
  8377.                  13          General protection
  8378.  
  8379.  Page Faults     14          Page fault
  8380.  
  8381.  
  8382.  Table 9-4. Double-Fault Definition
  8383.  
  8384.                                     SECOND EXCEPTION
  8385.  
  8386.                             Benign       Contributory    Page
  8387.                             Exception    Exception       Fault
  8388.  
  8389.  
  8390.             Benign          OK           OK              OK
  8391.             Exception
  8392.  
  8393.  FIRST      Contributory    OK           DOUBLE          OK
  8394.  EXCEPTION  Exception
  8395.  
  8396.             Page
  8397.             Fault           OK           DOUBLE          DOUBLE
  8398.  
  8399.  
  8400.  9.8.9  Interrupt 9 ── Coprocessor Segment Overrun
  8401.  
  8402.  This exception is raised in protected mode if the 80386 detects a page or
  8403.  segment violation while transferring the middle portion of a coprocessor
  8404.  operand to the NPX. This exception is avoidable. Refer to Chapter 11 for
  8405.  more information about the coprocessor interface.
  8406.  
  8407.  
  8408.  9.8.10  Interrupt 10 ── Invalid TSS
  8409.  
  8410.  Interrupt 10 occurs if during a task switch the new TSS is invalid. A TSS
  8411.  is considered invalid in the cases shown in Table 9-5. An error code is
  8412.  pushed onto the stack to help identify the cause of the fault. The EXT bit
  8413.  indicates whether the exception was caused by a condition outside the
  8414.  control of the program; e.g., an external interrupt via a task gate
  8415.  triggered a switch to an invalid TSS.
  8416.  
  8417.  This fault can occur either in the context of the original task or in the
  8418.  context of the new task. Until the processor has completely verified the
  8419.  presence of the new TSS, the exception occurs in the context of the original
  8420.  task. Once the existence of the new TSS is verified, the task switch is
  8421.  considered complete; i.e., TR is updated and, if the switch is due to a
  8422.  CALL or interrupt, the backlink of the new TSS is set to the old TSS. Any
  8423.  errors discovered by the processor after this point are handled in the
  8424.  context of the new task.
  8425.  
  8426.  To insure a proper TSS to process it, the handler for exception 10 must be
  8427.  a task invoked via a task gate.
  8428.  
  8429.  
  8430.  Table 9-5. Conditions That Invalidate the TSS
  8431.  
  8432.  Error Code              Condition
  8433.  
  8434.  TSS id + EXT            The limit in the TSS descriptor is less than 103
  8435.  LTD id + EXT            Invalid LDT selector or LDT not present
  8436.  SS id + EXT             Stack segment selector is outside table limit
  8437.  SS id + EXT             Stack segment is not a writable segment
  8438.  SS id + EXT             Stack segment DPL does not match new CPL
  8439.  SS id + EXT             Stack segment selector RPL < >  CPL
  8440.  CS id + EXT             Code segment selector is outside table limit
  8441.  CS id + EXT             Code segment selector does not refer to code
  8442.                          segment
  8443.  CS id + EXT             DPL of non-conforming code segment < > new CPL
  8444.  CS id + EXT             DPL of conforming code segment > new CPL
  8445.  DS/ES/FS/GS id + EXT    DS, ES, FS, or GS segment selector is outside
  8446.                          table limits
  8447.  DS/ES/FS/GS id + EXT    DS, ES, FS, or GS is not readable segment
  8448.  
  8449.  
  8450.  9.8.11  Interrupt 11 ── Segment Not Present
  8451.  
  8452.  Exception 11 occurs when the processor detects that the present bit of a
  8453.  descriptor is zero. The processor can trigger this fault in any of these
  8454.  cases:
  8455.  
  8456.    ■  While attempting to load the CS, DS, ES, FS, or GS registers; loading
  8457.       the SS register, however, causes a stack fault.
  8458.  
  8459.    ■  While attempting loading the LDT register with an LLDT instruction;
  8460.       loading the LDT register during a task switch operation, however,
  8461.       causes the "invalid TSS" exception.
  8462.  
  8463.    ■  While attempting to use a gate descriptor that is marked not-present.
  8464.  
  8465.  This fault is restartable. If the exception handler makes the segment
  8466.  present and returns, the interrupted program will resume execution.
  8467.  
  8468.  If a not-present exception occurs during a task switch, not all the steps
  8469.  of the task switch are complete. During a task switch, the processor first
  8470.  loads all the segment registers, then checks their contents for validity. If
  8471.  a not-present exception is discovered, the remaining segment registers have
  8472.  not been checked and therefore may not be usable for referencing memory. The
  8473.  not-present handler should not rely on being able to use the values found
  8474.  in CS, SS, DS, ES, FS, and GS without causing another exception. The
  8475.  exception handler should check all segment registers before trying to resume
  8476.  the new task; otherwise, general protection faults may result later under
  8477.  conditions that make diagnosis more difficult. There are three ways to
  8478.  handle this case:
  8479.  
  8480.    1.  Handle the not-present fault with a task. The task switch back to the
  8481.        interrupted task will cause the processor to check the registers as it
  8482.        loads them from the TSS.
  8483.  
  8484.    2.  PUSH and POP all segment registers. Each POP causes the processor to
  8485.        check the new contents of the segment register.
  8486.  
  8487.    3.  Scrutinize the contents of each segment-register image in the TSS,
  8488.        simulating the test that the processor makes when it loads a segment
  8489.        register.
  8490.  
  8491.  This exception pushes an error code onto the stack. The EXT bit of the
  8492.  error code is set if an event external to the program caused an interrupt
  8493.  that subsequently referenced a not-present segment. The I-bit is set if the
  8494.  error code refers to an IDT entry, e.g., an INT instruction referencing a
  8495.  not-present gate.
  8496.  
  8497.  An operating system typically uses the "segment not present" exception to
  8498.  implement virtual memory at the segment level. A not-present indication in a
  8499.  gate descriptor, however, usually does not indicate that a segment is not
  8500.  present (because gates do not necessarily correspond to segments).
  8501.  Not-present gates may be used by an operating system to trigger exceptions
  8502.  of special significance to the operating system.
  8503.  
  8504.  
  8505.  9.8.12  Interrupt 12 ── Stack Exception
  8506.  
  8507.  A stack fault occurs in either of two general conditions:
  8508.  
  8509.    ■  As a result of a limit violation in any operation that refers to the
  8510.       SS register. This includes stack-oriented instructions such as POP,
  8511.       PUSH, ENTER, and LEAVE, as well as other memory references that
  8512.       implicitly use SS (for example, MOV AX, [BP+6]). ENTER causes this
  8513.       exception when the stack is too small for the indicated local-variable
  8514.       space.
  8515.  
  8516.    ■  When attempting to load the SS register with a descriptor that is
  8517.       marked not-present but is otherwise valid. This can occur in a task
  8518.       switch, an interlevel CALL, an interlevel return, an LSS instruction,
  8519.       or a MOV or POP instruction to SS.
  8520.  
  8521.  When the processor detects a stack exception, it pushes an error code onto
  8522.  the stack of the exception handler. If the exception is due to a not-present
  8523.  stack segment or to overflow of the new stack during an interlevel CALL, the
  8524.  error code contains a selector to the segment in question (the exception
  8525.  handler can test the present bit in the descriptor to determine which
  8526.  exception occurred); otherwise the error code is zero.
  8527.  
  8528.  An instruction that causes this fault is restartable in all cases. The
  8529.  return pointer pushed onto the exception handler's stack points to the
  8530.  instruction that needs to be restarted. This instruction is usually the one
  8531.  that caused the exception; however, in the case of a stack exception due to
  8532.  loading of a not-present stack-segment descriptor during a task switch, the
  8533.  indicated instruction is the first instruction of the new task.
  8534.  
  8535.  When a stack fault occurs during a task switch, the segment registers may
  8536.  not be usable for referencing memory. During a task switch, the selector
  8537.  values are loaded before the descriptors are checked. If a stack fault is
  8538.  discovered, the remaining segment registers have not been checked and
  8539.  therefore may not be usable for referencing memory. The stack fault handler
  8540.  should not rely on being able to use the values found in CS, SS, DS, ES,
  8541.  FS, and GS without causing another exception. The exception handler should
  8542.  check all segment registers before trying to resume the new task; otherwise,
  8543.  general protection faults may result later under conditions that make
  8544.  diagnosis more difficult.
  8545.  
  8546.  
  8547.  9.8.13  Interrupt 13 ── General Protection Exception
  8548.  
  8549.  All protection violations that do not cause another exception cause a
  8550.  general protection exception. This includes (but is not limited to):
  8551.  
  8552.    1.  Exceeding segment limit when using CS, DS, ES, FS, or GS
  8553.  
  8554.    2.  Exceeding segment limit when referencing a descriptor table
  8555.  
  8556.    3.  Transferring control to a segment that is not executable
  8557.  
  8558.    4.  Writing into a read-only data segment or into a code segment
  8559.  
  8560.    5.  Reading from an execute-only segment
  8561.  
  8562.    6.  Loading the SS register with a read-only descriptor (unless the
  8563.        selector comes from the TSS during a task switch, in which case a TSS
  8564.        exception occurs
  8565.  
  8566.    7.  Loading SS, DS, ES, FS, or GS with the descriptor of a system segment
  8567.  
  8568.    8.  Loading DS, ES, FS, or GS with the descriptor of an executable
  8569.        segment that is not also readable
  8570.  
  8571.    9.  Loading SS with the descriptor of an executable segment
  8572.  
  8573.    10. Accessing memory via DS, ES, FS, or GS when the segment register
  8574.        contains a null selector
  8575.  
  8576.    11. Switching to a busy task
  8577.  
  8578.    12. Violating privilege rules
  8579.  
  8580.    13. Loading CR0 with PG=1 and PE=0.
  8581.  
  8582.    14. Interrupt or exception via trap or interrupt gate from V86 mode to
  8583.        privilege level other than zero.
  8584.  
  8585.    15. Exceeding the instruction length limit of 15 bytes (this can occur
  8586.        only if redundant prefixes are placed before an instruction)
  8587.  
  8588.  The general protection exception is a fault. In response to a general
  8589.  protection exception, the processor pushes an error code onto the exception
  8590.  handler's stack. If loading a descriptor causes the exception, the error
  8591.  code contains a selector to the descriptor; otherwise, the error code is
  8592.  null. The source of the selector in an error code may be any of the
  8593.  following:
  8594.  
  8595.    1.  An operand of the instruction.
  8596.    2.  A selector from a gate that is the operand of the instruction.
  8597.    3.  A selector from a TSS involved in a task switch.
  8598.  
  8599.  
  8600.  9.8.14  Interrupt 14 ── Page Fault
  8601.  
  8602.  This exception occurs when paging is enabled (PG=1) and the processor
  8603.  detects one of the following conditions while translating a linear address
  8604.  to a physical address:
  8605.  
  8606.    ■  The page-directory or page-table entry needed for the address
  8607.       translation has zero in its present bit.
  8608.  
  8609.    ■  The current procedure does not have sufficient privilege to access the
  8610.       indicated page.
  8611.  
  8612.  The processor makes available to the page fault handler two items of
  8613.  information that aid in diagnosing the exception and recovering from it:
  8614.  
  8615.    ■  An error code on the stack. The error code for a page fault has a
  8616.       format different from that for other exceptions (see Figure 9-8). The
  8617.       error code tells the exception handler three things:
  8618.  
  8619.       1.  Whether the exception was due to a not present page or to an access
  8620.           rights violation.
  8621.  
  8622.       2.  Whether the processor was executing at user or supervisor level at
  8623.           the time of the exception.
  8624.  
  8625.       3.  Whether the memory access that caused the exception was a read or
  8626.           write.
  8627.  
  8628.    ■  CR2 (control register two). The processor stores in CR2 the linear
  8629.       address used in the access that caused the exception (see Figure 9-9).
  8630.       The exception handler can use this address to locate the corresponding
  8631.       page directory and page table entries. If another page fault can occur
  8632.       during execution of the page fault handler, the handler should push CR2
  8633.       onto the stack.
  8634.  
  8635.  
  8636.  Figure 9-8.  Page-Fault Error Code Format
  8637.  
  8638.   ╔═════╤═════╤════════════════════════════════════════════════════════════╗
  8639.   ║Field│Value│                         Description                        ║
  8640.   ╟─────┼─────┼────────────────────────────────────────────────────────────╢
  8641.   ║ U/S │  0  │ The access causing the fault originated when the processor ║
  8642.   ║     │     │ was executing in supervisor mode.                          ║
  8643.   ║     │     │                                                            ║
  8644.   ║     │  1  │ The access causing the fault originated when the processor ║
  8645.   ║     │     │ was executing in user mode.                                ║
  8646.   ║     │     │                                                            ║
  8647.   ║ W/R │  0  │ The access causing the fault was a read.                   ║
  8648.   ║     │     │                                                            ║
  8649.   ║     │  1  │ The access causing the fault was a write.                  ║
  8650.   ║     │     │                                                            ║
  8651.   ║ P   │  0  │ The fault was caused by a not-present page.                ║
  8652.   ║     │     │                                                            ║
  8653.   ║     │  1  │ The fault was caused by a page-level protection violation. ║
  8654.   ╚═════╧═════╧════════════════════════════════════════════════════════════╝
  8655.  
  8656.         31                               15             7        3 2 1 0
  8657.        ╔════════════════════════════════╪═════════════════════════╤═╤═╤═╗
  8658.        ║▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒│U│W│ ║
  8659.        ║▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒UNDEFINED▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒│/│/│P║
  8660.        ║▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒│S│R│ ║
  8661.        ╚════════════════════════════════╪═════════════════════════╧═╧═╧═╝
  8662.  
  8663.  
  8664.  9.8.14.1  Page Fault During Task Switch
  8665.  
  8666.  The processor may access any of four segments during a task switch:
  8667.  
  8668.    1.  Writes the state of the original task in the TSS of that task.
  8669.  
  8670.    2.  Reads the GDT to locate the TSS descriptor of the new task.
  8671.  
  8672.    3.  Reads the TSS of the new task to check the types of segment
  8673.        descriptors from the TSS.
  8674.  
  8675.    4.  May read the LDT of the new task in order to verify the segment
  8676.        registers stored in the new TSS.
  8677.  
  8678.  A page fault can result from accessing any of these segments. In the latter
  8679.  two cases the exception occurs in the context of the new task. The
  8680.  instruction pointer refers to the next instruction of the new task, not to
  8681.  the instruction that caused the task switch. If the design of the operating
  8682.  system permits page faults to occur during task-switches, the page-fault
  8683.  handler should be invoked via a task gate.
  8684.  
  8685.  
  8686.  Figure 9-9.  CR2 Format
  8687.  
  8688.        31               23               15               7              0
  8689.       ╔════════════════╪════════════════╪════════════════╪════════════════╗
  8690.       ║                                                                   ║
  8691.       ║                      PAGE FAULT LINEAR ADDRESS                    ║
  8692.       ║                                                                   ║
  8693.       ╚════════════════╪════════════════╪════════════════╪════════════════╝
  8694.  
  8695.  
  8696.  9.8.14.2  Page Fault with Inconsistent Stack Pointer
  8697.  
  8698.  Special care should be taken to ensure that a page fault does not cause the
  8699.  processor to use an invalid stack pointer (SS:ESP). Software written for
  8700.  earlier processors in the 8086 family often uses a pair of instructions to
  8701.  change to a new stack; for example:
  8702.  
  8703.  MOV SS, AX
  8704.  MOV SP, StackTop
  8705.  
  8706.  With the 80386, because the second instruction accesses memory, it is
  8707.  possible to get a page fault after SS has been changed but before SP has
  8708.  received the corresponding change. At this point, the two parts of the stack
  8709.  pointer SS:SP (or, for 32-bit programs, SS:ESP) are inconsistent.
  8710.  
  8711.  The processor does not use the inconsistent stack pointer if the handling
  8712.  of the page fault causes a stack switch to a well defined stack (i.e., the
  8713.  handler is a task or a more privileged procedure). However, if the page
  8714.  fault handler is invoked by a trap or interrupt gate and the page fault
  8715.  occurs at the same privilege level as the page fault handler, the processor
  8716.  will attempt to use the stack indicated by the current (invalid) stack
  8717.  pointer.
  8718.  
  8719.  In systems that implement paging and that handle page faults within the
  8720.  faulting task (with trap or interrupt gates), software that executes at the
  8721.  same privilege level as the page fault handler should initialize a new stack
  8722.  by using the new LSS instruction rather than an instruction pair shown
  8723.  above. When the page fault handler executes at privilege level zero (the
  8724.  normal case), the scope of the problem is limited to privilege-level zero
  8725.  code, typically the kernel of the operating system.
  8726.  
  8727.  
  8728.  9.8.15  Interrupt 16 ── Coprocessor Error
  8729.  
  8730.  The 80386 reports this exception when it detects a signal from the 80287 or
  8731.  80387 on the 80386's ERROR# input pin. The 80386 tests this pin only at the
  8732.  beginning of certain ESC instructions and when it encounters a WAIT
  8733.  instruction while the EM bit of the MSW is zero (no emulation). Refer to
  8734.  Chapter 11 for more information on the coprocessor interface.
  8735.  
  8736.  
  8737.  9.9  Exception Summary
  8738.  
  8739.  
  8740.  Table 9-6 summarizes the exceptions recognized by the 386.
  8741.  
  8742.  Table 9-6. Exception Summary
  8743.  
  8744. ╓┌─────────────────────────┌───────────┌───────────────┌─────────────┌───────
  8745.  Description               Interrupt   Return Address  Exception     Function
  8746.                            Number      Points to       Type          the Excep
  8747.                                        Faulting
  8748.                                        Instruction
  8749.  
  8750.  Divide error               0          YES             FAULT         DIV, IDIV
  8751.  Debug exceptions           1                                      Any instr
  8752.  Breakpoint                 3          NO              TRAP          One-byte
  8753.  Overflow                   4          NO              TRAP          INTO
  8754.  Bounds check               5          YES             FAULT         BOUND
  8755.  Invalid opcode             6          YES             FAULT         Any illeg
  8756.  Coprocessor not available  7          YES             FAULT         ESC, WAIT
  8757.  Double fault               8          YES             ABORT         Any instr
  8758.                                                                      generate
  8759.  Description               Interrupt   Return Address  Exception     Function
  8760.                            Number      Points to       Type          the Excep
  8761.                                        Faulting
  8762.                                        Instruction
  8763.                                                                     generate
  8764.  Coprocessor Segment
  8765.  Overrun                    9          NO              ABORT         Any opera
  8766.                                                                      instructi
  8767.                                                                      the end o
  8768.  Invalid TSS               10          YES             FAULT        JMP, CALL
  8769.  Segment not present       11          YES             FAULT         Any segme
  8770.  Stack exception           12          YES             FAULT         Any memor
  8771.  General Protection        13          YES             FAULT/ABORT  Any memor
  8772.                                                                      fetch
  8773.  Page fault                14          YES             FAULT         Any memor
  8774.                                                                      fetch
  8775.  Coprocessor error         16          YES             FAULT        ESC, WAIT
  8776.  Two-byte SW Interrupt     0-255       NO              TRAP          INT n
  8777.  
  8778.  
  8779.  9.10  Error Code Summary
  8780.  
  8781.  Table 9-7 summarizes the error information that is available with each
  8782.  exception.
  8783.  
  8784.  
  8785.  Table 9-7. Error-Code Summary
  8786.  
  8787.  Description                       Interrupt     Error Code
  8788.                                    Number
  8789.  
  8790.  Divide error                       0            No
  8791.  Debug exceptions                   1            No
  8792.  Breakpoint                         3            No
  8793.  Overflow                           4            No
  8794.  Bounds check                       5            No
  8795.  Invalid opcode                     6            No
  8796.  Coprocessor not available          7            No
  8797.  System error                       8            Yes (always 0)
  8798.  Coprocessor Segment Overrun        9            No
  8799.  Invalid TSS                       10            Yes
  8800.  Segment not present               11            Yes
  8801.  Stack exception                   12            Yes
  8802.  General protection fault          13            Yes
  8803.  Page fault                        14            Yes
  8804.  Coprocessor error                 16            No
  8805.  Two-byte SW interrupt             0-255         No
  8806.  
  8807.  
  8808.  Chapter 10  Initialization
  8809.  
  8810.  ────────────────────────────────────────────────────────────────────────────
  8811.  
  8812.  After a signal on the RESET pin, certain registers of the 80386 are set to
  8813.  predefined values. These values are adequate to enable execution of a
  8814.  bootstrap program, but additional initialization must be performed by
  8815.  software before all the features of the processor can be utilized.
  8816.  
  8817.  
  8818.  10.1  Processor State After Reset
  8819.  
  8820.  The contents of EAX depend upon the results of the power-up self test. The
  8821.  self-test may be requested externally by assertion of BUSY# at the end of
  8822.  RESET. The EAX register holds zero if the 80386 passed the test. A nonzero
  8823.  value in EAX after self-test indicates that the particular 80386 unit is
  8824.  faulty. If the self-test is not requested, the contents of EAX after RESET
  8825.  is undefined.
  8826.  
  8827.  DX holds a component identifier and revision number after RESET as Figure
  8828.  10-1 illustrates. DH contains 3, which indicates an 80386 component. DL
  8829.  contains a unique identifier of the revision level.
  8830.  
  8831.  Control register zero (CR0) contains the values shown in Figure 10-2. The
  8832.  ET bit of CR0 is set if an 80387 is present in the configuration (according
  8833.  to the state of the ERROR# pin after RESET). If ET is reset, the
  8834.  configuration either contains an 80287 or does not contain a coprocessor. A
  8835.  software test is required to distinguish between these latter two
  8836.  possibilities.
  8837.  
  8838.  The remaining registers and flags are set as follows:
  8839.  
  8840.     EFLAGS             =00000002H
  8841.     IP                 =0000FFF0H
  8842.     CS selector        =000H
  8843.     DS selector        =0000H
  8844.     ES selector        =0000H
  8845.     SS selector        =0000H
  8846.     FS selector        =0000H
  8847.     GS selector        =0000H
  8848.     IDTR:
  8849.                base    =0
  8850.                limit   =03FFH
  8851.  
  8852.  All registers not mentioned above are undefined.
  8853.  
  8854.  These settings imply that the processor begins in real-address mode with
  8855.  interrupts disabled.
  8856.  
  8857.  
  8858.  Figure 10-1.  Contents of EDX after RESET
  8859.  
  8860.                                   EDX REGISTER
  8861.  
  8862.        31               23               15               7              0
  8863.       ╔════════════════╪════════════════╪════════════════╪════════════════╗
  8864.       ║▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒│       DH       │      DL        ║
  8865.       ║▒▒▒▒▒▒▒▒▒▒▒▒UNDEFINED▒▒▒▒▒▒▒▒▒▒▒▒│   DEVICE ID    │  STEPPING ID   ║
  8866.       ║▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒│       3        │   (UNIQUE)     ║
  8867.       ╚════════════════╪════════════════╪════════════════╪════════════════╝
  8868.  
  8869.  
  8870.  Figure 10-2.  Initial Contents of CR0
  8871.  
  8872.                                 CONTROL REGISTER ZERO
  8873.  
  8874.     31                23                15                  7     4 3   1  0
  8875.    ╔═╤═══════════════╪═════════════════╪═══════════════════╪═════╤═╤═╤═╤═╤═╗
  8876.    ║P│                                                           │E│T│E│M│P║
  8877.    ║ │                            UNDEFINED                      │ │ │ │ │ ║
  8878.    ║G│                                                           │T│S│M│P│E║
  8879.    ╚╤╧═══════════════╪═════════════════╪═══════════════════╪═════╧╤╧╤╧╤╧╤╧╤╝
  8880.     │                                                             │ │ │ │ │
  8881.     └─────────────0 - PAGING DISABLED                             │ │ │ │ │
  8882.                   * - INDICATES PRESENCE OF 80387─────────────────┘ │ │ │ │
  8883.                   0 - NO TASK SWITCH────────────────────────────────┘ │ │ │
  8884.                   0 - DO NOT MONITOR COPROCESSOR──────────────────────┘ │ │
  8885.                   0 - COPROCESSOR NOT PRESENT───────────────────────────┘ │
  8886.                   0 - PROTECTION NOT ENABLED (REAL ADDRESS MODE)──────────┘
  8887.  
  8888.  
  8889.  10.2  Software Initialization for Real-Address Mode
  8890.  
  8891.  In real-address mode a few structures must be initialized before a program
  8892.  can take advantage of all the features available in this mode.
  8893.  
  8894.  
  8895.  10.2.1  Stack
  8896.  
  8897.  No instructions that use the stack can be used until the stack-segment
  8898.  register (SS) has been loaded. SS must point to an area in RAM.
  8899.  
  8900.  
  8901.  10.2.2  Interrupt Table
  8902.  
  8903.  The initial state of the 80386 leaves interrupts disabled; however, the
  8904.  processor will still attempt to access the interrupt table if an exception
  8905.  or nonmaskable interrupt (NMI) occurs. Initialization software should take
  8906.  one of the following actions:
  8907.  
  8908.    ■  Change the limit value in the IDTR to zero. This will cause a shutdown
  8909.       if an exception or nonmaskable interrupt occurs. (Refer to the 80386
  8910.       Hardware Reference Manual to see how shutdown is signalled externally.)
  8911.  
  8912.    ■  Put pointers to valid interrupt handlers in all positions of the
  8913.       interrupt table that might be used by exceptions or interrupts.
  8914.  
  8915.    ■  Change the IDTR to point to a valid interrupt table.
  8916.  
  8917.  
  8918.  10.2.3  First Instructions
  8919.  
  8920.  After RESET, address lines A{31-20} are automatically asserted for
  8921.  instruction fetches. This fact, together with the initial values of CS:IP,
  8922.  causes instruction execution to begin at physical address FFFFFFF0H. Near
  8923.  (intrasegment) forms of control transfer instructions may be used to pass
  8924.  control to other addresses in the upper 64K bytes of the address space. The
  8925.  first far (intersegment) JMP or CALL instruction causes A{31-20} to drop
  8926.  low, and the 80386 continues executing instructions in the lower one
  8927.  megabyte of physical memory. This automatic assertion of address lines
  8928.  A{31-20} allows systems designers to use a ROM at the high end of
  8929.  the address space to initialize the system.
  8930.  
  8931.  
  8932.  10.3  Switching to Protected Mode
  8933.  
  8934.  Setting the PE bit of the MSW in CR0 causes the 80386 to begin executing in
  8935.  protected mode. The current privilege level (CPL) starts at zero. The
  8936.  segment registers continue to point to the same linear addresses as in real
  8937.  address mode (in real address mode, linear addresses are the same physical
  8938.  addresses).
  8939.  
  8940.  Immediately after setting the PE flag, the initialization code must flush
  8941.  the processor's instruction prefetch queue by executing a JMP instruction.
  8942.  The 80386 fetches and decodes instructions and addresses before they are
  8943.  used; however, after a change into protected mode, the prefetched
  8944.  instruction information (which pertains to real-address mode) is no longer
  8945.  valid. A JMP forces the processor to discard the invalid information.
  8946.  
  8947.  
  8948.  10.4  Software Initialization for Protected Mode
  8949.  
  8950.  Most of the initialization needed for protected mode can be done either
  8951.  before or after switching to protected mode. If done in protected mode,
  8952.  however, the initialization procedures must not use protected-mode features
  8953.  that are not yet initialized.
  8954.  
  8955.  
  8956.  10.4.1  Interrupt Descriptor Table
  8957.  
  8958.  The IDTR may be loaded in either real-address or protected mode. However,
  8959.  the format of the interrupt table for protected mode is different than that
  8960.  for real-address mode. It is not possible to change to protected mode and
  8961.  change interrupt table formats at the same time; therefore, it is inevitable
  8962.  that, if IDTR selects an interrupt table, it will have the wrong format at
  8963.  some time. An interrupt or exception that occurs at this time will have
  8964.  unpredictable results. To avoid this unpredictability, interrupts should
  8965.  remain disabled until interrupt handlers are in place and a valid IDT has
  8966.  been created in protected mode.
  8967.  
  8968.  
  8969.  10.4.2  Stack
  8970.  
  8971.  The SS register may be loaded in either real-address mode or protected
  8972.  mode. If loaded in real-address mode, SS continues to point to the same
  8973.  linear base-address after the switch to protected mode.
  8974.  
  8975.  
  8976.  10.4.3  Global Descriptor Table
  8977.  
  8978.  Before any segment register is changed in protected mode, the GDT register
  8979.  must point to a valid GDT. Initialization of the GDT and GDTR may be done in
  8980.  real-address mode. The GDT (as well as LDTs) should reside in RAM, because
  8981.  the processor modifies the accessed bit of descriptors.
  8982.  
  8983.  
  8984.  10.4.4  Page Tables
  8985.  
  8986.  Page tables and the PDBR in CR3 can be initialized in either real-address
  8987.  mode or in protected mode; however, the paging enabled (PG) bit of CR0
  8988.  cannot be set until the processor is in protected mode. PG may be set
  8989.  simultaneously with PE, or later. When PG is set, the PDBR in CR3 should
  8990.  already be initialized with a physical address that points to a valid page
  8991.  directory. The initialization procedure should adopt one of the following
  8992.  strategies to ensure consistent addressing before and after paging is
  8993.  enabled:
  8994.  
  8995.    ■  The page that is currently being executed should map to the same
  8996.       physical addresses both before and after PG is set.
  8997.  
  8998.    ■  A JMP instruction should immediately follow the setting of PG.
  8999.  
  9000.  
  9001.  10.4.5  First Task
  9002.  
  9003.  The initialization procedure can run awhile in protected mode without
  9004.  initializing the task register; however, before the first task switch, the
  9005.  following conditions must prevail:
  9006.  
  9007.    ■  There must be a valid task state segment (TSS) for the new task. The
  9008.       stack pointers in the TSS for privilege levels numerically less than or
  9009.       equal to the initial CPL must point to valid stack segments.
  9010.  
  9011.    ■  The task register must point to an area in which to save the current
  9012.       task state. After the first task switch, the information dumped in this
  9013.       area is not needed, and the area can be used for other purposes.
  9014.  
  9015.  
  9016.  10.5  Initialization Example
  9017.  
  9018.  $TITLE ('Initial Task')
  9019.  
  9020.      NAME    INIT
  9021.  
  9022.  init_stack  SEGMENT RW
  9023.              DW  20  DUP(?)
  9024.  tos         LABEL   WORD
  9025.  init_stack  ENDS
  9026.  
  9027.  init_data   SEGMENT RW PUBLIC
  9028.              DW  20  DUP(?)
  9029.  init_data   ENDS
  9030.  
  9031.  init_code   SEGMENT ER PUBLIC
  9032.  
  9033.  ASSUME      DS:init_data
  9034.  
  9035.      nop
  9036.      nop
  9037.      nop
  9038.  init_start:
  9039.                                      ; set up stack
  9040.      mov ax, init_stack
  9041.      mov ss, ax
  9042.      mov esp, offset tos
  9043.  
  9044.      mov a1,1
  9045.  blink:
  9046.      xor a1,1
  9047.      out 0e4h,a1
  9048.      mov cx,3FFFh
  9049.  here:
  9050.      dec cx
  9051.      jnz here
  9052.  
  9053.      jmp SHORT blink
  9054.  
  9055.      hlt
  9056.  init_code   ends
  9057.  
  9058.      END init_start, SS:init_stack, DS:init_data
  9059.  
  9060.  $TITLE('Protected Mode Transition -- 386 initialization')
  9061.  NAME  RESET
  9062.  
  9063.  ;*****************************************************************
  9064.  ; Upon reset the 386 starts executing at address 0FFFFFFF0H.  The
  9065.  ; upper 12 address bits remain high until a FAR call or jump is
  9066.  ; executed.
  9067.  ;
  9068.  ; Assume the following:
  9069.  ;
  9070.  ;
  9071.  ; -  a short jump at address 0FFFFFFF0H (placed there by the
  9072.  ;    system builder) causes execution to begin at START in segment
  9073.  ;    RESET_CODE.
  9074.  ;
  9075.  ;
  9076.  ; -  segment RESET_CODE is based at physical address 0FFFF0000H,
  9077.  ;    i.e.   at the start of the last  64K in the 4G address space.
  9078.  ;    Note that  this is the base of the CS register at reset.  If
  9079.  ;    you locate ROMcode above  this  address,  you  will  need  to
  9080.  ;    figure out an adjustment factor to address things within this
  9081.  ;    segment.
  9082.  ;
  9083.  ;*****************************************************************
  9084.  $EJECT ;
  9085.  
  9086.  ; Define addresses to locate GDT and IDT in RAM.
  9087.  ; These addresses are also used in the BLD386 file that defines
  9088.  ; the GDT and IDT. If you change these addresses, make sure you
  9089.  ; change the base addresses specified in the build file.
  9090.  
  9091.  GDTbase         EQU    00001000H   ; physical address for GDT base
  9092.  IDTbase         EQU    00000400H   ; physical address for IDT base
  9093.  
  9094.  PUBLIC     GDT_EPROM
  9095.  PUBLIC     IDT_EPROM
  9096.  PUBLIC     START
  9097.  
  9098.  DUMMY      segment rw      ; ONLY for ASM386 main module stack init
  9099.             DW 0
  9100.  DUMMY   ends
  9101.  
  9102.  ;*****************************************************************
  9103.  ;
  9104.  ; Note: RESET CODE must be USEl6 because the 386 initally executes
  9105.  ;       in real mode.
  9106.  ;
  9107.  
  9108.  RESET_CODE segment er PUBLIC    USE16
  9109.  
  9110.  ASSUME DS:nothing, ES:nothing
  9111.  
  9112.  ;
  9113.  ; 386 Descriptor template
  9114.  
  9115.  DESC       STRUC
  9116.      lim_0_15    DW  0              ; limit bits (0..15)
  9117.      bas_0_15    DW  0              ; base bits (0..15)
  9118.      bas_16_23   DB  0              ; base bits (16..23)
  9119.      access      DB  0              ; access byte
  9120.      gran        DB  0              ; granularity byte
  9121.      bas_24_31   DB  0              ; base bits (24..31)
  9122.  DESC       ENDS
  9123.  
  9124.  ; The following is the layout of the real GDT created by BLD386.
  9125.  ; It is located in EPROM and will be copied to RAM.
  9126.  ;
  9127.  ; GDT[O]      ...  NULL
  9128.  ; GDT[1]      ...  Alias for RAM GDT
  9129.  ; GDT[2]      ...  Alias for RAM IDT
  9130.  ; GDT[2]      ...  initial task TSS
  9131.  ; GDT[3]      ...  initial task TSS alias
  9132.  ; GDT[4]      ...  initial task LDT
  9133.  ; GDT[5]      ...  initial task LDT alias
  9134.  
  9135.  ;
  9136.  ; define entries in GDT and IDT.
  9137.  
  9138.  GDT_ENTRIES    EQU    8
  9139.  IDT_ENTRIES    EQU    32
  9140.  
  9141.  ; define some constants to index into the real GDT
  9142.  
  9143.  GDT_ALIAS      EQU    1*SIZE DESC
  9144.  IDT_ALIAS      EQU    2*SIZE DESC
  9145.  INIT_TSS       EQU    3*SIZE DESC
  9146.  INIT_TSS_A     EQU    4*SIZE DESC
  9147.  INIT_LDT       EQU    5*SIZE DESC
  9148.  INIT_LDT_A     EQU    6*SIZE DESC
  9149.  
  9150.  ;
  9151.  ; location of alias in INIT_LDT
  9152.  
  9153.  INIT_LDT_ALIAS    EQU    1*SIZE DESC
  9154.  
  9155.  ;
  9156.  ; access rights byte for DATA and TSS descriptors
  9157.  
  9158.  DS_ACCESS   EQU   010010010B
  9159.  TSS_ACCESS  EQU   010001001B
  9160.  
  9161.  
  9162.  ;
  9163.  ; This temporary GDT will be used to set up the real GDT in RAM.
  9164.  
  9165.  Temp_GDT    LABEL   BYTE        ; tag for begin of scratch GDT
  9166.  
  9167.  NULL_DES    DESC <>             ; NULL descriptor
  9168.  
  9169.                                  ; 32-Gigabyte data segment based at 0
  9170.  FLAT_DES    DESC <0FFFFH,0,0,92h,0CFh,0>
  9171.  
  9172.  GDT_eprom     DP    ?           ; Builder places GDT address and limit
  9173.                                  ; in this 6 byte area.
  9174.  
  9175.  IDT_eprom     DP    ?           ; Builder places IDT address and limit
  9176.                                  ; in this 6 byte area.
  9177.  
  9178.  ;
  9179.  ; Prepare operand for loadings GDTR and LDTR.
  9180.  
  9181.  
  9182.  TGDT_pword     LABEL  PWORD                 ; for temp GDT
  9183.          DW     end_Temp_GDT_Temp_GDT -1
  9184.          DD     0
  9185.  
  9186.  GDT_pword      LABEL  PWORD                 ; for GDT in RAM
  9187.          DW     GDT_ENTRIES * SIZE DESC -1
  9188.          DD     GDTbase
  9189.  
  9190.  IDT_pword      LABEL   PWORD                ; for IDT in RAM
  9191.          DW     IDT_ENTRIES * SIZE DESC -1
  9192.          DD     IDTbase
  9193.  
  9194.  
  9195.  end_Temp_GDT   LABEL   BYTE
  9196.  
  9197.  ;
  9198.  ; Define equates for addressing convenience.
  9199.  
  9200.  GDT_DES_FLAT        EQU DS:GDT_ALIAS +GDTbase
  9201.  IDT_DES_FLAT        EQU DS:IDT_ALIAS +GDTbase
  9202.  
  9203.  INIT_TSS_A_OFFSET   EQU DS:INIT_TSS_A
  9204.  INIT_TSS_OFFSET     EQU DS:INIT_TSS
  9205.  
  9206.  INIT_LDT_A_OFFSET   EQU DS:INIT_LDT_A
  9207.  INIT_LDT_OFFSET     EQU DS:INIT_LDT
  9208.  
  9209.  
  9210.  ; define pointer for first task switch
  9211.  
  9212.  ENTRY POINTER LABEL DWORD
  9213.               DW 0, INIT_TSS
  9214.  
  9215.  ;******************************************************************
  9216.  ;
  9217.  ;   Jump from reset vector to here.
  9218.  
  9219.  START:
  9220.  
  9221.      CLI                ;disable interrupts
  9222.      CLD                ;clear direction flag
  9223.  
  9224.      LIDT    NULL_des   ;force shutdown on errors
  9225.  
  9226.  ;
  9227.  ;   move scratch GDT to RAM at physical 0
  9228.  
  9229.      XOR DI,DI
  9230.      MOV ES,DI           ;point ES:DI to physical location 0
  9231.  
  9232.  
  9233.      MOV SI,OFFSET Temp_GDT
  9234.      MOV CX,end_Temp_GDT-Temp_GDT        ;set byte count
  9235.      INC CX
  9236.  ;
  9237.  ;   move table
  9238.  
  9239.      REP MOVS BYTE PTR ES:[DI],BYTE PTR CS:[SI]
  9240.  
  9241.      LGDT    tGDT_pword                ;load GDTR for Temp. GDT
  9242.                                        ;(located at 0)
  9243.  
  9244.  ;   switch to protected mode
  9245.  
  9246.      MOV EAX,CR0                       ;get current CRO
  9247.      MOV EAX,1                         ;set PE bit
  9248.      MOV CRO,EAX                       ;begin protected mode
  9249.  ;
  9250.  ;   clear prefetch queue
  9251.  
  9252.      JMP SHORT flush
  9253.  flush:
  9254.  
  9255.  ; set DS,ES,SS to address flat linear space (0 ... 4GB)
  9256.  
  9257.      MOV BX,FLAT_DES-Temp_GDT
  9258.      MOV US,BX
  9259.      MOV ES,BX
  9260.      MOV SS,BX
  9261.  ;
  9262.  ; initialize stack pointer to some (arbitrary) RAM location
  9263.  
  9264.      MOV ESP, OFFSET end_Temp_GDT
  9265.  
  9266.  ;
  9267.  ; copy eprom GDT to RAM
  9268.  
  9269.      MOV ESI,DWORD PTR GDT_eprom +2 ; get base of eprom GDT
  9270.                                     ; (put here by builder).
  9271.  
  9272.      MOV EDI,GDTbase                ; point ES:EDI to GDT base in RAM.
  9273.  
  9274.      MOV CX,WORD PTR gdt_eprom +0   ; limit of eprom GDT
  9275.      INC CX
  9276.      SHR CX,1                       ; easier to move words
  9277.      CLD
  9278.      REP MOVS   WORD PTR ES:[EDI],WORD PTR DS:[ESI]
  9279.  
  9280.  ;
  9281.  ; copy eprom IDT to RAM
  9282.  ;
  9283.      MOV ESI,DWORD PTR IDT_eprom +2 ; get base of eprom IDT
  9284.                                     ; (put here by builder)
  9285.  
  9286.      MOV EDI,IDTbase                ; point ES:EDI to IDT base in RAM.
  9287.  
  9288.      MOV CX,WORD PTR idt_eprom +0   ; limit of eprom IDT
  9289.      INC CX
  9290.      SHR CX,1
  9291.      CLD
  9292.      REP MOVS   WORD PTR ES:[EDI],WORD PTR DS:[ESI]
  9293.  
  9294.  ; switch to RAM GDT and IDT
  9295.  ;
  9296.      LIDT IDT_pword
  9297.      LGDT GDT_pword
  9298.  
  9299.  ;
  9300.      MOV BX,GDT_ALIAS               ; point DS to GDT alias
  9301.      MOV DS,BX
  9302.  ;
  9303.  ; copy eprom TSS to RAM
  9304.  ;
  9305.      MOV BX,INIT_TSS_A              ; INIT TSS A descriptor base
  9306.                                     ; has RAM location of INIT TSS.
  9307.  
  9308.      MOV ES,BX                      ; ES points to TSS in RAM
  9309.  
  9310.      MOV BX,INIT_TSS                ; get inital task selector
  9311.      LAR DX,BX                      ; save access byte
  9312.      MOV [BX].access,DS_ACCESS      ; set access as data segment
  9313.      MOV FS,BX                      ; FS points to eprom TSS
  9314.  
  9315.      XOR si,si                      ; FS:si points to eprom TSS
  9316.      XOR di,di                      ; ES:di points to RAM TSS
  9317.  
  9318.      MOV CX,[BX].lim_0_15           ; get count to move
  9319.      INC CX
  9320.  
  9321.  ;
  9322.  ; move INIT_TSS to RAM.
  9323.  
  9324.      REP MOVS BYTE PTR ES:[di],BYTE PTR FS:[si]
  9325.  
  9326.      MOV [BX].access,DH             ; restore access byte
  9327.  
  9328.  ;
  9329.  ; change base of INIT TSS descriptor to point to RAM.
  9330.  
  9331.      MOV AX,INIT_TSS_A_OFFSET.bas_0_15
  9332.      MOV INIT_TSS_OFFSET.bas_0_15,AX
  9333.      MOV AL,INIT_TSS_A_OFFSET.bas_16_23
  9334.      MOV INIT_TSS_OFFSET.bas_16_23,AL
  9335.      MOV AL,INIT_TSS_A_OFFSET.bas_24_31
  9336.      MOV INIT_TSS_OFFSET.bas_24_31,AL
  9337.  
  9338.  ;
  9339.  ; change INIT TSS A to form a save area for TSS on first task
  9340.  ; switch. Use RAM at location 0.
  9341.  
  9342.      MOV BX,INIT_TSS_A
  9343.      MOV WORD PTR [BX].bas_0_15,0
  9344.      MOV [BX].bas_16_23,0
  9345.      MOV [BX].bas_24_31,0
  9346.      MOV [BX].access,TSS_ACCESS
  9347.      MOV [BX].gran,O
  9348.      LTR BX                         ; defines save area for TSS
  9349.  
  9350.  ;
  9351.  ; copy eprom LDT to RAM
  9352.  
  9353.      MOV BX,INIT_LDT_A              ; INIT_LDT_A descriptor has
  9354.                                     ; base address in RAM for INIT_LDT.
  9355.  
  9356.      MOV ES,BX                      ; ES points LDT location in RAM.
  9357.  
  9358.      MOV AH,[BX].bas_24_31
  9359.      MOV AL,[BX].bas_16_23
  9360.      SHL EAX,16
  9361.      MOV AX,[BX].bas_0_15           ; save INIT_LDT base (ram) in EAX
  9362.  
  9363.      MOV BX,INIT_LDT                ; get inital LDT selector
  9364.      LAR DX,BX                      ; save access rights
  9365.      MOV [BX].access,DS_ACCESS      ; set access as data segment
  9366.      MOV FS,BX                      ; FS points to eprom LDT
  9367.  
  9368.      XOR si,si                      ; FS:SI points to eprom LDT
  9369.      XOR di,di                      ; ES:DI points to RAM LDT
  9370.  
  9371.      MOV CX,[BX].lim_0_15           ; get count to move
  9372.      INC CX
  9373.  ;
  9374.  ; move initial LDT to RAM
  9375.  
  9376.      REP MOVS BYTE PTR ES:[di],BYTE PTR FS:[si]
  9377.  
  9378.      MOV [BX].access,DH             ; restore access rights in
  9379.                                     ; INIT_LDT descriptor
  9380.  
  9381.  ;
  9382.  ; change base of alias (of INIT_LDT) to point to location in RAM.
  9383.  
  9384.      MOV ES:[INIT_LDT_ALIAS].bas_0_15,AX
  9385.      SHR EAX,16
  9386.      MOV ES:[INIT_LDT_ALIAS].bas_16_23,AL
  9387.      MOV ES:[INIT_LDT_ALIAS].bas_24_31,AH
  9388.  
  9389.  ;
  9390.  ; now set the base value in INIT_LDT descriptor
  9391.  
  9392.      MOV AX,INIT_LDT_A_OFFSET.bas_0_15
  9393.      MOV INIT_LDT_OFFSET.bas_0_15,AX
  9394.      MOV AL,INIT_LDT_A_OFFSET.bas_16_23
  9395.      MOV INIT_LDT_OFFSET.bas_16_23,AL
  9396.      MOV AL,INIT_LDT_A_OFFSET.bas_24_31
  9397.      MOV INIT_LDT_OFFSET.bas_24_31,AL
  9398.  
  9399.  ;
  9400.  ; Now GDT, IDT, initial TSS and initial LDT are all set up.
  9401.  ;
  9402.  ; Start the first task!
  9403.  '
  9404.     JMP ENTRY_POINTER
  9405.  
  9406.  RESET_CODE ends
  9407.     END START, SS:DUMMY,DS:DUMMY
  9408.  
  9409.  
  9410.  10.6  TLB Testing
  9411.  
  9412.  The 80386 provides a mechanism for testing the Translation Lookaside Buffer
  9413.  (TLB), the cache used for translating linear addresses to physical
  9414.  addresses. Although failure of the TLB hardware is extremely unlikely, users
  9415.  may wish to include TLB confidence tests among other power-up confidence
  9416.  tests for the 80386.
  9417.  
  9418.  ───────────────────────────────────────────────────────────────────────────
  9419.  NOTE
  9420.    This TLB testing mechanism is unique to the 80386 and may not be
  9421.    continued in the same way in future processors. Sortware that uses
  9422.    this mechanism may be incompatible with future processors.
  9423.  ───────────────────────────────────────────────────────────────────────────
  9424.  
  9425.  When testing the TLB it is recommended that paging be turned off (PG=0 in
  9426.  CR0) to avoid interference with the test data being written to the TLB.
  9427.  
  9428.  
  9429.  10.6.1  Structure of the TLB
  9430.  
  9431.  The TLB is a four-way set-associative memory. Figure 10-3 illustrates the
  9432.  structure of the TLB. There are four sets of eight entries each. Each entry
  9433.  consists of a tag and data. Tags are 24-bits wide. They contain the
  9434.  high-order 20 bits of the linear address, the valid bit, and three attribute
  9435.  bits. The data portion of each entry contains the high-order 20 bits of the
  9436.  physical address.
  9437.  
  9438.  
  9439.  10.6.2  Test Registers
  9440.  
  9441.  Two test registers, shown in Figure 10-4, are provided for the purpose of
  9442.  testing. TR6 is the test command register, and TR7 is the test data
  9443.  register. These registers are accessed by variants of the MOV
  9444.  instruction. A test register may be either the source operand or destination
  9445.  operand. The MOV instructions are defined in both real-address mode and
  9446.  protected mode. The test registers are privileged resources; in protected
  9447.  mode, the MOV instructions that access them can only be executed at
  9448.  privilege level 0. An attempt to read or write the test registers when
  9449.  executing at any other privilege level causes a general
  9450.  protection exception.
  9451.  
  9452.  The test command register (TR6) contains a command and an address tag to
  9453.  use in performing the command:
  9454.  
  9455.  C         This is the command bit. There are two TLB testing commands:
  9456.            write entries into the TLB, and perform TLB lookups. To cause an
  9457.            immediate write into the TLB entry, move a doubleword into TR6
  9458.            that contains a 0 in this bit. To cause an immediate TLB lookup,
  9459.            move a doubleword into TR6 that contains a 1 in this bit.
  9460.  
  9461.  Linear    On a TLB write, a TLB entry is allocated to this linear address;
  9462.  Address   the rest of that TLB entry is set per the value of TR7 and the
  9463.            value just written into TR6. On a TLB lookup, the TLB is
  9464.            interrogated per this value; if one and only one TLB entry
  9465.            matches, the rest of the fields of TR6 and TR7 are set from the
  9466.            matching TLB entry.
  9467.  
  9468.  V         The valid bit for this TLB entry. The TLB uses the valid bit to
  9469.            identify entries that contain valid data. Entries of the TLB
  9470.            that have not been assigned values have zero in the valid bit.
  9471.            All valid bits can be cleared by writing to CR3.
  9472.  
  9473.  D, D#     The dirty bit (and its complement) for/from the TLB entry.
  9474.  
  9475.  U, U#     The U/S bit (and its complement) for/from the TLB entry.
  9476.  
  9477.  W, W#     The R/W bit (and its complement) for/from the TLB entry.
  9478.  
  9479.            The meaning of these pairs of bits is given by Table 10-1,
  9480.            where X represents D, U, or W.
  9481.  
  9482.  The test data register (TR7) holds data read from or data to be written to
  9483.  the TLB.
  9484.  
  9485.  Physical  This is the data field of the TLB. On a write to the TLB, the
  9486.  Address   TLB entry allocated to the linear address in TR6 is set to this
  9487.            value. On a TLB lookup, if HT is set, the data field (physical
  9488.            address) from the TLB is read out to this field. If HT is not
  9489.            set, this field is undefined.
  9490.  
  9491.  HT        For a TLB lookup, the HT bit indicates whether the lookup was a
  9492.            hit (HT  1) or a miss (HT  0). For a TLB write, HT must be set
  9493.            to 1.
  9494.  
  9495.  REP       For a TLB write, selects which of four associative blocks of the
  9496.            TLB is to be written. For a TLB read, if HT is set, REP reports
  9497.            in which of the four associative blocks the tag was found; if HT
  9498.            is not set, REP is undefined.
  9499.  
  9500.  
  9501.  Table 10-1. Meaning of D, U, and W Bit Pairs
  9502.  
  9503.  X     X#      Effect during        Value of bit X
  9504.                TLB Lookup           after TLB Write
  9505.  
  9506.  0     0       (undefined)          (undefined)
  9507.  0     1       Match if X=0         Bit X becomes 0
  9508.  1     0       Match if X=1         Bit X becomes 1
  9509.  1     1       (undefined)          (undefined)
  9510.  
  9511.  
  9512.  Figure 10-3.  TLB Structure
  9513.  
  9514.                                     ╔═════════════════╦════════════════╗
  9515.                                    7║       TAG       ║      DATA      ║
  9516.                                     ╠═════════════════╬════════════════╣
  9517.                                                                      
  9518.                   ┌───────                                           
  9519.                   │       SET 11                                     
  9520.                   │    ┌──          ╠═════════════════╬════════════════╣
  9521.                   │    │           1║       TAG       ║      DATA      ║
  9522.                   │    │            ╠═════════════════╬════════════════╣
  9523.                   │    │           0║       TAG       ║      DATA      ║
  9524.                   │    │            ╚═════════════════╩════════════════╝
  9525.                   │    │
  9526.                   │    │            ╔═════════════════╦════════════════╗
  9527.                   │    │           7║       TAG       ║      DATA      ║
  9528.                   │    │            ╠═════════════════╬════════════════╣
  9529.                   │    │                                             
  9530.                   │    └──                                           
  9531.                   │       SET 10                                     
  9532.                   │    ┌──          ╠═════════════════╬════════════════╣
  9533.                   │    │           1║       TAG       ║      DATA      ║
  9534.        │ D │      │    │            ╠═════════════════╬════════════════╣
  9535.        │ A │      │    │           0║       TAG       ║      DATA      ║
  9536.        │ T └──────┘    │            ╚═════════════════╩════════════════╝
  9537.        │ A             │
  9538.        │   ┌──────┐    │            ╔═════════════════╦════════════════╗
  9539.        │ B │      │    │           7║       TAG       ║      DATA      ║
  9540.        │ U │      │    │            ╠═════════════════╬════════════════╣
  9541.        │ S │      │    │                                             
  9542.                   │    └──                                           
  9543.                   │       SET 01                                     
  9544.                   │    ┌──          ╠═════════════════╬════════════════╣
  9545.                   │    │           1║       TAG       ║      DATA      ║
  9546.                   │    │            ╠═════════════════╬════════════════╣
  9547.                   │    │           0║       TAG       ║      DATA      ║
  9548.                   │    │            ╚═════════════════╩════════════════╝
  9549.                   │    │
  9550.                   │    │            ╔═════════════════╦════════════════╗
  9551.                   │    │           7║       TAG       ║      DATA      ║
  9552.                   │    │            ╠═════════════════╬════════════════╣
  9553.                   │    │                                             
  9554.                   │    └──                                           
  9555.                   │       SET 00                                     
  9556.                   └───────          ╠═════════════════╬════════════════╣
  9557.                                    1║       TAG       ║      DATA      ║
  9558.                                     ╠═════════════════╬════════════════╣
  9559.                                    0║       TAG       ║      DATA      ║
  9560.                                     ╚═════════════════╩════════════════╝
  9561.  
  9562.  
  9563.  Figure 10-4.  Test Registers
  9564.  
  9565.        31                23              15   11      7             0
  9566.       ╔═════════════════╪═══════════════╪════╪═══════╪═════╤═╤═══╤═══╗
  9567.       ║                                      │             │H│   │   ║
  9568.       ║           PHYSICAL ADDRESS           │0 0 0 0 0 0 0│ │REP│0 0║ TR7
  9569.       ║                                      │             │T│   │   ║
  9570.       ╟──────────────────────────────────────┼─┬─┬─┬─┬─┬─┬─┼─┴───┴─┬─╢
  9571.       ║                                      │ │ │D│ │U│ │W│       │ ║
  9572.       ║            LINEAR ADDRESS            │V│D│ │U│ │ │ │0 0 0 0│C║ TR8
  9573.       ║                                      │ │ │#│ │#│ │#│       │ ║
  9574.       ╚═════════════════╪═══════════════╪════╪═╧═╧═╧═╪═╧═╧═╧═══════╧═╝
  9575.  
  9576.       NOTE: 0 INDICATES INTEL RESERVED. NO NOT DEFINE
  9577.  
  9578.  
  9579.  10.6.3  Test Operations
  9580.  
  9581.  To write a TLB entry:
  9582.  
  9583.    1.  Move a doubleword to TR7 that contains the desired physical address,
  9584.        HT, and REP values. HT must contain 1. REP must point to the
  9585.        associative block in which to place the entry.
  9586.  
  9587.    2.  Move a doubleword to TR6 that contains the appropriate linear
  9588.        address, and values for V, D, U, and W. Be sure C=0 for "write"
  9589.        command.
  9590.  
  9591.  Be careful not to write duplicate tags; the results of doing so are
  9592.  undefined.
  9593.  
  9594.  To look up (read) a TLB entry:
  9595.  
  9596.    1.  Move a doubleword to TR6 that contains the appropriate linear address
  9597.        and attributes. Be sure C=1 for "lookup" command.
  9598.  
  9599.    2.  Store TR7. If the HT bit in TR7 indicates a hit, then the other
  9600.        values reveal the TLB contents. If HT indicates a miss, then the other
  9601.        values in TR7 are indeterminate.
  9602.  
  9603.  For the purposes of testing, the V bit functions as another bit of
  9604.  addresss.  The V bit for a lookup request should usually be set, so that
  9605.  uninitialized tags do not match. Lookups with V=0 are unpredictable if any
  9606.  tags are uninitialized.
  9607.  
  9608.  
  9609.  Chapter 11  Coprocessing and Multiprocessing
  9610.  
  9611.  ────────────────────────────────────────────────────────────────────────────
  9612.  
  9613.  The 80386 has two levels of support for multiple parallel processing units:
  9614.  
  9615.    ■  A highly specialized interface for very closely coupled processors of
  9616.       a type known as coprocessors.
  9617.  
  9618.    ■  A more general interface for more loosely coupled processors of
  9619.       unspecified type.
  9620.  
  9621.  
  9622.  11.1  Coprocessing
  9623.  
  9624.  The components of the coprocessor interface include:
  9625.  
  9626.    ■  ET bit of control register zero (CR0)
  9627.    ■  The EM, and MP bits of CR0
  9628.    ■  The ESC instructions
  9629.    ■  The WAIT instruction
  9630.    ■  The TS bit of CR0
  9631.    ■  Exceptions
  9632.  
  9633.  
  9634.  11.1.1  Coprocessor Identification
  9635.  
  9636.  The 80386 is designed to operate with either an 80287 or 80387 math
  9637.  coprocessor. The ET bit of CR0 indicates which type of coprocessor is
  9638.  present. ET is set automatically by the 80386 after RESET according to the
  9639.  level detected on the ERROR# input. If desired, ET may also be set or reset
  9640.  by loading CR0 with a MOV instruction. If ET is set, the 80386 uses the
  9641.  32-bit protocol of the 80387; if reset, the 80386 uses the 16-bit protocol
  9642.  of the 80287.
  9643.  
  9644.  
  9645.  11.1.2  ESC and WAIT Instructions
  9646.  
  9647.  The 80386 interprets the pattern 11011B in the first five bits of an
  9648.  instruction as an opcode intended for a coprocessor. Instructions thus
  9649.  marked are called ESCAPE or ESC instructions. The CPU performs the following
  9650.  functions upon encountering an ESC instruction before sending the
  9651.  instruction to the coprocessor:
  9652.  
  9653.    ■  Tests the emulation mode (EM) flag to determine whether coprocessor
  9654.       functions are being emulated by software.
  9655.  
  9656.    ■  Tests the TS flag to determine whether there has been a context change
  9657.       since the last ESC instruction.
  9658.  
  9659.    ■  For some ESC instructions, tests the ERROR# pin to determine whether
  9660.       the coprocessor detected an error in the previous ESC instruction.
  9661.  
  9662.  The WAIT instruction is not an ESC instruction, but WAIT causes the CPU to
  9663.  perform some of the same tests that it performs upon encountering an ESC
  9664.  instruction. The processor performs the following actions for a WAIT
  9665.  instruction:
  9666.  
  9667.    ■  Waits until the coprocessor no longer asserts the BUSY# pin.
  9668.  
  9669.    ■  Tests the ERROR# pin (after BUSY# goes inactive). If ERROR# is active,
  9670.       the 80386 signals exception 16, which indicates that the coprocessor
  9671.       encountered an error in the previous ESC instruction.
  9672.  
  9673.    ■  WAIT can therefore be used to cause exception 16 if an error is
  9674.       pending from a previous ESC instruction. Note that, if no coprocessor
  9675.       is present, the ERROR# and BUSY# pins should be tied inactive to
  9676.       prevent WAIT from waiting forever or causing spurious exceptions.
  9677.  
  9678.  
  9679.  11.1.3  EM and MP Flags
  9680.  
  9681.  The EM and MP flags of CR0 control how the processor reacts to coprocessor
  9682.  instructions.
  9683.  
  9684.  The EM bit indicates whether coprocessor functions are to be emulated. If
  9685.  the processor finds EM set when executing an ESC instruction, it signals
  9686.  exception 7, giving the exception handler an opportunity to emulate the ESC
  9687.  instruction.
  9688.  
  9689.  The MP (monitor coprocessor) bit indicates whether a coprocessor is
  9690.  actually attached. The MP flag controls the function of the WAIT
  9691.  instruction. If, when executing a WAIT instruction, the CPU finds MP set,
  9692.  then it tests the TS flag; it does not otherwise test TS during a WAIT
  9693.  instruction. If it finds TS set under these conditions, the CPU signals
  9694.  exception 7.
  9695.  
  9696.  The EM and MP flags can be changed with the aid of a MOV instruction using
  9697.  CR0 as the destination operand and read with the aid of a MOV instruction
  9698.  with CR0 as the source operand. These forms of the MOV instruction can be
  9699.  executed only at privilege level zero.
  9700.  
  9701.  
  9702.  11.1.4  The Task-Switched Flag
  9703.  
  9704.  The TS bit of CR0 helps to determine when the context of the coprocessor
  9705.  does not match that of the task being executed by the 80386 CPU. The 80386
  9706.  sets TS each time it performs a task switch (whether triggered by software
  9707.  or by hardware interrupt). If, when interpreting one of the ESC
  9708.  instructions, the CPU finds TS already set, it causes exception 7. The WAIT
  9709.  instruction also causes exception 7 if both TS and MP are set. Operating
  9710.  systems can use this exception to switch the context of the coprocessor to
  9711.  correspond to the current task. Refer to the 80386 System Software Writer's
  9712.  Guide for an example.
  9713.  
  9714.  The CLTS instruction (legal only at privilege level zero) resets the TS
  9715.  flag.
  9716.  
  9717.  
  9718.  11.1.5  Coprocessor Exceptions
  9719.  
  9720.  Three exceptions aid in interfacing to a coprocessor: interrupt 7
  9721.  (coprocessor not available), interrupt 9 (coprocessor segment overrun), and
  9722.  interrupt 16 (coprocessor error).
  9723.  
  9724.  
  9725.  11.1.5.1  Interrupt 7 ── Coprocessor Not Available
  9726.  
  9727.  This exception occurs in either of two conditions:
  9728.  
  9729.    1.  The CPU encounters an ESC instruction and EM is set. In this case,
  9730.        the exception handler should emulate the instruction that caused the
  9731.        exception. TS may also be set.
  9732.  
  9733.    2.  The CPU encounters either the WAIT instruction or an ESC instruction
  9734.        when both MP and TS are set. In this case, the exception handler
  9735.        should update the state of the coprocessor, if necessary.
  9736.  
  9737.  
  9738.  11.1.5.2  Interrupt 9 ── Coprocessor Segment Overrun
  9739.  
  9740.  This exception occurs in protected mode under the following conditions:
  9741.  
  9742.    ■  An operand of a coprocessor instruction wraps around an addressing
  9743.       limit (0FFFFH for small segments, 0FFFFFFFFH for big segments, zero for
  9744.       expand-down segments). An operand may wrap around an addressing limit
  9745.       when the segment limit is near an addressing limit and the operand is
  9746.       near the largest valid address in the segment. Because of the
  9747.       wrap-around, the beginning and ending addresses of such an operand
  9748.       will be near opposite ends of the segment.
  9749.  
  9750.    ■  Both the first byte and the last byte of the operand (considering
  9751.       wrap-around) are at addresses located in the segment and in present and
  9752.       accessible pages.
  9753.  
  9754.    ■  The operand spans inaccessible addresses. There are two ways that such
  9755.       an operand may also span inaccessible addresses:
  9756.  
  9757.       1.  The segment limit is not equal to the addressing limit (e.g.,
  9758.           addressing limit is FFFFH and segment limit is FFFDH); therefore,
  9759.           the operand will span addresses that are not within the segment
  9760.           (e.g., an 8-byte operand that starts at valid offset FFFC will span
  9761.           addresses FFFC-FFFF and 0000-0003; however, addresses FFFE and FFFF
  9762.           are not valid, because they exceed the limit);
  9763.  
  9764.       2.  The operand begins and ends in present and accessible pages but
  9765.           intermediate bytes of the operand fall either in a not-present page
  9766.           or in a page to which the current procedure does not have access
  9767.           rights.
  9768.  
  9769.  The address of the failing numerics instruction and data operand may be
  9770.  lost; an FSTENV does not return reliable addresses. As with the 80286/80287,
  9771.  the segment overrun exception should be handled by executing an FNINIT
  9772.  instruction (i.e., an FINIT without a preceding WAIT). The return address on
  9773.  the stack does not necessarily point to the failing instruction nor to the
  9774.  following instruction. The failing numerics instruction is not restartable.
  9775.  
  9776.  Case 2 can be avoided by either aligning all segments on page boundaries or
  9777.  by not starting them within 108 bytes of the start or end of a page. (The
  9778.  maximum size of a coprocessor operand is 108 bytes.) Case 1 can be avoided
  9779.  by making sure that the gap between the last valid offset and the first
  9780.  valid offset of a segment is either no less than 108 bytes or is zero (i.e.,
  9781.  the segment is of full size). If neither software system design constraint
  9782.  is acceptable, the exception handler should execute FNINIT and should
  9783.  probably terminate the task.
  9784.  
  9785.  
  9786.  11.1.5.3  Interrupt 16 ── Coprocessor Error
  9787.  
  9788.  The numerics coprocessors can detect six different exception conditions
  9789.  during instruction execution. If the detected exception is not masked by a
  9790.  bit in the control word, the coprocessor communicates the fact that an error
  9791.  occurred to the CPU by a signal at the ERROR# pin. The CPU causes interrupt
  9792.  16 the next time it checks the ERROR# pin, which is only at the beginning of
  9793.  a subsequent WAIT or certain ESC instructions. If the exception is masked,
  9794.  the numerics coprocessor handles the exception according to on-board logic;
  9795.  it does not assert the ERROR# pin in this case.
  9796.  
  9797.  
  9798.  11.2  General Multiprocessing
  9799.  
  9800.  The components of the general multiprocessing interface include:
  9801.  
  9802.    ■  The LOCK# signal
  9803.  
  9804.    ■  The LOCK instruction prefix, which gives programmed control of the
  9805.       LOCK# signal.
  9806.  
  9807.    ■  Automatic assertion of the LOCK# signal with implicit memory updates
  9808.       by the processor
  9809.  
  9810.  
  9811.  11.2.1  LOCK and the LOCK# Signal
  9812.  
  9813.  The LOCK instruction prefix and its corresponding output signal LOCK# can
  9814.  be used to prevent other bus masters from interrupting a data movement
  9815.  operation. LOCK may only be used with the following 80386 instructions when
  9816.  they modify memory. An undefined-opcode exception results from using LOCK
  9817.  before any instruction other than:
  9818.  
  9819.    ■  Bit test and change: BTS, BTR, BTC.
  9820.    ■  Exchange: XCHG.
  9821.    ■  Two-operand arithmetic and logical: ADD, ADC, SUB, SBB, AND, OR, XOR.
  9822.    ■  One-operand arithmetic and logical: INC, DEC, NOT, and NEG.
  9823.  
  9824.  A locked instruction is only guaranteed to lock the area of memory defined
  9825.  by the destination operand, but it may lock a larger memory area. For
  9826.  example, typical 8086 and 80286 configurations lock the entire physical
  9827.  memory space. The area of memory defined by the destination operand is
  9828.  guaranteed to be locked against access by a processor executing a locked
  9829.  instruction on exactly the same memory area, i.e., an operand with
  9830.  identical starting address and identical length.
  9831.  
  9832.  The integrity of the lock is not affected by the alignment of the memory
  9833.  field. The LOCK signal is asserted for as many bus cycles as necessary to
  9834.  update the entire operand.
  9835.  
  9836.  
  9837.  11.2.2  Automatic Locking
  9838.  
  9839.  In several instances, the processor itself initiates activity on the data
  9840.  bus. To help ensure that such activities function correctly in
  9841.  multiprocessor configurations, the processor automatically asserts the LOCK#
  9842.  signal. These instances include:
  9843.  
  9844.    ■  Acknowledging interrupts.
  9845.  
  9846.       After an interrupt request, the interrupt controller uses the data bus
  9847.       to send the interrupt ID of the interrupt source to the CPU. The CPU
  9848.       asserts LOCK# to ensure that no other data appears on the data bus
  9849.       during this time.
  9850.  
  9851.    ■  Setting busy bit of TSS descriptor.
  9852.  
  9853.       The processor tests and sets the busy-bit in the type field of the TSS
  9854.       descriptor when switching to a task. To ensure that two different
  9855.       processors cannot simultaneously switch to the same task, the processor
  9856.       asserts LOCK# while testing and setting this bit.
  9857.  
  9858.    ■  Loading of descriptors.
  9859.  
  9860.       While copying the contents of a descriptor from a descriptor table into
  9861.       a segment register, the processor asserts LOCK# so that the descriptor
  9862.       cannot be modified by another processor while it is being loaded. For
  9863.       this action to be effective, operating-system procedures that update
  9864.       descriptors should adhere to the following steps:
  9865.  
  9866.       ──  Use a locked update to the access-rights byte to mark the
  9867.           descriptor not-present.
  9868.  
  9869.       ──  Update the fields of the descriptor.  (This may require several
  9870.           memory accesses; therefore, LOCK cannot be used.)
  9871.  
  9872.       ──  Use a locked update to the access-rights byte to mark the
  9873.           descriptor present again.
  9874.  
  9875.    ■  Updating page-table A and D bits.
  9876.  
  9877.       The processor exerts LOCK# while updating the A (accessed) and D
  9878.       (dirty) bits of page-table entries.  Also the processor bypasses the
  9879.       page-table cache and directly updates these bits in memory.
  9880.  
  9881.    ■  Executing XCHG instruction.
  9882.  
  9883.       The 80386 always asserts LOCK during an XCHG instruction that
  9884.       references memory (even if the LOCK prefix is not used).
  9885.  
  9886.  
  9887.  11.2.3  Cache Considerations
  9888.  
  9889.  Systems programmers must take care when updating shared data that may also
  9890.  be stored in on-chip registers and caches.  With the 80386, such  shared
  9891.  data includes:
  9892.  
  9893.    ■  Descriptors, which may be held in segment registers.
  9894.  
  9895.       A change to a descriptor that is shared among processors should be
  9896.       broadcast to all processors.  Segment registers are effectively
  9897.       "descriptor caches".  A change to a descriptor will not be utilized by
  9898.       another processor if that processor already has a copy of the old
  9899.       version of the descriptor in a segment register.
  9900.  
  9901.    ■  Page tables, which may be held in the page-table cache.
  9902.  
  9903.       A change to a page table that is shared among processors should be
  9904.       broadcast to all processors, so that others can flush their page-table
  9905.       caches and reload them with up-to-date page tables from memory.
  9906.  
  9907.  Systems designers can employ an interprocessor interrupt to handle the
  9908.  above cases. When one processor changes data that may be cached by other
  9909.  processors, it can send an interrupt signal to all other processors that may
  9910.  be affected by the change. If the interrupt is serviced by an interrupt
  9911.  task, the task switch automatically flushes the segment registers. The task
  9912.  switch also flushes the page-table cache if the PDBR (the contents of CR3)
  9913.  of the interrupt task is different from the PDBR of every other task.
  9914.  
  9915.  In multiprocessor systems that need a cacheability signal from the CPU, it
  9916.  is recommended that physical address pin A31 be used to indicate
  9917.  cacheability. Such a system can then possess up to 2 Gbytes of physical
  9918.  memory. The virtual address range available to the programmer is not
  9919.  affected by this convention.
  9920.  
  9921.  
  9922.  Chapter 12  Debugging
  9923.  
  9924.  ────────────────────────────────────────────────────────────────────────────
  9925.  
  9926.  The 80386 brings to Intel's line of microprocessors significant advances in
  9927.  debugging power. The single-step exception and breakpoint exception of
  9928.  previous processors are still available in the 80386, but the principal
  9929.  debugging support takes the form of debug registers. The debug registers
  9930.  support both instruction breakpoints and data breakpoints. Data breakpoints
  9931.  are an important innovation that can save hours of debugging time by
  9932.  pinpointing, for example, exactly when a data structure is being
  9933.  overwritten. The breakpoint registers also eliminate the complexities
  9934.  associated with writing a breakpoint instruction into a code segment
  9935.  (requires a data-segment alias for the code segment) or a code segment
  9936.  shared by multiple tasks (the breakpoint exception can occur in the context
  9937.  of any of the tasks). Breakpoints can even be set in code contained in ROM.
  9938.  
  9939.  
  9940.  12.1  Debugging Features of the Architecture
  9941.  
  9942.  The features of the 80386 architecture that support debugging include:
  9943.  
  9944.  Reserved debug interrupt vector
  9945.  
  9946.  Permits processor to automatically invoke a debugger task or procedure when
  9947.  an event occurs that is of interest to the debugger.
  9948.  
  9949.  Four debug address registers
  9950.  
  9951.  Permit programmers to specify up to four addresses that the CPU will
  9952.  automatically monitor.
  9953.  
  9954.  Debug control register
  9955.  
  9956.  Allows programmers to selectively enable various debug conditions
  9957.  associated with the four debug addresses.
  9958.  
  9959.  Debug status register
  9960.  
  9961.  Helps debugger identify condition that caused debug exception.
  9962.  
  9963.  Trap bit of TSS (T-bit)
  9964.  
  9965.  Permits monitoring of task switches.
  9966.  
  9967.  Resume flag (RF) of flags register
  9968.  
  9969.  Allows an instruction to be restarted after a debug exception without
  9970.  immediately causing another debug exception due to the same condition.
  9971.  
  9972.  Single-step flag (TF)
  9973.  
  9974.  Allows complete monitoring of program flow by specifying whether the CPU
  9975.  should cause a debug exception with the execution of every instruction.
  9976.  
  9977.  Breakpoint instruction
  9978.  
  9979.  Permits debugger intervention at any point in program execution and aids
  9980.  debugging of debugger programs.
  9981.  
  9982.  Reserved interrupt vector for breakpoint exception
  9983.  
  9984.  Permits processor to automatically invoke a handler task or procedure upon
  9985.  encountering a breakpoint instruction.
  9986.  
  9987.  These features make it possible to invoke a debugger that is either a
  9988.  separate task or a procedure in the context of the current task. The
  9989.  debugger can be invoked under any of the following kinds of conditions:
  9990.  
  9991.    ■  Task switch to a specific task.
  9992.    ■  Execution of the breakpoint instruction.
  9993.    ■  Execution of every instruction.
  9994.    ■  Execution of any instruction at a given address.
  9995.    ■  Read or write of a byte, word, or doubleword at any specified address.
  9996.    ■  Write to a byte, word, or doubleword at any specified address.
  9997.    ■  Attempt to change a debug register.
  9998.  
  9999.  
  10000.  12.2  Debug Registers
  10001.  
  10002.  Six 80386 registers are used to control debug features. These registers are
  10003.  accessed by variants of the MOV instruction. A debug register may be either
  10004.  the source operand or destination operand. The debug registers are
  10005.  privileged resources; the MOV instructions that access them can only be
  10006.  executed at privilege level zero. An attempt to read or write the debug
  10007.  registers when executing at any other privilege level causes a general
  10008.  protection exception. Figure 12-1 shows the format of the debug registers.
  10009.  
  10010.  
  10011.  Figure 12-1.  Debug Registers
  10012.  
  10013.        31              23              15              7             0
  10014.       ╔═══╤═══╤═══╤═══╪═══╤═══╤═══╤═══╪═══╤═╤═════╤═╤═╪═╤═╤═╤═╤═╤═╤═╤═╗
  10015.       ║LEN│R/W│LEN│R/W│LEN│R/W│LEN│R/W│   │ │     │G│L│G│L│G│L│G│L│G│L║
  10016.       ║   │   │   │   │   │   │   │   │0 0│0│0 0 0│ │ │ │ │ │ │ │ │ │ ║ DR7
  10017.       ║ 3 │ 3 │ 2 │ 2 │ 1 │ 1 │ 0 │ 0 │   │ │     │E│E│3│3│2│2│1│1│0│0║
  10018.       ╟───┴───┴───┴───┴───┴───┴───┴───┼─┬─┼─┼─────┴─┴─┴─┴─┴─┴─┼─┼─┼─┼─╢
  10019.       ║                               │B│B│B│                 │B│B│B│B║
  10020.       ║0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0│ │ │ │0 0 0 0 0 0 0 0 0│ │ │ │ ║ DR6
  10021.       ║                               │T│S│D│                 │3│2│1│0║
  10022.       ╟───────────────┼───────────────┼─┴─┴─┴─────────┼───────┴─┴─┴─┴─╢
  10023.       ║                                                               ║
  10024.       ║                            RESERVED                           ║ DR5
  10025.       ║                                                               ║
  10026.       ╟───────────────┼───────────────┼───────────────┼───────────────╢
  10027.       ║                                                               ║
  10028.       ║                            RESERVED                           ║ DR4
  10029.       ║                                                               ║
  10030.       ╟───────────────┼───────────────┼───────────────┼───────────────╢
  10031.       ║                                                               ║
  10032.       ║                 BREAKPOINT 3 LINEAR ADDRESS                   ║ DR3
  10033.       ║                                                               ║
  10034.       ╟───────────────┼───────────────┼───────────────┼───────────────╢
  10035.       ║                                                               ║
  10036.       ║                 BREAKPOINT 2 LINEAR ADDRESS                   ║ DR2
  10037.       ║                                                               ║
  10038.       ╟───────────────┼───────────────┼───────────────┼───────────────╢
  10039.       ║                                                               ║
  10040.       ║                 BREAKPOINT 1 LINEAR ADDRESS                   ║ DR1
  10041.       ║                                                               ║
  10042.       ╟───────────────┼───────────────┼───────────────┼───────────────╢
  10043.       ║                                                               ║
  10044.       ║                 BREAKPOINT 0 LINEAR ADDRESS                   ║ DR0
  10045.       ║                                                               ║
  10046.       ╚═══════════════╪═══════════════╪════════════════╪══════════════╝
  10047.  
  10048.  ────────────────────────────────────────────────────────────────────────────
  10049.  NOTE
  10050.        0 MEANS INTEL RESERVED. DO NOT DEFINE.
  10051.  ────────────────────────────────────────────────────────────────────────────
  10052.  
  10053.  
  10054.  12.2.1  Debug Address Registers (DR0-DR3)
  10055.  
  10056.  Each of these registers contains the linear address associated with one of
  10057.  four breakpoint conditions. Each breakpoint condition is further defined by
  10058.  bits in DR7.
  10059.  
  10060.  The debug address registers are effective whether or not paging is enabled.
  10061.  The addresses in these registers are linear addresses. If paging is enabled,
  10062.  the linear addresses are translated into physical addresses by the
  10063.  processor's paging mechanism (as explained in Chapter 5). If paging is not
  10064.  enabled, these linear addresses are the same as physical addresses.
  10065.  
  10066.  Note that when paging is enabled, different tasks may have different
  10067.  linear-to-physical address mappings. When this is the case, an address in a
  10068.  debug address register may be relevant to one task but not to another. For
  10069.  this reason the 80386  has both global and local enable bits in DR7. These
  10070.  bits indicate whether a given debug address has a global (all tasks) or
  10071.  local (current task only) relevance.
  10072.  
  10073.  
  10074.  12.2.2  Debug Control Register (DR7)
  10075.  
  10076.  The debug control register shown in Figure 12-1 both helps to define the
  10077.  debug conditions and selectively enables and disables those conditions.
  10078.  
  10079.  For each address in registers DR0-DR3, the corresponding fields R/W0
  10080.  through R/W3 specify the type of action that should cause a breakpoint. The
  10081.  processor interprets these bits as follows:
  10082.  
  10083.     00 ── Break on instruction execution only
  10084.     01 ── Break on data writes only
  10085.     10 ── undefined
  10086.     11 ── Break on data reads or writes but not instruction fetches
  10087.  
  10088.  Fields LEN0 through LEN3 specify the length of data item to be monitored. A
  10089.  length of 1, 2, or 4 bytes may be specified. The values of the length fields
  10090.  are interpreted as follows:
  10091.  
  10092.     00 ── one-byte length
  10093.     01 ── two-byte length
  10094.     10 ── undefined
  10095.     11 ── four-byte length
  10096.  
  10097.  If RWn is 00 (instruction execution), then LENn should also be 00. Any other
  10098.  length is undefined.
  10099.  
  10100.  The low-order eight bits of DR7 (L0 through L3 and G0 through G3)
  10101.  selectively enable the four address breakpoint conditions. There are two
  10102.  levels of enabling: the local (L0 through L3) and global (G0 through G3)
  10103.  levels. The local enable bits are automatically reset by the processor at
  10104.  every task switch to avoid unwanted breakpoint conditions in the new task.
  10105.  The global enable bits are not reset by a task switch; therefore, they can
  10106.  be used for conditions that are global to all tasks.
  10107.  
  10108.  The LE and GE bits control the "exact data breakpoint match" feature of the
  10109.  processor. If either LE or GE is set, the processor slows execution so that
  10110.  data breakpoints are reported on the instruction that causes them. It is
  10111.  recommended that one of these bits be set whenever data breakpoints are
  10112.  armed. The processor clears LE at a task switch but does not clear GE.
  10113.  
  10114.  
  10115.  12.2.3  Debug Status Register (DR6)
  10116.  
  10117.  The debug status register shown in Figure 12-1 permits the debugger to
  10118.  determine which debug conditions have occurred.
  10119.  
  10120.  When the processor detects an enabled debug exception, it sets the
  10121.  low-order bits of this register (B0 thru B3) before entering the debug
  10122.  exception handler. Bn is set if the condition described by DRn, LENn, and
  10123.  R/Wn occurs. (Note that the processor sets Bn regardless of whether Gn or
  10124.  Ln is set. If more than one breakpoint condition occurs at one time and if
  10125.  the breakpoint trap occurs due to an enabled condition other than n, Bn may
  10126.  be set, even though neither Gn nor Ln is set.)
  10127.  
  10128.  The BT bit is associated with the T-bit (debug trap bit) of the TSS (refer
  10129.  to 7 for the location of the T-bit). The processor sets the BT bit before
  10130.  entering the debug handler if a task switch has occurred and the T-bit of
  10131.  the new TSS is set. There is no corresponding bit in DR7 that enables and
  10132.  disables this trap; the T-bit of the TSS is the sole enabling bit.
  10133.  
  10134.  The BS bit is associated with the TF (trap flag) bit of the EFLAGS
  10135.  register. The BS bit is set if the debug handler is entered due to the
  10136.  occurrence of a single-step exception. The single-step trap is the
  10137.  highest-priority debug exception; therefore, when BS is set, any of the
  10138.  other debug status bits may also be set.
  10139.  
  10140.  The BD bit is set if the next instruction will read or write one of the
  10141.  eight debug registers and ICE-386 is also using the debug registers at the
  10142.  same time.
  10143.  
  10144.  Note that the bits of DR6 are never cleared by the processor. To avoid any
  10145.  confusion in identifying the next debug exception, the debug handler should
  10146.  move zeros to DR6 immediately before returning.
  10147.  
  10148.  
  10149.  12.2.4  Breakpoint Field Recognition
  10150.  
  10151.  The linear address and LEN field for each of the four breakpoint conditions
  10152.  define a range of sequential byte addresses for a data breakpoint. The LEN
  10153.  field permits specification of a one-, two-, or four-byte field. Two-byte
  10154.  fields must be aligned on word boundaries (addresses that are multiples of
  10155.  two) and four-byte fields must be aligned on doubleword boundaries
  10156.  (addresses that are multiples of four). These requirements are enforced by
  10157.  the processor; it uses the LEN bits to mask the low-order bits of the
  10158.  addresses in the debug address registers. Improperly aligned code or data
  10159.  breakpoint addresses will not yield the expected results.
  10160.  
  10161.  A data read or write breakpoint is triggered if any of the bytes
  10162.  participating in a memory access is within the field defined by a breakpoint
  10163.  address register and the corresponding LEN field. Table 12-1 gives some
  10164.  examples of breakpoint fields with memory references that both do and do not
  10165.  cause traps.
  10166.  
  10167.  To set a data breakpoint for a misaligned field longer than one byte, it
  10168.  may be desirable to put two sets of entries in the breakpoint register such
  10169.  that each entry is properly aligned and the two entries together span the
  10170.  length of the field.
  10171.  
  10172.  Instruction breakpoint addresses must have a length specification of one
  10173.  byte (LEN = 00); other values are undefined. The processor recognizes an
  10174.  instruction breakpoint address only when it points to the first byte of an
  10175.  instruction. If the instruction has any prefixes, the breakpoint address
  10176.  must point to the first prefix.
  10177.  
  10178.  
  10179.  Table 12-1. Breakpoint Field Recognition Examples
  10180.  
  10181.                                      Address (hex)          Length
  10182.  
  10183.                         DR0             0A0001          1 (LEN0 = 00)
  10184.  Register Contents      DR1             0A0002          1 (LEN1 = 00)
  10185.                         DR2             0B0002          2 (LEN2 = 01)
  10186.                         DR3             0C0000          4 (LEN3 = 11)
  10187.  
  10188.  Some Examples of Memory                0A0001          1
  10189.  References That Cause Traps            0A0002          1
  10190.                                         0A0001          2
  10191.                                         0A0002          2
  10192.                                         0B0002          2
  10193.                                         0B0001          4
  10194.                                         0C0000          4
  10195.                                         0C0001          2
  10196.                                         0C0003          1
  10197.  
  10198.  Some Examples of Memory                0A0000          1
  10199.  References That Don't Cause Traps      0A0003          4
  10200.                                         0B0000          2
  10201.                                         0C0004          4
  10202.  
  10203.  
  10204.  12.3  Debug Exceptions
  10205.  
  10206.  Two of the interrupt vectors of the 80386 are reserved for exceptions that
  10207.  relate to debugging. Interrupt 1 is the primary means of invoking debuggers
  10208.  designed expressly for the 80386; interrupt 3 is intended for debugging
  10209.  debuggers and for compatibility with prior processors in Intel's 8086
  10210.  processor family.
  10211.  
  10212.  
  10213.  12.3.1  Interrupt 1 ── Debug Exceptions
  10214.  
  10215.  The handler for this exception is usually a debugger or part of a debugging
  10216.  system. The processor causes interrupt 1 for any of several conditions. The
  10217.  debugger can check flags in DR6 and DR7 to determine what condition caused
  10218.  the exception and what other conditions might be in effect at the same time.
  10219.  Table 12-2 associates with each breakpoint condition the combination of
  10220.  bits that indicate when that condition has caused the debug exception.
  10221.  
  10222.  Instruction address breakpoint conditions are faults, while other debug
  10223.  conditions are traps. The debug exception may report either or both at one
  10224.  time. The following paragraphs present details for each class of debug
  10225.  exception.
  10226.  
  10227.  
  10228.  Table 12-2. Debug Exception Conditions
  10229.  
  10230.  Flags to Test              Condition
  10231.  
  10232.  BS=1                       Single-step trap
  10233.  B0=1 AND (GE0=1 OR LE0=1)  Breakpoint DR0, LEN0, R/W0
  10234.  B1=1 AND (GE1=1 OR LE1=1)  Breakpoint DR1, LEN1, R/W1
  10235.  B2=1 AND (GE2=1 OR LE2=1)  Breakpoint DR2, LEN2, R/W2
  10236.  B3=1 AND (GE3=1 OR LE3=1)  Breakpoint DR3, LEN3, R/W3
  10237.  BD=1                       Debug registers not available; in use by ICE-386.
  10238.  BT=1                       Task switch
  10239.  
  10240.  
  10241.  12.3.1.1  Instruction Addrees Breakpoint
  10242.  
  10243.  The processor reports an instruction-address breakpoint before it executes
  10244.  the instruction that begins at the given address; i.e., an instruction-
  10245.  address breakpoint exception is a fault.
  10246.  
  10247.  The RF (restart flag) permits the debug handler to retry instructions that
  10248.  cause other kinds of faults in addition to debug faults. When it detects a
  10249.  fault, the processor automatically sets RF in the flags image that it pushes
  10250.  onto the stack. (It does not, however, set RF for traps and aborts.)
  10251.  
  10252.  When RF is set, it causes any debug fault to be ignored during the next
  10253.  instruction. (Note, however, that RF does not cause breakpoint traps to be
  10254.  ignored, nor other kinds of faults.)
  10255.  
  10256.  The processor automatically clears RF at the successful completion of every
  10257.  instruction except after the IRET instruction, after the POPF instruction,
  10258.  and after a JMP, CALL, or INT instruction that causes a task switch. These
  10259.  instructions set RF to the value specified by the memory image of the EFLAGS
  10260.  register.
  10261.  
  10262.  The processor automatically sets RF in the EFLAGS image on the stack before
  10263.  entry into any fault handler. Upon entry into the fault handler for
  10264.  instruction address breakpoints, for example, RF is set in the EFLAGS image
  10265.  on the stack; therefore, the IRET instruction at the end of the handler will
  10266.  set RF in the EFLAGS register, and execution will resume at the breakpoint
  10267.  address without generating another breakpoint fault at the same address.
  10268.  
  10269.  If, after a debug fault, RF is set and the debug handler retries the
  10270.  faulting instruction, it is possible that retrying the instruction will
  10271.  raise other faults. The retry of the instruction after these faults will
  10272.  also be done with RF=1, with the result that debug faults continue to be
  10273.  ignored. The processor clears RF only after successful completion of the
  10274.  instruction.
  10275.  
  10276.  Real-mode debuggers can control the RF flag by using a 32-bit IRET. A
  10277.  16-bit IRET instruction does not affect the RF bit (which is in the
  10278.  high-order 16 bits of EFLAGS). To use a 32-bit IRET, the debugger must
  10279.  rearrange the stack so that it holds appropriate values for the 32-bit EIP,
  10280.  CS, and EFLAGS (with RF set in the EFLAGS image). Then executing an IRET
  10281.  with an operand size prefix causes a 32-bit return, popping the RF flag
  10282.  into EFLAGS.
  10283.  
  10284.  
  10285.  12.3.1.2  Data Address Breakpoint
  10286.  
  10287.  A data-address breakpoint exception is a trap; i.e., the processor reports
  10288.  a data-address breakpoint after executing the instruction that accesses the
  10289.  given memory item.
  10290.  
  10291.  When using data breakpoints it is recommended that either the LE or GE bit
  10292.  of DR7 be set also. If either LE or GE is set, any data breakpoint trap is
  10293.  reported exactly after completion of the instruction that accessed the
  10294.  specified memory item. This exact reporting is accomplished by forcing the
  10295.  80386 execution unit to wait for completion of data operand transfers before
  10296.  beginning execution of the next instruction. If neither GE nor LE is set,
  10297.  data breakpoints may not be reported until one instruction after the data is
  10298.  accessed or may not be reported at all. This is due to the fact that,
  10299.  normally, instruction execution is overlapped with memory transfers to such
  10300.  a degree that execution of the next instruction may begin before memory
  10301.  transfers for the prior instruction are completed.
  10302.  
  10303.  If a debugger needs to preserve the contents of a write breakpoint
  10304.  location, it should save the original contents before setting a write
  10305.  breakpoint. Because data breakpoints are traps, a write into a breakpoint
  10306.  location will complete before the trap condition is reported. The handler
  10307.  can report the saved value after the breakpoint is triggered. The data in
  10308.  the debug registers can be used to address the new value stored by the
  10309.  instruction that triggered the breakpoint.
  10310.  
  10311.  
  10312.  12.3.1.3  General Detect Fault
  10313.  
  10314.  This exception occurs when an attempt is made to use the debug registers at
  10315.  the same time that ICE-386 is using them. This additional protection feature
  10316.  is provided to guarantee that ICE-386 can have full control over the
  10317.  debug-register resources when required. ICE-386 uses the debug-registers;
  10318.  therefore, a software debugger that also uses these registers cannot run
  10319.  while ICE-386 is in use. The exception handler can detect this condition by
  10320.  examining the BD bit of DR6.
  10321.  
  10322.  
  10323.  12.3.1.4  Single-Step Trap
  10324.  
  10325.  This debug condition occurs at the end of an instruction if the trap flag
  10326.  (TF) of the flags register held the value one at the beginning of that
  10327.  instruction.  Note that the exception does not occur at the end of an
  10328.  instruction that sets TF. For example, if POPF is used to set TF, a
  10329.  single-step trap does not occur until after the instruction that follows
  10330.  POPF.
  10331.  
  10332.  The processor clears the TF bit before invoking the handler.  If TF=1 in
  10333.  the flags image of a TSS at the time of a task switch, the exception occurs
  10334.  after the first instruction is executed in the new task.
  10335.  
  10336.  The single-step flag is normally not cleared by privilege changes inside a
  10337.  task.  INT instructions, however, do clear TF.  Therefore, software
  10338.  debuggers that single-step code must recognize and emulate INT n or INTO
  10339.  rather than executing them directly.
  10340.  
  10341.  To maintain protection, system software should check the current execution
  10342.  privilege level after any single step interrupt to see whether single
  10343.  stepping should continue at the current privilege level.
  10344.  
  10345.  The interrupt priorities in hardware guarantee that if an external
  10346.  interrupt occurs, single stepping stops. When both an external interrupt and
  10347.  a single step interrupt occur together, the single step interrupt is
  10348.  processed first. This clears the TF bit. After saving the return address or
  10349.  switching tasks, the external interrupt input is examined before the first
  10350.  instruction of the single step handler executes.  If the external interrupt
  10351.  is still pending, it is then serviced. The external interrupt handler is not
  10352.  single-stepped. To single step an interrupt handler, just single step an INT
  10353.  n instruction that refers to the interrupt handler.
  10354.  
  10355.  
  10356.  12.3.1.5  Task Switch Breakpoint
  10357.  
  10358.  The debug exception also occurs after a switch to an 80386 task if the
  10359.  T-bit of the new TSS is set.  The exception occurs after control has passed
  10360.  to the new task, but before the first instruction of that task is executed.
  10361.  The exception handler can detect this condition by examining the BT bit of
  10362.  the debug status register DR6.
  10363.  
  10364.  Note that if the debug exception handler is a task, the T-bit of its TSS
  10365.  should not be set. Failure to observe this rule will cause the processor to
  10366.  enter an infinite loop.
  10367.  
  10368.  
  10369.  12.3.2  Interrupt 3 ── Breakpoint Exception
  10370.  
  10371.  This exception is caused by execution of the breakpoint instruction INT 3.
  10372.  Typically, a debugger prepares a breakpoint by substituting the opcode of
  10373.  the one-byte breakpoint instruction in place of the first opcode byte of the
  10374.  instruction to be trapped. When execution of the INT 3 instruction causes
  10375.  the exception handler to be invoked, the saved value of ES:EIP points to the
  10376.  byte following the INT 3 instruction.
  10377.  
  10378.  With prior generations of processors, this feature is used extensively for
  10379.  trapping execution of specific instructions. With the 80386, the needs
  10380.  formerly filled by this feature are more conveniently solved via the debug
  10381.  registers and interrupt 1.  However, the breakpoint exception is still
  10382.  useful for debugging debuggers, because the breakpoint exception can vector
  10383.  to a different exception handler than that used by the debugger. The
  10384.  breakpoint exception can also be useful when it is necessary to set a
  10385.  greater number of breakpoints than permitted by the debug registers.
  10386.  
  10387.  
  10388.                            PART III  COMPATIBILITY
  10389.  
  10390.  
  10391.  Chapter 13  Executing 80286 Protected-Mode Code
  10392.  
  10393.  ────────────────────────────────────────────────────────────────────────────
  10394.  
  10395.  13.1  80286 Code Executes as a Subset of the 80386
  10396.  
  10397.  In general, programs designed for execution in protected mode on an 80286
  10398.  execute without modification on the 80386, because the features of the 80286
  10399.  are a subset of those of the 80386.
  10400.  
  10401.  All the descriptors used by the 80286 are supported by the 80386 as long as
  10402.  the Intel-reserved word (last word) of the 80286 descriptor is zero.
  10403.  
  10404.  The descriptors for data segments, executable segments, local descriptor
  10405.  tables, and task gates are common to both the 80286 and the 80386. Other
  10406.  80286 descriptors──TSS segment, call gate, interrupt gate, and trap
  10407.  gate──are supported by the 80386. The 80386 also has new versions of
  10408.  descriptors for TSS segment, call gate, interrupt gate, and trap gate that
  10409.  support the 32-bit nature of the 80386. Both sets of descriptors can be
  10410.  used simultaneously in the same system.
  10411.  
  10412.  For those descriptors that are common to both the 80286 and the 80386, the
  10413.  presence of zeros in the final word causes the 80386 to interpret these
  10414.  descriptors exactly as 80286 does; for example:
  10415.  
  10416.  Base Address      The high-order eight bits of the 32-bit base address are
  10417.                    zero, limiting base addresses to 24 bits.
  10418.  
  10419.  Limit             The high-order four bits of the limit field are zero,
  10420.                    restricting the value of the limit field to 64K.
  10421.  
  10422.  Granularity bit   The granularity bit is zero, which implies that the value
  10423.                    of the 16-bit limit is interpreted in units of one byte.
  10424.  
  10425.  B-bit             In a data-segment descriptor, the B-bit is zero, implying
  10426.                    that the segment is no larger than 64 Kbytes.
  10427.  
  10428.  D-bit             In an executable-segment descriptor, the D-bit is zero,
  10429.                    implying that 16-bit addressing and operands are the
  10430.                    default.
  10431.  
  10432.  For formats of these descriptors and documentation of their use refer to
  10433.  the iAPX 286 Programmer's Reference Manual.
  10434.  
  10435.  
  10436.  13.2  Two ways to Execute 80286 Tasks
  10437.  
  10438.  When porting 80286 programs to the 80386, there are two cases to consider:
  10439.  
  10440.    1.  Porting an entire 80286 system to the 80386, complete with 80286
  10441.        operating system, loader, and system builder.
  10442.  
  10443.        In this case, all tasks will have 80286 TSSs. The 80386 is being used
  10444.        as a faster 286.
  10445.  
  10446.    2.  Porting selected 80286 applications to run in an 80386 environment
  10447.        with an 80386 operating system, loader, and system builder.
  10448.  
  10449.        In this case, the TSSs used to represent 80286 tasks should be
  10450.        changed to 80386 TSSs. It is theoretically possible to mix 80286 and
  10451.        80386 TSSs, but the benefits are slight and the problems are great. It
  10452.        is recommended that all tasks in a 80386 software system have 80386
  10453.        TSSs. It is not necessary to change the 80286 object modules
  10454.        themselves; TSSs are usually constructed by the operating system, by
  10455.        the loader, or by the system builder. Refer to Chapter 16 for further
  10456.        discussion of the interface between 16-bit and 32-bit code.
  10457.  
  10458.  
  10459.  13.3  Differences From 80286
  10460.  
  10461.  The few differences that do exist primarily affect operating system code.
  10462.  
  10463.  
  10464.  13.3.1  Wraparound of 80286 24-Bit Physical Address Space
  10465.  
  10466.  With the 80286, any base and offset combination that addresses beyond 16M
  10467.  bytes wraps around to the first megabyte of the 80286 address space. With
  10468.  the 80386, since it has a greater physical address space, any such address
  10469.  falls into the 17th megabyte. In the unlikely event that any software
  10470.  depends on this anomaly, the same effect can be simulated on the 80386 by
  10471.  using paging to map the first 64K bytes of the 17th megabyte of logical
  10472.  addresses to physical addresses in the first megabyte.
  10473.  
  10474.  
  10475.  13.3.2  Reserved Word of Descriptor
  10476.  
  10477.  Because the 80386 uses the contents of the reserved word (last word) of
  10478.  every descriptor, 80286 programs that place values in this word may not
  10479.  execute correctly on the 80386.
  10480.  
  10481.  
  10482.  13.3.3  New Descriptor Type Codes
  10483.  
  10484.  Operating-system code that manages space in descriptor tables often uses an
  10485.  invalid value in the access-rights field of descriptor-table entries to
  10486.  identify unused entries. Access rights values of 80H and 00H remain invalid
  10487.  for both the 80286 and 80386. Other values that were invalid on for the
  10488.  80286 may be valid for the 80386 because of the additional descriptor types
  10489.  defined by the 80386.
  10490.  
  10491.  
  10492.  13.3.4  Restricted Semantics of LOCK
  10493.  
  10494.  The 80286 processor implements the bus lock function differently than the
  10495.  80386. Programs that use forms of memory locking specific to the 80286 may
  10496.  not execute properly when transported to a specific application of the
  10497.  80386.
  10498.  
  10499.  The LOCK prefix and its corresponding output signal should only be used to
  10500.  prevent other bus masters from interrupting a data movement operation.  LOCK
  10501.  may only be used with the following 80386 instructions when they modify
  10502.  memory. An undefined-opcode exception results from using LOCK before any
  10503.  other instruction.
  10504.  
  10505.    ■  Bit test and change:  BTS, BTR, BTC.
  10506.    ■  Exchange: XCHG.
  10507.    ■  One-operand arithmetic and logical: INC, DEC, NOT, and NEG.
  10508.    ■  Two-operand arithmetic and logical:  ADD, ADC, SUB, SBB, AND, OR, XOR.
  10509.  
  10510.  A locked instruction is guaranteed to lock only the area of memory defined
  10511.  by the destination operand, but may lock a larger memory area.  For example,
  10512.  typical 8086 and 80286 configurations lock the entire physical memory space.
  10513.  With the 80386, the defined area of memory is guaranteed to be locked
  10514.  against access by a processor executing a locked instruction on exactly the
  10515.  same memory area, i.e., an operand with identical starting address and
  10516.  identical length.
  10517.  
  10518.  
  10519.  13.3.5  Additional Exceptions
  10520.  
  10521.  The 80386 defines new exceptions that can occur even in systems designed
  10522.  for the 80286.
  10523.  
  10524.    ■  Exception #6 ── invalid opcode
  10525.  
  10526.       This exception can result from improper use of the LOCK instruction.
  10527.  
  10528.    ■  Exception #14 ── page fault
  10529.  
  10530.       This exception may occur in an 80286 program if the operating system
  10531.       enables paging. Paging can be used in a system with 80286 tasks as long
  10532.       as all tasks use the same page directory. Because there is no place in
  10533.       an 80286 TSS to store the PDBR, switching to an 80286 task does not
  10534.       change the value of PDBR. Tasks ported from the 80286 should be given
  10535.       80386 TSSs so they can take full advantage of paging.
  10536.  
  10537.  
  10538.  Chapter 14  80386 Real-Address Mode
  10539.  
  10540.  ────────────────────────────────────────────────────────────────────────────
  10541.  
  10542.  The real-address mode of the 80386 executes object code designed for
  10543.  execution on 8086, 8088, 80186, or 80188 processors, or for execution in the
  10544.  real-address mode of an 80286:
  10545.  
  10546.  In effect, the architecture of the 80386 in this mode is almost identical
  10547.  to that of the 8086, 8088, 80186, and 80188. To a programmer, an 80386 in
  10548.  real-address mode appears as a high-speed 8086 with extensions to the
  10549.  instruction set and registers. The principal features of this architecture
  10550.  are defined in Chapters 2 and 3.
  10551.  
  10552.  This chapter discusses certain additional topics that complete the system
  10553.  programmer's view of the 80386 in real-address mode:
  10554.  
  10555.    ■  Address formation.
  10556.    ■  Extensions to registers and instructions.
  10557.    ■  Interrupt and exception handling.
  10558.    ■  Entering and leaving real-address mode.
  10559.    ■  Real-address-mode exceptions.
  10560.    ■  Differences from 8086.
  10561.    ■  Differences from 80286 real-address mode.
  10562.  
  10563.  
  10564.  14.1  Physical Address Formation
  10565.  
  10566.  The 80386 provides a one Mbyte + 64 Kbyte memory space for an 8086 program.
  10567.  Segment relocation is performed as in the 8086: the 16-bit value in a
  10568.  segment selector is shifted left by four bits to form the base address of a
  10569.  segment. The effective address is extended with four high order zeros and
  10570.  added to the base to form a linear address as Figure 14-1 illustrates. (The
  10571.  linear address is equivalent to the physical address, because paging is not
  10572.  used in real-address mode.) Unlike the 8086, the resulting linear address
  10573.  may have up to 21 significant bits. There is a possibility of a carry when
  10574.  the base address is added to the effective address. On the 8086, the carried
  10575.  bit is truncated, whereas on the 80386 the carried bit is stored in bit
  10576.  position 20 of the linear address.
  10577.  
  10578.  Unlike the 8086 and 80286, 32-bit effective addresses can be generated (via
  10579.  the address-size prefix); however, the value of a 32-bit address may not
  10580.  exceed 65535 without causing an exception. For full compatibility with 80286
  10581.  real-address mode, pseudo-protection faults (interrupt 12 or 13 with no
  10582.  error code) occur if an effective address is generated outside the range 0
  10583.  through 65535.
  10584.  
  10585.  
  10586.  Figure 14-1.  Real-Address Mode Address Formation
  10587.  
  10588.                        19                                3       0
  10589.                       ╔═════════════════════════════════╪═════════╗
  10590.           BASE        ║     16-BIT SEGMENT SELECTOR     │ 0 0 0 0 ║
  10591.                       ╚═════════════════════════════════╪═════════╝
  10592.  
  10593.           +
  10594.                        19        15                              0
  10595.                       ╔═════════╪═════════════════════════════════╗
  10596.           OFFSET      ║ 0 0 0 0 │    16-BIT EFFECTIVE ADDRESS     ║
  10597.                       ╚═════════╪═════════════════════════════════╝
  10598.  
  10599.           =
  10600.                      20                                          0
  10601.           LINEAR    ╔═════════════════════════════════════════════╗
  10602.           ADDRESS   ║ X X X X X X X X X X X X X X X X X X X X X X ║
  10603.                     ╚═════════════════════════════════════════════╝
  10604.  
  10605.  
  10606.  14.2  Registers and Instructions
  10607.  
  10608.  The register set available in real-address mode includes all the registers
  10609.  defined for the 8086 plus the new registers introduced by the 80386: FS, GS,
  10610.  debug registers, control registers, and test registers. New instructions
  10611.  that explicitly operate on the segment registers FS and GS are available,
  10612.  and the new segment-override prefixes can be used to cause instructions to
  10613.  utilize FS and GS for address calculations. Instructions can utilize 32-bit
  10614.  operands through the use of the operand size prefix.
  10615.  
  10616.  The instruction codes that cause undefined opcode traps (interrupt 6)
  10617.  include instructions of the protected mode that manipulate or interrogate
  10618.  80386 selectors and descriptors; namely, VERR, VERW, LAR, LSL, LTR, STR,
  10619.  LLDT, and SLDT. Programs executing in real-address mode are able to take
  10620.  advantage of the new applications-oriented instructions added to the
  10621.  architecture by the introduction of the 80186/80188, 80286 and 80386:
  10622.  
  10623.  ■ New instructions introduced by 80186/80188 and 80286.
  10624.  
  10625.     ── PUSH immediate data
  10626.     ── Push all and pop all (PUSHA and POPA)
  10627.     ── Multiply immediate data
  10628.     ── Shift and rotate by immediate count
  10629.     ── String I/O
  10630.     ── ENTER and LEAVE
  10631.     ── BOUND
  10632.  
  10633.  ■ New instructions introduced by 80386.
  10634.  
  10635.     ── LSS, LFS, LGS instructions
  10636.     ── Long-displacement conditional jumps
  10637.     ── Single-bit instructions
  10638.     ── Bit scan
  10639.     ── Double-shift instructions
  10640.     ── Byte set on condition
  10641.     ── Move with sign/zero extension
  10642.     ── Generalized multiply
  10643.     ── MOV to and from control registers
  10644.     ── MOV to and from test registers
  10645.     ── MOV to and from debug registers
  10646.  
  10647.  
  10648.  14.3  Interrupt and Exception Handling
  10649.  
  10650.  Interrupts and exceptions in 80386 real-address mode work as much as they
  10651.  do on an 8086. Interrupts and exceptions vector to interrupt procedures via
  10652.  an interrupt table. The processor multiplies the interrupt or exception
  10653.  identifier by four to obtain an index into the interrupt table. The entries
  10654.  of the interrupt table are far pointers to the entry points of interrupt or
  10655.  exception handler procedures. When an interrupt occurs, the processor
  10656.  pushes the current values of CS:IP onto the stack, disables interrupts,
  10657.  clears TF (the single-step flag), then transfers control to the location
  10658.  specified in the interrupt table. An IRET instruction at the end of the
  10659.  handler procedure reverses these steps before returning control to the
  10660.  interrupted procedure.
  10661.  
  10662.  The primary difference in the interrupt handling of the 80386 compared to
  10663.  the 8086 is that the location and size of the interrupt table depend on the
  10664.  contents of the IDTR (IDT register). Ordinarily, this fact is not apparent
  10665.  to programmers, because, after RESET, the IDTR contains a base address of 0
  10666.  and a limit of 3FFH, which is compatible with the 8086. However, the LIDT
  10667.  instruction can be used in real-address mode to change the base and limit
  10668.  values in the IDTR. Refer to Chapter 9 for details on the IDTR, and the
  10669.  LIDT and SIDT instructions. If an interrupt occurs and the corresponding
  10670.  entry of the interrupt table is beyond the limit stored in the IDTR, the
  10671.  processor raises exception 8.
  10672.  
  10673.  
  10674.  14.4  Entering and Leaving Real-Address Mode
  10675.  
  10676.  Real-address mode is in effect after a signal on the RESET pin. Even if the
  10677.  system is going to be used in protected mode, the start-up program will
  10678.  execute in real-address mode temporarily while initializing for protected
  10679.  mode.
  10680.  
  10681.  
  10682.  14.4.1  Switching to Protected Mode
  10683.  
  10684.  The only way to leave real-address mode is to switch to protected mode. The
  10685.  processor enters protected mode when a MOV to CR0 instruction sets the PE
  10686.  (protection enable) bit in CR0. (For compatibility with the 80286, the LMSW
  10687.  instruction may also be used to set the PE bit.)
  10688.  
  10689.  Refer to Chapter 10 "Initialization" for other aspects of switching to
  10690.  protected mode.
  10691.  
  10692.  
  10693.  14.5  Switching Back to Real-Address Mode
  10694.  
  10695.  The processor reenters real-address mode if software clears the PE bit in
  10696.  CR0 with a MOV to CR0 instruction. A procedure that attempts to do this,
  10697.  however, should proceed as follows:
  10698.  
  10699.    1.  If paging is enabled, perform the following sequence:
  10700.  
  10701.        ■  Transfer control to linear addresses that have an identity mapping;
  10702.           i.e., linear addresses equal physical addresses.
  10703.  
  10704.        ■  Clear the PG bit in CR0.
  10705.  
  10706.        ■  Move zeros to CR3 to clear out the paging cache.
  10707.  
  10708.    2.  Transfer control to a segment that has a limit of 64K (FFFFH). This
  10709.        loads the CS register with the limit it needs to have in real mode.
  10710.  
  10711.    3.  Load segment registers SS, DS, ES, FS, and GS with a selector that
  10712.        points to a descriptor containing the following values, which are
  10713.        appropriate to real mode:
  10714.  
  10715.        ■  Limit = 64K   (FFFFH)
  10716.        ■  Byte granular (G = 0)
  10717.        ■  Expand up     (E = 0)
  10718.        ■  Writable      (W = 1)
  10719.        ■  Present       (P = 1)
  10720.        ■  Base = any value
  10721.  
  10722.    4.  Disable interrupts. A CLI instruction disables INTR interrupts. NMIs
  10723.        can be disabled with external circuitry.
  10724.  
  10725.    5.  Clear the PE bit.
  10726.  
  10727.    6.  Jump to the real mode code to be executed using a far JMP. This
  10728.        action flushes the instruction queue and puts appropriate values in
  10729.        the access rights of the CS register.
  10730.  
  10731.    7.  Use the LIDT instruction to load the base and limit of the real-mode
  10732.        interrupt vector table.
  10733.  
  10734.    8.  Enable interrupts.
  10735.  
  10736.    9.  Load the segment registers as needed by the real-mode code.
  10737.  
  10738.  
  10739.  14.6  Real-Address Mode Exceptions
  10740.  
  10741.  The 80386 reports some exceptions differently when executing in
  10742.  real-address mode than when executing in protected mode. Table 14-1 details
  10743.  the real-address-mode exceptions.
  10744.  
  10745.  
  10746.  14.7  Differences From 8086
  10747.  
  10748.  In general, the 80386 in real-address mode will correctly execute ROM-based
  10749.  software designed for the 8086, 8088, 80186, and 80188. Following is a list
  10750.  of the minor differences between 8086 execution on the 80386 and on an 8086.
  10751.  
  10752.    1.  Instruction clock counts.
  10753.  
  10754.        The 80386 takes fewer clocks for most instructions than the 8086/8088.
  10755.        The areas most likely to be affected are:
  10756.  
  10757.        ■  Delays required by I/O devices between I/O operations.
  10758.  
  10759.        ■  Assumed delays with 8086/8088 operating in parallel with an 8087.
  10760.  
  10761.    2.  Divide Exceptions Point to the DIV instruction.
  10762.  
  10763.        Divide exceptions on the 80386 always leave the saved CS:IP value
  10764.        pointing to the instruction that failed. On the 8086/8088, the CS:IP
  10765.        value points to the next instruction.
  10766.  
  10767.    3.  Undefined 8086/8088 opcodes.
  10768.  
  10769.        Opcodes that were not defined for the 8086/8088 will cause exception
  10770.        6 or will execute one of the new instructions defined for the 80386.
  10771.  
  10772.    4.  Value written by PUSH SP.
  10773.  
  10774.        The 80386 pushes a different value on the stack for PUSH SP than the
  10775.        8086/8088. The 80386 pushes the value of SP before SP is incremented
  10776.        as part of the push operation; the 8086/8088 pushes the value of SP
  10777.        after it is incremented. If the value pushed is important, replace
  10778.        PUSH SP instructions with the following three instructions:
  10779.  
  10780.        PUSH  BP
  10781.        MOV   BP, SP
  10782.        XCHG  BP, [BP]
  10783.  
  10784.        This code functions as the 8086/8088 PUSH SP instruction on the 80386.
  10785.  
  10786.    5.  Shift or rotate by more than 31 bits.
  10787.  
  10788.        The 80386 masks all shift and rotate counts to the low-order five
  10789.        bits. This MOD 32 operation limits the count to a maximum of 31 bits,
  10790.        thereby limiting the time that interrupt response is delayed while
  10791.        the instruction is executing.
  10792.  
  10793.    6.  Redundant prefixes.
  10794.  
  10795.        The 80386 sets a limit of 15 bytes on instruction length. The only
  10796.        way to violate this limit is by putting redundant prefixes before an
  10797.        instruction. Exception 13 occurs if the limit on instruction length
  10798.        is violated. The 8086/8088 has no instruction length limit.
  10799.  
  10800.    7.  Operand crossing offset 0 or 65,535.
  10801.  
  10802.        On the 8086, an attempt to access a memory operand that crosses
  10803.        offset 65,535 (e.g., MOV a word to offset 65,535) or offset 0 (e.g.,
  10804.        PUSH a word when SP = 1) causes the offset to wrap around modulo
  10805.        65,536. The 80386 raises an exception in these cases──exception 13 if
  10806.        the segment is a data segment (i.e., if CS, DS, ES, FS, or GS is being
  10807.        used to address the segment), exception 12 if the segment is a stack
  10808.        segment (i.e., if SS is being used).
  10809.  
  10810.    8.  Sequential execution across offset 65,535.
  10811.  
  10812.        On the 8086, if sequential execution of instructions proceeds past
  10813.        offset 65,535, the processor fetches the next instruction byte from
  10814.        offset 0 of the same segment. On the 80386, the processor raises
  10815.        exception 13 in such a case.
  10816.  
  10817.    9.  LOCK is restricted to certain instructions.
  10818.  
  10819.        The LOCK prefix and its corresponding output signal should only be
  10820.        used to prevent other bus masters from interrupting a data movement
  10821.        operation. The 80386 always asserts the LOCK signal during an XCHG
  10822.        instruction with memory (even if the LOCK prefix is not used). LOCK
  10823.        may only be used with the following 80386 instructions when they
  10824.        update memory: BTS, BTR, BTC, XCHG, ADD, ADC, SUB, SBB, INC, DEC,
  10825.        AND, OR, XOR, NOT, and NEG. An undefined-opcode exception
  10826.        (interrupt 6) results from using LOCK before any other instruction.
  10827.  
  10828.   10.  Single-stepping external interrupt handlers.
  10829.  
  10830.        The priority of the 80386 single-step exception is different from that
  10831.        of the 8086/8088. The change prevents an external interrupt handler
  10832.        from being single-stepped if the interrupt occurs while a program is
  10833.        being single-stepped. The 80386 single-step exception has higher
  10834.        priority that any external interrupt. The 80386 will still single-step
  10835.        through an interrupt handler invoked by the INT instructions or by an
  10836.        exception.
  10837.  
  10838.   11.  IDIV exceptions for quotients of 80H or 8000H.
  10839.  
  10840.        The 80386 can generate the largest negative number as a quotient for
  10841.        the IDIV instruction. The 8086/8088 causes exception zero instead.
  10842.  
  10843.   12.  Flags in stack.
  10844.  
  10845.        The setting of the flags stored by PUSHF, by interrupts, and by
  10846.        exceptions is different from that stored by the 8086 in bit positions
  10847.        12 through 15. On the 8086 these bits are stored as ones, but in
  10848.        80386 real-address mode bit 15 is always zero, and bits 14 through 12
  10849.        reflect the last value loaded into them.
  10850.  
  10851.   13.  NMI interrupting NMI handlers.
  10852.  
  10853.        After an NMI is recognized on the 80386, the NMI interrupt is masked
  10854.        until an IRET instruction is executed.
  10855.  
  10856.   14.  Coprocessor errors vector to interrupt 16.
  10857.  
  10858.        Any 80386 system with a coprocessor must use interrupt vector 16 for
  10859.        the coprocessor error exception. If an 8086/8088 system uses another
  10860.        vector for the 8087 interrupt, both vectors should point to the
  10861.        coprocessor-error exception handler.
  10862.  
  10863.   15.  Numeric exception handlers should allow prefixes.
  10864.  
  10865.        On the 80386, the value of CS:IP saved for coprocessor exceptions
  10866.        points at any prefixes before an ESC instruction. On 8086/8088
  10867.        systems, the saved CS:IP points to the ESC instruction.
  10868.  
  10869.   16.  Coprocessor does not use interrupt controller.
  10870.  
  10871.        The coprocessor error signal to the 80386 does not pass through an
  10872.        interrupt controller (an 8087 INT signal does). Some instructions in
  10873.        a coprocessor error handler may need to be deleted if they deal with
  10874.        the interrupt controller.
  10875.  
  10876.   17.  Six new interrupt vectors.
  10877.  
  10878.        The 80386 adds six exceptions that arise only if the 8086 program has
  10879.        a hidden bug. It is recommended that exception handlers be added that
  10880.        treat these exceptions as invalid operations. This additional
  10881.        software does not significantly affect the existing 8086 software
  10882.        because the interrupts do not normally occur. These interrupt
  10883.        identifiers should not already have been used by the 8086 software,
  10884.        because they are in the range reserved by Intel. Table 14-2 describes
  10885.        the new 80386 exceptions.
  10886.  
  10887.   18.  One megabyte wraparound.
  10888.  
  10889.        The 80386 does not wrap addresses at 1 megabyte in real-address mode.
  10890.        On members of the 8086 family, it possible to specify addresses
  10891.        greater than one megabyte.  For example, with a selector value 0FFFFH
  10892.        and an offset of 0FFFFH, the effective address would be 10FFEFH (1
  10893.        Mbyte + 65519).  The 8086, which can form adresses only up to 20 bits
  10894.        long, truncates the high-order bit, thereby "wrapping" this address
  10895.        to 0FFEFH.  However, the 80386, which can form addresses up to 32
  10896.        bits long does not truncate such an address.
  10897.  
  10898.  
  10899.  Table 14-1. 80386 Real-Address Mode Exceptions
  10900.  
  10901. ╓┌────────────────────────────────┌──────────┌───────────────────────────────
  10902.  Description                      Interrupt  Function that Can
  10903.                                   Number     Generate the Exception
  10904.  
  10905.  Divide error                     0          DIV, IDIV
  10906.  Debug exceptions                 1          All
  10907.  
  10908.  
  10909.  
  10910.  
  10911.  Breakpoint                       3          INT
  10912.  Overflow                         4          INTO
  10913.  Bounds check                     5          BOUND
  10914.  Invalid opcode                   6          Any undefined opcode or LOCK
  10915.                                              used with wrong instruction
  10916.  Coprocessor not available        7          ESC or WAIT
  10917.  Interrupt table limit too small  8          INT vector is not within IDTR
  10918.                                              limit
  10919.  Reserved                         9-12
  10920.  Stack fault                      12         Memory operand crosses offset
  10921.                                              0 or 0FFFFH
  10922.  Description                      Interrupt  Function that Can
  10923.                                   Number     Generate the Exception
  10924.                                             0 or 0FFFFH
  10925.  Pseudo-protection exception      13         Memory operand crosses offset
  10926.                                              0FFFFH or attempt to execute
  10927.                                              past offset 0FFFFH or
  10928.                                              instruction longer than 15
  10929.                                              bytes
  10930.  Reserved                         14,15
  10931.  Coprocessor error                16         ESC or WAIT
  10932.  
  10933.  
  10934.  
  10935.  
  10936.  Two-byte SW interrupt            0-255      INT n
  10937.  
  10938.  
  10939.  Table 14-2. New 80386 Exceptions
  10940.  
  10941.  Interrupt   Function
  10942.  Identifier
  10943.  
  10944.      5       A BOUND instruction was executed with a register value outside
  10945.              the limit values.
  10946.  
  10947.      6       An undefined opcode was encountered or LOCK was used improperly
  10948.              before an instruction to which it does not apply.
  10949.  
  10950.      7       The EM bit in the MSW is set when an ESC instruction was
  10951.              encountered. This exception also occurs on a WAIT instruction
  10952.              if TS is set.
  10953.  
  10954.      8       An exception or interrupt has vectored to an interrupt table
  10955.              entry beyond the interrupt table limit in IDTR. This can occur
  10956.              only if the LIDT instruction has changed the limit from the
  10957.              default value of 3FFH, which is enough for all 256 interrupt
  10958.              IDs.
  10959.  
  10960.     12       Operand crosses extremes of stack segment, e.g., MOV operation
  10961.              at offset 0FFFFH or push with SP=1 during PUSH, CALL, or INT.
  10962.  
  10963.     13       Operand crosses extremes of a segment other than a stack
  10964.              segment; or sequential instruction execution attempts to
  10965.              proceed beyond offset 0FFFFH; or an instruction is longer than
  10966.              15 bytes (including prefixes).
  10967.  
  10968.  
  10969.  14.8  Differences From 80286 Real-Address Mode
  10970.  
  10971.  The few differences that exist between 80386 real-address mode and 80286
  10972.  real-address mode are not likely to affect any existing 80286 programs
  10973.  except possibly the system initialization procedures.
  10974.  
  10975.  
  10976.  14.8.1  Bus Lock
  10977.  
  10978.  The 80286 processor implements the bus lock function differently than the
  10979.  80386. Programs that use forms of memory locking specific to the 80286 may
  10980.  not execute properly if transported to a specific application of the 80386.
  10981.  
  10982.  The LOCK prefix and its corresponding output signal should only be used to
  10983.  prevent other bus masters from interrupting a data movement operation.  LOCK
  10984.  may only be used with the following 80386 instructions when they modify
  10985.  memory.  An undefined-opcode exception results from using LOCK before any
  10986.  other instruction.
  10987.  
  10988.    ■  Bit test and change:  BTS, BTR, BTC.
  10989.    ■  Exchange: XCHG.
  10990.    ■  One-operand arithmetic and logical: INC, DEC, NOT, and NEG.
  10991.    ■  Two-operand arithmetic and logical: ADD, ADC, SUB, SBB, AND, OR, XOR.
  10992.  
  10993.  A locked instruction is guaranteed to lock only the area of memory defined
  10994.  by the destination operand, but may lock a larger memory area.  For example,
  10995.  typical 8086 and 80286 configurations lock the entire physical memory space.
  10996.  With the 80386, the defined area of memory is guranteed to be locked against
  10997.  access by a processor executing a locked instruction on exactly the same
  10998.  memory area, i.e., an operand with identical starting address and identical
  10999.  length.
  11000.  
  11001.  
  11002.  14.8.2  Location of First Instruction
  11003.  
  11004.  The starting location is 0FFFFFFF0H (sixteen bytes from end of 32-bit
  11005.  address space) on the 80386 rather than 0FFFFF0H (sixteen bytes from end of
  11006.  24-bit address space) as on the 80286.  Many 80286 ROM initialization
  11007.  programs will work correctly in this new environment.  Others can be made to
  11008.  work correctly with external hardware that redefines the signals on
  11009.  A{31-20}.
  11010.  
  11011.  
  11012.  14.8.3  Initial Values of General Registers
  11013.  
  11014.  On the 80386, certain general registers may contain different values after
  11015.  RESET than on the 80286. This should not cause compatibility problems,
  11016.  because the content of 8086 registers after RESET is undefined.  If
  11017.  self-test is requested during the reset sequence and errors are detected in
  11018.  the 80386 unit, EAX will contain a nonzero value. EDX contains the component
  11019.  and revision identifier. Refer to Chapter 10 for more information.
  11020.  
  11021.  
  11022.  14.8.4  MSW Initialization
  11023.  
  11024.  The 80286 initializes the MSW register to FFF0H, but the 80386 initializes
  11025.  this register to 0000H. This difference should have no effect, because the
  11026.  bits that are different are undefined on the 80286.  Programs that read the
  11027.  value of the MSW will behave differently on the 80386 only if they depend on
  11028.  the setting of the undefined, high-order bits.
  11029.  
  11030.  
  11031.  Chapter 15  Virtual 8086 Mode
  11032.  
  11033.  ────────────────────────────────────────────────────────────────────────────
  11034.  
  11035.  The 80386 supports execution of one or more 8086, 8088, 80186, or 80188
  11036.  programs in an 80386 protected-mode environment. An 8086 program runs in
  11037.  this environment as part of a V86 (virtual 8086) task. V86 tasks take
  11038.  advantage of the hardware support of multitasking offered by the protected
  11039.  mode. Not only can there be multiple V86 tasks, each one executing an 8086
  11040.  program, but V86 tasks can be multiprogrammed with other 80386 tasks.
  11041.  
  11042.  The purpose of a V86 task is to form a "virtual machine" with which to
  11043.  execute an 8086 program. A complete virtual machine consists not only of
  11044.  80386 hardware but also of systems software. Thus, the emulation of an 8086
  11045.  is the result of cooperation between hardware and software:
  11046.  
  11047.    ■  The hardware provides a virtual set of registers (via the TSS), a
  11048.       virtual memory space (the first megabyte of the linear address space of
  11049.       the task), and directly executes all instructions that deal with these
  11050.       registers and with this address space.
  11051.  
  11052.    ■  The software controls the external interfaces of the virtual machine
  11053.       (I/O, interrupts, and exceptions) in a manner consistent with the
  11054.       larger environment in which it executes. In the case of I/O, software
  11055.       can choose either to emulate I/O instructions or to let the hardware
  11056.       execute them directly without software intervention.
  11057.  
  11058.  Software that helps implement virtual 8086 machines is called a V86
  11059.  monitor.
  11060.  
  11061.  
  11062.  15.1  Executing 8086 Code
  11063.  
  11064.  The processor executes in V86 mode when the VM (virtual machine) bit in the
  11065.  EFLAGS register is set. The processor tests this flag under two general
  11066.  conditions:
  11067.  
  11068.    1.  When loading segment registers to know whether to use 8086-style
  11069.        address formation.
  11070.  
  11071.    2.  When decoding instructions to determine which instructions are
  11072.        sensitive to IOPL.
  11073.  
  11074.  Except for these two modifications to its normal operations, the 80386 in
  11075.  V86 mode operated much as in protected mode.
  11076.  
  11077.  
  11078.  15.1.1  Registers and Instructions
  11079.  
  11080.  The register set available in V86 mode includes all the registers defined
  11081.  for the 8086 plus the new registers introduced by the 80386: FS, GS, debug
  11082.  registers, control registers, and test registers. New instructions that
  11083.  explicitly operate on the segment registers FS and GS are available, and the
  11084.  new segment-override prefixes can be used to cause instructions to utilize
  11085.  FS and GS for address calculations. Instructions can utilize 32-bit
  11086.  operands through the use of the operand size prefix.
  11087.  
  11088.  8086 programs running as V86 tasks are able to take advantage of the new
  11089.  applications-oriented instructions added to the architecture by the
  11090.  introduction of the 80186/80188, 80286 and 80386:
  11091.  
  11092.    ■  New instructions introduced by 80186/80188 and 80286.
  11093.       ── PUSH immediate data
  11094.       ── Push all and pop all (PUSHA and POPA)
  11095.       ── Multiply immediate data
  11096.       ── Shift and rotate by immediate count
  11097.       ── String I/O
  11098.       ── ENTER and LEAVE
  11099.       ── BOUND
  11100.  
  11101.    ■  New instructions introduced by 80386.
  11102.       ── LSS, LFS, LGS instructions
  11103.       ── Long-displacement conditional jumps
  11104.       ── Single-bit instructions
  11105.       ── Bit scan
  11106.       ── Double-shift instructions
  11107.       ── Byte set on condition
  11108.       ── Move with sign/zero extension
  11109.       ── Generalized multiply
  11110.  
  11111.  
  11112.  15.1.2  Linear Address Formation
  11113.  
  11114.  In V86 mode, the 80386 processor does not interpret 8086 selectors by
  11115.  referring to descriptors; instead, it forms linear addresses as an 8086
  11116.  would. It shifts the selector left by four bits to form a 20-bit base
  11117.  address. The effective address is extended with four high-order zeros and
  11118.  added to the base address to create a linear address as Figure 15-1
  11119.  illustrates.
  11120.  
  11121.  Because of the possibility of a carry, the resulting linear address may
  11122.  contain up to 21 significant bits. An 8086 program may generate linear
  11123.  addresses anywhere in the range 0 to 10FFEFH (one megabyte plus
  11124.  approximately 64 Kbytes) of the task's linear address space.
  11125.  
  11126.  V86 tasks generate 32-bit linear addresses. While an 8086 program can only
  11127.  utilize the low-order 21 bits of a linear address, the linear address can be
  11128.  mapped via page tables to any 32-bit physical address.
  11129.  
  11130.  Unlike the 8086 and 80286, 32-bit effective addresses can be generated (via
  11131.  the address-size prefix); however, the value of a 32-bit address may not
  11132.  exceed 65,535 without causing an exception. For full compatibility with
  11133.  80286 real-address mode, pseudo-protection faults (interrupt 12 or 13 with
  11134.  no error code) occur if an address is generated outside the range 0 through
  11135.  65,535.
  11136.  
  11137.  
  11138.  Figure 15-1.  V86 Mode Address Formation
  11139.  
  11140.                        19                                3       0
  11141.                       ╔═════════════════════════════════╪═════════╗
  11142.           BASE        ║     16-BIT SEGMENT SELECTOR     │ 0 0 0 0 ║
  11143.                       ╚═════════════════════════════════╪═════════╝
  11144.  
  11145.           +
  11146.                        19        15                              0
  11147.                       ╔═════════╪═════════════════════════════════╗
  11148.           OFFSET      ║ 0 0 0 0 │    16-BIT EFFECTIVE ADDRESS     ║
  11149.                       ╚═════════╪═════════════════════════════════╝
  11150.  
  11151.           =
  11152.                      20                                          0
  11153.           LINEAR    ╔═════════════════════════════════════════════╗
  11154.           ADDRESS   ║ X X X X X X X X X X X X X X X X X X X X X X ║
  11155.                     ╚═════════════════════════════════════════════╝
  11156.  
  11157.  
  11158.  15.2  Structure of a V86 Task
  11159.  
  11160.  A V86 task consists partly of the 8086 program to be executed and partly of
  11161.  80386 "native mode" code that serves as the virtual-machine monitor. The
  11162.  task must be represented by an 80386 TSS (not an 80286 TSS). The processor
  11163.  enters V86 mode to execute the 8086 program and returns to protected mode to
  11164.  execute the monitor or other 80386 tasks.
  11165.  
  11166.  To run successfully in V86 mode, an existing 8086 program needs the
  11167.  following:
  11168.  
  11169.    ■  A V86 monitor.
  11170.    ■  Operating-system services.
  11171.  
  11172.  The V86 monitor is 80386 protected-mode code that executes at
  11173.  privilege-level zero. The monitor consists primarily of initialization and
  11174.  exception-handling procedures. As for any other 80386 program,
  11175.  executable-segment descriptors for the monitor must exist in the GDT or in
  11176.  the task's LDT. The linear addresses above 10FFEFH are available for the
  11177.  V86 monitor, the operating system, and other systems software. The monitor
  11178.  may also need data-segment descriptors so that it can examine the interrupt
  11179.  vector table or other parts of the 8086 program in the first megabyte of the
  11180.  address space.
  11181.  
  11182.  In general, there are two options for implementing the 8086 operating
  11183.  system:
  11184.  
  11185.    1.  The 8086 operating system may run as part of the 8086 code. This
  11186.        approach is desirable for any of the following reasons:
  11187.  
  11188.        ■  The 8086 applications code modifies the operating system.
  11189.  
  11190.        ■  There is not sufficient development time to reimplement the 8086
  11191.           operating system as 80386 code.
  11192.  
  11193.    2.  The 8086 operating system may be implemented or emulated in the V86
  11194.        monitor. This approach is desirable for any of the following reasons:
  11195.  
  11196.        ■  Operating system functions can be more easily coordinated among
  11197.           several V86 tasks.
  11198.  
  11199.        ■  The functions of the 8086 operating system can be easily emulated
  11200.           by calls to the 80386 operating system.
  11201.  
  11202.  Note that, regardless of the approach chosen for implementing the 8086
  11203.  operating system, different V86 tasks may use different 8086 operating
  11204.  systems.
  11205.  
  11206.  
  11207.  15.2.1  Using Paging for V86 Tasks
  11208.  
  11209.  Paging is not necessary for a single V86 task, but paging is useful or
  11210.  necessary for any of the following reasons:
  11211.  
  11212.    ■  To create multiple V86 tasks. Each task must map the lower megabyte of
  11213.       linear addresses to different physical locations.
  11214.  
  11215.    ■  To emulate the megabyte wrap. On members of the 8086 family, it is
  11216.       possible to specify addresses larger than one megabyte. For example,
  11217.       with a selector value of 0FFFFH and an offset of 0FFFFH, the effective
  11218.       address would be 10FFEFH (one megabyte + 65519). The 8086, which can
  11219.       form addresses only up to 20 bits long, truncates the high-order bit,
  11220.       thereby "wrapping" this address to 0FFEFH. The 80386, however, which
  11221.       can form addresses up to 32 bits long does not truncate such an
  11222.       address. If any 8086 programs depend on this addressing anomaly, the
  11223.       same effect can be achieved in a V86 task by mapping linear addresses
  11224.       between 100000H and 110000H and linear addresses between 0 and 10000H
  11225.       to the same physical addresses.
  11226.  
  11227.    ■  To create a virtual address space larger than the physical address
  11228.       space.
  11229.  
  11230.    ■  To share 8086 OS code or ROM code that is common to several 8086
  11231.       programs that are executing simultaneously.
  11232.  
  11233.    ■  To redirect or trap references to memory-mapped I/O devices.
  11234.  
  11235.  
  11236.  15.2.2  Protection within a V86 Task
  11237.  
  11238.  Because it does not refer to descriptors while executing 8086 programs, the
  11239.  processor also does not utilize the protection mechanisms offered by
  11240.  descriptors. To protect the systems software that runs in a V86 task from
  11241.  the 8086 program, software designers may follow either of these approaches:
  11242.  
  11243.    ■  Reserve the first megabyte (plus 64 kilobytes) of each task's linear
  11244.       address space for the 8086 program. An 8086 task cannot generate
  11245.       addresses outside this range.
  11246.  
  11247.    ■  Use the U/S bit of page-table entries to protect the virtual-machine
  11248.       monitor and other systems software in each virtual 8086 task's space.
  11249.       When the processor is in V86 mode, CPL is 3. Therefore, an 8086 program
  11250.       has only user privileges. If the pages of the virtual-machine monitor
  11251.       have supervisor privilege, they cannot be accessed by the 8086 program.
  11252.  
  11253.  
  11254.  15.3  Entering and Leaving V86 Mode
  11255.  
  11256.  Figure 15-2 summarizes the ways that the processor can enter and leave an
  11257.  8086 program. The processor can enter V86 by either of two means:
  11258.  
  11259.    1.  A task switch to an 80386 task loads the image of EFLAGS from the new
  11260.        TSS. The TSS of the new task must be an 80386 TSS, not an 80286 TSS,
  11261.        because the 80286 TSS does not store the high-order word of EFLAGS,
  11262.        which contains the VM flag. A value of one in the VM bit of the new
  11263.        EFLAGS indicates that the new task is executing 8086 instructions;
  11264.        therefore, while loading the segment registers from the TSS, the
  11265.        processor forms base addresses as the 8086 would.
  11266.  
  11267.    2.  An IRET from a procedure of an 80386 task loads the image of EFLAGS
  11268.        from the stack. A value of one in VM in this case indicates that the
  11269.        procedure to which control is being returned is an 8086 procedure. The
  11270.        CPL at the time the IRET is executed must be zero, else the processor
  11271.        does not change VM.
  11272.  
  11273.  The processor leaves V86 mode when an interrupt or exception occurs. There
  11274.  are two cases:
  11275.  
  11276.    1.  The interrupt or exception causes a task switch. A task switch from a
  11277.        V86 task to any other task loads EFLAGS from the TSS of the new task.
  11278.        If the new TSS is an 80386 TSS and the VM bit in the EFLAGS image is
  11279.        zero or if the new TSS is an 80286 TSS, then the processor clears the
  11280.        VM bit of EFLAGS, loads the segment registers from the new TSS using
  11281.        80386-style address formation, and begins executing the instructions
  11282.        of the new task according to 80386 protected-mode semantics.
  11283.  
  11284.    2.  The interrupt or exception vectors to a privilege-level zero
  11285.        procedure. The processor stores the current setting of EFLAGS on the
  11286.        stack, then clears the VM bit. The interrupt or exception handler,
  11287.        therefore, executes as "native" 80386 protected-mode code. If an
  11288.        interrupt or exception vectors to a conforming segment or to a
  11289.        privilege level other than three, the processor causes a
  11290.        general-protection exception; the error code is the selector of the
  11291.        executable segment to which transfer was attempted.
  11292.  
  11293.  Systems software does not manipulate the VM flag directly, but rather
  11294.  manipulates the image of the EFLAGS register that is stored on the stack or
  11295.  in the TSS. The V86 monitor sets the VM flag in the EFLAGS image on the
  11296.  stack or in the TSS when first creating a V86 task. Exception and interrupt
  11297.  handlers can examine the VM flag on the stack. If the interrupted procedure
  11298.  was executing in V86 mode, the handler may need to invoke the V86 monitor.
  11299.  
  11300.  
  11301.  Figure 15-2.  Entering and Leaving the 8086 Program
  11302.  
  11303.                              MODE TRANSITION DIAGRAM
  11304.  
  11305.                                   ╔═══════════╗
  11306.                    TASK SWITCH    ║  INITIAL  ║
  11307.                  ┌────────────────╢   ENTRY   ║
  11308.                  │   OR IRET      ╚═══════════╝
  11309.                  │
  11310.                  
  11311.          ╔══════════════╗    INTERRUPT, EXCEPTION      ╔═════════════╗
  11312.          ║ 8086 PROGRAM ╟─────────────────────────────║ V86 MONITOR ║
  11313.          ║  (V86 MODE)  ║─────────────────────────────╢ (PROTECTED  ║
  11314.          ╚═══════╤══════╝            IRET              ║    MODE)    ║
  11315.                 │                                     ╚═════╤═══════╝
  11316.                │ │                                           │  
  11317.                │ │                                           │  │
  11318.                │ │                                           │  │
  11319.                │ │TASK SWITCH ╔═══════════════════╗ TASK SWITCH │
  11320.                │ └───────────║ OTHER 80386 TASKS ║─────────┘  │
  11321.                └──────────────╢ (PROTECTED MODE)  ╟─────────────┘
  11322.                   TASK SWITCH ╚═══════════════════╝ TASK SWITCH
  11323.  
  11324.  
  11325.  15.3.1  Transitions Through Task Switches
  11326.  
  11327.  A task switch to or from a V86 task may be due to any of three causes:
  11328.  
  11329.    1.  An interrupt that vectors to a task gate.
  11330.    2.  An action of the scheduler of the 80386 operating system.
  11331.    3.  An IRET when the NT flag is set.
  11332.  
  11333.  In any of these cases, the processor changes the VM bit in EFLAGS according
  11334.  to the image of EFLAGS in the new TSS. If the new TSS is an 80286 TSS, the
  11335.  high-order word of EFLAGS is not in the TSS; the processor clears VM in this
  11336.  case. The processor updates VM prior to loading the segment registers from
  11337.  the images in the new TSS. The new setting of VM determines whether the
  11338.  processor interprets the new segment-register images as 8086 selectors or
  11339.  80386/80286 selectors.
  11340.  
  11341.  
  11342.  15.3.2  Transitions Through Trap Gates and Interrupt Gates
  11343.  
  11344.  The processor leaves V86 mode as the result of an exception or interrupt
  11345.  that vectors via a trap or interrupt gate to a privilege-level zero
  11346.  procedure. The exception or interrupt handler returns to the 8086 code by
  11347.  executing an IRET.
  11348.  
  11349.  Because it was designed for execution by an 8086 processor, an 8086 program
  11350.  in a V86 task will have an 8086-style interrupt table starting at linear
  11351.  address zero. However, the 80386 does not use this table directly. For all
  11352.  exceptions and interrupts that occur in V86 mode, the processor vectors
  11353.  through the IDT. The IDT entry for an interrupt or exception that occurs in
  11354.  a V86 task must contain either:
  11355.  
  11356.    ■  A task gate.
  11357.  
  11358.    ■  An 80386 trap gate (type 14) or an 80386 interrupt gate (type 15),
  11359.       which must point to a nonconforming, privilege-level zero, code
  11360.       segment.
  11361.  
  11362.  Interrupts and exceptions that have 80386 trap or interrupt gates in the
  11363.  IDT vector to the appropriate handler procedure at privilege-level zero. The
  11364.  contents of all the 8086 segment registers are stored on the PL 0 stack.
  11365.  Figure 15-3 shows the format of the PL 0 stack after an exception or
  11366.  interrupt that occurs while a V86 task is executing an 8086 program.
  11367.  
  11368.  After the processor stores all the 8086 segment registers on the PL 0
  11369.  stack, it loads all the segment registers with zeros before starting to
  11370.  execute the handler procedure. This permits the interrupt handler to safely
  11371.  save and restore the DS, ES, FS, and GS registers as 80386 selectors.
  11372.  Interrupt handlers that may be invoked in the context of either a regular
  11373.  task or a V86 task, can use the same prolog and epilog code for register
  11374.  saving regardless of the kind of task. Restoring zeros to these registers
  11375.  before execution of the IRET does not cause a trap in the interrupt handler.
  11376.  Interrupt procedures that expect values in the segment registers or that
  11377.  return values via segment registers have to use the register images stored
  11378.  on the PL 0 stack. Interrupt handlers that need to know whether the
  11379.  interrupt occurred in V86 mode can examine the VM bit in the stored EFLAGS
  11380.  image.
  11381.  
  11382.  An interrupt handler passes control to the V86 monitor if the VM bit is set
  11383.  in the EFLAGS image stored on the stack and the interrupt or exception is
  11384.  one that the monitor needs to handle. The V86 monitor may either:
  11385.  
  11386.    ■  Handle the interrupt completely within the V86 monitor.
  11387.    ■  Invoke the 8086 program's interrupt handler.
  11388.  
  11389.  Reflecting an interrupt or exception back to the 8086 code involves the
  11390.  following steps:
  11391.  
  11392.    1.  Refer to the 8086 interrupt vector to locate the appropriate handler
  11393.        procedure.
  11394.  
  11395.    2.  Store the state of the 8086 program on the privilege-level three
  11396.        stack.
  11397.  
  11398.    3.  Change the return link on the privilege-level zero stack to point to
  11399.        the privilege-level three handler procedure.
  11400.  
  11401.    4.  Execute an IRET so as to pass control to the handler.
  11402.  
  11403.    5.  When the IRET by the privilege-level three handler again traps to the
  11404.        V86 monitor, restore the return link on the privilege-level zero stack
  11405.        to point to the originally interrupted, privilege-level three
  11406.        procedure.
  11407.  
  11408.    6.  Execute an IRET so as to pass control back to the interrupted
  11409.        procedure.
  11410.  
  11411.  
  11412.  Figure 15-3. PL 0 Stack after Interrupt in V86 Task
  11413.  
  11414.  
  11415.                  WITHOUT ERROR CODE            WITH ERROR CODE
  11416.                   31            0               31            0
  11417.                  ╔══════╦═══════╗────┐        ╔══════╦═══════╗────┐
  11418.                  ║▒▒▒▒▒▒║OLD GS ║     │        ║▒▒▒▒▒▒║OLD GS ║     │
  11419.                  ╠══════╬═══════╣   SS:ESP     ╠══════╬═══════╣   SS:ESP
  11420.        D  O      ║▒▒▒▒▒▒║OLD FS ║  FROM TSS    ║▒▒▒▒▒▒║OLD FS ║  FROM TSS
  11421.        I  F      ╠══════╬═══════╣              ╠══════╬═══════╣
  11422.        R         ║▒▒▒▒▒▒║OLD DS ║              ║▒▒▒▒▒▒║OLD DS ║
  11423.        E  E      ╠══════╬═══════╣              ╠══════╬═══════╣
  11424.        C  X      ║▒▒▒▒▒▒║OLD ES ║              ║▒▒▒▒▒▒║OLD ES ║
  11425.        T  P      ╠══════╬═══════╣              ╠══════╬═══════╣
  11426.        I  A      ║▒▒▒▒▒▒║OLD SS ║              ║▒▒▒▒▒▒║OLD SS ║
  11427.        O  N      ╠══════╩═══════╣              ╠══════╩═══════╣
  11428.        N  S      ║    OLD ESP   ║              ║    OLD ESP   ║
  11429.           I      ╠══════════════╣              ╠══════════════╣
  11430.         │ O      ║  OLD EFLAGS  ║              ║  OLD EFLAGS  ║
  11431.         │ N      ╠══════╦═══════╣              ╠══════╦═══════╣
  11432.         │        ║▒▒▒▒▒▒║OLD CS ║   NEW        ║▒▒▒▒▒▒║OLD CS ║
  11433.                 ╠══════╩═══════╣  SS:EIP      ╠══════╩═══════╣
  11434.                  ║    OLD EIP   ║    │         ║    OLD EIP   ║   NEW
  11435.                  ╠══════════════╣───┘         ╠══════════════╣  SS:EIP
  11436.                  ║              ║              ║  ERROR CODE  ║    │
  11437.                                              ╠══════════════╣───┘
  11438.                                              ║              ║
  11439.                                                            
  11440.  
  11441.  
  11442.  15.4  Additional Sensitive Instructions
  11443.  
  11444.  When the 80386 is executing in V86 mode, the instructions PUSHF, POPF,
  11445.  INT n, and IRET are sensitive to IOPL. The instructions IN, INS, OUT, and
  11446.  OUTS, which are ordinarily sensitive in protected mode, are not sensitive
  11447.  in V86 mode. Following is a complete list of instructions that are sensitive
  11448.  in V86 mode:
  11449.  
  11450.     CLI     ── Clear Interrupt-Enable Flag
  11451.     STI     ── Set Interrupt-Enable Flag
  11452.     LOCK    ── Assert Bus-Lock Signal
  11453.     PUSHF   ── Push Flags
  11454.     POPF    ── Pop Flags
  11455.     INT n   ── Software Interrupt
  11456.     RET     ── Interrupt Return
  11457.  
  11458.  CPL is always three in V86 mode; therefore, if IOPL < 3, these instructions
  11459.  will trigger a general-protection exceptions. These instructions are made
  11460.  sensitive so that their functions can be simulated by the V86 monitor.
  11461.  
  11462.  
  11463.  15.4.1  Emulating 8086 Operating System Calls
  11464.  
  11465.  INT n is sensitive so that the V86 monitor can intercept calls to the
  11466.  8086 OS. Many 8086 operating systems are called by pushing parameters onto
  11467.  the stack, then executing an INT n instruction. If IOPL < 3, INT n
  11468.  instructions will be intercepted by the V86 monitor. The V86 monitor can
  11469.  then emulate the function of the 8086 operating system or reflect the
  11470.  interrupt back to the 8086 operating system in V86 mode.
  11471.  
  11472.  
  11473.  15.4.2  Virtualizing the Interrupt-Enable Flag
  11474.  
  11475.  When the processor is executing 8086 code in a V86 task, the instructions
  11476.  PUSHF, POPF, and IRET are sensitive to IOPL so that the V86 monitor can
  11477.  control changes to the interrupt-enable flag (IF). Other instructions that
  11478.  affect IF (STI and CLI) are IOPL sensitive both in 8086 code and in
  11479.  80386/80386 code.
  11480.  
  11481.  Many 8086 programs that were designed to execute on single-task systems set
  11482.  and clear IF to control interrupts. However, when these same programs are
  11483.  executed in a multitasking environment, such control of IF can be
  11484.  disruptive. If IOPL is less than three, all instructions that change or
  11485.  interrogate IF will trap to the V86 monitor. The V86 monitor can then
  11486.  control IF in a manner that both suits the needs of the larger environment
  11487.  and is transparent to the 8086 program.
  11488.  
  11489.  
  11490.  15.5  Virtual I/O
  11491.  
  11492.  Many 8086 programs that were designed to execute on single-task systems use
  11493.  I/O devices directly. However, when these same programs are executed in a
  11494.  multitasking environment, such use of devices can be disruptive. The 80386
  11495.  provides sufficient flexibility to control I/O in a manner that both suits
  11496.  the needs of the new environment and is transparent to the 8086 program.
  11497.  Designers may take any of several possible approaches to controlling I/O:
  11498.  
  11499.    ■  Implement or emulate the 8086 operating system as an 80386 program and
  11500.       require the 8086 application to do I/O via software interrupts to the
  11501.       operating system, trapping all attempts to do I/O directly.
  11502.  
  11503.    ■  Let the 8086 program take complete control of all I/O.
  11504.  
  11505.    ■  Selectively trap and emulate references that a task makes to specific
  11506.       I/O ports.
  11507.  
  11508.    ■  Trap or redirect references to memory-mapped I/O addresses.
  11509.  
  11510.  The method of controlling I/O depends upon whether I/O ports are I/O mapped
  11511.  or memory mapped.
  11512.  
  11513.  
  11514.  15.5.1  I/O-Mapped I/O
  11515.  
  11516.  I/O-mapped I/O in V86 mode differs from protected mode only in that the
  11517.  protection mechanism does not consult IOPL when executing the I/O
  11518.  instructions IN, INS, OUT, OUTS. Only the I/O permission bit map controls
  11519.  the right for V86 tasks to execute these I/O instructions.
  11520.  
  11521.  The I/O permission map traps I/O instructions selectively depending on the
  11522.  I/O addresses to which they refer. The I/O permission bit map of each V86
  11523.  task determines which I/O addresses are trapped for that task. Because each
  11524.  task may have a different I/O permission bit map, the addresses trapped for
  11525.  one task may be different from those trapped for others. Refer to Chapter 8
  11526.  for more information about the I/O permission map.
  11527.  
  11528.  
  11529.  15.5.2  Memory-Mapped I/O
  11530.  
  11531.  In hardware designs that utilize memory-mapped I/O, the paging facilities
  11532.  of the 80386 can be used to trap or redirect I/O operations. Each task that
  11533.  executes memory-mapped I/O must have a page (or pages) for the memory-mapped
  11534.  address space. The V86 monitor may control memory-mapped I/O by any of
  11535.  these means:
  11536.  
  11537.    ■  Assign the memory-mapped page to appropriate physical addresses.
  11538.       Different tasks may have different physical addresses, thereby
  11539.       preventing the tasks from interfering with each other.
  11540.  
  11541.    ■  Cause a trap to the monitor by forcing a page fault on the
  11542.       memory-mapped page. Read-only pages trap writes. Not-present pages trap
  11543.       both reads and writes.
  11544.  
  11545.  Intervention for every I/O might be excessive for some kinds of I/O
  11546.  devices. A page fault can still be used in this case to cause intervention
  11547.  on the first I/O operation. The monitor can then at least make sure that the
  11548.  task has exclusive access to the device. Then the monitor can change the
  11549.  page status to present and read/write, allowing subsequent I/O to proceed at
  11550.  full speed.
  11551.  
  11552.  
  11553.  15.5.3  Special I/O Buffers
  11554.  
  11555.  Buffers of intelligent controllers (for example, a bit-mapped graphics
  11556.  buffer) can also be virtualized via page mapping. The linear space for the
  11557.  buffer can be mapped to a different physical space for each virtual 8086
  11558.  task. The V86 monitor can then assume responsibility for spooling the data
  11559.  or assigning the virtual buffer to the real buffer at appropriate times.
  11560.  
  11561.  
  11562.  15.6  Differences From 8086
  11563.  
  11564.  In general, V86 mode will correctly execute software designed for the 8086,
  11565.  8088, 80186, and 80188. Following is a list of the minor differences between
  11566.  8086 execution on the 80386 and on an 8086.
  11567.  
  11568.    1.  Instruction clock counts.
  11569.  
  11570.        The 80386 takes fewer clocks for most instructions than the
  11571.        8086/8088. The areas most likely to be affected are:
  11572.  
  11573.        ■  Delays required by I/O devices between I/O operations.
  11574.  
  11575.        ■  Assumed delays with 8086/8088 operating in parallel with an 8087.
  11576.  
  11577.    2.  Divide exceptions point to the DIV instruction.
  11578.  
  11579.        Divide exceptions on the 80386 always leave the saved CS:IP value
  11580.        pointing to the instruction that failed. On the 8086/8088, the CS:IP
  11581.        value points to the next instruction.
  11582.  
  11583.    3.  Undefined 8086/8088 opcodes.
  11584.  
  11585.        Opcodes that were not defined for the 8086/8088 will cause exception
  11586.        6 or will execute one of the new instructions defined for the 80386.
  11587.  
  11588.    4.  Value written by PUSH SP.
  11589.  
  11590.        The 80386 pushes a different value on the stack for PUSH SP than the
  11591.        8086/8088. The 80386 pushes the value of SP before SP is incremented
  11592.        as part of the push operation; the 8086/8088 pushes the value of SP
  11593.        after it is incremented. If the value pushed is important, replace
  11594.        PUSH SP instructions with the following three instructions:
  11595.  
  11596.        PUSH  BP
  11597.        MOV   BP, SP
  11598.        XCHG  BP, [BP]
  11599.  
  11600.        This code functions as the 8086/8088 PUSH SP instruction on the
  11601.        80386.
  11602.  
  11603.    5.  Shift or rotate by more than 31 bits.
  11604.  
  11605.        The 80386 masks all shift and rotate counts to the low-order five
  11606.        bits. This MOD 32 operation limits the count to a maximum of 31 bits,
  11607.        thereby limiting the time that interrupt response is delayed while
  11608.        the instruction is executing.
  11609.  
  11610.    6.  Redundant prefixes.
  11611.  
  11612.        The 80386 sets a limit of 15 bytes on instruction length. The only
  11613.        way to violate this limit is by putting redundant prefixes before an
  11614.        instruction. Exception 13 occurs if the limit on instruction length
  11615.        is violated. The 8086/8088 has no instruction length limit.
  11616.  
  11617.    7.  Operand crossing offset 0 or 65,535.
  11618.  
  11619.        On the 8086, an attempt to access a memory operand that crosses
  11620.        offset 65,535 (e.g., MOV a word to offset 65,535) or offset 0 (e.g.,
  11621.        PUSH a word when SP = 1) causes the offset to wrap around modulo
  11622.        65,536. The 80386 raises an exception in these cases──exception 13 if
  11623.        the segment is a data segment (i.e., if CS, DS, ES, FS, or GS is
  11624.        being used to address the segment), exception 12 if the segment is a
  11625.        stack segment (i.e., if SS is being used).
  11626.  
  11627.    8.  Sequential execution across offset 65,535.
  11628.  
  11629.        On the 8086, if sequential execution of instructions proceeds past
  11630.        offset 65,535, the processor fetches the next instruction byte from
  11631.        offset 0 of the same segment. On the 80386, the processor raises
  11632.        exception 13 in such a case.
  11633.  
  11634.    9.  LOCK is restricted to certain instructions.
  11635.  
  11636.        The LOCK prefix and its corresponding output signal should only be
  11637.        used to prevent other bus masters from interrupting a data movement
  11638.        operation. The 80386 always asserts the LOCK signal during an XCHG
  11639.        instruction with memory (even if the LOCK prefix is not used). LOCK
  11640.        may only be used with the following 80386 instructions when they
  11641.        update memory: BTS, BTR, BTC, XCHG, ADD, ADC, SUB, SBB, INC, DEC,
  11642.        AND, OR, XOR, NOT, and NEG. An undefined-opcode exception (interrupt
  11643.        6) results from using LOCK before any other instruction.
  11644.  
  11645.   10.  Single-stepping external interrupt handlers.
  11646.  
  11647.        The priority of the 80386 single-step exception is different from
  11648.        that of the 8086/8088. The change prevents an external interrupt
  11649.        handler from being single-stepped if the interrupt occurs while a
  11650.        program is being single-stepped. The 80386 single-step exception has
  11651.        higher priority that any external interrupt. The 80386 will still
  11652.        single-step through an interrupt handler invoked by the INT
  11653.        instructions or by an exception.
  11654.  
  11655.    11.  IDIV exceptions for quotients of 80H or 8000H.
  11656.  
  11657.        The 80386 can generate the largest negative number as a quotient for
  11658.        the IDIV instruction. The 8086/8088 causes exception zero instead.
  11659.  
  11660.   12.  Flags in stack.
  11661.  
  11662.        The setting of the flags stored by PUSHF, by interrupts, and by
  11663.        exceptions is different from that stored by the 8086 in bit positions
  11664.        12 through 15. On the 8086 these bits are stored as ones, but in V86
  11665.        mode bit 15 is always zero, and bits 14 through 12 reflect the last
  11666.        value loaded into them.
  11667.  
  11668.   13.  NMI interrupting NMI handlers.
  11669.  
  11670.        After an NMI is recognized on the 80386, the NMI interrupt is masked
  11671.        until an IRET instruction is executed.
  11672.  
  11673.   14.  Coprocessor errors vector to interrupt 16.
  11674.  
  11675.        Any 80386 system with a coprocessor must use interrupt vector 16 for
  11676.        the coprocessor error exception. If an 8086/8088 system uses another
  11677.        vector for the 8087 interrupt, both vectors should point to the
  11678.        coprocessor-error exception handler.
  11679.  
  11680.   15.  Numeric exception handlers should allow prefixes.
  11681.  
  11682.        On the 80386, the value of CS:IP saved for coprocessor exceptions
  11683.        points at any prefixes before an ESC instruction. On 8086/8088
  11684.        systems, the saved CS:IP points to the ESC instruction itself.
  11685.  
  11686.   16.  Coprocessor does not use interrupt controller.
  11687.  
  11688.        The coprocessor error signal to the 80386 does not pass through an
  11689.        interrupt controller (an 8087 INT signal does). Some instructions in
  11690.        a coprocessor error handler may need to be deleted if they deal with
  11691.        the interrupt controller.
  11692.  
  11693.  
  11694.  15.7  Differences From 80286 Real-Address Mode
  11695.  
  11696.  The 80286 processor implements the bus lock function differently than the
  11697.  80386. This fact may or may not be apparent to 8086 programs, depending on
  11698.  how the V86 monitor handles the LOCK prefix. LOCKed instructions are
  11699.  sensitive to IOPL; therefore, software designers can choose to emulate its
  11700.  function. If, however, 8086 programs are allowed to execute LOCK directly,
  11701.  programs that use forms of memory locking specific to the 8086 may not
  11702.  execute properly when transported to a specific application of the 80386.
  11703.  
  11704.  The LOCK prefix and its corresponding output signal should only be used to
  11705.  prevent other bus masters from interrupting a data movement operation. LOCK
  11706.  may only be used with the following 80386 instructions when they modify
  11707.  memory. An undefined-opcode exception results from using LOCK before any
  11708.  other instruction.
  11709.  
  11710.    ■  Bit test and change: BTS, BTR, BTC.
  11711.    ■  Exchange: XCHG.
  11712.    ■  One-operand arithmetic and logical: INC, DEC, NOT, and NEG.
  11713.    ■  Two-operand arithmetic and logical: ADD, ADC, SUB, SBB, AND, OR, XOR.
  11714.  
  11715.  A locked instruction is guaranteed to lock only the area of memory defined
  11716.  by the destination operand, but may lock a larger memory area. For example,
  11717.  typical 8086 and 80286 configurations lock the entire physical memory space.
  11718.  With the 80386, the defined area of memory is guaranteed to be locked
  11719.  against access by a processor executing a locked instruction on exactly the
  11720.  same memory area, i.e., an operand with identical starting address and
  11721.  identical length.
  11722.  
  11723.  
  11724.  Chapter 16  Mixing 16-Bit and 32 Bit Code
  11725.  
  11726.  ────────────────────────────────────────────────────────────────────────────
  11727.  
  11728.  The 80386 running in protected mode is a 32-bit microprocessor, but it is
  11729.  designed to support 16-bit processing at three levels:
  11730.  
  11731.    1.  Executing 8086/80286 16-bit programs efficiently with complete
  11732.        compatibility.
  11733.  
  11734.    2.  Mixing 16-bit modules with 32-bit modules.
  11735.  
  11736.    3.  Mixing 16-bit and 32-bit addresses and operands within one module.
  11737.  
  11738.  The first level of support for 16-bit programs has already been discussed
  11739.  in Chapter 13, Chapter 14, and Chapter 15. This chapter shows how 16-bit
  11740.  and 32-bit modules can cooperate with one another, and how one module can
  11741.  utilize both 16-bit and 32-bit operands and addressing.
  11742.  
  11743.  The 80386 functions most efficiently when it is possible to distinguish
  11744.  between pure 16-bit modules and pure 32-bit modules. A pure 16-bit module
  11745.  has these characteristics:
  11746.  
  11747.    ■  All segments occupy 64 Kilobytes or less.
  11748.    ■  Data items are either 8 bits or 16 bits wide.
  11749.    ■  Pointers to code and data have 16-bit offsets.
  11750.    ■  Control is transferred only among 16-bit segments.
  11751.  
  11752.  A pure 32-bit module has these characteristics:
  11753.  
  11754.    ■  Segments may occupy more than 64 Kilobytes (zero bytes to 4
  11755.       gigabytes).
  11756.  
  11757.    ■  Data items are either 8 bits or 32 bits wide.
  11758.  
  11759.    ■  Pointers to code and data have 32-bit offsets.
  11760.  
  11761.    ■  Control is transferred only among 32-bit segments.
  11762.  
  11763.  Pure 16-bit modules do exist; they are the modules designed for 16-bit
  11764.  microprocessors. Pure 32-bit modules may exist in new programs designed
  11765.  explicitly for the 80386. However, as systems designers move applications
  11766.  from 16-bit processors to the 32-bit 80386, it will not always be possible
  11767.  to maintain these ideals of pure 16-bit or 32-bit modules. It may be
  11768.  expedient to execute old 16-bit modules in a new 32-bit environment without
  11769.  making source-code changes to the old modules if any of the following
  11770.  conditions is true:
  11771.  
  11772.    ■  Modules will be converted one-by-one from 16-bit environments to
  11773.       32-bit environments.
  11774.  
  11775.    ■  Older, 16-bit compilers and software-development tools will be
  11776.       utilized in the new32-bit operating environment until new 32-bit
  11777.       versions can be created.
  11778.  
  11779.    ■  The source code of 16-bit modules is not available for modification.
  11780.  
  11781.    ■  The specific data structures used by a given module inherently utilize
  11782.       16-bit words.
  11783.  
  11784.    ■  The native word size of the source language is 16 bits.
  11785.  
  11786.  On the 80386, 16-bit modules can be mixed with 32-bit modules. To design a
  11787.  system that mixes 16- and 32-bit code requires an understanding of the
  11788.  mechanisms that the 80386 uses to invoke and control its 32-bit and 16-bit
  11789.  features.
  11790.  
  11791.  
  11792.  16.1  How the 80386 Implements 16-Bit and 32-Bit Features
  11793.  
  11794.  The features of the architecture that permit the 80386 to work equally well
  11795.  with 32-bit and 16-bit address and operand sizes include:
  11796.  
  11797.    ■  The D-bit (default bit) of code-segment descriptors, which determines
  11798.       the default choice of operand-size and address-size for the
  11799.       instructions of a code segment. (In real-address mode and V86 mode,
  11800.       which do not use descriptors, the default is 16 bits.) A code segment
  11801.       whose D-bit is set is known as a USE32 segment; a code segment whose
  11802.       D-bit is zero is a USE16 segment. The D-bit eliminates the need to
  11803.       encode the operand size and address size in instructions when all
  11804.       instructions use operands and effective addresses of the same size.
  11805.  
  11806.    ■  Instruction prefixes that explicitly override the default choice of
  11807.       operand size and address size (available in protected mode as well as
  11808.       in real-address mode and V86 mode).
  11809.  
  11810.    ■  Separate 32-bit and 16-bit gates for intersegment control transfers
  11811.       (including call gates, interrupt gates, and trap gates). The operand
  11812.       size for the control transfer is determined by the type of gate, not by
  11813.       the D-bit or prefix of the transfer instruction.
  11814.  
  11815.    ■  Registers that can be used both for 32-bit and 16-bit operands and
  11816.       effective-address calculations.
  11817.  
  11818.    ■  The B-bit (big bit) of data-segment descriptors, which determines the
  11819.       size of stack pointer (32-bit ESP or 16-bit SP) used by the CPU for
  11820.       implicit stack references.
  11821.  
  11822.  
  11823.  16.2  Mixing 32-Bit and 16-Bit Operations
  11824.  
  11825.  The 80386 has two instruction prefixes that allow mixing of 32-bit and
  11826.  16-bit operations within one segment:
  11827.  
  11828.    ■  The operand-size prefix (66H)
  11829.    ■  The address-size prefix (67H)
  11830.  
  11831.  These prefixes reverse the default size selected by the D-bit. For example,
  11832.  the processor can interpret the word-move instruction MOV mem, reg in any of
  11833.  four ways:
  11834.  
  11835.    ■  In a USE32 segment:
  11836.  
  11837.       1.  Normally moves 32 bits from a 32-bit register to a 32-bit
  11838.           effective address in memory.
  11839.  
  11840.       2.  If preceded by an operand-size prefix, moves 16 bits from a 16-bit
  11841.           register to 32-bit effective address in memory.
  11842.  
  11843.       3.  If preceded by an address-size prefix, moves 32 bits from a 32-bit
  11844.           register to a16-bit effective address in memory.
  11845.  
  11846.       4.  If preceded by both an address-size prefix and an operand-size
  11847.           prefix, moves 16 bits from a 16-bit register to a 16-bit effective
  11848.           address in memory.
  11849.  
  11850.    ■  In a USE16 segment:
  11851.  
  11852.       1.  Normally moves 16 bits from a 16-bit register to a 16-bit
  11853.           effective address in memory.
  11854.  
  11855.       2.  If preceded by an operand-size prefix, moves 32 bits from a 32-bit
  11856.           register to 16-bit effective address in memory.
  11857.  
  11858.       3.  If preceded by an address-size prefix, moves 16 bits from a 16-bit
  11859.           register to a32-bit effective address in memory.
  11860.  
  11861.       4.  If preceded by both an address-size prefix and an operand-size
  11862.           prefix, moves 32 bits from a 32-bit register to a 32-bit effective
  11863.           address in memory.
  11864.  
  11865.  These examples illustrate that any instruction can generate any combination
  11866.  of operand size and address size regardless of whether the instruction is in
  11867.  a USE16 or USE32 segment. The choice of the USE16 or USE32 attribute for a
  11868.  code segment is based upon these criteria:
  11869.  
  11870.    1.  The need to address instructions or data in segments that are larger
  11871.        than 64 Kilobytes.
  11872.  
  11873.    2.  The predominant size of operands.
  11874.  
  11875.    3.  The addressing modes desired. (Refer to Chapter 17 for an explanation
  11876.        of the additional addressing modes that are available when 32-bit
  11877.        addressing is used.)
  11878.  
  11879.  Choosing a setting of the D-bit that is contrary to the predominant size of
  11880.  operands requires the generation of an excessive number of operand-size
  11881.  prefixes.
  11882.  
  11883.  
  11884.  16.3  Sharing Data Segments Among Mixed Code Segments
  11885.  
  11886.  Because the choice of operand size and address size is defined in code
  11887.  segments and their descriptors, data segments can be shared freely among
  11888.  both USE16 and USE32 code segments. The only limitation is the one imposed
  11889.  by pointers with 16-bit offsets, which can only point to the first 64
  11890.  Kilobytes of a segment. When a data segment that contains more than 64
  11891.  Kilobytes is to be shared among USE32 and USE16 segments, the data that is
  11892.  to be accessed by the USE16 segments must be located within the first 64
  11893.  Kilobytes.
  11894.  
  11895.  A stack that spans addresses less than 64K can be shared by both USE16 and
  11896.  USE32 code segments. This class of stacks includes:
  11897.  
  11898.    ■  Stacks in expand-up segments with G=0 and B=0.
  11899.  
  11900.    ■  Stacks in expand-down segments with G=0 and B=0.
  11901.  
  11902.    ■  Stacks in expand-up segments with G=1 and B=0, in which the stack is
  11903.       contained completely within the lower 64 Kilobytes. (Offsets greater
  11904.       than 64K can be used for data, other than the stack, that is not
  11905.       shared.)
  11906.  
  11907.  The B-bit of a stack segment cannot, in general, be used to change the size
  11908.  of stack used by a USE16 code segment. The size of stack pointer used by the
  11909.  processor for implicit stack references is controlled by the B-bit of the
  11910.  data-segment descriptor for the stack. Implicit references are those caused
  11911.  by interrupts, exceptions, and instructions such as PUSH, POP, CALL, and
  11912.  RET. One might be tempted, therefore, to try to increase beyond 64K the
  11913.  size of the stack used by 16-bit code simply by supplying a larger stack
  11914.  segment with the B-bit set. However, the B-bit does not control explicit
  11915.  stack references, such as accesses to parameters or local variables. A USE16
  11916.  code segment can utilize a "big" stack only if the code is modified so that
  11917.  all explicit references to the stack are preceded by the address-size
  11918.  prefix, causing those references to use 32-bit addressing.
  11919.  
  11920.  In big, expand-down segments (B=1, G=1, and E=1), all offsets are greater
  11921.  than 64K, therefore USE16 code cannot utilize such a stack segment unless
  11922.  the code segment is modified to employ 32-bit addressing. (Refer to Chapter
  11923.  6 for a review of the B, G, and E bits.)
  11924.  
  11925.  
  11926.  16.4  Transferring Control Among Mixed Code Segments
  11927.  
  11928.  When transferring control among procedures in USE16 and USE32 code
  11929.  segments, programmers must be aware of three points:
  11930.  
  11931.    ■  Addressing limitations imposed by pointers with 16-bit offsets.
  11932.  
  11933.    ■  Matching of operand-size attribute in effect for the CALL/RET pair and
  11934.       theInterrupt/IRET pair so as to manage the stack correctly.
  11935.  
  11936.    ■  Translation of parameters, especially pointer parameters.
  11937.  
  11938.  Clearly, 16-bit effective addresses cannot be used to address data or code
  11939.  located beyond 64K in a 32-bit segment, nor can large 32-bit parameters be
  11940.  squeezed into a 16-bit word; however, except for these obvious limits, most
  11941.  interfacing problems between 16-bit and 32-bit modules can be solved. Some
  11942.  solutions involve inserting interface procedures between the procedures in
  11943.  question.
  11944.  
  11945.  
  11946.  16.4.1  Size of Code-Segment Pointer
  11947.  
  11948.  For control-transfer instructions that use a pointer to identify the next
  11949.  instruction (i.e., those that do not use gates), the size of the offset
  11950.  portion of the pointer is determined by the operand-size attribute. The
  11951.  implications of the use of two different sizes of code-segment pointer are:
  11952.  
  11953.    ■  JMP, CALL, or RET from 32-bit segment to 16-bit segment is always
  11954.       possible using a 32-bit operand size.
  11955.  
  11956.    ■  JMP, CALL, or RET from 16-bit segment using a 16-bit operand size
  11957.       cannot address the target in a 32-bit segment if the address of the
  11958.       target is greater than 64K.
  11959.  
  11960.  An interface procedure can enable transfers from USE16 segments to 32-bit
  11961.  addresses beyond 64K without requiring modifications any more extensive than
  11962.  relinking or rebinding the old programs. The requirements for such an
  11963.  interface procedure are discussed later in this chapter.
  11964.  
  11965.  
  11966.  16.4.2  Stack Management for Control Transfers
  11967.  
  11968.  Because stack management is different for 16-bit CALL/RET than for 32-bit
  11969.  CALL/RET, the operand size of RET must match that of CALL. (Refer to Figure
  11970.  16-1.) A 16-bit CALL pushes the 16-bit IP and (for calls between privilege
  11971.  levels) the 16-bit SP register. The corresponding RET must also use a 16-bit
  11972.  operand size to POP these 16-bit values from the stack into the 16-bit
  11973.  registers. A 32-bit CALL pushes the 32-bit EIP and (for interlevel calls)
  11974.  the 32-bit ESP register. The corresponding RET must also use a 32-bit
  11975.  operand size to POP these 32-bit values from the stack into the 32-bit
  11976.  registers. If the two halves of a CALL/RET pair do not have matching operand
  11977.  sizes, the stack will not be managed correctly and the values of the
  11978.  instruction pointer and stack pointer will not be restored to correct
  11979.  values.
  11980.  
  11981.  When the CALL and its corresponding RET are in segments that have D-bits
  11982.  with the same values (i.e., both have 32-bit defaults or both have 16-bit
  11983.  defaults), there is no problem. When the CALL and its corresponding RET are
  11984.  in segments that have different D-bit values, however, programmers (or
  11985.  program development software) must ensure that the CALL and RET match.
  11986.  
  11987.  There are three ways to cause a 16-bit procedure to execute a 32-bit call:
  11988.  
  11989.    1.  Use a 16-bit call to a 32-bit interface procedure that then uses a
  11990.        32-bit call to invoke the intended target.
  11991.  
  11992.    2.  Bind the 16-bit call to a 32-bit call gate.
  11993.  
  11994.    3.  Modify the 16-bit procedure, inserting an operand-size prefix before
  11995.        the call, thereby changing it to a 32-bit call.
  11996.  
  11997.  Likewise, there are three ways to cause a 32-bit procedure to execute a
  11998.  16-bit call:
  11999.  
  12000.    1.  Use a 32-bit call to a 32-bit interface procedure that then uses a
  12001.        16-bit call to invoke the intended target.
  12002.  
  12003.    2.  Bind the 32-bit call to a 16-bit call gate.
  12004.  
  12005.    3.  Modify the 32-bit procedure, inserting an operand-size prefix before
  12006.        the call, thereby changing it to a 16-bit call. (Be certain that the
  12007.        return offset does not exceed 64K.)
  12008.  
  12009.  Programmers can utilize any of the preceding methods to make a CALL in a
  12010.  USE16 segment match the corresponding RET in a USE32 segment, or to make a
  12011.  CALL in a USE32 segment match the corresponding RET in a USE16 segment.
  12012.  
  12013.  
  12014.  Figure 16-1.  Stack after Far 16-Bit and 32-Bit Calls
  12015.  
  12016.                             WITHOUT PRIVILEGE TRANSITION
  12017.  
  12018.                 AFTER 16-BIT CALL                AFTER 32-BIT CALL
  12019.  
  12020.                 31             0               31             0
  12021.         D  O    ║               ║                ║               ║
  12022.         I  F    ╠═══════╪═══════╣                ╠═══════╪═══════╣
  12023.         R       ║▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒║                ║▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒║
  12024.         E  E    ╠═══════╪═══════╣                ╠═══════╪═══════╣
  12025.         C  X    ║ PARM2 │ PARM1 ║                ║     PARM2     ║
  12026.         T  P    ╠═══════╪═══════╣                ╠═══════╪═══════╣
  12027.         I  A    ║  CS   │  IP   ║──SP           ║     PARM1     ║
  12028.         O  N    ╠═══════╪═══════╣                ╠═══════╪═══════╣
  12029.         N  S    ║               ║                ║▒▒▒▒▒▒▒│  CS   ║
  12030.            I    ╠═══════╪═══════╣                ╠═══════╪═══════╣
  12031.          │ O    ║               ║                ║      EIP      ║──ESP
  12032.          │ N    ╠═══════╪═══════╣                ╠═══════╪═══════╣
  12033.          │      ║               ║                ║               ║
  12034.                                                              
  12035.  
  12036.                             WITH PRIVILEGE TRANSITION
  12037.  
  12038.                 AFTER 16-BIT CALL                AFTER 32-BIT CALL
  12039.  
  12040.         D  O     31            0                  31            0
  12041.         I  F    ╔═══════╪═══════╗                ╔═══════╪═══════╗
  12042.         R       ║   SS  │  SP   ║                ║▒▒▒▒▒▒▒│  SS   ║
  12043.         E  E    ╠═══════╪═══════╣                ╠═══════╪═══════╣
  12044.         C  X    ║ PARM2 │ PARM1 ║                ║      ESP      ║
  12045.         T  P    ╠═══════╪═══════╣                ╠═══════╪═══════╣
  12046.         I  A    ║  CS   │  IP   ║──SP           ║     PARM2     ║
  12047.         O  N    ╠═══════╪═══════╣                ╠═══════╪═══════╣
  12048.         N  S    ║               ║                ║     PARM1     ║
  12049.            I    ╠═══════╪═══════╣                ╠═══════╪═══════╣
  12050.          │ O    ║               ║                ║▒▒▒▒▒▒▒│  CS   ║
  12051.          │ N    ╠═══════╪═══════╣                ╠═══════╪═══════╣
  12052.          │      ║               ║                ║      EIP      ║──ESP
  12053.                ╠═══════╪═══════╣                ╠═══════╪═══════╣
  12054.                 ║               ║                ║               ║
  12055.                                                               
  12056.  
  12057.  
  12058.  16.4.2.1  Controlling the Operand-Size for a Call
  12059.  
  12060.  When the selector of the pointer referenced by a CALL instruction selects a
  12061.  segment descriptor, the operand-size attribute in effect for the CALL
  12062.  instruction is determined by the D-bit in the segment descriptor and by any
  12063.  operand-size instruction prefix.
  12064.  
  12065.  When the selector of the pointer referenced by a CALL instruction selects a
  12066.  gate descriptor, the type of call is determined by the type of call gate. A
  12067.  call via an 80286 call gate (descriptor type 4)  always has a 16-bit
  12068.  operand-size attribute; a call via an 80386 call gate (descriptor type 12)
  12069.  always has a 32-bit operand-size attribute. The offset of the target
  12070.  procedure is taken from the gate descriptor; therefore, even a 16-bit
  12071.  procedure can call a procedure that is located more than 64 kilobytes from
  12072.  the base of a 32-bit segment, because a 32-bit call gate contains a 32-bit
  12073.  target offset.
  12074.  
  12075.  An unmodified 16-bit code segment that has run successfully on an 8086 or
  12076.  real-mode 80286 will always have a D-bit of zero and will not use
  12077.  operand-size override prefixes; therefore, it will always execute 16-bit
  12078.  versions of CALL. The only modification needed to make a16-bit procedure
  12079.  effect a 32-bit call is to relink the call to an 80386 call gate.
  12080.  
  12081.  
  12082.  16.4.2.2  Changing Size of Call
  12083.  
  12084.  When adding 32-bit gates to 16-bit procedures, it is important to consider
  12085.  the number of parameters. The count field of the gate descriptor specifies
  12086.  the size of the parameter string to copy from the current stack to the stack
  12087.  of the more privileged procedure. The count field of a 16-bit gate specifies
  12088.  the number of words to be copied, whereas the count field of a 32-bit gate
  12089.  specifies the number of doublewords to be copied; therefore, the 16-bit
  12090.  procedure must use an even number of words as parameters.
  12091.  
  12092.  
  12093.  16.4.3  Interrupt Control Transfers
  12094.  
  12095.  With a control transfer due to an interrupt or exception, a gate is always
  12096.  involved. The operand-size attribute for the interrupt is determined by the
  12097.  type of IDT gate.
  12098.  
  12099.  A 386 interrupt or trap gate (descriptor type 14 or 15) to a 32-bit
  12100.  interrupt procedure can be used to interrupt either 32-bit or 16-bit
  12101.  procedures. However, it is not generally feasible to permit an interrupt or
  12102.  exception to invoke a 16-bit handler procedure when 32-bit code is
  12103.  executing, because a 16-bit interrupt procedure has a return offset of only
  12104.  16-bits on its stack. If the 32-bit procedure is executing at an address
  12105.  greater than 64K, the 16-bit interrupt procedure cannot return correctly.
  12106.  
  12107.  
  12108.  16.4.4  Parameter Translation
  12109.  
  12110.  When segment offsets or pointers (which contain segment offsets) are passed
  12111.  as parameters between 16-bit and 32-bit procedures, some translation is
  12112.  required. Clearly, if a 32-bit procedure passes a pointer to data located
  12113.  beyond 64K to a 16-bit procedure, the 16-bit procedure cannot utilize it.
  12114.  Beyond this natural limitation, an interface procedure can perform any
  12115.  format conversion between 32-bit and 16-bit pointers that may be needed.
  12116.  
  12117.  Parameters passed by value between 32-bit and 16-bit code may also require
  12118.  translation between 32-bit and 16-bit formats. Such translation requirements
  12119.  are application dependent. Systems designers should take care to limit the
  12120.  range of values passed so that such translations are possible.
  12121.  
  12122.  
  12123.  16.4.5  The Interface Procedure
  12124.  
  12125.  Interposing an interface procedure between 32-bit and 16-bit procedures can
  12126.  be the solution to any of several interface requirements:
  12127.  
  12128.    ■  Allowing procedures in 16-bit segments to transfer control to
  12129.       instructions located beyond 64K in 32-bit segments.
  12130.  
  12131.    ■  Matching of operand size for CALL/RET.
  12132.  
  12133.    ■  Parameter translation.
  12134.  
  12135.  Interface procedures between USE32 and USE16 segments can be constructed
  12136.  with these properties:
  12137.  
  12138.    ■  The procedures reside in a code segment whose D-bit is set, indicating
  12139.       a default operand size of 32-bits.
  12140.  
  12141.    ■  All entry points that may be called by 16-bit procedures have offsets
  12142.       that are actually less than 64K.
  12143.  
  12144.    ■  All points to which called 16-bit procedures may return also lie
  12145.       within 64K.
  12146.  
  12147.  The interface procedures do little more than call corresponding procedures
  12148.  in other segments. There may be two kinds of procedures:
  12149.  
  12150.    ■  Those that are called by 16-bit procedures and call 32-bit procedures.
  12151.       These interface procedures are called by 16-bit CALLs and use the
  12152.       operand-size prefix before RET instructions to cause a 16-bit RET.
  12153.       CALLs to 32-bit segments are 32-bit calls (by default, because the
  12154.       D-bit is set), and the 32-bit code returns with 32-bit RET
  12155.       instructions.
  12156.  
  12157.    ■  Those that are called by 32-bit procedures and call 16-bit procedures.
  12158.       These interface procedures are called by 32-bit CALL instructions, and
  12159.       return with 32-bit RET instructions (by default, because the D-bit is
  12160.       set).  CALLs to 16-bit procedures use the operand-size prefix;
  12161.       procedures in the 16-bit code return with 16-bit RET instructions.
  12162.  
  12163.  
  12164.                           PART IV  INSTRUCTION SET
  12165.  
  12166.  
  12167.  Chapter 17  80386 Instruction Set
  12168.  
  12169.  ────────────────────────────────────────────────────────────────────────────
  12170.  
  12171.  This chapter presents instructions for the 80386 in alphabetical order. For
  12172.  each instruction, the forms are given for each operand combination,
  12173.  including object code produced, operands required, execution time, and a
  12174.  description. For each instruction, there is an operational description and a
  12175.  summary of exceptions generated.
  12176.  
  12177.  
  12178.  17.1  Operand-Size and Address-Size Attributes
  12179.  
  12180.  When executing an instruction, the 80386 can address memory using either 16
  12181.  or 32-bit addresses. Consequently, each instruction that uses memory
  12182.  addresses has associated with it an address-size attribute of either 16 or
  12183.  32 bits. 16-bit addresses imply both the use of a 16-bit displacement in
  12184.  the instruction and the generation of a 16-bit address offset (segment
  12185.  relative address) as the result of the effective address calculation.
  12186.  32-bit addresses imply the use of a 32-bit displacement and the generation
  12187.  of a 32-bit address offset. Similarly, an instruction that accesses words
  12188.  (16 bits) or doublewords (32 bits) has an operand-size attribute of either
  12189.  16 or 32 bits.
  12190.  
  12191.  The attributes are determined by a combination of defaults, instruction
  12192.  prefixes, and (for programs executing in protected mode) size-specification
  12193.  bits in segment descriptors.
  12194.  
  12195.  
  12196.  17.1.1  Default Segment Attribute
  12197.  
  12198.  For programs executed in protected mode, the D-bit in executable-segment
  12199.  descriptors determines the default attribute for both address size and
  12200.  operand size. These default attributes apply to the execution of all
  12201.  instructions in the segment. A value of zero in the D-bit sets the default
  12202.  address size and operand size to 16 bits; a value of one, to 32 bits.
  12203.  
  12204.  Programs that execute in real mode or virtual-8086 mode have 16-bit
  12205.  addresses and operands by default.
  12206.  
  12207.  
  12208.  17.1.2  Operand-Size and Address-Size Instruction Prefixes
  12209.  
  12210.  The internal encoding of an instruction can include two byte-long prefixes:
  12211.  the address-size prefix, 67H, and the operand-size prefix, 66H. (A later
  12212.  section, "Instruction Format," shows the position of the prefixes in an
  12213.  instruction's encoding.) These prefixes override the default segment
  12214.  attributes for the instruction that follows. Table 17-1 shows the effect of
  12215.  each possible combination of defaults and overrides.
  12216.  
  12217.  
  12218.  17.1.3  Address-Size Attribute for Stack
  12219.  
  12220.  Instructions that use the stack implicitly (for example: POP EAX also have
  12221.  a stack address-size attribute of either 16 or 32 bits. Instructions with a
  12222.  stack address-size attribute of 16 use the 16-bit SP stack pointer register;
  12223.  instructions with a stack address-size attribute of 32 bits use the 32-bit
  12224.  ESP register to form the address of the top of the stack.
  12225.  
  12226.  The stack address-size attribute is controlled by the B-bit of the
  12227.  data-segment descriptor in the SS register. A value of zero in the B-bit
  12228.  selects a stack address-size attribute of 16; a value of one selects a stack
  12229.  address-size attribute of 32.
  12230.  
  12231.  
  12232.  Table 17-1. Effective Size Attributes
  12233.  
  12234.  Segment Default D = ...      0    0    0    0    1    1    1    1
  12235.  Operand-Size Prefix 66H      N    N    Y    Y    N    N    Y    Y
  12236.  Address-Size Prefix 67H      N    Y    N    Y    N    Y    N    Y
  12237.  
  12238.  Effective Operand Size      16   16   32   32   32   32   16   16
  12239.  Effective Address Size      16   32   16   32   32   16   32   16
  12240.  
  12241.  Y = Yes, this instruction prefix is present
  12242.  N = No, this instruction prefix is not present
  12243.  
  12244.  
  12245.  17.2  Instruction Format
  12246.  
  12247.  All instruction encodings are subsets of the general instruction format
  12248.  shown in Figure 17-1. Instructions consist of optional instruction
  12249.  prefixes, one or two primary opcode bytes, possibly an address specifier
  12250.  consisting of the ModR/M byte and the SIB (Scale Index Base) byte, a
  12251.  displacement, if required, and an immediate data field, if required.
  12252.  
  12253.  Smaller encoding fields can be defined within the primary opcode or
  12254.  opcodes. These fields define the direction of the operation, the size of the
  12255.  displacements, the register encoding, or sign extension; encoding fields
  12256.  vary depending on the class of operation.
  12257.  
  12258.  Most instructions that can refer to an operand in memory have an addressing
  12259.  form byte following the primary opcode byte(s). This byte, called the ModR/M
  12260.  byte, specifies the address form to be used. Certain encodings of the ModR/M
  12261.  byte indicate a second addressing byte, the SIB (Scale Index Base) byte,
  12262.  which follows the ModR/M byte and is required to fully specify the
  12263.  addressing form.
  12264.  
  12265.  Addressing forms can include a displacement immediately following either
  12266.  the ModR/M or SIB byte. If a displacement is present, it can be 8-, 16- or
  12267.  32-bits.
  12268.  
  12269.  If the instruction specifies an immediate operand, the immediate operand
  12270.  always follows any displacement bytes. The immediate operand, if specified,
  12271.  is always the last field of the instruction.
  12272.  
  12273.  The following are the allowable instruction prefix codes:
  12274.  
  12275.     F3H    REP prefix (used only with string instructions)
  12276.     F3H    REPE/REPZ prefix (used only with string instructions
  12277.     F2H    REPNE/REPNZ prefix (used only with string instructions)
  12278.     F0H    LOCK prefix
  12279.  
  12280.  The following are the segment override prefixes:
  12281.  
  12282.     2EH    CS segment override prefix
  12283.     36H    SS segment override prefix
  12284.     3EH    DS segment override prefix
  12285.     26H    ES segment override prefix
  12286.     64H    FS segment override prefix
  12287.     65H    GS segment override prefix
  12288.     66H    Operand-size override
  12289.     67H    Address-size override
  12290.  
  12291.  
  12292.  Figure 17-1.  80386 Instruction Format
  12293.  
  12294.        ╔═══════════════╦═══════════════╦═══════════════╦═══════════════╗
  12295.        ║  INSTRUCTION  ║   ADDRESS-    ║    OPERAND-   ║   SEGMENT     ║
  12296.        ║    PREFIX     ║  SIZE PREFIX  ║  SIZE PREFIX  ║   OVERRIDE    ║
  12297.        ╠═══════════════╩═══════════════╩═══════════════╩═══════════════╣
  12298.        ║     0 OR 1         0 OR 1           0 OR 1         0 OR 1     ║
  12299.        ╟─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╢
  12300.        ║                        NUMBER OF BYTES                        ║
  12301.        ╚═══════════════════════════════════════════════════════════════╝
  12302.  
  12303.        ╔══════════╦═══════════╦═══════╦══════════════════╦═════════════╗
  12304.        ║  OPCODE  ║  MODR/M   ║  SIB  ║   DISPLACEMENT   ║  IMMEDIATE  ║
  12305.        ║          ║           ║       ║                  ║             ║
  12306.        ╠══════════╩═══════════╩═══════╩══════════════════╩═════════════╣
  12307.        ║  1 OR 2     0 OR 1    0 OR 1      0,1,2 OR 4       0,1,2 OR 4 ║
  12308.        ╟─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╢
  12309.        ║                        NUMBER OF BYTES                        ║
  12310.        ╚═══════════════════════════════════════════════════════════════╝
  12311.  
  12312.  
  12313.  17.2.1  ModR/M and SIB Bytes
  12314.  
  12315.  The ModR/M and SIB bytes follow the opcode byte(s) in many of the 80386
  12316.  instructions. They contain the following information:
  12317.  
  12318.    ■  The indexing type or register number to be used in the instruction
  12319.    ■  The register to be used, or more information to select the instruction
  12320.    ■  The base, index, and scale information
  12321.  
  12322.  The ModR/M byte contains three fields of information:
  12323.  
  12324.    ■  The mod field, which occupies the two most significant bits of the
  12325.       byte, combines with the r/m field to form 32 possible values: eight
  12326.       registers and 24 indexing modes
  12327.  
  12328.    ■  The reg field, which occupies the next three bits following the mod
  12329.       field, specifies either a register number or three more bits of opcode
  12330.       information. The meaning of the reg field is determined by the first
  12331.       (opcode) byte of the instruction.
  12332.  
  12333.    ■  The r/m field, which occupies the three least significant bits of the
  12334.       byte, can specify a register as the location of an operand, or can form
  12335.       part of the addressing-mode encoding in combination with the field as
  12336.       described above
  12337.  
  12338.  The based indexed and scaled indexed forms of 32-bit addressing require the
  12339.  SIB byte. The presence of the SIB byte is indicated by certain encodings of
  12340.  the ModR/M byte. The SIB byte then includes the following fields:
  12341.  
  12342.    ■  The ss field, which occupies the two most significant bits of the
  12343.       byte, specifies the scale factor
  12344.  
  12345.    ■  The index field, which occupies the next three bits following the ss
  12346.       field and specifies the register number of the index register
  12347.  
  12348.    ■  The base field, which occupies the three least significant bits of the
  12349.       byte, specifies the register number of the base register
  12350.  
  12351.  Figure 17-2 shows the formats of the ModR/M and SIB bytes.
  12352.  
  12353.  The values and the corresponding addressing forms of the ModR/M and SIB
  12354.  bytes are shown in Tables 17-2, 17-3, and 17-4. The 16-bit addressing
  12355.  forms specified by the ModR/M byte are in Table 17-2. The 32-bit addressing
  12356.  forms specified by ModR/M are in Table 17-3. Table 17-4 shows the 32-bit
  12357.  addressing forms specified by the SIB byte
  12358.  
  12359.  
  12360.  Figure 17-2.  ModR/M and SIB Byte Formats
  12361.  
  12362.                                   MODR/M BYTE
  12363.  
  12364.                       7    6    5    4    3    2    1    0
  12365.                      ╔════════╦═════════════╦═════════════╗
  12366.                      ║  MOD   ║ REG/OPCODE  ║     R/M     ║
  12367.                      ╚════════╩═════════════╩═════════════╝
  12368.  
  12369.                            SIB (SCALE INDEX BASE) BYTE
  12370.  
  12371.                       7    6    5    4    3    2    1    0
  12372.                      ╔════════╦═════════════╦═════════════╗
  12373.                      ║   SS   ║    INDEX    ║    BASE     ║
  12374.                      ╚════════╩═════════════╩═════════════╝
  12375.  
  12376.  
  12377.  Table 17-2. 16-Bit Addressing Forms with the ModR/M Byte
  12378.  
  12379. ╓┌───────────────┌─────────┌──────┌─────┌─────┌─────┌─────┌─────┌─────┌──────╖
  12380.  r8(/r)                     AL    CL    DL    BL    AH    CH    DH    BH
  12381.  r16(/r)                    AX    CX    DX    BX    SP    BP    SI    DI
  12382.  r32(/r)                    EAX   ECX   EDX   EBX   ESP   EBP   ESI   EDI
  12383.  /digit (Opcode)            0     1     2     3     4     5     6     7
  12384.  REG =                      000   001   010   011   100   101   110   111
  12385.  
  12386.     Effective
  12387.  ┌───Address──┐ ┌Mod R/M┐ ┌────────ModR/M Values in Hexadecimal────────┐
  12388.  
  12389.  [BX + SI]            000   00    08    10    18    20    28    30    38
  12390.  [BX + DI]            001   01    09    11    19    21    29    31    39
  12391.  [BP + SI]            010   02    0A    12    1A    22    2A    32    3A
  12392.  r8(/r)                     AL    CL    DL    BL    AH    CH    DH    BH
  12393.  r16(/r)                    AX    CX    DX    BX    SP    BP    SI    DI
  12394.  r32(/r)                    EAX   ECX   EDX   EBX   ESP   EBP   ESI   EDI
  12395.  /digit (Opcode)            0     1     2     3     4     5     6     7
  12396.  REG =                      000   001   010   011   100   101   110   111
  12397.  
  12398.     Effective
  12399.  ┌───Address──┐ ┌Mod R/M┐ ┌────────ModR/M Values in Hexadecimal────────┐
  12400. [BP + SI]            010   02    0A    12    1A    22    2A    32    3A
  12401.  [BP + DI]            011   03    0B    13    1B    23    2B    33    3B
  12402.  [SI]             00  100   04    0C    14    1C    24    2C    34    3C
  12403.  [DI]                 101   05    0D    15    1D    25    2D    35    3D
  12404.  disp16               110   06    0E    16    1E    26    2E    36    3E
  12405.  [BX]                 111   07    0F    17    1F    27    2F    37    3F
  12406.  
  12407.  [BX+SI]+disp8        000   40    48    50    58    60    68    70    78
  12408.  [BX+DI]+disp8        001   41    49    51    59    61    69    71    79
  12409.  [BP+SI]+disp8        010   42    4A    52    5A    62    6A    72    7A
  12410.  [BP+DI]+disp8        011   43    4B    53    5B    63    6B    73    7B
  12411.  [SI]+disp8       01  100   44    4C    54    5C    64    6C    74    7C
  12412.  [DI]+disp8           101   45    4D    55    5D    65    6D    75    7D
  12413.  r8(/r)                     AL    CL    DL    BL    AH    CH    DH    BH
  12414.  r16(/r)                    AX    CX    DX    BX    SP    BP    SI    DI
  12415.  r32(/r)                    EAX   ECX   EDX   EBX   ESP   EBP   ESI   EDI
  12416.  /digit (Opcode)            0     1     2     3     4     5     6     7
  12417.  REG =                      000   001   010   011   100   101   110   111
  12418.  
  12419.     Effective
  12420.  ┌───Address──┐ ┌Mod R/M┐ ┌────────ModR/M Values in Hexadecimal────────┐
  12421. [DI]+disp8           101   45    4D    55    5D    65    6D    75    7D
  12422.  [BP]+disp8           110   46    4E    56    5E    66    6E    76    7E
  12423.  [BX]+disp8           111   47    4F    57    5F    67    6F    77    7F
  12424.  
  12425.  [BX+SI]+disp16       000   80    88    90    98    A0    A8    B0    B8
  12426.  [BX+DI]+disp16       001   81    89    91    99    A1    A9    B1    B9
  12427.  [BX+SI]+disp16       010   82    8A    92    9A    A2    AA    B2    BA
  12428.  [BX+DI]+disp16       011   83    8B    93    9B    A3    AB    B3    BB
  12429.  [SI]+disp16      10  100   84    8C    94    9C    A4    AC    B4    BC
  12430.  [DI]+disp16          101   85    8D    95    9D    A5    AD    B5    BD
  12431.  [BP]+disp16          110   86    8E    96    9E    A6    AE    B6    BE
  12432.  [BX]+disp16          111   87    8F    97    9F    A7    AF    B7    BF
  12433.  
  12434.  r8(/r)                     AL    CL    DL    BL    AH    CH    DH    BH
  12435.  r16(/r)                    AX    CX    DX    BX    SP    BP    SI    DI
  12436.  r32(/r)                    EAX   ECX   EDX   EBX   ESP   EBP   ESI   EDI
  12437.  /digit (Opcode)            0     1     2     3     4     5     6     7
  12438.  REG =                      000   001   010   011   100   101   110   111
  12439.  
  12440.     Effective
  12441.  ┌───Address──┐ ┌Mod R/M┐ ┌────────ModR/M Values in Hexadecimal────────┐
  12442. 
  12443.  EAX/AX/AL            000   C0    C8    D0    D8    E0    E8    F0    F8
  12444.  ECX/CX/CL            001   C1    C9    D1    D9    E1    E9    F1    F9
  12445.  EDX/DX/DL            010   C2    CA    D2    DA    E2    EA    F2    FA
  12446.  EBX/BX/BL            011   C3    CB    D3    DB    E3    EB    F3    FB
  12447.  ESP/SP/AH        11  100   C4    CC    D4    DC    E4    EC    F4    FC
  12448.  EBP/BP/CH            101   C5    CD    D5    DD    E5    ED    F5    FD
  12449.  ESI/SI/DH            110   C6    CE    D6    DE    E6    EE    F6    FE
  12450.  EDI/DI/BH            111   C7    CF    D7    DF    E7    EF    F7    FF
  12451.  
  12452.  
  12453.  ───────────────────────────────────────────────────────────────────────────
  12454.  NOTES:
  12455.    disp8 denotes an 8-bit displacement following the ModR/M byte, to be
  12456.    sign-extended and added to the index. disp16 denotes a 16-bit displacement
  12457.    following the ModR/M byte, to be added to the index. Default segment
  12458.    register is SS for the effective addresses containing a BP index, DS for
  12459.    other effective addresses.
  12460.  ───────────────────────────────────────────────────────────────────────────
  12461.  
  12462.  
  12463.  Table 17-3. 32-Bit Addressing Forms with the ModR/M Byte
  12464.  
  12465. ╓┌───────────────┌─────────┌──────┌─────┌─────┌─────┌─────┌─────┌─────┌──────╖
  12466.  r8(/r)                     AL    CL    DL    BL    AH    CH    DH    BH
  12467.  r16(/r)                    AX    CX    DX    BX    SP    BP    SI    DI
  12468.  r32(/r)                    EAX   ECX   EDX   EBX   ESP   EBP   ESI   EDI
  12469.  /digit (Opcode)            0     1     2     3     4     5     6     7
  12470.  REG =                      000   001   010   011   100   101   110   111
  12471.  
  12472.     Effective
  12473.  ┌───Address──┐ ┌Mod R/M┐ ┌─────────ModR/M Values in Hexadecimal───────┐
  12474.  
  12475.  [EAX]                000   00    08    10    18    20    28    30    38
  12476.  r8(/r)                     AL    CL    DL    BL    AH    CH    DH    BH
  12477.  r16(/r)                    AX    CX    DX    BX    SP    BP    SI    DI
  12478.  r32(/r)                    EAX   ECX   EDX   EBX   ESP   EBP   ESI   EDI
  12479.  /digit (Opcode)            0     1     2     3     4     5     6     7
  12480.  REG =                      000   001   010   011   100   101   110   111
  12481.  
  12482.     Effective
  12483.  ┌───Address──┐ ┌Mod R/M┐ ┌─────────ModR/M Values in Hexadecimal───────┐
  12484. [EAX]                000   00    08    10    18    20    28    30    38
  12485.  [ECX]                001   01    09    11    19    21    29    31    39
  12486.  [EDX]                010   02    0A    12    1A    22    2A    32    3A
  12487.  [EBX]                011   03    0B    13    1B    23    2B    33    3B
  12488.  [--] [--]        00  100   04    0C    14    1C    24    2C    34    3C
  12489.  disp32               101   05    0D    15    1D    25    2D    35    3D
  12490.  [ESI]                110   06    0E    16    1E    26    2E    36    3E
  12491.  [EDI]                111   07    0F    17    1F    27    2F    37    3F
  12492.  
  12493.  disp8[EAX]           000   40    48    50    58    60    68    70    78
  12494.  disp8[ECX]           001   41    49    51    59    61    69    71    79
  12495.  disp8[EDX]           010   42    4A    52    5A    62    6A    72    7A
  12496.  disp8[EPX];          011   43    4B    53    5B    63    6B    73    7B
  12497.  r8(/r)                     AL    CL    DL    BL    AH    CH    DH    BH
  12498.  r16(/r)                    AX    CX    DX    BX    SP    BP    SI    DI
  12499.  r32(/r)                    EAX   ECX   EDX   EBX   ESP   EBP   ESI   EDI
  12500.  /digit (Opcode)            0     1     2     3     4     5     6     7
  12501.  REG =                      000   001   010   011   100   101   110   111
  12502.  
  12503.     Effective
  12504.  ┌───Address──┐ ┌Mod R/M┐ ┌─────────ModR/M Values in Hexadecimal───────┐
  12505. disp8[EPX];          011   43    4B    53    5B    63    6B    73    7B
  12506.  disp8[--] [--]   01  100   44    4C    54    5C    64    6C    74    7C
  12507.  disp8[ebp]           101   45    4D    55    5D    65    6D    75    7D
  12508.  disp8[ESI]           110   46    4E    56    5E    66    6E    76    7E
  12509.  disp8[EDI]           111   47    4F    57    5F    67    6F    77    7F
  12510.  
  12511.  disp32[EAX]          000   80    88    90    98    A0    A8    B0    B8
  12512.  disp32[ECX]          001   81    89    91    99    A1    A9    B1    B9
  12513.  disp32[EDX]          010   82    8A    92    9A    A2    AA    B2    BA
  12514.  disp32[EBX]          011   83    8B    93    9B    A3    AB    B3    BB
  12515.  disp32[--] [--]  10  100   84    8C    94    9C    A4    AC    B4    BC
  12516.  disp32[EBP]          101   85    8D    95    9D    A5    AD    B5    BD
  12517.  disp32[ESI]          110   86    8E    96    9E    A6    AE    B6    BE
  12518.  r8(/r)                     AL    CL    DL    BL    AH    CH    DH    BH
  12519.  r16(/r)                    AX    CX    DX    BX    SP    BP    SI    DI
  12520.  r32(/r)                    EAX   ECX   EDX   EBX   ESP   EBP   ESI   EDI
  12521.  /digit (Opcode)            0     1     2     3     4     5     6     7
  12522.  REG =                      000   001   010   011   100   101   110   111
  12523.  
  12524.     Effective
  12525.  ┌───Address──┐ ┌Mod R/M┐ ┌─────────ModR/M Values in Hexadecimal───────┐
  12526. disp32[ESI]          110   86    8E    96    9E    A6    AE    B6    BE
  12527.  disp32[EDI]          111   87    8F    97    9F    A7    AF    B7    BF
  12528.  
  12529.  EAX/AX/AL            000   C0    C8    D0    D8    E0    E8    F0    F8
  12530.  ECX/CX/CL            001   C1    C9    D1    D9    E1    E9    F1    F9
  12531.  EDX/DX/DL            010   C2    CA    D2    DA    E2    EA    F2    FA
  12532.  EBX/BX/BL            011   C3    CB    D3    DB    E3    EB    F3    FB
  12533.  ESP/SP/AH        11  100   C4    CC    D4    DC    E4    EC    F4    FC
  12534.  EBP/BP/CH            101   C5    CD    D5    DD    E5    ED    F5    FD
  12535.  ESI/SI/DH            110   C6    CE    D6    DE    E6    EE    F6    FE
  12536.  EDI/DI/BH            111   C7    CF    D7    DF    E7    EF    F7    FF
  12537.  
  12538.  
  12539.  ───────────────────────────────────────────────────────────────────────────
  12540.  NOTES:
  12541.    [--] [--] means a SIB follows the ModR/M byte. disp8 denotes an 8-bit
  12542.    displacement following the SIB byte, to be sign-extended and added to the
  12543.    index. disp32 denotes a 32-bit displacement following the ModR/M byte, to
  12544.    be added to the index.
  12545.  ───────────────────────────────────────────────────────────────────────────
  12546.  
  12547.  
  12548.  Table 17-4. 32-Bit Addressing Forms with the SIB Byte
  12549.  
  12550. ╓┌──────────────┌───────────┌──────┌─────┌─────┌─────┌─────┌─────┌─────┌─────╖
  12551.     r32                      EAX   ECX   EDX   EBX   ESP   [*]  ESI   EDI
  12552.     Base =                   0     1     2     3     4     5     6     7
  12553.     Base =                   000   001   010   011   100   101   110   111
  12554.  
  12555.  ┌Scaled Index┐┌SS Index┐  ┌────────ModR/M Values in Hexadecimal────────┐
  12556.  
  12557.  [EAX]                000    00    01    02    03    04    05    06    07
  12558.  [ECX]                001    08    09    0A    0B    0C    0D    0E    0F
  12559.  [EDX]                010    10    11    12    13    14    15    16    17
  12560.     r32                      EAX   ECX   EDX   EBX   ESP   [*]  ESI   EDI
  12561.     Base =                   0     1     2     3     4     5     6     7
  12562.     Base =                   000   001   010   011   100   101   110   111
  12563.  
  12564.  ┌Scaled Index┐┌SS Index┐  ┌────────ModR/M Values in Hexadecimal────────┐
  12565. [EDX]                010    10    11    12    13    14    15    16    17
  12566.  [EBX]                011    18    19    1A    1B    1C    1D    1E    1F
  12567.  none             00  100    20    21    22    23    24    25    26    27
  12568.  [EBP]                101    28    29    2A    2B    2C    2D    2E    2F
  12569.  [ESI]                110    30    31    32    33    34    35    36    37
  12570.  [EDI]                111    38    39    3A    3B    3C    3D    3E    3F
  12571.  
  12572.  [EAX*2]              000    40    41    42    43    44    45    46    47
  12573.  [ECX*2]              001    48    49    4A    4B    4C    4D    4E    4F
  12574.  [ECX*2]              010    50    51    52    53    54    55    56    57
  12575.  [EBX*2]              011    58    59    5A    5B    5C    5D    5E    5F
  12576.  none             01  100    60    61    62    63    64    65    66    67
  12577.  [EBP*2]              101    68    69    6A    6B    6C    6D    6E    6F
  12578.  [ESI*2]              110    70    71    72    73    74    75    76    77
  12579.  [EDI*2]              111    78    79    7A    7B    7C    7D    7E    7F
  12580.  
  12581.     r32                      EAX   ECX   EDX   EBX   ESP   [*]  ESI   EDI
  12582.     Base =                   0     1     2     3     4     5     6     7
  12583.     Base =                   000   001   010   011   100   101   110   111
  12584.  
  12585.  ┌Scaled Index┐┌SS Index┐  ┌────────ModR/M Values in Hexadecimal────────┐
  12586. 
  12587.  [EAX*4]              000    80    81    82    83    84    85    86    87
  12588.  [ECX*4]              001    88    89    8A    8B    8C    8D    8E    8F
  12589.  [EDX*4]              010    90    91    92    93    94    95    96    97
  12590.  [EBX*4]              011    98    89    9A    9B    9C    9D    9E    9F
  12591.  none             10  100    A0    A1    A2    A3    A4    A5    A6    A7
  12592.  [EBP*4]              101    A8    A9    AA    AB    AC    AD    AE    AF
  12593.  [ESI*4]              110    B0    B1    B2    B3    B4    B5    B6    B7
  12594.  [EDI*4]              111    B8    B9    BA    BB    BC    BD    BE    BF
  12595.  
  12596.  [EAX*8]              000    C0    C1    C2    C3    C4    C5    C6    C7
  12597.  [ECX*8]              001    C8    C9    CA    CB    CC    CD    CE    CF
  12598.  [EDX*8]              010    D0    D1    D2    D3    D4    D5    D6    D7
  12599.  [EBX*8]              011    D8    D9    DA    DB    DC    DD    DE    DF
  12600.  none             11  100    E0    E1    E2    E3    E4    E5    E6    E7
  12601.  [EBP*8]              101    E8    E9    EA    EB    EC    ED    EE    EF
  12602.     r32                      EAX   ECX   EDX   EBX   ESP   [*]  ESI   EDI
  12603.     Base =                   0     1     2     3     4     5     6     7
  12604.     Base =                   000   001   010   011   100   101   110   111
  12605.  
  12606.  ┌Scaled Index┐┌SS Index┐  ┌────────ModR/M Values in Hexadecimal────────┐
  12607. [EBP*8]              101    E8    E9    EA    EB    EC    ED    EE    EF
  12608.  [ESI*8]              110    F0    F1    F2    F3    F4    F5    F6    F7
  12609.  [EDI*8]              111    F8    F9    FA    FB    FC    FD    FE    FF
  12610.  
  12611.  
  12612.  ───────────────────────────────────────────────────────────────────────────
  12613.  NOTES:
  12614.    [*] means a disp32 with no base if MOD is 00, [ESP] otherwise. This
  12615.    provides the following addressing modes:
  12616.        disp32[index]        (MOD=00)
  12617.        disp8[EBP][index]    (MOD=01)
  12618.        disp32[EBP][index]   (MOD=10)
  12619.  ───────────────────────────────────────────────────────────────────────────
  12620.  
  12621.  
  12622.  17.2.2  How to Read the Instruction Set Pages
  12623.  
  12624.  The following is an example of the format used for each 80386 instruction
  12625.  description in this chapter:
  12626.  
  12627.  CMC ── Complement Carry Flag
  12628.  
  12629.  Opcode   Instruction         Clocks      Description
  12630.  
  12631.  F5        CMC                  2            Complement carry flag
  12632.  
  12633.  The above table is followed by paragraphs labelled "Operation,"
  12634.  "Description," "Flags Affected," "Protected Mode Exceptions," "Real
  12635.  Address Mode Exceptions," and, optionally, "Notes." The following sections
  12636.  explain the notational conventions and abbreviations used in these
  12637.  paragraphs of the instruction descriptions.
  12638.  
  12639.  
  12640.  17.2.2.1  Opcode
  12641.  
  12642.  The "Opcode" column gives the complete object code produced for each form
  12643.  of the instruction. When possible, the codes are given as hexadecimal bytes,
  12644.  in the same order in which they appear in memory. Definitions of entries
  12645.  other than hexadecimal bytes are as follows:
  12646.  
  12647.  /digit: (digit is between 0 and 7) indicates that the ModR/M byte of the
  12648.  instruction uses only the r/m (register or memory) operand. The reg field
  12649.  contains the digit that provides an extension to the instruction's opcode.
  12650.  
  12651.  /r: indicates that the ModR/M byte of the instruction contains both a
  12652.  register operand and an r/m operand.
  12653.  
  12654.  cb, cw, cd, cp: a 1-byte (cb), 2-byte (cw), 4-byte (cd) or 6-byte (cp)
  12655.  value following the opcode that is used to specify a code offset and
  12656.  possibly a new value for the code segment register.
  12657.  
  12658.  ib, iw, id: a 1-byte (ib), 2-byte (iw), or 4-byte (id) immediate operand to
  12659.  the instruction that follows the opcode, ModR/M bytes or scale-indexing
  12660.  bytes. The opcode determines if the operand is a signed value. All words and
  12661.  doublewords are given with the low-order byte first.
  12662.  
  12663.  +rb, +rw, +rd: a register code, from 0 through 7, added to the hexadecimal
  12664.  byte given at the left of the plus sign to form a single opcode byte. The
  12665.  codes are──
  12666.  
  12667.        rb         rw         rd
  12668.      AL = 0     AX = 0     EAX = 0
  12669.      CL = 1     CX = 1     ECX = 1
  12670.      DL = 2     DX = 2     EDX = 2
  12671.      BL = 3     BX = 3     EBX = 3
  12672.      AH = 4     SP = 4     ESP = 4
  12673.      CH = 5     BP = 5     EBP = 5
  12674.      DH = 6     SI = 6     ESI = 6
  12675.      BH = 7     DI = 7     EDI = 7
  12676.  
  12677.  
  12678.  17.2.2.2  Instruction
  12679.  
  12680.  The "Instruction" column gives the syntax of the instruction statement as
  12681.  it would appear in an ASM386 program. The following is a list of the symbols
  12682.  used to represent operands in the instruction statements:
  12683.  
  12684.  rel8: a relative address in the range from 128 bytes before the end of the
  12685.  instruction to 127 bytes after the end of the instruction.
  12686.  
  12687.  rel16, rel32: a relative address within the same code segment as the
  12688.  instruction assembled. rel16 applies to instructions with an operand-size
  12689.  attribute of 16 bits; rel32 applies to instructions with an operand-size
  12690.  attribute of 32 bits.
  12691.  
  12692.  ptr16:16, ptr16:32: a FAR pointer, typically in a code segment different
  12693.  from that of the instruction. The notation 16:16 indicates that the value of
  12694.  the pointer has two parts. The value to the right of the colon is a 16-bit
  12695.  selector or value destined for the code segment register. The value to the
  12696.  left corresponds to the offset within the destination segment. ptr16:16 is
  12697.  used when the instruction's operand-size attribute is 16 bits; ptr16:32 is
  12698.  used with the 32-bit attribute.
  12699.  
  12700.  r8: one of the byte registers AL, CL, DL, BL, AH, CH, DH, or BH.
  12701.  
  12702.  r16: one of the word registers AX, CX, DX, BX, SP, BP, SI, or DI.
  12703.  
  12704.  r32: one of the doubleword registers EAX, ECX, EDX, EBX, ESP, EBP, ESI, or
  12705.  EDI.
  12706.  
  12707.  imm8: an immediate byte value. imm8 is a signed number between -128 and
  12708.  +127 inclusive. For instructions in which imm8 is combined with a word or
  12709.  doubleword operand, the immediate value is sign-extended to form a word or
  12710.  doubleword. The upper byte of the word is filled with the topmost bit of the
  12711.  immediate value.
  12712.  
  12713.  imm16: an immediate word value used for instructions whose operand-size
  12714.  attribute is 16 bits. This is a number between -32768 and +32767 inclusive.
  12715.  
  12716.  imm32: an immediate doubleword value used for instructions whose
  12717.  operand-size attribute is 32-bits. It allows the use of a number between
  12718.  +2147483647 and -2147483648.
  12719.  
  12720.  r/m8: a one-byte operand that is either the contents of a byte register
  12721.  (AL, BL, CL, DL, AH, BH, CH, DH), or a byte from memory.
  12722.  
  12723.  r/m16: a word register or memory operand used for instructions whose
  12724.  operand-size attribute is 16 bits. The word registers are: AX, BX, CX, DX,
  12725.  SP, BP, SI, DI. The contents of memory are found at the address provided by
  12726.  the effective address computation.
  12727.  
  12728.  r/m32: a doubleword register or memory operand used for instructions whose
  12729.  operand-size attribute is 32-bits. The doubleword registers are: EAX, EBX,
  12730.  ECX, EDX, ESP, EBP, ESI, EDI. The contents of memory are found at the
  12731.  address provided by the effective address computation.
  12732.  
  12733.  m8: a memory byte addressed by DS:SI or ES:DI (used only by string
  12734.  instructions).
  12735.  
  12736.  m16: a memory word addressed by DS:SI or ES:DI (used only by string
  12737.  instructions).
  12738.  
  12739.  m32: a memory doubleword addressed by DS:SI or ES:DI (used only by string
  12740.  instructions).
  12741.  
  12742.  m16:16, M16:32: a memory operand containing a far pointer composed of two
  12743.  numbers. The number to the left of the colon corresponds to the pointer's
  12744.  segment selector. The number to the right corresponds to its offset.
  12745.  
  12746.  m16 & 32, m16 & 16, m32 & 32: a memory operand consisting of data item pairs
  12747.  whose sizes are indicated on the left and the right side of the ampersand.
  12748.  All memory addressing modes are allowed. m16 & 16 and m32 & 32 operands are
  12749.  used by the BOUND instruction to provide an operand containing an upper and
  12750.  lower bounds for array indices. m16 & 32 is used by LIDT and LGDT to
  12751.  provide a word with which to load the limit field, and a doubleword with
  12752.  which to load the base field of the corresponding Global and Interrupt
  12753.  Descriptor Table Registers.
  12754.  
  12755.  moffs8, moffs16, moffs32: (memory offset) a simple memory variable of type
  12756.  BYTE, WORD, or DWORD used by some variants of the MOV instruction. The
  12757.  actual address is given by a simple offset relative to the segment base. No
  12758.  ModR/M byte is used in the instruction. The number shown with moffs
  12759.  indicates its size, which is determined by the address-size attribute of the
  12760.  instruction.
  12761.  
  12762.  Sreg: a segment register. The segment register bit assignments are ES=0,
  12763.  CS=1, SS=2, DS=3, FS=4, and GS=5.
  12764.  
  12765.  
  12766.  17.2.2.3  Clocks
  12767.  
  12768.  The "Clocks" column gives the number of clock cycles the instruction takes
  12769.  to execute. The clock count calculations makes the following assumptions:
  12770.  
  12771.    ■  The instruction has been prefetched and decoded and is ready for
  12772.       execution.
  12773.  
  12774.    ■  Bus cycles do not require wait states.
  12775.  
  12776.    ■  There are no local bus HOLD requests delaying processor access to the
  12777.       bus.
  12778.  
  12779.    ■  No exceptions are detected during instruction execution.
  12780.  
  12781.    ■  Memory operands are aligned.
  12782.  
  12783.  Clock counts for instructions that have an r/m (register or memory) operand
  12784.  are separated by a slash. The count to the left is used for a register
  12785.  operand; the count to the right is used for a memory operand.
  12786.  
  12787.  The following symbols are used in the clock count specifications:
  12788.  
  12789.    ■  n, which represents a number of repetitions.
  12790.  
  12791.    ■  m, which represents the number of components in the next instruction
  12792.       executed, where the entire displacement (if any) counts as one
  12793.       component, the entire immediate data (if any) counts as one component,
  12794.       and every other byte of the instruction and prefix(es) each counts as
  12795.       one component.
  12796.  
  12797.    ■  pm=, a clock count that applies when the instruction executes in
  12798.       Protected Mode. pm= is not given when the clock counts are the same for
  12799.       Protected and Real Address Modes.
  12800.  
  12801.  When an exception occurs during the execution of an instruction and the
  12802.  exception handler is in another task, the instruction execution time is
  12803.  increased by the number of clocks to effect a task switch. This parameter
  12804.  depends on several factors:
  12805.  
  12806.    ■  The type of TSS used to represent the current task (386 TSS or 286
  12807.       TSS).
  12808.  
  12809.    ■  The type of TSS used to represent the new task.
  12810.  
  12811.    ■  Whether the current task is in V86 mode.
  12812.  
  12813.    ■  Whether the new task is in V86 mode.
  12814.  
  12815.  Table 17-5 summarizes the task switch times for exceptions.
  12816.  
  12817.  
  12818.  Table 17-5. Task Switch Times for Exceptions
  12819.  
  12820.                         New Task
  12821.  
  12822.  Old              386 TSS     286 TSS
  12823.  Task             VM = 0
  12824.  
  12825.  386   VM = 0       309        282
  12826.  TSS
  12827.  
  12828.  386   VM = 1       314        231
  12829.  TSS
  12830.  
  12831.  286                307        282
  12832.  TSS
  12833.  
  12834.  
  12835.  17.2.2.4  Description
  12836.  
  12837.  The "Description" column following the "Clocks" column briefly explains the
  12838.  various forms of the instruction. The "Operation" and "Description" sections
  12839.  contain more details of the instruction's operation.
  12840.  
  12841.  
  12842.  17.2.2.5  Operation
  12843.  
  12844.  The "Operation" section contains an algorithmic description of the
  12845.  instruction which uses a notation similar to the Algol or Pascal language.
  12846.  The algorithms are composed of the following elements:
  12847.  
  12848.  Comments are enclosed within the symbol pairs "(*" and "*)".
  12849.  
  12850.  Compound statements are enclosed between the keywords of the "if" statement
  12851.  (IF, THEN, ELSE, FI) or of the "do" statement (DO, OD), or of the "case"
  12852.  statement (CASE ... OF, ESAC).
  12853.  
  12854.  A register name implies the contents of the register. A register name
  12855.  enclosed in brackets implies the contents of the location whose address is
  12856.  contained in that register. For example, ES:[DI] indicates the contents of
  12857.  the location whose ES segment relative address is in register DI. [SI]
  12858.  indicates the contents of the address contained in register SI relative to
  12859.  SI's default segment (DS) or overridden segment.
  12860.  
  12861.  Brackets also used for memory operands, where they mean that the contents
  12862.  of the memory location is a segment-relative offset. For example, [SRC]
  12863.  indicates that the contents of the source operand is a segment-relative
  12864.  offset.
  12865.  
  12866.  A  B; indicates that the value of B is assigned to A.
  12867.  
  12868.  The symbols =, <>, ≥, and ≤ are relational operators used to compare two
  12869.  values, meaning equal, not equal, greater or equal, less or equal,
  12870.  respectively. A relational expression such as A = B is TRUE if the value of
  12871.  A is equal to B; otherwise it is FALSE.
  12872.  
  12873.  The following identifiers are used in the algorithmic descriptions:
  12874.  
  12875.    ■  OperandSize represents the operand-size attribute of the instruction,
  12876.       which is either 16 or 32 bits. AddressSize represents the address-size
  12877.       attribute, which is either 16 or 32 bits. For example,
  12878.  
  12879.     IF instruction = CMPSW
  12880.     THEN OperandSize  16;
  12881.     ELSE
  12882.        IF instruction = CMPSD
  12883.        THEN OperandSize  32;
  12884.        FI;
  12885.     FI;
  12886.  
  12887.  indicates that the operand-size attribute depends on the form of the CMPS
  12888.  instruction used. Refer to the explanation of address-size and operand-size
  12889.  attributes at the beginning of this chapter for general guidelines on how
  12890.  these attributes are determined.
  12891.  
  12892.    ■  StackAddrSize represents the stack address-size attribute associated
  12893.       with the instruction, which has a value of 16 or 32 bits, as explained
  12894.       earlier in the chapter.
  12895.  
  12896.    ■  SRC represents the source operand. When there are two operands, SRC is
  12897.       the one on the right.
  12898.  
  12899.    ■  DEST represents the destination operand. When there are two operands,
  12900.       DEST is the one on the left.
  12901.  
  12902.    ■  LeftSRC, RightSRC distinguishes between two operands when both are
  12903.       source operands.
  12904.  
  12905.    ■  eSP represents either the SP register or the ESP register depending on
  12906.       the setting of the B-bit for the current stack segment.
  12907.  
  12908.  The following functions are used in the algorithmic descriptions:
  12909.  
  12910.    ■  Truncate to 16 bits(value) reduces the size of the value to fit in 16
  12911.       bits by discarding the uppermost bits as needed.
  12912.  
  12913.    ■  Addr(operand) returns the effective address of the operand (the result
  12914.       of the effective address calculation prior to adding the segment base).
  12915.  
  12916.    ■  ZeroExtend(value) returns a value zero-extended to the operand-size
  12917.       attribute of the instruction. For example, if OperandSize = 32,
  12918.       ZeroExtend of a byte value of -10 converts the byte from F6H to
  12919.       doubleword with hexadecimal value 000000F6H. If the value passed to
  12920.       ZeroExtend and the operand-size attribute are the same size,
  12921.       ZeroExtend returns the value unaltered.
  12922.  
  12923.    ■  SignExtend(value) returns a value sign-extended to the operand-size
  12924.       attribute of the instruction. For example, if OperandSize = 32,
  12925.       SignExtend of a byte containing the value -10 converts the byte from
  12926.       F6H to a doubleword with hexadecimal value FFFFFFF6H. If the value
  12927.       passed to SignExtend and the operand-size attribute are the same size,
  12928.       SignExtend returns the value unaltered.
  12929.  
  12930.    ■  Push(value) pushes a value onto the stack. The number of bytes pushed
  12931.       is determined by the operand-size attribute of the instruction. The
  12932.       action of Push is as follows:
  12933.  
  12934.     IF StackAddrSize = 16
  12935.     THEN
  12936.        IF OperandSize = 16
  12937.        THEN
  12938.           SP  SP - 2;
  12939.           SS:[SP]  value; (* 2 bytes assigned starting at
  12940.                               byte address in SP *)
  12941.        ELSE (* OperandSize = 32 *)
  12942.           SP  SP - 4;
  12943.           SS:[SP]  value; (* 4 bytes assigned starting at
  12944.                               byte address in SP *)
  12945.        FI;
  12946.     ELSE (* StackAddrSize = 32 *)
  12947.        IF OperandSize = 16
  12948.        THEN
  12949.           ESP  ESP - 2;
  12950.           SS:[ESP]  value; (* 2 bytes assigned starting at
  12951.                                byte address in ESP*)
  12952.        ELSE (* OperandSize = 32 *)
  12953.           ESP  ESP - 4;
  12954.           SS:[ESP]  value; (* 4 bytes assigned starting at
  12955.                                byte address in ESP*)
  12956.        FI;
  12957.     FI;
  12958.  
  12959.    ■  Pop(value) removes the value from the top of the stack and returns it.
  12960.       The statement EAX  Pop( ); assigns to EAX the 32-bit value that Pop
  12961.       took from the top of the stack. Pop will return either a word or a
  12962.       doubleword depending on the operand-size attribute. The action of Pop
  12963.       is as follows:
  12964.  
  12965.     IF StackAddrSize = 16
  12966.     THEN
  12967.        IF OperandSize = 16
  12968.        THEN
  12969.           ret val  SS:[SP]; (* 2-byte value *)
  12970.           SP  SP + 2;
  12971.        ELSE (* OperandSize = 32 *)
  12972.           ret val  SS:[SP]; (* 4-byte value *)
  12973.           SP  SP + 4;
  12974.        FI;
  12975.     ELSE (* StackAddrSize = 32 *)
  12976.        IF OperandSize = 16
  12977.        THEN
  12978.           ret val  SS:[ESP]; (* 2 bytes value *)
  12979.           ESP  ESP + 2;
  12980.        ELSE (* OperandSize = 32 *)
  12981.           ret val  SS:[ESP]; (* 4 bytes value *)
  12982.           ESP  ESP + 4;
  12983.        FI;
  12984.     FI;
  12985.     RETURN(ret val); (*returns a word or doubleword*)
  12986.  
  12987.    ■  Bit[BitBase, BitOffset] returns the address of a bit within a bit
  12988.       string, which is a sequence of bits in memory or a register. Bits are
  12989.       numbered from low-order to high-order within registers and within
  12990.       memory bytes. In memory, the two bytes of a word are stored with the
  12991.       low-order byte at the lower address.
  12992.  
  12993.     If the base operand is a register, the offset can be in the range 0..31.
  12994.     This offset addresses a bit within the indicated register. An example,
  12995.     "BIT[EAX, 21]," is illustrated in Figure 17-3.
  12996.  
  12997.     If BitBase is a memory address, BitOffset can range from -2 gigabits to 2
  12998.     gigabits. The addressed bit is numbered (Offset MOD 8) within the byte at
  12999.     address (BitBase + (BitOffset DIV 8)), where DIV is signed division with
  13000.     rounding towards negative infinity, and MOD returns a positive number.
  13001.     This is illustrated in Figure 17-4.
  13002.  
  13003.    ■  I-O-Permission(I-O-Address, width) returns TRUE or FALSE depending on
  13004.     the I/O permission bitmap and other factors. This function is defined as
  13005.     follows:
  13006.  
  13007.     IF TSS type is 286 THEN RETURN FALSE; FI;
  13008.     Ptr  [TSS + 66]; (* fetch bitmap pointer *)
  13009.     BitStringAddr  SHR (I-O-Address, 3) + Ptr;
  13010.     MaskShift  I-O-Address AND 7;
  13011.     CASE width OF:
  13012.           BYTE: nBitMask  1;
  13013.           WORD: nBitMask  3;
  13014.           DWORD: nBitMask  15;
  13015.     ESAC;
  13016.     mask  SHL (nBitMask, MaskShift);
  13017.     CheckString  [BitStringAddr] AND mask;
  13018.     IF CheckString = 0
  13019.     THEN RETURN (TRUE);
  13020.     ELSE RETURN (FALSE);
  13021.     FI;
  13022.  
  13023.    ■  Switch-Tasks is the task switching function described in Chapter 7.
  13024.  
  13025.  
  13026.  17.2.2.6  Description
  13027.  
  13028.  The "Description" section contains further explanation of the instruction's
  13029.  operation.
  13030.  
  13031.  
  13032.  Figure 17-3.  Bit Offset for BIT[EAX, 21]
  13033.  
  13034.     31                    21                                               0
  13035.    ╔═════════════════════╦═╦═══════════════════════════════════════════════╗
  13036.    ║                     ║ ║                                               ║
  13037.    ╚═════════════════════╩═╩═══════════════════════════════════════════════╝
  13038.                                                                           
  13039.                           └────────────────────BITOFFSET = 21──────────────┘
  13040.  
  13041.  
  13042.  Figure 17-4.  Memory Bit Indexing
  13043.  
  13044.                           BIT INDEXING (POSITIVE OFFSET)
  13045.  
  13046.                 7 6 5 4 3 2 1 0 7 6 5 4 3 2 1 0 7 6 5 4 3 2 1 0
  13047.               ╔════╤═╤═════════╤═══════════════╤════════════════╗
  13048.               ║    │ │         │               │                ║
  13049.               ╚════╧═╧═════════╧═══════════════╪════════════════╝
  13050.               │  BITBASE + 1   │    BITBASE    │  BITBASE - 1   │
  13051.                                               │
  13052.                     └────────OFFSET = 13───────┘
  13053.  
  13054.                           BIT INDEXING (NEGATIVE OFFSET)
  13055.  
  13056.                 7 6 5 4 3 2 1 0 7 6 5 4 3 2 1 0 7 6 5 4 3 2 1 0
  13057.               ╔════════════════╤═══════════════╤═══╤═╤══════════╗
  13058.               ║                │               │   │ │          ║
  13059.               ╚════════════════╪═══════════════╧═══╧═╧══════════╝
  13060.               │    BITBASE     │  BITBASE - 1  │  BITBASE - 2   │
  13061.                                │                    
  13062.                                └─────OFFSET = -11───┘
  13063.  
  13064.  
  13065.  17.2.2.7  Flags Affected
  13066.  
  13067.  The "Flags Affected" section lists the flags that are affected by the
  13068.  instruction, as follows:
  13069.  
  13070.    ■  If a flag is always cleared or always set by the instruction, the
  13071.       value is given (0 or 1) after the flag name. Arithmetic and logical
  13072.       instructions usually assign values to the status flags in the uniform
  13073.       manner described in Appendix C. Nonconventional assignments are
  13074.       described in the "Operation" section.
  13075.  
  13076.    ■  The values of flags listed as "undefined" may be changed by the
  13077.       instruction in an indeterminate manner.
  13078.  
  13079.  All flags not listed are unchanged by the instruction.
  13080.  
  13081.  
  13082.  17.2.2.8  Protected Mode Exceptions
  13083.  
  13084.  This section lists the exceptions that can occur when the instruction is
  13085.  executed in 80386 Protected Mode. The exception names are a pound sign (#)
  13086.  followed by two letters and an optional error code in parentheses. For
  13087.  example, #GP(0) denotes a general protection exception with an error code of
  13088.  0. Table 17-6 associates each two-letter name with the corresponding
  13089.  interrupt number.
  13090.  
  13091.  Chapter 9 describes the exceptions and the 80386 state upon entry to the
  13092.  exception.
  13093.  
  13094.  Application programmers should consult the documentation provided with
  13095.  their operating systems to determine the actions taken when exceptions
  13096.  occur.
  13097.  
  13098.  
  13099.  Table 17-6. 80386 Exceptions
  13100.  
  13101.  Mnemonic     Interrupt    Description
  13102.  
  13103.  #UD           6           Invalid opcode
  13104.  #NM           7           Coprocessor not available
  13105.  #DF           8           Double fault
  13106.  #TS          10           Invalid TSS
  13107.  #NP          11           Segment or gate not present
  13108.  #SS          12           Stack fault
  13109.  #GP          13           General protection fault
  13110.  #PF          14           Page fault
  13111.  #MF          16           Math (coprocessor) fault
  13112.  
  13113.  
  13114.  17.2.2.9  Real Address Mode Exceptions
  13115.  
  13116.  Because less error checking is performed by the 80386 in Real Address Mode,
  13117.  this mode has fewer exception conditions. Refer to Chapter 14 for further
  13118.  information on these exceptions.
  13119.  
  13120.  
  13121.  17.2.2.10  Virtual-8086 Mode Exceptions
  13122.  
  13123.  Virtual 8086 tasks provide the ability to simulate Virtual 8086 machines.
  13124.  Virtual 8086 Mode exceptions are similar to those for the 8086 processor,
  13125.  but there are some differences. Refer to Chapter 15 for details.
  13126.  
  13127.  
  13128.  AAA ── ASCII Adjust after Addition
  13129.  
  13130.  Opcode    Instruction    Clocks    Description
  13131.  
  13132.  37        AAA            4         ASCII adjust AL after addition
  13133.  
  13134.  
  13135.  Operation
  13136.  
  13137.  IF ((AL AND 0FH) > 9) OR (AF = 1)
  13138.  THEN
  13139.     AL  (AL + 6) AND 0FH;
  13140.     AH  AH + 1;
  13141.     AF  1;
  13142.     CF  1;
  13143.  ELSE
  13144.     CF  0;
  13145.     AF  0;
  13146.  FI;
  13147.  
  13148.  Description
  13149.  
  13150.  Execute AAA only following an ADD instruction that leaves a byte result
  13151.  in the AL register. The lower nibbles of the operands of the ADD instruction
  13152.  should be in the range 0 through 9 (BCD digits). In this case, AAA adjusts
  13153.  AL to contain the correct decimal digit result. If the addition produced a
  13154.  decimal carry, the AH register is incremented, and the carry and auxiliary
  13155.  carry flags are set to 1. If there was no decimal carry, the carry and
  13156.  auxiliary flags are set to 0 and AH is unchanged. In either case, AL is left
  13157.  with its top nibble set to 0. To convert AL to an ASCII result, follow the
  13158.  AAA instruction with OR AL, 30H.
  13159.  
  13160.  Flags Affected
  13161.  
  13162.  AF and CF as described above; OF, SF, ZF, and PF are undefined
  13163.  
  13164.  Protected Mode Exceptions
  13165.  
  13166.  None
  13167.  
  13168.  Real Address Mode Exceptions
  13169.  
  13170.  None
  13171.  
  13172.  Virtual 8086 Mode Exceptions
  13173.  
  13174.  None
  13175.  
  13176.  
  13177.  AAD ── ASCII Adjust AX before Division
  13178.  
  13179.  Opcode    Instruction    Clocks    Description
  13180.  
  13181.  D5 0A     AAD            19        ASCII adjust AX before division
  13182.  
  13183.  
  13184.  Operation
  13185.  
  13186.  AL  AH * 10 + AL;
  13187.  AH  0;
  13188.  
  13189.  Description
  13190.  
  13191.  AAD is used to prepare two unpacked BCD digits (the least-significant
  13192.  digit in AL, the most-significant digit in AH) for a division operation that
  13193.  will yield an unpacked result. This is accomplished by setting AL to
  13194.  AL + (10 * AH), and then setting AH to 0. AX is then equal to the binary
  13195.  equivalent of the original unpacked two-digit number.
  13196.  
  13197.  Flags Affected
  13198.  
  13199.  SF, ZF, and PF as described in Appendix C; OF, AF, and CF are undefined
  13200.  
  13201.  Protected Mode Exceptions
  13202.  
  13203.  None
  13204.  
  13205.  Real Address Mode Exceptions
  13206.  
  13207.  None
  13208.  
  13209.  Virtual 8086 Mode Exceptions
  13210.  
  13211.  None
  13212.  
  13213.  
  13214.  AAM ── ASCII Adjust AX after Multiply
  13215.  
  13216.  Opcode    Instruction    Clocks    Description
  13217.  
  13218.  D4 0A     AAM            17        ASCII adjust AX after multiply
  13219.  
  13220.  
  13221.  Operation
  13222.  
  13223.  AH  AL / 10;
  13224.  AL  AL MOD 10;
  13225.  
  13226.  Description
  13227.  
  13228.  Execute AAM only after executing a MUL instruction between two unpacked
  13229.  BCD digits that leaves the result in the AX register. Because the result is
  13230.  less than 100, it is contained entirely in the AL register. AAM unpacks the
  13231.  AL result by dividing AL by 10, leaving the quotient (most-significant
  13232.  digit) in AH and the remainder (least-significant digit) in AL.
  13233.  
  13234.  Flags Affected
  13235.  
  13236.  SF, ZF, and PF as described in Appendix C; OF, AF, and CF are undefined
  13237.  
  13238.  Protected Mode Exceptions
  13239.  
  13240.  None
  13241.  
  13242.  Real Address Mode Exceptions
  13243.  
  13244.  None
  13245.  
  13246.  Virtual 8086 Mode Exceptions
  13247.  
  13248.  None
  13249.  
  13250.  
  13251.  AAS ── ASCII Adjust AL after Subtraction
  13252.  
  13253.  Opcode    Instruction    Clocks    Description
  13254.  
  13255.  3F        AAS            4         ASCII adjust AL after subtraction
  13256.  
  13257.  
  13258.  Operation
  13259.  
  13260.  IF (AL AND 0FH) > 9 OR AF = 1
  13261.  THEN
  13262.     AL  AL - 6;
  13263.     AL  AL AND 0FH;
  13264.     AH  AH - 1;
  13265.     AF  1;
  13266.     CF  1;
  13267.  ELSE
  13268.     CF  0;
  13269.     AF  0;
  13270.  FI;
  13271.  
  13272.  Description
  13273.  
  13274.  Execute AAS only after a SUB instruction that leaves the byte result in the
  13275.  AL register. The lower nibbles of the operands of the SUB instruction must
  13276.  have been in the range 0 through 9 (BCD digits). In this case, AAS adjusts
  13277.  AL so it contains the correct decimal digit result. If the subtraction
  13278.  produced a decimal carry, the AH register is decremented, and the carry and
  13279.  auxiliary carry flags are set to 1. If no decimal carry occurred, the carry
  13280.  and auxiliary carry flags are set to 0, and AH is unchanged. In either case,
  13281.  AL is left with its top nibble set to 0. To convert AL to an ASCII result,
  13282.  follow the AAS with OR AL, 30H.
  13283.  
  13284.  Flags Affected
  13285.  
  13286.  AF and CF as described above; OF, SF, ZF, and PF are undefined
  13287.  
  13288.  Protected Mode Exceptions
  13289.  
  13290.  None
  13291.  
  13292.  Real Address Mode Exceptions
  13293.  
  13294.  None
  13295.  
  13296.  Virtual 8086 Mode Exceptions
  13297.  
  13298.  None
  13299.  
  13300.  
  13301.  ADC ── Add with Carry
  13302.  
  13303. ╓┌─────────┌────────────────┌─────────┌──────────────────────────────────────╖
  13304.  Opcode    Instruction      Clocks    Description
  13305.  
  13306.  14 ib     ADC AL,imm8      2         Add with carry immediate byte to AL
  13307.  15 iw     ADC AX,imm16     2         Add with carry immediate word to AX
  13308.  15 id     ADC EAX,imm32    2         Add with carry immediate dword to EAX
  13309.  80 /2 ib  ADC r/m8,imm8    2/7       Add with carry immediate byte to r/m
  13310.                                       byte
  13311.  81 /2 iw  ADC r/m16,imm16  2/7       Add with carry immediate word to r/m
  13312.                                       word
  13313.  81 /2 id  ADC r/m32,imm32  2/7       Add with CF immediate dword to r/m
  13314.                                       dword
  13315.  83 /2 ib  ADC r/m16,imm8   2/7       Add with CF sign-extended immediate
  13316.  Opcode    Instruction      Clocks    Description
  13317. 83 /2 ib  ADC r/m16,imm8   2/7       Add with CF sign-extended immediate
  13318.                                       byte to r/m word
  13319.  83 /2 ib  ADC r/m32,imm8   2/7       Add with CF sign-extended immediate
  13320.                                       byte into r/m dword
  13321.  10 /r     ADC r/m8,r8      2/7       Add with carry byte register to r/m
  13322.                                       byte
  13323.  11 /r     ADC r/m16,r16    2/7       Add with carry word register to r/m
  13324.                                       word
  13325.  11 /r     ADC r/m32,r32    2/7       Add with CF dword register to r/m dword
  13326.  12 /r     ADC r8,r/m8      2/6       Add with carry r/m byte to byte
  13327.                                       register
  13328.  13 /r     ADC r16,r/m16    2/6       Add with carry r/m word to word
  13329.                                       register
  13330.  13 /r     ADC r32,r/m32    2/6       Add with CF r/m dword to dword register
  13331.  
  13332.  
  13333.  Operation
  13334.  
  13335.  DEST  DEST + SRC + CF;
  13336.  
  13337.  Description
  13338.  
  13339.  ADC performs an integer addition of the two operands DEST and SRC and the
  13340.  carry flag, CF. The result of the addition is assigned to the first operand
  13341.  (DEST), and the flags are set accordingly. ADC is usually executed as part
  13342.  of a multi-byte or multi-word addition operation. When an immediate byte
  13343.  value is added to a word or doubleword operand, the immediate value is first
  13344.  sign-extended to the size of the word or doubleword operand.
  13345.  
  13346.  Flags Affected
  13347.  
  13348.  OF, SF, ZF, AF, CF, and PF as described in Appendix C
  13349.  
  13350.  Protected Mode Exceptions
  13351.  
  13352.  #GP(0) if the result is in a nonwritable segment; #GP(0) for an illegal
  13353.  memory operand effective address in the CS, DS, ES, FS, or GS segments;
  13354.  #SS(0) for an illegal address in the SS segment; #PF(fault-code) if page
  13355.  fault
  13356.  
  13357.  Real Address Mode Exceptions
  13358.  
  13359.  Interrupt 13 if any part of the operand would lie outside of the effective
  13360.  address space from 0 to 0FFFFH
  13361.  
  13362.  Virtual 8086 Mode Exceptions
  13363.  
  13364.  Same exceptions as in Real Address Mode; #PF(fault-code) for a page fault
  13365.  
  13366.  
  13367.  ADD ── Add
  13368.  
  13369.  Opcode    Instruction         Clocks    Description
  13370.  
  13371.  04 ib     ADD AL,imm8          2        Add immediate byte to AL
  13372.  05 iw     ADD AX,imm16         2        Add immediate word to AX
  13373.  05 id     ADD EAX,imm32        2        Add immediate dword to EAX
  13374.  80 /0 ib  ADD r/m8,imm8        2/7      Add immediate byte to r/m byte
  13375.  81 /0 iw  ADD r/m16,imm16      2/7      Add immediate word to r/m word
  13376.  81 /0 id  ADD r/m32,imm32      2/7      Add immediate dword to r/m dword
  13377.  83 /0 ib  ADD r/m16,imm8       2/7      Add sign-extended immediate byte
  13378.                                          to r/m word
  13379.  83 /0 ib  ADD r/m32,imm8       2/7      Add sign-extended immediate byte
  13380.                                          to r/m dword
  13381.  00 /r     ADD r/m8,r8          2/7      Add byte register to r/m byte
  13382.  01 /r     ADD r/m16,r16        2/7      Add word register to r/m word
  13383.  01 /r     ADD r/m32,r32        2/7      Add dword register to r/m dword
  13384.  02 /r     ADD r8,r/m8          2/6      Add r/m byte to byte register
  13385.  03 /r     ADD r16,r/m16        2/6      Add r/m word to word register
  13386.  03 /r     ADD r32,r/m32        2/6      Add r/m dword to dword register
  13387.  
  13388.  
  13389.  Operation
  13390.  
  13391.  DEST  DEST + SRC;
  13392.  
  13393.  Description
  13394.  
  13395.  ADD performs an integer addition of the two operands (DEST and SRC). The
  13396.  result of the addition is assigned to the first operand (DEST), and the
  13397.  flags are set accordingly.
  13398.  
  13399.  When an immediate byte is added to a word or doubleword operand, the
  13400.  immediate value is sign-extended to the size of the word or doubleword
  13401.  operand.
  13402.  
  13403.  Flags Affected
  13404.  
  13405.  OF, SF, ZF, AF, CF, and PF as described in Appendix C
  13406.  
  13407.  Protected Mode Exceptions
  13408.  
  13409.  #GP(0) if the result is in a nonwritable segment; #GP(0) for an illegal
  13410.  memory operand effective address in the CS, DS, ES, FS, or GS segments;
  13411.  #SS(0) for an illegal address in the SS segment; #PF(fault-code) for a page
  13412.  fault
  13413.  
  13414.  Real Address Mode Exceptions
  13415.  
  13416.  Interrupt 13 if any part of the operand would lie outside of the effective
  13417.  address space from 0 to 0FFFFH
  13418.  
  13419.  Virtual 8086 Mode Exceptions
  13420.  
  13421.  Same exceptions as in Real Address Mode; #PF(fault-code) for a page fault
  13422.  
  13423.  
  13424.  AND ── Logical AND
  13425.  
  13426.  Opcode    Instruction          Clocks    Description
  13427.  
  13428.  24 ib     AND AL,imm8          2         AND immediate byte to AL
  13429.  25 iw     AND AX,imm16         2         AND immediate word to AX
  13430.  25 id     AND EAX,imm32        2         AND immediate dword to EAX
  13431.  80 /4 ib  AND r/m8,imm8        2/7       AND immediate byte to r/m byte
  13432.  81 /4 iw  AND r/m16,imm16      2/7       AND immediate word to r/m word
  13433.  81 /4 id  AND r/m32,imm32      2/7       AND immediate dword to r/m dword
  13434.  83 /4 ib  AND r/m16,imm8       2/7       AND sign-extended immediate byte
  13435.                                           with r/m word
  13436.  83 /4 ib  AND r/m32,imm8       2/7       AND sign-extended immediate byte
  13437.                                           with r/m dword
  13438.  20 /r     AND r/m8,r8          2/7       AND byte register to r/m byte
  13439.  21 /r     AND r/m16,r16        2/7       AND word register to r/m word
  13440.  21 /r     AND r/m32,r32        2/7       AND dword register to r/m dword
  13441.  22 /r     AND r8,r/m8          2/6       AND r/m byte to byte register
  13442.  23 /r     AND r16,r/m16        2/6       AND r/m word to word register
  13443.  23 /r     AND r32,r/m32        2/6       AND r/m dword to dword register
  13444.  
  13445.  
  13446.  Operation
  13447.  
  13448.  DEST  DEST AND SRC;
  13449.  CF  0;
  13450.  OF  0;
  13451.  
  13452.  Description
  13453.  
  13454.  Each bit of the result of the AND instruction is a 1 if both corresponding
  13455.  bits of the operands are 1; otherwise, it becomes a 0.
  13456.  
  13457.  Flags Affected
  13458.  
  13459.  CF = 0, OF = 0; PF, SF, and ZF as described in Appendix C
  13460.  
  13461.  Protected Mode Exceptions
  13462.  
  13463.  #GP(0) if the result is in a nonwritable segment; #GP(0) for an illegal
  13464.  memory operand effective address in the CS, DS, ES, FS, or GS segments;
  13465.  #SS(0) for an illegal address in the SS segment; #PF(fault-code) for a page
  13466.  fault
  13467.  
  13468.  Real Address Mode Exceptions
  13469.  
  13470.  Interrupt 13 if any part of the operand would lie outside of the effective
  13471.  address space from 0 to 0FFFFH
  13472.  
  13473.  Virtual 8086 Mode Exceptions
  13474.  
  13475.  Same exceptions as in Real Address Mode; #PF(fault-code) for a page fault
  13476.  
  13477.  
  13478.  ARPL ── Adjust RPL Field of Selector
  13479.  
  13480.  Opcode    Instruction          Clocks    Description
  13481.  
  13482.  63 /r     ARPL r/m16,r16       pm=20/21  Adjust RPL of r/m16 to not
  13483.                                           less than RPL of r16
  13484.  
  13485.  
  13486.  Operation
  13487.  
  13488.  IF RPL bits(0,1) of DEST < RPL bits(0,1) of SRC
  13489.  THEN
  13490.     ZF  1;
  13491.     RPL bits(0,1) of DEST  RPL bits(0,1) of SRC;
  13492.  ELSE
  13493.     ZF  0;
  13494.  FI;
  13495.  
  13496.  Description
  13497.  
  13498.  The ARPL instruction has two operands. The first operand is a 16-bit
  13499.  memory variable or word register that contains the value of a selector. The
  13500.  second operand is a word register. If the RPL field ("requested privilege
  13501.  level"──bottom two bits) of the first operand is less than the RPL field of
  13502.  the second operand, the zero flag is set to 1 and the RPL field of the
  13503.  first operand is increased to match the second operand. Otherwise, the zero
  13504.  flag is set to 0 and no change is made to the first operand.
  13505.  
  13506.  ARPL appears in operating system software, not in application programs. It
  13507.  is used to guarantee that a selector parameter to a subroutine does not
  13508.  request more privilege than the caller is allowed. The second operand of
  13509.  ARPL is normally a register that contains the CS selector value of the
  13510.  caller.
  13511.  
  13512.  Flags Affected
  13513.  
  13514.  ZF as described above
  13515.  
  13516.  Protected Mode Exceptions
  13517.  
  13518.  #GP(0) if the result is in a nonwritable segment; #GP(0) for an illegal
  13519.  memory operand effective address in the CS, DS, ES, FS, or GS segments;
  13520.  #SS(0) for an illegal address in the SS segment; #PF(fault-code) for a page
  13521.  fault
  13522.  
  13523.  Real Address Mode Exceptions
  13524.  
  13525.  Interrupt 6; ARPL is not recognized in Real Address Mode
  13526.  
  13527.  Virtual 8086 Mode Exceptions
  13528.  
  13529.  Same exceptions as in Real Address Mode; #PF(fault-code) for a page fault
  13530.  
  13531.  
  13532.  BOUND ── Check Array Index Against Bounds
  13533.  
  13534.  Opcode    Instruction          Clocks    Description
  13535.  
  13536.  62 /r     BOUND r16,m16&16     10        Check if r16 is within bounds
  13537.                                           (passes test)
  13538.  62 /r     BOUND r32,m32&32     10        Check if r32 is within bounds
  13539.                                           (passes test)
  13540.  
  13541.  
  13542.  Operation
  13543.  
  13544.  IF (LeftSRC < [RightSRC] OR LeftSRC > [RightSRC + OperandSize/8])
  13545.     (* Under lower bound or over upper bound *)
  13546.  THEN Interrupt 5;
  13547.  FI;
  13548.  
  13549.  Description
  13550.  
  13551.  BOUND ensures that a signed array index is within the limits specified by a
  13552.  block of memory consisting of an upper and a lower bound. Each bound uses
  13553.  one word for an operand-size attribute of 16 bits and a doubleword for an
  13554.  operand-size attribute of 32 bits. The first operand (a register) must be
  13555.  greater than or equal to the first bound in memory (lower bound), and less
  13556.  than or equal to the second bound in memory (upper bound). If the register
  13557.  is not within bounds, an Interrupt 5 occurs; the return EIP points to the
  13558.  BOUND instruction.
  13559.  
  13560.  The bounds limit data structure is usually placed just before the array
  13561.  itself, making the limits addressable via a constant offset from the
  13562.  beginning of the array.
  13563.  
  13564.  Flags Affected
  13565.  
  13566.  None
  13567.  
  13568.  Protected Mode Exceptions
  13569.  
  13570.  Interrupt 5 if the bounds test fails, as described above; #GP(0) for an
  13571.  illegal memory operand effective address in the CS, DS, ES, FS, or GS
  13572.  segments; #SS(0) for an illegal address in the SS segment; #PF(fault-code)
  13573.  for a page fault
  13574.  
  13575.  The second operand must be a memory operand, not a register. If BOUND is
  13576.  executed with a ModRM byte representing a register as the second operand,
  13577.  #UD occurs.
  13578.  
  13579.  Real Address Mode Exceptions
  13580.  
  13581.  Interrupt 5 if the bounds test fails; Interrupt 13 if any part of the
  13582.  operand would lie outside of the effective address space from 0 to 0FFFFH;
  13583.  Interrupt 6 if the second operand is a register
  13584.  
  13585.  Virtual 8086 Mode Exceptions
  13586.  
  13587.  Same exceptions as in Real Address Mode; #PF(fault-code) for a page fault
  13588.  
  13589.  
  13590.  BSF ── Bit Scan Forward
  13591.  
  13592.  Opcode    Instruction          Clocks    Description
  13593.  
  13594.  0F  BC    BSF r16,r/m16        10+3n     Bit scan forward on r/m word
  13595.  0F  BC    BSF r32,r/m32        10+3n     Bit scan forward on r/m dword
  13596.  
  13597.  
  13598.  Notes
  13599.  
  13600.   is the number of leading zero bits.
  13601.  
  13602.  Operation
  13603.  
  13604.  IF r/m = 0
  13605.  THEN
  13606.     ZF  1;
  13607.     register  UNDEFINED;
  13608.  ELSE
  13609.     temp  0;
  13610.     ZF  0;
  13611.     WHILE BIT[r/m, temp = 0]
  13612.     DO
  13613.        temp  temp + 1;
  13614.        register  temp;
  13615.     OD;
  13616.  FI;
  13617.  
  13618.  Description
  13619.  
  13620.  BSF scans the bits in the second word or doubleword operand starting with
  13621.  bit 0. The ZF flag is cleared if the bits are all 0; otherwise, the ZF flag
  13622.  is set and the destination register is loaded with the bit index of the
  13623.  first set bit.
  13624.  
  13625.  Flags Affected
  13626.  
  13627.  ZF as described above
  13628.  
  13629.  Protected Mode Exceptions
  13630.  
  13631.  #GP(0) for an illegal memory operand effective address in the CS, DS, ES,
  13632.  FS, or GS segments; #SS(0) for an illegal address in the SS segment;
  13633.  #PF(fault-code) for a page fault
  13634.  
  13635.  Real Address Mode Exceptions
  13636.  
  13637.  Interrupt 13 if any part of the operand would lie outside of the effective
  13638.  address space from 0 to 0FFFFH
  13639.  
  13640.  Virtual 8086 Mode Exceptions
  13641.  
  13642.  Same exceptions as in Real Address Mode; #PF(fault-code) for a page fault
  13643.  
  13644.  
  13645.  BSR ── Bit Scan Reverse
  13646.  
  13647.  Opcode    Instruction          Clocks    Description
  13648.  
  13649.  0F  BD    BSR r16,r/m16        10+3n     Bit scan reverse on r/m word
  13650.  0F  BD    BSR r32,r/m32        10+3n     Bit scan reverse on r/m dword
  13651.  
  13652.  
  13653.  Operation
  13654.  
  13655.  IF r/m = 0
  13656.  THEN
  13657.     ZF  1;
  13658.     register  UNDEFINED;
  13659.  ELSE
  13660.     temp  OperandSize - 1;
  13661.     ZF  0;
  13662.     WHILE BIT[r/m, temp] = 0
  13663.     DO
  13664.        temp  temp - 1;
  13665.        register  temp;
  13666.     OD;
  13667.  FI;
  13668.  
  13669.  Description
  13670.  
  13671.  BSR scans the bits in the second word or doubleword operand from the most
  13672.  significant bit to the least significant bit. The ZF flag is cleared if the
  13673.  bits are all 0; otherwise, ZF is set and the destination register is loaded
  13674.  with the bit index of the first set bit found when scanning in the reverse
  13675.  direction.
  13676.  
  13677.  Flags Affected
  13678.  
  13679.  ZF as described above
  13680.  
  13681.  Protected Mode Exceptions
  13682.  
  13683.  #GP(0) if the result is in a nonwritable segment; #GP(0) for an illegal
  13684.  memory operand effective address in the CS, DS, ES, FS, or GS segments;
  13685.  #SS(0) for an illegal address in the SS segment; #PF(fault-code) for a page
  13686.  fault
  13687.  
  13688.  Real Address Mode Exceptions
  13689.  
  13690.  Interrupt 13 if any part of the operand would lie outside of the effective
  13691.  address space from 0 to 0FFFFH
  13692.  
  13693.  Virtual 8086 Mode Exceptions
  13694.  
  13695.  Same exceptions as in Real Address Mode; #PF(fault-code) for a page fault
  13696.  
  13697.  
  13698.  BT ── Bit Test
  13699.  
  13700.  Opcode         Instruction     Clocks    Description
  13701.  
  13702.  0F  A3         BT r/m16,r16    3/12      Save bit in carry flag
  13703.  0F  A3         BT r/m32,r32    3/12      Save bit in carry flag
  13704.  0F  BA /4 ib   BT r/m16,imm8   3/6       Save bit in carry flag
  13705.  0F  BA /4 ib   BT r/m32,imm8   3/6       Save bit in carry flag
  13706.  
  13707.  
  13708.  Operation
  13709.  
  13710.  CF  BIT[LeftSRC, RightSRC];
  13711.  
  13712.  Description
  13713.  
  13714.  BT saves the value of the bit indicated by the base (first operand) and the
  13715.  bit offset (second operand) into the carry flag.
  13716.  
  13717.  Flags Affected
  13718.  
  13719.  CF as described above
  13720.  
  13721.  Protected Mode Exceptions
  13722.  
  13723.  #GP(0) for an illegal memory operand effective address in the CS, DS, ES,
  13724.  FS, or GS segments; #SS(0) for an illegal address in the SS segment;
  13725.  #PF(fault-code) for a page fault
  13726.  
  13727.  Real Address Mode Exceptions
  13728.  
  13729.  Interrupt 13 if any part of the operand would lie outside of the effective
  13730.  address space from 0 to 0FFFFH
  13731.  
  13732.  Virtual 8086 Mode Exceptions
  13733.  
  13734.  Same exceptions as in Real Address Mode; #PF(fault-code) for a page fault
  13735.  
  13736.  Notes
  13737.  
  13738.  The index of the selected bit can be given by the immediate constant in the
  13739.  instruction or by a value in a general register. Only an 8-bit immediate
  13740.  value is used in the instruction. This operand is taken modulo 32, so the
  13741.  range of immediate bit offsets is 0..31. This allows any bit within a
  13742.  register to be selected. For memory bit strings, this immediate field gives
  13743.  only the bit offset within a word or doubleword. Immediate bit offsets
  13744.  larger than 31 are supported by using the immediate bit offset field in
  13745.  combination with the displacement field of the memory operand. The low-order
  13746.  3 to 5 bits of the immediate bit offset are stored in the immediate bit
  13747.  offset field, and the high-order 27 to 29 bits are shifted and combined with
  13748.  the byte displacement in the addressing mode.
  13749.  
  13750.  When accessing a bit in memory, the 80386 may access four bytes starting
  13751.  from the memory address given by:
  13752.  
  13753.     Effective Address + (4 * (BitOffset DIV 32))
  13754.  
  13755.  for a 32-bit operand size, or two bytes starting from the memory address
  13756.  given by:
  13757.  
  13758.     Effective Address + (2 * (BitOffset DIV 16))
  13759.  
  13760.  for a 16-bit operand size. It may do so even when only a single byte needs
  13761.  to be accessed in order to reach the given bit. You must therefore avoid
  13762.  referencing areas of memory close to address space holes. In particular,
  13763.  avoid references to memory-mapped I/O registers. Instead, use the MOV
  13764.  instructions to load from or store to these addresses, and use the register
  13765.  form of these instructions to manipulate the data.
  13766.  
  13767.  
  13768.  BTC ── Bit Test and Complement
  13769.  
  13770.  Opcode        Instruction     Clocks  Description
  13771.  
  13772.  0F  BB        BTC r/m16,r16   6/13    Save bit in carry flag and complement
  13773.  0F  BB        BTC r/m32,r32   6/13    Save bit in carry flag and complement
  13774.  0F  BA /7 ib  BTC r/m16,imm8  6/8     Save bit in carry flag and complement
  13775.  0F  BA /7 ib  BTC r/m32,imm8  6/8     Save bit in carry flag and complement
  13776.  
  13777.  
  13778.  Operation
  13779.  
  13780.  CF  BIT[LeftSRC, RightSRC];
  13781.  BIT[LeftSRC, RightSRC]  NOT BIT[LeftSRC, RightSRC];
  13782.  
  13783.  Description
  13784.  
  13785.  BTC saves the value of the bit indicated by the base (first operand) and the
  13786.  bit offset (second operand) into the carry flag and then complements the
  13787.  bit.
  13788.  
  13789.  Flags Affected
  13790.  
  13791.  CF as described above
  13792.  
  13793.  Protected Mode Exceptions
  13794.  
  13795.  #GP(0) if the result is in a nonwritable segment; #GP(0) for an illegal
  13796.  memory operand effective address in the CS, DS, ES, FS, or GS segments;
  13797.  #SS(0) for an illegal address in the SS segment; #PF(fault-code) for a page
  13798.  fault
  13799.  
  13800.  Real Address Mode Exceptions
  13801.  
  13802.  Interrupt 13 if any part of the operand would lie outside of the effective
  13803.  address space from 0 to 0FFFFH
  13804.  
  13805.  Virtual 8086 Mode Exceptions
  13806.  
  13807.  Same exceptions as in Real Address Mode; #PF(fault-code) for a page fault
  13808.  
  13809.  Notes
  13810.  
  13811.  The index of the selected bit can be given by the immediate constant in the
  13812.  instruction or by a value in a general register. Only an 8-bit immediate
  13813.  value is used in the instruction. This operand is taken modulo 32, so the
  13814.  range of immediate bit offsets is 0..31. This allows any bit within a
  13815.  register to be selected. For memory bit strings, this immediate field gives
  13816.  only the bit offset within a word or doubleword. Immediate bit offsets
  13817.  larger than 31 are supported by using the immediate bit offset field in
  13818.  combination with the displacement field of the memory operand. The low-order
  13819.  3 to 5 bits of the immediate bit offset are stored in the immediate bit
  13820.  offset field, and the high-order 27 to 29 bits are shifted and combined with
  13821.  the byte displacement in the addressing mode.
  13822.  
  13823.  When accessing a bit in memory, the 80386 may access four bytes starting
  13824.  from the memory address given by:
  13825.  
  13826.     Effective Address + (4 * (BitOffset DIV 32))
  13827.  
  13828.  for a 32-bit operand size, or two bytes starting from the memory address
  13829.  given by:
  13830.  
  13831.     Effective Address + (2 * (BitOffset DIV 16))
  13832.  
  13833.  for a 16-bit operand size. It may do so even when only a single byte needs
  13834.  to be accessed in order to reach the given bit. You must therefore avoid
  13835.  referencing areas of memory close to address space holes. In particular,
  13836.  avoid references to memory-mapped I/O registers. Instead, use the MOV
  13837.  instructions to load from or store to these addresses, and use the register
  13838.  form of these instructions to manipulate the data.
  13839.  
  13840.  
  13841.  BTR ── Bit Test and Reset
  13842.  
  13843.  Opcode        Instruction     Clocks  Description
  13844.  
  13845.  0F  B3        BTR r/m16,r16   6/13    Save bit in carry flag and reset
  13846.  0F  B3        BTR r/m32,r32   6/13    Save bit in carry flag and reset
  13847.  0F  BA /6 ib  BTR r/m16,imm8  6/8     Save bit in carry flag and reset
  13848.  0F  BA /6 ib  BTR r/m32,imm8  6/8     Save bit in carry flag and reset
  13849.  
  13850.  
  13851.  Operation
  13852.  
  13853.  CF  BIT[LeftSRC, RightSRC];
  13854.  BIT[LeftSRC, RightSRC]  0;
  13855.  
  13856.  Description
  13857.  
  13858.  BTR saves the value of the bit indicated by the base (first operand) and the
  13859.  bit offset (second operand) into the carry flag and then stores 0 in the
  13860.  bit.
  13861.  
  13862.  Flags Affected
  13863.  
  13864.  CF as described above
  13865.  
  13866.  Protected Mode Exceptions
  13867.  
  13868.  #GP(0) if the result is in a nonwritable segment; #GP(0) for an illegal
  13869.  memory operand effective address in the CS, DS, ES, FS, or GS segments;
  13870.  #SS(0) for an illegal address in the SS segment; #PF(fault-code) for a page
  13871.  fault
  13872.  
  13873.  Real Address Mode Exceptions
  13874.  
  13875.  Interrupt 13 if any part of the operand would lie outside of the effective
  13876.  address space from 0 to 0FFFFH
  13877.  
  13878.  Virtual 8086 Mode Exceptions
  13879.  
  13880.  Same exceptions as in Real Address Mode; #PF(fault-code) for a page fault
  13881.  
  13882.  Notes
  13883.  
  13884.  The index of the selected bit can be given by the immediate constant in the
  13885.  instruction or by a value in a general register. Only an 8-bit immediate
  13886.  value is used in the instruction. This operand is taken modulo 32, so the
  13887.  range of immediate bit offsets is 0..31. This allows any bit within a
  13888.  register to be selected. For memory bit strings, this immediate field gives
  13889.  only the bit offset within a word or doubleword. Immediate bit offsets
  13890.  larger than 31 (or 15) are supported by using the immediate bit offset field
  13891.  in combination with the displacement field of the memory operand. The
  13892.  low-order 3 to 5 bits of the immediate bit offset are stored in the
  13893.  immediate bit offset field, and the high-order 27 to 29 bits are shifted and
  13894.  combined with the byte displacement in the addressing mode.
  13895.  
  13896.  When accessing a bit in memory, the 80386 may access four bytes starting
  13897.  from the memory address given by:
  13898.  
  13899.     Effective Address + 4 * (BitOffset DIV 32)
  13900.  
  13901.  for a 32-bit operand size, or two bytes starting from the memory address
  13902.  given by:
  13903.  
  13904.     Effective Address + 2 * (BitOffset DIV 16)
  13905.  
  13906.  for a 16-bit operand size. It may do so even when only a single byte needs
  13907.  to be accessed in order to reach the given bit. You must therefore avoid
  13908.  referencing areas of memory close to address space holes. In particular,
  13909.  avoid references to memory-mapped I/O registers. Instead, use the MOV
  13910.  instructions to load from or store to these addresses, and use the register
  13911.  form of these instructions to manipulate the data.
  13912.  
  13913.  
  13914.  BTS ── Bit Test and Set
  13915.  
  13916.  Opcode        Instruction     Clocks  Description
  13917.  
  13918.  0F  AB        BTS r/m16,r16   6/13    Save bit in carry flag and set
  13919.  0F  AB        BTS r/m32,r32   6/13    Save bit in carry flag and set
  13920.  0F  BA /5 ib  BTS r/m16,imm8  6/8     Save bit in carry flag and set
  13921.  0F  BA /5 ib  BTS r/m32,imm8  6/8     Save bit in carry flag and set
  13922.  
  13923.  
  13924.  Operation
  13925.  
  13926.  CF  BIT[LeftSRC, RightSRC];
  13927.  BIT[LeftSRC, RightSRC]  1;
  13928.  
  13929.  Description
  13930.  
  13931.  BTS saves the value of the bit indicated by the base (first operand) and the
  13932.  bit offset (second operand) into the carry flag and then stores 1 in the
  13933.  bit.
  13934.  
  13935.  Flags Affected
  13936.  
  13937.  CF as described above
  13938.  
  13939.  Protected Mode Exceptions
  13940.  
  13941.  #GP(0) if the result is in a nonwritable segment; #GP(0) for an illegal
  13942.  memory operand effective address in the CS, DS, ES, FS, or GS segments;
  13943.  #SS(0) for an illegal address in the SS segment; #PF(fault-code) for a page
  13944.  fault
  13945.  
  13946.  Real Address Mode Exceptions
  13947.  
  13948.  Interrupt 13 if any part of the operand would lie outside of the effective
  13949.  address space from 0 to 0FFFFH
  13950.  
  13951.  Virtual 8086 Mode Exceptions
  13952.  
  13953.  Same exceptions as in Real Address Mode; #PF(fault-code) for a page fault
  13954.  
  13955.  Notes
  13956.  
  13957.  The index of the selected bit can be given by the immediate constant in the
  13958.  instruction or by a value in a general register. Only an 8-bit immediate
  13959.  value is used in the instruction. This operand is taken modulo 32, so the
  13960.  range of immediate bit offsets is 0..31. This allows any bit within a
  13961.  register to be selected. For memory bit strings, this immediate field gives
  13962.  only the bit offset within a word or doubleword. Immediate bit offsets
  13963.  larger than 31 are supported by using the immediate bit offset field in
  13964.  combination with the displacement field of the memory operand. The
  13965.  low-order 3 to 5 bits of the immediate bit offset are stored in the
  13966.  immediate bit offset field, and the high order 27 to 29 bits are shifted and
  13967.  combined with the byte displacement in the addressing mode.
  13968.  
  13969.  When accessing a bit in memory, the processor may access four bytes starting
  13970.  from the memory address given by:
  13971.  
  13972.     Effective Address + (4 * (BitOffset DIV 32))
  13973.  
  13974.  for a 32-bit operand size, or two bytes starting from the memory address
  13975.  given by:
  13976.  
  13977.     Effective Address + (2 * (BitOffset DIV 16))
  13978.  
  13979.  for a 16-bit operand size. It may do this even when only a single byte needs
  13980.  to be accessed in order to get at the given bit. Thus the programmer must be
  13981.  careful to avoid referencing areas of memory close to address space holes.
  13982.  In particular, avoid references to memory-mapped I/O registers. Instead, use
  13983.  the MOV instructions to load from or store to these addresses, and use the
  13984.  register form of these instructions to manipulate the data.
  13985.  
  13986.  
  13987.  CALL ── Call Procedure
  13988.  
  13989. ╓┌─────────┌───────────────┌───────────────┌─────────────────────────────────╖
  13990.  Opcode    Instruction     Clocks         Description
  13991.  
  13992.  E8  cw    CALL rel16       7+m            Call near, displacement relative
  13993.                                            to next instruction
  13994.  FF  /2    CALL r/m16       7+m/10+m       Call near, register
  13995.                                            indirect/memory indirect
  13996.  9A  cd    CALL ptr16:16    17+m,pm=34+m   Call intersegment, to full
  13997.                                            pointer given
  13998.  9A  cd    CALL ptr16:16    pm=52+m        Call gate, same privilege
  13999.  9A  cd    CALL ptr16:16    pm=86+m        Call gate, more privilege, no
  14000.                                            parameters
  14001.  9A  cd    CALL ptr16:16    pm=94+4x+m     Call gate, more privilege, x
  14002.                                            parameters
  14003.  9A  cd    CALL ptr16:16    ts             Call to task
  14004.  FF  /3    CALL m16:16      22+m,pm=38+m   Call intersegment, address at
  14005.                                            r/m dword
  14006.  FF  /3    CALL m16:16      pm=56+m        Call gate, same privilege
  14007.  FF  /3    CALL m16:16      pm=90+m        Call gate, more privilege, no
  14008.                                            parameters
  14009.  Opcode    Instruction     Clocks         Description
  14010.                                           parameters
  14011.  FF  /3    CALL m16:16      pm=98+4x+m     Call gate, more privilege, x
  14012.                                            parameters
  14013.  FF  /3    CALL m16:16      5 + ts         Call to task
  14014.  E8  cd    CALL rel32       7+m            Call near, displacement relative
  14015.                                            to next instruction
  14016.  FF  /2    CALL r/m32       7+m/10+m       Call near, indirect
  14017.  9A  cp    CALL ptr16:32    17+m,pm=34+m   Call intersegment, to full
  14018.                                            pointer given
  14019.  9A  cp    CALL ptr16:32    pm=52+m        Call gate, same privilege
  14020.  9A  cp    CALL ptr16:32    pm=86+m        Call gate, more privilege, no
  14021.                                            parameters
  14022.  9A  cp    CALL ptr32:32    pm=94+4x+m     Call gate, more privilege, x
  14023.                                            parameters
  14024.  9A  cp    CALL ptr16:32    ts             Call to task
  14025.  FF  /3    CALL m16:32      22+m,pm=38+m   Call intersegment, address at
  14026.                                            r/m dword
  14027.  FF  /3    CALL m16:32      pm=56+m        Call gate, same privilege
  14028.  FF  /3    CALL m16:32      pm=90+m        Call gate, more privilege, no
  14029.                                            parameters
  14030.  Opcode    Instruction     Clocks         Description
  14031.                                           parameters
  14032.  FF  /3    CALL m16:32      pm=98+4x+m     Call gate, more privilege, x
  14033.                                            parameters
  14034.  FF  /3    CALL m16:32      5 + ts         Call to task
  14035.  
  14036.  
  14037.  ───────────────────────────────────────────────────────────────────────────
  14038.  NOTE:
  14039.    Values of ts are given by the following table:
  14040.  
  14041.                               New Task
  14042.                386 TSS         386 TSS         286 TSS
  14043.    Old         VM = 0          VM = 1
  14044.    Task                     Via Task Gate?
  14045.  
  14046.                N     Y         N     Y         N     Y
  14047.  
  14048.  386          300   309       217   226       273   282
  14049.  TSS VM=0
  14050.  
  14051.  286          298   307       217   226       273   282
  14052.  TSS
  14053.  ───────────────────────────────────────────────────────────────────────────
  14054.  
  14055.  Operation
  14056.  
  14057.  IF rel16 or rel32 type of call
  14058.  THEN (* near relative call *)
  14059.     IF OperandSize = 16
  14060.     THEN
  14061.        Push(IP);
  14062.        EIP  (EIP + rel16) AND 0000FFFFH;
  14063.     ELSE (* OperandSize = 32 *)
  14064.        Push(EIP);
  14065.        EIP  EIP + rel32;
  14066.     FI;
  14067.  FI;
  14068.  
  14069.  IF r/m16 or r/m32 type of call
  14070.  THEN (* near absolute call *)
  14071.     IF OperandSize = 16
  14072.     THEN
  14073.        Push(IP);
  14074.        EIP  [r/m16] AND 0000FFFFH;
  14075.     ELSE (* OperandSize = 32 *)
  14076.        Push(EIP);
  14077.        EIP  [r/m32];
  14078.     FI;
  14079.  FI;
  14080.  
  14081.  IF (PE = 0 OR (PE = 1 AND VM = 1))
  14082.  (* real mode or virtual 8086 mode *)
  14083.     AND instruction = far CALL
  14084.     (* i.e., operand type is m16:16, m16:32, ptr16:16, ptr16:32 *)
  14085.  THEN
  14086.     IF OperandSize = 16
  14087.     THEN
  14088.        Push(CS);
  14089.        Push(IP); (* address of next instruction; 16 bits *)
  14090.     ELSE
  14091.        Push(CS); (* padded with 16 high-order bits *)
  14092.        Push(EIP); (* address of next instruction; 32 bits *)
  14093.     FI;
  14094.     IF operand type is m16:16 or m16:32
  14095.     THEN (* indirect far call *)
  14096.        IF OperandSize = 16
  14097.        THEN
  14098.           CS:IP  [m16:16];
  14099.           EIP  EIP AND 0000FFFFH; (* clear upper 16 bits *)
  14100.        ELSE (* OperandSize = 32 *)
  14101.           CS:EIP  [m16:32];
  14102.        FI;
  14103.     FI;
  14104.     IF operand type is ptr16:16 or ptr16:32
  14105.     THEN (* direct far call *)
  14106.        IF OperandSize = 16
  14107.        THEN
  14108.           CS:IP  ptr16:16;
  14109.           EIP  EIP AND 0000FFFFH; (* clear upper 16 bits *)
  14110.        ELSE (* OperandSize = 32 *)
  14111.           CS:EIP  ptr16:32;
  14112.        FI;
  14113.     FI;
  14114.  FI;
  14115.  
  14116.  IF (PE = 1 AND VM = 0) (* Protected mode, not V86 mode *)
  14117.     AND instruction = far CALL
  14118.  THEN
  14119.     If indirect, then check access of EA doubleword;
  14120.        #GP(0) if limit violation;
  14121.     New CS selector must not be null else #GP(0);
  14122.     Check that new CS selector index is within its
  14123.        descriptor table limits; else #GP(new CS selector);
  14124.     Examine AR byte of selected descriptor for various legal values;
  14125.        depending on value:
  14126.        go to CONFORMING-CODE-SEGMENT;
  14127.        go to NONCONFORMING-CODE-SEGMENT;
  14128.        go to CALL-GATE;
  14129.        go to TASK-GATE;
  14130.        go to TASK-STATE-SEGMENT;
  14131.     ELSE #GP(code segment selector);
  14132.  FI;
  14133.  
  14134.  CONFORMING-CODE-SEGMENT:
  14135.     DPL must be ≤ CPL ELSE #GP(code segment selector);
  14136.     Segment must be present ELSE #NP(code segment selector);
  14137.     Stack must be big enough for return address ELSE #SS(0);
  14138.     Instruction pointer must be in code segment limit ELSE #GP(0);
  14139.     Load code segment descriptor into CS register;
  14140.     Load CS with new code segment selector;
  14141.     Load EIP with zero-extend(new offset);
  14142.     IF OperandSize=16 THEN EIP  EIP AND 0000FFFFH; FI;
  14143.  
  14144.  NONCONFORMING-CODE-SEGMENT:
  14145.     RPL must be ≤ CPL ELSE #GP(code segment selector)
  14146.     DPL must be = CPL ELSE #GP(code segment selector)
  14147.     Segment must be present ELSE #NP(code segment selector)
  14148.     Stack must be big enough for return address ELSE #SS(0)
  14149.     Instruction pointer must be in code segment limit ELSE #GP(0)
  14150.     Load code segment descriptor into CS register
  14151.     Load CS with new code segment selector
  14152.     Set RPL of CS to CPL
  14153.     Load EIP with zero-extend(new offset);
  14154.     IF OperandSize=16 THEN EIP  EIP AND 0000FFFFH; FI;
  14155.  
  14156.  CALL-GATE:
  14157.     Call gate DPL must be ≥ CPL ELSE #GP(call gate selector)
  14158.     Call gate DPL must be ≥ RPL ELSE #GP(call gate selector)
  14159.     Call gate must be present ELSE #NP(call gate selector)
  14160.     Examine code segment selector in call gate descriptor:
  14161.        Selector must not be null ELSE #GP(0)
  14162.        Selector must be within its descriptor table
  14163.           limits ELSE #GP(code segment selector)
  14164.     AR byte of selected descriptor must indicate code
  14165.        segment ELSE #GP(code segment selector)
  14166.     DPL of selected descriptor must be ≤ CPL ELSE
  14167.        #GP(code segment selector)
  14168.     IF non-conforming code segment AND DPL < CPL
  14169.     THEN go to MORE-PRIVILEGE
  14170.     ELSE go to SAME-PRIVILEGE
  14171.     FI;
  14172.  
  14173.  MORE-PRIVILEGE:
  14174.     Get new SS selector for new privilege level from TSS
  14175.        Check selector and descriptor for new SS:
  14176.           Selector must not be null ELSE #TS(0)
  14177.           Selector index must be within its descriptor
  14178.              table limits ELSE #TS(SS selector)
  14179.           Selector's RPL must equal DPL of code segment
  14180.              ELSE #TS(SS selector)
  14181.           Stack segment DPL must equal DPL of code
  14182.              segment ELSE #TS(SS selector)
  14183.           Descriptor must indicate writable data segment
  14184.              ELSE #TS(SS selector)
  14185.           Segment present ELSE #SS(SS selector)
  14186.        IF OperandSize=32
  14187.        THEN
  14188.           New stack must have room for parameters plus 16 bytes
  14189.              ELSE #SS(0)
  14190.           EIP must be in code segment limit ELSE #GP(0)
  14191.           Load new SS:eSP value from TSS
  14192.           Load new CS:EIP value from gate
  14193.        ELSE
  14194.           New stack must have room for parameters plus 8 bytes ELSE #SS(0)
  14195.           IP must be in code segment limit ELSE #GP(0)
  14196.           Load new SS:eSP value from TSS
  14197.           Load new CS:IP value from gate
  14198.        FI;
  14199.        Load CS descriptor
  14200.        Load SS descriptor
  14201.        Push long pointer of old stack onto new stack
  14202.        Get word count from call gate, mask to 5 bits
  14203.        Copy parameters from old stack onto new stack
  14204.        Push return address onto new stack
  14205.        Set CPL to stack segment DPL
  14206.        Set RPL of CS to CPL
  14207.  
  14208.  SAME-PRIVILEGE:
  14209.     IF OperandSize=32
  14210.     THEN
  14211.        Stack must have room for 6-byte return address (padded to 8 bytes)
  14212.           ELSE #SS(0)
  14213.        EIP must be within code segment limit ELSE #GP(0)
  14214.        Load CS:EIP from gate
  14215.     ELSE
  14216.        Stack must have room for 4-byte return address ELSE #SS(0)
  14217.        IP must be within code segment limit ELSE #GP(0)
  14218.        Load CS:IP from gate
  14219.     FI;
  14220.     Push return address onto stack
  14221.     Load code segment descriptor into CS register
  14222.     Set RPL of CS to CPL
  14223.  
  14224.  TASK-GATE:
  14225.     Task gate DPL must be ≥ CPL ELSE #TS(gate selector)
  14226.     Task gate DPL must be ≥ RPL ELSE #TS(gate selector)
  14227.     Task Gate must be present ELSE #NP(gate selector)
  14228.     Examine selector to TSS, given in Task Gate descriptor:
  14229.        Must specify global in the local/global bit ELSE #TS(TSS selector)
  14230.        Index must be within GDT limits ELSE #TS(TSS selector)
  14231.        TSS descriptor AR byte must specify nonbusy TSS
  14232.           ELSE #TS(TSS selector)
  14233.        Task State Segment must be present ELSE #NP(TSS selector)
  14234.     SWITCH-TASKS (with nesting) to TSS
  14235.     IP must be in code segment limit ELSE #TS(0)
  14236.  
  14237.  TASK-STATE-SEGMENT:
  14238.     TSS DPL must be ≥ CPL else #TS(TSS selector)
  14239.     TSS DPL must be ≥ RPL ELSE #TS(TSS selector)
  14240.     TSS descriptor AR byte must specify available TSS
  14241.        ELSE #TS(TSS selector)
  14242.     Task State Segment must be present ELSE #NP(TSS selector)
  14243.     SWITCH-TASKS (with nesting) to TSS
  14244.     IP must be in code segment limit ELSE #TS(0)
  14245.  
  14246.  Description
  14247.  
  14248.  The CALL instruction causes the procedure named in the operand to be
  14249.  executed. When the procedure is complete (a return instruction is executed
  14250.  within the procedure), execution continues at the instruction that follows
  14251.  the CALL instruction.
  14252.  
  14253.  The action of the different forms of the instruction are described below.
  14254.  
  14255.  Near calls are those with destinations of type r/m16, r/m32, rel16, rel32;
  14256.  changing or saving the segment register value is not necessary. The CALL
  14257.  rel16 and CALL rel32 forms add a signed offset to the address of the
  14258.  instruction following CALL to determine the destination. The rel16 form is
  14259.  used when the instruction's operand-size attribute is 16 bits; rel32 is used
  14260.  when the operand-size attribute is 32 bits. The result is stored in the
  14261.  32-bit EIP register. With rel16, the upper 16 bits of EIP are cleared,
  14262.  resulting in an offset whose value does not exceed 16 bits. CALL r/m16 and
  14263.  CALL r/m32 specify a register or memory location from which the absolute
  14264.  segment offset is fetched. The offset fetched from r/m is 32 bits for an
  14265.  operand-size attribute of 32 (r/m32), or 16 bits for an operand-size of 16
  14266.  (r/m16). The offset of the instruction following CALL is pushed onto the
  14267.  stack. It will be popped by a near RET instruction within the procedure. The
  14268.  CS register is not changed by this form of CALL.
  14269.  
  14270.  The far calls, CALL ptr16:16 and CALL ptr16:32, use a four-byte or six-byte
  14271.  operand as a long pointer to the procedure called. The CALL m16:16 and
  14272.  m16:32 forms fetch the long pointer from the memory location
  14273.  specified (indirection). In Real Address Mode or Virtual 8086 Mode, the long
  14274.  pointer provides 16 bits for the CS register and 16 or 32 bits for the EIP
  14275.  register (depending on the operand-size attribute). These forms of the
  14276.  instruction push both CS and IP or EIP as a return address.
  14277.  
  14278.  In Protected Mode, both long pointer forms consult the AR byte in the
  14279.  descriptor indexed by the selector part of the long pointer. Depending on
  14280.  the value of the AR byte, the call will perform one of the following types
  14281.  of control transfers:
  14282.  
  14283.    ■  A far call to the same protection level
  14284.    ■  An inter-protection level far call
  14285.    ■  A task switch
  14286.  
  14287.  For more information on Protected Mode control transfers, refer to
  14288.  Chapter 6 and Chapter 7.
  14289.  
  14290.  Flags Affected
  14291.  
  14292.  All flags are affected if a task switch occurs; no flags are affected if a
  14293.  task switch does not occur
  14294.  
  14295.  Protected Mode Exceptions
  14296.  
  14297.  For far calls: #GP, #NP, #SS, and #TS, as indicated in the list above
  14298.  
  14299.  For near direct calls: #GP(0) if procedure location is beyond the code
  14300.  segment limits; #SS(0) if pushing the return address exceeds the bounds of
  14301.  the stack segment; #PF (fault-code) for a page fault
  14302.  
  14303.  For a near indirect call: #GP(0) for an illegal memory operand effective
  14304.  address in the CS, DS, ES, FS, or GS segments; #SS(0) for an illegal address
  14305.  in the SS segment; #GP(0) if the indirect offset obtained is beyond the code
  14306.  segment limits; #PF(fault-code) for a page fault
  14307.  
  14308.  Real Address Mode Exceptions
  14309.  
  14310.  Interrupt 13 if any part of the operand would lie outside of the effective
  14311.  address space from 0 to 0FFFFH
  14312.  
  14313.  Virtual 8086 Mode Exceptions
  14314.  
  14315.  Same exceptions as in Real Address Mode; #PF(fault-code) for a page fault
  14316.  
  14317.  Notes
  14318.  
  14319.  Any far call from a 32-bit code segment to 16-bit code segments should be
  14320.  made from the first 64K bytes of the 32-bit code segment, since the
  14321.  operand-size attribute of the instruction is set to 16, thus allowing only a
  14322.  16-bit return address offset to be saved.
  14323.  
  14324.  
  14325.  CBW/CWDE ── Convert Byte to Word/Convert Word to Doubleword
  14326.  
  14327.  Opcode    Instruction     Clocks          Description
  14328.  
  14329.  98        CBW             3               AX  sign-extend of AL
  14330.  98        CWDE            3               EAX  sign-extend of AX
  14331.  
  14332.  
  14333.  Operation
  14334.  
  14335.  IF OperandSize = 16 (* instruction = CBW *)
  14336.  THEN AX  SignExtend(AL);
  14337.  ELSE (* OperandSize = 32, instruction = CWDE *)
  14338.     EAX  SignExtend(AX);
  14339.  FI;
  14340.  
  14341.  Description
  14342.  
  14343.  CBW converts the signed byte in AL to a signed word in AX by extending the
  14344.  most significant bit of AL (the sign bit) into all of the bits of AH. CWDE
  14345.  converts the signed word in AX to a doubleword in EAX by extending the most
  14346.  significant bit of AX into the two most significant bytes of EAX. Note that
  14347.  CWDE is different from CWD. CWD uses DX:AX rather than EAX as a destination.
  14348.  
  14349.  Flags Affected
  14350.  
  14351.  None
  14352.  
  14353.  Protected Mode Exceptions
  14354.  
  14355.  None
  14356.  
  14357.  Real Address Mode Exceptions
  14358.  
  14359.  None
  14360.  
  14361.  Virtual 8086 Mode Exceptions
  14362.  
  14363.  None
  14364.  
  14365.  
  14366.  CLC ── Clear Carry Flag
  14367.  
  14368.  Opcode    Instruction     Clocks          Description
  14369.  
  14370.  F8        CLC             2               Clear carry flag
  14371.  
  14372.  
  14373.  Operation
  14374.  
  14375.  CF  0;
  14376.  
  14377.  Description
  14378.  
  14379.  CLC sets the carry flag to zero. It does not affect other flags or
  14380.  registers.
  14381.  
  14382.  Flags Affected
  14383.  
  14384.  CF = 0
  14385.  
  14386.  Protected Mode Exceptions
  14387.  
  14388.  None
  14389.  
  14390.  Real Address Mode Exceptions
  14391.  
  14392.  None
  14393.  
  14394.  Virtual 8086 Mode Exceptions
  14395.  
  14396.  None
  14397.  
  14398.  
  14399.  CLD ── Clear Direction Flag
  14400.  
  14401.  Opcode    Instruction     Clocks   Description
  14402.  
  14403.  FC        CLD             2        Clear direction flag; SI and DI
  14404.                                     will increment during string
  14405.                                     instructions
  14406.  
  14407.  
  14408.  Operation
  14409.  
  14410.  DF  0;
  14411.  
  14412.  Description
  14413.  
  14414.  CLD clears the direction flag. No other flags or registers are affected.
  14415.  After CLD is executed, string operations will increment the index registers
  14416.  (SI and/or DI) that they use.
  14417.  
  14418.  Flags Affected
  14419.  
  14420.  DF = 0
  14421.  
  14422.  Protected Mode Exceptions
  14423.  
  14424.  None
  14425.  
  14426.  Real Address Mode Exceptions
  14427.  
  14428.  None
  14429.  
  14430.  Virtual 8086 Mode Exceptions
  14431.  
  14432.  None
  14433.  
  14434.  
  14435.  CLI ── Clear Interrupt Flag
  14436.  
  14437.  Opcode    Instruction    Clocks   Description
  14438.  
  14439.  FA        CLI            3        Clear interrupt flag; interrupts disabled
  14440.  
  14441.  
  14442.  Operation
  14443.  
  14444.  IF  0;
  14445.  
  14446.  Description
  14447.  
  14448.  CLI clears the interrupt flag if the current privilege level is at least as
  14449.  privileged as IOPL. No other flags are affected. External interrupts are not
  14450.  recognized at the end of the CLI instruction or from that point on until the
  14451.  interrupt flag is set.
  14452.  
  14453.  Flags Affected
  14454.  
  14455.  IF = 0
  14456.  
  14457.  Protected Mode Exceptions
  14458.  
  14459.  #GP(0) if the current privilege level is greater (has less privilege) than
  14460.  the IOPL in the flags register. IOPL specifies the least privileged level at
  14461.  which I/O can be performed.
  14462.  
  14463.  Real Address Mode Exceptions
  14464.  
  14465.  None
  14466.  
  14467.  Virtual 8086 Mode Exceptions
  14468.  
  14469.  #GP(0) as for Protected Mode
  14470.  
  14471.  
  14472.  CLTS ── Clear Task-Switched Flag in CR0
  14473.  
  14474.  Opcode    Instruction    Clocks   Description
  14475.  
  14476.  OF  06    CLTS           5        Clear task-switched flag
  14477.  
  14478.  
  14479.  Operation
  14480.  
  14481.  TS Flag in CR0  0;
  14482.  
  14483.  Description
  14484.  
  14485.  CLTS clears the task-switched (TS) flag in register CR0. This flag is set by
  14486.  the 80386 every time a task switch occurs. The TS flag is used to manage
  14487.  processor extensions as follows:
  14488.  
  14489.    ■  Every execution of an ESC instruction is trapped if the TS flag is set.
  14490.  
  14491.    ■  Execution of a WAIT instruction is trapped if the MP flag and the TS
  14492.       flag are both set.
  14493.  
  14494.  Thus, if a task switch was made after an ESC instruction was begun, the
  14495.  processor extension's context may need to be saved before a new ESC
  14496.  instruction can be issued. The fault handler saves the context and resets
  14497.  the TS flag.
  14498.  
  14499.  CLTS appears in operating system software, not in application programs. It
  14500.  is a privileged instruction that can only be executed at privilege level 0.
  14501.  
  14502.  Flags Affected
  14503.  
  14504.  TS = 0 (TS is in CR0, not the flag register)
  14505.  
  14506.  Protected Mode Exceptions
  14507.  
  14508.  #GP(0) if CLTS is executed with a current privilege level other than 0
  14509.  
  14510.  Real Address Mode Exceptions
  14511.  
  14512.  None (valid in Real Address Mode to allow initialization for Protected
  14513.  Mode)
  14514.  
  14515.  Virtual 8086 Mode Exceptions
  14516.  
  14517.  Same exceptions as in Real Address Mode
  14518.  
  14519.  
  14520.  CMC ── Complement Carry Flag
  14521.  
  14522.  Opcode    Instruction    Clocks   Description
  14523.  
  14524.  F5        CMC            2        Complement carry flag
  14525.  
  14526.  
  14527.  Operation
  14528.  
  14529.  CF  NOT CF;
  14530.  
  14531.  Description
  14532.  
  14533.  CMC reverses the setting of the carry flag. No other flags are affected.
  14534.  
  14535.  Flags Affected
  14536.  
  14537.  CF as described above
  14538.  
  14539.  Protected Mode Exceptions
  14540.  
  14541.  None
  14542.  
  14543.  Real Address Mode Exceptions
  14544.  
  14545.  None
  14546.  
  14547.  Virtual 8086 Mode Exceptions
  14548.  
  14549.  None
  14550.  
  14551.  
  14552.  CMP ── Compare Two Operands
  14553.  
  14554. ╓┌───────────────┌──────────────────┌────────┌───────────────────────────────╖
  14555.  Opcode          Instruction        Clocks   Description
  14556.  
  14557.  3C  ib          CMP AL,imm8        2        Compare immediate byte to AL
  14558.  3D  iw          CMP AX,imm16       2        Compare immediate word to AX
  14559.  3D  id          CMP EAX,imm32      2        Compare immediate dword to EAX
  14560.  80  /7 ib       CMP r/m8,imm8      2/5      Compare immediate byte to r/m
  14561.                                              byte
  14562.  81  /7 iw       CMP r/m16,imm16    2/5      Compare immediate word to r/m
  14563.                                              word
  14564.  81  /7 id       CMP r/m32,imm32    2/5      Compare immediate dword to r/m
  14565.                                              dword
  14566.  83  /7 ib       CMP r/m16,imm8     2/5      Compare sign extended immediate
  14567.                                              byte to r/m word
  14568.  83  /7 ib       CMP r/m32,imm8     2/5      Compare sign extended immediate
  14569.                                              byte to r/m dword
  14570.  38  /r          CMP r/m8,r8        2/5      Compare byte register to r/m
  14571.                                              byte
  14572.  39  /r          CMP r/m16,r16      2/5      Compare word register to r/m
  14573.                                              word
  14574.  39  /r          CMP r/m32,r32      2/5      Compare dword register to r/m
  14575.                                              dword
  14576.  Opcode          Instruction        Clocks   Description
  14577.                                             dword
  14578.  3A  /r          CMP r8,r/m8        2/6      Compare r/m byte to byte
  14579.                                              register
  14580.  3B  /r          CMP r16,r/m16      2/6      Compare r/m word to word
  14581.                                              register
  14582.  3B  /r          CMP r32,r/m32      2/6      Compare r/m dword to dword
  14583.                                              register
  14584.  
  14585.  
  14586.  Operation
  14587.  
  14588.  LeftSRC - SignExtend(RightSRC);
  14589.  (* CMP does not store a result; its purpose is to set the flags *)
  14590.  
  14591.  Description
  14592.  
  14593.  CMP subtracts the second operand from the first but, unlike the SUB
  14594.  instruction, does not store the result; only the flags are changed. CMP is
  14595.  typically used in conjunction with conditional jumps and the SETcc
  14596.  instruction. (Refer to Appendix D for the list of signed and unsigned flag
  14597.  tests provided.) If an operand greater than one byte is compared to an
  14598.  immediate byte, the byte value is first sign-extended.
  14599.  
  14600.  Flags Affected
  14601.  
  14602.  OF, SF, ZF, AF, PF, and CF as described in Appendix C
  14603.  
  14604.  Protected Mode Exceptions
  14605.  
  14606.  #GP(0) for an illegal memory operand effective address in the CS, DS, ES,
  14607.  FS, or GS segments; #SS(0) for an illegal address in the SS segment;
  14608.  #PF(fault-code) for a page fault
  14609.  
  14610.  Real Address Mode Exceptions
  14611.  
  14612.  Interrupt 13 if any part of the operand would lie outside of the effective
  14613.  address space from 0 to 0FFFFH
  14614.  
  14615.  Virtual 8086 Mode Exceptions
  14616.  
  14617.  Same exceptions as in Real Address Mode; #PF(fault-code) for a page fault
  14618.  
  14619.  
  14620.  CMPS/CMPSB/CMPSW/CMPSD ── Compare String Operands
  14621.  
  14622.  Opcode    Instruction        Clocks   Description
  14623.  
  14624.  A6        CMPS m8,m8         10       Compare bytes ES:[(E)DI] (second
  14625.                                        operand) with   [(E)SI] (first
  14626.                                        operand)
  14627.  A7        CMPS m16,m16       10       Compare words ES:[(E)DI] (second
  14628.                                        operand) with   [(E)SI] (first
  14629.                                        operand)
  14630.  A7        CMPS m32,m32       10       Compare dwords ES:[(E)DI]
  14631.                                        (second operand) with [(E)SI]
  14632.                                        (first operand)
  14633.  A6        CMPSB              10       Compare bytes ES:[(E)DI] with
  14634.                                        DS:[SI]
  14635.  A7        CMPSW              10       Compare words ES:[(E)DI] with
  14636.                                        DS:[SI]
  14637.  A7        CMPSD              10       Compare dwords ES:[(E)DI] with
  14638.                                        DS:[SI]
  14639.  
  14640.  
  14641.  Operation
  14642.  
  14643.  IF (instruction = CMPSD) OR
  14644.     (instruction has operands of type DWORD)
  14645.  THEN OperandSize  32;
  14646.  ELSE OperandSize  16;
  14647.  FI;
  14648.  IF AddressSize = 16
  14649.  THEN
  14650.     use SI for source-index and DI for destination-index
  14651.  ELSE (* AddressSize = 32 *)
  14652.     use ESI for source-index and EDI for destination-index;
  14653.  FI;
  14654.  IF byte type of instruction
  14655.  THEN
  14656.     [source-index] - [destination-index]; (* byte comparison *)
  14657.     IF DF = 0 THEN IncDec  1 ELSE IncDec  -1; FI;
  14658.  ELSE
  14659.     IF OperandSize = 16
  14660.     THEN
  14661.        [source-index] - [destination-index]; (* word comparison *)
  14662.        IF DF = 0 THEN IncDec  2 ELSE IncDec  -2; FI;
  14663.     ELSE (* OperandSize = 32 *)
  14664.        [source-index] - [destination-index]; (* dword comparison *)
  14665.        IF DF = 0 THEN IncDec  4 ELSE IncDec  -4; FI;
  14666.     FI;
  14667.  FI;
  14668.  source-index = source-index + IncDec;
  14669.  destination-index = destination-index + IncDec;
  14670.  
  14671.  Description
  14672.  
  14673.  CMPS compares the byte, word, or doubleword pointed to by the source-index
  14674.  register with the byte, word, or doubleword pointed to by the
  14675.  destination-index register.
  14676.  
  14677.  If the address-size attribute of this instruction is 16 bits, SI and DI
  14678.  will be used for source- and destination-index registers; otherwise ESI and
  14679.  EDI will be used. Load the correct index values into SI and DI (or ESI and
  14680.  EDI) before executing CMPS.
  14681.  
  14682.  The comparison is done by subtracting the operand indexed by
  14683.  the destination-index register from the operand indexed by the source-index
  14684.  register.
  14685.  
  14686.  Note that the direction of subtraction for CMPS is [SI] - [DI] or
  14687.  [ESI] - [EDI]. The left operand (SI or ESI) is the source and the right
  14688.  operand (DI or EDI) is the destination. This is the reverse of the usual
  14689.  Intel convention in which the left operand is the destination and the right
  14690.  operand is the source.
  14691.  
  14692.  The result of the subtraction is not stored; only the flags reflect the
  14693.  change. The types of the operands determine whether bytes, words, or
  14694.  doublewords are compared. For the first operand (SI or ESI), the DS register
  14695.  is used, unless a segment override byte is present. The second operand (DI
  14696.  or EDI) must be addressable from the ES register; no segment override is
  14697.  possible.
  14698.  
  14699.  After the comparison is made, both the source-index register and
  14700.  destination-index register are automatically advanced. If the direction flag
  14701.  is 0 (CLD was executed), the registers increment; if the direction flag is 1
  14702.  (STD was executed), the registers decrement. The registers increment or
  14703.  decrement by 1 if a byte is compared, by 2 if a word is compared, or by 4 if
  14704.  a doubleword is compared.
  14705.  
  14706.  CMPSB, CMPSW and CMPSD are synonyms for the byte, word, and
  14707.  doubleword CMPS instructions, respectively.
  14708.  
  14709.  CMPS can be preceded by the REPE or REPNE prefix for block comparison of CX
  14710.  or ECX bytes, words, or doublewords. Refer to the description of the REP
  14711.  instruction for more information on this operation.
  14712.  
  14713.  Flags Affected
  14714.  
  14715.  OF, SF, ZF, AF, PF, and CF as described in Appendix C
  14716.  
  14717.  Protected Mode Exceptions
  14718.  
  14719.  #GP(0) for an illegal memory operand effective address in the CS, DS, ES,
  14720.  FS, or GS segments; #SS(0) for an illegal address in the SS segment;
  14721.  #PF(fault-code) for a page fault
  14722.  
  14723.  Real Address Mode Exceptions
  14724.  
  14725.  Interrupt 13 if any part of the operand would lie outside of the effective
  14726.  address space from 0 to 0FFFFH
  14727.  
  14728.  Virtual 8086 Mode Exceptions
  14729.  
  14730.  Same exceptions as in Real Address Mode; #PF (fault-code) for a page fault
  14731.  
  14732.  
  14733.  CWD/CDQ ── Convert Word to Doubleword/Convert Doubleword to
  14734.             Quadword
  14735.  
  14736.  Opcode    Instruction        Clocks   Description
  14737.  
  14738.  99        CWD                2        DX:AX  sign-extend of AX
  14739.  99        CDQ                2        EDX:EAX  sign-extend of EAX
  14740.  
  14741.  
  14742.  Operation
  14743.  
  14744.  IF OperandSize = 16 (* CWD instruction *)
  14745.  THEN
  14746.     IF AX < 0 THEN DX  0FFFFH; ELSE DX  0; FI;
  14747.  ELSE (* OperandSize = 32, CDQ instruction *)
  14748.     IF EAX < 0 THEN EDX  0FFFFFFFFH; ELSE EDX  0; FI;
  14749.  FI;
  14750.  
  14751.  Description
  14752.  
  14753.  CWD converts the signed word in AX to a signed doubleword in DX:AX
  14754.  by extending the most significant bit of AX into all the bits of DX. CDQ
  14755.  converts the signed doubleword in EAX to a signed 64-bit integer in the
  14756.  register pair EDX:EAX by extending the most significant bit of EAX
  14757.  (the sign bit) into all the bits of EDX. Note that CWD is different from
  14758.  CWDE. CWDE uses EAX as a destination, instead of DX:AX.
  14759.  
  14760.  Flags Affected
  14761.  
  14762.  None
  14763.  
  14764.  Protected Mode Exceptions
  14765.  
  14766.  None
  14767.  
  14768.  Real Address Mode Exceptions
  14769.  
  14770.  None
  14771.  
  14772.  Virtual 8086 Mode Exceptions
  14773.  
  14774.  None
  14775.  
  14776.  
  14777.  DAA ── Decimal Adjust AL after Addition
  14778.  
  14779.  Opcode    Instruction        Clocks   Description
  14780.  
  14781.  27        DAA                4        Decimal adjust AL after addition
  14782.  
  14783.  
  14784.  Operation
  14785.  
  14786.  IF ((AL AND 0FH) > 9) OR (AF = 1)
  14787.  THEN
  14788.     AL  AL + 6;
  14789.     AF  1;
  14790.  ELSE
  14791.     AF  0;
  14792.  FI;
  14793.  IF (AL > 9FH) OR (CF = 1)
  14794.  THEN
  14795.     AL  AL + 60H;
  14796.     CF  1;
  14797.  ELSE CF  0;
  14798.  FI;
  14799.  
  14800.  Description
  14801.  
  14802.  Execute DAA only after executing an ADD instruction that leaves a
  14803.  two-BCD-digit byte result in the AL register. The ADD operands should
  14804.  consist of two packed BCD digits. The DAA instruction adjusts AL to
  14805.  contain the correct two-digit packed decimal result.
  14806.  
  14807.  Flags Affected
  14808.  
  14809.  AF and CF as described above; SF, ZF, PF, and CF as described in
  14810.  Appendix C.
  14811.  
  14812.  Protected Mode Exceptions
  14813.  
  14814.  None
  14815.  
  14816.  Real Address Mode Exceptions
  14817.  
  14818.  None
  14819.  
  14820.  Virtual 8086 Mode Exceptions
  14821.  
  14822.  None
  14823.  
  14824.  
  14825.  DAS ── Decimal Adjust AL after Subtraction
  14826.  
  14827.  Opcode    Instruction        Clocks   Description
  14828.  
  14829.  2F        DAS                4        Decimal adjust AL after subtraction
  14830.  
  14831.  
  14832.  Operation
  14833.  
  14834.  IF (AL AND 0FH) > 9 OR AF = 1
  14835.  THEN
  14836.     AL  AL - 6;
  14837.     AF  1;
  14838.  ELSE
  14839.     AF  0;
  14840.  FI;
  14841.  IF (AL > 9FH) OR (CF = 1)
  14842.  THEN
  14843.     AL  AL - 60H;
  14844.     CF  1;
  14845.  ELSE CF  0;
  14846.  FI;
  14847.  
  14848.  Description
  14849.  
  14850.  Execute DAS only after a subtraction instruction that leaves a
  14851.  two-BCD-digit byte result in the AL register. The operands should consist
  14852.  of two packed BCD digits. DAS adjusts AL to contain the correct packed
  14853.  two-digit decimal result.
  14854.  
  14855.  Flags Affected
  14856.  
  14857.  AF and CF as described above; SF, ZF, and PF as described in Appendix C.
  14858.  
  14859.  Protected Mode Exceptions
  14860.  
  14861.  None
  14862.  
  14863.  Real Address Mode Exceptions
  14864.  
  14865.  None
  14866.  
  14867.  Virtual 8086 Mode Exceptions
  14868.  
  14869.  None
  14870.  
  14871.  
  14872.  DEC ── Decrement by 1
  14873.  
  14874.  Opcode    Instruction        Clocks   Description
  14875.  
  14876.  FE /1     DEC r/m8           2/6      Decrement r/m byte by 1
  14877.  FF /1     DEC r/m16          2/6      Decrement r/m word by 1
  14878.            DEC r/m32          2/6      Decrement r/m dword by 1
  14879.  48+rw     DEC r16            2        Decrement word register by 1
  14880.  48+rw     DEC r32            2        Decrement dword register by 1
  14881.  
  14882.  
  14883.  Operation
  14884.  
  14885.  DEST  DEST - 1;
  14886.  
  14887.  Description
  14888.  
  14889.  DEC subtracts 1 from the operand. DEC does not change the carry flag.
  14890.  To affect the carry flag, use the SUB instruction with an immediate
  14891.  operand of 1.
  14892.  
  14893.  Flags Affected
  14894.  
  14895.  OF, SF, ZF, AF, and PF as described in Appendix C.
  14896.  
  14897.  Protected Mode Exceptions
  14898.  
  14899.  #GP(0) if the result is a nonwritable segment; #GP(0) for an illegal
  14900.  memory operand effective address in the CS, DS, ES, FS, or GS
  14901.  segments; #SS(0) for an illegal address in the SS segment; #PF(fault-code)
  14902.  for a page fault
  14903.  
  14904.  Real Address Mode Exceptions
  14905.  
  14906.  Interrupt 13 if any part of the operand would lie outside of the effective
  14907.  address space from 0 to 0FFFFH
  14908.  
  14909.  Virtual 8086 Mode Exceptions
  14910.  
  14911.  Same exceptions as in Real Address Mode; #PF(fault-code) for a page
  14912.  fault
  14913.  
  14914.  
  14915.  DIV ── Unsigned Divide
  14916.  
  14917.  Opcode    Instruction        Clocks   Description
  14918.  
  14919.  F6 /6     DIV AL,r/m8        14/17    Unsigned divide AX by r/m byte
  14920.                                        (AL=Quo, AH=Rem)
  14921.  F7 /6     DIV AX,r/m16       22/25    Unsigned divide DX:AX by r/m
  14922.                                        word (AX=Quo, DX=Rem)
  14923.  F7 /6     DIV EAX,r/m32      38/41    Unsigned divide EDX:EAX by r/m
  14924.                                        dword (EAX=Quo, EDX=Rem)
  14925.  
  14926.  
  14927.  Operation
  14928.  
  14929.  temp  dividend / divisor;
  14930.  IF temp does not fit in quotient
  14931.  THEN Interrupt 0;
  14932.  ELSE
  14933.     quotient  temp;
  14934.     remainder  dividend MOD (r/m);
  14935.  FI;
  14936.  
  14937.  ───────────────────────────────────────────────────────────────────────────
  14938.  Note:
  14939.    Divisions are unsigned. The divisor is given by the r/m operand.
  14940.    The dividend, quotient, and remainder use implicit registers. Refer to
  14941.    the table under "Description."
  14942.  ───────────────────────────────────────────────────────────────────────────
  14943.  
  14944.  Description
  14945.  
  14946.  DIV performs an unsigned division. The dividend is implicit; only the
  14947.  divisor is given as an operand. The remainder is always less than the
  14948.  divisor. The type of the divisor determines which registers to use as
  14949.  follows:
  14950.  
  14951.      Size    Dividend     Divisor   Quotient   Remainder
  14952.      byte    AX           r/m8       AL          AH
  14953.      word    DX:AX        r/m16      AX          DX
  14954.      dword   EDX:EAX      r/m32      EAX         EDX
  14955.  
  14956.  Flags Affected
  14957.  
  14958.  OF, SF, ZF, AR, PF, CF are undefined.
  14959.  
  14960.  Protected Mode Exceptions
  14961.  
  14962.  Interrupt 0 if the quotient is too large to fit in the designated register
  14963.  (AL, AX, or EAX), or if the divisor is 0; #GP(0) for an illegal memory
  14964.  operand effective address in the CS, DS, ES, FS, or GS segments; #SS(0)
  14965.  for an illegal address in the SS segment; #PF(fault-code) for a page fault
  14966.  
  14967.  Real Address Mode Exceptions
  14968.  
  14969.  Interrupt 0 if the quotient is too big to fit in the designated register
  14970.  (AL, AX, or EAX), or if the divisor is 0; Interrupt 13 if any part of the
  14971.  operand would lie outside of the effective address space from 0 to 0FFFFH
  14972.  
  14973.  Virtual 8086 Mode Exceptions
  14974.  
  14975.  Same exceptions as in Real Address Mode; #PF(fault-code) for a page
  14976.  fault
  14977.  
  14978.  
  14979.  ENTER ── Make Stack Frame for Procedure Parameters
  14980.  
  14981.  Opcode      Instruction        Clocks     Description
  14982.  
  14983.  C8 iw 00    ENTER imm16,0      10         Make procedure stack frame
  14984.  C8 iw 01    ENTER imm16,1      12         Make stack frame for procedure
  14985.                                            parameters
  14986.  C8 iw ib    ENTER imm16,imm8   15+4(n-1)  Make stack frame for
  14987.                                            procedure parameters
  14988.  
  14989.  
  14990.  Operation
  14991.  
  14992.  level  level MOD 32
  14993.  IF OperandSize = 16 THEN Push(BP) ELSE Push (EBP) FI;
  14994.     (* Save stack pointer *)
  14995.  frame-ptr  eSP
  14996.  IF level > 0
  14997.  THEN (* level is rightmost parameter *)
  14998.     FOR i  1 TO level - 1
  14999.     DO
  15000.        IF OperandSize = 16
  15001.        THEN
  15002.           BP  BP - 2;
  15003.           Push[BP]
  15004.        ELSE (* OperandSize = 32 *)
  15005.           EBP  EBP - 4;
  15006.           Push[EBP];
  15007.        FI;
  15008.     OD;
  15009.     Push(frame-ptr)
  15010.  FI;
  15011.  IF OperandSize = 16 THEN BP  frame-ptr ELSE EBP  frame-ptr; FI;
  15012.  IF StackAddrSize = 16
  15013.  THEN SP  SP - First operand;
  15014.  ELSE ESP  ESP - ZeroExtend(First operand);
  15015.  FI;
  15016.  
  15017.  Description
  15018.  
  15019.  ENTER creates the stack frame required by most block-structured
  15020.  high-level languages. The first operand specifies the number of bytes of
  15021.  dynamic storage allocated on the stack for the routine being entered.
  15022.  The second operand gives the lexical nesting level (0 to 31) of the routine
  15023.  within the high-level language source code. It determines the number of
  15024.  stack frame pointers copied into the new stack frame from the preceding
  15025.  frame. BP (or EBP, if the operand-size attribute is 32 bits) is the current
  15026.  stack frame pointer.
  15027.  
  15028.  If the operand-size attribute is 16 bits, the processor uses BP as the
  15029.  frame pointer and SP as the stack pointer. If the operand-size attribute is
  15030.  32 bits, the processor uses EBP for the frame pointer and ESP for the stack
  15031.  pointer.
  15032.  
  15033.  If the second operand is 0, ENTER pushes the frame pointer (BP or
  15034.  EBP) onto the stack; ENTER then subtracts the first operand from the
  15035.  stack pointer and sets the frame pointer to the current stack-pointer
  15036.  value.
  15037.  
  15038.  For example, a procedure with 12 bytes of local variables would have an
  15039.  ENTER 12,0 instruction at its entry point and a LEAVE instruction
  15040.  before every RET. The 12 local bytes would be addressed as negative
  15041.  offsets from the frame pointer.
  15042.  
  15043.  Flags Affected
  15044.  
  15045.  None
  15046.  
  15047.  Protected Mode Exceptions
  15048.  
  15049.  #SS(0) if SP or ESP would exceed the stack limit at any point during
  15050.  instruction execution; #PF(fault-code) for a page fault
  15051.  
  15052.  Real Address Mode Exceptions
  15053.  
  15054.  None
  15055.  
  15056.  Virtual 8086 Mode Exceptions
  15057.  
  15058.  None
  15059.  
  15060.  
  15061.  HLT ── Halt
  15062.  
  15063.  Opcode      Instruction        Clocks     Description
  15064.  
  15065.  F4          HLT                5          Halt
  15066.  
  15067.  
  15068.  Operation
  15069.  
  15070.  Enter Halt state;
  15071.  
  15072.  Description
  15073.  
  15074.  HALT stops instruction execution and places the 80386 in a HALT state.
  15075.  An enabled interrupt, NMI, or a reset will resume execution. If an
  15076.  interrupt (including NMI) is used to resume execution after HLT, the saved
  15077.  CS:IP (or CS:EIP) value points to the instruction following HLT.
  15078.  
  15079.  Flags Affected
  15080.  
  15081.  None
  15082.  
  15083.  Protected Mode Exceptions
  15084.  
  15085.  HLT is a privileged instruction; #GP(0) if the current privilege level is
  15086.  not 0
  15087.  
  15088.  Real Address Mode Exceptions
  15089.  
  15090.  None
  15091.  
  15092.  Virtual 8086 Mode Exceptions
  15093.  
  15094.  #GP(0); HLT is a privileged instruction
  15095.  
  15096.  
  15097.  IDIV ── Signed Divide
  15098.  
  15099.  Opcode      Instruction        Clocks   Description
  15100.  
  15101.  F6 /7       IDIV r/m8          19       Signed divide AX by r/m byte
  15102.                                          (AL=Quo, AH=Rem)
  15103.  F7 /7       IDIV AX,r/m16      27       Signed divide DX:AX by EA word
  15104.                                          (AX=Quo, DX=Rem)
  15105.  F7 /7       IDIV EAX,r/m32     43       Signed divide EDX:EAX by DWORD
  15106.                                          byte (EAX=Quo, EDX=Rem)
  15107.  
  15108.  
  15109.  Operation
  15110.  
  15111.  temp  dividend / divisor;
  15112.  IF temp does not fit in quotient
  15113.  THEN Interrupt 0;
  15114.  ELSE
  15115.     quotient  temp;
  15116.     remainder  dividend MOD (r/m);
  15117.  FI;
  15118.  
  15119.  ───────────────────────────────────────────────────────────────────────────
  15120.  Notes:
  15121.    Divisions are signed. The divisor is given by the r/m operand. The
  15122.    dividend, quotient, and remainder use implicit registers. Refer to the
  15123.    table under "Description."
  15124.  ───────────────────────────────────────────────────────────────────────────
  15125.  
  15126.  Description
  15127.  
  15128.  IDIV performs a signed division. The dividend, quotient, and remainder
  15129.  are implicitly allocated to fixed registers. Only the divisor is given as
  15130.  an explicit r/m operand. The type of the divisor determines which registers
  15131.  to use as follows:
  15132.  
  15133.  Size     Divisor    Quotient    Remainder  Dividend
  15134.  byte     r/m8        AL           AH       AX
  15135.  word     r/m16       AX           DX       DX:AX
  15136.  dword    r/m32       EAX          EDX      EDX:EAX
  15137.  
  15138.  If the resulting quotient is too large to fit in the destination, or if the
  15139.  division is 0, an Interrupt 0 is generated. Nonintegral quotients are
  15140.  truncated toward 0. The remainder has the same sign as the dividend
  15141.  and the absolute value of the remainder is always less than the absolute
  15142.  value of the divisor.
  15143.  
  15144.  Flags Affected
  15145.  
  15146.  OF, SF, ZF, AR, PF, CF are undefined.
  15147.  
  15148.  Protected Mode Exceptions
  15149.  
  15150.  Interrupt 0 if the quotient is too large to fit in the designated register
  15151.  (AL or AX), or if the divisor is 0; #GP (0) for an illegal memory operand
  15152.  effective address in the CS, DS, ES, FS, or GS segments; #SS(0) for an
  15153.  illegal address in the SS segment; #PF(fault-code) for a page fault
  15154.  
  15155.  Real Address Mode Exceptions
  15156.  
  15157.  Interrupt 0 if the quotient is too large to fit in the designated register
  15158.  (AL or AX), or if the divisor is 0; Interrupt 13 if any part of the operand
  15159.  would lie outside of the effective address space from 0 to 0FFFFH
  15160.  
  15161.  Virtual 8086 Mode Exceptions
  15162.  
  15163.  Same exceptions as in Real Address Mode; #PF(fault-code) for a page
  15164.  fault
  15165.  
  15166.  
  15167.  IMUL ── Signed Multiply
  15168.  
  15169. ╓┌───────────┌──────────────────────┌───────────┌────────────────────────────╖
  15170.  Opcode      Instruction            Clocks      Description
  15171.  
  15172.  F6  /5      IMUL r/m8              9-14/12-17  AX AL * r/m byte
  15173.  F7  /5      IMUL r/m16             9-22/12-25  DX:AX  AX * r/m word
  15174.  F7  /5      IMUL r/m32             9-38/12-41  EDX:EAX  EAX * r/m dword
  15175.  0F  AF /r   IMUL r16,r/m16         9-22/12-25  word register  word
  15176.                                                 register * r/m word
  15177.  0F  AF /r   IMUL r32,r/m32         9-38/12-41  dword register  dword
  15178.                                                 register * r/m dword
  15179.  6B  /r ib   IMUL r16,r/m16,imm8    9-14/12-17  word register  r/m16 *
  15180.                                                 sign-extended immediate byte
  15181.  6B  /r ib   IMUL r32,r/m32,imm8    9-14/12-17  dword register  r/m32 *
  15182.                                                 sign-extended immediate byte
  15183.  6B  /r ib   IMUL r16,imm8          9-14/12-17  word register  word
  15184.                                                 register * sign-extended
  15185.  Opcode      Instruction            Clocks      Description
  15186.                                                register * sign-extended
  15187.                                                 immediate byte
  15188.  6B  /r ib   IMUL r32,imm8          9-14/12-17  dword register  dword
  15189.                                                 register * sign-extended
  15190.                                                 immediate byte
  15191.  69  /r iw   IMUL r16,r/m16,imm16   9-22/12-25  word register  r/m16 *
  15192.                                                 immediate word
  15193.  69  /r id   IMUL r32,r/m32,imm32   9-38/12-41  dword register  r/m32 *
  15194.                                                 immediate dword
  15195.  69  /r iw   IMUL r16,imm16         9-22/12-25  word register  r/m16 *
  15196.                                                 immediate word
  15197.  69  /r id   IMUL r32,imm32         9-38/12-41  dword register  r/m32 *
  15198.                                                 immediate dword
  15199.  
  15200.  
  15201.  ───────────────────────────────────────────────────────────────────────────
  15202.  NOTES:
  15203.    The 80386 uses an early-out multiply algorithm. The actual number of
  15204.    clocks depends on the position of the most significant bit in the
  15205.    optimizing multiplier, shown underlined above. The optimization occurs for
  15206.    positive and negative values. Because of the early-out algorithm, clock
  15207.    counts given are minimum to maximum. To calculate the actual clocks, use
  15208.    the following formula:
  15209.  ───────────────────────────────────────────────────────────────────────────
  15210.  
  15211.    Actual clock = if m <> 0 then max(ceiling(log{2} │m│), 3) + 6 clocks
  15212.    Actual clock = if m = 0 then 9 clocks
  15213.    (where m is the multiplier)
  15214.  
  15215.  Add three clocks if the multiplier is a memory operand.
  15216.  
  15217.  Operation
  15218.  
  15219.  result  multiplicand * multiplier;
  15220.  
  15221.  Description
  15222.  
  15223.  IMUL performs signed multiplication. Some forms of the instruction
  15224.  use implicit register operands. The operand combinations for all forms
  15225.  of the instruction are shown in the "Description" column above.
  15226.  
  15227.  IMUL clears the overflow and carry flags under the following conditions:
  15228.  
  15229.     Instruction Form    Condition for Clearing CF and OF
  15230.     r/m8                AL = sign-extend of AL to 16 bits
  15231.     r/m16               AX = sign-extend of AX to 32 bits
  15232.     r/m32               EDX:EAX = sign-extend of EAX to 32 bits
  15233.     r16,r/m16           Result exactly fits within r16
  15234.     r/32,r/m32          Result exactly fits within r32
  15235.     r16,r/m16,imm16     Result exactly fits within r16
  15236.     r32,r/m32,imm32     Result exactly fits within r32
  15237.  
  15238.  Flags Affected
  15239.  
  15240.  OF and CF as described above; SF, ZF, AF, and PF are undefined
  15241.  
  15242.  Protected Mode Exceptions
  15243.  
  15244.  #GP(0) for an illegal memory operand effective address in the CS, DS,
  15245.  ES, FS, or GS segments; #SS(0) for an illegal address in the SS segment;
  15246.  #PF(fault-code) for a page fault
  15247.  
  15248.  Real Address Mode Exceptions
  15249.  
  15250.  Interrupt 13 if any part of the operand would lie outside of the effective
  15251.  address space from 0 to 0FFFFH
  15252.  
  15253.  Virtual 8086 Mode Exceptions
  15254.  
  15255.  Same exeptions as in Real Address Mode; #PF(fault-code) for a page
  15256.  fault
  15257.  
  15258.  Notes
  15259.  
  15260.  When using the accumulator forms (IMUL r/m8, IMUL r/m16, or IMUL
  15261.  r/m32), the result of the multiplication is available even if the overflow
  15262.  flag is set because the result is two times the size of the multiplicand
  15263.  and multiplier. This is large enough to handle any possible result.
  15264.  
  15265.  
  15266.  IN ── Input from Port
  15267.  
  15268.  Opcode    Instruction   Clocks            Description
  15269.  
  15270.  E4  ib    IN AL,imm8    12,pm=6*/26**     Input byte from immediate port
  15271.                                            into AL
  15272.  E5  ib    IN AX,imm8    12,pm=6*/26**     Input word from immediate port
  15273.                                            into AX
  15274.  E5  ib    IN EAX,imm8   12,pm=6*/26**     Input dword from immediate port
  15275.                                            into EAX
  15276.  EC        IN AL,DX      13,pm=7*/27**     Input byte from port DX into AL
  15277.  ED        IN AX,DX      13,pm=7*/27**     Input word from port DX into AX
  15278.  ED        IN EAX,DX     13,pm=7*/27**     Input dword from port DX into
  15279.                                            EAX
  15280.  
  15281.  
  15282.  ───────────────────────────────────────────────────────────────────────────
  15283.  NOTES:
  15284.     *If CPL ≤ IOPL
  15285.    **If CPL > IOPL or if in virtual 8086 mode
  15286.  ───────────────────────────────────────────────────────────────────────────
  15287.  
  15288.  Operation
  15289.  
  15290.  IF (PE = 1) AND ((VM = 1) OR (CPL > IOPL))
  15291.  THEN (* Virtual 8086 mode, or protected mode with CPL > IOPL *)
  15292.     IF NOT I-O-Permission (SRC, width(SRC))
  15293.     THEN #GP(0);
  15294.     FI;
  15295.  FI;
  15296.  DEST  [SRC]; (* Reads from I/O address space *)
  15297.  
  15298.  Description
  15299.  
  15300.  IN transfers a data byte or data word from the port numbered by the
  15301.  second operand into the register (AL, AX, or EAX) specified by the first
  15302.  operand. Access any port from 0 to 65535 by placing the port number
  15303.  in the DX register and using an IN instruction with DX as the second
  15304.  parameter. These I/O instructions can be shortened by using an 8-bit
  15305.  port I/O in the instruction. The upper eight bits of the port address will
  15306.  be 0 when 8-bit port I/O is used.
  15307.  
  15308.  Flags Affected
  15309.  
  15310.  None
  15311.  
  15312.  Protected Mode Exceptions
  15313.  
  15314.  #GP(0) if the current privilege level is larger (has less privilege) than
  15315.  IOPL and any of the corresponding I/O permission bits in TSS equals 1
  15316.  
  15317.  Real Address Mode Exceptions
  15318.  
  15319.  None
  15320.  
  15321.  Virtual 8086 Mode Exceptions
  15322.  
  15323.  #GP(0) fault if any of the corresponding I/O permission bits in TSS
  15324.  equals 1
  15325.  
  15326.  
  15327.  INC ── Increment by 1
  15328.  
  15329.  Opcode      Instruction        Clocks      Description
  15330.  
  15331.  FE  /0      INC r/m8                       Increment r/m byte by 1
  15332.  FF  /0      INC r/m16                      Increment r/m word by 1
  15333.  FF  /6      INC r/m32                      Increment r/m dword by 1
  15334.  40 + rw     INC r16                        Increment word register by 1
  15335.  40 + rd     INC r32                        Increment dword register by 1
  15336.  
  15337.  
  15338.  Operation
  15339.  
  15340.  DEST  DEST + 1;
  15341.  
  15342.  Description
  15343.  
  15344.  INC adds 1 to the operand. It does not change the carry flag. To affect
  15345.  the carry flag, use the ADD instruction with a second operand of 1.
  15346.  
  15347.  Flags Affected
  15348.  
  15349.  OF, SF, ZF, AF, and PF as described in Appendix C
  15350.  
  15351.  Protected Mode Exceptions
  15352.  
  15353.  #GP(0) if the operand is in a nonwritable segment; #GP(0) for an illegal
  15354.  memory operand effective address in the CS, DS, ES, FS, or GS
  15355.  segments; #SS(0) for an illegal address in the SS segment; #PF(fault-code)
  15356.  for a page fault
  15357.  
  15358.  Real Address Mode Exceptions
  15359.  
  15360.  Interrupt 13 if any part of the operand would lie outside of the effective
  15361.  address space from 0 to 0FFFFH
  15362.  
  15363.  Virtual 8086 Mode Exceptions
  15364.  
  15365.  Same exceptions as in Real Address Mode; #PF(fault-code) for a page
  15366.  fault
  15367.  
  15368.  
  15369.  INS/INSB/INSW/INSD ── Input from Port to String
  15370.  
  15371.  Opcode  Instruction    Clocks         Description
  15372.  
  15373.  6C      INS r/m8,DX    15,pm=9*/29**  Input byte from port DX into ES:(E)DI
  15374.  6D      INS r/m16,DX   15,pm=9*/29**  Input word from port DX into ES:(E)DI
  15375.  6D      INS r/m32,DX   15,pm=9*/29**  Input dword from port DX into ES:(E)DI
  15376.  6C      INSB           15,pm=9*/29**  Input byte from port DX into ES:(E)DI
  15377.  6D      INSW           15,pm=9*/29**  Input word from port DX into ES:(E)DI
  15378.  6D      INSD           15,pm=9*/29**  Input dword from port DX into ES:(E)DI
  15379.  
  15380.  
  15381.  ───────────────────────────────────────────────────────────────────────────
  15382.  NOTES:
  15383.     *If CPL ≤ IOPL
  15384.    **If CPL > IOPL or if in virtual 8086 mode
  15385.  ───────────────────────────────────────────────────────────────────────────
  15386.  
  15387.  Operation
  15388.  
  15389.  IF AddressSize = 16
  15390.  THEN use DI for dest-index;
  15391.  ELSE (* AddressSize = 32 *)
  15392.     use EDI for dest-index;
  15393.  FI;
  15394.  IF (PE = 1) AND ((VM = 1) OR (CPL > IOPL))
  15395.  THEN (* Virtual 8086 mode, or protected mode with CPL > IOPL *)
  15396.     IF NOT I-O-Permission (SRC, width(SRC))
  15397.     THEN #GP(0);
  15398.     FI;
  15399.  FI;
  15400.  IF byte type of instruction
  15401.  THEN
  15402.     ES:[dest-index]  [DX]; (* Reads byte at DX from I/O address space *)
  15403.     IF DF = 0 THEN IncDec  1 ELSE IncDec  -1; FI;
  15404.  FI;
  15405.  IF OperandSize = 16
  15406.  THEN
  15407.     ES:[dest-index]  [DX]; (* Reads word at DX from I/O address space *)
  15408.     IF DF = 0 THEN IncDec  2 ELSE IncDec  -2; FI;
  15409.  FI;
  15410.  IF OperandSize = 32
  15411.  THEN
  15412.     ES:[dest-index]  [DX]; (* Reads dword at DX from I/O address space *)
  15413.     IF DF = 0 THEN IncDec  4 ELSE IncDec  -4; FI;
  15414.  FI;
  15415.  dest-index  dest-index + IncDec;
  15416.  
  15417.  Description
  15418.  
  15419.  INS transfers data from the input port numbered by the DX register to
  15420.  the memory byte or word at ES:dest-index. The memory operand must
  15421.  be addressable from ES; no segment override is possible. The destination
  15422.  register is DI if the address-size attribute of the instruction is 16 bits,
  15423.  or EDI if the address-size attribute is 32 bits.
  15424.  
  15425.  INS does not allow the specification of the port number as an immediate
  15426.  value. The port must be addressed through the DX register value. Load
  15427.  the correct value into DX before executing the INS instruction.
  15428.  
  15429.  The destination address is determined by the contents of the destination
  15430.  index register. Load the correct index into the destination index register
  15431.  before executing INS.
  15432.  
  15433.  After the transfer is made, DI or EDI advances automatically. If the
  15434.  direction flag is 0 (CLD was executed), DI or EDI increments; if the
  15435.  direction flag is 1 (STD was executed), DI or EDI decrements. DI
  15436.  increments or decrements by 1 if a byte is input, by 2 if a word is input,
  15437.  or by 4 if a doubleword is input.
  15438.  
  15439.  INSB, INSW and INSD are synonyms of the byte, word, and doubleword
  15440.  INS instructions. INS can be preceded by the REP prefix for block input of
  15441.  CX bytes or words. Refer to the REP instruction for details of this
  15442.  operation.
  15443.  
  15444.  Flags Affected
  15445.  
  15446.  None
  15447.  
  15448.  Protected Mode Exceptions
  15449.  
  15450.  #GP(0) if CPL is numerically greater than IOPL and any of the
  15451.  corresponding I/O permission bits in TSS equals 1; #GP(0) if the
  15452.  destination is in a nonwritable segment; #GP(0) for an illegal memory
  15453.  operand effective address in the CS, DS, ES, FS, or GS segments; #SS(0) for
  15454.  an illegal address in the SS segment; #PF(fault-code) for a page fault
  15455.  
  15456.  Real Address Mode Exceptions
  15457.  
  15458.  Interrupt 13 if any part of the operand would lie outside of the effective
  15459.  address space from 0 to 0FFFFH
  15460.  
  15461.  Virtual 8086 Mode Exceptions
  15462.  
  15463.  #GP(0) fault if any of the corresponding I/O permission bits in TSS
  15464.  equals 1; #PF(fault-code) for a page fault
  15465.  
  15466.  
  15467.  INT/INTO ── Call to Interrupt Procedure
  15468.  
  15469. ╓┌─────────┌────────────┌───────────────┌────────────────────────────────────╖
  15470.  Opcode    Instruction  Clocks          Description
  15471.  
  15472.  CC        INT 3        33              Interrupt 3--trap to debugger
  15473.  CC        INT 3        pm=59           Interrupt 3--Protected Mode, same
  15474.                                         privilege
  15475.  CC        INT 3        pm=99           Interrupt 3--Protected Mode, more
  15476.                                         privilege
  15477.  CC        INT 3        pm=119          Interrupt 3--from V86 mode to PL 0
  15478.  CC        INT 3        ts              Interrupt 3--Protected Mode, via
  15479.  Opcode    Instruction  Clocks          Description
  15480. CC        INT 3        ts              Interrupt 3--Protected Mode, via
  15481.                                         task gate
  15482.  CD ib     INT imm8     37              Interrupt numbered by immediate
  15483.                                         byte
  15484.  CD ib     INT imm8     pm=59           Interrupt--Protected Mode, same
  15485.                                         privilege
  15486.  CD ib     INT imm8     pm=99           Interrupt--Protected Mode, more
  15487.                                         privilege
  15488.  CD ib     INT imm8     pm=119          Interrupt--from V86 mode to PL 0
  15489.  CD ib     INT imm8     ts              Interrupt--Protected Mode, via task
  15490.                                         gate
  15491.  CE        INTO         Fail:3,pm=3;
  15492.                         Pass:35         Interrupt 4--if overflow flag is 1
  15493.  CE        INTO         pm=59           Interrupt 4--Protected Mode, same
  15494.                                         privilege
  15495.  CE        INTO         pm=99           Interrupt 4--Protected Mode, more
  15496.                                         privilege
  15497.  CE        INTO         pm=119          Interrupt 4--from V86 mode to PL 0
  15498.  CE        INTO         ts              Interrupt 4--Protected Mode, via
  15499.                                         task gate
  15500.  Opcode    Instruction  Clocks          Description
  15501.                                        task gate
  15502.  
  15503.  
  15504.  ───────────────────────────────────────────────────────────────────────────
  15505.  NOTE:
  15506.    Approximate values of ts are given by the following table:
  15507.  
  15508.                              New Task
  15509.  
  15510.  Old Task       386 TSS       386 TSS       286 TSS
  15511.                 VM = 0        VM = 1
  15512.  
  15513.  386
  15514.  TSS VM=0         309           226           282
  15515.  
  15516.  386
  15517.  TSS VM=1         314           231           287
  15518.  
  15519.  286
  15520.  TSS              307           224           280
  15521.  ───────────────────────────────────────────────────────────────────────────
  15522.  
  15523.  Operation
  15524.  
  15525.  ───────────────────────────────────────────────────────────────────────────
  15526.  NOTE:
  15527.    The following operational description applies not only to the
  15528.    above instructions but also to external interrupts and exceptions.
  15529.  ───────────────────────────────────────────────────────────────────────────
  15530.  
  15531.  IF PE = 0
  15532.  THEN GOTO REAL-ADDRESS-MODE;
  15533.  ELSE GOTO PROTECTED-MODE;
  15534.  FI;
  15535.  
  15536.  REAL-ADDRESS-MODE:
  15537.     Push (FLAGS);
  15538.     IF  0; (* Clear interrupt flag *)
  15539.     TF  0; (* Clear trap flag *)
  15540.     Push(CS);
  15541.     Push(IP);
  15542.     (* No error codes are pushed *)
  15543.     CS  IDT[Interrupt number * 4].selector;
  15544.     IP  IDT[Interrupt number * 4].offset;
  15545.  
  15546.  PROTECTED-MODE:
  15547.     Interrupt vector must be within IDT table limits,
  15548.        else #GP(vector number * 8+2+EXT);
  15549.     Descriptor AR byte must indicate interrupt gate, trap gate, or task gate,
  15550.        else #GP(vector number * 8+2+EXT);
  15551.     IF software interrupt (* i.e. caused by INT n, INT 3, or INTO *)
  15552.     THEN
  15553.        IF gate descriptor DPL < CPL
  15554.        THEN #GP(vector number * 8+2+EXT);
  15555.        FI;
  15556.     FI;
  15557.     Gate must be present, else #NP(vector number * 8+2+EXT);
  15558.     IF trap gate OR interrupt gate
  15559.     THEN GOTO TRAP-GATE-OR-INTERRUPT-GATE;
  15560.     ELSE GOTO TASK-GATE;
  15561.     FI;
  15562.  
  15563.  TRAP-GATE-OR-INTERRUPT-GATE:
  15564.     Examine CS selector and descriptor given in the gate descriptor;
  15565.     Selector must be non-null, else #GP (EXT);
  15566.     Selector must be within its descriptor table limits
  15567.        ELSE #GP(selector+EXT);
  15568.     Descriptor AR byte must indicate code segment
  15569.        ELSE #GP(selector + EXT);
  15570.     Segment must be present, else #NP(selector+EXT);
  15571.     IF code segment is non-conforming AND DPL < CPL
  15572.     THEN GOTO INTERRUPT-TO-INNER-PRIVILEGE;
  15573.     ELSE
  15574.        IF code segment is conforming OR code segment DPL = CPL
  15575.        THEN GOTO INTERRUPT-TO-SAME-PRIVILEGE-LEVEL;
  15576.        ELSE #GP(CS selector + EXT);
  15577.        FI;
  15578.     FI;
  15579.  
  15580.  INTERRUPT-TO-INNER-PRIVILEGE:
  15581.     Check selector and descriptor for new stack in current TSS;
  15582.        Selector must be non-null, else #GP(EXT);
  15583.        Selector index must be within its descriptor table limits
  15584.           ELSE #TS(SS selector+EXT);
  15585.        Selector's RPL must equal DPL of code segment, else #TS(SS
  15586.           selector+EXT);
  15587.        Stack segment DPL must equal DPL of code segment, else #TS(SS
  15588.           selector+EXT);
  15589.        Descriptor must indicate writable data segment, else #TS(SS
  15590.           selector+EXT);
  15591.        Segment must be present, else #SS(SS selector+EXT);
  15592.     IF 32-bit gate
  15593.     THEN New stack must have room for 20 bytes else #SS(0)
  15594.     ELSE New stack must have room for 10 bytes else #SS(0)
  15595.     FI;
  15596.     Instruction pointer must be within CS segment boundaries else #GP(0);
  15597.     Load new SS and eSP value from TSS;
  15598.     IF 32-bit gate
  15599.     THEN CS:EIP  selector:offset from gate;
  15600.     ELSE CS:IP  selector:offset from gate;
  15601.     FI;
  15602.     Load CS descriptor into invisible portion of CS register;
  15603.     Load SS descriptor into invisible portion of SS register;
  15604.     IF 32-bit gate
  15605.     THEN
  15606.        Push (long pointer to old stack) (* 3 words padded to 4 *);
  15607.        Push (EFLAGS);
  15608.        Push (long pointer to return location) (* 3 words padded to 4*);
  15609.     ELSE
  15610.        Push (long pointer to old stack) (* 2 words *);
  15611.        Push (FLAGS);
  15612.        Push (long pointer to return location) (* 2 words *);
  15613.     FI;
  15614.     Set CPL to new code segment DPL;
  15615.     Set RPL of CS to CPL;
  15616.     IF interrupt gate THEN IF  0 (* interrupt flag to 0 (disabled) *); FI;
  15617.     TF  0;
  15618.     NT  0;
  15619.  
  15620.  INTERRUPT-FROM-V86-MODE:
  15621.     TempEFlags  EFLAGS;
  15622.     VM  0;
  15623.     TF  0;
  15624.     IF service through Interrupt Gate THEN IF  0;
  15625.     TempSS  SS;
  15626.     TempESP  ESP;
  15627.     SS  TSS.SS0; (* Change to level 0 stack segment *)
  15628.     ESP  TSS.ESP0; (* Change to level 0 stack pointer *)
  15629.     Push(GS); (* padded to two words *)
  15630.     Push(FS); (* padded to two words *)
  15631.     Push(DS); (* padded to two words *)
  15632.     Push(ES); (* padded to two words *)
  15633.     GS  0;
  15634.     FS  0;
  15635.     DS  0;
  15636.     ES  0;
  15637.     Push(TempSS); (* padded to two words *)
  15638.     Push(TempESP);
  15639.     Push(TempEFlags);
  15640.     Push(CS); (* padded to two words *)
  15641.     Push(EIP);
  15642.     CS:EIP  selector:offset from interrupt gate;
  15643.     (* Starts execution of new routine in 80386 Protected Mode *)
  15644.  
  15645.  INTERRUPT-TO-SAME-PRIVILEGE-LEVEL:
  15646.     IF 32-bit gate
  15647.     THEN Current stack limits must allow pushing 10 bytes, else #SS(0);
  15648.     ELSE Current stack limits must allow pushing 6 bytes, else #SS(0);
  15649.     FI;
  15650.     IF interrupt was caused by exception with error code
  15651.     THEN Stack limits must allow push of two more bytes;
  15652.     ELSE #SS(0);
  15653.     FI;
  15654.     Instruction pointer must be in CS limit, else #GP(0);
  15655.     IF 32-bit gate
  15656.     THEN
  15657.        Push (EFLAGS);
  15658.        Push (long pointer to return location); (* 3 words padded to 4 *)
  15659.        CS:EIP  selector:offset from gate;
  15660.     ELSE (* 16-bit gate *)
  15661.        Push (FLAGS);
  15662.        Push (long pointer to return location); (* 2 words *)
  15663.        CS:IP  selector:offset from gate;
  15664.     FI;
  15665.     Load CS descriptor into invisible portion of CS register;
  15666.     Set the RPL field of CS to CPL;
  15667.     Push (error code); (* if any *)
  15668.     IF interrupt gate THEN IF  0; FI;
  15669.     TF  0;
  15670.     NT  0;
  15671.  
  15672.  TASK-GATE:
  15673.     Examine selector to TSS, given in task gate descriptor;
  15674.        Must specify global in the local/global bit, else #TS(TSS selector);
  15675.        Index must be within GDT limits, else #TS(TSS selector);
  15676.        AR byte must specify available TSS (bottom bits 00001),
  15677.           else #TS(TSS selector;
  15678.        TSS must be present, else #NP(TSS selector);
  15679.     SWITCH-TASKS with nesting to TSS;
  15680.     IF interrupt was caused by fault with error code
  15681.     THEN
  15682.        Stack limits must allow push of two more bytes, else #SS(0);
  15683.        Push error code onto stack;
  15684.     FI;
  15685.     Instruction pointer must be in CS limit, else #GP(0);
  15686.  
  15687.  Description
  15688.  
  15689.  The INT  instruction generates via software a call to an interrupt
  15690.  handler. The immediate operand, from 0 to 255, gives the index number
  15691.  into the Interrupt Descriptor Table (IDT) of the interrupt routine to be
  15692.  called. In Protected Mode, the IDT consists of an array of eight-byte
  15693.  descriptors; the descriptor for the interrupt invoked must indicate an
  15694.  interrupt, trap, or task gate. In Real Address Mode, the IDT is an array
  15695.  of four byte-long pointers. In Protected and Real Address Modes, the
  15696.  base linear address of the IDT is defined by the contents of the IDTR.
  15697.  
  15698.  The INTO conditional software instruction is identical to the INT
  15699.  interrupt instruction except that the interrupt number is implicitly 4,
  15700.  and the interrupt is made only if the 80386 overflow flag is set.
  15701.  
  15702.  The first 32 interrupts are reserved by Intel for system use. Some of
  15703.  these interrupts are use for internally generated exceptions.
  15704.  
  15705.  INT n generally behaves like a far call except that the flags register is
  15706.  pushed onto the stack before the return address. Interrupt procedures
  15707.  return via the IRET instruction, which pops the flags and return address
  15708.  from the stack.
  15709.  
  15710.  In Real Address Mode, INT n pushes the flags, CS, and the return IP
  15711.  onto the stack, in that order, then jumps to the long pointer indexed by
  15712.  the interrupt number.
  15713.  
  15714.  Flags Affected
  15715.  
  15716.  None
  15717.  
  15718.  Protected Mode Exceptions
  15719.  
  15720.  #GP, #NP, #SS, and #TS as indicated under "Operation" above
  15721.  
  15722.  Real Address Mode Exceptions
  15723.  
  15724.  None; if the SP or ESP = 1, 3, or 5 before executing INT or INTO,
  15725.  the 80386 will shut down due to insufficient stack space
  15726.  
  15727.  Virtual 8086 Mode Exceptions
  15728.  
  15729.  #GP(0) fault if IOPL is less than 3, for INT  only, to permit emulation;
  15730.  Interrupt 3 (0CCH) generates Interrupt 3; INTO generates Interrupt 4
  15731.  if the overflow flag equals 1
  15732.  
  15733.  
  15734.  IRET/IRETD ── Interrupt Return
  15735.  
  15736.  Opcode  Instruction  Clocks       Description
  15737.  
  15738.  CF      IRET         22,pm=38     Interrupt return (far return and pop
  15739.                                    flags)
  15740.  CF      IRET         pm=82        Interrupt return to lesser privilege
  15741.  CF      IRET         ts           Interrupt return, different task (NT = 1)
  15742.  CF      IRETD        22,pm=38     Interrupt return (far return and pop
  15743.                                    flags)
  15744.  CF      IRETD        pm=82        Interrupt return to lesser privilege
  15745.  CF      IRETD        pm=60        Interrupt return to V86 mode
  15746.  CF      IRETD        ts           Interrupt return, different task (NT = 1)
  15747.  
  15748.  
  15749.  ───────────────────────────────────────────────────────────────────────────
  15750.  NOTE:
  15751.    Values of ts are given by the following table:
  15752.  
  15753.                              New Task
  15754.  
  15755.  Old Task       386 TSS       386 TSS       286 TSS
  15756.                 VM = 0        VM = 1
  15757.  
  15758.  386
  15759.  TSS VM=0         275           224           271
  15760.  
  15761.  286
  15762.  TSS              265           214           232
  15763.  ───────────────────────────────────────────────────────────────────────────
  15764.  
  15765.  Operation
  15766.  
  15767.  IF PE = 0
  15768.  THEN (* Real-address mode *)
  15769.     IF OperandSize = 32 (* Instruction = IRETD *)
  15770.     THEN EIP  Pop();
  15771.     ELSE (* Instruction = IRET *)
  15772.        IP  Pop();
  15773.     FI;
  15774.     CS  Pop();
  15775.     IF OperandSize = 32 (* Instruction = IRETD *)
  15776.     THEN EFLAGS  Pop();
  15777.     ELSE (* Instruction = IRET *)
  15778.        FLAGS  Pop();
  15779.     FI;
  15780.  ELSE (* Protected mode *)
  15781.     IF VM = 1
  15782.     THEN #GP(0);
  15783.     ELSE
  15784.        IF NT = 1
  15785.        THEN GOTO TASK-RETURN;
  15786.        ELSE
  15787.           IF VM = 1 in flags image on stack
  15788.           THEN GO TO STACK-RETURN-TO-V86;
  15789.           ELSE GOTO STACK-RETURN;
  15790.           FI;
  15791.        FI;
  15792.     FI;
  15793.  FI;STACK-RETURN-TO-V86: (* Interrupted procedure was in V86 mode *)
  15794.     IF return CS selector RPL < > 3
  15795.     THEN #GP(Return selector);
  15796.     FI;
  15797.     IF top 36 bytes of stack not within limits
  15798.     THEN #SS(0);
  15799.     FI;
  15800.     Examine return CS selector and associated descriptor:
  15801.        IF selector is null, THEN #GP(0); FI;
  15802.        IF selector index not within its descriptor table limits;
  15803.        THEN #GP(Return selector);
  15804.        FI;
  15805.        IF AR byte does not indicate code segment
  15806.        THEN #GP(Return selector);
  15807.        FI;
  15808.        IF code segment DPL not = 3;
  15809.        THEN #GP(Return selector);
  15810.        FI;
  15811.        IF code segment not present
  15812.        THEN #NP(Return selector);
  15813.        FI;
  15814.  
  15815.     Examine return SS selector and associated descriptor:
  15816.        IF selector is null THEN #GP(0); FI;
  15817.        IF selector index not within its descriptor table limits
  15818.        THEN #GP(SS selector);
  15819.        FI;
  15820.        IF selector RPL not = RPL of return CS selector
  15821.        THEN #GP(SS selector);
  15822.        FI;
  15823.        IF AR byte does not indicate a writable data segment
  15824.        THEN #GP(SS selector);
  15825.        FI;
  15826.        IF stack segment DPL not = RPL of return CS selector
  15827.        THEN #GP(SS selector);
  15828.        FI;
  15829.        IF SS not present
  15830.        THEN #NP(SS selector);
  15831.        FI;
  15832.  
  15833.     IF instruction pointer not within code segment limit  THEN #GP(0);
  15834.     FI;
  15835.     EFLAGS  SS:[eSP + 8]; (* Sets VM in interrupted routine *)
  15836.     EIP  Pop();
  15837.     CS  Pop(); (* CS behaves as in 8086, due to VM = 1 *)
  15838.     throwaway  Pop(); (* pop away EFLAGS already read *)
  15839.     ES  Pop(); (* pop 2 words; throw away high-order word *)
  15840.     DS  Pop(); (* pop 2 words; throw away high-order word *)
  15841.     FS  Pop(); (* pop 2 words; throw away high-order word *)
  15842.     GS  Pop(); (* pop 2 words; throw away high-order word *)
  15843.     IF CS.RPL > CPL
  15844.     THEN
  15845.        TempESP  Pop();
  15846.        TempSS  Pop();
  15847.        SS:ESP  TempSS:TempESP;
  15848.     FI;
  15849.  
  15850.     (* Resume execution in Virtual 8086 mode *)
  15851.  
  15852.  TASK-RETURN:
  15853.     Examine Back Link Selector in TSS addressed by the current task
  15854.        register:
  15855.        Must specify global in the local/global bit, else #TS(new TSS
  15856.           selector);
  15857.        Index must be within GDT limits, else #TS(new TSS selector);
  15858.        AR byte must specify TSS, else #TS(new TSS selector);
  15859.        New TSS must be busy, else #TS(new TSS selector);
  15860.        TSS must be present, else #NP(new TSS selector);
  15861.     SWITCH-TASKS without nesting to TSS specified by back link selector;
  15862.     Mark the task just abandoned as NOT BUSY;
  15863.     Instruction pointer must be within code segment limit ELSE #GP(0);
  15864.  
  15865.  STACK-RETURN:
  15866.     IF OperandSize=32
  15867.     THEN Third word on stack must be within stack limits, else #SS(0);
  15868.     ELSE Second word on stack must be within stack limits, else #SS(0);
  15869.     FI;
  15870.     Return CS selector RPL must be ≥ CPL, else #GP(Return selector);
  15871.     IF return selector RPL = CPL
  15872.     THEN GOTO RETURN-SAME-LEVEL;
  15873.     ELSE GOTO RETURN-OUTER-LEVEL;
  15874.     FI;
  15875.  
  15876.  RETURN-SAME-LEVEL:
  15877.     IF OperandSize=32
  15878.     THEN
  15879.        Top 12 bytes on stack must be within limits, else #SS(0);
  15880.        Return CS selector (at eSP+4) must be non-null, else #GP(0);
  15881.     ELSE
  15882.        Top 6 bytes on stack must be within limits, else #SS(0);
  15883.        Return CS selector (at eSP+2) must be non-null, else #GP(0);
  15884.     FI;
  15885.     Selector index must be within its descriptor table limits, else #GP
  15886.        (Return selector);
  15887.     AR byte must indicate code segment, else #GP(Return selector);
  15888.     IF non-conforming
  15889.     THEN code segment DPL must = CPL;
  15890.     ELSE #GP(Return selector);
  15891.     FI;
  15892.     IF conforming
  15893.     THEN code segment DPL must be ≤ CPL, else #GP(Return selector);
  15894.     Segment must be present, else #NP(Return selector);
  15895.     Instruction pointer must be within code segment boundaries, else #GP(0);
  15896.     FI;
  15897.     IF OperandSize=32
  15898.     THEN
  15899.        Load CS:EIP from stack;
  15900.        Load CS-register with new code segment descriptor;
  15901.        Load EFLAGS with third doubleword from stack;
  15902.        Increment eSP by 12;
  15903.     ELSE
  15904.        Load CS-register with new code segment descriptor;
  15905.        Load FLAGS with third word on stack;
  15906.        Increment eSP by 6;
  15907.     FI;
  15908.  
  15909.  RETURN-OUTER-LEVEL:
  15910.     IF OperandSize=32
  15911.     THEN Top 20 bytes on stack must be within limits, else #SS(0);
  15912.     ELSE Top 10 bytes on stack must be within limits, else #SS(0);
  15913.     FI;
  15914.     Examine return CS selector and associated descriptor:
  15915.        Selector must be non-null, else #GP(0);
  15916.        Selector index must be within its descriptor table limits;
  15917.           ELSE #GP(Return selector);
  15918.        AR byte must indicate code segment, else #GP(Return selector);
  15919.        IF non-conforming
  15920.        THEN code segment DPL must = CS selector RPL;
  15921.        ELSE #GP(Return selector);
  15922.        FI;
  15923.        IF conforming
  15924.        THEN code segment DPL must be > CPL;
  15925.        ELSE #GP(Return selector);
  15926.        FI;
  15927.        Segment must be present, else #NP(Return selector);
  15928.     Examine return SS selector and associated descriptor:
  15929.        Selector must be non-null, else #GP(0);
  15930.        Selector index must be within its descriptor table limits
  15931.           ELSE #GP(SS selector);
  15932.        Selector RPL must equal the RPL of the return CS selector
  15933.           ELSE #GP(SS selector);
  15934.        AR byte must indicate a writable data segment, else #GP(SS selector);
  15935.        Stack segment DPL must equal the RPL of the return CS selector
  15936.           ELSE #GP(SS selector);
  15937.        SS must be present, else #NP(SS selector);
  15938.  
  15939.     Instruction pointer must be within code segment limit ELSE #GP(0);
  15940.     IF OperandSize=32
  15941.     THEN
  15942.        Load CS:EIP from stack;
  15943.        Load EFLAGS with values at (eSP+8);
  15944.     ELSE
  15945.        Load CS:IP from stack;
  15946.        Load FLAGS with values at (eSP+4);
  15947.     FI;
  15948.     Load SS:eSP from stack;
  15949.     Set CPL to the RPL of the return CS selector;
  15950.     Load the CS register with the CS descriptor;
  15951.     Load the SS register with the SS descriptor;
  15952.     FOR each of ES, FS, GS, and DS
  15953.     DO;
  15954.        IF the current value of the register is not valid for the outer level;
  15955.        THEN zero the register and clear the valid flag;
  15956.        FI;
  15957.        To be valid, the register setting must satisfy the following
  15958.           properties:
  15959.           Selector index must be within descriptor table limits;
  15960.           AR byte must indicate data or readable code segment;
  15961.           IF segment is data or non-conforming code,
  15962.           THEN DPL must be ≥ CPL, or DPL must be ≥ RPL;
  15963.     OD;
  15964.  
  15965.  Description
  15966.  
  15967.  In Real Address Mode, IRET pops the instruction pointer, CS, and the
  15968.  flags register from the stack and resumes the interrupted routine.
  15969.  
  15970.  In Protected Mode, the action of IRET depends on the setting of the
  15971.  nested task flag (NT) bit in the flag register. When popping the new
  15972.  flag image from the stack, the IOPL bits in the flag register are changed
  15973.  only when CPL equals 0.
  15974.  
  15975.  If NT equals 0, IRET returns from an interrupt procedure without a
  15976.  task switch. The code returned to must be equally or less privileged than
  15977.  the interrupt routine (as indicated by the RPL bits of the CS selector
  15978.  popped from the stack). If the destination code is less privileged, IRET
  15979.  also pops the stack pointer and SS from the stack.
  15980.  
  15981.  If NT equals 1, IRET reverses the operation of a CALL or INT that
  15982.  caused a task switch. The updated state of the task executing IRET is
  15983.  saved in its task state segment. If the task is reentered later, the code
  15984.  that follows IRET is executed.
  15985.  
  15986.  Flags Affected
  15987.  
  15988.  All; the flags register is popped from stack
  15989.  
  15990.  Protected Mode Exceptions
  15991.  
  15992.  #GP, #NP, or #SS, as indicated under "Operation" above
  15993.  
  15994.  Real Address Mode Exceptions
  15995.  
  15996.  Interrupt 13 if any part of the operand being popped lies beyond address
  15997.  0FFFFH
  15998.  
  15999.  Virtual 8086 Mode Exceptions
  16000.  
  16001.  #GP(0) fault if IOPL is less than 3, to permit emulation
  16002.  
  16003.  
  16004.  Jcc ── Jump if Condition is Met
  16005.  
  16006. ╓┌──────────────┌─────────────────┌────────┌─────────────────────────────────╖
  16007.  Opcode         Instruction       Clocks   Description
  16008.  
  16009.  77  cb         JA rel8           7+m,3    Jump short if above (CF=0 and
  16010.                                            ZF=0)
  16011.  73  cb         JAE rel8          7+m,3    Jump short if above or equal
  16012.                                            (CF=0)
  16013.  72  cb         JB rel8           7+m,3    Jump short if below (CF=1)
  16014.  76  cb         JBE rel8          7+m,3    Jump short if below or equal
  16015.                                            (CF=1 or ZF=1)
  16016.  72  cb         JC rel8           7+m,3    Jump short if carry (CF=1)
  16017.  E3  cb         JCXZ rel8         9+m,5    Jump short if CX register is 0
  16018.  E3  cb         JECXZ rel8        9+m,5    Jump short if ECX register is 0
  16019.  74  cb         JE rel8           7+m,3    Jump short if equal (ZF=1)
  16020.  74  cb         JZ rel8           7+m,3    Jump short if 0 (ZF=1)
  16021.  7F  cb         JG rel8           7+m,3    Jump short if greater (ZF=0 and
  16022.                                            SF=OF)
  16023.  7D  cb         JGE rel8          7+m,3    Jump short if greater or equal
  16024.                                            (SF=OF)
  16025.  Opcode         Instruction       Clocks   Description
  16026.                                           (SF=OF)
  16027.  7C  cb         JL rel8           7+m,3    Jump short if less (SF<>OF)
  16028.  7E  cb         JLE rel8          7+m,3    Jump short if less or equal
  16029.                                            (ZF=1 and SF<>OF)
  16030.  76  cb         JNA rel8          7+m,3    Jump short if not above (CF=1 or
  16031.                                            ZF=1)
  16032.  72  cb         JNAE rel8         7+m,3    Jump short if not above or equal
  16033.                                            (CF=1)
  16034.  73  cb         JNB rel8          7+m,3    Jump short if not below (CF=0)
  16035.  77  cb         JNBE rel8         7+m,3    Jump short if not below or equal
  16036.                                            (CF=0 and ZF=0)
  16037.  73  cb         JNC rel8          7+m,3    Jump short if not carry (CF=0)
  16038.  75  cb         JNE rel8          7+m,3    Jump short if not equal (ZF=0)
  16039.  7E  cb         JNG rel8          7+m,3    Jump short if not greater (ZF=1
  16040.                                            or SF<>OF)
  16041.  7C  cb         JNGE rel8         7+m,3    Jump short if not greater or
  16042.                                            equal (SF<>OF)
  16043.  7D  cb         JNL rel8          7+m,3    Jump short if not less (SF=OF)
  16044.  7F  cb         JNLE rel8         7+m,3    Jump short if not less or equal
  16045.                                            (ZF=0 and SF=OF)
  16046.  Opcode         Instruction       Clocks   Description
  16047.                                           (ZF=0 and SF=OF)
  16048.  71  cb         JNO rel8          7+m,3    Jump short if not overflow
  16049.                                            (OF=0)
  16050.  7B  cb         JNP rel8          7+m,3    Jump short if not parity (PF=0)
  16051.  79  cb         JNS rel8          7+m,3    Jump short if not sign (SF=0)
  16052.  75  cb         JNZ rel8          7+m,3    Jump short if not zero (ZF=0)
  16053.  70  cb         JO rel8           7+m,3    Jump short if overflow (OF=1)
  16054.  7A  cb         JP rel8           7+m,3    Jump short if parity (PF=1)
  16055.  7A  cb         JPE rel8          7+m,3    Jump short if parity even (PF=1)
  16056.  7B  cb         JPO rel8          7+m,3    Jump short if parity odd (PF=0)
  16057.  78  cb         JS rel8           7+m,3    Jump short if sign (SF=1)
  16058.  74  cb         JZ rel8           7+m,3    Jump short if zero (ZF = 1)
  16059.  0F  87 cw/cd   JA rel16/32       7+m,3    Jump near if above (CF=0 and
  16060.                                            ZF=0)
  16061.  0F  83 cw/cd   JAE rel16/32      7+m,3    Jump near if above or equal
  16062.                                            (CF=0)
  16063.  0F  82 cw/cd   JB rel16/32       7+m,3    Jump near if below (CF=1)
  16064.  0F  86 cw/cd   JBE rel16/32      7+m,3    Jump near if below or equal
  16065.                                            (CF=1 or ZF=1)
  16066.  0F  82 cw/cd   JC rel16/32       7+m,3    Jump near if carry (CF=1)
  16067.  Opcode         Instruction       Clocks   Description
  16068. 0F  82 cw/cd   JC rel16/32       7+m,3    Jump near if carry (CF=1)
  16069.  0F  84 cw/cd   JE rel16/32       7+m,3    Jump near if equal (ZF=1)
  16070.  0F  84 cw/cd   JZ rel16/32       7+m,3    Jump near if 0 (ZF=1)
  16071.  0F  8F cw/cd   JG rel16/32       7+m,3    Jump near if greater (ZF=0 and
  16072.                                            SF=OF)
  16073.  0F  8D cw/cd   JGE rel16/32      7+m,3    Jump near if greater or equal
  16074.                                            (SF=OF)
  16075.  0F  8C cw/cd   JL rel16/32       7+m,3    Jump near if less (SF<>OF)
  16076.  0F  8E cw/cd   JLE rel16/32      7+m,3    Jump near if less or equal (ZF=1
  16077.                                            and SF<>OF)
  16078.  0F  86 cw/cd   JNA rel16/32      7+m,3    Jump near if not above (CF=1 or
  16079.                                            ZF=1)
  16080.  0F  82 cw/cd   JNAE rel16/32     7+m,3    Jump near if not above or equal
  16081.                                            (CF=1)
  16082.  0F  83 cw/cd   JNB rel16/32      7+m,3    Jump near if not below (CF=0)
  16083.  0F  87 cw/cd   JNBE rel16/32     7+m,3    Jump near if not below or equal
  16084.                                            (CF=0 and ZF=0)
  16085.  0F  83 cw/cd   JNC rel16/32      7+m,3    Jump near if not carry (CF=0)
  16086.  0F  85 cw/cd   JNE rel16/32      7+m,3    Jump near if not equal (ZF=0)
  16087.  0F  8E cw/cd   JNG rel16/32      7+m,3    Jump near if not greater (ZF=1
  16088.  Opcode         Instruction       Clocks   Description
  16089. 0F  8E cw/cd   JNG rel16/32      7+m,3    Jump near if not greater (ZF=1
  16090.                                            or SF<>OF)
  16091.  0F  8C cw/cd   JNGE rel16/32     7+m,3    Jump near if not greater or
  16092.                                            equal (SF<>OF)
  16093.  0F  8D cw/cd   JNL rel16/32      7+m,3    Jump near if not less (SF=OF)
  16094.  0F  8F cw/cd   JNLE rel16/32     7+m,3    Jump near if not less or equal
  16095.                                            (ZF=0 and SF=OF)
  16096.  0F  81 cw/cd   JNO rel16/32      7+m,3    Jump near if not overflow (OF=0)
  16097.  0F  8B cw/cd   JNP rel16/32      7+m,3    Jump near if not parity (PF=0)
  16098.  0F  89 cw/cd   JNS rel16/32      7+m,3    Jump near if not sign (SF=0)
  16099.  0F  85 cw/cd   JNZ rel16/32      7+m,3    Jump near if not zero (ZF=0)
  16100.  0F  80 cw/cd   JO rel16/32       7+m,3    Jump near if overflow (OF=1)
  16101.  0F  8A cw/cd   JP rel16/32       7+m,3    Jump near if parity (PF=1)
  16102.  0F  8A cw/cd   JPE rel16/32      7+m,3    Jump near if parity even (PF=1)
  16103.  0F  8B cw/cd   JPO rel16/32      7+m,3    Jump near if parity odd (PF=0)
  16104.  0F  88 cw/cd   JS rel16/32       7+m,3    Jump near if sign (SF=1)
  16105.  0F  84 cw/cd   JZ rel16/32       7+m,3    Jump near if 0 (ZF=1)
  16106.  
  16107.  
  16108.  ───────────────────────────────────────────────────────────────────────────
  16109.  NOTES:
  16110.    The first clock count is for the true condition (branch taken); the
  16111.    second clock count is for the false condition (branch not taken). rel16/32
  16112.    indicates that these instructions map to two; one with a 16-bit relative
  16113.    displacement, the other with a 32-bit relative displacement, depending on
  16114.    the operand-size attribute of the instruction.
  16115.  ───────────────────────────────────────────────────────────────────────────
  16116.  
  16117.  Operation
  16118.  
  16119.  IF condition
  16120.  THEN
  16121.     EIP  EIP + SignExtend(rel8/16/32);
  16122.     IF OperandSize = 16
  16123.     THEN EIP  EIP AND 0000FFFFH;
  16124.     FI;
  16125.  FI;
  16126.  
  16127.  Description
  16128.  
  16129.  Conditional jumps (except JCXZ) test the flags which have been set by
  16130.  a previous instruction. The conditions for each mnemonic are given in
  16131.  parentheses after each description above. The terms "less" and "greater"
  16132.  are used for comparisons of signed integers; "above" and "below" are
  16133.  used for unsigned integers.
  16134.  
  16135.  If the given condition is true, a jump is made to the location provided as
  16136.  the operand. Instruction coding is most efficient when the target for the
  16137.  conditional jump is in the current code segment and within -128 to
  16138.  +127 bytes of the next instruction's first byte. The jump can also target
  16139.  -32768 thru +32767 (segment size attribute 16) or -2^(31) thru +2^(31) -1
  16140.  (segment size attribute 32) relative to the next instruction's first byte.
  16141.  When the target for the conditional jump is in a different segment, use
  16142.  the opposite case of the jump instruction (i.e., JE and JNE), and then
  16143.  access the target with an unconditional far jump to the other segment.
  16144.  For example, you cannot code──
  16145.  
  16146.  JZ FARLABEL;
  16147.  
  16148.  You must instead code──
  16149.  
  16150.     JNZ BEYOND;
  16151.     JMP FARLABEL;
  16152.  BEYOND:
  16153.  
  16154.  Because there can be several ways to interpret a particular state of the
  16155.  flags, ASM386 provides more than one mnemonic for most of the
  16156.  conditional jump opcodes. For example, if you compared two characters in
  16157.  AX and want to jump if they are equal, use JE; or, if you ANDed AX
  16158.  with a bit field mask and only want to jump if the result is 0, use JZ, a
  16159.  synonym for JE.
  16160.  
  16161.  JCXZ differs from other conditional jumps because it tests the contents of
  16162.  the CX or ECX register for 0, not the flags. JCXZ is useful at the beginning
  16163.  of a conditional loop that terminates with a conditional loop instruction
  16164.  (such as LOOPNE TARGET LABEL. The JCXZ prevents entering the loop with CX or
  16165.  ECX equal to zero, which would cause the loop to execute 64K or 32G times
  16166.  instead of zero times.
  16167.  
  16168.  Flags Affected
  16169.  
  16170.  None
  16171.  
  16172.  Protected Mode Exceptions
  16173.  
  16174.  #GP(0) if the offset jumped to is beyond the limits of the code segment
  16175.  
  16176.  Real Address Mode Exceptions
  16177.  
  16178.  None
  16179.  
  16180.  Virtual 8086 Mode Exceptions
  16181.  
  16182.  None
  16183.  
  16184.  
  16185.  JMP ── Jump
  16186.  
  16187. ╓┌────────┌───────────────┌───────────────┌──────────────────────────────────╖
  16188.  Opcode    Instruction     Clocks          Description
  16189.  
  16190.  EB  cb    JMP rel8        7+m             Jump short
  16191.  E9  cw    JMP rel16       7+m             Jump near, displacement relative
  16192.                                            to next instruction
  16193.  Opcode    Instruction     Clocks          Description
  16194.                                           to next instruction
  16195.  FF  /4    JMP r/m16       7+m/10+m        Jump near indirect
  16196.  EA  cd    JMP ptr16:16    12+m,pm=27+m    Jump intersegment, 4-byte
  16197.                                            immediate address
  16198.  EA  cd    JMP ptr16:16    pm=45+m         Jump to call gate, same
  16199.                                            privilege
  16200.  EA  cd    JMP ptr16:16    ts              Jump via task state segment
  16201.  EA  cd    JMP ptr16:16    ts              Jump via task gate
  16202.  FF  /5    JMP m16:16      43+m,pm=31+m    Jump r/m16:16 indirect and
  16203.                                            intersegment
  16204.  FF  /5    JMP m16:16      pm=49+m         Jump to call gate, same
  16205.                                            privilege
  16206.  FF  /5    JMP m16:16      5 + ts          Jump via task state segment
  16207.  FF  /5    JMP m16:16      5 + ts          Jump via task gate
  16208.  E9  cd    JMP rel32       7+m             Jump near, displacement relative
  16209.                                            to next instruction
  16210.  FF  /4    JMP r/m32       7+m,10+m        Jump near, indirect
  16211.  EA  cp    JMP ptr16:32    12+m,pm=27+m    Jump intersegment, 6-byte
  16212.                                            immediate address
  16213.  EA  cp    JMP ptr16:32    pm=45+m         Jump to call gate, same
  16214.  Opcode    Instruction     Clocks          Description
  16215. EA  cp    JMP ptr16:32    pm=45+m         Jump to call gate, same
  16216.                                            privilege
  16217.  EA  cp    JMP ptr16:32    ts              Jump via task state segment
  16218.  EA  cp    JMP ptr16:32    ts              Jump via task gate
  16219.  FF  /5    JMP m16:32      43+m,pm=31+m    Jump intersegment, address at
  16220.                                            r/m dword
  16221.  FF  /5    JMP m16:32      pm=49+m         Jump to call gate, same
  16222.                                            privilege
  16223.  FF  /5    JMP m16:32      5 + ts          Jump via task state segment
  16224.  FF  /5    JMP m16:32      5 + ts          Jump via task gate
  16225.  
  16226.  
  16227.  ───────────────────────────────────────────────────────────────────────────
  16228.  NOTE:
  16229.  Values of ts are given by the following table:
  16230.  
  16231.                                 New Task
  16232.  
  16233.                  386 TSS       386 TASK       286 TSS
  16234.                  VM = 0        VM = 1
  16235.  
  16236.  Old Task                   Via Task Gate?
  16237.  
  16238.                  N     Y       N      Y       N     Y
  16239.  386
  16240.  TSS VM=0       303   312     220    229     276   285
  16241.  
  16242.  286
  16243.  TSS            301   310     218    227     274   283
  16244.  ───────────────────────────────────────────────────────────────────────────
  16245.  
  16246.  Operation
  16247.  
  16248.  IF instruction = relative JMP
  16249.     (* i.e. operand is rel8, rel16, or rel32 *)
  16250.  THEN
  16251.     EIP  EIP + rel8/16/32;
  16252.     IF OperandSize = 16
  16253.     THEN EIP  EIP AND 0000FFFFH;
  16254.     FI;
  16255.  FI;
  16256.  IF instruction = near indirect JMP
  16257.     (* i.e. operand is r/m16 or r/m32 *)
  16258.  THEN
  16259.     IF OperandSize = 16
  16260.     THEN
  16261.        EIP  [r/m16] AND 0000FFFFH;
  16262.     ELSE (* OperandSize = 32 *)
  16263.        EIP  [r/m32];
  16264.     FI;
  16265.  FI;
  16266.  
  16267.  IF (PE = 0 OR (PE = 1 AND VM = 1)) (* real mode or V86 mode *)
  16268.     AND instruction = far JMP
  16269.     (* i.e., operand type is m16:16, m16:32, ptr16:16, ptr16:32 *)
  16270.  THEN GOTO REAL-OR-V86-MODE;
  16271.     IF operand type = m16:16 or m16:32
  16272.     THEN (* indirect *)
  16273.        IF OperandSize = 16
  16274.        THEN
  16275.           CS:IP  [m16:16];
  16276.           EIP  EIP AND 0000FFFFH; (* clear upper 16 bits *)
  16277.        ELSE (* OperandSize = 32 *)
  16278.           CS:EIP  [m16:32];
  16279.        FI;
  16280.     FI;
  16281.     IF operand type = ptr16:16 or ptr16:32
  16282.     THEN
  16283.        IF OperandSize = 16
  16284.        THEN
  16285.           CS:IP  ptr16:16;
  16286.           EIP  EIP AND 0000FFFFH; (* clear upper 16 bits *)
  16287.        ELSE (* OperandSize = 32 *)
  16288.           CS:EIP  ptr16:32;
  16289.        FI;
  16290.     FI;
  16291.  FI;
  16292.  
  16293.  IF (PE = 1 AND VM = 0) (* Protected mode, not V86 mode *)
  16294.     AND instruction = far JMP
  16295.  THEN
  16296.     IF operand type = m16:16 or m16:32
  16297.     THEN (* indirect *)
  16298.        check access of EA dword;
  16299.        #GP(0) or #SS(0) IF limit violation;
  16300.     FI;
  16301.     Destination selector is not null ELSE #GP(0)
  16302.     Destination selector index is within its descriptor table limits ELSE
  16303.  #GP(selector)
  16304.     Depending on AR byte of destination descriptor:
  16305.        GOTO CONFORMING-CODE-SEGMENT;
  16306.        GOTO NONCONFORMING-CODE-SEGMENT;
  16307.        GOTO CALL-GATE;
  16308.        GOTO TASK-GATE;
  16309.        GOTO TASK-STATE-SEGMENT;
  16310.     ELSE #GP(selector); (* illegal AR byte in descriptor *)
  16311.  FI;
  16312.  
  16313.  CONFORMING-CODE-SEGMENT:
  16314.     Descriptor DPL must be ≤ CPL ELSE #GP(selector);
  16315.     Segment must be present ELSE #NP(selector);
  16316.     Instruction pointer must be within code-segment limit ELSE #GP(0);
  16317.     IF OperandSize = 32
  16318.     THEN Load CS:EIP from destination pointer;
  16319.     ELSE Load CS:IP from destination pointer;
  16320.     FI;
  16321.     Load CS register with new segment descriptor;
  16322.  
  16323.  NONCONFORMING-CODE-SEGMENT:
  16324.     RPL of destination selector must be ≤ CPL ELSE #GP(selector);
  16325.     Descriptor DPL must be = CPL ELSE #GP(selector);
  16326.     Segment must be present ELSE # NP(selector);
  16327.     Instruction pointer must be within code-segment limit ELSE #GP(0);
  16328.     IF OperandSize = 32
  16329.     THEN Load CS:EIP from destination pointer;
  16330.     ELSE Load CS:IP from destination pointer;
  16331.     FI;
  16332.     Load CS register with new segment descriptor;
  16333.     Set RPL field of CS register to CPL;
  16334.  
  16335.  CALL-GATE:
  16336.     Descriptor DPL must be ≥ CPL ELSE #GP(gate selector);
  16337.     Descriptor DPL must be ≥ gate selector RPL ELSE #GP(gate selector);
  16338.     Gate must be present ELSE #NP(gate selector);
  16339.     Examine selector to code segment given in call gate descriptor:
  16340.        Selector must not be null ELSE #GP(0);
  16341.        Selector must be within its descriptor table limits ELSE
  16342.           #GP(CS selector);
  16343.        Descriptor AR byte must indicate code segment
  16344.           ELSE #GP(CS selector);
  16345.        IF non-conforming
  16346.        THEN code-segment descriptor, DPL must = CPL
  16347.        ELSE #GP(CS selector);
  16348.        FI;
  16349.        IF conforming
  16350.        THEN code-segment descriptor DPL must be ≤ CPL;
  16351.        ELSE #GP(CS selector);
  16352.        Code segment must be present ELSE #NP(CS selector);
  16353.        Instruction pointer must be within code-segment limit ELSE #GP(0);
  16354.        IF OperandSize = 32
  16355.        THEN Load CS:EIP from call gate;
  16356.        ELSE Load CS:IP from call gate;
  16357.        FI;
  16358.     Load CS register with new code-segment descriptor;
  16359.     Set RPL of CS to CPL
  16360.  
  16361.  TASK-GATE:
  16362.     Gate descriptor DPL must be ≥ CPL ELSE #GP(gate selector);
  16363.     Gate descriptor DPL must be ≥ gate selector RPL ELSE #GP(gate
  16364.       selector);
  16365.     Task Gate must be present ELSE #NP(gate selector);
  16366.     Examine selector to TSS, given in Task Gate descriptor:
  16367.     Must specify global in the local/global bit ELSE #GP(TSS selector);
  16368.     Index must be within GDT limits ELSE #GP(TSS selector);
  16369.     Descriptor AR byte must specify available TSS (bottom bits 00001);
  16370.        ELSE #GP(TSS selector);
  16371.     Task State Segment must be present ELSE #NP(TSS selector);
  16372.  SWITCH-TASKS (without nesting) to TSS;
  16373.  Instruction pointer must be within code-segment limit ELSE #GP(0);
  16374.  
  16375.  TASK-STATE-SEGMENT:
  16376.     TSS DPL must be ≥ CPL ELSE #GP(TSS selector);
  16377.     TSS DPL must be ≥ TSS selector RPL ELSE #GP(TSS selector);
  16378.     Descriptor AR byte must specify available TSS (bottom bits 00001)
  16379.        ELSE #GP(TSS selector);
  16380.     Task State Segment must be present ELSE #NP(TSS selector);
  16381.     SWITCH-TASKS (without nesting) to TSS;
  16382.     Instruction pointer must be within code-segment limit ELSE #GP(0);
  16383.  
  16384.  Description
  16385.  
  16386.  The JMP instruction transfers control to a different point in the
  16387.  instruction stream without recording return information.
  16388.  
  16389.  The action of the various forms of the instruction are shown below.
  16390.  
  16391.  Jumps with destinations of type r/m16, r/m32, rel16, and rel32 are near
  16392.  jumps and do not involve changing the segment register value.
  16393.  
  16394.  The JMP rel16 and JMP rel32 forms of the instruction add an offset to
  16395.  the address of the instruction following the JMP to determine the
  16396.  destination. The rel16 form is used when the instruction's operand-size
  16397.  attribute is 16 bits (segment size attribute 16 only); rel32 is used when
  16398.  the operand-size attribute is 32 bits (segment size attribute 32 only). The
  16399.  result is stored in the 32-bit EIP register. With rel16, the upper 16 bits
  16400.  of EIP are cleared, which results in an offset whose value does not exceed
  16401.  16 bits.
  16402.  
  16403.  JMP r/m16 and JMP r/m32 specifies a register or memory location from which
  16404.  the absolute offset from the procedure is fetched. The offset fetched from
  16405.  r/m is 32 bits for an operand-size attribute of 32 bits (r/m32), or 16 bits
  16406.  for an operand-size attribute of 16 bits (r/m16).
  16407.  
  16408.  The JMP ptr16:16 and ptr16:32 forms of the instruction use a four-byte
  16409.  or six-byte operand as a long pointer to the destination. The JMP
  16410.  and  forms fetch the long pointer from the memory location
  16411.  specified (indirection). In Real Address Mode or Virtual 8086 Mode,
  16412.  the long pointer provides 16 bits for the CS register and 16 or 32 bits
  16413.  for the EIP register (depending on the operand-size attribute). In
  16414.  Protected Mode, both long pointer forms consult the Access Rights (AR)
  16415.  byte in the descriptor indexed by the selector part of the long pointer.
  16416.  
  16417.  Depending on the value of the AR byte, the jump will perform one of
  16418.  the following types of control transfers:
  16419.  
  16420.    ■  A jump to a code segment at the same privilege level
  16421.    ■  A task switch
  16422.  
  16423.  For more information on protected mode control transfers, refer to
  16424.  Chapter 6 and Chapter 7.
  16425.  
  16426.  Flags Affected
  16427.  
  16428.  All if a task switch takes place; none if no task switch occurs
  16429.  
  16430.  Protected Mode Exceptions
  16431.  
  16432.  Far jumps: #GP, #NP, #SS, and #TS, as indicated in the list above.
  16433.  
  16434.  Near direct jumps: #GP(0) if procedure location is beyond the code
  16435.  segment limits.
  16436.  
  16437.  Near indirect jumps: #GP(0) for an illegal memory operand effective
  16438.  address in the CS, DS, ES, FS, or GS segments: #SS(0) for an illegal
  16439.  address in the SS segment; #GP if the indirect offset obtained is beyond
  16440.  the code segment limits; #PF(fault-code) for a page fault.
  16441.  
  16442.  Real Address Mode Exceptions
  16443.  
  16444.  Interrupt 13 if any part of the operand would be outside of the effective
  16445.  address space from 0 to 0FFFFH
  16446.  
  16447.  Virtual 8086 Mode Exceptions
  16448.  
  16449.  Same exceptions as under Real Address Mode; #PF(fault-code) for a
  16450.  page fault
  16451.  
  16452.  
  16453.  LAHF ── Load Flags into AH Register
  16454.  
  16455.  Opcode  Instruction   Clocks   Description
  16456.  
  16457.  9F      LAHF          2        Load: AH = flags SF ZF xx AF xx PF xx CF
  16458.  
  16459.  
  16460.  Operation
  16461.  
  16462.  AH  SF:ZF:xx:AF:xx:PF:xx:CF;
  16463.  
  16464.  Description
  16465.  
  16466.  LAHF transfers the low byte of the flags word to AH. The bits, from
  16467.  MSB to LSB, are sign, zero, indeterminate, auxiliary, carry,
  16468.  indeterminate, parity, indeterminate, and carry.
  16469.  
  16470.  Flags Affected
  16471.  
  16472.  None
  16473.  
  16474.  Protected Mode Exceptions
  16475.  
  16476.  None
  16477.  
  16478.  Real Address Mode Exceptions
  16479.  
  16480.  None
  16481.  
  16482.  Virtual 8086 Mode Exceptions
  16483.  
  16484.  None
  16485.  
  16486.  
  16487.  LAR ── Load Access Rights Byte
  16488.  
  16489.  Opcode        Instruction      Clocks      Description
  16490.  
  16491.  0F  02 /r     LAR r16,r/m16    pm=15/16    r16  r/m16 masked by FF00
  16492.  0F  02 /r     LAR r32,r/m32    pm=15/16    r32  r/m32 masked by 00FxFF00
  16493.  
  16494.  
  16495.  Description
  16496.  
  16497.  The LAR instruction stores a marked form of the second doubleword of
  16498.  the descriptor for the source selector if the selector is visible at the
  16499.  CPL (modified by the selector's RPL) and is a valid descriptor type. The
  16500.  destination register is loaded with the high-order doubleword of the
  16501.  descriptor masked by 00FxFF00, and ZF is set to 1. The x indicates that the
  16502.  four bits corresponding to the upper four bits of the limit are undefined in
  16503.  the value loaded by LAR. If the selector is invisible or of the wrong type,
  16504.  ZF is cleared.
  16505.  
  16506.  If the 32-bit operand size is specified, the entire 32-bit value is loaded
  16507.  into the 32-bit destination register. If the 16-bit operand size is
  16508.  specified, the lower 16-bits of this value are stored in the 16-bit
  16509.  destination register.
  16510.  
  16511.  All code and data segment descriptors are valid for LAR.
  16512.  
  16513.  The valid special segment and gate descriptor types for LAR are given
  16514.  in the following table:
  16515.  
  16516.  Type   Name                     Valid/Invalid
  16517.  
  16518.    0    Invalid                  Invalid
  16519.    1    Available 80286 TSS      Valid
  16520.    2    LDT                      Valid
  16521.    3    Busy 80286 TSS           Valid
  16522.    4    80286 call gate          Valid
  16523.    5    80286/80386 task gate    Valid
  16524.    6    80286 trap gate          Valid
  16525.    7    80286 interrupt gate     Valid
  16526.    8    Invalid                  Invalid
  16527.    9    Available 80386 TSS      Valid
  16528.    A    Invalid                  Invalid
  16529.    B    Busy 80386 TSS           Valid
  16530.    C    80386 call gate          Valid
  16531.    D    Invalid                  Invalid
  16532.    E    80386 trap gate          Valid
  16533.    F    80386 interrupt gate     Valid
  16534.  
  16535.  Flags Affected
  16536.  
  16537.  ZF as described above
  16538.  
  16539.  Protected Mode Exceptions
  16540.  
  16541.  #GP(0) for an illegal memory operand effective address in the CS, DS,
  16542.  ES, FS, or GS segments; #SS(0) for an illegal address in the SS segment;
  16543.  #PF(fault-code) for a page fault
  16544.  
  16545.  Real Address Mode Exceptions
  16546.  
  16547.  Interrupt 6; LAR is unrecognized in Real Address Mode
  16548.  
  16549.  Virtual 8086 Mode Exceptions
  16550.  
  16551.  Same exceptions as in Real Address Mode
  16552.  
  16553.  
  16554.  LEA ── Load Effective Address
  16555.  
  16556.  Opcode  Instruction  Clocks  Description
  16557.  
  16558.  8D  /r  LEA r16,m    2       Store effective address for m in register r16
  16559.  8D  /r  LEA r32,m    2       Store effective address for m in register r32
  16560.  8D  /r  LEA r16,m    2       Store effective address for m in register r16
  16561.  8D  /r  LEA r32,m    2       Store effective address for m in register r32
  16562.  
  16563.  
  16564.  Operation
  16565.  
  16566.  IF OperandSize = 16 AND AddressSize = 16
  16567.  THEN r16  Addr(m);
  16568.  ELSE
  16569.     IF OperandSize = 16 AND AddressSize = 32
  16570.     THEN
  16571.        r16  Truncate_to_16bits(Addr(m));   (* 32-bit address *)
  16572.     ELSE
  16573.        IF OperandSize = 32 AND AddressSize = 16
  16574.        THEN
  16575.           r32  Truncate_to_16bits(Addr(m));
  16576.        ELSE
  16577.           IF OperandSize = 32 AND AddressSize = 32
  16578.           THEN  r32  Addr(m);
  16579.           FI;
  16580.        FI;
  16581.     FI;
  16582.  FI;
  16583.  
  16584.  Description
  16585.  
  16586.  LEA calculates the effective address (offset part) and stores it in the
  16587.  specified register. The operand-size attribute of the instruction
  16588.  (represented by OperandSize in the algorithm under "Operation" above) is
  16589.  determined by the chosen register. The address-size attribute (represented
  16590.  by AddressSize) is determined by the USE attribute of the segment containing
  16591.  the second operand. The address-size and operand-size attributes affect the
  16592.  action performed by LEA, as follows:
  16593.  
  16594.  Operand Size  Address Size  Action Performed
  16595.  
  16596.      16            16        16-bit effective address is calculated and
  16597.                              stored in requested 16-bit register
  16598.                              destination.
  16599.  
  16600.      16            32        32-bit effective address is calculated. The
  16601.                              lower 16 bits of the address are stored in
  16602.                              the requested 16-bit register destination.
  16603.  
  16604.      32            16        16-bit effective address is calculated. The
  16605.                              16-bit address is zero-extended and stored
  16606.                              in the requested 32-bit register destination.
  16607.  
  16608.      32            32        32-bit effective address is calculated and
  16609.                              stored in the requested 32-bit register
  16610.                              destination.
  16611.  
  16612.  Flags Affected
  16613.  
  16614.  None
  16615.  
  16616.  Protected Mode Exceptions
  16617.  
  16618.  #UD if the second operand is a register
  16619.  
  16620.  Real Address Mode Exceptions
  16621.  
  16622.  Interrupt 6 if the second operand is a register
  16623.  
  16624.  Virtual 8086 Mode Exceptions
  16625.  
  16626.  Same exceptions as in Real Address Mode
  16627.  
  16628.  
  16629.  LEAVE ── High Level Procedure Exit
  16630.  
  16631.  Opcode  Instruction  Clocks  Description
  16632.  
  16633.  C9      LEAVE        4       Set SP to BP, then pop BP
  16634.  C9      LEAVE        4       Set ESP to EBP, then pop EBP
  16635.  
  16636.  
  16637.  Operation
  16638.  
  16639.  IF StackAddrSize = 16
  16640.  THEN
  16641.     SP  BP;
  16642.  ELSE (* StackAddrSize = 32 *)
  16643.     ESP  EBP;
  16644.  FI;
  16645.  IF OperandSize = 16
  16646.  THEN
  16647.     BP  Pop();
  16648.  ELSE (* OperandSize = 32 *)
  16649.     EBP  Pop();
  16650.  FI;
  16651.  
  16652.  Description
  16653.  
  16654.  LEAVE reverses the actions of the ENTER instruction. By copying the
  16655.  frame pointer to the stack pointer, LEAVE releases the stack space used
  16656.  by a procedure for its local variables. The old frame pointer is popped
  16657.  into BP or EBP, restoring the caller's frame. A subsequent RET
  16658.  instruction removes any arguments pushed onto the stack of the exiting
  16659.  procedure.
  16660.  
  16661.  Flags Affected
  16662.  
  16663.  None
  16664.  
  16665.  Protected Mode Exceptions
  16666.  
  16667.  #SS(0) if BP does not point to a location within the limits of the current
  16668.  stack segment
  16669.  
  16670.  Real Address Mode Exceptions
  16671.  
  16672.  Interrupt 13 if any part of the operand would lie outside of the effective
  16673.  address space from 0 to 0FFFFH
  16674.  
  16675.  Virtual 8086 Mode Exceptions
  16676.  
  16677.  Same exceptions as in Real Address Mode
  16678.  
  16679.  
  16680.  LGDT/LIDT ── Load Global/Interrupt Descriptor Table Register
  16681.  
  16682.  Opcode       Instruction      Clocks        Description
  16683.  
  16684.  0F  01 /2    LGDT m16&32      11            Load m into GDTR
  16685.  0F  01 /3    LIDT m16&32      11            Load m into IDTR
  16686.  
  16687.  
  16688.  Operation
  16689.  
  16690.  IF instruction = LIDT
  16691.  THEN
  16692.     IF OperandSize = 16
  16693.     THEN IDTR.Limit:Base  m16:24 (* 24 bits of base loaded *)
  16694.     ELSE IDTR.Limit:Base  m16:32
  16695.     FI;
  16696.  ELSE (* instruction = LGDT *)
  16697.     IF OperandSize = 16
  16698.     THEN GDTR.Limit:Base  m16:24 (* 24 bits of base loaded *)
  16699.     ELSE GDTR.Limit:Base  m16:32;
  16700.     FI;
  16701.  FI;
  16702.  
  16703.  Description
  16704.  
  16705.  The LGDT and LIDT instructions load a linear base address and limit
  16706.  value from a six-byte data operand in memory into the GDTR or IDTR,
  16707.  respectively. If a 16-bit operand is used with LGDT or LIDT, the
  16708.  register is loaded with a 16-bit limit and a 24-bit base, and the
  16709.  high-order eight bits of the six-byte data operand are not used. If a 32-bit
  16710.  operand is used, a 16-bit limit and a 32-bit base is loaded; the high-order
  16711.  eight bits of the six-byte operand are used as high-order base address bits.
  16712.  
  16713.  The SGDT and SIDT instructions always store into all 48 bits of the
  16714.  six-byte data operand. With the 80286, the upper eight bits are undefined
  16715.  after SGDT or SIDT is executed. With the 80386, the upper eight bits
  16716.  are written with the high-order eight address bits, for both a 16-bit
  16717.  operand and a 32-bit operand. If LGDT or LIDT is used with a 16-bit
  16718.  operand to load the register stored by SGDT or SIDT, the upper eight
  16719.  bits are stored as zeros.
  16720.  
  16721.  LGDT and LIDT appear in operating system software; they are not used
  16722.  in application programs. They are the only instructions that directly load
  16723.  a linear address (i.e., not a segment relative address) in 80386 Protected
  16724.  Mode.
  16725.  
  16726.  Flags Affected
  16727.  
  16728.  None
  16729.  
  16730.  Protected Mode Exceptions
  16731.  
  16732.  #GP(0) if the current privilege level is not 0; #UD if the source operand
  16733.  is a register; #GP(0) for an illegal memory operand effective address in
  16734.  the CS, DS, ES, FS, or GS segments; #SS(0) for an illegal address in
  16735.  the SS segment; #PF(fault-code) for a page fault
  16736.  
  16737.  Real Address Mode Exceptions
  16738.  
  16739.  Interrupt 13 if any part of the operand would lie outside of the effective
  16740.  address space from 0 to 0FFFFH; Interrupt 6 if the source operand is a
  16741.  register
  16742.  
  16743.  ───────────────────────────────────────────────────────────────────────────
  16744.  Note:
  16745.    These instructions are valid in Real Address Mode to allow
  16746.    power-up initialization for Protected Mode
  16747.  ───────────────────────────────────────────────────────────────────────────
  16748.  
  16749.  Virtual 8086 Mode Exceptions
  16750.  
  16751.  Same exceptions as in Real Address Mode; #PF(fault-code) for a page
  16752.  fault
  16753.  
  16754.  
  16755.  LGS/LSS/LDS/LES/LFS ── Load Full Pointer
  16756.  
  16757.  Opcode      Instruction      Clocks   Description
  16758.  
  16759.  C5  /r      LDS r16,m16:16   7,p=22   Load DS:r16 with pointer from memory
  16760.  C5  /r      LDS r32,m16:32   7,p=22   Load DS:r32 with pointer from memory
  16761.  0F  B2 /r   LSS r16,m16:16   7,p=22   Load SS:r16 with pointer from memory
  16762.  0F  B2 /r   LSS r32,m16:32   7,p=22   Load SS:r32 with pointer from memory
  16763.  C4  /r      LES r16,m16:16   7,p=22   Load ES:r16 with pointer from memory
  16764.  C4  /r      LES r32,m16:32   7,p=22   Load ES:r32 with pointer from memory
  16765.  0F  B4 /r   LFS r16,m16:16   7,p=25   Load FS:r16 with pointer from memory
  16766.  0F  B4 /r   LFS r32,m16:32   7,p=25   Load FS:r32 with pointer from memory
  16767.  0F  B5 /r   LGS r16,m16:16   7,p=25   Load GS:r16 with pointer from memory
  16768.  0F  B5 /r   LGS r32,m16:32   7,p=25   Load GS:r32 with pointer from memory
  16769.  
  16770.  
  16771.  Operation
  16772.  
  16773.  CASE instruction OF
  16774.     LSS: Sreg is SS; (* Load SS register *)
  16775.     LDS: Sreg is DS; (* Load DS register *)
  16776.     LES: Sreg is ES; (* Load ES register *)
  16777.     LFS: Sreg is FS; (* Load FS register *)
  16778.     LGS: Sreg is DS; (* Load GS register *)
  16779.  ESAC;
  16780.  IF (OperandSize = 16)
  16781.  THEN
  16782.     r16  [Effective Address]; (* 16-bit transfer *)
  16783.     Sreg  [Effective Address + 2]; (* 16-bit transfer *)
  16784.     (* In Protected Mode, load the descriptor into the segment register *)
  16785.  ELSE (* OperandSize = 32 *)
  16786.     r32  [Effective Address]; (* 32-bit transfer *)
  16787.     Sreg  [Effective Address + 4]; (* 16-bit transfer *)
  16788.     (* In Protected Mode, load the descriptor into the segment register *)
  16789.  FI;
  16790.  
  16791.  Description
  16792.  
  16793.  These instructions read a full pointer from memory and store it in the
  16794.  selected segment register:register pair. The full pointer loads 16 bits
  16795.  into the segment register SS, DS, ES, FS, or GS. The other register loads 32
  16796.  bits if the operand-size attribute is 32 bits, or loads 16 bits if the
  16797.  operand-size attribute is 16 bits. The other 16- or 32-bit register to be
  16798.  loaded is determined by the r16 or r32 register operand specified.
  16799.  
  16800.  When an assignment is made to one of the segment registers, the
  16801.  descriptor is also loaded into the segment register. The data for the
  16802.  register is obtained from the descriptor table entry for the selector
  16803.  given.
  16804.  
  16805.  A null selector (values 0000-0003) can be loaded into DS, ES, FS, or
  16806.  GS registers without causing a protection exception. (Any subsequent
  16807.  reference to a segment whose corresponding segment register is loaded
  16808.  with a null selector to address memory causes a #GP(0) exception. No
  16809.  memory reference to the segment occurs.)
  16810.  
  16811.  The following is a listing of the Protected Mode checks and actions taken in
  16812.  the loading of a segment register:
  16813.  
  16814.  IF SS is loaded:
  16815.     IF selector is null THEN #GP(0); FI;
  16816.     Selector index must be within its descriptor table limits ELSE
  16817.        #GP(selector);
  16818.     Selector's RPL must equal CPL ELSE #GP(selector);
  16819.     AR byte must indicate a writable data segment ELSE #GP(selector);
  16820.     DPL in the AR byte must equal CPL ELSE #GP(selector);
  16821.     Segment must be marked present ELSE #SS(selector);
  16822.     Load SS with selector;
  16823.     Load SS with descriptor;
  16824.  IF DS, ES, FS, or GS is loaded with non-null selector:
  16825.     Selector index must be within its descriptor table limits ELSE
  16826.        #GP(selector);
  16827.     AR byte must indicate data or readable code segment ELSE
  16828.        #GP(selector);
  16829.     IF data or nonconforming code
  16830.     THEN both the RPL and the CPL must be less than or equal to DPL in
  16831.        AR byte;
  16832.     ELSE #GP(selector);
  16833.     Segment must be marked present ELSE #NP(selector);
  16834.  Load segment register with selector and RPL bits;
  16835.  Load segment register with descriptor;
  16836.  IF DS, ES, FS or GS is loaded with a null selector:
  16837.     Clear descriptor valid bit;
  16838.  
  16839.  Flags Affected
  16840.  
  16841.  None
  16842.  
  16843.  Protected Mode Exceptions
  16844.  
  16845.  #GP(0) for an illegal memory operand effective address in the CS, DS,
  16846.  ES, FS, or GS segments; #SS(0) for an illegal address in the SS segment;
  16847.  the second operand must be a memory operand, not a register; #GP(0)
  16848.  if a null selector is loaded into SS; #PF(fault-code) for a page fault
  16849.  
  16850.  Real Address Mode Exceptions
  16851.  
  16852.  The second operand must be a memory operand, not a register; Interrupt
  16853.  13 if any part of the operand would lie outside of the effective address
  16854.  space from 0 to 0FFFFH
  16855.  
  16856.  Virtual 8086 Mode Exceptions
  16857.  
  16858.  Same exceptions as in Real Address Mode; #PF(fault-code) for a page
  16859.  fault
  16860.  
  16861.  
  16862.  LLDT ── Load Local Descriptor Table Register
  16863.  
  16864.  Opcode      Instruction      Clocks   Description
  16865.  
  16866.  0F  00 /2   LLDT r/m16       20       Load selector r/m16 into LDTR
  16867.  
  16868.  
  16869.  Operation
  16870.  
  16871.  LDTR  SRC;
  16872.  
  16873.  Description
  16874.  
  16875.  LLDT loads the Local Descriptor Table register (LDTR). The word
  16876.  operand (memory or register) to LLDT should contain a selector to the
  16877.  Global Descriptor Table (GDT). The GDT entry should be a Local Descriptor
  16878.  Table. If so, then the LDTR is loaded from the entry. The descriptor
  16879.  registers DS, ES, SS, FS, GS, and CS are not affected. The LDT field in the
  16880.  task state segment does not change.
  16881.  
  16882.  The selector operand can be 0; if so, the LDTR is marked invalid. All
  16883.  descriptor references (except by the LAR, VERR, VERW or LSL
  16884.  instructions) cause a #GP fault.
  16885.  
  16886.  LLDT is used in operating system software; it is not used in application
  16887.  programs.
  16888.  
  16889.  Flags Affected
  16890.  
  16891.  None
  16892.  
  16893.  Protected Mode Exceptions
  16894.  
  16895.  #GP(0) if the current privilege level is not 0; #GP(selector) if the
  16896.  selector operand does not point into the Global Descriptor Table, or if the
  16897.  entry in the GDT is not a Local Descriptor Table; #NP(selector) if the
  16898.  LDT descriptor is not present; #GP(0) for an illegal memory operand
  16899.  effective address in the CS, DS, ES, FS, or GS segments; #SS(0) for an
  16900.  illegal address in the SS segment; #PF(fault-code) for a page fault
  16901.  
  16902.  Real Address Mode Exceptions
  16903.  
  16904.  Interrupt 6; LLDT is not recognized in Real Address Mode
  16905.  
  16906.  Virtual 8086 Mode Exceptions
  16907.  
  16908.  Same exceptions as in Real Address Mode (because the instruction is
  16909.  not recognized, it will not execute or perform a memory reference)
  16910.  
  16911.  Note
  16912.  
  16913.  The operand-size attribute has no effect on this instruction.
  16914.  
  16915.  
  16916.  LMSW ── Load Machine Status Word
  16917.  
  16918.  Opcode      Instruction      Clocks   Description
  16919.  
  16920.  0F  01 /6   LMSW r/m16       10/13    Load r/m16 in machine status word
  16921.  
  16922.  
  16923.  Operation
  16924.  
  16925.  MSW  r/m16; (* 16 bits is stored in the machine status word *)
  16926.  
  16927.  Description
  16928.  
  16929.  LMSW loads the machine status word (part of CR0) from the source
  16930.  operand. This instruction can be used to switch to Protected Mode; if so,
  16931.  it must be followed by an intrasegment jump to flush the instruction
  16932.  queue. LMSW will not switch back to Real Address Mode.
  16933.  
  16934.  LMSW is used only in operating system software. It is not used in
  16935.  application programs.
  16936.  
  16937.  Flags Affected
  16938.  
  16939.  None
  16940.  
  16941.  Protected Mode Exceptions
  16942.  
  16943.  #GP(0) if the current privilege level is not 0; #GP(0) for an illegal
  16944.  memory operand effective address in the CS, DS, ES, FS, or GS
  16945.  segments; #SS(0) for an illegal address in the SS segment; #PF(fault-code)
  16946.  for a page fault
  16947.  
  16948.  Real Address Mode Exceptions
  16949.  
  16950.  Interrupt 13 if any part of the operand would lie outside of the effective
  16951.  address space from 0 to 0FFFFH
  16952.  
  16953.  Virtual 8086 Mode Exceptions
  16954.  
  16955.  Same exceptions as in Real Address Mode; #PF(fault-code) for a page
  16956.  fault
  16957.  
  16958.  Notes
  16959.  
  16960.  The operand-size attribute has no effect on this instruction. This
  16961.  instruction is provided for compatibility with the 80286; 80386 programs
  16962.  should use MOV CR0, ... instead.
  16963.  
  16964.  
  16965.  LOCK ── Assert LOCK# Signal Prefix
  16966.  
  16967.  Opcode  Instruction  Clocks  Description
  16968.  
  16969.  F0      LOCK         0       Assert LOCK# signal for the next instruction
  16970.  
  16971.  
  16972.  Description
  16973.  
  16974.  The LOCK prefix causes the LOCK# signal of the 80386 to be asserted
  16975.  during execution of the instruction that follows it. In a multiprocessor
  16976.  environment, this signal can be used to ensure that the 80386 has
  16977.  exclusive use of any shared memory while LOCK# is asserted. The
  16978.  read-modify-write sequence typically used to implement test-and-set on the
  16979.  80386 is the BTS instruction.
  16980.  
  16981.  The LOCK prefix functions only with the following instructions:
  16982.  
  16983.  BT, BTS, BTR, BTC                   mem, reg/imm
  16984.  XCHG                                reg, mem
  16985.  XCHG                                mem, reg
  16986.  ADD, OR, ADC, SBB, AND, SUB, XOR    mem, reg/imm
  16987.  NOT, NEG, INC, DEC                  mem
  16988.  
  16989.  An undefined opcode trap will be generated if a LOCK prefix is used
  16990.  with any instruction not listed above.
  16991.  
  16992.  XCHG always asserts LOCK# regardless of the presence or absence of
  16993.  the LOCK prefix.
  16994.  
  16995.  The integrity of the LOCK is not affected by the alignment of the
  16996.  memory field. Memory locking is observed for arbitrarily misaligned
  16997.  fields.
  16998.  
  16999.  Locked access is not assured if another 80386 processor is executing an
  17000.  instruction concurrently that has one of the following characteristics:
  17001.  
  17002.    ■  Is not preceded by a LOCK prefix
  17003.  
  17004.    ■  Is not one of the instructions in the preceding list
  17005.  
  17006.    ■  Specifies a memory operand that does not exactly overlap the
  17007.       destination operand. Locking is not guaranteed for partial overlap,
  17008.       even if one memory operand is wholly contained within another.
  17009.  
  17010.  Flags Affected
  17011.  
  17012.  None
  17013.  
  17014.  Protected Mode Exceptions
  17015.  
  17016.  #UD if LOCK is used with an instruction not listed in the "Description"
  17017.  section above; other exceptions can be generated by the subsequent
  17018.  (locked) instruction
  17019.  
  17020.  Real Address Mode Exceptions
  17021.  
  17022.  Interrupt 6 if LOCK is used with an instruction not listed in the
  17023.  "Description" section above; exceptions can still be generated by the
  17024.  subsequent (locked) instruction
  17025.  
  17026.  Virtual 8086 Mode Exceptions
  17027.  
  17028.  #UD if LOCK is used with an instruction not listed in the "Description"
  17029.  section above; exceptions can still be generated by the subsequent (locked)
  17030.  instruction
  17031.  
  17032.  
  17033.  LODS/LODSB/LODSW/LODSD ── Load String Operand
  17034.  
  17035.  Opcode  Instruction   Clocks   Description
  17036.  
  17037.  AC      LODS m8       5        Load byte [(E)SI] into AL
  17038.  AD      LODS m16      5        Load word [(E)SI] into AX
  17039.  AD      LODS m32      5        Load dword [(E)SI] into EAX
  17040.  AC      LODSB         5        Load byte DS:[(E)SI] into AL
  17041.  AD      LODSW         5        Load word DS:[(E)SI] into AX
  17042.  AD      LODSD         5        Load dword DS:[(E)SI] into EAX
  17043.  
  17044.  
  17045.  Operation
  17046.  
  17047.  IF AddressSize = 16
  17048.  THEN use SI for source-index
  17049.  ELSE (* AddressSize = 32 *)
  17050.     use ESI for source-index;
  17051.  FI;
  17052.  IF byte type of instruction
  17053.  THEN
  17054.     AL  [source-index]; (* byte load *)
  17055.     IF DF = 0 THEN IncDec  1 ELSE IncDec  -1; FI;
  17056.  ELSE
  17057.     IF OperandSize = 16
  17058.     THEN
  17059.        AX  [source-index]; (* word load *)
  17060.        IF DF = 0 THEN IncDec  2 ELSE IncDec  -2; FI;
  17061.     ELSE (* OperandSize = 32 *)
  17062.        EAX  [source-index]; (* dword load *)
  17063.        IF DF = 0 THEN IncDec  4 ELSE IncDec  -4; FI;
  17064.     FI;
  17065.  FI;
  17066.  source-index  source-index + IncDec
  17067.  
  17068.  Description
  17069.  
  17070.  LODS loads the AL, AX, or EAX register with the memory byte, word,
  17071.  or doubleword at the location pointed to by the source-index register.
  17072.  After the transfer is made, the source-index register is automatically
  17073.  advanced. If the direction flag is 0 (CLD was executed), the source index
  17074.  increments; if the direction flag is 1 (STD was executed), it decrements.
  17075.  The increment or decrement is 1 if a byte is loaded, 2 if a word is loaded,
  17076.  or 4 if a doubleword is loaded.
  17077.  
  17078.  If the address-size attribute for this instruction is 16 bits, SI is used
  17079.  for the source-index register; otherwise the address-size attribute is 32
  17080.  bits, and the ESI register is used. The address of the source data is
  17081.  determined solely by the contents of ESI/SI. Load the correct index value
  17082.  into SI before executing the LODS instruction. LODSB, LODSW, LODSD are
  17083.  synonyms for the byte, word, and doubleword LODS instructions.
  17084.  
  17085.  LODS can be preceded by the REP prefix; however, LODS is used more typically
  17086.  within a LOOP construct, because further processing of the data moved into
  17087.  EAX, AX, or AL is usually necessary.
  17088.  
  17089.  Flags Affected
  17090.  
  17091.  None
  17092.  
  17093.  Protected Mode Exceptions
  17094.  
  17095.  #GP(0) for an illegal memory operand effective address in the CS, DS,
  17096.  ES, FS, or GS segments; #SS(0) for an illegal address in the SS segment;
  17097.  #PF(fault-code) for a page fault
  17098.  
  17099.  Real Address Mode Exceptions
  17100.  
  17101.  Interrupt 13 if any part of the operand would lie outside of the effective
  17102.  address space from 0 to 0FFFFH
  17103.  
  17104.  Virtual 8086 Mode Exceptions
  17105.  
  17106.  Same exceptions as in Real Address Mode; #PF(fault-code) for a page
  17107.  fault
  17108.  
  17109.  
  17110.  LOOP/LOOPcond ── Loop Control with CX Counter
  17111.  
  17112.  Opcode   Instruction  Clocks  Description
  17113.  
  17114.  E2  cb   LOOP rel8    11+m    DEC count; jump short if count <> 0
  17115.  E1  cb   LOOPE rel8   11+m    DEC count; jump short if count <> 0 and ZF=1
  17116.  E1  cb   LOOPZ rel8   11+m    DEC count; jump short if count <> 0 and ZF=1
  17117.  E0  cb   LOOPNE rel8  11+m    DEC count; jump short if count <> 0 and ZF=0
  17118.  E0  cb   LOOPNZ rel8  11+m    DEC count; jump short if count <> 0 and ZF=0
  17119.  
  17120.  
  17121.  Operation
  17122.  
  17123.  IF AddressSize = 16 THEN CountReg is CX ELSE CountReg is ECX; FI;
  17124.  CountReg  CountReg - 1;
  17125.  IF instruction <> LOOP
  17126.  THEN
  17127.     IF (instruction = LOOPE) OR (instruction = LOOPZ)
  17128.     THEN BranchCond  (ZF = 1) AND (CountReg <> 0);
  17129.     FI;
  17130.     IF (instruction = LOOPNE) OR (instruction = LOOPNZ)
  17131.     THEN BranchCond  (ZF = 0) AND (CountReg <> 0);
  17132.     FI;
  17133.  FI;
  17134.  
  17135.  IF BranchCond
  17136.  THEN
  17137.     IF OperandSize = 16
  17138.     THEN
  17139.        IP  IP + SignExtend(rel8);
  17140.     ELSE (* OperandSize = 32 *)
  17141.        EIP  EIP + SignExtend(rel8);
  17142.     FI;
  17143.  FI;
  17144.  
  17145.  Description
  17146.  
  17147.  LOOP decrements the count register without changing any of the flags.
  17148.  Conditions are then checked for the form of LOOP being used. If the
  17149.  conditions are met, a short jump is made to the label given by the operand
  17150.  to LOOP. If the address-size attribute is 16 bits, the CX register is used
  17151.  as the count register; otherwise the ECX register is used. The operand
  17152.  of LOOP must be in the range from 128 (decimal) bytes before the
  17153.  instruction to 127 bytes ahead of the instruction.
  17154.  
  17155.  The LOOP instructions provide iteration control and combine loop index
  17156.  management with conditional branching. Use the LOOP instruction by
  17157.  loading an unsigned iteration count into the count register, then code the
  17158.  LOOP at the end of a series of instructions to be iterated. The
  17159.  destination of LOOP is a label that points to the beginning of the
  17160.  iteration.
  17161.  
  17162.  Flags Affected
  17163.  
  17164.  None
  17165.  
  17166.  Protected Mode Exceptions
  17167.  
  17168.  #GP(0) if the offset jumped to is beyond the limits of the current code
  17169.  segment
  17170.  
  17171.  Real Address Mode Exceptions
  17172.  
  17173.  None
  17174.  
  17175.  Virtual 8086 Mode Exceptions
  17176.  
  17177.  None
  17178.  
  17179.  
  17180.  LSL ── Load Segment Limit
  17181.  
  17182.  Opcode       Instruction      Clocks      Description
  17183.  
  17184.  0F  03 /r    LSL r16,r/m16    pm=20/21    Load: r16  segment limit,
  17185.                                            selector r/m16 (byte granular)
  17186.  0F  03 /r    LSL r32,r/m32    pm=20/21    Load: r32  segment limit,
  17187.                                            selector r/m32 (byte granular)
  17188.  0F  03 /r    LSL r16,r/m16    pm=25/26    Load: r16  segment limit,
  17189.                                            selector r/m16 (page granular)
  17190.  0F  03 /r    LSL r32,r/m32    pm=25/26    Load: r32  segment limit,
  17191.                                            selector r/m32 (page granular)
  17192.  
  17193.  
  17194.  Description
  17195.  
  17196.  The LSL instruction loads a register with an unscrambled segment limit,
  17197.  and sets ZF to 1, provided that the source selector is visible at the CPL
  17198.  weakened by RPL, and that the descriptor is a type accepted by LSL.
  17199.  Otherwise, ZF is cleared to 0, and the destination register is unchanged.
  17200.  The segment limit is loaded as a byte granular value. If the descriptor
  17201.  has a page granular segment limit, LSL will translate it to a byte limit
  17202.  before loading it in the destination register (shift left 12 the 20-bit
  17203.  "raw" limit from descriptor, then OR with 00000FFFH).
  17204.  
  17205.  The 32-bit forms of this instruction store the 32-bit byte granular limit
  17206.  in the 16-bit destination register.
  17207.  
  17208.  Code and data segment descriptors are valid for LSL.
  17209.  
  17210.  The valid special segment and gate descriptor types for LSL are given
  17211.  in the following table:
  17212.  
  17213.  Type   Name                      Valid/Invalid
  17214.  
  17215.    0    Invalid                   Invalid
  17216.    1    Available 80286 TSS       Valid
  17217.    2    LDT                       Valid
  17218.    3    Busy 80286 TSS            Valid
  17219.    4    80286 call gate           Invalid
  17220.    5    80286/80386 task gate     Invalid
  17221.    6    80286 trap gate           Invalid
  17222.    7    80286 interrupt gate      Invalid
  17223.    8    Invalid                   Valid
  17224.    9    Available 80386 TSS       Valid
  17225.    A    Invalid                   Invalid
  17226.    B    Busy 80386 TSS            Valid
  17227.    C    80386 call gate           Invalid
  17228.    D    Invalid                   Invalid
  17229.    E    80386 trap gate           Invalid
  17230.    F    80386 interrupt gate      Invalid
  17231.  
  17232.  Flags Affected
  17233.  
  17234.  ZF as described above
  17235.  
  17236.  Protected Mode Exceptions
  17237.  
  17238.  #GP(0) for an illegal memory operand effective address in the CS, DS,
  17239.  ES, FS, or GS segments; #SS(0) for an illegal address in the SS segment;
  17240.  #PF(fault-code) for a page fault
  17241.  
  17242.  Real Address Mode Exceptions
  17243.  
  17244.  Interrupt 6; LSL is not recognized in Real Address Mode
  17245.  
  17246.  Virtual 8086 Mode Exceptions
  17247.  
  17248.  Same exceptions as in Real Address Mode
  17249.  
  17250.  
  17251.  LTR ── Load Task Register
  17252.  
  17253.  Opcode       Instruction    Clocks    Description
  17254.  
  17255.  0F  00 /3    LTR r/m16      pm=23/27  Load EA word into task register
  17256.  
  17257.  
  17258.  Description
  17259.  
  17260.  LTR loads the task register from the source register or memory location
  17261.  specified by the operand. The loaded task state segment is marked busy.
  17262.  A task switch does not occur.
  17263.  
  17264.  LTR is used only in operating system software; it is not used in
  17265.  application programs.
  17266.  
  17267.  Flags Affected
  17268.  
  17269.  None
  17270.  
  17271.  Protected Mode Exceptions
  17272.  
  17273.  #GP(0) for an illegal memory operand effective address in the CS, DS,
  17274.  ES, FS, or GS segments; #SS(0) for an illegal address in the SS segment;
  17275.  #GP(0) if the current privilege level is not 0; #GP(selector) if the object
  17276.  named by the source selector is not a TSS or is already busy;
  17277.  #NP(selector) if the TSS is marked "not present"; #PF(fault-code) for
  17278.  a page fault
  17279.  
  17280.  Real Address Mode Exceptions
  17281.  
  17282.  Interrupt 6; LTR is not recognized in Real Address Mode
  17283.  
  17284.  Virtual 8086 Mode Exceptions
  17285.  
  17286.  Same exceptions as in Real Address Mode
  17287.  
  17288.  Notes
  17289.  
  17290.  The operand-size attribute has no effect on this instruction.
  17291.  
  17292.  
  17293.  MOV ── Move Data
  17294.  
  17295. ╓┌───────┌─────────────────┌─────────────┌───────────────────────────────────╖
  17296.  Opcode   Instruction       Clocks        Description
  17297.  
  17298.  88  /r   MOV r/m8,r8       2/2           Move byte register to r/m byte
  17299.  89  /r   MOV r/m16,r16     2/2           Move word register to r/m word
  17300.  89  /r   MOV r/m32,r32     2/2           Move dword register to r/m dword
  17301.  8A  /r   MOV r8,r/m8       2/4           Move r/m byte to byte register
  17302.  8B  /r   MOV r16,r/m16     2/4           Move r/m word to word register
  17303.  8B  /r   MOV r32,r/m32     2/4           Move r/m dword to dword register
  17304.  8C  /r   MOV r/m16,Sreg    2/2           Move segment register to r/m word
  17305.  8D  /r   MOV Sreg,r/m16    2/5,pm=18/19  Move r/m word to segment register
  17306.  Opcode   Instruction       Clocks        Description
  17307. 8D  /r   MOV Sreg,r/m16    2/5,pm=18/19  Move r/m word to segment register
  17308.  A0       MOV AL,moffs8     4             Move byte at (seg:offset) to AL
  17309.  A1       MOV AX,moffs16    4             Move word at (seg:offset) to AX
  17310.  A1       MOV EAX,moffs32   4             Move dword at (seg:offset) to EAX
  17311.  A2       MOV moffs8,AL     2             Move AL to (seg:offset)
  17312.  A3       MOV moffs16,AX    2             Move AX to (seg:offset)
  17313.  A3       MOV moffs32,EAX   2             Move EAX to (seg:offset)
  17314.  B0 + rb  MOV reg8,imm8     2             Move immediate byte to register
  17315.  B8 + rw  MOV reg16,imm16   2             Move immediate word to register
  17316.  B8 + rd  MOV reg32,imm32   2             Move immediate dword to register
  17317.  C6       MOV r/m8,imm8     2/2           Move immediate byte to r/m byte
  17318.  C7       MOV r/m16,imm16   2/2           Move immediate word to r/m word
  17319.  C7       MOV r/m32,imm32   2/2           Move immediate dword to r/m dword
  17320.  
  17321.  
  17322.  ───────────────────────────────────────────────────────────────────────────
  17323.  NOTES:
  17324.    moffs8, moffs16, and moffs32 all consist of a simple offset relative
  17325.    to the segment base. The 8, 16, and 32 refer to the size of the data. The
  17326.    address-size attribute of the instruction determines the size of the
  17327.    offset, either 16 or 32 bits.
  17328.  ───────────────────────────────────────────────────────────────────────────
  17329.  
  17330.  Operation
  17331.  
  17332.  DEST  SRC;
  17333.  
  17334.  Description
  17335.  
  17336.  MOV copies the second operand to the first operand.
  17337.  
  17338.  If the destination operand is a segment register (DS, ES, SS, etc.), then
  17339.  data from a descriptor is also loaded into the register. The data for the
  17340.  register is obtained from the descriptor table entry for the selector
  17341.  given. A null selector (values 0000-0003) can be loaded into DS and ES
  17342.  registers without causing an exception; however, use of DS or ES causes a
  17343.  #GP(0), and no memory reference occurs.
  17344.  
  17345.  A MOV into SS inhibits all interrupts until after the execution of the
  17346.  next instruction (which is presumably a MOV into eSP).
  17347.  
  17348.  Loading a segment register under 80386 Protected Mode results in special
  17349.  checks and actions, as described in the following listing:
  17350.  
  17351.  IF SS is loaded;
  17352.  THEN
  17353.     IF selector is null THEN #GP(0);
  17354.  FI;
  17355.     Selector index must be within its descriptor table limits else
  17356.        #GP(selector);
  17357.     Selector's RPL must equal CPL else #GP(selector);
  17358.  AR byte must indicate a writable data segment else #GP(selector);
  17359.     DPL in the AR byte must equal CPL else #GP(selector);
  17360.     Segment must be marked present else #SS(selector);
  17361.     Load SS with selector;
  17362.     Load SS with descriptor.
  17363.  FI;
  17364.  IF DS, ES, FS or GS is loaded with non-null selector;
  17365.  THEN
  17366.     Selector index must be within its descriptor table limits
  17367.        else #GP(selector);
  17368.     AR byte must indicate data or readable code segment else
  17369.        #GP(selector);
  17370.     IF data or nonconforming code segment
  17371.     THEN both the RPL and the CPL must be less than or equal to DPL in
  17372.        AR byte;
  17373.     ELSE #GP(selector);
  17374.     FI;
  17375.     Segment must be marked present else #NP(selector);
  17376.     Load segment register with selector;
  17377.     Load segment register with descriptor;
  17378.  FI;
  17379.  IF DS, ES, FS or GS is loaded with a null selector;
  17380.  THEN
  17381.     Load segment register with selector;
  17382.     Clear descriptor valid bit;
  17383.  FI;
  17384.  
  17385.  Flags Affected
  17386.  
  17387.  None
  17388.  
  17389.  Protected Mode Exceptions
  17390.  
  17391.  #GP, #SS, and #NP if a segment register is being loaded; otherwise,
  17392.  #GP(0) if the destination is in a nonwritable segment; #GP(0) for an
  17393.  illegal memory operand effective address in the CS, DS, ES, FS, or GS
  17394.  segments; #SS(0) for an illegal address in the SS segment; #PF(fault-code)
  17395.  for a page fault
  17396.  
  17397.  Real Address Mode Exceptions
  17398.  
  17399.  Interrupt 13 if any part of the operand would lie outside of the effective
  17400.  address space from 0 to 0FFFFH
  17401.  
  17402.  Virtual 8086 Mode Exceptions
  17403.  
  17404.  Same exceptions as in Real Address Mode; #PF(fault-code) for a page
  17405.  fault
  17406.  
  17407.  
  17408.  MOV ── Move to/from Special Registers
  17409.  
  17410.  Opcode      Instruction           Clocks   Description
  17411.  
  17412.  0F  20 /r   MOV r32,CR0/CR2/CR3   6        Move (control register) to
  17413.                                             (register)
  17414.  0F  22 /r   MOV CR0/CR2/CR3,r32   10/4/5   Move (register) to (control
  17415.                                             register)
  17416.  0F  21 /r   MOV r32,DR0 -- 3      22       Move (debug register) to
  17417.                                             (register)
  17418.  0F  21 /r   MOV r32,DR6/DR7       14       Move (debug register) to
  17419.                                             (register)
  17420.  0F  23 /r   MOV DR0 -- 3,r32      22       Move (register) to (debug
  17421.                                             register)
  17422.  0F  23 /r   MOV DR6/DR7,r32       16       Move (register) to (debug
  17423.                                             register)
  17424.  0F  24 /r   MOV r32,TR6/TR7       12       Move (test register) to
  17425.                                             (register)
  17426.  0F  26 /r   MOV TR6/TR7,r32       12       Move (register) to (test
  17427.                                             register)
  17428.  
  17429.  
  17430.  Operation
  17431.  
  17432.  DEST  SRC;
  17433.  
  17434.  Description
  17435.  
  17436.  The above forms of MOV store or load the following special registers in
  17437.  or from a general purpose register:
  17438.  
  17439.    ■  Control registers CR0, CR2, and CR3
  17440.    ■  Debug Registers DR0, DR1, DR2, DR3, DR6, and DR7
  17441.    ■  Test Registers TR6 and TR7
  17442.  
  17443.  32-bit operands are always used with these instructions, regardless of the
  17444.  operand-size attribute.
  17445.  
  17446.  Flags Affected
  17447.  
  17448.  OF, SF, ZF, AF, PF, and CF are undefined
  17449.  
  17450.  Protected Mode Exceptions
  17451.  
  17452.  #GP(0) if the current privilege level is not 0
  17453.  
  17454.  Real Address Mode Exceptions
  17455.  
  17456.  None
  17457.  
  17458.  Virtual 8086 Mode Exceptions
  17459.  
  17460.  #GP(0) if instruction execution is attempted
  17461.  
  17462.  Notes
  17463.  
  17464.  The instructions must be executed at privilege level 0 or in real-address
  17465.  mode; otherwise, a protection exception will be raised.
  17466.  
  17467.  The reg field within the ModRM byte specifies which of the special
  17468.  registers in each category is involved. The two bits in the  field are
  17469.  always 11. The r/m field specifies the general register involved.
  17470.  
  17471.  
  17472.  MOVS/MOVSB/MOVSW/MOVSD ── Move Data from String to String
  17473.  
  17474.  Opcode  Instruction      Clocks   Description
  17475.  
  17476.  A4      MOVS m8,m8       7        Move byte [(E)SI] to ES:[(E)DI]
  17477.  A5      MOVS m16,m16     7        Move word [(E)SI] to ES:[(E)DI]
  17478.  A5      MOVS m32,m32     7        Move dword [(E)SI] to ES:[(E)DI]
  17479.  A4      MOVSB            7        Move byte DS:[(E)SI] to ES:[(E)DI]
  17480.  A5      MOVSW            7        Move word DS:[(E)SI] to ES:[(E)DI]
  17481.  A5      MOVSD            7        Move dword DS:[(E)SI] to ES:[(E)DI]
  17482.  
  17483.  
  17484.  Operation
  17485.  
  17486.  IF (instruction = MOVSD) OR (instruction has doubleword operands)
  17487.  THEN OperandSize  32;
  17488.  ELSE OperandSize  16;
  17489.  IF AddressSize = 16
  17490.  THEN use SI for source-index and DI for destination-index;
  17491.  ELSE (* AddressSize = 32 *)
  17492.     use ESI for source-index and EDI for destination-index;
  17493.  FI;
  17494.  IF byte type of instruction
  17495.  THEN
  17496.     [destination-index]  [source-index]; (* byte assignment *)
  17497.     IF DF = 0 THEN IncDec  1 ELSE IncDec  -1; FI;
  17498.  ELSE
  17499.     IF OperandSize = 16
  17500.     THEN
  17501.        [destination-index]  [source-index]; (* word assignment *)
  17502.        IF DF = 0 THEN IncDec  2 ELSE IncDec  -2; FI;
  17503.     ELSE (* OperandSize = 32 *)
  17504.        [destination-index]  [source-index]; (* doubleword assignment *)
  17505.        IF DF = 0 THEN IncDec  4 ELSE IncDec  -4; FI;
  17506.     FI;
  17507.  FI;
  17508.  source-index  source-index + IncDec;
  17509.  destination-index  destination-index + IncDec;
  17510.  
  17511.  Description
  17512.  
  17513.  MOVS copies the byte or word at [(E)SI] to the byte or word at
  17514.  ES:[(E)DI]. The destination operand must be addressable from the ES
  17515.  register; no segment override is possible for the destination. A segment
  17516.  override can be used for the source operand; the default is DS.
  17517.  
  17518.  The addresses of the source and destination are determined solely by the
  17519.  contents of (E)SI and (E)DI. Load the correct index values into (E)SI
  17520.  and (E)DI before executing the MOVS instruction. MOVSB, MOVSW,
  17521.  and MOVSD are synonyms for the byte, word, and doubleword MOVS
  17522.  instructions.
  17523.  
  17524.  After the data is moved, both (E)SI and (E)DI are advanced
  17525.  automatically. If the direction flag is 0 (CLD was executed), the registers
  17526.  are incremented; if the direction flag is 1 (STD was executed), the
  17527.  registers are decremented. The registers are incremented or decremented by 1
  17528.  if a byte was moved, 2 if a word was moved, or 4 if a doubleword was moved.
  17529.  
  17530.  MOVS can be preceded by the REP prefix for block movement of CX
  17531.  bytes or words. Refer to the REP instruction for details of this operation.
  17532.  
  17533.  Flags Affected
  17534.  
  17535.  None
  17536.  
  17537.  Protected Mode Exceptions
  17538.  
  17539.  #GP(0) if the result is in a nonwritable segment; #GP(0) for an illegal
  17540.  memory operand effective address in the CS, DS, ES, FS, or GS
  17541.  segments; #SS(0) for an illegal address in the SS segment; #PF(fault-code)
  17542.  for a page fault
  17543.  
  17544.  Real Address Mode Exceptions
  17545.  
  17546.  Interrupt 13 if any part of the operand would lie outside of the effective
  17547.  address space from 0 to 0FFFFH
  17548.  
  17549.  Virtual 8086 Mode Exceptions
  17550.  
  17551.  Same exceptions as in Real Address Mode; #PF(fault-code) for a page
  17552.  fault
  17553.  
  17554.  
  17555.  MOVSX ── Move with Sign-Extend
  17556.  
  17557.  Opcode     Instruction        Clocks   Description
  17558.  
  17559.  0F  BE /r  MOVSX r16,r/m8     3/6      Move byte to word with sign-extend
  17560.  0F  BE /r  MOVSX r32,r/m8     3/6      Move byte to dword, sign-extend
  17561.  0F  BF /r  MOVSX r32,r/m16    3/6      Move word to dword, sign-extend
  17562.  
  17563.  
  17564.  Operation
  17565.  
  17566.  DEST  SignExtend(SRC);
  17567.  
  17568.  Description
  17569.  
  17570.  MOVSX reads the contents of the effective address or register as a byte
  17571.  or a word, sign-extends the value to the operand-size attribute of the
  17572.  instruction (16 or 32 bits), and stores the result in the destination
  17573.  register.
  17574.  
  17575.  Flags Affected
  17576.  
  17577.  None
  17578.  
  17579.  Protected Mode Exceptions
  17580.  
  17581.  #GP(0) for an illegal memory operand effective address in the CS, DS,
  17582.  ES, FS or GS segments; #SS(0) for an illegal address in the SS segment;
  17583.  #PF(fault-code) for a page fault
  17584.  
  17585.  Real Address Mode Exceptions
  17586.  
  17587.  Interrupt 13 if any part of the operand would lie outside of the effective
  17588.  address space from 0 to 0FFFFH
  17589.  
  17590.  Virtual 8086 Mode Exceptions
  17591.  
  17592.  Same exceptions as in Real Address Mode; #PF(fault-code) for a page
  17593.  fault
  17594.  
  17595.  
  17596.  MOVZX ── Move with Zero-Extend
  17597.  
  17598.  Opcode      Instruction        Clocks   Description
  17599.  
  17600.  0F  B6 /r   MOVZX r16,r/m8     3/6      Move byte to word with zero-extend
  17601.  0F  B6 /r   MOVZX r32,r/m8     3/6      Move byte to dword, zero-extend
  17602.  0F  B7 /r   MOVZX r32,r/m16    3/6      Move word to dword, zero-extend
  17603.  
  17604.  
  17605.  Operation
  17606.  
  17607.  DEST  ZeroExtend(SRC);
  17608.  
  17609.  Description
  17610.  
  17611.  MOVZX reads the contents of the effective address or register as a byte
  17612.  or a word, zero extends the value to the operand-size attribute of the
  17613.  instruction (16 or 32 bits), and stores the result in the destination
  17614.  register.
  17615.  
  17616.  Flags Affected
  17617.  
  17618.  None
  17619.  
  17620.  Protected Mode Exceptions
  17621.  
  17622.  #GP(0) for an illegal memory operand effective address in the CS, DS,
  17623.  ES, FS, or GS segments; #SS(0) for an illegal address in the SS segment;
  17624.  #PF(fault-code) for a page fault
  17625.  
  17626.  Real Address Mode Exceptions
  17627.  
  17628.  Interrupt 13 if any part of the operand would lie outside of the effective
  17629.  address space from 0 to 0FFFFH
  17630.  
  17631.  Virtual 8086 Mode Exceptions
  17632.  
  17633.  Same exceptions as in Real Address Mode; #PF(fault-code) for a page
  17634.  fault
  17635.  
  17636.  
  17637.  MUL ── Unsigned Multiplication of AL or AX
  17638.  
  17639.  Opcode  Instruction     Clocks       Description
  17640.  
  17641.  F6  /4  MUL AL,r/m8     9-14/12-17   Unsigned multiply (AX  AL * r/m byte)
  17642.  F7  /4  MUL AX,r/m16    9-22/12-25   Unsigned multiply (DX:AX  AX * r/m
  17643.                                       word)
  17644.  F7  /4  MUL EAX,r/m32   9-38/12-41   Unsigned multiply (EDX:EAX  EAX * r/m
  17645.                                       dword)
  17646.  
  17647.  
  17648.  ───────────────────────────────────────────────────────────────────────────
  17649.  NOTES:
  17650.    The 80386 uses an early-out multiply algorithm. The actual number of
  17651.    clocks depends on the position of the most significant bit in the
  17652.    optimizing multiplier, shown underlined above. The optimization occurs
  17653.    for positive and negative multiplier values. Because of the early-out
  17654.    algorithm, clock counts given are minimum to maximum. To calculate the
  17655.    actual clocks, use the following formula:
  17656.  
  17657.      Actual clock = if  <> 0 then max(ceiling(log{2} │m│), 3) + 6 clocks;
  17658.  
  17659.      Actual clock = if  = 0 then 9 clocks
  17660.  
  17661.    where m is the multiplier.
  17662.  ───────────────────────────────────────────────────────────────────────────
  17663.  
  17664.  Operation
  17665.  
  17666.  IF byte-size operation
  17667.  THEN AX  AL * r/m8
  17668.  ELSE (* word or doubleword operation *)
  17669.     IF OperandSize = 16
  17670.     THEN DX:AX  AX * r/m16
  17671.     ELSE (* OperandSize = 32 *)
  17672.        EDX:EAX  EAX * r/m32
  17673.     FI;
  17674.  FI;
  17675.  
  17676.  Description
  17677.  
  17678.  MUL performs unsigned multiplication. Its actions depend on the size
  17679.  of its operand, as follows:
  17680.  
  17681.    ■  A byte operand is multiplied by AL; the result is left in AX. The
  17682.       carry and overflow flags are set to 0 if AH is 0; otherwise, they are
  17683.       set to 1.
  17684.  
  17685.    ■  A word operand is multiplied by AX; the result is left in DX:AX.
  17686.       DX contains the high-order 16 bits of the product. The carry and
  17687.       overflow flags are set to 0 if DX is 0; otherwise, they are set to 1.
  17688.  
  17689.    ■  A doubleword operand is multiplied by EAX and the result is left in
  17690.       EDX:EAX. EDX contains the high-order 32 bits of the product. The
  17691.       carry and overflow flags are set to 0 if EDX is 0; otherwise, they are
  17692.       set to 1.
  17693.  
  17694.  Flags Affected
  17695.  
  17696.  OF and CF as described above; SF, ZF, AF, PF, and CF are undefined
  17697.  
  17698.  Protected Mode Exceptions
  17699.  
  17700.  #GP(0) for an illegal memory operand effective address in the CS, DS,
  17701.  ES, FS, or GS segments; #SS(0) for an illegal address in the SS segment;
  17702.  #PF(fault-code) for a page fault
  17703.  
  17704.  Real Address Mode Exceptions
  17705.  
  17706.  Interrupt 13 if any part of the operand would lie outside of the effective
  17707.  address space from 0 to 0FFFFH
  17708.  
  17709.  Virtual 8086 Mode Exceptions
  17710.  
  17711.  Same exceptions as in Real Address Mode; #PF(fault-code) for a page
  17712.  fault
  17713.  
  17714.  
  17715.  NEG ── Two's Complement Negation
  17716.  
  17717.  Opcode  Instruction   Clocks    Description
  17718.  
  17719.  F6  /3  NEG r/m8      2/6       Two's complement negate r/m byte
  17720.  F7  /3  NEG r/m16     2/6       Two's complement negate r/m word
  17721.  F7  /3  NEG r/m32     2/6       Two's complement negate r/m dword
  17722.  
  17723.  
  17724.  Operation
  17725.  
  17726.  IF r/m = 0 THEN CF  0 ELSE CF  1; FI;
  17727.  r/m  - r/m;
  17728.  
  17729.  Description
  17730.  
  17731.  NEG replaces the value of a register or memory operand with its two's
  17732.  complement. The operand is subtracted from zero, and the result is placed
  17733.  in the operand.
  17734.  
  17735.  The carry flag is set to 1, unless the operand is zero, in which case the
  17736.  carry flag is cleared to 0.
  17737.  
  17738.  Flags Affected
  17739.  
  17740.  CF as described above; OF, SF, ZF, and PF as described in Appendix C
  17741.  
  17742.  Protected Mode Exceptions
  17743.  
  17744.  #GP(0) if the result is in a nonwritable segment; #GP(0) for an illegal
  17745.  memory operand effective address in the CS, DS, ES, FS, or GS
  17746.  segments; #SS(0) for an illegal address in the SS segment; #PF(fault-code)
  17747.  for a page fault
  17748.  
  17749.  Real Address Mode Exceptions
  17750.  
  17751.  Interrupt 13 if any part of the operand would lie outside of the effective
  17752.  address space from 0 to 0FFFFH
  17753.  
  17754.  Virtual 8086 Mode Exceptions
  17755.  
  17756.  Same exceptions as in real-address mode; #PF(fault-code) for a page
  17757.  fault
  17758.  
  17759.  
  17760.  NOP ── No Operation
  17761.  
  17762.  Opcode  Instruction   Clocks    Description
  17763.  
  17764.  90      NOP           3         No operation
  17765.  
  17766.  
  17767.  Description
  17768.  
  17769.  NOP performs no operation. NOP is a one-byte instruction that takes
  17770.  up space but affects none of the machine context except (E)IP.
  17771.  
  17772.  NOP is an alias mnemonic for the XCHG (E)AX, (E)AX instruction.
  17773.  
  17774.  Flags Affected
  17775.  
  17776.  None
  17777.  
  17778.  Protected Mode Exceptions
  17779.  
  17780.  None
  17781.  
  17782.  Real Address Mode Exceptions
  17783.  
  17784.  None
  17785.  
  17786.  Virtual 8086 Mode Exceptions
  17787.  
  17788.  None
  17789.  
  17790.  
  17791.  NOT ── One's Complement Negation
  17792.  
  17793.  Opcode    Instruction   Clocks    Description
  17794.  
  17795.  F6   /2   NOT r/m8       2/6      Reverse each bit of r/m byte
  17796.  F7   /2   NOT r/m16      2/6      Reverse each bit of r/m word
  17797.  F7   /2   NOT r/m32      2/6      Reverse each bit of r/m dword
  17798.  
  17799.  
  17800.  Operation
  17801.  
  17802.  r/m  NOT r/m;
  17803.  
  17804.  Description
  17805.  
  17806.  NOT inverts the operand; every 1 becomes a 0, and vice versa.
  17807.  
  17808.  Flags Affected
  17809.  
  17810.  None
  17811.  
  17812.  Protected Mode Exceptions
  17813.  
  17814.  #GP(0) if the result is in a nonwritable segment; #GP(0) for an illegal
  17815.  memory operand effective address in the CS, DS, ES, FS, or GS
  17816.  segments; #SS(0) for an illegal address in the SS segment; #PF(fault-code)
  17817.  for a page fault
  17818.  
  17819.  Real Address Mode Exceptions
  17820.  
  17821.  Interrupt 13 if any part of the operand would lie outside of the effective
  17822.  address space from 0 to 0FFFFH
  17823.  
  17824.  Virtual 8086 Mode Exceptions
  17825.  
  17826.  Same exceptions as in real-address mode; #PF(fault-code) for a page
  17827.  fault
  17828.  
  17829.  
  17830.  OR ── Logical Inclusive OR
  17831.  
  17832.  Opcode       Instruction       Clocks    Description
  17833.  
  17834.  0C  ib       OR AL,imm8        2         OR immediate byte to AL
  17835.  0D  iw       OR AX,imm16       2         OR immediate word to AX
  17836.  0D  id       OR EAX,imm32      2         OR immediate dword to EAX
  17837.  80  /1 ib    OR r/m8,imm8      2/7       OR immediate byte to r/m byte
  17838.  81  /1 iw    OR r/m16,imm16    2/7       OR immediate word to r/m word
  17839.  81  /1 id    OR r/m32,imm32    2/7       OR immediate dword to r/m dword
  17840.  83  /1 ib    OR r/m16,imm8     2/7       OR sign-extended immediate byte
  17841.                                           with r/m word
  17842.  83  /1 ib    OR r/m32,imm8     2/7       OR sign-extended immediate byte
  17843.                                           with r/m dword
  17844.  08  /r       OR r/m8,r8        2/6       OR byte register to r/m byte
  17845.  09  /r       OR r/m16,r16      2/6       OR word register to r/m word
  17846.  09  /r       OR r/m32,r32      2/6       OR dword register to r/m dword
  17847.  0A  /r       OR r8,r/m8        2/7       OR byte register to r/m byte
  17848.  0B  /r       OR r16,r/m16      2/7       OR word register to r/m word
  17849.  0B  /r       OR r32,r/m32      2/7       OR dword register to r/m dword
  17850.  
  17851.  
  17852.  Operation
  17853.  
  17854.  DEST  DEST OR SRC;
  17855.  CF  0;
  17856.  OF  0
  17857.  
  17858.  Description
  17859.  
  17860.  OR computes the inclusive OR of its two operands and places the result
  17861.  in the first operand. Each bit of the result is 0 if both corresponding
  17862.  bits of the operands are 0; otherwise, each bit is 1.
  17863.  
  17864.  Flags Affected
  17865.  
  17866.  OF  0, CF  0; SF, ZF, and PF as described in Appendix C; AF is
  17867.  undefined
  17868.  
  17869.  Protected Mode Exceptions
  17870.  
  17871.  #GP(0) if the result is in a nonwritable segment; #GP(0) for an illegal
  17872.  memory operand effective address in the CS, DS, ES, FS, or GS
  17873.  segments; #SS(0) for an illegal address in the SS segment; #PF(fault-code)
  17874.  for a page fault
  17875.  
  17876.  Real Address Mode Exceptions
  17877.  
  17878.  Interrupt 13 if any part of the operand would lie outside of the effective
  17879.  address space from 0 to 0FFFFH
  17880.  
  17881.  Virtual 8086 Mode Exceptions
  17882.  
  17883.  Same exceptions as in real-address mode; #PF(fault-code) for a page
  17884.  fault
  17885.  
  17886.  
  17887.  OUT ── Output to Port
  17888.  
  17889.  Opcode    Instruction     Clocks          Description
  17890.  
  17891.  E6  ib    OUT imm8,AL     10,pm=4*/24**   Output byte AL to immediate port
  17892.                                            number
  17893.  E7  ib    OUT imm8,AX     10,pm=4*/24**   Output word AL to immediate port
  17894.                                            number
  17895.  E7  ib    OUT imm8,EAX    10,pm=4*/24**   Output dword AL to immediate
  17896.                                            port number
  17897.  EE        OUT DX,AL       11,pm=5*/25**   Output byte AL to port number in
  17898.  DX
  17899.  EF        OUT DX,AX       11,pm=5*/25**   Output word AL to port number in
  17900.  DX
  17901.  EF        OUT DX,EAX      11,pm=5*/25**   Output dword AL to port number
  17902.                                            in DX
  17903.  
  17904.  
  17905.  ───────────────────────────────────────────────────────────────────────────
  17906.  NOTES:
  17907.     *If CPL ≤ IOPL
  17908.    **If CPL > IOPL or if in virtual 8086 mode
  17909.  ───────────────────────────────────────────────────────────────────────────
  17910.  
  17911.  Operation
  17912.  
  17913.  IF (PE = 1) AND ((VM = 1) OR (CPL > IOPL))
  17914.  THEN (* Virtual 8086 mode, or protected mode with CPL > IOPL *)
  17915.     IF NOT I-O-Permission (DEST, width(DEST))
  17916.     THEN #GP(0);
  17917.     FI;
  17918.  FI;
  17919.  [DEST]  SRC; (* I/O address space used *)
  17920.  
  17921.  Description
  17922.  
  17923.  OUT transfers a data byte or data word from the register (AL, AX, or
  17924.  EAX) given as the second operand to the output port numbered by the
  17925.  first operand. Output to any port from 0 to 65535 is performed by placing
  17926.  the port number in the DX register and then using an OUT instruction
  17927.  with DX as the first operand. If the instruction contains an eight-bit port
  17928.  ID, that value is zero-extended to 16 bits.
  17929.  
  17930.  Flags Affected
  17931.  
  17932.  None
  17933.  
  17934.  Protected Mode Exceptions
  17935.  
  17936.  #GP(0) if the current privilege level is higher (has less privilege) than
  17937.  IOPL and any of the corresponding I/O permission bits in TSS equals 1
  17938.  
  17939.  Real Address Mode Exceptions
  17940.  
  17941.  None
  17942.  
  17943.  Virtual 8086 Mode Exceptions
  17944.  
  17945.  #GP(0) fault if any of the corresponding I/O permission bits in TSS
  17946.  equals 1
  17947.  
  17948.  
  17949.  OUTS/OUTSB/OUTSW/OUTSD ── Output String to Port
  17950.  
  17951.  Opcode   Instruction     Clocks          Description
  17952.  
  17953.  6E       OUTS DX,r/m8    14,pm=8*/28**   Output byte [(E)SI] to port in DX
  17954.  6F       OUTS DX,r/m16   14,pm=8*/28**   Output word [(E)SI] to port in DX
  17955.  6F       OUTS DX,r/m32   14,pm=8*/28**   Output dword [(E)SI] to port in DX
  17956.  6E       OUTSB           14,pm=8*/28**   Output byte DS:[(E)SI] to port in
  17957.                                           DX
  17958.  6F       OUTSW           14,pm=8*/28**   Output word DS:[(E)SI] to port in
  17959.                                           DX
  17960.  6F       OUTSD           14,pm=8*/28**   Output dword DS:[(E)SI] to port in
  17961.                                           DX
  17962.  
  17963.  
  17964.  ───────────────────────────────────────────────────────────────────────────
  17965.  NOTES:
  17966.     *If CPL ≤ IOPL
  17967.    **If CPL > IOPL or if in virtual 8086 mode
  17968.  ───────────────────────────────────────────────────────────────────────────
  17969.  
  17970.  Operation
  17971.  
  17972.  IF AddressSize = 16
  17973.  THEN use SI for source-index;
  17974.  ELSE (* AddressSize = 32 *)
  17975.     use ESI for source-index;
  17976.  FI;
  17977.  
  17978.  IF (PE = 1) AND ((VM = 1) OR (CPL > IOPL))
  17979.  THEN (* Virtual 8086 mode, or protected mode with CPL > IOPL *)
  17980.     IF NOT I-O-Permission (DEST, width(DEST))
  17981.     THEN #GP(0);
  17982.     FI;
  17983.  FI;
  17984.  IF byte type of instruction
  17985.  THEN
  17986.     [DX]  [source-index]; (* Write byte at DX I/O address *)
  17987.     IF DF = 0 THEN IncDec  1 ELSE IncDec  -1; FI;
  17988.  FI;
  17989.  IF OperandSize = 16
  17990.  THEN
  17991.     [DX]  [source-index]; (* Write word at DX I/O address *)
  17992.     IF DF = 0 THEN IncDec  2 ELSE IncDec  -2; FI;
  17993.  FI;
  17994.  IF OperandSize = 32
  17995.  THEN
  17996.     [DX]  [source-index]; (* Write dword at DX I/O address *)
  17997.     IF DF = 0 THEN IncDec  4 ELSE IncDec  -4; FI;
  17998.     FI;
  17999.  FI;
  18000.  source-index  source-index + IncDec;
  18001.  
  18002.  Description
  18003.  
  18004.  OUTS transfers data from the memory byte, word, or doubleword at the
  18005.  source-index register to the output port addressed by the DX register. If
  18006.  the address-size attribute for this instruction is 16 bits, SI is used for
  18007.  the source-index register; otherwise, the address-size attribute is 32 bits,
  18008.  and ESI is used for the source-index register.
  18009.  
  18010.  OUTS does not allow specification of the port number as an immediate value.
  18011.  The port must be addressed through the DX register value. Load the correct
  18012.  value into DX before executing the OUTS instruction.
  18013.  
  18014.  The address of the source data is determined by the contents of
  18015.  source-index register. Load the correct index value into SI or ESI before
  18016.  executing the OUTS instruction.
  18017.  
  18018.  After the transfer, source-index register is advanced automatically. If
  18019.  the direction flag is 0 (CLD was executed), the source-index register is
  18020.  incremented; if the direction flag is 1 (STD was executed), it is
  18021.  decremented. The amount of the increment or decrement is 1 if a byte is
  18022.  output, 2 if a word is output, or 4 if a doubleword is output.
  18023.  
  18024.  OUTSB, OUTSW, and OUTSD are synonyms for the byte, word, and
  18025.  doubleword OUTS instructions. OUTS can be preceded by the REP
  18026.  prefix for block output of CX bytes or words. Refer to the REP
  18027.  instruction for details on this operation.
  18028.  
  18029.  Flags Affected
  18030.  
  18031.  None
  18032.  
  18033.  Protected Mode Exceptions
  18034.  
  18035.  #GP(0) if CPL is greater than IOPL and any of the corresponding I/O
  18036.  permission bits in TSS equals 1; #GP(0) for an illegal memory operand
  18037.  effective address in the CS, DS, or ES segments; #SS(0) for an illegal
  18038.  address in the SS segment; #PF(fault-code) for a page fault
  18039.  
  18040.  Real Address Mode Exceptions
  18041.  
  18042.  Interrupt 13 if any part of the operand would lie outside of the effective
  18043.  address space from 0 to 0FFFFH
  18044.  
  18045.  Virtual 8086 Mode Exceptions
  18046.  
  18047.  #GP(0) fault if any of the corresponding I/O permission bits in TSS
  18048.  equals 1; #PF(fault-code) for a page fault
  18049.  
  18050.  
  18051.  POP ── Pop a Word from the Stack
  18052.  
  18053.  Opcode      Instruction   Clocks     Description
  18054.  
  18055.  8F   /0     POP m16       5          Pop top of stack into memory word
  18056.  8F   /0     POP m32       5          Pop top of stack into memory dword
  18057.  58 + rw     POP r16       4          Pop top of stack into word register
  18058.  58 + rd     POP r32       4          Pop top of stack into dword register
  18059.  1F          POP DS        7,pm=21    Pop top of stack into DS
  18060.  07          POP ES        7,pm=21    Pop top of stack into ES
  18061.  17          POP SS        7,pm=21    Pop top of stack into SS
  18062.  0F   A1     POP FS        7,pm=21    Pop top of stack into FS
  18063.  0F   A9     POP GS        7,pm=21    Pop top of stack into GS
  18064.  
  18065.  
  18066.  Operation
  18067.  
  18068.  IF StackAddrSize = 16
  18069.  THEN
  18070.     IF OperandSize = 16
  18071.     THEN
  18072.        DEST  (SS:SP); (* copy a word *)
  18073.        SP  SP + 2;
  18074.     ELSE (* OperandSize = 32 *)
  18075.        DEST  (SS:SP); (* copy a dword *)
  18076.        SP  SP + 4;
  18077.     FI;
  18078.  ELSE (* StackAddrSize = 32 * )
  18079.     IF OperandSize = 16
  18080.     THEN
  18081.        DEST  (SS:ESP); (* copy a word *)
  18082.        ESP  ESP + 2;
  18083.     ELSE (* OperandSize = 32 *)
  18084.        DEST  (SS:ESP); (* copy a dword *)
  18085.        ESP  ESP + 4;
  18086.     FI;
  18087.  FI;
  18088.  
  18089.  Description
  18090.  
  18091.  POP replaces the previous contents of the memory, the register, or the
  18092.  segment register operand with the word on the top of the 80386 stack,
  18093.  addressed by SS:SP (address-size attribute of 16 bits) or SS:ESP
  18094.  (addresssize attribute of 32 bits). The stack pointer SP is incremented
  18095.  by 2 for an operand-size of 16 bits or by 4 for an operand-size of 32 bits.
  18096.  It then points to the new top of stack.
  18097.  
  18098.  POP CS is not an 80386 instruction. Popping from the stack into the CS
  18099.  register is accomplished with a RET instruction.
  18100.  
  18101.  If the destination operand is a segment register (DS, ES, FS, GS, or
  18102.  SS), the value popped must be a selector. In protected mode, loading the
  18103.  selector initiates automatic loading of the descriptor information
  18104.  associated with that selector into the hidden part of the segment register;
  18105.  loading also initiates validation of both the selector and the descriptor
  18106.  information.
  18107.  
  18108.  A null value (0000-0003) may be popped into the DS, ES, FS, or GS
  18109.  register without causing a protection exception. An attempt to reference
  18110.  a segment whose corresponding segment register is loaded with a null
  18111.  value causes a #GP(0) exception. No memory reference occurs. The saved
  18112.  value of the segment register is null.
  18113.  
  18114.  A POP SS instruction inhibits all interrupts, including NMI, until after
  18115.  execution of the next instruction. This allows sequential execution of POP
  18116.  SS and POP eSP instructions without danger of having an invalid stack
  18117.  during an interrupt. However, use of the LSS instruction is the preferred
  18118.  method of loading the SS and eSP registers.
  18119.  
  18120.  Loading a segment register while in protected mode results in special
  18121.  checks and actions, as described in the following listing:
  18122.  
  18123.  IF SS is loaded:
  18124.     IF selector is null THEN #GP(0);
  18125.     Selector index must be within its descriptor table limits ELSE
  18126.        #GP(selector);
  18127.     Selector's RPL must equal CPL ELSE #GP(selector);
  18128.     AR byte must indicate a writable data segment ELSE #GP(selector);
  18129.     DPL in the AR byte must equal CPL ELSE #GP(selector);
  18130.     Segment must be marked present ELSE #SS(selector);
  18131.     Load SS register with selector;
  18132.     Load SS register with descriptor;
  18133.  
  18134.  IF DS, ES, FS or GS is loaded with non-null selector:
  18135.     AR byte must indicate data or readable code segment ELSE
  18136.        #GP(selector);
  18137.     IF data or nonconforming code
  18138.     THEN both the RPL and the CPL must be less than or equal to DPL in
  18139.        AR byte
  18140.     ELSE #GP(selector);
  18141.     FI;
  18142.     Segment must be marked present ELSE #NP(selector);
  18143.     Load segment register with selector;
  18144.     Load segment register with descriptor;
  18145.  
  18146.  IF DS, ES, FS, or GS is loaded with a null selector:
  18147.     Load segment register with selector
  18148.     Clear valid bit in invisible portion of register
  18149.  
  18150.  Flags Affected
  18151.  
  18152.  None
  18153.  
  18154.  Protected Mode Exceptions
  18155.  
  18156.  #GP, #SS, and #NP if a segment register is being loaded; #SS(0) if the
  18157.  current top of stack is not within the stack segment; #GP(0) if the result
  18158.  is in a nonwritable segment; #GP(0) for an illegal memory operand
  18159.  effective address in the CS, DS, ES, FS, or GS segments; #SS(0) for an
  18160.  illegal address in the SS segment; #PF(fault-code) for a page fault
  18161.  
  18162.  Real Address Mode Exceptions
  18163.  
  18164.  Interrupt 13 if any part of the operand would lie outside of the effective
  18165.  address space from 0 to 0FFFFH
  18166.  
  18167.  Virtual 8086 Mode Exceptions
  18168.  
  18169.  Same exceptions as in real-address mode; #PF(fault-code) for a page
  18170.  fault
  18171.  
  18172.  
  18173.  POPA/POPAD ── Pop all General Registers
  18174.  
  18175.  Opcode   Instruction   Clocks   Description
  18176.  
  18177.  61       POPA          24       Pop DI, SI, BP, SP, BX, DX, CX, and AX
  18178.  61       POPAD         24       Pop EDI, ESI, EBP, ESP, EDX, ECX, and EAX
  18179.  
  18180.  
  18181.  Operation
  18182.  
  18183.  IF OperandSize = 16 (* instruction = POPA *)
  18184.  THEN
  18185.     DI  Pop();
  18186.     SI  Pop();
  18187.     BP  Pop();
  18188.     throwaway  Pop (); (* Skip SP *)
  18189.     BX  Pop();
  18190.     DX  Pop();
  18191.     CX  Pop();
  18192.     AX  Pop();
  18193.  ELSE (* OperandSize = 32, instruction = POPAD *)
  18194.     EDI  Pop();
  18195.     ESI  Pop();
  18196.     EBP  Pop();
  18197.     throwaway  Pop (); (* Skip ESP *)
  18198.     EBX  Pop();
  18199.     EDX  Pop();
  18200.     ECX  Pop();
  18201.     EAX  Pop();
  18202.  FI;
  18203.  
  18204.  Description
  18205.  
  18206.  POPA pops the eight 16-bit general registers. However, the SP value is
  18207.  discarded instead of loaded into SP. POPA reverses a previous PUSHA,
  18208.  restoring the general registers to their values before PUSHA was
  18209.  executed. The first register popped is DI.
  18210.  
  18211.  POPAD pops the eight 32-bit general registers. The ESP value is
  18212.  discarded instead of loaded into ESP. POPAD reverses the previous
  18213.  PUSHAD, restoring the general registers to their values before PUSHAD
  18214.  was executed. The first register popped is EDI.
  18215.  
  18216.  Flags Affected
  18217.  
  18218.  None
  18219.  
  18220.  Protected Mode Exceptions
  18221.  
  18222.  #SS(0) if the starting or ending stack address is not within the stack
  18223.  segment; #PF(fault-code) for a page fault
  18224.  
  18225.  Real Address Mode Exceptions
  18226.  
  18227.  Interrupt 13 if any part of the operand would lie outside of the effective
  18228.  address space from 0 to 0FFFFH
  18229.  
  18230.  Virtual 8086 Mode Exceptions
  18231.  
  18232.  Same exceptions as in real-address mode; #PF(fault-code) for a page
  18233.  fault
  18234.  
  18235.  
  18236.  POPF/POPFD ── Pop Stack into FLAGS or EFLAGS Register
  18237.  
  18238.  Opcode   Instruction   Clocks   Description
  18239.  
  18240.  9D       POPF          5        Pop top of stack FLAGS
  18241.  9D       POPFD         5        Pop top of stack into EFLAGS
  18242.  
  18243.  
  18244.  Operation
  18245.  
  18246.  Flags  Pop();
  18247.  
  18248.  Description
  18249.  
  18250.  POPF/POPFD pops the word or doubleword on the top of the stack and
  18251.  stores the value in the flags register. If the operand-size attribute of
  18252.  the instruction is 16 bits, then a word is popped and the value is stored in
  18253.  FLAGS. If the operand-size attribute is 32 bits, then a doubleword is popped
  18254.  and the value is stored in EFLAGS.
  18255.  
  18256.  Refer to Chapter 2 and Chapter 4 for information about the FLAGS
  18257.  and EFLAGS registers. Note that bits 16 and 17 of EFLAGS, called
  18258.  VM and RF, respectively, are not affected by POPF or POPFD.
  18259.  
  18260.  The I/O privilege level is altered only when executing at privilege level
  18261.  0. The interrupt flag is altered only when executing at a level at least as
  18262.  privileged as the I/O privilege level. (Real-address mode is equivalent to
  18263.  privilege level 0.) If a POPF instruction is executed with insufficient
  18264.  privilege, an exception does not occur, but the privileged bits do not
  18265.  change.
  18266.  
  18267.  Flags Affected
  18268.  
  18269.  All flags except VM and RF
  18270.  
  18271.  Protected Mode Exceptions
  18272.  
  18273.  #SS(0) if the top of stack is not within the stack segment
  18274.  
  18275.  Real Address Mode Exceptions
  18276.  
  18277.  Interrupt 13 if any part of the operand would lie outside of the effective
  18278.  address space from 0 to 0FFFFH
  18279.  
  18280.  Virtual 8086 Mode Exceptions
  18281.  
  18282.  #GP(0) fault if IOPL is less than 3, to permit emulation
  18283.  
  18284.  
  18285.  PUSH ── Push Operand onto the Stack
  18286.  
  18287.  Opcode     Instruction   Clocks   Description
  18288.  
  18289.  FF   /6    PUSH m16      5        Push memory word
  18290.  FF   /6    PUSH m32      5        Push memory dword
  18291.  50 + /r    PUSH r16      2        Push register word
  18292.  50 + /r    PUSH r32      2        Push register dword
  18293.  6A         PUSH imm8     2        Push immediate byte
  18294.  68         PUSH imm16    2        Push immediate word
  18295.  68         PUSH imm32    2        Push immediate dword
  18296.  0E         PUSH CS       2        Push CS
  18297.  16         PUSH SS       2        Push SS
  18298.  1E         PUSH DS       2        Push DS
  18299.  06         PUSH ES       2        Push ES
  18300.  0F   A0    PUSH FS       2        Push FS
  18301.  OF   A8    PUSH GS       2        Push GS
  18302.  
  18303.  
  18304.  Operation
  18305.  
  18306.  IF StackAddrSize = 16
  18307.  THEN
  18308.     IF OperandSize = 16 THEN
  18309.        SP  SP - 2;
  18310.        (SS:SP)  (SOURCE); (* word assignment *)
  18311.     ELSE
  18312.        SP  SP - 4;
  18313.        (SS:SP)  (SOURCE); (* dword assignment *)
  18314.     FI;
  18315.  ELSE (* StackAddrSize = 32 *)
  18316.     IF OperandSize = 16
  18317.     THEN
  18318.        ESP  ESP - 2;
  18319.        (SS:ESP)  (SOURCE); (* word assignment *)
  18320.     ELSE
  18321.        ESP  ESP - 4;
  18322.        (SS:ESP)  (SOURCE); (* dword assignment *)
  18323.     FI;
  18324.  FI;
  18325.  
  18326.  Description
  18327.  
  18328.  PUSH decrements the stack pointer by 2 if the operand-size attribute of
  18329.  the instruction is 16 bits; otherwise, it decrements the stack pointer by
  18330.  4. PUSH then places the operand on the new top of stack, which is
  18331.  pointed to by the stack pointer.
  18332.  
  18333.  The 80386 PUSH eSP instruction pushes the value of eSP as it existed
  18334.  before the instruction. This differs from the 8086, where PUSH SP
  18335.  pushes the new value (decremented by 2).
  18336.  
  18337.  Flags Affected
  18338.  
  18339.  None
  18340.  
  18341.  Protected Mode Exceptions
  18342.  
  18343.  #SS(0) if the new value of SP or ESP is outside the stack segment limit;
  18344.  #GP(0) for an illegal memory operand effective address in the CS, DS,
  18345.  ES, FS, or GS segments; #SS(0) for an illegal address in the SS segment;
  18346.  #PF(fault-code) for a page fault
  18347.  
  18348.  Real Address Mode Exceptions
  18349.  
  18350.  None; if SP or ESP is 1, the 80386 shuts down due to a lack of stack
  18351.  space
  18352.  
  18353.  Virtual 8086 Mode Exceptions
  18354.  
  18355.  Same exceptions as in real-address mode; #PF(fault-code) for a page
  18356.  fault
  18357.  
  18358.  
  18359.  PUSHA/PUSHAD ── Push all General Registers
  18360.  
  18361.  Opcode  Instruction  Clocks   Description
  18362.  
  18363.  60      PUSHA        18       Push AX, CX, DX, BX, original SP, BP, SI, and
  18364.                                DI
  18365.  60      PUSHAD       18       Push EAX, ECX, EDX, EBX, original ESP, EBP,
  18366.                                ESI, and EDI
  18367.  
  18368.  
  18369.  Operation
  18370.  
  18371.  IF OperandSize = 16 (* PUSHA instruction *)
  18372.  THEN
  18373.     Temp  (SP);
  18374.     Push(AX);
  18375.     Push(CX);
  18376.     Push(DX);
  18377.     Push(BX);
  18378.     Push(Temp);
  18379.     Push(BP);
  18380.     Push(SI);
  18381.     Push(DI);
  18382.  ELSE (* OperandSize = 32, PUSHAD instruction *)
  18383.     Temp  (ESP);
  18384.     Push(EAX);
  18385.     Push(ECX);
  18386.     Push(EDX);
  18387.     Push(EBX);
  18388.     Push(Temp);
  18389.     Push(EBP);
  18390.     Push(ESI);
  18391.     Push(EDI);
  18392.  FI;
  18393.  
  18394.  Description
  18395.  
  18396.  PUSHA and PUSHAD save the 16-bit or 32-bit general registers,
  18397.  respectively, on the 80386 stack. PUSHA decrements the stack pointer
  18398.  (SP) by 16 to hold the eight word values. PUSHAD decrements the
  18399.  stack pointer (ESP) by 32 to hold the eight doubleword values. Because
  18400.  the registers are pushed onto the stack in the order in which they were
  18401.  given, they appear in the 16 or 32 new stack bytes in reverse order. The
  18402.  last register pushed is DI or EDI.
  18403.  
  18404.  Flags Affected
  18405.  
  18406.  None
  18407.  
  18408.  Protected Mode Exceptions
  18409.  
  18410.  #SS(0) if the starting or ending stack address is outside the stack segment
  18411.  limit; #PF(fault-code) for a page fault
  18412.  
  18413.  Real Address Mode Exceptions
  18414.  
  18415.  Before executing PUSHA or PUSHAD, the 80386 shuts down if SP or
  18416.  ESP equals 1, 3, or 5; if SP or ESP equals 7, 9, 11, 13, or 15, exception
  18417.  13 occurs
  18418.  
  18419.  Virtual 8086 Mode Exceptions
  18420.  
  18421.  Same exceptions as in real-address mode; #PF(fault-code) for a page
  18422.  fault
  18423.  
  18424.  
  18425.  PUSHF/PUSHFD ── Push Flags Register onto the Stack
  18426.  
  18427.  Opcode  Instruction  Clocks   Description
  18428.  
  18429.  9C      PUSHF        4        Push FLAGS
  18430.  9C      PUSHFD       4        Push EFLAGS
  18431.  
  18432.  
  18433.  Operation
  18434.  
  18435.  IF OperandSize = 32
  18436.  THEN push(EFLAGS);
  18437.  ELSE push(FLAGS);
  18438.  FI;
  18439.  
  18440.  Description
  18441.  
  18442.  PUSHF decrements the stack pointer by 2 and copies the FLAGS
  18443.  register to the new top of stack; PUSHFD decrements the stack pointer by
  18444.  4, and the 80386 EFLAGS register is copied to the new top of stack
  18445.  which is pointed to by SS:eSP. Refer to Chapter 2 and Chapter 4 for
  18446.  information on the EFLAGS register.
  18447.  
  18448.  Flags Affected
  18449.  
  18450.  None
  18451.  
  18452.  Protected Mode Exceptions
  18453.  
  18454.  #SS(0) if the new value of eSP is outside the stack segment boundaries
  18455.  
  18456.  Real Address Mode Exceptions
  18457.  
  18458.  None; the 80386 shuts down due to a lack of stack space
  18459.  
  18460.  Virtual 8086 Mode Exceptions
  18461.  
  18462.  #GP(0) fault if IOPL is less than 3, to permit emulation
  18463.  
  18464.  
  18465.  RCL/RCR/ROL/ROR ── Rotate
  18466.  
  18467. ╓┌────────────┌─────────────────┌───────┌────────────────────────────────────╖
  18468.  Opcode       Instruction       Clocks  Description
  18469.  
  18470.  D0  /2       RCL r/m8,1        9/10    Rotate 9 bits (CF,r/m byte) left
  18471.                                         once
  18472.  D2  /2       RCL r/m8,CL       9/10    Rotate 9 bits (CF,r/m byte) left CL
  18473.                                         times
  18474.  C0  /2 ib    RCL r/m8,imm8     9/10    Rotate 9 bits (CF,r/m byte) left
  18475.                                         imm8 times
  18476.  D1  /2       RCL r/m16,1       9/10    Rotate 17 bits (CF,r/m word) left
  18477.                                         once
  18478.  D3  /2       RCL r/m16,CL      9/10    Rotate 17 bits (CF,r/m word) left
  18479.                                         CL times
  18480.  C1  /2 ib    RCL r/m16,imm8    9/10    Rotate 17 bits (CF,r/m word) left
  18481.                                         imm8 times
  18482.  Opcode       Instruction       Clocks  Description
  18483.                                        imm8 times
  18484.  D1  /2       RCL r/m32,1       9/10    Rotate 33 bits (CF,r/m dword) left
  18485.                                         once
  18486.  D3  /2       RCL r/m32,CL      9/10    Rotate 33 bits (CF,r/m dword) left
  18487.                                         CL times
  18488.  C1  /2 ib    RCL r/m32,imm8    9/10    Rotate 33 bits (CF,r/m dword) left
  18489.                                         imm8 times
  18490.  D0  /3       RCR r/m8,1        9/10    Rotate 9 bits (CF,r/m byte) right
  18491.                                         once
  18492.  D2  /3       RCR r/m8,CL       9/10    Rotate 9 bits (CF,r/m byte) right
  18493.                                         CL times
  18494.  C0  /3 ib    RCR r/m8,imm8     9/10    Rotate 9 bits (CF,r/m byte) right
  18495.                                         imm8 times
  18496.  D1  /3       RCR r/m16,1       9/10    Rotate 17 bits (CF,r/m word) right
  18497.                                         once
  18498.  D3  /3       RCR r/m16,CL      9/10    Rotate 17 bits (CF,r/m word) right
  18499.                                         CL times
  18500.  C1  /3 ib    RCR r/m16,imm8    9/10    Rotate 17 bits (CF,r/m word) right
  18501.                                         imm8 times
  18502.  D1  /3       RCR r/m32,1       9/10    Rotate 33 bits (CF,r/m dword) right
  18503.  Opcode       Instruction       Clocks  Description
  18504. D1  /3       RCR r/m32,1       9/10    Rotate 33 bits (CF,r/m dword) right
  18505.                                         once
  18506.  D3  /3       RCR r/m32,CL      9/10    Rotate 33 bits (CF,r/m dword) right
  18507.                                         CL times
  18508.  C1  /3 ib    RCR r/m32,imm8    9/10    Rotate 33 bits (CF,r/m dword) right
  18509.                                         imm8 times
  18510.  D0  /0       ROL r/m8,1        3/7     Rotate 8 bits r/m byte left once
  18511.  D2  /0       ROL r/m8,CL       3/7     Rotate 8 bits r/m byte left CL
  18512.                                         times
  18513.  C0  /0 ib    ROL r/m8,imm8     3/7     Rotate 8 bits r/m byte left imm8
  18514.                                         times
  18515.  D1  /0       ROL r/m16,1       3/7     Rotate 16 bits r/m word left once
  18516.  D3  /0       ROL r/m16,CL      3/7     Rotate 16 bits r/m word left CL
  18517.                                         times
  18518.  C1  /0 ib    ROL r/m16,imm8    3/7     Rotate 16 bits r/m word left imm8
  18519.                                         times
  18520.  D1  /0       ROL r/m32,1       3/7     Rotate 32 bits r/m dword left once
  18521.  D3  /0       ROL r/m32,CL      3/7     Rotate 32 bits r/m dword left CL
  18522.                                         times
  18523.  C1  /0 ib    ROL r/m32,imm8    3/7     Rotate 32 bits r/m dword left imm8
  18524.  Opcode       Instruction       Clocks  Description
  18525. C1  /0 ib    ROL r/m32,imm8    3/7     Rotate 32 bits r/m dword left imm8
  18526.                                         times
  18527.  D0  /1       ROR r/m8,1        3/7     Rotate 8 bits r/m byte right once
  18528.  D2  /1       ROR r/m8,CL       3/7     Rotate 8 bits r/m byte right CL
  18529.                                         times
  18530.  C0  /1 ib    ROR r/m8,imm8     3/7     Rotate 8 bits r/m word right imm8
  18531.                                         times
  18532.  D1  /1       ROR r/m16,1       3/7     Rotate 16 bits r/m word right once
  18533.  D3  /1       ROR r/m16,CL      3/7     Rotate 16 bits r/m word right CL
  18534.                                         times
  18535.  C1  /1 ib    ROR r/m16,imm8    3/7     Rotate 16 bits r/m word right imm8
  18536.                                         times
  18537.  D1  /1       ROR r/m32,1       3/7     Rotate 32 bits r/m dword right once
  18538.  D3  /1       ROR r/m32,CL      3/7     Rotate 32 bits r/m dword right CL
  18539.                                         times
  18540.  C1  /1 ib    ROR r/m32,imm8    3/7     Rotate 32 bits r/m dword right imm8
  18541.                                         times
  18542.  
  18543.  
  18544.  Operation
  18545.  
  18546.  (* ROL - Rotate Left *)
  18547.  temp  COUNT;
  18548.  WHILE (temp <> 0)
  18549.  DO
  18550.     tmpcf  high-order bit of (r/m);
  18551.     r/m  r/m * 2 + (tmpcf);
  18552.     temp  temp - 1;
  18553.  OD;
  18554.  IF COUNT = 1
  18555.  THEN
  18556.     IF high-order bit of r/m <> CF
  18557.     THEN OF  1;
  18558.     ELSE OF  0;
  18559.     FI;
  18560.  ELSE OF  undefined;
  18561.  FI;
  18562.  (* ROR - Rotate Right *)
  18563.  temp  COUNT;
  18564.  WHILE (temp <> 0 )
  18565.  DO
  18566.     tmpcf  low-order bit of (r/m);
  18567.     r/m  r/m / 2 + (tmpcf * 2^(width(r/m)));
  18568.     temp  temp - 1;
  18569.  DO;
  18570.  IF COUNT = 1
  18571.  THEN
  18572.     IF (high-order bit of r/m) <> (bit next to high-order bit of r/m)
  18573.     THEN OF  1;
  18574.     ELSE OF  0;
  18575.     FI;
  18576.  ELSE OF  undefined;
  18577.  FI;
  18578.  
  18579.  Description
  18580.  
  18581.  Each rotate instruction shifts the bits of the register or memory operand
  18582.  given. The left rotate instructions shift all the bits upward, except for
  18583.  the top bit, which is returned to the bottom. The right rotate instructions
  18584.  do the reverse: the bits shift downward until the bottom bit arrives at
  18585.  the top.
  18586.  
  18587.  For the RCL and RCR instructions, the carry flag is part of the rotated
  18588.  quantity. RCL shifts the carry flag into the bottom bit and shifts the top
  18589.  bit into the carry flag; RCR shifts the carry flag into the top bit and
  18590.  shifts the bottom bit into the carry flag. For the ROL and ROR
  18591.  instructions, the original value of the carry flag is not a part of the
  18592.  result, but the carry flag receives a copy of the bit that was shifted from
  18593.  one end to the other.
  18594.  
  18595.  The rotate is repeated the number of times indicated by the second
  18596.  operand, which is either an immediate number or the contents of the CL
  18597.  register. To reduce the maximum instruction execution time, the 80386
  18598.  does not allow rotation counts greater than 31. If a rotation count greater
  18599.  than 31 is attempted, only the bottom five bits of the rotation are used.
  18600.  The 8086 does not mask rotation counts. The 80386 in Virtual 8086 Mode does
  18601.  mask rotation counts.
  18602.  
  18603.  The overflow flag is defined only for the single-rotate forms of the
  18604.  instructions (second operand = 1). It is undefined in all other cases. For
  18605.  left shifts/rotates, the CF bit after the shift is XORed with the
  18606.  high-order result bit. For right shifts/rotates, the high-order two bits of
  18607.  the result are XORed to get OF.
  18608.  
  18609.  Flags Affected
  18610.  
  18611.  OF only for single rotates; OF is undefined for multi-bit rotates; CF as
  18612.  described above
  18613.  
  18614.  Protected Mode Exceptions
  18615.  
  18616.  #GP(0) if the result is in a nonwritable segment; #GP(0) for an illegal
  18617.  memory operand effective address in the CS, DS, ES, FS, or GS
  18618.  segments; #SS(0) for an illegal address in the SS segment; #PF(fault-code)
  18619.  for a page fault
  18620.  
  18621.  Real Address Mode Exceptions
  18622.  
  18623.  Interrupt 13 if any part of the operand would lie outside of the effective
  18624.  address space from 0 to 0FFFFH
  18625.  
  18626.  Virtual 8086 Mode Exceptions
  18627.  
  18628.  Same exceptions as in Real Address Mode; #PF(fault-code) for a page
  18629.  fault
  18630.  
  18631.  
  18632.  REP/REPE/REPZ/REPNE/REPNZ ── Repeat Following String Operation
  18633.  
  18634. ╓┌────────┌───────────────────┌────────────────┌─────────────────────────────╖
  18635.  Opcode    Instruction         Clocks           Description
  18636.  
  18637.  F3  6C    REP INS r/m8, DX    13+6*(E)CX,
  18638.                                pm=7+6*(E)CX/
  18639.                                27+6*(E)CX      Input (E)CX bytes from port
  18640.                                                 DX into ES:[(E)DI]
  18641.  F3  6D    REP INS r/m16,DX    13+6*(E)CX,
  18642.                                pm=7+6*(E)CX/
  18643.                                27+6*(E)CX      Input (E)CX words from port
  18644.                                                 DX into ES:[(E)DI]
  18645.  F3  6D    REP INS r/m32,DX    13+6*(E)CX,
  18646.                                pm=7+6*(E)CX/
  18647.                                27+6*(E)CX      Input (E)CX dwords from port
  18648.                                                 DX into ES:[(E)DI]
  18649.  F3  A4    REP MOVS m8,m8      5+4*(E)CX        Move (E)CX bytes from
  18650.  Opcode    Instruction         Clocks           Description
  18651. F3  A4    REP MOVS m8,m8      5+4*(E)CX        Move (E)CX bytes from
  18652.                                                 [(E)SI] to ES:[(E)DI]
  18653.  F3  A5    REP MOVS m16,m16    5+4*(E)CX        Move (E)CX words from
  18654.                                                 [(E)SI] to ES:[(E)DI]
  18655.  F3  A5    REP MOVS m32,m32    5+4*(E)CX        Move (E)CX dwords from
  18656.                                                 [(E)SI] to ES:[(E)DI]
  18657.  F3  6E    REP OUTS DX,r/m8    5+12*(E)CX,
  18658.                                pm=6+5*(E)CX/
  18659.                                26+5*(E)CX      Output (E)CX bytes from
  18660.                                                 [(E)SI] to port DX
  18661.  F3  6F    REP OUTS DX,r/m16   5+12*(E)CX,
  18662.                                pm=6+5*(E)CX/
  18663.                                26+5*(E)CX      Output (E)CX words from
  18664.                                                 [(E)SI] to port DX
  18665.  F3  6F    REP OUTS DX,r/m32   5+12*(E)CX,
  18666.                                pm=6+5*(E)CX/
  18667.                                26+5*(E)CX      Output (E)CX dwords from
  18668.                                                 [(E)SI] to port DX
  18669.  F3  AA    REP STOS m8         5+5*(E)CX        Fill (E)CX bytes at
  18670.                                                 ES:[(E)DI] with AL
  18671.  Opcode    Instruction         Clocks           Description
  18672.                                                ES:[(E)DI] with AL
  18673.  F3  AB    REP STOS m16        5+5*(E)CX        Fill (E)CX words at
  18674.                                                 ES:[(E)DI] with AX
  18675.  F3  AB    REP STOS m32        5+5*(E)CX        Fill (E)CX dwords at
  18676.                                                 ES:[(E)DI] with EAX
  18677.  F3  A6    REPE CMPS m8,m8     5+9*N            Find nonmatching bytes in
  18678.                                                 ES:[(E)DI] and [(E)SI]
  18679.  F3  A7    REPE CMPS m16,m16   5+9*N            Find nonmatching words in
  18680.                                                 ES:[(E)DI] and [(E)SI]
  18681.  F3  A7    REPE CMPS m32,m32   5+9*N            Find nonmatching dwords in
  18682.                                                 ES:[(E)DI] and [(E)SI]
  18683.  F3  AE    REPE SCAS m8        5+8*N            Find non-AL byte starting
  18684.                                                 at ES:[(E)DI]
  18685.  F3  AF    REPE SCAS m16       5+8*N            Find non-AX word starting
  18686.                                                 at ES:[(E)DI]
  18687.  F3  AF    REPE SCAS m32       5+8*N            Find non-EAX dword starting
  18688.                                                 at ES:[(E)DI]
  18689.  F2  A6    REPNE CMPS m8,m8    5+9*N            Find matching bytes in
  18690.                                                 ES:[(E)DI] and [(E)SI]
  18691.  F2  A7    REPNE CMPS m16,m16  5+9*N            Find matching words in
  18692.  Opcode    Instruction         Clocks           Description
  18693. F2  A7    REPNE CMPS m16,m16  5+9*N            Find matching words in
  18694.                                                 ES:[(E)DI] and [(E)SI]
  18695.  F2  A7    REPNE CMPS m32,m32  5+9*N            Find matching dwords in
  18696.                                                 ES:[(E)DI] and [(E)SI]
  18697.  F2  AE    REPNE SCAS m8       5+8*N            Find AL, starting at
  18698.                                                 ES:[(E)DI]
  18699.  F2  AF    REPNE SCAS m16      5+8*N            Find AX, starting at
  18700.                                                 ES:[(E)DI]
  18701.  F2  AF    REPNE SCAS m32      5+8*N            Find EAX, starting at
  18702.                                                 ES:[(E)DI]
  18703.  
  18704.  
  18705.  Operation
  18706.  
  18707.  IF AddressSize = 16
  18708.  THEN use CX for CountReg;
  18709.  ELSE (* AddressSize = 32 *) use ECX for CountReg;
  18710.  FI;
  18711.  WHILE CountReg <> 0
  18712.  DO
  18713.     service pending interrupts (if any);
  18714.     perform primitive string instruction;
  18715.     CountReg  CountReg - 1;
  18716.     IF primitive operation is CMPB, CMPW, SCAB, or SCAW
  18717.     THEN
  18718.        IF (instruction is REP/REPE/REPZ) AND (ZF=1)
  18719.        THEN exit WHILE loop
  18720.        ELSE
  18721.           IF (instruction is REPNZ or REPNE) AND (ZF=0)
  18722.           THEN exit WHILE loop;
  18723.           FI;
  18724.        FI;
  18725.     FI;
  18726.  OD;
  18727.  
  18728.  Description
  18729.  
  18730.  REP, REPE (repeat while equal), and REPNE (repeat while not equal)
  18731.  are prefix that are applied to string operation. Each prefix cause the
  18732.  string instruction that follows to be repeated the number of times
  18733.  indicated in the count register or (for REPE and REPNE) until the
  18734.  indicated condition in the zero flag is no longer met.
  18735.  
  18736.  Synonymous forms of REPE and REPNE are REPZ and REPNZ,
  18737.  respectively.
  18738.  
  18739.  The REP prefixes apply only to one string instruction at a time. To repeat
  18740.  a block of instructions, use the LOOP instruction or another looping
  18741.  construct.
  18742.  
  18743.  The precise action for each iteration is as follows:
  18744.  
  18745.    1.  If the address-size attribute is 16 bits, use CX for the count
  18746.        register; if the address-size attribute is 32 bits, use ECX for the
  18747.        count register.
  18748.  
  18749.    2.  Check CX. If it is zero, exit the iteration, and move to the next
  18750.        instruction.
  18751.  
  18752.    3.  Acknowledge any pending interrupts.
  18753.  
  18754.    4.  Perform the string operation once.
  18755.  
  18756.    5.  Decrement CX or ECX by one; no flags are modified.
  18757.  
  18758.    6.  Check the zero flag if the string operation is SCAS or CMPS. If
  18759.        the repeat condition does not hold, exit the iteration and move to
  18760.        the next instruction. Exit the iteration if the prefix is REPE and ZF
  18761.        is 0 (the last comparison was not equal), or if the prefix is REPNE
  18762.        and ZF is one (the last comparison was equal).
  18763.  
  18764.    7.  Return to step 1 for the next iteration.
  18765.  
  18766.  Repeated CMPS and SCAS instructions can be exited if the count is
  18767.  exhausted or if the zero flag fails the repeat condition. These two cases
  18768.  can be distinguished by using either the JCXZ instruction, or by using
  18769.  the conditional jumps that test the zero flag (JZ, JNZ, and JNE).
  18770.  
  18771.  Flags Affected
  18772.  
  18773.  ZF by REP CMPS and REP SCAS as described above
  18774.  
  18775.  Protected Mode Exceptions
  18776.  
  18777.  #UD if a repeat prefix is used before an instruction that is not in the
  18778.  list above; further exceptions can be generated when the string operation is
  18779.  executed; refer to the descriptions of the string instructions themselves
  18780.  
  18781.  Real Address Mode Exceptions
  18782.  
  18783.  Interrupt 6 if a repeat prefix is used before an instruction that is not in
  18784.  the list above; further exceptions can be generated when the string
  18785.  operation is executed; refer to the descriptions of the string instructions
  18786.  themselves
  18787.  
  18788.  Virtual 8086 Mode Exceptions
  18789.  
  18790.  #UD if a repeat prefix is used before an instruction that is not in the
  18791.  list above; further exceptions can be generated when the string operation is
  18792.  executed; refer to the descriptions of the string instructions themselves
  18793.  
  18794.  Notes
  18795.  
  18796.  Not all input/output ports can handle the rate at which the REP INS
  18797.  and REP OUTS instructions execute.
  18798.  
  18799.  
  18800.  RET ── Return from Procedure
  18801.  
  18802.  Opcode     Instruction  Clocks         Description
  18803.  
  18804.  C3         RET          10+m           Return (near) to caller
  18805.  CB         RET          18+m,pm=32+m   Return (far) to caller, same
  18806.                                         privilege
  18807.  CB         RET          pm=68          Return (far), lesser privilege,
  18808.                                         switch stacks
  18809.  C2  iw     RET imm16    10+m           Return (near), pop imm16 bytes of
  18810.                                         parameters
  18811.  CA  iw     RET imm16    18+m,pm=32+m   Return (far), same privilege, pop
  18812.                                         imm16 bytes
  18813.  CA  iw     RET imm16    pm=68          Return (far), lesser privilege, pop
  18814.                                         imm16 bytes
  18815.  
  18816.  
  18817.  Operation
  18818.  
  18819.  IF instruction = near RET
  18820.  THEN;
  18821.     IF OperandSize = 16
  18822.     THEN
  18823.        IP  Pop();
  18824.        EIP  EIP AND 0000FFFFH;
  18825.     ELSE (* OperandSize = 32 *)
  18826.        EIP  Pop();
  18827.     FI;
  18828.     IF instruction has immediate operand THEN eSP  eSP + imm16; FI;
  18829.  FI;
  18830.  
  18831.  IF (PE = 0 OR (PE = 1 AND VM = 1))
  18832.     (* real mode or virtual 8086 mode *)
  18833.     AND instruction = far RET
  18834.  THEN;
  18835.     IF OperandSize = 16
  18836.     THEN
  18837.        IP  Pop();
  18838.        EIP  EIP AND 0000FFFFH;
  18839.        CS  Pop(); (* 16-bit pop *)
  18840.     ELSE (* OperandSize = 32 *)
  18841.        EIP  Pop();
  18842.        CS  Pop(); (* 32-bit pop, high-order 16-bits discarded *)
  18843.     FI;
  18844.     IF instruction has immediate operand THEN eSP  eSP + imm16; FI;
  18845.  FI;
  18846.  
  18847.  IF (PE = 1 AND VM = 0) (* Protected mode, not V86 mode *)
  18848.     AND instruction = far RET
  18849.  THEN
  18850.     IF OperandSize=32
  18851.     THEN Third word on stack must be within stack limits else #SS(0);
  18852.     ELSE Second word on stack must be within stack limits else #SS(0);
  18853.     FI;
  18854.     Return selector RPL must be ≥ CPL ELSE #GP(return selector)
  18855.     IF return selector RPL = CPL
  18856.     THEN GOTO SAME-LEVEL;
  18857.     ELSE GOTO OUTER-PRIVILEGE-LEVEL;
  18858.     FI;
  18859.  FI;
  18860.  
  18861.  SAME-LEVEL:
  18862.     Return selector must be non-null ELSE #GP(0)
  18863.     Selector index must be within its descriptor table limits ELSE
  18864.        #GP(selector)
  18865.     Descriptor AR byte must indicate code segment ELSE #GP(selector)
  18866.     IF non-conforming
  18867.     THEN code segment DPL must equal CPL;
  18868.     ELSE #GP(selector);
  18869.     FI;
  18870.     IF conforming
  18871.     THEN code segment DPL must be ≤ CPL;
  18872.     ELSE #GP(selector);
  18873.     FI;
  18874.     Code segment must be present ELSE #NP(selector);
  18875.     Top word on stack must be within stack limits ELSE #SS(0);
  18876.     IP must be in code segment limit ELSE #GP(0);
  18877.     IF OperandSize=32
  18878.     THEN
  18879.        Load CS:EIP from stack
  18880.        Load CS register with descriptor
  18881.        Increment eSP by 8 plus the immediate offset if it exists
  18882.     ELSE (* OperandSize=16 *)
  18883.        Load CS:IP from stack
  18884.        Load CS register with descriptor
  18885.        Increment eSP by 4 plus the immediate offset if it exists
  18886.     FI;
  18887.  
  18888.  OUTER-PRIVILEGE-LEVEL:
  18889.     IF OperandSize=32
  18890.     THEN Top (16+immediate) bytes on stack must be within stack limits
  18891.        ELSE #SS(0);
  18892.     ELSE Top (8+immediate) bytes on stack must be within stack limits ELSE
  18893.        #SS(0);
  18894.     FI;
  18895.     Examine return CS selector and associated descriptor:
  18896.        Selector must be non-null ELSE #GP(0);
  18897.        Selector index must be within its descriptor table limits ELSE
  18898.           #GP(selector)
  18899.        Descriptor AR byte must indicate code segment ELSE #GP(selector);
  18900.        IF non-conforming
  18901.        THEN code segment DPL must equal return selector RPL
  18902.        ELSE #GP(selector);
  18903.        FI;
  18904.        IF conforming
  18905.        THEN code segment DPL must be ≤ return selector RPL;
  18906.        ELSE #GP(selector);
  18907.        FI;
  18908.        Segment must be present ELSE #NP(selector)
  18909.     Examine return SS selector and associated descriptor:
  18910.        Selector must be non-null ELSE #GP(0);
  18911.        Selector index must be within its descriptor table limits
  18912.           ELSE #GP(selector);
  18913.        Selector RPL must equal the RPL of the return CS selector ELSE
  18914.           #GP(selector);
  18915.        Descriptor AR byte must indicate a writable data segment ELSE
  18916.           #GP(selector);
  18917.        Descriptor DPL must equal the RPL of the return CS selector ELSE
  18918.           #GP(selector);
  18919.        Segment must be present ELSE #NP(selector);
  18920.     IP must be in code segment limit ELSE #GP(0);
  18921.     Set CPL to the RPL of the return CS selector;
  18922.     IF OperandMode=32
  18923.     THEN
  18924.        Load CS:EIP from stack;
  18925.        Set CS RPL to CPL;
  18926.        Increment eSP by 8 plus the immediate offset if it exists;
  18927.        Load SS:eSP from stack;
  18928.     ELSE (* OperandMode=16 *)
  18929.        Load CS:IP from stack;
  18930.        Set CS RPL to CPL;
  18931.        Increment eSP by 4 plus the immediate offset if it exists;
  18932.        Load SS:eSP from stack;
  18933.     FI;
  18934.     Load the CS register with the return CS descriptor;
  18935.     Load the SS register with the return SS descriptor;
  18936.     For each of ES, FS, GS, and DS
  18937.     DO
  18938.        IF the current register setting is not valid for the outer level,
  18939.           set the register to null (selector  AR  0);
  18940.        To be valid, the register setting must satisfy the following
  18941.           properties:
  18942.           Selector index must be within descriptor table limits;
  18943.           Descriptor AR byte must indicate data or readable code segment;
  18944.           IF segment is data or non-conforming code, THEN
  18945.              DPL must be ≥ CPL, or DPL must be ≥ RPL;
  18946.        FI;
  18947.     OD;
  18948.  
  18949.  Description
  18950.  
  18951.  RET transfers control to a return address located on the stack. The
  18952.  address is usually placed on the stack by a CALL instruction, and the
  18953.  return is made to the instruction that follows the CALL.
  18954.  
  18955.  The optional numeric parameter to RET gives the number of stack bytes
  18956.  (OperandMode=16) or words (OperandMode=32) to be released after the return
  18957.  address is popped. These items are typically used as input parameters to the
  18958.  procedure called.
  18959.  
  18960.  For the intrasegment (near) return, the address on the stack is a segment
  18961.  offset, which is popped into the instruction pointer. The CS register is
  18962.  unchanged. For the intersegment (far) return, the address on the stack
  18963.  is a long pointer. The offset is popped first, followed by the selector.
  18964.  
  18965.  In real mode, CS and IP are loaded directly. In Protected Mode, an
  18966.  intersegment return causes the processor to check the descriptor
  18967.  addressed by the return selector. The AR byte of the descriptor must
  18968.  indicate a code segment of equal or lesser privilege (or greater or equal
  18969.  numeric value) than the current privilege level. Returns to a lesser
  18970.  privilege level cause the stack to be reloaded from the value saved beyond
  18971.  the parameter block.
  18972.  
  18973.  The DS, ES, FS, and GS segment registers can be set to 0 by the RET
  18974.  instruction during an interlevel transfer. If these registers refer to
  18975.  segments that cannot be used by the new privilege level, they are set to
  18976.  0 to prevent unauthorized access from the new privilege level.
  18977.  
  18978.  Flags Affected
  18979.  
  18980.  None
  18981.  
  18982.  Protected Mode Exceptions
  18983.  
  18984.  #GP, #NP, or #SS, as described under "Operation" above; #PF(fault-code) for
  18985.  a page fault
  18986.  
  18987.  Real Address Mode Exceptions
  18988.  
  18989.  Interrupt 13 if any part of the operand would be outside the effective
  18990.  address space from 0 to 0FFFFH
  18991.  
  18992.  Virtual 8086 Mode Exceptions
  18993.  
  18994.  Same exceptions as in Real Address Mode; #PF(fault-code) for a page
  18995.  fault
  18996.  
  18997.  
  18998.  SAHF ── Store AH into Flags
  18999.  
  19000.  Opcode  Instruction  Clocks   Description
  19001.  
  19002.  9E      SAHF         3        Store AH into flags SF ZF xx AF xx PF xx CF
  19003.  
  19004.  
  19005.  Operation
  19006.  
  19007.  SF:ZF:xx:AF:xx:PF:xx:CF  AH;
  19008.  
  19009.  Description
  19010.  
  19011.  SAHF loads the flags listed above with values from the AH register,
  19012.  from bits 7, 6, 4, 2, and 0, respectively.
  19013.  
  19014.  Flags Affected
  19015.  
  19016.  SF, ZF, AF, PF, and CF as described above
  19017.  
  19018.  Protected Mode Exceptions
  19019.  
  19020.  None
  19021.  
  19022.  Real Address Mode Exceptions
  19023.  
  19024.  None
  19025.  
  19026.  Virtual 8086 Mode Exceptions
  19027.  
  19028.  None
  19029.  
  19030.  
  19031.  SAL/SAR/SHL/SHR ── Shift Instructions
  19032.  
  19033. ╓┌───────────────┌─────────────────┌───────┌─────────────────────────────────╖
  19034.  Opcode          Instruction       Clocks  Description
  19035.  
  19036.  D0   /4         SAL r/m8,1        3/7     Multiply r/m byte by 2, once
  19037.  D2   /4         SAL r/m8,CL       3/7     Multiply r/m byte by 2, CL times
  19038.  C0   /4 ib      SAL r/m8,imm8     3/7     Multiply r/m byte by 2, imm8
  19039.                                            times
  19040.  D1   /4         SAL r/m16,1       3/7     Multiply r/m word by 2, once
  19041.  D3   /4         SAL r/m16,CL      3/7     Multiply r/m word by 2, CL times
  19042.  C1   /4 ib      SAL r/m16,imm8    3/7     Multiply r/m word by 2, imm8
  19043.                                            times
  19044.  D1   /4         SAL r/m32,1       3/7     Multiply r/m dword by 2, once
  19045.  D3   /4         SAL r/m32,CL      3/7     Multiply r/m dword by 2, CL
  19046.                                            times
  19047.  C1   /4 ib      SAL r/m32,imm8    3/7     Multiply r/m dword by 2, imm8
  19048.                                            times
  19049.  Opcode          Instruction       Clocks  Description
  19050.                                           times
  19051.  D0   /7         SAR r/m8,1        3/7     Signed divide^(1) r/m byte by 2,
  19052.                                            once
  19053.  D2   /7         SAR r/m8,CL       3/7     Signed divide^(1) r/m byte by 2,
  19054.                                            CL times
  19055.  C0   /7 ib      SAR r/m8,imm8     3/7     Signed divide^(1) r/m byte by 2,
  19056.                                            imm8 times
  19057.  D1   /7         SAR r/m16,1       3/7     Signed divide^(1) r/m word by 2,
  19058.                                            once
  19059.  D3   /7         SAR r/m16,CL      3/7     Signed divide^(1) r/m word by 2,
  19060.                                            CL times
  19061.  C1   /7 ib      SAR r/m16,imm8    3/7     Signed divide^(1) r/m word by 2,
  19062.                                            imm8 times
  19063.  D1   /7         SAR r/m32,1       3/7     Signed divide^(1) r/m dword by 2,
  19064.                                            once
  19065.  D3   /7         SAR r/m32,CL      3/7     Signed divide^(1) r/m dword by 2,
  19066.                                            CL times
  19067.  C1   /7 ib      SAR r/m32,imm8    3/7     Signed divide^(1) r/m dword by 2,
  19068.                                            imm8 times
  19069.  D0   /4         SHL r/m8,1        3/7     Multiply r/m byte by 2, once
  19070.  Opcode          Instruction       Clocks  Description
  19071. D0   /4         SHL r/m8,1        3/7     Multiply r/m byte by 2, once
  19072.  D2   /4         SHL r/m8,CL       3/7     Multiply r/m byte by 2, CL times
  19073.  C0   /4 ib      SHL r/m8,imm8     3/7     Multiply r/m byte by 2, imm8
  19074.                                            times
  19075.  D1   /4         SHL r/m16,1       3/7     Multiply r/m word by 2, once
  19076.  D3   /4         SHL r/m16,CL      3/7     Multiply r/m word by 2, CL times
  19077.  C1   /4 ib      SHL r/m16,imm8    3/7     Multiply r/m word by 2, imm8
  19078.                                            times
  19079.  D1   /4         SHL r/m32,1       3/7     Multiply r/m dword by 2, once
  19080.  D3   /4         SHL r/m32,CL      3/7     Multiply r/m dword by 2, CL
  19081.                                            times
  19082.  C1   /4 ib      SHL r/m32,imm8    3/7     Multiply r/m dword by 2, imm8
  19083.                                            times
  19084.  D0   /5         SHR r/m8,1        3/7     Unsigned divide r/m byte by 2,
  19085.                                            once
  19086.  D2   /5         SHR r/m8,CL       3/7     Unsigned divide r/m byte by 2,
  19087.                                            CL times
  19088.  C0   /5 ib      SHR r/m8,imm8     3/7     Unsigned divide r/m byte by 2,
  19089.                                            imm8 times
  19090.  D1   /5         SHR r/m16,1       3/7     Unsigned divide r/m word by 2,
  19091.  Opcode          Instruction       Clocks  Description
  19092. D1   /5         SHR r/m16,1       3/7     Unsigned divide r/m word by 2,
  19093.                                            once
  19094.  D3   /5         SHR r/m16,CL      3/7     Unsigned divide r/m word by 2,
  19095.                                            CL times
  19096.  C1   /5 ib      SHR r/m16,imm8    3/7     Unsigned divide r/m word by 2,
  19097.                                            imm8 times
  19098.  D1   /5         SHR r/m32,1       3/7     Unsigned divide r/m dword by 2,
  19099.                                            once
  19100.  D3   /5         SHR r/m32,CL      3/7     Unsigned divide r/m dword by 2,
  19101.                                            CL times
  19102.  C1   /5 ib      SHR r/m32,imm8    3/7     Unsigned divide r/m dword by 2,
  19103.                                            imm8 times
  19104.  
  19105.  
  19106.  Not the same division as IDIV; rounding is toward negative infinity.
  19107.  
  19108.  Operation
  19109.  
  19110.  (* COUNT is the second parameter *)
  19111.  (temp)  COUNT;
  19112.  WHILE (temp <> 0)
  19113.  DO
  19114.     IF instruction is SAL or SHL
  19115.     THEN CF  high-order bit of r/m;
  19116.     FI;
  19117.     IF instruction is SAR or SHR
  19118.     THEN CF  low-order bit of r/m;
  19119.     FI;
  19120.     IF instruction = SAL or SHL
  19121.     THEN r/m  r/m * 2;
  19122.     FI;
  19123.     IF instruction = SAR
  19124.     THEN r/m  r/m /2 (*Signed divide, rounding toward negative infinity*);
  19125.     FI;
  19126.     IF instruction = SHR
  19127.     THEN r/m  r/m / 2; (* Unsigned divide *);
  19128.     FI;
  19129.     temp  temp - 1;
  19130.  OD;
  19131.  (* Determine overflow for the various instructions *)
  19132.  IF COUNT = 1
  19133.  THEN
  19134.     IF instruction is SAL or SHL
  19135.     THEN OF  high-order bit of r/m <> (CF);
  19136.     FI;
  19137.     IF instruction is SAR
  19138.     THEN OF  0;
  19139.     FI;
  19140.     IF instruction is SHR
  19141.     THEN OF  high-order bit of operand;
  19142.     FI;
  19143.  ELSE OF  undefined;
  19144.  FI;
  19145.  
  19146.  Description
  19147.  
  19148.  SAL (or its synonym, SHL) shifts the bits of the operand upward. The
  19149.  high-order bit is shifted into the carry flag, and the low-order bit is set
  19150.  to 0.
  19151.  
  19152.  SAR and SHR shift the bits of the operand downward. The low-order
  19153.  bit is shifted into the carry flag. The effect is to divide the operand by
  19154.  2. SAR performs a signed divide with rounding toward negative infinity (not
  19155.  the same as IDIV); the high-order bit remains the same. SHR performs an
  19156.  unsigned divide; the high-order bit is set to 0.
  19157.  
  19158.  The shift is repeated the number of times indicated by the second
  19159.  operand, which is either an immediate number or the contents of the CL
  19160.  register. To reduce the maximum execution time, the 80386 does not
  19161.  allow shift counts greater than 31. If a shift count greater than 31 is
  19162.  attempted, only the bottom five bits of the shift count are used. (The
  19163.  8086 uses all eight bits of the shift count.)
  19164.  
  19165.  The overflow flag is set only if the single-shift forms of the instructions
  19166.  are used. For left shifts, OF is set to 0 if the high bit of the answer is
  19167.  the same as the result of the carry flag (i.e., the top two bits of the
  19168.  original operand were the same); OF is set to 1 if they are different. For
  19169.  SAR, OF is set to 0 for all single shifts. For SHR, OF is set to the
  19170.  high-order bit of the original operand.
  19171.  
  19172.  Flags Affected
  19173.  
  19174.  OF for single shifts; OF is undefined for multiple shifts; CF, ZF, PF,
  19175.  and SF as described in Appendix C
  19176.  
  19177.  Protected Mode Exceptions
  19178.  
  19179.  #GP(0) if the result is in a nonwritable segment; #GP(0) for an illegal
  19180.  memory operand effective address in the CS, DS, ES, FS, or GS
  19181.  segments; #SS(0) for an illegal address in the SS segment; #PF(fault-code)
  19182.  for a page fault
  19183.  
  19184.  Real Address Mode Exceptions
  19185.  
  19186.  Interrupt 13 if any part of the operand would lie outside of the effective
  19187.  address space from 0 to 0FFFFH
  19188.  
  19189.  Virtual 8086 Mode Exceptions
  19190.  
  19191.  Same exceptions as in Real Address Mode; #PF(fault-code) for a page
  19192.  fault
  19193.  
  19194.  
  19195.  SBB ── Integer Subtraction with Borrow
  19196.  
  19197. ╓┌────────────┌────────────────┌───────┌─────────────────────────────────────╖
  19198.  Opcode       Instruction       Clocks  Description
  19199.  
  19200.  1C  ib       SBB AL,imm8       2       Subtract with borrow immediate byte
  19201.                                         from AL
  19202.  1D  iw       SBB AX,imm16      2       Subtract with borrow immediate word
  19203.                                         from AX
  19204.  1D  id       SBB EAX,imm32     2       Subtract with borrow immediate
  19205.                                         dword from EAX
  19206.  80  /3 ib    SBB r/m8,imm8     2/7     Subtract with borrow immediate byte
  19207.                                         from r/m byte
  19208.  81  /3 iw    SBB r/m16,imm16   2/7     Subtract with borrow immediate word
  19209.                                         from r/m word
  19210.  81  /3 id    SBB r/m32,imm32   2/7     Subtract with borrow immediate
  19211.                                         dword from r/m dword
  19212.  83  /3 ib    SBB r/m16,imm8    2/7     Subtract with borrow sign-extended
  19213.                                         immediate byte from r/m word
  19214.  83  /3 ib    SBB r/m32,imm8    2/7     Subtract with borrow sign-extended
  19215.                                         immediate byte from r/m dword
  19216.  18  /r       SBB r/m8,r8       2/6     Subtract with borrow byte register
  19217.  Opcode       Instruction       Clocks  Description
  19218. 18  /r       SBB r/m8,r8       2/6     Subtract with borrow byte register
  19219.                                         from r/m byte
  19220.  19  /r       SBB r/m16,r16     2/6     Subtract with borrow word register
  19221.                                         from r/m word
  19222.  19  /r       SBB r/m32,r32     2/6     Subtract with borrow dword register
  19223.                                         from r/m dword
  19224.  1A  /r       SBB r8,r/m8       2/7     Subtract with borrow byte register
  19225.                                         from r/m byte
  19226.  1B  /r       SBB r16,r/m16     2/7     Subtract with borrow word register
  19227.                                         from r/m word
  19228.  1B  /r       SBB r32,r/m32     2/7     Subtract with borrow dword register
  19229.                                         from r/m dword
  19230.  
  19231.  
  19232.  Operation
  19233.  
  19234.  IF SRC is a byte and DEST is a word or dword
  19235.  THEN DEST = DEST - (SignExtend(SRC) + CF)
  19236.  ELSE DEST  DEST - (SRC + CF);
  19237.  
  19238.  Description
  19239.  
  19240.  SBB adds the second operand (DEST) to the carry flag (CF) and
  19241.  subtracts the result from the first operand (SRC). The result of the
  19242.  subtraction is assigned to the first operand (DEST), and the flags are
  19243.  set accordingly.
  19244.  
  19245.  When an immediate byte value is subtracted from a word operand, the
  19246.  immediate value is first sign-extended.
  19247.  
  19248.  Flags Affected
  19249.  
  19250.  OF, SF, ZF, AF, PF, and CF as described in Appendix C
  19251.  
  19252.  Protected Mode Exceptions
  19253.  
  19254.  #GP(0) if the result is in a nonwritable segment; #GP(0) for an illegal
  19255.  memory operand effective address in the CS, DS, ES, FS, or GS
  19256.  segments; #SS(0) for an illegal address in the SS segment; #PF(fault-code)
  19257.  for a page fault
  19258.  
  19259.  Real Address Mode Exceptions
  19260.  
  19261.  Interrupt 13 if any part of the operand would lie outside of the effective
  19262.  address space from 0 to 0FFFFH
  19263.  
  19264.  Virtual 8086 Mode Exceptions
  19265.  
  19266.  Same exceptions as in Real Address Mode; #PF(fault-code) for a page
  19267.  fault
  19268.  
  19269.  
  19270.  SCAS/SCASB/SCASW/SCASD ── Compare String Data
  19271.  
  19272.  Opcode  Instruction  Clocks  Description
  19273.  
  19274.  AE      SCAS m8      7       Compare bytes AL-ES:[DI], update (E)DI
  19275.  AF      SCAS m16     7       Compare words AX-ES:[DI], update (E)DI
  19276.  AF      SCAS m32     7       Compare dwords EAX-ES:[DI], update (E)DI
  19277.  AE      SCASB        7       Compare bytes AL-ES:[DI], update (E)DI
  19278.  AF      SCASW        7       Compare words AX-ES:[DI], update (E)DI
  19279.  AF      SCASD        7       Compare dwords EAX-ES:[DI], update (E)DI
  19280.  
  19281.  
  19282.  Operation
  19283.  
  19284.  IF AddressSize = 16
  19285.  THEN use DI for dest-index;
  19286.  ELSE (* AddressSize = 32 *) use EDI for dest-index;
  19287.  FI;
  19288.  IF byte type of instruction
  19289.  THEN
  19290.     AL - [dest-index]; (* Compare byte in AL and dest *)
  19291.     IF DF = 0 THEN IndDec  1 ELSE IncDec  -1; FI;
  19292.  ELSE
  19293.     IF OperandSize = 16
  19294.     THEN
  19295.        AX - [dest-index]; (* compare word in AL and dest *)
  19296.        IF DF = 0 THEN IncDec  2 ELSE IncDec  -2; FI;
  19297.     ELSE (* OperandSize = 32 *)
  19298.        EAX - [dest-index];(* compare dword in EAX & dest *)
  19299.        IF DF = 0 THEN IncDec  4 ELSE IncDec  -4; FI;
  19300.     FI;
  19301.  FI;
  19302.  dest-index = dest-index + IncDec
  19303.  
  19304.  Description
  19305.  
  19306.  SCAS subtracts the memory byte or word at the destination register from
  19307.  the AL, AX or EAX register. The result is discarded; only the flags are set.
  19308.  The operand must be addressable from the ES segment; no segment override is
  19309.  possible.
  19310.  
  19311.  If the address-size attribute for this instruction is 16 bits, DI is used
  19312.  as the destination register; otherwise, the address-size attribute is 32
  19313.  bits and EDI is used.
  19314.  
  19315.  The address of the memory data being compared is determined solely by the
  19316.  contents of the destination register, not by the operand to SCAS. The
  19317.  operand validates ES segment addressability and determines the data type.
  19318.  Load the correct index value into DI or EDI before executing SCAS.
  19319.  
  19320.  After the comparison is made, the destination register is automatically
  19321.  updated. If the direction flag is 0 (CLD was executed), the destination
  19322.  register is incremented; if the direction flag is 1 (STD was executed), it
  19323.  is decremented. The increments or decrements are by 1 if bytes are compared,
  19324.  by 2 if words are compared, or by 4 if doublewords are compared.
  19325.  
  19326.  SCASB, SCASW, and SCASD are synonyms for the byte, word and
  19327.  doubleword SCAS instructions that don't require operands. They are
  19328.  simpler to code, but provide no type or segment checking.
  19329.  
  19330.  SCAS can be preceded by the REPE or REPNE prefix for a block search
  19331.  of CX or ECX bytes or words. Refer to the REP instruction for further
  19332.  details.
  19333.  
  19334.  Flags Affected
  19335.  
  19336.  OF, SF, ZF, AF, PF, and CF as described in Appendix C
  19337.  
  19338.  Protected Mode Exceptions
  19339.  
  19340.  #GP(0) for an illegal memory operand effective address in the CS, DS,
  19341.  ES, FS, or GS segments; #SS(0) for an illegal address in the SS segment;
  19342.  #PF(fault-code) for a page fault
  19343.  
  19344.  Real Address Mode Exceptions
  19345.  
  19346.  Interrupt 13 if any part of the operand would lie outside of the effective
  19347.  address space from 0 to 0FFFFH
  19348.  
  19349.  Virtual 8086 Mode Exceptions
  19350.  
  19351.  Same exceptions as in Real Address Mode; #PF(fault-code) for a page
  19352.  fault
  19353.  
  19354.  
  19355.  SETcc ── Byte Set on Condition
  19356.  
  19357. ╓┌────────┌────────────┌───────┌─────────────────────────────────────────────╖
  19358.  Opcode   Instruction  Clocks  Description
  19359.  
  19360.  0F  97   SETA r/m8    4/5     Set byte if above (CF=0 and ZF=0)
  19361.  0F  93   SETAE r/m8   4/5     Set byte if above or equal (CF=0)
  19362.  0F  92   SETB r/m8    4/5     Set byte if below (CF=1)
  19363.  0F  96   SETBE r/m8   4/5     Set byte if below or equal (CF=1 or (ZF=1)
  19364.  Opcode   Instruction  Clocks  Description
  19365. 0F  96   SETBE r/m8   4/5     Set byte if below or equal (CF=1 or (ZF=1)
  19366.  0F  92   SETC r/m8    4/5     Set if carry (CF=1)
  19367.  0F  94   SETE r/m8    4/5     Set byte if equal (ZF=1)
  19368.  0F  9F   SETG r/m8    4/5     Set byte if greater (ZF=0 or SF=OF)
  19369.  0F  9D   SETGE r/m8   4/5     Set byte if greater or equal (SF=OF)
  19370.  0F  9C   SETL r/m8    4/5     Set byte if less (SF<>OF)
  19371.  0F  9E   SETLE r/m8   4/5     Set byte if less or equal (ZF=1 and
  19372.                                SF<>OF)
  19373.  0F  96   SETNA r/m8   4/5     Set byte if not above (CF=1)
  19374.  0F  92   SETNAE r/m8  4/5     Set byte if not above or equal (CF=1)
  19375.  0F  93   SETNB r/m8   4/5     Set byte if not below (CF=0)
  19376.  0F  97   SETNBE r/m8  4/5     Set byte if not below or equal (CF=0 and
  19377.                                ZF=0)
  19378.  0F  93   SETNC r/m8   4/5     Set byte if not carry (CF=0)
  19379.  0F  95   SETNE r/m8   4/5     Set byte if not equal (ZF=0)
  19380.  0F  9E   SETNG r/m8   4/5     Set byte if not greater (ZF=1 or SF<>OF)
  19381.  0F  9C   SETNGE r/m8  4/5     Set if not greater or equal (SF<>OF)
  19382.  0F  9D   SETNL r/m8   4/5     Set byte if not less (SF=OF)
  19383.  0F  9F   SETNLE r/m8  4/5     Set byte if not less or equal (ZF=1 and
  19384.                                SF<>OF)
  19385.  Opcode   Instruction  Clocks  Description
  19386.                               SF<>OF)
  19387.  0F  91   SETNO r/m8   4/5     Set byte if not overflow (OF=0)
  19388.  0F  9B   SETNP r/m8   4/5     Set byte if not parity (PF=0)
  19389.  0F  99   SETNS r/m8   4/5     Set byte if not sign (SF=0)
  19390.  0F  95   SETNZ r/m8   4/5     Set byte if not zero (ZF=0)
  19391.  0F  90   SETO r/m8    4/5     Set byte if overflow (OF=1)
  19392.  0F  9A   SETP r/m8    4/5     Set byte if parity (PF=1)
  19393.  0F  9A   SETPE r/m8   4/5     Set byte if parity even (PF=1)
  19394.  0F  9B   SETPO r/m8   4/5     Set byte if parity odd (PF=0)
  19395.  0F  98   SETS r/m8    4/5     Set byte if sign (SF=1)
  19396.  0F  94   SETZ r/m8    4/5     Set byte if zero (ZF=1)
  19397.  
  19398.  
  19399.  Operation
  19400.  
  19401.  IF condition THEN r/m8  1 ELSE r/m8  0; FI;
  19402.  
  19403.  Description
  19404.  
  19405.  SETcc stores a byte at the destination specified by the effective address
  19406.  or register if the condition is met, or a 0 byte if the condition is not
  19407.  met.
  19408.  
  19409.  Flags Affected
  19410.  
  19411.  None
  19412.  
  19413.  Protected Mode Exceptions
  19414.  
  19415.  #GP(0) if the result is in a non-writable segment; #GP(0) for an illegal
  19416.  memory operand effective address in the CS, DS, ES, FS, or GS segments;
  19417.  #SS(0) for an illegal address in the SS segment; #PF(fault-code) for a page
  19418.  fault
  19419.  
  19420.  Real Address Mode Exceptions
  19421.  
  19422.  Interrupt 13 if any part of the operand would lie outside of the effective
  19423.  address space from 0 to 0FFFFH
  19424.  
  19425.  Virtual 8086 Mode Exceptions
  19426.  
  19427.  Same exceptions as in Real Address Mode; #PF(fault-code) for a page
  19428.  fault
  19429.  
  19430.  
  19431.  SGDT/SIDT ── Store Global/Interrupt Descriptor Table Register
  19432.  
  19433.  Opcode       Instruction   Clocks   Description
  19434.  
  19435.  0F  01 /0    SGDT m        9        Store GDTR to m
  19436.  0F  01 /1    SIDT m        9        Store IDTR to m
  19437.  
  19438.  
  19439.  Operation
  19440.  
  19441.  DEST  48-bit BASE/LIMIT register contents;
  19442.  
  19443.  Description
  19444.  
  19445.  SGDT/SIDT copies the contents of the descriptor table register the six
  19446.  bytes of memory indicated by the operand. The LIMIT field of the
  19447.  register is assigned to the first word at the effective address. If the
  19448.  operand-size attribute is 32 bits, the next three bytes are assigned the
  19449.  BASE field of the register, and the fourth byte is written with zero. The
  19450.  last byte is undefined. Otherwise, if the operand-size attribute is 16
  19451.  bits, the next four bytes are assigned the 32-bit BASE field of the
  19452.  register.
  19453.  
  19454.  SGDT and SIDT are used only in operating system software; they are
  19455.  not used in application programs.
  19456.  
  19457.  Flags Affected
  19458.  
  19459.  None
  19460.  
  19461.  Protected Mode Exceptions
  19462.  
  19463.  Interrupt 6 if the destination operand is a register; #GP(0) if the
  19464.  destination is in a nonwritable segment; #GP(0) for an illegal memory
  19465.  operand effective address in the CS, DS, ES, FS, or GS segments; #SS(0) for
  19466.  an illegal address in the SS segment; #PF(fault-code) for a page fault
  19467.  
  19468.  Real Address Mode Exceptions
  19469.  
  19470.  Interrupt 6 if the destination operand is a register; Interrupt 13 if any
  19471.  part of the operand would lie outside of the effective address space from
  19472.  0 to 0FFFFH
  19473.  
  19474.  Virtual 8086 Mode Exceptions
  19475.  
  19476.  Same exceptions as in Real Address Mode; #PF(fault-code) for a page
  19477.  fault
  19478.  
  19479.  Compatability Note
  19480.  
  19481.  The 16-bit forms of the SGDT/SIDT instructions are compatible with
  19482.  the 80286, if the value in the upper eight bits is not referenced. The
  19483.  80286 stores 1's in these upper bits, whereas the 80386 stores 0's if the
  19484.  operand-size attribute is 16 bits. These bits were specified as undefined
  19485.  by the SGDT/SIDT instructions in the iAPX 286 Programmer's
  19486.  Reference Manual.
  19487.  
  19488.  
  19489.  SHLD ── Double Precision Shift Left
  19490.  
  19491.  Opcode   Instruction          Clocks   Description
  19492.  
  19493.  0F  A4   SHLD r/m16,r16,imm8  3/7      r/m16 gets SHL of r/m16 concatenated
  19494.                                         with r16
  19495.  0F  A4   SHLD r/m32,r32,imm8  3/7      r/m32 gets SHL of r/m32 concatenated
  19496.                                         with r32
  19497.  0F  A5   SHLD r/m16,r16,CL    3/7      r/m16 gets SHL of r/m16 concatenated
  19498.                                         with r16
  19499.  0F  A5   SHLD r/m32,r32,CL    3/7      r/m32 gets SHL of r/m32 concatenated
  19500.                                         with r32
  19501.  
  19502.  
  19503.  Operation
  19504.  
  19505.  (* count is an unsigned integer corresponding to the last operand of the
  19506.  instruction, either an immediate byte or the byte in register CL *)
  19507.  ShiftAmt  count MOD 32;
  19508.  inBits  register; (* Allow overlapped operands *)
  19509.  IF ShiftAmt = 0
  19510.  THEN no operation
  19511.  ELSE
  19512.     IF ShiftAmt ≥ OperandSize
  19513.     THEN (* Bad parameters *)
  19514.        r/m  UNDEFINED;
  19515.        CF, OF, SF, ZF, AF, PF  UNDEFINED;
  19516.     ELSE (* Perform the shift *)
  19517.        CF  BIT[Base, OperandSize - ShiftAmt];
  19518.           (* Last bit shifted out on exit *)
  19519.     FOR i  OperandSize - 1 DOWNTO ShiftAmt
  19520.     DO
  19521.        BIT[Base, i]  BIT[Base, i - ShiftAmt];
  19522.     OF;
  19523.     FOR i  ShiftAmt - 1 DOWNTO 0
  19524.     DO
  19525.        BIT[Base, i]  BIT[inBits, i - ShiftAmt + OperandSize];
  19526.     OD;
  19527.     Set SF, ZF, PF (r/m);
  19528.        (* SF, ZF, PF are set according to the value of the result *)
  19529.     AF  UNDEFINED;
  19530.     FI;
  19531.  FI;
  19532.  
  19533.  Description
  19534.  
  19535.  SHLD shifts the first operand provided by the r/m field to the left as
  19536.  many bits as specified by the count operand. The second operand (r16 or r32)
  19537.  provides the bits to shift in from the right (starting with bit 0). The
  19538.  result is stored back into the r/m operand. The register remains unaltered.
  19539.  
  19540.  The count operand is provided by either an immediate byte or the contents
  19541.  of the CL register. These operands are taken MODULO 32 to provide a number
  19542.  between 0 and 31 by which to shift. Because the bits to shift are provided
  19543.  by the specified registers, the operation is useful for multiprecision
  19544.  shifts (64 bits or more). The SF, ZF and PF flags are set according to the
  19545.  value of the result. CS is set to the value of the last bit shifted out. OF
  19546.  and AF are left undefined.
  19547.  
  19548.  Flags Affected
  19549.  
  19550.  OF, SF, ZF, PF, and CF as described above; AF and OF are undefined
  19551.  
  19552.  Protected Mode Exceptions
  19553.  
  19554.  #GP(0) if the result is in a nonwritable segment; #GP(0) for an illegal
  19555.  memory operand effective address in the CS, DS, ES, FS, or GS segments;
  19556.  #SS(0) for an illegal address in the SS segment; #PF(fault-code) for a page
  19557.  fault
  19558.  
  19559.  Real Address Mode Exceptions
  19560.  
  19561.  Interrupt 13 if any part of the operand would lie outside of the effective
  19562.  address space from 0 to 0FFFFH
  19563.  
  19564.  Virtual 8086 Mode Exceptions
  19565.  
  19566.  Same exceptions as in Real Address Mode; #PF(fault-code) for a page fault
  19567.  
  19568.  
  19569.  SHRD ── Double Precision Shift Right
  19570.  
  19571.  Opcode   Instruction           Clocks  Description
  19572.  
  19573.  0F  AC   SHRD r/m16,r16,imm8   3/7     r/m16 gets SHR of r/m16 concatenated
  19574.                                         with r16
  19575.  0F  AC   SHRD r/m32,r32,imm8   3/7     r/m32 gets SHR of r/m32 concatenated
  19576.                                         with r32
  19577.  0F  AD   SHRD r/m16,r16,CL     3/7     r/m16 gets SHR of r/m16 concatenated
  19578.                                         with r16
  19579.  0F  AD   SHRD r/m32,r32,CL     3/7     r/m32 gets SHR of r/m32 concatenated
  19580.                                         with r32
  19581.  
  19582.  
  19583.  Operation
  19584.  
  19585.  (* count is an unsigned integer corresponding to the last operand of the
  19586.  instruction, either an immediate byte or the byte in register CL *)
  19587.  ShiftAmt  count MOD 32;
  19588.  inBits  register; (* Allow overlapped operands *)
  19589.  IF ShiftAmt = 0
  19590.  THEN no operation
  19591.  ELSE
  19592.     IF ShiftAmt ≥ OperandSize
  19593.     THEN (* Bad parameters *)
  19594.        r/m  UNDEFINED;
  19595.        CF, OF, SF, ZF, AF, PF  UNDEFINED;
  19596.     ELSE (* Perform the shift *)
  19597.        CF  BIT[r/m, ShiftAmt - 1]; (* last bit shifted out on exit *)
  19598.        FOR i  0 TO OperandSize - 1 - ShiftAmt
  19599.        DO
  19600.           BIT[r/m, i]  BIT[r/m, i - ShiftAmt];
  19601.        OD;
  19602.        FOR i  OperandSize - ShiftAmt TO OperandSize - 1
  19603.        DO
  19604.           BIT[r/m,i]  BIT[inBits,i+ShiftAmt - OperandSize];
  19605.        OD;
  19606.        Set SF, ZF, PF (r/m);
  19607.           (* SF, ZF, PF are set according to the value of the result *)
  19608.        Set SF, ZF, PF (r/m);
  19609.        AF  UNDEFINED;
  19610.     FI;
  19611.  FI;
  19612.  
  19613.  Description
  19614.  
  19615.  SHRD shifts the first operand provided by the r/m field to the right as many
  19616.  bits as specified by the count operand. The second operand (r16 or r32)
  19617.  provides the bits to shift in from the left (starting with bit 31). The
  19618.  result is stored back into the r/m operand. The register remains unaltered.
  19619.  
  19620.  The count operand is provided by either an immediate byte or the contents
  19621.  of the CL register. These operands are taken MODULO 32 to provide a number
  19622.  between 0 and 31 by which to shift. Because the bits to shift are provided
  19623.  by the specified register, the operation is useful for multi-precision
  19624.  shifts (64 bits or more). The SF, ZF and PF flags are set according to the
  19625.  value of the result. CS is set to the value of the last bit shifted out. OF
  19626.  and AF are left undefined.
  19627.  
  19628.  Flags Affected
  19629.  
  19630.  OF, SF, ZF, PF, and CF as described above; AF and OF are undefined
  19631.  
  19632.  Protected Mode Exceptions
  19633.  
  19634.  #GP(0) if the result is in a nonwritable segment; #GP(0) for an illegal
  19635.  memory operand effective address in the CS, DS, ES, FS, or GS
  19636.  segments; #SS(0) for an illegal address in the SS segment; #PF(fault-code)
  19637.  for a page fault
  19638.  
  19639.  Real Address Mode Exceptions
  19640.  
  19641.  Interrupt 13 if any part of the operand would lie outside of the effective
  19642.  address space from 0 to 0FFFFH
  19643.  
  19644.  Virtual 8086 Mode Exceptions
  19645.  
  19646.  Same exceptions as in Real Address Mode; #PF(fault-code) for a page
  19647.  fault
  19648.  
  19649.  
  19650.  SLDT ── Store Local Descriptor Table Register
  19651.  
  19652.  Opcode      Instruction   Clocks      Description
  19653.  
  19654.  0F  00 /0   SLDT r/m16    pm=2/2      Store LDTR to EA word
  19655.  
  19656.  
  19657.  Operation
  19658.  
  19659.  r/m16  LDTR;
  19660.  
  19661.  Description
  19662.  
  19663.  SLDT stores the Local Descriptor Table Register (LDTR) in the two-byte
  19664.  register or memory location indicated by the effective address operand.
  19665.  This register is a selector that points into the Global Descriptor Table.
  19666.  
  19667.  SLDT is used only in operating system software. It is not used in
  19668.  application programs.
  19669.  
  19670.  Flags Affected
  19671.  
  19672.  None
  19673.  
  19674.  Protected Mode Exceptions
  19675.  
  19676.  #GP(0) if the result is in a nonwritable segment; #GP(0) for an illegal
  19677.  memory operand effective address in the CS, DS, ES, FS, or GS segments;
  19678.  #SS(0) for an illegal address in the SS segment; #PF(fault-code) for a page
  19679.  fault
  19680.  
  19681.  Real Address Mode Exceptions
  19682.  
  19683.  Interrupt 6; SLDT is not recognized in Real Address Mode
  19684.  
  19685.  Virtual 8086 Mode Exceptions
  19686.  
  19687.  Same exceptions as in Real Address Mode; #PF(fault-code) for a page fault
  19688.  
  19689.  Notes
  19690.  
  19691.  The operand-size attribute has no effect on the operation of the
  19692.  instruction.
  19693.  
  19694.  
  19695.  SMSW ── Store Machine Status Word
  19696.  
  19697.  Opcode      Instruction     Clocks          Description
  19698.  
  19699.  0F  01 /4   SMSW r/m16      2/3,pm=2/2      Store machine status word to EA
  19700.                                              word
  19701.  
  19702.  
  19703.  Operation
  19704.  
  19705.  r/m16  MSW;
  19706.  
  19707.  Description
  19708.  
  19709.  SMSW stores the machine status word (part of CR0) in the two-byte register
  19710.  or memory location indicated by the effective address operand.
  19711.  
  19712.  Flags Affected
  19713.  
  19714.  None
  19715.  
  19716.  Protected Mode Exceptions
  19717.  
  19718.  #GP(0) if the result is in a nonwritable segment; #GP(0) for an illegal
  19719.  memory operand effective address in the CS, DS, ES, FS, or GS segments;
  19720.  #SS(0) for an illegal address in the SS segment; #PF(fault-code) for a page
  19721.  fault
  19722.  
  19723.  Real Address Mode Exceptions
  19724.  
  19725.  Interrupt 13 if any part of the operand would lie outside of the effective
  19726.  address space from 0 to 0FFFFH
  19727.  
  19728.  Virtual 8086 Mode Exceptions
  19729.  
  19730.  Same exceptions as in Real Address Mode; #PF(fault-code) for a page fault
  19731.  
  19732.  Notes
  19733.  
  19734.  This instruction is provided for compatibility with the 80286; 80386
  19735.  programs should use MOV ..., CR0.
  19736.  
  19737.  
  19738.  STC ── Set Carry Flag
  19739.  
  19740.  Opcode      Instruction     Clocks      Description
  19741.  
  19742.  F9          STC             2           Set carry flag
  19743.  
  19744.  
  19745.  Operation
  19746.  
  19747.  CF  1;
  19748.  
  19749.  Description
  19750.  
  19751.  STC sets the carry flag to 1.
  19752.  
  19753.  Flags Affected
  19754.  
  19755.  CF = 1
  19756.  
  19757.  Protected Mode Exceptions
  19758.  
  19759.  None
  19760.  
  19761.  Real Address Mode Exceptions
  19762.  
  19763.  None
  19764.  
  19765.  Virtual 8086 Mode Exceptions
  19766.  
  19767.  None
  19768.  
  19769.  
  19770.  STD ── Set Direction Flag
  19771.  
  19772.  Opcode  Instruction   Clocks    Description
  19773.  
  19774.  FD      STD           2         Set direction flag so (E)SI and/or (E)DI
  19775.                                  decrement
  19776.  
  19777.  
  19778.  Operation
  19779.  
  19780.  DF  1;
  19781.  
  19782.  Description
  19783.  
  19784.  STD sets the direction flag to 1, causing all subsequent string operations
  19785.  to decrement the index registers, (E)SI and/or (E)DI, on which they
  19786.  operate.
  19787.  
  19788.  Flags Affected
  19789.  
  19790.  DF = 1
  19791.  
  19792.  Protected Mode Exceptions
  19793.  
  19794.  None
  19795.  
  19796.  Real Address Mode Exceptions
  19797.  
  19798.  None
  19799.  
  19800.  Virtual 8086 Mode Exceptions
  19801.  
  19802.  None
  19803.  
  19804.  
  19805.  STI ── Set Interrupt Flag
  19806.  
  19807.  Opcode  Instruction   Clocks   Description
  19808.  
  19809.  F13     STI           3        Set interrupt flag; interrupts enabled at the
  19810.                                 end of the next instruction
  19811.  
  19812.  
  19813.  Operation
  19814.  
  19815.  IF  1
  19816.  
  19817.  Description
  19818.  
  19819.  STI sets the interrupt flag to 1. The 80386 then responds to external
  19820.  interrupts after executing the next instruction if the next instruction
  19821.  allows the interrupt flag to remain enabled. If external interrupts are
  19822.  disabled and you code STI, RET (such as at the end of a subroutine),
  19823.  the RET is allowed to execute before external interrupts are recognized.
  19824.  Also, if external interrupts are disabled and you code STI, CLI, then
  19825.  external interrupts are not recognized because the CLI instruction clears
  19826.  the interrupt flag during its execution.
  19827.  
  19828.  Flags Affected
  19829.  
  19830.  IF = 1
  19831.  
  19832.  Protected Mode Exceptions
  19833.  
  19834.  #GP(0) if the current privilege level is greater (has less privilege) than
  19835.  the I/O privilege level
  19836.  
  19837.  Real Address Mode Exceptions
  19838.  
  19839.  None
  19840.  
  19841.  Virtual 8086 Mode Exceptions
  19842.  
  19843.  None
  19844.  
  19845.  
  19846.  STOS/STOSB/STOSW/STOSD ── Store String Data
  19847.  
  19848.  Opcode  Instruction  Clocks   Description
  19849.  
  19850.  AA      STOS m8      4        Store AL in byte ES:[(E)DI], update (E)DI
  19851.  AB      STOS m16     4        Store AX in word ES:[(E)DI], update (E)DI
  19852.  AB      STOS m32     4        Store EAX in dword ES:[(E)DI], update (E)DI
  19853.  AA      STOSB        4        Store AL in byte ES:[(E)DI], update (E)DI
  19854.  AB      STOSW        4        Store AX in word ES:[(E)DI], update (E)DI
  19855.  AB      STOSD        4        Store EAX in dword ES:[(E)DI], update (E)DI
  19856.  
  19857.  
  19858.  Operation
  19859.  
  19860.  IF AddressSize = 16
  19861.  THEN use ES:DI for DestReg
  19862.  ELSE (* AddressSize = 32 *) use ES:EDI for DestReg;
  19863.  FI;
  19864.  IF byte type of instruction
  19865.  THEN
  19866.     (ES:DestReg)  AL;
  19867.     IF DF = 0
  19868.     THEN DestReg  DestReg + 1;
  19869.     ELSE DestReg  DestReg - 1;
  19870.     FI;
  19871.  ELSE IF OperandSize = 16
  19872.     THEN
  19873.        (ES:DestReg)  AX;
  19874.        IF DF = 0
  19875.        THEN DestReg  DestReg + 2;
  19876.        ELSE DestReg  DestReg - 2;
  19877.        FI;
  19878.     ELSE (* OperandSize = 32 *)
  19879.        (ES:DestReg)  EAX;
  19880.        IF DF = 0
  19881.        THEN DestReg  DestReg + 4;
  19882.        ELSE DestReg  DestReg - 4;
  19883.        FI;
  19884.     FI;
  19885.  FI;
  19886.  
  19887.  Description
  19888.  
  19889.  STOS transfers the contents of all AL, AX, or EAX register to the memory
  19890.  byte or word given by the destination register relative to the ES segment.
  19891.  The destination register is DI for an address-size attribute of 16 bits or
  19892.  EDI for an address-size attribute of 32 bits.
  19893.  
  19894.  The destination operand must be addressable from the ES register. A segment
  19895.  override is not possible.
  19896.  
  19897.  The address of the destination is determined by the contents of the
  19898.  destination register, not by the explicit operand of STOS. This operand is
  19899.  used only to validate ES segment addressability and to determine the data
  19900.  type. Load the correct index value into the destination register before
  19901.  executing STOS.
  19902.  
  19903.  After the transfer is made, DI is automatically updated. If the direction
  19904.  flag is 0 (CLD was executed), DI is incremented; if the direction flag is
  19905.  1 (STD was executed), DI is decremented. DI is incremented or decremented by
  19906.  1 if a byte is stored, by 2 if a word is stored, or by 4 if a doubleword is
  19907.  stored.
  19908.  
  19909.  STOSB, STOSW, and STOSD are synonyms for the byte, word, and doubleword STOS
  19910.  instructions, that do not require an operand. They are simpler to use, but
  19911.  provide no type or segment checking.
  19912.  
  19913.  STOS can be preceded by the REP prefix for a block fill of CX or ECX bytes,
  19914.  words, or doublewords. Refer to the REP instruction for further details.
  19915.  
  19916.  Flags Affected
  19917.  
  19918.  None
  19919.  
  19920.  Protected Mode Exceptions
  19921.  
  19922.  #GP(0) if the result is in a nonwritable segment; #GP(0) for an illegal
  19923.  memory operand effective address in the CS, DS, ES, FS, or GS segments;
  19924.  #SS(0) for an illegal address in the SS segment; #PF(fault-code) for a page
  19925.  fault
  19926.  
  19927.  Real Address Mode Exceptions
  19928.  
  19929.  Interrupt 13 if any part of the operand would lie outside of the effective
  19930.  address space from 0 to 0FFFFH
  19931.  
  19932.  Virtual 8086 Mode Exceptions
  19933.  
  19934.  Same exceptions as in Real Address Mode; #PF(fault-code) for a page fault
  19935.  
  19936.  
  19937.  STR ── Store Task Register
  19938.  
  19939.  Opcode        Instruction   Clocks       Description
  19940.  
  19941.  0F  00 /1     STR r/m16     pm=23/27     Load EA word into task register
  19942.  
  19943.  
  19944.  Operation
  19945.  
  19946.  r/m  task register;
  19947.  
  19948.  Description
  19949.  
  19950.  The contents of the task register are copied to the two-byte register or
  19951.  memory location indicated by the effective address operand.
  19952.  
  19953.  STR is used only in operating system software. It is not used in application
  19954.  programs.
  19955.  
  19956.  Flags Affected
  19957.  
  19958.  None
  19959.  
  19960.  Protected Mode Exceptions
  19961.  
  19962.  #GP(0) if the result is in a nonwritable segment; #GP(0) for an illegal
  19963.  memory operand effective address in the CS, DS, ES, FS, or GS segments;
  19964.  #SS(0) for an illegal address in the SS segment; #PF(fault-code) for a page
  19965.  fault
  19966.  
  19967.  Real Address Mode Exceptions
  19968.  
  19969.  Interrupt 6; STR is not recognized in Real Address Mode
  19970.  
  19971.  Virtual 8086 Mode Exceptions
  19972.  
  19973.  Same exceptions as in Real Address Mode
  19974.  
  19975.  Notes
  19976.  
  19977.  The operand-size attribute has no effect on this instruction.
  19978.  
  19979.  
  19980.  SUB ── Integer Subtraction
  19981.  
  19982.  Opcode      Instruction      Clocks   Description
  19983.  
  19984.  2C  ib      SUB AL,imm8      2        Subtract immediate byte from AL
  19985.  2D  iw      SUB AX,imm16     2        Subtract immediate word from AX
  19986.  2D  id      SUB EAX,imm32    2        Subtract immediate dword from EAX
  19987.  80  /5 ib   SUB r/m8,imm8    2/7      Subtract immediate byte from r/m byte
  19988.  81  /5 iw   SUB r/m16,imm16  2/7      Subtract immediate word from r/m word
  19989.  81  /5 id   SUB r/m32,imm32  2/7      Subtract immediate dword from r/m
  19990.                                        dword
  19991.  83  /5 ib   SUB r/m16,imm8   2/7      Subtract sign-extended immediate byte
  19992.                                        from r/m word
  19993.  83  /5 ib   SUB r/m32,imm8   2/7      Subtract sign-extended immediate byte
  19994.                                        from r/m dword
  19995.  28  /r      SUB r/m8,r8      2/6      Subtract byte register from r/m byte
  19996.  29  /r      SUB r/m16,r16    2/6      Subtract word register from r/m word
  19997.  29  /r      SUB r/m32,r32    2/6      Subtract dword register from r/m
  19998.                                        dword
  19999.  2A  /r      SUB r8,r/m8      2/7      Subtract byte register from r/m byte
  20000.  2B  /r      SUB r16,r/m16    2/7      Subtract word register from r/m word
  20001.  2B  /r      SUB r32,r/m32    2/7      Subtract dword register from r/m
  20002.                                        dword
  20003.  
  20004.  
  20005.  Operation
  20006.  
  20007.  IF SRC is a byte and DEST is a word or dword
  20008.  THEN DEST = DEST - SignExtend(SRC);
  20009.  ELSE DEST  DEST - SRC;
  20010.  FI;
  20011.  
  20012.  Description
  20013.  
  20014.  SUB subtracts the second operand (SRC) from the first operand (DEST). The
  20015.  first operand is assigned the result of the subtraction, and the flags are
  20016.  set accordingly.
  20017.  
  20018.  When an immediate byte value is subtracted from a word operand, the
  20019.  immediate value is first sign-extended to the size of the destination
  20020.  operand.
  20021.  
  20022.  Flags Affected
  20023.  
  20024.  OF, SF, ZF, AF, PF, and CF as described in Appendix C
  20025.  
  20026.  Protected Mode Exceptions
  20027.  
  20028.  #GP(0) if the result is in a nonwritable segment; #GP(0) for an illegal
  20029.  memory operand effective address in the CS, DS, ES, FS, or GS segments;
  20030.  #SS(0) for an illegal address in the SS segment; #PF(fault-code) for a page
  20031.  fault
  20032.  
  20033.  Real Address Mode Exceptions
  20034.  
  20035.  Interrupt 13 if any part of the operand would lie outside of the effective
  20036.  address space from 0 to 0FFFFH
  20037.  
  20038.  Virtual 8086 Mode Exceptions
  20039.  
  20040.  Same exceptions as in Real Address Mode; #PF(fault-code) for a page fault
  20041.  
  20042.  
  20043.  TEST ── Logical Compare
  20044.  
  20045.  Opcode       Instruction       Clocks   Description
  20046.  
  20047.  A8   ib      TEST AL,imm8      2        AND immediate byte with AL
  20048.  A9   iw      TEST AX,imm16     2        AND immediate word with AX
  20049.  A9   id      TEST EAX,imm32    2        AND immediate dword with EAX
  20050.  F6   /0 ib   TEST r/m8,imm8    2/5      AND immediate byte with r/m byte
  20051.  F7   /0 iw   TEST r/m16,imm16  2/5      AND immediate word with r/m word
  20052.  F7   /0 id   TEST r/m32,imm32  2/5      AND immediate dword with r/m dword
  20053.  84   /r      TEST r/m8,r8      2/5      AND byte register with r/m byte
  20054.  85   /r      TEST r/m16,r16    2/5      AND word register with r/m word
  20055.  85   /r      TEST r/m32,r32    2/5      AND dword register with r/m dword
  20056.  
  20057.  
  20058.  Operation
  20059.  
  20060.  DEST : = LeftSRC AND RightSRC;
  20061.  CF  0;
  20062.  OF  0;
  20063.  
  20064.  Description
  20065.  
  20066.  TEST computes the bit-wise logical AND of its two operands. Each bit
  20067.  of the result is 1 if both of the corresponding bits of the operands are 1;
  20068.  otherwise, each bit is 0. The result of the operation is discarded and only
  20069.  the flags are modified.
  20070.  
  20071.  Flags Affected
  20072.  
  20073.  OF = 0, CF = 0; SF, ZF, and PF as described in Appendix C
  20074.  
  20075.  Protected Mode Exceptions
  20076.  
  20077.  #GP(0) for an illegal memory operand effective address in the CS, DS,
  20078.  ES, FS, or GS segments; #SS(0) for an illegal address in the SS segment;
  20079.  #PF(fault-code) for a page fault
  20080.  
  20081.  Real Address Mode Exceptions
  20082.  
  20083.  Interrupt 13 if any part of the operand would lie outside of the effective
  20084.  address space from 0 to 0FFFFH
  20085.  
  20086.  Virtual 8086 Mode Exceptions
  20087.  
  20088.  Same exceptions as in Real Address Mode; #PF(fault-code) for a page
  20089.  fault
  20090.  
  20091.  
  20092.  VERR, VERW ── Verify a Segment for Reading or Writing
  20093.  
  20094.  Opcode       Instruction   Clocks      Description
  20095.  
  20096.  0F  00 /4    VERR r/m16    pm=10/11    Set ZF=1 if segment can be read,
  20097.                                         selector in r/m16
  20098.  0F  00 /5    VERW r/m16    pm=15/16    Set ZF=1 if segment can be written,
  20099.                                         selector in r/m16
  20100.  
  20101.  
  20102.  Operation
  20103.  
  20104.  IF segment with selector at (r/m) is accessible
  20105.     with current protection level
  20106.     AND ((segment is readable for VERR) OR
  20107.        (segment is writable for VERW))
  20108.  THEN ZF  0;
  20109.  ELSE ZF  1;
  20110.  FI;
  20111.  
  20112.  Description
  20113.  
  20114.  The two-byte register or memory operand of VERR and VERW contains
  20115.  the value of a selector. VERR and VERW determine whether the
  20116.  segment denoted by the selector is reachable from the current privilege
  20117.  level and whether the segment is readable (VERR) or writable (VERW).
  20118.  If the segment is accessible, the zero flag is set to 1; if the segment is
  20119.  not accessible, the zero flag is set to 0. To set ZF, the following
  20120.  conditions must be met:
  20121.  
  20122.    ■  The selector must denote a descriptor within the bounds of the table
  20123.       (GDT or LDT); the selector must be "defined."
  20124.  
  20125.    ■  The selector must denote the descriptor of a code or data segment
  20126.       (not that of a task state segment, LDT, or a gate).
  20127.  
  20128.    ■  For VERR, the segment must be readable. For VERW, the segment
  20129.       must be a writable data segment.
  20130.  
  20131.    ■  If the code segment is readable and conforming, the descriptor
  20132.       privilege level (DPL) can be any value for VERR. Otherwise, the
  20133.       DPL must be greater than or equal to (have less or the same
  20134.       privilege as) both the current privilege level and the selector's RPL.
  20135.  
  20136.  The validation performed is the same as if the segment were loaded into
  20137.  DS, ES, FS, or GS, and the indicated access (read or write) were
  20138.  performed. The zero flag receives the result of the validation. The
  20139.  selector's value cannot result in a protection exception, enabling the
  20140.  software to anticipate possible segment access problems.
  20141.  
  20142.  Flags Affected
  20143.  
  20144.  ZF as described above
  20145.  
  20146.  Protected Mode Exceptions
  20147.  
  20148.  Faults generated by illegal addressing of the memory operand that
  20149.  contains the selector, the selector is not loaded into any segment
  20150.  register, and no faults attributable to the selector operand are generated
  20151.  
  20152.  #GP(0) for an illegal memory operand effective address in the CS, DS,
  20153.  ES, FS, or GS segments; #SS(0) for an illegal address in the SS segment;
  20154.  #PF(fault-code) for a page fault
  20155.  
  20156.  Real Address Mode Exceptions
  20157.  
  20158.  Interrupt 6; VERR and VERW are not recognized in Real Address Mode
  20159.  
  20160.  Virtual 8086 Mode Exceptions
  20161.  
  20162.  Same exceptions as in Real Address Mode; #PF(fault-code) for a page
  20163.  fault
  20164.  
  20165.  
  20166.  WAIT ── Wait until BUSY# Pin is Inactive (HIGH)
  20167.  
  20168.  Opcode   Instruction   Clocks     Description
  20169.  
  20170.  9B       WAIT          6 min.     Wait until BUSY pin is inactive (HIGH)
  20171.  
  20172.  
  20173.  Description
  20174.  
  20175.  WAIT suspends execution of 80386 instructions until the BUSY# pin is
  20176.  inactive (high). The BUSY# pin is driven by the 80287 numeric processor
  20177.  extension.
  20178.  
  20179.  Flags Affected
  20180.  
  20181.  None
  20182.  
  20183.  Protected Mode Exceptions
  20184.  
  20185.  #NM if the task-switched flag in the machine status word (the lower 16 bits
  20186.  of register CR0) is set; #MF if the ERROR# input pin is asserted (i.e., the
  20187.  80287 has detected an unmasked numeric error)
  20188.  
  20189.  Real Address Mode Exceptions
  20190.  
  20191.  Same exceptions as in Protected Mode
  20192.  
  20193.  Virtual 8086 Mode Exceptions
  20194.  
  20195.  Same exceptions as in Protected Mode
  20196.  
  20197.  
  20198.  XCHG ── Exchange Register/Memory with Register
  20199.  
  20200.  Opcode    Instruction      Clocks     Description
  20201.  
  20202.  90 + r    XCHG AX,r16      3          Exchange word register with AX
  20203.  90 + r    XCHG r16,AX      3          Exchange word register with AX
  20204.  90 + r    XCHG EAX,r32     3          Exchange dword register with EAX
  20205.  90 + r    XCHG r32,EAX     3          Exchange dword register with EAX
  20206.  86  /r    XCHG r/m8,r8     3          Exchange byte register with EA byte
  20207.  86  /r    XCHG r8,r/m8     3/5        Exchange byte register with EA byte
  20208.  87  /r    XCHG r/m16,r16   3          Exchange word register with EA word
  20209.  87  /r    XCHG r16,r/m16   3/5        Exchange word register with EA word
  20210.  87  /r    XCHG r/m32,r32   3          Exchange dword register with EA dword
  20211.  87  /r    XCHG r32,r/m32   3/5        Exchange dword register with EA dword
  20212.  
  20213.  
  20214.  Operation
  20215.  
  20216.  temp  DEST
  20217.  DEST  SRC
  20218.  SRC  temp
  20219.  
  20220.  Description
  20221.  
  20222.  XCHG exchanges two operands. The operands can be in either order. If a
  20223.  memory operand is involved, BUS LOCK is asserted for the duration of the
  20224.  exchange, regardless of the presence or absence of the LOCK prefix or of the
  20225.  value of the IOPL.
  20226.  
  20227.  Flags Affected
  20228.  
  20229.  None
  20230.  
  20231.  Protected Mode Exceptions
  20232.  
  20233.  #GP(0) if either operand is in a nonwritable segment; #GP(0) for an
  20234.  illegal memory operand effective address in the CS, DS, ES, FS, or GS
  20235.  segments; #SS(0) for an illegal address in the SS segment; #PF(fault-code)
  20236.  for a page fault
  20237.  
  20238.  Real Address Mode Exceptions
  20239.  
  20240.  Interrupt 13 if any part of the operand would lie outside of the effective
  20241.  address space from 0 to 0FFFFH
  20242.  
  20243.  Virtual 8086 Mode Exceptions
  20244.  
  20245.  Same exceptions as in Real Address Mode; #PF(fault-code) for a page
  20246.  fault
  20247.  
  20248.  
  20249.  XLAT/XLATB ── Table Look-up Translation
  20250.  
  20251.  D7    XLAT m8    5     Set AL to memory byte DS:[(E)BX + unsigned AL]
  20252.  D7    XLATB      5     Set AL to memory byte DS:[(E)BX + unsigned AL]
  20253.  
  20254.  
  20255.  Operation
  20256.  
  20257.  IF AddressSize = 16
  20258.  THEN
  20259.     AL  (BX + ZeroExtend(AL))
  20260.  ELSE (* AddressSize = 32 *)
  20261.     AL  (EBX + ZeroExtend(AL));
  20262.  FI;
  20263.  
  20264.  Description
  20265.  
  20266.  XLAT changes the AL register from the table index to the table entry. AL
  20267.  should be the unsigned index into a table addressed by DS:BX (for an
  20268.  address-size attribute of 16 bits) or DS:EBX (for an address-size attribute
  20269.  of 32 bits).
  20270.  
  20271.  The operand to XLAT allows for the possibility of a segment override. XLAT
  20272.  uses the contents of BX even if they differ from the offset of the operand.
  20273.  The offset of the operand should have been moved intoBX/EBX with a previous
  20274.  instruction.
  20275.  
  20276.  The no-operand form, XLATB, can be used if the BX/EBX table will always
  20277.  reside in the DS segment.
  20278.  
  20279.  Flags Affected
  20280.  
  20281.  None
  20282.  
  20283.  Protected Mode Exceptions
  20284.  
  20285.  #GP(0) for an illegal memory operand effective address in the CS, DS, ES,
  20286.  FS, or GS segments; #SS(0) for an illegal address in the SS segment;
  20287.  #PF(fault-code) for a page fault
  20288.  
  20289.  Real Address Mode Exceptions
  20290.  
  20291.  Interrupt 13 if any part of the operand would lie outside of the effective
  20292.  address space from 0 to 0FFFFH
  20293.  
  20294.  Virtual 8086 Mode Exceptions
  20295.  
  20296.  Same exceptions as in Real Address Mode; #PF(fault-code) for a page fault
  20297.  
  20298.  
  20299.  XOR ── Logical Exclusive OR
  20300.  
  20301. ╓┌───────────┌────────────────┌────────┌─────────────────────────────────────╖
  20302.  Opcode      Instruction      Clocks   Description
  20303.  
  20304.  34  ib      XOR AL,imm8      2        Exclusive-OR immediate byte to AL
  20305.  35  iw      XOR AX,imm16     2        Exclusive-OR immediate word to AX
  20306.  35  id      XOR EAX,imm32    2        Exclusive-OR immediate dword to EAX
  20307.  80  /6 ib   XOR r/m8,imm8    2/7      Exclusive-OR immediate byte to r/m
  20308.                                        byte
  20309.  Opcode      Instruction      Clocks   Description
  20310.                                       byte
  20311.  81  /6 iw   XOR r/m16,imm16  2/7      Exclusive-OR immediate word to r/m
  20312.                                        word
  20313.  81  /6 id   XOR r/m32,imm32  2/7      Exclusive-OR immediate dword to r/m
  20314.                                        dword
  20315.  83  /6 ib   XOR r/m16,imm8   2/7      XOR sign-extended immediate byte
  20316.                                        with r/m word
  20317.  83  /6 ib   XOR r/m32,imm8   2/7      XOR sign-extended immediate byte
  20318.                                        with r/m dword
  20319.  30  /r      XOR r/m8,r8      2/6      Exclusive-OR byte register to r/m
  20320.                                        byte
  20321.  31  /r      XOR r/m16,r16    2/6      Exclusive-OR word register to r/m
  20322.                                        word
  20323.  31  /r      XOR r/m32,r32    2/6      Exclusive-OR dword register to r/m
  20324.                                        dword
  20325.  32  /r      XOR r8,r/m8      2/7      Exclusive-OR byte register to r/m
  20326.                                        byte
  20327.  33  /r      XOR r16,r/m16    2/7      Exclusive-OR word register to r/m
  20328.                                        word
  20329.  33  /r      XOR r32,r/m32    2/7      Exclusive-OR dword register to r/m
  20330.  Opcode      Instruction      Clocks   Description
  20331. 33  /r      XOR r32,r/m32    2/7      Exclusive-OR dword register to r/m
  20332.                                        dword
  20333.  
  20334.  
  20335.  Operation
  20336.  
  20337.  DEST  LeftSRC XOR RightSRC
  20338.  CF  0
  20339.  OF  0
  20340.  
  20341.  Description
  20342.  
  20343.  XOR computes the exclusive OR of the two operands. Each bit of the result
  20344.  is 1 if the corresponding bits of the operands are different; each bit is 0
  20345.  if the corresponding bits are the same. The answer replaces the first
  20346.  operand.
  20347.  
  20348.  Flags Affected
  20349.  
  20350.  CF = 0, OF = 0; SF, ZF, and PF as described in Appendix C; AF is undefined
  20351.  
  20352.  Protected Mode Exceptions
  20353.  
  20354.  #GP(0) if the result is in a nonwritable segment; #GP(0) for an illegal
  20355.  memory operand effective address in the CS, DS, ES, FS, or GS segments;
  20356.  #SS(0) for an illegal address in the SS segment; #PF(fault-code) for a page
  20357.  fault
  20358.  
  20359.  Real Address Mode Exceptions
  20360.  
  20361.  Interrupt 13 if any part of the operand would lie outside of the effective
  20362.  address space from 0 to 0FFFFH
  20363.  
  20364.  Virtual 8086 Mode Exceptions
  20365.  
  20366.  Same exceptions as in Real Address Mode; #PF(fault-code) for a page
  20367.  fault
  20368.  
  20369.  
  20370.  Appendix A  Opcode Map
  20371.  
  20372.  ────────────────────────────────────────────────────────────────────────────
  20373.  
  20374.  The opcode tables that follow aid in interpreting 80386 object code. Use
  20375.  the high-order four bits of the opcode as an index to a row of the opcode
  20376.  table; use the low-order four bits as an index to a column of the table. If
  20377.  the opcode is 0FH, refer to the two-byte opcode table and use the second
  20378.  byte of the opcode to index the rows and columns of that table.
  20379.  
  20380.  
  20381.  Key to Abbreviations
  20382.  
  20383.  Operands are identified by a two-character code of the form Zz. The first
  20384.  character, an uppercase letter, specifies the addressing method; the second
  20385.  character, a lowercase letter, specifies the type of operand.
  20386.  
  20387.  
  20388.  Codes for Addressing Method
  20389.  
  20390.  A  Direct address; the instruction has no modR/M byte; the address of the
  20391.     operand is encoded in the instruction; no base register, index register,
  20392.     or scaling factor can be applied; e.g., far JMP (EA).
  20393.  
  20394.  C  The reg field of the modR/M byte selects a control register; e.g., MOV
  20395.     (0F20, 0F22).
  20396.  
  20397.  D  The reg field of the modR/M byte selects a debug register; e.g., MOV
  20398.     (0F21,0F23).
  20399.  
  20400.  E  A modR/M byte follows the opcode and specifies the operand. The operand
  20401.     is either a general register or a memory address. If it is a memory
  20402.     address, the address is computed from a segment register and any of the
  20403.     following values: a base register, an index register, a scaling factor,
  20404.     a displacement.
  20405.  
  20406.  F  Flags Register.
  20407.  
  20408.  G  The reg field of the modR/M byte selects a general register; e.g., ADD
  20409.     (00).
  20410.  
  20411.  I  Immediate data. The value of the operand is encoded in subsequent bytes
  20412.     of the instruction.
  20413.  
  20414.  J  The instruction contains a relative offset to be added to the
  20415.     instruction pointer register; e.g., JMP short, LOOP.
  20416.  
  20417.  M  The modR/M byte may refer only to memory; e.g., BOUND, LES, LDS, LSS,
  20418.     LFS, LGS.
  20419.  
  20420.  O  The instruction has no modR/M byte; the offset of the operand is coded as
  20421.     a word or double word (depending on address size attribute) in the
  20422.     instruction. No base register, index register, or scaling factor can be
  20423.     applied; e.g., MOV (A0-A3).
  20424.  
  20425.  R  The mod field of the modR/M byte may refer only to a general register;
  20426.     e.g., MOV (0F20-0F24, 0F26).
  20427.  
  20428.  S  The reg field of the modR/M byte selects a segment register; e.g., MOV
  20429.     (8C,8E).
  20430.  
  20431.  T  The reg field of the modR/M byte selects a test register; e.g., MOV
  20432.     (0F24,0F26).
  20433.  
  20434.  X  Memory addressed by DS:SI; e.g., MOVS, COMPS, OUTS, LODS, SCAS.
  20435.  
  20436.  Y  Memory addressed by ES:DI; e.g., MOVS, CMPS, INS, STOS.
  20437.  
  20438.  
  20439.  Codes for Operant Type
  20440.  
  20441.  a  Two one-word operands in memory or two double-word operands in memory,
  20442.     depending on operand size attribute (used only by BOUND).
  20443.  
  20444.  b  Byte (regardless of operand size attribute)
  20445.  
  20446.  c  Byte or word, depending on operand size attribute.
  20447.  
  20448.  d  Double word (regardless of operand size attribute)
  20449.  
  20450.  p  32-bit or 48-bit pointer, depending on operand size attribute.
  20451.  
  20452.  s  Six-byte pseudo-descriptor
  20453.  
  20454.  v  Word or double word, depending on operand size attribute.
  20455.  
  20456.  w  Word (regardless of operand size attribute)
  20457.  
  20458.  
  20459.  Register Codes
  20460.  
  20461.  When an operand is a specific register encoded in the opcode, the register
  20462.  is identified by its name; e.g., AX, CL, or ESI. The name of the register
  20463.  indicates whether the register is 32-, 16-, or 8-bits wide. A register
  20464.  identifier of the form eXX is used when the width of the register depends on
  20465.  the operand size attribute; for example, eAX indicates that the AX register
  20466.  is used when the operand size attribute is 16 and the EAX register is used
  20467.  when the operand size attribute is 32.
  20468.  
  20469.  
  20470.  One-Byte Opcode Map
  20471.  
  20472.  
  20473.         0         1         2         3         4         5         6        7
  20474.   ╔═══════════════════════════════════════════════════════════╤════════╤══════
  20475.   ║                              ADD                          │  PUSH  │   POP
  20476.  0╟─────────┬─────────┬─────────┬─────────┬─────────┬─────────┤        │
  20477.   ║  Eb,Gb  │  Ev,Gv  │  Gb,Eb  │  Gv,Ev  │  AL,Ib  │ eAX,Iv  │   ES   │   ES
  20478.   ╠═════════╧═════════╧═════════╧═════════╧═════════╧═════════╪════════╪══════
  20479.   ║                              ADC                          │  PUSH  │   POP
  20480.  1╟─────────┬─────────┬─────────┬─────────┬─────────┬─────────┤        │
  20481.   ║  Eb,Gb  │  Ev,Gv  │  Gb,Eb  │  Gv,Ev  │  AL,Ib  │ eAX,Iv  │   SS   │   SS
  20482.   ╠═════════╧═════════╧═════════╧═════════╧═════════╧═════════╪════════╪══════
  20483.   ║                              AND                          │  SEG   │
  20484.  2╟─────────┬─────────┬─────────┬─────────┬─────────┬─────────┤        │   DAA
  20485.   ║  Eb,Gb  │  Ev,Gv  │  Gb,Eb  │  Gv,Ev  │  AL,Ib  │ eAX,Iv  │  =ES   │
  20486.   ╠═════════╧═════════╧═════════╧═════════╧═════════╧═════════╪════════╪══════
  20487.   ║                              XOR                          │  SEG   │
  20488.  3╟─────────┬─────────┬─────────┬─────────┬─────────┬─────────┤        │   AAA
  20489.   ║  Eb,Gb  │  Ev,Gv  │  Gb,Eb  │  Gv,Ev  │  AL,Ib  │ eAX,Iv  │  =SS   │
  20490.   ╠═════════╧═════════╧═════════╧═════════╧═════════╧═════════╧════════╧══════
  20491.   ║                               INC general register
  20492.  4╟─────────┬─────────┬─────────┬─────────┬─────────┬─────────┬────────┬──────
  20493.   ║   eAX   │   eCX   │   eDX   │   eBX   │   eSP   │   eBP   │  eSI   │  eDI
  20494.   ╠═════════╧═════════╧═════════╧═════════╧═════════╧═════════╧════════╧══════
  20495.   ║                               PUSH general register
  20496.  5╟─────────┬─────────┬─────────┬─────────┬─────────┬─────────┬────────┬──────
  20497.   ║   eAX   │   eCX   │   eDX   │   eBX   │   eSP   │   eBP   │  eSI   │  eDI
  20498.   ╠═════════╪═════════╪═════════╪═════════╪═════════╪═════════╪════════╪══════
  20499.   ║         │         │  BOUND  │  ARPL   │   SEG   │   SEG   │ Operand│ Addre
  20500.  6║  PUSHA  │  POPA   │         │         │         │         │        │
  20501.   ║         │         │  Gv,Ma  │  Ew,Rw  │   =FS   │   =GS   │  Size  │  Size
  20502.   ╠═════════╧═════════╧═════════╧═════════╧═════════╧═════════╧════════╧══════
  20503.   ║                    Short displacement jump of condition (Jb)
  20504.  7╟─────────┬─────────┬─────────┬─────────┬─────────┬─────────┬────────┬──────
  20505.   ║   JO    │   JNO   │   JB    │   JNB   │   JZ    │  JNZ    │   JBE  │  JNBE
  20506.   ╠═════════╧═════════╪═════════╪═════════╪═════════╧═════════╪════════╧══════
  20507.   ║  Immediate Grpl   │         │  Grpl   │       TEST        │      XCNG
  20508.  8╟─────────┬─────────┤         │         ├─────────┬─────────┼────────┬──────
  20509.   ║  Eb,Ib  │  Ev,Iv  │         │  Ev,Iv  │  Eb,Gb  │  Ev,Gv  │  Eb,Gb │  Ev,G
  20510.   ╠═════════╪═════════╧═════════╧═════════╧═════════╧═════════╧════════╧══════
  20511.   ║         │              XCHG word or double-word register with eAX
  20512.  9║   NOP   ├─────────┬─────────┬─────────┬─────────┬─────────┬────────┬──────
  20513.   ║         │   eCX   │   eDX   │   eBX   │   eSP   │   eBP   │  eSI   │  eDI
  20514.   ╠═════════╧═════════╧═════════╧═════════╪═════════╪═════════╪════════╪══════
  20515.   ║                  MOV                  │  MOVSB  │ MOVSW/D │ CMPSB  │CMPSW/
  20516.  A╟─────────┬─────────┬─────────┬─────────┤         │         │        │
  20517.   ║  AL,Ob  │  eAX,Ov │  Ob,AL  │  Ov,eAX │  Xb,Yb  │  Xv,Yv  │  Xb,Yb │  Xv,Y
  20518.   ╠═════════╧═════════╧═════════╧═════════╧═════════╧═════════╧════════╧══════
  20519.   ║                       MOV immediate byte into byte register
  20520.  B╟─────────┬─────────┬─────────┬─────────┬─────────┬─────────┬────────┬──────
  20521.   ║    AL   │   CL    │   DL    │   BL    │   AH    │   CH    │   DH   │   BH
  20522.   ╠═════════╧═════════╪═════════╧═════════╪═════════╪═════════╪════════╧══════
  20523.   ║    Shift Grp2     │      RET near     │   LES   │   LDS   │       MOV
  20524.  C╟─────────┬─────────┼─────────┬─────────┤         │         ├────────┬──────
  20525.   ║  Eb,Ib  │  Ev,Iv  │    Iw   │         │  Gv,Mp  │  Gv,Mp  │  Eb,Ib │  Ev,I
  20526.   ╠═════════╧═════════╧═════════╧═════════╪═════════╪═════════╪════════╪══════
  20527.   ║               Shift Grp2              │         │         │        │
  20528.  D╟─────────┬─────────┬─────────┬─────────┤   AAM   │   AAD   │        │  XLAT
  20529.   ║   Eb,1  │  Ev,1   │  Eb,CL  │  Ev,CL  │         │         │        │
  20530.   ╠═════════╪═════════╪═════════╪═════════╪═════════╧═════════╪════════╧══════
  20531.   ║ LOOPNE  │  LOOPE  │   LOOP  │  JCXZ   │        IN         │       OUT
  20532.  E║         │         │         │         ├─────────┬─────────┼────────┬──────
  20533.   ║   Jb    │   Jb    │    Jb   │   Jb    │  AL,Ib  │ eAX,Ib  │  Ib,AL │ Ib,eA
  20534.   ╠═════════╪═════════╪═════════╪═════════╪═════════╪═════════╪════════╧══════
  20535.   ║         │         │         │   REP   │         │         │     Unary Grp3
  20536.  F║  LOCK   │         │  REPNE  │         │   HLT   │   CMC   ├────────┬──────
  20537.   ║         │         │         │  REPE   │         │         │   Eb   │   Ev
  20538.   ╚═════════╧═════════╧═════════╧═════════╧═════════╧═════════╧════════╧══════
  20539.  
  20540.  
  20541.  Two-Byte Opcode Map (first byte is 0FH)
  20542.  
  20543.  
  20544.        0         1         2         3         4         5         6        7
  20545.   ╔═════════╤═════════╤═════════╤═════════╤═════════╤═════════╤════════╤══════
  20546.   ║         │         │   LAR   │   LSL   │         │         │        │
  20547.  0║  Grp6   │  Grp7   │         │         │         │         │  CLTS  │
  20548.   ║         │         │  Gw,Ew  │  Gv,Ew  │         │         │        │
  20549.   ╠═════════╪═════════╪═════════╪═════════╪═════════╪═════════╪════════╪══════
  20550.   ║         │         │         │         │         │         │        │
  20551.  1║         │         │         │         │         │         │        │
  20552.   ║         │         │         │         │         │         │        │
  20553.   ╠═════════╪═════════╪═════════╪═════════╪═════════╪═════════╪════════╪══════
  20554.   ║   MOV   │   MOV   │   MOV   │   MOV   │   MOV   │         │   MOV  │
  20555.  2║         │         │         │         │         │         │        │
  20556.   ║  Cd,Rd  │  Dd,Rd  │  Rd,Cd  │  Rd,Dd  │  Td,Rd  │         │  Rd,Td │
  20557.   ╠═════════╪═════════╪═════════╪═════════╪═════════╪═════════╪════════╪══════
  20558.   ║         │         │         │         │         │         │        │
  20559.  3║         │         │         │         │         │         │        │
  20560.   ║         │         │         │         │         │         │        │
  20561.   ╠═════════╪═════════╪═════════╪═════════╪═════════╪═════════╪════════╪══════
  20562.   ║         │         │         │         │         │         │        │
  20563.  4║         │         │         │         │         │         │        │
  20564.   ║         │         │         │         │         │         │        │
  20565.   ╠═════════╪═════════╪═════════╪═════════╪═════════╪═════════╪════════╪══════
  20566.   ║         │         │         │         │         │         │        │
  20567.  5║         │         │         │         │         │         │        │
  20568.   ║         │         │         │         │         │         │        │
  20569.   ╠═════════╪═════════╪═════════╪═════════╪═════════╪═════════╪════════╪══════
  20570.   ║         │         │         │         │         │         │        │
  20571.  6║         │         │         │         │         │         │        │
  20572.   ║         │         │         │         │         │         │        │
  20573.   ╠═════════╪═════════╪═════════╪═════════╪═════════╪═════════╪════════╪══════
  20574.   ║         │         │         │         │         │         │        │
  20575.  7║         │         │         │         │         │         │        │
  20576.   ║         │         │         │         │         │         │        │
  20577.   ╠═════════╧═════════╧═════════╧═════════╧═════════╧═════════╧════════╧══════
  20578.   ║                      Long-displacement jump on condition (Jv)
  20579.  8╟─────────┬─────────┬─────────┬─────────┬─────────┬─────────┬────────┬──────
  20580.   ║   JO    │   JNO   │   JB    │   JNB   │   JZ    │   JNZ   │   JBE  │  JNBE
  20581.   ╠═════════╧═════════╧═════════╧═════════╧═════════╧═════════╧════════╧══════
  20582.   ║                              Byte Set on condition (Eb)
  20583.  9╟─────────┬─────────┬─────────┬─────────┬─────────┬─────────┬────────┬──────
  20584.   ║  SETO   │  SETNO  │  SETB   │  SETNB  │  SETZ   │  SETNZ  │  SETBE │ SETNB
  20585.   ╠═════════╪═════════╪═════════╪═════════╪═════════╪═════════╪════════╪══════
  20586.   ║  PUSH   │   POP   │         │   BT    │  SHLD   │  SHLD   │        │
  20587.  A║         │         │         │         │         │         │        │
  20588.   ║   FS    │   FS    │         │  Ev,Gv  │ EvGvIb  │ EvGvCL  │        │
  20589.   ╠═════════╪═════════╪═════════╪═════════╪═════════╪═════════╪════════╧══════
  20590.   ║         │         │   LSS   │   BTR   │   LFS   │   LGS   │      MOVZX
  20591.  B║         │         │         │         │         │         ├────────┬──────
  20592.   ║         │         │   Mp    │  Ev,Gv  │   Mp    │   Mp    │ Gv,Eb  │ Gv,Ew
  20593.   ╠═════════╪═════════╪═════════╪═════════╪═════════╪═════════╪════════╪══════
  20594.   ║         │         │         │         │         │         │        │
  20595.  C║         │         │         │         │         │         │        │
  20596.   ║         │         │         │         │         │         │        │
  20597.   ╠═════════╪═════════╪═════════╪═════════╪═════════╪═════════╪════════╪══════
  20598.   ║         │         │         │         │         │         │        │
  20599.  D║         │         │         │         │         │         │        │
  20600.   ║         │         │         │         │         │         │        │
  20601.   ╠═════════╪═════════╪═════════╪═════════╪═════════╪═════════╪════════╪══════
  20602.   ║         │         │         │         │         │         │        │
  20603.  E║         │         │         │         │         │         │        │
  20604.   ║         │         │         │         │         │         │        │
  20605.   ╠═════════╪═════════╪═════════╪═════════╪═════════╪═════════╪════════╪══════
  20606.   ║         │         │         │         │         │         │        │
  20607.  F║         │         │         │         │         │         │        │
  20608.   ║         │         │         │         │         │         │        │
  20609.   ╚═════════╧═════════╧═════════╧═════════╧═════════╧═════════╧════════╧══════
  20610.  
  20611.  
  20612.  Opcodes determined by bits 5,4,3 of modR/M byte:
  20613.  
  20614.       G                       ┌───────┬───────┬───────┐
  20615.       r                       │  mod  │  nnn  │  R/M  │
  20616.       o                       └───────┴───────┴───────┘
  20617.       u
  20618.       p   000     001     010     011     100     101     110     111
  20619.        ┌───────┬───────┬───────┬───────┬───────┬───────┬───────┬───────┐
  20620.       1│  ADD  │  OR   │  ADC  │  SBB  │  AND  │  SUB  │  XOR  │  CMP  │
  20621.        │       │       │       │       │       │       │       │       │
  20622.        ├───────┼───────┼───────┼───────┼───────┼───────┼───────┼───────┤
  20623.       2│  ROL  │  ROR  │  RCL  │  RCR  │  SHL  │  SHR  │       │  SAR  │
  20624.        │       │       │       │       │       │       │       │       │
  20625.        ├───────┼───────┼───────┼───────┼───────┼───────┼───────┼───────┤
  20626.       3│ TEST  │       │  NOT  │  NEG  │  MUL  │ IMUL  │  DIV  │ IDIV  │
  20627.        │ Ib/Iv │       │       │       │AL/eAX │AL/eAX │AL/eAX │AL/eAX │
  20628.        ├───────┼───────┼───────┼───────┼───────┼───────┼───────┼───────┤
  20629.       4│  INC  │  DEC  │       │       │       │       │       │       │
  20630.        │  Eb   │  Eb   │       │       │       │       │       │       │
  20631.        ├───────┼───────┼───────┼───────┼───────┼───────┼───────┼───────┤
  20632.       5│  INC  │  DEC  │ CALL  │ CALL  │  JMP  │  JMP  │ PUSH  │       │
  20633.        │  Ev   │  Ev   │  Ev   │  eP   │  Ev   │  Ep   │  Ev   │       │
  20634.        └───────┴───────┴───────┴───────┴───────┴───────┴───────┴───────┘
  20635.  
  20636.  
  20637.  Opcodes determined by bits 5,4,3 of modR/M byte:
  20638.  
  20639.       G                       ┌───────┬───────┬───────┐
  20640.       r                       │  mod  │  nnn  │  R/M  │
  20641.       o                       └───────┴───────┴───────┘
  20642.       u
  20643.       p   000     001     010     011     100     101     110     111
  20644.        ┌───────┬───────┬───────┬───────┬───────┬───────┬───────┬───────┐
  20645.       6│ SLDT  │  STR  │ LLDT  │  LTR  │ VERR  │ VERW  │       │       │
  20646.        │  Ew   │  Ew   │  Ew   │  Ew   │  Ew   │  Ew   │       │       │
  20647.        ├───────┼───────┼───────┼───────┼───────┼───────┼───────┼───────┤
  20648.       7│ SGDT  │ SIDT  │ LGDT  │ LIDT  │ SMSW  │       │ LMSW  │       │
  20649.        │  Ms   │  Ms   │  Ms   │   Ms  │  Ew   │       │  Ew   │       │
  20650.        ├───────┼───────┼───────┼───────┼───────┼───────┼───────┼───────┤
  20651.       8│       │       │       │       │  BT   │  BTS  │  BTR  │  BTC  │
  20652.        │       │       │       │       │       │       │       │       │
  20653.        └───────┴───────┴───────┴───────┴───────┴───────┴───────┴───────┘
  20654.  
  20655.  
  20656.  Appendix B  Complete Flag Cross-Reference
  20657.  
  20658.  ────────────────────────────────────────────────────────────────────────────
  20659.  
  20660.  Key to Codes
  20661.  
  20662.  T     = instruction tests flag
  20663.  
  20664.  M     = instruction modifies flag
  20665.          (either sets or resets depending on operands)
  20666.  
  20667.  0     = instruction resets flag
  20668.  
  20669.  1     = instruction sets flag
  20670.  
  20671.  ──    = instruction's effect on flag is undefined
  20672.  
  20673.  R     = instruction restores prior value of flag
  20674.  
  20675.  blank = instruction does not affect flag
  20676.  
  20677. ╓┌──────────────────────┌────┌────┌────┌────┌────┌────┌────┌────┌────┌────┌──╖
  20678.  Instruction            OF   SF   ZF   AF   PF   CF   TF   IF   DF   NT   RF
  20679.  
  20680.  AAA                    ──   ──   ──   TM   ──   M
  20681.  AAD                    ──   M    M    ──   M    ──
  20682.  AAM                    ──   M    M    ──   M    ──
  20683.  AAS                    ──   ──   ──   TM   ──   M
  20684.  ADC                    M    M    M    M    M    TM
  20685.  ADD                    M    M    M    M    M    M
  20686.  AND                    0    M    M    ──   M    0
  20687.  Instruction            OF   SF   ZF   AF   PF   CF   TF   IF   DF   NT   RF
  20688. AND                    0    M    M    ──   M    0
  20689.  ARPL                             M
  20690.  BOUND
  20691.  BSF/BSR                ──   ──   M    ──   ──   ──
  20692.  BT/BTS/BTR/BTC         ──   ──   ──   ──   ──   M
  20693.  CALL
  20694.  CBW
  20695.  CLC                                             0
  20696.  CLD                                                            0
  20697.  CLI                                                       0
  20698.  CLTS
  20699.  CMC                                             M
  20700.  CMP                    M    M    M    M    M    M
  20701.  CMPS                   M    M    M    M    M    M              T
  20702.  CWD
  20703.  DAA                    ──   M    M    TM   M    TM
  20704.  DAS                    ──   M    M    TM   M    TM
  20705.  DEC                    M    M    M    M    M
  20706.  DIV                    ──   ──   ──   ──   ──   ──
  20707.  ENTER
  20708.  Instruction            OF   SF   ZF   AF   PF   CF   TF   IF   DF   NT   RF
  20709. ENTER
  20710.  ESC
  20711.  HLT
  20712.  IDIV                   ──   ──   ──   ──   ──   ──
  20713.  IMUL                   M    ──   ──   ──   ──   M
  20714.  IN
  20715.  INC                    M    M    M    M    M
  20716.  INS                                                            T
  20717.  INT                                                  0              0
  20718.  INTO                   T                             0              0
  20719.  IRET                   R    R    R    R    R    R    R    R    R    T
  20720.  Jcond                  T    T    T    T    T
  20721.  JCXZ
  20722.  JMP
  20723.  LAHF
  20724.  LAR                              M
  20725.  LDS/LES/LSS/LFS/LGS
  20726.  LEA
  20727.  LEAVE
  20728.  LGDT/LIDT/LLDT/LMSW
  20729.  Instruction            OF   SF   ZF   AF   PF   CF   TF   IF   DF   NT   RF
  20730. LGDT/LIDT/LLDT/LMSW
  20731.  LOCK
  20732.  LODS                                                           T
  20733.  LOOP
  20734.  LOOPE/LOOPNE                     T
  20735.  LSL                              M
  20736.  LTR
  20737.  MOV
  20738.  MOV control, debug     ──   ──   ──   ──   ──   ──
  20739.  MOVS                                                           T
  20740.  MOVSX/MOVZX
  20741.  MUL                    M    ──   ──   ──   ──   M
  20742.  NEG                    M    M    M    M    M    M
  20743.  NOP
  20744.  NOT
  20745.  OR                     0    M    M    ──   M    0
  20746.  OUT
  20747.  OUTS                                                           T
  20748.  POP/POPA
  20749.  POPF                   R    R    R    R    R    R    R    R    R    R
  20750.  Instruction            OF   SF   ZF   AF   PF   CF   TF   IF   DF   NT   RF
  20751. POPF                   R    R    R    R    R    R    R    R    R    R
  20752.  PUSH/PUSHA/PUSHF
  20753.  RCL/RCR 1              M                        TM
  20754.  RCL/RCR count          ──                       TM
  20755.  REP/REPE/REPNE
  20756.  RET
  20757.  ROL/ROR 1              M                        M
  20758.  ROL/ROR count          ──                       M
  20759.  SAHF                        R    R    R    R    R
  20760.  SAL/SAR/SHL/SHR 1      M    M    M    ──   M    M
  20761.  SAL/SAR/SHL/SHR count  ──   M    M    ──   M    M
  20762.  SBB                    M    M    M    M    M    TM
  20763.  SCAS                   M    M    M    M    M    M              T
  20764.  SET cond               T    T    T         T    T
  20765.  SGDT/SIDT/SLDT/SMSW
  20766.  SHLD/SHRD              ──   M    M    ──   M    M
  20767.  STC                                             1
  20768.  STD                                                            1
  20769.  STI                                                       1
  20770.  STOS                                                           T
  20771.  Instruction            OF   SF   ZF   AF   PF   CF   TF   IF   DF   NT   RF
  20772. STOS                                                           T
  20773.  STR
  20774.  SUB                    M    M    M    M    M    M
  20775.  TEST                   0    M    M    ──   M    0
  20776.  VERR/VERRW                       M
  20777.  WAIT
  20778.  XCHG
  20779.  XLAT
  20780.  XOR                    0    M    M    ──   M    0
  20781.  
  20782.  
  20783.  Appendix C  Status Flag Summary
  20784.  
  20785.  ────────────────────────────────────────────────────────────────────────────
  20786.  
  20787.  Status Flags' Functions
  20788.  
  20789.  Bit  Name   Function
  20790.  
  20791.   0   CF     Carry Flag ── Set on high-order bit carry or borrow; cleared
  20792.              otherwise.
  20793.   2   PF     Parity Flag ── Set if low-order eight bits of result contain
  20794.              an even number of 1 bits; cleared otherwise.
  20795.   4   AF     Adjust flag ── Set on carry from or borrow to the low order
  20796.              four bits of   AL; cleared otherwise. Used for decimal
  20797.              arithmetic.
  20798.   6   ZF     Zero Flag ── Set if result is zero; cleared otherwise.
  20799.   7   SF     Sign Flag ── Set equal to high-order bit of result (0 is
  20800.              positive, 1 if negative).
  20801.  11   OF     Overflow Flag ── Set if result is too large a positive number
  20802.              or too small a negative number (excluding sign-bit) to fit in
  20803.              destination operand; cleared otherwise.
  20804.  
  20805.  Key to Codes
  20806.  
  20807.  T     = instruction tests flag
  20808.  M     = instruction modifies flag
  20809.          (either sets or resets depending on operands)
  20810.  0     = instruction resets flag
  20811.  ──    = instruction's effect on flag is undefined
  20812.  blank = instruction does not affect flag
  20813.  
  20814.  
  20815. ╓┌──────────────────────┌────┌────┌────┌────┌────┌───────────────────────────╖
  20816.  Instruction            OF   SF   ZF   AF   PF   CF
  20817.  AAA                    ──   ──   ──   TM   ──   M
  20818.  AAS                    ──   ──   ──   TM   ──   M
  20819.  AAD                    ──   M    M    ──   M    ──
  20820.  AAM                    ──   M    M    ──   M    ──
  20821.  DAA                    ──   M    M    TM   M    TM
  20822.  DAS                    ──   M    M    TM   M    TM
  20823.  ADC                    M    M    M    M    M    TM
  20824.  ADD                    M    M    M    M    M    M
  20825.  SBB                    M    M    M    M    M    TM
  20826.  SUB                    M    M    M    M    M    M
  20827.  CMP                    M    M    M    M    M    M
  20828.  CMPS                   M    M    M    M    M    M
  20829.  SCAS                   M    M    M    M    M    M
  20830.  NEG                    M    M    M    M    M    M
  20831.  DEC                    M    M    M    M    M
  20832.  INC                    M    M    M    M    M
  20833.  IMUL                   M    ──   ──   ──   ──   M
  20834.  Instruction            OF   SF   ZF   AF   PF   CF
  20835. IMUL                   M    ──   ──   ──   ──   M
  20836.  MUL                    M    ──   ──   ──   ──   M
  20837.  RCL/RCR 1              M                        TM
  20838.  RCL/RCR count          ──                       TM
  20839.  ROL/ROR 1              M                        M
  20840.  ROL/ROR count          ──                       M
  20841.  SAL/SAR/SHL/SHR 1      M    M    M    ──   M    M
  20842.  SAL/SAR/SHL/SHR count  ──   M    M    ──   M    M
  20843.  SHLD/SHRD              ──   M    M    ──   M    M
  20844.  BSF/BSR                ──   ──   M    ──   ──   ──
  20845.  BT/BTS/BTR/BTC         ──   ──   ──   ──   ──   M
  20846.  AND                    0    M    M    ──   M    0
  20847.  OR                     0    M    M    ──   M    0
  20848.  TEST                   0    M    M    ──   M    0
  20849.  XOR                    0    M    M    ──   M    0
  20850.  
  20851.  
  20852.  Appendix D  Condition Codes
  20853.  
  20854.  ───────────────────────────────────────────────────────────────────────────
  20855.  
  20856.  ───────────────────────────────────────────────────────────────────────────
  20857.  Note:
  20858.    The terms "above" and "below" refer to the relation between two
  20859.    unsigned values (neither SF nor OF is tested). The terms "greater" and
  20860.    "less" refer to the relation between two signed values (SF and OF are
  20861.    tested).
  20862.  ───────────────────────────────────────────────────────────────────────────
  20863.  
  20864.  Definition of Conditions
  20865.  
  20866.  (For conditional instructions Jcond, and SETcond)
  20867.  
  20868. ╓┌─────────┌────────────────────────────┌────────────┌───────────────────────╖
  20869.                                         Instruction  Condition
  20870.  Mnemonic  Meaning                      Subcode      Tested
  20871.  
  20872.  O         Overflow                     0000         OF = 1
  20873.  
  20874.  NO        No overflow                  0001         OF = 0
  20875.  
  20876.                                         Instruction  Condition
  20877.  Mnemonic  Meaning                      Subcode      Tested
  20878. 
  20879.  B         Below
  20880.  NAE       Neither above nor equal      0010         CF = 1
  20881.  
  20882.  NB        Not below
  20883.  AE       Above or equal                0011         CF = 0
  20884.  
  20885.  E         Equal
  20886.  Z         Zero                         0100         ZF = 1
  20887.  
  20888.  NE        Not equal
  20889.  NZ        Not zero                     0101         ZF = 0
  20890.  
  20891.  BE        Below or equal
  20892.  NA        Not above                    0110         (CF or ZF) = 1
  20893.  
  20894.  NBE       Neither below nor equal
  20895.  NA        Above                        0111         (CF or ZF) = 0
  20896.  
  20897.                                         Instruction  Condition
  20898.  Mnemonic  Meaning                      Subcode      Tested
  20899. 
  20900.  S         Sign                         1000         SF = 1
  20901.  
  20902.  NS        No sign                      1001         SF = 0
  20903.  
  20904.  P         Parity
  20905.  PE        Parity even                  1010         PF = 1
  20906.  
  20907.  NP        No parity
  20908.  PO        Parity odd                   1011         PF = 0
  20909.  
  20910.  L         Less
  20911.  NGE       Neither greater nor equal    1100         (SF xor OF) = 1
  20912.  
  20913.  NL        Not less
  20914.  GE        Greater or equal             1101         (SF xor OF) = 0
  20915.  
  20916.  LE        Less or equal
  20917.  NG        Not greater                  1110         ((SF xor OF) or ZF) = 1
  20918.                                         Instruction  Condition
  20919.  Mnemonic  Meaning                      Subcode      Tested
  20920. NG        Not greater                  1110         ((SF xor OF) or ZF) = 1
  20921.  
  20922.  NLE       Neither less nor equal
  20923.  G         Greater                      1111         ((SF xor OF) or ZF) = 0
  20924.  
  20925.