home *** CD-ROM | disk | FTP | other *** search
/ CD-X 1 / cdx_01.iso / demodisc / tyrant / docs / 386code / 2.386 < prev    next >
Encoding:
Text File  |  1994-01-20  |  55.4 KB  |  1,194 lines

  1. Chapter 2  Basic Programming Model
  2.  
  3. ────────────────────────────────────────────────────────────────────────────
  4.  
  5. This chapter describes the 80386 application programming environment as
  6. seen by assembly language programmers when the processor is executing in
  7. protected mode. The chapter introduces programmers to those features of the
  8. 80386 architecture that directly affect the design and implementation of
  9. 80386 applications programs. Other chapters discuss 80386 features that
  10. relate to systems programming or to compatibility with other processors of
  11. the 8086 family.
  12.  
  13. The basic programming model consists of these aspects:
  14.  
  15.   ■  Memory organization and segmentation
  16.   ■  Data types
  17.   ■  Registers
  18.   ■  Instruction format
  19.   ■  Operand selection
  20.   ■  Interrupts and exceptions
  21.  
  22. Note that input/output is not included as part of the basic programming
  23. model. Systems designers may choose to make I/O instructions available to
  24. applications or may choose to reserve these functions for the operating
  25. system. For this reason, the I/O features of the 80386 are discussed in Part
  26. II.
  27.  
  28. This chapter contains a section for each aspect of the architecture that is
  29. normally visible to applications.
  30.  
  31.  
  32. 2.1  Memory Organization and Segmentation
  33.  
  34. The physical memory of an 80386 system is organized as a sequence of 8-bit
  35. bytes. Each byte is assigned a unique address that ranges from zero to a
  36. maximum of 2^(32) -1 (4 gigabytes).
  37.  
  38. 80386 programs, however, are independent of the physical address space.
  39. This means that programs can be written without knowledge of how much
  40. physical memory is available and without knowledge of exactly where in
  41. physical memory the instructions and data are located.
  42.  
  43. The model of memory organization seen by applications programmers is
  44. determined by systems-software designers. The architecture of the 80386
  45. gives designers the freedom to choose a model for each task. The model of
  46. memory organization can range between the following extremes:
  47.  
  48.   ■  A "flat" address space consisting of a single array of up to 4
  49.      gigabytes.
  50.  
  51.   ■  A segmented address space consisting of a collection of up to 16,383
  52.      linear address spaces of up to 4 gigabytes each.
  53.  
  54. Both models can provide memory protection. Different tasks may employ
  55. different models of memory organization. The criteria that designers use to
  56. determine a memory organization model and the means that systems programmers
  57. use to implement that model are covered in Part II──Systems Programming.
  58.  
  59.  
  60. 2.1.1  The "Flat" Model
  61.  
  62. In a "flat" model of memory organization, the applications programmer sees
  63. a single array of up to 2^(32) bytes (4 gigabytes). While the physical
  64. memory can contain up to 4 gigabytes, it is usually much smaller; the
  65. processor maps the 4 gigabyte flat space onto the physical address space by
  66. the address translation mechanisms described in Chapter 5. Applications
  67. programmers do not need to know the details of the mapping.
  68.  
  69. A pointer into this flat address space is a 32-bit ordinal number that may
  70. range from 0 to 2^(32) -1. Relocation of separately-compiled modules in this
  71. space must be performed by systems software (e.g., linkers, locators,
  72. binders, loaders).
  73.  
  74.  
  75. 2.1.2  The Segmented Model
  76.  
  77. In a segmented model of memory organization, the address space as viewed by
  78. an applications program (called the logical address space) is a much larger
  79. space of up to 2^(46) bytes (64 terabytes). The processor maps the 64
  80. terabyte logical address space onto the physical address space (up to 4
  81. gigabytes) by the address translation mechanisms described in Chapter 5.
  82. Applications programmers do not need to know the details of this mapping.
  83.  
  84. Applications programmers view the logical address space of the 80386 as a
  85. collection of up to 16,383 one-dimensional subspaces, each with a specified
  86. length. Each of these linear subspaces is called a segment. A segment is a
  87. unit of contiguous address space. Segment sizes may range from one byte up
  88. to a maximum of 2^(32) bytes (4 gigabytes).
  89.  
  90. A complete pointer in this address space consists of two parts (see Figure
  91. 2-1):
  92.  
  93.   1.  A segment selector, which is a 16-bit field that identifies a
  94.       segment.
  95.  
  96.   2.  An offset, which is a 32-bit ordinal that addresses to the byte level
  97.       within a segment.
  98.  
  99. During execution of a program, the processor associates with a segment
  100. selector the physical address of the beginning of the segment. Separately
  101. compiled modules can be relocated at run time by changing the base address
  102. of their segments. The size of a segment is variable; therefore, a segment
  103. can be exactly the size of the module it contains.
  104.  
  105.  
  106. 2.2  Data Types
  107.  
  108. Bytes, words, and doublewords are the fundamental data types (refer to
  109. Figure 2-2). A byte is eight contiguous bits starting at any logical
  110. address. The bits are numbered 0 through 7; bit zero is the least
  111. significant bit.
  112.  
  113. A word is two contiguous bytes starting at any byte address. A word thus
  114. contains 16 bits. The bits of a word are numbered from 0 through 15; bit 0
  115. is the least significant bit. The byte containing bit 0 of the word is
  116. called the low byte; the byte containing bit 15 is called the high byte.
  117.  
  118. Each byte within a word has its own address, and the smaller of the
  119. addresses is the address of the word. The byte at this lower address
  120. contains the eight least significant bits of the word, while the byte at the
  121. higher address contains the eight most significant bits.
  122.  
  123. A doubleword is two contiguous words starting at any byte address. A
  124. doubleword thus contains 32 bits. The bits of a doubleword are numbered from
  125. 0 through 31; bit 0 is the least significant bit. The word containing bit 0
  126. of the doubleword is called the low word; the word containing bit 31 is
  127. called the high word.
  128.  
  129. Each byte within a doubleword has its own address, and the smallest of the
  130. addresses is the address of the doubleword. The byte at this lowest address
  131. contains the eight least significant bits of the doubleword, while the byte
  132. at the highest address contains the eight most significant bits. Figure 2-3
  133. illustrates the arrangement of bytes within words anddoublewords.
  134.  
  135. Note that words need not be aligned at even-numbered addresses and
  136. doublewords need not be aligned at addresses evenly divisible by four. This
  137. allows maximum flexibility in data structures (e.g., records containing
  138. mixed byte, word, and doubleword items) and efficiency in memory
  139. utilization. When used in a configuration with a 32-bit bus, actual
  140. transfers of data between processor and memory take place in units of
  141. doublewords beginning at addresses evenly divisible by four; however, the
  142. processor converts requests for misaligned words or doublewords into the
  143. appropriate sequences of requests acceptable to the memory interface. Such
  144. misaligned data transfers reduce performance by requiring extra memory
  145. cycles. For maximum performance, data structures (including stacks) should
  146. be designed in such a way that, whenever possible, word operands are aligned
  147. at even addresses and doubleword operands are aligned at addresses evenly
  148. divisible by four. Due to instruction prefetching and queuing within the
  149. CPU, there is no requirement for instructions to be aligned on word or
  150. doubleword boundaries. (However, a slight increase in speed results if the
  151. target addresses of control transfers are evenly divisible by four.)
  152.  
  153. Although bytes, words, and doublewords are the fundamental types of
  154. operands, the processor also supports additional interpretations of these
  155. operands. Depending on the instruction referring to the operand, the
  156. following additional data types are recognized:
  157.  
  158. Integer:
  159. A signed binary numeric value contained in a 32-bit doubleword,16-bit word,
  160. or 8-bit byte. All operations assume a 2's complement representation. The
  161. sign bit is located in bit 7 in a byte, bit 15 in a word, and bit 31 in a
  162. doubleword. The sign bit has the value zero for positive integers and one
  163. for negative. Since the high-order bit is used for a sign, the range of an
  164. 8-bit integer is -128 through +127; 16-bit integers may range from -32,768
  165. through +32,767; 32-bit integers may range from -2^(31) through +2^(31) -1.
  166. The value zero has a positive sign.
  167.  
  168. Ordinal:
  169. An unsigned binary numeric value contained in a 32-bit doubleword,
  170. 16-bit word, or 8-bit byte. All bits are considered in determining
  171. magnitude of the number. The value range of an 8-bit ordinal number
  172. is 0-255; 16 bits can represent values from 0 through 65,535; 32 bits
  173. can represent values from 0 through 2^(32) -1.
  174.  
  175. Near Pointer:
  176. A 32-bit logical address. A near pointer is an offset within a segment.
  177. Near pointers are used in either a flat or a segmented model of memory
  178. organization.
  179.  
  180. Far Pointer:
  181. A 48-bit logical address of two components: a 16-bit segment selector
  182. component and a 32-bit offset component. Far pointers are used by
  183. applications programmers only when systems designers choose a
  184. segmented memory organization.
  185.  
  186. String:
  187. A contiguous sequence of bytes, words, or doublewords. A string may
  188. contain from zero bytes to 2^(32) -1 bytes (4 gigabytes).
  189.  
  190. Bit field:
  191. A contiguous sequence of bits. A bit field may begin at any bit position
  192. of any byte and may contain up to 32 bits.
  193.  
  194. Bit string:
  195. A contiguous sequence of bits. A bit string may begin at any bit position
  196. of any byte and may contain up to 2^(32) -1 bits.
  197.  
  198. BCD:
  199. A byte (unpacked) representation of a decimal digit in the range0 through
  200. 9. Unpacked decimal numbers are stored as unsigned byte quantities. One
  201. digit is stored in each byte. The magnitude of the number is determined from
  202. the low-order half-byte; hexadecimal values 0-9 are valid and are
  203. interpreted as decimal numbers. The high-order half-byte must be zero for
  204. multiplication and division; it may contain any value for addition and
  205. subtraction.
  206.  
  207. Packed BCD:
  208. A byte (packed) representation of two decimal digits, each in the range
  209. 0 through 9. One digit is stored in each half-byte. The digit in the
  210. high-order half-byte is the most significant. Values 0-9 are valid in each
  211. half-byte. The range of a packed decimal byte is 0-99.
  212.  
  213. Figure 2-4 graphically summarizes the data types supported by the 80386.
  214.  
  215.  
  216. Figure 2-1.  Two-Component Pointer
  217.  
  218.                                                   
  219.                                    ║               ║
  220.                                    ╠═══════════════╣─┐
  221.        32            0             ║               ║ │
  222.       ╔═══════╪═══════╗   ╔═══╗    ╠═══════════════╣ │
  223.       ║    OFFSET     ╟───╢ + ╟───║    OPERAND    ║ │
  224.       ╚═══════╪═══════╝   ╚═══╝    ╠═══════════════╣ ├─ SELECTED SEGMENT
  225.                                   ║               ║ │
  226.            16    0          │      ║               ║ │
  227.           ╔═══════╗         │      ║               ║ │
  228.           ║SEGMENT╟─────────∙─────╠═══════════════╣─┘
  229.           ╚═══════╝                ║               ║
  230.                                    ║               ║
  231.                                    ║               ║
  232.                                                   
  233.  
  234.  
  235. Figure 2-2.  Fundamental Data Types
  236.  
  237.   7              0
  238.  ╔═══════════════╗
  239.  ║     BYTE      ║  BYTE
  240.  ╚═══════════════╝
  241.  
  242.   15              7              0
  243.  ╔═══════════════╤═══════════════╗
  244.  ║   HIGH BYTE   │   LOW BYTE    ║  WORD
  245.  ╚═══════════════╧═══════════════╝
  246.     address n+1      address n
  247.  
  248.   31              23              15              7             0
  249.  ╔═══════════════╪═══════════════╪═══════════════╪══════════════╗
  250.  ║           HIGH WORD           │            LOW WORD          ║ DOUBLEWORD
  251.  ╚═══════════════╪═══════════════╪═══════════════╪══════════════╝
  252.     address n+3     address n+2     address n+1      address n
  253.  
  254.  
  255. Figure 2-3.  Bytes, Words, and Doublewords in Memory
  256.  
  257.                   MEMORY
  258.        BYTE       VALUES
  259. All values in hexadecimal
  260.      ADDRESS   ╔══════════╗
  261.               E║          ║
  262.                ╠══════════╣──┐
  263.               D║    7A    ║  ├─ DOUBLE WORD AT ADDRESS A
  264.                ╠══════════╣─┐│  CONTAINS 7AFE0636
  265.               C║    FE    ║ ││
  266.                ╠══════════╣ ├─ WORD AT ADDRESS B
  267.               B║    06    ║ ││ CONTAINS FE06
  268.                ╠══════════╣─┘│
  269.               A║    36    ║  │
  270.                ╠══════════╣══╡
  271.               9║    1F    ║  ├─ WORD AT ADDRESS 9
  272.                ╠══════════╣──┘  CONTAINS IF
  273.               8║          ║
  274.                ╠══════════╣──┐
  275.               7║    23    ║  │
  276.                ╠══════════╣  ├─ WORD AT ADDRESS 6
  277.               6║    OB    ║  │  CONTAINS 23OB
  278.                ╠══════════╣──┘
  279.               5║          ║
  280.                ╠══════════╣
  281.               4║          ║
  282.                ╠══════════╣──┐
  283.               3║    74    ║  │
  284.                ╠══════════╣─┐├─ WORD AT ADDRESS 2
  285.               2║    CB    ║ ││  CONTAINS 74CB
  286.                ╠══════════╣──┘
  287.               1║    31    ║ ├── WORD AT ADDRESS 1
  288.                ╠══════════╣─┘   CONTAINS CB31
  289.               0║          ║
  290.                ╚══════════╝
  291.  
  292.  
  293. Figure 2-4.  80386 Data Types
  294.  
  295.                                                            +1       0
  296.           7       0              7       0            15 14    8 7      0
  297.      BYTE ╔╤╤╤╤╤╤╤╗         BYTE ╔╤╤╤╤╤╤╤╗         WORD ╔╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╗
  298.   INTEGER ║│  │   ║      ORDINAL ║   │   ║      INTEGER ║│  │   │   │   ║
  299.           ╚╧══════╝              ╚═══════╝              ╚╧══════╧═══════╝
  300.   SIGN BIT┘└──────┘              └───────┘      SIGN BIT┘└MSB           │
  301.            MAGNITUDE             MAGNITUDE              └───────────────┘
  302.                                                             MAGNITUDE
  303.  
  304.  
  305.             +1       0                       +3     +2      +1       0
  306.          15              0               31            16 15             0
  307.     WORD ╔╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╗    DOUBLEWORD ╔╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╗
  308.  ORDINAL ║│  │   │   │   ║       INTEGER ║│  │   │   │   │   │   │   │   ║
  309.          ╚╧══════╧═══════╝               ╚╧══════╧═══════╧═══════╧═══════╝
  310.          │               │       SIGN BIT┘└MSB                           │
  311.          └───────────────┘               └───────────────────────────────┘
  312.              MAGNITUDE                              MAGNITUDE
  313.  
  314.  
  315.                            +3      +2      +1       0
  316.                         31                              0
  317.              DOUBLEWORD ╔╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╗
  318.                 ORDINAL ║   │   │   │   │   │   │   │   ║
  319.                         ╚═══════╧═══════╧═══════╧═══════╝
  320.                         └───────────────────────────────┘
  321.                                     MAGNITUDE
  322.  
  323.                            +N              +1       0
  324.                         7       0       7      0 7      0
  325.            BINARY CODED ╔╤╤╤╤╤╤╤╗       ╔╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╗
  326.           DECIMAL (BCD) ║   │   ║    ║   │   │   │   ║
  327.                         ╚═══════╝       ╚═══════╧═══════╝
  328.                            BCD             BCD     BCD
  329.                          DIGIT N         DIGIT 1  DIGIT 0
  330.  
  331.                            +N              +1       0
  332.                         7       0       7      0 7      0
  333.                  PACKED ╔╤╤╤╤╤╤╤╗       ╔╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╗
  334.                     BCD ║   │   ║    ║   │   │   │   ║
  335.                         ╚═══════╝       ╚═══════╧═══════╝
  336.                         └───┘                       └───┘
  337.                         MOST                        LEAST
  338.                         SIGNIFICANT           SIGNIFICANT
  339.                         DIGIT                       DIGIT
  340.  
  341.                            +N              +1       0
  342.                         7       0       7      0 7      0
  343.                    BYTE ╔╤╤╤╤╤╤╤╗       ╔╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╗
  344.                  STRING ║   │   ║    ║   │   │   │   ║
  345.                         ╚═══════╝       ╚═══════╧═══════╝
  346.  
  347.                                               -2 GIGABYTES
  348.                 +2 GIGABYTES                           210
  349.      BIT ╔╤╤╤╤════════════╤╤═══════  ════════════════╤╤╤╤╗
  350.   STRING ║││││            ││                         ││││║
  351.          ╚╧╧╧╧════════════╧╧════════  ═══════════════╧╧╧╧╝
  352.                          BIT 0
  353.  
  354.                +3      +2      +1       0
  355.             31                              0
  356. NEAR 32-BIT ╔╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╗
  357.     POINTER ║   │   │   │   │   │   │   │   ║
  358.             ╚═══════╧═══════╧═══════╧═══════╝
  359.             └───────────────────────────────┘
  360.                              OFFSET
  361.  
  362.               +5      +4      +3      +2      +1       0
  363.            48                                              0
  364. FAR 48-BIT ╔╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╗
  365.    POINTER ║   │   │   │   │   │   │   │   │   │   │   │   ║
  366.            ╚═══════╧═══════╧═══════╧═══════╧═══════╧═══════╝
  367.            └───────────────┴───────────────────────────────┘
  368.                 SELECTOR                 OFFSET
  369.  
  370.                   +5      +4      +3      +2      +1       0
  371.     32-BIT ╔╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╗
  372.  BIT FIELD ║   │   │   │   │   │   │   │   │   │   │   │   ║
  373.            ╚═══════╧═══════╧═══════╧═══════╧═══════╧═══════╝
  374.                    │───────── BIT FIELD ─────────│
  375.                              1 TO 32 BITS
  376.  
  377.  
  378. 2.3  Registers
  379.  
  380. The 80386 contains a total of sixteen registers that are of interest to the
  381. applications programmer. As Figure 2-5 shows, these registers may be
  382. grouped into these basic categories:
  383.  
  384.   1.  General registers. These eight 32-bit general-purpose registers are
  385.       used primarily to contain operands for arithmetic and logical
  386.       operations.
  387.  
  388.   2.  Segment registers. These special-purpose registers permit systems
  389.       software designers to choose either a flat or segmented model of
  390.       memory organization. These six registers determine, at any given time,
  391.       which segments of memory are currently addressable.
  392.  
  393.   3.  Status and instruction registers. These special-purpose registers are
  394.       used to record and alter certain aspects of the 80386 processor state.
  395.  
  396.  
  397. 2.3.1  General Registers
  398.  
  399. The general registers of the 80386 are the 32-bit registers EAX, EBX, ECX,
  400. EDX, EBP, ESP, ESI, and EDI. These registers are used interchangeably to
  401. contain the operands of logical and arithmetic operations. They may also be
  402. used interchangeably for operands of address computations (except that ESP
  403. cannot be used as an index operand).
  404.  
  405. As Figure 2-5 shows, the low-order word of each of these eight registers
  406. has a separate name and can be treated as a unit. This feature is useful for
  407. handling 16-bit data items and for compatibility with the 8086 and 80286
  408. processors. The word registers are named AX, BX, CX, DX, BP, SP, SI, and DI.
  409.  
  410. Figure 2-5 also illustrates that each byte of the 16-bit registers AX, BX,
  411. CX, and DX has a separate name and can be treated as a unit. This feature is
  412. useful for handling characters and other 8-bit data items. The byte
  413. registers are named AH, BH, CH, and DH (high bytes); and AL, BL, CL, and DL
  414. (low bytes).
  415.  
  416. All of the general-purpose registers are available for addressing
  417. calculations and for the results of most arithmetic and logical
  418. calculations; however, a few functions are dedicated to certain registers.
  419. By implicitly choosing registers for these functions, the 80386 architecture
  420. can encode instructions more compactly. The instructions that use specific
  421. registers include: double-precision multiply and divide, I/O, string
  422. instructions, translate, loop, variable shift and rotate, and stack
  423. operations.
  424.  
  425.  
  426. 2.3.2  Segment Registers
  427.  
  428. The segment registers of the 80386 give systems software designers the
  429. flexibility to choose among various models of memory organization.
  430. Implementation of memory models is the subject of Part II ── Systems
  431. Programming. Designers may choose a model in which applications programs do
  432. not need to modify segment registers, in which case applications programmers
  433. may skip this section.
  434.  
  435. Complete programs generally consist of many different modules, each
  436. consisting of instructions and data. However, at any given time during
  437. program execution, only a small subset of a program's modules are actually
  438. in use. The 80386 architecture takes advantage of this by providing
  439. mechanisms to support direct access to the instructions and data of the
  440. current module's environment, with access to additional segments on demand.
  441.  
  442. At any given instant, six segments of memory may be immediately accessible
  443. to an executing 80386 program. The segment registers CS, DS, SS, ES, FS, and
  444. GS are used to identify these six current segments. Each of these registers
  445. specifies a particular kind of segment, as characterized by the associated
  446. mnemonics ("code," "data," or "stack") shown in Figure 2-6. Each register
  447. uniquely determines one particular segment, from among the segments that
  448. make up the program, that is to be immediately accessible at highest speed.
  449.  
  450. The segment containing the currently executing sequence of instructions is
  451. known as the current code segment; it is specified by means of the CS
  452. register. The 80386 fetches all instructions from this code segment, using
  453. as an offset the contents of the instruction pointer. CS is changed
  454. implicitly as the result of intersegment control-transfer instructions (for
  455. example, CALL and JMP), interrupts, and exceptions.
  456.  
  457. Subroutine calls, parameters, and procedure activation records usually
  458. require that a region of memory be allocated for a stack. All stack
  459. operations use the SS register to locate the stack. Unlike CS, the SS
  460. register can be loaded explicitly, thereby permitting programmers to define
  461. stacks dynamically.
  462.  
  463. The DS, ES, FS, and GS registers allow the specification of four data
  464. segments, each addressable by the currently executing program. Accessibility
  465. to four separate data areas helps programs efficiently access different
  466. types of data structures; for example, one data segment register can point
  467. to the data structures of the current module, another to the exported data
  468. of a higher-level module, another to a dynamically created data structure,
  469. and another to data shared with another task. An operand within a data
  470. segment is addressed by specifying its offset either directly in an
  471. instruction or indirectly via general registers.
  472.  
  473. Depending on the structure of data (e.g., the way data is parceled into one
  474. or more segments), a program may require access to more than four data
  475. segments. To access additional segments, the DS, ES, FS, and GS registers
  476. can be changed under program control during the course of a program's
  477. execution. This simply requires that the program execute an instruction to
  478. load the appropriate segment register prior to executing instructions that
  479. access the data.
  480.  
  481. The processor associates a base address with each segment selected by a
  482. segment register. To address an element within a segment, a 32-bit offset is
  483. added to the segment's base address. Once a segment is selected (by loading
  484. the segment selector into a segment register), a data manipulation
  485. instruction only needs to specify the offset. Simple rules define which
  486. segment register is used to form an address when only an offset is
  487. specified.
  488.  
  489.  
  490. Figure 2-5.  80386 Applications Register Set
  491.  
  492.                               GENERAL REGISTERS
  493.  
  494.   31                23                15                7               0
  495.  ╔═════════════════╪═════════════════╬═════════════════╧═════════════════╗
  496.  ║                                  EAX       AH       AX      AL        ║
  497.  ╠═════════════════╪═════════════════╬═════════════════╩═════════════════╣
  498.  ║                                  EDX       DH       DX      DL        ║
  499.  ╠═════════════════╪═════════════════╬═════════════════╩═════════════════╣
  500.  ║                                  ECX       CH       CX      CL        ║
  501.  ╠═════════════════╪═════════════════╬═════════════════╩═════════════════╣
  502.  ║                                  EBX       BH       BX      BL        ║
  503.  ╠═════════════════╪═════════════════╬═════════════════╩═════════════════╣
  504.  ║                                  EBP                BP                ║
  505.  ╠═════════════════╪═════════════════╬═════════════════╪═════════════════╣
  506.  ║                                  ESI                SI                ║
  507.  ╠═════════════════╪═════════════════╬═════════════════╪═════════════════╣
  508.  ║                                  EDI                DI                ║
  509.  ╠═════════════════╪═════════════════╬═════════════════╪═════════════════╣
  510.  ║                                  ESP                SP                ║
  511.  ╚═════════════════╪═════════════════╬═════════════════╪═════════════════╝
  512.  
  513.  
  514.                       15                7               0
  515.                      ╔═════════════════╪═════════════════╗
  516.                      ║         CS (CODE SEGMENT)         ║
  517.                      ╟─────────────────┼─────────────────╢
  518.                      ║         SS (STACK SEGMENT)        ║
  519.     SEGMENT          ╟─────────────────┼─────────────────╢
  520.     REGISTERS        ║         DS (DATA SEGMENT)         ║
  521.                      ╟─────────────────┼─────────────────╢
  522.                      ║         ES (DATA SEGMENT)         ║
  523.                      ╟─────────────────┼─────────────────╢
  524.                      ║         FS (DATA SEGMENT)         ║
  525.                      ╟─────────────────┼─────────────────╢
  526.                      ║         GS (DATA SEGMENT)         ║
  527.                      ╚═════════════════╪═════════════════╝
  528.  
  529.  
  530.                        STATUS AND INSTRUCTION REGISTERS
  531.  
  532.      31               23                15                7              0
  533.    ╔═════════════════╪═════════════════╪═════════════════╪═════════════════╗
  534.    ║                                EFLAGS                                 ║
  535.    ╟───────────────────────────────────────────────────────────────────────╢
  536.    ║                        EIP (INSTRUCTION POINTER)                      ║
  537.    ╚═════════════════╪═════════════════╪═════════════════╪═════════════════╝
  538.  
  539.  
  540. Figure 2-6.  Use of Memory Segmentation
  541.  
  542.      ╔════════════════╗                                ╔════════════════╗
  543.      ║     MODULE     ║                                ║     MODULE     ║
  544.      ║       A        ║──┐                        ┌──║       A        ║
  545.      ║      CODE      ║   │                        │   ║      DATA      ║
  546.      ╚════════════════╝   │  ╔══════════════════╗  │   ╚════════════════╝
  547.                           └──╢    CS (CODE)     ║  │
  548.                              ╠══════════════════╣  │
  549.      ╔════════════════╗   ┌──╢    SS (STACK)    ║  │   ╔════════════════╗
  550.      ║                ║   │  ╠══════════════════╣  │   ║      DATA      ║
  551.      ║     STACK      ║──┘  ║    DS (DATA)     ╟──┘┌─║   STRUCTURE    ║
  552.      ║                ║      ╠══════════════════╣   │  ║       1        ║
  553.      ╚════════════════╝      ║    ES (DATA)     ╟───┘  ╚════════════════╝
  554.                              ╠══════════════════╣
  555.                           ┌──╢    FS (DATA)     ║
  556.      ╔════════════════╗   │  ╠══════════════════╣      ╔════════════════╗
  557.      ║      DATA      ║   │  ║    GS (DATA)     ╟──┐   ║      DATA      ║
  558.      ║   STRUCTURE    ║──┘  ╚══════════════════╝  └──║   STRUCTURE    ║
  559.      ║       2        ║                                ║       3        ║
  560.      ╚════════════════╝                                ╚════════════════╝
  561.  
  562.  
  563. 2.3.3  Stack Implementation
  564.  
  565. Stack operations are facilitated by three registers:
  566.  
  567.   1.  The stack segment (SS) register. Stacks are implemented in memory. A
  568.       system may have a number of stacks that is limited only by the maximum
  569.       number of segments. A stack may be up to 4 gigabytes long, the maximum
  570.       length of a segment. One stack is directly addressable at a time──the
  571.       one located by SS. This is the current stack, often referred to simply
  572.       as "the" stack. SS is used automatically by the processor for all
  573.       stack operations.
  574.  
  575.   2.  The stack pointer (ESP) register. ESP points to the top of the
  576.       push-down stack (TOS). It is referenced implicitly by PUSH and POP
  577.       operations, subroutine calls and returns, and interrupt operations.
  578.       When an item is pushed onto the stack (see Figure 2-7), the processor
  579.       decrements ESP, then writes the item at the new TOS. When an item is
  580.       popped off the stack, the processor copies it from TOS, then
  581.       increments ESP. In other words, the stack grows down in memory toward
  582.       lesser addresses.
  583.  
  584.   3.  The stack-frame base pointer (EBP) register. The EBP is the best
  585.       choice of register for accessing data structures, variables and
  586.       dynamically allocated work space within the stack. EBP is often used
  587.       to access elements on the stack relative to a fixed point on the stack
  588.       rather than relative to the current TOS. It typically identifies the
  589.       base address of the current stack frame established for the current
  590.       procedure. When EBP is used as the base register in an offset
  591.       calculation, the offset is calculated automatically in the current
  592.       stack segment (i.e., the segment currently selected by SS). Because
  593.       SS does not have to be explicitly specified, instruction encoding in
  594.       such cases is more efficient. EBP can also be used to index into
  595.       segments addressable via other segment registers.
  596.  
  597.  
  598. Figure 2-7.  80386 Stack
  599.  
  600.      31                         0
  601.      ╔══════╪══════╪══════╪══════╗ ───────BOTTOM OF STACK
  602.      ║                           ║       (INITIAL ESP VALUE)
  603.      ╟══════╪══════╪══════╪══════╣
  604.      ║                           ║
  605.      ╠══════╪══════╪══════╪══════╣        
  606.      ║                           ║        │POP
  607.      ╠══════╪══════╪══════╪══════╣        │
  608.      ║                           ║        │
  609.      ╠══════╪══════╪══════╪══════╣        │      TOP OF     ╔═════════════╗
  610.      ║                           ║ ──────┼─────────────────╢     ESP     ║
  611.      ╠══════╪══════╪══════╪══════╣        │      STACK      ╚═════════════╝
  612.      ║                           ║        │
  613.      ║                           ║        │
  614.      ║                           ║        │PUSH
  615.      ║                           ║        
  616.  
  617.  
  618. 2.3.4  Flags Register
  619.  
  620. The flags register is a 32-bit register named EFLAGS. Figure 2-8 defines
  621. the bits within this register. The flags control certain operations and
  622. indicate the status of the 80386.
  623.  
  624. The low-order 16 bits of EFLAGS is named FLAGS and can be treated as a
  625. unit. This feature is useful when executing 8086 and 80286 code, because
  626. this part of EFLAGS is identical to the FLAGS register of the 8086 and the
  627. 80286.
  628.  
  629. The flags may be considered in three groups: the status flags, the control
  630. flags, and the systems flags. Discussion of the systems flags is delayed
  631. until Part II.
  632.  
  633.  
  634. Figure 2-8.  EFLAGS Register  
  635.  
  636.                                               16-BIT FLAGS REGISTER
  637.                                                          A
  638.                                      ┌───────────────────┴───────────────┐
  639.   31                  23                  15               7            0
  640.  ╔═══════════════════╪═══════════════╤═╤═╤═╤═╤═╪═╤═╤═╤═╤═╤═╤═╤═╤═╤═╤═╤═╤═╗
  641.  ║                                   │V│R│ │N│ IO│O│D│I│T│S│Z│ │A│ │P│ │C║
  642.  ║ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 │ │ │0│ │   │ │ │ │ │ │ │0│ │0│ │1│ ║
  643.  ║                                   │M│F│ │T│ PL│F│F│F│F│F│F│ │F│ │F│ │F║
  644.  ╚═══════════════════╪═══════════════╧╤╧╤╧═╧╤╧═╪═╧╤╧╤╧╤╧╤╧╤╧╤╧═╧╤╧═╧╤╧═╧╤╝
  645.                                       │ │   │  │  │ │ │ │ │ │   │   │   │
  646.        VIRTUAL 8086 MODE───X──────────┘ │   │  │  │ │ │ │ │ │   │   │   │
  647.              RESUME FLAG───X────────────┘   │  │  │ │ │ │ │ │   │   │   │
  648.         NESTED TASK FLAG───X────────────────┘  │  │ │ │ │ │ │   │   │   │
  649.      I/O PRIVILEGE LEVEL───X───────────────────┘  │ │ │ │ │ │   │   │   │
  650.                 OVERFLOW───S──────────────────────┘ │ │ │ │ │   │   │   │
  651.           DIRECTION FLAG───C────────────────────────┘ │ │ │ │   │   │   │
  652.         INTERRUPT ENABLE───X──────────────────────────┘ │ │ │   │   │   │
  653.                TRAP FLAG───S────────────────────────────┘ │ │   │   │   │
  654.                SIGN FLAG───S──────────────────────────────┘ │   │   │   │
  655.                ZERO FLAG───S────────────────────────────────┘   │   │   │
  656.          AUXILIARY CARRY───S────────────────────────────────────┘   │   │
  657.              PARITY FLAG───S────────────────────────────────────────┘   │
  658.               CARRY FLAG───S────────────────────────────────────────────┘
  659.  
  660.           S = STATUS FLAG, C = CONTROL FLAG, X = SYSTEM FLAG
  661.  
  662.           NOTE: 0 OR 1 INDICATES INTEL RESERVED. DO NOT DEFINE
  663.  
  664.  
  665. 2.3.4.1  Status Flags
  666.  
  667. The status flags of the EFLAGS register allow the results of one
  668. instruction to influence later instructions. The arithmetic instructions use
  669. OF, SF, ZF, AF, PF, and CF. The SCAS (Scan String), CMPS (Compare String),
  670. and LOOP instructions use ZF to signal that their operations are complete.
  671. There are instructions to set, clear, and complement CF before execution of
  672. an arithmetic instruction. Refer to Appendix C for definition of each
  673. status flag.
  674.  
  675.  
  676. 2.3.4.2  Control Flag
  677.  
  678. The control flag DF of the EFLAGS register controls string instructions.
  679.  
  680. DF (Direction Flag, bit 10)
  681.  
  682.   Setting DF causes string instructions to auto-decrement; that is, to
  683.   process strings from high addresses to low addresses. Clearing DF causes
  684.   string instructions to auto-increment, or to process strings from low
  685.   addresses to high addresses.
  686.  
  687.  
  688. 2.3.4.3  Instruction Pointer
  689.  
  690. The instruction pointer register (EIP) contains the offset address,
  691. relative to the start of the current code segment, of the next sequential
  692. instruction to be executed. The instruction pointer is not directly visible
  693. to the programmer; it is controlled implicitly by control-transfer
  694. instructions, interrupts, and exceptions.
  695.  
  696. As Figure 2-9 shows, the low-order 16 bits of EIP is named IP and can be
  697. used by the processor as a unit. This feature is useful when executing
  698. instructions designed for the 8086 and 80286 processors.
  699.  
  700.  
  701. Figure 2-9.  Instruction Pointer Register
  702.  
  703.                                                  16-BIT IP REGISTER
  704.                                        ┌──────────────────┴────────────────┐
  705.     31                23                15                7               0
  706.    ╔═════════════════╪═════════════════╪═════════════════╪═════════════════╗
  707.    ║                       EIP (INSTRUCTION POINTER)                       ║
  708.    ╚═════════════════╪═════════════════╪═════════════════╪═════════════════╝
  709.  
  710.  
  711. 2.4  Instruction Format
  712.  
  713. The information encoded in an 80386 instruction includes a specification of
  714. the operation to be performed, the type of the operands to be manipulated,
  715. and the location of these operands. If an operand is located in memory, the
  716. instruction must also select, explicitly or implicitly, which of the
  717. currently addressable segments contains the operand.
  718.  
  719. 80386 instructions are composed of various elements and have various
  720. formats. The exact format of instructions is shown in Appendix B; the
  721. elements of instructions are described below. Of these instruction elements,
  722. only one, the opcode, is always present. The other elements may or may not
  723. be present, depending on the particular operation involved and on the
  724. location and type of the operands. The elements of an instruction, in order
  725. of occurrence are as follows:
  726.  
  727.   ■  Prefixes ── one or more bytes preceding an instruction that modify the
  728.      operation of the instruction. The following types of prefixes can be
  729.      used by applications programs:
  730.  
  731.      1.  Segment override ── explicitly specifies which segment register an
  732.          instruction should use, thereby overriding the default
  733.          segment-register selection used by the 80386 for that instruction.
  734.  
  735.      2.  Address size ── switches between 32-bit and 16-bit address 
  736.          generation.
  737.  
  738.      3.  Operand size ── switches between 32-bit and 16-bit operands.
  739.  
  740.      4.  Repeat ── used with a string instruction to cause the instruction
  741.          to act on each element of the string.
  742.  
  743.   ■  Opcode ── specifies the operation performed by the instruction. Some
  744.      operations have several different opcodes, each specifying a different
  745.      variant of the operation.
  746.  
  747.   ■  Register specifier ── an instruction may specify one or two register
  748.      operands. Register specifiers may occur either in the same byte as the
  749.      opcode or in the same byte as the addressing-mode specifier.
  750.  
  751.   ■  Addressing-mode specifier ── when present, specifies whether an operand
  752.      is a register or memory location; if in memory, specifies whether a
  753.      displacement, a base register, an index register, and scaling are to be
  754.      used.
  755.  
  756.   ■  SIB (scale, index, base) byte ── when the addressing-mode specifier
  757.      indicates that an index register will be used to compute the address of
  758.      an operand, an SIB byte is included in the instruction to encode the
  759.      base register, the index register, and a scaling factor.
  760.  
  761.   ■  Displacement ── when the addressing-mode specifier indicates that a
  762.      displacement will be used to compute the address of an operand, the
  763.      displacement is encoded in the instruction. A displacement is a signed
  764.      integer of 32, 16, or eight bits. The eight-bit form is used in the
  765.      common case when the displacement is sufficiently small. The processor
  766.      extends an eight-bit displacement to 16 or 32 bits, taking into
  767.      account the sign.
  768.  
  769.   ■  Immediate operand ── when present, directly provides the value of an
  770.      operand of the instruction. Immediate operands may be 8, 16, or 32 bits
  771.      wide. In cases where an eight-bit immediate operand is combined in some
  772.      way with a 16- or 32-bit operand, the processor automatically extends
  773.      the size of the eight-bit operand, taking into account the sign.
  774.  
  775.  
  776. 2.5  Operand Selection
  777.  
  778. An instruction can act on zero or more operands, which are the data
  779. manipulated by the instruction. An example of a zero-operand instruction is
  780. NOP (no operation). An operand can be in any of these locations:
  781.  
  782.   ■  In the instruction itself (an immediate operand)
  783.  
  784.   ■  In a register (EAX, EBX, ECX, EDX, ESI, EDI, ESP, or EBP in the case
  785.      of 32-bit operands; AX, BX, CX, DX, SI, DI, SP, or BP in the case of
  786.      16-bit operands; AH, AL, BH, BL, CH, CL, DH, or DL in the case of 8-bit
  787.      operands; the segment registers; or the EFLAGS register for flag
  788.      operations)
  789.  
  790.   ■  In memory
  791.  
  792.   ■  At an I/O port
  793.  
  794. Immediate operands and operands in registers can be accessed more rapidly
  795. than operands in memory since memory operands must be fetched from memory.
  796. Register operands are available in the CPU. Immediate operands are also
  797. available in the CPU, because they are prefetched as part of the
  798. instruction.
  799.  
  800. Of the instructions that have operands, some specify operands implicitly;
  801. others specify operands explicitly; still others use a combination of
  802. implicit and explicit specification; for example:
  803.  
  804. Implicit operand: AAM
  805.  
  806.   By definition, AAM (ASCII adjust for multiplication) operates on the
  807.   contents of the AX register.
  808.  
  809. Explicit operand: XCHG EAX, EBX
  810.  
  811.   The operands to be exchanged are encoded in the instruction after the
  812.   opcode.
  813.  
  814. Implicit and explicit operands: PUSH COUNTER
  815.  
  816.   The memory variable COUNTER (the explicit operand) is copied to the top of
  817.   the stack (the implicit operand).
  818.  
  819. Note that most instructions have implicit operands. All arithmetic
  820. instructions, for example, update the EFLAGS register.
  821.  
  822. An 80386 instruction can explicitly reference one or two operands.
  823. Two-operand instructions, such as MOV, ADD, XOR, etc., generally overwrite
  824. one of the two participating operands with the result. A distinction can
  825. thus be made between the source operand (the one unaffected by the
  826. operation) and the destination operand (the one overwritten by the result).
  827.  
  828. For most instructions, one of the two explicitly specified operands──either
  829. the source or the destination──can be either in a register or in memory.
  830. The other operand must be in a register or be an immediate source operand.
  831. Thus, the explicit two-operand instructions of the 80386 permit operations
  832. of the following kinds:
  833.  
  834.   ■  Register-to-register
  835.   ■  Register-to-memory
  836.   ■  Memory-to-register
  837.   ■  Immediate-to-register
  838.   ■  Immediate-to-memory
  839.  
  840. Certain string instructions and stack manipulation instructions, however,
  841. transfer data from memory to memory. Both operands of some string
  842. instructions are in memory and are implicitly specified. Push and pop stack
  843. operations allow transfer between memory operands and the memory-based
  844. stack.
  845.  
  846.  
  847. 2.5.1  Immediate Operands
  848.  
  849. Certain instructions use data from the instruction itself as one (and
  850. sometimes two) of the operands. Such an operand is called an immediate
  851. operand. The operand may be 32-, 16-, or 8-bits long. For example:
  852.  
  853. SHR PATTERN, 2
  854.  
  855. One byte of the instruction holds the value 2, the number of bits by which
  856. to shift the variable PATTERN.
  857.  
  858. TEST PATTERN, 0FFFF00FFH
  859.  
  860. A doubleword of the instruction holds the mask that is used to test the
  861. variable PATTERN.
  862.  
  863.  
  864. 2.5.2  Register Operands
  865.  
  866. Operands may be located in one of the 32-bit general registers (EAX, EBX,
  867. ECX, EDX, ESI, EDI, ESP, or EBP), in one of the 16-bit general registers
  868. (AX, BX, CX, DX, SI, DI, SP, or BP), or in one of the 8-bit general
  869. registers (AH, BH, CH, DH, AL, BL, CL,or DL).
  870.  
  871. The 80386 has instructions for referencing the segment registers (CS, DS,
  872. ES, SS, FS, GS). These instructions are used by applications programs only
  873. if systems designers have chosen a segmented memory model.
  874.  
  875. The 80386 also has instructions for referring to the flag register. The
  876. flags may be stored on the stack and restored from the stack. Certain
  877. instructions change the commonly modified flags directly in the EFLAGS
  878. register. Other flags that are seldom modified can be modified indirectly
  879. via the flags image in the stack.
  880.  
  881.  
  882. 2.5.3  Memory Operands
  883.  
  884. Data-manipulation instructions that address operands in memory must specify
  885. (either directly or indirectly) the segment that contains the operand and
  886. the offset of the operand within the segment. However, for speed and compact
  887. instruction encoding, segment selectors are stored in the high speed segment
  888. registers. Therefore, data-manipulation instructions need to specify only
  889. the desired segment register and an offset in order to address a memory
  890. operand.
  891.  
  892. An 80386 data-manipulation instruction that accesses memory uses one of the
  893. following methods for specifying the offset of a memory operand within its
  894. segment:
  895.  
  896.   1.  Most data-manipulation instructions that access memory contain a byte
  897.       that explicitly specifies the addressing method for the operand. A
  898.       byte, known as the modR/M byte, follows the opcode and specifies
  899.       whether the operand is in a register or in memory. If the operand is
  900.       in memory, the address is computed from a segment register and any of
  901.       the following values: a base register, an index register, a scaling
  902.       factor, a displacement. When an index register is used, the modR/M
  903.       byte is also followed by another byte that identifies the index
  904.       register and scaling factor. This addressing method is the
  905.       mostflexible.
  906.  
  907.   2.  A few data-manipulation instructions implicitly use specialized
  908.       addressing methods:
  909.  
  910.       ■   For a few short forms of MOV that implicitly use the EAX register,
  911.           the offset of the operand is coded as a doubleword in the
  912.           instruction. No base register, index register, or scaling factor
  913.           are used.
  914.  
  915.       ■   String operations implicitly address memory via DS:ESI, (MOVS,
  916.           CMPS, OUTS, LODS, SCAS) or via ES:EDI (MOVS, CMPS, INS, STOS).
  917.  
  918.       ■   Stack operations implicitly address operands via SS:ESP
  919.           registers; e.g., PUSH, POP, PUSHA, PUSHAD, POPA, POPAD, PUSHF,
  920.           PUSHFD, POPF, POPFD, CALL, RET, IRET, IRETD, exceptions, and
  921.           interrupts.
  922.  
  923.  
  924. 2.5.3.1  Segment Selection
  925.  
  926. Data-manipulation instructions need not explicitly specify which segment
  927. register is used. For all of these instructions, specification of a segment
  928. register is optional. For all memory accesses, if a segment is not
  929. explicitly specified by the instruction, the processor automatically chooses
  930. a segment register according to the rules of Table 2-1. (If systems
  931. designers have chosen a flat model of memory organization, the segment
  932. registers and the rules that the processor uses in choosing them are not
  933. apparent to applications programs.)
  934.  
  935. There is a close connection between the kind of memory reference and the
  936. segment in which that operand resides. As a rule, a memory reference implies
  937. the current data segment (i.e., the implicit segment selector is in DS).
  938. However, ESP and EBP are used to access items on the stack; therefore, when
  939. the ESP or EBP register is used as a base register, the current stack
  940. segment is implied (i.e., SS contains the selector).
  941.  
  942. Special instruction prefix elements may be used to override the default
  943. segment selection. Segment-override prefixes allow an explicit segment
  944. selection. The 80386 has a segment-override prefix for each of the segment
  945. registers. Only in the following special cases is there an implied segment
  946. selection that a segment prefix cannot override:
  947.  
  948.   ■  The use of ES for destination strings in string instructions.
  949.   ■  The use of SS in stack instructions.
  950.   ■  The use of CS for instruction fetches.
  951.  
  952.  
  953. Table 2-1. Default Segment Register Selection Rules
  954.  
  955. Memory Reference Needed  Segment     Implicit Segment Selection Rule
  956.                          Register
  957.                          Used
  958.  
  959. Instructions             Code (CS)   Automatic with instruction prefetch
  960. Stack                    Stack (SS)  All stack pushes and pops. Any
  961.                                      memory reference that uses ESP or
  962.                                      EBP as a base register.
  963. Local Data               Data (DS)   All data references except when
  964.                                      relative to stack or string 
  965.                                      destination.
  966. Destination Strings      Extra (ES)  Destination of string instructions.
  967.  
  968.  
  969. 2.5.3.2  Effective-Address Computation
  970.  
  971. The modR/M byte provides the most flexible of the addressing methods, and
  972. instructions that require a modR/M byte as the second byte of the
  973. instruction are the most common in the 80386 instruction set. For memory
  974. operands defined by modR/M, the offset within the desired segment is
  975. calculated by taking the sum of up to three components:
  976.  
  977.   ■  A displacement element in the instruction.
  978.  
  979.   ■  A base register.
  980.  
  981.   ■  An index register. The index register may be automatically multiplied
  982.      by a scaling factor of 2, 4, or 8.
  983.  
  984. The offset that results from adding these components is called an effective
  985. address. Each of these components of an effective address may have either a
  986. positive or negative value. If the sum of all the components exceeds 2^(32),
  987. the effective address is truncated to 32 bits.Figure 2-10 illustrates the
  988. full set of possibilities for modR/M addressing.
  989.  
  990. The displacement component, because it is encoded in the instruction, is
  991. useful for fixed aspects of addressing; for example:
  992.  
  993.   ■  Location of simple scalar operands.
  994.   ■  Beginning of a statically allocated array.
  995.   ■  Offset of an item within a record.
  996.  
  997. The base and index components have similar functions. Both utilize the same
  998. set of general registers. Both can be used for aspects of addressing that
  999. are determined dynamically; for example:
  1000.  
  1001.   ■  Location of procedure parameters and local variables in stack.
  1002.  
  1003.   ■  The beginning of one record among several occurrences of the same
  1004.      record type or in an array of records.
  1005.  
  1006.   ■  The beginning of one dimension of multiple dimension array.
  1007.  
  1008.   ■  The beginning of a dynamically allocated array.
  1009.  
  1010. The uses of general registers as base or index components differ in the
  1011. following respects:
  1012.  
  1013.   ■  ESP cannot be used as an index register.
  1014.  
  1015.   ■  When ESP or EBP is used as the base register, the default segment is
  1016.      the one selected by SS. In all other cases the default segment is DS.
  1017.  
  1018. The scaling factor permits efficient indexing into an array in the common
  1019. cases when array elements are 2, 4, or 8 bytes wide. The shifting of the
  1020. index register is done by the processor at the time the address is evaluated
  1021. with no performance loss. This eliminates the need for a separate shift or
  1022. multiply instruction.
  1023.  
  1024. The base, index, and displacement components may be used in any
  1025. combination; any of these components may be null. A scale factor can be used
  1026. only when an index is also used. Each possible combination is useful for
  1027. data structures commonly used by programmers in high-level languages and
  1028. assembly languages. Following are possible uses for some of the various
  1029. combinations of address components.
  1030.  
  1031. DISPLACEMENT
  1032.  
  1033.    The displacement alone indicates the offset of the operand. This
  1034.    combination is used to directly address a statically allocated scalar
  1035.    operand. An 8-bit, 16-bit, or 32-bit displacement can be used.
  1036.  
  1037. BASE
  1038.  
  1039.    The offset of the operand is specified indirectly in one of the general
  1040.    registers, as for "based" variables.
  1041.  
  1042. BASE + DISPLACEMENT
  1043.  
  1044.    A register and a displacement can be used together for two distinct
  1045.    purposes:
  1046.  
  1047.    1.  Index into static array when element size is not 2, 4, or 8 bytes.
  1048.        The displacement component encodes the offset of the beginning of
  1049.        the array. The register holds the results of a calculation to
  1050.        determine the offset of a specific element within the array.
  1051.  
  1052.    2.  Access item of a record. The displacement component locates an
  1053.        item within record. The base register selects one of several
  1054.        occurrences of record, thereby providing a compact encoding for
  1055.        this common function.
  1056.  
  1057.    An important special case of this combination, is to access parameters
  1058.    in the procedure activation record in the stack.  In this case, EBP is
  1059.    the best choice for the base register, because when EBP is used as a
  1060.    base register, the processor automatically uses the stack segment
  1061.    register (SS) to locate the operand, thereby providing a compact
  1062.    encoding for this common function.
  1063.  
  1064. (INDEX * SCALE) + DISPLACEMENT
  1065.  
  1066.    This combination provides efficient indexing into a static array when
  1067.    the element size is 2, 4, or 8 bytes. The displacement addresses the
  1068.    beginning of the array, the index register holds the subscript of the
  1069.    desired array element, and the processor automatically converts the
  1070.    subscript into an index by applying the scaling factor.
  1071.  
  1072. BASE + INDEX + DISPLACEMENT
  1073.  
  1074.    Two registers used together support either a two-dimensional array (the
  1075.    displacement determining the beginning of the array) or one of several
  1076.    instances of an array of records (the displacement indicating an item
  1077.    in the record).
  1078.  
  1079. BASE + (INDEX * SCALE) + DISPLACEMENT
  1080.  
  1081.    This combination provides efficient indexing of a two-dimensional array
  1082.    when the elements of the array are 2, 4, or 8 bytes wide.
  1083.  
  1084.  
  1085. Figure 2-10.  Effective Address Computation
  1086.  
  1087.       SEGMENT +    BASE   +    (INDEX * SCALE)  +     DISPLACEMENT
  1088.  
  1089.                  ┌     ┐
  1090.                  │ --- │     ┌     ┐     ┌   ┐
  1091.       ┌    ┐     │ EAX │     │ EAX │     │ 1 │
  1092.       │ CS │     │ ECX │     │ ECX │     │   │     ┌                     ┐
  1093.       │ SS │     │ EDX │     │ EDX │     │ 2 │     │     NO DISPLACEMENT │
  1094.      ─┤ DS ├─ + ─┤ EBX ├─ + ─┤ EBX ├─ * ─┤   ├─ + ─┤  8-BIT DISPLACEMENT ├─
  1095.       │ ES │     │ ESP │     │ --- │     │ 4 │     │ 32-BIT DISPLACEMENT │
  1096.       │ FS │     │ EBP │     │ EBP │     │   │     └                     ┘
  1097.       │ GS │     │ ESI │     │ ESI │     │ 6 │
  1098.       └    ┘     │ EDI │     │ EDI │     └   ┘
  1099.                  └     ┘     └     ┘
  1100.  
  1101.  
  1102. 2.6  Interrupts and Exceptions
  1103.  
  1104. The 80386 has two mechanisms for interrupting program execution:
  1105.  
  1106.   1.  Exceptions are synchronous events that are the responses of the CPU
  1107.       to certain conditions detected during the execution of an instruction.
  1108.  
  1109.   2.  Interrupts are asynchronous events typically triggered by external
  1110.       devices needing attention.
  1111.  
  1112. Interrupts and exceptions are alike in that both cause the processor to
  1113. temporarily suspend its present program execution in order to execute a
  1114. program of higher priority. The major distinction between these two kinds of
  1115. interrupts is their origin. An exception is always reproducible by
  1116. re-executing with the program and data that caused the exception, whereas an
  1117. interrupt is generally independent of the currently executing program.
  1118.  
  1119. Application programmers are not normally concerned with servicing
  1120. interrupts. More information on interrupts for systems programmers may be
  1121. found in Chapter 9. Certain exceptions, however, are of interest to
  1122. applications programmers, and many operating systems give applications
  1123. programs the opportunity to service these exceptions. However, the operating
  1124. system itself defines the interface between the applications programs and
  1125. the exception mechanism of the 80386.
  1126.  
  1127. Table 2-2 highlights the exceptions that may be of interest to applications
  1128. programmers.
  1129.  
  1130.   ■  A divide error exception results when the instruction DIV or IDIV is
  1131.      executed with a zero denominator or when the quotient is too large for
  1132.      the destination operand. (Refer to Chapter 3 for a discussion of DIV
  1133.      and IDIV.)
  1134.  
  1135.   ■  The debug exception may be reflected back to an applications program
  1136.      if it results from the trap flag (TF).
  1137.  
  1138.   ■  A breakpoint exception results when the instruction INT 3 is executed.
  1139.      This instruction is used by some debuggers to stop program execution at
  1140.      specific points.
  1141.  
  1142.   ■  An overflow exception results when the INTO instruction is executed
  1143.      and the OF (overflow) flag is set (after an arithmetic operation that
  1144.      set the OF flag). (Refer to Chapter 3 for a discussion of INTO).
  1145.  
  1146.   ■  A bounds check exception results when the BOUND instruction is
  1147.      executed and the array index it checks falls outside the bounds of the
  1148.      array. (Refer to Chapter 3 for a discussion of the BOUND instruction.)
  1149.  
  1150.   ■  Invalid opcodes may be used by some applications to extend the
  1151.      instruction set. In such a case, the invalid opcode exception presents
  1152.      an opportunity to emulate the opcode.
  1153.  
  1154.   ■  The "coprocessor not available" exception occurs if the program
  1155.      contains instructions for a coprocessor, but no coprocessor is present
  1156.      in the system.
  1157.  
  1158.   ■  A coprocessor error is generated when a coprocessor detects an illegal
  1159.      operation.
  1160.  
  1161. The instruction INT generates an interrupt whenever it is executed; the
  1162. processor treats this interrupt as an exception. The effects of this
  1163. interrupt (and the effects of all other exceptions) are determined by
  1164. exception handler routines provided by the application program or as part of
  1165. the systems software (provided by systems programmers). The INT instruction
  1166. itself is discussed in Chapter 3. Refer to Chapter 9 for a more complete
  1167. description of exceptions.
  1168.  
  1169.  
  1170. Table 2-2. 80386 Reserved Exceptions and Interrupts
  1171.  
  1172. Vector Number      Description
  1173.  
  1174. 0                  Divide Error
  1175. 1                  Debug Exceptions
  1176. 2                  NMI Interrupt
  1177. 3                  Breakpoint
  1178. 4                  INTO Detected Overflow
  1179. 5                  BOUND Range Exceeded
  1180. 6                  Invalid Opcode
  1181. 7                  Coprocessor Not Available
  1182. 8                  Double Exception
  1183. 9                  Coprocessor Segment Overrun
  1184. 10                 Invalid Task State Segment
  1185. 11                 Segment Not Present
  1186. 12                 Stack Fault
  1187. 13                 General Protection
  1188. 14                 Page Fault
  1189. 15                 (reserved)
  1190. 16                 Coprocessor Error
  1191. 17-32              (reserved)
  1192.  
  1193.  
  1194.