home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft_Programmers_Library.7z / MPL / intel / 80286.txt next >
Encoding:
Text File  |  2013-11-08  |  700.8 KB  |  15,600 lines

  1.  INTEL 80286 PROGRAMMER'S REFERENCE MANUAL 1987
  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, Insite,
  18.  Intel, intel, intelBOS, Intelevision, inteligent Identifier, inteligent
  19.  Programming, Intellec, Intellink, iOSP, iPDS, iPSC, iRMX, iSBC, iSBX, iSDM,
  20.  iSXM, KEPROM, Library Manager, MAP-NET, MCS, Megachassis, MICROMAINFRAME,
  21.  MULTIBUS, MULTICHANNEL, MULTIMODULE, MultiSERVER, ONCE, OpenNET, OTP,
  22.  PC-BUBBLE, Plug-A-Bubble, PROMPT, Promware, QUEST, QueX, Quick-Pulse
  23.  Programming, Ripplemode, RMX/80, RUPI, Seamless, SLD, UPI, and VLSiCEL, and
  24.  the combination of ICE, iCS, iRMX, iSBC, iSBX, MCS, or UPI and a numerical
  25.  suffix, 4-SITE.
  26.  
  27.  MDS is an ordering code only and is not used as a product name or
  28.  trademark. MDS(R) is a registered trademark of Mohawk Data Sciences
  29.  Corporation.
  30.  
  31.  *MULTIBUS is a patented Intel bus.
  32.  
  33.  Additional copies of this manual or other Intel literature may be obtained
  34.  from:
  35.  
  36.  Intel Corporation
  37.  Literature Distribution
  38.  Mail Stop SC6-59
  39.  3065 Bowers Avenue
  40.  Santa Clara, CA 95051
  41.  
  42.  (c)INTEL CORPORATION 1987   CG-10/86
  43.  
  44.  
  45.  Preface
  46.  
  47.  ─────────────────────────────────────────────────────────────────────────────
  48.  
  49.  
  50.  This manual describes the 80286, the most powerful 16-bit microprocessor in
  51.  the 8086 family, and the 80287 Numeric Processor Extension (NPX).
  52.  
  53.  Organization of This Manual
  54.  
  55.  80286
  56.  
  57.  The 80286 contains a table of contents, eleven chapters, four appendices,
  58.  and an index. For more information on the 80286 book's organization, see its
  59.  first chapter, Chapter 1, "Introduction to the 80286." Section 1.4 in that
  60.  chapter explains the organization in detail.
  61.  
  62.  Notational Conventions
  63.  This manual uses special notation to represent sub- and superscript
  64.  characters. Subscript characters are surrounded by {curly brackets}, for
  65.  example 10{2} = 10 base 2. Superscript characters are preceeded by a caret
  66.  and enclosed within (parentheses), for example 10^(3) = 10 to the third
  67.  power.
  68.  
  69.  Table of Contents
  70.  
  71.  ─────────────────────────────────────────────────────────────────────────────
  72.  
  73.  
  74.  Chapter 1  Introduction to the 80286
  75.  
  76.  1.1  General Attributes
  77.  1.2  Modes of Operation
  78.  1.3  Advanced Features
  79.        1.3.1  Memory Management
  80.        1.3.2  Task Management
  81.        1.3.3  Protection Mechanisms
  82.        1.3.4  Support for Operating Systems
  83.  
  84.  1.4  Organization of This Book
  85.  1.5  Related Publications
  86.  
  87.  Chapter 2  80286 Base Architecture
  88.  
  89.  2.1  Memory Organization and Segmentation
  90.  2.2  Data Types
  91.  2.3  Registers
  92.        2.3.1  General Registers
  93.        2.3.2  Memory Segmentation and Segment Registers
  94.        2.3.3  Index, Pointer, and Base Registers
  95.        2.3.4  Status and Control Registers
  96.  
  97.  2.4  Addressing Modes
  98.        2.4.1  Operands
  99.        2.4.2  Register and Immediate Modes
  100.        2.4.3  Memory Addressing Modes
  101.                2.4.3.1  Segment Selection
  102.                2.4.3.2  Offset Computation
  103.                2.4.3.3  Memory Mode
  104.  
  105.  2.5  Input/Output
  106.        2.5.1  I/O Address Space
  107.        2.5.2  Memory-Mapped I\0
  108.  
  109.  2.6  Interrupts and Exceptions
  110.  2.7  Hierarchy of Instruction Sets
  111.  
  112.  Chapter 3  Basic Instruction Set
  113.  
  114.  3.1  Data Movement Instructions
  115.        3.1.1  General-Purpose Data Movement Instructions
  116.        3.1.2  Stack Manipulation Instructions
  117.  
  118.  3.2  Flag Operation with the Basic Instruction Set
  119.        3.2.1  Status Flags  4
  120.        3.2.2  Control Flags 4
  121.  
  122.  3.3  Arithmetic Instructions
  123.        3.3.1  Addition Instructions
  124.        3.3.2  Subtraction Instructions
  125.        3.3.3  Muitiplication Instructions
  126.        3.3.4  Division Instructions
  127.  
  128.  3.4  Logical Instructions
  129.        3.4.1  Boolean Operation Instructions
  130.        3.4.2  Shift and Rotate Instructions
  131.                3.4.2.1  Shift Instructions
  132.                3.4.2.2  Rotate Instructions
  133.  
  134.        3.4.3  Type Conversion and No-Operation Instructions
  135.  
  136.  3.5  Test and Compare Instructions
  137.  3.6  Control Transfer Instructions
  138.        3.6.1  Unconditional Transfer Instructions
  139.                3.6.1.1  Jump instruction
  140.                3.6.1.2  Call Instruction
  141.                3.6.1.3  Return and Return from interrupt Instruction
  142.  
  143.        3.6.2  Conditional Transfer Instructions
  144.                3.6.2.1  Conditional Jump Instructions
  145.                3.6.2.2  Loop Instructions
  146.                3.6.2.3  Executing a Loop or Repeat Zero Times
  147.  
  148.        3.6.3  Software-Generated Interrupts
  149.                3.6.3.1  Software Interrupt Instruction
  150.  
  151.  3.7  Character Translation and String Instructions
  152.        3.7.1  Translate Instruction
  153.        3.7.2  String Manipulation Instructions and Repeat Prefixes
  154.                3.7.2.1  String Movement Instructions
  155.                3.7.2.2  Other String Operations
  156.  
  157.  3.8  Address Manipulation Instructions
  158.  3.9  Flag Control instructions
  159.        3.9.1  Carry Flag Control Instructions
  160.        3.9.2  Direction Flag Control Instructions
  161.        3.9.3  Flag Transfer Instructions
  162.  
  163.  3.10  Binary-Coded Decimal Arithmetic Instructions
  164.         3.10.1  Packed BCD Adjustment Instructions
  165.         3.10.2  Unpacked BCD Adjustment Instructions
  166.  
  167.  3.11  Trusted Instructions
  168.         3.11.1  Trusted and Privileged Restrictions on POPF and IRET
  169.         3.11.2  Machine State Instructions
  170.         3.11.3  Inputand Output Instructions
  171.  
  172.  3.12  Processor Extension Instructions
  173.         3.12.1  Processor Extension Synchronization Instructions
  174.         3.12.2  Numeric Data Processor Instructions
  175.                  3.12.2.1  Arithmetic Instructions
  176.                  3.12.2.2  Comparison Instructions
  177.                  3.12.2.3  Transcendental Instructions
  178.                  3.12.2.4  Data Transfer Instructions
  179.                  3.12.2.5  Constant Instructions
  180.  
  181.  Chapter 4  Extended Instruction Set
  182.  
  183.  4.1  Block I\O Instructions
  184.  4.2  High-Level Instructions
  185.  
  186.  Chapter 5  Real Address Mode
  187.  
  188.  5.1  Addressing and Segmentation
  189.  5.2  Interrupt Handling
  190.        5.2.1  Interrupt Vector Table
  191.                 5.2.1.1  Interrupt Procedures
  192.        5.2.2  Interrupt Priorities
  193.        5.2.3  Reserved and Dedicated Interrupt Vectors
  194.  
  195.  5.3  System Initialization.
  196.  
  197.  Chapter 6  Memory Management and Virtual Addressing
  198.  
  199.  6.1  Memory Management Overview
  200.  6.2  Virtual Addresses
  201.  6.3  Descriptor Tables
  202.  6.4  Virtual-to-Physical Address Translation
  203.  6.5  Segments and Segment Descriptors
  204.  6.6  Memory Management Registers
  205.        6.6.1  Segment Address Translation Registers
  206.        6.6.2  System Address Registers
  207.  
  208.  Chapter 7  Protection
  209.  
  210.  7.1  Introduction
  211.        7.1.1  Types of Protection
  212.        7.1.2  Protection Implementation
  213.  
  214.  7.2  Memory Management and Protection
  215.        7.2.1  Separation of Address Spaces
  216.        7.2.2  LDT and GDT Access Checks
  217.        7.2.3  Type Validation
  218.  
  219.  7.3  Privilege Levels and Protection
  220.        7.3.1  Example of Using Four Privilege Levels
  221.        7.3.2  Privilege Usage
  222.  
  223.  7.4  Segment Descriptor
  224.        7.4.1  Data Accesses
  225.        7.4.2  Code Segment Access
  226.        7.4.3  Data Access Restriction by Privilege Level
  227.        7.4.4  Pointer Privilege Stamping via ARPL
  228.  
  229.  7.5  Control Transfers
  230.        7.5.1  Gates
  231.                7.5.1.1  Call Gates
  232.                7.5.1.2  Intra-Level Transfers via Call Gate
  233.                7.5.1.3  Inter-Level Control Transfer via Call Gates
  234.                7.5.1.4  Stack Changes Caused by Call Gates
  235.  
  236.        7.5.2  Inter-Level Returns
  237.  
  238.  Chapter 8  Tasks and State Transitions
  239.  
  240.  8.1  Introduction
  241.  8.2  Task State Segments and Descriptors
  242.        8.2.1  Task State Segment Descriptors
  243.  
  244.  8.3  Task Switching
  245.  8.4  Task Linking
  246.  8.5  Task Gates
  247.  
  248.  Chapter 9  Interrupts and Exceptions
  249.  
  250.  9.1  Interrupt Descriptor Table
  251.  9.2  Hardware Initiated Interrupts
  252.  9.3  Software Initiated Interrupts
  253.  9.4  Interrupt Gates and Trap Gates
  254.  9.5  Task Gates and Interrupt Tasks
  255.        9.5.1  Scheduling Considerations
  256.        9.5.2  Deciding Between Task, Trap, and Interrupt Gates
  257.  
  258.  9.6  Protection Exceptions and Reserved Vectors
  259.        9.6.1  Invalid OP-Code (Interrupt 6)
  260.        9.6.2  Double Fault (Interrupt 8)
  261.        9.6.3  Processor Extension Segment Overrun (Interrupt 9)
  262.        9.6.4  Invalid Task State Segment (Interrupt 10)
  263.        9.6.5  Not Present (Interrupt 11)
  264.        9.6.6  Stack Fault (Interrupt 12)
  265.        9.6.7  General Protection Fault (Interrupt 13)
  266.  
  267.  9.7  Additional Exceptions and Interrupts
  268.        9.7.1  Single Step Interrupt (Interrupt 1)
  269.  
  270.  Chapter 10  System Control and Initialization
  271.  
  272.  10.1  System Flags and Registers
  273.         10.1.1  Descriptor Table Registers
  274.  
  275.  10.2  System Control Instructions
  276.         10.2.1  Machine Status Word
  277.         10.2.2  Other Instructions
  278.  
  279.  10.3  Privileged and Trusted Instructions
  280.  
  281.  10.4  Initialization
  282.         10.4.1  Real Address Mode
  283.         10.4.2  Protected Mode
  284.  
  285.  Chapter 11  Advanced Topics
  286.  
  287.  11.1  Virtual Memory Management
  288.  11.2  Special Segment Attributes
  289.         11.2.1  Conforming Code Segments
  290.         11.2.2  Expand-Down Data Segments
  291.  
  292.  11.3  Pointer Validation
  293.         11.3.1  Descriptor Validation
  294.         11.3.2  Pointer Integrity: RPL and the"Trojan Horse Problem"
  295.  
  296.  11.4  NPX Context Switching
  297.  11.5  Multiprocessor Considerations
  298.  11.6  Shutdown
  299.  
  300.  Appendix A  80286 System Initialization
  301.  
  302.  Appendix B  The 80286 Instruction Set
  303.  
  304.  Appendix C  8086/8088 Compatibility Considerations
  305.  
  306.  Appendix D  80286/80386 Software Compatibility Considerations
  307.  
  308.  Index
  309.  
  310.  Figures
  311.  
  312.  1-1     Four Privilege Levels
  313.  
  314.  2-1     Segmented Virtual Memory
  315.  2-2     Bytes and Words in Memory.
  316.  2-3     80286/80287 Supported Data Types
  317.  2-4     80286 Base Architecture Register Set
  318.  2-5     Real Address Mode Segment Selector Interpretation
  319.  2-6     Protected Mode Segment Selector Interpretation
  320.  2-7     80286 Stack
  321.  2-8     Stack Operation
  322.  2-9     BP Usage as a Stack Frame Base Pointer
  323.  2-10    Flags Register.
  324.  2-11    Two-Component Address
  325.  2-12    Use of Memory Segmentation
  326.  2-13    Complex Addressing Modes
  327.  2-14    Memory-Mapped I/O
  328.  2-15    Hierarchy of Instructions
  329.  
  330.  3-1     PUSH
  331.  3-2     PUSHA
  332.  3-3     POP
  333.  3-4     POPA.
  334.  3-5     Flag Word Contents
  335.  3-6     SAL and SHL
  336.  3-7     SHR
  337.  3-8     SAR
  338.  3-9     ROL
  339.  3-10    ROR
  340.  3-11    RCL
  341.  3-12    RCR
  342.  3-13    LAHF and SAHF
  343.  3-14    PUSHF and POPF
  344.  
  345.  4-1     Formal Definition of the ENTER Instruction
  346.  4-2     Variable Access in Nested Procedures
  347.  4-2a    Stack Frame for MAIN at Level 1
  348.  4-2b    Stack Frame for Procedure A
  349.  4-2c    Stack Frame for Procedure B at Level 3 Called from A
  350.  4-2d    Stack Frame for Procedure C at Level 3 Called from B
  351.  
  352.  5-1a    Forming the Segment Base Address
  353.  5-1b    Forming the 20-Bit Physical Address in the Real Address Mode
  354.  5-2     Overlapping Segments to Save Physical Memory
  355.  5-3     Interrupt Vector Table for Real Address Mode
  356.  5-4     Stack Structure after Interrupt (Real Address Mode)
  357.  
  358.  6-1     Format of the Segment Selector Component
  359.  6-2     Address Spaces and Task Isolation
  360.  6-3     Segment Descriptor (S=1)
  361.  6-4     Special Purpose Descriptors or System Segment Descriptors (S=O)
  362.  6-5     LDT Descriptor
  363.  6-6     Virtual-to-Physical Address Translation
  364.  6-7     Segment Descriptor Access Bytes
  365.  6-8     Memory Management Registers
  366.  6-9     Descriptor Loading
  367.  
  368.  7-1     Addressing Segments of a Module within a Task
  369.  7-2     Descriptor Cache Registers
  370.  7-3     80286 Virtual Address Space
  371.  7-4     Local and Global Descriptor Table Definitions
  372.  7-5     Error Code Format (on the stack)
  373.  7-6     Code and Data Segments Assigned to a Privilege Level.
  374.  7-7     Selector Fields
  375.  7-8     Access Byte Examples.
  376.  7-9     Pointer Privilege Stamping
  377.  7-10    Gate Descriptor Format.
  378.  7-11    Call Gate
  379.  7-12    Stack Contents after an Inter-Level Call
  380.  
  381.  8-1     Task State Segment and TSS Registers
  382.  8-2     TSS Descriptor
  383.  8-3     Task Gate Descriptor
  384.  8-4     Task Switch Through a Task Gate
  385.  
  386.  9-1     Interrupt Descriptor Table Definition
  387.  9-2     IDT Selector Error Code.
  388.  9-3     Trap/Interrupt Gate Descriptors
  389.  9-4     Stack Layout after an Exception with an Error Code
  390.  
  391.  10-1    Local and Global Descriptor Table Definition
  392.  10-2    Interrupt Descriptor Table Definition
  393.  10-3    Data Type for Global Descriptor Table and Interrupt Descriptor Table
  394.  
  395.  11-1    Expand-Down Segment
  396.  11-2    Dynamic Segment Relocation and Expansion of Segment Limit
  397.  11-3    Example of NPX Context Switching
  398.  
  399.  B-1     /n Instruction Byte Format
  400.  B-2     /r Instruction Byte Format
  401.  
  402.  Tables
  403.  
  404.  2-1     Implied Segment Usage by Index, Pointer, and Base Registers
  405.  2-2     Segment Register Selection Rules
  406.  2-3     Memory Operand Addressing Modes
  407.  2-4     80286 Interrupt Vector Assignments (Real Address Mode)
  408.  
  409.  3-1     Status Flags' Functions
  410.  3-2     Control Flags' Functions
  411.  3-3     Interpretation of Conditional Transfers
  412.  
  413.  5-1     Interrupt Processing Order
  414.  5-2     Dedicated and Reserved Interrupt Vectors in Real Address Mode
  415.  5-3     Processor State after RESET
  416.  
  417.  7-1     Segment Access Rights Byte Format
  418.  7-2     Allowed Segment Types in Segment Registers
  419.  7-3     Call Gate Checks
  420.  7-4     Inter-Level Return Checks
  421.  
  422.  8-1     Checks Made during a Task Switch
  423.  8-2     Effect of a Task Switch on BUSY and NT Bits and the Link Word
  424.  
  425.  9-1     Trap and Interrupt Gate Checks
  426.  9-2     Interrupt and Gate Interactions
  427.  9-3     Reserved Exceptions and Interrupts
  428.  9-4     Interrupt Processing Order
  429.  9-5     Conditions That Invalidate the TSS
  430.  
  431.  10-1    MSW Bit Functions
  432.  10-2    Recommended MSW Encodings for Processor Extension Control
  433.  
  434.  11-1    NPX Context Switching
  435.  
  436.  B-1     ModRM Values
  437.  B-2     Protection Exceptions of the 80286
  438.  B-3     Hexadecimal Values for the Access Rights Byte
  439.  
  440.  C-1     New 80286 Interrupts
  441.  
  442.  
  443.  
  444.  Chapter 1  Introduction to the 80286
  445.  
  446.  ───────────────────────────────────────────────────────────────────────────
  447.  
  448.  The 80286 is the most powerful 16-bit processor in the 8086 series of
  449.  microprocessors, which includes the 8086, the 8088, the 80186, the 80188,
  450.  and the 80286. It is designed for applications that require very high
  451.  performance. It is also an excellent choice for sophisticated "high end"
  452.  applications that will benefit from its advanced architectural features:
  453.  memory management, protection mechanisms, task management, and virtual
  454.  memory support. The 80286 provides, on a single VLSI chip, computational
  455.  and architectural characteristics normally associated with much larger
  456.  minicomputers.
  457.  
  458.  Sections 1.1, 1.2, and 1.3 of this chapter provide an overview of the 8028
  459.  architecture. Because the 80286 represents an extension of the 8086
  460.  architecture, some of this overview material may be new and unfamiliar to
  461.  previous users of the 8086 and similar microprocessors. But the 80286 is
  462.  also an evolutionary development, with the new architecture superimposed
  463.  upon the industry standard 8086 in such a way as to affect only the design
  464.  and programming of operating systems and other such system software.
  465.  Section 1.4 of this chapter provides a guide to the organization of this
  466.  manual, suggesting which chapters are relevant to the needs of particular
  467.  readers.
  468.  
  469.  
  470.  1.1  General Attributes
  471.  
  472.  The 80286 base architecture has many features in common with the
  473.  architecture of other members of the 8086 family, such as byte addressable
  474.  memory, I/O interfacing hardware, interrupt vectoring, and support for both
  475.  multiprocessing and processor extensions. The entire family has a common
  476.  set of addressing modes and basic instructions. The 80286 base architecture
  477.  also includes a number of extensions which add to the versatility of the
  478.  computer.
  479.  
  480.  The 80286 processor can function in two modes of operation (see section 1.2
  481.  of this chapter, Modes of Operation). In one of these modes only the base
  482.  architecture is available to programmers, whereas in the other mode a number
  483.  of very powerful advanced features have been added, including support for
  484.  virtual memory, multitasking, and a sophisticated protection mechanism.
  485.  These advanced features are described in section 1.3 of this chapter.
  486.  
  487.  The 80286 base architecture was designed to support programming in
  488.  high-level languages, such as Pascal, C or PL/M. The register set and
  489.  instructions are well suited to compiler-generated code. The addressing
  490.  modes (see section 2.4.3 in Chapter 2) allow efficient addressing
  491.  of complex data structures, such as static and dynamic arrays, records,
  492.  and arrays within records, which are commonly supported by high-level
  493.  languages. The data types supported by the architecture include, along with
  494.  bytes and words, high level language constructs such as strings, BCD, and
  495.  floating point.
  496.  
  497.  The memory architecture of the 80286 was designed to support modular
  498.  programming techniques. Memory is divided into segments, which may be of
  499.  arbitrary size, that can be used to contain procedures and data structures.
  500.  Segmentation has several advantages over more conventional linear memory
  501.  architectures. It supports structured software, since segments can contain
  502.  meaningful program units and data, and more compact code, since references
  503.  within a segment can be shorter (and locality of reference usually insures
  504.  that the next few references will be within the same segment). Segmentation
  505.  also lends itself to efficient implementation of sophisticated memory
  506.  management, virtual memory, and memory protection.
  507.  
  508.  In addition, new instructions have been added to the base architecture to
  509.  give hardware support for procedure invocations, parameter passing, and
  510.  array bounds checking.
  511.  
  512.  
  513.  1.2  Modes of Operation
  514.  
  515.  The 80286 can be operated in either of two different modes: Real Address
  516.  Mode or Protected Virtual Address Mode (also referred to as Protected Mode).
  517.  In either mode of operation, the 80286 represents an upwardly compatible
  518.  addition to the 8086 family of processors.
  519.  
  520.  In Real Address Mode, the 80286 operates essentially as a very
  521.  high-performance 8086. Programs written for the 8086 or the 80186 can be
  522.  executed in this mode without any modification (the few exceptions are
  523.  described in Appendix C, "Compatibility Considerations"). Such upward
  524.  compatibility extends even to the object code level; for example, an 8086
  525.  program stored in read-only memory will execute successfully in 80286 Real
  526.  Address Mode. An 80286 operating in Real Address Mode provides a number of
  527.  instructions not found on the 8086. These additional instructions, also
  528.  present with the 80186, allow for efficient subroutine linkage, parameter
  529.  validation, index calculations, and block I/O transfers.
  530.  
  531.  The advanced architectural features and full capabilities of the 80286 are
  532.  realized in its native Protected Mode. Among these features are
  533.  sophisticated mechanisms to support data protection, system integrity, task
  534.  concurrency, and memory management, including virtual storage.
  535.  Nevertheless, even in Protected Mode, the 80286 remains upwardly compatible
  536.  with most 8086 and 80186 application programs. Most 8086 applications
  537.  programs can be re-compiled or re-assembled and executed on the 80286 in
  538.  Protected Mode.
  539.  
  540.  
  541.  1.3  Advanced Features
  542.  
  543.  The architectural features described in section 1.1 of this chapter
  544.  are common to both operating modes of the processor. In addition to these
  545.  common features, Protected Mode provides a number of advanced features,
  546.  including a greatly extended physical and logical address space, new
  547.  instructions, and support for additional hardware-recognized data
  548.  structures. The Protected Mode 80286 includes a sophisticated memory
  549.  management and multilevel protection mechanism. Full hardware support is
  550.  included for multitasking and task switching operations.
  551.  
  552.  
  553.  1.3.1  Memory Management
  554.  
  555.  The memory architecture of the Protected Mode 80286 represents a
  556.  significant advance over that of the 8086. The physical address space has
  557.  been increased from 1 megabyte to 16 megabytes (2^(24) bytes), while the
  558.  virtual address space (i.e., the address space visible to a program) has
  559.  been increased from 1 megabyte to 1 gigabyte (2^(30) bytes). Moreover,
  560.  separate virtual address spaces are provided for each task in a
  561.  multi-tasking system (see the next section, 1.3.2, "Task Management").
  562.  
  563.  The 80286 supports on-chip memory management instead of relying on an
  564.  external memory management unit. The one-chip solution is preferable because
  565.  no software is required to manage an external memory management unit,
  566.  performance is much better, and hardware designs are significantly simpler.
  567.  
  568.  Mechanisms have been included in the 80286 architecture to allow the
  569.  efficient implementation of virtual memory systems. (In virtual memory
  570.  systems, the user regards the combination of main and external storage as a
  571.  single large memory. The user can write large programs without worrying
  572.  about the physical memory limitations of the system. To accomplish this, the
  573.  operating system places some of the user programs and data in external
  574.  storage and brings them into main memory only as they are needed.) All
  575.  instructions that can cause a segment-not-present fault are fully
  576.  restartable. Thus, a not-present segment can be loaded from external
  577.  storage, and the task can be restarted at the point where the fault
  578.  occurred.
  579.  
  580.  The 80286, like all members of the 8086 series, supports a segmented memory
  581.  architecture. The 80286 also fully integrates memory segmentation into a
  582.  comprehensive protection scheme. This protection scheme includes
  583.  hardware-enforced length and type checking to protect segments from
  584.  inadvertent misuse.
  585.  
  586.  
  587.  1.3.2  Task Management
  588.  
  589.  The 80286 is designed to support multi-tasking systems. The architecture
  590.  provides direct support for the concept of a task. For example, task state
  591.  segments (see section 8.2 in Chapter 8) are hardware-recognized and
  592.  hardware-manipulated structures that contain information on the current
  593.  state of all tasks in the system.
  594.  
  595.  Very efficient context-switching (task-switching) can be invoked with a
  596.  single instruction. Separate logical address spaces are provided for each
  597.  task in the system. Finally, mechanisms exist to support intertask
  598.  communication, synchronization, memory sharing, and task scheduling. Task
  599.  Management is described in Chapter 8.
  600.  
  601.  
  602.  1.3.3  Protection Mechanisms
  603.  
  604.  The 80286 allows the system designer to define a comprehensive protection
  605.  policy to be applied, uniformly and continuously, to all ongoing operations
  606.  of the system. Such a policy may be desirable to ensure system reliability,
  607.  privacy of data, rapid error recovery, and separation of multiple users.
  608.  
  609.  The 80286 protection mechanisms are based on the notion of a "hierarchy of
  610.  trust." Four privilege levels are distinguished, ranging from Level 0 (most
  611.  trusted) to Level 3 (least trusted). Level 0 is usually reserved for the
  612.  operating system kernel. The four levels may be visualized as concentric
  613.  rings, with the most privileged level in the center (see figure 1-1).
  614.  
  615.  This four-level scheme offers system reliability, flexibility, and design
  616.  options not possible with the typical two-level (supervisor/user) separation
  617.  provided by other processors. A four-level division is capable of separating
  618.  kernel, executive, system services, and application software, each with
  619.  different privileges.
  620.  
  621.  At any one time, a task executes at one of the four levels. Moreover, all
  622.  data segments and code segments are also assigned to privilege levels. A
  623.  task executing at one level cannot access data at a more privileged level,
  624.  nor can it call a procedure at a less privileged level (i.e., trust a less
  625.  privileged procedure to do work for it). Thus, both access to data and
  626.  transfer of control are restricted in appropriate ways.
  627.  
  628.  A complete separation can exist between the logical address spaces local to
  629.  different tasks, providing users with automatic protection against
  630.  accidental or malicious interference by other users. The hardware also
  631.  provides immediate detection of a number of fault and error conditions, a
  632.  feature that can be useful in the development and maintenance of software.
  633.  
  634.  Finally, these protection mechanisms require relatively little system
  635.  overhead because they are integrated into the memory management and
  636.  protection hardware of the processor itself.
  637.  
  638.  
  639.  Figure 1-1.  Four Privilege Levels
  640.  
  641.                         ╔═══════════════════════════╗
  642.                         ║          LEVEL 3 ────────╫──LEAST TRUSTED
  643.                         ║  ╔═════════════════════╗  ║
  644.                         ║  ║       LEVEL 2       ║  ║
  645.                         ║  ║  ╔═══════════════╗  ║  ║
  646.                         ║  ║  ║    LEVEL 1    ║  ║  ║
  647.                         ║  ║  ║  ╔═════════╗  ║  ║  ║
  648.                         ║  ║  ║  ║ LEVEL 0 ║  ║  ║  ║
  649.                         ║  ║  ║  ║        ║  ║  ║  ║
  650.                         ║  ║  ║  ╚═══════╪═╝  ║  ║  ║
  651.                         ║  ║  ║          │    ║  ║  ║
  652.                         ║  ║  ╚══════════╪════╝  ║  ║
  653.                         ║  ║             │       ║  ║
  654.                         ║  ╚═════════════╪═══════╝  ║
  655.                         ║                │          ║
  656.                         ╚════════════════╪══════════╝
  657.                                          │
  658.                                          └MOST TRUSTED
  659.  
  660.  
  661.  1.3.4  Support for Operating Systems
  662.  
  663.  Most operating systems involve some degree of concurrency, with multiple
  664.  tasks vying for system resources. The task management mechanisms described
  665.  above provide the 80286 with inherent support for such multi-tasking
  666.  systems. Moreover, the advanced memory management features of the 80286
  667.  allow the implementation of sophisticated virtual memory systems.
  668.  
  669.  Operating system implementors have found that a multi-level approach to
  670.  system services provides better security and more reliable systems. For
  671.  example, a very secure kernel might implement critical functions such as
  672.  task scheduling and resource allocation, while less fundamental functions
  673.  (such asI/O) are built around the kernel. This layered approach also makes
  674.  program development and enhancement simpler and facilitates error detection
  675.  and debugging. The 80286 supports the layered approach through its
  676.  four-level privilege scheme.
  677.  
  678.  
  679.  1.4  Organization of This Book
  680.  
  681.  To facilitate the use of this book both as an introduction to the 80286
  682.  architecture and as a reference guide, the remaining chapters are divided
  683.  into three major parts.
  684.  
  685.  Part I, comprising chapters 2 through 4, should be read by all those who
  686.  wish to acquire a basic familiarity with the 80286 architecture. These
  687.  chapters provide detailed information on memory segmentation, registers,
  688.  addressing modes and the general (application level) 80286 instruction set.
  689.  In conjunction with the 80286 Assembly Language Reference Manual, these
  690.  chapters provide sufficient information for an assembly language programmer
  691.  to design and write application programs.
  692.  
  693.  The chapters in Part I are:
  694.  
  695.  Chapter 2, "Architectural Features." This chapter discusses those features
  696.  of the 80286 architecture that are significant for application programmers.
  697.  The information presented can also function as an introduction to the
  698.  machine for system programmers. Memory organization and segmentation,
  699.  processor registers, addressing modes, and instruction formats are all
  700.  discussed.
  701.  
  702.  Chapter 3, "Basic Instruction Set." This chapter presents the core
  703.  instructions of the 8086 family.
  704.  
  705.  Chapter 4, "Extended Instruction Set." This chapter presents the extended
  706.  instructions shared by the 80186 and 80286 processors.
  707.  
  708.  Part II of the book consists of a single chapter:
  709.  
  710.  Chapter 5, "Real Address Mode." This chapter presents the system
  711.  programmer's view of the 80286 when the processor is operated in Real
  712.  Address Mode.
  713.  
  714.  Part III of the book comprises chapters 6 through 11. Aimed primarily at
  715.  system programmers, these chapters discuss the more advanced architectural
  716.  features of the 80286, which are available when the processor is in
  717.  Protected Mode. Details on memory management, protection mechanisms, and
  718.  task switching are provided.
  719.  
  720.  The chapters in Part III are:
  721.  
  722.  Chapter 6, "Virtual Memory." This chapter describes the 80286 address
  723.  translation mechanisms that support virtual memory. Segment descriptors,
  724.  global and local descriptor tables, and descriptor caches are discussed.
  725.  
  726.  Chapter 7, "Protection." This chapter describes the protection features of
  727.  the 80286. Privilege levels, segment attributes, access restrictions, and
  728.  call gates are discussed.
  729.  
  730.  Chapter 8, "Tasks and State Transitions." This chapter describes the 80286
  731.  mechanisms that support concurrent tasks. Context-switching, task state
  732.  segments, task gates, and interrupt tasks are discussed.
  733.  
  734.  Chapter 9, "Interrupts, Traps and Faults." This chapter describes interrupt
  735.  and trap handling. Special attention is paid to the exception traps, or
  736.  faults, which may occur in Protected Mode. Interrupt gates, trap gates, and
  737.  the interrupt descriptor table are discussed.
  738.  
  739.  Chapter 10, "System Control and Initialization." This chapter describes the
  740.  actual instructions used to implement the memory management, protection, and
  741.  task support features of the 80286. System registers, privileged
  742.  instructions, and the initial machine state are discussed.
  743.  
  744.  Chapter 11, "Advanced Topics." This chapter completes Part III with a
  745.  description of several advanced topics, including special segment attributes
  746.  and pointer validation.
  747.  
  748.  
  749.  1.5  Related Publications
  750.  
  751.  The following manuals also contain information of interest to programmers
  752.  of 80287 systems:
  753.  
  754.    ■  Introduction to the 80286, order number 210308
  755.    ■  ASM286 Assembly Language Reference Manual, order number 121924
  756.    ■  80286 Operating System Writer's Guide, order number 121960
  757.    ■  80286 Hardware Reference Manual, order number 210760
  758.    ■  Microprocessor and Peripheral Handbook, order number 230843
  759.    ■  PL/M-286 User's Guide, order number 121945
  760.    ■  80287 Support Library Reference Manual, order number 122129
  761.    ■  8086 Software Toolbox Manual, order number 122203 (includes
  762.       information about 80287 Emulator Software)
  763.  
  764.  
  765.  Chapter 2  80286 Base Architecture
  766.  
  767.  ───────────────────────────────────────────────────────────────────────────
  768.  
  769.  This chapter describes the 80286 application programming environment as
  770.  seen by assembly language programmers. It is intended to introduce the
  771.  programmer to those features of the 80286 architecture that directly affect
  772.  the design and implementation of 80286 application programs.
  773.  
  774.  
  775.  2.1  Memory Organization and Segmentation
  776.  
  777.  The main memory of an 80286 system makes up its physical address space.
  778.  This address space is organized as a sequence of 8-bit quantities, called
  779.  bytes. Each byte is assigned a unique address ranging from 0 up to a maximum
  780.  of 2^(20) (1 megabyte) in Real Address Mode, and up to 2^(24) (16 megabytes)
  781.  in Protected Mode.
  782.  
  783.  A virtual address space is the organization of memory as viewed by a
  784.  program. Virtual address space is also organized in units of bytes. (Other
  785.  addressable units such as words, strings, and BCD digits are described below
  786.  in section 2.2, "Data Types.") In Real Address Mode, as with the 8086
  787.  itself, programs view physical memory directly, inasmuch as they manipulate
  788.  pure physical addresses. Thus, the virtual address space is identical to the
  789.  physical address space (1 megabyte).
  790.  
  791.  In Protected Mode, however, programs have no direct access to physical
  792.  addresses. Instead, memory is viewed as a much larger virtual address space
  793.  of 2^(30) bytes (1 gigabyte). This 1 gigabyte virtual address is mapped onto
  794.  the Protected Mode's 16-megabyte physical address space by the address
  795.  translation mechanisms described in Chapter 6.
  796.  
  797.  The programmer views the virtual address space on the 80286 as a collection
  798.  of up to sixteen thousand linear subspaces, each with a specified size or
  799.  length. Each of these linear address spaces is called a segment. A segment
  800.  is a logical unit of contiguous memory. Segment sizes may range from one
  801.  byte up to 64K (65,536) bytes.
  802.  
  803.  80286 memory segmentation supports the logical structure of programs and
  804.  data in memory. Programs are not written as single linear sequences of
  805.  instructions and data, but rather as modules of code and data. For example,
  806.  program code may include a main routine and several separate procedures.
  807.  Data may also be organized into various data structures, some private and
  808.  some shared with other programs in the system. Run-time stacks constitute
  809.  yet another data requirement. Each of these several modules of code and
  810.  data, moreover, may be very different in size or vary dynamically with
  811.  program execution.
  812.  
  813.  Segmentation supports this logical structure (see figure 2-1). Each
  814.  meaningful module of a program may be separately contained in individual
  815.  segments. The degree of modularization, of course, depends on the
  816.  requirements of a particular application. Use of segmentation benefits
  817.  almost all applications. Programs execute faster and require less space.
  818.  Segmentation also simplifies the design of structured software.
  819.  
  820.  
  821.  2.2  Data Types
  822.  
  823.  Bytes and words are the fundamental units in which the 80286 manipulates
  824.  data, i.e., the fundamental data types.
  825.  
  826.  A byte is 8 contiguous bits starting on an addressable byte boundary. The
  827.  bits are numbered 0 through 7, starting from the right. Bit 7 is the most
  828.  significant bit:
  829.  
  830.                      7                             0
  831.                     ┌───┬───┬───┬───┬───┬───┬───┬───┐
  832.                     │              BYTE             │
  833.                     └───┴───┴───┴───┴───┴───┴───┴───┘
  834.  
  835.  A word is defined as two contiguous bytes starting on an arbitrary byte
  836.  boundary; a word thus contains 16 bits. The bits are numbered 0 through 15,
  837.  starting from the right. Bit 15 is the most significant bit. The byte
  838.  containing bit 0 of the word is called the low byte; the byte containing
  839.  bit 15 is called the high byte.
  840.  
  841.      15                                                             0
  842.      ┌───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┐
  843.      │           HIGH BYTE           │           LOW BYTE            │
  844.      └───┴───┴───┴───┴───┴───┴───┴───┴───┴───┴───┴───┴───┴───┴───┴───┘
  845.               LOCATION N + 1                    LOCATION N
  846.  
  847.  Each byte within a word has its own particular address, and the smaller of
  848.  the two addresses is used as the address of the word. The byte at this lower
  849.  address contains the eight least significant bits of the word, while the
  850.  byte at the higher address contains the eight most significant bits. The
  851.  arrangement of bytes within words is illustrated in figure 2-2.
  852.  
  853.  Note that a word need not be aligned at an even-numbered byte address. This
  854.  allows maximum flexibility in data structures (e.g., records containing
  855.  mixed byte and word entries) and efficiency in memory utilization. Although
  856.  actual transfers of data between the processor and memory take place at
  857.  physically aligned word boundaries, the 80286 converts requests for
  858.  unaligned words into the appropriate sequences of requests acceptable to the
  859.  memory interface. Such odd aligned word transfers, however, may impact
  860.  performance by requiring two memory cycles to transfer the word rather than
  861.  one. Data structures (e.g., stacks) should therefore be designed in such a
  862.  way that word operands are aligned on word boundaries whenever possible for
  863.  maximum system performance. Due to instruction prefetching and queueing
  864.  within the CPU, there is no requirement for instructions to be aligned on
  865.  word boundaries and no performance loss if they are not.
  866.  
  867.  Although bytes and words are the fundamental data types of operands, the
  868.  processor also supports additional interpretations on these bytes or words.
  869.  Depending on the instruction referencing the operand, the following
  870.  additional data types can be recognized:
  871.  
  872.  Integer:
  873.  A signed binary numeric value contained in an 8-bit byte or a 16-bit word.
  874.  All operations assume a 2's complement representation. (Signed 32- and
  875.  64-bit integers are supported using the 80287 Numeric Data Processor.)
  876.  
  877.  Ordinal:
  878.  An unsigned binary numeric value contained in an 8-bit byte or 16-bit word.
  879.  
  880.  Pointer:
  881.  A 32-bit address quantity composed of a segment selector component and an
  882.  offset component. Each component is a 16-bit word.
  883.  
  884.  String:
  885.  A contiguous sequence of bytes or words. A string may contain from 1 byte
  886.  to 64K bytes.
  887.  
  888.  ASCII:
  889.  A byte representation of alphanumeric and control characters using the
  890.  ASCII standard of character representation.
  891.  
  892.  BCD:
  893.  A byte (unpacked) representation of the decimal digits (0-9).
  894.  
  895.  Packed BCD:
  896.  A byte (packed) representation of two decimal digits (0-9). One digit is
  897.  stored in each nibble of the byte.
  898.  
  899.  Floating Point:
  900.  A signed 32-, 64-, or 80-bit real number representation. (Floating operands
  901.  are supported using the 80287 Numeric Processor Configuration.)
  902.  
  903.  Figure 2-3 graphically represents the data types supported by the 80286.
  904.  80286 arithmetic operations may be performed on five types of numbers:
  905.  unsigned binary, signed binary (integers), unsigned packed decimal, unsigned
  906.  unpacked decimal, and floating point. Binary numbers may be 8 or 16 bits
  907.  long. Decimal numbers are stored in bytes; two digits per byte for packed
  908.  decimal, one digit per byte for unpacked decimal. The processor always
  909.  assumes that the operands specified in arithmetic instructions contain data
  910.  that represent valid numbers for the type of instruction being performed.
  911.  Invalid data may produce unpredictable results.
  912.  
  913.  Unsigned binary numbers may be either 8 or 16 bits long; all bits are
  914.  considered in determining a number's magnitude. The value range of an 8-bit
  915.  unsigned binary number is 0-255; 16 bits can represent values from 0 through
  916.  65,535. Addition, subtraction, multiplication and division operations are
  917.  available for unsigned binary numbers.
  918.  
  919.  Signed binary numbers (integers) may be either 8 or 16 bits long. The
  920.  high-order (leftmost) bit is interpreted as the number's sign: 0 = positive
  921.  and 1 = negative. Negative numbers are represented in standard two's
  922.  complement notation. Since the high-order bit is used for a sign, the range
  923.  of an 8-bit integer is -128 through +127; 16-bit integers may range from
  924.  -32,768 through +32,767. The value zero has a positive sign.
  925.  
  926.  Separate multiplication and division operations are provided for both
  927.  signed and unsigned binary numbers. The same addition and subtraction
  928.  instructions are used with signed or unsigned binary values. Conditional
  929.  jump instructions, as well as an "interrupt on overflow" instruction, can
  930.  be used following an unsigned operation on an integer to detect overflow
  931.  into the sign bit.
  932.  
  933.  Unpacked decimal numbers are stored as unsigned byte quantities. One digit
  934.  is stored in each byte. The magnitude of the number is determined from the
  935.  low-order half-byte; hexadecimal values 0-9 are valid and are interpreted as
  936.  decimal numbers. The high-order half-byte must be zero for multiplication
  937.  and division; it may contain any value for addition and subtraction.
  938.  
  939.  Arithmetic on unpacked decimal numbers is performed in two steps. The
  940.  unsigned binary addition, subtraction and multiplication operations are used
  941.  to produce an intermediate result. An adjustment instruction then changes
  942.  the value to a final correct unpacked decimal number. Division is performed
  943.  similarly, except that the adjustment is carried out on the two digit
  944.  numerator operand in register AX first, followed by an unsigned binary
  945.  division instruction that produces a correct result.
  946.  
  947.  Unpacked decimal numbers are similar to the ASCII character representations
  948.  of the digits 0-9. Note, however, that the high-order half-byte of an ASCII
  949.  numeral is always 3. Unpacked decimal arithmetic may be performed on ASCII
  950.  numeric characters under the following conditions:
  951.  
  952.    ■  the high-order half-byte of an ASCII numeral must be set to 0H prior
  953.       to multiplication or division.
  954.  
  955.    ■  unpacked decimal arithmetic leaves the high-order half-byte set to 0H;
  956.       it must be set to 3 to produce a valid ASCII numeral.
  957.  
  958.  Packed decimal numbers are stored as unsigned byte quantities. The byte is
  959.  treated as having one decimal digit in each half-byte (nibble); the digit in
  960.  the high-order half-byte is the most significant. Values 0-9 are valid in
  961.  each half-byte, and the range of a packed decimal number is 0-99. Additions
  962.  and subtractions are performed in two steps. First, an addition or
  963.  subtraction instruction is used to produce an intermediate result. Then, an
  964.  adjustment operation is performed which changes the intermediate value to a
  965.  final correct packed decimal result. Multiplication and division
  966.  adjustments are only available for unpacked decimal numbers.
  967.  
  968.  Pointers and addresses are described below in section 2.3.3, "Index,
  969.  Pointer, and Base Registers," and in section 3.8, "Address Manipulation
  970.  Instructions."
  971.  
  972.  Strings are contiguous bytes or words from 1 to 64K bytes in length. They
  973.  generally contain ASCII or other character data representations. The 80286
  974.  provides string manipulation instructions to move, examine, or modify a
  975.  string (see section 3.7, "Character Translation and String Instructions").
  976.  
  977.  If the 80287 numeric processor extension (NPX) is present in the system ──
  978.  see the 80287 NPX book──the 80286 architecture also supports floating point
  979.  numbers, 32- and 64-bit integers, and 18-digit BCD data types.
  980.  
  981.  The 80287 Numeric Data Processor supports and stores real numbers in a
  982.  three-field binary format as required by IEEE standard 754 for floating
  983.  point numerics (see figure 2-3). The number's significant digits are held
  984.  in the significand field, the exponent field locates the binary point within
  985.  the significant digits (and therefore determines the number's magnitude),
  986.  and the sign field indicates whether the number is positive or negative.
  987.  (The exponent and significand are analogous to the terms "characteristic"
  988.  and "mantissa," typically used to describe floating point numbers on some
  989.  computers.) This format is used by the 80287 with various length
  990.  significands and exponents to support single precision, double precision and
  991.  extended (80-bit) precision floating point data types. Negative numbers
  992.  differ from positive numbers only in their sign bits.
  993.  
  994.  
  995.  Figure 2-1.  Segmented Virtual Memory
  996.  
  997.  ┌─ ── ── ── ── ── ── ── ── ┐
  998.   20000╔════════════════╗      8000╔═══════════════╗
  999.  │     ║CS              ║   │      ║               ║   8600╔═══════════════╗
  1000.        ║      MAIN      ║          ║  PROCEDURE A  ║       ║   PROCEDURE   ║
  1001.  │     ║    PROCEDURE   ║   │      ║               ║       ║       B       ║
  1002.       0╚════════════════╝         0╚═══════════════╝      0╚═══════════════╝
  1003.  │                          │
  1004.        ╔════════════════╗     72535╔═══════════════╗       ╔═══════════════╗
  1005.  │     ║DS              ║   │      ║               ║       ║               ║
  1006.        ║   DATA (MAIN)  ║          ║    DATA (A)   ║       ║    DATA (B)   ║
  1007.  │    0╚════════════════╝   │     0╚═══════════════╝      0╚═══════════════╝
  1008.    2000╔════════════════╗
  1009.  │     ║SS   PROCESS    ║   │
  1010.        ║      STACK     ║
  1011.  │    0╚════════════════╝   │
  1012.        ╔════════════════╗
  1013.  │     ║ES PROCESS-WIDE ║   │
  1014.        ║       DATA     ║
  1015.  │    0╚════════════════╝   │
  1016.  └─ ── ── ── ── ── ── ── ── ┘
  1017.      CURRENTLY ACCESSIBLE
  1018.  
  1019.  
  1020.  Figure 2-2.  Bytes and Words in Memory
  1021.  
  1022.  BYTE
  1023.  ADDRESS     MEMORY VALUES
  1024.                                
  1025.         ╠═══════════════════════╣
  1026.       E ║                       ║
  1027.         ╠═══════════════════════╣
  1028.       D ║                       ║
  1029.         ╠═══════════════════════╣
  1030.       C ║          FE           ║─┐
  1031.         ╠═══════════════════════╣ ├─ WORD AT ADDRESS B CONTAINS FE06
  1032.       B ║          06           ║─┘
  1033.         ╠═══════════════════════╣
  1034.       A ║                       ║
  1035.         ╠═══════════════════════╣─┐
  1036.       9 ║          1F           ║ ├─BYTE AT ADDRESS 9 CONTAINS 1F
  1037.         ╠═══════════════════════╣─┘
  1038.       8 ║                       ║
  1039.         ╠═══════════════════════╣
  1040.       7 ║          23           ║─┐
  1041.         ╠═══════════════════════╣ ├─ WORD AT ADDRESS 6 CONTAINS 23OB
  1042.       6 ║          OB           ║─┘
  1043.         ╠═══════════════════════╣
  1044.       5 ║                       ║
  1045.         ╠═══════════════════════╣
  1046.       4 ║                       ║
  1047.         ╠═══════════════════════╣
  1048.       3 ║          74           ║ ─┐
  1049.         ╠═══════════════════════╣─┐├─ WORD AT ADDRESS 2 CONTAINS 74CB
  1050.       2 ║          CB           ║ ─┘
  1051.         ╠═══════════════════════╣ ├─ WORD AT ADDRESS 1 CONTAINS CB31
  1052.       1 ║          31           ║ │
  1053.         ╠═══════════════════════╣─┘
  1054.       0 ║                       ║
  1055.         ╚═══════════════════════╝
  1056.  
  1057.  
  1058.  Figure 2-3.  80286/80287 Supported Data Types
  1059.  
  1060.                                                             +1       0
  1061.            7       0              7       0            15 14    8 7      0
  1062.     SIGNED ╔╤╤╤╤╤╤╤╗     UNSIGNED ╔╤╤╤╤╤╤╤╗       SIGNED ╔╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╗
  1063.       BYTE ║│  │   ║         BYTE ║   │   ║         WORD ║│  │   │   │   ║
  1064.            ╚╧══════╝              ╚═══════╝              ╚╧══════╧═══════╝
  1065.    SIGN BIT┘└──────┘              │└MSB   │      SIGN BIT┘└MSB           │
  1066.             MAGNITUDE             └───────┘              └───────────────┘
  1067.                                   MAGNITUDE                  MAGNITUDE
  1068.  
  1069.                            +3      +2      +1       0
  1070.                          31            16 15             0
  1071.     SIGNED DOUBLE WORD ╔╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╗
  1072.                         ║│  │   │   │   │   │   │   │   ║
  1073.                         ╚╧══════╧═══════╧═══════╧═══════╝
  1074.                 SIGN BIT┘└MBS                           │
  1075.                         └───────────────────────────────┘
  1076.                                     MAGNITUDE
  1077.  
  1078.                           +7  +6  +5  +4  +3  +2  +1   0
  1079.                         63    48 47   32 31   16 15     0
  1080.       SIGNED QUAD WORD ╔╤══╤═══╤═══╤═══╤═══╤═══╤═══╤═══╗
  1081.                         ║│  │   │   │   │   │   │   │   ║
  1082.                         ╚╧══╧═══╧═══╧═══╧═══╧═══╧═══╧═══╝
  1083.                 SIGN BIT┘└MSB                           │
  1084.                          └──────────────────────────────┘
  1085.                                      MAGNITUDE
  1086.  
  1087.                            +1       0
  1088.                         15              0
  1089.           UNSIGNED WORD ╔╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╗
  1090.                         ║│  │   │   │   ║
  1091.                         ╚╧══════╧═══════╝
  1092.                         │└MSB           │
  1093.                         └───────────────┘
  1094.                             MAGNITUDE
  1095.  
  1096.                            +N                +1       0
  1097.                         7       0         7      0 7      0
  1098.    BINARY CODED DECIMAL ╔╤╤╤╤╤╤╤╗         ╔╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╗
  1099.           (BCD)         ║   │   ║      ║   │   │   │   ║
  1100.                         ╚═══════╝         ╚═══════╧═══════╝
  1101.                            BCD               BCD     BCD
  1102.                          DIGIT N           DIGIT 1  DIGIT 0
  1103.  
  1104.                            +N                +1       0
  1105.                         7       0         7      0 7      0
  1106.                   ASCII ╔╤╤╤╤╤╤╤╗         ╔╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╗
  1107.                         ║   │   ║      ║   │   │   │   ║
  1108.                         ╚═══════╝         ╚═══════╧═══════╝
  1109.                           ASCII              ASCII ASCII
  1110.                        CHARACTER[N]   CHARACTER{1} CHARACTER{0}
  1111.  
  1112.                            +N                +1       0
  1113.                         7       0         7      0 7      0
  1114.              PACKED BCD ╔╤╤╤╤╤╤╤╗         ╔╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╗
  1115.                         ║   │   ║      ║   │   │   │   ║
  1116.                         ╚═══════╝         ╚═══════╧═══════╝
  1117.                         └───┘                         └───┘
  1118.                         MOST                          LEAST
  1119.                         SIGNIFICANT             SIGNIFICANT
  1120.                         DIGIT                         DIGIT
  1121.  
  1122.                            +N                +1       0
  1123.                         7/15    0         7/15   0 7/15   0
  1124.                  STRING ╔╤╤╤╤╤╤╤╗         ╔╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╗
  1125.                         ║   │   ║      ║   │   │   │   ║
  1126.                         ╚═══════╝         ╚═══════╧═══════╝
  1127.                        BYTE/WORD N       BYTE/WORD BYTE/WORD
  1128.                                              1         0
  1129.  
  1130.                            +3      +2      +1       0
  1131.                         31            16 15             0
  1132.                 POINTER ╔╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╗
  1133.                         ║│  │   │   │   │   │   │   │   ║
  1134.                         ╚╧══════╧═══════╧═══════╧═══════╝
  1135.                         └───────────────┴───────────────┘
  1136.                              SELECTOR        OFFSET
  1137.  
  1138.                           +9  +8  +7  +6  +5  +4  +3  +2  +1   0
  1139.                         79                                       0
  1140.         FLOATING POINT ╔╤══╤═══╤═══╤═══╤═══╤═══╤═══╤═══╤═══╤═══╗
  1141.                         ║│  │   │   │   │   │   │   │   │   │   ║
  1142.                         ╚╧══╧═══╧═══╧═══╧═══╧═══╧═══╧═══╧═══╧═══╝
  1143.                 SIGN BIT┘└──────────┴───────────────────────────┘
  1144.                            EXPONENT           MAGNITUDE
  1145.  
  1146.  
  1147.  2.3  Registers
  1148.  
  1149.  The 80286 contains a total of fourteen registers that are of interest to
  1150.  the application programmer. (Five additional registers used by system
  1151.  programmers are covered in section 10.1.) As shown in figure 2-4, these
  1152.  registers may be grouped into four basic categories:
  1153.  
  1154.    ■  General registers. These eight 16-bit general-purpose registers are
  1155.       used primarily to contain operands for arithmetic and logical
  1156.       operations.
  1157.  
  1158.    ■  Segment registers. These four special-purpose registers determine, at
  1159.       any given time, which segments of memory are currently addressable.
  1160.  
  1161.    ■  Status and Control registers. These three special-purpose registers
  1162.       are used to record and alter certain aspects of the 80286 processor
  1163.       state.
  1164.  
  1165.  
  1166.  2.3.1  General Registers
  1167.  
  1168.  The general registers of the 80286 are the 16-bit registers AX, BX, CX, DX,
  1169.  SP, BP, SI, and DI. These registers are used interchangeably to contain the
  1170.  operands of logical and arithmetic operations.
  1171.  
  1172.  Some instructions and addressing modes (see section 2.4), however, dedicate
  1173.  certain general registers to specific uses. BX and BP are often used to
  1174.  contain the base address of data structures in memory (for example, the
  1175.  starting address of an array); for this reason, they are often referred to
  1176.  as the base registers. Similarly, SI and DI are often used to contain an
  1177.  index value that will be incremented to step through a data structure; these
  1178.  two registers are called the index registers. Finally, SP and BP are used
  1179.  for stack manipulation. Both SP and BP normally contain offsets into the
  1180.  current stack. SP generally contains the offset of the top of the stack and
  1181.  BP contains the offset or base address of the current stack frame. The use
  1182.  of these general-purpose registers for operand addressing is discussed in
  1183.  section 2.3.3, "Index, Pointer, and Base Registers." Register usage for
  1184.  individual instructions is discussed in chapters 3 and 4.
  1185.  
  1186.  As shown in figure 2-4, eight byte registers overlap four of the 16-bit
  1187.  general registers. These registers are named AH, BH, CH, and DH (high
  1188.  bytes); and AL, BL, CL, and DL (low bytes); they overlap AX, BX, CX, and DX.
  1189.  These registers can be used either in their entirety or as individual 8-bit
  1190.  registers. This dual interpretation simplifies the handling of both 8- and
  1191.  16-bit data elements.
  1192.  
  1193.  
  1194.  Figure 2-4.  80286 Base Architecture Register Set
  1195.  
  1196.                        16-BIT                 SPECIAL
  1197.                       REGISTER                REGISTER
  1198.                         NAME                  FUNCTIONS
  1199.  GENERAL REGISTERS           7    0 7    0
  1200.                        ┌─   ╔══════╤══════╗─┐
  1201.                        │ AX ║  AH  │  AL  ║ │
  1202.                   BYTE │    ╟──────┼──────╢ ├─MULTIPLY/DIVIDE
  1203.            ADDRESSABLE │ DX ║  DH  │  DL  ║ │ I/O INSTRUCTIONS
  1204.                 (8-BIT─┤    ╟──────┼──────╢═╡
  1205.               REGISTER │ CX ║  CH  │  CL  ║ ├─LOOP/SHIFT/REPEAT COUNT
  1206.                  NAMES │    ╟──────┼──────╢═╡
  1207.                 SHOWN) │ BX ║  BH  │  BL  ║ │
  1208.                        └─   ╟──────┴──────╢ ├─BASE REGISTERS
  1209.                          BP ║             ║ │
  1210.                             ╟─────────────╢═╡
  1211.                          SI ║             ║ │
  1212.                             ╟─────────────╢ ├─INDEX REGISTERS
  1213.                          DI ║             ║ │
  1214.                             ╟─────────────╢═╡
  1215.                          SP ║             ║ ├─STACK POINTER
  1216.                             ╚═════════════╝─┘
  1217.                              15          0
  1218.  
  1219.  SEGMENT REGISTERS           15          0
  1220.                             ╔═════════════╗
  1221.                          CS ║             ║ CODE SEGMENT SELECTOR
  1222.                             ╟─────────────╢
  1223.                          DS ║             ║ DATA SEGMENT SELECTOR
  1224.                             ╟─────────────╢
  1225.                          SS ║             ║ STACK SEGMENT SELECTOR
  1226.                             ╟─────────────╢
  1227.                          ES ║             ║ EXTRA SEGMENT SELECTOR
  1228.                             ╚═════════════╝
  1229.  
  1230.  STATUS AND CONTROL          15          0
  1231.      REGISTERS              ╔═════════════╗
  1232.                           F ║             ║ FLAGS
  1233.                             ╟─────────────╢
  1234.                          IP ║             ║ INSTRUCTION POINTER
  1235.                             ╟─────────────╢
  1236.                         MSW ║             ║ MACHINE STATUS WORD
  1237.                             ╚═════════════╝
  1238.  
  1239.  
  1240.  2.3.2  Memory Segmentation and Segment Registers
  1241.  
  1242.  Complete programs generally consist of many different code modules (or
  1243.  segments), and different types of data segments. However, at any given time
  1244.  during program execution, only a small subset of a program's segments are
  1245.  actually in use. Generally, this subset will include code, data, and
  1246.  possibly a stack. The 80286 architecture takes advantage of this by
  1247.  providing mechanisms to support direct access to the working set of a
  1248.  program's execution environment and access to additional segments on
  1249.  demand.
  1250.  
  1251.  At any given instant, four segments of memory are immediately accessible to
  1252.  an executing 80286 program. The segment registers DS, ES, SS, and CS are
  1253.  used to identify these four current segments. Each of these registers
  1254.  specifies a particular kind of segment, as characterized by the associated
  1255.  mnemonics ("code," "stack," "data," or "extra") shown in figure 2-4.
  1256.  
  1257.  An executing program is provided with concurrent access to the four
  1258.  individual segments of memory──a code segment, a stack segment, and two
  1259.  data segments──by means of the four segment registers. Each may be said to
  1260.  select a segment, since it uniquely determines the one particular segment
  1261.  from among the numerous segments in memory, which is to be immediately
  1262.  accessible at highest speed. Thus, the 16-bit contents of a segment register
  1263.  is called a segment selector.
  1264.  
  1265.  Once a segment is selected, a base address is associated with it. To
  1266.  address an element within a segment, a 16-bit offset from the segment's base
  1267.  address must be supplied. The 16-bit segment selector and the 16-bit offset
  1268.  taken together form the high and low order halves, respectively, of a
  1269.  32-bit virtual address pointer. Once a segment is selected, only the lower
  1270.  16-bits of the pointer, called the offset, generally need to be specified by
  1271.  an instruction. Simple rules define which segment register is used to form
  1272.  an address when only a 16-bit offset is specified.
  1273.  
  1274.  An executing program requires, first of all, that its instructions reside
  1275.  somewhere in memory. The segment of memory containing the currently
  1276.  executing sequence of instructions is known as the current code segment; it
  1277.  is specified by means of the CS register. All instructions are fetched from
  1278.  this code segment, using as an offset the contents of the instruction
  1279.  pointer (IP). The CS:IP register combination therefore forms the full 32-bit
  1280.  pointer for the next sequential program instruction. The CS register is
  1281.  manipulated indirectly. Transitions from one code segment to another (e.g.,
  1282.  a procedure call) are effected implicitly as the result of control-transfer
  1283.  instructions, interrupts, and trap operations.
  1284.  
  1285.  Stacks play a fundamental role in the 80286 architecture; subroutine calls,
  1286.  for example, involve a number of implicit stack operations. Thus, an
  1287.  executing program will generally require a region of memory for its stack.
  1288.  The segment containing this region is known as the current stack segment,
  1289.  and it is specified by means of the SS register. All stack operations are
  1290.  performed within this segment, usually in terms of address offsets contained
  1291.  in the stack pointer (SP) and stack frame base (BP) registers. Unlike CS,
  1292.  the SS register can be loaded explicitly for dynamic stack definition.
  1293.  
  1294.  Beyond their code and stack requirements, most programs must also fetch and
  1295.  store data in memory. The DS and ES registers allow the specification of two
  1296.  data segments, each addressable by the currently executing program.
  1297.  Accessibility to two separate data areas supports differentiation and
  1298.  access requirements like local procedure data and global process data. An
  1299.  operand within a data segment is addressed by specifying its offset either
  1300.  directly in an instruction or indirectly via index and/or base registers
  1301.  (described in the next subsection).
  1302.  
  1303.  Depending on the data structure (e.g., the way data is parceled into one or
  1304.  more segments), a program may require access to multiple data segments. To
  1305.  access additional segments, the DS and ES registers can be loaded under
  1306.  program control during the course of a program's execution. This simply
  1307.  requires loading the appropriate data pointer prior to accessing the data.
  1308.  
  1309.  The interpretation of segment selector values depends on the operating mode
  1310.  of the processor. In Real Address Mode, a segment selector is a physical
  1311.  address (figure 2-5). In Protected Mode, a segment selector selects a
  1312.  segment of the user's virtual address space (figure 2-6). An intervening
  1313.  level of logical-to-physical address translation converts the logical
  1314.  address to a physical memory address. Chapter 6, "Memory Management,"
  1315.  provides a detailed discussion of Protected Mode addressing. In general,
  1316.  considerations of selector formats and the details of memory mapping need
  1317.  not concern the application programmer.
  1318.  
  1319.  
  1320.  2.3.3  Index, Pointer, and Base Registers
  1321.  
  1322.  Five of the general-purpose registers are available for offset address
  1323.  calculations. These five registers, shown in figure 2-4, are SP, BP, BX,
  1324.  SI, and DI. SP is called a pointer register; BP and BX are called base
  1325.  registers; SI and DI are called index registers.
  1326.  
  1327.  As described in the previous section, segment registers define the set of
  1328.  four segments currently addressable by a program. A pointer, base, or index
  1329.  register may contain an offset value relative to the start of one of these
  1330.  segments; it thereby points to a particular operand's location within that
  1331.  segment. To allow for efficient computations of effective address offsets,
  1332.  all base and index registers may participate interchangeably as operands in
  1333.  most arithmetical operations.
  1334.  
  1335.  Stack operations are facilitated by the stack pointer (SP) and stack frame
  1336.  base (BP) registers. By specifying offsets into the current stack segment,
  1337.  each of these registers provides access to data on the stack. The SP
  1338.  register is the customary top-of-stack pointer, addressing the uppermost
  1339.  datum on a push-down stack. It is referenced implicitly by PUSH and POP
  1340.  operations, subroutine calls, and interrupt operations. The BP register
  1341.  provides yet another offset into the stack segment. The existence of this
  1342.  stack relative base register, in conjunction with certain addressing modes
  1343.  described in section 2.4.3, is particularly useful for accessing data
  1344.  structures, variables and dynamically allocated work space within the stack.
  1345.  
  1346.  Stacks in the 80286 are implemented in memory and are located by the stack
  1347.  segment register (SS) and the stack pointer register (SP). A system may have
  1348.  an unlimited number of stacks, and a stack may be up to 64K bytes long, the
  1349.  maximum length of a segment.
  1350.  
  1351.  One stack is directly addressable at a time; this is the current stack,
  1352.  often referred to simply as "the" stack. SP contains the current top of the
  1353.  stack (TOS). In other words, SP contains the offset to the top of the push
  1354.  down stack from the stack segment's base address. Note, however, that the
  1355.  stack's base address (contained in SS) is not the "bottom" of the stack
  1356.  (figure 2-7).
  1357.  
  1358.  80286 stack entries are 16 bits wide. Instructions operate on the stack by
  1359.  adding and removing stack items one word at a time. An item is pushed onto
  1360.  the stack (see figure 2-8) by decrementing SP by 2 and writing the item at
  1361.  the new TOS. An item is popped off the stack by copying it from TOS and then
  1362.  incrementing SP by 2. In other words, the stack grows down in memory toward
  1363.  its base address. Stack operations never move items on the stack; nor do
  1364.  they erase them. The top of the stack changes only as a result of updating
  1365.  the stack pointer.
  1366.  
  1367.  The stack frame base pointer (BP) is often used to access elements on the
  1368.  stack relative to a fixed point on the stack rather than relative to the
  1369.  current TOS. It typically identifies the base address of the current
  1370.  stack frame established for the current procedure (figure 2-9). If an index
  1371.  register is used relative to BP (e.g., base + index addressing mode using BP
  1372.  as the base), the offset will be calculated automatically in the current
  1373.  stack segment.
  1374.  
  1375.  Accessing data structures in data segments is facilitated by the BX
  1376.  register, which has the same function in addressing operands within data
  1377.  segments that BP does for stack segments. They are called base registers
  1378.  because they may contain an offset to the base of a data structure. The
  1379.  similar usage of these two registers is especially important when discussing
  1380.  addressing modes (see section 2.4, "Addressing Modes").
  1381.  
  1382.  Operations on data are also facilitated by the SI and DI registers. By
  1383.  specifying an offset relative to the start of the currently addressable data
  1384.  segment, an index register can be used to address an operand in the segment.
  1385.  If an index register is used in conjunction with the BX base register
  1386.  (i.e., base + index addressing) to form an offset address, the data is also
  1387.  assumed to reside in the current data segment. As a rule, data referenced
  1388.  through an index register or BX is presumed to reside in the current data
  1389.  segment. That is, if an instruction invokes addressing for one of its
  1390.  operands using either BX, DI, SI, or BX with SI or DI, the contents of the
  1391.  register(s) (BX, DI, or SI) implicitly specify an offset in the current data
  1392.  segment. As previously mentioned, data referenced via SP, BP or BP with SI
  1393.  or DI implicitly specify an operand in the current stack segment (refer to
  1394.  table 2-1).
  1395.  
  1396.  There are two exceptions to the rules listed above. The first concerns the
  1397.  operation of certain 80286 string instructions. For the most flexibility,
  1398.  these instructions assume that the DI register addresses destination strings
  1399.  not in the data segment, but rather in the extra segment (ES register).
  1400.  This allows movement of strings between different segments. This has led to
  1401.  the descriptive names "source index" and "destination index." In all cases
  1402.  other than string instructions, however, the SI and DI registers may be used
  1403.  interchangeably to reference either source or destination operands.
  1404.  
  1405.  A second more general override capability allows the programmer complete
  1406.  control of which segment is used for a specific operation. Segment-override
  1407.  prefixes, discussed in section 2.4.3, allow the index and base registers to
  1408.  address data in any of the four currently addressable segments.
  1409.  
  1410.  
  1411.  Table 2-1. Implied Segment Usage by Index, Pointer, and Base Registers
  1412.  
  1413.   Register            Implied Segment
  1414.   SP                  SS
  1415.   BP                  SS
  1416.   BX                  DS
  1417.   DI                  DS, ES for String Operations
  1418.   BP + SI, DI         SS
  1419.   BX + SI, DI         DS
  1420.  
  1421.  ────────────────────────────────────────────────────────────────────────────
  1422.  NOTE
  1423.    All implied Segment usage, except SP to SS and DI to ES for String
  1424.    Operations, may be explicitly specified with a segment override prefix for
  1425.    any of the four segments. The prefix precedes the instruction for which
  1426.    explicit reference is desired.
  1427.  ────────────────────────────────────────────────────────────────────────────
  1428.  
  1429.  
  1430.  Figure 2-5.  Real Address Mode Segment Selector Interpretation
  1431.  
  1432.                                         ╔═══════════════╗─┐
  1433.                                         ║               ║ │
  1434.                                         ║               ║ │
  1435.                                       ┌─╠═══════════════╣ │  1 MEGABYTE
  1436.                SEGMENT     64K BYTES ─┤ ║     SEG 1     ║ ├─ PHYSICAL
  1437.               ┌──────────────────────└─╠═══════════════╣ │  ADDRESS
  1438.               │     BASE ADDRESS        ║               ║ │  SPACE
  1439.               │                         ║               ║ │
  1440.         ╔═════╧══════╤══════╗           ║               ║ │
  1441.         ║  SELECTOR  │ 0000 ║           ╚═══════════════╝─┘
  1442.         ╚════════════╧══════╝
  1443.  
  1444.  ───────────────────────────────────────────────────────────────────────────
  1445.  NOTES:
  1446.    1. The selector inentifies a segment in physical memory.
  1447.    2. A selector specifies the segments base address, Modulo 16, within
  1448.       the 1 Megabyte address space.
  1449.    3. The selector is the 16 most significant bits of a segments physical
  1450.       base address.
  1451.    4. The values of selectors determines the amount they overlap in real
  1452.       memory.
  1453.    5. Segments may overlap by increments of 16 bytes. Overlap ranges from
  1454.       complete (SEG 1 = SEG 1) to none (SEG 1 ╪ SEG 2 ± 64K).
  1455.  ───────────────────────────────────────────────────────────────────────────
  1456.  
  1457.  
  1458.  Figure 2-6.  Protected Mode Segment Selector Interpretation
  1459.  
  1460.                                          ╔════════════╗─┐
  1461.                                          ║  SEG 3FFF  ║ │
  1462.                                          ╠════════════╣ │
  1463.                                          ║  SEG 3FFE  ║ │
  1464.             ┌───────────────────────────╠════════════╣ │
  1465.     ╔═══════╧══════╗                     ║  SEG 3FFD  ║ │
  1466.     ║   SELECTOR   ║                   ┌─╠════════════╣ │
  1467.     ╚══════════════╝    1 TO 64K BYTES─┤ ║  SEG 3FFC  ║ │
  1468.                                        └─╠════════════╣ │
  1469.                                          ║  SEG 3FFB  ║ │
  1470.                                          ╠════════════╣ │  1 GIGABYTE
  1471.                                          ≈            ≈ ├─ VIRTUAL
  1472.                                          ╠════════════╣ │  ADDRESS
  1473.                                          ║   SEG 4    ║ │  SPACE
  1474.                                          ╠════════════╣ │
  1475.                                          ║   SEG 3    ║ │
  1476.                                          ╠════════════╣ │
  1477.                                          ║   SEG 2    ║ │
  1478.                                          ╠════════════╣ │
  1479.                                          ║   SEG 1    ║ │
  1480.                                          ╠════════════╣ │
  1481.                                          ║   SEG 0    ║ │
  1482.                                          ╚════════════╝─┘
  1483.  
  1484.  ───────────────────────────────────────────────────────────────────────────
  1485.  NOTES:
  1486.    1. A selector uniquely identifies (names) one of 16K possible segments
  1487.       in the task's virtual address space.
  1488.    2. The selector value does not specify the segment's location in
  1489.       physical memory.
  1490.    3. The selector does not imply any overlap with other segments (This
  1491.       depends on the base address of the segment via the memory management
  1492.       and protection information).
  1493.  ───────────────────────────────────────────────────────────────────────────
  1494.  
  1495.  
  1496.  Figure 2-7.  80286 Stack
  1497.  
  1498.                            ╔═════════════════╗     LOGICAL
  1499.                            ║                 ║─── BOTTOM OF STACK
  1500.                            ╠═════════════════╣     (initial SP value)
  1501.                            ║                 ║
  1502.                            ╠═════════════════╣
  1503.                            ║                 ║
  1504.                            ╠═════════════════╣
  1505.                            ║                 ║    POP-UP
  1506.                            ╠═════════════════╣  │
  1507.               ┌───────────║                 ║─── LOGICAL TOP OF STACK
  1508.               │            ╠═════════════════╣  │
  1509.               │            ║                 ║    PUSH-DOWN
  1510.    ╔══════╤═══╧══╗         ║                 ║
  1511.    ║  SS  │  SP  ║         ║                 ║
  1512.    ╚══╤═══╧══════╝         ║                 ║
  1513.       │                    ║                 ║
  1514.       │                    ║                 ║
  1515.       │                    ║                 ║
  1516.       └───────────────────╚═════════════════╝  STACK SEGMENT BASE ADDRESS
  1517.  
  1518.  
  1519.  Figure 2-8.  Stack Operation
  1520.  
  1521.    STACK OPERATION FOR CODE SEQUENCE:
  1522.    PUSH AX                                    STACK
  1523.    POP AX                                    SEGMENT
  1524.    POP BX         ┌──────────────┐                    
  1525.                   │EXISTING STACK│         ╟─────────╢  │  BOTTOM
  1526.                   │ BEFORE PUSH  │    1062 ║ 0 0 0 0 ║  │    OF
  1527.                   └──────────────┘         ╟─────────╢  │  STACK
  1528.                                       1060 ║ 1 1 1 1 ║
  1529.                                            ╟─────────╢
  1530.                                       105E ║ 2 2 2 2 ║
  1531.                                            ╟─────────╢
  1532.                                       105C ║ 3 3 3 3 ║
  1533.                                            ╟─────────╢
  1534.                                       105A ║ 4 4 4 4 ║
  1535.                                            ╟─────────╢
  1536.                     ┌──────────────  1058 ║ 5 5 5 5 ║
  1537.    SS               │   SP                 ╟─────────╢─┐
  1538.    ╔══════════╤═════╧════╗            1056 ║ 6 6 6 6 ║ │
  1539.    ║ SELECTOR │  OFFSET  ║                 ╟─────────╢ │  NOT
  1540.    ╚════╤═════╧══════════╝            1054 ║ 7 7 7 7 ║ ├─ PRESENTLY
  1541.         │                                  ╟─────────╢ │  USED
  1542.         │                             1052 ║ 8 8 8 8 ║ │
  1543.         │                                  ╟─────────╢─┘
  1544.         │                             1050 ║ 9 9 9 9 ║
  1545.         │                                  ≈         ≈
  1546.         └──────────────────────────  0000 ╟─────────╢
  1547.                                                     
  1548.  
  1549.                                               STACK
  1550.                                              SEGMENT
  1551.                                                     
  1552.                                            ╟─────────╢
  1553.                                       1062 ║ 0 0 0 0 ║
  1554.                                            ╟─────────╢
  1555.                                       1060 ║ 1 1 1 1 ║
  1556.                                            ╟─────────╢
  1557.                                       105E ║ 2 2 2 2 ║
  1558.                                            ╟─────────╢
  1559.                                       105C ║ 3 3 3 3 ║
  1560.                                            ╟─────────╢
  1561.                                       105A ║ 4 4 4 4 ║  PUSH AX
  1562.                                            ╟─────────╢╔═════════╗
  1563.                     ┌──────────────  1058 ║ 5 5 5 5 ║║ A A A A ║
  1564.    SS               │   SP                 ╟─────────╢╚══╤══════╝
  1565.    ╔══════════╤═════╧════╗            1056 ║ A A A A ║──┘
  1566.    ║ SELECTOR │  OFFSET  ║                 ╟─────────╢
  1567.    ╚════╤═════╧══════════╝            1054 ║ 7 7 7 7 ║
  1568.         │                                  ╟─────────╢
  1569.         │                             1052 ║ 8 8 8 8 ║
  1570.         │                                  ╟─────────╢
  1571.         │                             1050 ║ 9 9 9 9 ║
  1572.         │                                  ≈         ≈
  1573.         └──────────────────────────  0000 ╟─────────╢
  1574.                                                     
  1575.  
  1576.                                               STACK
  1577.                                              SEGMENT
  1578.                                                     
  1579.                                            ╟─────────╢
  1580.                                       1062 ║ 0 0 0 0 ║
  1581.                                            ╟─────────╢
  1582.                                       1060 ║ 1 1 1 1 ║
  1583.                                            ╟─────────╢
  1584.                                       105E ║ 2 2 2 2 ║
  1585.                                            ╟─────────╢   POP BX
  1586.                                       105C ║ 3 3 3 3 ║ ╔═════════╗
  1587.                                            ╟─────────╢ ║ 5 5 5 5 ║
  1588.                                       105A ║ 4 4 4 4 ║ ╚═════════╝
  1589.                                            ╟─────────╢      
  1590.                     ┌──────────────  1058 ║ 5 5 5 5 ║──────┘
  1591.    SS               │   SP                 ╟─────────╢
  1592.    ╔══════════╤═════╧════╗            1056 ║ A A A A ║──────┐
  1593.    ║ SELECTOR │  OFFSET  ║                 ╟─────────╢      
  1594.    ╚════╤═════╧══════════╝            1054 ║ 7 7 7 7 ║ ╔═════════╗
  1595.         │                                  ╟─────────╢ ║ A A A A ║
  1596.         │                             1052 ║ 8 8 8 8 ║ ╚═════════╝
  1597.         │                                  ╟─────────╢   POP AX
  1598.         │                             1050 ║ 9 9 9 9 ║
  1599.         │                                  ≈         ≈
  1600.         └──────────────────────────  0000 ╟─────────╢
  1601.                                                     
  1602.  
  1603.  
  1604.  Figure 2-9.  BP Usage as a Stack Frame Base Pointer
  1605.  
  1606.  BP is a constant pointer to stack based variables and work space. All
  1607.  references use BP and are independent of SP, which may vary during a routine
  1608.  execution.
  1609.  
  1610.  PROC N
  1611.    PUSH AX
  1612.    PUSH ARRAY_SIZE
  1613.    CALL PROC_N 1 ───────── PROC_N+1
  1614.                  ───────┐    PUSH BP
  1615.                          │    PUSH CX
  1616.                          │    MOVE BP, SP
  1617.                          │    SUB SP, WORK_SPACE
  1618.                          │         ∙
  1619.                          │         ∙
  1620.                          │         ∙
  1621.                          │   "PROCEDURE BODY"
  1622.                          │         ∙
  1623.                          │         ∙
  1624.                          │         ∙
  1625.                          │    MOV SP, BP
  1626.                          │    POP CX
  1627.                          │    POP BP
  1628.                          └─── RET
  1629.  
  1630.                                             
  1631.                                ╠═════════════╣─┐
  1632.                                ║  PARAMETERS ║ │
  1633.                                ╟─────────────╢ │
  1634.                                ║ RETURN ADDR ║ │
  1635.                                ╟─────────────╢ ├─PROCEDURE N
  1636.                ╔ ═ ═╗          ║  REGISTERS  ║ │ STACK FRAME
  1637.                  BP  ─────────╟─────────────╢ │
  1638.                ╚═ ═ ╝          ║             ║ │  PROCEDURE
  1639.                               ║  WORK_SPACE ║ │  N+1 STACK
  1640.                       BOTTOM │ ╟─────────────╢═╡    FRAME
  1641.                       OF     │ ║  PARAMETERS ║ ├──────┘
  1642.                       STACK  │ ╟─────────────╢ │ DYNAMICALLY
  1643.                                ║ RETURN ADDR ║ │ ALLOCATED
  1644.                                ╟─────────────╢ │ ON DEMAND
  1645.                ╔════╗          ║  REGISTERS  ║ │ RATHER THAN
  1646.                ║ BP ╟─────────╟─────────────╢ │┐STATICALLY
  1647.                ╚════╝          ║             ║ │├─────┘
  1648.                                ║  WORK_SPACE ║ ││
  1649.                  ┌─ ── ── ── ─╟─────────────╢─┘┘ ───┐
  1650.                  ├─ ── ── ── ─║             ║    TOP OF STACK
  1651.       ╔══════╤═══╧══╗          ║             ║
  1652.       ║  SS  │  SP  ║          ╚═════════════╝ STACK SEGMENT BASE
  1653.       ╚══════╧══════╝
  1654.  
  1655.  
  1656.  2.3.4  Status and Control Registers
  1657.  
  1658.  Two status and control registers are of immediate concern to applications
  1659.  programmers: the instruction pointer and the FLAGS registers.
  1660.  
  1661.  The instruction pointer register (IP) contains the offset address, relative
  1662.  to the start of the current code segment, of the next sequential instruction
  1663.  to be executed. Together, the CS:IP registers thus define a 32-bit
  1664.  program-counter. The instruction pointer is not directly visible to the
  1665.  programmer; it is controlled implicitly, by interrupts, traps, and
  1666.  control-transfer operations.
  1667.  
  1668.  The FLAGS register encompasses eleven flag fields, mostly one-bit wide, as
  1669.  shown in figure 2-10. Six of the flags are status flags that record
  1670.  processor status information. The status flags are affected by the execution
  1671.  of arithmetic and logical instructions. The carry flag is also modifiable
  1672.  with instructions that will clear, set or complement this flag bit. See
  1673.  Chapters 3 and 4.
  1674.  
  1675.  The carry flag (CF) generally indicates a carry or borrow out of the most
  1676.  significant bit of an 8- or 16-bit operand after performing an arithmetic
  1677.  operation; this flag is also useful for bit manipulation operations
  1678.  involving the shift and rotate instructions. The effect on the remaining
  1679.  status flags, when defined for a particular instruction, is generally as
  1680.  follows: the zero flag (ZF) indicates a zero result when set; the sign flag
  1681.  (SF) indicates whether the result was negative (SF=1) or positive (SF=0);
  1682.  when set, the overflow flag (OF) indicates whether an operation results in
  1683.  a carry into the high order bit of the result but not a carry out of the
  1684.  high-order bit, or vice versa; the parity flag (PF) indicates whether the
  1685.  modulo 2 sum of the low-order eight bits of the operation is even (PF=0) or
  1686.  odd (PF=1) parity. The auxiliary carry flag (AF) represents a carry out of
  1687.  or borrow into the least significant 4-bit digit when performing binary
  1688.  coded decimal (BCD) arithmetic.
  1689.  
  1690.  The FLAGS register also contains three control flags that are used, under
  1691.  program control, to direct certain processor operations. The
  1692.  interrupt-enable flag (IF), if set, enables external interrupts; otherwise,
  1693.  interrupts are disabled. The trap flag (TF), if set, puts the processor
  1694.  into a single-step mode for debugging purposes where the target program is
  1695.  automatically interrupted to a user supplied debug routine after the
  1696.  execution of each target program instruction. The direction flag (DF)
  1697.  controls the forward or backward direction of string operations: 0 = forward
  1698.  or auto increment the address register(s) (SI, DI or SI and DI),
  1699.  1 = backward or auto-decrement the address register(s) (SI, DI or SI
  1700.  and DI).
  1701.  
  1702.  In general, the interrupt enable flag may be set or reset with special
  1703.  instructions (STI = set, CLI = clear) or by placing the flags on the stack,
  1704.  modifying the stack, and returning the flag image from the stack to the flag
  1705.  register. If operating in Protected Mode, the ability to alter the IF bit
  1706.  is subject to protection checks to prevent non-privileged programs from
  1707.  effecting the interrupt state of the CPU. This applies to both instruction
  1708.  and stack options for modifying the IF bit.
  1709.  
  1710.  The TF flag may only be modified by copying the flag register to the stack,
  1711.  setting the TF bit in the stack image, and returning the modified stack
  1712.  image to the flag register. The trap interrupt occurs on completion of the
  1713.  next instruction. Entry to the single step routine saves the flag register
  1714.  on the stack with the TF bit set, and resets the TF bit in the register.
  1715.  After completion of the single step routine, the TF bit is automatically set
  1716.  on return to the program being single stepped to interrupt the program again
  1717.  after completion of the next instruction. Use of TF is not inhibited by the
  1718.  protection mechanism in Protected Mode.
  1719.  
  1720.  The DF flag, like the IF flag, is controlled by instructions (CLD = clear,
  1721.  STD = set) or flag register modification through the stack. Typically,
  1722.  routines that use string instructions will save the flags on the stack,
  1723.  modify DF as necessary via the instructions provided, and restore DF to its
  1724.  original state by restoring the Flag register from the stack before
  1725.  returning. Access or control of the DF flag is not inhibited by the
  1726.  protection mechanism in Protected Mode.
  1727.  
  1728.  The Special Fields bits are only relevant in Protected Mode. Real Address
  1729.  Mode programs should treat these bits as don't-care's, making no assumption
  1730.  about their status. Attempts to modify the IOPL and NT fields are subject to
  1731.  protection checking in Protected Mode. In general, the application's
  1732.  programmer will not be able to and should not attempt to modify these bits.
  1733.  (See section 10.3, "Privileged and Trusted Instructions" for more details.)
  1734.  
  1735.  
  1736.  Figure 2-10.  Flags Register
  1737.  
  1738.      STATUS FLAGS:
  1739.       CARRY────────────────────────────────────────────────┐
  1740.       PARITY─────────────────────────────────────────┐     │
  1741.       AUXILLIARY CARRY─────────────────────────┐     │     │
  1742.       ZERO───────────────────────────────┐     │     │     │
  1743.       SIGN────────────────────────────┐  │     │     │     │
  1744.       OVERFLOW────────────┐           │  │     │     │     │
  1745.                           │           │  │     │     │     │
  1746.                15 14 13 1211 10  9  8 7 6  5 4  3 2  1 0
  1747.              ╔══╤══╤══╤══╤══╤══╤══╤══╤══╤══╤══╤══╤══╤══╤══╤══╗
  1748.        FLAGS:║▒▒│NT│IOPL │OF│DF│IF│TF│SF│ZF│▒▒│AF│▒▒│PF│▒▒│CF║
  1749.              ╚══╧══╧══╧══╧══╧══╧══╧══╧══╧══╧══╧══╧══╧══╧══╧══╝
  1750.                                 
  1751.                   │   │       │  │  │          CONTROL FLAGS:
  1752.                   │   │       │  │  └───────────TRAP FLAG
  1753.                   │   │       │  └──────────────INTERRUPT ENABLE
  1754.                   │   │       └─────────────────DIRECTION FLAG
  1755.                   │   │                        SPECIAL FIELDS:
  1756.                   │   └─────────────────────────I/O PRIVILEGE LEVEL
  1757.                   └─────────────────────────────NESTED TASK FLAG
  1758.  
  1759.  
  1760.  2.4  Addressing Modes
  1761.  
  1762.  The information encoded in an 80286 instruction includes a specification of
  1763.  the operation to be performed, the type of the operands to be manipulated,
  1764.  and the location of these operands. If an operand is located in memory, the
  1765.  instruction must also select, explicitly or implicitly, which of the
  1766.  currently addressable segments contains the operand. This section covers the
  1767.  operand addressing mechanisms; 80286 operators are discussed in Chapter 3.
  1768.  
  1769.  The five elements of a general instruction are briefly described below. The
  1770.  exact format of 80286 instructions is specified in Appendix B.
  1771.  
  1772.    ■  The opcode is present in all instructions; in fact, it is the only
  1773.       required element. Its principal function is the specification of the
  1774.       operation performed by the instruction.
  1775.  
  1776.    ■  A register specifier.
  1777.  
  1778.    ■  The addressing mode specifier, when present, is used to specify the
  1779.       addressing mode of an operand for referencing data or performing
  1780.       indirect calls or jumps.
  1781.  
  1782.    ■  The displacement, when present, is used to compute the effective
  1783.       address of an operand in memory.
  1784.  
  1785.    ■  The immediate operand, when present, directly specifies one operand of
  1786.       the instruction.
  1787.  
  1788.  Of the four elements, only one, the opcode, is always present. The other
  1789.  elements may or may not be present, depending on the particular operation
  1790.  involved and on the location and type of the operands.
  1791.  
  1792.  
  1793.  2.4.1  Operands
  1794.  
  1795.  Generally speaking, an instruction is an operation performed on zero, one,
  1796.  or two operands, which are the data manipulated by the instruction. An
  1797.  operand can be located either in a register (AX, BX, CX, DX, SI, DI, SP, or
  1798.  BP in the case of 16-bit operands; AH, AL, BH, BL, CH, CL, DH, or DL in the
  1799.  case of 8-bit operands; the FLAG register for flag operations in the
  1800.  instruction itself (as an immediate operand)), or in memory or an I/O port.
  1801.  Immediate operands and operands in registers can be accessed more rapidly
  1802.  than operands in memory since memory operands must be fetched from memory
  1803.  while immediate and register operands are available in the processor.
  1804.  
  1805.  An 80286 instruction can reference zero, one, or two operands. The three
  1806.  forms are as follows:
  1807.  
  1808.    ■  Zero-operand instructions, such as RET, NOP, and HLT. Consult Appendix
  1809.       B.
  1810.  
  1811.    ■  One-operand instructions, such as INC or DEC. The location of the
  1812.       single operand can be specified implicitly, as in AAM (where the
  1813.       register AX contains the operand), or explicitly, as in INC (where
  1814.       the operand can be in any register or memory location). Explicitly
  1815.       specified operands are accessed via one of the addressing modes
  1816.       described in section 2.4.2.
  1817.  
  1818.    ■  Two operand instructions such as MOV, ADD, XOR, etc., generally
  1819.       overwrite one of the two participating operands with the result. A
  1820.       distinction can thus be made between the source operand (the one left
  1821.       unaffected by the operation) and the destination operand (the one
  1822.       overwritten by the result). Like one-operand instructions, two-operand
  1823.       instructions can specify the location of operands either explicitly or
  1824.       implicitly. If an instruction contains two explicitly specified
  1825.       operands, only one of them──either the source or the destination──can
  1826.       be in a register or memory location. The other operand must be in a
  1827.       register or be an immediate source operand. Special cases of
  1828.       two-operand instructions are the string instructions and stack
  1829.       manipulation. Both operands of some string instructions are in memory
  1830.       and are explicitly specified. Push and pop stack operations allow
  1831.       transfer between memory operands and the memory based stack.
  1832.  
  1833.  Thus, the two-operand instructions of the 80286 permit operations of the
  1834.  following sort:
  1835.  
  1836.    ■  Register-to-register
  1837.    ■  Register-to-memory
  1838.    ■  Memory-to-register
  1839.    ■  Immediate-to-register
  1840.    ■  Immediate-to-memory
  1841.    ■  Memory-to-memory
  1842.  
  1843.  Instructions can specify the location of their operands by means of eight
  1844.  addressing modes, which are described in sections 2.4.2 and 2.4.3.
  1845.  
  1846.  
  1847.  2.4.2  Register and Immediate Modes
  1848.  
  1849.  Two addressing modes are used to reference operands contained in registers
  1850.  and instructions:
  1851.  
  1852.    ■  Register Operand Mode. The operand is located in one of the 16-bit
  1853.       registers (AX, BX, CX, DX, SI, DI, SP, or BP) or in one of the 8-bit
  1854.       general registers (AH, BH, CH, DH, AL, BL, CL, or DL).
  1855.  
  1856.  Special instructions are also included for referencing the CS, DS, ES, SS,
  1857.  and Flag registers as operands also.
  1858.  
  1859.    ■  Immediate Operand Mode. The operand is part of the instruction itself
  1860.       (the immediate operand element).
  1861.  
  1862.  
  1863.  2.4.3  Memory Addressing Modes
  1864.  
  1865.  Six modes are used to access operands in memory. Memory operands are
  1866.  accessed by means of a pointer consisting of a segment selector (see section
  1867.  2.3.2) and an offset, which specifies the operand's displacement in bytes
  1868.  from the beginning of the segment in which it resides. Both the segment
  1869.  selector component and the offset component are 16-bit values. (See section
  1870.  2.1 for a discussion of segmentation.) Only some instructions use a full
  1871.  32-bit address.
  1872.  
  1873.  Most memory references do not require the instruction to specify a full
  1874.  32-bit pointer address. Operands that are located within one of the
  1875.  currently addressable segments, as determined by the four segment registers
  1876.  (see section 2.3.2, "Segment Registers"), can be referenced very
  1877.  efficiently simply by means of the 16-bit offset. This form of address is
  1878.  called by short address. The choice of segment (CS, DS, ES, or SS) is either
  1879.  implicit within the instruction itself or explicitly specified by means of
  1880.  a segment override prefix (see below).
  1881.  
  1882.  See figure 2-11 for a diagram of the addressing process.
  1883.  
  1884.  
  1885.  2.4.3.1  Segment Selection
  1886.  
  1887.  All instructions that address operands in memory must specify the segment
  1888.  and the offset. For speed and compact instruction encoding, segment
  1889.  selectors are usually stored in the high speed segment registers. An
  1890.  instruction need specify only the desired segment register and an offset in
  1891.  order to address a memory operand.
  1892.  
  1893.  Most instructions need not explicitly specify which segment register is
  1894.  used. The correct segment register is automatically chosen according to the
  1895.  rules of table 2-1 and table 2-2. These rules follow the way programs are
  1896.  written (see figure 2-12) as independent modules that require areas for
  1897.  code and data, a stack, and access to external data areas.
  1898.  
  1899.  There is a close connection between the type of memory reference and the
  1900.  segment in which that operand resides (see the next section for a
  1901.  discussion of how memory addressing mode calculations are performed). As a
  1902.  rule, a memory reference implies the current data segment (i.e., the
  1903.  implicit segment selector is in DS) unless the BP register is involved in
  1904.  the address specification, in which case the current stack segment is
  1905.  implied (i.e, SS contains the selector).
  1906.  
  1907.  The 80286 instruction set defines special instruction prefix elements (see
  1908.  Appendix B). One of these is SEG, the segment-override prefix.
  1909.  Segment-override prefixes allow an explicit segment selection. Only in two
  1910.  special cases──namely, the use of DI to reference destination strings in
  1911.  the ES segment, and the use of SP to reference stack locations in the SS
  1912.  segment──is there an implied segment selection which cannot be overridden.
  1913.  The format of segment override prefixes is shown in Appendix B.
  1914.  
  1915.  
  1916.  Table 2-2 Segment Register Selection Rules
  1917.  
  1918.  Memory Reference       Segment Register     Implicit Segment
  1919.   Needed                 Used                 Selection Rule
  1920.  
  1921.  Instructions            Code (CS)            Automatic with
  1922.                                               instruction prefetch.
  1923.  
  1924.  Stack                   Stack (SS)           All stack pushes and
  1925.                                               pops. Any memory reference
  1926.                                               which uses BP as a base
  1927.                                               register.
  1928.  
  1929.  Local Data              Data (DS)            All data references
  1930.                                               except when relative to
  1931.                                               stack or string destination.
  1932.  
  1933.  External (Global)       Extra (ES)           Alternate data segment
  1934.   Data                                        and destination of string
  1935.                                               operation.
  1936.  
  1937.  
  1938.  Figure 2-11.  Two-Component Address
  1939.  
  1940.                                                           
  1941.                POINTER                       ║             ║
  1942.       ┌───────────┴───────────┐              ╠═════════════╣─┐
  1943.       ╔═══════════╤═══════════╗              ║             ║ │
  1944.       ║  SEGMENT  │  OFFSET   ║              ║             ║ │
  1945.       ╚═══════════╧═══════════╝              ║             ║ │
  1946.        31       16 15        0               ╟─────────────╢ │
  1947.        └────┬────┘ └────┬────┘               ║   OPERAND   ║ │  SELECTED
  1948.             │           │                    ║   SELECTED  ║ ├─ SEGMENT
  1949.             │           └───────────────────╟─────────────╢ │
  1950.             │                                ║             ║ │
  1951.             │                                ║             ║ │
  1952.             │                                ║             ║ │
  1953.             │                                ║             ║ │
  1954.             └───────────────────────────────╠═════════════╣─┘
  1955.                                              ║             ║
  1956.                                                 MEMORY    
  1957.  
  1958.  
  1959.  2.4.3.2  Offset Computation
  1960.  
  1961.  The offset within the desired segment is calculated in accordance with the
  1962.  desired addressing mode. The offset is calculated by taking the sum of up to
  1963.  three components:
  1964.  
  1965.    ■  the displacement element in the instruction
  1966.    ■  the base (contents of BX or BP──a base register)
  1967.    ■  the index (contents of SI or DI──an index register)
  1968.  
  1969.  Each of the three components of an offset may be either a positive or
  1970.  negative value. Offsets are calculated modulo 2^(16).
  1971.  
  1972.  The six memory addressing modes are generated using various combinations of
  1973.  these three components. The six modes are used for accessing different types
  1974.  of data stored in memory:
  1975.  
  1976.  addressing mode                        offset calculation
  1977.  direct address                         displacement alone
  1978.  register indirect                      base or index alone
  1979.  based                                  base + displacement
  1980.  indexed                                index + displacement
  1981.  based indexed                          base + index
  1982.  based indexed with displacement        base + index + disp
  1983.  
  1984.  In all six modes, the operand is located at the specified offset within the
  1985.  selected segment. All displacements, except direct address mode, are
  1986.  optionally 8- or 16-bit values. 8-bit displacements are automatically
  1987.  sign-extended to 16 bits. The six addressing modes are described and
  1988.  demonstrated in the following section on memory addressing modes.
  1989.  
  1990.  
  1991.  Figure 2-12.  Use of Memory Segmentation
  1992.  
  1993.                                                ┌─ ── ─┐
  1994.                                                ╔══════╗
  1995.                                                ║ CODE ║
  1996.                                                ╟──────╢  MODULE A
  1997.                                                ║ DATA ║
  1998.                                                ╚══════╝
  1999.              CPU                               │      │
  2000.         ┌───────────┐                          ╔══════╗
  2001.         │ ╔═══════╗ │                          ║ CODE ║
  2002.         │ ║ CODE  ╟─┼─────────────────────────╟──────╢  MODULE B
  2003.         │ ╟───────╢ │                          ║ DATA ║
  2004.         │ ║ DATA  ╟─┼─────────────────────────╚══════╝
  2005.         │ ╟───────╢ │                          │      │
  2006.         │ ║ STACK ╟─┼───────────────┐          ╔══════╗
  2007.         │ ╟───────╢ │               │          ║      ║  PROCESS STACK
  2008.         │ ║ EXTRA ╟─┼───────────┐   └─────────╚══════╝
  2009.         │ ╚═══════╝ │           │              │      │
  2010.         │  SEGMENT  │           │              ╔══════╗  PROCESS
  2011.         │ REGISTERS │           │              ║      ║  DATA
  2012.         └───────────┘           └─────────────╚══════╝  BLOCK 1
  2013.                                                │      │
  2014.                                                ╔══════╗  PROCESS
  2015.                                                ║      ║  DATA
  2016.                                                ╚══════╝  BLOCK 2
  2017.                                                └─ ── ─┘
  2018.                                                 MEMORY
  2019.  
  2020.  
  2021.  2.4.3.3  Memory Mode
  2022.  
  2023.  Two modes are used for simple scalar operands located in memory:
  2024.  
  2025.    ■  Direct Address Mode. The offset of the operand is contained in the
  2026.       instruction as the displacement element. The offset is a 16-bit
  2027.       quantity.
  2028.  
  2029.    ■  Register Indirect Mode. The offset of the operand is in one of the
  2030.       registers SI, DI, or BX. (BP is excluded; if BP is used as a stack
  2031.       frame base, it requires an index or displacement component to reference
  2032.       either parameters passed on the stack or temporary variables allocated
  2033.       on the stack. The instruction level bit encoding for the BP only
  2034.       address mode is used to specify Direct Address mode.)
  2035.  
  2036.  The following four modes are used for accessing complex data structures in
  2037.  memory (see figure 2-13):
  2038.  
  2039.    ■  Based Mode. The operand is located within the selected segment at an
  2040.       offset computed as the sum of the displacement and the contents of a
  2041.       base register (BX or BP). Based mode is often used to access the same
  2042.       field in different copies of a structure (often called a record). The
  2043.       base register points to the base of the structure (hence the term
  2044.       "base" register), and the displacement selects a particular field.
  2045.       Corresponding fields within a collection of structures can be accessed
  2046.       simply by changing the base register. (See figure 2-13, example 1.)
  2047.  
  2048.    ■  Indexed Mode. The operand is located within the selected segment at an
  2049.       offset computed as the sum of the displacement and the contents of an
  2050.       index register (SI or DI). Indexed mode is often used to access
  2051.       elements in a static array (e.g., an array whose starting location is
  2052.       fixed at translation time). The displacement locates the beginning of
  2053.       the array, and the value of the index register selects one element.
  2054.       Since all array elements are the same length, simple arithmetic on the
  2055.       index register will select any element. (See figure 2-13, example 2.)
  2056.  
  2057.    ■  Based Indexed Mode. The operand is located within the selected segment
  2058.       at an offset computed as the sum of the base register's contents and an
  2059.       index register's contents. Based Indexed mode is often used to access
  2060.       elements of a dynamic array (i.e., an array whose base address can
  2061.       change during execution). The base register points to the base of the
  2062.       array, and the value of the index register is used to select one
  2063.       element. (See figure 2-13, example 3.)
  2064.  
  2065.    ■  Based Indexed Mode with Displacement. The operand is located with the
  2066.       selected segment at an offset computed as the sum of a base register's
  2067.       contents, an index register's contents, and the displacement. This mode
  2068.       is often used to access elements of an array within a structure. For
  2069.       example, the structure could be an activation record (i.e., a region
  2070.       of the stack containing the register contents, parameters, and
  2071.       variables associated with one instance of a procedure); and one
  2072.       variable could be an array. The base register points to the start of
  2073.       the activation record, the displacement expresses the distance from the
  2074.       start of the record to the beginning of the array variable, and the
  2075.       index register selects a particular element of the array. (See figure
  2076.       2-13, example 4.)
  2077.  
  2078.  Table 2-3 gives a summary of all memory operand addressing options.
  2079.  
  2080.  
  2081.  Table 2-3. Memory Operand Addressing Modes
  2082.  
  2083.  Addressing Mode                  Offset Calculation
  2084.  
  2085.  Direct                           16-bit Displacement in the instruction
  2086.  Register Indirect                BX, SI, DI
  2087.  Based                            (BX or BP) + Displacement
  2088.  Indexed                          (SI or DI) + Displacement
  2089.  Based Indexed                    (BX or BP) + (SI or DI)
  2090.  Based Indexed + Displacement     (BX or BP) + (SI or DI) + Displacement
  2091.  
  2092.  
  2093.  Figure 2-13. Complex Addressing Modes
  2094.  
  2095.  1. BASED MODE                         2. INDEXED MODE
  2096.  
  2097.  MOV AX, [BP + DATE_CODE]              MOV ID[SI], DX
  2098.  ADD[BX + BALANCE], CX                 SUB BX, DATA_TBL[SI]
  2099.  
  2100.                                                                      F
  2101.                    ╠═══════════╣─┐                       ╠═══════════╣─┐ I
  2102.                    ║           ║ │                       ║           ║ │ X
  2103.  ╔═══════════╗     ╟───────────╢ │     ╔═══════════╗     ╟───────────╢ │ E
  2104.  ║   DISPL   ╟────║  OPERAND  ║ ├─    ║   INDEX   ╟────║  OPERAND  ║ ├─D
  2105.  ╚═══════════╝     ╟───────────╢ │     ╚═══════════╝     ╟───────────╢ │
  2106.        +           ║           ║ │           +           ║           ║ │ A
  2107.  ╔═══════════╗ ┌──╟───────────╢─┘     ╔═══════════╗ ┌──╟───────────╢─┘ R
  2108.  ║   BASE    ╟─┘   ║           ║       ║   DISPL   ╟─┘   ║           ║   R
  2109.  ╚═══════════╝     ║           ║       ╚═══════════╝     ║           ║   A
  2110.        +           ║           ║             +           ║           ║   Y
  2111.  ╔═══════════╗     ║           ║       ╔═══════════╗     ║           ║
  2112.  ║  SEGMENT  ╟────╚═══════════╝       ║  SEGMENT  ╟────╚═══════════╝
  2113.  ╚═══════════╝                         ╚═══════════╝
  2114.  
  2115.  3. BASED INDEXED                      4. BASED INDEXED MODE
  2116.                                           WITH DISPLACEMENT             BASED
  2117.  MOV DX, [BP][DI]                                                   STRUCTURE
  2118.  AND [BX + SI], 3FFH                   MOV CX, [BP][SI + CNT]      CONTAINING
  2119.                                        SHR[BX + DI + MASK]              ARRAY
  2120.                                  B                                    └─┐
  2121.                    ╠═══════════╣─┐ A                     ╠═══════════╣   ──┐│
  2122.                    ║           ║ │ S                     ║           ║     ││
  2123.  ╔═══════════╗     ╟───────────╢ │ E   ╔═══════════╗     ╟───────────╢─┐   ││
  2124.  ║   INDEX   ╟────║  OPERAND  ║ ├─D   ║   INDEX   ╟───┐ ║▒▒▒▒▒▒▒▒▒▒▒║ │ A ││
  2125.  ╚═══════════╝     ╟───────────╢ │     ╚═══════════╝   │ ╟───────────╢ │ R ││
  2126.        +           ║           ║ │ A         +         └║  OPERAND  ║ ├─R ├┘
  2127.  ╔═══════════╗ ┌──╟───────────╢─┘ R   ╔═══════════╗ ┌──╟───────────╢ │ A │
  2128.  ║   BASE    ╟─┘   ║           ║   R   ║   DISPL   ╟─┘   ║▒▒▒▒▒▒▒▒▒▒▒║ │ Y │
  2129.  ╚═══════════╝     ║           ║   A   ╚═══════════╝   ┌╟───────────╢─┘   │
  2130.        +           ║           ║   Y         +         │ ║           ║     │
  2131.  ╔═══════════╗     ║           ║       ╔═══════════╗   │ ╟───────────╢   ──┘
  2132.  ║  SEGMENT  ╟────╚═══════════╝       ║   BASE    ╟───┘ ║           ║
  2133.  ╚═══════════╝                         ╚═══════════╝     ║           ║
  2134.                                              +           ║           ║
  2135.                                        ╔═══════════╗     ║           ║
  2136.                                        ║  SEGMENT  ╟────╚═══════════╝
  2137.                                        ╚═══════════╝
  2138.  
  2139.  
  2140.  2.5  Input/Output
  2141.  
  2142.  The 80286 allows input/output to be performed in either of two ways: by
  2143.  means of a separate I/O address space (using specific I/O instructions) or
  2144.  by means of memory-mapped I/O (using general-purpose operand manipulation
  2145.  instructions).
  2146.  
  2147.  
  2148.  2.5.1  I/O Address Space
  2149.  
  2150.  The 80286 provides a separate I/O address space, distinct from physical
  2151.  memory, to address the input/output ports that are used for external
  2152.  devices. The I/O address space consists of 2^(16) (64K) individually
  2153.  addressable 8-bit ports. Any two consecutive 8-bit ports can be treated as
  2154.  a 16-bit port. Thus, the I/O address space can accommodate up to 64K 8-bit
  2155.  ports or up to 32K 16-bit ports. I/O port addresses 00F8H to 00FFH are
  2156.  reserved by Intel.
  2157.  
  2158.  The 80286 can transfer either 8 or 16 bits at a time to a device located in
  2159.  the I/O space. Like words in memory, 16-bit ports should be aligned at
  2160.  even-numbered addresses so that the 16 bits will be transferred in a single
  2161.  access. An 8-bit port may be located at either an even or odd address. The
  2162.  internal registers in a given peripheral controller device should be
  2163.  assigned addresses as shown below.
  2164.  
  2165.  Port Register              Port Addresses            Example
  2166.  
  2167.  16-bit                     even word addresses       OUT  FE,AX
  2168.  8-bit; device on
  2169.  lower half of 16-bit
  2170.  
  2171.  data bus                   even byte addresses       IN  AL,FE
  2172.  
  2173.  8-bit; device on upper
  2174.  half of 16-bit data bus    odd byte addresses        OUT  FF,AL
  2175.  
  2176.  The I/O instructions IN and OUT (described in section 3.11.3) are provided
  2177.  to move data between I/O ports and the AX (16-bit I/O) or AL (8-bit I/O)
  2178.  general registers. The block I/O instructions INS and OUTS (described in
  2179.  section 4.1) move blocks of data between I/O ports and memory space (as
  2180.  shown below). In Protected Mode, an operating system may prevent a program
  2181.  from executing these I/O instructions. Otherwise, the function of the I/O
  2182.  instructions and the structure of the I/O space are identical for both modes
  2183.  of operation.
  2184.  
  2185.  INS  es:byte ptr [di], DX
  2186.  OUTS DX, byte ptr [si]
  2187.  
  2188.  IN and OUT instructions address I/O with either a direct address to one of
  2189.  up to 256 port addresses, or indirectly via the DX register to one of up to
  2190.  64K port addresses. Block I/O uses the DX register to specify the I/O
  2191.  address and either SI or DI to designate the source or destination memory
  2192.  address. For each transfer, SI or DI are either incremented or decremented
  2193.  as specified by the direction bit in the flag word while DX is constant to
  2194.  select the I/O device.
  2195.  
  2196.  
  2197.  2.5.2  Memory-Mapped I/O
  2198.  
  2199.  I/O devices also may be placed in the 80286 memory address space. So long
  2200.  as the devices respond like memory components, they are indistinguishable to
  2201.  the processor.
  2202.  
  2203.  Memory-mapped I/O provides additional programming flexibility. Any
  2204.  instruction that references memory may be used to access an I/O port located
  2205.  in the memory space. For example, the MOV instruction can transfer data
  2206.  between any register and a port; and the AND, OR, and TEST instructions may
  2207.  be used to manipulate bits in the internal registers of a device (see
  2208.  figure 2-14). Memory-mapped I/O performed via the full instruction set
  2209.  maintains the full complement of addressing modes for selecting the desired
  2210.  I/O device.
  2211.  
  2212.  Memory-mapped I/O, like any other memory reference, is subject to access
  2213.  protection and control when executing in protected mode.
  2214.  
  2215.  
  2216.  Figure 2-14.  Memory-Mapped I/O
  2217.  
  2218.                  MEMORY
  2219.               ADDRESS SPACE
  2220.            ╔════════════════╗                     I/O DEVICE 1
  2221.            ║                ║                ╔═════════════════════╗
  2222.            ║                ║                ║  INTERNAL REGISTER  ║
  2223.            ╟────────────────╢─ ── ─ ── ─ ── ─║─╔═════════════════╗ ║
  2224.            ║                ║                ║ ║                 ║ ║
  2225.            ╟────────────────╢─ ── ─ ── ─ ── ─║─╚═════════════════╝ ║
  2226.            ║                ║                ╚═════════════════════╝
  2227.            ║                ║
  2228.            ║                ║                     I/O DEVICE 2
  2229.            ║                ║                ╔═════════════════════╗
  2230.            ║                ║                ║  INTERNAL REGISTER  ║
  2231.            ║────────────────║─ ── ─ ── ─ ── ─║─╔═════════════════╗ ║
  2232.            ║                ║                ║ ║                 ║ ║
  2233.            ║────────────────║─ ── ─ ── ─ ── ─║─╚═════════════════╝ ║
  2234.            ║                ║                ╚═════════════════════╝
  2235.            ║                ║
  2236.            ╚════════════════╝
  2237.  
  2238.  
  2239.  2.6  Interrupts and Exceptions
  2240.  
  2241.  The 80286 architecture supports several mechanisms for interrupting program
  2242.  execution. Internal interrupts are synchronous events that are the responses
  2243.  of the CPU to certain events detected during the execution of an
  2244.  instruction. External interrupts are asynchronous events typically
  2245.  triggered by external devices needing attention. The 80286 supports both
  2246.  maskable (controlled by the IF flag) and non-maskable interrupts. They cause
  2247.  the processor to temporarily suspend its present program execution in order
  2248.  to service the requesting device. The major distinction between these two
  2249.  kinds of interrupts is their origin: an internal interrupt is always
  2250.  reproducible by re-executing with the program and data that caused the
  2251.  interrupt, whereas an external interrupt is generally independent of the
  2252.  currently executing task.
  2253.  
  2254.  Interrupts 0-31 are reserved by Intel.
  2255.  
  2256.  Application programmers will normally not be concerned with servicing
  2257.  external interrupts. More information on external interrupts for system
  2258.  programmers may be found in Chapter 5, section 5.2, "Interrupt Handling for
  2259.  Real Address Mode," and in Chapter 9, "Interrupts, Traps and Faults for
  2260.  Protected Virtual Address Mode."
  2261.  
  2262.  In Real Address Mode, the application programmer is affected by two kinds
  2263.  of internal interrupts. (Internal interrupts are the result of executing an
  2264.  instruction which causes the interrupt.) One type of interrupt is called an
  2265.  exception because the interrupt only occurs if a particular fault condition
  2266.  exists. The other type of interrupt generates the interrupt every time the
  2267.  instruction is executed.
  2268.  
  2269.  The exceptions are: divide error, INTO detected overflow, bounds check,
  2270.  segment overrun, invalid operation code, and processor extension error (see
  2271.  table 2-4). A divide error exception results when the instructions DIV or
  2272.  IDIV are executed with a zero denominator; otherwise, the quotient will be
  2273.  too large for the destination operand (see section 3.3.4 for a discussion
  2274.  of DIV and IDIV). An overflow exception results when the INTO instruction is
  2275.  executed and the OF flag is set (after an arithmetic operation that set the
  2276.  overflow (OF) flag). (See section 3.6.3, "Software Generated Interrupts,"
  2277.  for a discussion of INTO.) A bounds check exception results when the BOUND
  2278.  instruction is executed and the array index it checks falls outside the
  2279.  bounds of the array. (See section 4.2 for a discussion of the BOUND
  2280.  instruction.) The segment overrun exception occurs when a word memory
  2281.  reference is attempted which extends beyond the end of a segment. An invalid
  2282.  operation code exception occurs if an attempt is made to execute an
  2283.  undefined instruction operation code. A processor extension error is
  2284.  generated when a processor extension detects an illegal operation. Refer to
  2285.  Chapter 5 for a more complete description of these exception conditions.
  2286.  
  2287.  The instruction INT generates an internal interrupt whenever it is
  2288.  executed. The effects of this interrupt (and the effects of all interrupts)
  2289.  is determined by the interrupt handler routines provided by the application
  2290.  program or as part of the system software (provided by system programmers).
  2291.  See Chapter 5 for more on this topic. The INT instruction itself is
  2292.  discussed in section 3.6.3.
  2293.  
  2294.  In Protected Mode, many more fault conditions are detected and result in
  2295.  internal interrupts. Protected Mode interrupts and faults are discussed in
  2296.  Chapter 9.
  2297.  
  2298.  
  2299.  2.7  Hierarchy of Instruction Sets
  2300.  
  2301.  For descriptive purposes, the 80286 instruction set is partitioned into
  2302.  three distinct subsets: the Basic Instruction Set, the Extended Instruction
  2303.  Set, and the System Control Instruction Set. The "hierarchy" of instruction
  2304.  sets defined by this partitioning helps to clarify the relationships
  2305.  between the various processors in the 8086 family (see figure 2-15).
  2306.  
  2307.  The Basic Instruction Set, presented in Chapter 3, comprises the common
  2308.  subset of instructions found on all processors of the 8086 family. Included
  2309.  are instructions for logical and arithmetic operations, data movement,
  2310.  input/output, string manipulation, and transfer of control.
  2311.  
  2312.  The Extended Instruction Set, presented in Chapter 4, consists of those
  2313.  instructions found only on the 80186, 80188, and 80286 processors. Included
  2314.  are instructions for block structured procedure entry and exit, parameter
  2315.  validation, and block I/O transfers.
  2316.  
  2317.  The System Control Instruction Set, presented in Chapter 10, consists of
  2318.  those instructions unique to the 80286. These instructions control the
  2319.  memory management and protection mechanisms of the 80286.
  2320.  
  2321.  
  2322.  Table 2-4. 80286 Interrupt Vector Assignments (Real Address Mode)
  2323.  
  2324. ╓┌──────────────────────────────────┌──────────┌──────────────┌──────────────
  2325.  Function                           Interupt   Related        Return Address
  2326.                                     Number     Instructions   Before Instructi
  2327.                                                               Causing Exceptio
  2328.  Divide error exception             0          DIV, IDIV          Yes
  2329.  Single step interrupt              1          All
  2330.  NMI interrupt                      2          All
  2331.  Breakpoint interrupt               3          INT
  2332.  INTO detected overflow exception   4          INTO               No
  2333.  Function                           Interupt   Related        Return Address
  2334.                                     Number     Instructions   Before Instructi
  2335.                                                               Causing Exceptio
  2336. INTO detected overflow exception   4          INTO               No
  2337.  BOUND range exceeded exception     5          BOUND              Yes
  2338.  Invalid opcode exception           6          Any undefined      Yes
  2339.                                                opcode
  2340.  Processor extension                7          ESC or WAIT        Yes
  2341.  not available exception
  2342.  Interrupt table limit              8          INT vector         Yes
  2343.  too small exception                           is not within
  2344.                                                table limit
  2345.  Processor extension segment        9          ESC with memory    No
  2346.  overrun   interrupt                           operand extending
  2347.                                                beyond offset
  2348.                                                FFFF(H)
  2349.  Reserved                          10-12
  2350.  Segment overrun exception         13          Word memory        Yes
  2351.                                                reference with
  2352.                                                offset = FFFF(H)
  2353.                                                or an attempt to
  2354.  Function                           Interupt   Related        Return Address
  2355.                                     Number     Instructions   Before Instructi
  2356.                                                               Causing Exceptio
  2357.                                               or an attempt to
  2358.                                                execute past the
  2359.                                                end of a segment
  2360.  Reserved                          14, 15
  2361.  Processor extension               16          ESC or WAIT
  2362.  error interrupt
  2363.  Reserved                          17-31
  2364.  User defined                      32-255
  2365.  
  2366.  
  2367.  
  2368.  Figure 2-15.  Hierarchy of Instructions
  2369.  
  2370.               ╔══════════════════════╗
  2371.               ║                      ║
  2372.               ║                      ║
  2373.               ║  ╔════════════════╗  ║
  2374.               ║  ║                ║  ║
  2375.               ║  ║  ╔══════════╗  ║  ║
  2376.               ║  ║  ║   8086  ╫──╫──╫──BASIC INSTRUCTION SET
  2377.               ║  ║  ║   8088   ║  ║  ║
  2378.               ║  ║  ╚══════════╝  ║  ║
  2379.               ║  ║      80186 ───╫──╫──EXTENDED INSTRUCTION SET
  2380.               ║  ║      80188     ║  ║
  2381.               ║  ╚════════════════╝  ║
  2382.               ║         80286 ──────╫──SYSTEM CONTROL INSTRUCTION SET
  2383.               ║                      ║
  2384.               ╚══════════════════════╝
  2385.  
  2386.  
  2387.  Chapter 3  Basic Instruction Set
  2388.  
  2389.  ───────────────────────────────────────────────────────────────────────────
  2390.  
  2391.  The base architecture of the 80286 is identical to the complete instruction
  2392.  set of the 8086, 8088, 80188, and 80186 processors. The 80286 instruction
  2393.  set includes new forms of some instructions. These new forms reduce program
  2394.  size and improve the performance and ease of implementation of source code.
  2395.  
  2396.  This chapter describes the instructions which programmers can use to write
  2397.  application software for the 80286. The following chapters describe the
  2398.  operation of more complicated I/O and system control instructions.
  2399.  
  2400.  All instructions described in this chapter are available for both Real
  2401.  Address Mode and Protected Virtual Address Mode operation. The instruction
  2402.  descriptions note any differences that exist between the operation of an
  2403.  instruction in these two modes.
  2404.  
  2405.  This chapter also describes the operation of each application
  2406.  program-relative instruction and includes an example of using the
  2407.  instruction. The Instruction Dictionary in Appendix B contains formal
  2408.  descriptions of all instructions. Any opcode pattern that is not described
  2409.  in the Instruction Dictionary is undefined and results in an opcode
  2410.  violation trap (interrupt 6).
  2411.  
  2412.  
  2413.  3.1  Data Movement Instructions
  2414.  
  2415.  These instructions provide convenient methods for moving bytes or words of
  2416.  data between memory and the registers of the base architecture.
  2417.  
  2418.  
  2419.  3.1.1  General-Purpose Data Movement Instructions
  2420.  
  2421.  MOV (Move) transfers a byte or a word from the source operand to the
  2422.  destination operand. The MOV instruction is useful for transferring data to
  2423.  a register from memory, to memory from a register, between registers,
  2424.  immediate-to-register, or immediate-to-memory. Memory-to-memory or segment
  2425.  register-to-segment register moves are not allowed.
  2426.  
  2427.  Example:
  2428.    MOV DS,AX. Replaces the contents of register DS with the contents of
  2429.    register AX.
  2430.  
  2431.  XCHG (Exchange) swaps the contents of two operands. This instruction takes
  2432.  the place of three MOV instructions. It does not require a temporary memory
  2433.  location to save the contents of one operand while you load the other.
  2434.  
  2435.  The XCHG instruction can swap two byte operands or two word operands, but
  2436.  not a byte for a word or a word for a byte. The operands for the XCHG
  2437.  instruction may be two register operands, or a register operand with a
  2438.  memory operand. When used with a memory operand, XCHG automatically
  2439.  activates the LOCK signal.
  2440.  
  2441.  Example:
  2442.    XCHG BX,WORDOPRND. Swaps the contents of register BX with the contents
  2443.    of the memory word identified by the label WORDOPRND after asserting
  2444.    bus lock.
  2445.  
  2446.  
  2447.  3.1.2  Stack Manipulation Instructions
  2448.  
  2449.  PUSH (Push) decrements the stack pointer (SP) by two and then transfers a
  2450.  word from the source operand to the top of stack indicated by SP. See figure
  2451.  3-1. PUSH is often used to place parameters on the stack before calling a
  2452.  procedure; it is also the basic means of storing temporary variables on the
  2453.  stack. The PUSH instruction operates on memory operands, immediate operands
  2454.  (new with the 80286), and register operands (including segment registers).
  2455.  
  2456.  Example:
  2457.    PUSH WORDOPRND. Transfers a 16-bit value from the memory word identified
  2458.    by the label WORDOPRND to the memory location which represents the current
  2459.    top of stack (byte transfers are not allowed).
  2460.  
  2461.  PUSHA (Push All Registers) saves the contents of the eight general
  2462.  registers on the stack. See figure 3-2. This instruction simplifies
  2463.  procedure calls by reducing the number of instructions required to retain
  2464.  the contents of the general registers for use in a procedure. PUSHA is
  2465.  complemented by POPA (see below).
  2466.  
  2467.  The processor pushes the general registers on the stack in the following
  2468.  order: AX, CX, DX, BX, the initial value of SP before AX was pushed, BP, SI,
  2469.  and DI.
  2470.  
  2471.  Example:
  2472.    PUSHA. Pushes onto the stack the contents of the eight general registers.
  2473.  
  2474.  POP (Pop) transfers the word at the current top of stack (indicated by SP)
  2475.  to the destination operand, and then increments SP by two to point to the
  2476.  new top of stack. See figure 3-3. POP moves information from the stack to
  2477.  either a register or memory. The only restriction on POP is that it cannot
  2478.  place a value in register CS.
  2479.  
  2480.  Example:
  2481.    POP BX. Replaces the contents of register BX with the contents of the
  2482.    memory location at the top of stack.
  2483.  
  2484.  POPA (Pop All Registers) restores the registers saved on the stack by
  2485.  PUSHA, except that it ignores the value of SP. See figure 3-4.
  2486.  
  2487.  Example:
  2488.    POPA. Pops from the stack the saved contents of the general registers,
  2489.    and restores the registers (except SP) to their original state.
  2490.  
  2491.  
  2492.  Figure 3-1.  PUSH
  2493.  
  2494.                                                       
  2495.  HIGH ADDRESS       ║               ║    ║               ║
  2496.                     ╠═══════════════╣    ╠═══════════════╣  SS LIMIT
  2497.  OPERANDS FROM      ║▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒║    ║▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒║
  2498.  PREVIOUS PUSH      ╠═══════════════╣    ╠═══════════════╣
  2499.  INSTRUCTIONS   SP─║▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒║    ║▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒║
  2500.                     ╠═══════════════╣    ╠═══════════════╣  SP ALWAYS POINTS
  2501.                     ║               ║    ║    OPERAND    ║─TO THE LAST WORD
  2502.                     ╠═══════════════╣    ╠═══════════════╣  PUSHED ONTO THE
  2503.                     ║               ║    ║               ║  STACK (TOS)
  2504.                     ╠═══════════════╣    ╠═══════════════╣
  2505.                     ≈               ≈    ≈               ≈
  2506.                     ╠═══════════════╣    ╠═══════════════╣
  2507.                     ║               ║    ║               ║  SS ALWAYS POINTS
  2508.      LOW ADDRESS    ╠═══════════════╣    ╠═══════════════╣  TO LOWEST ADDRESS
  2509.                     ║               ║    ║               ║  USED BY THE STACK
  2510.                      BEFORE             AFTER         
  2511.                       PUSH OPERAND         PUSH OPERAND
  2512.  
  2513.   PUSH decrements SP by 2 bytes and places the operand in the stack at the
  2514.   location to which SP points.
  2515.  
  2516.  
  2517.  Figure 3-2.  PUSHA
  2518.  
  2519.                                                        
  2520.     HIGH ADDRESS     ║               ║    ║               ║
  2521.                      ╠═══════════════╣    ╠═══════════════╣    SS LIMIT
  2522.  OPERANDS FROM       ║▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒║    ║▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒║
  2523.  PREVIOUS PUSH       ╠═══════════════╣    ╠═══════════════╣
  2524.  INSTRUCTIONS    ┌──║▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒║    ║▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒║
  2525.                  │   ╠═══════════════╣    ╠═══════════════╣
  2526.              SP──┘   ║               ║    ║      AX       ║
  2527.                      ╠═══════════════╣    ╠═══════════════╣
  2528.                      ║               ║    ║      CX       ║
  2529.                      ╠═══════════════╣    ╠═══════════════╣
  2530.                      ║               ║    ║      DX       ║
  2531.                      ╠═══════════════╣    ╠═══════════════╣
  2532.                      ║               ║    ║      BX       ║
  2533.                      ╠═══════════════╣    ╠═══════════════╣
  2534.                      ║               ║    ║    OLD SP     ║
  2535.                      ╠═══════════════╣    ╠═══════════════╣
  2536.                      ║               ║    ║      BP       ║
  2537.                      ╠═══════════════╣    ╠═══════════════╣
  2538.                      ║               ║    ║      SI       ║
  2539.                      ╠═══════════════╣    ╠═══════════════╣
  2540.                      ║               ║    ║      DI       ║───SP
  2541.                      ╠═══════════════╣    ╠═══════════════╣
  2542.                      ║               ║    ║               ║
  2543.                      ╠═══════════════╣    ╠═══════════════╣
  2544.                      ≈               ≈    ≈               ≈
  2545.                      ╠═══════════════╣    ╠═══════════════╣
  2546.                      ║               ║    ║               ║
  2547.                      ╠═══════════════╣    ╠═══════════════╣
  2548.                      ║               ║    ║               ║
  2549.                      ╠═══════════════╣    ╠═══════════════╣
  2550.                      ║               ║    ║               ║
  2551.      LOW ADDRESS     ╠═══════════════╣    ╠═══════════════╣   SS
  2552.                      ║               ║    ║               ║
  2553.                                                        
  2554.                        BEFORE               AFTER
  2555.                        PUSHA                PUSHA
  2556.  
  2557.   PUSHA copies the contents of the eight general registers to the stack in
  2558.   the above order. The instruction decrements SP by 16 bytes (8 words) to
  2559.   point to the last word pushed on the stack.
  2560.  
  2561.  
  2562.  Figure 3-3.  POP
  2563.  
  2564.                                                         
  2565.      HIGH ADDRESS     ║               ║    ║               ║
  2566.                       ╠═══════════════╣    ╠═══════════════╣    SS LIMIT
  2567.   OPERANDS FROM       ║▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒║    ║▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒║
  2568.   PREVIOUS PUSH       ╠═══════════════╣    ╠═══════════════╣
  2569.   INSTRUCTIONS        ║▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒║    ║▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒║───SP
  2570.                       ╠═══════════════╣    ╠═══════════════╣
  2571.                 SP───║    OPERAND    ║    ║               ║
  2572.                       ╠═══════════════╣    ╠═══════════════╣
  2573.                       ║               ║    ║               ║
  2574.                       ╠═══════════════╣    ╠═══════════════╣
  2575.                       ≈               ≈    ≈               ≈
  2576.                       ╠═══════════════╣    ╠═══════════════╣
  2577.                       ║               ║    ║               ║
  2578.       LOW ADDRESS     ╠═══════════════╣    ╠═══════════════╣   SS
  2579.                       ║               ║    ║               ║
  2580.                        BEFORE             AFTER         
  2581.                         POP OPERAND          POP OPERAND
  2582.  
  2583.   POP copies the contents of the stack location before SP to the operand in
  2584.   the instruction. POP then increments SP by 2 bytes (1 word).
  2585.  
  2586.  Figure 3-4.  POPA
  2587.  
  2588.                                                         
  2589.      HIGH ADDRESS     ║               ║    ║               ║
  2590.                       ╠═══════════════╣    ╠═══════════════╣    SS LIMIT
  2591.   OPERANDS FROM       ║▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒║    ║▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒║
  2592.   PREVIOUS PUSH       ╠═══════════════╣    ╠═══════════════╣
  2593.   INSTRUCTIONS        ║▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒║    ║▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒║───SP
  2594.                       ╠═══════════════╣    ╠═══════════════╣
  2595.                       ║      AX       ║    ║               ║
  2596.                       ╠═══════════════╣    ╠═══════════════╣
  2597.                       ║      CX       ║    ║               ║
  2598.                       ╠═══════════════╣    ╠═══════════════╣
  2599.                       ║      DX       ║    ║               ║
  2600.                       ╠═══════════════╣    ╠═══════════════╣
  2601.                       ║      BX       ║    ║               ║
  2602.                       ╠═══════════════╣    ╠═══════════════╣
  2603.                       ║      SP       ║    ║               ║
  2604.                       ╠═══════════════╣    ╠═══════════════╣
  2605.                       ║      BP       ║    ║               ║
  2606.                       ╠═══════════════╣    ╠═══════════════╣
  2607.                       ║      SI       ║    ║               ║
  2608.                       ╠═══════════════╣    ╠═══════════════╣
  2609.                 SP───║      DI       ║    ║               ║
  2610.                       ╠═══════════════╣    ╠═══════════════╣
  2611.                       ║               ║    ║               ║
  2612.                       ╠═══════════════╣    ╠═══════════════╣
  2613.                       ≈               ≈    ≈               ≈
  2614.                       ╠═══════════════╣    ╠═══════════════╣
  2615.                       ║               ║    ║               ║
  2616.                       ╠═══════════════╣    ╠═══════════════╣
  2617.                       ║               ║    ║               ║
  2618.                       ╠═══════════════╣    ╠═══════════════╣
  2619.                       ║               ║    ║               ║
  2620.      LOW ADDRESS      ╠═══════════════╣    ╠═══════════════╣   SS
  2621.                        BEFORE             AFTER         
  2622.                         POPA                 POPA
  2623.  
  2624.   POPA copies the contents of seven stack locations to the corresponding
  2625.   general registers. POPA discards the stored value of SP.
  2626.  
  2627.  
  2628.  3.2  Flag Operation With the Basic Instruction Set
  2629.  
  2630.  
  2631.  3.2.1  Status Flags
  2632.  
  2633.  The status flags of the FLAGS register reflect conditions that result from
  2634.  a previous instruction or instructions. The arithmetic instructions use OF,
  2635.  SF, ZF, AF, PF, and CF.
  2636.  
  2637.  The SCAS (Scan String), CMPS (Compare String), and LOOP instructions use ZF
  2638.  to signal that their operations are complete. The base architecture includes
  2639.  instructions to set, clear, and complement CF before execution of an
  2640.  arithmetic instruction. See figure 3-5 and tables 3-1 and 3-2.
  2641.  
  2642.  
  2643.  3.2.2  Control Flags
  2644.  
  2645.  The control flags of the FLAGS register determine processor operations for
  2646.  string instructions, maskable interrupts, and debugging.
  2647.  
  2648.  Setting DF (direction flag) causes string instructions to auto-decrement;
  2649.  that is, to process strings from high addresses to low addresses, or from
  2650.  "right-to-left." Clearing DF causes string instructions to auto-increment,
  2651.  or to process strings from "left-to-right."
  2652.  
  2653.  Setting IF (interrupt flag) allows the CPU to recognize external (maskable)
  2654.  interrupt requests. Clearing IF disables these interrupts. IF has no effect
  2655.  on either internally generated interrupts, nonmaskable external interrupts,
  2656.  or processor extension segment overrun interrupts.
  2657.  
  2658.  Setting TF (trap flag) puts the processor into single-step mode for
  2659.  debugging. In this mode, the CPU automatically generates an internal
  2660.  interrupt after each instruction, allowing a program to be inspected as it
  2661.  executes each instruction, instruction by instruction.
  2662.  
  2663.  
  2664.  Table 3-1. Status Flags' Functions
  2665.  
  2666.  Bit Position    Name  Function
  2667.  
  2668.   0              CF    Carry Flag--Set on high-order bit carry or borrow;
  2669.                        cleared otherwise
  2670.  
  2671.   2              PF    Parity Flag--Set if low-order eight bits of result
  2672.                        contain an even number of 1 bits; cleared otherwise
  2673.  
  2674.   4              AF    Set on carry from or borrow to the low order four
  2675.                        bits of AL; cleared otherwise
  2676.  
  2677.   6              ZF    Zero Flag--Set if result is zero; cleared otherwise
  2678.  
  2679.   7              SF    Sign Flag--Set equal to high-order bit of result (0
  2680.                        if positive, 1 if negative)
  2681.  
  2682.   11             OF    Overflow Flag--Set if result is too-large a positive
  2683.                        number or too-small a negative number (excluding
  2684.                        sign-bit) to fit in destination operand; cleared
  2685.                        otherwise
  2686.  
  2687.  
  2688.  Table 3-2. Control Flags' Functions
  2689.  
  2690.  Bit Position   Name   Function
  2691.  
  2692.   8             TF     Trap (Single Step) Flag--Once set, a single step
  2693.                        interrupt occurs after the next instruction executes.
  2694.                        TF is cleared by the single step interrupt.
  2695.  
  2696.   9             IF     Interrupt-enable Flag--When set, maskable interrupts
  2697.                        will cause the CPU to transfer control to an interrupt
  2698.                        vector-specified location.
  2699.  
  2700.  10             DF     Direction Flag--Causes string instructions to auto
  2701.                        decrement the appropriate index registers when set.
  2702.                        Clearing DF causes auto increment.
  2703.  
  2704.  
  2705.  Figure 3-5.  Flag Word Contents
  2706.  
  2707.      STATUS FLAGS:
  2708.       CARRY────────────────────────────────────────────────┐
  2709.       PARITY─────────────────────────────────────────┐     │
  2710.       AUXILLIARY CARRY─────────────────────────┐     │     │
  2711.       ZERO───────────────────────────────┐     │     │     │
  2712.       SIGN────────────────────────────┐  │     │     │     │
  2713.       OVERFLOW────────────┐           │  │     │     │     │
  2714.                           │           │  │     │     │     │
  2715.                15 14 13 1211 10  9  8 7 6  5 4  3 2  1 0
  2716.              ╔══╤══╤══╤══╤══╤══╤══╤══╤══╤══╤══╤══╤══╤══╤══╤══╗
  2717.        FLAGS:║▒▒│NT│IOPL │OF│DF│IF│TF│SF│ZF│▒▒│AF│▒▒│PF│▒▒│CF║
  2718.              ╚══╧══╧══╧══╧══╧══╧══╧══╧══╧══╧══╧══╧══╧══╧══╧══╝
  2719.                                 
  2720.                   │   │       │  │  │          CONTROL FLAGS:
  2721.                   │   │       │  │  └───────────TRAP FLAG
  2722.                   │   │       │  └──────────────INTERRUPT ENABLE
  2723.                   │   │       └─────────────────DIRECTION FLAG
  2724.                   │   │                        SPECIAL FIELDS:
  2725.                   │   └─────────────────────────I/O PRIVILEGE LEVEL
  2726.                   └─────────────────────────────NESTED TASK FLAG
  2727.  
  2728.  
  2729.  3.3  Arithmetic Instructions
  2730.  
  2731.  The arithmetic instructions of the 8086-family processors simplify the
  2732.  manipulation of numerical data. Multiplication and division instructions
  2733.  ease the handling of signed and unsigned binary integers as well as unpacked
  2734.  decimal integers.
  2735.  
  2736.  An arithmetic operation may consist of two register operands, a general
  2737.  register source operand with a memory destination operand, a memory source
  2738.  operand with a register destination operand, or an immediate field with
  2739.  either a register or memory destination operand, but not two memory
  2740.  operands. Arithmetic instructions can operate on either byte or word
  2741.  operands.
  2742.  
  2743.  
  2744.  3.3.1  Addition Instructions
  2745.  
  2746.  ADD (Add Integers) replaces the destination operand with the sum of the
  2747.  source and destination operands. ADD affects OF, SF, AF, PF, CF, and ZF.
  2748.  
  2749.  Example:
  2750.    ADD BL, BYTEOPRND. Adds the contents of the memory byte labeled
  2751.    BYTEOPRND to the contents of BL, and replaces BL with the resulting sum.
  2752.  
  2753.  ADC (Add Integers with Carry) sums the operands, adds one if CF is set, and
  2754.  replaces the destination operand with the result. ADC can be used to add
  2755.  numbers longer than 16 bits. ADC affects OF, SF, AF, PF, CF, and ZF.
  2756.  
  2757.  Example:
  2758.    ADC BX, CX. Replaces the contents of the destination operand BX with
  2759.    the sum of BX, CS, and 1 (if CF is set). If CF is cleared, ADC
  2760.    performs the same operation as the ADD instruction.
  2761.  
  2762.  INC (Increment) adds one to the destination operand. The processor treats
  2763.  the operand as an unsigned binary number. INC updates AF, OF, PF, SF, and
  2764.  ZF, but it does not affect CF. Use ADD with an immediate value of 1 if an
  2765.  increment that updates carry (CF) is needed.
  2766.  
  2767.  Example:
  2768.    INC BL. Adds 1 to the contents of BL.
  2769.  
  2770.  
  2771.  3.3.2  Subtraction Instructions
  2772.  
  2773.  SUB (Subtract Integers) subtracts the source operand from the destination
  2774.  operand and replaces the destination operand with the result. If a borrow is
  2775.  required, carry flag is set. The operands may be signed or unsigned bytes or
  2776.  words. SUB affects OF, SF, ZF, AF, PF, and CF.
  2777.  
  2778.  Example:
  2779.    SUB WORDOPRND, AX. Replaces the contents of the destination operand
  2780.    WORDOPRND with the result obtained by subtracting the contents of AX from
  2781.    the contents of the memory word labeled WORDOPRND.
  2782.  
  2783.  SBB (Subtract Integers with Borrow) subtracts the source operand from the
  2784.  destination operand, subtracts 1 if CF is set, and returns the result to the
  2785.  destination operand. The operands may be signed or unsigned bytes or words.
  2786.  SBB may be used to subtract numbers longer than 16 bits. This instruction
  2787.  affects OF, SF, ZF, AF, PF, and CF. The carry flag is set if a borrow is
  2788.  required.
  2789.  
  2790.  Example:
  2791.    SBB BL, 32. Subtracts 32 from the contents of BL and then decrements the
  2792.    result of this subtraction by one if CF is set. If CF is cleared, SBB
  2793.    performs the same operation as SUB.
  2794.  
  2795.  DEC (Decrement) subtracts 1 from the destination operand. DEC updates AF,
  2796.  OF, PF, SF, and ZF, but it does not affect CF. Use SUB with an immediate
  2797.  value of 1 to perform a decrement that affects carry.
  2798.  
  2799.  Example:
  2800.    DEC BX. Subtracts 1 from the contents of BX and places the result back in
  2801.    BX.
  2802.  
  2803.  
  2804.  3.3.3  Multiplication Instructions
  2805.  
  2806.  MUL (Unsigned Integer Multiply) performs an unsigned multiplication of the
  2807.  source operand and the accumulator. If the source is a byte, the processor
  2808.  multiplies it by the contents of AL and returns the double-length result to
  2809.  AH and AL.
  2810.  
  2811.  If the source operand is a word, the processor multiplies it by the
  2812.  contents of AX and returns the double-length result to DX and AX. MUL sets
  2813.  CF and OF to indicate that the upper half of the result is nonzero;
  2814.  otherwise, they are cleared. This instruction leaves SF, ZF, AF, and PF
  2815.  undefined.
  2816.  
  2817.  Example:
  2818.    MUL BX. Replaces the contents of DX and AX with the product of BX and AX.
  2819.    The low-order 16 bits of the result replace the contents of AX; the
  2820.    high-order word goes to DX. The processor sets CF and OF if the unsigned
  2821.    result is greater than 16 bits.
  2822.  
  2823.  IMUL (Signed Integer Multiply) performs a signed multiplication operation.
  2824.  IMUL uses AX and DX in the same way as the MUL instruction, except when used
  2825.  in the immediate form.
  2826.  
  2827.  The immediate form of IMUL allows the specification of a destination
  2828.  register other than the combination of DX and AX. In this case, the result
  2829.  cannot exceed 16 bits without causing an overflow. If the immediate operand
  2830.  is a byte, the processor automatically extends it to 16 bits before
  2831.  performing the multiplication.
  2832.  
  2833.  The immediate form of IMUL may also be used with unsigned operands because
  2834.  the low 16 bits of a signed or unsigned multiplication of two 16-bit values
  2835.  will always be the same.
  2836.  
  2837.  IMUL clears CF and OF to indicate that the upper half of the result is the
  2838.  sign of the lower half. This instruction leaves SF, ZF, AF, and PF
  2839.  undefined.
  2840.  
  2841.  Example:
  2842.    IMUL BL. Replaces the contents of AX with the product of BL and AL. The
  2843.    processor sets CF and OF if the result is more than 8 bits long.
  2844.  
  2845.  Example:
  2846.    IMUL BX, SI, 5. Replaces the contents of BX with the product of the
  2847.    contents of SI and an immediate value of 5. The processor sets CF and OF
  2848.    if the signed result is longer than 16 bits.
  2849.  
  2850.  
  2851.  3.3.4  Division Instructions
  2852.  
  2853.  DIV (Unsigned Integer Divide) performs an unsigned division of the
  2854.  accumulator by the source operand. If the source operand is a byte, it is
  2855.  divided into the double-length dividend assumed to be in registers AL and AH
  2856.  (AH = most significant byte; AL = least significant byte). The
  2857.  single-length quotient is returned in AL, and the single-length remainder is
  2858.  returned in AH.
  2859.  
  2860.  If the source operand is a word, it is divided into the double-length
  2861.  dividend in registers AX and DX. The single-length quotient is returned in
  2862.  AX, and the single-length remainder is returned in DX. Non-integral
  2863.  quotients are truncated to integers toward 0. The remainder is always less
  2864.  than the quotient.
  2865.  
  2866.  For unsigned byte division, the largest quotient is 255. For unsigned word
  2867.  division, the largest quotient is 65,535. DIV leaves OF, SF, ZF, AF, PF, and
  2868.  CF undefined. Interrupt (INT 0) occurs if the divisor is zero or if the
  2869.  quotient is too large for AL or AX.
  2870.  
  2871.  Example:
  2872.    DIV BX. Replaces the contents of AX with the unsigned quotient of the
  2873.    doubleword value contained in DX and AX, divided by BX. The unsigned
  2874.    modulo replaces the contents of DX.
  2875.  
  2876.  Example:
  2877.    DIV BL. Replaces the contents of AL with the unsigned quotient of the
  2878.    word value in AX, divided by BL. The unsigned modulo replaces the
  2879.    contents of AH.
  2880.  
  2881.  IDIV (Signed Integer Divide) performs a signed division of the accumulator
  2882.  by the source operand. IDIV uses the same registers as the DIV instruction.
  2883.  
  2884.  For signed byte division, the maximum positive quotient is +127 and the
  2885.  minimum negative quotient is -128. For signed word division, the maximum
  2886.  positive quotient is +32,767 and the minimum negative quotient is -32,768.
  2887.  Non-integral results are truncated towards 0. The remainder will always
  2888.  have the same sign as the dividend and will be less than the divisor in
  2889.  magnitude. IDIV leaves OF, SF, ZF, AF, PF, and CF undefined. A division by
  2890.  zero causes an interrupt (INT 0) to occur if the divisor is 0 or if the
  2891.  quotient is too large for AL or AX.
  2892.  
  2893.  Example:
  2894.    IDIV WORDOPRND. Replaces the contents of AX with the signed quotient
  2895.    of the double-word value contained in DX and AX, divided by the value
  2896.    contained in the memory word labeled WORDOPRND. The signed modulo
  2897.    replaces the contents of DX.
  2898.  
  2899.  
  2900.  3.4  Logical Instructions
  2901.  
  2902.  The group of logical instructions includes the Boolean operation
  2903.  instructions, rotate and shift instructions, type conversion instructions,
  2904.  and the no-operation (NOP)instruction.
  2905.  
  2906.  
  2907.  3.4.1  Boolean Operation Instructions
  2908.  
  2909.  Except for the NOT and NEG instructions, the Boolean operation instructions
  2910.  can use two register operands, a general purpose register operand with a
  2911.  memory operand, an immediate operand with a general purpose register
  2912.  operand, or a memory operand. The NOT and NEG instructions are unary
  2913.  operations that use a single operand in a register or memory.
  2914.  
  2915.  AND (And) performs the logical "and" of the operands (byte or word) and
  2916.  returns the result to the destination operand. AND clears OF and DF, leaves
  2917.  AF undefined, and updates SF, ZF, and PF.
  2918.  
  2919.  Example:
  2920.    AND WORDOPRND, BX. Replaces the contents of WORDOPRND with the logical
  2921.    "and" of the contents of the memory word labeled WORDOPRND and the
  2922.    contents of BX.
  2923.  
  2924.  NOT (Not) inverts the bits in the specified operand to form a one's
  2925.  complement of the operand. NOT has no effect on the flags.
  2926.  
  2927.  Example:
  2928.    NOT BYTEOPRND. Replaces the original contents of BYTEOPRND with the
  2929.    one's complement of the contents of the memory word labeled BYTEOPRND.
  2930.  
  2931.  OR (Or) performs the logical "inclusive or" of the two operands and returns
  2932.  the result to the destination operand. OR clears OF and DF, leaves AF
  2933.  undefined, and updates SF, ZF, and PF.
  2934.  
  2935.  Example:
  2936.    OR AL,5. Replaces the original contents of AL with the logical
  2937.    "inclusive or" of the contents of AL and the immediate value 5.
  2938.  
  2939.  XOR (Exclusive OR) performs the logical "exclusive or" of the two operands
  2940.  and returns the result to the destination operand. XOR clears OF and DF,
  2941.  leaves AF undefined, and updates SF, ZF, and PF.
  2942.  
  2943.  Example:
  2944.    XOR DX, WORDOPRND. Replaces the original contents of DX with the logical
  2945.    "exclusive or" or the contents of DX and the contents of the memory word
  2946.    labeled WORDOPRND.
  2947.  
  2948.  NEG (Negate) forms a two's complement of a signed byte or word operand. The
  2949.  effect of NEG is to reverse the sign of the operand from positive to
  2950.  negative or from negative to positive. NEG updates OF, SF, ZF, AF, PF, and
  2951.  CF.
  2952.  
  2953.  Example:
  2954.    NEG AX. Replaces the original contents of AX with the two's complement
  2955.    of the contents of AX.
  2956.  
  2957.  
  2958.  3.4.2  Shift and Rotate Instructions
  2959.  
  2960.  The shift and rotate instructions reposition the bits within the specified
  2961.  operand. The shift instructions provide a convenient way to accomplish
  2962.  division or multiplication by binary power. The rotate instructions are
  2963.  useful for bit testing.
  2964.  
  2965.  
  2966.  3.4.2.1  Shift Instructions
  2967.  
  2968.  The bits in bytes and words may be shifted arithmetically or logically.
  2969.  Depending on the value of a specified count, up to 31 shifts may be
  2970.  performed.
  2971.  
  2972.  A shift instruction can specify the count in one of three ways. One form of
  2973.  shift instruction implicitly specifies the count as a single shift. The
  2974.  second form specifies the count as an immediate value. The third form
  2975.  specifies the count as the value contained in CL. This last form allows the
  2976.  shift count to be a variable that the program supplies during execution.
  2977.  Only the low order 5 bits of CL are used.
  2978.  
  2979.  Shift instructions affect the flags as follows. AF is always undefined
  2980.  following a shift operation. PF, SF, and ZF are updated normally as in the
  2981.  logical instructions.
  2982.  
  2983.  CF always contains the value of the last bit shifted out of the destination
  2984.  operand. In a single-bit shift, OF is set if the value of the high-order
  2985.  (sign) bit was changed by the operation. Otherwise, OF is cleared. Following
  2986.  a multibit shift, however, the content of OF is always undefined.
  2987.  
  2988.  SAL (Shift Arithmetic Left) shifts the destination byte or word operand left
  2989.  by one or by the number of bits specified in the count operand (an immediate
  2990.  value or the value contained in CL). The processor shifts zeros in from the
  2991.  right side of the operand as bits exit from the left side. See figure 3-6.
  2992.  
  2993.  Example:
  2994.    SAL BL,2. Shifts the contents of BL left by 2 bits and replaces the two
  2995.    low-order bits with zeros.
  2996.  
  2997.  Example:
  2998.    SAL BL,1. Shifts the contents of BL left by 1 bit and replaces the
  2999.    low-order bit with a zero. Because the processor does not have to decode
  3000.    the immediate count operand to obtain the shift count, this from of the
  3001.    instruction takes 2 clock cycles rather than the 6 clock cycles (5 + 1
  3002.    cycle for each bit shifted) required by the previous example.
  3003.  
  3004.  SHL (Shift Logical Left) is physically the same instruction as SAL (see SAL
  3005.  above).
  3006.  
  3007.  SHR (Shift Logical Right) shifts the destination byte or word operand right
  3008.  by one or by the number of bits specified in the count operand (an immediate
  3009.  value or the value contained in CL). The processor shifts zeros in from the
  3010.  left side of the operand as bits exit from the right side. See figure 3-7.
  3011.  
  3012.  Example:
  3013.    SHR BYTEOPRND, CL. Shifts the contents of the memory byte labeled
  3014.    BYTEOPRND right by the number of bits specified in CL, and pads the left
  3015.    side of BYTEOPRND with an equal number of zeros.
  3016.  
  3017.  SAR (Shift Arithmetic Right) shifts the destination byte or word operand to
  3018.  the right by one or by the number of bits specified in the count operand (an
  3019.  immediate value or the value contained in CL). The processor preserves the
  3020.  sign of the operand by shifting in zeros on the left side if the value is
  3021.  positive or by shifting by ones if the value is negative. See figure 3-8.
  3022.  
  3023.  Example:
  3024.    SAR WORDPRND,1. Shifts the contents of the memory byte labeled WORDPRND
  3025.    right by one, and replaces the high-order sign bit with a value equal to
  3026.    the original sign of WORDPRND.
  3027.  
  3028.  SHR shifts the bits in the register or memory operand to the right by the
  3029.  specified number of bit positions. CF receives the last bit shifted out of
  3030.  the right of the operand. SHR shifts in zeros to fill the vacated bit
  3031.  locations. This instruction operates on byte operands as well as word
  3032.  operands.
  3033.  
  3034.  
  3035.  Figure 3-6.  SAL and SHL
  3036.  
  3037.                            BEFORE SAL OR SHL
  3038.  ╔═╗ ╔═╗  ╔═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╗
  3039.  ║X║ ║X║  ║ 1 ║ 1 ║ 1 ║ 1 ║ 1 ║ 0 ║ 0 ║ 0 ║ 1 ║ 1 ║ 1 ║ 1 ║ 0 ║ 0 ║ 1 ║ 1 ║
  3040.  ╚═╝ ╚═╝  ╚═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╝
  3041.  
  3042.                       AFTER SAL OR SHL BY 1 BIT
  3043.  ╔═╗ ╔═╗  ╔═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╗
  3044.  ║0║ ║1║─╢ 1 ║ 1 ║ 1 ║ 1 ║ 0 ║ 0 ║ 0 ║ 1 ║ 1 ║ 1 ║ 1 ║ 0 ║ 0 ║ 1 ║ 1 ║ 0 ║
  3045.  ╚═╝ ╚═╝  ╚═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╝
  3046.  
  3047.                       AFTER SAL OR SHL BY 8 BITS
  3048.  ╔═╗ ╔═╗  ╔═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╗
  3049.  ║X║ ║ ║─╢ 1 ║ 1 ║ 1 ║ 1 ║ 0 ║ 0 ║ 1 ║ 1 ║ 0 ║ 0 ║ 0 ║ 0 ║ 0 ║ 0 ║ 0 ║ 0 ║
  3050.  ╚═╝ ╚═╝  ╚═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╝
  3051.  OF  CF
  3052.  
  3053.   Both SAL and SHL shift the bits in the register or memory operand to the
  3054.   left by the specified number of bit positions. CF receives the last bit
  3055.   shifted out of the left of the operand. SAL and SHL shift in zeros to fill
  3056.   the vacated bit locations. These instructions operate on byte operands as
  3057.   well as word operands.
  3058.  
  3059.  
  3060.  Figure 3-7.  SHR
  3061.  
  3062.                                   BEFORE SHR
  3063.   ╔═╗ ╔═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╗  ╔═╗
  3064.   ║X║ ║ 1 ║ 1 ║ 0 ║ 0 ║ 1 ║ 1 ║ 1 ║ 1 ║ 0 ║ 1 ║ 1 ║ 1 ║ 0 ║ 0 ║ 0 ║ 1 ║  ║X║
  3065.   ╚═╝ ╚═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╝  ╚═╝
  3066.  
  3067.                               AFTER SHR BY 1 BIT
  3068.   ╔═╗ ╔═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╗  ╔═╗
  3069.   ║1║ ║ 0 ║ 1 ║ 1 ║ 0 ║ 0 ║ 1 ║ 1 ║ 1 ║ 1 ║ 0 ║ 1 ║ 1 ║ 1 ║ 0 ║ 0 ║ 0 ╟─║1║
  3070.   ╚═╝ ╚═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╝  ╚═╝
  3071.  
  3072.                              AFTER SHR BY 10 BITS
  3073.   ╔═╗ ╔═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╗  ╔═╗
  3074.   ║X║ ║ 0 ║ 0 ║ 0 ║ 0 ║ 0 ║ 0 ║ 0 ║ 0 ║ 0 ║ 0 ║ 0 ║ 1 ║ 1 ║ 0 ║ 0 ║ 1 ╟─║1║
  3075.   ╚═╝ ╚═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╝  ╚═╝
  3076.   OF                                OPERAND                              CF
  3077.  
  3078.   SHR shifts the bits in the register or memory operand to the right by the
  3079.   specified number of bit positions. CF receives the last bit shifted out of
  3080.   the right of the operand. SHR shifts in zeros to fill the vacated bit
  3081.   locations. This instruction operates on byte operands as well as word
  3082.   operands.
  3083.  
  3084.  
  3085.  Figure 3-8.  SAR
  3086.  
  3087.                       BEFORE SAR WITH A POSITIVE OPERAND
  3088.   ╔═╗ ╔═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╗  ╔═╗
  3089.   ║X║ ║ 0 ║ 0 ║ 0 ║ 0 ║ 0 ║ 0 ║ 0 ║ 0 ║ 0 ║ 0 ║ 0 ║ 0 ║ 0 ║ 0 ║ 0 ║ 1 ║  ║X║
  3090.   ╚═╝ ╚═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╝  ╚═╝
  3091.  
  3092.                AFTER SAR WITH A POSITIVE OPERAND SHIFTED 1 BIT
  3093.   ╔═╗ ╔═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╗  ╔═╗
  3094.   ║X║ ║ 0 ║ 0 ║ 0 ║ 0 ║ 0 ║ 0 ║ 0 ║ 0 ║ 0 ║ 0 ║ 0 ║ 0 ║ 0 ║ 0 ║ 0 ║ 0 ╟─║1║
  3095.   ╚═╝ ╚═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╝  ╚═╝
  3096.  
  3097.                       BEFORE SAR WITH A NEGATIVE OPERAND
  3098.   ╔═╗ ╔═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╗  ╔═╗
  3099.   ║X║ ║ 1 ║ 0 ║ 0 ║ 0 ║ 1 ║ 1 ║ 1 ║ 1 ║ 0 ║ 0 ║ 0 ║ 1 ║ 1 ║ 0 ║ 1 ║ 0 ╟─║X║
  3100.   ╚═╝ ╚═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╝  ╚═╝
  3101.  
  3102.                AFTER SAR WITH A NEGATIVE OPERAND SHIFTED 6 BITS
  3103.   ╔═╗ ╔═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╗  ╔═╗
  3104.   ║X║ ║ 1 ║ 1 ║ 1 ║ 1 ║ 1 ║ 1 ║ 1 ║ 0 ║ 0 ║ 0 ║ 1 ║ 1 ║ 1 ║ 1 ║ 0 ║ 0 ╟─║0║
  3105.   ╚═╝ ╚═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╝  ╚═╝
  3106.   OF                               OPERAND                               CF
  3107.  
  3108.   SAR preserves the sign of the register or memory operand as it shifts the
  3109.   operand to the right the specified number of bit positions. CF receives the
  3110.   last bit shifted out of the right of the operand. This instruction also
  3111.   operates on byte operands.
  3112.  
  3113.  
  3114.  3.4.2.2  Rotate Instructions
  3115.  
  3116.  Rotate instructions allow bits in bytes and words to be rotated. Bits
  3117.  rotated out of an operand are not lost as in a shift, but are "circled" back
  3118.  into the other "end" of the operand.
  3119.  
  3120.  Rotates affect only the carry and overflow flags. CF may act as an
  3121.  extension of the operand in two of the rotate instructions, allowing a bit
  3122.  to be isolated and then tested by a conditional jump instruction (JC or
  3123.  JNC). CF always contains the value of the last bit rotated out, even if the
  3124.  instruction does not use this bit as an extension of the rotated operand.
  3125.  
  3126.  In single-bit rotates, OF is set if the operation changes the high-order
  3127.  (sign) bit of the destination operand. If the sign bit retains its original
  3128.  value, OF is cleared. On multibit rotates, the value of OF is always
  3129.  undefined.
  3130.  
  3131.  ROL (Rotate Left) rotates the byte or word destination operand left by one
  3132.  or by the number of bits specified in the count operand (an immediate value
  3133.  or the value contained in CL). For each rotation specified, the high-order
  3134.  bit that exists from the left of the operand returns at the right to become
  3135.  the new low-order bit of the operand. See figure 3-9.
  3136.  
  3137.  Example:
  3138.    ROL AL, 8. Rotates the contents of AL left by 8 bits. This rotate
  3139.    instruction returns AL to its original state but isolates the low-order
  3140.    bit in CF for testing by a JC or JNC instruction.
  3141.  
  3142.  ROR (Rotate Right) rotates the byte or word destination operand right by
  3143.  one or by the number of bits specified in the count operand (an immediate
  3144.  value or the value contained in CL). For each rotation specified, the
  3145.  low-order bit that exits from the right of the operand returns at the left
  3146.  to become the new high-order bit of the operand. See figure 3-10.
  3147.  
  3148.  Example:
  3149.    ROR WORDOPRND, CL. Rotates the contents of the memory word labeled
  3150.    WORDOPRND by the number of bits specified by the value contained in CL.
  3151.    CF reflects the value of the last bit rotated from the right to the left
  3152.    side of the operand.
  3153.  
  3154.  RCL (Rotate Through Carry Left) rotates bits in the byte or word
  3155.  destination operand left by one or by the number of bits specified in the
  3156.  count operand (an immediate value or the value contained in CL).
  3157.  
  3158.  This instruction differs from ROL in that it treats CF as a high-order
  3159.  1-bit extension of the destination operand. Each high-order bit that exits
  3160.  from the left side of the operand moves to CF before it returns to the
  3161.  operand as the low-order bit on the next rotation cycle. See figure 3-11.
  3162.  
  3163.  Example:
  3164.    RCL BX,1. Rotates the contents of BX left by one bit. The high-order bit
  3165.    of the operand moves to CF, the remaining 15 bits move left one position,
  3166.    and the original value of CF becomes the new low-order bit.
  3167.  
  3168.  RCR (Rotate Through Carry Right) rotates bits in the byte or word
  3169.  destination operand right by one or by the number of bits specified in the
  3170.  count operand (an immediate value or the value contained in CL).
  3171.  
  3172.  This instruction differs from ROR in that it treats CF as a low-order 1-bit
  3173.  extension of the destination operand. Each low-order bit that exits from the
  3174.  right side of the operand moves to CF before it returns to the operand as
  3175.  the high-order bit on the next rotation cycle. See figure 3-12.
  3176.  
  3177.  Example:
  3178.    RCR BYTEOPRND,3. Rotates the contents of the memory byte labeled BYTEOPRND
  3179.    to the right by 3 bits. Following the execution of this instruction, CF
  3180.    reflects the original value of bit number 5 of BYTEOPRND, and the original
  3181.    value of CF becomes bit 2.
  3182.  
  3183.  RCL rotates the bits in the memory or register operand to the left in the
  3184.  same way as ROL except that RCL treats CF as a 1-bit extension of the
  3185.  operand. Note that a 16-bit RCL produces the same result as a 1-bit RCR
  3186.  (though it takes much longer to execute). This instruction also operates on
  3187.  byte operands.
  3188.  
  3189.  RCR rotates the bits in the memory or register operand to the right in the
  3190.  same way as ROR except that RCR treats CF as a 1-bit extension of the
  3191.  operand. This instruction also operates on byte operands.
  3192.  
  3193.  
  3194.  Figure 3-9.  ROL
  3195.  
  3196.                                 BEFORE ROL
  3197.   ╔═╗╔═╗  ╔═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╗
  3198.   ║X║║X║  ║ 1 ║ 0 ║ 0 ║ 0 ║ 1 ║ 1 ║ 1 ║ 0 ║ 1 ║ 0 ║ 0 ║ 1 ║ 1 ║ 0 ║ 0 ║ 0 ║
  3199.   ╚═╝╚═╝  ╚═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╝
  3200.  
  3201.                             AFTER ROL BY 1 BIT
  3202.   ╔═╗╔═╗  ╔═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╗
  3203.   ║1║║1║┬╢ 0 ║ 0 ║ 0 ║ 1 ║ 1 ║ 1 ║ 0 ║ 1 ║ 0 ║ 0 ║ 1 ║ 1 ║ 0 ║ 0 ║ 0 ║ 1 ┐
  3204.   ╚═╝╚═╝ │╚═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╝│
  3205.          └─────────────────────────────────────────────────────────────────┘
  3206.  
  3207.                            AFTER ROL BY 12 BITS
  3208.   ╔═╗╔═╗  ╔═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╗
  3209.   ║X║║1║┬╢ 1 ║ 0 ║ 0 ║ 0 ║ 1 ║ 0 ║ 0 ║ 0 ║ 1 ║ 1 ║ 1 ║ 0 ║ 1 ║ 0 ║ 0 ║ 1 ┐
  3210.   ╚═╝╚═╝ │╚═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╝│
  3211.   OF CF  │                        OPERAND                                  │
  3212.          └─────────────────────────────────────────────────────────────────┘
  3213.  
  3214.   ROL shifts the bits in the memory or register operand to the left by the
  3215.   specified number of bit positions. It copies the bit shifted out of the
  3216.   left of the operand into the right of the operand. The last bit shifted
  3217.   into the least significant bit of the operand also appears in CF. This
  3218.   instruction also operates on byte operands.
  3219.  
  3220.  
  3221.  Figure 3-10.  ROR
  3222.  
  3223.                                   BEFORE ROR
  3224.    ╔═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╗  ╔═══╗
  3225.    ║ 1 ║ 1 ║ 0 ║ 1 ║ 1 ║ 1 ║ 0 ║ 0 ║ 1 ║ 0 ║ 1 ║ 1 ║ 1 ║ 0 ║ 0 ║ 0 ║  ║ X ║
  3226.    ╚═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╝  ╚═══╝
  3227.  
  3228.                               AFTER ROR BY 1 BIT
  3229.    ╔═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╗  ╔═══╗
  3230.   ┌ 0 ║ 1 ║ 1 ║ 0 ║ 1 ║ 1 ║ 1 ║ 0 ║ 0 ║ 1 ║ 0 ║ 1 ║ 1 ║ 1 ║ 0 ║ 0 ╟┬║ 0 ║
  3231.   │╚═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╝│ ╚═══╝
  3232.   └─────────────────────────────────────────────────────────────────┘
  3233.  
  3234.                              AFTER ROR BY 8 BITS
  3235.    ╔═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╗  ╔═══╗
  3236.   ┌ 1 ║ 0 ║ 1 ║ 1 ║ 1 ║ 0 ║ 0 ║ 0 ║ 1 ║ 1 ║ 0 ║ 1 ║ 1 ║ 1 ║ 0 ║ 0 ╟┬║ 1 ║
  3237.   │╚═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╝│ ╚═══╝
  3238.   │                             OPERAND                             │  CF
  3239.   └─────────────────────────────────────────────────────────────────┘
  3240.  
  3241.   ROR shifts the bits in the memory or register operand to the right by the
  3242.   specified number of bit positions. It copies each bit shifted out of the
  3243.   right of the operand into the left of the operand. The last bit shifted
  3244.   into the most significant bit of the operand also appears in CF. This
  3245.   instruction also operates on byte operands.
  3246.  
  3247.  
  3248.  Figure 3-11.  RCL
  3249.  
  3250.                                  BEFORE RCL
  3251.     ╔═══╗  ╔═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╗
  3252.     ║ 1 ║  ║ 1 ║ 1 ║ 1 ║ 0 ║ 0 ║ 0 ║ 1 ║ 1 ║ 0 ║ 0 ║ 1 ║ 1 ║ 0 ║ 0 ║ 0 ║ 0 ║
  3253.     ╚═══╝  ╚═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╝
  3254.  
  3255.                              AFTER RCL BY 1 BIT
  3256.     ╔═══╗  ╔═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╗
  3257.   ┌─╢ 1 ║─╢ 1 ║ 1 ║ 0 ║ 0 ║ 0 ║ 1 ║ 1 ║ 0 ║ 0 ║ 1 ║ 1 ║ 0 ║ 0 ║ 0 ║ 0 ║ 1 ┐
  3258.   │ ╚═══╝  ╚═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╝│
  3259.   └─────────────────────────────────────────────────────────────────────────┘
  3260.  
  3261.                             AFTER RCL BY 16 BITS
  3262.     ╔═══╗  ╔═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╗
  3263.   ┌─╢ 0 ║─╢ 1 ║ 1 ║ 1 ║ 1 ║ 0 ║ 0 ║ 0 ║ 1 ║ 1 ║ 0 ║ 0 ║ 1 ║ 1 ║ 0 ║ 0 ║ 0 ┐
  3264.   │ ╚═══╝  ╚═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╝│
  3265.   │  CF                            OPERAND                                  │
  3266.   └─────────────────────────────────────────────────────────────────────────┘
  3267.  
  3268.   RCL rotates the bits in the memory or register operand to the left in the
  3269.   same way as ROL except that RCL treats CF as a 1-bit extension of the
  3270.   operand. Note that a 16-bit RCL produces the same result as a 1-bit RCR
  3271.   (though it takes much longer to execute). This instruction also operates
  3272.   on byte operands.
  3273.  
  3274.  
  3275.  Figure 3-12.  RCR
  3276.  
  3277.                                   BEFORE RCR
  3278.    ╔═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╗  ╔═══╗
  3279.    ║ 1 ║ 1 ║ 1 ║ 0 ║ 0 ║ 0 ║ 1 ║ 1 ║ 0 ║ 0 ║ 1 ║ 1 ║ 0 ║ 0 ║ 0 ║ 0 ║  ║ 1 ║
  3280.    ╚═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╝  ╚═══╝
  3281.  
  3282.                               AFTER RCR BY 1 BIT
  3283.    ╔═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╗  ╔═══╗
  3284.   ┌ 1 ║ 1 ║ 1 ║ 1 ║ 0 ║ 0 ║ 0 ║ 1 ║ 1 ║ 0 ║ 0 ║ 1 ║ 1 ║ 0 ║ 0 ║ 0 ╟─║ 0 ╟─┐
  3285.   │╚═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╝  ╚═══╝ │
  3286.   └─────────────────────────────────────────────────────────────────────────┘
  3287.  
  3288.                              AFTER RCR BY 8 BITS
  3289.    ╔═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╗  ╔═══╗
  3290.   ┌ 0 ║ 0 ║ 1 ║ 1 ║ 1 ║ 1 ║ 0 ║ 0 ║ 0 ║ 1 ║ 1 ║ 0 ║ 0 ║ 1 ║ 1 ║ 0 ╟─║ 0 ╟─┐
  3291.   │╚═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╝  ╚═══╝ │
  3292.   │                                OPERAND                             CF   │
  3293.   └─────────────────────────────────────────────────────────────────────────┘
  3294.  
  3295.   RCR rotates the bits in the memory or register operand to the right in the
  3296.   same way as ROR except that RCR treats CF as a 1-bit extension of the
  3297.   operand. This instruction also operates on byte operands.
  3298.  
  3299.  
  3300.  3.4.3  Type Conversion and No-Operation Instructions
  3301.  
  3302.  The type conversion instructions prepare operands for division. The NOP
  3303.  instruction is a 1-byte filler instruction with no effect on registers or
  3304.  flags.
  3305.  
  3306.  CWD (Convert Word to Double-Word) extends the sign of the word in register
  3307.  AX throughout register DX. CWD does not affect any flags. CWD can be used to
  3308.  produce a double-length (double-word) dividend from a word before a word
  3309.  division.
  3310.  
  3311.  CBW (Convert Byte to Word) extends the sign of the byte in register AL
  3312.  throughout AX. CBW does not affect any flags.
  3313.  
  3314.  Example:
  3315.    CWD. Sign-extends the 16-bit value in AX to a 32-bit value in DX and AX
  3316.    with the high-order 16-bits occupying DX.
  3317.  
  3318.  NOP (No Operation) occupies a byte of storage but affects nothing but the
  3319.  instruction pointer, IP. The amount of time that a NOP instruction requires
  3320.  for execution varies in proportion to the CPU clocking rate. This variation
  3321.  makes it inadvisable to use NOP instructions in the construction of timing
  3322.  loops because the operation of such a program will not be independent of the
  3323.  system hardware configuration.
  3324.  
  3325.  Example:
  3326.    NOP. The processor performs no operation for 2 clock cycles.
  3327.  
  3328.  
  3329.  3.5  Test and Compare Instructions
  3330.  
  3331.  The test and compare instructions are similar in that they do not alter
  3332.  their operands. Instead, these instructions perform operations that only set
  3333.  the appropriate flags to indicate the relationship between the two operands.
  3334.  
  3335.  TEST (Test) performs the logical "and" of the two operands, clears OF and
  3336.  DF, leaves AF undefined, and updates SF, ZF, and PF. The difference between
  3337.  TEST and AND is that TEST does not alter the destination operand.
  3338.  
  3339.  Example:
  3340.    TEST BL,32. Performs a logical "and" and sets SF, ZF, and PF according to
  3341.    the results of this operation. The contents of BL remain unchanged.
  3342.  
  3343.  CMP (Compare) subtracts the source operand from the destination operand. It
  3344.  updates OF, SF, ZF, AF, PF, and CF but does not alter the source and
  3345.  destination operands. A subsequent signed or unsigned conditional transfer
  3346.  instruction can test the result using the appropriate flag result.
  3347.  
  3348.  CMP can compare two register operands, a register operand and a memory
  3349.  operand, a register operand and an immediate operand, or an immediate
  3350.  operand and a memory operand. The operands may be words or bytes, but CMP
  3351.  cannot compare a byte with a word.
  3352.  
  3353.  Example:
  3354.    CMP BX,32. Subtracts the immediate operand, 32, from the contents of BX
  3355.    and  sets OF, SF, ZF, AF, PF, and CF to reflect the result. The contents
  3356.    of BX remain unchanged.
  3357.  
  3358.  
  3359.  3.6  Control Transfer Instructions
  3360.  
  3361.  The 80286 provides both conditional and unconditional program transfer
  3362.  instructions to direct the flow of execution. Conditional program transfers
  3363.  depend on the results of operations that affect the flag register.
  3364.  Unconditional program transfers are always executed.
  3365.  
  3366.  
  3367.  3.6.1  Unconditional Transfer Instructions
  3368.  
  3369.  JMP, CALL, RET, INT and IRET instructions transfer control from one code
  3370.  segment location to another. These locations can be within the same code
  3371.  segment or in different code segments.
  3372.  
  3373.  
  3374.  3.6.1.1  Jump Instruction
  3375.  
  3376.  JMP (Jump) unconditionally transfers control to the target location. JMP is
  3377.  a one-way transfer of execution; it does not save a return address on the
  3378.  stack.
  3379.  
  3380.  The JMP instruction always performs the same basic function of transferring
  3381.  control from the current location to a new location. Its implementation
  3382.  varies depending on the following factors:
  3383.  
  3384.    ■  Is the address specified directly within the instruction or indirectly
  3385.       through a register or memory?
  3386.  
  3387.    ■  Is the target location inside or outside the current code segment
  3388.       selected in CS?
  3389.  
  3390.  A direct JMP instruction includes the destination address as part of the
  3391.  instruction. An indirect JMP instruction obtains the destination address
  3392.  indirectly through a register or a pointer variable.
  3393.  
  3394.  Control transfers through a gate or to a task state segment are available
  3395.  only in Protected Mode operation of the 80286. The formats of the
  3396.  instructions that transfer control through a call gate, a task gate, or to a
  3397.  task state segment are the same. The label included in the instruction
  3398.  selects one of these three paths to a new code segment.
  3399.  
  3400.  Direct JMP within the current code segment. A direct JMP that transfers
  3401.  control to a target location within the current code segment uses a relative
  3402.  displacement value contained in the instruction. This can be either a 16-bit
  3403.  value or an 8-bit value sign extended to 16 bits. The processor forms an
  3404.  effective address by adding this relative displacement to the address
  3405.  contained in IP. IP refers to the next instruction when the additions are
  3406.  performed.
  3407.  
  3408.  Example:
  3409.    JMP NEAR_NEWCODE. Transfers control to the target location labeled
  3410.    NEAR_NEWCODE, which is within the code segment currently selected in CS.
  3411.  
  3412.  Indirect JMP within the current code segment. Indirect JMP instructions
  3413.  that transfer control to a location within the current code segment specify
  3414.  an absolute address in one of several ways. First, the program can JMP to a
  3415.  location specified by a 16-bit register (any of AX, DX, CX, BX, BP, SI, or
  3416.  DI). The processor moves this 16-bit value into IP and resumes execution.
  3417.  
  3418.  Example:
  3419.    JMP SI. Transfers control to the target address formed by adding the
  3420.    16-bit value contained in SI to the base address contained in CS.
  3421.  
  3422.  The processor can also obtain the destination address within a current
  3423.  segment from a memory word operand specified in the instruction.
  3424.  
  3425.  Example:
  3426.    JMP PTR_X. Transfers control to the target address formed by adding the
  3427.    16-bit value contained in the memory word labeled PTR X to the base
  3428.    address contained in CS.
  3429.  
  3430.  A register can modify the address of the memory word pointer to select a
  3431.  destination address.
  3432.  
  3433.  Example:
  3434.    JMP CASE_TABLE [BX]. CASE_TABLE is the first word in an array of word
  3435.    pointers. The value of BX determines which pointer the program selects
  3436.    from the array. The JMP instruction then transfers control to the
  3437.    location specified by the selected pointer.
  3438.  
  3439.  Direct JMP outside of the current code segment. Direct JMP instructions
  3440.  that specify a target location outside the current code segment contain a
  3441.  full 32-bit pointer. This pointer consists of a selector for the new code
  3442.  segment and an offset within the new segment.
  3443.  
  3444.  Example:
  3445.    JMP FAR_NEWCODE_FOO. Places the selector contained in the instruction into
  3446.    CS and the offset into IP. The program resumes execution at this location
  3447.    in the new code segment.
  3448.  
  3449.  Indirect JMP outside of the current code segment. Indirect JMP instructions
  3450.  that specify a target location outside the current code segment use a
  3451.  double-word variable to specify the pointer.
  3452.  
  3453.  Example:
  3454.    JMP NEWCODE. NEWCODE the first word of two consecutive words in memory
  3455.    which represent the new pointer. NEWCODE contains the new offset for IP
  3456.    and the word following NEWCODE contains the selector for CS. The program
  3457.    resumes execution at this location in the new code segment. (Protected
  3458.    mode programs treat this differently. See Chapters 6 and 7).
  3459.  
  3460.  Direct JMP outside of the current code segment to a call gate. If the
  3461.  selector included with the instruction refers to a call gate, then the
  3462.  processor ignores the offset in the instruction and takes the pointer of the
  3463.  routine being entered from the call gate.
  3464.  
  3465.  JMP outside of current code segment may only go to the same level.
  3466.  
  3467.  Example:
  3468.    JMP CALL_GATE_FOO. The selector in the instruction refers to the call gate
  3469.    CALL_GATE_FOO, and the call gate actually provides the new contents of CS
  3470.    and IP to specify the address of the next instructions.
  3471.  
  3472.  Indirect JMP outside the current code segment to a call gate. If the
  3473.  selector specified by the instruction refers to a call gate, the processor
  3474.  ignores the offset in the double-word and takes the address of the routine
  3475.  being entered from the call gate. The JMP instruction uses the same format
  3476.  to indirectly specify a task gate or a task state segment.
  3477.  
  3478.  Example:
  3479.    JMP CASE_TABLE [BX]. The instruction refers to the double-word in the
  3480.    array of pointers called CASE_TABLE. The specific double-word chosen
  3481.    depends on the value in BX when the instruction executes. The selector
  3482.    portion of this double-word selects a call gate, and the processor takes
  3483.    the address of the routine being entered from the call gate.
  3484.  
  3485.  ROL shifts the bits in the memory or register operand to the left by the
  3486.  specified number of bit positions. It copies the bit shifted out of the left
  3487.  of the operand into the right of the operand. The last bit shifted into the
  3488.  least significant bit of the operand also appears in CF. This instruction
  3489.  also operates on byte operands.
  3490.  
  3491.  ROR shifts the bits in the memory or register operand to the right by the
  3492.  specified number of bit positions. It copies each bit shifted out of the
  3493.  right of the operand into the left of the operand. The last bit shifted into
  3494.  the most significant bit of the operand also appears in CF. This instruction
  3495.  also operates on byte operands.
  3496.  
  3497.  
  3498.  3.6.1.2  Call Instruction
  3499.  
  3500.  CALL (Call Procedure) activates an out-of-line procedure, saving on the
  3501.  stack the address of the instruction following the CALL for later use by a
  3502.  RET (Return) instruction. An intrasegment CALL places the current value of
  3503.  IP on the stack. An intersegment CALL places both the value of IP and CS on
  3504.  the stack. The RET instruction in the called procedure uses this address to
  3505.  transfer control back to the calling program.
  3506.  
  3507.  A long CALL instruction that invokes a task-switch stores the outgoing
  3508.  task's task state segment selector in the incoming task state segment's link
  3509.  field and sets the nested task flag in the new task. In this case, the IRET
  3510.  instruction takes the place of the RET instruction to return control to the
  3511.  nested task.
  3512.  
  3513.  Examples:
  3514.    CALL NEAR_NEWCODE
  3515.    CALL SI
  3516.    CALL PTR_X
  3517.    CALL CASE_TABLE [BP]
  3518.    CALL FAR_NEWCODE_FOO
  3519.    CALL NEWCODE
  3520.    CALL CALL_GATE_FOO
  3521.    CALL CASE_TABLE [BX]
  3522.  
  3523.  See the previous treatment of JMP for a discussion of the operations of
  3524.  these instructions.
  3525.  
  3526.  
  3527.  3.6.1.3  Return And Return From Interrupt Instruction
  3528.  
  3529.  RET (Return From Procedure) terminates the execution of a procedure and
  3530.  transfers control through a back-link on the stack to the program that
  3531.  originally invoked the procedure.
  3532.  
  3533.  An intrasegment RET restores the value of IP that was saved on the stack by
  3534.  the previous intrasegment CALL instruction. An intersegment RET restores the
  3535.  values of both CS and IP which were saved on the stack by the previous
  3536.  intersegment CALL instruction.
  3537.  
  3538.  RET instructions may optionally specify a constant to the stack pointer.
  3539.  This constant specifies the new top of stack to effectively remove any
  3540.  arguments that the calling program pushed on the stack before the execution
  3541.  of the CALL instruction.
  3542.  
  3543.  Example:
  3544.    RET. If the previous CALL instruction did not transfer control to a new
  3545.    code segment, RET restores the value of IP pushed by the CALL instruction.
  3546.    If the previous CALL instruction transferred control to a new segment, RET
  3547.    restores the values of both IP and CS which were pushed on the stack by
  3548.    the CALL instruction.
  3549.  
  3550.  Example:
  3551.    RET n. This form of the RET instruction performs identically to the above
  3552.    example except that it adds n (which must be an even value) to the value
  3553.    of SP to eliminate n bytes of parameter information previously pushed by
  3554.    the calling program.
  3555.  
  3556.  IRET (Return From Interrupt or Nested Task) returns control to an
  3557.  interrupted routine or, optionally, reverses the action of a CALL or INT
  3558.  instruction that caused a task switch. See Chapter 8 for further
  3559.  information on task switching.
  3560.  
  3561.  Example:
  3562.    IRET. Returns from an interrupt with or without a task switch based on
  3563.    the value of the NT bit.
  3564.  
  3565.  
  3566.  3.6.2  Conditional Transfer Instructions
  3567.  
  3568.  The conditional transfer instructions are jumps that may or may not transfer
  3569.  control, depending on the state of the CPU flags when the instruction
  3570.  executes. Instruction encoding is most efficient when the target for the
  3571.  conditional jumps is in the current code segment and within -128 to +127
  3572.  bytes of the first byte of the next instruction. Alternatively, the opposite
  3573.  sense of the conditional jump can skip around an unconditional jump to the
  3574.  destination.
  3575.  
  3576.  
  3577.  3.6.2.1  Conditional Jump Instructions
  3578.  
  3579.  Table 3-3 shows the conditional transfer mnemonics and their
  3580.  interpretations. The conditional jumps that are listed as pairs are actually
  3581.  the same instruction. The assembler provides the alternate mnemonics for
  3582.  greater clarity within a program listing.
  3583.  
  3584.  
  3585.  Table 3-3. Interpretation of Conditional Transfers
  3586.  
  3587.                        Unsigned Conditional Transfers
  3588.  Mnemonic      Condition Tested           "Jump If. . ."
  3589.  
  3590.  JA/JNBE       (CF or ZF) = 0             above/not below nor equal
  3591.  JAE/JNB       CF = 0                     above or equal/not below
  3592.  JB/JNAE       CF = 1                     below/not above nor equal
  3593.  JBE/JNA       (CF or ZF) = 1             below or equal/not above
  3594.  JC            CF = 1                     carry
  3595.  JE/JZ         ZF = 1                     equal/zero
  3596.  JNC           CF = 0                     not carry
  3597.  JNE/JNZ       ZF = 0                     not equal/not zero
  3598.  JNP/JPO       PF = 0                     not parity/parity odd
  3599.  JP/JPE        PF = 1                     parity/parity even
  3600.  
  3601.                        Signed Conditional Transfers
  3602.  Mnemonic     Condition Tested            "Jump If. . ."
  3603.  
  3604.  JG/JNLE     ((SF xor OF) or ZF) = 0      greater/not less nor equal
  3605.  JGE/JNL      (SF xor OF) = 0             greater or equal/not less
  3606.  JL/JNGE      (SF xor OF) = 0             less/not greater nor equal
  3607.  JLE/JNG      ((SF xor OF) or ZF) = 1     less or equal/not greater
  3608.  JNO          OF = 0                      not overflow
  3609.  JNS          SF = 0                      not sign (positive, including 0)
  3610.  JO           OF = 1                      overflow
  3611.  JS           SF = 1                      sign (negative)
  3612.  
  3613.  
  3614.  3.6.2.2  Loop Instructions
  3615.  
  3616.  The loop instructions are conditional jumps that use a value placed in CX
  3617.  to specify the number of repetitions of a software loop. All loop
  3618.  instructions automatically decrement CX and terminate the loop when CX=0.
  3619.  Four of the five loop instructions specify a condition of ZF that
  3620.  terminates the loop before CX decrements to zero.
  3621.  
  3622.  LOOP (Loop While CX Not Zero) is a conditional transfer that
  3623.  auto-decrements the CX register before testing CX for the branch condition.
  3624.  If CX is non-zero, the program branches to the target label specified in the
  3625.  instruction. The LOOP instruction causes the repetition of a code section
  3626.  until the operation of the LOOP instruction decrements CX to a value of
  3627.  zero. If LOOP finds CX=0, control transfers to the instruction immediately
  3628.  following the LOOP instruction. If the value of CX is initially zero, then
  3629.  the LOOP executes 65,536 times.
  3630.  
  3631.  Example:
  3632.    LOOP START_LOOP. Each time the program encounters this instruction, it
  3633.    decrements CX and then tests it. If the value of CX is non-zero, then the
  3634.    program branches to the instruction labeled START_LOOP. If the value in CX
  3635.    is zero, then the program continues with the instruction that follows the
  3636.    LOOP instruction.
  3637.  
  3638.  LOOPE (Loop While Equal) and LOOPZ (Loop While Zero) are physically the
  3639.  same instruction. These instructions auto-decrement the CX register before
  3640.  testing CX and ZF for the branch conditions. If CX is non-zero and ZF=1, the
  3641.  program branches to the target label specified in the instruction. If LOOPE
  3642.  or LOOPZ finds that CX=0 or ZF=0, control transfers to the instruction
  3643.  immediately succeeding the LOOPE or LOOPZ instruction.
  3644.  
  3645.  Example:
  3646.    LOOPE START_LOOP (or LOOPZ START_LOOP). Each time the program encounters
  3647.    this instruction, it decrements CX and tests CX and ZF. If the value in
  3648.    CX is non-zero and the value of ZF is 1, the program branches to the
  3649.    instruction labeled START_LOOP. If CX=0 or ZF=0, the program continues
  3650.    with the instruction that follows the LOOPE (or LOOPZ) instruction.
  3651.  
  3652.  LOOPNE (Loop While Not Equal) and LOOPNZ (Loop While Not Zero) are
  3653.  physically the same instruction. These instructions auto-decrement the CX
  3654.  register before testing CX and ZF for the branch conditions. If CX is
  3655.  non-zero and ZF=0, the program branches to the target label specified in
  3656.  the instruction. If LOOPNE or LOOPNZ finds that CX=0 or ZF=1, control
  3657.  transfers to the instruction immediately succeeding the LOOPNE or LOOPNZ
  3658.  instruction.
  3659.  
  3660.  Example:
  3661.    LOOPNE START_LOOP (or LOOPNZ START_LOOP). Each time the program encounters
  3662.    this instruction, it decrements CX and tests CX and ZF. If the value of CX
  3663.    is non-zero and the value of ZF is 0, the program branches to the
  3664.    instruction labeled START_LOOP. If CX=0 or ZF=1, the program continues
  3665.    with the instruction that follows the LOOPNE (or LOOPNZ) instruction.
  3666.  
  3667.  
  3668.  3.6.2.3  Executing a Loop or Repeat Zero Times
  3669.  
  3670.  JCXZ (Jump if CX Zero) branches to the label specified in the instruction
  3671.  if it finds a value of zero in CX. Sometimes, it is desirable to design a
  3672.  loop that executes zero times if the count variable in CX is initialized to
  3673.  zero. Because the LOOP instructions (and repeat prefixes) decrement CX
  3674.  before they test it, a loop will execute 65,536 times if the program enters
  3675.  the loop with a zero value in CX. A programmer may conveniently overcome
  3676.  this problem with JCXZ, which enables the program to branch around the code
  3677.  within the loop if CX is zero when JCXZ executes.
  3678.  
  3679.  Example:
  3680.    JCXZ TARGETLABEL. Causes the program to branch to the instruction labeled
  3681.    TARGETLABEL if CX=0 when the instruction executes.
  3682.  
  3683.  
  3684.  3.6.3  Software-Generated Interrupts
  3685.  
  3686.  The INT n and INTO instructions allow the programmer to specify a transfer
  3687.  to an interrupt service routine from within a program. Interrupts 0-31 are
  3688.  reserved by Intel.
  3689.  
  3690.  
  3691.  3.6.3.1  Software Interrupt Instruction
  3692.  
  3693.  INT n (Software Interrupt) activates the interrupt service routine that
  3694.  corresponds to the number coded within the instruction. Interrupt type 3 is
  3695.  reserved for internal software-generated interrupts. However, the INT
  3696.  instruction may specify any interrupt type to allow multiple types of
  3697.  internal interrupts or to test the operation of a service routine. The
  3698.  interrupt service routine terminates with an IRET instruction that returns
  3699.  control to the instruction that follows INT.
  3700.  
  3701.  Example:
  3702.    INT 3. Transfers control to the interrupt service routine specified by a
  3703.    type 3 interrupt.
  3704.  
  3705.  Example:
  3706.    INT 0. Transfers control to the interrupt service routine specified by a
  3707.    type 0 interrupt, which is reserved for a divide error.
  3708.  
  3709.  INTO (Interrupt on Overflow) invokes a type 4 interrupt if OF is set when
  3710.  the INTO instruction executes. The type 4 interrupt is reserved for this
  3711.  purpose.
  3712.  
  3713.  Example:
  3714.    INTO. If the result of a previous operation has set OF and no intervening
  3715.    operation has reset OF, then INTO invokes a type 4 interrupt. The
  3716.    interrupt service routine terminates with an IRET instruction, which
  3717.    returns control to the instruction following INTO.
  3718.  
  3719.  
  3720.  3.7  Character Translation and String Instructions
  3721.  
  3722.  The instructions in this category operate on characters or string elements
  3723.  rather than on logical or numeric values.
  3724.  
  3725.  
  3726.  3.7.1  Translate Instruction
  3727.  
  3728.  XLAT (Translate) replaces a byte in the AL register with a byte from a
  3729.  user-coded translation table. When XLAT is executed, AL should have the
  3730.  unsigned index to the table addressed by BX. XLAT changes the contents of AL
  3731.  from table index to table entry. BX is unchanged. The XLAT instruction is
  3732.  useful for translating from one coding system to another, such as from
  3733.  ASCII to EBCDIC. The translate table may be up to 256 bytes long. The value
  3734.  placed in the AL register serves as an index to the location of the
  3735.  corresponding translation value. Used with a LOOP instruction, the XLAT
  3736.  instruction can translate a block of codes up to 64K bytes long.
  3737.  
  3738.  Example:
  3739.    XLAT. Replaces the byte in AL with the byte from the translate table that
  3740.    is selected by the value in AL.
  3741.  
  3742.  
  3743.  3.7.2  String Manipulation Instructions and Repeat Prefixes
  3744.  
  3745.  The string instructions (also called primitives) operate on string elements
  3746.  to move, compare, and scan byte or word strings. One-byte repeat prefixes
  3747.  can cause the operation of a string primitive to be repeated to process
  3748.  strings as long as 64K bytes.
  3749.  
  3750.  The repeated string primitives use the direction flag, DF, to specify
  3751.  left-to-right or right-to-left string processing, and use a count in CX to
  3752.  limit the processing operation. These instructions use the register pair
  3753.  DS:SI to point to the source string element and the register pair ES:DI to
  3754.  point to the destination.
  3755.  
  3756.  One of two possible opcodes represent each string primitive, depending on
  3757.  whether it is operating on byte strings or word strings. The string
  3758.  primitives are generic and require one or more operands along with the
  3759.  primitive to determine the size of the string elements being processed.
  3760.  These operands do not determine the addresses of the strings; the addresses
  3761.  must already be present in the appropriate registers.
  3762.  
  3763.  Each repetition of a string operation using the Repeat prefixes includes
  3764.  the following steps:
  3765.  
  3766.    1.  Acknowledge pending interrupts.
  3767.  
  3768.    2.  Check CX for zero and stop repeating if CX is zero.
  3769.  
  3770.    3.  Perform the string operation once.
  3771.  
  3772.    4.  Adjust the memory pointers in DS:SI and ES:DI by incrementing SI
  3773.        and DI if DF is 0 or by decrementing SI and DI if DF is 1.
  3774.  
  3775.    5.  Decrement CX (this step does not affect the flags).
  3776.  
  3777.    6.  For SCAS (Scan String) and CMPS (Compare String), check ZF for a
  3778.        match with the repeat condition and stop repeating if the ZF fails to
  3779.        match.
  3780.  
  3781.  The Load String and Store String instructions allow a program to perform
  3782.  arithmetic or logical operations on string characters (using AX for word
  3783.  strings and AL for byte strings). Repeated operations that include
  3784.  instructions other than string primitives must use the loop instructions
  3785.  rather than a repeat prefix.
  3786.  
  3787.  
  3788.  3.7.2.1  String Movement Instructions
  3789.  
  3790.  REP (Repeat While CX Not Zero) specifies a repeated operation of a string
  3791.  primitive. The REP prefix causes the hardware to automatically repeat the
  3792.  associated string primitive until CX=0. This form of iteration allows the
  3793.  CPU to process strings much faster than would be possible with a regular
  3794.  software loop.
  3795.  
  3796.  When the REP prefix accompanies a MOVS instruction, it operates as a
  3797.  memory-to-memory block transfer. To set up for this operation, the program
  3798.  must initialize CX and the register pairs DS:SI and ES:DI. CX specifies the
  3799.  number of bytes or words in the block.
  3800.  
  3801.  If DF=0, the program must point DS:SI to the first element of the source
  3802.  string and point ES:DI to the destination address for the first element. If
  3803.  DF=1, the program must point these two register pairs to the last element of
  3804.  the source string and to the destination address for the last element,
  3805.  respectively.
  3806.  
  3807.  Example:
  3808.    REP MOVSW. The processor checks the value in CX for zero. If this value is
  3809.    not zero, the processor moves a word from the location pointed to by DS:SI
  3810.    to the location pointed to by ES:DI and increments SI and DI by two (if
  3811.    DF=0). Next, the processor decrements CX by one and returns to the
  3812.    beginning of the repeat cycle to check CX again. After CX decrements to
  3813.    zero, the processor executes the instruction that follows.
  3814.  
  3815.  MOVS (Move String) moves the string character pointed to by the combination
  3816.  of DS and SI to the location pointed to by the combination of ES and DI.
  3817.  This is the only memory-to-memory transfer supported by the instruction set
  3818.  of the base architecture. MOVSB operates on byte elements. The destination
  3819.  segment register cannot be overridden by a segment override prefix while
  3820.  the source segment register can be overridden.
  3821.  
  3822.  Example:
  3823.    MOVSW. Moves the contents of the memory byte pointed to by DS:SI to the
  3824.    location pointed to by ES:DI.
  3825.  
  3826.  
  3827.  3.7.2.2  Other String Operations
  3828.  
  3829.  CMPS (Compare Strings) subtracts the destination string element (ES:DI)
  3830.  from the source string element (DS:SI) and updates the flags AF, SF, PF, CF
  3831.  and OF. If the string elements are equal, ZF=1; otherwise, ZF=0. If DF=0,
  3832.  the processor increments the memory pointers (SI and DI) for the two
  3833.  strings. The segment register used for the source address can be changed
  3834.  with a segment override prefix, while the destination segment register
  3835.  cannot be overridden.
  3836.  
  3837.  Example:
  3838.    CMPSB. Compares the source and destination string elements with each other
  3839.    and returns the result of the comparison to ZF.
  3840.  
  3841.  SCAS (Scan String) subtracts the destination string element at ES:DI from
  3842.  AX or AL and updates the flags AF, SF, ZF, PF, CF and OF. If the values are
  3843.  equal, ZF=1; otherwise, ZF=0. If DF=0, the processor increments the memory
  3844.  pointer (DI) for the string. The segment register used for the source
  3845.  address can be changed with a segment override prefix while the destination
  3846.  segment register cannot be overridden.
  3847.  
  3848.  Example:
  3849.    SCASW. Compares the value in AX with the destination string element.
  3850.  
  3851.  REPE/REPZ (Repeat While CX Equal/Zero) and REPNE/REPNZ (Repeat While CX Not
  3852.  Equal/Not Zero) are the prefixes that are used exclusively with the SCAS
  3853.  (ScanString) and CMPS (Compare String) primitives.
  3854.  
  3855.  The difference between these two types of prefix bytes is that REPE/REPZ
  3856.  terminates when ZF=0 and REPNE/REPNZ terminates when ZF=1. ZF does not
  3857.  require initialization before execution of a repeated string instruction.
  3858.  
  3859.  When these prefixes modify either the SCAS or CMPS primitives, the
  3860.  processor compares the value of the current string element with the value in
  3861.  AX for word elements or with the value in AL for byte elements. The
  3862.  resulting state of ZF can then limit the operation of the repeated
  3863.  operation as well as a zero value in CX.
  3864.  
  3865.  Example:
  3866.    REPE SCASB. Causes the processor to scan the string pointed to by ES:DI
  3867.    until it encounters a match with the byte value in AL or until CX
  3868.    decrements to zero.
  3869.  
  3870.  LODS (Load String) places the source string element at DS:SI into AX for
  3871.  word strings or into AL for byte strings.
  3872.  
  3873.  Example:
  3874.    LODSW. Loads AX with the value pointed to by DS:SI.
  3875.  
  3876.  
  3877.  3.8  Address Manipulation Instructions
  3878.  
  3879.  The set of address manipulation instructions provide a way to perform
  3880.  address calculations or to move to a new data segment or extra segment.
  3881.  
  3882.  LEA (Load Effective Address) transfers the offset of the source operand
  3883.  (rather than its value) to the destination operand. The source operand must
  3884.  be a memory operand, and the destination operand must be a 16-bit general
  3885.  register (AX, DX, BX, CX, BP, SP, SI, or DI).
  3886.  
  3887.  LEA does not affect any flags. This instruction is useful for initializing
  3888.  the registers before the execution of the string primitives or the XLAT
  3889.  instruction.
  3890.  
  3891.  Example:
  3892.    LEA BX EBCDIC_TABLE. Causes the processor to place the address of the
  3893.    starting location of the table labeled EBCDIC_TABLE into BX.
  3894.  
  3895.  LDS (Load Pointer Using DS) transfers a 32-bit pointer variable from the
  3896.  source operand to DS and the destination register. The source operand must
  3897.  be a memory operand, and the destination operand must be a 16-bit general
  3898.  register (AX, DX, BX, CX, BP, SP, SI or DI). DS receives the high-order
  3899.  segment word of the pointer. The destination register receives the
  3900.  low-order word, which points to a specific location within the segment.
  3901.  
  3902.  Example:
  3903.    LDS SI, STRING_X. Loads DS with the word identifying the segment pointed
  3904.    to by STRING_X, and loads the offset of STRING_X into SI. Specifying SI as
  3905.    the destination operand is a convenient way to prepare for a string
  3906.    operation on a source string that is not in the current data segment.
  3907.  
  3908.  LES (Load Pointer Using ES) operates identically to LDS except that ES
  3909.  receives the offset word rather than DS.
  3910.  
  3911.  Example:
  3912.    LES DI, DESTINATION_X. Loads ES with the word identifying the segment
  3913.    pointed to by DESTINATION_X, and loads the offset of DESTINATION_X into
  3914.    DI. This instruction provides a convenient way to select a destination for
  3915.    a string operation if the desired location is not in the current extra
  3916.    segment.
  3917.  
  3918.  
  3919.  3.9  Flag Control Instructions
  3920.  
  3921.  The flag control instructions provide a method of changing the state of
  3922.  bits in the flag register.
  3923.  
  3924.  
  3925.  3.9.1  Carry Flag Control Instructions
  3926.  
  3927.  The carry flag instructions are useful in conjunction with
  3928.  rotate-with-carry instructions RCL and RCR. They can initialize the carry
  3929.  flag, CF, to a known state before execution of a rotate that moves the carry
  3930.  bit into one end of the rotated operand.
  3931.  
  3932.  STC (Set Carry Flag) sets the carry flag (CF) to 1.
  3933.  
  3934.  Example:
  3935.    STC
  3936.  
  3937.  CLC (Clear Carry Flag) zeros the carry flag (CF).
  3938.  
  3939.  Example:
  3940.    CLC
  3941.  
  3942.  CMC (Complement Carry Flag) reverses the current status of the carry flag
  3943.  (CF).
  3944.  
  3945.  Example:
  3946.    CMC
  3947.  
  3948.  
  3949.  3.9.2  Direction Flag Control Instructions
  3950.  
  3951.  The direction flag control instructions are specifically included to set or
  3952.  clear the direction flag, DF, which controls the left-to-right or
  3953.  right-to-left direction of string processing. IF DF=0, the processor
  3954.  automatically increments the string memory pointers, SI and DI, after each
  3955.  execution of a string primitive. If DF=1, the processor decrements these
  3956.  pointer values. The initial state of DF is 0.
  3957.  
  3958.  CLD (Clear Direction Flag) zeros DF, causing the string instructions to
  3959.  auto-increment SI and/or DI. CLD does not affect any other flags.
  3960.  
  3961.  Example:
  3962.    CLD
  3963.  
  3964.  STD (Set Direction Flag) sets DF to 1, causing the string instructions to
  3965.  auto-decrement SI and/or DI. STD does not affect any other flags.
  3966.  
  3967.  Example:
  3968.    STD
  3969.  
  3970.  
  3971.  3.9.3  Flag Transfer Instructions
  3972.  
  3973.  Though specific instructions exist to alter CF and DF, there is no direct
  3974.  method of altering the other flags. The flag transfer instructions allow a
  3975.  program to alter the other flag bits with the bit manipulation instructions
  3976.  after transferring these flags to the stack or the AH register.
  3977.  
  3978.  The PUSHF and POPF instructions are also useful for preserving the state of
  3979.  the flag register before executing a procedure.
  3980.  
  3981.  LAHF (Load AH from Flags) copies SF, ZF, AF, PF, and CF to AH bits 7, 6, 4,
  3982.  2, and 0, respectively (see figure 3-13). The contents of the remaining
  3983.  bits (5, 3, and 1) are undefined. The flags remain unaffected. This
  3984.  instruction can assist in converting 8080/8085 assembly language programs to
  3985.  run on the base architecture of the 8086, 8088, 80186, 80188, and 80286.
  3986.  
  3987.  Example:
  3988.    LAHF
  3989.  
  3990.  SAHF (Store AH into Flags) transfers bits 7, 6, 4, 2, and 0 from AH into
  3991.  SF, ZF, AF, PF, and CF, respectively (see figure 3-13). This instruction
  3992.  also provides 8080/8085 compatibility with the 8086, 8088, 80186, 80188, and
  3993.  80286.
  3994.  
  3995.  Example:
  3996.    SAHF
  3997.  
  3998.  PUSHF (Push Flags) decrements SP by two and then transfers all flags to the
  3999.  word at the top of stack pointed to by SP (see figure 3-14). The flags
  4000.  remain unaffected. This instruction enables a procedure to save the state of
  4001.  the flag register for later use.
  4002.  
  4003.  Example:
  4004.    PUSHF
  4005.  
  4006.  POPF (Pop Flags) transfers specific bits from the word at the top of stack
  4007.  into the low-order byte of the flag register (see figure 3-14). The
  4008.  processor then increments SP by two.
  4009.  
  4010.  Note that an application program in the protected virtual address mode may
  4011.  not alter IOPL (the I/O privilege level flag) unless the program is
  4012.  executing at privilege level 0. A program may alter IF (the interrupt flag)
  4013.  only when executing at a level that is at least as privileged as IOPL.
  4014.  
  4015.  Procedures may use this instruction to restore the flag status from a
  4016.  previous value.
  4017.  
  4018.  Example:
  4019.    POPF
  4020.  
  4021.  
  4022.  Figure 3-13.  LAHF and SAHF
  4023.  
  4024.                          7    6    5    4    3    2    1    0
  4025.                     ╔════╤════╤════╤════╤════╤════╤════╤════╗
  4026.                     ║ SF │ ZF │▒▒▒▒│ AF │▒▒▒▒│ PF │▒▒▒▒│ CF ║
  4027.                     ╚════╧════╧════╧════╧════╧════╧════╧════╝
  4028.                                    REGISTER AH
  4029.  
  4030.   LAHF loads five flags from the flag register into register AH. SAHF stores
  4031.   these same five flgs from AH into the flag register. The bit position of
  4032.   each flag is the same in AH as it is in the flag register. The remaining
  4033.   bits are indeterminate.
  4034.  
  4035.  
  4036.  Figure 3-14.  PUSHF and POPF
  4037.  
  4038.          15  14  13  12  11  10   9   8   7   6   5   4   3   2   1   0
  4039.       ╔═══╤═══╤═══╤═══╤═══╤═══╤═══╤═══╤═══╤═══╤═══╤═══╤═══╤═══╤═══╤═══╗
  4040.       ║▒▒▒│NT │  IOPL │OF │DF │IF │TF │SF │ZF │▒▒▒│AF │▒▒▒│PF │▒▒▒│CF ║
  4041.       ╚═══╧═══╧═══╧═══╧═══╧═══╧═══╧═══╧═══╧═══╧═══╧═══╧═══╧═══╧═══╧═══╝
  4042.                                 STACK WORD
  4043.  
  4044.   PUSHF decrements SP by 2 bytes (1 word) and copies the contents of the flag
  4045.   register to the top of the stack. POPF loads the flag register with the
  4046.   contents of the last word pushed onto the stack. The bit position of each
  4047.   flag is the same in the stack word as it is in the flag register. Only
  4048.   programs executing at the highest privilege level (level 0) may alter the
  4049.   2-bit IOPL flag. Only programs executing at a level at least as privileged
  4050.   as that indicated by IOPL may alter IF.
  4051.  
  4052.  
  4053.  3.10  Binary-Coded Decimal Arithmetic Instructions
  4054.  
  4055.  These instructions adjust the results of a previous arithmetic operation to
  4056.  produce a valid packed or unpacked decimal result. These instructions
  4057.  operate only on AL or AH registers.
  4058.  
  4059.  
  4060.  3.10.1  Packed BCD Adjustment Instructions
  4061.  
  4062.  DAA (Decimal Adjust) corrects the result of adding two valid packed decimal
  4063.  operands in AL. DAA must always follow the addition of two pairs of packed
  4064.  decimal numbers (one digit in each nibble) to obtain a pair of valid packed
  4065.  decimal digits as results. The carry flag will be set if carry was needed.
  4066.  
  4067.  Example:
  4068.    DAA
  4069.  
  4070.  DAS (Decimal Adjust for Subtraction) corrects the result of subtracting two
  4071.  valid packed decimal operands in AL. DAS must always follow the subtraction
  4072.  of one pair of packed decimal numbers (one digit in each nibble) from
  4073.  another to obtain a pair of valid packed decimal digits as results. The
  4074.  carry flag will be set if a borrow was needed.
  4075.  
  4076.  Example:
  4077.    DAS
  4078.  
  4079.  
  4080.  3.10.2  Unpacked BCD Adjustment Instructions
  4081.  
  4082.  AAA (ASCII Adjust for Addition) changes the contents of register AL to a
  4083.  valid unpacked decimal number, and zeros the top 4 bits. AAA must always
  4084.  follow the addition of two unpacked decimal operands in AL. The carry flag
  4085.  will be set and AH will be incremented if a carry was necessary.
  4086.  
  4087.  Example:
  4088.    AAA
  4089.  
  4090.  AAS (ASCII Adjust for Subtraction) changes the contents of register AL to a
  4091.  valid unpacked decimal number, and zeros the top 4 bits. AAS must always
  4092.  follow the subtraction of one unpacked decimal operand from another in AL.
  4093.  The carry flag will be set and AH decremented if a borrow was necessary.
  4094.  
  4095.  Example:
  4096.    AAS
  4097.  
  4098.  AAM (ASCII Adjust for Multiplication) corrects the result of a
  4099.  multiplication of two valid unpacked decimal numbers. AAM must always follow
  4100.  the multiplication of two decimal numbers to produce a valid decimal result.
  4101.  The high order digit will be left in AH, the low order digit in AL.
  4102.  
  4103.  Example:
  4104.    AAM
  4105.  
  4106.  AAD (ASCII Adjust for Division) modifies the numerator in AH and AL to
  4107.  prepare for the division of two valid unpacked decimal operands so that the
  4108.  quotient produced by the division will be a valid unpacked decimal number.
  4109.  AH should contain the high-order digit and AL the low-order digit. This
  4110.  instruction will adjust the value and leave it in AL. AH will contain 0.
  4111.  
  4112.  Example:
  4113.    AAD
  4114.  
  4115.  
  4116.  3.11  Trusted Instructions
  4117.  
  4118.  When operating in Protected Mode (Chapter 6 and following), the 80286
  4119.  processor restricts the execution of trusted instructions according to the
  4120.  Current Privilege Level (CPL) and the current value of IOPL, the 2-bit I/O
  4121.  privilege flag. Only a program operating at the highest privilege level
  4122.  (level 0) may alter the value of IOPL. A program may execute trusted
  4123.  instructions only when executing at a level that is at least as privileged
  4124.  as that specified by IOPL.
  4125.  
  4126.  Trusted instructions control I/O operations, interprocessor communications
  4127.  in a multiprocessor system, interrupt enabling, and the HLT instruction.
  4128.  
  4129.  These protection considerations do not apply in the real address mode.
  4130.  
  4131.  
  4132.  3.11.1  Trusted and Privileged Restrictions on POPF and IRET
  4133.  
  4134.  POPF (POP Flags) and IRET (Interrupt Return) are not affected by IOPL
  4135.  unless they attempt to alter IF (flag register bit 9). To change IF, POPF
  4136.  must be part of a program that is executing at a privilege level greater
  4137.  than or equal to that specified by IOPL. Any attempt to change IF when
  4138.  CPL ≥ 0 will be ignored (i.e., the IF flag will be ignored). To change the
  4139.  IOPL field, CPL must be zero.
  4140.  
  4141.  
  4142.  3.11.2  Machine State Instructions
  4143.  
  4144.  These trusted instructions affect the machine state control interrupt
  4145.  response, the processor halt state, and the bus LOCK signal that regulates
  4146.  memory access in multiprocessor systems.
  4147.  
  4148.  CLI (Clear Interrupt-Enable Flag) and STI (Set Interrupt-Enable Flag) alter
  4149.  bit 9 in the flag register. When IF=0, the processor responds only to
  4150.  internal interrupts and to non-maskable external interrupts. When IF=1, the
  4151.  processor responds to all interrupts. An interrupt service routine might
  4152.  use these instructions to avoid further interruption while it processes a
  4153.  previous interrupt request. As with the other flag bits, the processor
  4154.  clears IF during initialization. These instructions may be executed only if
  4155.  CPL ≤ IOPL. A protection exception will occur if they are executed when
  4156.  CPL > IOPL.
  4157.  
  4158.  Example:
  4159.    STI. Sets IF=1, which enables the processing of maskable external
  4160.    interrupts.
  4161.  
  4162.  Example:
  4163.    CLI. Sets IF=0 to disable maskable interrupt processing.
  4164.  
  4165.  HLT (Halt) causes the processor to suspend processing operations pending an
  4166.  interrupt or a system reset. This trusted instruction provides an
  4167.  alternative to an endless software loop in situations where a program must
  4168.  wait for an interrupt. The return address saved after the interrupt will
  4169.  point to the instruction immediately following HLT. This instruction may be
  4170.  executed only when CPL = 0.
  4171.  
  4172.  Example:
  4173.    HLT
  4174.  
  4175.  LOCK (Assert Bus Lock) is a 1-byte prefix code that causes the processor to
  4176.  assert the bus LOCK signal during execution of the instruction that follows.
  4177.  LOCK does not affect any flags. LOCK may be used only when CPL ≤ IOPL. A
  4178.  protection exception will occur if LOCK is used when CPL > IOPL.
  4179.  
  4180.  
  4181.  3.11.3  Input and Output Instructions
  4182.  
  4183.  These trusted instructions provide access to the processor's I/O ports to
  4184.  transfer data to and from peripheral devices. In Protected Mode, these
  4185.  instructions may be executed only when CPL ≤ IOPL.
  4186.  
  4187.  IN (Input from Port) transfers a byte or a word from an input port to AL or
  4188.  AX. If a program specifies AL with the IN instruction, the processor
  4189.  transfers 8 bits from the selected port to AL. Alternately, if a program
  4190.  specifies AX with the IN instruction, the processor transfers 16 bits from
  4191.  the port to AX.
  4192.  
  4193.  The program can specify the number of the port in two ways. Using an
  4194.  immediate byte constant, the program can specify 256 8-bit ports numbered 0
  4195.  through 255 or 128 16-bit ports numbered 0,2,4,...,252,254. Using the
  4196.  current value contained in DX, the program can specify 8-bit ports numbered
  4197.  0 through 65,535, or 16-bit ports using even-numbered ports in the same
  4198.  range.
  4199.  
  4200.  Example:
  4201.    IN AL,
  4202.    BYTE_PORT_NUMBER. Transfers 8 bits to AL from the port identified by the
  4203.    immediate constant BYTE_PORT_NUMBER.
  4204.  
  4205.  OUT (Output to Port) transfers a byte or a word to an output port from AL
  4206.  or AX. The program can specify the number of the port using the same methods
  4207.  of the IN instruction.
  4208.  
  4209.  Example:
  4210.    OUT AX, DX. Transfers 16 bits from AX to the port identified by the 16-bit
  4211.    number contained in DX.
  4212.  
  4213.  INS and OUTS (Input String and Output String) cause block input or output
  4214.  operations using a Repeat prefix. See Chapter 4 for more information on INS
  4215.  and OUTS.
  4216.  
  4217.  
  4218.  3.12  Processor Extension Instructions
  4219.  
  4220.  Processor Extension provides an extension to the instruction set of the
  4221.  base architecture (e.g., 80287). The NPX extends the instruction set of the
  4222.  CPU-based architecture to support high-precision integer and floating-point
  4223.  calculations. This extended instruction set includes arithmetic,
  4224.  comparison, transcendental, and data transfer instructions. The NPX also
  4225.  contains a set of useful constants to enhance the speed of numeric
  4226.  calculations.
  4227.  
  4228.  A program contains instructions for the NPX in line with the instructions
  4229.  for the CPU. The system executes these instructions in the same order as
  4230.  they appear in the instruction stream. The NPX operates concurrently with
  4231.  the CPU to provide maximum throughput for numeric calculations.
  4232.  
  4233.  The software emulation of the NPX is transparent to application software
  4234.  but requires more time for execution.
  4235.  
  4236.  
  4237.  3.12.1  Processor Extension Synchronization Instructions
  4238.  
  4239.  Escape and wait instructions allow a processor extension such as the 80287
  4240.  NPX to obtain instructions and data from the system bus and to wait for the
  4241.  NPX to return a result.
  4242.  
  4243.  ESC (Escape) identifies floating point numeric instructions and allows the
  4244.  80286 to send the opcode to the NPX or to transfer a memory operand to the
  4245.  NPX. The 80287 NPX uses the Escape instructions to perform high-performance,
  4246.  high-precision floating point arithmetic that conforms to the IEEE floating
  4247.  point standard 754.
  4248.  
  4249.  Example:
  4250.    ESC 6, ARRAY [SI]. The CPU sends the escape opcode 6 and the location of
  4251.    the array pointed to by SI to the NPX.
  4252.  
  4253.  WAIT (Wait) suspends program execution until the 80286 CPU detects a signal
  4254.  on the BUSY pin. In a configuration that includes a numeric processor
  4255.  extension, the NPX activates the BUSY pin to signal that it has completed
  4256.  its processing task and that the CPU may obtain the results.
  4257.  
  4258.  Example:
  4259.    WAIT
  4260.  
  4261.  
  4262.  3.12.2  Numeric Data Processor Instructions
  4263.  
  4264.  This section describes the categories of instructions available with
  4265.  Numeric Data Processor systems that include a Numeric Processor Extension or
  4266.  a software emulation of this processor extension.
  4267.  
  4268.  
  4269.  3.12.2.1  Arithmetic Instructions
  4270.  
  4271.  The extended instruction set includes not only the four arithmetic
  4272.  operations (add, subtract, multiply, and divide), but also subtract-reversed
  4273.  and divide-reversed instructions. The arithmetic functions include square
  4274.  root, modulus, absolute value, integer part, change sign, scale exponent,
  4275.  and extract exponent instructions.
  4276.  
  4277.  
  4278.  3.12.2.2  Comparison Instructions
  4279.  
  4280.  The comparison operations are the compare, examine, and test instructions.
  4281.  Special forms of the compare instruction can optimize algorithms by allowing
  4282.  comparisons of binary integers with real numbers in memory.
  4283.  
  4284.  
  4285.  3.12.2.3  Transcendental Instructions
  4286.  
  4287.  The instructions in this group perform the otherwise time-consuming
  4288.  calculations for all common trigonometric, inverse trigonometric,
  4289.  hyperbolic, inverse hyperbolic, logarithmic, and exponential functions. The
  4290.  transcendental instructions include tangent, arctangent, 2 x-1, Y. log{2} X,
  4291.  and Y. log{2} (X+1).
  4292.  
  4293.  
  4294.  3.12.2.4  Data Transfer Instructions
  4295.  
  4296.  The data transfer instructions move operands among the registers and
  4297.  between a register and memory. This group includes the load, store, and
  4298.  exchange instructions.
  4299.  
  4300.  
  4301.  3.12.2.5  Constant Instructions
  4302.  
  4303.  Each of the constant instructions loads a commonly used constant into an
  4304.  NPX register. The values have a real precision of 64 bits and are accurate
  4305.  to approximately 19 decimal places. The constants loaded by these
  4306.  instructions include 0, 1, Pi, log{e} 10, log{2} e, log{10} 2, and log 2{e}.
  4307.  
  4308.  
  4309.  Chapter 4  Extended Instruction Set
  4310.  
  4311.  ───────────────────────────────────────────────────────────────────────────
  4312.  
  4313.  The instructions described in this chapter extend the capabilities of the
  4314.  base architecture instruction set described in Chapter 3. These extensions
  4315.  consist of new instructions and variations of some instructions that are not
  4316.  strictly part of the base architecture (in other words, not included on the
  4317.  8086 and 8088). These instructions are also available on the 80186 and
  4318.  80188. The instruction variations, described in Chapter 3, include the
  4319.  immediate forms of the PUSH and MUL instructions, PUSHA, POPA, and the
  4320.  privilege level restrictions on POPF.
  4321.  
  4322.  New instructions described in this chapter include the string input and
  4323.  output instructions (INS and OUTS), the ENTER procedure and LEAVE procedure
  4324.  instructions, and the check index BOUND instruction.
  4325.  
  4326.  
  4327.  4.1  Block I/O Instructions
  4328.  
  4329.  REP, the Repeat prefix, modifies INS and OUTS (the string I/O instructions)
  4330.  to provide a means of transferring blocks of data between an I/O port and
  4331.  Memory. These block I/O instructions are string primitives. They simplify
  4332.  programming and increase the speed of data transfer by eliminating the need
  4333.  to use a separate LOOP instruction or an intermediate register to hold the
  4334.  data.
  4335.  
  4336.  INS and OUTS are trusted instructions. To use trusted instructions, a
  4337.  program must execute at a privilege level at least as privileged as that
  4338.  specified by the 2-bit IOPL flag (CPL ≤ IOPL). Any attempt by a
  4339.  less-privileged program to use a trusted instruction results in a
  4340.  protection exception. See Chapter 7 for information on protection concepts.
  4341.  
  4342.  One of two possible opcodes represents each string primitive depending on
  4343.  whether it operates on byte strings or word strings. After each transfer,
  4344.  the memory address in SI or DI is updated by 1 for byte values and by 2 for
  4345.  word values. The value in the DF field determines if SI or DI is to be auto
  4346.  incremented (DF=0) or auto decremented (DF=1).
  4347.  
  4348.  INS and OUTS use DX to specify I/O ports numbered 0 through 65,535 or
  4349.  16-bit ports using only even port addresses in the same range.
  4350.  
  4351.  INS (Input String from Port) transfers a byte or a word string element from
  4352.  an input port to memory. If a program specifies INSB, the processor
  4353.  transfers 8 bits from the selected port to the memory location indicated by
  4354.  ES:DI. Alternately, if a program specifies INSW, the processor transfers 16
  4355.  bits from the port to the memory location indicated by ES:DI. The
  4356.  destination segment register choice (ES) cannot be changed for the INS
  4357.  instruction.
  4358.  
  4359.  Combined with the REP prefix, INS moves a block of information from an
  4360.  input port to a series of consecutive memory locations.
  4361.  
  4362.  Example:
  4363.    REP INSB. The processor repeatedly transfers 8 bits to the memory
  4364.    location indicated by ES:DI from the port selected by the 16-bit port
  4365.    number contained in DX. Following each byte transfer, the CPU
  4366.    decrements CX. The instruction terminates the block transfer when CX=0.
  4367.    After decrementing CX, the processor increments DI by one if DF=0. It
  4368.    decrements DI by one if DF=1.
  4369.  
  4370.  OUTS (Output String to Port) transfers a byte or a word string element to
  4371.  an output port from memory. Combined with the REP prefix, OUTS moves a block
  4372.  of information from a series of consecutive memory locations indicated by
  4373.  DS:SI to an output port.
  4374.  
  4375.  Example:
  4376.    REP OUTS WSTRING. Assuming that the program declares WSTRING to be a
  4377.    word-length string element, the assembler uses the 16-bit form of the OUTS
  4378.    instruction to create the object code for the program. The processor
  4379.    repeatedly transfers words from the memory locations indicated by DI to
  4380.    the output port selected by the 16-bit port number in DX.
  4381.  
  4382.  Following each word transfer, the CPU decrements CX. The instruction
  4383.  terminates the block transfer when CX=0. After decrementing CX, the
  4384.  processor increments SI by two to point to the next word in memory if DF=0;
  4385.  it decrements SI by two if DF=1.
  4386.  
  4387.  
  4388.  4.2  High-Level Instructions
  4389.  
  4390.  The instructions in this section provide machine-language functions
  4391.  normally found only in high-level languages. These instructions include
  4392.  ENTER and LEAVE, which simplify the programming of procedures, and BOUND,
  4393.  which provides a simple method of testing an index against its predefined
  4394.  range.
  4395.  
  4396.  ENTER (Enter Procedure) creates the stack frame required by most
  4397.  block-structured high-level languages. A LEAVE instruction at the end of a
  4398.  procedure complements an ENTER at the beginning of the procedure to simplify
  4399.  stack management and to control access to variables for nested procedures.
  4400.  
  4401.  Example:
  4402.    ENTER 2048,3. Allocates 2048 bytes of dynamic storage on the stack and
  4403.    sets up pointers to two previous stack frames in the stack frame that
  4404.    ENTER creates for this procedure.
  4405.  
  4406.  The ENTER instruction includes two parameters. The first parameter
  4407.  specifies the number of bytes of dynamic storage to be allocated on the
  4408.  stack for the routine being entered. The second parameter corresponds to the
  4409.  lexical nesting level (0-31) of the routine. (Note that the lexical level
  4410.  has no relationship to either the protection privilege levels or to the I/O
  4411.  privilege level.)
  4412.  
  4413.  The specified lexical level determines how many sets of stack frame
  4414.  pointers the CPU copies into the new stack frame from the preceding frame.
  4415.  This list of stack frame pointers is sometimes called the "display." The
  4416.  first word of the display is a pointer to the last stack frame. This
  4417.  pointer enables a LEAVE instruction to reverse the action of the previous
  4418.  ENTER instruction by effectively discarding the last stack frame.
  4419.  
  4420.  After ENTER creates the new display for a procedure, it allocates the
  4421.  dynamic storage space for that procedure by decrementing SP by the number of
  4422.  bytes specified in the first parameter. This new value of SP serves as a
  4423.  base for all PUSH and POP operations within that procedure.
  4424.  
  4425.  To enable a procedure to address its display, ENTER leaves BP pointing to
  4426.  the beginning of the new stack frame. Data manipulation instructions that
  4427.  specify BP as a base register implicitly address locations within the stack
  4428.  segment instead of the data segment. Two forms of the ENTER instruction
  4429.  exist: nested and non-nested. If the lexical level is 0, the non-nested form
  4430.  is used. Since the second operand is 0, ENTER pushes BP, copies SP to BP and
  4431.  then subtracts the first operand from SP. The nested form of ENTER occurs
  4432.  when the second parameter (lexical level) is not 0. Figure 4-1 gives the
  4433.  formal definition of ENTER.
  4434.  
  4435.  The main procedure (with other procedures nested within) operates at the
  4436.  highest lexical level, level 1. The first procedure it calls operates at the
  4437.  next deeper lexical level, level 2. A level 2 procedure can access the
  4438.  variables of the main program which are at fixed locations specified by the
  4439.  compiler. In the case of level 1, ENTER allocates only the requested dynamic
  4440.  storage on the stack because there is no previous display to copy.
  4441.  
  4442.  A program operating at a higher lexical level calling a program at a lower
  4443.  lexical level requires that the called procedure should have access to the
  4444.  variables of the calling program. ENTER provides this access through a
  4445.  display that provides addressability to the calling program's stack frame.
  4446.  
  4447.  A procedure calling another procedure at the same lexical level implies
  4448.  that they are parallel procedures and that the called procedure should not
  4449.  have access to the variables of the calling procedure. In this case, ENTER
  4450.  copies only that portion of the display from the calling procedure which
  4451.  refers to previously nested procedures operating at higher lexical levels.
  4452.  The new stack frame does not include the pointer for addressing the calling
  4453.  procedure's stack frame.
  4454.  
  4455.  ENTER treats a reentrant procedure as a procedure calling another procedure
  4456.  at the same lexical level. In this case, each succeeding iteration of the
  4457.  reentrant procedure can address only its own variables and the variables of
  4458.  the calling procedures at higher lexical levels. A reentrant procedure can
  4459.  always address its own variables; it does not require pointers to the stack
  4460.  frames of previous iterations.
  4461.  
  4462.  By copying only the stack frame pointers of procedures at higher lexical
  4463.  levels, ENTER makes sure that procedures access only those variables of
  4464.  higher lexical levels, not those at parallel lexical levels (see figure
  4465.  4-2). Figures 4-2a, 4-2b, 4-2c, and 4-2d demonstrate the actions of the
  4466.  ENTER instruction if the modules shown in figure 4-1 were to call one
  4467.  another in alphabetic order.
  4468.  
  4469.  Block-structured high-level languages can use the lexical levels defined by
  4470.  ENTER to control access to the variables of previously nested procedures.
  4471.  For example, if PROCEDURE A calls PROCEDURE B which, in turn, calls
  4472.  PROCEDURE C, then PROCEDURE C will have access to the variables of MAIN and
  4473.  PROCEDURE A, but not PROCEDURE B because they operate at the same lexical
  4474.  level. Following is the complete definition of the variable access for
  4475.  figure 4-2.
  4476.  
  4477.    1.  MAIN PROGRAM has variables at fixed locations.
  4478.  
  4479.    2.  PROCEDURE A can access only the fixed variables of MAIN.
  4480.  
  4481.    3.  PROCEDURE B can access only the variables of PROCEDURE A and MAIN.
  4482.        PROCEDURE B cannot access the variables of PROCEDURE C or PROCEDURE D.
  4483.  
  4484.    4.  PROCEDURE C can access only the variables of PROCEDURE A and MAIN.
  4485.        PROCEDURE C cannot access the variables of PROCEDURE B or PROCEDURE D.
  4486.  
  4487.    5.  PROCEDURE D can access the variables of PROCEDURE C, PROCEDURE A, and
  4488.        MAIN. PROCEDURE D cannot access the variables of PROCEDURE B.
  4489.  
  4490.  ENTER at the beginning of the MAIN PROGRAM creates dynamic storage space
  4491.  for MAIN but copies no pointers. The first and only word in the display
  4492.  points to itself because there is no previous value for LEAVE to return to
  4493.  BP. See figure 4-2a.
  4494.  
  4495.  After MAIN calls PROCEDURE A, ENTER creates a new display for PROCEDURE A
  4496.  with the first word pointing to the previous value of BP (BPM for LEAVE to
  4497.  return to the MAIN stack frame) and the second word pointing to the current
  4498.  value of BP. Procedure A can access variables in MAIN since MAIN is at level
  4499.  1. Therefore the base for the dynamic storage for MAIN is at [BP-2]. All
  4500.  dynamic variables for MAIN will be at a fixed offset from this value. See
  4501.  figure 4-2b.
  4502.  
  4503.  After PROCEDURE A calls PROCEDURE B, ENTER creates a new display for
  4504.  PROCEDURE B with the first word pointing to the previous value of BP, the
  4505.  second word pointing to the value of BP for MAIN, and the third word
  4506.  pointing to the value of BP for A and the last word pointing to the current
  4507.  BP. B can access variables in A and MAIN by fetching from the display the
  4508.  base addresses of the respective dynamic storage areas. See figure 4-2c.
  4509.  
  4510.  After PROCEDURE B calls PROCEDURE C, ENTER creates a new display for
  4511.  PROCEDURE C with the first word pointing to the previous value of BP, the
  4512.  second word pointing to the value of BP for MAIN, and the third word
  4513.  pointing to the BP value for A and the third word pointing to the current
  4514.  value of BP. Because PROCEDURE B and PROCEDURE C have the same lexical
  4515.  level, PROCEDURE C is not allowed access to variables in B and therefore
  4516.  does not receive a pointer to the beginning of PROCEDURE B's stack frame.
  4517.  See figure 4-2d.
  4518.  
  4519.  LEAVE (Leave Procedure) reverses the action of the previous ENTER
  4520.  instruction. The LEAVE instruction does not include any operands.
  4521.  
  4522.  Example:
  4523.    LEAVE. First, LEAVE copies BP to SP to release all stack space allocated
  4524.    to the procedure by the most recent ENTER instruction. Next, LEAVE pops
  4525.    the old value of BP from the stack. A subsequent RET instruction can then
  4526.    remove any arguments that were pushed on the stack by the calling program
  4527.    for use by the called procedure.
  4528.  
  4529.  BOUND (Detect Value Out of Range) verifies that the signed value contained
  4530.  in the specified register lies within specified limits. An interrupt (INT 5)
  4531.  occurs if the value contained in the register is less than the lower bound
  4532.  or greater than the upper bound.
  4533.  
  4534.  The BOUND instruction includes two operands. The first operand specifies
  4535.  the register being tested. The second operand contains the effective
  4536.  relative address of the two signed BOUND limit values. The BOUND instruction
  4537.  assumes that it can obtain the upper limit from the memory word that
  4538.  immediately follows the lower limit. These limit values cannot be register
  4539.  operands; if they are, an invalid opcode exception occurs.
  4540.  
  4541.  BOUND is useful for checking array bounds before using a new index value to
  4542.  access an element within the array. BOUND provides a simple way to check the
  4543.  value of an index register before the program overwrites information in a
  4544.  location beyond the limit of the array.
  4545.  
  4546.  The two-word block of memory that specifies the lower and upper limits of
  4547.  an array might typically reside just before the array itself. This makes the
  4548.  array bounds accessible at a constant offset of -4 from the beginning of the
  4549.  array. Because the address of the array will already be present in a
  4550.  register, this practice avoids extra calculations to obtain the effective
  4551.  address of the array bounds.
  4552.  
  4553.  Example:
  4554.    BOUND BX,ARRAY-4. Compares the value in BX with the lower limit at
  4555.    address ARRAY-4 and the upper limit at address ARRAY-2. If the signed
  4556.    value in BX is less than the lower bound or greater than the upper bound,
  4557.    the interrupt for this instruction (INT 5) occurs. Otherwise, this
  4558.    instruction has no effect.
  4559.  
  4560.  
  4561.  Figure 4-1. Formal Definition of the ENTER Instruction
  4562.  
  4563.  The Formal Definition Of The ENTER Instruction. For All Cases Is Given By
  4564.  The Following Listing. LEVEL Denotes The Value Of The Second Operand.
  4565.  
  4566.  Push BP
  4567.  Set a temporary value FRAME_PTR:=SP
  4568.  If LEVEL > 0 then
  4569.     Repeat (LEVEL - 1) times:
  4570.        BP:=BP - 2
  4571.        Push the word pointed to by BP
  4572.     End repeat
  4573.     Push FRAME_PTR
  4574.  End if
  4575.  BP:=FRAME_PTR
  4576.  SP:=SP - first operand.
  4577.  
  4578.  
  4579.  Figure 4-2.  Variable Access in Nested Procedures
  4580.  
  4581.                     ╔═════════════════════════════════════════╗
  4582.                     ║      MAIN PROGRAM (LEXICAL LEVEL 1)     ║
  4583.                     ║ ╔═════════════════════════════════════╗ ║
  4584.                     ║ ║    PROCEDURE A (LEXICAL LEVEL 2)    ║ ║
  4585.                     ║ ║   ╔═════════════════════════════╗   ║ ║
  4586.                     ║ ║   ║PROCEDURE B (LEXICAL LEVEL 3)║   ║ ║
  4587.                     ║ ║   ╚═════════════════════════════╝   ║ ║
  4588.                     ║ ║                                     ║ ║
  4589.                     ║ ║ ╔═════════════════════════════════╗ ║ ║
  4590.                     ║ ║ ║  PROCEDURE C (LEXICAL LEVEL 3)  ║ ║ ║
  4591.                     ║ ║ ║ ╔═════════════════════════════╗ ║ ║ ║
  4592.                     ║ ║ ║ ║PROCEDURE D (LEXICAL LEVEL 4)║ ║ ║ ║
  4593.                     ║ ║ ║ ╚═════════════════════════════╝ ║ ║ ║
  4594.                     ║ ║ ║                                 ║ ║ ║
  4595.                     ║ ║ ╚═════════════════════════════════╝ ║ ║
  4596.                     ║ ║                                     ║ ║
  4597.                     ║ ╚═════════════════════════════════════╝ ║
  4598.                     ║                                         ║
  4599.                     ╚═════════════════════════════════════════╝
  4600.  
  4601.  
  4602.  Figure 4-2a. Stack Frame for MAIN at Level 1
  4603.  
  4604.                                             
  4605.                              ║ 15          0 ║
  4606.                              ╠═══════════════╣─┐
  4607.                              ║    OLD BP     ║ │
  4608.                BP FOR MAIN──╠═══════════════╣ ├─DISPLAY
  4609.                              ║      BPM     ║ │
  4610.                              ╠═══════════════╣─┘
  4611.                              ║               ║
  4612.                              ║               ║
  4613.                              ║               ║∙DYNAMIC
  4614.                              ║               ║ STORAGE
  4615.                              ║               ║
  4616.                         SP──╠═══════════════╣
  4617.                              ║               ║
  4618.  
  4619.  
  4620.  Figure 4-2b. Stack Frame for PROCEDURE A
  4621.  
  4622.                              ║15            0║
  4623.                              ╠═══════════════╣
  4624.                              ║     OLD BP    ║
  4625.                              ╠═══════════════╣
  4626.                              ║      BPM      ║
  4627.                              ╠═══════════════╣
  4628.                              ║               ║
  4629.                              ║               ║
  4630.                              ║               ║
  4631.                              ║               ║
  4632.                              ╠═══════════════╣─┐
  4633.                              ║      BPM      ║ │
  4634.                   BP FOR A──╠═══════════════╣ │
  4635.                              ║      BPM      ║ │
  4636.                              ╠═══════════════╣ ├─DISPLAY
  4637.                              ║      BPA     ║ │
  4638.                              ╠═══════════════╣═╡
  4639.                              ║               ║ │
  4640.                              ║               ║ ├─DYNAMIC
  4641.                              ║               ║ │ STORAGE
  4642.                              ║               ║ │
  4643.                         SP──╠═══════════════╣─┘
  4644.                              ║               ║
  4645.                              ╠═══════════════╣
  4646.                              ║               ║
  4647.  
  4648.  
  4649.  Figure 4-2c. Stack Frame for PROCEDURE B at Level 3 Called from A
  4650.  
  4651.                              ║15            0║
  4652.                              ╠═══════════════╣
  4653.                              ║     OLD BP    ║
  4654.                              ╠═══════════════╣
  4655.                              ║      BPM      ║
  4656.                              ╠═══════════════╣
  4657.                              ║               ║
  4658.                              ║               ║
  4659.                              ║               ║
  4660.                              ║               ║
  4661.                              ╠═══════════════╣
  4662.                              ║      BPM      ║
  4663.                              ╠═══════════════╣
  4664.                              ║      BPM      ║
  4665.                              ╠═══════════════╣
  4666.                              ║      BPA      ║
  4667.                              ╠═══════════════╣
  4668.                              ║               ║
  4669.                              ║               ║
  4670.                              ║               ║
  4671.                              ║               ║
  4672.                              ╠═══════════════╣─┐
  4673.                              ║      BPA      ║ │
  4674.                         BP──╠═══════════════╣ │
  4675.                              ║      BPM      ║ │
  4676.                              ╠═══════════════╣ ├─DISPLAY
  4677.                              ║      BPA      ║ │
  4678.                              ╠═══════════════╣ │
  4679.                              ║      BPB      ║ │
  4680.                              ╠═══════════════╣═╡
  4681.                              ║               ║ │
  4682.                              ║               ║ ├─DYNAMIC
  4683.                              ║               ║ │ STORAGE
  4684.                              ║               ║ │
  4685.                              ╠═══════════════╣─┘
  4686.                              ║               ║
  4687.                         SP──╠═══════════════╣
  4688.                              ║               ║
  4689.  
  4690.  
  4691.  Figure 4-2d. Stack Frame for PROCEDURE C at Level 3 Called from B
  4692.  
  4693.                              ║15            0║
  4694.                              ╠═══════════════╣
  4695.                              ║     OLD BP    ║
  4696.                              ╠═══════════════╣
  4697.                              ║      BPM      ║
  4698.                              ╠═══════════════╣
  4699.                              ║               ║
  4700.                              ║               ║
  4701.                              ║               ║
  4702.                              ║               ║
  4703.                              ╠═══════════════╣
  4704.                              ║      BPM      ║
  4705.                              ╠═══════════════╣
  4706.                              ║      BPM      ║
  4707.                              ╠═══════════════╣
  4708.                              ║      BPA      ║
  4709.                              ╠═══════════════╣
  4710.                              ║               ║
  4711.                              ║               ║
  4712.                              ║               ║
  4713.                              ║               ║
  4714.                              ╠═══════════════╣
  4715.                              ║      BPA      ║
  4716.                         BP──╠═══════════════╣─┐
  4717.                              ║      BPM      ║ │
  4718.                              ╠═══════════════╣ │
  4719.                              ║      BPA      ║ ├─DISPLAY
  4720.                              ╠═══════════════╣ │
  4721.                              ║      BPB      ║ │
  4722.                              ╠═══════════════╣═╡
  4723.                              ║               ║ │
  4724.                              ║               ║ ├─DYNAMIC
  4725.                              ║               ║ │ STORAGE
  4726.                              ║               ║ │
  4727.                         SP──╠═══════════════╣─┘
  4728.                              ║               ║
  4729.  
  4730.  
  4731.  Chapter 5  Real Address Mode
  4732.  
  4733.  ───────────────────────────────────────────────────────────────────────────
  4734.  
  4735.  The 80286 can be operated in either of two modes according to the status of
  4736.  the Protection Enabled bit of the MSW status register. In contrast to the
  4737.  "modes" and "mode bits" of some processors, however, the 80286 modes do not
  4738.  represent a radical transition between conflicting architectures. Instead,
  4739.  the setting of the Protection Enabled bit simply determines whether certain
  4740.  advanced features, in addition to the baseline architecture of the 80286,
  4741.  are to be made available to system designers and programmers.
  4742.  
  4743.  If the Protection Enabled (PE) bit is set by the programmer, the processor
  4744.  changes into Protected Virtual Address Mode. In this mode of operation,
  4745.  memory addressing is performed in terms of virtual addresses, with on-chip
  4746.  mapping mechanisms performing the virtual-to-physical translation. Only in
  4747.  this mode can the system designer make use of the advanced architectural
  4748.  features of the 80286: virtual memory support, system-wide protection, and
  4749.  built-in multitasking mechanisms are among the new features provided in this
  4750.  mode of operation. Refer to Part II of this book (Chapters 6, 7, 8, 9,
  4751.  10, and 11) for details on Protected Mode operation.
  4752.  
  4753.  Initially, upon system reset, the processor starts up in Real Address Mode.
  4754.  In this mode of operation, all memory addressing is performed in terms of
  4755.  real physical addresses. In effect, the architecture of the 80286 in
  4756.  this mode is identical to that of the 8086 and other processors in the 8086
  4757.  family. The principal features of this baseline architecture have already
  4758.  been discussed throughout Part I (Chapters 2, 3, and 4) of this book.
  4759.  This chapter discusses certain additional topics──addressing, interrupt
  4760.  handling, and system initialization──that complete the system programmer's
  4761.  view of the 80286 in Real Address Mode.
  4762.  
  4763.  
  4764.  5.1  Addressing and Segmentation
  4765.  
  4766.  Like other processors in the 8086 family, the 80286 provides a one-megabyte
  4767.  memory space (2^(20) bytes) when operated in Real Address Mode. Physical
  4768.  addresses are the 20-bit values that uniquely identify each byte location in
  4769.  this address space. Physical addresses, therefore, may range from 0 through
  4770.  FFFFFH. Address bits A20-A23 may not always be zero in Real Address Mode.
  4771.  A20-A23 should not be used by the system while the 80286 is operating in
  4772.  Real Address Mode.
  4773.  
  4774.  An address is specified by a 32-bit pointer containing two components: (1)
  4775.  a 16-bit effective address offset that determines the displacement, in
  4776.  bytes, of a particular location within a segment; and (2) a 16-bit segment
  4777.  selector component that determines the starting address of the segment.
  4778.  Both components of an address may be referenced explicitly by an instruction
  4779.  (such as JMP, LES, LDS, or CALL); more often, however, the segment selector
  4780.  is simply the contents of a segment register.
  4781.  
  4782.  The interpretation of the first component, the effective address offset, is
  4783.  straight-forward. Segments are at most 64K (2^(16)) bytes in length, so an
  4784.  unsigned 16-bit quantity is sufficient to address any arbitrary byte
  4785.  location with a segment. The lowest-addressed byte within a segment has an
  4786.  offset of 0, and the highest-addressed byte has an offset of FFFFH. Data
  4787.  operands must be completely contained within a segment and must be
  4788.  contiguous. (These rules apply in both modes.)
  4789.  
  4790.  A segment selector is the second component of a logical address. This
  4791.  16-bit quantity specifies the starting address of a segment within a
  4792.  physical address space of 2^(20) bytes.
  4793.  
  4794.  Whenever the 80286 accesses memory in Real Address Mode, it generates a
  4795.  20-bit physical address from a segment selector and offset value. The
  4796.  segment selector value is left-shifted four bit positions to form the
  4797.  segment base address. The offset is extended with 4 high order zeroes and
  4798.  added to the base to form the physical address (see figure 5-1).
  4799.  
  4800.  Therefore, every segment is required to start at a byte address that is
  4801.  evenly divisible by 16; thus, each segment is positioned at a 20-bit
  4802.  physical address whose least significant four bits are zeroes. This
  4803.  arrangement allows the 80286 to interpret a segment selector as the
  4804.  high-order 16 bits of a 20-bit segment base address.
  4805.  
  4806.  No limit or access checks are performed by the 80286 in the Real Address
  4807.  Mode. All segments are readable, writable, executable, and have a limit of
  4808.  0FFFFH (65,535 bytes). To save physical memory, you can use unused portions
  4809.  of a segment as another segment by overlapping the two (see figure 5-2).
  4810.  The Intel 8086 software development tools support this feature via the
  4811.  segment override and group operators. However, programs that access segment
  4812.  B from segment A become incompatible in the protected virtual address mode.
  4813.  
  4814.  
  4815.  Figure 5-1a.  Forming the Segment Base Address
  4816.  
  4817.                        16 BIT SEGMENT SELECTOR
  4818.           ┌───────────────────────┴───────────────────────┐
  4819.           15                                              0
  4820.           ╔══╤══╤══╤══╤══╤══╤══╤══╤══╤══╤══╤══╤══╤══╤══╤══╤ ═ ═ ═ ═ ═ ╗
  4821.           ║  │  │  │  │  │  │  │  │  │  │  │  │  │  │  │  │0 │0 │0 │0
  4822.           ╚══╧══╧══╧══╧══╧══╧══╧══╧══╧══╧══╧══╧══╧══╧══╧══╧ ═ ═ ═ ═ ═ ╝
  4823.           19                                                          0
  4824.  
  4825.  
  4826.  Figure 5-1b. Forming the 20-bit Physical Address in the Real Address Mode
  4827.  
  4828.                 ╔══╤══╤══╤══╤══╤══╤══╤══╤══╤══╤══╤══╤══╤══╤══╤══╤ ═ ═ ═ ═ ═ ╗
  4829.   SEGMENT BASE  ║  │  │  │  │  │  │  │  │  │  │  │  │  │  │  │  │0 │0 │0 │0
  4830.                 ╚══╧══╧══╧══╧══╧══╧══╧══╧══╧══╧══╧══╧══╧══╧══╧══╧ ═ ═ ═ ═ ═ ╝
  4831.                 19                                                          0
  4832.         +
  4833.                 ╔ ═ ═ ═ ═ ═ ╤══╤══╤══╤══╤══╤══╤══╤══╤══╤══╤══╤══╤══╤══╤══╤══╗
  4834.      OFFSET      0 │0 │0 │0 │  │  │  │  │  │  │  │  │  │  │  │  │  │  │  │  ║
  4835.                 ╚ ═ ═ ═ ═ ═ ╧══╧══╧══╧══╧══╧══╧══╧══╧══╧══╧══╧══╧══╧══╧══╧══╝
  4836.                 19          15                                              0
  4837.         =
  4838.                 ─────────────────────────────────────────────────────────────
  4839.                 ╔══╤══╤══╤══╤══╤══╤══╤══╤══╤══╤══╤══╤══╤══╤══╤══╤══╤══╤══╤══╗
  4840.     PHYSICAL    ║  │  │  │  │  │  │  │  │  │  │  │  │  │  │  │  │  │  │  │  ║
  4841.     ADDRESS     ╚══╧══╧══╧══╧══╧══╧══╧══╧══╧══╧══╧══╧══╧══╧══╧══╧══╧══╧══╧══╝
  4842.                 19                                                          0
  4843.  
  4844.  
  4845.  Figure 5-2.  Overlapping Segments to Save Physical Memory
  4846.  
  4847.                                             
  4848.                              ║               ║
  4849.                              ╟─ ── ── ── ── ─╫ ───
  4850.                              ║               ║  
  4851.                              ║               ║  │
  4852.                              ║               ║ 64K SEGMENT B
  4853.                              ║               ║  │
  4854.                          ─── ╫─ ── ── ── ── ─╫  │
  4855.                             ║    OVERLAP    ║  
  4856.                           │  ╠═══════════════╣───── BASE OF
  4857.                SEGMENT A 64K ║               ║     SEGMENT B
  4858.                           │  ║               ║
  4859.                           │  ║               ║
  4860.                             ║               ║
  4861.                          ─── ╠═══════════════╣───── BASE OF
  4862.                              ║               ║     SEGMENT A
  4863.                                             
  4864.  
  4865.  
  4866.  5.2  Interrupt Handling
  4867.  
  4868.  Program interrupts may be generated in either of two distinct ways. An
  4869.  internal interrupt is caused directly by the currently executing program.
  4870.  The execution of a particular instruction results in the occurrence of an
  4871.  interrupt, whether intentionally (e.g., an INT n instruction) or as an
  4872.  unanticipated exception (e.g., invalid opcode). On the other hand, an
  4873.  external interrupt occurs asynchronously as the result of an event
  4874.  external to the processor, and bears no necessary relationship with the
  4875.  currently executing program. The INTR and NMI pins of the 80286 provide the
  4876.  means by which external hardware signals the occurrence of such events.
  4877.  
  4878.  
  4879.  5.2.1  Interrupt Vector Table
  4880.  
  4881.  Whatever its origin, whether internal or external, an interrupt demands
  4882.  immediate attention from an associated service routine. Control must be
  4883.  transferred, at least for the moment, from the currently executing program
  4884.  to the appropriate interrupt service routine. By means of interrupt
  4885.  vectors, the 80286 handles such control transfers uniformly for both kinds
  4886.  of interrupts.
  4887.  
  4888.  An interrupt vector is an unsigned integer in the range of 0-255; every
  4889.  interrupt is assigned such a vector. In some cases, the assignment is
  4890.  predetermined and fixed: for example, an external NMI interrupt is
  4891.  invariably associated with vector 2, while an internal divide exception is
  4892.  always associated with vector 0. In most cases, however, the association of
  4893.  an interrupt and a vector is established dynamically. An external INTR
  4894.  interrupt, for example, supplies a vector in response to an interrupt
  4895.  acknowledge bus cycle, while the INT n instruction supplies a vector
  4896.  incorporated within the instruction itself. The vector is shifted two places
  4897.  left to form a byte address into the table (see figure 5-3).
  4898.  
  4899.  In any case, the 80286 uses the interrupt vector as an index into a table
  4900.  in order to determine the address of the corresponding interrupt service
  4901.  routine. For Real Address Mode, this table is known as the Interrupt Vector
  4902.  Table. Its format is illustrated in figure 5-3.
  4903.  
  4904.  The Interrupt Vector Table consists of as many as 256 consecutive entries,
  4905.  each four bytes long. Each entry defines the address of a service routine to
  4906.  be associated with the correspondingly numbered interrupt vector code.
  4907.  Within each entry, an address is specified by a full 32-bit pointer that
  4908.  consists of a 16-bit offset and a 16-bit segment selector. Interrupts 0-31
  4909.  are reserved by Intel.
  4910.  
  4911.  In Real Address Mode, the interrupt table can be accessed directly at
  4912.  physical memory location 0 through 1023. In the protected virtual address
  4913.  mode, however, the interrupt vector table has no fixed physical address and
  4914.  cannot be directly accessed. Therefore, Real Address mode programs that
  4915.  directly manipulate the interrupt vector table will not work in the
  4916.  protected virtual address mode.
  4917.  
  4918.  
  4919.  Table 5-1. Interrupt Processing Order
  4920.  
  4921.  Order     Interrupt
  4922.  1.        Instruction exception
  4923.  2.        Single step
  4924.  3.        NMI
  4925.  4.        Processor extension segment overrun
  4926.  5.        INTR
  4927.  
  4928.  
  4929.  Figure 5-3.  Interrupt Vector Table for Real Address Mode
  4930.  
  4931.       POWER TO
  4932.   INTERRUPT HANDLER                    PHYSICAL
  4933.         FOR:                           ADDRESS
  4934.  
  4935.                     ╔═══════════════╗
  4936.    INTERRUPT 255    ║    POINTER    ║   1020
  4937.                     ╠═══════════════╣
  4938.    INTERRUPT 254    ║    POINTER    ║   1018
  4939.                     ╠═══════════════╣
  4940.    INTERRUPT 253    ║    POINTER    ║┐ 1012
  4941.                     ╠═══════════════╣ └────────────────────┐
  4942.                     ║               ║   ╔═════════════════╤╧═══════════╤═╤═╗
  4943.                     ║               ║   ║0 ∙ ∙ ∙ ∙ ∙ ∙ ∙ 0│   VECTOR   │0│0║
  4944.                     ≈               ≈   ╚═════════════════╧════════════╧═╧═╝
  4945.                     ║               ║   19              10 9           2 1 0
  4946.                     ║               ║
  4947.                     ╠═══════════════╣
  4948.    INTERRUPT 1      ║    POINTER    ║   4
  4949.                     ╠═══════════════╣
  4950.    INTERRUPT 0      ║    POINTER    ║   0
  4951.                     ╚═══════════════╝
  4952.  
  4953.  
  4954.  5.2.1.1  Interrupt Priorities
  4955.  
  4956.  When simultaneous interrupt requests occur, they are processed in a fixed
  4957.  order as shown in table 5-1. Interrupt processing involves saving the
  4958.  flags, the return address, and setting CS:IP to point at the first
  4959.  instruction of the interrupt handler. If other interrupts remain enabled,
  4960.  they are processed before the first instruction of the current interrupt
  4961.  handler is executed. The last interrupt processed is therefore the first one
  4962.  serviced.
  4963.  
  4964.  
  4965.  5.2.2  Interrupt Procedures
  4966.  
  4967.  When an interrupt occurs in Real Address Mode, the 8086 performs the
  4968.  following sequence of steps. First, the FLAGS register, as well as the old
  4969.  values of CS and IP, are pushed onto the stack (see figure 5-4). The IF and
  4970.  TF flag bits are cleared. The vector number is then used to read the
  4971.  address of the interrupt service routine from the interrupt table. Execution
  4972.  begins at this address.
  4973.  
  4974.  Thus, when control is passed to an interrupt service routine, the return
  4975.  linkage is placed on the stack, interrupts are disabled, and single-step
  4976.  trace (if in effect) is turned off. The IRET instruction at the end of the
  4977.  interrupt service routine will reverse these steps before transferring
  4978.  control to the program that was interrupted.
  4979.  
  4980.  An interrupt service routine may affect registers other than other IP, CS,
  4981.  and FLAGS. It is the responsibility of an interrupt routine to save
  4982.  additional context information before proceeding so that the state of the
  4983.  machine can be restored upon completion of the interrupt service routine
  4984.  (PUSHA and POPA instructions are intended for these operations). Finally,
  4985.  execution of the IRET instruction pops the old IP, CS, and FLAGS from the
  4986.  stack and resumes the execution of the interrupted program.
  4987.  
  4988.  
  4989.  Figure 5-4. Stack Structure after Interrupt (Real Address Mode)
  4990.  
  4991.                                 ║               ║
  4992.                                 ≈               ≈
  4993.                                ╠═══════════════╣
  4994.                               │ ║   OLD FLAGS   ║
  4995.                   INCREASING  │ ╠═══════════════╣
  4996.                   ADDRESSES   │ ║    OLD CS     ║
  4997.                               │ ╠═══════════════╣
  4998.                               │ ║    OLD IP     ║── <SS:SP>
  4999.                               │ ╠═══════════════╣
  5000.                                 ║              ║
  5001.                                 ≈              ≈
  5002.                                 ║              ║
  5003.  
  5004.  
  5005.  5.2.3  Reserved and Dedicated Interrupt Vectors
  5006.  
  5007.  In general, the system designer is free to use almost any interrupt vectors
  5008.  for any given purpose. Some of the lowest-numbered vectors, however, are
  5009.  reserved by Intel for dedicated functions; their use is specifically implied
  5010.  by certain types of exceptions. None of the first 32 vectors should be
  5011.  defined by the user; these vectors are either invoked by pre-defined
  5012.  exceptions or reserved by Intel for future expansion. Table 5-2 shows the
  5013.  dedicated and reserved vectors of the 80286 in Real Address Mode.
  5014.  
  5015.  The purpose and function of the dedicated interrupt vectors may be
  5016.  summarized as follows (the saved value of CS:IP will include all leading
  5017.  prefixes):
  5018.  
  5019.    ■  Divide error (Interrupt 0). This exception will occur if the quotient
  5020.       is too large or an attempt is made to divide by zero using either the
  5021.       DIV or IDIV instruction. The saved CS:IP points at the first byte of
  5022.       the failing instruction. DX and AX are unchanged.
  5023.  
  5024.    ■  Single-Step (Interrupt 1). This interrupt will occur after
  5025.       each instruction if the Trap Flag (TF) bit of the FLAGS register is
  5026.       set. Of course, TF is cleared upon entry to this or any other interrupt
  5027.       to prevent infinite recursion. The saved value of CS:IP will point to
  5028.       the next instruction.
  5029.  
  5030.    ■  Nonmaskable (Interrupt 2). This interrupt will occur upon receipt of
  5031.       an external signal on the NMI pin. Typically, the nonmaskable interrupt
  5032.       is used to implement power-fail/auto-restart procedures. The saved
  5033.       value of CS:IP will point to the first byte of the interrupted
  5034.       instruction.
  5035.  
  5036.    ■  Breakpoint (Interrupt 3). Execution of the one-byte breakpoint
  5037.       instruction causes this interrupt to occur. This instruction is useful
  5038.       for the implementation of software debuggers since it requires only one
  5039.       code byte and can be substituted for any instruction opcode byte. The
  5040.       saved value of CS:IP will point to the next instruction.
  5041.  
  5042.    ■  INTO Detected Overflow (Interrupt 4). Execution of the INTO
  5043.       conditional software interrupt instruction will cause this interrupt
  5044.       to occur if the overflow bit (OF) of the FLAGS register is set. The
  5045.       saved value of CS:IP will point to the next instruction.
  5046.  
  5047.    ■  BOUND Range Exceeded (Interrupt 5). Execution of the BOUND instruction
  5048.       will cause this interrupt to occur if the specified array index is
  5049.       found to be invalid with respect to the given array bounds. The saved
  5050.       value of CS:IP will point to the first byte of the BOUND instruction.
  5051.  
  5052.    ■  Invalid Opcode (Interrupt 6). This exception will occur if execution
  5053.       of an invalid opcode is attempted. (In Real Address Mode, most of the
  5054.       Protected Virtual Address Mode instructions are classified as invalid
  5055.       and should not be used). This interrupt can also occur if the
  5056.       effective address given by certain instructions, notably BOUND, LDS,
  5057.       LES, and LIDT, specifies a register rather than a memory location. The
  5058.       saved value of CS:IP will point to the first byte of the
  5059.       invalid instruction or opcode.
  5060.  
  5061.    ■  Processor Extension Not Available (Interrupt 7). Execution of the ESC
  5062.       instruction will cause this interrupt to occur if the status bits of
  5063.       the MSW indicate that processor extension functions are to be emulated
  5064.       in software. Refer to section 10.2.1 for more details. The saved value
  5065.       of CS:IP will point to the first byte of the ESC or the WAIT
  5066.       instruction.
  5067.  
  5068.    ■  Interrupt Table Limit Too Small (Interrupt 8). This interrupt will
  5069.       occur if the limit of the interrupt vector table was changed from 3FFH
  5070.       by the LIDT instruction and an interrupt whose vector is outside the
  5071.       limit occurs. The saved value of CS:IP will point to the first byte of
  5072.       the instruction that caused the interrupt or that was ready to execute
  5073.       before an external interrupt occurred. No error code is pushed.
  5074.  
  5075.    ■  Processor Extension Segment Overrun Interrupt (Interrupt 9). The
  5076.       interrupt will occur if a processor extension memory operand does not
  5077.       fit in a segment. The saved CS:IP will point at the first byte of the
  5078.       instruction that caused the interrupt.
  5079.  
  5080.    ■  Segment Overrun Exception (Interrupt 13). This interrupt will occur if
  5081.       a memory operand does not fit in a segment. In Real Mode this will
  5082.       occur only when a word operand begins at segment offset 0FFFFH. The
  5083.       saved CS:IP will point at the first byte of the instruction that
  5084.       caused the interrupt. No error code is pushed.
  5085.  
  5086.    ■  Processor Extension Error (Interrupt 16). This interrupt occurs after
  5087.       the numeric instruction that caused the error. It can only occur while
  5088.       executing a subsequent WAIT or ESC. The saved value of CS:IP will point
  5089.       to the first byte of the ESC or the WAIT instruction. The address of
  5090.       the failed numeric instruction is saved in the NPX.
  5091.  
  5092.  
  5093.  Table 5-2. Dedicated and Reserved Interrupt Vectors in Real Address Mode
  5094.  
  5095. ╓┌─────────────────────────┌───────────┌───────────────────────┌─────────────
  5096.  Function                  Interrupt   Related Instructions    Return Address
  5097.                            Number                              Before Instruct
  5098.                                                                Causing Excepti
  5099.  Divide error exception        0       DIV, IDIV               Yes
  5100.  Single step interrupt         1       All                     N/A
  5101.  NMI interrupt                 2       All                     N/A
  5102.  Breakpoint interrupt          3       INT                     N/A
  5103.  INTO detected overflow        4       INTO                    No
  5104.  exception
  5105.  Function                  Interrupt   Related Instructions    Return Address
  5106.                            Number                              Before Instruct
  5107.                                                                Causing Excepti
  5108. exception
  5109.  BOUND range exceeded          5       BOUND                   Yes
  5110.  exception
  5111.  Invalid opcode exception      6       Any undefined opcode    Yes
  5112.  Processor extension           7       ESC or WAIT             Yes
  5113.  not available exception
  5114.  Interrupt table               8       LIDT                    Yes
  5115.  limit too small
  5116.  Processor extension           9       ESC                     Yes
  5117.  segment overrun interrupt
  5118.  Segment overrun exception     13      Any memory reference    Yes
  5119.                                        instruction that
  5120.                                        attempts to reference
  5121.                                        16-bit word at
  5122.                                        offset 0FFFFH.
  5123.  Reserved                      10-12,
  5124.                                14, 15
  5125.  Processor extension           16      ESC or WAIT             N/A
  5126.  Function                  Interrupt   Related Instructions    Return Address
  5127.                            Number                              Before Instruct
  5128.                                                                Causing Excepti
  5129. Processor extension           16      ESC or WAIT             N/A
  5130.  error interrupt
  5131.  Reserved                      17-31
  5132.  User defined                  32-255
  5133.  
  5134.  N/A = Not Applicable
  5135.  
  5136.  
  5137.  5.3  System Initialization
  5138.  
  5139.  The 80286 provides an orderly way to start or restart an executing system.
  5140.  Upon receipt of the RESET signal, certain processor registers go into the
  5141.  determinate state shown in table 5-3.
  5142.  
  5143.  Since the CS register contains F000 (thus specifying a code segment
  5144.  starting at physical address F0000) and the instruction pointer contains
  5145.  FFF0, the processor will execute its first instruction at physical address
  5146.  FFFF0H. The uppermost 16 bytes of physical memory are therefore reserved
  5147.  for initial startup logic. Ordinarily, this location contains an
  5148.  intersegment direct JMP instruction whose target is the actual beginning of
  5149.  a system initialization or restart program.
  5150.  
  5151.  Some of the steps normally performed by a system initialization routine are
  5152.  as follows:
  5153.  
  5154.    ■  Allocate a stack.
  5155.  
  5156.    ■  Load programs and data from secondary storage into memory.
  5157.  
  5158.    ■  Initialize external devices.
  5159.  
  5160.    ■  Enable interrupts (i.e., set the IF bit of the FLAGS register). Set
  5161.       any other desired FLAGS bit as well.
  5162.  
  5163.    ■  Set the appropriate MSW flags if a processor extension is present, or
  5164.       if processor extension functions are to be emulated by software.
  5165.  
  5166.    ■  Set other registers, as appropriate, to the desired initial values.
  5167.  
  5168.    ■  Execute. (Ordinarily, this last step is performed as an intersegment
  5169.       JMP to the main system program.)
  5170.  
  5171.  
  5172.  Table 5-3. Processor State after RESET
  5173.  
  5174.  Register     Contents
  5175.  FLAGS        0002
  5176.  MSW          FFF0
  5177.  IP           FFF0
  5178.  CS           F000
  5179.  DS           0000
  5180.  SS           0000
  5181.  ES           0000
  5182.  
  5183.  
  5184.  Chapter 6  Memory Management and Virtual Addressing
  5185.  
  5186.  ───────────────────────────────────────────────────────────────────────────
  5187.  
  5188.  In Protected Virtual Address Mode, the 80286 provides an advanced
  5189.  architecture that retains substantial compatibility with the 8086 and other
  5190.  processors in the 8086 family. In many respects, the baseline architecture
  5191.  of the processor remains constant regardless of the mode of operation.
  5192.  Application programmers continue to use the same set of instructions,
  5193.  addressing modes, and data types in Protected Mode as in Real Address Mode.
  5194.  
  5195.  The major difference between the two modes of operation is that the
  5196.  Protected Mode provides system programmers with additional architectural
  5197.  features, supplementary to the baseline architecture, that can be used to
  5198.  good advantage in the design and implementation of advanced systems.
  5199.  Especially noteworthy are the mechanisms provided for memory management,
  5200.  protection, and multitasking.
  5201.  
  5202.  This chapter focuses on the memory management mechanisms of Protected Mode;
  5203.  the concept of a virtual address and the process of virtual-to-physical
  5204.  address translation are described in detail in this chapter. Subsequent
  5205.  chapters deal with other key aspects of Protected Mode operation. Chapter 7
  5206.  discusses the issue of protection and the integrated mechanisms that
  5207.  support a system-wide protection policy. Chapter 8 discusses the notion of
  5208.  a task and its central role in the 80286 architecture. Chapters 9, 10, and
  5209.  11 discuss certain additional topics──interrupt handling, special
  5210.  instructions, system initialization, etc.──that complete the system
  5211.  programmer's view of 80286 Protected Mode.
  5212.  
  5213.  
  5214.  6.1  Memory Management Overview
  5215.  
  5216.  A memory management scheme interposes a mapping operation between logical
  5217.  addresses (i.e., addresses as they are viewed by programs) and physical
  5218.  addresses (i.e., actual addresses in real memory). Since the logical address
  5219.  spaces are independent of physical memory (dynamically relocatable), the
  5220.  mapping (the assignment of real address space to virtual address space) is
  5221.  transparent to software. This allows the program development tools (for
  5222.  static systems) or the system software (for reprogrammable systems) to
  5223.  control the allocation of space in real memory without regard to the
  5224.  specifics of individual programs.
  5225.  
  5226.  Application programs may be translated and loaded independently since they
  5227.  deal strictly with virtual addresses. Any program can be relocated to use
  5228.  any available segments of physical memory.
  5229.  
  5230.  The 80286, when operated in Protected Mode, provides an efficient on-chip
  5231.  memory management architecture. Moreover, as described in Chapter 11, the
  5232.  80286 also supports the implementation of virtual memory systems──that is,
  5233.  systems that dynamically swap chunks of code and data between real memory
  5234.  and secondary storage devices (e.g., a disk) independent of and transparent
  5235.  to the executing application programs. Thus, a program-visible address is
  5236.  more aptly termed a virtual address rather than a logical address since it
  5237.  may actually refer to a location not currently present in real memory.
  5238.  
  5239.  Memory management, then, consists of a mechanism for mapping the virtual
  5240.  addresses that are visible to the program onto the physical addresses of
  5241.  real memory. With the 80286, segmentation is the key to virtual memory
  5242.  addressing. Virtual memory is partitioned into a number of individual
  5243.  segments, which are the units of memory that are mapped into physical memory
  5244.  and swapped to and from secondary storage devices. Most of this chapter is
  5245.  devoted to a detailed discussion of the mapping and virtual memory
  5246.  mechanisms of the 80286.
  5247.  
  5248.  The concept of a task also plays a significant role in memory management
  5249.  since distinct memory mappings may be assigned to the different tasks in a
  5250.  multitask or multi-user environment. A complete discussion of tasks is
  5251.  deferred until Chapter 8, "Tasks and State Transition." For present
  5252.  purposes, it is sufficient to think of a task as an ongoing process, or
  5253.  execution path, that is dedicated to a particular function. In a multi-user
  5254.  time-sharing environment, for example, the processing required to interact
  5255.  with a particular user may be considered as a single task, functionally
  5256.  independent of the other tasks (i.e., users) in the system.
  5257.  
  5258.  
  5259.  6.2  Virtual Addresses
  5260.  
  5261.  In Protected Mode, application programs deal exclusively with virtual
  5262.  addresses; programs have no access whatsoever to the actual physical
  5263.  addresses generated by the processor. As discussed in Chapter 2, an address
  5264.  is specified by a program in terms of two components: (1) a 16-bit
  5265.  effective address offset that determines the displacement, in bytes, of a
  5266.  location within a segment; and (2) a 16-bit segment selector that uniquely
  5267.  references a particular segment. Jointly, these two components constitute a
  5268.  complete 32-bit address (pointer data type), as shown in figure 6-1.
  5269.  
  5270.  These 32-bit virtual addresses are manipulated by programs in exactly the
  5271.  same way as the two-component addresses of Real Address Mode. After a
  5272.  program loads the segment selector component of an address into a segment
  5273.  register, each subsequent reference to locations within the selected
  5274.  segment requires only a 16-bit offset be specified. Locality of reference
  5275.  will ordinarily insure that addresses can be specified very efficiently
  5276.  using only 16-bit offsets.
  5277.  
  5278.  An important difference between Real Address Mode and Protected Mode,
  5279.  however, concerns the actual format and information content of segment
  5280.  selectors. In Real Address Mode, as with the 8086 and other processors in
  5281.  the 8086 family, a 16-bit selector is merely the upper bits of a segment's
  5282.  physical base address. By contrast, segment selectors in Protected Mode
  5283.  follow an entirely different format, as illustrated by figure 6-1.
  5284.  
  5285.  Two of the selector bits, designated as the RPL field in figure 6-1, are
  5286.  not actually involved in the selection and specification of segments; their
  5287.  use is discussed in Chapter 7.
  5288.  
  5289.  The remaining 14 bits of the selector component uniquely designate a
  5290.  particular segment. The virtual address space of a program, therefore, may
  5291.  encompass as many as 16,384 (2^(14)) distinct segments. Segments themselves
  5292.  are of variable size, ranging from as small as a single byte to as large as
  5293.  64K (2^(16)) bytes. Thus, a program's virtual address space may contain,
  5294.  altogether, up to a full gigabyte (2^(30) = 2^(14) * 2^(16)) of individually
  5295.  addressable byte locations.
  5296.  
  5297.  The entirety of a program's virtual address space is further subdivided
  5298.  into two separate halves, as distinguished by the TI ("table indicator") bit
  5299.  in the virtual address. These two halves are the global address space and
  5300.  the local address space.
  5301.  
  5302.  The global address space is used for system-wide data and procedures
  5303.  including operating system software, library routines, runtime language
  5304.  support and other commonly shared system services. (To application programs,
  5305.  the operating system appears to be a set of service routines that are
  5306.  accessible to all tasks.) Global space is shared by all tasks to avoid
  5307.  unnecessary replication of system service routines and to facilitate shared
  5308.  data and interrupt handling. Global address space is defined by addresses
  5309.  with a zero in the TI bit position; it is identically mapped for all tasks
  5310.  in the system.
  5311.  
  5312.  The other half of the virtual address space──comprising those addresses
  5313.  with the TI bit set──is separately mapped for each task in the system.
  5314.  Because such an address space is local to the task for which it is defined,
  5315.  it is referred to as a local address space. In general, code and data
  5316.  segments within a task's local address space are private to that particular
  5317.  task or user. Figure 6-2 illustrates the task isolation made possible by
  5318.  partitioning the virtual address spaces into local and global regions.
  5319.  
  5320.  Within each of the two regions addressable by a program──either the global
  5321.  address space or a particular local address space──as many as 8,192 (2^(13))
  5322.  distinct segments may be defined. The INDEX field of the segment selector
  5323.  allows for a unique specification of each of these segments. This 13-bit
  5324.  quantity acts as an index into a memory-resident table, called a descriptor
  5325.  table, that records the mapping between segment address and the physical
  5326.  locations allocated to each distinct segment. (These descriptor tables, and
  5327.  their role in virtual-to-physical address translation, are described in the
  5328.  sections that follow.)
  5329.  
  5330.  In summary, a Protected Mode virtual address is a 32-bit pointer to a
  5331.  particular byte location within a one-gigabyte virtual address space. Each
  5332.  such pointer consists of a 16-bit selector component and a 16-bit offset
  5333.  component. The selector component, in turn, comprises a 13-bit table index,
  5334.  a 1-bit table indicator (local versus global), and a 2-bit RPL field; all
  5335.  but this last field serve to select a particular segment from among the 16K
  5336.  segments in a task's virtual address space. The offset component of a full
  5337.  pointer is an unsigned 16-bit integer that specifies the desired byte
  5338.  location within the selected segment.
  5339.  
  5340.  
  5341.  Figure 6-1.  Format of the Segment Selector Component
  5342.  
  5343.                               32-BIT POINTER
  5344.                  ┌───────────────────┴───────────────────┐
  5345.                  31                16 15                 0
  5346.                 ╔════════════════════╤════════════════════╗
  5347.                 ║  SEGMENT SELECTOR  │   SEGMENT OFFSET   ║
  5348.                 ╚════════════════════╧════════════════════╝
  5349.                 │                    │
  5350.                 │                    │
  5351.                 │                    │
  5352.              ┌──┘                    └───────────────────────┐
  5353.              15                                    3  2  1  0
  5354.              ╔══╤══╤══╤══╤══╤══╤══╤══╤══╤══╤══╤══╤══╤══╤══╤══╗
  5355.              ║                  INDEX               │TI│ RPL ║
  5356.              ╚══╧══╧══╧══╧══╧══╧══╧══╧══╧══╧══╧══╧══╧══╧══╧══╝
  5357.              └───────────────────────┬───────────────────────┘
  5358.                                  SELECTOR
  5359.  
  5360.  
  5361.  Figure 6-2.  Address Spaces and Task Isolation
  5362.  
  5363.                              ┌──────────────┐
  5364.                              │ ╔══════════╗ │
  5365.                              │ ║TASK 1    ║ │
  5366.                              │ ║LOCAL     ║ │
  5367.   TASK 3                     │ ║ADDRESS   ║ │
  5368.   VIRTUAL ADDRESS SPACE ┐    │ ║SPACE     ║ │────TASK 1
  5369.                             │ ╚══════════╝ │     VIRTUAL ADDRESS SPACE
  5370.                ┌─────────────│──────────────│┐
  5371.                │ ╔══════════╗│┌─────────────│───────────────┐
  5372.                │ ║TASK 3    ║││ ╔════════╗  ││ ╔══════════╗ │
  5373.                │ ║LOCAL     ║││ ║GLOBAL  ║  ││ ║TASK 2    ║ │
  5374.                │ ║ADDRESS   ║││ ║ADDRESS ║  ││ ║LOCAL     ║ │
  5375.                │ ║SPACE     ║││ ║SPACE   ║  ││ ║ADDRESS   ║ │
  5376.                │ ╚══════════╝││ ╚════════╝  ││ ║SPACE     ║ │
  5377.                └─────────────└──────────────┘┘ ╚══════════╝ │
  5378.                               └─────────────────────────────┘
  5379.                                             
  5380.                                             └TASK 2
  5381.                                              VIRTUAL ADDRESS SPACE
  5382.  
  5383.  
  5384.  6.3  Descriptor Tables
  5385.  
  5386.  A descriptor table is a memory-resident table either defined by program
  5387.  development tools in a static system or controlled by operating system
  5388.  software in systems that are reprogrammable. The descriptor table contents
  5389.  govern the interpretation of virtual addresses. Whenever the 80286 decodes
  5390.  a virtual address, translating a full 32-bit pointer into a corresponding
  5391.  24-bit physical address, it implicitly references one of these tables.
  5392.  
  5393.  Within a Protected Mode system, there are ordinarily several descriptor
  5394.  tables resident in memory. One of these is the global descriptor table
  5395.  (GDT); this table provides a complete description of the global address
  5396.  space. In addition, there may be one or more local descriptor tables
  5397.  (LDTs), each describing the local address space of one or more tasks.
  5398.  
  5399.  For each task in the system, a pair of descriptor tables──consisting of the
  5400.  GDT (shared by all tasks) and a particular LDT (private to the task or to a
  5401.  group of closely related tasks)──provides a complete description of that
  5402.  task's virtual address space. The protection mechanism described in Chapter
  5403.  7, "Protection," ensures that a task is granted access only to its own
  5404.  virtual address space. In the simplest of system configurations, tasks can
  5405.  reside entirely within the GDT without the use of local descriptor tables.
  5406.  This will simplify system software by only requiring maintenance of one
  5407.  table (the GDT) at the expense of no isolation between tasks. The point is:
  5408.  the 80286 memory management scheme is flexible enough to accommodate a
  5409.  variety of implementations and does not require use of all possible
  5410.  facilities when implementing a system.
  5411.  
  5412.  The descriptor tables consist of a sequence of 8-byte entries called
  5413.  descriptors. A descriptor table may contain from 1 to 8192 entries.
  5414.  
  5415.  Within a descriptor table, two main classes of descriptors are recognized
  5416.  by the 80286 architecture. The most important of these, from the standpoint
  5417.  of memory management, are called segment descriptors; these determine the
  5418.  set of segments that are included within a given address space. The other
  5419.  class are special-purpose control descriptors──such as call gates and task
  5420.  descriptors──to implement protection (described in succeeding chapters) and
  5421.  special system data segments.
  5422.  
  5423.  Figure 6-3 shows the format of a segment descriptor. Note that it provides
  5424.  information about the physical-memory base address and size of a segment, as
  5425.  well as certain access information. If a particular segment is to be
  5426.  included within a virtual address space, then a segment descriptor that
  5427.  describes that segment must be included within the appropriate descriptor
  5428.  table. Thus, within the GDT, there are segment descriptors for all of the
  5429.  segments that comprise a system's global address space. Similarly, within a
  5430.  task's LDT, there must be a descriptor for each of the segments that are to
  5431.  be included in that task's local address space.
  5432.  
  5433.  Each local descriptor table is itself a special system segment,
  5434.  recognizable as such by the 80286 architecture and described by a specific
  5435.  type of segment descriptor (see figure 6-4). Because there is only a single
  5436.  GDT segment, it is not defined by a segment descriptor. Its base and size
  5437.  information is maintained in a dedicated register, GDTR, as described below
  5438.  (section 6.6.2).
  5439.  
  5440.  Similarly, there is another dedicated register within the 80286, LDTR, that
  5441.  records the base and size of the current LDT segment (i.e., the LDT
  5442.  associated with the currently executing task). The LDTR register state,
  5443.  however, is volatile: its contents are automatically altered whenever a
  5444.  task switch is made from one task to another. An alternate specification
  5445.  independent of changeable register contents must therefore exist for each
  5446.  LDT in the system. This independent specification is accomplished by means
  5447.  of special system segment descriptors known as descriptor table descriptors
  5448.  or LDT descriptors.
  5449.  
  5450.  Figure 6-4 shows the format of a descriptor table descriptor. (Note that it
  5451.  is distinguished from an ordinary segment descriptor by the contents of
  5452.  certain bits in the access byte.) This special type of descriptor is used to
  5453.  specify the physical base address and size of a local descriptor table that
  5454.  defines the virtual address space and address mapping for an individual user
  5455.  or task (figure 6-5).
  5456.  
  5457.  Each LDT segment in a system must lie within that system's global address
  5458.  space. Thus, all of the descriptor table descriptors must be included among
  5459.  the entries in the global descriptor table (the GDT) of a system. In fact,
  5460.  these special descriptors may appear only in the GDT. Reference to an LDT
  5461.  descriptor within an LDT will cause a protection violation. Even though
  5462.  they are in the global address space available to all tasks, the descriptor
  5463.  table descriptors are protected from corruption within the GDT since they
  5464.  are special system segments and can only be accessed for loading into the
  5465.  LDTR register.
  5466.  
  5467.  
  5468.  Figure 6-3.  Segment Descriptors (S=1)
  5469.  
  5470.           7                             0 7                              0
  5471.          ╔═══════════════════════════════╤════════════════════════════════╗
  5472.        +7║                   INTEL RESERVED MUST BE 0                    ║+6
  5473.  ACCESS  ╟───┬───────┬───┬───────────┬───┬────────────────────────────────╢
  5474.        +5║ P │  DPL  │S=1│   TYPE    │ A │          BASE{23-16}           ║+4
  5475.  RIGHTS  ╟───┴───┴───┴───┴───┴───┴───┴───┴────────────────────────────────╢
  5476.        +3║                           BASE{15-0}                           ║+2
  5477.  BYTES   ╟────────────────────────────────────────────────────────────────╢
  5478.        +1║                           LIMIT{15-0}                          ║ 0
  5479.          ╚═══════════════════════════════╧════════════════════════════════╝
  5480.           15                            8 7                              0
  5481.  
  5482.            ACCESS RIGHTS BYTES:
  5483.             P    = PRESENT
  5484.             DPL  = DESCRIPTOR PRIVILEGE LEVEL
  5485.             S    = SEGMENT DESCRIPTOR
  5486.             TYPE = SEGMENT TYPE AND ACCESS INFORMATION
  5487.                    (see Figure 6-7)
  5488.             A    = ACCESSED
  5489.  
  5490.  
  5491.  Figure 6-4.  Special Purpose Descriptors or System Segment Descriptors (S=1)
  5492.  
  5493.        7                             0 7                              0
  5494.       ╔═══════════════════════════════╤════════════════════════════════╗
  5495.     +7║                    INTEL RESERVED MUST BE 0                   ║+6
  5496.       ╟───┬───────┬───┬───────────────┬────────────────────────────────╢
  5497.     +5║ P │  DPL  │S=1│   TYPE        │           BASE{23-16}          ║+4
  5498.       ╟───┴───┴───┴───┴───┴───┴───┴───┴────────────────────────────────╢
  5499.     +3║                           BASE{15-0}                           ║+2
  5500.       ║───────────────────────────────┴────────────────────────────────║
  5501.     +1║                           LIMIT{15-0}                          ║ 0
  5502.       ╚═══════════════════════════════╧════════════════════════════════╝
  5503.        15                            8 7                              0
  5504.  
  5505.         ACCESS RIGHTS BYTES:
  5506.          P    = PRESENT
  5507.          DPL  = DESCRIPTOR PRIVILEGE LEVEL
  5508.          S    = SEGMENT DESCRIPTOR
  5509.          TYPE = SEGMENT TYPE AND ACCESS INFORMATION
  5510.                 (Includes control and system segments)
  5511.  
  5512.                 0   = INVALID DESCRIPTOR
  5513.                 1   = AVAILABLE TASK STATE SEGMENT
  5514.                 2   = LDT DESCRIPTOR
  5515.                 3   = BUSY TASK STATE SEGMENT
  5516.                 4-7 = CONTROL DESCRIPTOR (see Chapter 7)
  5517.                 8   = INVALID DESCRIPTOR (reserved by Intel)
  5518.                 9-F = RESERVED BY INTEL
  5519.  
  5520.  Figure 6-5.  LDT Descriptors
  5521.  
  5522.                                                                     
  5523.                                                    ║               ║
  5524.                         ┌─║                 ║    ┌─╠═══════════════╣
  5525.                         │  ≈                 ≈    │  ║               ║
  5526.                       │  ║                 ║    │  ║      ONE      ║
  5527.  ║                 ║    │  ╠═════════════════╣    │  ║    SEGMENT    ║
  5528.  ╠═════════════════╣    │  ║  RESERVED ZERO  ║    │  ║    OF THE     ║SEGMENT
  5529.  ║  RESERVED ZERO  ║    │  ╠════════╤════════╣┐   │  ║     TASKS     ║LIMIT
  5530.  ╠════════╤════════╣┐   │  ║        │BASE{23-16}  │  ║     LOCAL     ║
  5531.  ║        │BASE{23-16}  │  ╠════════╧════════╣├─┐ │  ║   (private)   ║
  5532.  ╠════════╧════════╣├─┐ │  ║    BASE{15-0}   ║│ │ │  ║    ADDRESS    ║
  5533.  ║    BASE{15-0}   ║│ │ │  ╠═════════════════╣┘ │ │  ║     SPACE     ║
  5534.  ╠═════════════════╣┘ │ │  ║    BASE{15-0}   ╟──│─┘  ║               ║
  5535.  ║   LIMIT{15-0}   ╟──│─┘  ╠═════════════════╣  └───╠═══════════════╣SEGMENT
  5536.  ╠═════════════════╣  │    ║                 ║       ║               ║BASE
  5537.  ║       LDT       ║  │    ║                 ║       ║               ║
  5538.   DESCRIPTION IN    │    ║                 ║       ║               ║
  5539.   THE GDT IN MEMORY   │    ╠═════════════════╣       ║               ║
  5540.                       │    ≈                 ≈       ║               ║
  5541.                       │    ║                 ║       ║               ║
  5542.                       └───╠═════════════════╣       ║               ║
  5543.                            ║   DESCRIPTOR    ║       ║    SEGMENT    ║
  5544.                              TABLES IN RAM             IN RAM     
  5545.  
  5546.  
  5547.  6.4  Virtual-to-Physical Address Translation
  5548.  
  5549.  The translation of a full 32-bit virtual address pointer into a real 24-bit
  5550.  physical address is shown by figure 6-6. When the segment's base address is
  5551.  determined as a result of the mapping process, the offset value is added to
  5552.  the result to obtain the physical address.
  5553.  
  5554.  The actual mapping is performed on the selector component of the virtual
  5555.  address. The 16-bit segment selector is mapped to a 24-bit segment base
  5556.  address via a segment descriptor maintained in one of the descriptor tables.
  5557.  
  5558.  The TI bit in the segment selector (see figure 6-1) determines which of two
  5559.  descriptor tables, either the GDT or the current LDT, is to be chosen for
  5560.  memory mapping. In either case, using the GDTR or LDTR register, the
  5561.  processor can readily determine the physical base address of the
  5562.  memory-resident table.
  5563.  
  5564.  The INDEX field in the segment selector specifies a particular descriptor
  5565.  entry within the chosen table. The processor simply multiplies this index
  5566.  value by 8 (the length of a descriptor), and adds the result to the base
  5567.  address of the descriptor table in order to access the appropriate segment
  5568.  descriptor in the table.
  5569.  
  5570.  Finally, the segment descriptor contains the physical base address of the
  5571.  target segment, as well as size (limit) and access information. The
  5572.  processor sums the 24-bit segment base and the specified 16-bit offset to
  5573.  generate the resulting 24-bit physical address.
  5574.  
  5575.  
  5576.  Figure 6-6.  Virtual-to-Physical Address Translation
  5577.  
  5578.      ╔═════════════════════════════════════════════════════╗
  5579.      ║                   VIRTUAL ADDRESS                   ║
  5580.      ║ ╔════════════════════════╤════════════════════════╗ ║     TARGET  
  5581.      ║ ║        SELECTOR        │         OFFSET         ║ ║    ║ SEGMENT ║
  5582.      ║ ╚════╤═══════════╤═══════╧═════╤══════════════════╝ ║    ║         ║
  5583.      ╚══════│═══════════│═════════════│════════════════════╝    ║         ║
  5584.             │           │ TI                                   ║         ║
  5585.             │           │            ╔═╗                        ╟─────────╢
  5586.             │                       ║+╟───────────────────────║  DATUM  ║
  5587.             │       DESCRIPTOR       ╚═╝    PHYSICAL ADDRESS    ╟─────────╢
  5588.             │        TABLE                                   ║         ║
  5589.             │     ╠════════════╣      │                         ║         ║
  5590.             │     ≈            ≈      │                         ║         ║
  5591.             │     ╟────────────╢      │                         ║         ║
  5592.             │     ║  SEGMENT   ║      │            SEGMENT BASE ║         ║
  5593.                  ║ DESCRIPTOR ╟──────┴────────────────────────╠═════════╣
  5594.       ─ ─ ─ ─ ─ ─ ╫────────────╢                                ║         ║
  5595.       INDEX      ≈            ≈                                         
  5596.       ─ ─ ─ ┴ ─ ─ ╫════════════╣
  5597.                               
  5598.  
  5599.  
  5600.  6.5  Segments and Segment Descriptors
  5601.  
  5602.  Segments are the basic units of 80286 memory management. In contrast to
  5603.  schemes based on fixed-size pages, segmentation allows for a very efficient
  5604.  implementation of software: variable-length segments can be tailored to the
  5605.  exact requirements of an application. Segmentation, moreover, is consistent
  5606.  with the way a programmer naturally deals with his virtual address space:
  5607.  programmers are encouraged to divide code and data into clearly defined
  5608.  modules and structures which are manipulated as consistent entities. This
  5609.  reduces (minimizes) the potential for virtual memory thrashing.
  5610.  Segmentation also eliminates the restrictions on data structures that span a
  5611.  page (e.g., a word that crosses page boundaries).
  5612.  
  5613.  Each segment within an 80286 system is defined by an associated segment
  5614.  descriptor, which may appear in one or more descriptor tables. Its inclusion
  5615.  within a descriptor table represents the presence of its associated segment
  5616.  within the virtual address space defined by that table. Conversely, its
  5617.  ommission from a descriptor table means that the segment is absent from the
  5618.  corresponding address space.
  5619.  
  5620.  As shown previously in figure 6-3, an 8-byte segment descriptor encodes the
  5621.  following information about a particular segment:
  5622.  
  5623.    ■  Size. This 16-bit field, comprising bytes 0 and 1 of a segment
  5624.       descriptor, specifies an unsigned integer as the size, in bytes (from 1
  5625.       byte to 64K bytes), of the segment.
  5626.  
  5627.       Unlike segments in the 8086 (or the 80286 in Real Address Mode)──which
  5628.       are never explicitly limited to less than a full 64K bytes──Protected
  5629.       Mode segments are always assigned a specific size value. In conjunction
  5630.       with the protection features described in Chapter 7, this assigned
  5631.       size allows the enforcement of a very desirable and natural rule:
  5632.       inadvertent accesses to locations beyond a segment's actual boundaries
  5633.       are prohibited.
  5634.  
  5635.    ■  Base. This 24-bit field, comprising bytes 2 through 4 of a segment
  5636.       descriptor, specifies the physical base address of the segment; it thus
  5637.       defines the actual location of the segment within the 16-megabyte real
  5638.       memory space. The base may be any byte address within the 16-megabyte
  5639.       real memory space.
  5640.  
  5641.    ■  Access. This 8-bit field comprises byte 5 of a segment descriptor.
  5642.       This access byte specifies a variety of additional information about a
  5643.       segment, particularly in regard to the protection features of the
  5644.       80286. For example, code segments are distinguished from data
  5645.       segments; and certain special access restrictions (such as Execute-Only
  5646.       or Read-Only) may be defined for segments of each type. Access byte
  5647.       values of 00H or 80H will always denote "invalid."
  5648.  
  5649.  Figure 6-7 shows the access byte format for both code and data segment
  5650.  descriptors. Detailed discussion of the protection related fields within an
  5651.  access byte (Conforming, Execute-Only, Descriptor Privilege Level, Expand
  5652.  Down, and Write-Permitted), and their use in implementing protection
  5653.  policies, is deferred to Chapter 7. The two fields Accessed and Present are
  5654.  used for virtual memory implementations.
  5655.  
  5656.  
  5657.  Figure 6-7.  Segment Descriptor Access Bytes
  5658.  
  5659.                                             CODE SEGMENT      TYPE
  5660.                                              MSB          ┌─────┴─────┐LSB
  5661.                                             ╔═══╤═════╤═══╤═══╤═══╤═══╤═══╗
  5662.                                             ║ P │ DPL │ 1 │ 1 │ C │ R │ A ║
  5663.                                             ╚═══╧═════╧═══╧═══╧═══╧═══╧═══╝
  5664.                                                └──┬──┘             
  5665.      PRESENT (1 = yes)────────────────────────┘        │   │   │   │   │
  5666.      DESCRIPTOR PRIVILEGE LEVEL────────────────────┘    │   │   │   │   │
  5667.      (indicates segment descriptor)─────────────────────┘   │   │   │   │
  5668.      EXECUTABLE (1 = yes for code)──────────────────────────┘   │   │   │
  5669.      CONFORMING (1 = yes)───────────────────────────────────────┘   │   │
  5670.      READABLE (1 = yes)─────────────────────────────────────────────┘   │
  5671.      ACCESSED (1 = yes)─────────────────────────────────────────────────┘
  5672.  
  5673.                                            DATA OR STACK SEGMENT
  5674.                                             MSB                        LSB
  5675.                                            ╔═══╤═════╤═══╤═══╤════╤═══╤═══╗
  5676.                                            ║ P │ DPL │ 1 │ 0 │ ED │ W │ A ║
  5677.                                            ╚═══╧═════╧═══╧═══╧════╧═══╧═══╝
  5678.                                                                   
  5679.      PRESENT (1 = yes)───────────────────────┘    │    │   │   │    │   │
  5680.      DESCRIPTOR PRIVILEGE LEVEL───────────────────┘    │   │   │    │   │
  5681.      (indicates segment descriptor)────────────────────┘   │   │    │   │
  5682.      EXECUTABLE (0 = no for data)  ────────────────────────┘   │    │   │
  5683.      CONFORMING (1 = yes)──────────────────────────────────────┘    │   │
  5684.      WRITEABLE (1 = yes)────────────────────────────────────────────┘   │
  5685.      ACCESSED (1 = yes)─────────────────────────────────────────────────┘
  5686.  
  5687.  
  5688.  6.6  Memory Management Registers
  5689.  
  5690.  The Protected Virtual Address Mode features of the 80286 operate at high
  5691.  performance due to extensions to the basic 8086 register set. Figure 6-8
  5692.  illustrates that portion of the extended register structure that pertains to
  5693.  memory management. (For a complete summary of all Protected Mode registers,
  5694.  refer to section 10.1).
  5695.  
  5696.  
  5697.  6.6.1  Segment Address Translation Registers
  5698.  
  5699.  Figure 6-8 shows the segment registers CS, DS, ES, and SS. In contrast to
  5700.  their usual representation, however, these registers are now depicted as
  5701.  64-bit registers, each with "visible" and "hidden" components.
  5702.  
  5703.  The visible portions of these segment address translation registers are
  5704.  manipulated by programs exactly as if they were simply the 16-bit segment
  5705.  registers of Real Address Mode. By loading a segment selector into one of
  5706.  these registers, the program makes the associated segment one of its four
  5707.  currently addressable segments.
  5708.  
  5709.  The operations that load these registers──or, more exactly, those that load
  5710.  the visible portion of these registers──are normal program instructions.
  5711.  These instructions may be divided into two categories:
  5712.  
  5713.    1.  Direct segment-register load instructions. These instructions (such
  5714.        as LDS, LES, MOV, POP, etc.) can explicitly reference the SS, DS, or
  5715.        ES segment registers as the destination operand.
  5716.  
  5717.    2.  Implied segment-register load instructions. These instructions (such
  5718.        as intersegment CALL and JMP) implicitly reference the CS code segment
  5719.        register; as a result of these operations, the contents of CS are
  5720.        altered.
  5721.  
  5722.  Using these instructions, a program loads the visible part of the segment
  5723.  register with a 16-bit selector (i.e., the high-order word of a virtual
  5724.  address pointer). Whenever this is done, the processor automatically uses
  5725.  the selector to reference the appropriate descriptor and loads the 48-bit
  5726.  hidden descriptor cache for that segment register.
  5727.  
  5728.  The correspondence between selectors and descriptors has already been
  5729.  described. Remember that the selector's TI bit indicates one of the two
  5730.  descriptor tables, either the LDT or the GDT. Within the indicated table, a
  5731.  particular entry is chosen by the selector's 13-bit INDEX field. This
  5732.  index, scaled by a factor of 8, represents the relative displacement of the
  5733.  chosen table entry (a descriptor).
  5734.  
  5735.  Thus, so long as a particular selector value is valid (i.e., it points to a
  5736.  valid segment descriptor within the bounds of the descriptor table), it can
  5737.  be readily associated with an 8-byte descriptor. When a selector value is
  5738.  loaded into the visible part of a segment register, the 80286 automatically
  5739.  loads 6 bytes of the associated descriptor into the hidden part of the
  5740.  register. These 6 bytes, therefore, contain the size, base, and access type
  5741.  of the selected segment. Figure 6-9 illustrates this transparent process of
  5742.  descriptor loading.
  5743.  
  5744.  In effect, the hidden descriptor fields of the segment registers function
  5745.  as the memory management cache of the 80286. All the information required to
  5746.  address the current working set of segments──that is, the base address,
  5747.  size, and access rights of the currently addressable segments──is stored in
  5748.  this memory cache. Unlike the probabilistic caches of other architectures,
  5749.  however, the 80286 cache is completely deterministic: the caching of
  5750.  descriptors is explicitly controlled by the program.
  5751.  
  5752.  Most memory references do not require the translation of a full 32-bit
  5753.  virtual address, or long pointer. Operands that are located within one of
  5754.  the currently addressable segments, as determined by the four segment
  5755.  registers, can be referenced very efficiently by means of a short pointer,
  5756.  which is simply a 16-bit offset.
  5757.  
  5758.  In fact, most 80286 instructions reference memory locations in precisely
  5759.  this way, specifying only a 16-bit offset with respect to one of the
  5760.  currently addressable segments. The choice of segments (CS, DS, ES, or SS)
  5761.  is either implicit within the instruction itself, or explicitly specified
  5762.  by means of a segment-override prefix (as described in Chapter 2).
  5763.  
  5764.  Thus, in most cases, virtual-to-physical address translation is actually
  5765.  performed in two separate steps. First, when a program loads a new value
  5766.  into a segment register, the processor immediately performs a mapping
  5767.  operation; the physical base address of the selected segment (as well as
  5768.  certain additional information) is automatically loaded into the hidden
  5769.  portion of the register. The internal cache registers (virtual address
  5770.  translation hardware) are therefore dynamically shared among the 16K
  5771.  different segments potentially addressable within the user's virtual address
  5772.  space. No software overhead (either system or application) is required to
  5773.  perform this operation.
  5774.  
  5775.  Subsequently, as the program utilizes a short pointer to reference a
  5776.  location within a segment, the processor generates a 24-bit physical address
  5777.  simply by adding the specified offset value to the previously cached segment
  5778.  base address. By encouraging the use of short pointers in this way, rather
  5779.  than requiring a full 32-bit virtual address for every memory reference, the
  5780.  80286 provides a very efficient on-chip mechanism for address translation,
  5781.  with minimum overhead for references to memory-based tables or the need for
  5782.  external address-translation devices.
  5783.  
  5784.  
  5785.  Figure 6-8.  Memory Management Registers
  5786.  
  5787.   SEGMENT ADDRESS TRANSLATION REGISTERS
  5788.  
  5789.       16-BIT       48-BIT HIDDEN DESCRIPTOR CACHE
  5790.      SELECTOR    (PROGRAM INVISIBLE──LOADED BY CPU)
  5791.    ╔═════════╤══════╤══════════════════╤══════════════╗
  5792.  CS║         │      │                  │              ║CODE SEGMENT REGISTER
  5793.    ╟─────────┼──────┼──────────────────┼──────────────╢
  5794.  DS║         │      │                  │              ║DATA SEGMENT REGISTER
  5795.    ╟─────────┼──────┼──────────────────┼──────────────╢
  5796.  ES║         │      │                  │              ║EXTRA SEGMENT REGISTER
  5797.    ╟─────────┼──────┼──────────────────┼──────────────╢
  5798.  SS║         │      │                  │              ║STACK SEGMENT REGISTER
  5799.    ╚═════════╧══════╧══════════════════╧══════════════╝
  5800.     63     48 47  40 39              16 15           0
  5801.               ACCESS     SEGMENT BASE       SEGMENT
  5802.               RIGHTS       ADDRESS           SIZE
  5803.  
  5804.  
  5805.   SYSTEM ADDRESS REGISTERS
  5806.  
  5807.           40-BIT EXPLICIT REGISTER
  5808.        ╔═════════════════╤══════════╗
  5809.   GDTR ║                            ║GLOBAL DESCRIPTOR TABLE REGISTER
  5810.        ╟─────────────────┼──────────╢
  5811.   IDTR ║                            ║INTERRUPT DESCRIPTOR TABLE REGISTER
  5812.        ╚═════════════════╧══════════╝
  5813.         39             16 15       0
  5814.               BASE           LIMIT
  5815.  
  5816.  
  5817.         16-BIT
  5818.        VISIBLE          40-BIT HIDDEN
  5819.        SELECTOR        DESCRIPTOR CACHE
  5820.      ╔══════════╤═════════════════╤══════════╗
  5821.  LDTR║          │                            ║LOCAL DESCRIPTOR TABLE REGISTER
  5822.      ╚══════════╧═════════════════╧══════════╝
  5823.       55      40 39             16 15       0
  5824.                        BASE           LIMIT
  5825.  
  5826.  
  5827.  Figure 6-9.  Descriptor Loading
  5828.  
  5829.       ┌── ── ── ── ── ── ── ── ── ── ──  ─┐
  5830.       │ CPU                               │
  5831.           APPLICATION       DESCRIPTOR                       
  5832.       │     VISIBLE           CACHE       │   ║    SYSTEM     ║
  5833.           ┌────┴─────┐     ┌────┴─────┐       ║    MEMORY     ║
  5834.       │     SEGMENT          SEGMENT      │   ║               ║
  5835.             REGISTER        DESCRIPTOR        ║               ║
  5836.       │   ╔══════════╗     ╔══════════╗   │   ║               ║
  5837.           ║ SELECTOR ║     ║   TYPE   ║       ║               ║
  5838.       │   ╚═════╤════╝     ╟──────────╢   │   ║               ║
  5839.                            ║   BASE   ║       ║               ║
  5840.       │         │          ╟──────────╢   │   ≈               ≈
  5841.                            ║  LIMIT   ║       ║               ║
  5842.       │         │          ╚══════════╝   │   ║               ║
  5843.                                              ║               ║
  5844.       │         │             │TRANSPARENT│   ║               ║
  5845.                               │DESCRIPTOR     ╠═══════════════╣─┐
  5846.       │         │             │LOADING    │   ║               ║ │
  5847.                               └───────────────╫───────────────╢ │
  5848.       │         │                         │   ║               ║ ├─DESCRIPTOR
  5849.                               ───────────────╟───────────────╢ │ TABLE
  5850.       │         └── ── ── ── ──   INDEX │   ║               ║ │
  5851.                 ┌ ── ── ── ── ── ─┴ ── ── ── ╠═══════════════╣─┘
  5852.       │                                   │   ║               ║
  5853.           ╔═════╧════╗                        ║               ║
  5854.       │   ║DESCRIPTOR║                    │   ║               ║
  5855.           ║TABLE BASE║                        ║               ║
  5856.       │   ╚══════════╝                    │                  
  5857.       └── ── ── ── ── ── ── ── ── ── ── ──┘
  5858.  
  5859.  
  5860.  6.6.2  System Address Registers
  5861.  
  5862.  The Global Descriptor Table Register (GDTR) is a dedicated 40-bit (5 byte)
  5863.  register used to record the base and size of a system's global descriptor
  5864.  table (GDT). Thus, two of these bytes define the size of the GDT, and three
  5865.  bytes define its base address.
  5866.  
  5867.  In figure 6-8, the contents of the GDTR are referred to as a "hidden
  5868.  descriptor." The term "descriptor" here emphasizes the analogy with the
  5869.  segment descriptors ordinarily found in descriptor tables. Just as these
  5870.  descriptors specify the base and size (limit) of ordinary segments, the
  5871.  GDTR register specifies these same parameters for that segment of memory
  5872.  serving as the system GDT. The limit prevents accesses to descriptors in the
  5873.  GDT from accessing beyond the end of the GDT and thus provides address space
  5874.  isolation at the system level as well as at the task level.
  5875.  
  5876.  The register contents are "hidden" only in the sense that they are not
  5877.  accessible by means of ordinary instructions. Instead, the dedicated
  5878.  protected instructions LGDT and SGDT are reserved for loading and storing,
  5879.  respectively, the contents of the GDTR at Protected Mode initialization
  5880.  (refer to section 10.2 for details). Subsequent alteration of the GDT base
  5881.  and size values is not recommended but is a system option at the most
  5882.  privileged level of software (see section 7.3 for a discussion of privilege
  5883.  levels).
  5884.  
  5885.  The Local Descriptor Table Register (LDTR) is a dedicated 40-bit register
  5886.  that contains, at any given moment, the base and size of the local
  5887.  descriptor table (LDT) associated with the currently executing task. Unlike
  5888.  GDTR, the LDTR register contains both a "visible" and a "hidden" component.
  5889.  Only the visible component is accessible, while the hidden component remains
  5890.  truly inaccessible even to dedicated instructions.
  5891.  
  5892.  The visible component of the LDTR is a 16-bit "selector" field. The format
  5893.  of these 16 bits corresponds exactly to that of a segment selector in a
  5894.  virtual address pointer. Thus, it contains a 13-bit INDEX field, a 1-bit TI
  5895.  field, and a 2-bit RPL field. The TI "table indicator" bit must be zero,
  5896.  indicating a reference to the GDT (i.e., to global address space). The INDEX
  5897.  field consequently provides an index to a particular entry within the GDT.
  5898.  This entry, in turn, must be an LDT descriptor (or descriptor table
  5899.  descriptor), as defined in the previous section. In this way, the visible
  5900.  "selector" field of the LDTR, by selecting an LDT descriptor, uniquely
  5901.  designates a particular LDT in the system.
  5902.  
  5903.  The dedicated, protected instructions LLDT and SLDT are reserved for
  5904.  loading and storing, respectively, the visible selector component of the
  5905.  LDTR register (refer to section 10.2 for details). Whenever a new value is
  5906.  loaded into the visible "selector" portion of LDTR, an LDT descriptor will
  5907.  have been uniquely chosen (assuming, of course, that the "selector" value is
  5908.  valid). In this case, the 80286 automatically loads the hidden "descriptor"
  5909.  portion of LDTR with five bytes from the chosen LDT descriptor. Thus, size
  5910.  and base information about a particular LDT, as recorded in a
  5911.  memory-resident global descriptor table entry, is cached in the LDTR
  5912.  register.
  5913.  
  5914.  New values may be loaded into the visible portion of the LDTR (and, thus,
  5915.  into the hidden portion as well) in either of two ways. The LLDT
  5916.  instruction, during system initialization, is used explicitly to set an
  5917.  initial value for the LDTR register; in this way, a local address space is
  5918.  provided for the first task in a multitasking environment. After system
  5919.  startup, explicit changes are not required since operations that
  5920.  automatically invoke a task switch (described in section 8.4) appropriately
  5921.  manage the LDTR.
  5922.  
  5923.  At all times, the LDTR register thus records the physical base address (and
  5924.  size) of the current task's LDT; the descriptor table required for mapping
  5925.  the current local address space, therefore, is immediately accessible to the
  5926.  processor. Moreover, since GDTR always maintains the base address of the
  5927.  GDT, the table that maps the global address space is similarly accessible.
  5928.  The two system address registers, GDTR and LDTR, act as a special processor
  5929.  cache, maintaining current information about the two descriptor tables
  5930.  required, at any given time, for addressing the entire current virtual
  5931.  address space.
  5932.  
  5933.  
  5934.  Chapter 7  Protection
  5935.  
  5936.  ───────────────────────────────────────────────────────────────────────────
  5937.  
  5938.  7.1  Introduction
  5939.  
  5940.  In most microprocessor based products, the product's availability, quality,
  5941.  and reliability are determined by the software it contains. Software is
  5942.  often the key to a product's success. Protection is a tool used to shorten
  5943.  software development time, and improve software quality and reliability.
  5944.  
  5945.  Program testing is an important step in developing software. A system with
  5946.  protection will detect software errors more quickly and accurately than a
  5947.  system without protection. Eliminating errors via protection reduces the
  5948.  development time for a product.
  5949.  
  5950.  Testing software is difficult. Many errors occur only under complex
  5951.  circumstances which are difficult to anticipate. The result is that products
  5952.  are shipped with undetected errors. When such errors occur, products appear
  5953.  unreliable. The impact of a software error is multiplied if it introduces
  5954.  errors in other bug-free programs. Thus, the total system reliability
  5955.  reduces to that of the least reliable program running at any given time.
  5956.  
  5957.  Protection improves the reliability of an entire system by preventing
  5958.  software errors in one program from affecting other programs. Protection can
  5959.  keep the system running even when some user program attempts an invalid or
  5960.  prohibited operation.
  5961.  
  5962.  Hardware protection performs run-time checks in parallel with the execution
  5963.  of the program. But, hardware protection has traditionally resulted in a
  5964.  design that is more expensive and slower than a system without protection.
  5965.  However, the 80286 provides hardware-enforced protection without the
  5966.  performance or cost penalties normally associated with protection.
  5967.  
  5968.  The protected mode 80286 implements extensive protection by integrating
  5969.  these functions on-chip. The 80286 protection is more comprehensive and
  5970.  flexible than comparable solutions. It can locate and isolate a large number
  5971.  of program errors and prevent the propagation of such errors to other tasks
  5972.  or programs. The protection of the total system detects and isolates bugs
  5973.  both during development and installed usage. Chapter 9 discusses exceptions
  5974.  in more detail.
  5975.  
  5976.  The remaining sections of this chapter explain the protection model
  5977.  implemented in the 80286.
  5978.  
  5979.  
  5980.  7.1.1  Types of Protection
  5981.  
  5982.  Protection in the 80286 has three basic aspects:
  5983.  
  5984.    1.  Isolation of system software from user applications.
  5985.    2.  Isolation of users from each other (Inter-task protection).
  5986.    3.  Data-type checking.
  5987.  
  5988.  The 80286 provides a four-level, ringed-type, increasingly-privileged
  5989.  protection mechanism to isolate applications software from various layers of
  5990.  system software. This is a major improvement and extension over the simpler
  5991.  two-level user/supervisor mechanism found in many systems. Software modules
  5992.  in a supervisor level are protected from modules in the application level
  5993.  and from software in less privileged supervisor levels.
  5994.  
  5995.  Restricting the addressability of a software module enables an operating
  5996.  system to control system resources and priorities. This is especially
  5997.  important in an environment that supports multiple concurrent users.
  5998.  Multi-user, multi-tasking, and distributed processing systems require this
  5999.  complete control of system resources for efficient, reliable operation.
  6000.  
  6001.  The second aspect of protection is isolating users from each other. Without
  6002.  such isolation an error in one user program could affect the operation of
  6003.  another error-free user program. Such subtle interactions are difficult to
  6004.  diagnose and repair. The reliability of applications programs is greatly
  6005.  enhanced by such isolation of users.
  6006.  
  6007.  Within a system or application level program, the 80286 will ensure that
  6008.  all code and data segments are properly used (e.g., data cannot be executed,
  6009.  programs cannot be modified, and offset must be within defined limits,
  6010.  etc.). Such checks are performed on every memory access to provide full
  6011.  run-time error checking.
  6012.  
  6013.  
  6014.  7.1.2  Protection Implementation
  6015.  
  6016.  The protection hardware of the 80286 establishes constraints on memory and
  6017.  instruction usage. The number of possible interactions between instructions,
  6018.  memory, and I/O devices is practically unlimited. Out of this very large
  6019.  field the protection mechanism limits interactions to a controlled,
  6020.  understandable subset. Within this subset fall the list of "correct"
  6021.  operations. Any operation that does not fall into this subset is not allowed
  6022.  by the protection mechanism and is signalled as a protection violation.
  6023.  
  6024.  To understand protection on the 80286, you must begin with its basic parts:
  6025.  segments and tasks. 80286 segments are the smallest region of memory which
  6026.  have unique protection attributes. Modular programming automatically
  6027.  produces separate regions of memory (segments) whose contents are treated as
  6028.  a whole. Segments reflect the natural construction of a program, e.g., code
  6029.  for module A, data for module A, stack for the task, etc. All parts of the
  6030.  segment are treated in the same way by the 80286. Logically separate regions
  6031.  of memory should be in separate segments.
  6032.  
  6033.  The memory segmentation model (see figure 7-1) of the 80286 was designed to
  6034.  optimally execute code for software composed of independent modules. Modular
  6035.  programs are easier to construct and maintain. Compared to monolithic
  6036.  software systems, modular software systems have enhanced capabilities, and
  6037.  are typically easier to develop and test for proper operation.
  6038.  
  6039.  Each segment in the system is defined by a memory-resident descriptor. The
  6040.  protection hardware prevents accesses outside the data areas and attempts to
  6041.  modify instructions, etc., as defined by the descriptors. Segmentation on
  6042.  the 80286 allows protection hardware to be integrated into the CPU for full
  6043.  data access control without any performance impact.
  6044.  
  6045.  The segmented memory architecture of the 80286 provides unique capabilities
  6046.  for regulating the transfer of control between programs.
  6047.  
  6048.  Programs are given direct but controlled access to other procedures and
  6049.  modules. This capability is the heart of isolating application and system
  6050.  programs. Since this access is provided and controlled directly by the 80286
  6051.  hardware, there is no performance penalty. A system designer can take
  6052.  advantage of the 80286 access control to design high-performance modular
  6053.  systems with a high degree of confidence in the integrity of the system.
  6054.  
  6055.  Access control between programs and the operating system is implemented via
  6056.  address space separation and a privilege mechanism. The address space
  6057.  control separates applications programs from each other while the privilege
  6058.  mechanism isolates system software from applications software. The
  6059.  privilege mechanism grants different capabilities to programs to access
  6060.  code, data, and I/O resources based on the associated protection level.
  6061.  Trusted software that controls the whole system is typically placed at the
  6062.  most privileged level. Ordinary application software does not have to deal
  6063.  with these control mechanisms. They come into play only when there is a
  6064.  transfer of control between tasks, or if the Operating System routines have
  6065.  to be invoked.
  6066.  
  6067.  The protection features of multiple privilege levels extend to ensuring
  6068.  reliable I/O control. However, for a system designer to enable only one
  6069.  specific level to do I/O would excessively constrain subsequent extensions
  6070.  or application development. Instead, the 80286 permits each task to be
  6071.  assigned a separate minimum level where I/O is allowed. I/O privilege is
  6072.  discussed in section 10.3.
  6073.  
  6074.  An important distinction exists between tasks and programs. Programs (e.g.,
  6075.  instructions in code segments) are static and consist of a fixed set of code
  6076.  and data segments each with an associated privilege level. The privilege
  6077.  assigned to a program determines what the program may do when executed by a
  6078.  task. Privilege is assigned to a program when the system is built or when
  6079.  the program is loaded.
  6080.  
  6081.  Tasks are dynamic; they execute one or more programs. Task privilege
  6082.  changes with time according to the privilege level of the program being
  6083.  executed. Each task has a unique set of attributes that define it, e.g.,
  6084.  address space, register values, stack, data, etc. A task may execute a
  6085.  program if that program appears in the task's address space. The rules of
  6086.  protection control determine when a program may be executed by a task, and
  6087.  once executed, determine what the program may do.
  6088.  
  6089.  
  6090.  Figure 7-1.  Addressing Segments of a Module within a Task
  6091.  
  6092.                                                ┌─ ── ─┐
  6093.                                                ╔══════╗
  6094.                                                ║ CODE ║
  6095.                                                ╟──────╢  MODULE A
  6096.                                                ║ DATA ║
  6097.                                                ╚══════╝
  6098.              CPU                               │      │
  6099.         ┌───────────┐                          ╔══════╗
  6100.         │ ╔═══════╗ │                          ║ CODE ║
  6101.         │ ║ CODE  ╟─┼─────────────────────────╟──────╢  MODULE B
  6102.         │ ╟───────╢ │                          ║ DATA ║
  6103.         │ ║ DATA  ╟─┼─────────────────────────╚══════╝
  6104.         │ ╟───────╢ │                          │      │
  6105.         │ ║ STACK ╟─┼───────────────┐          ╔══════╗
  6106.         │ ╟───────╢ │               │          ║      ║  TASK STACK
  6107.         │ ║ EXTRA ╟─┼───────────┐   └─────────╚══════╝
  6108.         │ ╚═══════╝ │           │              │      │
  6109.         │  SEGMENT  │           │              ╔══════╗  TASK
  6110.         │ REGISTERS │           │              ║      ║  DATA
  6111.         └───────────┘           └─────────────╚══════╝  BLOCK 1
  6112.                                                │      │
  6113.                                                ╔══════╗  TASK
  6114.                                                ║      ║  DATA
  6115.                                                ╚══════╝  BLOCK 2
  6116.                                                └─ ── ─┘
  6117.                                                 MEMORY
  6118.  
  6119.  
  6120.  7.2  Memory Management and Protection
  6121.  
  6122.  The protection hardware of the 80286 is related to the memory management
  6123.  hardware. Since protection attributes are assigned to segments, they are
  6124.  stored along with the memory management information in the segment
  6125.  descriptor. The protection information is specified when the segment is
  6126.  created. In addition to privilege levels, the descriptor defines the segment
  6127.  type (e.g., Code segment, Data segment, etc.). Descriptors may be created
  6128.  either by program development tools or by a loader in a dynamically loaded
  6129.  reprogrammable environment.
  6130.  
  6131.  The protection control information consists of a segment type, its
  6132.  privilege level, and size. These are fields in the access byte of the
  6133.  segment descriptor (see figure 7-2). This information is saved on-chip in
  6134.  the programmer invisible section of the segment register for fast access
  6135.  during execution. These entries are changed only when a segment register is
  6136.  loaded. The protection data is used at two times: upon loading a segment
  6137.  register and upon each reference to the selected segment.
  6138.  
  6139.  The hardware performs several checks while loading a segment register.
  6140.  These checks enforce the protection rules before any memory reference is
  6141.  generated. The hardware verifies that the selected segment is valid (is
  6142.  identified by a descriptor, is in memory, and is accessible from the
  6143.  privilege level in which the program is executing) and that the type is
  6144.  consistent with the target segment register. For example, you cannot load a
  6145.  read-only segment descriptor into SS because the stack must always be
  6146.  writable.
  6147.  
  6148.  Each reference into the segment defined by a segment register is checked by
  6149.  the hardware to verify that it is within the defined limits of the segment
  6150.  and is of the proper type. For example, a code segment or read-only data
  6151.  segment cannot be written. All these checks are made before the memory cycle
  6152.  is started; any violation will prevent that cycle from starting and cause
  6153.  an exception to occur. Since the checks are performed concurrently with
  6154.  address formation, there is no performance penalty.
  6155.  
  6156.  By controlling the access rights and privilege attributes of segments, the
  6157.  system designer can assure a program will not change its code or overwrite
  6158.  data belonging to another task. Such assurances are vital to maintaining
  6159.  system integrity in the face of error-prone programs.
  6160.  
  6161.  
  6162.   Figure 7-2.  Descriptor Cache Registers
  6163.  
  6164.                           ┌─ ── ── ── ── ── ── ── ── ── ── ── ── ── ── ── ─┐
  6165.         PROGRAM VISIBLE   │               PROGRAM INVISIBLE                │
  6166.  
  6167.                           │  ACCESS                                        │
  6168.        SEGMENT SELECTORS     RIGHTS  SEGMENT BASE ADDRESS   SEGMENT SIZE
  6169.       ╔═════════════════╗ │ ╔══════╤══════════════════════╤══════════════╗ │
  6170.     CS╟─────────────────╢   ╟──────┼──────────────────────┼──────────────╢
  6171.     DS╟─────────────────╢ │ ╟──────┼──────────────────────┼──────────────╢ │
  6172.     SS╟─────────────────╢   ╟──────┼──────────────────────┼──────────────╢
  6173.       ╚═════════════════╝ │ ╚══════╧══════════════════════╧══════════════╝ │
  6174.       15                0   47   40 39                 16 15            0
  6175.                           │                                                │
  6176.        SEGMENT REGISTERS          SEGMENT DESCRIPTOR CACHE REGISTERS
  6177.       (loaded by program) │                (loaded by CPU)                 │
  6178.                           └─ ── ── ── ── ── ── ── ── ── ── ── ── ── ── ── ─┘
  6179.  
  6180.  
  6181.  7.2.1  Separation of Address Spaces
  6182.  
  6183.  As described in Chapter 6, each task can address up to a gigabyte
  6184.  (2^(14) - 2 segments of up to 65,536 bytes each) of virtual memory defined
  6185.  by the task's LDT (Local Descriptor Table) and the system GDT. Up to
  6186.  one-half gigabyte (2^(13) segments of up to 65,536 bytes each) of the task's
  6187.  address space is defined by the LDT and represents the task's private
  6188.  address space. The remaining virtual address space is defined by the GDT and
  6189.  is common to all tasks in the system.
  6190.  
  6191.  Each descriptor table is itself a special kind of segment recognized by the
  6192.  80286 architecture. These tables are defined by descriptors in the GDT
  6193.  (Global Descriptor Table). The CPU has a set of base and limit registers
  6194.  that point to the GDT and the LDT of the currently running task. The local
  6195.  descriptor table register is loaded by a task switch operation.
  6196.  
  6197.  An active task can only load selectors that reference segments defined by
  6198.  descriptors in either the GDT or its private LDT. Since a task cannot
  6199.  reference descriptors in other LDTs, and no descriptors in its LDT refer to
  6200.  data or code belonging to other tasks, it cannot gain access to another
  6201.  tasks' private code and data (see figure 7-3).
  6202.  
  6203.  Since the GDT contains information that is accessible by all users (e.g.,
  6204.  library routines, common data, Operating System services, etc.), the 80286
  6205.  uses privilege levels and special descriptor types to control access (see
  6206.  section 7.2.2). Privilege levels protect more trusted data and code (in GDT
  6207.  and LDT) from less trusted access (WITHIN a task), while the private virtual
  6208.  address spaces defined by unique LDTs provide protection BETWEEN tasks (see
  6209.  figure 7-4).
  6210.  
  6211.  
  6212.  Figure 7-3.  80286 Virtual Address Space
  6213.  
  6214.                                       ╔═════════════════════════════════════╗
  6215.  ╔═════════════════════════════════╗  ║ ╔═════════════════════════════════╗ ║
  6216.  ║                   ╔══════╗65535 ║  ║ ║                   ╔══════╗65535 ║ ║
  6217.  ║                   ║ SEG. ║     ║  ║ ║                   ║ SEG. ║     ║ ║
  6218.  ║                   ║      ║OFFSET║  ║ ║                   ║      ║OFFSET║ ║
  6219.  ║  8191╔═════╗   ┌─╚══════╝0    ║  ║ ║  8191╔═════╗   ┌─╚══════╝0    ║ ║
  6220.  ║     ║ LDT ╟───┘               ║  ║ ║     ║ LDT ╟───┘               ║ ║
  6221.  ║     ║  A  ╟───┐               ║  ║ ║     ║  B  ╟───┐               ║ ║
  6222.  ║     0╚═════╝   │               ║  ║ ║     0╚═════╝   │               ║ ║
  6223.  ║                │  ╔══════╗65535 ║  ║ ║                │  ╔══════╗65535 ║ ║
  6224.  ║                │  ║ SEG. ║     ║  ║ ║                │  ║ SEG. ║     ║ ║
  6225.  ║                │  ║      ║OFFSET║  ║ ║                │  ║      ║OFFSET║ ║
  6226.  ║                └─╚══════╝0    ║  ║ ║                └─╚══════╝0    ║ ║
  6227.  ╚═════════════════════════════════╝  ║ ╚═════════════════════════════════╝ ║
  6228.     TASK A PRIVATE ADDRESS SPACE      ║    TASK B PRIVATE ADDRESS SPACE     ║
  6229.                                       ║                                     ║
  6230.  ╔═════════════════════════════════╗  ║ ╔═════════════════════════════════╗ ║
  6231.  ║                   ╔══════╗65535 ║  ║ ║                   ╔══════╗65535 ║ ║
  6232.  ║                   ║ SEG. ║     ║  ║ ║                   ║ SEG. ║     ║ ║
  6233.  ║                   ║      ║OFFSET║  ║ ║                   ║      ║OFFSET║ ║
  6234.  ║  8191╔═════╗   ┌─╚══════╝0    ║  ║ ║  8191╔═════╗   ┌─╚══════╝0    ║ ║
  6235.  ║     ║ LDT ╟───┘               ║  ║ ║     ║ GDT ╟───┘               ║ ║
  6236.  ║     ║  C  ╟───┐               ║  ║ ║     ║     ╟───┐               ║ ║
  6237.  ║     0╚═════╝   │               ║  ║ ║     0╚═════╝   │               ║ ║
  6238.  ║                │  ╔══════╗65535 ║  ║ ║                │  ╔══════╗65535 ║ ║
  6239.  ║                │  ║ SEG. ║     ║  ║ ║                │  ║ SEG. ║     ║ ║
  6240.  ║                │  ║      ║OFFSET║  ║ ║                │  ║      ║OFFSET║ ║
  6241.  ║                └─╚══════╝0    ║  ║ ║                └─╚══════╝0    ║ ║
  6242.  ╚═════════════════════════════════╝  ║ ╚═════════════════════════════════╝ ║
  6243.     TASK C PRIVATE ADDRESS SPACE      ║        SHARED ADDRESS SPACE         ║
  6244.                                       ╚═════════════════════════════════════╝
  6245.                                                TASK B ADDRESS SPACE
  6246.  
  6247.  Figure 7-4.  Local and Global Descriptor Table Definitions
  6248.  
  6249.                                                MEMORY
  6250.                                                         
  6251.                                   ┌─┬───╟───────────────╢─┐
  6252.                                   │      ╟───────────────╢ │
  6253.                      CPU          │ │    ╟───────────────╢ │
  6254.           ╔═════════════════════╗ │      ║       ∙       ║ │
  6255.           ║                     ║ │ │    ║       ∙       ║ ├─ GDT
  6256.           ║       15        0   ║ │      ║       ∙       ║ │
  6257.           ║       ╔═════════╗   ║ │ │    ╟───────────────╢ │
  6258.           ║   23  ║LDT LIMIT╟───╫─┘      ╟───────────────╢ │
  6259.           ║   ╔═══╨─────────╢   ║   │    ╟───────────────╢ │
  6260.      GDTR ║   ║   GDT BASE  ╟───╫───────╟───────────────╢─┘
  6261.           ║   ╚═════════════╝   ║        ║               ║
  6262.           ║                     ║        ║               ║
  6263.           ╟─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╢        ║               ║
  6264.           ║                     ║        ║     LDT{1}    ║
  6265.           ║     15         0    ║ ┌─┬───╟───────────────╢─┐
  6266.           ║     ╔══════════╗    ║ │      ╟───────────────╢ │
  6267.           ║     ║   LDT    ║    ║ │ │    ╟───────────────╢ │
  6268.           ║     ║ SELECTOR ║    ║ │      ║       ∙       ║ │
  6269.           ║     ╚══════════╝    ║ │ │    ║       ∙       ║ ├─ CURRENT LDT
  6270.           ║┌─ ── ── ── ── ── ──┐║ │      ║       ∙       ║ │
  6271.           ║│      15        0  │║ │ │    ╟───────────────╢ │
  6272.           ║       ╔═════════╗   ║ │      ╟───────────────╢ │
  6273.           ║│  23  ║LDT LIMIT╟──┼╫─┘ │    ╟───────────────╢ │
  6274.           ║   ╔═══╨─────────╢   ║  ┌────╟───────────────╢─┘
  6275.      LDTR ║│  ║   LDT BASE  ╟──┼╟──┘     ║               ║
  6276.           ║   ╚═════════════╝   ║        ║               ║
  6277.           ║│ PROGRAM INVISIBLE │║        ║               ║
  6278.           ║└─ ── ── ── ── ── ──┘║        ║     LDT{n}    ║
  6279.           ╚═════════════════════╝        ╟───────────────╢
  6280.                                          ╟───────────────╢
  6281.                                          ╟───────────────╢
  6282.                                          ╟───────────────╢
  6283.                                          ║       ∙       ║
  6284.                                          ║       ∙       ║
  6285.                                          ║       ∙       ║
  6286.                                          ╟───────────────╢
  6287.                                          ╟───────────────╢
  6288.                                          ╟───────────────╢
  6289.                                          ╟───────────────╢
  6290.                                          ╟───────────────╢
  6291.                                                         
  6292.  
  6293.  
  6294.  7.2.2  LDT and GDT Access Checks
  6295.  
  6296.  All descriptor tables have a limit used by the protection hardware to
  6297.  ensure address space separation of tasks. Each task's LDT can be a different
  6298.  size as defined by its descriptor in the GDT. The GDT may also contain less
  6299.  than 8191 descriptors as defined by the GDT limit value. The descriptor
  6300.  table limit identifies the last valid byte of the last descriptor in that
  6301.  table. Since each descriptor is eight bytes long, the limit value is
  6302.  N * 8 - 1 for N descriptors.
  6303.  
  6304.  Any attempt by a program to load a segment register, local descriptor table
  6305.  register (LDTR), or task register (TR) with a selector that refers to a
  6306.  descriptor outside the corresponding limit causes an exception with an error
  6307.  code identifying the invalid selector used (see figure 7-5).
  6308.  
  6309.  Not all descriptor entries in the GDT or LDT need contain a valid
  6310.  descriptor. There can be holes, or "empty" descriptors, in the LDT and GDT.
  6311.  "Empty" descriptors allow dynamic allocation and deletion of segments or
  6312.  other system objects without changing the size of the GDT or LDT. Any
  6313.  descriptor with an access byte equal to zero is considered empty. Any
  6314.  attempt to load a segment register with a selector that refers to an empty
  6315.  descriptor will cause an exception with an error code identifying the
  6316.  invalid selection.
  6317.  
  6318.  
  6319.  Figure 7-5.  Error Code Format (on the stack)
  6320.  
  6321.     15                                                3   2   1   0
  6322.    ╔═══════════════════════════════════════════════════╤═══╤═══╤═══╗
  6323.    ║                                                   │ T │ I │ E ║
  6324.    ║                       INDEX                       │ I │ D │ X ║
  6325.    ║                                                   │   │ T │ T ║
  6326.    ╚═══════════════════════════════════════════════════╧═╤═╧═╤═╧═╤═╝
  6327.    └─────────────────────────┬─────────────────────────┘ │   │   │
  6328.        ┌─────────────────────┘                           │   │   │
  6329.        │         ┌───────────────────────────────────────┘   │   │
  6330.        │         │             ┌─────────────────────────────┘   │
  6331.                                                               
  6332.  ┌───────┐┌─────────┐┌───────────────┐┌─────────────────────────────────────┐
  6333.  │ Entry ││ 1 means ││ 1 means use   ││ 1 means that an event external to   │
  6334.  │ in    ││   use   ││   IDT and     ││   the program caused the exception  │
  6335.  │ IDT,  ││   LDT   ││   ignore      ││   (i.e., external interrupt, single │
  6336.  │ GDT,  ││ 0 means ││   bit 2.      ││   step, processor extension error)  │
  6337.  │ or    ││   use   ││ 0 means bit 2 ││ 0 means that an exception occurred  │
  6338.  │ LDT   ││   GDT   ││   indicates   ││   while processing the instruction  │
  6339.  │       ││         ││   table usage ││   at CS:IP saved on the stack.      │
  6340.  └───────┘└─────────┘└───────────────┘└─────────────────────────────────────┘
  6341.  
  6342.  
  6343.  7.2.3  Type Validation
  6344.  
  6345.  After checking that a selector reference is within the bounds of a
  6346.  descriptor table and refers to a non-empty descriptor, the type of segment
  6347.  defined by the descriptor is checked against the destination register. Since
  6348.  each segment register has predefined functions, each must refer to certain
  6349.  types of segments (see section 7.4.1). An attempt to load a segment
  6350.  register in violation of the protection rules causes an exception.
  6351.  
  6352.  The "null" selector is a special type of segment selector. It has an index
  6353.  field of all zeros and a table indicator of 0. The null selector appears to
  6354.  refer to GDT descriptor entry #0 (see GDT in figure 7-3). This selector
  6355.  value may be used as a place holder in the DS or ES segment registers; it
  6356.  may be loaded into them without causing an exception. However, any attempt
  6357.  to use the null segment registers to reference memory will cause an
  6358.  exception and prevent any memory cycle from occurring.
  6359.  
  6360.  
  6361.  7.3  Privilege Levels and Protection
  6362.  
  6363.  As explained in section 6.2, each task has its own separate virtual address
  6364.  space defined by its LDT. All tasks share a common address space defined by
  6365.  the GDT. The system software then has direct access to task data and can
  6366.  treat all pointers in the same way.
  6367.  
  6368.  Protection is required to prevent programs from improperly using code or
  6369.  data that belongs to the operating system. The four privilege levels of the
  6370.  80286 provide the isolation needed between the various layers of the system.
  6371.  The 80286 privilege levels are numbered from 0 to 3, where 0 is the most
  6372.  trusted level, 3 the least.
  6373.  
  6374.  Privilege level is a protection attribute assigned to all segments. It
  6375.  determines which procedures can access the segment. Like access rights and
  6376.  limit checks, privilege checks are automatically performed by the hardware,
  6377.  and thus protect both data and code segments.
  6378.  
  6379.  Privilege on the 80286 is hierarchical. Operating system code and data
  6380.  segments placed at the most privileged level (0) cannot be accessed directly
  6381.  by programs at other privilege levels. Programs at privilege level 0 may
  6382.  access data at all other levels. Programs at privilege levels 1-3 may only
  6383.  access data at the same or less trusted (numerically greater) privilege
  6384.  levels. Figure 7-6 illustrates the privilege level protection of code or
  6385.  data within tasks.
  6386.  
  6387.  In figure 7-6, programs can access data at the same or outer level, but not
  6388.  at inner levels. Code and data segments placed at level 1 cannot be accessed
  6389.  by programs executing at levels 2 or 3. Programs at privilege level 0 can
  6390.  access data at level 1 in the course of providing service to that level.
  6391.  80286 provides mechanisms for inter-level transfer of control when needed
  6392.  (see section 7.5).
  6393.  
  6394.  The four privilege levels of the 80286 are an extension of the typical
  6395.  two-level user/supervisor privilege mechanism. Like user mode, application
  6396.  programs in the outer level are not permitted direct access to data
  6397.  belonging to more privileged system services (supervisor mode). The 80286
  6398.  adds two more privilege levels to provide protection for different layers of
  6399.  system software (system services, I/O drivers, etc.).
  6400.  
  6401.  
  6402.  Figure 7-6.  Code and Data Segments Assigned to a Privilege Level
  6403.  
  6404.                                       TASK C
  6405.                ┌───────────────────────────────────────────────────┐
  6406.                │ ╔═══════════════════════════════════════════════╗ │
  6407.                │ ║                 APPLICATIONS                  ║ │
  6408.                │ ║     ╔═══════════════════════════════════╗     ║ │
  6409.                │ ║     ║        CUSTOM EXTENSIONS          ║     ║ │
  6410.                │ ║     ║     ╔═══════════════════════╗     ║     ║ │
  6411.                │ ║     ║     ║    SYSTEM SERVICES    ║     ║     ║ │
  6412.                │ ║     ║     ║     ╔═══════════╗     ║     ║     ║ │
  6413.                │ ║     ║     ║     ║  KERNAL   ║     ║     ║     ║ │
  6414.                ╞═╟─────╫─────╫─────╫─────┬─────╫─────╫─────╫─────╢═╡
  6415.                │ ║     ║     ║     ║     │LEVEL║LEVEL║LEVEL║LEVEL║ │
  6416.                │ ║     ║     ║     ║     │  0  ║  1  ║  2  ║  3  ║ │
  6417.                │ ║     ║     ║     ╚═════╪═════╝     ║     ║     ║ │
  6418.                │ ║     ║     ║           │           ║     ║     ║ │
  6419.                │ ║     ║     ╚═══════════╪═══════════╝     ║     ║ │
  6420.                │ ║     ║                 │                 ║     ║ │
  6421.                │ ║     ╚═════════════════╪═════════════════╝     ║ │
  6422.                │ ║                       │                       ║ │
  6423.          TASK B┤ ╚═══════════════════════╧═══════════════════════╝ ├TASK A
  6424.                └────────────────────────┘ └────────────────────────┘
  6425.  
  6426.  
  6427.  7.3.1  Example of Using Four Privilege Levels
  6428.  
  6429.  Two extra privilege levels allow development of more reliable, and flexible
  6430.  system software. This is achieved by dividing the system into small,
  6431.  independent units. Figure 7-6 shows an example of the usage of different
  6432.  protection levels. Here, the most privileged level is called the kernel.
  6433.  This software would provide basic, application-independent, CPU-oriented
  6434.  services to all tasks. Such services include memory management, task
  6435.  isolation, multitasking, inter-task communication, and I/O resource
  6436.  control. Since the kernel is only concerned with simple functions and cannot
  6437.  be affected by software at other privilege levels, it can be kept small,
  6438.  safe, and understandable.
  6439.  
  6440.  Privilege level one is designated system services. This software provides
  6441.  high-level functions like file access scheduling, character I/O, data
  6442.  communcations, and resource allocation policy which are commonly expected in
  6443.  all systems. Such software remains isolated from applications programs and
  6444.  relies on the services of the kernel, yet cannot affect the integrity of
  6445.  level 0.
  6446.  
  6447.  Privilege level 2 is the custom operating system extensions level. It
  6448.  allows standard system software to be customized. Such customizing can be
  6449.  kept isolated from errors in applications programs, yet cannot affect the
  6450.  basic integrity of the system software. Examples of customized software are
  6451.  the data base manager, logical file access services, etc.
  6452.  
  6453.  This is just one example of protection mechanism usage. Levels 1 and 2 may
  6454.  be used in many different ways. The usage (or non-usage) is up to the system
  6455.  designer.
  6456.  
  6457.  Programs at each privilege level are isolated from programs at outer
  6458.  layers, yet cannot affect programs in inner layers. Programs written for
  6459.  each privilege level can be smaller, easier to develop, and easier to
  6460.  maintain than a monolithic system where all system software can affect all
  6461.  other system software.
  6462.  
  6463.  
  6464.  7.3.2  Privilege Usage
  6465.  
  6466.  Privilege applies to tasks and three types of descriptors:
  6467.  
  6468.    1.  Main memory segments
  6469.  
  6470.    2.  Gates (control descriptors for state or task transitions, discussed
  6471.        in sections 7.5.1, 8.3, 8.4 and 9.2)
  6472.  
  6473.    3.  Task state segments (discussed in Chapter 8).
  6474.  
  6475.  Task privilege is a dynamic value. It is derived from the code segment
  6476.  currently being executed. Task privilege can change only when a control
  6477.  transfers to a different code segment.
  6478.  
  6479.  Descriptor privilege, including code segment privilege, is assigned when
  6480.  the descriptor (and any associated segment) is created. The system designer
  6481.  assigns privilege directly when the system is constructed with the system
  6482.  builder (see the 80286 Builder User's Guide) or indirectly via a loader.
  6483.  
  6484.  Each task operates at only one privilege level at any given moment: namely
  6485.  that of the code segment being executed. (The conforming segments discussed
  6486.  in section 11.2 permit some flexibility in this regard.) However, as figure
  6487.  7-6 indicates, the task may contain segments at one, two, three, or four
  6488.  levels, all of which are to be used at appropriate times. The privilege
  6489.  level of the task, then, changes under the carefully enforced rules for
  6490.  transfer of control from one code segment to another.
  6491.  
  6492.  The descriptor privilege attribute is stored in the access byte of a
  6493.  descriptor and is called the Descriptor Privilege Level (DPL). Task
  6494.  privilege is called the Current Privilege Level (CPL). The least significant
  6495.  two bits of the CS register specify the CPL.
  6496.  
  6497.  A few general rules of privilege can be stated before the detailed
  6498.  discussions of later sections. Data access is restricted to those data
  6499.  segments whose privilege level is the same as or less privileged
  6500.  (numerically greater) than the current privilege level (CPL). Direct code
  6501.  access, e.g., via call or jump, is restricted to code segments of equal
  6502.  privilege. A gate (section 7.5.1) is required for access to code at more
  6503.  privileged levels.
  6504.  
  6505.  
  6506.  7.4  Segment Descriptor
  6507.  
  6508.  Although the format of access control information, discussed below, is
  6509.  similar for both data and code segment descriptors, the rules for accessing
  6510.  data segments differ from those for transferring control to code segments.
  6511.  Data segments are meant to be accessible from many privilege levels, e.g.,
  6512.  from other programs at the same level or from deep within the operating
  6513.  system. The main restriction is that they cannot be accessed by less
  6514.  privileged code.
  6515.  
  6516.  Code segments, on the other hand, are meant to be executed at a single
  6517.  privilege level. Transfers of control that cross privilege boundaries are
  6518.  tightly restricted, requiring the use of gates. Control transfers within a
  6519.  privilege level can also use gates, but they are not required. Control
  6520.  transfers are discussed in section 7.5.
  6521.  
  6522.  Protection checks are automatically invoked at several points in selecting
  6523.  and using new segments. The process of addressing memory begins when the
  6524.  currently executing program attempts to load a selector into one of the
  6525.  segment registers. As discussed in Chapter 6, the selector has the form
  6526.  shown in figure 7-7.
  6527.  
  6528.  When a new selector is loaded into a segment register, the processor
  6529.  accesses the associated descriptor to perform the necessary loading and
  6530.  privilege checks.
  6531.  
  6532.  The protection mechanism verifies that the selector points to a valid
  6533.  descriptor type for the segment register (see section 7.4.1). After
  6534.  verifying the descriptor type, the CPU compares the privilege level of the
  6535.  task (CPL) to the privilege level in the descriptor (DPL) before loading
  6536.  the descriptor's information into the cache.
  6537.  
  6538.  The general format of the eight bits in the segment descriptor's access
  6539.  rights byte is shown in table 7-1.
  6540.  
  6541.  For example, the access rights byte for a data and code segment present in
  6542.  real memory but not yet accessed (at the same privilege level) is shown in
  6543.  figure 7-8.
  6544.  
  6545.  Whenever a segment descriptor is loaded into a segment register, the
  6546.  accessed bit in the descriptor table is set to 1. This bit is useful for
  6547.  determining the usage profile of the segment.
  6548.  
  6549.  
  6550.  Table 7-1. Segment Access Rights Byte Format
  6551.  
  6552. ╓┌──────┌─────────────┌──────────────────────────────────────────────────────╖
  6553.  Bit    Name          Description
  6554.  Bit    Name          Description
  6555.  
  6556.  7      Present       1 means Present and addressable in real memory;
  6557.                       0 means not present. See section 11.3.
  6558.  
  6559.  6,5    DPL           2-bit Descriptor Privilege Level, 0 to 3.
  6560.  
  6561.  4      Segment       1 means Segment descriptor; 0 means control
  6562.                       descriptor.
  6563.  
  6564.           For Segment=1, the remaining bits have the following meanings:
  6565.  
  6566.  3      Executable    1 means code, 0 means data.
  6567.  
  6568.  2      C or ED       If code, Conforming: 1 means yes, 0 no.
  6569.                       If data, Expand Down: 1 yes, 0 no──normal case.
  6570.  
  6571.  1      R or W        If code, Readable: 1 means readable, 0 not.
  6572.                       If data, Writable: 1 means writable, 0 not.
  6573.  
  6574.  0      Accessed      1 if segment descriptor has been Accessed, 0 if not.
  6575.  Bit    Name          Description
  6576. 0      Accessed      1 if segment descriptor has been Accessed, 0 if not.
  6577.  
  6578.  
  6579.  ───────────────────────────────────────────────────────────────────────────
  6580.  NOTE
  6581.    When the Segment bit (bit 4) is 0, the descriptor is for a gate, a
  6582.    task state segment, or a Local Descriptor Table, and the meanings of bits
  6583.    0 through 3 change. Control transfers and descriptors are discussed in
  6584.    section 7.5.
  6585.  ───────────────────────────────────────────────────────────────────────────
  6586.  
  6587.  Table 7-2. Allowed Segment Types in Segment Registers
  6588.  
  6589.                                     Allowed Segment Types
  6590.                   ┌──────────────────────────┴─────────────────────────────┐
  6591.  Segment Register  Read Only     Read-Write     Execute Only   Execute-Read
  6592.                    Data Segment  Data Segment   Code Segment   Code Segment
  6593.        DS             Yes           Yes            No             Yes
  6594.        ES             Yes           Yes            No             Yes
  6595.        SS             No            Yes            No             No
  6596.        CS             No            No             Yes            Yes
  6597.  
  6598.  ─────────────────────────────────────────────────────────────────────────────
  6599.  NOTE
  6600.    The Intel reserved bytes in the segment descriptor must be set to 0 for
  6601.    compatibility with the 80386.
  6602.  ─────────────────────────────────────────────────────────────────────────────
  6603.  
  6604.  Figure 7-7.  Selector Fields
  6605.  
  6606.                                  SELECTOR
  6607.      ╔═══════════════════════════════════════════════════╤═══╤═══════╗
  6608.      ║                       INDEX                       │ T │       ║
  6609.      ║                                                   │ I │  RPL  ║
  6610.      ╚═══╧═══╧═══╧═══╧═══╧═══╧═══╧═══╧═══╧═══╧═══╧═══╧═══╧═══╧═══╧═══╝
  6611.        15                          8   7                   2   1   0
  6612.  
  6613.  ╔══════╤══════════════════════╤════════════════════════════════════════════╗
  6614.  ║ BITS │ NAME                 │ FUNCTION                                   ║
  6615.  ╟──────┼──────────────────────┼────────────────────────────────────────────╢
  6616.  ║ 1-0  │ REQUESTED PRIVELEGE  │ INDICATES SELECTOR PRIVILEGE LEVEL DESIRED ║
  6617.  ║      │     LEVEL (RPL)      │                                            ║
  6618.  ╟──────┼──────────────────────┼────────────────────────────────────────────╢
  6619.  ║ 2    │ TABLE INDICATOR (TI) │ TI = 0 USE GLOBAL DESCRIPTOR TABLE (GDT)   ║
  6620.  ║      │                      │ TI = 1 USE LOCAL DESCRIPTORTABLE (LDT)     ║
  6621.  ╟──────┼──────────────────────┼────────────────────────────────────────────╢
  6622.  ║ 15-3 │ INDEX                │ SELECT DESCRIPTOR ENTRY IN TABLE           ║
  6623.  ╚══════╧══════════════════════╧════════════════════════════════════════════╝
  6624.  
  6625.  
  6626.  Figure 7-8.  Access Byte Examples
  6627.  
  6628.        READABLE CODE SEGMENT                     WRITABLE CODE SEGMENT
  6629.  
  6630.      P   DPL  S   E   C   R   A               P   DPL  S   E   ED   W   A
  6631.   ╔══════════════════════════════╗         ╔═══════════════════════════════╗
  6632.   ║  1   01   1   1   0   1   0  ║         ║  1   01   1   0   0    1   0  ║
  6633.   ╚══════════════════════════════╝         ╚═══════════════════════════════╝
  6634.    7                            0           7                             0
  6635.  
  6636.  
  6637.  7.4.1  Data Accesses
  6638.  
  6639.  Data may be accessed in data segments or readable code segments. When DS or
  6640.  ES is loaded with a new selector, e.g., by an LDS, LES, or MOV to ES, SS, or
  6641.  DS instruction, the bits in the access byte are checked to verify legitimate
  6642.  descriptor type and access (see table 7-2). If any test fails, an error
  6643.  code is pushed onto the stack identifying the selector involved (see figure
  6644.  7-5 for the error code format).
  6645.  
  6646.  A privilege check is made when the segment register is loaded. In general,
  6647.  a data segment's DPL must be numerically greater than or equal to the CPL.
  6648.  The DPL of a descriptor loaded into the SS must equal the CPL. Conforming
  6649.  code segments are an exception to privilege checking rules (see section
  6650.  11.2).
  6651.  
  6652.  Once the segment descriptor and selector are loaded, the offset of
  6653.  subsequent accesses within the segment are checked against the limit given
  6654.  in the segment descriptor. Violating the segment size limit causes a General
  6655.  Protection exception with an error code of 0.
  6656.  
  6657.  A normal data segment is addressed with offset values ranging from 0 to the
  6658.  size of the segment. When the ED bit of the access rights byte in the
  6659.  segment descriptor is 0, the allowed range of offsets is 0000H to the limit.
  6660.  If limit is 0FFFFH, the data segment contains 65,536 bytes.
  6661.  
  6662.  Since stacks normally occupy different offset ranges (lower limit to
  6663.  0FFFFH) than data segments, the limit field of a segment descriptor can be
  6664.  interpreted in two ways. The Expand Down (ED) bit in the access byte allows
  6665.  offsets for stack segments to be greater than the limit field. When ED is
  6666.  1, the allowed range of offsets within the segment is limit + 1 to 0FFFFH.
  6667.  To allow a full stack segment, set ED to 1 and the limit to 0FFFFH. The ED
  6668.  bit of a data segment descriptor does not have to be set for use in SS
  6669.  (i.e., it will not cause an exception). Section 7.5.1.4 discusses stack
  6670.  segment usage in greater detail. An expand down (ED=1) segment can also be
  6671.  loaded into ES or DS.
  6672.  
  6673.  Limit and access checks are performed before any memory reference is
  6674.  started. For stack push instructions (PUSH, PUSHA, ENTER, CALL, INT), a
  6675.  possible limit violation is identified before any internal registers are
  6676.  updated. Therefore, these instructions are fully restartable after a stack
  6677.  size violation.
  6678.  
  6679.  
  6680.  7.4.2  Code Segment Access
  6681.  
  6682.  Code segments are accessed via CS for execution. Segments that are
  6683.  execute-only can ONLY be executed; they cannot be accessed via DS or ES, nor
  6684.  read via CS with a CS override prefix. If a segment is executable (bit 3=1
  6685.  in the access byte), access via DS or ES is possible only if it is also
  6686.  readable. Thus, any code segment that also contains data must be readable.
  6687.  (Refer to Chapter 2 for a discussion of segment override prefixes.)
  6688.  
  6689.  An execute-only segment preserves the privacy of the code against any
  6690.  attempt to read it; such an attempt causes a general protection fault with
  6691.  an error code of 0. A code segment cannot be loaded into SS and is never
  6692.  writable. Any attempted write will cause a general protection fault with an
  6693.  error code of 0.
  6694.  
  6695.  The limit field of a code segment descriptor identifies the last byte in
  6696.  the segment. Any offset greater than the limit value will cause a general
  6697.  protection fault. The prefetcher of the 80286 can never cause a code segment
  6698.  limit violation with an error code of 0. The program must actually attempt
  6699.  to execute an instruction beyond the end of the code segment to cause an
  6700.  exception.
  6701.  
  6702.  If a readable non-conforming code segment is to be loaded into DS or ES,
  6703.  the privilege level requirements are the same as those stated for data
  6704.  segments in 7.4.1.
  6705.  
  6706.  Code segments are subject to different privilege checks when executed. The
  6707.  normal privilege requirement for a jump or call to another code segment is
  6708.  that the current privilege level equal the descriptor privilege level of the
  6709.  new code segment. Jumps and calls within the current code segment
  6710.  automatically obey this rule.
  6711.  
  6712.  Return instructions may pass control to code segments at the same or less
  6713.  (numerically greater) privileged level. Code segments at more privileged
  6714.  levels may only be reached via a call through a call gate as described in
  6715.  section 7.5.
  6716.  
  6717.  An exception to this, previously stated, is the conforming code segment
  6718.  that allows the DPL of the requested code segment to be numerically less
  6719.  than (of greater privilege than) the CPL. Conforming code segments are
  6720.  discussed in section 11.2.
  6721.  
  6722.  
  6723.  7.4.3  Data Access Restriction by Privilege Level
  6724.  
  6725.  This section describes privilege verification when accessing either data
  6726.  segments (loading segment selectors into DS, ES, or SS) or readable code
  6727.  segments. Privilege verification when loading CS for transfer of control
  6728.  across privilege levels is described in the next section.
  6729.  
  6730.  Three basic kinds of privilege level indicators are used when determining
  6731.  accessibility to a segment for reading and writing. They are termed Current
  6732.  Privilege Level (CPL), Descriptor Privilege Level (DPL), and Requested
  6733.  Privilege Level (RPL). The CPL is simply the privilege level of the code
  6734.  segment that is executing (except if the current code segment is
  6735.  conforming). The CPL is stored as bits 0 and 1 of the CS and SS registers.
  6736.  Bits 0 and 1 of DS and ES are not related to CPL.
  6737.  
  6738.  DPL is the privilege level of the segment; it is stored in bits 5 and 6 of
  6739.  the access byte of a descriptor. For data access to data segments and
  6740.  non-conforming code segments, CPL must be numerically less than or equal to
  6741.  DPL (the task must be of equal or greater privilege) for access to be
  6742.  granted. Violation of this rule during segment load instruction causes a
  6743.  general protection exception with an error code identifying the selector.
  6744.  
  6745.  While the enforcement of DPL protection rules provides the mechanism for
  6746.  the isolation of code and data at different privilege levels, it is
  6747.  conceivable that an erroneous pointer passed onto a more trusted program
  6748.  might result in the illegal modification of data with a higher privilege
  6749.  level. This possibility is prevented by the enforcement of effective
  6750.  privilege level protection rules and correct usage of the RPL value.
  6751.  
  6752.  The RPL (requested privilege level) is used for pointer validation. It is
  6753.  the least significant two bits in the selector value loaded into any segment
  6754.  register. RPL is intended to indicate the privilege level of the originator
  6755.  of that selector. A selector may be passed down through several procedures
  6756.  at different levels. The RPL reflects the privilege level of the original
  6757.  supplier of the selector, not the privilege level of the intermediate
  6758.  supplier. The RPL must be numerically less than or equal to the DPL of the
  6759.  descriptor selected, thereby indicating greater or equal privilege of the
  6760.  supplier; otherwise, access is denied and a general protection violation
  6761.  occurs.
  6762.  
  6763.  Pointer validity testing is required in any system concerned with
  6764.  preventing program errors from destroying system integrity. The 80286
  6765.  provides hardware support for pointer validity testing. The RPL field
  6766.  indicates the privilege level of the originator of the pointer to the
  6767.  hardware. Access will be denied if the originator of the pointer did not
  6768.  have access to the selected segment even if the CPL is numerically less than
  6769.  or equal to the DPL. RPL can reduce the effective privilege of a task when
  6770.  using a particular selector. RPL never allows access to more privileged
  6771.  segments (CPL must always be numerically less than or equal to DPL).
  6772.  
  6773.  A fourth term is sometimes used: the Effective Privilege Level (EPL). It is
  6774.  defined as the numeric maximum of the CPL and the RPL──meaning the one of
  6775.  lesser privilege. Access to a protected entity is granted only when the EPL
  6776.  is numerically less than or equal to the DPL of that entity. This is simply
  6777.  another way of saying that both CPL and RPL must be numerically less than
  6778.  or equal to DPL for access to be granted.
  6779.  
  6780.  
  6781.  7.4.4  Pointer Privilege Stamping via ARPL
  6782.  
  6783.  The ARPL instruction is provided in the 80286 to fill the RPL field of a
  6784.  selector with the minimum privilege (maximum numeric value) of the
  6785.  selector's current RPL and the caller's CPL (given in an
  6786.  instruction-specified register). A straight insertion of the caller's CPL
  6787.  would stamp the pointer with the privilege level of the caller, but not
  6788.  necessarily the ultimate originator of the selector (e.g., Level 3 supplies
  6789.  a selector to a level 2 routine that calls a level 0 routine with the same
  6790.  selector).
  6791.  
  6792.  Figure 7-9 shows a program with an example of such a situation. The program
  6793.  at privilege level 3 calls a routine at level 2 via a gate. The routine at
  6794.  level 2 uses the ARPL instruction to assure that the selector's RPL is 3.
  6795.  When the level 2 routine calls a routine at level 0 and passes the
  6796.  selector, the ARPL instruction at level 0 leaves the RPL field unchanged.
  6797.  
  6798.  Stamping a pointer with the originator's privilege eliminates the complex
  6799.  and time-consuming software typically associated with pointer validation in
  6800.  less comprehensive architectures. The 80286 hardware performs the pointer
  6801.  test automatically while loading the selector.
  6802.  
  6803.  Privilege errors are trapped at the time the selector is loaded because
  6804.  pointers are commonly passed to other routines, and it may not be possible
  6805.  to identify a pointer's originator. To verify the access capabilities of a
  6806.  pointer, it should be tested when the pointer is first received from an
  6807.  untrusted source. The VERR (Verify Read), VERW (Verify Write), and LAR (Load
  6808.  Access Rights) instructions are provided for this purpose.
  6809.  
  6810.  Although pointer validation is fully supported in the 80286, its use is an
  6811.  option of the system designer. To accommodate systems that do not require
  6812.  it, RPL can be ignored by setting selector RPLs to zero (except stack
  6813.  segment selectors) and not adjusting them with the ARPL instruction.
  6814.  
  6815.  
  6816.  Figure 7-9. Pointer Privilege Stamping
  6817.  
  6818.  Level 3      PUSH  SELECTOR          ; RPL value doesn't matter at level 3
  6819.               CALL  LEVEL_2
  6820.  
  6821.         Level_2:
  6822.               ENTER 4,0
  6823.               MOV   AX, [BP] + 4      ; GET CS of return address, RPL=3
  6824.               ARPL  [BP] + 6, AX      ; Put 3 in RPL field
  6825.  Level 2       ∙
  6826.                ∙
  6827.                ∙
  6828.               PUSH  WORD PTR [BP] + 6 ; Pass selector
  6829.               CALL  LEVEL_0
  6830.  
  6831.         Level_0:
  6832.               ENTER 6,0
  6833.  Level 0      MOV   AX, [BP] + 4      ; Get CS of return address, RPL=2
  6834.               ARPL  [BP] + 6, AX      ; Leaves RPL unchanged
  6835.  
  6836.  
  6837.  7.5  Control Transfers
  6838.  
  6839.  Three kinds of control transfers can occur within a task:
  6840.  
  6841.    1.  Within a segment, causing no change of privilege level (a short jump,
  6842.        call, or return).
  6843.  
  6844.    2.  Between segments at the same privilege level (a long jump, call, or
  6845.        return).
  6846.  
  6847.    3.  Between segments at different privilege levels (a long call, or
  6848.        return). (NOTE: A JUMP to a different privilege level is not allowed.)
  6849.  
  6850.  The first two types of control transfers need no special controls (with
  6851.  respect to privilege protection) beyond those discussed in section 7.4.
  6852.  
  6853.  Inter-level transfers require special consideration to maintain system
  6854.  integrity. The protection hardware must check that:
  6855.  
  6856.    ■  The task is currently allowed to access the destination address.
  6857.    ■  The correct entry address is used.
  6858.  
  6859.  To achieve control transfers, a special descriptor type called a gate is
  6860.  provided to mediate the change in privilege level. Control transfer
  6861.  instructions call the gate rather than transfer directly to a code segment.
  6862.  From the viewpoint of the program, a control transfer to a gate is the same
  6863.  as to another code segment.
  6864.  
  6865.  Gates allow programs to use other programs at more privileged levels in the
  6866.  same manner as a program at the same privilege level. Programmers need never
  6867.  distinguish between programs or subroutines that are more privileged than
  6868.  the current program and those that are not. The system designer may,
  6869.  however, elect to use gates only for control transfers that cross privilege
  6870.  levels.
  6871.  
  6872.  
  6873.  7.5.1  Gates
  6874.  
  6875.  A gate is a four-word control descriptor used to redirect a control
  6876.  transfer to a different code segment in the same or more privileged level or
  6877.  to a different task. There are four types of gates: call, trap, interrupt,
  6878.  and task gates. The access rights byte distinguishes a gate from a segment
  6879.  descriptor, and determines which type of gate is involved. Figure 7-10
  6880.  shows the format of a gate descriptor.
  6881.  
  6882.  A key feature of a gate is the re-direction it provides. All four gate
  6883.  types define a new address which transfers control when invoked. This
  6884.  destination address normally cannot be accessed by a program. Loading the
  6885.  selector to a call gate into SS, DS, or ES will cause a general protection
  6886.  fault with an error code identifying the invalid selector.
  6887.  
  6888.  Only the selector portion of an address is used to invoke a gate. The
  6889.  offset is ignored. All that a program need know about the desired function
  6890.  is the selector required to invoke the gate. The 80286 will automatically
  6891.  start the execution at the correct address stored within the gate.
  6892.  
  6893.  A further advantage of a gate is that it provides a fixed address for any
  6894.  program to invoke another program. The calling program's address remains
  6895.  unaltered even if the entry address of the destination program changes.
  6896.  Thus, gates provide a fixed set of entry points that allow a task to access
  6897.  Operating System functions such as simple subroutines, yet the task is
  6898.  prohibited from simply jumping into the middle of the Operating System.
  6899.  
  6900.  Call gates, as described in the next section, are used for control
  6901.  transfers within a task which must either be transparently redirected or
  6902.  which require an increase in privilege level. A call gate normally specifies
  6903.  a subroutine at a greater privilege level, and the called routine returns
  6904.  via a return instruction. Call gates also support delayed binding
  6905.  (resolution of target routine addresses at run-time rather than
  6906.  program-generation-time).
  6907.  
  6908.  Trap and interrupt gates handle interrupt operations that are to be
  6909.  serviced within the current task. Interrupt gates cause interrupts to be
  6910.  disabled; trap gates do not. Trap and interrupt gates both require a return
  6911.  via the interrupt return instruction.
  6912.  
  6913.  Task gates are used to control transfers between tasks and to make use of
  6914.  task state segments for task control and status information. Tasks are
  6915.  discussed in Chapter 8, interrupts in Chapter 9.
  6916.  
  6917.  In the 80286 protection model, each privilege level has its own stack.
  6918.  Therefore, a control transfer (call or return) that changes the privilege
  6919.  level causes a new stack to be invoked.
  6920.  
  6921.  
  6922.  Figure 7-10.  Gate Descriptor Format
  6923.  
  6924.                                        0  7                            0
  6925.         ╔═══════════════════════════════════════════════════════════════╗
  6926.      +7 ║                        INTEL RESERVED                        ║ +6
  6927.         ╟───┬───────┬───┬───────────────┬───────────────────────────────╢
  6928.      +5 ║ P │  DPL  │ 0 │ 0   1   0   1 │            UNUSED             ║ +4
  6929.         ╟───┴───┴───┴───┴───┴───┴───┴───┴───────────────────────────────╢
  6930.      +3 ║                         TSS SELECTOR                          ║ +2
  6931.         ╟───────────────────────────────────────────────────────────────╢
  6932.      +1 ║                            UNUSED                             ║ 0
  6933.         ╚═══════════════════════════════════════════════════════════════╝
  6934.           15                                                           0
  6935.  
  6936.                              Gate Descriptor Fields
  6937.   ╔═══════════════╤══════════╤═════════════════════════════════════════════╗
  6938.   ║ Name          │ Value    │ Description                                 ║
  6939.   ╠═══════════════╪══════════╪═════════════════════════════════════════════╣
  6940.   ║               │    4     │ Call Gate.                                  ║
  6941.   ║     TYPE      │    5     │ Task Gate.                                  ║
  6942.   ║               │    6     │ Interrupt Gate.                             ║
  6943.   ║               │    7     │ Trap Gate.                                  ║
  6944.   ╟───────────────┼──────────┼─────────────────────────────────────────────╢
  6945.   ║       P       │    0     │ Descriptor Contents are not valid.          ║
  6946.   ║               │    1     │ Descriptor Contents are valid.              ║
  6947.   ╟───────────────┼──────────┼─────────────────────────────────────────────╢
  6948.   ║      DPL      │   0-3    │ Descriptor Privilege Level.                 ║
  6949.   ╟───────────────┼──────────┼─────────────────────────────────────────────╢
  6950.   ║               │   0-31   │ Number of words to copy from caller's       ║
  6951.   ║  WORD COUNT   │          │ stack to called procedure's stack. Only     ║
  6952.   ║               │          │ used with call gate.                        ║
  6953.   ╟───────────────┼──────────┼─────────────────────────────────────────────╢
  6954.   ║               │  16-bit  │ Selector to the target code segment (Call,  ║
  6955.   ║  DESTINATION  │ selector │ Interrupt or Trap Gate).                    ║
  6956.   ║   SELECTOR    │          │ Selector to the target task state segment   ║
  6957.   ║               │          │ (Task Gate).                                ║
  6958.   ╟───────────────┼──────────┼─────────────────────────────────────────────╢
  6959.   ║  DESTINATION  │  16-bit  │ Entry point within the target code segment. ║
  6960.   ║    OFFSET     │  offset  │                                             ║
  6961.   ╚═══════════════╧══════════╧═════════════════════════════════════════════╝
  6962.  
  6963.  
  6964.  7.5.1.1  Call Gates
  6965.  
  6966.  Call gate descriptors are used by call and jump instructions in the same
  6967.  manner as a code segment descriptor. The hardware automatically recognizes
  6968.  that the destination selector refers to a gate descriptor. Then, the
  6969.  operation of the instruction is expanded as determined by the contents of
  6970.  the call gate. A jump instruction can access a call gate only if the target
  6971.  code segment is at the same privilege level. A call instruction uses a call
  6972.  gate for the same or more privileged access.
  6973.  
  6974.  A call gate descriptor may reside in either the GDT or the LDT, but not in
  6975.  the IDT. Figure 7-10 gives the complete layout of a call gate descriptor.
  6976.  
  6977.  A call gate can be referred to by either the long JMP or CALL instructions.
  6978.  From the viewpoint of the program executing a JMP or CALL instruction, the
  6979.  fact that the destination was reached via a call gate and not directly from
  6980.  the destination address of the instruction is not apparent.
  6981.  
  6982.  The following is a description of the protection checks performed while
  6983.  transferring control (with the CALL instruction) through a call gate:
  6984.  
  6985.    ■  Verifying that access to the call gate is allowed. One of the
  6986.       protection features provided by call gates is the access checks made to
  6987.       determine if the call gate may be used (i.e., checking if the privilege
  6988.       level of the calling program is adequate).
  6989.  
  6990.    ■  Determining the destination address and whether a privilege transition
  6991.       is required. This feature makes privilege transitions transparent to
  6992.       the caller.
  6993.  
  6994.    ■  Performing the privilege transition, if required.
  6995.  
  6996.  Verifying access to a call gate is the same for any call gate and is
  6997.  independent of whether a JMP or CALL instruction was used. The rules of
  6998.  privilege used to determine whether a data segment may be accessed are
  6999.  employed to check if a call gate may be jumped-to or called. Thus,
  7000.  privileged subroutines can be hidden from untrusted programs by the absence
  7001.  of a call gate.
  7002.  
  7003.  When an inter-segment CALL or JMP instruction selects a call gate, the
  7004.  gate's privilege and presence will be checked. The gate's DPL (in the access
  7005.  byte) is checked against the EPL (MAX (task CPL, selector RPL)). If EPL >
  7006.  CPL, the program is less privileged than the gate and therefore it may not
  7007.  make a transition. In this case, a general protection fault occurs with an
  7008.  error code identifying the gate. Otherwise, the gate is accessible from the
  7009.  program executing the call, and the control transfer is allowed to continue.
  7010.  After the privilege checks, the descriptor presence is checked. If the
  7011.  present bit of the gate access rights byte is 0 (i.e., the target code
  7012.  segment is not present), not present fault occurs with an error code
  7013.  identifying the gate.
  7014.  
  7015.  The checks indicated in table 7-3 are applied to the contents of the call
  7016.  gate. Violating any of them causes the exception shown. The low order two
  7017.  bits of the error code are zero for these exceptions.
  7018.  
  7019.  
  7020.  Table 7-3. Call Gate Checks
  7021.  
  7022.            Type of Check                     Fault      Error Code
  7023.  Selector is not Null                           GP       0
  7024.  Selector is within Descriptor Table Limit      GP       Selector id
  7025.  Descriptor is a Code Segment                   GP       Code Segment id
  7026.  Code Segment is Present                        NP       Code Segment id
  7027.  Nonconforming Code Segment DPL > CPL           GP       Code Segment id
  7028.  
  7029.  ───────────────────────────────────────────────────────────────────────────
  7030.  NOTE
  7031.    The offset portion of the JMP or CALL destination address which refers to
  7032.    a call gate is always ignored.
  7033.  ───────────────────────────────────────────────────────────────────────────
  7034.  
  7035.  
  7036.  7.5.1.2  Intra-Level Transfers Via Call Gate
  7037.  
  7038.  The transfer is Intra-level if the destination code segment is at the same
  7039.  privilege level as CPL. Either the code segment is non-conforming with
  7040.  DPL = CPL, or it is conforming, with DPL ≤ CPL (see section 11.2 for this
  7041.  case). The 32-bit destination address in the gate is loaded into CS:IP.
  7042.  
  7043.  If the IP value is not within the limit of the code segment, a general
  7044.  protection fault occurs with an error code of 0. If a CALL instruction is
  7045.  used, the return address is saved in the normal manner. The only effect of
  7046.  the call gate is to place a different address into CS:IP than that
  7047.  specified in the destination address of the JMP or CALL instruction. This
  7048.  feature is useful for systems which require that a fixed address be provided
  7049.  to programs, even though the entry address for the routine may change due to
  7050.  different functions, software changes, or segment relocation.
  7051.  
  7052.  
  7053.  7.5.1.3  Inter-Level Control Transfer Via Call Gates
  7054.  
  7055.  If the destination code segment of the call gate is at a different
  7056.  privilege level than the CPL, an inter-level transfer is being requested.
  7057.  However, if the destination code segment DPL > CPL, then a general
  7058.  protection fault occurs with an error code identifying the destination code
  7059.  segment.
  7060.  
  7061.  The gate guarantees that all transitions to a more privileged level will go
  7062.  to a valid entry point rather than possibly into the middle of a procedure
  7063.  (or worse, into the middle of an instruction). See figure 7-11.
  7064.  
  7065.  Calls to more privileged levels may be performed only through call gates. A
  7066.  JMP instruction can never cause a privilege change. Any attempt to use a
  7067.  call gate in this manner will cause a general protection fault with an error
  7068.  code identifying the gate. Returns to more privileged levels are also
  7069.  prohibited. Inter-level transitions due to interrupts use a different gate,
  7070.  as discussed in Chapter 9.
  7071.  
  7072.  The RPL field of the CS selector saved as part of the return address will
  7073.  always identify the caller's CPL. This information is necessary to correctly
  7074.  return to the caller's privilege level during the return instruction. Since
  7075.  the CALL instruction places the CS value on the more privileged stack, and
  7076.  JMP instructions cannot change privilege levels, it is not possible for a
  7077.  program to maliciously place an invalid return address on the caller's
  7078.  stack.
  7079.  
  7080.  
  7081.  Figure 7-11.  Call Gate
  7082.  
  7083.            ╔═════════════╤════════════╤══════════════╗
  7084.            ║ CALL OPCODE │   OFFSET   │   SELECTOR   ║         INSTRUCTION
  7085.            ╚═════════════╧════════════╧══════╤═══════╝
  7086.                                              │
  7087.                                              
  7088.            ═╦══════════════╤══ ═══╤══════════════╦═ 
  7089.              ║     CODE     │        │              ║          DESCRIPTOR
  7090.              ║     SEG.     │        │  CALL GATE   ║          TABLES
  7091.              ║    DESCR.    │        │              ║
  7092.            ═╩════╤═════════╧══ ═══╧════╤═══╤═════╩═ 
  7093.                   │                      │   │
  7094.                   │   └───────────────────┘   │ OFFSET
  7095.                                              
  7096.           ═╦════════════════════════════════════════╦═ 
  7097.             ║                                        ║         TARGET
  7098.             ║                 ENTER                  ║         CODE
  7099.             ║                                        ║         SEGMENT
  7100.           ═╩════════════════════════════════════════╩═ 
  7101.  
  7102.  
  7103.  7.5.1.4  Stack Changes Caused By Call Gates
  7104.  
  7105.  To maintain system integrity, each privilege level has a separate stack.
  7106.  Furthermore, each task normally uses separate stacks from other tasks for
  7107.  each privilege level. These stacks assure sufficient stack space to process
  7108.  calls from less privileged levels. Without them, trusted programs may not
  7109.  work correctly, especially if the calling program does not provide
  7110.  sufficient space on the caller's stack.
  7111.  
  7112.  When a call gate is used to change privilege levels, a new stack is
  7113.  selected as determined by the new CPL. The new stack pointer value is loaded
  7114.  from the Task State Segment (TSS). The privilege level of the new stack data
  7115.  segment must equal the new CPL; if it does not, a task stack fault occurs
  7116.  with the saved machine state pointing at the CALL instruction and the error
  7117.  code identifying the invalid stack selector.
  7118.  
  7119.  The new stack should contain enough space to hold the old SS:SP, the return
  7120.  address, and all parameters and local variables required to process the
  7121.  call. The initial stack pointers for privilege levels 0-2 in the TSS are
  7122.  strictly read only values. They are never changed during the course of
  7123.  execution.
  7124.  
  7125.  The normal technique for passing parameters to a subroutine is to place
  7126.  them onto the stack. To make privilege transitions transparent to the called
  7127.  program, a call gate specifies that parameters are to be copied from the old
  7128.  stack to the new stack. The word count field in a call gate (see figure
  7129.  7-10) specifies how many words (up to 31) are to be copied from the
  7130.  caller's stack to the new stack. If the word count is zero, no parameters
  7131.  are copied.
  7132.  
  7133.  Before copying the parameters, the new stack is checked to assure that it
  7134.  is large enough to hold the parameters; if it is not, a stack fault occurs
  7135.  with an error code of 0. After the parameters are copied, the return link is
  7136.  on the new stack (i.e., a pointer to the old stack is placed in the new
  7137.  stack). In particular, the return address is pointed at by SS:SP. The call
  7138.  and return example of figure 7-12 illustrate the stack contents after a
  7139.  successful inter-level call.
  7140.  
  7141.  The stack pointer of the caller is saved above the caller's return address
  7142.  as the first two words pushed onto the new stack. The caller's stack can
  7143.  only be saved for calls to procedures at privilege levels 2, 1, and 0. Since
  7144.  level 3 cannot be called by any procedure at any other privilege level, the
  7145.  level 3 stack will never contain links to other stacks.
  7146.  
  7147.  Procedures requiring more than the 31 words for parameters that may be
  7148.  called from another privilege level must use the saved SS:SP link to access
  7149.  all parameters beyond the last word copied.
  7150.  
  7151.  The call gate does not check the values of the words copied onto the new
  7152.  stack. The called procedure should check each parameter for validity.
  7153.  Section 11.3 discusses how the ARPL, VERR, VERW, LSL, and LAR instructions
  7154.  can be used to check pointer values.
  7155.  
  7156.  
  7157.  Figure 7-12.  Stack Contents after an Inter-Level Call
  7158.  
  7159.                       ╔══════════╗               ╔══════════╗
  7160.        │               ║          ║               ║  OLD SS  ║    │
  7161.        │               ║          ║               ╟──────────╢    │ DIRECTION
  7162.     HIGHER             ║          ║               ║  OLD SP  ║    │ OF STACK
  7163.    ADDRESSES           ║          ║               ╟──────────╢    │ GROWTH
  7164.                        ║          ║               ║  PARM 3  ║    │
  7165.                        ║          ║               ╟──────────╢    │
  7166.                        ║          ║               ║  PARM 2  ║    │
  7167.                        ╟──────────╢               ╟──────────╢    │
  7168.                        ║  PARM 3  ║               ║  PARM 1  ║    
  7169.                        ╟──────────╢               ╟──────────╢
  7170.                        ║  PARM 2  ║               ║  OLD CS  ║
  7171.                  OLD   ╟──────────╢        NEW    ╟──────────╢
  7172.                 SS:SP  ║  PARM 1  ║      SS + SP  ║  OLD IP  ║
  7173.     LOWER         └───╚══════════╝         └────╚══════════╝
  7174.   ADDRESSES
  7175.       │                 OLD STACK                  NEW STACK
  7176.       │                 (AT "OUTER"                (AT "INNER"
  7177.                        PRIVILEGE                  PRIVILEGE
  7178.                         LEVEL)                     LEVEL)
  7179.  
  7180.  
  7181.  7.5.2  Inter-Level Returns
  7182.  
  7183.  An inter-segment return instruction can also change levels, but only toward
  7184.  programs of equal or lesser privilege (when code segment DPL is numerically
  7185.  greater or equal than the CPL). The RPL of the selector popped off the stack
  7186.  by the return instruction identifies the privilege level to resume
  7187.  execution of the calling program.
  7188.  
  7189.  When the RET instruction encounters a saved CS value whose RPL > CPL, an
  7190.  inter-level return occurs. Checks shown in table 7-4 are made during such a
  7191.  return.
  7192.  
  7193.  The old SS:SP value is then adjusted by the number of bytes indicated in
  7194.  the RET instruction and loaded into SS:SP. The new SP value is not checked
  7195.  for validity. If SP is invalid it is not recognized until the first stack
  7196.  operation. The SS:SP value of the returning program is not saved. (Note:
  7197.  this value normally is the same as that saved in the TSS.)
  7198.  
  7199.  The last step in the return is checking the contents of the DS and ES
  7200.  descriptor register. If DS or ES refer to segments whose DPL is greater than
  7201.  the new CPL (excluding conforming code segments), the segment registers are
  7202.  loaded with the null selector. Any subsequent memory reference that
  7203.  attempts to use the segment register containing the null selector will cause
  7204.  a general protection fault. This prevents less privileged code from
  7205.  accessing more privileged data previously accessed by the more privileged
  7206.  program.
  7207.  
  7208.  
  7209.  Table 7-4. Inter-Level Return Checks
  7210.  
  7211.  Type of Check                           Exception     Error Code
  7212.  SP is not within Segment Limit              SF         0
  7213.  SP + N + 7 is not in Segment Limit          SF         0
  7214.  RPL of Return CS is Greater than CPL        GP         Return CS id
  7215.  Return CS Selector is not null              GP         Return CS id
  7216.  Return CS segment is within Descriptor      GP         Return CS id
  7217.   Table Limit
  7218.  Return CS Descriptor is a Code Segment      GP         Return CS id
  7219.  Return CS Segment is Present                NP         Return CS id
  7220.  DPL of Return Non-Conforming Code           GP         Return CS id
  7221.   Segment = RPL of CS
  7222.  SS Selector at SP + N + 6 is not Null       SF         Return SS id
  7223.  SS Selector at SP + N + 6 is within         SF         Return SS id
  7224.   Descriptor Table Limit
  7225.  SS Descriptor is Writable Data Segment      SF         Return SS id
  7226.  SS Segment is Present                       SF         Return SS id
  7227.  SS Segment DPL = RPL of CS                  SF         Return SS id
  7228.  
  7229.  
  7230.  Chapter 8  Tasks and State Transitions
  7231.  
  7232.  ───────────────────────────────────────────────────────────────────────────
  7233.  
  7234.  8.1  Introduction
  7235.  
  7236.  An 80286 task is a single, sequential thread of execution. Each task can be
  7237.  isolated from all other tasks. There may be many tasks associated with an
  7238.  80286 CPU, but only one task executes at any time. Switching the CPU from
  7239.  executing one task to executing another can occur as the result of either an
  7240.  interrupt or an inter-task CALL, JMP or IRET. A hardware-recognized data
  7241.  structure defines each task.
  7242.  
  7243.  The 80286 provides a high performance task switch operation with complete
  7244.  isolation between tasks. A full task-switch operation takes only 22
  7245.  microseconds at 8 MHz (18 microseconds at 10 MHz). High-performance,
  7246.  interrupt-driven, multi-application systems that need the benefits of
  7247.  protection are feasible with the 80286.
  7248.  
  7249.  A performance advantage and system design advantage arise from the 80286
  7250.  task switch:
  7251.  
  7252.    ■  Faster task switch: A task switch is a single instruction performed by
  7253.       microcode. Such a scheme is 2-3 times faster than an explicit task
  7254.       switch instruction. A fast task switch translates to a significant
  7255.       performance boost for heavily multi-tasked systems over conventional
  7256.       methods.
  7257.  
  7258.    ■  More reliable, flexible systems: The isolation between tasks and the
  7259.       high speed task switch allows interrupts to be handled by separate
  7260.       tasks rather than within the currently interrupted task. This isolation
  7261.       of interrupt handling code from normal programs prevents undesirable
  7262.       interactions between them. The interrupt system can become more
  7263.       flexible since adding an interrupt handler is as safe and easy as
  7264.       adding a new task.
  7265.  
  7266.    ■  Every task is protected from all others via the separation of address
  7267.       spaces described in Chapter 7, including allocation of unique stacks
  7268.       to each active privilege level in each task (unless explicit sharing is
  7269.       planned in advance). If the address spaces of two tasks include no
  7270.       shared data, one task cannot affect the data of another task. Code
  7271.       sharing is always safe since code segments may never be written into.
  7272.  
  7273.  
  7274.  8.2  Task State Segments and Descriptors
  7275.  
  7276.  Tasks are defined by a special control segment called a Task State Segment
  7277.  (TSS). For each task, there must be an unique TSS. The definition of a task
  7278.  includes its address space and execution state. A task is invoked (made
  7279.  active) by inter-segment jump or call instructions whose destination
  7280.  address refers to a task state segment or a task gate.
  7281.  
  7282.  The Task State Segment (TSS) has a special descriptor. The Task Register
  7283.  within the CPU contains a selector to that descriptor. Each TSS selector
  7284.  value is unique, providing an unambiguous "identifier" for each task. Thus,
  7285.  an operating system can use the value of the TSS selector to uniquely
  7286.  identify the task.
  7287.  
  7288.  A TSS contains 22 words that define the contents of all registers and
  7289.  flags, the initial stacks for privilege levels 0-2, the LDT selector, and a
  7290.  link to the TSS of the previously executing task. Figure 8-1 shows the
  7291.  layout of the TSS. The TSS can not be written into like an ordinary data
  7292.  segment.
  7293.  
  7294.  Each TSS consists of two parts, a static portion and a dynamic portion. The
  7295.  static entries are never changed by the 80286, while the dynamic entries are
  7296.  changed by each task switch out of this task. The static portions of this
  7297.  segment are the task LDT selector and the initial SS:SP stack pointer
  7298.  addresses for levels 0-2.
  7299.  
  7300.  The modifiable or dynamic portion of the task state segment consists of all
  7301.  dynamically-variable and programmer-visible processor registers, including
  7302.  flags, segment registers, and the instruction pointer. It also includes the
  7303.  linkage word used to chain nested invocations of different tasks.
  7304.  
  7305.  The link word provides a history of which tasks invoked others. The link
  7306.  word is important for restarting an interrupted task when the interrupt has
  7307.  been serviced. Placing the back link in the TSS protects the identity of the
  7308.  interrupted task from changes by the interrupt task, since the TSS is not
  7309.  writable by the interrupt task. (In most systems only the operating system
  7310.  has sufficient privilege to create or use a writable data segment "alias"
  7311.  descriptor for the TSS.)
  7312.  
  7313.  The stack pointer entries in the TSS for privilege levels 0-2 are static
  7314.  (i.e., never written during a privilege or task switch). They define the
  7315.  stack to use upon entry to that privilege level. These stack entries are
  7316.  initialized by the operating system when the task is created. If a
  7317.  privilege level is never used, no stack need be allocated for it.
  7318.  
  7319.  When entering a more privileged level, the caller's stack pointer is saved
  7320.  on the stack of the new privilege level, not in the TSS. Leaving the
  7321.  privilege level requires popping the caller's return address and stack
  7322.  pointer off the current stack. The stack pointer at that time will be the
  7323.  same as the initial value loaded from the TSS upon entry to the privilege
  7324.  level.
  7325.  
  7326.  There is only one stack active at any time, the one defined by the SS and
  7327.  SP registers. The only other stacks that may be non-empty are those at outer
  7328.  (less privileged) levels that called the current level. Stacks for inner
  7329.  levels must be empty, since outward (to numerically larger privilege
  7330.  levels) calls from inner levels are not allowed.
  7331.  
  7332.  The location of the stack pointer for an outer privilege level will always
  7333.  be found at the start of the stack of the inner privilege level called by
  7334.  that level. That stack may be the initial stack for this privilege level or
  7335.  an outer level. Look at the start of the stack for this privilege level.
  7336.  The TSS contains the starting stack address for levels 0-2. If the RPL of
  7337.  the saved SS selector is the privilege level required, then the stack
  7338.  pointer has been found. Otherwise, go to the beginning of the stack defined
  7339.  by that value and look at the saved SS:SP value there.
  7340.  
  7341.  
  7342.  Figure 8-1.  Task State Segment and TSS Registers
  7343.  
  7344.                                                                
  7345.                               ┌─╟───────────────────────────────╢
  7346.                               │ ║        INTEL RESERVED         ║
  7347.                               │ ╟─┬───┬─┬───────┬───────────────║─┐
  7348.                   TSS         │ ║PDPL│0│ TYPE │  BASE 23-16   ║ │
  7349.                   DESCRIPTOR ─┤ ╟─┴─┴─┴─┴─┴─┴─┴─┴───────────────╢ │
  7350.                              │ ║           BASE 15-0           ║ ├ ┐
  7351.                       │       │ ╟───────────────────────────────╢ │
  7352.                               │ ║          LIMIT 15-0           ║ │ │
  7353.                       │       └─╟───────────────────────────────╢─┘
  7354.             CPU            ┌─ ──║── ── ── ── ── ── ── ── ── ── ─╫ ──┘
  7355.  ╔════════════════════╪══╗      ≈                               ≈
  7356.  ║     TASK REGISTER     ║ │    ║15                            0║ BYTE
  7357.  ║       ╔═══════╗    │  ║    ┌─╟───────────────────────────────╢ OFFSET
  7358.  ║       ║       ╟── ─┘  ║ │  │ ║  TASK LDT SELECTOR            ║ 42
  7359.  ║       ╚═══════╝       ║    │ ╟───────────────────────────────╢   ─┐
  7360.  ║        15    0        ║ │  │ ║  DS SELECTOR                  ║ 40 │
  7361.  ║ ┌── ── ── ── ── ── ─┐ ║    │ ╟───────────────────────────────╢    │
  7362.  ║   PROGRAM INVISIBLE   ║ │  │ ║  SS SELECTOR                  ║ 38 │
  7363.  ║ │      15    0      │ ║    │ ╟───────────────────────────────╢    │
  7364.  ║       ╔═══════╗   ┐   ║ │  │ ║  CS SELECTOR                  ║ 36 │
  7365.  ║ │     ║ LIMIT ╟─┐ │ │ ║    │ ╟───────────────────────────────╢    │
  7366.  ║   ╔═══╩───────╢ │ │─ ╫─┘  │ ║  ES SELECTOR                  ║ 34 │
  7367.  ║ │ ║   BASE    ║ │ │ │ ║    │ ╟───────────────────────────────╢    │
  7368.  ║   ╚╤══════════╝ │ ┘   ║    │ ║  DI                           ║ 32 │
  7369.  ║ │  │         0  │   │ ║    │ ╟───────────────────────────────╢    │
  7370.  ║ └──│── ── ── ── ┼─ ─┘ ║    │ ║  SI                           ║ 30 │
  7371.  ╚════╪════════════╪═════╝    │ ╟───────────────────────────────╢    │
  7372.       │            │          │ ║  BP                           ║ 28 │CURRENT
  7373.       │            │          │ ╟───────────────────────────────╢    ├TASK
  7374.       │            │          │ ║  SP                           ║ 26 │STATE
  7375.       │            │          │ ╟───────────────────────────────╢    │
  7376.       │            │          │ ║  BX                           ║ 24 │
  7377.       │            │          │ ╟───────────────────────────────╢    │
  7378.       │            │     TASK │ ║  DX                           ║ 22 │
  7379.       │            └   STATE ┤ ╟───────────────────────────────╢    │
  7380.       │               SEGMENT │ ║  CX                           ║ 20 │
  7381.       │                       │ ╟───────────────────────────────╢    │
  7382.       │                       │ ║  AX                           ║ 18 │
  7383.       │                       │ ╟───────────────────────────────╢    │
  7384.       │                       │ ║  FLAG WORD                    ║ 16 │
  7385.       │                       │ ╟───────────────────────────────╢    │
  7386.       │                       │ ║  IP (ENTRY POINT)             ║ 14 │
  7387.       │                       │ ╟───────────────────────────────╢   ═╡
  7388.       │                       │ ║  SS FOR CPL 2                 ║ 12 │
  7389.       │                       │ ╟───────────────────────────────╢    │
  7390.       │                       │ ║  SP FOR CPL 2                 ║ 10 │
  7391.       │                       │ ╟───────────────────────────────╢    │INITIAL
  7392.       │                       │ ║  SS FOR CPL 1                 ║ 8  │STACKS
  7393.       │                       │ ╟───────────────────────────────╢    ├FOR
  7394.       │                       │ ║  SP FOR CPL 1                 ║ 6  │CPL
  7395.       │                       │ ╟───────────────────────────────╢    │0,1,2
  7396.       │                       │ ║  SS FOR CPL 0                 ║ 4  │
  7397.       │                       │ ╟───────────────────────────────╢    │
  7398.       │                       │ ║  SP FOR CPL 0                 ║ 2  │
  7399.       │                       │ ╟───────────────────────────────╢   ─┘
  7400.       │                       │ ║  BACK LINK SELECTOR TO TSS    ║ 0 ─────
  7401.       └──────────────────────└─╟───────────────────────────────╢
  7402.                                 ║                               ║
  7403.                                                                
  7404.  
  7405.  
  7406.  8.2.1 Task State Segment Descriptors
  7407.  
  7408.  A special descriptor is used for task state segments. This descriptor must
  7409.  be accessible at all times; therefore, it can appear only in the GDT. The
  7410.  access byte distinguishes TSS descriptors from data or code segment
  7411.  descriptors. When bits 0 through 4 of the access byte are 00001 or 00011,
  7412.  the descriptor is for a TSS.
  7413.  
  7414.  The complete layout of a task state segment descriptor is shown in figure
  7415.  8-2.
  7416.  
  7417.  Like a data segment, the descriptor contains a base address and limit
  7418.  field. The limit must be at least 002BH (43) to contain the minimum amount
  7419.  of information required for a TSS. An invalid task exception will occur if
  7420.  an attempt is made to switch to a task whose TSS descriptor limit is less
  7421.  than 43. The error code will identify the bad TSS.
  7422.  
  7423.  The P-bit (Present) flag indicates whether this descriptor contains
  7424.  currently valid information: 1 means yes, 0 no. A task switch that attempts
  7425.  to reference a not-present TSS causes a not-present exception code
  7426.  identifying the task state segment selector.
  7427.  
  7428.  The descriptor privilege level (DPL) controls use of the TSS by JMP or CALL
  7429.  instructions. By the same reasoning as that for call gates, DPL can prevent
  7430.  a program from calling the TSS and thereby cause a task switch. Section 8.3
  7431.  discusses privilege considerations during a task switch in greater detail.
  7432.  
  7433.  Bit 4 is always 0 since TSS is a control segment descriptor. Control
  7434.  segments cannot be accessed by SS, DS, or ES. Any attempt to load those
  7435.  segment registers with a selector that refers to a control segment causes
  7436.  general protection trap. This rule prevents the program from improperly
  7437.  changing the contents of a control segment.
  7438.  
  7439.  TSS descriptors can have two states: idle and busy. Bit 1 of the access
  7440.  byte distinguishes them. The distinction is necessary since tasks are not
  7441.  re-entrant; a busy TSS may not be invoked.
  7442.  
  7443.  
  7444.  Figure 8-2.  TSS Descriptor
  7445.  
  7446.          7                             0 7                             0
  7447.         ╔═══════════════════════════════╤═══════════════════════════════╗
  7448.       +7║                        INTEL RESERVED                        ║+6
  7449.         ╟───┬───────┬───┬───────────┬───┬───────────────────────────────╢
  7450.       +5║ P │  DPL  │ 0 │ 0   0   B │ 1 │         TSB BASE 23-16        ║+4
  7451.         ╟───┴───┴───┴───┴───┴───┴───┴───┴───────────────────────────────╢
  7452.       +3║                          TSS BASE 15-0                        ║+2
  7453.         ║───────────────────────────────────────────────────────────────║
  7454.       +1║                            TSS LIMIT                          ║ 0
  7455.         ╚═══════════════════════════════════════════════════════════════╝
  7456.          15                                                            0
  7457.  
  7458.          B=1 MEANS TASK IS BUSY AND NOT AVAILABLE
  7459.  
  7460.  
  7461.  8.3  Task Switching
  7462.  
  7463.  A task switch may occur in one of four ways:
  7464.  
  7465.    1.  The destination selector of a long JMP or CALL instruction refers to
  7466.        a TSS descriptor. The offset portion of the destination address is
  7467.        ignored.
  7468.  
  7469.    2.  An IRET instruction is executed when the NT bit in the flag word = 1.
  7470.        The new task TSS selector is in the back link field of the current
  7471.        TSS.
  7472.  
  7473.    3.  The destination selector of a long JMP or CALL instruction refers to
  7474.        a task gate. The offset portion of the destination address is ignored.
  7475.        The new task TSS selector is in the gate. (See section 8.5 for more
  7476.        information on task gates.)
  7477.  
  7478.    4.  An interrupt occurs. This interrupt's vector refers to a task gate in
  7479.        the interrupt descriptor table. The new task TSS selector is in the
  7480.        gate. See section 9.4 for more information on interrupt tasks.
  7481.  
  7482.  No new instructions are required for a task switch operation. The standard
  7483.  8086 JMP, CALL, IRET, or interrupt operations perform this function. The
  7484.  distinction between the standard instruction and a task switch is made
  7485.  either by the type of descriptor referenced (for CALL, JMP, or INT) or by
  7486.  the NT bit (for IRET) in flag word.
  7487.  
  7488.  Using the CALL or INT instruction to switch tasks implies a return is
  7489.  expected from the called task. The JMP and IRET instructions imply no return
  7490.  is expected from the new task.
  7491.  
  7492.  When NT=1, the IRET instruction causes a return to the task that called the
  7493.  current one via CALL or INT instruction.
  7494.  
  7495.  Access to TSS and task gate descriptors is restricted by the rules of
  7496.  privilege level. The data access rules are used, thereby allowing task
  7497.  switches to be restricted to programs of sufficient privilege. Address space
  7498.  separation does not apply to TSS descriptors since they must be in the GDT.
  7499.  The access rules for interrupts are discussed in section 9.4.
  7500.  
  7501.  The task switch operation consists of the following eight steps:
  7502.  
  7503.    1.  Validate the requested task switch. For a task switch requested via a
  7504.        JMP, CALL, or an INT instruction, check that the current task is
  7505.        allowed to switch to the requested task.  The DPL of the gate or the
  7506.        TSS descriptor for the requested task must be greater than or equal
  7507.        to both the CPL and the RPL of the requesting task.  If it is not, the
  7508.        General Protection fault (#13) will occur with an error code
  7509.        identifying the descriptor (i.e., the gate selector if the task
  7510.        switch is requested via a task gate, or the selector for the TSS if
  7511.        the task switch is requested via a TSS descriptor).
  7512.  
  7513.        These checks are not performed if a task switch occurs due to an IRET
  7514.        instruction.
  7515.  
  7516.    2.  Check that the new TSS is present and that the new task is available
  7517.        (i.e. not Busy).  A Not Present exception (#11) is signaled if the new
  7518.        TSS descriptor is marked 'Not Present' (P = 0). The General Protection
  7519.        exception (#13) is raised if the new TSS is marked 'Busy'.
  7520.  
  7521.        The task switch operation actually begins now and a detailed
  7522.        verification of the new TSS is carried out.  Conditions which may
  7523.        disqualify the new TSS are listed in table 8-1 along with the
  7524.        exception raised and the error code pushed on the stack for each case.
  7525.        These tests are performed at different points during the course of the
  7526.        following remaining steps of the task switch operation.
  7527.  
  7528.    3.  Mark the new task to be BUSY by setting the 'BUSY' bit in the new TSS
  7529.        descriptor to 1.
  7530.  
  7531.    4.  Save the dynamic portion of the old TSS and load TR with the
  7532.        selector, base and limit for the new TSS.  Set all CPU registers to
  7533.        corresponding values from the new TSS except DS, ES, CS, SS, and LDT.
  7534.  
  7535.    5.  If nesting tasks, set the Nested Task (NT) flag in the new TSS to 1.
  7536.        Also set the Task Switched flag (TS) of the CPU flag register to 1.
  7537.  
  7538.    6.  Validate the LDT selector and the LDT descriptor of the new TSS. Load
  7539.        the LDT cache (LDTR) with the LDT descriptor.
  7540.  
  7541.    7.  Validate the SS, CS, DS, and ES fields of the new TSS and load these
  7542.        values in their respective caches (i.e., SS, CS, DS, and ES
  7543.        registers).
  7544.  
  7545.    8.  Validate the IP field of the new TSS and then start executing the new
  7546.        task from CS:IP.
  7547.  
  7548.  A more detailed explanation of steps 3-5 is given in Appendix B (80286
  7549.  Instruction Set) under a pseudo procedure 'SWITCH_TASKS'. Notice how the
  7550.  exceptions described in table 8-1 may actually occur during a task switch.
  7551.  Similarly the exceptions that may occur during steps 1-2, and step 8 are
  7552.  explained in greater detail in the pseudo code description of the 286
  7553.  instructions CALL, JMP, INT, and IRET in Appendix B. This information can
  7554.  be very helpful when debugging any protected mode code.
  7555.  
  7556.  Note that the state of the outgoing task is always saved. If execution of
  7557.  that task is resumed, it will start after the instruction that caused the
  7558.  task switch. The values of the registers will be the same as that when the
  7559.  task stopped running.
  7560.  
  7561.  Any task switch sets the Task Switched (TS) bit in the Machine Status Word
  7562.  (MSW). This flag is used when processor extensions such as the 80287 Numeric
  7563.  Processor Extension are present. The TS bit signals that the context of the
  7564.  processor extension may not belong to the current 80286 task. Chapter 11
  7565.  discusses the TS bit and processor extensions in more detail.
  7566.  
  7567.  Validity tests on a selector ensure that the selector is in the proper
  7568.  table (i.e., the LDT selector refers to GDT), lies within the bounds of the
  7569.  table, and refers to the proper type of descriptor (i.e., the LDT selector
  7570.  refers to the LDT descriptor).
  7571.  
  7572.  Note that between steps 3 and 4 in table 8-1, all the registers of the new
  7573.  task are loaded. Several protection rule violations may exist in the new
  7574.  segment register contents. If an exception occurs in the context of the new
  7575.  task due to checks performed on the newly loaded descriptors, the DS and ES
  7576.  segments may not be accessible even though the segment registers contain
  7577.  non-zero values. These selector values must be saved for later reuse. When
  7578.  the exception handler reloads these segment registers, another protection
  7579.  exception may occur unless the exception handler pre-examines them and
  7580.  fixes any potential problems.
  7581.  
  7582.  A task switch allows flexibility in the privilege level of the outgoing and
  7583.  incoming tasks. The privilege level at which execution resumes in the
  7584.  incoming task is not restricted by the privilege level of the outgoing task.
  7585.  This is reasonable, since both tasks are isolated from each other with
  7586.  separate address spaces and machine states. The privilege rules prevent
  7587.  improper access to a TSS. The only interaction between the tasks is to the
  7588.  extent that one started the other and the incoming task may restart the
  7589.  outgoing task by executing an IRET instruction.
  7590.  
  7591.  
  7592.  Table 8-1. Checks Made during a Task Switch
  7593.  
  7594. ╓┌────┌────────────────────────────┌────────────────┌────────────────────────╖
  7595.       Test                         Exception       Error Code
  7596.  1    Incoming TSS descriptor           NP          Incoming TSS selector
  7597.       is present
  7598.  2    Incoming TSS is idle              G           Incoming TSS selector
  7599.  3    Limit of incoming TSS        Invalid TSS      Incoming TSS selector
  7600.       greater than 43
  7601.  4    LDT selector of incoming     Invalid TSS      LDT selector
  7602.       TSS is valid
  7603.  5    LDT of incoming TSS          Invalid TSS      LDT selector
  7604.       Test                         Exception       Error Code
  7605. 5    LDT of incoming TSS          Invalid TSS      LDT selector
  7606.       is present
  7607.  6    CS selector is valid         Invalid TSS      Code segment selector
  7608.  7    Code segment is present           NP          Code segment selector
  7609.  8    Code segment DPL matches     Invalid TSS      Code segment selector
  7610.       CS RPL
  7611.  9    Stack segment is valid            SF          Stack segment selector
  7612.  10   Stack segment is writable         GP          Stack segment selector
  7613.       data segment
  7614.  11   Stack segment is present          SF          Stack segment selector
  7615.  12   Stack segment DPL = CPL           SF          Stack segment selector
  7616.  13   DS/ES selectors are valid         GP          Segment selector
  7617.  14   DS/ES segments are readable       GP          Segment selector
  7618.  15   DS/ES segments are present        NP          Segment selector
  7619.  16   DS/ES segment DPL ≥ CPL if        GP          Segment
  7620.       not conform
  7621.  
  7622.  
  7623.  8.4  Task Linking
  7624.  
  7625.  The TSS has a field called "back link" which contains the selector of the
  7626.  TSS of a task that should be restarted when the current task completes. The
  7627.  back link field of an interrupt-initiated task is automatically written with
  7628.  the TSS selector of the interrupted task.
  7629.  
  7630.  A task switch initiated by a CALL instruction also points the back link at
  7631.  the outgoing task's TSS. Such task nesting is indicated to programs via the
  7632.  Nested Task (NT) bit in the flag word of the incoming task.
  7633.  
  7634.  Task nesting is necessary for interrupt functions to be processed as
  7635.  separate tasks. The interrupt function is thereby isolated from all other
  7636.  tasks in the system. To restart the interrupted task, the interrupt handler
  7637.  executes an IRET instruction much in the same manner as an 8086 interrupt
  7638.  handler. The IRET instruction will then cause a task switch to the
  7639.  interrupted task.
  7640.  
  7641.  Completion of a task occurs when the IRET instruction is executed with the
  7642.  NT bit in the flag word set. The NT bit is automatically set/reset by task
  7643.  switch operations as appropriate. Executing an IRET instruction with NT
  7644.  cleared causes the normal 8086 interrupt return function to be performed,
  7645.  and no task switch occurs.
  7646.  
  7647.  Executing IRET with NT set causes a task switch to the task defined by the
  7648.  back link field of the current TSS. The selector value is fetched and
  7649.  verified as pointing to a valid, accessible TSS. The normal task switch
  7650.  operation described in section 8.3 then occurs. After the task switch is
  7651.  complete, the outgoing task is now idle and considered ready to process
  7652.  another interrupt.
  7653.  
  7654.  Table 8-2 shows how the busy bit, NT bit, and link word of the incoming and
  7655.  outgoing task are affected by task switch operations caused by JMP, CALL, or
  7656.  IRET instructions.
  7657.  
  7658.  Violation of any of the busy bit requirements shown in table 8-2 causes a
  7659.  general protection fault with the saved machine state appearing as if the
  7660.  instruction had not executed. The error code identifies the selector of the
  7661.  TSS with the busy bit.
  7662.  
  7663.  A bus lock is applied during the testing and setting of the TSS descriptor
  7664.  busy bit to ensure that two processors do not invoke the same task at the
  7665.  same time. See also section 11.4 for other multi-processor considerations.
  7666.  
  7667.  The linking order of tasks may need to be changed to restart an interrupted
  7668.  task before the task that interrupted it completes. To remove a task from
  7669.  the list, trusted operating system software must change the backlink field
  7670.  in the TSS of the interrupting task first, then clear the busy bit in the
  7671.  TSS descriptor of the task removed from the list.
  7672.  
  7673.  When trusted software deletes the link from one task to another, it should
  7674.  place a value in the backlink field, which will pass control to that trusted
  7675.  software when the task attempts to resume execution of another task via
  7676.  IRET.
  7677.  
  7678.  
  7679.  Table 8-2. Effect of a Task Switch on BUSY and NT Bits and the Link Word
  7680.  
  7681. ╓┌───────────────────────────┌──────────────┌─────────────────┌──────────────╖
  7682.                              JMP            CALL/INT          IRET
  7683.  Affected Field              Instruction    Instruction       Instruction
  7684.                              Effect         Effect            Effect
  7685.  
  7686.  Busy bit of incoming        Set, must be   Set, must be 0    Unchanged,
  7687.   task TSS descriptor        0 before       before            must be set
  7688.                              JMP            CALL/INT          IRET
  7689.  Affected Field              Instruction    Instruction       Instruction
  7690.                              Effect         Effect            Effect
  7691.  task TSS descriptor        0 before       before            must be set
  7692.  
  7693.  Busy bit of outgoing        Cleared        Unchanged (will   Cleared
  7694.   tasl TSS descriptor                       already be 1)
  7695.  
  7696.  NT bit in incoming task     Cleared        Set               Unchanged
  7697.   flag word
  7698.  
  7699.  NT bit in outgoing task     Unchanged      Unchanged         Cleared
  7700.   flag word
  7701.  
  7702.  Back link in incoming       Unchanged      Set to outgoing   Unchanged
  7703.   task TSS                                  task TSS selector
  7704.  
  7705.  Back link of outgoing       Unchanged      Unchanged         Unchanged
  7706.   tasl TSS
  7707.  
  7708.  
  7709.  
  7710.  8.5  Task Gates
  7711.  
  7712.  A task may be invoked by several different events. Task gates are provided
  7713.  to support this need. Task gates are used in the same way as call and
  7714.  interrupt gates. The ultimate effect of jumping to or calling a task gate is
  7715.  the same as jumping to or calling directly to the TSS in the task gate.
  7716.  
  7717.  Figure 8-3 depicts the layout of a task gate.
  7718.  
  7719.  A task gate is identified by the access byte field in bits 0 through 4
  7720.  being 00101. The gate provides an extra level of indirection between the
  7721.  destination address and the TSS selector value. The offset portion of the
  7722.  JMP or CALL destination address is ignored.
  7723.  
  7724.  Gate use provides flexibility in controlling access to tasks. Task gates
  7725.  can appear in the GDT, IDT, or LDT. The TSS descriptors for all tasks must
  7726.  be kept in the GDT. They are normally placed at level 0 to prevent any task
  7727.  from improperly invoking another task. Task gates placed in the LDT allow
  7728.  private access to selected tasks with full privilege control.
  7729.  
  7730.  The data segment access rules apply to accessing a task gate via JMP, CALL,
  7731.  or INT instructions. The effective privilege level (EPL) of the destination
  7732.  selector must be numerically less than or equal to the DPL of the task gate
  7733.  descriptor. Any violation of this requirement causes a general protection
  7734.  fault with an error code identifying the task gate involved.
  7735.  
  7736.  Once access to the task gate has been verified, the TSS selector from the
  7737.  gate is read. The RPL of the TSS selector is ignored. From this point, all
  7738.  the checks and actions performed for a JMP or CALL to a TSS after access has
  7739.  been verified are performed (see section 8.4). Figure 8-4 illustrates an
  7740.  example of a task switch through a task gate.
  7741.  
  7742.  
  7743.  Figure 8-3.  Task Gate Descriptor
  7744.  
  7745.          7                             0 7                             0
  7746.         ╔═══════════════════════════════════════════════════════════════╗
  7747.       +7║                        INTEL RESERVED                        ║+6
  7748.         ╟───┬───────┬───┬───────────┬───┬───────────────────────────────╢
  7749.       +5║ P │  DPL  │ O │ O   1   O │ 1 │            UNUSED             ║+4
  7750.         ╟───┴───────┴───┴───┴───┴───┴───┴───────────────────────────────╢
  7751.       +3║                         TSS SELECTOR                          ║+2
  7752.         ║───────────────────────────────────────────────────────────────║
  7753.       +1║                            UNUSED                             ║ 0
  7754.         ╚═══════════════════════════════════════════════════════════════╝
  7755.          15                                                            0
  7756.  
  7757.  
  7758.  Figure 8-4.  Task Switch Through a Task Gate
  7759.  
  7760.                          TASK A                                   TASK B
  7761.                     ╔══════════════╗     ╔══════════════╗    ╔══════════════╗
  7762.                     ║              ║     ║              ║    ║              ║
  7763.                     ║              ║     ║              ║    ≈              ≈
  7764.                     ║              ║     ╟──────────────╢    ║              ║
  7765.                     ║              ║TASK┌║LDT DESCRIPTOR╟───╚══════════════╝
  7766.                     ║              ║   B┤╟──────────────╢─┐       LDT
  7767.  ╔═══════╤════════╗ ╟──────────────╢    └║TSS DESCRIPTOR╟─┐│
  7768.  ║       │SELECTOR║║  TASK GATE   ║────╟──────────────╢ ││ ╔══════════════╗
  7769.  ╚═══════╧════════╝ ╟──────────────╢     ║              ║ │└─╢ LDT SELECTOR ║
  7770.                     ≈              ≈     ║              ║ │  ╟──────────────╢
  7771.                     ║              ║     ║              ║ │  ║              ║
  7772.                     ╚══════════════╝─┐  ║              ║ │  ║              ║
  7773.                            LDT        │  ║              ║ │  ║              ║
  7774.                                       └──╫──────────────╢ │  ║              ║
  7775.                     ╔══════════════╗────║LDT DESCRIPTOR║┐│  ║              ║
  7776.                     ║ LDT SELECTOR ║     ╟──────────────╢│└─╟──────────────╢
  7777.                     ╟──────────────╢     ║TSS DESCRIPTOR║┴┐  ║  BACK LINK   ║
  7778.                     ║              ║  ┌──╫──────────────╢───╚══════════════╝
  7779.                     ║              ║  │  ║              ║ │        TSS
  7780.                     ║              ║  │  ║              ║ └TASK
  7781.                     ╚══════════════╝─┘  ╚══════════════╝     A
  7782.                            TSS                 GDT
  7783.  
  7784.  
  7785.  Chapter 9  Interrupts and Exceptions
  7786.  
  7787.  ───────────────────────────────────────────────────────────────────────────
  7788.  
  7789.  Interrupts and exceptions are special cases of control transfer within a
  7790.  program. An interrupt occurs as a result of an event that is independent of
  7791.  the currently executing program, while exceptions are a direct result of the
  7792.  program currently being executed. Interrupts may be external or internal.
  7793.  External interrupts are generated by either the INTR or NMI input pins.
  7794.  Internal interrupts are caused by the INT instruction. Exceptions occur when
  7795.  an instruction cannot be completed normally. Although their causes differ,
  7796.  interrupts and exceptions use the same control transfer techniques and
  7797.  privilege rules; therefore, in the following discussions the term interrupt
  7798.  will also apply to exceptions.
  7799.  
  7800.  The program used to service an interrupt may execute in the context of the
  7801.  task that caused the interrupt (i.e., used the same TSS, LDT, stacks, etc.)
  7802.  or may be a separate task. The choice depends on the function to be
  7803.  performed and the level of isolation required.
  7804.  
  7805.  
  7806.  9.1  Interrupt Descriptor Table
  7807.  
  7808.  Many different events may cause an interrupt. To allow the reason for an
  7809.  interrupt to be easily identified, each interrupt source is given a number
  7810.  called the interrupt vector. Up to 256 different interrupt vectors (numbers)
  7811.  are possible. See figure 9-1.
  7812.  
  7813.  A table is used to define the handler for each interrupt vector. The
  7814.  Interrupt Descriptor Table (IDT) defines the interrupt handlers for up to
  7815.  256 different interrupts. The IDT is in physical memory, pointed to by the
  7816.  contents of the on-chip IDT register that contains a 24-bit base and a
  7817.  16-bit limit. The IDTR is normally loaded with the LIDT instruction by code
  7818.  that executes at privilege level 0 during system initialization. The IDT may
  7819.  be located anywhere in the physical address space of the 80286.
  7820.  
  7821.  Each IDT entry is a 4-word gate descriptor that contains a pointer to the
  7822.  handler. The three types of gates permitted in the IDT are interrupt gates,
  7823.  trap gates (discussed in section 9.3), and task gates (discussed in section
  7824.  9.5). Interrupt and task gates process interrupts in the same task, while
  7825.  task gates cause a task switch. Any other descriptor type in the IDT will
  7826.  cause an exception if it is referenced by an interrupt.
  7827.  
  7828.  The IDT need not contain all 256 entries. A 16-bit limit register allows
  7829.  less than the full number of entries. Unused entries may be signaled by
  7830.  placing a zero in the access rights byte. If an attempt is made to access an
  7831.  entry outside the table limit, or if the wrong descriptor type is found, a
  7832.  general protection fault occurs with an error code pushed on the stack
  7833.  identifying the invalid interrupt vector (see figure 9-2).
  7834.  
  7835.  Exception error codes that refer to an IDT entry can be identified by bit 1
  7836.  of the error code that will be set. Bit 0 of the error code is 1 if the
  7837.  interrupt was caused by an event external to the program (i.e., an external
  7838.  interrupt, a single step, a processor extension error, or a processor
  7839.  extension not present).
  7840.  
  7841.  Interrupts 0-31 are reserved for use by Intel. Some of the interrupts are
  7842.  used for instruction exceptions. The IDT limit must be at least 255
  7843.  (32 * 8 - 1) to accommodate the minimum number of interrupts. The remaining
  7844.  224 interrupts are available to the user.
  7845.  
  7846.  
  7847.  Figure 9-1.  Interrupt Descriptor Table Definition
  7848.  
  7849.                                            MEMORY
  7850.                                                      
  7851.                                       ║               ║         THE IDT MAY
  7852.                                     ┌─╠═══════════════╣─┐       CONTAIN
  7853.                                     │ ║   GATE FOR    ║ │       INTERRUPT
  7854.                                     │ ║ INTERRUPT #n  ║ │       GATES, TRAPS
  7855.                                     │ ╠═══════════════╣ │       OR TASK GATES
  7856.                                     │ ║   GATE FOR    ║ │       ONLY.
  7857.                                     │ ║INTERRUPT #n-1 ║ │
  7858.                                     │ ╠═══════════════╣ │
  7859.                                     │ ║       ∙       ║ │  INTERRUPT
  7860.                                 ┌──┤ ║       ∙       ║ ├─ DESCRIPTOR
  7861.                                 │   │ ║       ∙       ║ │  TABLE (IDT)
  7862.                    CPU          │   │ ╠═══════════════╣ │
  7863.          ┌───────────────────┐  │   │ ║   GATE FOR    ║ │
  7864.          │      15        0  │  │   │ ║ INTERRUPT #1  ║ │
  7865.          │      ╔═════════╗  │  │   │ ╠═══════════════╣ │
  7866.          │      ║IDT LIMIT╟──┼──┘   │ ║   GATE FOR    ║ │
  7867.     IDTR │  ╔═══╨─────────╢  │      └─║ INTERRUPT #0  ║ │
  7868.          │  ║   IDT BASE  ╟──┼───────╠═══════════════╣─┘
  7869.          │  ╚═════════════╝  │        ║               ║
  7870.          │  23            0  │                       
  7871.          └───────────────────┘
  7872.  
  7873.  
  7874.  Figure 9-2.  IDT Selector Error Code
  7875.  
  7876.          15  14  13  12  11  10  9   8   7   6   5   4   3   2   1   0
  7877.        ╔═══════════════════╤═══════════════════════════════╤═══╤═══╤═══╗
  7878.        ║                   │                               │   │   │ E ║
  7879.        ║ 0   0   0   0   0 │          IDT VECTOR           │ 0 │ 1 │ X ║
  7880.        ║                   │                               │   │   │ T ║
  7881.        ╚═══════════════════╧═══════════════════════════════╧═══╧═══╧═╤═╝
  7882.                                    ┌─────────────────────────────────┘
  7883.                                    
  7884.                                    1 An even external to the program
  7885.                                      caused the exception (i.e. external
  7886.                                      interrupt, single step, processor
  7887.                                      extension error)
  7888.  
  7889.                                    0 An exception occurred while
  7890.                                      processing an instruction at CS:IP
  7891.                                      saved on stack
  7892.  
  7893.  
  7894.  9.2  Hardware Initiated Interrupts
  7895.  
  7896.  Hardware-initiated interrupts are caused by some external event that
  7897.  activates either the INTR or NMI input pins of the processor. Events that
  7898.  use the INTR input are classified as maskable interrupts. Events that use
  7899.  the NMI input are classified as non-maskable interrupts.
  7900.  
  7901.  All 224 user-defined interrupt sources share the INTR input, but each has
  7902.  the ability to use a separate interrupt handler. An 8-bit vector supplied by
  7903.  the interrupt controller identifies which interrupt is being signaled. To
  7904.  read the interrupt id, the processor performs the interrupt acknowledge bus
  7905.  sequence.
  7906.  
  7907.  Maskable interrupts (from the INTR input) can be inhibited by software by
  7908.  setting the interrupt flag bit (IF) to 0 in the flag word. The IF bit does
  7909.  not inhibit exceptions or interrupts caused by the INT instruction. The IF
  7910.  bit also does not inhibit processor extension interrupts.
  7911.  
  7912.  The type of gate placed into the IDT for the interrupt vector will control
  7913.  whether other maskable interrupts remain enabled or not during the servicing
  7914.  of that interrupt. The flag word that was saved on the stack reflects the
  7915.  maskable interrupt enable status of the processor prior to the interrupt.
  7916.  The procedure servicing a maskable interrupt can also prevent further
  7917.  maskable interrupts during its work by resetting the IF flag.
  7918.  
  7919.  Non-maskable interrupts are caused by the NMI input. They have a higher
  7920.  priority than the maskable interrupts (meaning that in case of simultaneous
  7921.  requests, the non-maskable interrupt will be serviced first). A non-maskable
  7922.  interrupt has a fixed vector (#2) and therefore does not require an
  7923.  interrupt acknowledge sequence on the bus. A typical use of an NMI is to
  7924.  invoke a procedure to handle a power failure or some other critical hardware
  7925.  exception.
  7926.  
  7927.  A procedure servicing an NMI will not be further interrupted by other
  7928.  non-maskable interrupt requests until an IRET instruction is executed. A
  7929.  further NMI request is remembered by the hardware and will be serviced after
  7930.  the first IRET instruction. Only one NMI request can be remembered. To
  7931.  prevent a maskable interrupt from interrupting the NMI interrupt handler,
  7932.  the IF flag should be cleared either by using an interrupt gate in the IDT
  7933.  or by setting IF = 0 in the flag word of the task involved.
  7934.  
  7935.  
  7936.  9.3  Software Initiated Interrupts
  7937.  
  7938.  Software initiated interrupts occur explicitly as interrupt instructions or
  7939.  may arise as the result of an exceptional condition that prevents the
  7940.  continuation of program execution. Software interrupts are not maskable. Two
  7941.  interrupt instructions exist which explicitly cause an interrupt: INT n and
  7942.  INT 3. The first allows specification of any interrupt vector; the second
  7943.  implies interrupt vector 3 (Breakpoint).
  7944.  
  7945.  Other instructions like INTO, BOUND, DIV, and IDIV may cause an interrupt,
  7946.  depending on the overflow flag or values of the operands. These instructions
  7947.  have predefined vectors associated with them in the first 32 interrupts
  7948.  reserved by Intel.
  7949.  
  7950.  A whole class of interrupts called exceptions are intended to detect faults
  7951.  or programming errors (in the use of operands or privilege levels).
  7952.  Exceptions cannot be masked. They also have fixed vectors within the first
  7953.  32 interrupts. Many of these exceptions pass an error code on the stack,
  7954.  which is not the case with the other interrupt types discussed in section
  7955.  9.2. Section 9.5 discusses these error codes as well as the priority among
  7956.  interrupts that can occur simultaneously.
  7957.  
  7958.  
  7959.  9.4  Interrupt Gates and Trap Gates
  7960.  
  7961.  Interrupt gates and trap gates are special types of descriptors that may
  7962.  only appear in the interrupt descriptor table. The difference between a trap
  7963.  and an interrupt gate is whether the interrupt enable flag is to be cleared
  7964.  or not. An interrupt gate specifies a procedure that enters with interrupts
  7965.  disabled (i.e., with the interrupt enable flag cleared); entry via a trap
  7966.  gate leaves the interrupt enable status unchanged. The NT flag is always
  7967.  cleared (after the old NT state is saved on the stack) when an interrupt
  7968.  uses these gates. Interrupts that have either gate in the associated IDT
  7969.  entry will be processed in the current task.
  7970.  
  7971.  Interrupts and trap gates have the same structure as the call gates
  7972.  discussed in section 7.5.1. The selector and entry point for a code segment
  7973.  to handle the interrupt or exception is contained in the gate. See figure
  7974.  9-3.
  7975.  
  7976.  The access byte contains the Present bit, the descriptor privilege level,
  7977.  and the type identifier. Bits 0-4 of the access byte have a value of 00110
  7978.  for interrupt gates, 00111 for trap gates. Byte 5 of the descriptor is not
  7979.  used by either of these gates; it is used only by the call gate, which uses
  7980.  it as the parameter word-count.
  7981.  
  7982.  Trap and interrupt gates allow a privilege level transition to occur when
  7983.  passing control to a non-conforming code segment. Like a call gate, the DPL
  7984.  of the target code segment selected determines the new CPL. The DPL of the
  7985.  new non-conforming code segment must be numerically less than or equal to
  7986.  CPL.
  7987.  
  7988.  No privilege transition occurs if the new code segment is conforming. If
  7989.  the DPL of the conforming code segment is greater than the CPL, a general
  7990.  protection exception will occur.
  7991.  
  7992.  As with all descriptors, these gates in the IDT carry a privilege level.
  7993.  The DPL controls access to interrupts with the INT n and INT 3 instructions.
  7994.  For access, the CPL of the program must be less than or equal to the gate
  7995.  DPL. If the CPL is not, a general protection exception will result with an
  7996.  error code identifying the selected IDT gate. For exceptions and external
  7997.  interrupts, the CPL of the program is ignored while accessing the IDT.
  7998.  
  7999.  Interrupts using a trap or an interrupt gate are handled in the same manner
  8000.  as an 8086 interrupt. The flags and return address of the interrupted
  8001.  program are saved on the stack of the interrupt handler. To return to the
  8002.  interrupted program, the interrupt handler executes an IRET instruction.
  8003.  
  8004.  If an increase in privilege is required for handling the interrupt, a new
  8005.  stack will be loaded from the TSS. The stack pointer of the old privilege
  8006.  level will also be saved on the new stack in the same manner as a call gate.
  8007.  Figure 9-4 shows the stack contents after an exception with an error code
  8008.  (with and without a privilege level change).
  8009.  
  8010.  If an interrupt or trap gate is used to handle an exception that passes an
  8011.  error code, the error code will be pushed onto the new stack after the
  8012.  return address (as shown in figure 9-4). If a task gate is used, the error
  8013.  code is pushed onto the stack of the new task. The return address is saved
  8014.  in the old TSS.
  8015.  
  8016.  If an interrupt gate is used to handle an interrupt, it is assumed that the
  8017.  selected code segment has sufficient privilege to re-enable interrupts. The
  8018.  IRET instruction will not re-enable interrupts if CPL is numerically greater
  8019.  than IOPL.
  8020.  
  8021.  Table 9-1 shows the checks performed during an interrupt operation that
  8022.  uses an interrupt or trap gate. EXT equals 1 when an event external to the
  8023.  program is involved; 0 otherwise. External events are maskable or
  8024.  non-maskable interrupts, single step interrupt, processor extension segment
  8025.  overrun interrupt, numeric processor not-present exception or numeric
  8026.  processor error. The EXT bit signals that the interrupt or exception is not
  8027.  related to the instruction at CS:IP. Each error code has bit 1 set to
  8028.  indicate an IDT entry is involved.
  8029.  
  8030.  When the interrupt has been serviced, the service routine returns control
  8031.  via an IRET instruction to the routine that was interrupted. If an error
  8032.  code was passed, the exception handler must remove the error code from the
  8033.  stack before executing IRET.
  8034.  
  8035.  The NT flag is cleared when an interrupt occurs which uses an interrupt or
  8036.  trap gate. Executing IRET with NT=0 causes the normal interrupt return
  8037.  function. Executing IRET with NT=1 causes a task switch (see section 8.4
  8038.  for more details).
  8039.  
  8040.  Like the RET instruction, IRET is restricted to return to a level of equal
  8041.  or lesser privilege unless a task switch occurs. The IRET instruction works
  8042.  like the inter-segment RET instruction except that the flag word is popped
  8043.  and no stack pointer update for parameters is performed since no parameters
  8044.  are on the stack. See section 7.5.2 for information on inter-level returns.
  8045.  
  8046.  To distinguish an inter-level IRET, the new CPL (which is the RPL of the
  8047.  return address CS selector) is compared with the current CPL. If they are
  8048.  the same, the IP and flags are popped and execution continues.
  8049.  
  8050.  An inter-level return via IRET has all the same checks as shown in table
  8051.  7-4. The only difference is the extra word on the stack for the old flag
  8052.  word.
  8053.  
  8054.  Interrupt gates are typically associated with high-priority hardware
  8055.  interrupts for automatically disabling interrupts upon their invocation.
  8056.  Trap gates are typically software-invoked since they do not disable the
  8057.  maskable hardware interrupts. However, low-priority interrupts (e.g., a
  8058.  timer) are often invoked via a trap gate to allow other devices of higher
  8059.  priority to interrupt the handler of that lower priority interrupt.
  8060.  
  8061.  Table 9-2 illustrates how the interrupt enable flag and interrupt type
  8062.  interact with the type of gate used.
  8063.  
  8064.  Table 9-1. Trap and Interrupt Gate Checks
  8065.  
  8066. ╓┌─────────────────────────────────┌──────────────┌──────────────────────────
  8067.  Check                             Exception     Error Code
  8068.  
  8069.  Interrupt vector                      GP         IDT entry * 8 + 2 + EXT
  8070.  is in IDT limit
  8071.  
  8072.  Trap, Interrupt, or                   GP         IDT entry * 8 + 2 + EXT
  8073.  Task Gate in IDT Entry
  8074.  
  8075.  If INT instruction,                   GP         IDT entry * 8 + 2 + EXT
  8076.  gate DPL ≥ CPL
  8077.  
  8078.  P bit of gate is set                  NP         IDT entry * 8 + 2 + EXT
  8079.  
  8080.  Code segment selector                 GP         CS selector * 8 + EXT
  8081.  is in descriptor table limit
  8082.  
  8083.  CS selector refers                    GP         CS selector * 8 + EXT
  8084.  to a code segment
  8085.  
  8086.  If code segment is                    GP         CS selector * 8 + EXT
  8087.  Check                             Exception     Error Code
  8088. If code segment is                    GP         CS selector * 8 + EXT
  8089.  non-conforming, Code
  8090.  Segment DPL ≤ CPL
  8091.  
  8092.  If code segment is                    TS         SS selector * 8 + EXT
  8093.  non-conforming, and
  8094.  DPL < CPL and if
  8095.  SS selector in TSS
  8096.  is in descriptor
  8097.  table limit
  8098.  
  8099.  If code segment is                    TS         SS selector * 8 + EXT
  8100.  non-conforming, and
  8101.  DPL < CPL and if
  8102.  SS is a writable
  8103.  data segment
  8104.  
  8105.  If code segment is                    TS         Stack segment selector + EXT
  8106.  non-conforming, and
  8107.  DPL < CPL and
  8108.  Check                             Exception     Error Code
  8109. DPL < CPL and
  8110.  code segment
  8111.  DPL = stack segment DPL
  8112.  
  8113.  If code segment is                    SF         Stack segment selector + EXT
  8114.  non-conforming, and
  8115.  DPL < CPL and if
  8116.  SS is present
  8117.  
  8118.  If code segment is                    SF         SS selector + EXT
  8119.  non-conforming, and
  8120.  DPL < CPL and if
  8121.  there is enough space
  8122.  for 5 words on the stack
  8123.  (or 6 if error code
  8124.  is required)
  8125.  
  8126.  If code segment is                    GP         Code segment selector + EXT
  8127.  conforming, then DPL ≤CPL
  8128.  
  8129.  Check                             Exception     Error Code
  8130. 
  8131.  If code segment                       NP         Code segment selector + EXT
  8132.  is not present
  8133.  
  8134.  If IP is not within                   GP         0 + EXT
  8135.  the limit of code segment
  8136.  
  8137.  
  8138.  Table 9-2. Interrupt and Gate Interactions
  8139.  
  8140.  Type of     Type of    Further   Further  Further      Further software
  8141.  Interrupt   Gate       NMIs?     INTRs?   Exceptions?  Interrupts?
  8142.  NMI         Trap         No        Yes      Yes           Yes
  8143.  NMI         Interrupt    No        No       Yes           Yes
  8144.  INTR        Trap         Yes       Yes      Yes           Yes
  8145.  INTR        Interrupt    Yes       No       Yes           Yes
  8146.  Software    Trap         Yes       Yes      Yes           Yes
  8147.  Software    Interrupt    Yes       No       Yes           Yes
  8148.  Exception   Trap         Yes       Yes      Yes           Yes
  8149.  Exception   Interrupt    Yes       No       Yes           Yes
  8150.  
  8151.  Figure 9-3.  Trap/Interrupt Gate Descriptors
  8152.  
  8153.         ╔═══════════════════════════════╤═══════════════════════════════╗
  8154.       +7║                        INTEL RESERVED                        ║+6
  8155.         ╟───┬───────┬───┬───────────────┬───────────────────────────────╢
  8156.       +5║ P │  DP2  │ 0 │ 0   1   1   T │            UNUSED             ║+4
  8157.         ╟───┴───┴───┴───┴───┴───┴───┴───┴───────────────────────────────╢
  8158.       +3║                INTERRUPT CODE SEGMENT SELECTOR                ║+2
  8159.         ║───────────────────────────────────────────────────────────────║
  8160.       +1║                     INTERRUPT CODE OFFSET                     ║ 0
  8161.         ╚═══════════════════════════════════════════════════════════════╝
  8162.  
  8163.         T = 1 FOR TRAP GATE
  8164.  
  8165.         T = 0 FOR INTERRUPT GATE
  8166.  
  8167.  
  8168.  Figure 9-4.  Stack Layout after an Exception with an Error Code
  8169.  
  8170.                   OLD SP─────╔════════════════╗ NO PRIVILEGE TRANSITION
  8171.                               ║   OLD FLAGS    ║
  8172.                               ╟────────────────╢
  8173.                               ║     OLD CS     ║
  8174.                               ╟────────────────╢
  8175.                               ║     OLD IP     ║
  8176.                               ╟────────────────╢
  8177.                               ║   ERROR CODE   ║
  8178.                       SP─────╟────────────────╢
  8179.                               ║                ║
  8180.                               ║                ║
  8181.                               ≈                ≈
  8182.                               ║                ║
  8183.                       SS─────╚════════════════╝
  8184.  
  8185.              SP FROM TSS─ ─ ─╔════════════════╗ WITH PRIVILEGE TRANSITION
  8186.                               ║     OLD SS     ║
  8187.                               ╟────────────────╢
  8188.                               ║     OLD SP     ║
  8189.                               ╟────────────────╢
  8190.                               ║   OLD FLAGS    ║
  8191.                               ╟────────────────╢
  8192.                               ║     OLD CS     ║
  8193.                               ╟────────────────╢
  8194.                               ║     OLD IP     ║
  8195.                               ╟────────────────╢
  8196.                               ║   ERROR CODE   ║
  8197.                       SP─────╟────────────────╢
  8198.                               ║                ║
  8199.                               ║                ║
  8200.                               ≈                ≈
  8201.                               ║                ║
  8202.              SP FROM TSS─────╚════════════════╝
  8203.                                  STACK SEGMENT
  8204.  
  8205.  
  8206.  9.5  Task Gates and Interrupt Tasks
  8207.  
  8208.  The 80286 allows interrupts to directly cause a task switch. When an
  8209.  interrupt vector selects an entry in the IDT which is a task gate, a task
  8210.  switch occurs. The format of a task gate is described in section 8.5. If a
  8211.  task gate is used to handle an exception that passes an error code, the
  8212.  error code will be pushed onto the new task's stack.
  8213.  
  8214.  A task gate offers two advantages over interrupt gates:
  8215.  
  8216.    1.  It automatically saves all of the processor registers as part of the
  8217.        task-switch operation, whereas an interrupt gate saves only the flag
  8218.        register and CS:IP.
  8219.  
  8220.    2.  The new task is completely isolated from the task that was
  8221.        interrupted. Address spaces are isolated and the interrupt-handling
  8222.        task is unaffected by the privilege level of the interrupted task.
  8223.  
  8224.  An interrupt task switch works like any other task switch once the TSS
  8225.  selector is fetched from the task gate. Like a trap or an interrupt gate,
  8226.  privilege and presence rules are applied to accessing a task gate during an
  8227.  interrupt.
  8228.  
  8229.  Interrupts that cause a task switch set the NT bit in the flags of the new
  8230.  task. The TSS selector of the interrupted task is saved in the back link
  8231.  field of the new TSS. The interrupting task executes IRET to perform a task
  8232.  switch to return to the interrupted task because NT was previously set. The
  8233.  interrupt task state is saved in its TSS before returning control to the
  8234.  task that was interrupted; NT is restored to its original value in the
  8235.  interrupted task.
  8236.  
  8237.  Since the interrupt handler state after executing IRET is saved, a re-entry
  8238.  of the interrupt service task will result in the execution of the
  8239.  instruction that follows IRET. Therefore, when the next interrupt occurs,
  8240.  the machine state will be the same as that when the IRET instruction was
  8241.  executed.
  8242.  
  8243.  Note that an interrupt task resumes execution each time it is re-invoked,
  8244.  whereas an interrupt procedure starts executing at the beginning of the
  8245.  procedure each time. The interrupted task restarts execution at the point of
  8246.  interruption because interrupts occur before the execution of an
  8247.  instruction.
  8248.  
  8249.  When an interrupt task is used, the task must be concerned with avoiding
  8250.  further interrupts while it is operating. A general protection exception
  8251.  will occur if a task gate referring to a busy TSS is used while processing
  8252.  an interrupt. If subsequent interrupts can occur while the task is
  8253.  executing, the IF bit in the flag word (saved in the TSS) must be zero.
  8254.  
  8255.  
  8256.  9.5.1  Scheduling Considerations
  8257.  
  8258.  A software-scheduled operating system must be designed to handle the fact
  8259.  that interrupts can come along in the middle of scheduled tasks and cause a
  8260.  task switch to other tasks. The interrupt-scheduled tasks may call the
  8261.  operating system and eventually the scheduler, which needs to recognize
  8262.  that the task that just called it is not the one the operating system last
  8263.  scheduled.
  8264.  
  8265.  If the Task Register (TR) does not contain the TSS selector of the last
  8266.  scheduled task, an interrupt initiated task switch has occurred. More than
  8267.  one task may have been interrupt-scheduled since the scheduler last ran. The
  8268.  scheduler must find via the backlink fields in each TSS all tasks that have
  8269.  been interrupted. The scheduler can clear those links and reset the busy bit
  8270.  in the TSS descriptors, putting them back in the scheduling queue for a new
  8271.  analysis of execution priorities. Unless the interrupted tasks are placed
  8272.  back in the scheduling queue, they would have to await a later restart via
  8273.  the task that interrupted them.
  8274.  
  8275.  To locate tasks that have been interrupt-scheduled, the scheduler looks
  8276.  into the current task's TSS backlink (word one of the TSS), which points at
  8277.  the interrupted task. If that task was not the last task scheduled, then
  8278.  it's backlink field in the TSS also points to an interrupted task.
  8279.  
  8280.  The backlink field of each interrupt-scheduled task should be set by the
  8281.  scheduler to point to a scheduling task that will reschedule the highest
  8282.  priority task when the interrupt-scheduled task executes IRET.
  8283.  
  8284.  
  8285.  9.5.2  Deciding Between Task, Trap, and Interrupt Gates
  8286.  
  8287.  Interrupts and exceptions can be handled with either a trap/interrupt gate
  8288.  or a task gate. The advantages of a task gate are all the registers are
  8289.  saved and a new set is loaded with full isolation between the interrupted
  8290.  task and the interrupt handler. The advantages of a trap/interrupt gate are
  8291.  faster response to an interrupt for simple operations and easy access to
  8292.  pointers in the context of the interrupted task. All interrupt handlers use
  8293.  IRET to resume the interrupted program.
  8294.  
  8295.  Trap/interrupt gates require that the interrupt handler be able to execute
  8296.  at the same or greater privilege level than the interrupted program. If any
  8297.  program executing at level 0 can be interrupted through a trap/task gate,
  8298.  the interrupt handler must also execute at level 0 to avoid general
  8299.  protection exception. All code, data, and stack segment descriptors must be
  8300.  in the GDT to allow access from any task. But, placing all system interrupt
  8301.  handlers at privilege level 0 may be in consistent with maintaining the
  8302.  integrity of level 0 programs.
  8303.  
  8304.  Some exceptions require the use of a task gate. The invalid task state
  8305.  segment exception (#10) can arise from errors in the original TSS as well as
  8306.  in the target TSS. Handling the exception within the same task could lead to
  8307.  recursive interrupts or other undesirable effects that are difficult to
  8308.  trace. The double fault exception (#8) should also use a task gate to
  8309.  prevent shutdown from another protection violation occurring during the
  8310.  servicing of the exception.
  8311.  
  8312.  
  8313.  9.6  Protection Exceptions and Reserved Vectors
  8314.  
  8315.  A protection violation will cause an exception, i.e., a non-maskable
  8316.  interrupt. Such a fault can be handled by the task that caused it if an
  8317.  interrupt or trap gate is used, or by a different task if a task gate is
  8318.  used (in the IDT).
  8319.  
  8320.  Protection exceptions can be classified into program errors or implicit
  8321.  requests for service. The latter include stack overflow and not-present
  8322.  faults. Examples of program errors include attempting to write into a
  8323.  read-only segment, or violating segment limits.
  8324.  
  8325.  Requests for service may use different interrupt vectors, but many diverse
  8326.  types of protection violation use the same general protection fault vector.
  8327.  Table 9-3 shows the reserved exceptions and interrupts. Interrupts 0-31 are
  8328.  reserved by Intel.
  8329.  
  8330.  When simultaneous external interrupt requests occur, they are processed in
  8331.  the fixed order shown in table 9-4. For each interrupt serviced, the
  8332.  machine state is saved. The new CS:IP is loaded from the gate or TSS. If
  8333.  other interrupts remain enabled, they are processed before the first
  8334.  instruction of the current interrupt handler, i.e., the last interrupt
  8335.  processed is serviced first.
  8336.  
  8337.  All but two exceptions are restartable after the exceptional condition is
  8338.  removed. The two non-restartable exceptions are the processor extension
  8339.  segment overrun and writing into read only segments with XCHG, ADC, SBB,
  8340.  RCL, and RCR instructions. The return address normally points to the failing
  8341.  instruction, including all leading prefixes.
  8342.  
  8343.  The instruction and data addresses for the processor extension segment
  8344.  overrun are contained in the processor extension status registers.
  8345.  
  8346.  Interrupt handlers for most exceptions receive an error code that
  8347.  identifies the selector involved, or a 0 in bits 15-3 of the error code
  8348.  field if there is no selector involved. The error code is pushed last,
  8349.  after the return address, on the stack that will be active when the trap
  8350.  handler begins execution. This ensures that the handler will not have to
  8351.  access another stack segment to find the error code.
  8352.  
  8353.  The following sections describe the exceptions in greater detail.
  8354.  
  8355.  
  8356.  Table 9-3. Reserved Exceptions and Interrupts
  8357.  
  8358.  Vector  Description                         Restartable   Code on
  8359.  Number                                        Error        Stack
  8360.  0       Divide Error Exception                 Yes           No
  8361.  1       Single Step Interrupt                  Yes           No
  8362.  2       NMI Interrupt                          Yes           No
  8363.  3       Breakpoint Interrupt                   Yes           No
  8364.  4       INTO Detected Overflow Exception       Yes           No
  8365.  5       BOUND Range Exceeded Exception         Yes           No
  8366.  6       Invalid Opcode Exception               Yes           No
  8367.  7       Processor Extension Not Available
  8368.          Exception                              Yes           No
  8369.  8       Double Exception Detected              No            Yes (Always 0)
  8370.  9       Processor Extension Segment Overrun
  8371.          Interrupt                              No            No
  8372.  10      Invalid Task State Segment             Yes           Yes
  8373.  11      Segment Not Present                    Yes           Yes
  8374.  12      Stack Segment Overrun or Not Present   Yes           Yes
  8375.  13      General Protection                     Yes          Yes
  8376.  
  8377.  
  8378.  Table 9-4. Interrupt Processing Order
  8379.  
  8380.  Order        Interrupt
  8381.   1           Instruction exception
  8382.   2           Single step
  8383.   3           NMI
  8384.   4           Processor extension segment overrun
  8385.   5           INTR
  8386.  
  8387.  
  8388.  9.6.1  Invalid OP-Code (Interrupt 6)
  8389.  
  8390.  When an invalid opcode is detected by the execution unit, interrupt 6 is
  8391.  invoked. (It is not detected until an attempt is made to execute it, i.e.,
  8392.  prefetching an invalid opcode does not cause this exception.) The saved
  8393.  CS:IP will point to the invalid opcode or any leading prefixes; no error
  8394.  code is pushed on the stack. The exception can be handled within the same
  8395.  task, and is restartable.
  8396.  
  8397.  This exception will occur for all cases of an invalid operand. Examples
  8398.  include an inter-segment jump referencing a register operand, or an LES
  8399.  instruction with a register source operand.
  8400.  
  8401.  
  8402.  9.6.2  Double Fault (Interrupt 8)
  8403.  
  8404.  If two separate faults occur during a single instruction, end if the first
  8405.  fault is any of #0, #10, #11, #12, and #13, exception 8 (Double Fault)
  8406.  occurs (e.g., a general protection fault in level 3 is followed by a
  8407.  not-present fault due to a segment not-present). If another protection
  8408.  violation occurs during the processing of exception 8, the 80286 enters
  8409.  shutdown, during which time no further instructions or exceptions are
  8410.  processed.
  8411.  
  8412.  Either NMI or RESET can force the CPU out of shutdown. An NMI input can
  8413.  bring the CPU out of shutdown if no errors occur while processing the NMI
  8414.  interrupt; otherwise, shutdown can only be exited via the RESET input. NMI
  8415.  causes the CPU to remain in protected mode, and RESET causes it to exit
  8416.  protected mode. Shutdown is signaled externally via a HALT bus operation
  8417.  with A1 LOW.
  8418.  
  8419.  A task gate must be used for the double fault handler to assure a proper
  8420.  task state to respond to the exception. The back link field in the current
  8421.  TSS will identify the TSS of the task causing the exception. The saved
  8422.  address will point at the instruction that was being executed (or was ready
  8423.  to execute) when the error was detected. The error code will be null.
  8424.  
  8425.  The "double fault" exception does not occur when detecting a new exception
  8426.  while trying to invoke handlers for the following exceptions: 1, 2, 3, 4, 5,
  8427.  6, 7, 9, and 16.
  8428.  
  8429.  
  8430.  9.6.3  Processor Extension Segment Overrun (Interrupt 9)
  8431.  
  8432.  Interrupt 9 signals that the processor extension (such as the 80287
  8433.  numerics processor) has overrun the limit of a segment while attempting to
  8434.  read/write the second or subsequent words of an operand. The interrupt is
  8435.  generated by the processor extension data channel within the 80286 during
  8436.  the limit test performed on each transfer of data between memory and the
  8437.  processor extension. This interrupt can be handled in the same task but is
  8438.  not restartable.
  8439.  
  8440.  As with all external interrupts, Interrupt 9 is an asynchronous demand
  8441.  caused by the processor extension referencing something outside a segment
  8442.  boundary. Since Interrupt 9 can occur any time after the processor extension
  8443.  is started, the 80286 does not save any information that identifies what
  8444.  particular operation had been initiated in the processor extension. The
  8445.  processor extension maintains special registers that identify the last
  8446.  instruction it executed and the address of the desired operand.
  8447.  
  8448.  After this interrupt occurs, no WAIT or escape instruction, except FNINIT,
  8449.  can be executed until the interrupt condition is cleared or the processor
  8450.  extension is reset. The interrupt signals that the processor extension is
  8451.  requesting an invalid data transfer. The processor extension will always be
  8452.  busy when waiting on data. Deadlock results if the CPU executes an
  8453.  instruction that causes it to wait for the processor extension before
  8454.  resetting the processor extension. Deadlock means the CPU is waiting for the
  8455.  processor extension to become idle while the processor extension waits for
  8456.  the CPU to service its data request.
  8457.  
  8458.  The FNINIT instruction is guaranteed to reset the processor extension
  8459.  without causing deadlock. After the interrupt is cleared, this restriction
  8460.  is lifted. It is then possible to read the instruction and operand address
  8461.  via FSTENV or FSAVE, causing the segment overrun in the processor
  8462.  extension's special registers.
  8463.  
  8464.  The task interrupted by interrupt 9 is not necessarily the task that
  8465.  executed the ESC instruction that caused the interrupt. The operating system
  8466.  should keep track of which task last used the NPX (see section 11.4). If
  8467.  the interrupted task did not execute the ESC instruction, it can be
  8468.  restarted. The task that executed the ESC instruction cannot.
  8469.  
  8470.  
  8471.  9.6.4  Invalid Task State Segment (Interrupt 10)
  8472.  
  8473.  Interrupt 10 is invoked if during a task switch the new TSS pointed to by
  8474.  the task gate is invalid. The EXT bit indicates whether the exception was
  8475.  caused by an event outside the control of the program.
  8476.  
  8477.  A TSS is considered invalid in the cases shown in table 9-5.
  8478.  
  8479.  Once the existence of the new TSS is verified, the task switch is
  8480.  considered complete, with the backlink set to the old task if necessary. All
  8481.  errors are handled in the context of the new task.
  8482.  
  8483.  Exception 10 must be handled through a task gate to insure a proper TSS to
  8484.  process it. The handler must reset the busy bit in the new TSS.
  8485.  
  8486.  
  8487.  9.6.5  Not Present (Interrupt 11)
  8488.  
  8489.  Exception 11 occurs when an attempt is made to load a not-present segment
  8490.  or to use a control descriptor that is marked not-present. (If, however, the
  8491.  missing segment is an LDT that is needed in a task switch, exception 10
  8492.  occurs.) This exception is fully restartable.
  8493.  
  8494.  Any segment load instruction can cause this exception. Interrupt 11 is
  8495.  always processed in the context of the task in which it occurs.
  8496.  
  8497.  The error code has the form shown in Table 9-5. The EXT bit will be set if
  8498.  an event external to the program caused an interrupt that subsequently
  8499.  referenced a not-present segment. Bit 1 will be set if the error code refers
  8500.  to an IDT entry, e.g., an INT instruction referencing a not-present gate.
  8501.  The upper 14 bits are the upper 14 bits of the segment selector involved.
  8502.  
  8503.  During a task switch, when a not-present exception occurs, the ES and DS
  8504.  segment registers may not be usable for referencing memory (the selector
  8505.  values are loaded before the descriptors are checked). The not-present
  8506.  handler should not rely on being able to use the values found in ES, SS,
  8507.  and DS without causing another exception. This is because the task switch
  8508.  itself may have changed the values in the registers. The exception occurs in
  8509.  the new task and the return pointer points to the first instruction of the
  8510.  new task. Caution: the loading of the DS or ES descriptors may not have
  8511.  been completed. The exception II handler should ensure that the DS and ES
  8512.  descriptors have been properly loaded before the execution of the first
  8513.  instruction of the new task.
  8514.  
  8515.  
  8516.  Table 9-5. Conditions That Invalidate the TSS
  8517.  
  8518.  Reason                                                   Error Code
  8519.  The limit in the TSS descriptor is less than 43          TSS id + EXT
  8520.  Invalid LDT selector or LDT not present                  LDT id + EXT
  8521.  Stack segment selector is null                           SS id + EXT
  8522.  Stack segment selector is outside table limit            SS id + EXT
  8523.  Stack segment is not a writable segment                  SS id + EXT
  8524.  Stack segment DPL does not match new CPL                 SS id + EXT
  8525.  Stack segment selector RPL <> ECPL                       SS id + EXT
  8526.  Code segment selector is outside table limit             CS id + EXT
  8527.  Code segment selector does not refer to code segment     CS id + EXT
  8528.  Non-conforming code segment DPL <> ECPL                  CS id + EXT
  8529.  Conforming code segment DPL > CPL                        CS id + EXT
  8530.  DS or ES segment selector is outside table limits        ES/DS id + EXT
  8531.  DS or ES are not readable segments                       ES/DS id + EXT
  8532.  
  8533.  
  8534.  9.6.6  Stack Fault (Interrupt 12)
  8535.  
  8536.  Stack underflow or overflow causes exception 12, as does a not-present
  8537.  stack segment referenced during an inter-task or inter-level transition.
  8538.  This exception is fully restartable. A limit violation of the current stack
  8539.  results in an error code of 0. The EXT bit of the error code tells whether
  8540.  an interrupt external to the program caused the exception.
  8541.  
  8542.  Any instruction that loads a selector to SS (e.g., POP SS, task switch) can
  8543.  cause this exception. This exception must use a task gate if there is a
  8544.  possibility that any level 0 stack may not be present.
  8545.  
  8546.  When a stack fault occurs, the ES and DS segment registers may not be
  8547.  usable for referencing memory. During a task switch, the selector values are
  8548.  loaded before the descriptors are checked. The stack fault handler should
  8549.  check the saved values of SS, CS, DS, and ES to be sure that they refer to
  8550.  present segments before restoring them.
  8551.  
  8552.  
  8553.  9.6.7  General Protection Fault (Interrupt 13)
  8554.  
  8555.  If a protection violation occurs which is not covered in the preceding
  8556.  paragraphs, it is classed as Interrupt 13, a general protection fault. The
  8557.  error code is zero for limit violations, write to read-only segment
  8558.  violations, and accesses relative to DS or ES when they are zero or refer
  8559.  to a segment at a greater privilege level than CPL. Other access violations
  8560.  (e.g., a wrong descriptor type) push a non-zero error code that identifies
  8561.  the selector used on the stack. Error codes with bit 0 cleared and bits
  8562.  15-2 non-zero indicate a restartable condition.
  8563.  
  8564.  Bit 1 of the error code identifies whether the selector is in the IDT or
  8565.  LDT/GDT. If bit 1 = 0 then bit 2 separates LDT from GDT. Bit 0 (EXT)
  8566.  indicates whether the exception was caused by the program or an event
  8567.  external to it (i.e., single stepping, an external interrupt, a processor
  8568.  extension not-present or a segment overrun). If bit 0 is set, the selector
  8569.  typically has nothing to do with the instruction that was interrupted. The
  8570.  selector refers instead to some step of servicing an interrupt that failed.
  8571.  
  8572.  When bit 0 of the error code is set, the interrupted program can be
  8573.  restarted, except for processor extension segment overrun exceptions (see
  8574.  section 9.6.3). The exception with the bit 0 of the errorcode = 1 indicates
  8575.  some interrupt has been lost due to a fault in the descriptor pointed to by
  8576.  the error code.
  8577.  
  8578.  A non-zero error code with bit 0 cleared may be an operand of the
  8579.  interrupted instruction, an operand from a gate referenced by the
  8580.  instruction, or a field from the invalid TSS.
  8581.  
  8582.  During a task switch, when a general protection exception occurs, the ES
  8583.  and DS segment registers may not be usable for referencing memory (the
  8584.  selector vaues are loaded before the descriptors are checked). The general
  8585.  protection handler should not rely on being able to use the values found in
  8586.  ES, SS, and DS without causing another exception. This is because the task
  8587.  switch itself may have changed the values in the registers. The exception
  8588.  occurs in the new task and the return pointer points to the first
  8589.  instruction of the new task. Caution: the loading of the DS or ES
  8590.  descriptors may not have been completed. The exception 13 handler should
  8591.  ensure that the DS and ES descriptors have been properly loaded before the
  8592.  execution of the first instruction of the new task.
  8593.  
  8594.  In Real Address Mode, Interrupt 13 will occur if software attempts to read
  8595.  or write a 16-bit word at segment offset 0FFFFH.
  8596.  
  8597.  
  8598.  9.7  Additional Exceptions and Interrupts
  8599.  
  8600.  Interrupts 0, 5, and 1 have not yet been discussed. Interrupt 0 is the
  8601.  divide-error exception, Interrupt 5 the bound-range exceeded exceptions, and
  8602.  Interrupt 1 the single step interrupt. The divide-error or bound-range
  8603.  exceptions make it appear as if that instruction had never executed: the
  8604.  registers are restored and the instruction can be restarted. The
  8605.  divide-error exception occurs during a DIV or an IDIV instruction when the
  8606.  quotient will be too large to be representable, or when the divisor is
  8607.  zero.
  8608.  
  8609.  Interrupt 5 occurs when a value exceeds the limit set for it. A program can
  8610.  use the BOUND instruction to check a signed array index against signed
  8611.  limits defined in a two-word block of memory. The block can be located just
  8612.  before the array to simplify addressing. The block's first word specifies
  8613.  the array's lower limit, the second word specifies the array's upper limit,
  8614.  and a register specifies the array index to be tested.
  8615.  
  8616.  
  8617.  9.7.1  Single Step Interrupt (Interrupt 1)
  8618.  
  8619.  Interrupt 1 allows programs to execute one instruction at a time. This
  8620.  single-stepping is controlled by the TF bit in the flag word. Once this bit
  8621.  is set, an internal single step interrupt will occur after the next
  8622.  instruction has been executed. The interrupt saves the flags and return
  8623.  address on the stack, clears the TF bit, and uses an internally supplied
  8624.  vector of 1 to transfer control to the service routine via the IDT.
  8625.  
  8626.  The IRET instruction or a task switch must be used to set the TF bit and to
  8627.  transfer control to the next instruction to be single stepped. If TF=1 in a
  8628.  TSS and that task is invoked, it will execute the first instruction and then
  8629.  be interrupted.
  8630.  
  8631.  The single-step flag is normally not cleared by privilege changes inside a
  8632.  task. INT instructions, however, do clear TF. Therefore, software debuggers
  8633.  that single-step code must recognize and emulate INT n or INT 0 rather than
  8634.  executing them directly. System software should check the current execution
  8635.  privilege level after any single step interrupt to see whether single
  8636.  stepping should continue.
  8637.  
  8638.  The interrupt priorities in hardware guarantee that if an external
  8639.  interrupt occurs, single stepping stops. When both an external interrupt and
  8640.  a single step interrupt occur together, the single step interrupt is
  8641.  processed first. This clears the TF bit. After saving the return address or
  8642.  switching tasks, the external interrupt input is examined before the first
  8643.  instruction of the single step handler executes. If the external interrupt
  8644.  is still pending, it is then serviced. The external interrupt handler is
  8645.  not single-stepped. Therefore, to single step an interrupt handler, just
  8646.  single step an interrupt instruction that refers to the interrupt handler.
  8647.  
  8648.  
  8649.  Chapter 10  System Control and Initialization
  8650.  
  8651.  ───────────────────────────────────────────────────────────────────────────
  8652.  
  8653.  Special flags, registers, and instructions provide contol of the critical
  8654.  processes and interaction in 80286 operations. The flag register includes 3
  8655.  bits that represent the current I/O privilege level (IOPL: 2 bits) and the
  8656.  nested task bit (NT). Four additional registers support the virtual
  8657.  addressing and memory protection features, one points to the current Task
  8658.  State Segment and the other three point to the memory-based descriptor
  8659.  tables: GDT, LDT, and IDT. These flags and registers are discussed in the
  8660.  next section. The machine status word, (which indicates processor
  8661.  configuration and status) and the instructions that load and store it are
  8662.  discussed in section 10.2.1.
  8663.  
  8664.  Similar instructions pertaining to the other registers are the subject of
  8665.  sections 10.2 and 10.3. A detailed description of initialization states
  8666.  and processes, which appears in section 10.4, is supplemented by the
  8667.  extensive example in Appendix A. Instructions that validate descriptors
  8668.  and pointers are covered in section 11.3.
  8669.  
  8670.  
  8671.  10.1  System Flags and Registers
  8672.  
  8673.  The IOPL flag (bits 12 and 13 of the flags word) controls access to I/O
  8674.  operations and interrupt control instructions. These two bits represent the
  8675.  maximum privilege level (highest numerical CPL) at which the task is
  8676.  permitted to perform I/O instructions. Alteration of the IOPL flags is
  8677.  restricted to programs at level 0 or to a task switch.
  8678.  
  8679.  IRET uses the NT flag to select the proper return: if NT=0, the normal
  8680.  return within a task is performed. As discussed in Chapter 8, the nested
  8681.  task flag (bit 14 of flags) is set when a task initiates a task switch via a
  8682.  CALL or INT instruction. The old and new task state segments are marked
  8683.  busy and the backlink field of the new TSS is set to the old TSS selector.
  8684.  An interrupt that does not cause a task switch will clear NT after the old
  8685.  NT state is saved. To prevent a program from causing an illegal task switch
  8686.  by setting NT and then executing IRET, a zero selector should be placed in
  8687.  the backlink field of the TSS. An illegal task switch using IRET will then
  8688.  cause exception 13. The instructions POPF and IRET can also set or clear NT
  8689.  when flags are restored from the stack. POPF and IRET can also change the
  8690.  interrupt enable flag. If CPL ≤ IOPL, then the Interrupt Flag (IF) can be
  8691.  changed by POPF and IRET. Otherwise, the state of the IF bit in the new
  8692.  flag word is ignored by these instructions. Note that the CLI and STI
  8693.  instructions are valid only when CPL ≤ IOPL; otherwise exception 13 occurs.
  8694.  
  8695.  
  8696.  10.1.1  Descriptor Table Registers
  8697.  
  8698.  The three descriptor tables used for all memory accesses are based at
  8699.  addresses supplied by (stored in) three registers: the global descriptor
  8700.  table register (GDTR), the interrupt descriptor table register (IDTR), and
  8701.  the local descriptor table register (LDTR). Each register contains a 24-bit
  8702.  base field and a 16-bit limit field. The base field gives the real memory
  8703.  address of the beginning of the table; the limit field tells the maximum
  8704.  offset permitted in accessing table entries. See figures 10-1, 10-2, and
  8705.  10-3.
  8706.  
  8707.  The LDTR also contains a selector field that identifies the descriptor for
  8708.  that table. LDT descriptors must reside in the GDT.
  8709.  
  8710.  The task register (TR) points to the task state segment for the currently
  8711.  active task. It is similar to a segment register, with selector, base, and
  8712.  limit fields, of which only the selector field is readable under normal
  8713.  circumstances. Each such selector serves as a unique identifier for its
  8714.  task. The uses of the TR are described in Chapter 8.
  8715.  
  8716.  The instructions controlling these special registers are described in the
  8717.  next section.
  8718.  
  8719.  
  8720.  Figure 10-1.  Local and Global Descriptor Table Definition
  8721.  
  8722.                                                   MEMORY
  8723.                                                            
  8724.                                      ┌─┬───╟───────────────╢─┐
  8725.                                      │      ╟───────────────╢ │
  8726.                         CPU          │ │    ╟───────────────╢ │
  8727.              ╔═════════════════════╗ │      ║       ∙       ║ │
  8728.              ║                     ║ │ │    ║       ∙       ║ ├─ GDT
  8729.              ║       15        0   ║ │      ║       ∙       ║ │
  8730.              ║       ╔═════════╗   ║ │ │    ╟───────────────╢ │
  8731.              ║   23  ║LDT LIMIT╟───╫─┘      ╟───────────────╢ │
  8732.              ║   ╔═══╨─────────╢   ║   │    ╟───────────────╢ │
  8733.         GDTR ║   ║   GDT BASE  ╟───╫───────╟───────────────╢─┘
  8734.              ║   ╚═════════════╝   ║        ║               ║
  8735.              ║                     ║        ║               ║
  8736.              ╟─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╢        ║               ║
  8737.              ║                     ║        ║     LDT{1}    ║
  8738.              ║     15         0    ║ ┌─┬───╟───────────────╢─┐
  8739.              ║     ╔══════════╗    ║ │      ╟───────────────╢ │
  8740.              ║     ║   LDT    ║    ║ │ │    ╟───────────────╢ │
  8741.              ║     ║ SELECTOR ║    ║ │      ║       ∙       ║ │
  8742.              ║     ╚══════════╝    ║ │ │    ║       ∙       ║ ├─ CURRENT LDT
  8743.              ║┌─ ── ── ── ── ── ──┐║ │      ║       ∙       ║ │
  8744.              ║│      15        0  │║ │ │    ╟───────────────╢ │
  8745.              ║       ╔═════════╗   ║ │      ╟───────────────╢ │
  8746.              ║│  23  ║LDT LIMIT╟──┼╫─┘ │    ╟───────────────╢ │
  8747.              ║   ╔═══╨─────────╢   ║  ┌────╟───────────────╢─┘
  8748.         LDTR ║│  ║   LDT BASE  ╟──┼╟──┘     ║               ║
  8749.              ║   ╚═════════════╝   ║        ║               ║
  8750.              ║│ PROGRAM INVISIBLE │║        ║               ║
  8751.              ║└─ ── ── ── ── ── ──┘║        ║     LDT{n}    ║
  8752.              ╚═════════════════════╝        ╟───────────────╢
  8753.                                             ╟───────────────╢
  8754.                                             ╟───────────────╢
  8755.                                             ╟───────────────╢
  8756.                                             ║       ∙       ║
  8757.                                             ║       ∙       ║
  8758.                                             ║       ∙       ║
  8759.                                             ╟───────────────╢
  8760.                                             ╟───────────────╢
  8761.                                             ╟───────────────╢
  8762.                                             ╟───────────────╢
  8763.                                             ╟───────────────╢
  8764.                                                            
  8765.  
  8766.  
  8767.  Figure 10-2.  Interrupt Descriptor Table Definition
  8768.  
  8769.                                            MEMORY
  8770.                                                      
  8771.                                       ║               ║
  8772.                                ┌───┬─╠═══════════════╣─┐
  8773.                                │      ║   GATE FOR    ║ │
  8774.                                │   │  ║ INTERRUPT #n  ║ │
  8775.                                │      ╠═══════════════╣ │
  8776.                                │   │  ║   GATE FOR    ║ │
  8777.                                │      ║INTERRUPT #n-1 ║ │
  8778.                                │   │  ╠═══════════════╣ │
  8779.                                │      ║       ∙       ║ │  INTERRUPT
  8780.                                │   │  ║       ∙       ║ ├─ DESCRIPTOR
  8781.                                │      ║       ∙       ║ │  TABLE (IDT)
  8782.                    CPU         │   │  ╠═══════════════╣ │
  8783.          ┌───────────────────┐ │      ║   GATE FOR    ║ │
  8784.          │      15        0  │ │   │  ║ INTERRUPT #1  ║ │
  8785.          │      ╔═════════╗  │ │      ╠═══════════════╣ │
  8786.          │      ║IDT LIMIT╟──┼─┘   │  ║   GATE FOR    ║ │
  8787.          │  ╔═══╨─────────╢  │        ║ INTERRUPT #0  ║ │
  8788.     IDTR │  ║   IDT BASE  ╟──┼─────┴─╠═══════════════╣─┘
  8789.          │  ╚═════════════╝  │        ║               ║
  8790.          │  23            0  │                       
  8791.          └───────────────────┘
  8792.  
  8793.  
  8794.  Figure 10-3.  Data Type for Global Descriptor Table and
  8795.                Interrupt Descriptor Table
  8796.  
  8797.         7                             0 7                             0
  8798.        ╔═══════════════════════════════╤═══════════════════════════════╗
  8799.      +5║         INTEL RESERVED       │           BASE{23-16}         ║+4
  8800.        ╟───────────────────────────────┴───────────────────────────────╢
  8801.      +3║                           BASE{15-0}                          ║+2
  8802.        ╟───────────────────────────────┴───────────────────────────────╢
  8803.      +1║                          LIMIT{15-0}                          ║ 0
  8804.        ╚═══════════════════════════════╧═══════════════════════════════╝
  8805.                                                                       0
  8806.  
  8807.  
  8808.  10.2  System Control Instructions
  8809.  
  8810.  The instructions that load the GDTR and IDTR from memory can only be
  8811.  executed in real address mode or at privilege level 0; otherwise exception
  8812.  13 occurs. The store instructions for GDTR and IDTR may be executed at any
  8813.  privilege level. The four instructions are LIDT, LGDT, SIDT, and SGDT. The
  8814.  instructions move 3 words between the indicated descriptor table register
  8815.  and the effective real memory address supplied (see figure 10-3). The
  8816.  format of the 3 words is: a 2-byte limit, a 3-byte real base address,
  8817.  followed by an unused byte. These instructions are normally used during
  8818.  system initialization.
  8819.  
  8820.  The LLDT instruction loads the LDT registers from a descriptor in the GDT.
  8821.  LLDT uses a selector operand to that descriptor rather than referencing the
  8822.  descriptor directly. LLDT is only executable at privilege level 0; otherwise
  8823.  exception 13 occurs. LLDT is normally required only during system
  8824.  initialization because the processor automatically exchanges the LDTR
  8825.  contents as part of the task-switch operation.
  8826.  
  8827.  Executing an LLDT instruction does not automatically update the TSS or the
  8828.  register caches. To properly change the LDT of the currently running task so
  8829.  that the change holds across task switches, you must perform, in order, the
  8830.  following three steps:
  8831.  
  8832.    1.  Store the new LDT selector into the appropriate word of TSS.
  8833.    2.  Load the new LDT selector into LDTR.
  8834.    3.  Reload the DS and ES registers if they refer to LDT-based
  8835.        descriptors.
  8836.  
  8837.  Note that the current code segment and stack segment descriptors should
  8838.  reside in the GDT or be copied to the same location in the new LDT.
  8839.  
  8840.  SLDT (store LDT) can be executed at any privilege level. SLDT stores the
  8841.  local descriptor table selector from the program visible portion of the LDTR
  8842.  register.
  8843.  
  8844.  Task Register loading or storing is again similar to that of the LDT. The
  8845.  LTR instruction, operating only at level 0, loads the LTR at initialization
  8846.  time with a selector for the initial TSS. LTR does NOT cause a task switch;
  8847.  it just changes the current TSS. Note that the busy bit of the old TSS
  8848.  descriptor is not changed while the busy bit of the new TSS selector must be
  8849.  zero and will be set by LTR. The LDT and any segment registers referring to
  8850.  the old LDT should be reloaded. STR, which permits the storing of TR
  8851.  contents into memory, can be executed at any privilege level. LTR is not
  8852.  usually needed after initialization because the TR is managed by the
  8853.  task-switch operation.
  8854.  
  8855.  
  8856.  10.2.1  Machine Status Word
  8857.  
  8858.  The Machine Status Word (MSW) indicates the 80286 configuration and status.
  8859.  It is not part of a task's state. The MSW word is loaded by the LMSW
  8860.  instruction executed in real address mode or at privilege level 0 only, or
  8861.  is stored by the SMSW instruction executing at any privilege level. MSW is a
  8862.  16-bit register, the lower four bits of which are used by the 80286. These
  8863.  bits have the meanings shown in table 10-1. Bits 15-4 of the MSW will be
  8864.  used by the 80386. 80286 software should not change these bits. If the bits
  8865.  are changed by the 286 software, compatibility with the 80386 will be
  8866.  destroyed.
  8867.  
  8868.  The TS flag is set under hardware control and reset under software control.
  8869.  Once the TS flag is set, the next instruction using a processor extension
  8870.  causes a processor extension not-present exception (#7). This feature allows
  8871.  software to test whether the current processor extension state belongs to
  8872.  the current task as discussed in section 11.4. If the current processor
  8873.  extension state belongs to a different task, the software can save the state
  8874.  of any processor extension with the state of the task that uses it. Thus,
  8875.  the TS bit protects a task from processor extension errors that result from
  8876.  the actions of a previous task.
  8877.  
  8878.  The CLTS instruction is used to reset the TS flag after the exception
  8879.  handler has set up the proper processor extension state. The CLTS
  8880.  instruction can be executed at privilege level 0 only.
  8881.  
  8882.  The EM flag indicates a processor extension function is to be emulated by
  8883.  software. If EM=1 and MP=0, all ESCAPE instructions will be trapped via the
  8884.  processor extension not-present exception (#7).
  8885.  
  8886.  MP flag tells whether a processor extension is present. If MP=1 and TS=1,
  8887.  escape and wait instructions will cause exception 7.
  8888.  
  8889.  If ESC instructions are to be used, either the MP or the EM bit must be
  8890.  set, but not both.
  8891.  
  8892.  The PE flag indicates that the 80286 is in the protected virtual address
  8893.  mode. Once the PE flag is set, it can be cleared only by a reset, which then
  8894.  puts the system in real address mode emulating the 8086.
  8895.  
  8896.  Table 10-2 shows the recommended usage of the MSW. Other encodings of
  8897.  these bits are not recommended.
  8898.  
  8899.  Table 10-1. MSW Bit Functions
  8900.  
  8901.  Bit
  8902.  Position     Name  Function
  8903.  0             PE    Protected mode enable places the 80286 into protected
  8904.                      mode and cannot be cleared except by RESET.
  8905.  
  8906.  1             MP    Monitor processor extension allows WAIT instructions to
  8907.                      cause a processor extension not-present exception
  8908.                      (number 7) if TS is also set.
  8909.  
  8910.  2             EM    Emulate processor extension causes a processor
  8911.                      extension not-present exception (number 7) on
  8912.                      ESC instructions to allow a processor extension to
  8913.                      be emulated.
  8914.  
  8915.  3             TS    Task switched indicates the next instruction using a
  8916.                      processor extension will cause exception 7, allowing
  8917.                      software to test whether the current processor
  8918.                      extension context belongs to the current task.
  8919.  
  8920.  
  8921.  Table 10-2. Recommended MSW Encodings for Processor Extension Control
  8922.  
  8923. ╓┌───┌───┌────┌────────────────────────────────────────────────┌─────────────
  8924.  TS  MP  EM   Recommended Use                                  Instructions
  8925.                                                                Causing
  8926.                                                                Exception
  8927.  TS  MP  EM   Recommended Use                                  Instructions
  8928.                                                                Causing
  8929.                                                                Exception
  8930.  0   0   0    Initial encoding after RESET. 80286 operation
  8931.               is identical to 8086,   8088. Use this encoding
  8932.               only if no ESC instructions are to be executed.   None
  8933.  
  8934.  0   0   1    No processor extension is available. Software
  8935.               will emulate its function. Wait instructions do
  8936.               not cause exception 7.                            ESC
  8937.  
  8938.  1   0   1    No processor extension is available. Software
  8939.               will emulate its function. The current processor
  8940.               extension context may belong to   another task.   ESC
  8941.  
  8942.  0   1   0    A processor extension exists.                     WAIT (if TS=1)
  8943.  
  8944.  1   1   0    A processor extension exists. The current
  8945.               processor extension    context may belong to
  8946.               another task. The exception on WAIT allows
  8947.               software to test for an error pending from a
  8948.  TS  MP  EM   Recommended Use                                  Instructions
  8949.                                                                Causing
  8950.                                                                Exception
  8951.              software to test for an error pending from a
  8952.               previous processor extension operation.           ESC or
  8953.                                                                 WAIT (if TS=1)
  8954.  
  8955.  
  8956.  10.2.2  Other Instructions
  8957.  
  8958.  Instructions that verify or adjust access rights, segment limits, or
  8959.  privilege levels can be used to avoid exceptions or faults that are
  8960.  correctable. Section 10.3 describes such instructions.
  8961.  
  8962.  
  8963.  10.3  Privileged and Trusted Instructions
  8964.  
  8965.  Instructions that execute only at CPL=0 are called "privileged." An attempt
  8966.  to execute the privileged instructions at any other privilege level causes a
  8967.  general protection exception (#13) with an error code of zero. The
  8968.  privileged instructions manipulate descriptor tables or system registers.
  8969.  Incorrect use of these instructions can produce unrecoverable conditions.
  8970.  Some of these instructions (LGDT, LLDT, and LTR) are discussed in section
  8971.  10.2.
  8972.  
  8973.  Other privileged instructions are:
  8974.  
  8975.    ■  LIDT──Load interrupt descriptor table register
  8976.    ■  LMSW──Load machine status word
  8977.    ■  CLTS──Clear task switch flag
  8978.    ■  HALT──Halt processor execution
  8979.    ■  POPF (POP flags) or IRET can change the IF value only if the user is
  8980.       operating at a trusted privilege level. POPF does not change IOPL
  8981.       except at Level 0.
  8982.  
  8983.  "Trusted" instructions are restricted to execution at a privilege level of
  8984.  CPL ≥ IOPL. For each task, the operating system defines a privilege level
  8985.  below which these instructions cannot be used. Most of these instructions
  8986.  deal with input/output or interrupt management. The IOPL field in the flag
  8987.  word that holds the privilege level limit can be changed only when CPL=0.
  8988.  The trusted instructions are:
  8989.  
  8990.    ■  Input/Output──Block I/O, Input, and Output: IN, INW, OUT, OUTW, INSB,
  8991.       INSW, OUTSB, OUTSW
  8992.  
  8993.    ■  Interrupts──Enable Interrupts, Disable Interrupts: STI, CLI
  8994.  
  8995.    ■  Other──Lock Prefix
  8996.  
  8997.  
  8998.  10.4  Initialization
  8999.  
  9000.  Whenever the 80286 is initialized or reset, certain registers are set to
  9001.  predefined values. All additional desired initialization must be performed
  9002.  by user software. (See Appendix A for an example of a 286 initialization
  9003.  routine.) RESET forces the 80286 to terminate all execution and local bus
  9004.  activity; no instruction or bus action will occur as long as RESET is
  9005.  active. Execution in real address mode begins after RESET becomes inactive
  9006.  and an internal processing interval (3-4 clocks) occurs. The initial state
  9007.  at reset is:
  9008.  
  9009.       FLAGS = 0002
  9010.       MSW = FFF0H
  9011.       IP = FFF0H
  9012.       CS Selector = F000H    CS.base = FF0000H   CS.limit = FFFFH
  9013.       CS Selector = 0000H    CS.base = 000000H   CS.limit = FFFFH
  9014.       ES Selector = 0000H    ES.base = 000000H   ES.limit = FFFFH
  9015.       IDT base = 000000H     IDT.limit = 03FFH
  9016.  
  9017.  Two fixed areas of memory are reserved: the system initialization area and
  9018.  the interrupt table area. The system initialization area begins at FFFFF0H
  9019.  (through FFFFFFH) and the interrupt table area begins at 000000H (through
  9020.  0003FFH). The interrupt table area is not reserved.
  9021.  
  9022.  At this point, segment registers are valid and protection bits are set to
  9023.  0. The 80286 begins operation in real address mode, with PE=0. Maskable
  9024.  interrupts are disabled, and no processor extension is assumed or emulated
  9025.  (EM=MP=0).
  9026.  
  9027.  DS, ES, and SS are initialized at reset to allow access to the first 64K of
  9028.  memory (exactly as in the 8086). The CS:IP combination specifies a starting
  9029.  address of FFFF0H. For real address mode, the four most significant bits are
  9030.  not used, providing the same FFF0H address as the 8086 reset location. Use
  9031.  of (or upgrade to) the protected mode can be supported by a bootstrap
  9032.  loader at the high end of the address space. As mentioned in Chapter 5,
  9033.  location FFF0H ordinarily contains a JMP instruction whose target is the
  9034.  actual beginning of a system initialization or restart program.
  9035.  
  9036.  After RESET, CS points to the top 64K bytes in the 16-Mbyte physical
  9037.  address space. Reloading CS register by a control transfer to a different
  9038.  code segment in real address mode will put zeros in the upper 4 bits. Since
  9039.  the initial IP is FFF0H, all of the upper 64K bytes of address space may be
  9040.  used for initialization.
  9041.  
  9042.  Sections 10.4.1 and 10.4.2 describe the steps needed to initialize the
  9043.  80286 in the real address mode and the protected mode, respectively.
  9044.  
  9045.  
  9046.  10.4.1  Real Address Mode
  9047.  
  9048.    1.  Allocate a stack.
  9049.  
  9050.    2.  Load programs and data into memory from secondary storage.
  9051.  
  9052.    3.  Initialize external devices and the Interrupt Vector Table.
  9053.  
  9054.    4.  Set registers and MSW bits to desired values.
  9055.  
  9056.    5.  Set FLAG bits to desired values──including the IF bit to enable
  9057.        interrupts──after insuring that a valid interrupt handler exists for
  9058.        each possible interrupt.
  9059.  
  9060.    6.  Execute (usually via an inter-segment JMP to the main system
  9061.        program).
  9062.  
  9063.  
  9064.  10.4.2  Protected Mode
  9065.  
  9066.  The full 80286 virtual address mode initialization procedure requires
  9067.  additional steps to operate correctly:
  9068.  
  9069.    1.  Load programs and associated descriptor tables.
  9070.  
  9071.    2.  Load valid GDT and IDT descriptor tables, setting the GDTR and IDTR to
  9072.        their correct value.
  9073.  
  9074.    3.  Set the PE bit to enter protected mode.
  9075.  
  9076.    4.  Execute an intra-segment JMP to clear the processor queues.
  9077.  
  9078.    5.  Load or construct a valid task state segment for the initial task to
  9079.        be executed in protected mode.
  9080.  
  9081.    6.  Load the LDTR selector from the task's GDT or 0000H (null) if an LDT
  9082.        is not needed.
  9083.  
  9084.    7.  Set the stack pointer (SS, SP) to a valid location in a valid stack
  9085.        segment.
  9086.  
  9087.    8.  Mark all items not in memory as not-present.
  9088.  
  9089.    9.  Set FLAGS and MSW bits to correct values for the desired system
  9090.        configuation.
  9091.  
  9092.    10. Initialize external devices.
  9093.  
  9094.    11. Ensure that a valid interrupt handler exists for each possible
  9095.        interrupt.
  9096.  
  9097.    12. Enable interrupts.
  9098.  
  9099.    13. Execute.
  9100.  
  9101.  The example in Appendix A shows the steps necessary to load all the
  9102.  required tables and registers that permit execution of the first task of a
  9103.  protected mode system. The program in Appendix A assumes that Intel
  9104.  development tools have been used to construct a prototype GDT, IDT, LDT,
  9105.  TSS, and all the data segments necessary to start up that first task.
  9106.  Typically, these items are stored on EPROM; on most systems it is necessary
  9107.  to copy them all into RAM to get going. Otherwise, the 80286 will attempt to
  9108.  write into the EPROM to set the accessed or busy bits.
  9109.  
  9110.  The example in Appendix A also illustrates the ability to allocate unused
  9111.  entries in descriptor tables to grow the tables dynamically during
  9112.  execution. Using suitable naming conventions, the builder can allocate alias
  9113.  data segments that are larger than the prototype EPROM version. The code in
  9114.  the example will zero out the extra entries to permit later dynamic usage.
  9115.  
  9116.  
  9117.  Chapter 11  Advanced Topics
  9118.  
  9119.  ───────────────────────────────────────────────────────────────────────────
  9120.  
  9121.  This chapter describes some of the advanced topics as virtual memory
  9122.  management, restartable instructions, special segment attributes, and the
  9123.  validation of descriptors and pointers.
  9124.  
  9125.  
  9126.  11.1  Virtual Memory Management
  9127.  
  9128.  When access to a segment is requested and the access byte in its descriptor
  9129.  indicates the segment is not present in real memory, the not-present fault
  9130.  occurs (exception 11, or 12 for stacks). The handler for this fault can be
  9131.  set up to bring the absent segment into real memory (swapping or
  9132.  overwriting another segment if necessary), or to terminate execution of the
  9133.  requesting program if this is not possible.
  9134.  
  9135.  The accessed bit (bit 0) of the access byte is provided in both executable
  9136.  and data segment descriptors to support segment usage profiling. Whenever
  9137.  the descriptor is accessed by the 80286 hardware, the A-bit will be set in
  9138.  memory. This applies to selector test instructions (described below) as
  9139.  well as to the loading of a segment register. The reading of the access byte
  9140.  and the restoration of it with the A-bit set is an indivisible operation,
  9141.  i.e., it is performed as a read-modify-write with bus lock. If an operating
  9142.  system develops a profile of segment usage over time, it can recognize
  9143.  segments of low or zero access and choose among these candidates for
  9144.  replacement.
  9145.  
  9146.  When a not-present segment is brought into real memory, the task that
  9147.  requested access to it can continue its execution because all instructions
  9148.  that load a segment register are restartable.
  9149.  
  9150.  Not-present exceptions occur only on segment register load operations, gate
  9151.  accesses, and task switches. The saved instruction pointer refers to the
  9152.  first byte of the violating instruction. All other aspects of the saved
  9153.  machine state are exactly as they were before execution of the violating
  9154.  instruction began. After the fault handler clears up the fault condition and
  9155.  performs an IRET, the program continues to execute. The only external
  9156.  indication of a segment swap is the additional execution time.
  9157.  
  9158.  
  9159.  11.2  Special Segment Attributes
  9160.  
  9161.  
  9162.  11.2.1  Conforming Code Segments
  9163.  
  9164.  Code segments intended for use at potentially different privilege levels
  9165.  need an attribute that permits them to emulate the privilege level of the
  9166.  calling task. Such segments are termed "conforming" segments. Conforming
  9167.  segments are also useful for interrupt-driven error routines that need only
  9168.  be as privileged as the routine that caused the error.
  9169.  
  9170.  A conforming code segment has bit 2 of its access byte set to 1. This means
  9171.  it can be referenced by a CALL or JMP instruction in a task of equal or
  9172.  lesser privilege, i.e., CPL of the task is numerically greater than or equal
  9173.  to DPL of this segment. CPL does not change when executing the conforming
  9174.  code segment. A conforming segment continues to use the stack from the CPL.
  9175.  This is the only case in which the DPL of a code segment can be numerically
  9176.  less than the CPL. If bit 2 is a 0, the segment is not conforming and can be
  9177.  referenced only by a task of CPL = DPL.
  9178.  
  9179.  Inter-segment Returns that refer to conforming code segments use the RPL
  9180.  field of the code selector of the return address to determine the new CPL.
  9181.  The RPL becomes the new CPL if the conforming code segment DPL ≤ RPL.
  9182.  
  9183.  If a conforming segment is readable, it can be read from any privilege
  9184.  level without restriction. This is the only exception to the protection
  9185.  rules. This allows constants to be stored with conforming code. For example,
  9186.  a read-only look-up table can be embedded in a conforming code segment that
  9187.  can be used to convert system-wide logical ID's into character strings that
  9188.  represent those logical entities.
  9189.  
  9190.  
  9191.  11.2.2  Expand-Down Data Segments
  9192.  
  9193.  If bit 2 in the access byte of a data segment is 1, the segment is an
  9194.  expand-down segment. All the offsets that reference such a segment must be
  9195.  strictly greater than the segment limit, as opposed to normal data segments
  9196.  (bit 2 = 0) where all offsets must be less than or equal to the segment
  9197.  limit. Figure 11-1 shows an expand-down segment.
  9198.  
  9199.  The size of the expand down segment can be changed by changing either the
  9200.  base or the limit. An expand down segment with Limit = 0 will have a size of
  9201.  2^(16)-1 bytes. With a limit value of FFFFH, the expand down segment
  9202.  will have a size of 0 bytes. In an expand down segment, the base + offset
  9203.  value should always be greater than the base + limit value. Therefore, a
  9204.  full size segment (2^(16) bytes) can only be obtained by using an expand up
  9205.  segment.
  9206.  
  9207.  The operating system should check the Expand-Down bit when a protection
  9208.  fault indicates that the limit of a data segment has been reached. If the
  9209.  Expand-Down bit is not set, the operating system should increase the segment
  9210.  limit; if it is set, the limit should be lowered. This supplies more room
  9211.  in either case (assuming the segment is not write-protected, i.e., that bit
  9212.  1 is not 0). In some cases, if the operating system can ascertain that there
  9213.  is not enough room to expand the data segment to meet the need that caused
  9214.  the fault, it can move the data segment to a region of memory where there
  9215.  is enough room. See figure 11-2.
  9216.  
  9217.  
  9218.  Figure 11-1.  Expand-Down Segment
  9219.  
  9220.                                  ║             ║
  9221.                                  ║             ║
  9222.                                  ║             ║
  9223.                BASE + FFFEH ────╟─────────────╢─┐
  9224.                                  ║▒▒▒▒▒▒▒▒▒▒▒▒▒║ │
  9225.                                  ║▒▒▒▒▒▒▒▒▒▒▒▒▒║ │
  9226.                                  ║▒▒▒▒▒▒▒▒▒▒▒▒▒║ │
  9227.                                  ║▒▒▒▒▒▒▒▒▒▒▒▒▒║ │
  9228.               BASE + OFFSET ────║▒▒▒▒▒▒▒▒▒▒▒▒▒║ ├─ EXPAND DOWN
  9229.               >BASE + LIMIT      ║▒▒▒▒▒▒▒▒▒▒▒▒▒║ │  SEGMENT
  9230.                                  ║▒▒▒▒▒▒▒▒▒▒▒▒▒║ │
  9231.                                  ║▒▒▒▒▒▒▒▒▒▒▒▒▒║ │
  9232.                                  ║▒▒▒▒▒▒▒▒▒▒▒▒▒║ │
  9233.                BASE + LIMIT ────╟─────────────╢─┘
  9234.                                  ║             ║
  9235.                                  ║             ║
  9236.                                  ║             ║
  9237.                                  ║             ║
  9238.                             ────║── ── ── ── ─║
  9239.                                  ║             ║
  9240.  
  9241.  
  9242.  Figure 11-2.  Dynamic Segment Relocation and Expansion of Segment Limit
  9243.  
  9244.  
  9245.                    ║▒▒▒▒▒▒▒▒▒▒▒▒▒║                      ║▒▒▒▒▒▒▒▒▒▒▒▒▒║
  9246.                    ║▒▒▒▒▒▒▒▒▒▒▒▒▒║                      ║▒▒▒▒▒▒▒▒▒▒▒▒▒║
  9247.                    ║▒▒▒▒▒▒▒▒▒▒▒▒▒║     BASE + 10000H───╟─────────────╢
  9248.                    ║▒▒▒▒▒▒▒▒▒▒▒▒▒║                      ║             ║
  9249.                    ║             ║                      ║    STACK    ║
  9250.                    ║    SEG. B   ║                      ║             ║
  9251.                    ║             ║                      ║             ║
  9252.   BASE + 10000H───╟─────────────╢          NEW BASE───╟─────────────╢
  9253.                    ║             ║        + NEW LIMIT   ║    SEG. B   ║
  9254.                    ║    STACK    ║                      ║             ║
  9255.                    ║             ║          NEW BASE───║▒▒▒▒▒▒▒▒▒▒▒▒▒║
  9256.                    ║             ║                      ║▒▒▒▒▒▒▒▒▒▒▒▒▒║
  9257.        OLD BASE───╟─────────────╢                      ║▒▒▒▒▒▒▒▒▒▒▒▒▒║
  9258.      + OLD LIMIT   ║             ║                      ║             ║
  9259.                    ║    SEG. A   ║                      ║    SEG. A   ║
  9260.                    ║             ║                      ║             ║
  9261.        OLD BASE───║▒▒▒▒▒▒▒▒▒▒▒▒▒║                      ║▒▒▒▒▒▒▒▒▒▒▒▒▒║
  9262.                    ║▒▒▒▒▒▒▒▒▒▒▒▒▒║                      ║▒▒▒▒▒▒▒▒▒▒▒▒▒║
  9263.                    ║▒▒▒▒▒▒▒▒▒▒▒▒▒║                      ║▒▒▒▒▒▒▒▒▒▒▒▒▒║
  9264.                    ║▒▒▒▒▒▒▒▒▒▒▒▒▒║                      ║▒▒▒▒▒▒▒▒▒▒▒▒▒║
  9265.  
  9266.  
  9267.  11.3  Pointer Validation
  9268.  
  9269.  Pointer validation is an important part of locating programming errors.
  9270.  Pointer validation is necessary for maintaining isolation between the
  9271.  privilege levels. Pointer validation consists of the following steps:
  9272.  
  9273.    1.  Check if the supplier of the pointer is entitled to access the
  9274.        segment.
  9275.  
  9276.    2.  Check if the segment type is appropriate to its intended use.
  9277.  
  9278.    3.  Check if the pointer violates the segment limit.
  9279.  
  9280.  The 80286 hardware automatically performs checks 2 and 3 during instruction
  9281.  execution, while software must assist in performing the first check. This
  9282.  point is discussed in section 11.3.2. Software can explicitly perform steps
  9283.  2 and 3 to check for potential violations (rather than causing an
  9284.  exception). The unprivileged instructions LSL, LAR, VERR, and VERW are
  9285.  provided for this purpose.
  9286.  
  9287.  The load access rights (LAR) instruction obtains the access rights byte of
  9288.  a descriptor pointed to by the selector used in the instruction. If that
  9289.  selector is visible at the CPL, the instruction loads the access byte into
  9290.  the specified destination register as the higher byte (the low byte is
  9291.  zero) and the zero flag is set. Once loaded, the access bits can be tested.
  9292.  System segments such as a task state segment or a descriptor table cannot be
  9293.  read or modified. This instruction is used to verify that a pointer refers
  9294.  to a segment of the proper privilege level and type. If the RPL or CPL is
  9295.  greater than DPL, or the selector is outside the table limit, no access
  9296.  value is returned and the zero flag is cleared. Conforming code segments may
  9297.  be accessed from any RPL or CPL.
  9298.  
  9299.  Additional parameter checking can be performed via the load segment limit
  9300.  (LSL) instruction. If the descriptor denoted by the given selector (in
  9301.  memory or a register) is visible at the CPL, LSL loads the specified
  9302.  register with a word that consists of the limit field of that descriptor.
  9303.  This can only be done for segments, task state segments, and local
  9304.  descriptor tables (i.e., words from control descriptors are inaccessible).
  9305.  Interpreting the limit is a function of the segment type. For example,
  9306.  downward expandable data segments treat the limit differently than code
  9307.  segments do.
  9308.  
  9309.  For both LAR and LSL, the zero flag (ZF) is set if the loading was
  9310.  performed; otherwise, the zero flag is cleared. Both instructions are
  9311.  undefined in real address mode, causing an invalid opcode exception
  9312.  (interrupt #6).
  9313.  
  9314.  
  9315.  11.3.1  Descriptor Validation
  9316.  
  9317.  The 80286 has two instructions, VERR and VERW, which determine whether a
  9318.  selector points to a segment that can be read or written at the current
  9319.  privilege level. Neither instruction causes a protection fault if the result
  9320.  is negative.
  9321.  
  9322.  VERR verifies a segment for reading and loads ZF with 1 if that segment is
  9323.  readable from the current privilege level. The validation process checks
  9324.  that: 1) the selector points to a descriptor within the bounds of the GDT or
  9325.  LDT, 2) it denotes a segment descriptor (as opposed to a control
  9326.  descriptor), and 3) the segment is readable and of appropriate privilege
  9327.  level. The privilege check for data segments and non-conforming code
  9328.  segments is that the DPL must be numerically greater than or equal to both
  9329.  the CPL and the selector's RPL. Conforming segments are not checked for
  9330.  privilege level.
  9331.  
  9332.  VERW provides the same capability as VERR for verifying writability. Like
  9333.  the VERR instruction, VERW loads ZF if the result of the writability check
  9334.  is positive. The instruction checks that the descriptor is within bounds, is
  9335.  a segment descriptor, is writable, and that its DPL is numerically greater
  9336.  than or equal to both the CPL and the selector's RPL. Code segments are
  9337.  never writable, conforming or not.
  9338.  
  9339.  
  9340.  11.3.2  Pointer Integrity: RPL and the "Trojan Horse Problem"
  9341.  
  9342.  The Requested Privilege Level (RPL) feature can prevent inappropriate use
  9343.  of pointers that could corrupt the operation of more privileged code or data
  9344.  from a less privileged level.
  9345.  
  9346.  A common example is a file system procedure, FREAD (file_id, nybytes,
  9347.  buffer-ptr). This hypothetical procedure reads data from a file into a
  9348.  buffer, overwriting whatever is there. Normally, FREAD would be available at
  9349.  the user level, supplying only pointers to the file system procedures and
  9350.  data located and operating at a privileged level. Normally, such a procedure
  9351.  prevents user-level procedures from directly changing the file tables.
  9352.  However, in the absence of a standard protocol for checking pointer
  9353.  validity, a user-level procedure could supply a pointer into the file
  9354.  tables in place of its buffer pointer, causing the FREAD procedure to
  9355.  corrupt them unwittingly.
  9356.  
  9357.  By using the RPL, you can avoid such problems. The RPL field allows a
  9358.  privilege attribute to be assigned to a selector. This privilege attribute
  9359.  would normally indicate the privilege level of the code which generated the
  9360.  selector. The 80286 hardware will automatically check the RPL of any
  9361.  selector loaded into a segment register or a control register to see if the
  9362.  RPL allows access.
  9363.  
  9364.  To guard against invalid pointers, the called procedure need only ensure
  9365.  that all selectors passed to it have an RPL at least as high (numerically)
  9366.  as the original caller's CPL. This indicates that the selectors were not
  9367.  more trusted than their supplier. If one of the selectors is used to access
  9368.  a segment that the caller would not be able to access directly, i.e., the
  9369.  RPL is numerically greater than the DPL, then a protection fault will result
  9370.  when loaded into a segment or control register.
  9371.  
  9372.  The caller's CPL is available in the CS selector that was pushed on the
  9373.  stack as the return address. A special instruction, ARPL, can be used to
  9374.  appropriately adjust the RPL field of the pointer. ARPL (Adjust RPL field of
  9375.  selector instruction) adjusts the RPL field of a selector to become the
  9376.  larger of its original value and the value of the RPL field in a specified
  9377.  register. The latter is normally loaded from the caller's CS register which
  9378.  can be found on the stack. If the adjustment changes the selector's RPL, ZF
  9379.  is set; otherwise, the zero flag is cleared.
  9380.  
  9381.  
  9382.  11.4  NPX Context Switching
  9383.  
  9384.  The context of a processor extension (such as the 80287 numerics processor)
  9385.  is not changed by the task switch operation. A processor extension context
  9386.  need only be changed when a different task attempts to use the processor
  9387.  extension (which still contains the context of a previous task). The 80286
  9388.  detects the first use of a processor extension after a task switch by
  9389.  causing the processor extension not-present exception (#7) if the TS bit is
  9390.  set. The interrupt handler may then decide whether a context change is
  9391.  necessary.
  9392.  
  9393.  The 286 services numeric errors only when it executes wait or escape
  9394.  instructions because the processor extension is running independently.
  9395.  Therefore, the numerics error from one task may not be recorded until the
  9396.  286 is running a different task. If the 286 task has changed, it makes
  9397.  sense to defer handling that error until the original task is restored. For
  9398.  example, interrupt handlers that use the NPX should not have their timing
  9399.  upset by a numeric error interrupt that pertains to some earlier process.
  9400.  It is of little value to service someone else's error.
  9401.  
  9402.  If the task switch bit is set (bit 3 of MSW) when the CPU begins to execute
  9403.  a wait or escape instruction, the processor-extension not-present exception
  9404.  results (#7). The handler for this interrupt must know who currently "owns"
  9405.  the NPX, i.e., the handler must know the last task to issue a command to the
  9406.  NPX. If the owner is the same as the current task, then it was merely
  9407.  interrupted and the interrupt handler has since returned; the handler for
  9408.  interrupt 7 simply clears the TS bit, restores the working registers, and
  9409.  returns (restoring interrupts if enabled).
  9410.  
  9411.  If the recorded owner is different from the current task, the handler must
  9412.  first save the existing NPX context in the save area of the old task. It can
  9413.  then re-establish the correct NPX context from the current task's save area.
  9414.  
  9415.  The code example in figure 11-3 relies on the convention that each TSS
  9416.  entry in the GDT is followed by an alias entry for a data segment that
  9417.  points to the same physical region of memory that contains the TSS. The
  9418.  alias segment also contains an area for saving the NPX context, the kernel
  9419.  stack, and certain kernel data. That is, the first 44 bytes in that segment
  9420.  are the 286 context, followed by 94 bytes for the processor extension
  9421.  context, followed in some cases by the kernel stack and kernel private data
  9422.  areas.
  9423.  
  9424.  The implied convention is that the stack segment selector points to this
  9425.  data segment alias so that whenever there is an interrupt at level zero and
  9426.  SS is automatically loaded, all of the above information is immediately
  9427.  addressable.
  9428.  
  9429.  It is assumed that the program example knows about only one data segment
  9430.  that points to a global data area in which it can find the one word NPX
  9431.  owner to begin the processing described. The specific operations needed, and
  9432.  shown in the figure, are listed in table 11-1.
  9433.  
  9434.  
  9435.  Table 11-1. NPX Context Switching
  9436.  
  9437.  Step       Operation                                Lines
  9438.                                                      (Figure 11-3)
  9439.  
  9440.  1.         Save the working registers                28, 29
  9441.  2.         Set up address for kernel work area       30, 31
  9442.  3.         Get current task ID from Task Register    32
  9443.  4.         Clear Task Switch flag to allow NPX work  34
  9444.  5.         Inhibit interrupts                        35
  9445.  6.         Compare owner with current task ID        37
  9446.  
  9447.  If same owner:
  9448.  7a.        Restore working registers                 48, 49
  9449.  7b.        and return                                50
  9450.  
  9451.  If owner is not current task:
  9452.  8a.        Use owner ID to save old context
  9453.             in its TSS                                42, 43, 44
  9454.  8b.        Restore context of current task;          45
  9455.             restore working registers;                46
  9456.             and return                                52
  9457.  
  9458.  Figure 11-3. Example of NPX Context Switching
  9459.  
  9460.  ASSEMBLER INVOKED BY:   ASM286,86  :FS:SWNPX.A86
  9461.  
  9462.  LOC  OBJ                  LINE       SOURCE
  9463.                               1  + 1  $title('Switch the NPX Context on First
  9464.                               2
  9465.                               3                                name    switch_
  9466.                               4
  9467.                               5                                public  switch_
  9468.                               6                                extrn   last_np
  9469.                               7       ;
  9470.                               8       ;          This interrupt handler will s
  9471.                               9       ;        is attempting to use the NPX co
  9472.                              10       ;        switch.  If the NPX context bel
  9473.                              11       ;
  9474.                              12       ;        A trap gate should be placed in
  9475.                              13       ;        The DPL of the gate should be 0
  9476.                              14       ;        must be at privilege level 0.
  9477.                              15       ;
  9478.                              16       ;        The kernel stack is assumed to
  9479.                              17       ;        is placed at the end of the TSS
  9480.                              18       ;
  9481.                              19       ;        A global word variable LAST_NPX
  9482.                              20       ;        the last task to use the NPX.
  9483.                              21       ;
  9484.    002C                      22       npx_save_area             equ     word p
  9485.                              23 + 1   $eject
  9486.  ∙∙∙∙                        24       kernal_code               segment er pub
  9487.                              25
  9488.  0000                        26       switch_npx_context        proc    far wc
  9489.                              27
  9490.  0000 50                     28                push    ax
  9491.  0001 1E                     29                push    ds
  9492.  0002 B8∙∙∙∙         E       30                mov     ax,seg last_npx_task
  9493.  0005 8ED8                   31                mov     ds,ax
  9494.  0007 0F00C8                 32                str     ax
  9495.  000A 24FC                   33                and     al,not 3
  9496.  000C 0F06                   34                clts
  9497.  000E FA                     35                cli
  9498.                              36       ;
  9499.                              37       ;        Last_npx_word cannot change due
  9500.                              38       ;
  9501.  000F 3B060000       E       39                cmp     ax,ds:last_npx_task
  9502.  0013 7412                   40                je      same_task
  9503.                              41
  9504.  0015 87060000       E       42                xchg    ax,ds:last_npx_task
  9505.  0019 050800                 43                add     ax,8
  9506.  001C 8ED8                   44                mov     ds,ax
  9507.  001E DD362C00               45                fsave   ds:npx_save_area
  9508.  0022 36DD262C00             46                frstor  ss:npx_save_area
  9509.  0027                        47       same_task:
  9510.  0027 1F                     48                pop     ds
  9511.  0028 58                     49                pop     ax
  9512.  0029 CF                     50                iret
  9513.                              51
  9514.                              52       switch_npx_context        endp
  9515.                              53
  9516.  - - - -                     54       kernel_code               ends
  9517.  ***  WARNING #160, LINE #54, SEGMENT CONTAINS PRIVILEGED INSTRUCTIONS
  9518.                              55                                 end
  9519.  
  9520.  
  9521.  11.5  Multiprocessor Condiderations
  9522.  
  9523.  As mentioned in Chapter 8, a bus lock is applied during the testing and
  9524.  setting of the task busy bit to ensure that two processors do not invoke the
  9525.  same task at the same time. However, protection traps and conflicting use of
  9526.  dynamically varying segments or descriptors must be addressed by an
  9527.  inter-processor synchronization protocol. The protocol can use the
  9528.  indivisible semaphore operation of the base instruction set. Coordination of
  9529.  interrupt and trap vectoring must also be addressed when multiple concurrent
  9530.  processors are operating.
  9531.  
  9532.  The interrupt bus cycles are locked so no interleaving occurs on those
  9533.  cycles. Descriptor caching is locked so that a descriptor reference cannot
  9534.  be altered while it is being fetched.
  9535.  
  9536.  When a program changes a descriptor that is shared with other processors,
  9537.  it should broadcast this fact to the other processors. This broadcasting can
  9538.  be done with an inter-processor interrupt. The handler for this interrupt
  9539.  must ensure that the segment registers, the LDTR and the TR, are re-loaded.
  9540.  This happens automatically if the interrupt is serviced by a task switch.
  9541.  
  9542.  Modification of descriptors of shared segments in multi-processor systems
  9543.  may require that the on-chip descriptors also be updated. For example, one
  9544.  processor may attempt to mark the descriptor of a shared segment as
  9545.  not-present while another is using it. Software has to ensure that the
  9546.  descriptors in the segment register caches are updated with the new
  9547.  information. The segment register caches can be updated by a re-entrant
  9548.  procedure that is invoked by an inter-processor interrupt. The handler must
  9549.  ensure that the segment registers, the LDTR and the TR, are re-loaded. This
  9550.  happens automatically if the interrupt is serviced by a task switch.
  9551.  
  9552.  
  9553.  11.6  Shutdown
  9554.  
  9555.  Shutdown occurs when a severe error condition prevents further processing.
  9556.  Shutdown is very similar to HLT in that the 80286 stops executing
  9557.  instructions. The 80286 externally signals shutdown as a Halt bus cycle with
  9558.  A1=0. The NMI or RESET input will force the 80286 out of shutdown. The INTR
  9559.  input is ignored during shutdown.
  9560.  
  9561.  
  9562.  Appendix A  80286 System Initialization
  9563.  
  9564.  ───────────────────────────────────────────────────────────────────────────
  9565.  
  9566.  $title('Switch the 80286 from Real Address Mode to Protected Mode')
  9567.                   name    switch 80286_modes
  9568.                  public   idt_desc,gdt_desc
  9569.  ;
  9570.  ;     Switch the 80286 from real address mode into protected mode.
  9571.  ;   The initial EPROM GDT, IDT, TSS, and LDT (if any) constructed by BLD286
  9572.  ;   will be copied from EPROM into RAM.  The RAM areas are defined by data
  9573.  ;   segments allocated as fixed entries in the GDT. The CPU registers for
  9574.  ;   GDT, IDT, TSS, and LDT will be set to point at the RAM-based
  9575.  ;   segments. The base fields in the RAM-based GDT will also be updated to
  9576.  ;   point at the RAM-based segments.
  9577.  ;
  9578.  ;     This code is used by adding it to the list of object modules given
  9579.  ;   to BLD286.  BLD286 must then be told to place the setment
  9580.  ;   init_code at address FFFE10H. Execution of the mode switch code begins
  9581.  ;   after RESET. This happens because the mode switch code will start at
  9582.  ;   physical address FFFFF0H, which is the power up address. This code then
  9583.  ;   sets up RAM copies of the EPROM-based segments before jumping to the
  9584.  ;   initial tsk placed at a fixed GDT entry. After the jump, the CPU
  9585.  ;   executes in the state of the first task defined by BLD286.
  9586.  ;
  9587.  ;     This code will not use any of the EPROM-based tables directly.
  9588.  ;   Such use would result in the 80286 writing into EPROM to set
  9589.  ;   the A bit. Any use of a GDT or TSS will always be in the RAM copy.
  9590.  ;   The limit and size of the EPROM-based GDT and IDT must be stored at
  9591.  ;   the public symbols idt_desc and gdt_desc. The location commands of BLD286
  9592.  ;   provide this function.
  9593.  ;
  9594.  ;     Interrupts are disabled during this mode switching code. Full error
  9595.  ;   checking is made of the EPROM-based GDT, IDT, TSS, and LDT to assure
  9596.  ;   they are valid before copying them to RAM.  If any of the RAM-based
  9597.  ;   aslias segments are smaller than the EPROM segments they are to hold,
  9598.  ;   halt or shutdown will occur. In general, any exception or NMI will
  9599.  ;   cause shutdown to occur until the first task is invoked.
  9600.  ;
  9601.  ;     If the RAM segment is larger than the EPROM segment, the RAM segment
  9602.  ;   will be expanded with zeros. If the initial TSS specifies an LDT,
  9603.  ;   the LDT will also be copied into ldt_alias with zero fill if needed.
  9604.  ;   The IPROM-based or RAM-based GDT, IDT, TSS, and LDT segments may be locat
  9605.  ;   anywhere in physical memory.
  9606.  ;
  9607.  ;
  9608.  ;            Define layout of a descriptor.
  9609.  ;
  9610.  desc              struc
  9611.  limit             dw      0               ; Offset of last byte in segment
  9612.  base_low          dw      0               ; Low 16 bits of 24-bit address
  9613.  base_high         db      0               ; High 8 bits of 24-bit address
  9614.  access            db      0               ; Access rights byte
  9615.  res               dw      0               ; Reserved word
  9616.   desc             ends
  9617.  ;
  9618.  ;          Define the fixed GDT selector values for the descriptors that
  9619.  ;        define the EPROM-based tables.  BLD286 must be instructed to place t
  9620.  ;        appropriate descriptors into the GDT.
  9621.  ;
  9622.  gdt_alias         equ     1*size desc     ; GDT(1) is data segment in RAM for
  9623.  idt_alias         equ     2*size desc     ; GDT(2) is data segment in RAM for
  9624.  start_TSS_alias   equ     3*size desc     ; GDT(3) is data segment in RAM for
  9625.  start_task        equ     4*size desc     ; GDT(4) is TSS for starting task
  9626.  start_LDT_alias   equ     5*size desc     ; GDT(5) is data segment in RAM for
  9627.  ;
  9628.  ;          Define machine status word bit positions.
  9629.  ;
  9630.  PE                equ     1               ; Protection enable
  9631.  MP                equ     2               ; Monitor processor extension
  9632.  EM                equ     4               ; Emulate processor extension
  9633.  ;
  9634.  ;          Define particular values of descriptor access rights byte.
  9635.  ;
  9636.  DT_ACCESS         equ     82H             ; Access byte value for an LDT
  9637.  DS_ACCESS         equ     92H             ; Access byte value for data segmen
  9638.                                            ;  which is grow up, at level 0, wr
  9639.  TSS_ACCESS        equ     81H             ; Access byte value for an idle TSS
  9640.  DPL               equ     60H             ; Privilege level field of access r
  9641.  ACCESSED          equ     1               ; Define accessed bit
  9642.  TI                equ     4               ; Position of TI bit
  9643.  TSS_SIZE          equ     44              ; Size of a TSS
  9644.  LDT_OFFSET        equ     42              ; Position of LDT in TSS
  9645.  TIRPL_MASK        equ     size desc-1     ; TI and RPL field mask
  9646.  ;
  9647.  ;          Pass control from the power-up address to the mode switch code.
  9648.  ;        The segment containing this code must be at physical address FFFE10H
  9649.  ;        to place the JMP instruction at physical address FFFFF0H.  The base
  9650.  ;        address is chosen according to the size of this segment.
  9651.  ;
  9652.  init_code         segment er
  9653.  
  9654.  cs_offset         equ     0FE10H          ; Low 16 bits of starting address
  9655.                    org     0FFF0H-cs_offset; Start at address FFFFF0H
  9656.                    jmp     reset_startup   ; Do not change CS!
  9657.  ;
  9658.  ;          Define the template for a temporary GDT used to locate the initial
  9659.  ;        GDT and stack.  This data will be copied to location 0.
  9660.  ;        This space is also used for a temporary stack and finally serves
  9661.  ;        as the TSS written into when entering the initial TSS.
  9662.  ;
  9663.                    org 0   ; Place remaining code below power_up
  9664.  
  9665.  initial_gdt       desc    <>              ; Filler and null IDT descriptor
  9666.  gdt_desc          desc    <>              ; Descriptor for EPROM GDT
  9667.  idt_desc          desc    <>              ; Descriptor for EPROM IDT
  9668.  temp_desc         desc    <>              ; Temporary descriptor
  9669.  ;
  9670.  ;          Define a descriptor that will point the GDT at location 0.
  9671.  ;        This descriptor will also be loaded into SS to define the initial
  9672.  ;        protected mode stack segment.
  9673.  ;
  9674.  temp_stack        desc    <end_gdt-initial_gdt-1,0,0,DS_ACCESS,0>
  9675.  ;
  9676.  ;          Define the TSS descriptor used to allow the task switch to the
  9677.  ;        first task to overwrite this region of memory.  The TSS will overlay
  9678.  ;        the initial GDT and stack at location 0.
  9679.  ;
  9680.  save_tss          desc    <end_gdt-initial_gdt-1,0,0,TSS_ACCESS,0>
  9681.  ;
  9682.  ;          Define the initial stack space and filler for the end of the TSS.
  9683.  ;
  9684.                    dw       8 dup (0)
  9685.  end_gdt           label    word
  9686.  
  9687.  start_pointer     label    dword
  9688.                    dw       0,start_task   ; Pointer to initial task
  9689.  ;
  9690.  ;          Define template for the task definition list.
  9691.  ;
  9692.  task_entry        struc                   ; Define layout of task description
  9693.  TSS_sel           dw       ?              ; Selector for TSS
  9694.  TSS_alias         dw       ?              ; Data segment alias for TSS
  9695.  LDT_alias         dw       ?              ; Data segment alias for LDT if any
  9696.  task_entry        ends
  9697.  
  9698.  task_list         task_entry       <start_task,start_TSS_alias,start_LDT_alia
  9699.                    dw  0                   ; Terminate list
  9700.  
  9701.  reset_startup:
  9702.           cli                              ; No interrupts allowed!
  9703.           cld                              ; Use autoincrement mode
  9704.           xor      di,di                   ; Point ES:DI at physical address 0
  9705.           mov      ds,di
  9706.           mov      es,di
  9707.           mov      ss,di                   ; Set stack at end of reserved area
  9708.           mov      sp,end_gdt-initial_gdt
  9709.  ;
  9710.  ;
  9711.  ;          Form an adjustment factor from the real CS base of FF0000H to the
  9712.  ;        segment base address assumed by ASM286.  Any data reference mode
  9713.  ;        into CS must add an indexing term [BP] to compensate for the differe
  9714.  ;        between the offset generated by ASM286 and the offset required from
  9715.  ;        the base of FF0000H.
  9716.  ;
  9717.  start    proc                             ; The value of IP at run time will
  9718.                                            ;  the same as the one used by ASM2
  9719.           call     start1                  ; Get true offset of start1
  9720.  start1:
  9721.           pop      bp
  9722.           sub      bp, offset start1       ; Subtract ASM286 offset of start1
  9723.                                            ;  leaving adjustment factor in BP
  9724.           lidt     initial_gdt[bp]         ; Setup null IDT to force shutdown
  9725.                                            ;  on any protection error or inter
  9726.  ;
  9727.  ;          Copy the EPROM-based temporary GDT into RAM.
  9728.  ;
  9729.           lea      si,initial_gdt[bp]      ; Setup pointer to temporary GDT
  9730.                                            ;  template in EPROM
  9731.           mov      cs,(end_gdt-initial_gdt)/2  ; Set length
  9732.   rep     movs     es:word ptr [di],cs:[si]; Put into reserved RAM area
  9733.  ;
  9734.  ;          Look for 80287 processor extension.  Assume all ones will be read
  9735.  ;        if an 80287 is not present.
  9736.  ;
  9737.           fninit                           ; Initialize 80287 if present
  9738.           mov      bx,EM                   ; Assume no 80287
  9739.           fstsw    ax                      ; Look at status of 80287
  9740.           or       al,al                   ; No errors should be present
  9741.           jnz      set_mode                ; Jump if no 80287
  9742.  
  9743.           fsetpm                           ; Put 80287 into protected mode
  9744.           mov      bx,MP
  9745.  ;
  9746.  ;          Switch to protected mode and setup a stack, GDT, and LDT.
  9747.  ;
  9748.  set_mode;
  9749.           smsw     ax                      ; Get current MSW
  9750.           or       ax,PE                   ; Set PE bit
  9751.           or       ax,bx                   ; Set NPX status flags
  9752.           lmsw     ax                      ; nter protected mode!
  9753.           jmp      $+2                     ; Clear queue of instructions decod
  9754.                                            ;  while in Real Address Mode
  9755.                                            ; CPL is now 0, CS still points at
  9756.                                            ; FFFE10 in physical memory
  9757.          lgdt     temp_stack[bp]          ; Use initial GDT in RAM area
  9758.           mov      ax,temp_stack-initial_gdt   ; Setup SS with valid protected
  9759.           mov      ss,ax                   ; selector to the RAM GDT and stack
  9760.           xor      ax,ax                   ; Set the current LDT to null
  9761.           lidt     ax                      ; Any references to it will cause
  9762.                                            ;  an exception causing shutdown
  9763.           mov      ax,save_tss-initial_gdt ; Set initial TSS into the low RAM
  9764.                    ltr ax                  ; The task switch needs a valid TSS
  9765.  ;
  9766.  ;          Copy the EPROM-based GDT into the RAM data segment alias.
  9767.  ;        First the descriptor for the RAM data segment must be copied into
  9768.  ;        the temporary GDT.
  9769.  ;
  9770.           mov      ax,gdt_desc[bp].limit   ; Get size of GDT
  9771.           cmp      ax,6*size desc-1        ; Be sure the last entry expected b
  9772.                                            ; this code is inside the GDT
  9773.           jb       bad_gdt                 ; Jump if GDT is not big enough
  9774.  
  9775.           mov      bx,gdt_desc-initial_gdt ; Form selector to EPROM GDT
  9776.           mov      si,gdt_alias            ; Get selector of GDT alias
  9777.           call     copy_EPROM_dt           ; Copy into EPROM
  9778.           mov      si,idt_alias            ; Get selector of IDT alias
  9779.           mov      bx,ldt_desc-initial_gdt ; Indicate EPROM IDT
  9780.           call     copy_EPROM_dt
  9781.           mov      ax,gdt_desc-initial_gdt ; Setup addressing into EPROM GDT
  9782.           mov      ds,ax
  9783.           mov      bx,gdt_alias            ; Get GDT alias data segment select
  9784.           lgdt     [bx]                    ; Set GDT to RAM GDT
  9785.                                            ; SS and TR remain in low RAM
  9786.  ;
  9787.  ;          Copy all task's TSS and LDT segments into RAM
  9788.  ;
  9789.           lea     bx,task_list[bp]         ; Define list of tasks to setup
  9790.  copy_task_loop:
  9791.           call    copy_tasks               ; Copy them into RAM
  9792.           add     bx,size task_entry       ; Go to next entry
  9793.           mov     ax,cs:[bx].tss_sel       ; See if there is another entry
  9794.           or      ax,ax
  9795.           jnz     copy_task_loop
  9796.  ;
  9797.  ;          With TSS, GDT, and LDT set, startup the initial task!
  9798.  ;
  9799.           mov     bx,gdt_alias             ; Point DS at GDT
  9800.           mov     ds,bx
  9801.           mov     bx,idt_alias             ; Get IDT alias data segment select
  9802.           lidt    [bx]                     ; Start the first task!
  9803.           jmp     start_pointer[bp]        ; The low RAM area is overwritten w
  9804.                                            ;  the current CPU context
  9805.  
  9806.  bad_gdt:                                  ; Wait here if GDT is not big enoug
  9807.           hlt
  9808.  astart    endp
  9809.  ;
  9810.  ;          Copy the TSS and LDT for the task pointed at by CS:BX.
  9811.  ;        If the task has an LDT it will also be copied down.
  9812.  ;        BX and BP are transparent.
  9813.  ;
  9814.  bad_tss:
  9815.           hlt                              ; Halt here if TSS is invalid
  9816.  copy_tasks       proc
  9817.  
  9818.           mov     si,gdt_alias             ; Get addressability to GDT
  9819.           mov     ds,si
  9820.           mov     si,cs:[bx].tss_alias     ; Get selector for TSS alias
  9821.           mov     es,si                    ; Point ES at alias data segment
  9822.           lsl     ax,si                    ; Get length of TSS alias
  9823.           mov     si,cs:[bx].tss_sel       ; Get TSS selector
  9824.           lar     dx,si                    ; Get alias access rights
  9825.           jnz     bad_tss                  ; Jump if invalid reference
  9826.  
  9827.           mov     dl,dh                    ; Save TSS descriptor access byte
  9828.           and     dh,not DPL               ; Ignore privilege
  9829.           cmp     dh,TSS_ACCESS            ; See if TSS
  9830.           jnz     bad_tss                  ; Jump if not
  9831.  
  9832.           lsl     cs,si                    ; Get length of EPROM based TSS
  9833.           cmp     cs,TSS_SIZE-1            ; Verify it is of proper size
  9834.           jb      bad_tss                  ; Jump if it is not big enough
  9835.  ;
  9836.  ;          Setup for moving the EPROM-based TSS to RAM
  9837.  ;        DS points at GDT
  9838.  ;
  9839.           mov     [si].access,DS_ACCESS    ; Make TSS into data segment
  9840.           mov     ds,si                    ; Point DS at EPROM TSS
  9841.           call    copy_with_fill           ; Copy DS segment to ES with zero f
  9842.                                            ;  CX has copy count, AX-CX fill co
  9843.  ;
  9844.  ;          Set the GDT TSS limit and base address to the RAM values
  9845.  ;
  9846.           mov     ax,gdt_alias             ; Restore GDT addressing
  9847.           mov     ds,ax
  9848.           mov     es,ax
  9849.           mov     di,cs:[bx].tss_sel       ; Get TSS selector
  9850.           mov     si,cs:[bx].tss_alias     ; Get RAM alias selector
  9851.           movsw                            ; Copy limit
  9852.           movsw                            ; Copy low 16 bits of adress
  9853.           lodsw                            ; Get high 8 bits of address
  9854.           mov     ah,dl                    ; Mark as TSS descriptor
  9855.           stosw                            ; Fill in high address and access b
  9856.           movsw                            ; Copy reserved word
  9857.  ;
  9858.  ;          See if a valid LDT is specified for the startup task
  9859.  ;        If so then copy the EPROM version into the RAM alias.
  9860.  ;
  9861.           mov     ds,cs:[bx].tss_alias     ; Address TSS to get LDT
  9862.           mov     si,ds:word ptr LDT_OFFSET
  9863.           and     si,not TIRPL_MASK        ; Ignore TI and RPL
  9864.           jz      no_ldt                   ; Skip this if no LDT used
  9865.  
  9866.           push    si                       ; Save LDT selector
  9867.           lar     dx,si                    ; Test descriptor
  9868.           jnz     bad_ldt                  ; Jump if invalid selector
  9869.  
  9870.           mov     dl,dh                    ; Save LDT descriptor access byte
  9871.           and     dh,not DPL               ; Ignore privilege
  9872.           cmp     dh, DT_ACCESS            ; Be sure it is an LDT descriptor
  9873.           jne     bad_ldt                  ; Jump if invalid
  9874.  
  9875.           mov     es:[si].access,DS_ACCESS ; Mark LDT as data segment
  9876.           mov     ds,si                    ; Point DS at EPROM LDT
  9877.           lsl     ax,si                    ; Get LDT limit
  9878.           call    test_dt_limit            ; Verify it is valid
  9879.           mov     cx,ax                    ; Save for later
  9880.  
  9881.  ;
  9882.  ;          Examine the LDT alias segment and, if good, copy to RAM
  9883.  ;
  9884.           mov si,cs:[bx].ldt_alias         ; Get ldt alias selector
  9885.           mov     es,si                    ; Point ES at alias segment
  9886.           lsl     ax,si                    ; Get length of alias segmewnt
  9887.           call    test_dt_limit            ; Verify it is valid
  9888.           call    copy_with_fill           ; Copy LDT into RAM alias segment
  9889.  ;
  9890.  ;          Set the LDT limit and base address to the RAM copy of the LDT.
  9891.  ;
  9892.           mov     si,cs:[bx].ldt_alias     ; Restore LDT alias selector
  9893.           pop     di                       ; Restore LDT selector
  9894.           mov     ax,gdt_alias             ; Restore GDT addressing
  9895.           mov     ds,ax
  9896.           mov     es,ax
  9897.           movsw                            ; Move the RAM LDT limit
  9898.           movsw                            ; Move the low 16 bits across
  9899.           lodsw                            ; Get the high 8 bits
  9900.           mov     ah,dl                    ; Set high address and access right
  9901.           stosw                            ; Copy reserved word
  9902.           movsw
  9903.  no_ldt:
  9904.           ret                              ; All done
  9905.  bad_ldt:
  9906.           hit                              ; Halt here if LDT is invalid
  9907.  copy_tasks       endp
  9908.  
  9909.  ;
  9910.  ;          Test the descriptor table size in AX to verify that it is an
  9911.  ;        even number of descriptors in length.
  9912.  ;
  9913.  test_dt_limit    proc
  9914.  
  9915.           push    ax                       ; Save length
  9916.           end     al,7                     ; Look at low order bits
  9917.           cmp     al,7                     ; Must be all ones
  9918.           pop     ax                       ; Restore length
  9919.           jne     bad_dt_limit;
  9920.  
  9921.           ret                              ; All OK
  9922.  bad_dt_limit:
  9923.           hit:                             ; Die!
  9924.  
  9925.  test_dt_limit    endp
  9926.  
  9927.  ;
  9928.  ;          Copy the EPROM DT at selector BX in the temporary GDT to the alias
  9929.  ;        data segment at selector SI.  Any improper descriptors or limits
  9930.  ;        will cause shutdown!
  9931.  ;
  9932.  copy_EPROM_dt    proc
  9933.  
  9934.           mov     ax,ss                    ; Point ES:DI at temporary descript
  9935.           mov     es,ax
  9936.           mov     es:[bx].access,DS_ACCESS ; Mark descriptor as a data segment
  9937.           mov     es:[bx].res,0            ; Clear reserved word
  9938.           lsl     ax,bx                    ; Get limit of EPROM DT
  9939.           mov     cx,ax                    ; Save for later
  9940.           call    test_dt_limit            ; Verify it is a proper limit
  9941.           mov     di,gdt_desc-initial_gdt  ; Address EPROM GDT in DS
  9942.           mov     ds,di
  9943.           mov     di,temp_desc-initial_gdt ; Get selector for temporary descri
  9944.           push    di                       ; Save offset for later use as sele
  9945.           lodsw                            ; Get alias segment size
  9946.           call    test_dt_limit            ; Verify it is an even multiple of
  9947.                                            ;  descriptors in length
  9948.           stosw                            ; Put length into temporary
  9949.           movsw                            ; Copy remaining entries into tempo
  9950.           movsw
  9951.           movsw
  9952.           pop     es                       ; ES now points at the GDT alias ar
  9953.           mov     ds,bx                    ; DS now points at EPROM DT as data
  9954.                                            ; Copy segment ot alias with zero f
  9955.                                            ; CX is copy count, AX-CX is fill c
  9956.                                            ; Fall into copy_with_fill
  9957.  
  9958.  copy_EPROM_dt    endp
  9959.  
  9960.  ;
  9961.  ;          Copy the segment at DS to the segment at ES for length CX.
  9962.  ;        Fill th end with AX-CX zeros.s  Use word operations for speed but
  9963.  ;        allow odd byte operations.
  9964.  ;
  9965.  copy_with_fill   proc
  9966.  
  9967.           xor     si,si                    ; Start at beginning of segments
  9968.           xor     di,di
  9969.           sub     ax,cx                    ; Form fill count
  9970.           add     cs,1                     ; Convert laimit to count
  9971.           rcr     cs,1                     ; Allow full 64K move
  9972.      rep movsw                             ; Copy DT into alias area
  9973.           xchg    ax,cx                    ; Get fill count and zero AX
  9974.           jnc     even_copy                ; Jump if even byte count on copy
  9975.  
  9976.           movsb                            ; Copy odd byte
  9977.           or      cx,cx
  9978.           jz      exit_copy                ; Exit if no fill
  9979.  
  9980.           stosb                            ; Even out the segment offset
  9981.           dec     cx                       ; Adjust remaining fill count
  9982.  even_copy:
  9983.           shr     cx,1                     ; Form word count on fill
  9984.       rep stosw                            ; Clear unused words at end
  9985.           jnc     exit_copy                ; Exit if no odd byte remains
  9986.  
  9987.           stosb                            ; Clear last odd byte
  9988.  exit_copy:
  9989.           ret
  9990.  
  9991.  copy_with_fill   endp
  9992.  
  9993.  init_code        ends
  9994.                   end
  9995.  
  9996.  
  9997.  $B
  9998.  
  9999.  
  10000.  Appendix B  The 80286 Instruction Set
  10001.  
  10002.  ───────────────────────────────────────────────────────────────────────────
  10003.  
  10004.  This section presents the 80286 instruction set using Intel's ASM286
  10005.  notation. All possible operand types are shown. Instructions are organized
  10006.  alphabetically according to generic operations. Within each operation, many
  10007.  different instructions are possible depending on the operand. The pages are
  10008.  presented in a standardized format, the elements of which are described in
  10009.  the following paragraphs.
  10010.  
  10011.  Opcode
  10012.  
  10013.  This column gives the complete object code produced for each form of the
  10014.  instruction. Where possible, the codes are given as hexadecimal bytes,
  10015.  presented in the order in which they will appear in memory. Several
  10016.  shorthand conventions are used for the parts of instructions which specify
  10017.  operands. These conventions are as follows:
  10018.  
  10019.  /n: (n is a digit from 0 through 7) A ModRM byte, plus a possible immediate
  10020.  and displacement field follow the opcode. See figure B-1 for the encoding
  10021.  of the fields. The digit n is the value of the REG field of the ModRM byte.
  10022.  To obtain the possible hexadecimal values for /n, refer to column n of table
  10023.  B-1. Each row gives a possible value for the effective address operand
  10024.  to the instruction. The entry at the end of the row indicates whether the
  10025.  effective address operand is a register or memory; if memory, the entry
  10026.  indicates what kind of indexing and/or displacement is used. Entries with
  10027.  D8 or D16 signify that a one-byte or two-byte displacement quantity
  10028.  immediately follows the ModRM and optional immediate field bytes. The
  10029.  signed displacement is added to the effective address offset.
  10030.  
  10031.  /r: A ModRM byte that contains both a register operand and an effective
  10032.  address operand, followed by a possible immediate and displacement field.
  10033.  See figure B-2 for the encoding of the fields. The ModRM byte could be any
  10034.  value appearing in table B-1. The column determines which register
  10035.  operand was selected; the row determines the form of effective address. If
  10036.  the row entry mentions D8 or D16, then a one-byte or two-byte displacement
  10037.  follows, as described in the previous paragraph.
  10038.  
  10039.  cb: A one-byte signed displacement in the range of -128 to +127 follows the
  10040.  opcode. The displacement is sign-extended to 16 bits, and added modulo 65536
  10041.  to the offset of the instruction FOLLOWING this instruction to obtain the
  10042.  new IP value.
  10043.  
  10044.  cw: A two-byte displacement is added modulo 65536 to the offset of the
  10045.  instruction FOLLOWING this instruction to obtain the new IP value.
  10046.  
  10047.  cd: A two-word pointer which will be the new CS:IP value. The offset is
  10048.  given first, followed by the selector.
  10049.  
  10050.  db: An immediate byte operand to the instruction which follows the opcode
  10051.  and ModRM bytes. The opcode determines if it is a signed value.
  10052.  
  10053.  dw: An immediate word operand to the instruction which follows the opcode
  10054.  and ModRM bytes. All words are given in the 80286 with the low-order byte
  10055.  first.
  10056.  
  10057.  +rb: A register code from 0 through 7 which is added to the hexadecimal byte
  10058.  given at the left of the plus sign to form a single opcode byte. The codes
  10059.  are: AL=0, CL=1, DL=2, BL=3, AH=4, CH=5, DH=6, and BH=7.
  10060.  
  10061.  +rw: A register code from 0 through 7 which is added to the hexadecimal byte
  10062.  given at the left of the plus sign to form a single opcode byte. The codes
  10063.  are: AX=0, CX=1, DX=2, BX=3, SP=4, BP=5, SI=6, and DI=7.
  10064.  
  10065.  
  10066.  Table B-1. ModRM Values
  10067.  
  10068. ╓┌──────────┌─────┌─────┌─────┌─────┌─────┌─────┌─────┌─────┌────────────────
  10069.  Rb  =      AL    CL    DL    BL    AH    CH    DH    BH
  10070.  Rw  =      AX    CX    DX    BX    SP    BP    SI    DI
  10071.  REG =       0     1     2     3     4     5     6     7
  10072.  
  10073.                          ModRM values                       Effective address
  10074.  
  10075.             00    08    10    18    20    28    30    38     [BX + SI]
  10076.             01    09    11    19    21    29    31    39     [BX + DI]
  10077.             02    0A    12    1A    22    2A    32    3A     [BP + SI]
  10078.             03    0B    13    1B    23    2B    33    3B     [BP + DI]
  10079.  mod=00     04    0C    14    1C    24    2C    34    3C     [SI]
  10080.             05    0D    15    1D    25    2D    35    3D     [DI]
  10081.             06    0E    16    1E    26    2E    36    3E     D16 (simple var)
  10082.  Rb  =      AL    CL    DL    BL    AH    CH    DH    BH
  10083.  Rw  =      AX    CX    DX    BX    SP    BP    SI    DI
  10084.  REG =       0     1     2     3     4     5     6     7
  10085.  
  10086.                          ModRM values                       Effective address
  10087.            06    0E    16    1E    26    2E    36    3E     D16 (simple var)
  10088.             07    0F    17    1F    27    2F    37    3F     [BX]
  10089.  
  10090.             40    48    50    58    60    68    70    78     [BX + SI] + D8
  10091.  
  10092.  
  10093.             41    49    51    59    61    69    71    79     [BX + DI] + D8
  10094.             42    4A    52    5A    62    6A    72    7A     [BP + SI] + D8
  10095.             43    4B    53    5B    63    6B    73    7B     [BP + DI] + D8
  10096.  mod=01     44    4C    54    5C    64    6C    74    7C     [SI] + D8
  10097.             45    4D    55    5D    65    6D    75    7D     [DI] + D8
  10098.             46    4E    56    5E    66    6E    76    7E     [BP] + D8
  10099.  
  10100.  
  10101.             47    4F    57    5F    67    6F    77    7F     [BX] + D8
  10102.  
  10103.  Rb  =      AL    CL    DL    BL    AH    CH    DH    BH
  10104.  Rw  =      AX    CX    DX    BX    SP    BP    SI    DI
  10105.  REG =       0     1     2     3     4     5     6     7
  10106.  
  10107.                          ModRM values                       Effective address
  10108. 
  10109.             80    88    90    98    A0    A8    B0    B8     [BX + SI] + D16
  10110.  
  10111.  
  10112.             81    89    91    99    A1    A9    B1    B9     [BX + DI] + D16
  10113.             82    8A    92    9A    A2    AA    B2    BA     [BP +SI] + D16
  10114.             83    8B    93    9B    A3    AB    B3    BB     [BP + DI] + D16
  10115.  mod=10     84    8C    94    9C    A4    AC    B4    BC     [SI] + D16
  10116.             85    8D    95    9D    A5    AD    B5    BD     [DI] + D16
  10117.             86    8E    96    9E    A6    AE    B6    BE     [BP] + D16
  10118.  
  10119.  
  10120.             87    8F    97    9F    A7    AF    B7    BF     [BX] + D16
  10121.  
  10122.             C0    C8    D0    D8    E0    E8    F0    F8     Ew=AX  Eb=AL
  10123.             C1    C9    D1    D9    E1    E9    F1    F9     Ew=CX  Eb=CL
  10124.  Rb  =      AL    CL    DL    BL    AH    CH    DH    BH
  10125.  Rw  =      AX    CX    DX    BX    SP    BP    SI    DI
  10126.  REG =       0     1     2     3     4     5     6     7
  10127.  
  10128.                          ModRM values                       Effective address
  10129.            C1    C9    D1    D9    E1    E9    F1    F9     Ew=CX  Eb=CL
  10130.             C2    CA    D2    DA    E2    EA    F2    FA     Ew=DX  Eb=DL
  10131.             C3    CB    D3    DB    E3    EB    F3    FB     Ew=BX  Eb=BL
  10132.  mod=11     C4    CC    D4    DC    E4    EC    F4    FC     Ew=SP  Eb=AH
  10133.             C5    CD    D5    DD    E5    ED    F5    FD     Ew=BP  Eb=CH
  10134.             C6    CE    D6    DE    E6    EE    F6    FE     Ew=SI  Eb=DH
  10135.             C7    CF    D7    DF    E7    EF    F7    FF     Ew=DI  Eb=BH
  10136.  
  10137.  
  10138.  
  10139.  Figure B-1. /n Instruction Byte Format
  10140.  
  10141.                           pp/n Instruction Byte Format
  10142.   ╔════╤═══════╤═══════╤════════════╤════════════╤════════════╤════════════╗
  10143.   ║ mod│   n   │  r/m  │ imm. low  │ imm. high │  disp-low  │ disp-high  ║
  10144.   ╚════╧═══════╧═══════╧════════════╧════════════╧════════════╧════════════╝
  10145.   7   6 5  4  3 2  1  0 7          0 7          0 7          0 7           0
  10146.  
  10147.                           "mod" Field Bit Assignments
  10148.   ╔══════════╤═════════════════════════════════════════════════════════════╗
  10149.   ║    mod   │                     Displacement                            ║
  10150.   ╠══════════╪═════════════════════════════════════════════════════════════╣
  10151.   ║    00    │DISP = 0, disp-low and disp-high are absent                 ║
  10152.   ║    01    │DISP = disp-low sign-extended to 16-bit, disp-high is absent ║
  10153.   ║    10    │DISP = disp-high: disp-low                                   ║
  10154.   ║    11    │r/m is treated as a "reg" field                              ║
  10155.   ╚══════════╧═════════════════════════════════════════════════════════════╝
  10156.  
  10157.                           "r/m" Field Bit Assignments
  10158.   ╔════════════════════════════════════╤═══════════════════════════════════╗
  10159.   ║                r/m                 │        Operand Address            ║
  10160.   ╠════════════════════════════════════╪═══════════════════════════════════╣
  10161.   ║                000                 │        (BX) + (SI) + DISP         ║
  10162.   ║                001                 │        (BX) + (DI) + DISP         ║
  10163.   ║                010                 │        (BP) + (SI) + DISP         ║
  10164.   ║                011                 │        (BP) + (DI) + DISP         ║
  10165.   ║                100                 │        (SI) + DISP                ║
  10166.   ║                101                 │        (DI) + DISP                ║
  10167.   ║                110                 │        (BP) + DISP               ║
  10168.   ║                111                 │        (BX) + DISP                ║
  10169.   ╚════════════════════════════════════╧═══════════════════════════════════╝
  10170.   DISP follows 2nd byte of instruction (before data if required).
  10171.  
  10172.  
  10173.  Figure B-2. /r Instruction Byte Format
  10174.  
  10175.                             /r Instruction Byte Format
  10176.   ╔════╤═══════╤═══════╤════════════╤════════════╤════════════╤════════════╗
  10177.   ║ mod│   r   │  r/m  │ imm. low  │ imm. high │  disp-low  │ disp-high  ║
  10178.   ╚════╧═══════╧═══════╧════════════╧════════════╧════════════╧════════════╝
  10179.   7   6 5  4  3 2  1  0 7          0 7          0 7          0 7           0
  10180.  
  10181.                           "mod" Field Bit Assignments
  10182.   ╔══════════╤═════════════════════════════════════════════════════════════╗
  10183.   ║    mod   │                     Displacement                            ║
  10184.   ╠══════════╪═════════════════════════════════════════════════════════════╣
  10185.   ║    00    │DISP = 0, disp-low and disp-high are absent                 ║
  10186.   ║    01    │DISP = disp-low sign-extended to 16-bit, disp-high is absent ║
  10187.   ║    10    │DISP = disp-high: disp-low                                   ║
  10188.   ║    11    │r/m is treated as a "reg" field                              ║
  10189.   ╚══════════╧═════════════════════════════════════════════════════════════╝
  10190.  
  10191.                           "r" Field Bit Assignments
  10192.   ╔═══════════════════════╤═══════════════════════╤════════════════════════╗
  10193.   ║     16-Bit (w = 1)    │     6-Bit (w = 0)     │        Segment         ║
  10194.   ╠═══════════════════════╪═══════════════════════╪════════════════════════╣
  10195.   ║        000 AX         │        000 AL         │         00 ES          ║
  10196.   ║        001 CX         │        001 CL         │         01 CS          ║
  10197.   ║        010 DX         │        010 DL         │         10 SS          ║
  10198.   ║        011 BX         │        011 BL         │         11 DS          ║
  10199.   ║        100 SP         │        100 AH         │                        ║
  10200.   ║        101 BP         │        101 CH         │                        ║
  10201.   ║        110 SI         │        110 DH         │                        ║
  10202.   ║        111 DI         │        111 BH         │                        ║
  10203.   ╚═══════════════════════╧═══════════════════════╧════════════════════════╝
  10204.  
  10205.                           "r/m" Field Bit Assignments
  10206.   ╔════════════════════════════════════╤═══════════════════════════════════╗
  10207.   ║                r/m                 │        Operand Address            ║
  10208.   ╠════════════════════════════════════╪═══════════════════════════════════╣
  10209.   ║                000                 │        (BX) + (SI) + DISP         ║
  10210.   ║                001                 │        (BX) + (DI) + DISP         ║
  10211.   ║                010                 │        (BP) + (SI) + DISP         ║
  10212.   ║                011                 │        (BP) + (DI) + DISP         ║
  10213.   ║                100                 │        (SI) + DISP                ║
  10214.   ║                101                 │        (DI) + DISP                ║
  10215.   ║                110                 │        (BP) + DISP               ║
  10216.   ║                111                 │        (BX) + DISP                ║
  10217.   ╚════════════════════════════════════╧═══════════════════════════════════╝
  10218.   DISP follows 2nd byte of instruction (before data if required).
  10219.  
  10220.  
  10221.  Instruction
  10222.  
  10223.  This column gives the instruction mnemonic and possible operands. The type
  10224.  of operand used will determine the opcode and operand encodings. The
  10225.  following entries list the type of operand which can be encoded in the
  10226.  format shown in the instruction column. The Intel convention is to place
  10227.  the destination operand as the left hand operand. Source-only operands
  10228.  follow the destination operand.
  10229.  
  10230.  In many cases, the same instruction can be encoded several ways. It is
  10231.  recommended that you use the shortest encoding. The short encodings are
  10232.  provided to save memory space.
  10233.  
  10234.  cb: a destination instruction offset in the range of 128 bytes before the
  10235.  end of this instruction to 127 bytes after the end of this instruction.
  10236.  
  10237.  cw: a destination offset within the same code segment as this instruction.
  10238.  Some instructions allow a short form of destination offset. See cb type for
  10239.  more information.
  10240.  
  10241.  cd: a destination address, typically in a different code segment from this
  10242.  instruction. Using the cd: address form with call instructions saves the
  10243.  code segment selector.
  10244.  
  10245.  db: a signed value between -128 and +127 inclusive which is an operand of
  10246.  the instruction. For instructions in which the db is to be combined in some
  10247.  way with a word operand, the immediate value is sign-extended to form a
  10248.  word. The upper byte of the word is filled with the topmost bit of the
  10249.  immediate value.
  10250.  
  10251.  dw: an immediate word value which is an operand of the instruction.
  10252.  
  10253.  eb: a byte-sized operand. This is either a byte register or a (possibly
  10254.  indexed) byte memory variable. Either operand location may be encoded in the
  10255.  ModRM field. Any memory addressing mode may be used.
  10256.  
  10257.  ed: a memory-based pointer operand. Any memory addressing mode may be used.
  10258.  Use of a register addressing mode will cause exception 6.
  10259.  
  10260.  ew: a word-sized operand. This is either a word register or a (possibly
  10261.  indexed) word memory variable. Either operand location may be encoded in the
  10262.  ModRM field. Any memory addressing mode may be used.
  10263.  
  10264.  m: a memory location. Operands in registers do not have a memory address.
  10265.  Any memory addressing mode may be used. Use of a register addressing mode
  10266.  will cause exception 6.
  10267.  
  10268.  mb: a memory-based byte-sized operand. Any memory addressing mode may be
  10269.  used.
  10270.  
  10271.  mw: a memory-based word operand. Any memory addressing mode may be used.
  10272.  
  10273.  rb: one of the byte registers AL, CL, DL, BL, AH, CH, DH, or BH; rb has the
  10274.  value 0,1,2,3,4,5,6, and 7, respectively.
  10275.  
  10276.  rw: one of the word registers AX, CX, DX, BX, SP, BP, SI, or DI; rw has the
  10277.  value 0,1,2,3,4,5,6, and 7, respectively.
  10278.  
  10279.  xb: a simple byte memory variable without a base or index register. MOV
  10280.  instructions between AL and memory have this optimized form if no indexing
  10281.  is required.
  10282.  
  10283.  xw: a simple word memory variable without a base or index register. MOV
  10284.  instructions between AX and memory have this optimized form if no indexing
  10285.  is required.
  10286.  
  10287.  
  10288.  Clocks
  10289.  
  10290.  This column gives the number of clock cycles that this form of the
  10291.  instruction takes to execute. The amount of time for each clock cycle is
  10292.  computed by dividing one microsecond by the number of MHz at which the 80286
  10293.  is running. For example, a 10-MHz 80286 (with the CLK pin connected to
  10294.  a 20-MHz crystal) takes 100 nanoseconds for each clock cycle.
  10295.  
  10296.  Add one clock to instructions that use the base plus index plus
  10297.  displacement form of addressing. Add two clocks for each 16-bit memory based
  10298.  operand reference located on an odd physical address. Add one clock for each
  10299.  wait state added to each memory read. Wait states inserted in memory writes
  10300.  or instruction fetches do not necessarily increase execution time.
  10301.  
  10302.  The clock counts establish the maximum execution rate of the 80286. With no
  10303.  delays in bus cycles, the actual clock count of an 80286 program will
  10304.  average 5-10% more than the calculated clock count due to instruction
  10305.  sequences that execute faster than they can be fetched from memory.
  10306.  
  10307.  Some instruction forms give two clock counts, one unlabelled and one
  10308.  labelled. These counts indicate that the instruction has two different clock
  10309.  times for two different circumstances. Following are the circumstances for
  10310.  each possible label:
  10311.  
  10312.  mem: The instruction has an operand that can either be a register or a
  10313.  memory variable. The unlabelled time is for the register; the mem time is
  10314.  for the memory variable. Also, one additional clock cycle is taken for
  10315.  indexed memory variables for which all three possible indices (base
  10316.  register, index register, and displacement) must be added.
  10317.  
  10318.  noj: The instruction involves a conditional jump or interrupt. The
  10319.  unlabelled time holds when the jump is made; the noj time holds when the
  10320.  jump is not made.
  10321.  
  10322.  pm: If the instruction takes more time to execute when the 80286 is in
  10323.  Protected Mode. The unlabelled time is for Real Address Mode; the pm time is
  10324.  for Protected Mode.
  10325.  
  10326.  
  10327.  Description
  10328.  
  10329.  This is a concise description of the operation performed for this form of
  10330.  the instruction. More details are given in the "Operation" section that
  10331.  appears later in this chapter.
  10332.  
  10333.  
  10334.  Flags Modified
  10335.  
  10336.  This is a list of the flags that are set to a meaningful value by the
  10337.  instruction. If a flag is always set to the same value by the instruction,
  10338.  the value is given ("=0" or "=1") after the flag name.
  10339.  
  10340.  
  10341.  Flags Undefined
  10342.  
  10343.  This is a list of the flags that have an undefined (meaningless) setting
  10344.  after the instruction is executed.
  10345.  
  10346.  All flags not mentioned under "Flags Modified" or "Flags Undefined" are
  10347.  unchanged by the instruction.
  10348.  
  10349.  
  10350.  Operation
  10351.  
  10352.  This section fully describes the operation performed by the instruction.
  10353.  For some of the more complicated instructions, suggested usage is also
  10354.  indicated.
  10355.  
  10356.  
  10357.  Protected Mode Exceptions
  10358.  
  10359.  The possible exceptions involved with this instruction when running under
  10360.  the 80286 Protected Mode are listed below. These exceptions are abbreviated
  10361.  with a pound sign (#) followed by two capital letters and an optional error
  10362.  code in parenthesis. For example, #GP(0) denotes the general protection
  10363.  exception with an error code of zero. The next section describes all of the
  10364.  80286 exceptions and the machine state upon entry to the exception.
  10365.  
  10366.  If you are an applications programmer, consult the documentation provided
  10367.  with your operating system to determine what actions are taken by the system
  10368.  when exceptions occur.
  10369.  
  10370.  
  10371.  Real Address Mode Exceptions
  10372.  
  10373.  Since less error checking is performed by the 80286 when it is in Real
  10374.  Address Mode, there are fewer exceptions in this mode. One exception that is
  10375.  possible in many instructions is #GP(0). Exception 13 is generated whenever
  10376.  a word operand is accessed from effective address 0FFFFH in a segment. This
  10377.  happens because the second byte of the word is considered located at
  10378.  location 10000H, not at location 0, and thus exceeds the segment's
  10379.  addressability limit.
  10380.  
  10381.  
  10382.  Protection Exceptions
  10383.  
  10384.  In parallel with the execution of instructions, the protected-mode 80286
  10385.  checks all memory references for validity of addressing and type of access.
  10386.  Violation of the memory protection rules built into the processor will cause
  10387.  a transfer of program control to one of the interrupt procedures described
  10388.  in this section. The interrupts have dedicated positions within the
  10389.  Interrupt Descriptor Table, which is shown in table B-2. The interrupts are
  10390.  referenced within the instruction set pages by a pound sign (#) followed by
  10391.  a two-letter mnemonic and the optional error code in parenthesis.
  10392.  
  10393.  
  10394.  Table B-2. Protection Exceptions of the 80286
  10395.  
  10396.  Abbreviation        Interrupt Number      Description
  10397.  
  10398.   #UD                       6              Undefined Opcode
  10399.   #NM                       7              No Math Unit Available
  10400.   #DF                       8              Double Fault
  10401.   #MP                       9              Math Unit Protection Fault
  10402.   #TS                      10              Invalid Task State Segment
  10403.   #NP                      11              Not Present
  10404.   #SS                      12              Stack Fault
  10405.   #GP                      13              General Protection
  10406.   #MF                      16              Math Fault
  10407.  
  10408.  
  10409.  Error Codes
  10410.  
  10411.  Some exceptions cause the 80286 to pass a 16-bit error code to the
  10412.  interrupt procedure. When this happens, the error code is the last item
  10413.  pushed onto the stack before control is tranferred to the interrupt
  10414.  procedure. If stacks were switched as a result of the interrupt (causing a
  10415.  privilege change or task switch), the error code appears on the interrupt
  10416.  procedure's stack, not on the stack of the task that was interrupted.
  10417.  
  10418.  The error code generally contains the selector of the segment that caused
  10419.  the protection violation. The RPL field (bottom two bits) of the error code
  10420.  does not, however, contain the privilege level. Instead, it contains the
  10421.  following information:
  10422.  
  10423.    ■  Bit 0 contains the value 1 if the exception was detected during an
  10424.       interrupt caused by an event external to the program (i.e., an external
  10425.       interrupt, a single step, a processor extension not-present exception,
  10426.       or a processor extension segment overrun). Bit 0 is 0 if the exception
  10427.       was detected while processing the regular instruction stream, even if
  10428.       the instruction stream is part of an external interrupt handling
  10429.       procedure or task. If bit 0 is set, the instruction pointed to by the
  10430.       saved CS:IP address is not responsible for the error. The current task
  10431.       can be restarted unless this is exception 9.
  10432.  
  10433.    ■  Bit 1 is 1 if the selector points to the Interrupt Descriptor Table.
  10434.       In this case, bit 2 can be ignored, and bits 3-10 contain the index
  10435.       into the IDT.
  10436.  
  10437.    ■  Bit 1 is 0 if the selector points to the Global or Local Descriptor
  10438.       Tables. In this case, bits 2-15 have their usual selector
  10439.       interpretation: bit 2 selects the table (1=Local, 0=Global), and
  10440.       bits 3-15 are the index into the table.
  10441.  
  10442.  In some cases the 80286 chooses to pass an error code with no information
  10443.  in it. In these cases, all 16 bits of the error code are zero.
  10444.  
  10445.  The existence and type of error codes are described under each of the
  10446.  following individual exceptions.
  10447.  
  10448.  
  10449.  #DF 8 Double Fault (Zero Error Code)
  10450.  
  10451.  This exception is generated when a second exception is detected while the
  10452.  processor is attempting to transfer control to the handler for an exception.
  10453.  For instance, it is generated if the code segment containing the exception
  10454.  handler is marked not present. It is also generated if invoking the
  10455.  exception handler causes a stack overflow.
  10456.  
  10457.  This exception is not generated during the execution of an exeception
  10458.  handler. Faults detected within the instruction stream are handled by
  10459.  regular exceptions.
  10460.  
  10461.  The error code is normally zero. The saved CS:IP will point at the
  10462.  instruction that was attempting to execute when the double fault occurred.
  10463.  Since the error code is normally zero, no information on the source of the
  10464.  exception is available. Restart is not possible.
  10465.  
  10466.  The "double fault" exception does not occur when detecting a new exception
  10467.  while trying to invoke handlers for the following exceptions: 1, 2, 3, 4, 5,
  10468.  6, 7, 9, and 16.
  10469.  
  10470.  If another exception is detected while attempting to perform the double
  10471.  fault exception, the 80286 will enter shutdown (see section 11.5).
  10472.  
  10473.  
  10474.  #GP 13 General Protection (Selector or Zero Error Code)
  10475.  
  10476.  This exception is generated for all protection violations not covered by
  10477.  the other exceptions in this section. Examples of this include:
  10478.  
  10479.    1.  An attempt to address a memory location by using an offset that
  10480.        exceeds the limit for the segment involved.
  10481.  
  10482.    2.  An attempt to jump to a data segment.
  10483.  
  10484.    3.  An attempt to load SS with a selector for a read-only segment.
  10485.  
  10486.    4.  An attempt to write to a read-only segment.
  10487.  
  10488.    5.  Exceeding the maximum instruction length of 10 bytes.
  10489.  
  10490.  If #GP occurred while loading a descriptor, the error code passed contains
  10491.  the selector involved. Otherwise, the error code is zero.
  10492.  
  10493.  If the error code is not zero, the instruction can be restarted if the
  10494.  erroneous condition is rectified. If the error code is zero either a limit
  10495.  violation, a write protect violation, or an illegal use of invalid segment
  10496.  register occurred. An invalid segment register contains the values 0-3. A
  10497.  write protect fault on ADC, SBB, RCL, RCR, or XCHG is not restartable.
  10498.  
  10499.  
  10500.  #MF 16 Math Fault (No Error Code)
  10501.  
  10502.  This exception is generated when the numeric processor extension (the
  10503.  80287) detects an error signalled by the ERROR input pin leading from the
  10504.  80287 to the 80286. The ERROR pin is tested at the beginning of most
  10505.  floating point instructions, and when a WAIT instruction is executed with
  10506.  the EM bit of the Machine Status Word set to 0 (i.e., no emulation of the
  10507.  math unit). The floating point instructions that do not cause the ERROR pin
  10508.  to be tested are FNCLEX, FNINIT, FSETPM, FNSTCW, FNSTSW, FNSAVE, and
  10509.  FNSTENV.
  10510.  
  10511.  If the handler corrects the error condition causing the exception, the
  10512.  floating point instruction that caused #MF can be restarted. This is not
  10513.  accomplished by IRET, however, since the fault occurs at the floating point
  10514.  instruction that follows the offending instruction. Before restarting the
  10515.  numeric instruction, the handler must obtain from the 80287 the address of
  10516.  the offending instruction and the address of the optional numeric operand.
  10517.  
  10518.  
  10519.  #MP 9 Math Unit Protection Fault (No Error Code)
  10520.  
  10521.  This exception is generated if the numeric operand is larger than one word
  10522.  and has the second or subsequent words outside the segment's limit. Not all
  10523.  math addressing errors cause exception 9. If the effective address of an
  10524.  ESCAPE instruction is not in the segment's limit, or if a write is
  10525.  attempted on a read-only segment, or if a one-word operand violates a
  10526.  segment limit, exception 13 will occur.
  10527.  
  10528.  The #MP exception occurs during the execution of the numeric instruction by
  10529.  the 80287. Thus, the 80286 may be in an unrelated instruction stream at the
  10530.  time. Exception 9 may occur in a task unrelated to the task that executed
  10531.  the ESC instruction. The operating system should keep track of which task
  10532.  last used the NPX (see section 11.4).
  10533.  
  10534.  The offending floating point instruction cannot be restarted; the task
  10535.  which attempted to execute the offending numeric instruction must be
  10536.  aborted. However, if exception 9 interrupted another task, the interrupted
  10537.  task may be restarted.
  10538.  
  10539.  The exception 9 handler must execute FNINIT before executing any ESCAPE or
  10540.  WAIT instruction.
  10541.  
  10542.  
  10543.  #NM 7 No Math Unit Available (No Error Code)
  10544.  
  10545.  This exception occurs when any floating point instruction is executed while
  10546.  the EM bit or the TS bit of the Machine Status Word is 1. It also occurs
  10547.  when a WAIT instruction is encountered and both the MP and TS bits of the
  10548.  Machine Status Word are 1.
  10549.  
  10550.  Depending on the setting of the MSW bits that caused this exception, the
  10551.  exception handler could provide emulation of the 80287, or it could perform
  10552.  a context switch of the math processor to prepare it for use by another
  10553.  task.
  10554.  
  10555.  The instruction causing #NM can be restarted if the handler performs a
  10556.  numeric context switch. If the handler provided emulation of the math unit,
  10557.  it should advance the return pointer beyond the floating point instruction
  10558.  that caused NM.
  10559.  
  10560.  
  10561.  #NP 11 Not Present (Selector Error Code)
  10562.  
  10563.  This exception occurs when CS, DS, ES, or the Task Register is loaded with
  10564.  a descriptor that is marked not present but is otherwise valid. It can occur
  10565.  in an LLDT instruction, but the #NP exception will not occur if the
  10566.  processor attempts to load the LDT register during a task switch. A
  10567.  not-present LDT encountered during a task switch causes the #TS exception.
  10568.  
  10569.  The error code passed is the selector of the descriptor that is marked not
  10570.  present.
  10571.  
  10572.  Typically, the Not Present exception handler is used to implement a virtual
  10573.  memory system. The operating system can swap inactive memory segments to a
  10574.  mass-storage device such as a disk. Applications programs need not be told
  10575.  about this; the next time they attempt to access the swapped-out memory
  10576.  segment, the Not Present handler will be invoked, the segment will be
  10577.  brought back into memory, and the offending instruction within the
  10578.  applications program will be restarted.
  10579.  
  10580.  If #NP is detected on loading CS, DS, or ES in a task switch, the exception
  10581.  occurs in the new task, and the IRET from the exception handler jumps
  10582.  directly to the next instruction in the new task.
  10583.  
  10584.  The Not Present exception handler must contain special code to complete the
  10585.  loading of segment registers when #NP is detected in loading the CS or DS
  10586.  registers in a task switch and a trap or interrupt gate was used. The DS and
  10587.  ES registers have been loaded but their descriptors have not been loaded.
  10588.  Any memory reference using the segment register may cause exception 13. The
  10589.  #NP exception handler should execute code such as the following to ensure
  10590.  full loading of the segment registers:
  10591.  
  10592.     MOV AX,DS
  10593.     MOV DS,AX
  10594.     MOV AX,ES
  10595.     MOV ES,AX
  10596.  
  10597.  #SS 12 Stack Fault (Selector or Zero Error Code)
  10598.  
  10599.  This exception is generated when a limit violation is detected in
  10600.  addressing through the SS register. It can occur on stack-oriented
  10601.  instructions such as PUSH or POP, as well as other types of memory
  10602.  references using SS such as MOV AX,[BP+28]. It also can occur on an ENTER
  10603.  instruction when there is not enough space on the stack for the indicated
  10604.  local variable space, even if the stack exception is not triggered by
  10605.  pushing BP or copying the display stack. A stack exception can therefore
  10606.  indicate a stack overflow, a stack underflow or a wild offset. The error
  10607.  code will be zero.
  10608.  
  10609.  #SS is also generated on an attempt to load SS with a descriptor that is
  10610.  marked not present but is otherwise valid. This can occur in a task switch,
  10611.  an inter-level call, an inter-level return, a move to the SS instruction or
  10612.  a pop to the SS instruction. The error code will be non-zero.
  10613.  
  10614.  #SS is never generated when addressing through the DS or ES registers even
  10615.  if the offending register points to the same segment as the SS register.
  10616.  
  10617.  The #SS exception handler must contain special code to complete the loading
  10618.  of segment registers. The DS and ES registers will not be fully loaded if a
  10619.  not-present condition is detected while loading the SS register. Therefore,
  10620.  the #SS exception handler should execute code such as the following to
  10621.  insure full loading of the segment registers:
  10622.  
  10623.     MOV AX,DS
  10624.     MOV DS,AX
  10625.     MOV AX,ES
  10626.     MOV ES,AX
  10627.  
  10628.  Generally, the instruction causing #SS can be restarted, but there is one
  10629.  special case when it cannot: when a PUSHA or POPA instruction attempts to
  10630.  wrap around the 64K boundary of a stack segment. This condition is
  10631.  identified by the value of the saved SP, which can be either 0000H, 0001H,
  10632.  0FFFEH, or 0FFFFH.
  10633.  
  10634.  
  10635.  #TS 10 Invalid Task State Segment (Selector Error Code)
  10636.  
  10637.  This exception is generated during a task switch when the new task state
  10638.  segment is invalid, that is, when a task state segment is too small; when
  10639.  the LDT indicated in a TSS is invalid or not present; when the SS, CS, DS,
  10640.  or ES indicated in a TSS are invalid (task switch); when the back link in a
  10641.  TSS is invalid (inter-task IRET).
  10642.  
  10643.  #TS is not generated when the SS, CS, DS, or ES back link or privileged
  10644.  stack selectors point to a descriptor that is not present but otherwise is
  10645.  valid. #NP is generated in these cases.
  10646.  
  10647.  The error code passed to the exception handler contains the selector of the
  10648.  offending segment, which can either be the Task State Segment itself, or a
  10649.  selector found within the Task State Segment.
  10650.  
  10651.  The instruction causing #TS can be restarted.
  10652.  
  10653.  #TS must be handled through a task gate.
  10654.  
  10655.  The exception handler must reset the busy bit in the new TSS.
  10656.  
  10657.  
  10658.  #UD 6 Undefined Opcode (No Error Code)
  10659.  
  10660.  This exception is generated when an invalid operation code is detected in
  10661.  the instruction stream. Following are the cases in which #UD can occur:
  10662.  
  10663.    1.  The first byte of an instruction is completely invalid (e.g., 64H).
  10664.  
  10665.    2.  The first byte indicates a 2-byte opcode and the second byte is
  10666.        invalid (e.g., 0FH followed by 0FFH).
  10667.  
  10668.    3.  An invalid register is used with an otherwise valid opcode (e.g., MOV
  10669.        CS,AX).
  10670.  
  10671.    4.  An invalid opcode extension is given in the REG field of the ModRM
  10672.        byte (e.g., 0F6H /1).
  10673.  
  10674.    5.  A register operand is given in an instruction that requires a memory
  10675.        operand (e.g., LGDT AX).
  10676.  
  10677.  Since the offending opcode will always be invalid, it cannot be restarted.
  10678.  However, the #UD handler might be coded to implement an extension of the
  10679.  80286 instruction set. In that case, the handler could advance the return
  10680.  pointer beyond the extended instruction and return control to the program
  10681.  after the extended instruction is emulated. Any such extensions may be
  10682.  incompatible with the 80386.
  10683.  
  10684.  
  10685.  Privilege Level and Task Switching on the 80286
  10686.  
  10687.  The 80286 supports many of the functions necessary to implement a
  10688.  protected, multi-tasking operating system in hardware. This support is
  10689.  provided not by additional instructions, but by extension of the semantics
  10690.  of 8086/8088 instructions that change the value of CS:IP.
  10691.  
  10692.  Whenever the 80286 performs an inter-segment jump, call, interrupt, or
  10693.  return, it consults the Access Rights (AR) byte found in the descriptor
  10694.  table entry of the selector associated with the new CS value. The AR byte
  10695.  determines whether the long jump being made is through a gate, or is a task
  10696.  switch, or is a simple long jump to the same privilege level. Table B-3
  10697.  lists the possible values of the AR byte. The "privilege" headings at the
  10698.  top of the table give the Descriptor Privilege Level, which is referred to
  10699.  as the DPL within the instruction descriptions.
  10700.  
  10701.  Each of the CALL, INT, IRET, JMP, and RET instructions contains on its
  10702.  instruction set pages a listing of the access rights checking and actions
  10703.  taken to implement the instruction. Instructions involving task switches
  10704.  contain the symbol SWITCH_TASKS, which is an abbreviation for the following
  10705.  list of checks and actions:
  10706.  
  10707.  SWITCH_TASKS:
  10708.    Locked set AR byte of new TSS descriptor to Busy TSS (Bit 1 = 1)
  10709.    Current TSS cache must be valid with limit ≥ 41 else #TS (error code will
  10710.    be new TSS, but back link points at old TSS)
  10711.    Save machine state in current TSS
  10712.    If nesting tasks, set the new TSS link to the current TSS selector
  10713.    Any exception will be in new context Else set the AR byte of current TSS
  10714.    descriptor to Available TSS (Bit 1 = 0)
  10715.    Set the current TR to selector, base and limit of new TSS
  10716.    New TSS limit ≥ 43 else #TS (new TSS)
  10717.    Set all machine registers to values from new TSS without loading
  10718.    descriptors for DS, ES, CS, SS, LDT
  10719.    Clear valid flags for LDT, SS, CS, DS, ES (not valid yet)
  10720.    If nesting tasks, set the Nested Task flag to 1
  10721.    Set the Task Switched flag to 1
  10722.    LDT from the new TSS must be within GDT talbe limits else #TS(LDT)
  10723.    AR byte from LDT descriptor must specifiy LDT segment else #TS(LDT)
  10724.    AR byte from LDT descriptor must indicate PRESENT else #TS(LDT)
  10725.    Load LDT cache with new LDT descriptor  and set valid bit
  10726.    Set CPL to the RPL of the CS selector in the new TSS
  10727.    If new stack selector is null #TS(SS)
  10728.    SS selector must be within its descriptor table limits else #TS(SS)
  10729.    SS selector RPL must be equal to CPL else #TS(SS)
  10730.    DPL of SS descriptor must equal to CPL else #TS(SS)
  10731.    SS descriptor AR byte must indicate writable data segment else #TS(SS)
  10732.    SS descriptor AR byte must indicate PRESENT else #TS(SS)
  10733.    Load SS cache with new stack segment and set valid bit
  10734.    New CS selector must not be null else #TS(SS)
  10735.    CS selector must be within its descriptor table limits else #TS(SS)
  10736.    CS descriptor AR byte must indicate code segment else #TS(SS)
  10737.    If non-conforming then DPL must equal CPL else #TS(SS)
  10738.    If conforming then DPL must be ≤ CPL else #TS(SS)
  10739.    CS descriptor AR byte must indicate PRESENT else #TS(SS)
  10740.    Load CS cache with new code segment descriptor and set valid bit
  10741.    For DS and ES:
  10742.    If new selector is not null then perform following checks:
  10743.        Index must be within its descriptor table limits else
  10744.        #TS(segment selector)
  10745.        AR byte must indicate data or readable code else
  10746.        #TS(segment selector)
  10747.        If data or non-conforming code then:
  10748.            DPL must be ≥ CPL else #TS(SS)
  10749.            DPL must be ≥ RPL else #TS(SS)
  10750.        AR byte must indicate PRESENT else #TS(SS)
  10751.        Load cache with new segment descriptor and set valid bit
  10752.  
  10753.  
  10754.  Table B-3. Hexadecimal Values for the Access Rights Byte
  10755.  
  10756. ╓┌────┌────┌────┌───────┌────┌────┌────┌────┌────────────────────────────────
  10757.     Not present,           Present,         Descriptor Type
  10758.     privilege=             privilege=
  10759.  0    1    2    3       0    1    2    3
  10760.  
  10761.  00   20   40   60      80   A0   C0   E0   Illegal
  10762.  01   21   41   61      81   A1   C1   E1   Available Task State Segment
  10763.  02   22   42   62      82   A2   C2   E2   Local Descriptor Table Segment
  10764.  03   23   43   63      83   A3   C3   E3   Busy Task State Segment
  10765.  04   24   44   64      84   A4   C4   E4   Call Gate
  10766.  05   25   45   65      85   A5   C5   E5   Task Gate
  10767.  06   26   46   66      86   A6   C6   E6   Interrupt Gate
  10768.  07   27   47   67      87   A7   C7   E7   Trap Gate
  10769.  08   28   48   68      88   A8   C8   E8   Illegal
  10770.  09   29   49   69      89   A9   C9   E9   Illegal
  10771.  0A   2A   4A   6A      8A   AA   CA   EA   Illegal
  10772.  0B   2B   4B   6B      8B   AB   CB   EB   Illegal
  10773.  0C   2C   4C   6C      8C   AC   CC   EC   Illegal
  10774.  0D   2D   4D   6D      8D   AD   CD   ED   Illegal
  10775.     Not present,           Present,         Descriptor Type
  10776.     privilege=             privilege=
  10777.  0    1    2    3       0    1    2    3
  10778. 0D   2D   4D   6D      8D   AD   CD   ED   Illegal
  10779.  0E   2E   4E   6E      8E   AE   CE   EE   Illegal
  10780.  0F   2F   4F   6F      8F   AF   CF   EF   Illegal
  10781.  10   30   50   70      90   B0   D0   F0   Expand-up, read only, ignored Data
  10782.  11   31   51   71      91   B1   D1   F1   Expand-up, read only, accessed Dat
  10783.  12   32   52   72      92   B2   D2   F2   Expand-up, writable, ignored Data
  10784.  13   33   53   73      93   B3   D3   F3   Expand-up, writable, accessed Data
  10785.  14   34   54   74      94   B4   D4   F4   Expand-down, read only, ignored Da
  10786.  15   35   55   75      95   B5   D5   F5   Expand-down, read only, accessed D
  10787.  16   36   56   76      96   B6   D6   F6   Expand-down, writable, ignored Dat
  10788.  17   37   57   77      97   B7   D7   F7   Expand-down, writable, accessed Da
  10789.  18   38   58   78      98   B8   D8   F8   Non-conform, no read, ignored Code
  10790.  19   39   59   79      99   B9   D9   F9   Non-conform, no read, accessed Cod
  10791.  1A   3A   5A   7A      9A   BA   DA   FA   Non-conform, readable, ignored Cod
  10792.  1B   3B   5B   7B      9B   BB   DB   FB   Non-conform, readable, accessed Co
  10793.  1C   3C   5C   7C      9C   BC   DC   FC   Conforming, no read, ignored Code
  10794.  1D   3D   5D   7D      9D   BD   DD   FD   Conforming, no read, accessed Code
  10795.  1E   3E   5E   7E      9E   BE   DE   FE   Conforming, readable, ignored Code
  10796.     Not present,           Present,         Descriptor Type
  10797.     privilege=             privilege=
  10798.  0    1    2    3       0    1    2    3
  10799. 1E   3E   5E   7E      9E   BE   DE   FE   Conforming, readable, ignored Code
  10800.  1F   3F   5F   7F      9F   BF   DF   FF   Conforming, readable, accessed Cod
  10801.  
  10802.  
  10803.  AAA──ASCII Adjust AL After Addition
  10804.  
  10805.  Opcode       Instruction      Clocks       Description
  10806.  
  10807.    37            AAA             3          ASCII adjust AL after addition
  10808.  
  10809.  Flags Mofified
  10810.  
  10811.  Auxiliary carry, carry
  10812.  
  10813.  Flags Undefined
  10814.  
  10815.  Overflow, sign, zero, parity
  10816.  
  10817.  Operation
  10818.  
  10819.  AAA should be executed only after an ADD instruction which leaves a byte
  10820.  result in the AL register. The lower nibbles of the operands to the ADD
  10821.  instruction should be in the range 0 through 9 (BCD digits). In this case,
  10822.  the AAA instruction will adjust AL to contain the correct decimal digit
  10823.  result. If the addition produced a decimal carry, the AH register is
  10824.  incremented, and the carry and auxiliary carry flags are set to 1. If there
  10825.  was no decimal carry, the carry and auxiliary carry flags are set to 0, and
  10826.  AH is unchanged. In any case, AL is left with its top nibble set to 0. To
  10827.  convert AL to an ASCII result, you can follow the AAA instruction with OR
  10828.  AL,30H.
  10829.  
  10830.  The precise definition of AAA is as follows: if the lower 4 bits of AL are
  10831.  greater than nine, or if the auxiliary carry flag is 1, then increment AL by
  10832.  6, AH by 1, and set the carry and auxiliary carry flags. Otherwise, reset
  10833.  the carry and auxiliary carry flags. In any case, conclude the AAA
  10834.  operation by setting the upper four bits of AL to zero.
  10835.  
  10836.  Protected Mode Exceptions
  10837.  
  10838.  None
  10839.  
  10840.  Real Address Mode Exceptions
  10841.  
  10842.  None
  10843.  
  10844.  AAD──ASCII Adjust AX Before Division
  10845.  
  10846.  Opcode     Instruction     Clocks       Description
  10847.  
  10848.   D5 0A      AAD             14          ASCII adjust AX before division
  10849.  
  10850.  Flags Modified
  10851.  
  10852.  Sign, zero, parity
  10853.  
  10854.  Flags Undefined
  10855.  
  10856.  Overflow, auxiliary carry, carry
  10857.  
  10858.  Operation
  10859.  
  10860.  AAD is used to prepare two unpacked BCD digits (least significant in AL,
  10861.  most significant in AH) for a division operation which will yield an
  10862.  unpacked result. This is accomplished by setting AL to AL + (10 * AH), and
  10863.  then setting AH to 0. This leaves AX equal to the binary equivalent of the
  10864.  original unpacked 2-digit number.
  10865.  
  10866.  Protected Mode Exceptions
  10867.  
  10868.  None
  10869.  
  10870.  Real Address Mode Exceptions
  10871.  
  10872.  None
  10873.  
  10874.  
  10875.  AAM──ASCII Adjust AX After Multiply
  10876.  
  10877.  Opcode     Instruction      Clocks       Description
  10878.  
  10879.   D4 0A      AAM              16          ASCII adjust AX after multiply
  10880.  
  10881.  Flags Modified
  10882.  
  10883.  Sign, zero, parity
  10884.  
  10885.  Flags Undefined
  10886.  
  10887.  Overflow, auxiliary carry, carry
  10888.  
  10889.  Operation
  10890.  
  10891.  AAM should be used only after executing a MUL instruction between two
  10892.  unpacked BCD digits, leaving the result in the AX register. Since the result
  10893.  is less than one hundred, it is contained entirely in the AL register. AAM
  10894.  unpacks the AL result by dividing AL by ten, leaving the quotient (most
  10895.  significant digit) in AH, and the remainder (least significant digit) in
  10896.  AL.
  10897.  
  10898.  Protected Mode Exceptions
  10899.  
  10900.  None
  10901.  
  10902.  Real Address Mode Exceptions
  10903.  
  10904.  None
  10905.  
  10906.  
  10907.  AAS──ASCII Adjust AL After Subtraction
  10908.  
  10909.  Opcode      Instruction      Clocks       Description
  10910.  
  10911.   3F          AAS               3          ASCII adjust AL after subtraction
  10912.  
  10913.  Flags Modified
  10914.  
  10915.  Auxiliary carry, carry
  10916.  
  10917.  Flags Undefined
  10918.  
  10919.  Overflow, sign, zero, parity
  10920.  
  10921.  Operation
  10922.  
  10923.  AAS should be executed only after a subtraction instruction which left the
  10924.  byte result in the AL register. The lower nibbles of the operands to the SUB
  10925.  instruction should have been in the range 0 through 9 (BCD digits). In this
  10926.  case, the AAS instruction will adjust AL to contain the correct decimal
  10927.  digit result. If the subtraction produced a decimal carry, the AH register
  10928.  is decremented, and the carry and auxiliary carry flags are set to 1. If
  10929.  there was no decimal carry, the carry and auxiliary carry flags are set to
  10930.  0, and AH is unchanged. In any case, AL is left with its top nibble set to
  10931.  0. To convert AL to an ASCII result, you can follow the AAS instruction with
  10932.  OR AL,30H.
  10933.  
  10934.  The precise definition of AAS is as follows: if the lower four bits of AL
  10935.  are greater than 9, or if the auxiliary carry flag is 1, then decrement AL
  10936.  by 6, AH by 1, and set the carry and auxiliary carry flags. Otherwise, reset
  10937.  the carry and auxiliary carry flags. In any case, conclude the AAS
  10938.  operation by setting the upper four bits of AL to zero.
  10939.  
  10940.  Protected Mode Exceptions
  10941.  
  10942.  None
  10943.  
  10944.  Real Address Mode Exceptions
  10945.  
  10946.  None
  10947.  
  10948.  
  10949.  ADC/ADD──Integer Addition
  10950.  
  10951. ╓┌─────────┌────────────┌──────────┌─────────────────────────────────────────
  10952.  Opcode    Instruction  Clocks     Description
  10953.  
  10954.  10 /r      ADC eb,rb   2,mem=7    Add with carry byte register into EA byte
  10955.  11 /r      ADC ew,rw   2,mem=7    Add with carry word register into EA word
  10956.  12 /r      ADC rb,eb   2,mem=7    Add with carry EA byte into byte register
  10957.  13 /r      ADC rw,ew   2,mem=7    Add with carry EA word into word register
  10958.  14 db      ADC AL,db   3          Add with carry immediate byte into AL
  10959.  15 dw      ADC AX,dw   3          Add with carry immediate word into AX
  10960.  80 /2 db   ADC eb,db   3,mem=7    Add with carry immediate byte into EA byte
  10961.  81 /2 dw   ADC ew,dw   3,mem=7    Add with carry immediate word into EA word
  10962.  83 /2 db   ADC ew,db   3,mem=7    Add with carry immediate byte into EA word
  10963.  00 /r      ADD eb,rb   2,mem=7    Add byte register into EA byte
  10964.  Opcode    Instruction  Clocks     Description
  10965. 00 /r      ADD eb,rb   2,mem=7    Add byte register into EA byte
  10966.  01 /r      ADD ew,rw   2,mem=7    Add word register into EA word
  10967.  02 /r      ADD rb,eb   2,mem=7    Add EA byte into byte register
  10968.  03 /r      ADD rw,ew   2,mem=7    Add EA word into word register
  10969.  04 db      ADD AL,db   3          Add immediate byte into AL
  10970.  05 dw      ADD AX,dw   3          Add immediate word into AX
  10971.  80 /0 db   ADD eb,db   3,mem=7    Add immediate byte into EA byte
  10972.  81 /0 dw   ADD ew,dw   3,mem=7    Add immediate word into EA word
  10973.  83 /0 db   ADD ew,db   3,mem=7    Add immediate byte into EA word
  10974.  
  10975.  
  10976.  Flags Modified
  10977.  
  10978.  Overflow, sign, zero, auxiliary carry, parity, carry
  10979.  
  10980.  Flags Undefined
  10981.  
  10982.  None
  10983.  
  10984.  Operation
  10985.  
  10986.  ADD and ADC perform an integer addition on the two operands. The ADC
  10987.  instruction also adds in the initial state of the carry flag. The result of
  10988.  the addition goes to the first operand. ADC is usually executed as part of a
  10989.  multi-byte or multi-word addition operation.
  10990.  
  10991.  When a byte immediate value is added to a word operand, the immediate value
  10992.  is first sign-extended.
  10993.  
  10994.  Protected Mode Exceptions
  10995.  
  10996.  #GP(0) if the result is in a non-writable segment. #GP(0) for an illegal
  10997.  memory operand effective address in the CS, DS, or ES segments; #SS(0) for
  10998.  an illegal address in the SS segment.
  10999.  
  11000.  Real Address Mode Exceptions
  11001.  
  11002.  Interrupt 13 for a word operand at offset 0FFFFH.
  11003.  
  11004.  
  11005.  AND──Logical AND
  11006.  
  11007.  Opcode     Instruction   Clocks     Description
  11008.  
  11009.  20 /r      AND eb,rb     2,mem=7    Logical-AND byte register into EA byte
  11010.  21 /r      AND ew,rw     2,mem=7    Logical-AND word register into EA word
  11011.  22 /r      AND rb,eb     2,mem=7    Logical-AND EA byte into byte register
  11012.  23 /r      AND rw,ew     2,mem=7    Logical-AND EA word into word register
  11013.  24 db      AND AL,db     3          Logical-AND immediate byte into AL
  11014.  25 dw      AND AX,dw     3          Logical-AND immediate word into AX
  11015.  80 /4 db   AND eb,db     3,mem=7    Logical-AND immediate byte into EA byte
  11016.  81 /4 dw   AND ew,dw     3,mem=7    Logical-AND immediate word into EA word
  11017.  
  11018.  Flags Modified
  11019.  
  11020.  Overflow=0, sign, zero, parity, carry=0
  11021.  
  11022.  Flags Undefined
  11023.  
  11024.  Auxiliary carry
  11025.  
  11026.  Operation
  11027.  
  11028.  Each bit of the result is a 1 if both corresponding bits of the operands
  11029.  were 1; it is 0 otherwise.
  11030.  
  11031.  Protected Mode Exceptions
  11032.  
  11033.  #GP(0) if the result is in a non-writable segment. #GP(0) for an illegal
  11034.  memory operand effective address in the CS, DS, or ES segments; #SS(0) for
  11035.  an illegal address in the SS segment.
  11036.  
  11037.  Real Address Mode Exceptions
  11038.  
  11039.  Interrupt 13 for a word operand at offset 0FFFFH.
  11040.  
  11041.  
  11042.  ARPL──Adjust RPL Field of Selector
  11043.  
  11044.  Opcode  Instruction   Clocks       Description
  11045.  
  11046.  63 /r    ARPL ew,rw   10,mem=11    Adjust RPL of EA word not less than
  11047.                                     RPL of rw
  11048.  
  11049.  Flags Modified
  11050.  
  11051.  Zero
  11052.  
  11053.  Flags Undefined
  11054.  
  11055.  None
  11056.  
  11057.  Operation
  11058.  
  11059.  The ARPL instruction has two operands. The first operand is a 16-bit memory
  11060.  variable or word register that contains the value of a selector. The second
  11061.  operand is a word register. If the RPL field (bottom two bits) of the first
  11062.  operand is less than the RPL field of the second operand, then the zero
  11063.  flag is set to 1 and the RPL field of the first operand is increased to
  11064.  match the second RPL. Otherwise, the zero flag is set to 0 and no change is
  11065.  made to the first operand.
  11066.  
  11067.  ARPL appears in operating systems software, not in applications programs.
  11068.  It is used to guarantee that a selector parameter to a subroutine does not
  11069.  request more privilege than the caller was entitled to. The second operand
  11070.  used by ARPL would normally be a register that contains the CS selector
  11071.  value of the caller.
  11072.  
  11073.  Protected Mode Exceptions
  11074.  
  11075.  #GP(0) if the result is in a non-writable segment. #GP(0) for an illegal
  11076.  memory operand effective address in the CS, DS, or ES segments; #SS(0) for
  11077.  an illegal address in the SS segment.
  11078.  
  11079.  Real Address Mode Exceptions
  11080.  
  11081.  Interrupt 6. ARPL is not recognized in Real Address mode.
  11082.  
  11083.  
  11084.  BOUND──Check Array Index Against Bounds
  11085.  
  11086.  Opcode     Instruction     Clocks      Description
  11087.  
  11088.   62 /r      BOUND rw,md     noj=13     INT 5 if rw not within bounds
  11089.  
  11090.  Flags Modified
  11091.  
  11092.  None
  11093.  
  11094.  Flags Undefined
  11095.  
  11096.  None
  11097.  
  11098.  Operation
  11099.  
  11100.  BOUND is used to ensure that a signed array index is within the limits
  11101.  defined by a two-word block of memory. The first operand (a register) must
  11102.  be greater than or equal to the first word in memory, and less than or equal
  11103.  to the second word in memory. If the register is not within the bounds, an
  11104.  INTERRUPT 5 occurs.
  11105.  
  11106.  The two-word block might typically be found just before the array itself
  11107.  and therefore would be accessible at a constant offset of -4 from the array,
  11108.  simplifying the addressing.
  11109.  
  11110.  Protected Mode Exceptions
  11111.  
  11112.  INTERRUPT 5 if the bounds test fails, as described above. #GP(0) for an
  11113.  illegal memory operand effective address in the CS, DS, or ES segments;
  11114.  #SS(0) for an illegal address in the SS segment.
  11115.  
  11116.  The second operand must be a memory operand, not a register. If the BOUND
  11117.  instruction is executed with a ModRM byte representing a register second
  11118.  operand, then fault #UD will occur.
  11119.  
  11120.  Real Address Mode Exceptions
  11121.  
  11122.  INTERRUPT 5 if the bounds test fails, as described above. Interrupt 13 for
  11123.  a second operand at offset 0FFFDH or higher. Interrupt 6 if the second
  11124.  operand is a register, as described in the paragraph above.
  11125.  
  11126.  
  11127.  CALL──Call Procedure
  11128.  
  11129. ╓┌───────┌─────────────┌─────────────┌───────────────────────────────────────╖
  11130.  Opcode  Instruction   Clocks        Description
  11131.  
  11132.  Opcode  Instruction   Clocks        Description
  11133.  
  11134.  E8 cw      CALL cw       7          Call near, offset relative to
  11135.                                      next instruction
  11136.  FF /2      CALL ew       7,mem=11   Call near, offset absolute at EA word
  11137.  9A cd      CALL cd       13,pm=26   Call inter-segment, immediate
  11138.                                      4-byte address
  11139.  9A cd      CALL cd       41         Call gate, same privilege
  11140.  9A cd      CALL cd       82         Call gate, more privilege,
  11141.                                      no parameters
  11142.  9A cd      CALL cd       86+4X      Call gate, more privilege,
  11143.                                      X parameters
  11144.  9A cd      CALL cd       177        Call via Task State Segment
  11145.  9A cd      CALL cd       182        Call via task gate
  11146.  FF /3      CALL ed       16,mem=29  Call inter-segment, address at
  11147.                                      EA doubleword
  11148.  FF /3      CALL ed       44         Call gate, same privilege
  11149.  FF /3      CALL ed       83         Call gate, more privilege,
  11150.                                      no parameters
  11151.  FF /3      CALL ed       90+4X      Call gate, more privilege,
  11152.                                      X parameters
  11153.  Opcode  Instruction   Clocks        Description
  11154.                                     X parameters
  11155.  FF /3      CALL ed       180        Call via Task State Segment
  11156.  FF /3      CALL ed       185        Call via task gate
  11157.  
  11158.  
  11159.  Flags Modified
  11160.  
  11161.  None, except when a task switch occurs
  11162.  
  11163.  Flags Undefined
  11164.  
  11165.  None
  11166.  
  11167.  Operation
  11168.  
  11169.  The CALL instruction causes the procedure named in the operand to be
  11170.  executed. When the procedure is complete (a return instruction is executed
  11171.  within the procedure), execution continues at the instruction that follows
  11172.  the CALL instruction.
  11173.  
  11174.  The CALL cw form of the instruction adds modulo 65536 (the 2-byte operand)
  11175.  to the offset of the instruction following the CALL and sets IP to the
  11176.  resulting offset. The 2-byte offset of the instruction that follows the CALL
  11177.  is pushed onto the stack. It will be popped by a near RET instruction
  11178.  within the procedure. The CS register is not changed by this form.
  11179.  
  11180.  The CALL ew form of the instruction is the same as CALL cw except that the
  11181.  operand specifies a memory location from which the absolute 2-byte offset
  11182.  for the procedure is fetched.
  11183.  
  11184.  The CALL cd form of the instruction uses the 4-byte operand as a pointer to
  11185.  the procedure called. The CALL ed form fetches the long pointer from the
  11186.  memory location specified. Both long pointer forms consult the AR byte in
  11187.  the descriptor indexed by the selector part of the long pointer. The AR byte
  11188.  can indicate one of the following descriptor types:
  11189.  
  11190.    1.  Code Segment──The access rights are checked, the return pointer is
  11191.        pushed onto the stack, and the procedure is jumped to.
  11192.  
  11193.    2.  Call Gate──The offset part of the pointer is ignored. Instead, the
  11194.        entire address of the procedure is taken from the call gate descriptor
  11195.        entry. If the routine being entered is more privileged, then a new
  11196.        stack (both SS and SP) is loaded from the task state segment for the
  11197.        new privilege level, and parameters determined by the wordcount field
  11198.        of the call gate are copied from the old stack to the new stack.
  11199.  
  11200.    3.  Task Gate──The current task's context is saved in its Task State
  11201.        Segment (TSS), and the TSS named in the task-gate is used to load the
  11202.        new context. The selector for the outgoing task (from TR) is stored
  11203.        into the new TSS's link field, and the new task's Nested Task flag is
  11204.        set. The outgoing task is left marked busy, the new TSS is marked
  11205.        busy, and execution resumes at the point at which the new task was
  11206.        last suspended.
  11207.  
  11208.    4.  Task State Segment──The current task is suspended and the new task
  11209.        initiated as in 3 above except that there is no intervening gate.
  11210.  
  11211.  For long calls involving no task switch, the return link is the pointer of
  11212.  the instruction that follows the CALL, i.e., the caller's CS and updated IP.
  11213.  Task switches invoked by CALLs are linked by storing the outgoing task's TSS
  11214.  selector in the incoming TSS's link field and setting the Nested Task flag
  11215.  in the new task. Nested tasks must be terminated by an IRET. IRET releases
  11216.  the nested task and follows the back link to the calling task if the NT flag
  11217.  is set.
  11218.  
  11219.  A precise list of the protection checks made and the actions taken is given
  11220.  by the following list:
  11221.  
  11222.  CALL FAR:
  11223.    If indirect then check access of EA doubleword #GP(0) if limit violation
  11224.    New CS selector must not be null else #GP(0)
  11225.    Check that new CS selector index is within its descriptor table limits;
  11226.    else #GP (new CS selector)
  11227.    Examine AR byte of selected descriptor for various legal values:
  11228.  
  11229.      CALL CONFORMING CODE SEGMENT:
  11230.        DPL must be ≤ CPL else #GP (code segment selector)
  11231.        Segment must be PRESENT else #NP (code segment selector)
  11232.        Stack must be big enough for return address else #SS(0)
  11233.        IP must be in code segment limit else #GP(0)
  11234.        Load code segment descriptor into CS cache
  11235.        Load CS with new code segment selector
  11236.        Load IP with new offset
  11237.  
  11238.      CALL NONCONFORMING CODE SEGMENT:
  11239.        RPL must be ≤ CPL else #GP (code segment selector)
  11240.        DPL must be = CPL else #GP (code segment selector)
  11241.        Segment must be PRESENT else #NP (code segment selector)
  11242.        Stack must be big enough for return address else #SS(0)
  11243.        IP must be in code segment limit else #GP(0)
  11244.        Load code segment descriptor into CS cache
  11245.        Load CS with new code segment selector
  11246.        Set RPL of CS to CPL
  11247.        Load IP with new offset
  11248.  
  11249.      CALL TO CALL GATE:
  11250.        Call gate DPL must be ≥ CPL else #GP (call gate selector)
  11251.        Call gate DPL must be ≥ RPL else #GP (call gate selector)
  11252.        Call gate must be PRESENT else #NP (call gate selector)
  11253.        Examine code segment selector in call gate descriptor:
  11254.          Selector must not be null else #GP(0)
  11255.          Selector must be within its descriptor table limits else #GP (code
  11256.          segment selector)
  11257.          AR byte of selected descriptor must indicate code segment else #GP
  11258.          (code segment selector)
  11259.          DPL of selected descriptor must be ≤ CPL else #GP(code segment
  11260.          selector)
  11261.          If non-conforming code segment and DPL < CPL then
  11262.  
  11263.      CALL GATE TO MORE PRIVILEGE:
  11264.      Get new SS selector for new privilege level from TSS
  11265.      Check selector and descriptor for new SS:
  11266.        Selector must not be null else #TS(0)
  11267.        Selector index must be within its descriptor table limits else #TS
  11268.        (SS selector)
  11269.        Selector's RPL must equal DPL of code segment else #TS (SS selector)
  11270.        Stack segment DPL must equal DPL of code segment else #TS
  11271.        (SS selector)
  11272.        Descriptor must indicate writable data segment else #TS (SS selector)
  11273.        Segment PRESENT else #SS (SS selector)
  11274.          New stack must have room for parameters plus 8 bytes else #SS(0)
  11275.          IP must be in code segment limit else #GP(0)
  11276.          Load new SS:SP value from TSS
  11277.          Load new CS:IP value from gate
  11278.          Load CS descriptor
  11279.          Load SS descriptor
  11280.          Push long pointer of old stack onto new stack
  11281.          Get word count from call gate, mask to 5 bits
  11282.          Copy parameters from old stack onto new stack
  11283.          Push return address onto new stack
  11284.          Set CPL to stack segment DPL
  11285.          Set RPL of CS to CPL
  11286.        Else
  11287.          CALL GATE TO SAME PRIVILEGE:
  11288.          Stack must have room for 4-byte return address else #SS(0)
  11289.          IP must be in code segment limit else #GP(0)
  11290.          Load CS:IP from gate
  11291.          Push return address onto stack
  11292.          Load code segment descriptor into CS-cache
  11293.          Set RPL of CS to CPL
  11294.  
  11295.        CALL TASK GATE:
  11296.          Task gate DPL must be ≥ CPL else #GP (gate selector)
  11297.          Task gate DPL must be ≥ RPL else #GP (gate selector)
  11298.          Task Gate must be PRESENT else #NP (gate selector)
  11299.          Examine selector to TSS, given in Task Gate descriptor:
  11300.             Must specify global in the local/global bit else #GP
  11301.             (TSS selector)
  11302.             Index must be within GDT limits else #GP (TSS selector)
  11303.             TSS descriptor AR byte must specify available TSS (bottom bits
  11304.             00001) else #GP (TSS selector)
  11305.             Task State Segment must be PRESENT else #NP (TSS selector)
  11306.          SWITCH_TASKS with nesting to TSS
  11307.          IP must be in code segment limit else #GP(0)
  11308.  
  11309.        TASK STATE SEGMENT:
  11310.           TSS DPL must be ≥ CPL else #GP (TSS selector)
  11311.           TSS DPL must be ≥ RPL else #GP (TSS selector)
  11312.           TSS descriptor AR byte must specify available TSS else #GP
  11313.           (TSS selector)
  11314.           Task State Segment must be PRESENT else #NP (TSS selector)
  11315.           SWITCH_TASKS with nesting to TSS
  11316.           IP must be in code segment limit else #GP(0)
  11317.  
  11318.        ELSE #GP (code segment selector)
  11319.  
  11320.  Protected Mode Exceptions
  11321.  
  11322.  FAR calls: #GP, #NP, #SS, and #TS, as indicated in the list above.
  11323.  
  11324.  NEAR direct calls: #GP(0) if procedure location is beyond the code segment
  11325.  limits.
  11326.  
  11327.  NEAR indirect CALL: #GP(0) for an illegal memory operand effective address
  11328.  in the CS, DS, or ES segments; #SS(0) for an illegal address in the SS
  11329.  segment. #GP if the indirect offset obtained is beyond the code segment
  11330.  limits.
  11331.  
  11332.  Real Address Mode Exceptions
  11333.  
  11334.  Interrupt 13 for a word operand at offset 0FFFFH.
  11335.  
  11336.  
  11337.  CBW──Convert Byte into Word
  11338.  
  11339.  Opcode   Instruction   Clocks    Description
  11340.  
  11341.   98       CBW           2        Convert byte into word (AH = top bit of AL)
  11342.  
  11343.  Flags Modified
  11344.  
  11345.  None
  11346.  
  11347.  Flags Undefined
  11348.  
  11349.  None
  11350.  
  11351.  Operation
  11352.  
  11353.  CBW converts the signed byte in AL to a signed word in AX. It does so by
  11354.  extending the top bit of AL into all of the bits of AH.
  11355.  
  11356.  Protected Mode Exceptions
  11357.  
  11358.  None
  11359.  
  11360.  Real Address Mode Exceptions
  11361.  
  11362.  None
  11363.  
  11364.  
  11365.  CLC──Clear Carry Flag
  11366.  
  11367.  Opcode     Instruction     Clocks       Description
  11368.  
  11369.   F8         CLC             2           Clear carry flag
  11370.  
  11371.  Flags Modified
  11372.  
  11373.  Carry=0
  11374.  
  11375.  Flags Undefined
  11376.  
  11377.  None
  11378.  
  11379.  Operation
  11380.  
  11381.  CLC sets the carry flag to zero. No other flags or registers are affected.
  11382.  
  11383.  Protected Mode Exceptions
  11384.  
  11385.  None
  11386.  
  11387.  Real Address Mode Exceptions
  11388.  
  11389.  None
  11390.  
  11391.  
  11392.  CLD──Clear Direction Flag
  11393.  
  11394.  Opcode  Instruction  Clocks   Description
  11395.  
  11396.   FC      CLD          2       Clear direction flag, SI and DI
  11397.                                will increment
  11398.  
  11399.  Flags Modified
  11400.  
  11401.  Direction=0
  11402.  
  11403.  Flags Undefined
  11404.  
  11405.  None
  11406.  
  11407.  Operation
  11408.  
  11409.  CLD clears the direction flag. No other flags or registers are affected.
  11410.  After CLD is executed, string operations will increment the index registers
  11411.  (SI and/or DI) that they use.
  11412.  
  11413.  Protected Mode Exceptions
  11414.  
  11415.  None
  11416.  
  11417.  Real Address Mode Exceptions
  11418.  
  11419.  None
  11420.  
  11421.  
  11422.  CLI──Clear Interrupt Flag
  11423.  
  11424.  Opcode   Instruction   Clocks      Description
  11425.  
  11426.   FA       CLI           3          Clear interrupt flag; interrupts disabled
  11427.  
  11428.  Flags Modified
  11429.  
  11430.  Interrupt=0
  11431.  
  11432.  Flags Undefined
  11433.  
  11434.  None
  11435.  
  11436.  Operation
  11437.  
  11438.  CLI clears the interrupt enable flag if the current privilege level is at
  11439.  least as privileged as IOPL. No other flags are affected. External
  11440.  interrupts will not be recognized at the end of the CLI instruction or
  11441.  thereafter until the interrupt flag is set.
  11442.  
  11443.  Protected Mode Exceptions
  11444.  
  11445.  #GP(0) if the current privilege level is bigger (has less privilege) than
  11446.  the IOPL in the flags register. IOPL specifies the least privileged level at
  11447.  which I/O may be performed.
  11448.  
  11449.  Real Address Mode Exceptions
  11450.  
  11451.  None
  11452.  
  11453.  
  11454.  CLTS──Clear Task Switched Flag
  11455.  
  11456.  Opcode     Instruction     Clocks      Description
  11457.  
  11458.   0F 06      CLTS            2          Clear task switched flag
  11459.  
  11460.  Flags Modified
  11461.  
  11462.  Task switched=0
  11463.  
  11464.  Flags Undefined
  11465.  
  11466.  None
  11467.  
  11468.  Operation
  11469.  
  11470.  CLTS clears the task switched flag in the Machine Status Word. This flag is
  11471.  set by the 80286 every time a task switch occurs. The TS flag is used to
  11472.  manage processor extensions as follows: every execution of a WAIT or an ESC
  11473.  instruction will be trapped if the MP flag of MSW is set and the task
  11474.  switched flag is set. Thus, if a processor extension is present and a task
  11475.  switch has been made since the last ESC instruction was begun, the processor
  11476.  extension's context must be saved before a new instruction can be issued.
  11477.  The fault routine will save the context and reset the task switched flag or
  11478.  place the task requesting the processor extension into a queue until the
  11479.  current processor extension instruction is completed.
  11480.  
  11481.  CLTS appears in operating systems software, not in applications programs.
  11482.  It is a privileged instruction that can only be executed at level 0.
  11483.  
  11484.  Protected Mode Exceptions
  11485.  
  11486.  #GP(0) if CLTS is executed with a current privilege level other than 0.
  11487.  
  11488.  Real Address Mode Exceptions
  11489.  
  11490.  None (valid in REAL ADDRESS MODE to allow power-up initialization for
  11491.  Protected Mode)
  11492.  
  11493.  
  11494.  CMC──Complement Carry Flag
  11495.  
  11496.  Opcode     Instruction    Clocks      Description
  11497.  
  11498.   F5         CMC            2          Complement carry flag
  11499.  
  11500.  Flags Modified
  11501.  
  11502.  Carry
  11503.  
  11504.  Flags Undefined
  11505.  
  11506.  None
  11507.  
  11508.  Operation
  11509.  
  11510.  CMC reverses the setting of the carry flag. No other flags are affected.
  11511.  
  11512.  Protected Mode Exceptions
  11513.  
  11514.  None
  11515.  
  11516.  Real Address Mode Exceptions
  11517.  
  11518.  None
  11519.  
  11520.  
  11521.  CMP──Compare Two Operands
  11522.  
  11523.  Opcode       Instruction     Clocks      Description
  11524.  
  11525.  3C db        CMP AL,db       3           Compare immediate byte from AL
  11526.  3D dw        CMP AX,dw       3           Compare immediate word from AX
  11527.  80 /7 db     CMP eb,db      3,mem=6      Compare immediate byte from EA byte
  11528.  38 /r        CMP eb,rb      2,mem=7      Compare byte register from EA byte
  11529.  83 /7 db     CMP ew,db      3,mem=6      Compare immediate byte from EA word
  11530.  81 /7 dw     CMP ew,dw      3,mem=6      Compare immediate word from EA word
  11531.  39 /r        CMP ew,rw      2,mem=7      Compare word register from EA word
  11532.  3A /r        CMP rb,eb      2,mem=6      Compare EA byte from byte register
  11533.  3B /r        CMP rw,ew      2,mem=6      Compare EA word from word register
  11534.  
  11535.  Flags Modified
  11536.  
  11537.  Overflow, sign, zero, auxiliary carry, parity, carry
  11538.  
  11539.  Flags Undefined
  11540.  
  11541.  None
  11542.  
  11543.  Operation
  11544.  
  11545.  CMP subtracts the second operand from the first operand, but it does not
  11546.  place the result anywhere. Only the flags are changed by this instruction.
  11547.  CMP is usually followed by a conditional jump instruction. See the "Jcond"
  11548.  instructions in this chapter for the list of signed and unsigned flag tests
  11549.  provided by the 80286.
  11550.  
  11551.  If a word operand is compared to an immediate byte value, the byte value is
  11552.  first sign-extended.
  11553.  
  11554.  Protected Mode Exceptions
  11555.  
  11556.  #GP(0) for an illegal memory operand effective address in the CS, DS, or ES
  11557.  segments; #SS(0) for an illegal address in the SS segment.
  11558.  
  11559.  Real Address Mode Exceptions
  11560.  
  11561.  Interrupt 13 for a word operand at offset 0FFFFH.
  11562.  
  11563.  
  11564.  CMPS/CMPSB/CMPSW──Compare string operands
  11565.  
  11566.  Opcode    Instruction   Clocks    Description
  11567.  
  11568.  A6        CMPS mb,mb    8         Compare bytes ES:[DI] from [SI]
  11569.  A6        CMPSB         8         Compare bytes ES:[DI] from DS:[SI]
  11570.  A7        CMPSW         8         Compare words ES:[DI] from DS:[SI]
  11571.  
  11572.  Flags Modified
  11573.  
  11574.  Overflow, sign, zero, auxiliary carry, parity, carry
  11575.  
  11576.  Flags Undefined
  11577.  
  11578.  None
  11579.  
  11580.  Operation
  11581.  
  11582.  CMPS compares the byte or word pointed to by SI with the byte or word
  11583.  pointed to by DI by performing the subtraction [SI] - [DI]. The result is
  11584.  not placed anywhere; only the flags reflect the result of the subtraction.
  11585.  The types of the operands to CMPS determine whether bytes or words are
  11586.  compared. The segment addressability of the first (SI) operand determines
  11587.  whether a segment override byte will be produced or whether the default
  11588.  segment register DS is used. The second (DI) operand must be addressible
  11589.  from the ES register; no segment override is possible.
  11590.  
  11591.  After the comparison is made, both SI and DI are automatically advanced. If
  11592.  the direction flag is 0 (CLD was executed), the registers increment; if the
  11593.  direction flag is 1 (STD was executed), the registers decrement. The
  11594.  registers increment or decrement by 1 if a byte was moved; by 2 if a word
  11595.  was moved.
  11596.  
  11597.  CMPS cn be preceded by the REPE or REPNE prefix for block comparison of CX
  11598.  bytes or words. Refer to the REP instruction for details of this operation.
  11599.  
  11600.  Protected Mode Exceptions
  11601.  
  11602.  #GP(0) for an illegal memory operand effective address in the CS, DS, or ES
  11603.  segments; #SS(0) for an illegal address in the SS segment.
  11604.  
  11605.  Real Address Mode Exceptions
  11606.  
  11607.  Interrupt 13 for a word operand at offset 0FFFFH.
  11608.  
  11609.  
  11610.  CWD──Convert Word to Doubleword
  11611.  
  11612.  Opcode   Instruction     Clocks      Description
  11613.  
  11614.  99       CWD             2           Convert word to doubleword (DX:AX = AX)
  11615.  
  11616.  Flags Modified
  11617.  
  11618.  None
  11619.  
  11620.  Flags Undefined
  11621.  
  11622.  None
  11623.  
  11624.  Operation
  11625.  
  11626.  CWD converts the signed word in AX to a signed doubleword in DX:AX. It does
  11627.  so by extending the top bit of AX into all the bits of DX.
  11628.  
  11629.  Protected Mode Exceptions
  11630.  
  11631.  None
  11632.  
  11633.  Real Address Mode Exceptions
  11634.  
  11635.  None
  11636.  
  11637.  
  11638.  DAA──Decimal Adjust AL After Addition
  11639.  
  11640.  Opcode  Instruction    Clocks    Description
  11641.  
  11642.  27      DAA            3         Decimal adjust AL after addition
  11643.  
  11644.  Flags Modified
  11645.  
  11646.  Sign, zero, auxiliary carry, parity, carry
  11647.  
  11648.  Flags Undefined
  11649.  
  11650.  Overflow
  11651.  
  11652.  Operation
  11653.  
  11654.  DAA should be executed only after an ADD instruction which leaves a
  11655.  two-BCD-digit byte result in the AL register. The ADD operands should
  11656.  consist of two packed BCD digits. In this case, the DAA instruction will
  11657.  adjust AL to contain the correct two-digit packed decimal result.
  11658.  
  11659.  The precise definition of DAA is as follows:
  11660.  
  11661.    1.  If the lower 4 bits of AL are greater than nine, or if the auxiliary
  11662.        carry flag is 1, then increment AL by 6, and set the auxiliary carry
  11663.        flag. Otherwise, reset the auxiliary carry flag.
  11664.  
  11665.    2.  If AL is now greater than 9FH, or if the carry flag is set, then
  11666.        increment AL by 60H, and set the carry flag. Otherwise, clear the
  11667.        carry flag.
  11668.  
  11669.  Protected Mode Exceptions
  11670.  
  11671.  None
  11672.  
  11673.  Real Address Mode Exceptions
  11674.  
  11675.  None
  11676.  
  11677.  
  11678.  DAS──Decimal Adjust AL After Subtraction
  11679.  
  11680.  Opcode    Instruction     Clocks     Description
  11681.  
  11682.  2F        DAS             3          Decimal adjust AL after subtraction
  11683.  
  11684.  Flags Modified
  11685.  
  11686.  Sign, zero, auxiliary carry, parity, carry
  11687.  
  11688.  Flags Undefined
  11689.  
  11690.  Overflow
  11691.  
  11692.  Operation
  11693.  
  11694.  DAS should be executed only after a subtraction instruction which leaves a
  11695.  two-BCD-digit byte result in the AL register. The operands should consist of
  11696.  two packed BCD digits. In this case, the DAS instruction will adjust AL to
  11697.  contain the correct packed two-digit decimal result.
  11698.  
  11699.  The precise definition of DAS is as follows:
  11700.  
  11701.    1.  If the lower four bits of AL are greater than 9, or if the auxiliary
  11702.        carry flag is 1, then decrement AL by 6, and set the auxiliary carry
  11703.        flag. Otherwise, reset the auxiliary carry flag.
  11704.  
  11705.    2.  If AL is now greater than 9FH, or if the carry flag is set, then
  11706.        decrement AL by 60H, and set the carry flag. Otherwise, clear the
  11707.        carry flag.
  11708.  
  11709.  Protected Mode Exceptions
  11710.  
  11711.  None
  11712.  
  11713.  Real Address Mode Exceptions
  11714.  
  11715.  None
  11716.  
  11717.  
  11718.  DEC──Decrement by 1
  11719.  
  11720.  Opcode      Instruction    Clocks        Description
  11721.  
  11722.  FE   /1     DEC eb         2,mem=7       Decrement EA byte by 1
  11723.  FF   /1     DEC ew         2,mem=7       Decrement EA word by 1
  11724.  48+  rw     DEC rw         2             Decrement word register by 1
  11725.  
  11726.  Flags Modified
  11727.  
  11728.  Overflow, sign, zero, auxiliary carry, parity
  11729.  
  11730.  Flags Undefined
  11731.  
  11732.  None
  11733.  
  11734.  Operation
  11735.  
  11736.  1 is subtracted from the operand. Note that the carry flag is not changed
  11737.  by this instruction. If you want the carry flag set, use the SUB instruction
  11738.  with a second operand of 1.
  11739.  
  11740.  Protected Mode Exceptions
  11741.  
  11742.  #GP(0) if the operand is in a non-writable segment. #GP(0) for an illegal
  11743.  memory operand effective address in the CS, DS, or ES segments; #SS(0) for
  11744.  an illegal address in the SS segment.
  11745.  
  11746.  Real Address Mode Exceptions
  11747.  
  11748.  Interrupt 13 for a word operand at offset 0FFFFH.
  11749.  
  11750.  
  11751.  DIV──Unsigned Divide
  11752.  
  11753.  Opcode     Instruction     Clocks         Description
  11754.  
  11755.  F6  /6     DIV eb          14,mem=17      Unsigned divide AX by EA byte
  11756.  F7  /6     DIV ew          22,mem=25      Unsigned divide DX:AX by
  11757.                                            EA word
  11758.  
  11759.  Flags Modified
  11760.  
  11761.  None
  11762.  
  11763.  Flags Undefined
  11764.  
  11765.  Overflow, sign, zero, auxiliary carry, parity, carry
  11766.  
  11767.  Operation
  11768.  
  11769.  DIV performs an unsigned divide. The dividend is implicit; only the divisor
  11770.  is given as an operand. If the source operand is a BYTE operand, divide AX
  11771.  by the byte. The quotient is stored in AL, and the remainder is stored in
  11772.  AH. If the source operand is a WORD operand, divide DX:AX by the word. The
  11773.  high-order 16 bits of the dividend are kept in DX. The quotient is stored
  11774.  in AX, and the remainder is stored in DX. Non-integral quotients are
  11775.  truncated towards 0. The remainder is always less than the dividend.
  11776.  
  11777.  Protected Mode Exceptions
  11778.  
  11779.  Interrupt 0 if the quotient is too big to fit in the designated register
  11780.  (AL or AX), or if the divisor is zero. #GP(0) for an illegal memory operand
  11781.  effective address in the CS, DS, or ES segments; #SS(0) for an illegal
  11782.  address in the SS segment.
  11783.  
  11784.  Real Address Mode Exceptions
  11785.  
  11786.  Interrupt 0 if the quotient is too big to fit in the designated register
  11787.  (AL or AX), or if the divisor is zero. Interrupt 13 for a word operand at
  11788.  offset 0FFFFH.
  11789.  
  11790.  
  11791.  ENTER──Make Stack Frame for Procedure Parameters
  11792.  
  11793.  Opcode     Instruction    Clocks   Description
  11794.  
  11795.  C8 dw 00   ENTER dw,0     11       Make stack frame for procedure parameters
  11796.  C8 dw 01   ENTER dw,1     15       Make stack frame for procedure parameters
  11797.  C8 dw db   ENTER dw,db    12+4db   Make stack frame for procedure parameters
  11798.  
  11799.  Flags Modified
  11800.  
  11801.  None
  11802.  
  11803.  Flags Undefined
  11804.  
  11805.  None
  11806.  
  11807.  Operation
  11808.  
  11809.  ENTER is used to create the stack frame required by most block-structured
  11810.  high-level languages. The first operand specifies how many bytes of dynamic
  11811.  storage are to be allocated on the stack for the routine being entered. The
  11812.  second operand gives the lexical nesting level of the routine within the
  11813.  high-level-language source code. It determines how many stack frame
  11814.  pointers are copied into the new stack frame from the preceding frame. BP is
  11815.  used as the current stack frame pointer.
  11816.  
  11817.  If the second operand is 0, ENTER pushes BP, sets BP to SP, and subtracts
  11818.  the first operand from SP.
  11819.  
  11820.  For example, a procedure with 12 bytes of local variables would have an
  11821.  ENTER 12,0 instruction at its entry point and a LEAVE instruction before
  11822.  every RET. The 12 local bytes would be addressed as negative offsets from
  11823.  [BP]. See also section 4.2.
  11824.  
  11825.  The formal definition of the ENTER instruction for all cases is given by
  11826.  the following listing. LEVEL denotes the value of the second operand.
  11827.  
  11828.  LEVEL:=LEVEL MOD 32
  11829.  Push BP
  11830.  Set a temporary value FRAME_PTR := SP
  11831.  If LEVEL > 0 then
  11832.      Repeat (LEVEL-1) times:
  11833.         BP := BP - 2
  11834.         Push the word pointed to by BP
  11835.      End repeat
  11836.      Push FRAME_PTR
  11837.  End if
  11838.  BP := FRAME-PTR
  11839.  SP := SP - first operand
  11840.  
  11841.  Protected Mode Exceptions
  11842.  
  11843.  #SS(0) if SP were to go outside of the stack limit within any part of the
  11844.  instruction execution.
  11845.  
  11846.  Real Address Mode Exceptions
  11847.  
  11848.  None
  11849.  
  11850.  
  11851.  HLT──Halt
  11852.  
  11853.  Opcode    Instruction    Clocks    Description
  11854.  
  11855.  F4        HLT            2         Halt
  11856.  
  11857.  Flags Modified
  11858.  
  11859.  None
  11860.  
  11861.  Flags Undefined
  11862.  
  11863.  None
  11864.  
  11865.  Operation
  11866.  
  11867.  Successful execution of HLT causes the 80286 to cease executing
  11868.  instructions and to enter a HALT state. Execution resumes only upon receipt
  11869.  of an enabled interrupt or a reset. If an interrupt is used to resume
  11870.  program execution after HLT, the saved CS:IP value will point to the
  11871.  instruction that follows HLT.
  11872.  
  11873.  Protected Mode Exceptions
  11874.  
  11875.  HLT is a privileged instruction. #GP(0) if the current privilege level is
  11876.  not 0.
  11877.  
  11878.  Real Address Mode Exceptions
  11879.  
  11880.  None
  11881.  
  11882.  
  11883.  IDIV──Signed Divide
  11884.  
  11885.  Opcode   Instruction    Clocks       Description
  11886.  
  11887.  F6 /7    IDIV eb       17,mem=20     Signed divide AX by EA byte
  11888.                                       (AL=Quo,AH=Rem)
  11889.  F7 /7    IDIV ew       25,mem=28     Signed divide DX:AX by
  11890.                                       EA word (AX=Quo,DX=Rem)
  11891.  
  11892.  Flags Modified
  11893.  
  11894.  None
  11895.  
  11896.  Flags Undefined
  11897.  
  11898.  Overflow, sign, zero, auxiliary carry, parity, carry
  11899.  
  11900.  Operation
  11901.  
  11902.  IDIV performs a signed divide. The dividend is implicit; only the divisor
  11903.  is given as an operand. If the source operand is a BYTE operand, divide AX
  11904.  by the byte. The quotient is stored in AL, and the remainder is stored in
  11905.  AH. If the source operand is a WORD operand, divide DX:AX by the word. The
  11906.  high-order 16 bits of the dividend are in DX. The quotient is stored in AX,
  11907.  and the remainder is stored in DX. Non-integral quotients are truncated
  11908.  towards 0. The remainder has the same sign as the dividend and always has
  11909.  less magnitude than the dividend.
  11910.  
  11911.  Protected Mode Exceptions
  11912.  
  11913.  Interrupt 0 if the quotient is too big to fit in the designated register
  11914.  (AL or AX), or if the divisor is 0. #GP(0) for an illegal memory operand
  11915.  effective address in the CS, DS, or ES segments; #SS(0) for an illegal
  11916.  address in the SS segment.
  11917.  
  11918.  Real Address Mode Exceptions
  11919.  
  11920.  Interrupt 0 if the quotient is too big to fit in the designated register
  11921.  (AL or AX), or if the divisor is 0. Interrupt 13 for a word operand at
  11922.  offset 0FFFFH.
  11923.  
  11924.  
  11925.  IMUL──Signed Multiply
  11926.  
  11927.  Opcode     Instruction    Clocks      Description
  11928.  
  11929.  F6 /5      IMUL eb        13,mem=16   Signed multiply (AX = AL * EA byte)
  11930.  F7 /5      IMUL ew        21,mem=24   Signed multiply (DXAX = AX * EA word)
  11931.  6B /r db   IMUL rw,db     21,mem=24   Signed multiply imm. byte
  11932.                                        into word reg.
  11933.  69 /r dw   IMUL rw,ew,dw  21,mem=24   Signed multiply
  11934.                                        (rw = EA word * imm. word)
  11935.  6B /r db   IMUL rw,ew,db  21,mem=24   Signed multiply
  11936.                                        (rw = EA word * imm. byte)
  11937.  
  11938.  Flags Modified
  11939.  
  11940.  Overflow, carry
  11941.  
  11942.  Flags Undefined
  11943.  
  11944.  Sign, zero, auxiliary carry, parity
  11945.  
  11946.  Operation
  11947.  
  11948.  IMUL performs signed multiplication. If IMUL has a single byte source
  11949.  operand, then the source is multiplied by AL and the 16-bit signed result is
  11950.  left in AX. Carry and overflow are set to 0 if AH is a sign extension of AL;
  11951.  they are set to 1 otherwise.
  11952.  
  11953.  If IMUL has a single word source operand, then the source operand is
  11954.  multiplied by AX and the 32-bit signed result is left in DX:AX. DX contains
  11955.  the high-order 16 bits of the product. Carry and overflow are set to 0 if DX
  11956.  is a sign extension of AX; they are set to 1 otherwise.
  11957.  
  11958.  If IMUL has three operands, then the second operand (an effective address
  11959.  word) is multiplied by the third operand (an immediate word), and the 16
  11960.  bits of the result are placed in the first operand (a word register). Carry
  11961.  and overflow are set to 0 if the result fits in a signed word (between
  11962.  -32768 and +32767, inclusive); they are set to 1 otherwise.
  11963.  
  11964.  The low 16 bits of the product of a 16-bit signed multiply are the same as
  11965.  those of an unsigned multiply. The three operand IMUL instruction can be
  11966.  used for unsigned operands as well.
  11967.  
  11968.  Protected Mode Exceptions
  11969.  
  11970.  #GP(0) for an illegal memory operand effective address in the CS, DS, or ES
  11971.  segments; #SS(0) for an illegal address in the SS segment.
  11972.  
  11973.  Real Address Mode Exceptions
  11974.  
  11975.  Interrupt 13 for a word operand at offset 0FFFFH.
  11976.  
  11977.  
  11978.  IN──Input from Port
  11979.  
  11980.  Opcode    Instruction   Clocks     Description
  11981.  
  11982.  E4 db     IN AL,db       5         Input byte from immediate portinto AL
  11983.  EC        IN AL,DX       5         Input byte from port DX into AL
  11984.  E5 db     IN AX,db       5         Input word from immediate portinto AX
  11985.  ED        IN AX,DX       5         Input word from port DX into AX
  11986.  
  11987.  Flags Modified
  11988.  
  11989.  None
  11990.  
  11991.  Flags Undefined
  11992.  
  11993.  None
  11994.  
  11995.  Operation
  11996.  
  11997.  IN transfers a data byte or data word from the port numbered by the second
  11998.  operand into the register (AL or AX) given as the first operand. You can
  11999.  access any port from 0 to 65535 by placing the port number in the DX
  12000.  register then using an IN instruction with DX as the second parameter.
  12001.  These I/O instructions can be shortened by using an 8-bit port I/O in the
  12002.  instruction. The upper 8 bits of the port address will be zero when an 8-bit
  12003.  port I/O is used.
  12004.  
  12005.  Intel has reserved I/O port addresses 00F8H through 00FFH; they should not
  12006.  be used.
  12007.  
  12008.  Protected Mode Exceptions
  12009.  
  12010.  #GP(0) if the current privilege level is bigger (has less privilege) than
  12011.  IOPL, which is the privilege level found in the flags register.
  12012.  
  12013.  Real Address Mode Exceptions
  12014.  
  12015.  None
  12016.  
  12017.  
  12018.  INC──Increment by 1
  12019.  
  12020.  Opcode    Instruction    Clocks        Description
  12021.  
  12022.  FE /0     INC eb         2,mem=7       Increment EA byte by 1
  12023.  FF /0     INC ew         2,mem=7       Increment EA word by 1
  12024.  40+rw     INC rw         2             Increment word register by 1
  12025.  
  12026.  Flags Modified
  12027.  
  12028.  Overflow, sign, zero, auxiliary carry, parity
  12029.  
  12030.  Flags Undefined
  12031.  
  12032.  None
  12033.  
  12034.  Operation
  12035.  
  12036.  1 is added to the operand. Note that the carry flag is not changed by this
  12037.  instruction. If you want the carry flag set, use the ADD instruction with a
  12038.  second operand of 1.
  12039.  
  12040.  Protected Mode Exceptions
  12041.  
  12042.  #GP(0) if the operand is in a non-writable segment. #GP(0) for an illegal
  12043.  memory operand effective address in the CS, DS, or ES segments; #SS(0) for
  12044.  an illegal address in the SS segment.
  12045.  
  12046.  Real Address Mode Exceptions
  12047.  
  12048.  Interrupt 13 for a word operand at offset 0FFFFH.
  12049.  
  12050.  
  12051.  INS/INSB/INSW──Input from Port to String
  12052.  
  12053.  Opcode  Instruction    Clocks    Description
  12054.  
  12055.  6C      INS eb,DX      5         Input byte from port DX into ES:[DI]
  12056.  6D      INS ew,DX      5         Input word from port DX into ES:[DI]
  12057.  6C      INSB           5         Input byte from port DX into ES:[DI]
  12058.  6D      INSW           5         Input word from port DX into ES:[DI]
  12059.  
  12060.  Flags Modified
  12061.  
  12062.  None
  12063.  
  12064.  Flags Undefined
  12065.  
  12066.  None
  12067.  
  12068.  Operation
  12069.  
  12070.  INS transfers data from the input port numbered by the DX register to the
  12071.  memory byte or word at ES:DI. The memory operand must be addressable from
  12072.  the ES register; no segment override ispossible.
  12073.  
  12074.  INS does not allow the specification of the port number as an immediate
  12075.  value. The port must be addressed through the DX register.
  12076.  
  12077.  After the transfer is made, DI is automatically advanced. If the direction
  12078.  flag is 0 (CLD was executed), DI increments; if the direction flag is 1 (STD
  12079.  was executed), DI decrements. DI increments or decrements by 1 if a byte was
  12080.  moved; by 2 if a word was moved.
  12081.  
  12082.  INS can be preceded by the REP prefix for block input of CX bytes or words.
  12083.  Refer to the REP instruction for details of this operation.
  12084.  
  12085.  Intel has reserved I/O port addresses 00F8H through 00FFH; they should not
  12086.  be used.
  12087.  
  12088.  Not all input port devices can handle the rate at which this instruction
  12089.  transfers input data to memory.
  12090.  
  12091.  Protected Mode Exceptions
  12092.  
  12093.  #GP(0) if CPL > IOPL. #GP(0) if the destination is in a non-writable
  12094.  segment. #GP(0) for an illegal memory operand effective address in the CS,
  12095.  DS, or ES segments; #SS(0) for an illegal address in the SS segment.
  12096.  
  12097.  Real Address Mode Exceptions
  12098.  
  12099.  Interrupt 13 for a word operand at offset 0FFFFH.
  12100.  
  12101.  
  12102.  INT/INTO──Call to Interrupt Procedure
  12103.  
  12104.  Opcode  Instruction   Clocks    Description
  12105.  
  12106.  CC      INT 3         23        Interrupt 3 (trap to debugger)
  12107.  CC      INT 3         40         Interrupt 3, protected mode, same privilege
  12108.  CC      INT 3         78         Interrupt 3, protected mode, more privilege
  12109.  CC      INT 3         167        Interrupt 3, protected mode, via task gate
  12110.  CD db   INT db        23        Interrupt numbered by immediate byte
  12111.  CD db   INT db        40         Interrupt, protected mode, same privilege
  12112.  CD db   INT db        78         Interrupt, protected mode, more privilege
  12113.  CD db   INT db        167        Interrupt, protected mode, via task gate
  12114.  CE      INTO          24,noj=3  Interrupt 4 if overflow flag is 1
  12115.  
  12116.  
  12117.  Flags Modified
  12118.  
  12119.  All if a task switch takes place; Trap Flag reset if no task switch takes
  12120.  place. Interrupt Flag is always reset in Real Mode, and reset in Protected
  12121.  Mode when INT references an interrupt gate.
  12122.  
  12123.  Flags Undefined
  12124.  
  12125.  None
  12126.  
  12127.  Operation
  12128.  
  12129.  The INT instruction generates via software a call to an interrupt
  12130.  procedure. The immediate operand, from 0 to 255, gives the index number into
  12131.  the Interrupt Descriptor Table of the interrupt routine to be called. In
  12132.  protected mode, the IDT consists of 8-byte descriptors; the descriptor for
  12133.  the interrupt invoked must indicate an interrupt gate, a trap gate, or a
  12134.  task gate. In real address mode, the IDT is an array of 4-byte long pointers
  12135.  at the fixed location 00000H.
  12136.  
  12137.  The INTO instruction is identical to the INT instruction except that the
  12138.  interrupt number is implicitly 4, and the interrupt is made only if the
  12139.  overflow flag of the 80286 is on. The clock counts for the four forms of
  12140.  INT db are valid for INTO, with the number of clocks increased by 1 for the
  12141.  overflow flag test.
  12142.  
  12143.  The first 32 interrupts are reserved by Intel for systems use. Some of
  12144.  these interrupts are exception handlers for internally-generated faults.
  12145.  Most of these exception handlers should not be invoked with the INT
  12146.  instruction.
  12147.  
  12148.  Generally, interrupts behave like far CALLs except that the flags register
  12149.  is pushed onto the stack before the return address. Interrupt procedures
  12150.  return via the IRET instruction, which pops the flags from the stack.
  12151.  
  12152.  In Real Address mode, INT pushes the flags, CS and the return IP onto the
  12153.  stack in that order, then resets the Trap Flag, then jumps to the long
  12154.  pointer indexed by the interrupt number, in the interrupt vector table.
  12155.  
  12156.  In Protected mode, INT also resets the Trap Flag. In Protected mode, the
  12157.  precise semantics of the INT instruction are given by the following:
  12158.  
  12159.  INTERRUPT
  12160.   Interrupt vector must be within IDT table limits else #GP (vector number *
  12161.   8+2+EXT)
  12162.   Descriptor AR byte must indicate interrupt gate, trap gate, or task gate
  12163.   else #GP (vector number * 8+2+EXT)
  12164.   If INT instruction then gate descriptor DPL must be ≥ CPL else #GP (vector
  12165.   number * 8+2+EXT)
  12166.   Gate must be PRESENT else #NP (vector number * 8+2+EXT)
  12167.   If TRAP GATE or INTERRUPT GATE:
  12168.   Examine CS selector and descriptor given in the gate descriptor:
  12169.        Selector must be non-null else #GP (EXT)
  12170.        Selector must be within its descriptor table limits else
  12171.        #GP (selector+EXT)
  12172.        Descriptor AR byte must indicate code segment else
  12173.        #GP (selector + EXT)
  12174.        Segment must be PRESENT else #NP (selector+EXT)
  12175.   If code segment is non-conforming and DPL < CPL then
  12176.     INTERRUPT TO INNER PRIVILEGE:
  12177.        Check selector and descriptor for new stack in current Task State
  12178.        Segment:
  12179.            Selector must be non-null else #TS(EXT)
  12180.            Selector index must be within its descriptor table limits else
  12181.               #TS (SS selector+EXT)
  12182.            Selector's RPL must equal DPL of code segment else
  12183.               #TS (SS selector+EXT)
  12184.            Stack segment DPL must equal DPL of code segment else #TS (SS
  12185.              selector+EXT)
  12186.            Descriptor must indicate writable data segment else #TS (SS
  12187.              selector+EXT)
  12188.            Segment must be PRESENT else #SS (SS selector+EXT)
  12189.        New stack must have room for 10 bytes else #SS(0)
  12190.        IP must be in CS limit else #GP(0)
  12191.        Load new SS and SP value from TSS
  12192.        Load new CS and IP value from gate
  12193.        Load CS descriptor
  12194.        Load SS descriptor
  12195.        Push long pointer to old stack onto new stack
  12196.        Push return address onto new stack
  12197.        Set CPL to new code segment DPL
  12198.        Set RPL of CS to CPL
  12199.        If INTERRUPT GATE then set the Interrupts Enabled Flag to 0 (disabled)
  12200.        Set the Trap Flag to 0
  12201.        Set the Nested Task Flag to 0
  12202.   If code segment is conforming or code segment DPL = CPL then
  12203.     INTERRUPT TO SAME PRIVILEGE LEVEL:
  12204.        Current stack limits must allow pushing 6 bytes else #SS(0)
  12205.        If interrupt was caused by fault with error code then
  12206.        Stack limits must allow push of two more bytes else #SS(0)
  12207.        IP must be in CS limit else #GP(0)
  12208.        Push flags onto stack
  12209.        Push current CS selector onto stack
  12210.        Push return offset onto stack
  12211.        Load CS:IP from gate
  12212.        Load CS descriptor
  12213.        Set the RPL field of CS to CPL
  12214.        Push error code (if any) onto stack
  12215.        If INTERRUPT GATE then set the Interrupts Enabled Flag to 0 (disabled)
  12216.        Set the Trap Flag to 0
  12217.        Set the Nested Task Flag to 0
  12218.  
  12219.  Else #GP (CS selector + EXT)
  12220.  
  12221.  If TASK GATE:
  12222.     Examine selector to TSS, given in Task Gate descriptor:
  12223.      Must specify global in the local/global bit else #GP (TSS selector)
  12224.      Index must be within GDT limits else #GP (TSS selector)
  12225.      AR byte must specify available TSS (bottom bits 00001) else #GP (TSS
  12226.      selector)
  12227.      Task State Segment must be PRESENT else #NP (TSS selector)
  12228.  SWITCH_TASKS with nesting to TSS
  12229.  If interrupt was caused by fault with error code then
  12230.      Stack limits must allow push of two more bytes else #SS(0)
  12231.      Push error code onto stack
  12232.  IP must be in CS limit else #GP(0)
  12233.  
  12234.  EXT is 1 if an external event (i.e., a single step, an external interrupt,
  12235.  an MF exception, or an MP exception) caused the interrupt; 0 if not (i.e.,
  12236.  an INT instruction or other exceptions).
  12237.  
  12238.  Protected Mode Exceptions
  12239.  
  12240.  #GP, #NP, #SS, and #TS, as indicated in the list above.
  12241.  
  12242.  Real Address Mode Exceptions
  12243.  
  12244.  None; the 80286 will shut down if the SP = 1, 3, or 5 before executing the
  12245.  INT or INTO instruction──due to lack of stack space.
  12246.  
  12247.  
  12248.  IRET──Interrupt Return
  12249.  
  12250.  Opcode   Instruction  Clocks    Description
  12251.  
  12252.   CF        IRET       17,pm=31   Interrupt return (far return and pop flags)
  12253.   CF        IRET       55         Interrupt return, lesser privilege
  12254.   CF        IRET       169        Interrupt return, different task (NT=1)
  12255.  
  12256.  
  12257.  Flags Modified
  12258.  
  12259.  Entire flags register popped from stack
  12260.  
  12261.  Flags Undefined
  12262.  
  12263.  None
  12264.  
  12265.  Operation
  12266.  
  12267.  In real address mode, IRET pops IP, CS, and FLAGS from the stack in that
  12268.  order, and resumes the interrupted routine.
  12269.  
  12270.  In protected mode, the action of IRET depends on the setting of the Nested
  12271.  Task Flag (NT) bit in the flag register. When popping the new flag image
  12272.  from the stack, note that the IOPL bits in the flag register are changed
  12273.  only when CPL=0.
  12274.  
  12275.  If NT=0, IRET returns from an interrupt procedure without a task switch.
  12276.  The code returned to must be equally or less privileged than the interrupt
  12277.  routine as indicated by the RPL bits of the CS selector popped from the
  12278.  stack. If the destination code is of less privilege, IRET then also pops SP
  12279.  and SS from the stack.
  12280.  
  12281.  If NT=1, IRET reverses the operation of a CALL or INT that caused a task
  12282.  switch. The task executing IRET has its updated state saved in its Task
  12283.  State Segment. This means that if the task is re-entered, the code that
  12284.  follows IRET will be executed.
  12285.  
  12286.  The exact checks and actions performed by IRET in protected mode are given
  12287.  on the following page.
  12288.  
  12289.  INTERRUPT RETURN:
  12290.    If Nested Task Flag=1 then
  12291.      RETURN FROM NESTED TASK:
  12292.        Examine Back Link Selector in TSS addressed by the current Task
  12293.         Register:
  12294.           Must specify global in the local/global bit else
  12295.           #TS (new TSS selector)
  12296.           Index must be within GDT limits else #TS (new TSS selector)
  12297.           AR byte must specify TSS else #TS (new TSS selector)
  12298.           New TSS must be busy else #TS (new TSS selector)
  12299.           Task State Segment must be PRESENT else #NP (new TSS selector)
  12300.        SWITCH_TASKS without nesting to TSS specified by back link selector
  12301.        Mark the task just abandoned as NOT BUSY
  12302.        IP must be in code segment limit else #GP(0)
  12303.    If Nested Task Flag=0 then
  12304.      INTERRUPT RETURN ON STACK:
  12305.        Second word on stack must be within stack limits else #SS(0)
  12306.        Return CS selector RPL must be ≥ CPL else #GP (Return selector)
  12307.        If return selector RPL = CPL then
  12308.           INTERRUPT RETURN TO SAME LEVEL:
  12309.           Top 6 bytes on stack must be within limits else #SS(0)
  12310.           Return CS selector (at SP+2) must be non-null else #GP(0)
  12311.           Selector index must be within its descriptor table limits else
  12312.           #GP (Return selector)
  12313.           AR byte must indicate code segment else #GP (Return selector)
  12314.           If non-conforming then code segment DPL must = CPL else
  12315.           #GP (Return selector)
  12316.           If conforming then code segment DPL must be ≤ CPL else
  12317.           #GP (Return selector)
  12318.           Segment must be PRESENT else #NP (Return selector)
  12319.           IP must be in code segment limit else #GP(0)
  12320.           Load CS:IP from stack
  12321.           Load CS-cache with new code segment descriptor
  12322.           Load flags with third word on stack
  12323.           Increment SP by 6
  12324.        Else
  12325.        INTERRUPT RETURN TO OUTER PRIVILEGE LEVEL:
  12326.        Top 10 bytes on stack must be within limits else #SS(0)
  12327.        Examine return CS selector (at SP+2) and associated descriptor:
  12328.           Selector must be non-null else #GP(0)
  12329.           Selector index must be within its descriptor table limits else
  12330.           #GP (Return selector)
  12331.           AR byte must indicate code segment else #GP (Return selector)
  12332.           If non-conforming then code segment DPL must = CS selector RPL else
  12333.           #GP (Return selector)
  12334.           If conforming then code segment DPL must be > CPL else #GP (Return
  12335.           selector)
  12336.           Segment must be PRESENT else #NP (Return selector)
  12337.        Examine return SS selector (at SP+8) and associated descriptor:
  12338.           Selector must be non-null else #GP(0)
  12339.           Selector index must be within its descriptor table limits else
  12340.           #GP (SS selector)
  12341.           Selector RPL must equal the RPL of the return CS selector else
  12342.           #GP (SS selector)
  12343.           AR byte must indicate a writable data segment else
  12344.           #GP (SS selector)
  12345.           Stack segment DPL must equal the RPL of the return CS selector else
  12346.           #GP (SS selector)
  12347.           SS must be PRESENT else #SS (SS selector)
  12348.        IP must be in code segment limit else #GP(0)
  12349.        Load CS:IP from stack
  12350.        Load flags with values at (SP+4)
  12351.        Load SS:SP from stack
  12352.        Set CPL to the RPL of the return CS selector
  12353.        Load the CS-cache with the CS descriptor
  12354.        Load the SS-cache with the SS descriptor
  12355.        For each of ES and DS:
  12356.           If the current register setting is not valid for the outer level,
  12357.               then zero the register and clear the valid flag
  12358.           To be valid, the register setting must satisfy the following
  12359.           properties:
  12360.               Selector index must be within descriptor table limits
  12361.               AR byte must indicate data or readable code segment
  12362.               If segment is data or non-conforming code, then:
  12363.               DPL must be ≥ CPL, or
  12364.               DPL must be ≥ RPL.
  12365.  
  12366.  
  12367.  Protected Mode Exceptions
  12368.  
  12369.  #GP, #NP, or #SS, as indicated in the above listing.
  12370.  
  12371.  Real Address Mode Exceptions
  12372.  
  12373.  Interrupt 13 if the stack is popped when it has offset 0FFFFH.
  12374.  
  12375.  
  12376.  Jcond──Jump Short If Condition Met
  12377.  
  12378. ╓┌───────┌────────────┌─────────┌────────────────────────────────────────────╖
  12379.  Opcode  Instruction   Clocks   Description
  12380.  77  cb     JA cb      7,noj=3   Jump short if above (CF=0 and ZF=0)
  12381.  73  cb     JAE cb     7,noj=3   Jump short if above or equal (CF=0)
  12382.  72  cb     JB cb      7,noj=3   Jump short if below (CF=1)
  12383.  76  cb     JBE cb     7,noj=3   Jump short if below or equal (CF=1 or ZF=1)
  12384.  72  cb     JC cb      7,noj=3   Jump short if carry (CF=1)
  12385.  E3  cb     JCXZ cb    8,noj=4   Jump short if CX register is zero
  12386.  74  cb     JE cb      7,noj=3   Jump short if equal (ZF=1)
  12387.  7F  cb     JG cb      7,noj=3   Jump short if greater (ZF=0 and SF=OF)
  12388.  7D  cb     JGE cb     7,noj=3   Jump short if greater or equal (SF=OF)
  12389.  7C  cb     JL cb      7,noj=3   Jump short if less (SF/=OF)
  12390.  7E  cb     JLE cb     7,noj=3   Jump short if less or equal (ZF=1 or SF/=OF)
  12391.  76  cb     JNA cb     7,noj=3   Jump short if not above (CF=1 or ZF=1)
  12392.  Opcode  Instruction   Clocks   Description
  12393. 76  cb     JNA cb     7,noj=3   Jump short if not above (CF=1 or ZF=1)
  12394.  72  cb     JNAE cb    7,noj=3   Jump short if not above/equal (CF=1)
  12395.  73  cb     JNB cb     7,noj=3   Jump short if not below (CF=0)
  12396.  77  cb     JNBE cb    7,noj=3   Jump short if not below/equal
  12397.                                  (CF=0 and ZF=0)
  12398.  73  cb     JNC cb     7,noj=3   Jump short if not carry (CF=0)
  12399.  75  cb     JNE cb     7,noj=3   Jump short if not equal (ZF=0)
  12400.  7E  cb     JNG cb     7,noj=3   Jump short if not greater (ZF=1 or SF/=OF)
  12401.  7C  cb     JNGE cb    7,noj=3   Jump short if not greater/equal (SF/=OF)
  12402.  7D  cb     JNL cb     7,noj=3   Jump short if not less (SF=OF)
  12403.  7F  cb     JNLE cb    7,noj=3   Jump short if not less/equal
  12404.                                  (ZF=0 and SF=OF)
  12405.  71  cb     JNO cb     7,noj=3   Jump short if notoverflow (OF=0)
  12406.  7B  cb     JNP cb     7,noj=3   Jump short if not parity (PF=0)
  12407.  79  cb     JNS cb     7,noj=3   Jump short if not sign (SF=0)
  12408.  75  cb     JNZ cb     7,noj=3   Jump short if not zero (ZF=0)
  12409.  70  cb     JO cb      7,noj=3   Jump short if overflow (OF=1)
  12410.  7A  cb     JP cb      7,noj=3   Jump short if parity (PF=1)
  12411.  7A  cb     JPE cb     7,noj=3   Jump short if parity even (PF=1)
  12412.  7B  cb     JPO cb     7,noj=3   Jump short if parity odd (PF=0)
  12413.  Opcode  Instruction   Clocks   Description
  12414. 7B  cb     JPO cb     7,noj=3   Jump short if parity odd (PF=0)
  12415.  78  cb     JS cb      7,noj=3   Jump short if sign (SF=1)
  12416.  74  cb     JZ cb      7,noj=3   Jump short if zero (ZF=1)
  12417.  
  12418.  
  12419.  Flags Modified
  12420.  
  12421.  None
  12422.  
  12423.  Flags Undefined
  12424.  
  12425.  None
  12426.  
  12427.  Operation
  12428.  
  12429.  Conditional jumps (except for JCXZ, explained below) test the flags, which
  12430.  presumably have been set in some meaningful way by a previous instruction.
  12431.  The conditions for each mnemonic are given in parentheses after each
  12432.  description above. The terms "less" and "greater" are used for comparing
  12433.  signed integers; "above" and "below" are used for unsigned integers.
  12434.  
  12435.  If the given condition is true, then a short jump is made to the label
  12436.  provided as the operand. Instruction encoding is most efficient when the
  12437.  target for the conditional jump is in the current code segment and within
  12438.  -128 to +127 bytes of the first byte of the next instruction.
  12439.  Alternatively, the opposite sense (e.g., JNZ has opposite sense to that of
  12440.  JZ) of the conditional jump can skip around an unconditional jump to the
  12441.  destination.
  12442.  
  12443.  This range is necessary for the assembler to construct a one-byte signed
  12444.  displacement from the end of the current instruction. If the label is
  12445.  out-of-range, or if the label is a FAR label, then you must perform a jump
  12446.  with the opposite condition around an unconditional jump to the non-short
  12447.  label.
  12448.  
  12449.  Because there are, in many instances, several ways to interpret a
  12450.  particular state of the flags, ASM286 provides more than one mnemonic for
  12451.  most of the conditional jump opcodes. For example, consider that a
  12452.  programmer who has just compared a character to another in AL might wish to
  12453.  jump if the two were equal (JE), while another programmer who had just ANDed
  12454.  AX with a bit field mask would prefer to consider only whether the result
  12455.  was zero or not (he would use JZ, a synonym for JE).
  12456.  
  12457.  JCXZ differs from the other conditional jumps in that it actually tests the
  12458.  contents of the CX register for zero, rather than interrogating the flags.
  12459.  This instruction is useful following a conditionally repeated string
  12460.  operation (REPE SCASB, for example) or a conditional loop instruction (such
  12461.  as LOOPNE TARGETLABEL). These instructions implicitly use a limiting count
  12462.  in the CX register. Looping (repeating) ends when either the CX register
  12463.  goes to zero or the condition specified in the instruction (flags indicating
  12464.  equals in both of the above cases) occurs. JCXZ is useful when the
  12465.  terminations must be handled differently.
  12466.  
  12467.  Protected Mode Exceptions
  12468.  
  12469.  #GP(0) if the offset jumped to is beyond the limits of the code segment.
  12470.  
  12471.  Real Address Mode Exceptions
  12472.  
  12473.  None
  12474.  
  12475.  
  12476.  JMP──Jump
  12477.  
  12478.  Opcode       Instruction    Clocks      Description
  12479.  
  12480.   EB   cb     JMP cb         7            Jump short
  12481.   EA   cd     JMP cd         180          Jump to task gate
  12482.   E9   cw     JMP cw         7            Jump near
  12483.   EA   cd     JMP cd         11,pm=23     Jump far (4-byte immediate address)
  12484.   EA   cd     JMP cd         38           Jump to call gate, same privilege
  12485.   EA   cd     JMP cd         175          Jump via Task State Segment
  12486.   FF   /4     JMP ew         7,mem=11     Jump near to EA word
  12487.                                           (absolute offset)
  12488.   FF   /5     JMP ed         15,pm=26     Jump far (4-byte effective address
  12489.                                           in memory doubleword)
  12490.   FF   /5     JMP ed         41           Jump to call gate, same privilege
  12491.   FF   /5     JMP ed         178          Jump via Task State Segment
  12492.   FF   /5     JMP ed         183          Jump to task gate
  12493.  
  12494.  
  12495.  Flags Modified
  12496.  
  12497.  All if a task switch takes place; none if no task switch occurs.
  12498.  
  12499.  Flags Undefined
  12500.  
  12501.  None
  12502.  
  12503.  Operation
  12504.  
  12505.  The JMP instruction transfers program control to a different instruction
  12506.  stream without recording any return information.
  12507.  
  12508.  For inter-segment jumps, the destination can be a code segment, a call
  12509.  gate, a task gate, or a Task State Segment. The latter two destinations
  12510.  cause a complete task switch to take place.
  12511.  
  12512.  Control transfers within a segment use the JMP cw or JMP cb forms. The
  12513.  operand is a relative offset added modulo 65536 to the offset of the
  12514.  instruction that follows the JMP. The result is the new value of IP; the
  12515.  value of CS is unchanged. The byte operand is sign-extended before it is
  12516.  added; it can therefore be used to address labels within 128 bytes in either
  12517.  direction from the next instruction.
  12518.  
  12519.  Indirect jumps within a segment use the JMP ew form. The contents of the
  12520.  register or memory operand is an absolute offset, which becomes the new
  12521.  value of IP. Again, CS is unchanged.
  12522.  
  12523.  Inter-segment jumps in real address mode simply set IP to the offset part
  12524.  of the long pointer and set CS to the selector part of the pointer.
  12525.  
  12526.  In protected mode, inter-segment jumps cause the 80286 to consult the
  12527.  descriptor addressed by the selector part of the long pointer. The AR byte
  12528.  of the descriptor determines the type of the destination. (See table B-3
  12529.  for possible values of the AR byte.) Following are the possible
  12530.  destinations:
  12531.  
  12532.    1.  Code segment──The addressability and visibility of the destination
  12533.        are verified, and CS and IP are loaded with the destination pointer
  12534.        values.
  12535.  
  12536.    2.  Call gate──The offset part of the destination pointer is ignored.
  12537.        After checking for validity, the processor jumps to the location
  12538.        stored in the call gate descriptor.
  12539.  
  12540.    3.  Task gate──The current task's state is saved in its Task State
  12541.        Segment (TSS), and the TSS named in the task gate is used to load a
  12542.        new context. The outgoing task is marked not busy, the new TSS is
  12543.        marked busy, and execution resumes at the point at which the new task
  12544.        was last suspended.
  12545.  
  12546.    4.  TSS──The current task is suspended and the new task is initiated as
  12547.        in 3 above except that there is no intervening gate.
  12548.  
  12549.  Following is the list of checks and actions taken for long jumps in
  12550.  protected mode:
  12551.  
  12552.  JUMP FAR:
  12553.    If indirect then check access of EA doubleword #GP(0) or #SS(0) if limit
  12554.    violation
  12555.    Destination selector is not null else #GP(0)
  12556.    Destination selector index is within its descriptor table limits else
  12557.    #GP (selector)
  12558.    Examine AR byte of destination selector for legal values:
  12559.  
  12560.     JUMP CONFORMING CODE SEGMENT:
  12561.        Descriptor DPL must be ≤ CPL else #GP (selector)
  12562.        Segment must be PRESENT else #NP (selector)
  12563.        IP must be in code segment limit else #GP(0)
  12564.        Load CS:IP from destination pointer
  12565.        Load CS-cache with new segment descriptor
  12566.  
  12567.     JUMP NONCONFORMING CODE SEGMENT:
  12568.        RPL of destination selector must be ≤ CPL else #GP (selector)
  12569.        Descriptor DPL must = CPL else #GP (selector)
  12570.        Segment must be PRESENT else #NP (selector)
  12571.        IP must be in code segment limit else #GP(0)
  12572.        Load CS:IP from destination pointer
  12573.        Load CS-cache with new segment descriptor
  12574.        Set RPL field of CS register to CPL
  12575.  
  12576.     JUMP TO CALL GATE:
  12577.        Descriptor DPL must be ≥ CPL else #GP (gate selector)
  12578.        Descriptor DPL must be ≥ gate selector RPL else #GP (gate selector)
  12579.        Gate must be PRESENT else #NP (gate selector)
  12580.        Examine selector to code segment given in call gate descriptor:
  12581.        Selector must not be null else #GP(0)
  12582.        Selector must be within its descriptor table limits else
  12583.        #GP (CS selector)
  12584.        Descriptor AR byte must indicate code segment else #GP (CS selector)
  12585.        If non-conforming, code segment descriptor DPL must = CPL else
  12586.        #GP (CS selector)
  12587.        If conforming, then code segment descriptor DPL must be ≤ CPL else
  12588.        #GP (CS selector)
  12589.        Code Segment must be PRESENT else #NP (CS selector)
  12590.        IP must be in code segment limit else #GP(0)
  12591.        Load CS:IP from call gate
  12592.        Load CS-cache with new code segment
  12593.        Set RPL of CS to CPL
  12594.  
  12595.     JUMP TASK GATE:
  12596.        Gate descriptor DPL must be ≥ CPL else #GP (gate selector)
  12597.        Gate descriptor DPL must be ≥ gate selector RPL else
  12598.        #GP (gate selector)
  12599.        Task Gate must be PRESENT else #NP (gate selector)
  12600.        Examine selector to TSS, given in Task Gate descriptor:
  12601.        Must specify global in the local/global bit else #GP (TSS selector)
  12602.        Index must be within GDT limits else #GP (TSS selector)
  12603.        Descriptor AR byte must specify available TSS (bottom bits 00001) else
  12604.        #GP (TSS selector)
  12605.        Task State Segment must be PRESENT else #NP (TSS selector)
  12606.        SWITCH_TASKS without nesting to TSS
  12607.        IP must be in code segment limit else #GP(0)
  12608.  
  12609.     JUMP TASK STATE SEGMENT:
  12610.        TSS DPL must be ≥ CPL else #GP (TSS selector)
  12611.        TSS DPL must be ≥ TSS selector RPL else #GP (TSS selector)
  12612.        Descriptor AR byte must specify available TSS (bottom bits 00001) else
  12613.        #GP (TSS selector)
  12614.        Task State Segment must be PRESENT else #NP (TSS selector)
  12615.        SWITCH_TASKS with nesting to TS.
  12616.        IP must be in code segment limit else #GP(0)
  12617.  
  12618.     Else GP (selector)
  12619.  
  12620.  Protected Mode Exceptions
  12621.  
  12622.  For NEAR jumps, #GP(0) if the destination offset is beyond the limits of
  12623.  the current code segment. For FAR jumps, #GP, #NP, #SS, and #TS, as
  12624.  indicated above. #UD if indirect inter-segment jump operand is a register.
  12625.  
  12626.  Real Address Mode Exceptions
  12627.  
  12628.  #UD if indirect inter-segment jump operand is a register.
  12629.  
  12630.  
  12631.  LAHF──Load Flags into AH Register
  12632.  
  12633.  Opcode    Instruction     Clocks      Description
  12634.  
  12635.    9F         LAHF            2        Load: AH = flags
  12636.                                        SF ZF xx AF xx PF xx CF
  12637.  
  12638.  Flags Modified
  12639.  
  12640.  None
  12641.  
  12642.  Flags Undefined
  12643.  
  12644.  None
  12645.  
  12646.  Operation
  12647.  
  12648.  The low byte of the flags word is transferred to AH. The bits, from MSB to
  12649.  LSB, are as follows: sign, zero, indeterminate, auxiliary carry,
  12650.  indeterminate, parity, indeterminate, and carry. See figure 3-5.
  12651.  
  12652.  Protected Mode Exceptions
  12653.  
  12654.  None
  12655.  
  12656.  Real Address Mode Exceptions
  12657.  
  12658.  None
  12659.  
  12660.  
  12661.  LAR──Load Access Rights Byte
  12662.  
  12663.  Opcode     Instruction     Clocks         Description
  12664.  
  12665.  0F 02 /r   LAR rw,ew      14,mem=16       Load: high(rw)= Access Rights
  12666.                                            byte, selector ew
  12667.  
  12668.  Flags Modified
  12669.  
  12670.  Zero
  12671.  
  12672.  Flags Undefined
  12673.  
  12674.  None
  12675.  
  12676.  Operation
  12677.  
  12678.  LAR expects the second operand (memory or register word) to contain a
  12679.  selector. If the associated descriptor is visible at the current privilege
  12680.  level and at the selector RPL, then the access rights byte of the descriptor
  12681.  is loaded into the high byte of the first (register) operand, and the low
  12682.  byte is set to zero. The zero flag is set if the loading was performed
  12683.  (i.e., the selector index is within the table limit, descriptor DPL ≥ CPL,
  12684.  and descriptor DPL ≥ selector RPL); the zero flag is cleared otherwise.
  12685.  
  12686.  Selector operands cannot cause protection exceptions.
  12687.  
  12688.  Protected Mode Exceptions
  12689.  
  12690.  #GP(0) for an illegal memory operand effective address in the CS, DS, or ES
  12691.  segments; #SS(0) for an illegal address in the SS segment.
  12692.  
  12693.  Real Address Mode Exception
  12694.  
  12695.  INTERRUPT 6; LAR is unrecognized in Real Address mode.
  12696.  
  12697.  
  12698.  LDS/LES──Load Doubleword Pointer
  12699.  
  12700.  Opcode   Instruction  Clocks    Description
  12701.  
  12702.  C5 /r    LDS rw,ed    7,pm=21   Load EA doubleword into DS and word register
  12703.  C4 /r    LES rw,ed    7,pm=21   Load EA doubleword into ES and word register
  12704.  
  12705.  Flags Modified
  12706.  
  12707.  None
  12708.  
  12709.  Flags Undefined
  12710.  
  12711.  None
  12712.  
  12713.  Operation
  12714.  
  12715.  The four-byte pointer at the memory location indicated by the second
  12716.  operand is loaded into a segment register and a word register. The first
  12717.  word of the pointer (the offset) is loaded into the register indicated by
  12718.  the first operand. The last word of the pointer (the selector) is loaded
  12719.  into the segment register (DS or ES) given by the instruction opcode.
  12720.  
  12721.  When the segment register is loaded, its associated cache is also loaded.
  12722.  The data for the cache is obtained from the descriptor table entry for the
  12723.  selector given.
  12724.  
  12725.  A null selector (values 0000-0003) can be loaded into DS or ES without a
  12726.  protection exception. Any memory reference using such a segment register
  12727.  value will cause a #GP(0) exception but will not result in a memory
  12728.  reference. The saved segment register value will be null.
  12729.  
  12730.  Following is a list of checks and actions taken when loading the DS or ES
  12731.  registers:
  12732.  
  12733.  If selector is non-null then:
  12734.      Selector index must be within its descriptor table limits else
  12735.      #GP(selector)
  12736.      Examine descriptor AR byte:
  12737.  
  12738.         Data segment or readable non-conforming code segment
  12739.            Descriptor DPL ≥ CPL else #GP (selector)
  12740.            Descriptor DPL ≥ selector RPL else #GP (selector)
  12741.  
  12742.         Readable conforming code segment
  12743.            No DPL, RPL, or CPL checks
  12744.  
  12745.         Else #GP (selector)
  12746.  
  12747.      Segment must be present else #NP (selector)
  12748.      Load registers from operand
  12749.      Load segment register descriptor cache
  12750.  
  12751.  If selector is null then:
  12752.      Load registers from operand
  12753.      Mark segment register cache as invalid
  12754.  
  12755.  Protected Mode Exceptions
  12756.  
  12757.  #GP or #NP, as indicated in the list above. #GP(0) or #SS(0) if operand
  12758.  lies outside segment limit. #UD if the source operand is a register.
  12759.  
  12760.  Real Address Mode Exceptions
  12761.  
  12762.  Interrupt 13 for operand at offset 0FFFFH or 0FFFDH. #UD if the source
  12763.  operand is a register.
  12764.  
  12765.  
  12766.  LEA──Load Effective Address Offset
  12767.  
  12768.  Opcode  Instruction    Clocks    Description
  12769.  
  12770.  8D /r    LEA rw,m        3       Calculate EA offset given by m, place in rw
  12771.  
  12772.  Flags Modified
  12773.  
  12774.  None
  12775.  
  12776.  Flags Undefined
  12777.  
  12778.  None
  12779.  
  12780.  Operation
  12781.  
  12782.  The effective address (offset part) of the second operand is placed in the
  12783.  first (register) operand.
  12784.  
  12785.  Protected Mode Exceptions
  12786.  
  12787.  #UD if second operand is a register.
  12788.  
  12789.  Real Address Mode Exceptions
  12790.  
  12791.  #UD if second operand is a register.
  12792.  
  12793.  
  12794.  LEAVE──High Level Procedure Exit
  12795.  
  12796.  Opcode     Instruction       Clocks           Description
  12797.  
  12798.  C9         LEAVE                5             Set SP to BP, then POP BP
  12799.  
  12800.  Flags Modified
  12801.  
  12802.  None
  12803.  
  12804.  Flags Undefined
  12805.  
  12806.  None
  12807.  
  12808.  Operation
  12809.  
  12810.  LEAVE is the complementary operation to ENTER; it reverses the effects of
  12811.  that instruction. By copying BP to SP, LEAVE releases the stack space used
  12812.  by a procedure for its dynamics and display. The old frame pointer is now
  12813.  popped into BP, restoring the caller's frame, and a subsequent RET
  12814.  instruction will follow the back-link and remove any arguments pushed on
  12815.  the stack for the exiting procedure.
  12816.  
  12817.  Protected Mode Exceptions
  12818.  
  12819.  #SS(0) if BP does not point to a location within the current stack segment.
  12820.  
  12821.  Real Address Mode Exceptions
  12822.  
  12823.  Interrupt 13 for a word operand at offset 0FFFFH.
  12824.  
  12825.  
  12826.  LGDT/LIDT──Load Global/Interrupt Descriptor Table Register
  12827.  
  12828.  Opcode    Instruction  Clocks     Description
  12829.  
  12830.  0F 01 /2   LGDT m       11        Load m into Global Descriptor Table reg
  12831.  0F 01 /3   LIDT m       12        Load m into Interrupt Descriptor Table reg
  12832.  
  12833.  Flags Modified
  12834.  
  12835.  None
  12836.  
  12837.  Flags Undefined
  12838.  
  12839.  None
  12840.  
  12841.  Operation
  12842.  
  12843.  The Global or the Interrupt Descriptor Table Register is loaded from the
  12844.  six bytes of memory pointed to by the effective address operand (see figure
  12845.  10-3). The LIMIT field of the descriptor table register loads from the
  12846.  first word; the next three bytes go to the BASE field of the register; the
  12847.  last byte is ignored.
  12848.  
  12849.  LGDT and LIDT appear in operating systems software; they are not used in
  12850.  application programs. These are the only instructions that directly load a
  12851.  physical memory address in 80286 protected mode.
  12852.  
  12853.  Protected Mode Exceptions
  12854.  
  12855.  #GP(0) if the current privilege level is not 0.
  12856.  
  12857.  #UD if source operand is a register.
  12858.  
  12859.  #GP(0) for an illegal memory operand effective address in the CS, DS, or ES
  12860.  segments; #SS(0) for an illegal address in the SS segment.
  12861.  
  12862.  Real Address Mode Exceptions
  12863.  
  12864.  These instructions are valid in Real Address mode to allow the power-up
  12865.  initialization for Protected mode.
  12866.  
  12867.  Interrupt 13 for a word operand at offset 0FFFFH. #UD if source operand is
  12868.  a register.
  12869.  
  12870.  
  12871.  LLDT──Load Local Descriptor Table Register
  12872.  
  12873.  Opcode     Instruction    Clocks     Description
  12874.  
  12875.  0F 00 /2   LLDT ew        17,mem=19  Load selector ew into Local
  12876.                                       Descriptor Table register
  12877.  
  12878.  Flags Modified
  12879.  
  12880.  None
  12881.  
  12882.  Flags Undefined
  12883.  
  12884.  None
  12885.  
  12886.  Operation
  12887.  
  12888.  The word operand (memory or register) to LLDT should contain a selector
  12889.  pointing to the Global Descriptor Table. The GDT entry should be a Local
  12890.  Descriptor Table Descriptor. If so, then the Local Descriptor Table Register
  12891.  is loaded from the entry. The descriptor cache entries for DS, ES, SS, and
  12892.  CS are not affected. The LDT field in the TSS is not changed.
  12893.  
  12894.  The selector operand is allowed to be zero. In that case, the Local
  12895.  Descriptor Table Register is marked invalid. All descriptor references
  12896.  (except by LAR, VERR, VERW or LSL instructions) will cause a #GP fault.
  12897.  
  12898.  LLDT appears in operating systems software; it does not appear in
  12899.  applications programs.
  12900.  
  12901.  Protected Mode Exceptions
  12902.  
  12903.  #GP(0) if the current privilege level is not 0. #GP (selector) if the
  12904.  selector operand does not point into the Global Descriptor Table, or if the
  12905.  entry in the GDT is not a Local Descriptor Table. #NP (selector) if LDT
  12906.  descriptor is not present. #GP(0) for an illegal memory operand effective
  12907.  address in the CS, DS, or ES segments; #SS(0) for an illegal address in the
  12908.  SS segment.
  12909.  
  12910.  Real Address Mode Exceptions
  12911.  
  12912.  Interrupt 6; LLDT is not recognized in Real Address Mode.
  12913.  
  12914.  
  12915.  LMSW──Load Machine Status Word
  12916.  
  12917.  Opcode      Instruction    Clocks     Description
  12918.  
  12919.  0F 01 /6    LMSW ew        3,mem=6    Load EA word into Machine Status Word
  12920.  
  12921.  Flags Modified
  12922.  
  12923.  None
  12924.  
  12925.  Flags Undefined
  12926.  
  12927.  None
  12928.  
  12929.  Operation
  12930.  
  12931.  The Machine Status Word is loaded from the source operand. This instruction
  12932.  may be used to switch to protected mode. If so, then it must be followed by
  12933.  an intra-segment jump to flush the instruction queue. LMSW will not switch
  12934.  back to Real Address Mode.
  12935.  
  12936.  LMSW appears only in operating systems software. It does not appear in
  12937.  applications programs.
  12938.  
  12939.  Protected Mode Exceptions
  12940.  
  12941.  #GP(0) if the current privilege level is not 0. #GP(0) for an illegal
  12942.  memory operand effective address in the CS, DS, or ES segments; #SS(0) for
  12943.  an illegal address in the SS segment.
  12944.  
  12945.  Real Address Mode Exceptions
  12946.  
  12947.  Interrupt 13 for a word operand at offset 0FFFFH.
  12948.  
  12949.  
  12950.  LOCK──Assert BUS LOCK Signal
  12951.  
  12952.  Opcode  Instruction  Clocks   Description
  12953.  
  12954.  F0      LOCK          0       Assert BUSLOCK signal for the next instruction
  12955.  
  12956.  Flags Modified
  12957.  
  12958.  None
  12959.  
  12960.  Flags Undefined
  12961.  
  12962.  None
  12963.  
  12964.  Operation
  12965.  
  12966.  LOCK is a prefix that will cause the BUS LOCK signal of the 80286 to be
  12967.  asserted for the duration of the instruction that it prefixes. In a
  12968.  multiprocessor environment, this signal should be used to ensure that the
  12969.  80286 has exclusive use of any shared memory while BUS LOCK is asserted.
  12970.  The read-modify-write sequence typically used to implement TEST-AND-SET in
  12971.  the 80286 is the XCHG instruction.
  12972.  
  12973.  The 80286 LOCK prefix activates the lock signal for the following
  12974.  instructions: MOVS, INS, and OUTS. XCHG always asserts BUS LOCK regardless
  12975.  of the presence or absence of the LOCK prefix.
  12976.  
  12977.  Protected Mode Exceptions
  12978.  
  12979.  #GP(0) if the current privilege level is bigger (less privileged) than the
  12980.  I/O privilege level.
  12981.  
  12982.  Other exceptions may be generated by the subsequent (locked) instruction.
  12983.  
  12984.  Real Address Mode Exceptions
  12985.  
  12986.  None. Exceptions may still be generated by the subsequent (locked)
  12987.  instruction.
  12988.  
  12989.  
  12990.  LODS/LODSB/LODSW──Load String Operand
  12991.  
  12992.   Opcode    Instruction      Clocks      Description
  12993.  
  12994.   AC         LODS mb           5         Load byte [SI] into AL
  12995.   AD         LODS mw           5         Load word [SI] into AX
  12996.   AC         LODSB             5         Load byte DS:[SI] into AL
  12997.   AD         LODSW             5         Load word DS:[SI] into AX
  12998.  
  12999.  Flags Modified
  13000.  
  13001.  None
  13002.  
  13003.  Flags Undefined
  13004.  
  13005.  None
  13006.  
  13007.  Operation
  13008.  
  13009.  LODS loads the AL or AX register with the memory byte or word at SI. After
  13010.  the transfer is made, SI is automatically advanced. If the direction flag is
  13011.  0 (CLD was executed), SI increments; if the direction flag is 1 (STD was
  13012.  executed), SI decrements. SI increments or decrements by 1 if a byte was
  13013.  moved; by 2 if a word was moved.
  13014.  
  13015.  Protected Address Mode Exceptions
  13016.  
  13017.  #GP(0) for an illegal memory operand effective address in the CS, DS, or ES
  13018.  segments; #SS(0) for an illegal address in the SS segment.
  13019.  
  13020.  Real Address Mode Exceptions
  13021.  
  13022.  Interrupt 13 for a word operand at offset 0FFFFH.
  13023.  
  13024.  
  13025.  LOOP/LOOPcond──Loop Control with CX Counter
  13026.  
  13027.  Opcode   Instruction   Clocks   Description
  13028.  
  13029.  E2 cb    LOOP cb       8,noj=4  DEC CX; jump short if CX<>0
  13030.  E1 cb    LOOPE cb      8,noj=4  DEC CX; jump short if CX<>E0 and equal
  13031.                                  (ZF=1)
  13032.  E0 cb    LOOPNE cb     8,noj=4  DEC CX; jump short if CX<>E0 and not equal
  13033.                                  (ZF=0)
  13034.  E0 cb    LOOPNZ cb     8,noj=4  DEC CX; jump short if CX<>E0 and ZF=0
  13035.  E1 cb    LOOPZ cb      8,noj=4  DEC CX; jump short if CX<>E0 and zero (ZF=1)
  13036.  
  13037.  Flags Modified
  13038.  
  13039.  None
  13040.  
  13041.  Flags Undefined
  13042.  
  13043.  None
  13044.  
  13045.  Operation
  13046.  
  13047.  LOOP first decrements the CX register without changing any of the flags.
  13048.  Then, conditions are checked as given in the description above for the form
  13049.  of LOOP being used. If the conditions are met, then an intra-segment jump is
  13050.  made. The destination to LOOP is in the range from 126 (decimal) bytes
  13051.  before the instruction to 127 bytes beyond the instruction.
  13052.  
  13053.  The LOOP instructions are intended to provide iteration control and to
  13054.  combine loop index management with conditional branching. To use the LOOP
  13055.  instruction you load an unsigned iteration count into CX, then code the LOOP
  13056.  at the end of a series of instructions to be iterated. The destination of
  13057.  LOOP is a label that points to the beginning of the iteration.
  13058.  
  13059.  Protected Address Mode Exceptions
  13060.  
  13061.  #GP(0) if the offset jumped to is beyond the limits of the current code
  13062.  segment.
  13063.  
  13064.  Real Address Mode Exceptions
  13065.  
  13066.  None
  13067.  
  13068.  
  13069.  LSL──Load Segment Limit
  13070.  
  13071.  Opcode    Instruction    Clocks      Description
  13072.  
  13073.  0F 03 /r     LSL rw,ew   14,mem=16   Load: rw = Segment Limit, selector ew
  13074.  
  13075.  Flags Modified
  13076.  
  13077.  Zero
  13078.  
  13079.  Flags Undefined
  13080.  
  13081.  None
  13082.  
  13083.  Operation
  13084.  
  13085.  If the descriptor denoted by the selector in the second (memory or
  13086.  register) operand is visible at the CPL, a word that consists of the limit
  13087.  field of the descriptor is loaded into the left operand, which must be a
  13088.  register. The value is the limit field for that segment. The zero flag is
  13089.  set if the loading was performed (that is, if the selector is non-null, the
  13090.  selector index is within the descriptor table limits, the descriptor is a
  13091.  non-conforming segment descriptor with DPL ≥ CPL, and the descriptor DPL ≥
  13092.  selector RPL); the zero flag is cleared otherwise.
  13093.  
  13094.  The LSL instruction returns only the limit field of segments, task state
  13095.  segments, and local descriptor tables. The interpretation of the limit value
  13096.  depends on the type of segment.
  13097.  
  13098.  The selector operand's value cannot result in a protection exception.
  13099.  
  13100.  Protected Address Mode Exceptions
  13101.  
  13102.  #GP(0) for an illegal memory operand effective address in the CS, DS, or ES
  13103.  segments; #SS(0) for an illegal address in the SS segment.
  13104.  
  13105.  Real Address Mode Exceptions
  13106.  
  13107.  Interrupt 6; LSL is not recognized in Real Address mode.
  13108.  
  13109.  
  13110.  LTR──Load Task Register
  13111.  
  13112.  Opcode    Instruction     Clocks       Description
  13113.  
  13114.  0F 00 /3   LTR ew         17,mem=19    Load EA word into Task Register
  13115.  
  13116.  Flags Modified
  13117.  
  13118.  None
  13119.  
  13120.  Flags Undefined
  13121.  
  13122.  None
  13123.  
  13124.  Operation
  13125.  
  13126.  The Task Register is loaded from the source register or memory location
  13127.  given by the operand. The loaded TSS is marked busy. A task switch operation
  13128.  does not occur.
  13129.  
  13130.  LTR appears only in operating systems software. It is not used in
  13131.  applications programs.
  13132.  
  13133.  Protected Address Mode Exceptions
  13134.  
  13135.  #GP for an illegal memory operand effective address in the CS, DS, or ES
  13136.  segments; #SS for an illegal address in the SS segment.
  13137.  
  13138.  #GP(0) if the current privilege level is not 0. #GP (selector) if the
  13139.  object named by the source selector is not a TSS or is already busy. #NP
  13140.  (selector) if the TSS is marked not present.
  13141.  
  13142.  Real Address Mode Exceptions
  13143.  
  13144.  Interrupt 6; LTR is not recognized in Real Address mode.
  13145.  
  13146.  
  13147.  MOV──Move Data
  13148.  
  13149. ╓┌─────────┌────────────┌─────────────┌──────────────────────────────────────
  13150.  Opcode    Instruction  Clocks        Description
  13151.  88 /r      MOV eb,rb   2,mem=3       Move byte register into EA byte
  13152.  89 /r      MOV ew,rw   2,mem=3       Move word register into EA word
  13153.  8A /r      MOV rb,eb   2,mem=5       Move EA byte into byte register
  13154.  8B /r      MOV rw,ew   2,mem=5       Move EA word into word register
  13155.  8C /0      MOV ew,ES   2,mem=3       Move ES into EA word
  13156.  8C /1      MOV ew,CS   2,mem=3       Move CS into EA word
  13157.  8C /2      MOV ew,SS   2,mem=3       Move SS into EA word
  13158.  8C /3      MOV ew,DS   2,mem=3       Move DS into EA word
  13159.  8E /0      MOV ES,mw   5,pm=19       Move memory word into ES
  13160.  8E /0      MOV ES,rw   2,pm=17       Move word register into ES
  13161.  8E /2      MOV SS,mw   5,pm=19       Move memory word into SS
  13162.  8E /2      MOV SS,rw   2,pm=17       Move word register into SS
  13163.  8E /3      MOV DS,mw   5,pm=19       Move memory word into DS
  13164.  8E /3      MOV DS,rw   2,pm=17       Move word register into DS
  13165.  A0 dw      MOV AL,xb   5             Move byte variable (offset dw) into AL
  13166.  A1 dw      MOV AX,xw   5             Move word variable (offset dw=) into AX
  13167.  A2 dw      MOV xb,AL   3             Move AL into byte variable (offset dw=)
  13168.  A3 dw      MOV xw,AX   3             Move AX into word register (offset dw=)
  13169.  Opcode    Instruction  Clocks        Description
  13170. A3 dw      MOV xw,AX   3             Move AX into word register (offset dw=)
  13171.  B0+ rb db  MOV rb,db   2             Move immediate byte into byte register
  13172.  B8+ rw dw  MOV rw,dw   2             Move immediate word into word register
  13173.  C6 /0 db   MOV eb,db   2,mem=3       Move immediate byte into EA byte
  13174.  C7 /0 dw   MOV ew,dw   2,mem=3       Move immediate word into EA word
  13175.  
  13176.  
  13177.  Flags Modified
  13178.  
  13179.  None
  13180.  
  13181.  Flags Undefined
  13182.  
  13183.  None
  13184.  
  13185.  Operation
  13186.  
  13187.  The second operand is copied to the first operand.
  13188.  
  13189.  If the destination operand is a segment register (DS, ES, or SS), then the
  13190.  associated segment register cache is also loaded. The data for the cache is
  13191.  obtained from the descriptor table entry for the selector given.
  13192.  
  13193.  A null selector (values 0000-0003) can be loaded into DS and ES registers
  13194.  without causing a protection exception. Any use of a segment register with a
  13195.  null selector to address memory will cause #GP(0) exception. No memory
  13196.  reference will occur.
  13197.  
  13198.  Any move into SS will inhibit all interrupts until after the execution of
  13199.  the next instruction.
  13200.  
  13201.  Following is a listing of the protected-mode checks and actions taken in
  13202.  the loading of a segment register:
  13203.  
  13204.  If SS is loaded:
  13205.      If selector is null then #GP(0)
  13206.      Selector index must be within its descriptor table limits else
  13207.      #GP (selector)
  13208.      Selector's RPL must equal CPL else #GP (selector)
  13209.      AR byte must indicate a writable data segment else #GP (selector)
  13210.      DPL in the AR byte must equal CPL else #GP (selector)
  13211.      Segment must be marked PRESENT else #SS (selector)
  13212.      Load SS with selector
  13213.      Load SS cache with descriptor
  13214.  If ES or DS is loaded with non-null selector
  13215.      Selector index must be within its descriptor table limits else
  13216.        #GP (selector)
  13217.      AR byte must indicate data or readable code segment else #GP (selector)
  13218.      If data or non-conforming code, then both the RPL and the
  13219.           CPL must be less than or equal to DPL in AR byte else
  13220.           #GP (selector)
  13221.      Segment must be marked PRESENT else #NP (selector)
  13222.  Load segment register with selector
  13223.  Load segment register cache with descriptor
  13224.  If ES or DS is loaded with a null selector:
  13225.      Load segment register with selector
  13226.      Clear descriptor valid bit
  13227.  
  13228.  Real Address Mode Exceptions
  13229.  
  13230.  Interrupt 13 for a word operand at offset 0FFFFH.
  13231.  
  13232.  
  13233.  MOVS/MOVSB/MOVSW──Move Data from String to String
  13234.  
  13235.  Opcode    Instruction   Clocks   Description
  13236.  
  13237.  A4        MOVS mb,mb    5        Move byte [SI] to ES:[DI]
  13238.  A5        MOVS mw,mw    5        Move word [SI] to ES:[DI]
  13239.  A4        MOVSB         5        Move byte DS:[SI] to ES:[DI]
  13240.  A5        MOVSW         5        Move word DS:[SI] to ES:[DI]
  13241.  
  13242.  Flags Modified
  13243.  
  13244.  None
  13245.  
  13246.  Flags Undefined
  13247.  
  13248.  None
  13249.  
  13250.  Operation
  13251.  
  13252.  MOVS copies the byte or word at [SI] to the byte or word at ES:[DI]. The
  13253.  destination operand must be addressable from the ES register; no segment
  13254.  override is possible. A segment override may be used for the source operand.
  13255.  
  13256.  After the data movement is made, both SI and DI are automatically advanced.
  13257.  If the direction flag is 0 (CLD was executed), the registers increment; if
  13258.  the direction flag is 1 (STD was executed), the registers decrement. The
  13259.  registers increment or decrement by 1 if a byte was moved; by 2 if a word
  13260.  was moved.
  13261.  
  13262.  MOVS can be preceded by the REP prefix for block movement of CX bytes or
  13263.  words. Refer to the REP instruction for details of this operation.
  13264.  
  13265.  Protected Address Mode Exceptions
  13266.  
  13267.  #GP(0) if the destination is in a non-writable segment. #GP(0) for an
  13268.  illegal memory operand effective address in the CS, DS, or ES segments;
  13269.  #SS(0) for an illegal address in the SS segment.
  13270.  
  13271.  Real Address Mode Exceptions
  13272.  
  13273.  Interrupt 13 for a word operand at offset 0FFFFH.
  13274.  
  13275.  
  13276.  MUL──Unsigned Multiplication of AL or AX
  13277.  
  13278.  Opcode   Instruction    Clocks       Description
  13279.  
  13280.  F6  /4   MUL eb         13,mem=16    Unsigned multiply (AX = AL * EA byte)
  13281.  F7  /4   MUL ew         21,mem=24    Unsigned multiply (DXAX = AX * EA word)
  13282.  
  13283.  Flags Modified
  13284.  
  13285.  Overflow, carry
  13286.  
  13287.  Flags Undefined
  13288.  
  13289.  Sign, zero, auxiliary carry, parity
  13290.  
  13291.  Operation
  13292.  
  13293.  If MUL has a byte operand, then the byte is multiplied by AL, and the
  13294.  result is left in AX. Carry and overflow are set to 0 if AH is 0; they are
  13295.  set to 1 otherwise.
  13296.  
  13297.  If MUL has a word operand, then the word is multiplied by AX, and the
  13298.  result is left in DX:AX. DX contains the high order 16 bits of the product.
  13299.  Carry and overflow are set to 0 if DX is 0; they are set to 1 otherwise.
  13300.  
  13301.  Protected Address Mode Exceptions
  13302.  
  13303.  #GP(0) for an illegal memory operand effective address in the CS, DS, or ES
  13304.  segments; #SS(0) for an illegal address in the SS segment.
  13305.  
  13306.  Real Address Mode Exceptions
  13307.  
  13308.  Interrupt 13 for a word operand at offset 0FFFFH.
  13309.  
  13310.  
  13311.  NEG──Two's Complement Negation
  13312.  
  13313.  Opcode    Instruction      Clocks         Description
  13314.  
  13315.  F6  /3       NEG eb        2,mem=7        Two's complement negate EA byte
  13316.  F7  /3       NEG ew        2,mem=7        Two's complement negate EA word
  13317.  
  13318.  Flags Modified
  13319.  
  13320.  Overflow, sign, zero, auxiliary carry, parity, carry
  13321.  
  13322.  Flags Undefined
  13323.  
  13324.  None
  13325.  
  13326.  Operation
  13327.  
  13328.  The two's complement of the register or memory operand replaces the old
  13329.  operand value. Likewise, the operand is subtracted from zero, and the result
  13330.  is placed in the operand.
  13331.  
  13332.  The carry flag is set to 1 except when the input operand is zero, in which
  13333.  case the carry flag is cleared to 0.
  13334.  
  13335.  Protected Address Mode Exceptions
  13336.  
  13337.  #GP(0) if the result is in a non-writable segment. #GP(0) for an illegal
  13338.  memory operand effective address in the CS, DS, or ES segments; #SS(0) for
  13339.  an illegal address in the SS segment.
  13340.  
  13341.  Real Address Mode Exceptions
  13342.  
  13343.  Interrupt 13 for a word operand at offset 0FFFFH.
  13344.  
  13345.  
  13346.  NOP──No OPERATION
  13347.  
  13348.  Opcode     Instruction      Clocks      Description
  13349.  
  13350.  90            NOP              3        No OPERATION
  13351.  
  13352.  Flags Modified
  13353.  
  13354.  None
  13355.  
  13356.  Flags Undefined
  13357.  
  13358.  None
  13359.  
  13360.  Operation
  13361.  
  13362.  Performs no operation. NOP is a one-byte filler instruction that takes up
  13363.  space but affects none of the machine context except IP.
  13364.  
  13365.  Protected Address Mode Exceptions
  13366.  
  13367.  None
  13368.  
  13369.  Real Address Mode Exceptions
  13370.  
  13371.  None
  13372.  
  13373.  
  13374.  NOT──One's Complement Negation
  13375.  
  13376.  Opcode    Instruction   Clocks     Description
  13377.  
  13378.  F6  /2    NOT eb        2,mem=7    Reverse each bit of EA byte
  13379.  F7  /2    NOT ew        2,mem=7    Reverse each bit of EA word
  13380.  
  13381.  Flags Modified
  13382.  
  13383.  None
  13384.  
  13385.  Flags Undefined
  13386.  
  13387.  None
  13388.  
  13389.  Operation
  13390.  
  13391.  The operand is inverted; that is, every 1 becomes a 0 and vice versa.
  13392.  
  13393.  Protected Address Mode Exceptions
  13394.  
  13395.  #GP(0) if the result is in a non-writable segment. #GP(0) for an illegal
  13396.  memory operand effective address in the CS, DS, or ES segments; #SS(0) for
  13397.  an illegal address in the SS segment.
  13398.  
  13399.  Real Address Mode Exceptions
  13400.  
  13401.  Interrupt 13 for a word operand at offset 0FFFFH.
  13402.  
  13403.  
  13404.  OR──Logical Inclusive OR
  13405.  
  13406.  Opcode    Instruction     Clocks    Description
  13407.  
  13408.  08 /r      OR eb,rb       2,mem=7   Logical-OR byte register into EA byte
  13409.  09 /r      OR ew,rw       2,mem=7   Logical-OR word register into EA word
  13410.  0A /r      OR rb,eb       2,mem=7   Logical-OR EA byte into byte register
  13411.  0B /r      OR rw,ew       2,mem=7   Logical-OR EA word into word register
  13412.  0C db      OR AL,db       3         Logical-OR immediate byte into AL
  13413.  0D dw      OR AX,dw       3         Logical-OR immediate word into AX
  13414.  80 /1 db   OR eb,db       3,mem=7   Logical-OR immediate byte  into EA byte
  13415.  81 /1 dw   OR ew,dw       3,mem=7   Logical-OR immediate word into EA word
  13416.  
  13417.  Flags Modified
  13418.  
  13419.  Overflow=0, sign, zero, parity, carry=0
  13420.  
  13421.  Flags Undefined
  13422.  
  13423.  Auxiliary carry
  13424.  
  13425.  Operation
  13426.  
  13427.  This instruction computes the inclusive OR of the two operands. Each bit of
  13428.  the result is 0 if both corresponding bits of the operands are 0; each bit
  13429.  is 1 otherwise. The result is placed in the first operand.
  13430.  
  13431.  Protected Address Mode Exceptions
  13432.  
  13433.  #GP(0) if the result is in a non-writable segment. #GP(0) for an illegal
  13434.  memory operand effective address in the CS, DS, or ES segments; #SS(0) for
  13435.  an illegal address in the SS segment.
  13436.  
  13437.  Real Address Mode Exceptions
  13438.  
  13439.  Interrupt 13 for a word operand at offset 0FFFFH.
  13440.  
  13441.  
  13442.  OUT──Output to Port
  13443.  
  13444.  Opcode   Instruction   Clocks    Description
  13445.  
  13446.  E6 db    OUT db,AL     3         Output byte AL to immediate port number db
  13447.  E7 db    OUT db,AX     3         Output word AX to immediate port number db
  13448.  EE       OUT DX,AL     3         Output byte AL to port number DX
  13449.  EF       OUT DX,AX     3         Output word AX to port number DX
  13450.  
  13451.  Flags Modified
  13452.  
  13453.  None
  13454.  
  13455.  Flags Undefined
  13456.  
  13457.  None
  13458.  
  13459.  Operation
  13460.  
  13461.  OUT transfers a data byte or data word from the register (AL or AX) given
  13462.  as the second operand to the output port numbered by the first operand. You
  13463.  can output to any port from 0-65535 by placing the port number in the DX
  13464.  register then using an OUT instruction with DX as the first operand. If the
  13465.  instruction contains an 8-bit port ID, that value is zero-extended to 16
  13466.  bits.
  13467.  
  13468.  Intel reserves I/O port addresses 00F8H through 00FFH; these addresses
  13469.  should not be used.
  13470.  
  13471.  Protected Address Mode Exceptions
  13472.  
  13473.  #GP(0) if the current privilege level is bigger (has less privilege) than
  13474.  IOPL, which is the privilege level found in the flags register.
  13475.  
  13476.  Real Address Mode Exceptions
  13477.  
  13478.  None
  13479.  
  13480.  
  13481.  OUTS/OUTSB/OUTSW──Output String to Port
  13482.  
  13483.  Opcode  Instruction    Clocks     Description
  13484.  
  13485.  6E      OUTS DX,eb     5          Output byte [SI] to port number DX
  13486.  6F      OUTS DX,ew     5          Output word [SI] to port number DX
  13487.  6E      OUTSB          5          Output byte DS:[SI] to port number DX
  13488.  6F      OUTSW          5          Output word DS:[SI] to port number DX
  13489.  
  13490.  Flags Modified
  13491.  
  13492.  None
  13493.  
  13494.  Flags Undefined
  13495.  None
  13496.  
  13497.  Operation
  13498.  
  13499.  OUTS transfers data from the memory byte or word at SI to the output port
  13500.  numbered by the DX register.
  13501.  
  13502.  OUTS does not allow the specification of the port number as an immediate
  13503.  value. The port must be addressed through the DX register.
  13504.  
  13505.  After the transfer is made, SI is automatically advanced. If the direction
  13506.  flag is 0 (CLD was executed), SI increments; if the direction flag is 1 (STD
  13507.  was executed), SI decrements. SI increments or decrements by 1 if a byte was
  13508.  moved; by 2 if a word was moved.
  13509.  
  13510.  OUTS can be preceded by the REP prefix for block output of CX bytes or
  13511.  words. Refer to the REP instruction for details of this operation.
  13512.  
  13513.  Intel reserves I/O port addresses 00F8H through 00FFH; these addresses
  13514.  should not be used.
  13515.  
  13516.  ───────────────────────────────────────────────────────────────────────────
  13517.  NOTE
  13518.    Not all output devices can handle the rate at which this instruction
  13519.    transfers data.
  13520.  ───────────────────────────────────────────────────────────────────────────
  13521.  
  13522.  Protected Mode Exceptions
  13523.  
  13524.  #GP(0) if CPL > IOPL. #GP(0) for an illegal memory operand effective
  13525.  address in the CS, DS, or ES segments; #SS(0) for an illegal address in the
  13526.  SS segment.
  13527.  
  13528.  Real Address Mode Exceptions
  13529.  
  13530.  Interrupt 13 for a word operand at offset 0FFFFH.
  13531.  
  13532.  
  13533.  POP──Pop a Word from the Stack
  13534.  
  13535.  Opcode     Instruction       Clocks      Description
  13536.  
  13537.   1F          POP DS           5,pm=20    Pop top of stack into DS
  13538.   07          POP ES           5,pm=20    Pop top of stack into ES
  13539.   17          POP SS           5,pm=20    Pop top of stack into SS
  13540.   8F /0       POP mw           5          Pop top of stack into memory word
  13541.   58+rw       POP rw           5          Pop top of stack into word register
  13542.  
  13543.  Flags Modified
  13544.  
  13545.  None
  13546.  
  13547.  Flags Undefined
  13548.  
  13549.  None
  13550.  
  13551.  Operation
  13552.  
  13553.  The word on the top of the 80286 stack, addressed by SS:SP, replaces the
  13554.  previous contents of the memory, register, or segment register operand. The
  13555.  stack pointer SP is incremented by 2 to point to the new top of stack.
  13556.  
  13557.  If the destination operand is another segment register (DS, ES, or SS), the
  13558.  value popped must be a selector. In protected mode, loading the selector
  13559.  initiates automatic loading of the descriptor information associated with
  13560.  that selector into the hidden part of the segment register; loading also
  13561.  initiates validation of both the selector and the descriptor information.
  13562.  
  13563.  A null value (0000-0003) may be loaded into the DS or ES register without
  13564.  causing a protection exception. Attempts to reference memory using a segment
  13565.  register with a null value will cause #GP(0) exception. No memory reference
  13566.  will occur. The saved value of the segment register will be null.
  13567.  
  13568.  A POP SS instruction will inhibit all interrupts, including NMI, until
  13569.  after the execution of the next instruction. This permits a POP SP
  13570.  instruction to be performed first.
  13571.  
  13572.  Following is a listing of the protected-mode checks and actions taken in
  13573.  the loading of a segment register:
  13574.  
  13575.  If SS is loaded:
  13576.    If selector is null then #GP(0)
  13577.    Selector index must be within its descriptor table limits else #GP
  13578.    (selector)
  13579.    Selector's RPL must equal CPL else #GP (selector)
  13580.    AR byte must indicate a writable data segment else #GP (selector)
  13581.    DPL in the AR byte must equal CPL else #GP (selector)
  13582.    Segment must be marked PRESENT else #SS (selector)
  13583.    Load SS register with selector
  13584.    Load SS cache with descriptor
  13585.  If ES or DS is loaded with non-null selector:
  13586.    AR byte must indicate data or readable code segment else #GP (selector)
  13587.    If data or non-conforming code, then both the RPL and the
  13588.            CPL must be less than or equal to DPL in AR byte else
  13589.            #GP (selector)
  13590.    Segment must be marked PRESENT else #NP (selector)
  13591.    Load segment register with selector
  13592.    Load segment register cache with descriptor
  13593.  If ES or DS is loaded with a null selector:
  13594.    Load segment register with selector
  13595.    Clear valid bit in cache
  13596.  
  13597.  Protected Mode Exceptions
  13598.  
  13599.  If a segment register is being loaded, #GP, #SS, and #NP, as described in
  13600.  the listing above.
  13601.  
  13602.  Otherwise, #SS(0) if the current top of stack is not within the stack
  13603.  segment.
  13604.  
  13605.  #GP(0) if the destination is in a non-writable segment. #GP(0) for an
  13606.  illegal memory operand effective address in the CS, DS, or ES segments;
  13607.  #SS(0) for an illegal address in the SS segment.
  13608.  
  13609.  Real Address Mode Exceptions
  13610.  
  13611.  Interrupt 13 for a word operand at offset 0FFFFH.
  13612.  
  13613.  
  13614.  POPA──Pop All General Registers
  13615.  
  13616.  Opcode     Instruction     Clocks     Description
  13617.  
  13618.   61         POPA            19         Pop in order: DI,SI,BP,SP,BX,DX,CX,AX
  13619.  
  13620.  Flags Modified
  13621.  
  13622.  None
  13623.  
  13624.  Flags Undefined
  13625.  
  13626.  None
  13627.  
  13628.  Operation
  13629.  
  13630.  POPA pops the eight general registers given in the description above,
  13631.  except that the SP value is discarded instead of loaded into SP. POPA
  13632.  reverses a previous PUSHA, restoring the general registers to their values
  13633.  before PUSHA was executed. The first register popped is DI.
  13634.  
  13635.  Protected Mode Exceptions
  13636.  
  13637.  #SS(0) if the starting or ending stack address is not within the stack
  13638.  segment.
  13639.  
  13640.  Real Address Mode Exceptions
  13641.  
  13642.  Interrupt 13 for a word operand at offset 0FFFFH.
  13643.  
  13644.  
  13645.  POPF──Pop from Stack into the Flags Register
  13646.  
  13647.  Opcode     Instruction     Clocks     Description
  13648.  
  13649.   9D         POPF            5          Pop top of stack into flags register
  13650.  
  13651.  Flags Modified
  13652.  
  13653.  Entire flags register is popped from stack
  13654.  
  13655.  Flags Undefined
  13656.  
  13657.  None
  13658.  
  13659.  Operation
  13660.  
  13661.  The top of the 80286 stack, pointed to by SS:SP, is copied into the 80286
  13662.  flags register. The stack pointer SP is incremented by 2 to point to the new
  13663.  top of stack. The flags, from the top bit (bit 15) to the bottom (bit 0),
  13664.  are as follows: undefined, nested task, I/O privilege level (2 bits),
  13665.  overflow, direction, interrupts enabled, trap, sign, zero, undefined,
  13666.  auxiliary carry, undefined, parity, undefined, and carry.
  13667.  
  13668.  The I/O privilege level will be altered only when executing at privilege
  13669.  level 0. The interrupt enable flag will be altered only when executing at a
  13670.  level at least as privileged as the I/O privilege level. If you execute a
  13671.  POPF instruction with insufficient privilege, there will be no exception
  13672.  nor will the privileged bits be changed.
  13673.  
  13674.  Protected Mode Exceptions
  13675.  
  13676.  #SS(0) if the top of stack is not within the stack segment.
  13677.  
  13678.  Real Address Mode Exceptions
  13679.  
  13680.  Interrupt 13 for a word operand at 0FFFFH.
  13681.  
  13682.  In real mode the NT and IOPL bits will not be modified.
  13683.  
  13684.  
  13685.  PUSH──Push a Word onto the Stack
  13686.  
  13687.  Opcode     Instruction  Clocks    Description
  13688.  
  13689.   06         PUSH ES      3         Push ES
  13690.   0E         PUSH CS      3         Push CS
  13691.   16         PUSH SS      3         Push SS
  13692.   1E         PUSH DS      3         Push DS
  13693.   50+ rw     PUSH rw      3         Push word register
  13694.   FF  /6     PUSH mw      5         Push memory word
  13695.   68  dw     PUSH dw      3         Push immediate word
  13696.   6A  db     PUSH db      3         Push immediate sign-extended byte
  13697.  
  13698.  Flags Modified
  13699.  
  13700.  None
  13701.  
  13702.  Flags Undefined
  13703.  
  13704.  None
  13705.  
  13706.  Operation
  13707.  
  13708.  The stack pointer SP is decremented by 2, and the operand is placed on the
  13709.  new top of stack, which is pointed to by SS:SP.
  13710.  
  13711.  The 80286 PUSH SP instruction pushes the value of SP as it existed before
  13712.  the instruction. This differs from the 8086, which pushes the new
  13713.  (decremented by 2) value.
  13714.  
  13715.  Protected Mode Exceptions
  13716.  
  13717.  #SS(0) if the new value of SP is outside the stack segment limit.
  13718.  
  13719.  #GP(0) for an illegal memory operand effective address in the CS, DS, or ES
  13720.  segments; #SS(0) for an illegal address in the SS segment.
  13721.  
  13722.  Real Address Mode Exceptions
  13723.  
  13724.  None; the 80286 will shut down if SP = 1 ──due to lack of stack space.
  13725.  
  13726.  
  13727.  PUSHA──Push All General Registers
  13728.  
  13729.  Opcode     Instruction     Clocks     Description
  13730.  
  13731.   60         PUSHA           17         Push in order: AX,CX,DX,BX,original
  13732.                                         SP,BP,SI,DI
  13733.  
  13734.  Flags Modified
  13735.  
  13736.  None
  13737.  
  13738.  Flags Undefined
  13739.  
  13740.  None
  13741.  
  13742.  Operation
  13743.  
  13744.  PUSHA saves the registers noted above on the 80286 stack. The stack pointer
  13745.  SP is decremented by 16 to hold the 8 word values. Since the registers are
  13746.  pushed onto the stack in the order in which they were given, they will
  13747.  appear in the 16 new stack bytes in the reverse order. The last register
  13748.  pushed is DI.
  13749.  
  13750.  Protected Mode Exceptions
  13751.  
  13752.  #SS(0) if the starting or ending address is outside the stack segment
  13753.  limit.
  13754.  
  13755.  Real Address Mode Exceptions
  13756.  
  13757.  The 80286 will shut down if SP = 1, 3, or 5 before executing PUSHA. If SP =
  13758.  7, 9, 11, 13, or 15, exception 13 will occur.
  13759.  
  13760.  
  13761.  PUSHF──Push Flags Register onto the Stack
  13762.  
  13763.  Opcode     Instruction     Clocks     Description
  13764.  
  13765.   9C         PUSHF           3          Push flags register
  13766.  
  13767.  Flags Modified
  13768.  
  13769.  None
  13770.  
  13771.  Flags Undefined
  13772.  
  13773.  None
  13774.  
  13775.  Operation
  13776.  
  13777.  The stack pointer SP is decremented by 2, and the 80286 flags register is
  13778.  copied to the new top of stack, which is pointed to by SS:SP. The flags,
  13779.  from the top bit (15) to the bottom bit (0), are as follows: undefined,
  13780.  nested task, I/O privilege level (2 bits), overflow, direction, interrupts
  13781.  enabled, trap, sign, zero, undefined, auxiliary carry, undefined, parity,
  13782.  undefined, and carry.
  13783.  
  13784.  Protected Mode Exceptions
  13785.  
  13786.  #SS(0) if the new value of SP is outside the stack segment limit.
  13787.  
  13788.  Real Address Mode Exceptions
  13789.  
  13790.  None; the 80286 will shut down if SP=1 due──to lack of stack space.
  13791.  
  13792.  
  13793.  RCL/RCR/ROL/ROR──Rotate Instructions
  13794.  
  13795. ╓┌─────────┌────────────┌─────────┌──────────────────────────────────────────
  13796.  Opcode    Instruction  Clocks-N Description
  13797.  D0 /2     RCL eb,1     2,mem=7   Rotate 9-bits (CF, EA byte) left once
  13798.  D2 /2     RCL eb,CL    5,mem=8   Rotate 9-bits (CF, EA byte) left CL times
  13799.  Opcode    Instruction  Clocks-N Description
  13800. D2 /2     RCL eb,CL    5,mem=8   Rotate 9-bits (CF, EA byte) left CL times
  13801.  C0 /2 db  RCL eb,db    5,mem=8   Rotate 9-bits (CF, EA byte) left db times
  13802.  D1 /2     RCL ew,1     2,mem=7   Rotate 17-bits (CF, EA word) left once
  13803.  D3 /2     RCL ew,CL    5,mem=8   Rotate 17-bits (CF, EA word) left CL times
  13804.  C1 /2 db  RCL ew,db    5,mem=8   Rotate 17-bits (CF, EA word) left db times
  13805.  D0 /3     RCR eb,1     2,mem=7   Rotate 9-bits (CF, EA byte) right once
  13806.  D2 /3     RCR eb,CL    5,mem=8   Rotate 9-bits (CF, EA byte) right CL times
  13807.  C0 /3 db  RCR eb,db    5,mem=8   Rotate 9-bits (CF, EA byte) right db times
  13808.  D1 /3     RCR ew,1     2,mem=7   Rotate 17-bits (CF, EA word) right once
  13809.  D3 /3     RCR ew,CL    5,mem=8   Rotate 17-bits (CF, EA word) right CL times
  13810.  C1 /3 db  RCR ew,db    5,mem=8   Rotate 17-bits (CF, EA word) right db times
  13811.  D0 /0     ROL eb,1     2,mem=7   Rotate 8-bit EA byte left once
  13812.  D2 /0     ROL eb,CL    5,mem=8   Rotate 8-bit EA byte left CL times
  13813.  C0 /0 db  ROL eb,db    5,mem=8   Rotate 8-bit EA byte left db times
  13814.  D1 /0     ROL ew,1     2,mem=7   Rotate 16-bit EA word left once
  13815.  D3 /0     ROL ew,CL    5,mem=8   Rotate 16-bit EA word left CL times
  13816.  C1 /0 db  ROL ew,db    5,mem=8   Rotate 16-bit EA word left db times
  13817.  D0 /1     ROR eb,1     2,mem=7   Rotate 8-bit EA byte right once
  13818.  D2 /1     ROR eb,CL    5,mem=8   Rotate 8-bit EA byte right CL times
  13819.  C0 /1 db  ROR eb,db    5,mem=8   Rotate 8-bit EA byte right db times
  13820.  Opcode    Instruction  Clocks-N Description
  13821. C0 /1 db  ROR eb,db    5,mem=8   Rotate 8-bit EA byte right db times
  13822.  D1 /1     ROR ew,1     2,mem=7   Rotate 16-bit EA word right once
  13823.  D3 /1     ROR ew,CL    5,mem=8   Rotate 16-bit EA word right CL times
  13824.  C1 /1 db  ROR ew,db    5,mem=8   Rotate 16-bit EA word right db times
  13825.  
  13826.  
  13827.  Flags Modified
  13828.  
  13829.  Overflow (only for single rotates), carry
  13830.  
  13831.  Flags Undefined
  13832.  
  13833.  Overflow for multi-bit rotates
  13834.  
  13835.  Operation
  13836.  
  13837.  Each rotate instruction shifts the bits of the register or memory operand
  13838.  given. The left rotate instructions shift all of the bits upward, except for
  13839.  the top bit, which comes back around to the bottom. The right rotate
  13840.  instructions do the reverse: the bits shift downward, with the bottom bit
  13841.  coming around to the top.
  13842.  
  13843.  For the RCL and RCR instructions, the carry flag is part of the rotated
  13844.  quantity. RCL shifts the carry flag into the bottom bit and shifts the top
  13845.  bit into the carry flag; RCR shifts the carry flag into the top bit and
  13846.  shifts the bottom bit into the carry flag. For the ROL and ROR
  13847.  instructions, the original value of the carry flag is not a part of the
  13848.  result; nonetheless, the carry flag receives a copy of the bit that was
  13849.  shifted from one end to the other.
  13850.  
  13851.  The rotate is repeated the number of times indicated by the second operand,
  13852.  which is either an immediate number or the contents of the CL register. To
  13853.  reduce the maximum execution time, the 80286 does not allow rotation counts
  13854.  greater than 31. If a rotation count greater than 31 is attempted, only the
  13855.  bottom five bits of the rotation are used. The 8086 does not mask rotate
  13856.  counts.
  13857.  
  13858.  The overflow flag is set only for the single-rotate (second operand = 1)
  13859.  forms of the instructions. The OF bit is set to be accurate if a shift of
  13860.  length 1 is done. Since it is undefined for all other values, including a
  13861.  zero shift, it can always be set for the count-of-1 case regardless of the
  13862.  actual count. For left shifts/rotates, the CF bit after the shift is XORed
  13863.  with the high-order result bit. For right shifts/rotates, the high-order
  13864.  two bits of the result are XORed to get OF. Neither flag bit is modified
  13865.  when the count value is zero.
  13866.  
  13867.  Protected Mode Exceptions
  13868.  
  13869.  #GP(0) if the result is in a non-writable segment. #GP(0) for an illegal
  13870.  memory operand effective address in the CS, DS, or ES segments; #SS(0) for
  13871.  an illegal address in the SS segment.
  13872.  
  13873.  Real Address Mode Exceptions
  13874.  
  13875.  Interrupt 13 for a word operand at offset 0FFFFH.
  13876.  
  13877.  
  13878.  REP/REPE/REPNE──Repeat Following String Operation
  13879.  
  13880. ╓┌───────┌────────────────────────┌─────────┌────────────────────────────────
  13881.  Opcode  Instruction      Clocks   Description
  13882.  F3 6C   REP INS eb,DX    5+4 CX    Input CX bytes from port DX into ES:[DI]
  13883.  Opcode  Instruction      Clocks   Description
  13884.  F3 6C   REP INS eb,DX    5+4 CX    Input CX bytes from port DX into ES:[DI]
  13885.  F3 6D   REP INS ew,DX    5+4 CX    Input CX words from port DX into ES:[DI]
  13886.  F3 6C   REP INSB         5+4 CX    Input CX bytes from port DX into ES:[DI]
  13887.  F3 6D   REP INSW         5+4 CX    Input CX words from port DX into ES:[DI]
  13888.  F3 A4   REP MOVS mb,mb   5+4 CX    Move CX bytes from [SI] to ES:[DI]
  13889.  F3 A5   REP MOVS mw,mw   5+4 CX    Move CX words from [SI] to ES:[DI]
  13890.  F3 A4   REP MOVSB        5+4 CX    Move CX bytes from DS:[SI] to ES:[DI]
  13891.  F3 A5   REP MOVSW        5+4 CX    Move CX words from DS:[SI] to ES:[DI]
  13892.  F3 6E   REP OUTS DX,eb   5+4 CX    Output CX bytes from [SI] to port DX
  13893.  F3 6F   REP OUTS DX,ew   5+4 CX    Output CX words from [SI] to port DX
  13894.  F3 6E   REP OUTSB        5+4 CX    Output CX bytes from DS:[SI] to port DX
  13895.  F3 6F   REP OUTSW        5+4 CX    Output CX words from DS:[SI] to port DX
  13896.  F3 AA   REP STOS mb      4+3 CX    Fill CX bytes at ES:[DI] with AL
  13897.  F3 AB   REP STOS mw      4+3 CX    Fill CX words at ES:[DI] with AX
  13898.  F3 AA   REP STOSB        4+3 CX    Fill CX bytes at ES:[DI] with AL
  13899.  F3 AB   REP STOSW        4+3 CX    Fill CX words at ES:[DI] with AX
  13900.  F3 A6   REPE CMPS mb,mb  5+9 N     Find nonmatching bytes in
  13901.                                     ES:[DI] and [SI]
  13902.  F3 A7   REPE CMPS mw,mw  5+9 N     Find nonmatching words in
  13903.                                     ES:[DI] and [SI]
  13904.  Opcode  Instruction      Clocks   Description
  13905.                                    ES:[DI] and [SI]
  13906.  F3 A6   REPE CMPSB       5+9 N     Find nonmatching bytes in ES:[DI]
  13907.                                     and DS:[SI]
  13908.  F3 A7   REPE CMPSW       5+9 N     Find nonmatching words in ES:[DI]
  13909.                                     and DS:[SI]
  13910.  F3 AE   REPE SCAS mb     5+8 N     Find non-AL byte starting at ES:[DI]
  13911.  F3 AF   REPE SCAS mw     5+8 N     Find non-AX word starting at ES:[DI]
  13912.  F3 AE   REPE SCASB       5+8 N     Find non-AL byte starting at ES:[DI]
  13913.  F3 AF   REPE SCASW       5+8 N     Find non-AX word starting at ES:[DI]
  13914.  F2 A6   REPNE CMPS mb,mb 5+9 N     Find matching bytes in
  13915.                                     ES:[DI] and [SI]
  13916.  F2 A7   REPNE CMPS mw,mw 5+9 N     Find matching words in
  13917.                                     ES:[DI] and [SI]
  13918.  F2 A6   REPNE CMPSB      5+9 N     Find matching bytes in ES:[DI]
  13919.                                     and DS:[SI]
  13920.  F2 A7   REPNE CMPSW      5+9 N     Find matching words in ES:[DI]
  13921.                                     and DS:[SI]
  13922.  F2 AE   REPNE SCAS mb    5+8 N     Find AL, starting at ES:[DI]
  13923.  F2 AF   REPNE SCAS mw    5+8 N     Find AX, starting at ES:[DI]
  13924.  F2 AE   REPNE SCASB      5y+8 N    Find AL, starting at ES:[DI]
  13925.  Opcode  Instruction      Clocks   Description
  13926. F2 AE   REPNE SCASB      5y+8 N    Find AL, starting at ES:[DI]
  13927.  F2 AF   REPNE SCASW      5+8 N     Find AX, starting at ES:[DI]
  13928.  
  13929.  
  13930.  Flags Modified
  13931.  
  13932.  By CMPS and SCAS, none by REP
  13933.  
  13934.  Flags Undefined
  13935.  
  13936.  None
  13937.  
  13938.  Operation
  13939.  
  13940.  REP, REPE, and REPNE are prefix operations. These prefixes cause the string
  13941.  instruction that follows to be repeated CX times or (for REPE and REPNE)
  13942.  until the indicated condition in the zero flag is no longer met. Thus, REPE
  13943.  stands for "Repeat while equal," REPNE for "Repeat while not equal."
  13944.  
  13945.  The REP prefixes make sense only in the contexts listed above. They cannot
  13946.  be applied to anything other than string operations.
  13947.  
  13948.  Synonymous forms of REPE and REPNE are REPZ and REPNZ, respectively.
  13949.  
  13950.  The REP prefixes apply only to one string instruction at a time. To repeat
  13951.  a block of instructions, use a LOOP construct.
  13952.  
  13953.  The precise action for each iteration is as follows:
  13954.  
  13955.    1.  Check the CX register. If it is zero, exit the iteration and move to
  13956.        the next instruction.
  13957.  
  13958.    2.  Acknowledge any pending interrupts.
  13959.  
  13960.    3.  Perform the string operation once.
  13961.  
  13962.    4.  Decrement CX by 1; no flags are modified.
  13963.  
  13964.    5.  If the string operation is SCAS or CMPS, check the zero flag. If the
  13965.        repeat condition does not hold, then exit the iteration and move to
  13966.        the next instruction. Exit if the prefix is REPE and ZF=0 (the last
  13967.        comparison was not equal), or if the prefix is REPNE and ZF=1 (the
  13968.        last comparison was equal).
  13969.  
  13970.    6.  Go to step 1 for the next iteration.
  13971.  
  13972.  As defined by the individual string-ops, the direction of movement through
  13973.  the block is determined by the direction flag. If the direction flag is 1
  13974.  (STD was executed), SI and/or DI start at the end of the block and move
  13975.  backward; if the direction flag is 0 (CLD was executed), SI and/or DI start
  13976.  at the beginning of the block and move forward.
  13977.  
  13978.  For repeated SCAS and CMPS operations the repeat can be exited for one of
  13979.  two different reasons: the CX count can be exhausted or the zero flag can
  13980.  fail the repeat condition. Your code will probably want to distinguish
  13981.  between the two cases. It can do so via either the JCXZ instruction or the
  13982.  conditional jumps that test the zero flag (JZ, JNZ, JE, and JNE).
  13983.  
  13984.  Not all input/output ports can handle the rate at which the repeated I/O
  13985.  instructions execute.
  13986.  
  13987.  Protected Mode Exceptions
  13988.  
  13989.  None by REP; exceptions can be generated when the string-op is executed.
  13990.  
  13991.  Real Address Mode Exceptions
  13992.  
  13993.  None by REP; exceptions can be generated when the string-op is executed.
  13994.  
  13995.  
  13996.  RET──Return from Procedure
  13997.  
  13998.  Opcode  Instruction  Clocks    Description
  13999.  
  14000.   CB      RET          15,pm=25   Return to far caller, same privilege
  14001.   CB      RET          55         Return, lesser privilege, switch stacks
  14002.   C3      RET          11         Return to near caller, same privilege
  14003.   CA dw   RET dw       15,pm=25   RET (far), same privilege, pop dw bytes
  14004.   CA dw   RET dw       55         RET (far), lesser privilege, pop dw bytes
  14005.   C2 dw   RET dw       11         RET (near), same privilege, pop dw bytes
  14006.                                   pushed before Call
  14007.  
  14008.  Flags Modified
  14009.  
  14010.  None
  14011.  
  14012.  Flags Undefined
  14013.  
  14014.  None
  14015.  
  14016.  Operation
  14017.  
  14018.  RET transfers control to a return address located on the stack. The address
  14019.  is usually placed on the stack by a CALL instruction; in that case, the
  14020.  return is made to the instruction that follows the CALL.
  14021.  
  14022.  There is an optional numeric parameter to RET. It gives the number of stack
  14023.  bytes to be released after the return address is popped. These bytes are
  14024.  typically used as input parameters to the procedure called.
  14025.  
  14026.  For the intra-segment return, the address on the stack is a 2-byte quantity
  14027.  popped into IP. The CS register is unchanged.
  14028.  
  14029.  For the inter-segment return, the address on the stack is a 4-byte-long
  14030.  pointer. The offset is popped first, followed by the selector. In real
  14031.  address mode, CS and IP are directly loaded.
  14032.  
  14033.  In protected mode, an inter-segment return causes the processor to consult
  14034.  the descriptor addressed by the return selector. The AR byte of the
  14035.  descriptor must indicate a code segment of equal or less privilege (of
  14036.  greater or equal numeric value) than the current privilege level. Returns
  14037.  to a lesser privilege level cause the stack to be reloaded from the value
  14038.  saved beyond the parameter block.
  14039.  
  14040.  The DS and ES segment registers may be set to zero by the inter-segment RET
  14041.  instruction. If these registers refer to segments which cannot be used by
  14042.  the new privilege level, they are set to zero to prevent unauthorized
  14043.  access.
  14044.  
  14045.  The following list of checks and actions describes the protected-mode
  14046.  inter-segment return in detail.
  14047.  
  14048.  Inter-segment RET:
  14049.      Second word on stack must be within stack limits else #SS(0)
  14050.      Return selector RPL must be ≥ CPL else #GP (return selector)
  14051.      If return selector RPL = CPL then
  14052.      RETURN TO SAME LEVEL:
  14053.      Return selector must be non-null else #GP(0)
  14054.      Selector index must be within its descriptor table limits else
  14055.      #GP (selector)
  14056.      Descriptor AR byte must indicate code segment else #GP (selector)
  14057.      If non-conforming then code segment DPL must equal CPL else
  14058.      #GP (selector)
  14059.      If conforming then code segment DPL must be ≤ CPL else #GP (selector)
  14060.      Code segment must be PRESENT else #NP (selector)
  14061.      Top word on stack must be within stack limits else #SS(0)
  14062.      IP must be in code segment limit else #GP(0)
  14063.      Load CS:IP from stack
  14064.      Load CS-cache with descriptor
  14065.      Increment SP by 4 plus the immediate offset if it exists
  14066.  Else
  14067.      RETURN TO OUTER PRIVILEGE LEVEL:
  14068.      Top (8+immediate) bytes on stack must be within stack limits else #SS(0)
  14069.      Examine return CS selector (at SP+2) and associated descriptor:
  14070.         Selector must be non-null else #GP(0)
  14071.         Selector index must be within its descriptor table limits else
  14072.         #GP (selector)
  14073.         Descriptor AR byte must indicate code segment else #GP (selector)
  14074.         If non-conforming then code segment DPL must equal return selector
  14075.         RPL else #GP (selector)
  14076.         If conforming then code segment DPL must be ≤ return selector RPL
  14077.         else #GP (selector)
  14078.         Segment must be PRESENT else #NP (selector)
  14079.      Examine return SS selector (at SP+6+imm) and associated descriptor:
  14080.         Selector must be non-null else #GP(0)
  14081.         Selector index must be within its descriptor table limits else
  14082.         #GP (selector)
  14083.         Selector RPL must equal the RPL of the return CS selector else
  14084.         #GP (selector)
  14085.         Descriptor AR byte must indicate a writable data segment else
  14086.         #GP (selector)
  14087.         Descriptor DPL must equal the RPL of the return CS selector else
  14088.         #GP (selector)
  14089.         Segment must be PRESENT else #SS (selector)
  14090.      IP must be in code segment limit else # GP(0)
  14091.      Set CPL to the RPL of the return CS selector
  14092.      Load CS:IP from stack
  14093.      Set CS RPL to CPL
  14094.      Increment SP by 4 plus the immediate offset if it exists
  14095.      Load SS:SP from stack
  14096.      Load the CS-cache with the return CS descriptor
  14097.      Load the SS-cache with the return SS descriptor
  14098.      For each of ES and DS:
  14099.         If the current register setting is not valid for the outer level,
  14100.         set the register to null (selector = AR = 0)
  14101.         To be valid, the register setting must satisfy the following
  14102.         properties:
  14103.             Selector index must be within descriptor table limits
  14104.             Descriptor AR byte must indicate data or readable code segment
  14105.             If segment is data or non-conforming code, then:
  14106.                DPL must be ≥ CPL, or
  14107.                DPL must be ≥ RPL
  14108.  
  14109.  Protected Mode Exceptions
  14110.  
  14111.  #GP, #NP, or #SS, as described in the above listing.
  14112.  
  14113.  Real Address Mode Exceptions
  14114.  
  14115.  Interrupt 13 if the stack pop wraps around from 0FFFFH to 0.
  14116.  
  14117.  
  14118.  SAHF──Store AH into Flags
  14119.  
  14120.  Opcode    Instruction     Clocks   Description
  14121.  
  14122.   9E         SAHF            2       Store AH into flags
  14123.                                      SF ZF xx AF xx PF xx CF
  14124.  
  14125.  Flags Modified
  14126.  
  14127.  Sign, zero, auxiliary carry, parity, carry
  14128.  
  14129.  Flags Undefined
  14130.  
  14131.  None
  14132.  
  14133.  Operation
  14134.  
  14135.  The flags listed above are loaded with values from the AH register, from
  14136.  bits 7, 6, 4, 2, and 0, respectively.
  14137.  
  14138.  Protected Mode Exceptions
  14139.  
  14140.  None
  14141.  
  14142.  Real Address Mode Exceptions
  14143.  
  14144.  None
  14145.  
  14146.  
  14147.  SAL/SAR/SHL/SHR──Shift Instructions
  14148.  
  14149.  Opcode     Instruction   Clocks-N    Description
  14150.  
  14151.  D0 /4      SAL eb,1      2,mem=7      Multiply EA byte by 2, once
  14152.  D2 /4      SAL eb,CL     5,mem=8      Multiply EA byte by 2, CL times
  14153.  C0 /4 db   SAL eb,db     5,mem=8      Multiply EA byte by 2, db times
  14154.  D1 /4      SAL ew,1      2,mem=7      Multiply EA word by 2, once
  14155.  D3 /4      SAL ew,CL     5,mem=8      Multiply EA word by 2, CL times
  14156.  C1 /4 db   SAL ew,db     5,mem=8      Multiply EA word by 2, db times
  14157.  D0 /7      SAR eb,1      2,mem=7      Signed divide EA byte by 2, once
  14158.  D2 /7      SAR eb,CL     5,mem=8      Signed divide EA byte by 2, CL times
  14159.  C0 /7 db   SAR eb,db     5,mem=8      Signed divide EA byte by 2, db times
  14160.  D1 /7      SAR ew,1      2,mem=7      Signed divide EA word by 2, once
  14161.  D3 /7      SAR ew,CL     5,mem=8      Signed divide EA word by 2, CL times
  14162.  C1 /7 db   SAR ew,db     5,mem=8      Signed divide EA word by 2, db times
  14163.  D0 /5      SHR eb,1      2,mem=7      Unsigned divide EA byte by 2, once
  14164.  D2 /5      SHR eb,CL     5,mem=8      Unsigned divide EA byte by 2, CL times
  14165.  C0 /5 db   SHR eb,db     5,mem=8      Unsigned divide EA byte by 2, db times
  14166.  D1 /5      SHR ew,1      2,mem=7      Unsigned divide EA word by 2, once
  14167.  D3 /5      SHR ew,CL     5,mem=8      Unsigned divide EA word by 2, CL times
  14168.  
  14169.  
  14170.  Flags Modified
  14171.  
  14172.  Overflow (only for single-shift form), carry, zero, parity, sign
  14173.  
  14174.  Flags Undefined
  14175.  
  14176.  Auxiliary carry; also overflow for multibit shifts (only).
  14177.  
  14178.  Operation
  14179.  
  14180.  SAL (or its synonym SHL) shifts the bits of the operand upward. The
  14181.  high-order bit is shifted into the carry flag, and the low-order bit is set
  14182.  to 0.
  14183.  
  14184.  SAR and SHR shift the bits of the operand downward. The low-order bit is
  14185.  shifted into the carry flag. The effect is to divide the operand by 2. SAR
  14186.  performs a signed divide: the high-order bit remains the same. SHR performs
  14187.  an unsigned divide: the high-order bit is set to 0.
  14188.  
  14189.  The shift is repeated the number of times indicated by the second operand,
  14190.  which is either an immediate number or the contents of the CL register. To
  14191.  reduce the maximum execution time, the 80286 does not allow shift counts
  14192.  greater than 31. If a shift count greater than 31 is attempted, only the
  14193.  bottom five bits of the shift count are used. The 8086 uses all 8 bits of
  14194.  the shift count.
  14195.  
  14196.  The overflow flag is set only if the single-shift forms of the instructions
  14197.  are used. For left shifts, it is set to 0 if the high bit of the answer is
  14198.  the same as the result carry flag (i.e., the top two bits of the original
  14199.  operand were the same); it is set to 1 if they are different. For SAR it is
  14200.  set to 0 for all single shifts. For SHR, it is set to the high-order bit of
  14201.  the original operand. Neither flag bit is modified when the count value is
  14202.  zero.
  14203.  
  14204.  Protected Mode Exceptions
  14205.  
  14206.  #GP(0) if the operand is in a non-writable segment. #GP(0) for an illegal
  14207.  memory operand effective address in the CS, DS, or ES segments; #SS(0) for
  14208.  an illegal address in the SS segment.
  14209.  
  14210.  Real Address Mode Exceptions
  14211.  
  14212.  Interrupt 13 for a word operand at offset 0FFFFH.
  14213.  
  14214.  
  14215.  SBB──Integer Subtraction With Borrow
  14216.  
  14217.  Opcode      Instruction  Clocks    Description
  14218.  
  14219.  18 /r       SBB eb,rb    2,mem=7   Subtract with borrow byte
  14220.                                     register from EA byte
  14221.  19 /r       SBB ew,rw    2,mem=7   Subtract with borrow word
  14222.                                     register from EA word
  14223.  1A /r       SBB rb,eb    2,mem=7   Subtract with borrow EA byte
  14224.                                     from byte register
  14225.  1B /r       SBB rw,ew    2,mem=7   Subtract with borrow EA word
  14226.                                     from word register
  14227.  1C db       SBB AL,db    3         Subtract with borrow imm.
  14228.                                     byte from AL
  14229.  1D dw       SBB AX,dw    3         Subtract with borrow imm.
  14230.                                     word from AX
  14231.  80 /3 db    SBB eb,db    3,mem=7   Subtract with borrow imm. byte
  14232.                                     from EA byte
  14233.  81 /3 dw    SBB ew,dw    3,mem=7   Subtract with borrow imm. word
  14234.                                     from EA word
  14235.  83 /3 db    SBB ew,db    3,mem=7   Subtract with borrow imm. byte
  14236.                                     from EA word
  14237.  
  14238.  Flags Modified
  14239.  
  14240.  Overflow, sign, zero, auxiliary carry, parity, carry
  14241.  
  14242.  Flags Undefined
  14243.  
  14244.  None
  14245.  
  14246.  Operation
  14247.  
  14248.  The second operand is added to the carry flag and the result is subtracted
  14249.  from the first operand. The first operand is replaced with the result of the
  14250.  subtraction, and the flags are set accordingly.
  14251.  
  14252.  When a byte-immediate value is subtracted from a word operand, the
  14253.  immediate value is first sign-extended.
  14254.  
  14255.  Protected Mode Exceptions
  14256.  
  14257.  #GP(0) if the result is in a non-writable segment. #GP(0) for an illegal
  14258.  memory operand effective address in the CS, DS, or ES segments; #SS(0) for
  14259.  an illegal address in the SS segment.
  14260.  
  14261.  Real Address Mode Exceptions
  14262.  
  14263.  Interrupt 13 for a word operand at offset 0FFFFH.
  14264.  
  14265.  
  14266.  SCAS/SCASB/SCASW──Compare String Data
  14267.  
  14268.  Opcode   Instruction   Clocks   Description
  14269.  
  14270.   AE       SCAS mb       7        Compare bytes AL - ES:[DI], advance DI
  14271.   AF       SCAS mw       7        Compare words AX - ES:[DI], advance DI
  14272.   AE       SCASB         7        Compare bytes AL - ES:[DI], advance DI
  14273.   AF       SCASW         7        Compare words AX - ES:[DI], advance DI
  14274.  
  14275.  Flags Modified
  14276.  
  14277.  Overflow, sign, zero, auxiliary carry, parity, carry
  14278.  
  14279.  Flags Undefined
  14280.  
  14281.  None
  14282.  
  14283.  Operation
  14284.  
  14285.  SCAS subtracts the memory byte or word at ES:DI from the AL or AX register.
  14286.  The result is discarded; only the flags are set. The operand must be
  14287.  addressable from the ES register; no segment override is possible.
  14288.  
  14289.  After the comparison is made, DI is automatically advanced. If the
  14290.  direction flag is 0 (CLD was executed), DI increments; if the direction flag
  14291.  is 1 (STD was executed), DI decrements. DI increments or decrements by 1 if
  14292.  bytes were compared; by 2 if words were compared.
  14293.  
  14294.  SCAS can be preceded by the REPE or REPNE prefix for a block search of CX
  14295.  bytes or words. Refer to the REP instruction for details of this operation.
  14296.  
  14297.  Protected Mode Exceptions
  14298.  
  14299.  #GP(0) for an illegal memory operand effective address in the CS, DS, or ES
  14300.  segments; #SS(0) for an illegal address in the SS segment.
  14301.  
  14302.  Real Address Mode Exceptions
  14303.  
  14304.  Interrupt 13 for a word operand at offset 0FFFFH.
  14305.  
  14306.  
  14307.  SGDT/SIDT──Store Global/Interrupt Descriptor Table Register
  14308.  
  14309.  Opcode     Instruction  Clocks    Description
  14310.  
  14311.  0F 01 /0   SGDT m       11        Store Global Descriptor Table register
  14312.                                    to m
  14313.  0F 01 /1   SIDT m       12        Store Interrupt Descriptor Table
  14314.                                    register to m
  14315.  
  14316.  Flags Modified
  14317.  
  14318.  None
  14319.  
  14320.  Flags Undefined
  14321.  
  14322.  None
  14323.  
  14324.  Operation
  14325.  
  14326.  The contents of the descriptor table register are copied to six bytes of
  14327.  memory indicated by the operand. The LIMIT field of the register goes to the
  14328.  first word at the effective address; the next three bytes get the BASE field
  14329.  of the register; and the last byte is undefined.
  14330.  
  14331.  SGDT and SIDT appear only in operating systems software; they are not used
  14332.  in applications programs.
  14333.  
  14334.  Protected Mode Exceptions
  14335.  
  14336.  #UD if the destination operand is a register. #GP(0) if the destination is
  14337.  in a non-writable segment. #GP(0) for an illegal memory operand effective
  14338.  address in the CS, DS, or ES segments; #SS(0) for an illegal address in the
  14339.  SS segment.
  14340.  
  14341.  Real Address Mode Exceptions
  14342.  
  14343.  These instructions are valid in Real Address mode to facilitate power-up or
  14344.  to reset initialization prior to entering Protected mode.
  14345.  
  14346.  #UD if the destination operand is a register. Interrupt 13 for a word
  14347.  operand at offset 0FFFFH.
  14348.  
  14349.  
  14350.  SLDT──Store Local Descriptor Table Register
  14351.  
  14352.  Opcode    Instruction   Clocks    Description
  14353.  
  14354.  0F 00 /0   SLDT ew       2,mem=3   Store Local Descriptor Table register to
  14355.                                     EA word
  14356.  
  14357.  Flags Modified
  14358.  
  14359.  None
  14360.  
  14361.  Flags Undefined
  14362.  
  14363.  None
  14364.  
  14365.  Operation
  14366.  
  14367.  The Local Descriptor Table register is stored in the 2-byte register or
  14368.  memory location indicated by the effective address operand. This register is
  14369.  a selector that points into the Global Descriptor Table.
  14370.  
  14371.  SLDT appears only in operating systems software. It is not used in
  14372.  applications programs.
  14373.  
  14374.  Protected Mode Exceptions
  14375.  
  14376.  #GP(0) if the destination is in a non-writable segment. #GP(0) for an
  14377.  illegal memory operand effective address in the CS, DS, or ES segments;
  14378.  #SS(0) for an illegal address in the SS segment.
  14379.  
  14380.  Real Address Mode Exceptions
  14381.  
  14382.  Interrupt 6; SLDT is not recognized in Real Address mode.
  14383.  
  14384.  
  14385.  SMSW──Store Machine Status Word
  14386.  
  14387.  Opcode     Instruction   Clocks      Description
  14388.  
  14389.  0F 01 /4   SMSW ew       2,mem=3     Store Machine Status Word to EA word
  14390.  
  14391.  Flags Modified
  14392.  
  14393.  None
  14394.  
  14395.  Flags Undefined
  14396.  
  14397.  None
  14398.  
  14399.  Operation
  14400.  
  14401.  The Machine Status Word is stored in the 2-byte register or memory location
  14402.  indicated by the effective address operand.
  14403.  
  14404.  Protected Mode Exceptions
  14405.  
  14406.  #GP(0) if the destination is in a non-writable segment. #GP(0) for an
  14407.  illegal memory operand effective address in the CS, DS, or ES segments;
  14408.  #SS(0) for an illegal address in the SS segment.
  14409.  
  14410.  Real Address Mode Exceptions
  14411.  
  14412.  Interrupt 13 for a word operand at offset 0FFFFH.
  14413.  
  14414.  
  14415.  STC──Set Carry Flag
  14416.  
  14417.  Opcode     Instruction      Clocks      Description
  14418.  
  14419.   F9         STC              2           Set carry flag
  14420.  
  14421.  Flags Modified
  14422.  
  14423.  Carry=1
  14424.  
  14425.  Flags Undefined
  14426.  
  14427.  None
  14428.  
  14429.  Operation
  14430.  
  14431.  The carry flag is set to 1.
  14432.  
  14433.  Protected Mode Exceptions
  14434.  
  14435.  None
  14436.  
  14437.  Real Address Mode Exceptions
  14438.  
  14439.  None
  14440.  
  14441.  
  14442.  STD──Set Direction Flag
  14443.  
  14444.  Opcode   Instruction   Clocks    Description
  14445.  
  14446.  FD        STD           2         Set direction flag so SI and DI
  14447.                                    will decrement
  14448.  
  14449.  Flags Modified
  14450.  
  14451.  Direction=1
  14452.  
  14453.  Flags Undefined
  14454.  
  14455.  None
  14456.  
  14457.  Operation
  14458.  
  14459.  The direction flag is set to 1. This causes all subsequent string
  14460.  operations to decrement the index registers (SI and/or DI) on which they
  14461.  operate.
  14462.  
  14463.  Protected Mode Exceptions
  14464.  
  14465.  None
  14466.  
  14467.  Real Address Mode Exceptions
  14468.  
  14469.  None
  14470.  
  14471.  
  14472.  STI──Set Interrupt Enable Flag
  14473.  
  14474.  Opcode   Instruction   Clocks   Description
  14475.  
  14476.  FB        STI             2      Set interrupt enable flag,
  14477.                                   interrupts enabled
  14478.  
  14479.  Flags Modified
  14480.  
  14481.  Interrupt=1 (enabled)
  14482.  
  14483.  Flags Undefined
  14484.  
  14485.  None
  14486.  
  14487.  Operation
  14488.  
  14489.  The interrupts-enabled flag is set to 1. The 80286 will now respond to
  14490.  external interrupts after executing the STI instruction.
  14491.  
  14492.  Protected Mode Exceptions
  14493.  
  14494.  #GP(0) if the current privilege level is bigger (has less privilege) than
  14495.  the I/O privilege level.
  14496.  
  14497.  Real Address Mode Exceptions
  14498.  
  14499.  None
  14500.  
  14501.  
  14502.  STOS/STOSB/STOSW──Store String Data
  14503.  
  14504.  Opcode  Instruction     Clocks    Description
  14505.  
  14506.  AA       STOS mb         3          Store AL to byte ES:[DI], advance DI
  14507.  AB       STOS mw         3          Store AX to word ES:[DI], advance DI
  14508.  AA       STOSB           3          Store AL to byte ES:[DI], advance DI
  14509.  AB       STOSW           3          Store AX to word ES:[DI], advance DI
  14510.  
  14511.  Flags Modified
  14512.  
  14513.  None
  14514.  
  14515.  Flags Undefined
  14516.  
  14517.  None
  14518.  
  14519.  Operation
  14520.  
  14521.  STOS transfers the contents the AL or AX register to the memory byte or
  14522.  word at ES:DI. The operand must be addressable from the ES register; no
  14523.  segment override is possible.
  14524.  
  14525.  After the transfer is made, DI is automatically advanced. If the direction
  14526.  flag is 0 (CLD was executed), DI increments; if the direction flag is 1 (STD
  14527.  was executed), DI decrements. DI increments or decrements by 1 if a byte was
  14528.  moved; by 2 if a word was moved.
  14529.  
  14530.  STOS can be preceded by the REP prefix for a block fill of CX bytes or
  14531.  words. Refer to the REP instruction for details of this operation.
  14532.  
  14533.  Protected Mode Exceptions
  14534.  
  14535.  #GP(0) if the destination is in a non-writable segment. #GP(0) for an
  14536.  illegal memory operand effective address in the CS, DS, or ES segments;
  14537.  #SS(0) for an illegal address in the SS segment.
  14538.  
  14539.  Real Address Mode Exceptions
  14540.  
  14541.  Interrupt 13 for a word operand at offset 0FFFFH.
  14542.  
  14543.  
  14544.  STR──Store Task Register
  14545.  
  14546.  Opcode      Instruction    Clocks      Description
  14547.  
  14548.  0F 00 /1     STR ew         2,mem=3     Store Task Register to EA word
  14549.  
  14550.  Flags Modified
  14551.  
  14552.  None
  14553.  
  14554.  Flags Undefined
  14555.  
  14556.  None
  14557.  
  14558.  Operation
  14559.  
  14560.  The contents of the Task Register are copied to the 2-byte register or
  14561.  memory location indicated by the effective address operand.
  14562.  
  14563.  Protected Mode Exceptions
  14564.  
  14565.  #GP(0) if the destination is in a non-writable segment. #GP(0) for an
  14566.  illegal memory operand effective address in the CS, DS, or ES segments;
  14567.  #SS(0) for an illegal address in the SS segment.
  14568.  
  14569.  Real Address Mode Exceptions
  14570.  
  14571.  Interrupt 6; STR is not recognized in Real Address mode.
  14572.  
  14573.  
  14574.  SUB──Integer Subtraction
  14575.  
  14576.  Opcode     Instruction    Clocks      Description
  14577.  
  14578.  28 /r      SUB eb,rb      2,mem=7     Subtract byte register from EA byte
  14579.  29 /r      SUB ew,rw      2,mem=7     Subtract word register from EA word
  14580.  2A /r      SUB rb,eb      2,mem=7     Subtract EA byte from byte register
  14581.  2B /r      SUB rw,ew      2,mem=7     Subtract EA word from word register
  14582.  2C db      SUB AL,db      3           Subtract immediate byte from AL
  14583.  2D dw      SUB AX,dw      3           Subtract immediate word from AX
  14584.  80 /5 db   SUB eb,db      3,mem=7     Subtract immediate byte from EA byte
  14585.  81 /5 dw   SUB ew,dw      3,mem=7     Subtract immediate word from EA word
  14586.  83 /5 db   SUB ew,db      3,mem=7     Subtract immediate byte from
  14587.                                        EA word
  14588.  
  14589.  Flags Modified
  14590.  
  14591.  Overflow, sign, zero, auxiliary carry, parity, carry
  14592.  
  14593.  Flags Undefined
  14594.  
  14595.  None
  14596.  
  14597.  Operation
  14598.  
  14599.  The second operand is subtracted from the first operand, and the first
  14600.  operand is replaced with the result.
  14601.  
  14602.  When a byte-immediate value is subtracted from a word operand, the
  14603.  immediate value is firstsign-extended.
  14604.  
  14605.  Protected Mode Exceptions
  14606.  
  14607.  #GP(0) if the result is in a non-writable segment. #GP(0) for an illegal
  14608.  memory operand effective address in the CS, DS, or ES segments; #SS(0) for
  14609.  an illegal address in the SS segment.
  14610.  
  14611.  Real Address Mode Exceptions
  14612.  
  14613.  Interrupt 13 for a word operand at offset 0FFFFH.
  14614.  
  14615.  
  14616.  TEST──Logical Compare
  14617.  
  14618.  Opcode    Instruction    Clocks     Description
  14619.  
  14620.  84 /r      TEST eb,rb    2,mem=6    AND byte register into EA byte
  14621.                                      for flags only
  14622.  84 /r      TEST rb,eb    2,mem=6    AND EA byte into byte register
  14623.                                      for flags only
  14624.  85 /r      TEST ew,rw    2,mem=6    AND word register into EA word
  14625.                                      for flags only
  14626.  85 /r      TEST rw,ew    2,mem=6    AND EA word into word register
  14627.                                      for flags only
  14628.  A8 db      TEST AL,db    3          AND immediate byte into AL
  14629.                                      for flags only
  14630.  A9 dw      TEST AX,dw    3          AND immediate word into AX
  14631.                                      for flags only
  14632.  F6 /0 db   TEST eb,db    3,mem=6    AND immediate byte into EA byte
  14633.                                      for flags only
  14634.  F7 /0 dw   TEST ew,dw    3,mem=6    AND immediate word into EA word
  14635.                                      for flags only
  14636.  
  14637.  Flags Modified
  14638.  
  14639.  Overflow=0, sign, zero, parity, carry=0
  14640.  
  14641.  Flags Undefined
  14642.  
  14643.  Auxiliary carry
  14644.  
  14645.  Operation
  14646.  
  14647.  TEST computes the bit-wise logical AND of the two operands given. Each bit
  14648.  of the result is 1 if both of the corresponding bits of the operands are 1;
  14649.  each bit is 0 otherwise. The result of the operation is discarded; only the
  14650.  flags are modified.
  14651.  
  14652.  Protected Mode Exceptions
  14653.  
  14654.  #GP(0) for an illegal memory operand effective address in the CS, DS, or ES
  14655.  segments; #SS(0) for an illegal address in the SS segment.
  14656.  
  14657.  Real Address Mode Exceptions
  14658.  
  14659.  Interrupt 13 for a word operand at offset 0FFFFH.
  14660.  
  14661.  
  14662.  VERR,VERW──Verify a Segment for Reading or Writing
  14663.  
  14664.  Opcode     Instruction  Clocks      Description
  14665.  
  14666.  0F 00 /4   VERR ew      14,mem=16   Set ZF=1 if seg. can be read,
  14667.                                      selector ew
  14668.  0F 00 /5   VERW ew      14,mem=16   Set ZF=1 if seg. can be written,
  14669.                                      selector ew
  14670.  
  14671.  Flags Modified
  14672.  
  14673.  Zero
  14674.  
  14675.  Flags Undefined
  14676.  
  14677.  None
  14678.  
  14679.  Operation
  14680.  
  14681.  VERR and VERW expect the 2-byte register or memory operand to contain the
  14682.  value of a selector. The instructions determine whether the segment denoted
  14683.  by the selector is reachable from the current privilege level; the
  14684.  instructions also determine whether it is readable or writable. If the
  14685.  segment is determined to be accessible, the zero flag is set to 1; if the
  14686.  segment is not accessible, it is set to 0. To set ZF, the following
  14687.  conditions must be met:
  14688.  
  14689.    1.  The selector must denote a descriptor within the bounds of the table
  14690.        (GDT or LDT); that is, the selector must be "defined."
  14691.  
  14692.    2.  The selector must denote the descriptor of a code or data segment.
  14693.  
  14694.    3.  If the instruction is VERR, the segment must be readable. If the
  14695.        instruction is VERW, the segment must be a writable data segment.
  14696.  
  14697.    4.  If the code segment is readable and conforming, the descriptor
  14698.        privilege level (DPL) can be any value for VERR. Otherwise, the DPL
  14699.        must be greater than or equal to (have less or the same privilege as)
  14700.        both the current privilege level and the selector's RPL.
  14701.  
  14702.  The validation performed is the same as if the segment were loaded into DS
  14703.  or ES and the indicated access (read or write) were performed. The zero flag
  14704.  receives the result of the validation. The selector's value cannot result in
  14705.  a protection exception. This enables the software to anticipate possible
  14706.  segment access problems.
  14707.  
  14708.  Protected Mode Exceptions
  14709.  
  14710.  The only faults that can occur are those generated by illegally addressing
  14711.  the memory operand which contains the selector. The selector is not loaded
  14712.  into any segment register, and no faults attributable to the selector
  14713.  operand are generated.
  14714.  
  14715.  #GP(0) for an illegal memory operand effective address in the CS, DS, or ES
  14716.  segments; #SS(0) for an illegal address in the SS segment.
  14717.  
  14718.  Real Address Mode Exceptions
  14719.  
  14720.  Interrupt 6; VERR and VERW are not recognized in Real Address Mode.
  14721.  
  14722.  
  14723.  WAIT──Wait Until BUSY Pin Is Inactive (HIGH)
  14724.  
  14725.  Opcode   Instruction    Clocks   Description
  14726.  
  14727.  9B        WAIT           3        Wait until BUSY pin is inactive (HIGH)
  14728.  
  14729.  Flags Modified
  14730.  
  14731.  None
  14732.  
  14733.  Flags Undefined
  14734.  
  14735.  None
  14736.  
  14737.  Operation
  14738.  
  14739.  WAIT suspends execution of 80286 instructions until the BUSY pin is inactive
  14740.  (high). The BUSY pin is driven by the 80287 numeric processor extension.
  14741.  WAIT is issued to ensure that the numeric instruction being executed is
  14742.  complete, and to check for a possible numeric fault (see below).
  14743.  
  14744.  Protected Mode Exceptions
  14745.  
  14746.  #NM if task switch flag in MSW is set. #MF if 80287 has detected an
  14747.  unmasked numeric error.
  14748.  
  14749.  Real Address Mode Exceptions
  14750.  
  14751.  Same as Protected mode.
  14752.  
  14753.  
  14754.  XCHG──Exchange Memory/Register with Register
  14755.  
  14756.  Opcode    Instruction    Clocks      Description
  14757.  
  14758.  86 /r     XCHG eb,rb     3,mem=5     Exchange byte register with EA byte
  14759.  86 /r     XCHG rb,eb     3,mem=5     Exchange EA byte with byte register
  14760.  87 /r     XCHG ew,rw     3,mem=5     Exchange word register with EA word
  14761.  87 /r     XCHG rw,ew     3,mem=5     Exchange EA word with word register
  14762.  90+ rw    XCHG AX,rw     3           Exchange word register with AX
  14763.  90+ rw    XCHG rw,AX     3           Exchange with word register
  14764.  
  14765.  Flags Modified
  14766.  
  14767.  None
  14768.  
  14769.  Flags Undefined
  14770.  
  14771.  None
  14772.  
  14773.  Operation
  14774.  
  14775.  The two operands are exchanged. The order of the operands is immaterial.
  14776.  BUS LOCK is asserted for the duration of the exchange, regardless of the
  14777.  presence or absence of the LOCK prefix or IOPL.
  14778.  
  14779.  Protected Mode Exceptions
  14780.  
  14781.  #GP(0) if either operand is in a non-writable segment. #GP(0) for an
  14782.  illegal memory operand effective address in the CS, DS, or ES segments;
  14783.  #SS(0) for an illegal address in the SS segment.
  14784.  
  14785.  Real Address Mode Exceptions
  14786.  
  14787.  Interrupt 13 for a word operand at offset 0FFFFH.
  14788.  
  14789.  
  14790.  XLAT──Table Look-up Translation
  14791.  
  14792.  Opcode    Instruction  Clocks   Description
  14793.  
  14794.  D7        XLAT mb      5        Set AL to memory byte DS:[BX + unsigned AL]
  14795.  D7        XLATB        5        Set AL to memory byte DS:[BX + unsigned AL]
  14796.  
  14797.  Flags Modified
  14798.  
  14799.  None
  14800.  
  14801.  Flags Undefined
  14802.  
  14803.  None
  14804.  
  14805.  Operation
  14806.  
  14807.  When XLAT is executed, AL should be the unsigned index into a table
  14808.  addressed by DS:BX. XLAT changes the AL register from the table index into
  14809.  the table entry. BX is unchanged.
  14810.  
  14811.  Protected Mode Exceptions
  14812.  
  14813.  #GP(0) for an illegal memory operand effective address in the CS, DS, or ES
  14814.  segments; #SS(0) for an illegal address in the SS segment.
  14815.  
  14816.  Real Address Mode Exceptions
  14817.  
  14818.  Interrupt 13 for a word operand at offset 0FFFFH.
  14819.  
  14820.  
  14821.  XOR──Logical Exclusive OR
  14822.  
  14823.  Opcode    Instruction Clocks    Description
  14824.  
  14825.  30 /r     XOR eb,rb   2,mem=7   Exclusive-OR byte register into EA byte
  14826.  31 /r     XOR ew,rw   2,mem=7   Exclusive-OR word register into EA word
  14827.  32 /r     XOR rb,eb   2,mem=7   Exclusive-OR EA byte into byte register
  14828.  33 /r     XOR rw,ew   2,mem=7   Exclusive-OR EA word into word register
  14829.  34 db     XOR AL,db   3         Exclusive-OR immediate byte into AL
  14830.  35 dw     XOR AX,dw   3         Exclusive-OR immediate word into AX
  14831.  80 /6 db  XOR eb,db   3,mem=7   Exclusive-OR immediate byte into EA byte
  14832.  81 /6 dw  XOR ew,dw   3,mem=7   Exclusive-OR immediate word into EA word
  14833.  
  14834.  Flags Modified
  14835.  
  14836.  Overflow=0, sign, zero, parity, carry=0
  14837.  
  14838.  Flags Undefined
  14839.  
  14840.  Auxiliary carry
  14841.  
  14842.  Operation
  14843.  
  14844.  XOR computes the exclusive OR of the two operands. Each bit of the result
  14845.  is 1 if the corresponding bits of the operands are different; each bit is 0
  14846.  if the corresponding bits are the same. The answer replaces the first
  14847.  operand.
  14848.  
  14849.  Protected Mode Exceptions
  14850.  
  14851.  #GP(0) if the result is in a non-writable segment. #GP(0) for an illegal
  14852.  memory operand effective address in the CS, DS, or ES segments; #SS(0) for
  14853.  an illegal address in the SS segment.
  14854.  
  14855.  Real Address Mode Exceptions
  14856.  
  14857.  Interrupt 13 for a word operand at offset 0FFFFH.
  14858.  
  14859.  
  14860.  Appendix C  8086/8088 Compatibility Considerations
  14861.  
  14862.  ───────────────────────────────────────────────────────────────────────────
  14863.  
  14864.  Software Compatibility Considerations
  14865.  
  14866.  In general, the real address mode 80286 will correctly execute ROM-based
  14867.  8086/8088 software. The following is a list of the minor differences between
  14868.  8086 and 80286 (Real mode).
  14869.  
  14870.    1.  Add Six Interrupt Vectors. The 80286 adds six interrupts which arise
  14871.        only if the 8086 program has a hidden bug. These interrupts occur only
  14872.        for instructions which were undefined on the 8086/8088 or if a segment
  14873.        wraparound is attempted. It is recommended that you add an interrupt
  14874.        handler to the 8086 software that is to be run on the 80286, which
  14875.        will treat these interrupts as invalid operations. This additional
  14876.        software does not significantly effect the existing 8086 software
  14877.        because the interrupts do not normally occur and should not already
  14878.        have been used since they are in the interrupt group reserved by
  14879.        Intel. Table C-1 describes the new 80286 interrupts.
  14880.  
  14881.    2.  Do not Rely on 8086/8088 Instruction Clock Counts. The 80286 takes
  14882.        fewer clocks for most instructions than the 8086/8088. The areas to
  14883.        look into are delays between I/O operations, and assumed delays in
  14884.        8086/8088 operating in parallel with an 8087.
  14885.  
  14886.    3.  Divide Exceptions Point at the DIV Instruction. Any interrupt on the
  14887.        80286 will always leave the saved CS:IP value pointing at the
  14888.        beginning of the instruction that failed (including prefixes). On the
  14889.        8086, the CS:IP value saved for a divide exception points at the next
  14890.        instruction.
  14891.  
  14892.    4.  Use Interrupt 16 for Numeric Exceptions. Any 80287 system must use
  14893.        interrupt vector 16 for the numeric error interrupt. If an 8086/8087
  14894.        or 8088/8087 system uses another vector for the 8087 interrupt, both
  14895.        vectors should point at the numeric error interrupt handler.
  14896.  
  14897.    5.  Numeric Exception Handlers Should allow Prefixes. The saved CS:IP
  14898.        value in the NPX environment save area will point at any leading
  14899.        prefixes before an ESC instruction. On 8086/8088 systems, this value
  14900.        points only at the ESC instruction.
  14901.  
  14902.    6.  Do Not Attempt Undefined 8086/8088 Operations. Instructions like
  14903.        POP CS or MOV CS,op will either cause exception 6 (undefined opcode)
  14904.        or perform a protection setup operation like LIDT on the 80286.
  14905.        Undefined bit encodings for bits 5-3 of the second byte of POP MEM or
  14906.        PUSH MEM will cause exception 13 on the 80286.
  14907.  
  14908.    7.  Place a Far JMP Instruction at FFFF0H. After reset, CS:IP = F000:FFF0
  14909.        on the 80286 (versus FFFF:0000 on the 8086/8088). This change was made
  14910.        to allow sufficient code space to enter protected mode without
  14911.        reloading CS. Placing a far JMP instruction at FFFF0H will avoid this
  14912.        difference. Note that the BOOTSTRAP option of LOC86 will automatically
  14913.        generate this jump instruction.
  14914.  
  14915.    8.  Do not Rely on the Value Written by PUSH SP. The 80286 will push a
  14916.        different value on the stack for PUSH SP than the 8086/8088. If the
  14917.        value pushed is important, replace PUSH SP instructions with the
  14918.        following three instructions:
  14919.           PUSH      BP
  14920.           MOV       BP,SP
  14921.           XCHG      BP,[BP]
  14922.        This code functions as the 8086/8088 PUSH SP instruction on the 80286.
  14923.  
  14924.    9.  Do not Shift or Rotate by More than 31 Bits. The 80286 masks all
  14925.        shift/rotate counts to the low 5 bits. This MOD 32 operation limits
  14926.        the count to a maximum of 31 bits. With this change, the longest
  14927.        shift/rotate instruction is 39 clocks. Without this change, the
  14928.        longest shift/rotate instruction would be 264 clocks, which delays
  14929.        interrupt response until the instruction completes execution.
  14930.  
  14931.    10. Do not Duplicate Prefixes. The 80286 sets an instruction length limit
  14932.        of 10 bytes. The only way to violate this limit is by duplicating a
  14933.        prefix two or more times before an instruction. Exception 6 occurs if
  14934.        the instruction length limit is violated. The 8086/8088 has no
  14935.        instruction length limit.
  14936.  
  14937.    11. Do not Rely on Odd 8086/8088 LOCK Characteristics. The LOCK prefix and
  14938.        its corresponding output signal should only be used to prevent other
  14939.        bus masters from interrupting a data movement operation. The 80286
  14940.        will always assert LOCK during an XCHG instruction with memory (even
  14941.        if the LOCK prefix was not used). LOCK should only be used with the
  14942.        XCHG, MOV, MOVS, INS, and OUTS instructions. The 80286 LOCK signal
  14943.        will not go active during an instruction prefetch.
  14944.  
  14945.    12. Do not Single Step External Interrupt Handlers. The priority of the
  14946.        80286 single step interrupt is different from that of the 8086/8088.
  14947.        This change was made to prevent an external interrupt from being
  14948.        single-stepped if it occurs while single stepping through a program.
  14949.        The 80286 single step interrupt has higher priority than any external
  14950.        interrupt. The 80286 will still single step through an interrupt
  14951.        handler invoked by INT instructions or an instruction exception.
  14952.  
  14953.    13. Do not Rely on IDIV Exceptions for Quotients of 80H or 8000H. The
  14954.        80286 can generate the largest negative number as a quotient for IDIV
  14955.        instructions. The 8086 will instead cause exception 0.
  14956.  
  14957.    14. Do not Rely on NMI Interrupting NMI Handlers. After an NMI is
  14958.        recognized, the NMI input and processor extension limit error
  14959.        interrupt is masked until the first IRET instruction is executed.
  14960.  
  14961.    15. The NPX error signal does not pass through an interrupt controller
  14962.        (an 8087 INT signal does). Any interrupt controller-oriented
  14963.        instructions for the 8087 may have to be deleted.
  14964.  
  14965.    16. If any real-mode program relies on address space wrap-around (e.g.,
  14966.        FFF0:0400=0000:0300), then external hardware should be used to force
  14967.        the upper 4 addresses to zero during real mode.
  14968.  
  14969.    17. Do not use I/O ports 00F8-00FFH. These are reserved for controlling
  14970.        80287 and future processor extensions.
  14971.  
  14972.  
  14973.  Table C-1. New 80286 Interrupts
  14974.  
  14975.  Interrupt   Function
  14976.  Number
  14977.  
  14978.    5         A BOUND instruction was executed with a register value outside
  14979.              the two limit values.
  14980.    6         An undefined opcode was encountered.
  14981.    7         The EM bit in the MSW has been set and an ESC instruction was
  14982.              executed. This interrupt will also occur on WAIT instructions
  14983.              if TS is set.
  14984.    8         The interrupt table limit was changed by the LIDT instruction
  14985.              to a value between 20H and 43H. The default limit after reset is
  14986.              3FFH, enough for all 256 interrupts.
  14987.    9         A processor extension data transfer exceeded offset 0FFFFH in a
  14988.              segment.  This interrupt handler must execute FNINIT before
  14989.              any ESC or WAIT instruction is executed.
  14990.    13        Segment wraparound was attempted by a word operation at offset
  14991.              0FFFFH.
  14992.    16        When 80286 attempted to execute a coprocessor instruction
  14993.              ERROR pin indicated an unmasked exception from previous
  14994.              coprocessor instruction.
  14995.  
  14996.  
  14997.  Hardware Compatibility Considerations
  14998.  
  14999.    1.  Address after Reset
  15000.  
  15001.        8086 has CS:IP = FFFF:0000 and physical address FFFF0.
  15002.        80286 has CS:IP = F000:FFF0 and physical address FFFFF0.
  15003.  
  15004.  ───────────────────────────────────────────────────────────────────────────
  15005.  NOTE
  15006.    After 80286 reset, until the first 80286 far JMP or far CALL, the
  15007.    code segment base is FF0000. This means A20-A23 will be high for
  15008.    CS-relative bus cycles (code fetch or use of CS override prefix)
  15009.    after reset until the first far JMP or far CALL instruction is
  15010.    performed.
  15011.  ───────────────────────────────────────────────────────────────────────────
  15012.  
  15013.    2.  Physical Address Formation
  15014.  
  15015.        In real mode or protected mode, the 80286 always forms a physical
  15016.        address by adding a 16-bit offset with a 24-bit segment base value
  15017.        (8086 has 20-bit base value). Therefore, if the 80286 in real mode
  15018.        has a segment base within 64K of the top of the 1 Mbyte address space,
  15019.        and the program adds an offset of ffffh to the segment base, the
  15020.        physical address will be slightly above 1Mbyte. Thus, to fully
  15021.        duplicate 1Mbyte wraparound that the 8086 has, it is always necessary
  15022.        to force A20 low externally when the 80286 is in real mode, but system
  15023.        hardware uses all 24 address lines.
  15024.  
  15025.    3.  LOCK signal
  15026.  
  15027.        On the 8086, LOCK asserted means this bus cycle is within a group of
  15028.        two or more locked bus cycles. On the 80286, the LOCK signal means
  15029.        lock this bus cycle to the NEXT bus cycle. Therefore, on the 80286,
  15030.        the LOCK signal is not asserted on the last locked bus cycle of the
  15031.        group of locked bus cycles.
  15032.  
  15033.    4.  Coprocessor Interface
  15034.  
  15035.        8086, synchronous to 8086, can become a bus master.
  15036.        80287, asynchronous to 80286 and 80287, cannot become a bus master.
  15037.        8087 pulls opcode and pointer information directly from data bus.
  15038.        80286 passes opcode and pointer information to 80287.
  15039.        8087 uses interrupt path to signal errors to 8086.
  15040.        80287 uses dedicated ERROR signal.
  15041.        8086 requires explicit WAIT opcode preceding all ESC instructions to
  15042.        synchronize with 8087. 80286 has automatic instruction synchronization
  15043.        with 80287.
  15044.  
  15045.    5.  Bus Cycles
  15046.  
  15047.        8086 has four-clock minimum bus cycle, with a time-multiplexed
  15048.        address/data bus. 80286 has two-clock minimum bus cycle, with separate
  15049.        buses for address and data.
  15050.  
  15051.  
  15052.  Appendix D  80286/80386 Software Compatibility Considerations
  15053.  
  15054.  ───────────────────────────────────────────────────────────────────────────
  15055.  
  15056.  This appendix describes the considerations required in designing an
  15057.  Operating System for the protected mode 80286 so that it will operate
  15058.  on an 80386. An 80286 Operating System running on the 80386 would not use
  15059.  any of the advanced features of the 80386 (i.e., paging or segments larger
  15060.  than 64K), but would run 80286 code faster. Use of the new 80386 features
  15061.  requires changes in the 80286Operating System.
  15062.  
  15063.  The 80386 is no different than any other software compatible processor in
  15064.  terms of requiring the same system environment to run the same software; the
  15065.  80386 must have the same amount of physical memory and I/O devices in the
  15066.  system as the 80286 system to run the same software. Note that an 80386
  15067.  system requires a different memory system to achieve the higher
  15068.  performance.
  15069.  
  15070.  The 80286 design considerations can be generally characterized as avoiding
  15071.  use of functions or memory that the 80386 will use. The exception to this
  15072.  rule is initialization code executed after power up. Such code must be
  15073.  changed to configure the 80386 system to match that of the 80286 system.
  15074.  
  15075.  The following are 80286/80386 software compatibility design considerations:
  15076.  
  15077.    1.  Isolate the protected mode initialization code.
  15078.  
  15079.        System initialization code will be required on the 80386 to program
  15080.        operating parameters before executing any significant amount of 80286
  15081.        software. The 80286 initialization software should be isolated from
  15082.        the rest of the Operating System.
  15083.  
  15084.        The initialization code in Appendix A is an example of isolated
  15085.        initialization code. Such code can be extended to include programming
  15086.        of operating parameters before executing the initial protected
  15087.        mode task.
  15088.  
  15089.    2.  Avoid wraparound of 80286 24-bit physical address space.
  15090.  
  15091.        Since the 80386 has a larger physical address space, any segment
  15092.        whose base address is greater than FF0000 and whose limit is beyond
  15093.        FFFFFF will address the seventeenth megabyte of memory in the 80386
  15094.        32-bit physical address space instead of the first megabyte on an
  15095.        80286.
  15096.  
  15097.        No expand-down segments shouldhave a base address in the range
  15098.        FF00001-FFFFFF. No expand-up segments should wrap around the 80286
  15099.        address space (the sum of their base and limit is in the range
  15100.        000000-00FFFE).
  15101.  
  15102.    3.  Zero the last word of every 80286 descriptor.
  15103.  
  15104.        The 80386 uses the last word of each descriptor to expand the base
  15105.        address and limit fields of segments. Placing zeros in the descriptor
  15106.        will cause the 80386 to treat the segments the same way as an 80286
  15107.        (except for address space wraparound as mentioned above).
  15108.  
  15109.    4.  Use only 80H or 00H for invalid descriptors.
  15110.  
  15111.        The 80386 uses more descriptor types than the 80286. Numeric values
  15112.        of 8-15 in bits 3-0 of the access byte for control descriptors will
  15113.        cause a protection exception on the 80286, but may be defined for
  15114.        other segment types on the 80386. Access byte values of 80H and 00H
  15115.        will remain undefined descriptors on both the 80286 and the 80386.
  15116.  
  15117.    5.  Put error interrupt handlers in reserved interrupts 14, 15, 17-31.
  15118.  
  15119.        Some of the unused, Intel-reserved interrupts of the 80286
  15120.        will be used by the 80386 (i.e., page fault or bus error). These
  15121.        interrupts should not occur while executing an 80286 operating system
  15122.        on an 80386. However, it is safest to place an interrupt handler in
  15123.        these interrupts to print an error message and stop the system if
  15124.        they do occur.
  15125.  
  15126.    6.  Do not change bits 15-4 of MSW.
  15127.  
  15128.        The 80386 uses some of the undefined bits in the machine status word.
  15129.        80286 software should ignore bits 15-4 of the MSW. To change the MSW
  15130.        on an 80286, read the old value first with LMSW, change bits 3-0 only,
  15131.        then write the new value with SMSW.
  15132.  
  15133.    7.  Use a restricted LOCK protocol for multiprocessor systems.
  15134.  
  15135.        The 80386 supports the 8086/80286 LOCK functions for simple
  15136.        instructions, but not the string move instructions. Any need for
  15137.        locked string moves can be satisfied by gaining control of a status
  15138.        semaphore before using the string move instruction. Any attempt to
  15139.        execute a locked string move will cause a protection exception on the
  15140.        80386.
  15141.  
  15142.        The general 80286 LOCK protocol does not efficiently extend to large
  15143.        multiprocessor systems. If all the processors in the system frequently
  15144.        use the 8086/80286 LOCK, they will prevent other processors from
  15145.        accessing memory and thereby impact system performance.
  15146.  
  15147.  Access to semaphores in the future, including current 80286 Operating
  15148.  Systems, should use a protocol with the following restrictions:
  15149.  
  15150.    ■  Be sure the semaphore starts at a physical memory address that is a
  15151.       multiple of 4.
  15152.  
  15153.    ■  Do not use string moves to access the variable.
  15154.  
  15155.    ■  All accesses by any instruction or I/O device (even simple reads or
  15156.       writes) must use the LOCK prefix or system LOCK signal.
  15157.  
  15158.  
  15159.  Index
  15160.  ───────────────────────────────────────────────────────────────────────────
  15161.  
  15162.  A
  15163.  ───────────────────────────────────────────────────────────────────────────
  15164.  AAA
  15165.  AAD
  15166.  AAM
  15167.  AAS
  15168.  ADC
  15169.  ADD
  15170.  Addressing Modes
  15171.   Based Indexed Mode
  15172.   Based Indexed Mode with Displacement
  15173.   Based Mode (on BX or BP Registers)
  15174.   Direct Address Mode
  15175.   Displacement
  15176.   Immediate Operand
  15177.   Indexed Mode (by DI or SI)
  15178.   Opcode
  15179.   Register Indirect Mode
  15180.   Summary
  15181.  AF Flag, (see Flags)
  15182.  AH Register
  15183.  AL Register
  15184.  AND Instruction
  15185.  Arithmetic Instructions
  15186.  ASCII (see Data Types)
  15187.  AX Register
  15188.  
  15189.  
  15190.  B
  15191.  ───────────────────────────────────────────────────────────────────────────
  15192.  Based Index Mode (see Addressing Modes)
  15193.  Based Index Mode with Displacement (see Addressing Modes)
  15194.  Based Mode (see Addressing Modes)
  15195.  BCD Arithmetic (see Data Management Instructions)
  15196.  BH Register
  15197.  BL Register
  15198.  BOUND Instruction (see Extended Instruction Set)
  15199.  Bound Range Exceeded (Interrupt 5), (see Interrupt Handling)
  15200.  BP Register
  15201.  Breakpoint Interrupt 3, (see Interrupt Handling)
  15202.  BUSY
  15203.  BX Register
  15204.      (cont.)
  15205.  Byte (See Data Types)
  15206.  
  15207.  
  15208.  C
  15209.  ───────────────────────────────────────────────────────────────────────────
  15210.  CALL Instructions
  15211.  Call Gates
  15212.  CBW Instructions
  15213.  CF (Carry Flag) (see Flags)
  15214.  CH Register
  15215.  CL Register
  15216.  CLC Instruction
  15217.  CLD Instruction
  15218.  CLI Instruction
  15219.  CLTS Instruction
  15220.  CMP Instruction
  15221.  Code Segment Access
  15222.  Comparison Instructions
  15223.  Conforming Code Segments
  15224.  Constant Instructions
  15225.  Control Transfers
  15226.  CPL (Current Privilege Level)
  15227.  CS Register
  15228.  CWD Instruction
  15229.  CX Register
  15230.  
  15231.  
  15232.  D
  15233.  ───────────────────────────────────────────────────────────────────────────
  15234.  DAA
  15235.  DAS
  15236.  Data Management Instructions
  15237.   Address Manipulation
  15238.   Arithmetic Instructions
  15239.    Addition Instructions
  15240.    Division Instructions
  15241.    Multiplication Instructions
  15242.    Subtraction Instructions
  15243.   BCD Arithmetic
  15244.   Character Transfer and String Instructions
  15245.    Repeat Prefixes
  15246.    String Move
  15247.    String Translate
  15248.   Control Transfer Instructions
  15249.    Conditional Transfer
  15250.    Software Generated Interrupts
  15251.     Interrupt Instructions
  15252.    Unconditional Transfer
  15253.   Flag Control
  15254.   Logical Instructions
  15255.    Shift and Rotate Instructions
  15256.    Type Conversion Instructions
  15257.   Processor Extension Intructions
  15258.   Test and Compare Instructions
  15259.   Trusted Instructions
  15260.    Input/Output Instructions
  15261.   Stack Manipulation
  15262.  Data Transfer Instructions
  15263.  Data Types
  15264.   ASCII
  15265.   BCD
  15266.   Byte
  15267.   Floating Point
  15268.   Integer
  15269.   Packed BCD
  15270.   Pointer
  15271.   Strings
  15272.   Word
  15273.  DEC Instruction
  15274.  Dedicated Interrupt Vector
  15275.  Descriptor Table
  15276.  Descriptor Table Register
  15277.  DF Flag, (see Flags)
  15278.  DH Register
  15279.  DI Instruction
  15280.  Direct Address Mode (see Addressing Modes)
  15281.  Divide Error (Interrupt 0) (see Interrupt Handling)
  15282.  DIV Instruction
  15283.  DL Register
  15284.  DPL (Descriptor Privilege Level)
  15285.  DS Register
  15286.  DX Register
  15287.  
  15288.  
  15289.  E
  15290.  ───────────────────────────────────────────────────────────────────────────
  15291.  EM (Bit in MSW)
  15292.  ENTER Instruction
  15293.  ES Register
  15294.  ESC (Instructions for Coprocessor)
  15295.  Extended Instruction Set (Chapter 4)
  15296.   ENTER Build Stackframe
  15297.   LEAVE Remove Stackframe
  15298.   Repeated IN and OUT String Instructions
  15299.  
  15300.  
  15301.  F
  15302.  ───────────────────────────────────────────────────────────────────────────
  15303.  Flag Register
  15304.  Flags see also Use of Flags with Basic
  15305.     Instructions
  15306.   AF (Auxilliary Carry Flag)
  15307.   CF (Carry Flag)
  15308.           (cont.)
  15309.   DF (Direction Flag)
  15310.   IF (Interrupt Flag)
  15311.   IOPL (Privilege Level)
  15312.   NT (Nested Task Flag)
  15313.   OF (Overflow Flag)
  15314.   PF (Parity Flag)
  15315.   SF (Sign Flag)
  15316.   TF (Trap Flag)
  15317.   TS (Task Switch)
  15318.   ZF (Zero Flag)
  15319.  Floating Point (see Data Types)
  15320.  
  15321.  
  15322.  G
  15323.  ───────────────────────────────────────────────────────────────────────────
  15324.  Gates
  15325.  GDT
  15326.  GDTR (Global Descriptor Register)
  15327.  General Protection Fault (Interrupt 3), (see Interrupt Handling)
  15328.  General Registers
  15329.  
  15330.  
  15331.  H
  15332.  ───────────────────────────────────────────────────────────────────────────
  15333.  HLT Instruction
  15334.  Hierarchy of 86, 186, 286 Instruction Sets
  15335.   Basic Instruction Set, Chapter 3
  15336.   Extended Instruction Set
  15337.   Instruction Set Overview
  15338.   System Control Register Set, Chapter 4, Chapter 5, Chapter 6, Chapter 7
  15339.                        (cont.) Chapter 8, Chapter 9, Chapter 10
  15340.  
  15341.  
  15342.  I
  15343.  ───────────────────────────────────────────────────────────────────────────
  15344.  I/O
  15345.  IDIV Instruction
  15346.  IDT (Interrupt Descriptor Table)
  15347.  IDTR (Interrupt Descriptor Table Register)
  15348.  IF (Interrupt Flag), (see Flags)
  15349.  IMUL Instruction
  15350.  IN Instruction
  15351.  INC Instruction
  15352.  INDEX Field
  15353.  Indexed Mode
  15354.  Index, Pointer and Base Register
  15355.  Input/Output
  15356.   Instructions
  15357.   Memory Mapped I/O
  15358.   Restrictions in Protected Mode
  15359.   Separate I/O Space
  15360.  INS/INSB/INSW Instruction
  15361.  INT Instruction, (see Interrupt Handling)
  15362.  Integer, (see Data Types)
  15363.  Interrupt Handling
  15364.             (cont.)
  15365.  Interrupt Priorities
  15366.   Interrupt 0 Divide Error
  15367.   Interrupt 1 Single-Step
  15368.   Interrupt 2 Nonmaskable
  15369.   Interrupt 3 Breakpoint
  15370.   Interrupt 4 INTO Detected Overflow
  15371.   Interrupt 5 BOUND Range Exceeded
  15372.   Interrupt 6 Invalid Opcode
  15373.   Interrupt 7 Processor Extension Not Available
  15374.   Interrupt 8, Interrupt Table Limit Too Small
  15375.   Interrupt Vectors
  15376.   Reserved Vectors
  15377.  Interrupt Vector Table
  15378.  Interrupts and Exceptions,(see Interrupt Handling and Interrupt Priorities)
  15379.  INTO Detected Overflow (Interrupt 4), (see Interrupt Handling and Interrupt
  15380.   Priorities)
  15381.  INTO Instruction
  15382.  INTR
  15383.  Invalid opcode (Interrupt 6), (see Interrupt Handling and Interrupt
  15384.   Priorities)
  15385.  IOPL (I/O Privilege Level), (see Flags)
  15386.  IP Register
  15387.  IRET Instruction
  15388.  
  15389.  
  15390.  J
  15391.  ───────────────────────────────────────────────────────────────────────────
  15392.  JCXZ Instruction
  15393.  JMP Instruction
  15394.  
  15395.  
  15396.  L
  15397.  ───────────────────────────────────────────────────────────────────────────
  15398.  LAHF Instruction
  15399.  LAR Instruction
  15400.  LDS Instruction
  15401.  LDT (Local Descriptor Table)
  15402.                       (cont.)
  15403.  LEA Instruction
  15404.  LEAVE Instruction
  15405.  LES Instruction
  15406.  LGDT Instruction
  15407.  LIDT Instruction
  15408.  LLDT Instruction
  15409.  LMSW Instruction
  15410.  LOCK Prefix
  15411.  LODS/LODSB/LODSW
  15412.  LOOP Instruction
  15413.  LOOPE Instruction
  15414.  LOOPNE
  15415.  LOOPNZ
  15416.  LSL Instruction
  15417.  
  15418.  
  15419.  M
  15420.  ───────────────────────────────────────────────────────────────────────────
  15421.  Memory,
  15422.   Physical Size
  15423.   Segmentation
  15424.    Implied Usage
  15425.    Interpretation in Protected Mode
  15426.    Interpretation in Real Mode
  15427.    Modularity
  15428.   Virtual Size
  15429.  Memory Addressing Modes
  15430.  Memory Management
  15431.   Task Managment, Chapter 8
  15432.    Context Switching (Task Switching)
  15433.    Overview
  15434.  Memory Management Registers, Chapter 6
  15435.  Memory Mapped I/O, (see Input/Output)
  15436.  Memory Mode
  15437.  Memory Segmentation and Segment Registers
  15438.  MOV Instructions
  15439.  MOVS Instructions
  15440.  MOVSB Instructions
  15441.  MOVSW Instruction
  15442.  MSW Register
  15443.  MUL Instruction
  15444.  
  15445.  
  15446.  N
  15447.  ───────────────────────────────────────────────────────────────────────────
  15448.  NEG Instruction
  15449.  NMI (Non maskable Interrupt)
  15450.  Nonmaskable (Interrupt 2), (see Interrupt Priorities)
  15451.  NOP Instruction
  15452.  NOT Instruction
  15453.  Not Present (Interrupt 11) (see Interrupt Priorities)
  15454.  NPX Processor Extension
  15455.  NT (Nested Task Flag), (see Flags)
  15456.  Numeric Data Processor Instructions
  15457.  
  15458.  
  15459.  O
  15460.  ───────────────────────────────────────────────────────────────────────────
  15461.  OF (Overflow Flag), (see Flags)
  15462.  Offset Computation
  15463.  Operands
  15464.  OR Instruction
  15465.  OUT/OUTW
  15466.  OUTS/OUTSB/OUTSW Instruction
  15467.  
  15468.  
  15469.  P
  15470.  ───────────────────────────────────────────────────────────────────────────
  15471.  PF (Parity Flag), (see Flags)
  15472.  Pointer, (see Data Types)
  15473.  POP Instruction
  15474.  POPA Instruction
  15475.  POPF Instruction
  15476.  Processor Extension Error (Interrupt 6), (see Interrupt Handling and
  15477.   Interrupt Priorities)
  15478.  Processor Extension Not Available, (Interrupt 7), (see Interrupt and
  15479.   Interrupt Priorities)
  15480.  Processor Extension Segment Overrun Interrupt (Interrupt 9), (see Interrupt
  15481.   and Interrupt Priorities)
  15482.  Protected Mode
  15483.   Protected Virtual Address Mode
  15484.  Protection Implementation
  15485.  Protection Mechanisms
  15486.  PUSH
  15487.  PUSHA
  15488.  PUSHF
  15489.  
  15490.  
  15491.  R
  15492.  ───────────────────────────────────────────────────────────────────────────
  15493.  Real Address Mode
  15494.  Register,
  15495.   Base Architecture Diagram
  15496.   Base Register BX
  15497.   Flags Register
  15498.   General Registers
  15499.   Index Registers DI, SI
  15500.   Overview
  15501.   Pointer Registers BP and SP
  15502.   Segment Registers
  15503.   Status and Control
  15504.  Register Direct Mode
  15505.  Register and Immediate Modes
  15506.  Register Indirect Mode (see Addressing Modes)
  15507.  Reserved Interrupt Vectors, (see Interrupt Handling and Interrupt
  15508.   Priorities)
  15509.  RESET
  15510.  RCL Instruction
  15511.  RCR Instruction
  15512.  REP Prefix
  15513.  REPE Prefix
  15514.  REPNE Prefix
  15515.  REPNZ Prefix
  15516.  REPZ Prefix
  15517.  RET Instructon
  15518.  ROL Instruction
  15519.  ROR Instruction
  15520.  RPL
  15521.  
  15522.  
  15523.  S
  15524.  ───────────────────────────────────────────────────────────────────────────
  15525.  SAL Instruction
  15526.  SAR Instruction
  15527.  SBB Instruction
  15528.  SCAS Instruction
  15529.  SEG (Segment Override Prefix)
  15530.  Segment Address Translation Registers
  15531.  Segment Descriptor
  15532.  Segment Overrun Exception (Interrupt 13), (see Interrupt Handling and
  15533.   Interrupt Priorities)
  15534.  Segment Selection
  15535.  SF (Sign Flag), (see Flags)
  15536.  SGDT Instruction
  15537.  SHL Instruction
  15538.  SHR Instruction
  15539.  SI Register
  15540.  SIDT Instruction
  15541.  Single Step (Interrupt 1), (see Interrupt Priorities)
  15542.  SMSW Instruction
  15543.  SP Register
  15544.  SS Register
  15545.      (cont.)
  15546.  Status and Control Registers
  15547.  Stack Flag, (see Flags)
  15548.  Stack Fault (Interrupt 12), (see Interrupt Priorities)
  15549.  Stack Manipulation Instructions
  15550.  Stack Operations
  15551.   Grow Down
  15552.   Overview
  15553.   Segment Register Usage
  15554.   Segment Usage Override
  15555.   Stack Frame Base Pointer BP
  15556.   Top of Stack
  15557.   TOS
  15558.   with BP and SP Registers
  15559.  Status Flags
  15560.  STC Instructions
  15561.  STD Instructions
  15562.  STI Instructions
  15563.  String Instructions
  15564.  SUB Instruction
  15565.  System Address Registers
  15566.  System Initialization
  15567.  System Control Instructions
  15568.  
  15569.  
  15570.  T
  15571.  ───────────────────────────────────────────────────────────────────────────
  15572.  TEST Instruction
  15573.  TF (Trap Flags), (see Flags)
  15574.  TOS (Top of Stack), (see Stack Operation)
  15575.  TR (Task Register)
  15576.  Transcendental Instruction
  15577.  TSS (Task State Segment)
  15578.  
  15579.  
  15580.  U
  15581.  ───────────────────────────────────────────────────────────────────────────
  15582.  Use of Flags with Basic Instructions
  15583.  
  15584.  
  15585.  V
  15586.  ───────────────────────────────────────────────────────────────────────────
  15587.  Virtual Address
  15588.  
  15589.  
  15590.  W
  15591.  ───────────────────────────────────────────────────────────────────────────
  15592.  WAIT Instruction
  15593.  
  15594.  
  15595.  X
  15596.  ───────────────────────────────────────────────────────────────────────────
  15597.  XCHG Instruction
  15598.  XLAT Instruction
  15599.  XOR Instruction
  15600.