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

  1. Chapter 3  Applications Instruction Set
  2.  
  3. ────────────────────────────────────────────────────────────────────────────
  4.  
  5. This chapter presents an overview of the instructions which programmers can
  6. use to write application software for the 80386 executing in protected
  7. virtual-address mode. The instructions are grouped by categories of related
  8. functions.
  9.  
  10. The instructions not discussed in this chapter are those that are normally
  11. used only by operating-system programmers. Part II describes the operation
  12. of these instructions.
  13.  
  14. The descriptions in this chapter assume that the 80386 is operating in
  15. protected mode with 32-bit addressing in effect; however, all instructions
  16. discussed are also available when 16-bit addressing is in effect in
  17. protected mode, real mode, or virtual 8086 mode. For any differences of
  18. operation that exist in the various modes, refer to Chapter 13,
  19. Chapter 14, or Chapter 15.
  20.  
  21. The instruction dictionary in Chapter 17 contains more detailed
  22. descriptions of all instructions, including encoding, operation, timing,
  23. effect on flags, and exceptions.
  24.  
  25.  
  26. 3.1  Data Movement Instructions
  27.  
  28. These instructions provide convenient methods for moving bytes, words, or
  29. doublewords of data between memory and the registers of the base
  30. architecture. They fall into the following classes:
  31.  
  32.   1.  General-purpose data movement instructions.
  33.   2.  Stack manipulation instructions.
  34.   3.  Type-conversion instructions.
  35.  
  36.  
  37. 3.1.1  General-Purpose Data Movement Instructions
  38.  
  39. MOV (Move) transfers a byte, word, or doubleword from the source operand to
  40. the destination operand. The MOV instruction is useful for transferring data
  41. along any of these paths
  42. There are also variants of MOV that operate on segment registers.  These
  43. are covered in a later section of this chapter.:
  44.  
  45.   ■  To a register from memory
  46.   ■  To memory from a register
  47.   ■  Between general registers
  48.   ■  Immediate data to a register
  49.   ■  Immediate data to a memory
  50.  
  51. The MOV instruction cannot move from memory to memory or from segment
  52. register to segment register are not allowed. Memory-to-memory moves can be
  53. performed, however, by the string move instruction MOVS.
  54.  
  55. XCHG (Exchange) swaps the contents of two operands. This instruction takes
  56. the place of three MOV instructions. It does not require a temporary
  57. location to save the contents of one operand while load the other is being
  58. loaded. XCHG is especially useful for implementing semaphores or similar
  59. data structures for process synchronization.
  60.  
  61. The XCHG instruction can swap two byte operands, two word operands, or two
  62. doubleword operands. The operands for the XCHG instruction may be two
  63. register operands, or a register operand with a memory operand. When used
  64. with a memory operand, XCHG automatically activates the LOCK signal. (Refer
  65. to Chapter 11 for more information on the bus lock.)
  66.  
  67.  
  68. 3.1.2  Stack Manipulation Instructions
  69.  
  70. PUSH (Push) decrements the stack pointer (ESP), then transfers the source
  71. operand to the top of stack indicated by ESP (see Figure 3-1). PUSH is
  72. often used to place parameters on the stack before calling a procedure; it
  73. is also the basic means of storing temporary variables on the stack. The
  74. PUSH instruction operates on memory operands, immediate operands, and
  75. register operands (including segment registers).
  76.  
  77. PUSHA (Push All Registers) saves the contents of the eight general
  78. registers on the stack (see Figure 3-2). This instruction simplifies
  79. procedure calls by reducing the number of instructions required to retain
  80. the contents of the general registers for use in a procedure. The processor
  81. pushes the general registers on the stack in the following order: EAX, ECX,
  82. EDX, EBX, the initial value of ESP before EAX was pushed, EBP, ESI, and
  83. EDI. PUSHA is complemented by the POPA instruction.
  84.  
  85. POP (Pop) transfers the word or doubleword at the current top of stack
  86. (indicated by ESP) to the destination operand, and then increments ESP to
  87. point to the new top of stack. See Figure 3-3. POP moves information from
  88. the stack to a general register, or to memory
  89. There are also a variant of POP that operates on segment registers. This
  90. is covered in a later section of this chapter..
  91.  
  92. POPA (Pop All Registers) restores the registers saved on the stack by
  93. PUSHA, except that it ignores the saved value of ESP. See Figure 3-4.
  94.  
  95.  
  96. Figure 3-1.  PUSH
  97.  
  98.        D  O        BEFORE PUSH                  AFTER PUSH
  99.        I  F      31          0             31          0 
  100.        R        ║               ║           ║               ║
  101.        E  E     ╠═══════╪═══════╣           ╠═══════╪═══════╣
  102.        C  X     ║▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒║           ║▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒║
  103.        T  P     ╠═══════╪═══════╣           ╠═══════╪═══════╣
  104.        I  A     ║▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒║           ║▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒║
  105.        O  N     ╠═══════╪═══════╣──ESP     ╠═══════╪═══════╣
  106.        N  S     ║               ║           ║    OPERAND    ║
  107.           I     ╠═══════╪═══════╣           ╠═══════╪═══════╣──ESP
  108.         │ O     ║               ║           ║               ║
  109.         │ N     ╠═══════╪═══════╣           ╠═══════╪═══════╣
  110.         │       ║               ║           ║               ║
  111.                ╠═══════╪═══════╣           ╠═══════╪═══════╣
  112.                 ║               ║           ║               ║
  113.                                                          
  114.  
  115.  
  116. Figure 3-2.  PUSHA
  117.  
  118.                    BEFORE PUSHA                AFTER PUSHA
  119.                  31          0             31          0 
  120.          D  O   ║               ║           ║               ║
  121.          I  F   ╠═══════╪═══════╣           ╠═══════╪═══════╣
  122.          R      ║▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒║           ║▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒║
  123.          E  E   ╠═══════╪═══════╣           ╠═══════╪═══════╣
  124.          C  X   ║▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒║           ║▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒║
  125.          T  P   ╠═══════╪═══════╣──ESP     ╠═══════╪═══════╣
  126.          I  A   ║               ║           ║      EAX      ║
  127.          O  N   ╠═══════╪═══════╣           ╠═══════╪═══════╣
  128.          N  S   ║               ║           ║      ECX      ║
  129.             I   ╠═══════╪═══════╣           ╠═══════╪═══════╣
  130.           │ O   ║               ║           ║      EDX      ║
  131.           │ N   ╠═══════╪═══════╣           ╠═══════╪═══════╣
  132.           │     ║               ║           ║      EBX      ║
  133.                ╠═══════╪═══════╣           ╠═══════╪═══════╣
  134.                 ║               ║           ║    OLD ESP    ║
  135.                 ╠═══════╪═══════╣           ╠═══════╪═══════╣
  136.                 ║               ║           ║      EBP      ║
  137.                 ╠═══════╪═══════╣           ╠═══════╪═══════╣
  138.                 ║               ║           ║      ESI      ║
  139.                 ╠═══════╪═══════╣           ╠═══════╪═══════╣
  140.                 ║               ║           ║      EDI      ║
  141.                 ╠═══════╪═══════╣           ╠═══════╪═══════╣──ESP
  142.                 ║               ║           ║               ║
  143.                 ╠═══════╪═══════╣           ╠═══════╪═══════╣
  144.                 ║               ║           ║               ║
  145.                                                          
  146.  
  147.  
  148. 3.1.3  Type Conversion Instructions
  149.  
  150. The type conversion instructions convert bytes into words, words into
  151. doublewords, and doublewords into 64-bit items (quad-words). These
  152. instructions are especially useful for converting signed integers, because
  153. they automatically fill the extra bits of the larger item with the value of
  154. the sign bit of the smaller item. This kind of conversion, illustrated by
  155. Figure 3-5, is called sign extension.
  156.  
  157. There are two classes of type conversion instructions:
  158.  
  159.   1.  The forms CWD, CDQ, CBW, and CWDE which operate only on data in the
  160.       EAX register.
  161.  
  162.   2.  The forms MOVSX and MOVZX, which permit one operand to be in any
  163.       general register while permitting the other operand to be in memory or
  164.       in a register.
  165.  
  166. CWD (Convert Word to Doubleword) and CDQ (Convert Doubleword to Quad-Word)
  167. double the size of the source operand. CWD extends the sign of the
  168. word in register AX throughout register DX. CDQ extends the sign of the
  169. doubleword in EAX throughout EDX. CWD can be used to produce a doubleword
  170. dividend from a word before a word division, and CDQ can be used to produce
  171. a quad-word dividend from a doubleword before doubleword division.
  172.  
  173. CBW (Convert Byte to Word) extends the sign of the byte in register AL
  174. throughout AX.
  175.  
  176. CWDE (Convert Word to Doubleword Extended) extends the sign of the word in
  177. register AX throughout EAX.
  178.  
  179. MOVSX (Move with Sign Extension) sign-extends an 8-bit value to a 16-bit
  180. value and a 8- or 16-bit value to 32-bit value.
  181.  
  182. MOVZX (Move with Zero Extension) extends an 8-bit value to a 16-bit value
  183. and an 8- or 16-bit value to 32-bit value by inserting high-order zeros.
  184.  
  185.  
  186. Figure 3-3.  POP
  187.  
  188.        D  O         BEFORE POP                  AFTER POP
  189.        I  F      31          0             31          0 
  190.        R        ║               ║           ║               ║
  191.        E  E     ╠═══════╪═══════╣           ╠═══════╪═══════╣
  192.        C  X     ║▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒║           ║▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒║
  193.        T  P     ╠═══════╪═══════╣           ╠═══════╪═══════╣
  194.        I  A     ║▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒║           ║▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒║
  195.        O  N     ╠═══════╪═══════╣           ╠═══════╪═══════╣──ESP
  196.        N  S     ║    OPERAND    ║           ║               ║
  197.           I     ╠═══════╪═══════╣──ESP     ╠═══════╪═══════╣
  198.         │ O     ║               ║           ║               ║
  199.         │ N     ╠═══════╪═══════╣           ╠═══════╪═══════╣
  200.         │       ║               ║           ║               ║
  201.                ╠═══════╪═══════╣           ╠═══════╪═══════╣
  202.                 ║               ║           ║               ║
  203.                                                          
  204.  
  205.  
  206. Figure 3-4.  POPA
  207.  
  208.                    BEFORE POPA                  AFTER POPA
  209.                  31          0             31          0 
  210.          D  O   ║               ║           ║               ║
  211.          I  F   ╠═══════╪═══════╣           ╠═══════╪═══════╣
  212.          R      ║▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒║           ║▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒║
  213.          E  E   ╠═══════╪═══════╣           ╠═══════╪═══════╣
  214.          C  X   ║▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒║           ║▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒║
  215.          T  P   ╠═══════╪═══════╣           ╠═══════╪═══════╣──ESP
  216.          I  A   ║      EAX      ║           ║               ║
  217.          O  N   ╠═══════╪═══════╣           ╠═══════╪═══════╣
  218.          N  S   ║      ECX      ║           ║               ║
  219.             I   ╠═══════╪═══════╣           ╠═══════╪═══════╣
  220.           │ O   ║      EDX      ║           ║               ║
  221.           │ N   ╠═══════╪═══════╣           ╠═══════╪═══════╣
  222.           │     ║      EBX      ║           ║               ║
  223.                ╠═══════╪═══════╣           ╠═══════╪═══════╣
  224.                 ║      ESP      ║           ║               ║
  225.                 ╠═══════╪═══════╣           ╠═══════╪═══════╣
  226.                 ║      EPB      ║           ║               ║
  227.                 ╠═══════╪═══════╣           ╠═══════╪═══════╣
  228.                 ║      ESI      ║           ║               ║
  229.                 ╠═══════╪═══════╣           ╠═══════╪═══════╣
  230.                 ║      EDI      ║           ║               ║
  231.                 ╠═══════╪═══════╣──ESP     ╠═══════╪═══════╣
  232.                 ║               ║           ║               ║
  233.                 ╠═══════╪═══════╣           ╠═══════╪═══════╣
  234.                 ║               ║           ║               ║
  235.                                                          
  236.  
  237.  
  238. Figure 3-5.  Sign Extension
  239.  
  240.                                     15                7               0
  241.                                     ╔═╦══════════════╪════════════════╗
  242.      BEFORE SIGN EXTENSION─────────║S║ N N N N N N N N N N N N N N N ║
  243.                                     ╚═╩══════════════╪════════════════╝
  244.      AFTER SIGN EXTENSION──────┐
  245.                                │
  246.     31               23             15              7              0
  247.     ╔═╦═════════════╪═══════════════╪═══════════════╪═══════════════╗
  248.     ║S║S S S S S S S S S S S S S S S S N N N N N N N N N N N N N N N║
  249.     ╚═╩═════════════╪═══════════════╪═══════════════╪═══════════════╝
  250.  
  251.  
  252. 3.2  Binary Arithmetic Instructions
  253.  
  254. The arithmetic instructions of the 80386 processor simplify the
  255. manipulation of numeric data that is encoded in binary. Operations include
  256. the standard add, subtract, multiply, and divide as well as increment,
  257. decrement, compare, and change sign. Both signed and unsigned binary
  258. integers are supported. The binary arithmetic instructions may also be used
  259. as one step in the process of performing arithmetic on decimal integers.
  260.  
  261. Many of the arithmetic instructions operate on both signed and unsigned
  262. integers. These instructions update the flags ZF, CF, SF, and OF in such a
  263. manner that subsequent instructions can interpret the results of the
  264. arithmetic as either signed or unsigned. CF contains information relevant to
  265. unsigned integers; SF and OF contain information relevant to signed
  266. integers. ZF is relevant to both signed and unsigned integers; ZF is set
  267. when all bits of the result are zero.
  268.  
  269. If the integer is unsigned, CF may be tested after one of these arithmetic
  270. operations to determine whether the operation required a carry or borrow of
  271. a one-bit in the high-order position of the destination operand. CF is set
  272. if a one-bit was carried out of the high-order position (addition
  273. instructions ADD, ADC, AAA, and DAA) or if a one-bit was carried (i.e.
  274. borrowed) into the high-order bit (subtraction instructions SUB, SBB, AAS,
  275. DAS, CMP, and NEG).
  276.  
  277. If the integer is signed, both SF and OF should be tested. SF always has
  278. the same value as the sign bit of the result. The most significant bit (MSB)
  279. of a signed integer is the bit next to the sign──bit 6 of a byte, bit 14 of
  280. a word, or bit 30 of a doubleword. OF is set in either of these cases:
  281.  
  282.   ■  A one-bit was carried out of the MSB into the sign bit but no one bit
  283.      was carried out of the sign bit (addition instructions ADD, ADC, INC,
  284.      AAA, and DAA). In other words, the result was greater than the greatest
  285.      positive number that could be contained in the destination operand.
  286.  
  287.   ■  A one-bit was carried from the sign bit into the MSB but no one bit
  288.      was carried into the sign bit (subtraction instructions SUB, SBB, DEC,
  289.      AAS, DAS, CMP, and NEG). In other words, the result was smaller that
  290.      the smallest negative number that could be contained in the destination
  291.      operand.
  292.  
  293. These status flags are tested by executing one of the two families of
  294. conditional instructions: Jcc (jump on condition cc) or SETcc (byte set on
  295. condition).
  296.  
  297.  
  298. 3.2.1  Addition and Subtraction Instructions
  299.  
  300. ADD (Add Integers) replaces the destination operand with the sum of the
  301. source and destination operands. Sets CF if overflow.
  302.  
  303. ADC (Add Integers with Carry) sums the operands, adds one if CF is set, and
  304. replaces the destination operand with the result. If CF is cleared, ADC
  305. performs the same operation as the ADD instruction. An ADD followed by
  306. multiple ADC instructions can be used to add numbers longer than 32 bits.
  307.  
  308. INC (Increment) adds one to the destination operand. INC does not affect
  309. CF. Use ADD with an immediate value of 1 if an increment that updates carry
  310. (CF) is needed.
  311.  
  312. SUB (Subtract Integers) subtracts the source operand from the destination
  313. operand and replaces the destination operand with the result. If a borrow is
  314. required, the CF is set. The operands may be signed or unsigned bytes,
  315. words, or doublewords.
  316.  
  317. SBB (Subtract Integers with Borrow) subtracts the source operand from the
  318. destination operand, subtracts 1 if CF is set, and returns the result to the
  319. destination operand. If CF is cleared, SBB performs the same operation as
  320. SUB. SUB followed by multiple SBB instructions may be used to subtract
  321. numbers longer than 32 bits. If CF is cleared, SBB performs the same
  322. operation as SUB.
  323.  
  324. DEC (Decrement) subtracts 1 from the destination operand. DEC does not
  325. update CF. Use SUB with an immediate value of 1 to perform a decrement that
  326. affects carry.
  327.  
  328.  
  329. 3.2.2  Comparison and Sign Change Instruction
  330.  
  331. CMP (Compare) subtracts the source operand from the destination operand. It
  332. updates OF, SF, ZF, AF, PF, and CF but does not alter the source and
  333. destination operands. A subsequent Jcc or SETcc instruction can test the
  334. appropriate flags.
  335.  
  336. NEG (Negate) subtracts a signed integer operand from zero. The effect of
  337. NEG is to reverse the sign of the operand from positive to negative or from
  338. negative to positive.
  339.  
  340.  
  341. 3.2.3  Multiplication Instructions
  342.  
  343. The 80386 has separate multiply instructions for unsigned and signed
  344. operands. MUL operates on unsigned numbers, while IMUL operates on signed
  345. integers as well as unsigned.
  346.  
  347. MUL (Unsigned Integer Multiply) performs an unsigned multiplication of the
  348. source operand and the accumulator. If the source is a byte, the processor
  349. multiplies it by the contents of AL and returns the double-length result to
  350. AH and AL. If the source operand is a word, the processor multiplies it by
  351. the contents of AX and returns the double-length result to DX and AX. If the
  352. source operand is a doubleword, the processor multiplies it by the contents
  353. of EAX and returns the 64-bit result in EDX and EAX. MUL sets CF and OF
  354. when the upper half of the result is nonzero; otherwise, they are cleared.
  355.  
  356. IMUL (Signed Integer Multiply) performs a signed multiplication operation.
  357. IMUL has three variations:
  358.  
  359.   1.  A one-operand form. The operand may be a byte, word, or doubleword
  360.       located in memory or in a general register. This instruction uses EAX
  361.       and EDX as implicit operands in the same way as the MUL instruction.
  362.  
  363.   2.  A two-operand form. One of the source operands may be in any general
  364.       register while the other may be either in memory or in a general
  365.       register. The product replaces the general-register operand.
  366.  
  367.   3.  A three-operand form; two are source and one is the destination
  368.       operand. One of the source operands is an immediate value stored in
  369.       the instruction; the second may be in memory or in any general
  370.       register. The product may be stored in any general register. The
  371.       immediate operand is treated as signed. If the immediate operand is a
  372.       byte, the processor automatically sign-extends it to the size of the
  373.       second operand before performing the multiplication.
  374.  
  375. The three forms are similar in most respects:
  376.  
  377.   ■  The length of the product is calculated to twice the length of the
  378.      operands.
  379.  
  380.   ■  The CF and OF flags are set when significant bits are carried into the
  381.      high-order half of the result. CF and OF are cleared when the
  382.      high-order half of the result is the sign-extension of the low-order
  383.      half.
  384.  
  385. However, forms 2 and 3 differ in that the product is truncated to the
  386. length of the operands before it is stored in the destination register.
  387. Because of this truncation, OF should be tested to ensure that no
  388. significant bits are lost. (For ways to test OF, refer to the INTO and PUSHF
  389. instructions.)
  390.  
  391. Forms 2 and 3 of IMUL may also be used with unsigned operands because,
  392. whether the operands are signed or unsigned, the low-order half of the
  393. product is the same.
  394.  
  395.  
  396. 3.2.4  Division Instructions
  397.  
  398. The 80386 has separate division instructions for unsigned and signed
  399. operands. DIV operates on unsigned numbers, while IDIV operates on signed
  400. integers as well as unsigned. In either case, an exception (interrupt zero)
  401. occurs if the divisor is zero or if the quotient is too large for AL, AX, or
  402. EAX.
  403.  
  404. DIV (Unsigned Integer Divide) performs an unsigned division of the
  405. accumulator by the source operand. The dividend (the accumulator) is twice
  406. the size of the divisor (the source operand); the quotient and remainder
  407. have the same size as the divisor, as the following table shows.
  408.  
  409. Size of Source Operand
  410.       (divisor)             Dividend       Quotient      Remainder
  411.  
  412. Byte                        AX             AL            AH
  413. Word                        DX:AX          AX            DX
  414. Doubleword                  EDX:EAX        EAX           EDX
  415.  
  416. Non-integral quotients are truncated to integers toward 0. The remainder is
  417. always less than the divisor. For unsigned byte division, the largest
  418. quotient is 255. For unsigned word division, the largest quotient is 65,535.
  419. For unsigned doubleword division the largest quotient is 2^(32) -1.
  420.  
  421. IDIV (Signed Integer Divide) performs a signed division of the accumulator
  422. by the source operand. IDIV uses the same registers as the DIV instruction.
  423.  
  424. For signed byte division, the maximum positive quotient is +127, and the
  425. minimum negative quotient is -128. For signed word division, the maximum
  426. positive quotient is +32,767, and the minimum negative quotient is -32,768.
  427. For signed doubleword division the maximum positive quotient is 2^(31) -1,
  428. the minimum negative quotient is -2^(31). Non-integral results are truncated
  429. towards 0. The remainder always has the same sign as the dividend and is
  430. less than the divisor in magnitude.
  431.  
  432.  
  433. 3.3  Decimal Arithmetic Instructions
  434.  
  435. Decimal arithmetic is performed by combining the binary arithmetic
  436. instructions (already discussed in the prior section) with the decimal
  437. arithmetic instructions. The decimal arithmetic instructions are used in one
  438. of the following ways:
  439.  
  440.   ■  To adjust the results of a previous binary arithmetic operation to
  441.      produce a valid packed or unpacked decimal result.
  442.  
  443.   ■  To adjust the inputs to a subsequent binary arithmetic operation so
  444.      that the operation will produce a valid packed or unpacked decimal
  445.      result.
  446.  
  447. These instructions operate only on the AL or AH registers. Most utilize the
  448. AF flag.
  449.  
  450.  
  451. 3.3.1  Packed BCD Adjustment Instructions
  452.  
  453. DAA (Decimal Adjust after Addition) adjusts the result of adding two valid
  454. packed decimal operands in AL. DAA must always follow the addition of two
  455. pairs of packed decimal numbers (one digit in each half-byte) to obtain a
  456. pair of valid packed decimal digits as results. The carry flag is set if
  457. carry was needed.
  458.  
  459. DAS (Decimal Adjust after Subtraction) adjusts the result of subtracting
  460. two valid packed decimal operands in AL. DAS must always follow the
  461. subtraction of one pair of packed decimal numbers (one digit in each half-
  462. byte) from another to obtain a pair of valid packed decimal digits as
  463. results. The carry flag is set if a borrow was needed.
  464.  
  465.  
  466. 3.3.2  Unpacked BCD Adjustment Instructions
  467.  
  468. AAA (ASCII Adjust after Addition) changes the contents of register AL to a
  469. valid unpacked decimal number, and zeros the top 4 bits. AAA must always
  470. follow the addition of two unpacked decimal operands in AL. The carry flag
  471. is set and AH is incremented if a carry is necessary.
  472.  
  473. AAS (ASCII Adjust after Subtraction) changes the contents of register AL to
  474. a valid unpacked decimal number, and zeros the top 4 bits. AAS must always
  475. follow the subtraction of one unpacked decimal operand from another in AL.
  476. The carry flag is set and AH decremented if a borrow is necessary.
  477.  
  478. AAM (ASCII Adjust after Multiplication) corrects the result of a
  479. multiplication of two valid unpacked decimal numbers. AAM must always follow
  480. the multiplication of two decimal numbers to produce a valid decimal result.
  481. The high order digit is left in AH, the low order digit in AL.
  482.  
  483. AAD (ASCII Adjust before Division) modifies the numerator in AH and AL to
  484. prepare for the division of two valid unpacked decimal operands so that the
  485. quotient produced by the division will be a valid unpacked decimal number.
  486. AH should contain the high-order digit and AL the low-order digit. This
  487. instruction adjusts the value and places the result in AL. AH will contain
  488. zero.
  489.  
  490.  
  491. 3.4  Logical Instructions
  492.  
  493. The group of logical instructions includes:
  494.  
  495.   ■  The Boolean operation instructions
  496.   ■  Bit test and modify instructions
  497.   ■  Bit scan instructions
  498.   ■  Rotate and shift instructions
  499.   ■  Byte set on condition
  500.  
  501.  
  502. 3.4.1  Boolean Operation Instructions
  503.  
  504. The logical operations are AND, OR, XOR, and NOT.
  505.  
  506. NOT (Not) inverts the bits in the specified operand to form a one's
  507. complement of the operand. The NOT instruction is a unary operation that
  508. uses a single operand in a register or memory. NOT has no effect on the
  509. flags.
  510.  
  511. The AND, OR, and XOR instructions perform the standard logical operations
  512. "and", "(inclusive) or", and "exclusive or". These instructions can use the
  513. following combinations of operands:
  514.  
  515.   ■  Two register operands
  516.  
  517.   ■  A general register operand with a memory operand
  518.  
  519.   ■  An immediate operand with either a general register operand or a
  520.      memory operand.
  521.  
  522. AND, OR, and XOR clear OF and CF, leave AF undefined, and update SF, ZF,
  523. and PF.
  524.  
  525.  
  526. 3.4.2  Bit Test and Modify Instructions
  527.  
  528. This group of instructions operates on a single bit which can be in memory
  529. or in a general register. The location of the bit is specified as an offset
  530. from the low-order end of the operand. The value of the offset either may be
  531. given by an immediate byte in the instruction or may be contained in a
  532. general register.
  533.  
  534. These instructions first assign the value of the selected bit to CF, the
  535. carry flag. Then a new value is assigned to the selected bit, as determined
  536. by the operation. OF, SF, ZF, AF, PF are left in an undefined state. Table
  537. 3-1 defines these instructions.
  538.  
  539.  
  540. Table 3-1. Bit Test and Modify Instructions
  541.  
  542. Instruction                      Effect on CF            Effect on
  543.                                                          Selected Bit
  544.  
  545. Bit (Bit Test)                   CF  BIT                (none)
  546. BTS (Bit Test and Set)           CF  BIT                BIT  1
  547. BTR (Bit Test and Reset)         CF  BIT                BIT  0
  548. BTC (Bit Test and Complement)    CF  BIT                BIT  NOT(BIT)
  549.  
  550.  
  551. 3.4.3  Bit Scan Instructions
  552.  
  553. These instructions scan a word or doubleword for a one-bit and store the
  554. index of the first set bit into a register.  The bit string being scanned
  555. may be either in a register or in memory. The ZF flag is set if the entire
  556. word is zero (no set bits are found); ZF is cleared if a one-bit is found.
  557. If no set bit is found, the value of the destination register is undefined.
  558.  
  559. BSF (Bit Scan Forward) scans from low-order to high-order (starting from
  560. bit index zero).
  561.  
  562. BSR (Bit Scan Reverse) scans from high-order to low-order (starting from
  563. bit index 15 of a word or index 31 of a doubleword).
  564.  
  565.  
  566. 3.4.4  Shift and Rotate Instructions
  567.  
  568. The shift and rotate instructions reposition the bits within the specified
  569. operand.
  570.  
  571. These instructions fall into the following classes:
  572.  
  573.   ■  Shift instructions
  574.   ■  Double shift instructions
  575.   ■  Rotate instructions
  576.  
  577.  
  578. 3.4.4.1  Shift Instructions
  579.  
  580. The bits in bytes, words, and doublewords may be shifted arithmetically or
  581. logically. Depending on the value of a specified count, bits can be shifted
  582. up to 31 places.
  583.  
  584. A shift instruction can specify the count in one of three ways. One form of
  585. shift instruction implicitly specifies the count as a single shift. The
  586. second form specifies the count as an immediate value. The third form
  587. specifies the count as the value contained in CL. This last form allows the
  588. shift count to be a variable that the program supplies during execution.
  589. Only the low order 5 bits of CL are used.
  590.  
  591. CF always contains the value of the last bit shifted out of the destination
  592. operand. In a single-bit shift, OF is set if the value of the high-order
  593. (sign) bit was changed by the operation. Otherwise, OF is cleared. Following
  594. a multibit shift, however, the content of OF is always undefined.
  595.  
  596. The shift instructions provide a convenient way to accomplish division or
  597. multiplication by binary power. Note however that division of signed numbers
  598. by shifting right is not the same kind of division performed by the IDIV
  599. instruction.
  600.  
  601. SAL (Shift Arithmetic Left) shifts the destination byte, word, or
  602. doubleword operand left by one or by the number of bits specified in the
  603. count operand (an immediate value or the value contained in CL). The
  604. processor shifts zeros in from the right (low-order) side of the operand as
  605. bits exit from the left (high-order) side. See Figure 3-6.
  606.  
  607. SHL (Shift Logical Left) is a synonym for SAL (refer to SAL).
  608.  
  609. SHR (Shift Logical Right) shifts the destination byte, word, or doubleword
  610. operand right by one or by the number of bits specified in the count operand
  611. (an immediate value or the value contained in CL). The processor shifts
  612. zeros in from the left side of the operand as bits exit from the right side.
  613. See Figure 3-7.
  614.  
  615. SAR (Shift Arithmetic Right) shifts the destination byte, word, or
  616. doubleword operand to the right by one or by the number of bits specified in
  617. the count operand (an immediate value or the value contained in CL). The
  618. processor preserves the sign of the operand by shifting in zeros on the left
  619. (high-order) side if the value is positive or by shifting by ones if the
  620. value is negative. See Figure 3-8.
  621.  
  622. Even though this instruction can be used to divide integers by a power of
  623. two, the type of division is not the same as that produced by the IDIV
  624. instruction. The quotient of IDIV is rounded toward zero, whereas the
  625. "quotient" of SAR is rounded toward negative infinity. This difference is
  626. apparent only for negative numbers. For example, when IDIV is used to divide
  627. -9 by 4, the result is -2 with a remainder of -1. If SAR is used to shift
  628. -9 right by two bits, the result is -3. The "remainder" of this kind of
  629. division is +3; however, the SAR instruction stores only the high-order bit
  630. of the remainder (in CF).
  631.  
  632. The code sequence in Figure 3-9 produces the same result as IDIV for any M
  633. = 2^(N), where 0 < N < 32. This sequence takes about 12 to 18 clocks,
  634. depending on whether the jump is taken; if ECX contains M, the corresponding
  635. IDIV ECX instruction will take about 43 clocks.
  636.  
  637.  
  638. Figure 3-6.  SAL and SHL
  639.  
  640.                      OF   CF                OPERAND
  641.  
  642.        BEFORE SHL    X    X     10001000100010001000100010001111
  643.        OR SAL
  644.  
  645.        AFTER SHL     1    1 ── 00010001000100010001000100011110 ── 0
  646.        OR SAL BY 1
  647.  
  648.        AFTER SHL     X    0 ── 00100010001000100011110000000000 ── 0
  649.        OR SAL BY 10
  650.  
  651. SHL (WHICH HAS THE SYNONYM SAL) SHIFTS THE BITS IN THE REGISTER OR MEMORY
  652. OPERAND TO THE LEFT BY THE SPECIFIED NUMBER OF BIT POSITIONS. CF RECEIVES
  653. THE LAST BIT SHIFTED OUT OF THE LEFT OF THE OPERAND. SHL SHIFTS IN ZEROS
  654. TO FILL THE VACATED BIT LOCATIONS. THESE INSTRUCTIONS OPERATE ON BYTE,
  655. WORD, AND DOUBLEWORD OPERANDS.
  656.  
  657.  
  658. Figure 3-7.  SHR
  659.  
  660.                                       OPERAND                  CF
  661.  
  662.        BEFORE SHR         10001000100010001000100010001111     X
  663.  
  664.        AFTER SHR    0────01000100010001000100010001000111────1
  665.        BY 1
  666.  
  667.        AFTER SHR    0────00000000001000100010001000100010────O
  668.        BY 10
  669.  
  670. SHR SHIFTS THE BITS OF THE REGISTER OR MEMORY OPERAND TO THE RIGHT BY THE
  671. SPECIFIED NUMBER OF BIT POSITIONS. CF RECEIVES THE LAST BIT SHIFTED OUT OF
  672. THE RIGHT OF THE OPERAND. SHR SHIFTS IN ZEROS TO FILL THE VACATED BIT
  673. LOCATIONS.
  674.  
  675.  
  676. Figure 3-8.  SAR
  677.  
  678.                                   POSITIVE OPERAND             CF
  679.  
  680.        BEFORE SAR         01000100010001000100010001000111     X
  681.  
  682.        AFTER SAR    0────00100010001000100010001000100011────1
  683.        BY 1
  684.  
  685.                                   NEGATIVE OPERAND             CF
  686.  
  687.        BEFORE SAR         11000100010001000100010001000111     X
  688.  
  689.        AFTER SAR    0────11100010001000100010001000100011────1
  690.        BY 1
  691.  
  692. SAR PRESERVES THE SIGN OF THE REGISTER OR MEMORY OPERAND AS IT SHIFTS THE
  693. OPERAND TO THE RIGHT BY THE SPECIFIED NUMBER OF BIT POSITIONS. CF RECIEVES
  694. THE LAST BIT SHIFTED OUT OF THE RIGHT OF THE OPERAND.
  695.  
  696.  
  697. Figure 3-9.  Using SAR to Simulate IDIV
  698.  
  699.     ; assuming N is in ECX, and the dividend is in EAX
  700.     ;                                               CLOCKS
  701.     CMP     EAX, 0      ; to set sign flag          2
  702.     JGE     NoAdjust    ; jump if sign is zero      3 or 9
  703.     ADD     EAX, ECX    ;                           2
  704.     DEC     EAX         ; EAX := EAX + (N-1)        2
  705. NoAdjust:
  706.     SAR     EAX, CL     ;                           3
  707.     ;                       TOTAL CLOCKS           12 or 18]
  708.  
  709.  
  710. 3.4.4.2  Double-Shift Instructions
  711.  
  712. These instructions provide the basic operations needed to implement
  713. operations on long unaligned bit strings. The double shifts operate either
  714. on word or doubleword operands, as follows:
  715.  
  716.   1.  Taking two word operands as input and producing a one-word output.
  717.  
  718.   2.  Taking two doubleword operands as input and producing a doubleword
  719.       output.
  720.  
  721. Of the two input operands, one may either be in a general register or in
  722. memory, while the other may only be in a general register. The results
  723. replace the memory or register operand. The number of bits to be shifted is
  724. specified either in the CL register or in an immediate byte of the
  725. instruction.
  726.  
  727. Bits are shifted from the register operand into the memory or register
  728. operand. CF is set to the value of the last bit shifted out of the
  729. destination operand. SF, ZF, and PF are set according to the value of the
  730. result. OF and AF are left undefined.
  731.  
  732. SHLD (Shift Left Double) shifts bits of the R/M field to the left, while
  733. shifting high-order bits from the Reg field into the R/M field on the right
  734. (see Figure 3-10). The result is stored back into the R/M operand. The Reg
  735. field is not modified.
  736.  
  737. SHRD (Shift Right Double) shifts bits of the R/M field to the right, while
  738. shifting low-order bits from the Reg field into the R/M field on the left
  739. (see Figure 3-11). The result is stored back into the R/M operand. The Reg
  740. field is not modified.
  741.  
  742.  
  743. 3.4.4.3  Rotate Instructions
  744.  
  745. Rotate instructions allow bits in bytes, words, and doublewords to be
  746. rotated. Bits rotated out of an operand are not lost as in a shift, but are
  747. "circled" back into the other "end" of the operand.
  748.  
  749. Rotates affect only the carry and overflow flags. CF may act as an
  750. extension of the operand in two of the rotate instructions, allowing a bit
  751. to be isolated and then tested by a conditional jump instruction (JC or
  752. JNC). CF always contains the value of the last bit rotated out, even if the
  753. instruction does not use this bit as an extension of the rotated operand.
  754.  
  755. In single-bit rotates, OF is set if the operation changes the high-order
  756. (sign) bit of the destination operand. If the sign bit retains its original
  757. value, OF is cleared. On multibit rotates, the value of OF is always
  758. undefined.
  759.  
  760. ROL (Rotate Left) rotates the byte, word, or doubleword destination operand
  761. left by one or by the number of bits specified in the count operand (an
  762. immediate value or the value contained in CL). For each rotation specified,
  763. the high-order bit that exits from the left of the operand returns at the
  764. right to become the new low-order bit of the operand. See Figure 3-12.
  765.  
  766. ROR (Rotate Right) rotates the byte, word, or doubleword destination
  767. operand right by one or by the number of bits specified in the count operand
  768. (an immediate value or the value contained in CL). For each rotation
  769. specified, the low-order bit that exits from the right of the operand
  770. returns at the left to become the new high-order bit of the operand.
  771. See Figure 3-13.
  772.  
  773. RCL (Rotate Through Carry Left) rotates bits in the byte, word, or
  774. doubleword destination operand left by one or by the number of bits
  775. specified in the count operand (an immediate value or the value contained in
  776. CL).
  777.  
  778. This instruction differs from ROL in that it treats CF as a high-order
  779. one-bit extension of the destination operand. Each high-order bit that exits
  780. from the left side of the operand moves to CF before it returns to the
  781. operand as the low-order bit on the next rotation cycle. See Figure 3-14.
  782.  
  783. RCR (Rotate Through Carry Right) rotates bits in the byte, word, or
  784. doubleword destination operand right by one or by the number of bits
  785. specified in the count operand (an immediate value or the value contained in
  786. CL).
  787.  
  788. This instruction differs from ROR in that it treats CF as a low-order
  789. one-bit extension of the destination operand. Each low-order bit that exits
  790. from the right side of the operand moves to CF before it returns to the
  791. operand as the high-order bit on the next rotation cycle. See Figure 3-15.
  792.  
  793.  
  794. Figure 3-10.  Shift Left Double
  795.  
  796.                   31                   DESTINATION                 0
  797.     ╔════╗       ╔══════════════════════════════════════════════════╗
  798.     ║ CF ║──────╢                 MEMORY OF REGISTER               ║───┐
  799.     ╚════╝       ╚══════════════════════════════════════════════════╝    │
  800.              ┌───────────────────────────────────────────────────────────┘
  801.              │    31                     SOURCE                    0
  802.              │   ╔══════════════════════════════════════════════════╗
  803.              └───╢                      REGISTER                    ║
  804.                  ╚══════════════════════════════════════════════════╝
  805.  
  806.  
  807. Figure 3-11.  Shift Right Double
  808.  
  809.         31                     SOURCE                    0
  810.        ╔══════════════════════════════════════════════════╗
  811.        ║                      REGISTER                    ╟───┐
  812.        ╚══════════════════════════════════════════════════╝   │
  813.    ┌──────────────────────────────────────────────────────────┘
  814.    │    31                   DESTINATION                 0
  815.    │   ╔══════════════════════════════════════════════════╗        ╔════╗
  816.    └──║                 MEMORY OF REGISTER               ╟───────║ CF ║
  817.        ╚══════════════════════════════════════════════════╝        ╚════╝
  818.  
  819.  
  820. Figure 3-12.  ROL
  821.  
  822.                   31                   DESTINATION                 0
  823.     ╔════╗       ╔══════════════════════════════════════════════════╗
  824.     ║ CF ║───┬──╢                 MEMORY OF REGISTER               ║──┐
  825.     ╚════╝    │  ╚══════════════════════════════════════════════════╝   │
  826.               └─────────────────────────────────────────────────────────┘
  827.  
  828.  
  829. Figure 3-13.  ROR
  830.  
  831.      ┌──────────────────────────────────────────────────────────┐
  832.      │    31                   DESTINATION                 0    │
  833.      │   ╔══════════════════════════════════════════════════╗   │    ╔════╗
  834.      └──║                 MEMORY OF REGISTER               ╟───┴───║ CF ║
  835.          ╚══════════════════════════════════════════════════╝        ╚════╝
  836.  
  837.  
  838. Figure 3-14.  RCL
  839.  
  840.                   31                   DESTINATION                 0
  841.     ╔════╗       ╔══════════════════════════════════════════════════╗
  842.   ┌─╢ CF ║──────╢                 MEMORY OF REGISTER               ║──┐
  843.   │ ╚════╝       ╚══════════════════════════════════════════════════╝   │
  844.   └─────────────────────────────────────────────────────────────────────┘
  845.  
  846.  
  847. Figure 3-15.  RCR
  848.  
  849.    ┌──────────────────────────────────────────────────────────────────────┐
  850.    │    31                   DESTINATION                 0                │
  851.    │   ╔══════════════════════════════════════════════════╗        ╔════╗ │
  852.    └──║                 MEMORY OF REGISTER               ╟───────║ CF ╟─┘
  853.        ╚══════════════════════════════════════════════════╝        ╚════╝
  854.  
  855.  
  856. 3.4.4.4  Fast "BIT BLT" Using Double Shift Instructions
  857.  
  858. One purpose of the double shifts is to implement a bit string move, with
  859. arbitrary misalignment of the bit strings.  This is called a "bit blt" (BIT
  860. BLock Transfer.)  A simple example is to move a bit string from an arbitrary
  861. offset into a doubleword-aligned byte string.  A left-to-right string is
  862. moved 32 bits at a time if a double shift is used inside the move loop.
  863.  
  864.      MOV   ESI,ScrAddr
  865.      MOV   EDI,DestAddr
  866.      MOV   EBX,WordCnt
  867.      MOV   CL,RelOffset      ; relative offset Dest-Src
  868.      MOV   EDX,[ESI]         ; load first word of source
  869.      ADD   ESI,4             ; bump source address
  870. BltLoop:
  871.      LODS                    ; new low order part
  872.      SHLD  EDX,EAX,CL        ; EDX overwritten with aligned stuff
  873.      XCHG  EDX,EAS           ; Swap high/low order parts
  874.      STOS                    ; Write out next aligned chunk
  875.      DEC   EBX
  876.      JA    BltLoop
  877.  
  878. This loop is simple yet allows the data to be moved in 32-bit pieces for
  879. the highest possible performance. Without a double shift, the best that can
  880. be achieved is 16 bits per loop iteration by using a 32-bit shift and
  881. replacing the XCHG with a ROR by 16 to swap high and low order parts of
  882. registers. A more general loop than shown above would require some extra
  883. masking on the first doubleword moved (before the main loop), and on the
  884. last doubleword moved (after the main loop), but would have the same basic
  885. 32-bits per loop iteration as the code above.
  886.  
  887.  
  888. 3.4.4.5  Fast Bit-String Insert and Extract
  889.  
  890. The double shift instructions also enable:
  891.  
  892.   ■  Fast insertion of a bit string from a register into an arbitrary bit
  893.      location in a larger bit string in memory without disturbing the bits
  894.      on either side of the inserted bits.
  895.  
  896.   ■  Fast extraction of a bits string into a register from an arbitrary bit
  897.      location in a larger bit string in memory without disturbing the bits
  898.      on either side of the extracted bits.
  899.  
  900. The following coded examples illustrate bit insertion and extraction under
  901. variousconditions:
  902.  
  903.   1.  Bit String Insert into Memory (when bit string is 1-25 bits long,
  904.       i.e., spans four bytes or less):
  905.  
  906.       ; Insert a right-justified bit string from register into
  907.       ; memory bit string.
  908.       ;
  909.       ; Assumptions:
  910.       ; 1) The base of the string array is dword aligned, and
  911.       ; 2) the length of the bit string is an immediate value
  912.       ;    but the bit offset is held in a register.
  913.       ;
  914.       ; Register ESI holds the right-justified bit string
  915.       ; to be inserted.
  916.       ; Register EDI holds the bit offset of the start of the
  917.       ; substring.
  918.       ; Registers EAX and ECX are also used by this
  919.       ; "insert" operation.
  920.       ;
  921.       MOV   ECX,EDI      ; preserve original offset for later use
  922.       SHR   EDI,3        ; signed divide offset by 8 (byte address)
  923.       AND   CL,7H        ; isolate low three bits of offset in CL
  924.       MOV   EAX,[EDI]strg_base      ; move string dword into EAX
  925.       ROR   EAX,CL       ; right justify old bit field
  926.       SHRD  EAX,ESI,length          ; bring in new bits
  927.       ROL   EAX,length   ; right justify new bit field
  928.       ROL   EAX,CL       ; bring to final position
  929.       MOV   [EDI]strg_base,EAX      ; replace dword in memory
  930.  
  931.   2.  Bit String Insert into Memory (when bit string is 1-31 bits long, i.e.
  932.       spans five bytes or less):
  933.  
  934.       ; Insert a right-justified bit string from register into
  935.       ; memory bit string.
  936.       ;
  937.       ; Assumptions:
  938.       ; 1) The base of the string array is dword aligned, and
  939.       ; 2) the length of the bit string is an immediate value
  940.       ;    but the bit offset is held in a register.
  941.       ;
  942.       ; Register ESI holds the right-justified bit string
  943.       ; to be inserted.
  944.       ; Register EDI holds the bit offset of the start of the
  945.       ; substring.
  946.       ; Registers EAX, EBX, ECX, and EDI are also used by
  947.       ; this "insert" operation.
  948.       ;
  949.       MOV   ECX,EDI     ; temp storage for offset
  950.       SHR   EDI,5       ; signed divide offset by 32 (dword address)
  951.       SHL   EDI,2       ; multiply by 4 (in byte address format)
  952.       AND   CL,1FH      ; isolate low five bits of offset in CL
  953.       MOV   EAX,[EDI]strg_base      ; move low string dword into EAX
  954.       MOV   EDX,[EDI]strg_base+4    ; other string dword into EDX
  955.       MOV   EBX,EAX     ; temp storage for part of string     ┐ rotate
  956.       SHRD  EAX,EDX,CL  ; double shift by offset within dword ├ EDX:EAX
  957.       SHRD  EAX,EBX,CL  ; double shift by offset within dword ┘ right
  958.       SHRD  EAX,ESI,length          ; bring in new bits
  959.       ROL   EAX,length  ; right justify new bit field
  960.       MOV   EBX,EAX     ; temp storage for part of string         ┐ rotate
  961.       SHLD  EAX,EDX,CL  ; double shift back by offset within word ├ EDX:EAX
  962.       SHLD  EDX,EBX,CL  ; double shift back by offset within word ┘ left
  963.       MOV   [EDI]strg_base,EAX      ; replace dword in memory
  964.       MOV   [EDI]strg_base+4,EDX    ; replace dword in memory
  965.  
  966.   3.  Bit String Insert into Memory (when bit string is exactly 32 bits
  967.       long, i.e., spans five or four types of memory):
  968.  
  969.       ; Insert right-justified bit string from register into
  970.       ; memory bit string.
  971.       ;
  972.       ; Assumptions:
  973.       ; 1) The base of the string array is dword aligned, and
  974.       ; 2) the length of the bit string is 32
  975.       ;    but the bit offset is held in a register.
  976.       ;
  977.       ; Register ESI holds the 32-bit string to be inserted.
  978.       ; Register EDI holds the bit offset of the start of the
  979.       ; substring.
  980.       ; Registers EAX, EBX, ECX, and EDI are also used by
  981.       ; this "insert" operation.
  982.       ;
  983.       MOV   EDX,EDI     ; preserve original offset for later use
  984.       SHR   EDI,5       ; signed divide offset by 32 (dword address)
  985.       SHL   EDI,2       ; multiply by 4 (in byte address format)
  986.       AND   CL,1FH      ; isolate low five bits of offset in CL
  987.       MOV   EAX,[EDI]strg_base      ; move low string dword into EAX
  988.       MOV   EDX,[EDI]strg_base+4    ; other string dword into EDX
  989.       MOV   EBX,EAX     ; temp storage for part of string     ┐ rotate
  990.       SHRD  EAX,EDX     ; double shift by offset within dword ├ EDX:EAX
  991.       SHRD  EDX,EBX     ; double shift by offset within dword ┘ right
  992.       MOV   EAX,ESI     ; move 32-bit bit field into position
  993.       MOV   EBX,EAX     ; temp storage for part of string         ┐ rotate
  994.       SHLD  EAX,EDX     ; double shift back by offset within word ├ EDX:EAX
  995.       SHLD  EDX,EBX     ; double shift back by offset within word ┘ left
  996.       MOV   [EDI]strg_base,EAX      ; replace dword in memory
  997.       MOV   [EDI]strg_base,+4,EDX   ; replace dword in memory
  998.  
  999.   4.  Bit String Extract from Memory (when bit string is 1-25 bits long,
  1000.       i.e., spans four bytes or less):
  1001.  
  1002.       ; Extract a right-justified bit string from memory bit
  1003.       ; string into register
  1004.       ;
  1005.       ; Assumptions:
  1006.       ; 1) The base of the string array is dword aligned, and
  1007.       ; 2) the length of the bit string is an immediate value
  1008.       ;    but the bit offset is held in a register.
  1009.       ;
  1010.       ; Register EAX holds the right-justified, zero-padded
  1011.       ; bit string that was extracted.
  1012.       ; Register EDI holds the bit offset of the start of the
  1013.       ; substring.
  1014.       ; Registers EDI, and ECX are also used by this "extract."
  1015.       ;
  1016.       MOV  ECX,EDI      ; temp storage for offset
  1017.       SHR  EDI,3        ; signed divide offset by 8 (byte address)
  1018.       AND  CL,7H        ; isolate low three bits of offset
  1019.       MOV  EAX,[EDI]strg_base       ; move string dword into EAX
  1020.       SHR  EAX,CL       ; shift by offset within dword
  1021.       AND  EAX,mask     ; extracted bit field in EAX
  1022.  
  1023.   5.  Bit String Extract from Memory (when bit string is 1-32 bits long, 
  1024.       i.e., spans five bytes or less):
  1025.  
  1026.       ; Extract a right-justified bit string from memory bit
  1027.       ; string into register.
  1028.       ;
  1029.       ; Assumptions:
  1030.       ; 1) The base of the string array is dword aligned, and
  1031.       ; 2) the length of the bit string is an immediate
  1032.       ;    value but the bit offset is held in a register.
  1033.       ;
  1034.       ; Register EAX holds the right-justified, zero-padded
  1035.       ; bit string that was extracted.
  1036.       ; Register EDI holds the bit offset of the start of the
  1037.       ; substring.
  1038.       ; Registers EAX, EBX, and ECX are also used by this "extract."
  1039.       MOV   ECX,EDI     ; temp storage for offset
  1040.       SHR   EDI,5       ; signed divide offset by 32 (dword address)
  1041.       SHL   EDI,2       ; multiply by 4 (in byte address format)
  1042.       AND   CL,1FH      ; isolate low five bits of offset in CL
  1043.       MOV   EAX,[EDI]strg_base      ; move low string dword into EAX
  1044.       MOV   EDX,[EDI]strg_base+4    ; other string dword into EDX
  1045.       SHRD  EAX,EDX,CL  ; double shift right by offset within dword
  1046.       AND   EAX,mask    ; extracted bit field in EAX
  1047.  
  1048.  
  1049. 3.4.5  Byte-Set-On-Condition Instructions
  1050.  
  1051. This group of instructions sets a byte to zero or one depending on any of
  1052. the 16 conditions defined by the status flags. The byte may be in memory or
  1053. may be a one-byte general register. These instructions are especially useful
  1054. for implementing Boolean expressions in high-level languages such as Pascal.
  1055.  
  1056. SETcc (Set Byte on Condition cc) set a byte to one if condition cc is true;
  1057. sets the byte to zero otherwise. Refer to Appendix D for a definition of
  1058. the possible conditions.
  1059.  
  1060.  
  1061. 3.4.6  Test Instruction
  1062.  
  1063. TEST (Test) performs the logical "and" of the two operands, clears OF and
  1064. CF, leaves AF undefined, and updates SF, ZF, and PF. The flags can be tested
  1065. by conditional control transfer instructions or by the byte-set-on-condition
  1066. instructions. The operands may be doublewords, words, or bytes.
  1067.  
  1068. The difference between TEST and AND is that TEST does not alter the
  1069. destination operand. TEST differs from BT in that TEST is useful for testing
  1070. the value of multiple bits in one operations, whereas BT tests a single bit.
  1071.  
  1072.  
  1073. 3.5  Control Transfer Instructions
  1074.  
  1075. The 80386 provides both conditional and unconditional control transfer
  1076. instructions to direct the flow of execution. Conditional control transfers
  1077. depend on the results of operations that affect the flag register.
  1078. Unconditional control transfers are always executed.
  1079.  
  1080.  
  1081. 3.5.1  Unconditional Transfer Instructions
  1082.  
  1083. JMP, CALL, RET, INT and IRET instructions transfer control from one code
  1084. segment location to another. These locations can be within the same code
  1085. segment (near control transfers) or in different code segments (far control
  1086. transfers). The variants of these instructions that transfer control to
  1087. other segments are discussed in a later section of this chapter. If the
  1088. model of memory organization used in a particular 80386 application does
  1089. not make segments visible to applications programmers, intersegment control
  1090. transfers will not be used.
  1091.  
  1092.  
  1093. 3.5.1.1  Jump Instruction
  1094.  
  1095. JMP (Jump) unconditionally transfers control to the target location. JMP is
  1096. a one-way transfer of execution; it does not save a return address on the
  1097. stack.
  1098.  
  1099. The JMP instruction always performs the same basic function of transferring
  1100. control from the current location to a new location. Its implementation
  1101. varies depending on whether the address is specified directly within the
  1102. instruction or indirectly through a register or memory.
  1103.  
  1104. A direct JMP instruction includes the destination address as part of the
  1105. instruction. An indirect JMP instruction obtains the destination address
  1106. indirectly through a register or a pointer variable.
  1107.  
  1108. Direct near JMP. A direct JMP uses a relative displacement value contained
  1109. in the instruction. The displacement is signed and the size of the
  1110. displacement may be a byte, word, or doubleword. The processor forms an
  1111. effective address by adding this relative displacement to the address
  1112. contained in EIP. When the additions have been performed, EIP refers to the
  1113. next instruction to be executed.
  1114.  
  1115. Indirect near JMP. Indirect JMP instructions specify an absolute address in
  1116. one of several ways:
  1117.  
  1118.   1.  The program can JMP to a location specified by a general register
  1119.       (any of EAX, EDX, ECX, EBX, EBP, ESI, or EDI). The processor moves
  1120.       this 32-bit value into EIP and resumes execution.
  1121.  
  1122.   2.  The processor can obtain the destination address from a memory
  1123.       operand specified in the instruction.
  1124.  
  1125.   3.  A register can modify the address of the memory pointer to select a
  1126.       destination address.
  1127.  
  1128.  
  1129. 3.5.1.2  Call Instruction
  1130.  
  1131. CALL (Call Procedure) activates an out-of-line procedure, saving on the
  1132. stack the address of the instruction following the CALL for later use by a
  1133. RET (Return) instruction. CALL places the current value of EIP on the stack.
  1134. The RET instruction in the called procedure uses this address to transfer
  1135. control back to the calling program.
  1136.  
  1137. CALL instructions, like JMP instructions have relative, direct, and
  1138. indirect versions.
  1139.  
  1140. Indirect CALL instructions specify an absolute address in one of these
  1141. ways:
  1142.  
  1143.   1.  The program can CALL a location specified by a general register (any
  1144.       of EAX, EDX, ECX, EBX, EBP, ESI, or EDI). The processor moves this
  1145.       32-bit value into EIP.
  1146.  
  1147.   2.  The processor can obtain the destination address from a memory
  1148.       operand specified in the instruction.
  1149.  
  1150.  
  1151. 3.5.1.3  Return and Return-From-Interrupt Instruction
  1152.  
  1153. RET (Return From Procedure) terminates the execution of a procedure and
  1154. transfers control through a back-link on the stack to the program that
  1155. originally invoked the procedure. RET restores the value of EIP that was
  1156. saved on the stack by the previous CALL instruction.
  1157.  
  1158. RET instructions may optionally specify an immediate operand. By adding
  1159. this constant to the new top-of-stack pointer, RET effectively removes any
  1160. arguments that the calling program pushed on the stack before the execution
  1161. of the CALL instruction.
  1162.  
  1163. IRET (Return From Interrupt) returns control to an interrupted procedure.
  1164. IRET differs from RET in that it also pops the flags from the stack into the
  1165. flags register. The flags are stored on the stack by the interrupt
  1166. mechanism.
  1167.  
  1168.  
  1169. 3.5.2  Conditional Transfer Instructions
  1170.  
  1171. The conditional transfer instructions are jumps that may or may not
  1172. transfer control, depending on the state of the CPU flags when the
  1173. instruction executes.
  1174.  
  1175.  
  1176. 3.5.2.1  Conditional Jump Instructions
  1177.  
  1178. Table 3-2 shows the conditional transfer mnemonics and their
  1179. interpretations. The conditional jumps that are listed as pairs are actually
  1180. the same instruction. The assembler provides the alternate mnemonics for
  1181. greater clarity within a program listing.
  1182.  
  1183. Conditional jump instructions contain a displacement which is added to the
  1184. EIP register if the condition is true. The displacement may be a byte, a
  1185. word, or a doubleword. The displacement is signed; therefore, it can be used
  1186. to jump forward or backward.
  1187.  
  1188.  
  1189. Table 3-2. Interpretation of Conditional Transfers
  1190.  
  1191. Unsigned Conditional Transfers
  1192.  
  1193. Mnemonic         Condition Tested          "Jump If..."
  1194.  
  1195. JA/JNBE           (CF or ZF) = 0           above/not below nor equal
  1196. JAE/JNB           CF = 0                   above or equal/not below
  1197. JB/JNAE           CF = 1                   below/not above nor equal
  1198. JBE/JNA           (CF or ZF) = 1           below or equal/not above
  1199. JC                CF = 1                   carry
  1200. JE/JZ             ZF = 1                   equal/zero
  1201. JNC               CF = 0                   not carry
  1202. JNE/JNZ           ZF = 0                   not equal/not zero
  1203. JNP/JPO           PF = 0                   not parity/parity odd
  1204. JP/JPE            PF = 1                   parity/parity even
  1205.  
  1206. Signed Conditional Transfers
  1207.  
  1208. Mnemonic         Condition Tested          "Jump If..."
  1209. JG/JNLE          ((SF xor OF) or ZF) = 0   greater/not less nor equal
  1210. JGE/JNL          (SF xor OF) = 0           greater or equal/not less
  1211. JL/JNGE          (SF xor OF) = 1           less/not greater nor equal
  1212. JLE/JNG          ((SF xor OF) or ZF) = 1   less or equal/not greater
  1213. JNO              OF = 0                    not overflow
  1214. JNS              SF = 0                    not sign (positive, including 0)
  1215. JO               OF = 1                    overflow
  1216. JS               SF = 1                    sign (negative)
  1217.  
  1218.  
  1219. 3.5.2.2  Loop Instructions
  1220.  
  1221. The loop instructions are conditional jumps that use a value placed in ECX
  1222. to specify the number of repetitions of a software loop. All loop
  1223. instructions automatically decrement ECX and terminate the loop when ECX=0.
  1224. Four of the five loop instructions specify a condition involving ZF that
  1225. terminates the loop before ECX reaches zero.
  1226.  
  1227. LOOP (Loop While ECX Not Zero) is a conditional transfer that automatically
  1228. decrements the ECX register before testing ECX for the branch condition. If
  1229. ECX is non-zero, the program branches to the target label specified in the
  1230. instruction. The LOOP instruction causes the repetition of a code section
  1231. until the operation of the LOOP instruction decrements ECX to a value of
  1232. zero. If LOOP finds ECX=0, control transfers to the instruction immediately
  1233. following the LOOP instruction. If the value of ECX is initially zero, then
  1234. the LOOP executes 2^(32) times.
  1235.  
  1236. LOOPE (Loop While Equal) and LOOPZ (Loop While Zero) are synonyms for the
  1237. same instruction. These instructions automatically decrement the ECX
  1238. register before testing ECX and ZF for the branch conditions. If ECX is
  1239. non-zero and ZF=1, the program branches to the target label specified in the
  1240. instruction. If LOOPE or LOOPZ finds that ECX=0 or ZF=0, control transfers
  1241. to the instruction immediately following the LOOPE or LOOPZ instruction.
  1242.  
  1243. LOOPNE (Loop While Not Equal) and LOOPNZ (Loop While Not Zero) are synonyms
  1244. for the same instruction. These instructions automatically decrement the ECX
  1245. register before testing ECX and ZF for the branch conditions. If ECX is
  1246. non-zero and ZF=0, the program branches to the target label specified in the
  1247. instruction. If LOOPNE or LOOPNZ finds that ECX=0 or ZF=1, control transfers
  1248. to the instruction immediately following the LOOPNE or LOOPNZ instruction.
  1249.  
  1250.  
  1251. 3.5.2.3  Executing a Loop or Repeat Zero Times
  1252.  
  1253. JCXZ (Jump if ECX Zero) branches to the label specified in the instruction
  1254. if it finds a value of zero in ECX. JCXZ is useful in combination with the
  1255. LOOP instruction and with the string scan and compare instructions, all of
  1256. which decrement ECX. Sometimes, it is desirable to design a loop that
  1257. executes zero times if the count variable in ECX is initialized to zero.
  1258. Because the LOOP instructions (and repeat prefixes) decrement ECX before
  1259. they test it, a loop will execute 2^(32) times if the program enters the
  1260. loop with a zero value in ECX. A programmer may conveniently overcome this
  1261. problem with JCXZ, which enables the program to branch around the code
  1262. within the loop if ECX is zero when JCXZ executes. When used with repeated
  1263. string scan and compare instructions, JCXZ can determine whether the
  1264. repetitions terminated due to zero in ECX or due to satisfaction of the
  1265. scan or compare conditions.
  1266.  
  1267.  
  1268. 3.5.3  Software-Generated Interrupts
  1269.  
  1270. The INT n, INTO, and BOUND instructions allow the programmer to specify a
  1271. transfer to an interrupt service routine from within a program.
  1272.  
  1273. INT n (Software Interrupt) activates the interrupt service routine that
  1274. corresponds to the number coded within the instruction. The INT instruction
  1275. may specify any interrupt type. Programmers may use this flexibility to
  1276. implement multiple types of internal interrupts or to test the operation of
  1277. interrupt service routines. (Interrupts 0-31 are reserved by Intel.) The
  1278. interrupt service routine terminates with an IRET instruction that returns
  1279. control to the instruction that follows INT.
  1280.  
  1281. INTO (Interrupt on Overflow) invokes interrupt 4 if OF is set. Interrupt 4
  1282. is reserved for this purpose. OF is set by several arithmetic, logical, and
  1283. string instructions.
  1284.  
  1285. BOUND (Detect Value Out of Range) verifies that the signed value contained
  1286. in the specified register lies within specified limits. An interrupt (INT 5)
  1287. occurs if the value contained in the register is less than the lower bound
  1288. or greater than the upper bound.
  1289.  
  1290. The BOUND instruction includes two operands. The first operand specifies
  1291. the register being tested. The second operand contains the effective
  1292. relative address of the two signed BOUND limit values. The BOUND instruction
  1293. assumes that the upper limit and lower limit are in adjacent memory
  1294. locations. These limit values cannot be register operands; if they are, an
  1295. invalid opcode exception occurs.
  1296.  
  1297. BOUND is useful for checking array bounds before using a new index value to
  1298. access an element within the array. BOUND provides a simple way to check the
  1299. value of an index register before the program overwrites information in a
  1300. location beyond the limit of the array.
  1301.  
  1302. The block of memory that specifies the lower and upper limits of an array
  1303. might typically reside just before the array itself. This makes the array
  1304. bounds accessible at a constant offset from the beginning of the array.
  1305. Because the address of the array will already be present in a register, this
  1306. practice avoids extra calculations to obtain the effective address of the
  1307. array bounds.
  1308.  
  1309. The upper and lower limit values may each be a word or a doubleword.
  1310.  
  1311.  
  1312. 3.6  String and Character Translation Instructions
  1313.  
  1314. The instructions in this category operate on strings rather than on logical
  1315. or numeric values. Refer also to the section on I/O for information about
  1316. the string I/O instructions (also known as block I/O).
  1317.  
  1318. The power of 80386 string operations derives from the following features of
  1319. the architecture:
  1320.  
  1321. 1.  A set of primitive string operations
  1322.  
  1323.    MOVS   ── Move String
  1324.    CMPS   ── Compare string
  1325.    SCAS   ── Scan string
  1326.    LODS   ── Load string
  1327.    STOS   ── Store string
  1328.  
  1329. 2.  Indirect, indexed addressing, with automatic incrementing or
  1330.     decrementing of the indexes.
  1331.  
  1332.    Indexes:
  1333.  
  1334.       ESI    ── Source index register
  1335.       EDI    ── Destination index register
  1336.  
  1337.    Control flag:
  1338.  
  1339.       DF     ── Direction flag
  1340.  
  1341.    Control flag instructions:
  1342.  
  1343.       CLD    ── Clear direction flag instruction
  1344.       STD    ── Set direction flag instruction
  1345.  
  1346. 3.  Repeat prefixes
  1347.  
  1348.    REP          ── Repeat while ECX not xero
  1349.    REPE/REPZ    ── Repeat while equal or zero
  1350.    REPNE/REPNZ  ── Repeat while not equal or not zero
  1351.  
  1352. The primitive string operations operate on one element of a string. A
  1353. string element may be a byte, a word, or a doubleword. The string elements
  1354. are addressed by the registers ESI and EDI. After every primitive operation
  1355. ESI and/or EDI are automatically updated to point to the next element of the
  1356. string. If the direction flag is zero, the index registers are incremented;
  1357. if one, they are decremented. The amount of the increment or decrement is
  1358. 1, 2, or 4 depending on the size of the string element.
  1359.  
  1360.  
  1361. 3.6.1  Repeat Prefixes
  1362.  
  1363. The repeat prefixes REP (Repeat While ECX Not Zero), REPE/REPZ (Repeat
  1364. While Equal/Zero), and REPNE/REPNZ (Repeat While Not Equal/Not Zero) specify
  1365. repeated operation of a string primitive. This form of iteration allows the
  1366. CPU to process strings much faster than would be possible with a regular
  1367. software loop.
  1368.  
  1369. When a primitive string operation has a repeat prefix, the operation is
  1370. executed repeatedly, each time using a different element of the string. The
  1371. repetition terminates when one of the conditions specified by the prefix is
  1372. satisfied.
  1373.  
  1374. At each repetition of the primitive instruction, the string operation may
  1375. be suspended temporarily in order to handle an exception or external
  1376. interrupt. After the interruption, the string operation can be restarted
  1377. again where it left off. This method of handling strings allows operations
  1378. on strings of arbitrary length, without affecting interrupt response.
  1379.  
  1380. All three prefixes causes the hardware to automatically repeat the
  1381. associated string primitive until ECX=0. The differences among the repeat
  1382. prefixes have to do with the second termination condition. REPE/REPZ and
  1383. REPNE/REPNZ are used exclusively with the SCAS (Scan String) and CMPS
  1384. (Compare String) primitives. When these prefixes are used, repetition of the
  1385. next instruction depends on the zero flag (ZF) as well as the ECX register.
  1386. ZF does not require initialization before execution of a repeated string
  1387. instruction, because both SCAS and CMPS set ZF according to the results of
  1388. the comparisons they make. The differences are summarized in the
  1389. accompanying table.
  1390.  
  1391. Prefix                      Termination         Termination
  1392.                             Condition 1         Condition 2
  1393.  
  1394. REP                           ECX = 0             (none)
  1395. REPE/REPZ                     ECX = 0             ZF = 0
  1396. REPNE/REPNZ                   ECX = 0             ZF = 1
  1397.  
  1398.  
  1399. 3.6.2  Indexing and Direction Flag Control
  1400.  
  1401. The addresses of the operands of string primitives are determined by the
  1402. ESI and EDI registers. ESI points to source operands. By default, ESI refers
  1403. to a location in the segment indicated by the DS segment register. A
  1404. segment-override prefix may be used, however, to cause ESI to refer to CS,
  1405. SS, ES, FS, or GS. EDI points to destination operands in the segment
  1406. indicated by ES; no segment override is possible. The use of two different
  1407. segment registers in one instruction allows movement of strings between
  1408. different segments.
  1409.  
  1410. This use of ESI and DSI has led to the descriptive names source index and
  1411. destination index for the ESI and EDI registers, respectively. In all
  1412. cases other than string instructions, however, the ESI and EDI registers may
  1413. be used as general-purpose registers.
  1414.  
  1415. When ESI and EDI are used in string primitives, they are automatically
  1416. incremented or decremented after to operation. The direction flag determines
  1417. whether they are incremented or decremented. The instruction CLD puts zero
  1418. in DF, causing the index registers to be incremented; the instruction STD
  1419. puts one in DF, causing the index registers to be decremented. Programmers
  1420. should always put a known value in DF before using string instructions in a
  1421. procedure.
  1422.  
  1423.  
  1424. 3.6.3  String Instructions
  1425.  
  1426. MOVS (Move String) moves the string element pointed to by ESI to the
  1427. location pointed to by EDI. MOVSB operates on byte elements, MOVSW operates
  1428. on word elements, and MOVSD operates on doublewords. The destination segment
  1429. register cannot be overridden by a segment override prefix, but the source
  1430. segment register can be overridden.
  1431.  
  1432. The MOVS instruction, when accompanied by the REP prefix, operates as a
  1433. memory-to-memory block transfer. To set up for this operation, the program
  1434. must initialize ECX and the register pairs ESI and EDI. ECX specifies the
  1435. number of bytes, words, or doublewords in the block.
  1436.  
  1437. If DF=0, the program must point ESI to the first element of the source
  1438. string and point EDI to the destination address for the first element. If
  1439. DF=1, the program must point these two registers to the last element of the
  1440. source string and to the destination address for the last element,
  1441. respectively.
  1442.  
  1443. CMPS (Compare Strings) subtracts the destination string element (at ES:EDI)
  1444. from the source string element (at ESI) and updates the flags AF, SF, PF, CF
  1445. and OF. If the string elements are equal, ZF=1; otherwise, ZF=0. If DF=0,
  1446. the processor increments the memory pointers (ESI and EDI) for the two
  1447. strings. CMPSB compares bytes, CMPSW compares words, and CMPSD compares
  1448. doublewords. The segment register used for the source address can be changed
  1449. with a segment override prefix while the destination segment register
  1450. cannot be overridden.
  1451.  
  1452. SCAS (Scan String) subtracts the destination string element at ES:EDI from
  1453. EAX, AX, or AL and updates the flags AF, SF, ZF, PF, CF and OF. If the
  1454. values are equal, ZF=1; otherwise, ZF=0. If DF=0, the processor increments
  1455. the memory pointer (EDI) for the string. SCASB scans bytes; SCASW scans
  1456. words; SCASD scans doublewords. The destination segment register (ES) cannot
  1457. be overridden.
  1458.  
  1459. When either the REPE or REPNE prefix modifies either the SCAS or CMPS
  1460. primitives, the processor compares the value of the current string element
  1461. with the value in EAX for doubleword elements, in AX for word elements, or
  1462. in AL for byte elements. Termination of the repeated operation depends on
  1463. the resulting state of ZF as well as on the value in ECX.
  1464.  
  1465. LODS (Load String) places the source string element at ESI into EAX for
  1466. doubleword strings, into AX for word strings, or into AL for byte strings.
  1467. LODS increments or decrements ESI according to DF.
  1468.  
  1469. STOS (Store String) places the source string element from EAX, AX, or AL
  1470. into the string at ES:DSI. STOS increments or decrements EDI according to
  1471. DF.
  1472.  
  1473.  
  1474. 3.7  Instructions for Block-Structured Languages
  1475.  
  1476. The instructions in this section provide machine-language support for
  1477. functions normally found in high-level languages. These instructions include
  1478. ENTER and LEAVE, which simplify the programming of procedures.
  1479.  
  1480. ENTER (Enter Procedure) creates a stack frame that may be used to implement
  1481. the scope rules of block-structured high-level languages. A LEAVE
  1482. instruction at the end of a procedure complements an ENTER at the beginning
  1483. of the procedure to simplify stack management and to control access to
  1484. variables for nested procedures.
  1485.  
  1486. The ENTER instruction includes two parameters. The first parameter
  1487. specifies the number of bytes of dynamic storage to be allocated on the
  1488. stack for the routine being entered. The second parameter corresponds to the
  1489. lexical nesting level (0-31) of the routine. (Note that the lexical level
  1490. has no relationship to either the protection privilege levels or to the I/O
  1491. privilege level.)
  1492.  
  1493. The specified lexical level determines how many sets of stack frame
  1494. pointers the CPU copies into the new stack frame from the preceding frame.
  1495. This list of stack frame pointers is sometimes called the display. The first
  1496. word of the display is a pointer to the last stack frame. This pointer
  1497. enables a LEAVE instruction to reverse the action of the previous ENTER
  1498. instruction by effectively discarding the last stack frame.
  1499.  
  1500.    Example: ENTER 2048,3
  1501.  
  1502.    Allocates 2048 bytes of dynamic storage on the stack and sets up pointers
  1503.    to two previous stack frames in the stack frame that ENTER creates for
  1504.    this procedure.
  1505.  
  1506. After ENTER creates the new display for a procedure, it allocates the
  1507. dynamic storage space for that procedure by decrementing ESP by the number
  1508. of bytes specified in the first parameter. This new value of ESP serves as a
  1509. starting point for all PUSH and POP operations within that procedure.
  1510.  
  1511. To enable a procedure to address its display, ENTER leaves EBP pointing to
  1512. the beginning of the new stack frame. Data manipulation instructions that
  1513. specify EBP as a base register implicitly address locations within the stack
  1514. segment instead of the data segment.
  1515.  
  1516. The ENTER instruction can be used in two ways: nested and non-nested. If
  1517. the lexical level is 0, the non-nested form is used. Since the second
  1518. operand is 0, ENTER pushes EBP, copies ESP to EBP and then subtracts the
  1519. first operand from ESP. The nested form of ENTER occurs when the second
  1520. parameter (lexical level) is not 0.
  1521.  
  1522. Figure 3-16 gives the formal definition of ENTER.
  1523.  
  1524. The main procedure (with other procedures nested within) operates at the
  1525. highest lexical level, level 1. The first procedure it calls operates at the
  1526. next deeper lexical level, level 2. A level 2 procedure can access the
  1527. variables of the main program which are at fixed locations specified by the
  1528. compiler. In the case of level 1, ENTER allocates only the requested
  1529. dynamic storage on the stack because there is no previous display to copy.
  1530.  
  1531. A program operating at a higher lexical level calling a program at a lower
  1532. lexical level requires that the called procedure should have access to the
  1533. variables of the calling program. ENTER provides this access through a
  1534. display that provides addressability to the calling program's stack frame.
  1535.  
  1536. A procedure calling another procedure at the same lexical level implies
  1537. that they are parallel procedures and that the called procedure should not
  1538. have access to the variables of the calling procedure. In this case, ENTER
  1539. copies only that portion of the display from the calling procedure which
  1540. refers to previously nested procedures operating at higher lexical levels.
  1541. The new stack frame does not include the pointer for addressing the calling
  1542. procedure's stack frame.
  1543.  
  1544. ENTER treats a reentrant procedure as a procedure calling another procedure
  1545. at the same lexical level. In this case, each succeeding iteration of the
  1546. reentrant procedure can address only its own variables and the variables of
  1547. the calling procedures at higher lexical levels. A reentrant procedure can
  1548. always address its own variables; it does not require pointers to the stack
  1549. frames of previous iterations.
  1550.  
  1551. By copying only the stack frame pointers of procedures at higher lexical
  1552. levels, ENTER makes sure that procedures access only those variables of
  1553. higher lexical levels, not those at parallel lexical levels (see Figure
  1554. 3-17). Figures 3-18 through 3-21 demonstrate the actions of the ENTER
  1555. instruction if the modules shown in Figure 3-17 were to call one another in
  1556. alphabetic order.
  1557.  
  1558. Block-structured high-level languages can use the lexical levels defined by
  1559. ENTER to control access to the variables of previously nested procedures.
  1560. Referring to Figure 3-17 for example, if PROCEDURE A calls PROCEDURE B
  1561. which, in turn, calls PROCEDURE C, then PROCEDURE C will have access to the
  1562. variables of MAIN and PROCEDURE A, but not PROCEDURE B because they operate
  1563. at the same lexical level. Following is the complete definition of access to
  1564. variables for Figure 3-17.
  1565.  
  1566.   1.  MAIN PROGRAM has variables at fixed locations.
  1567.  
  1568.   2.  PROCEDURE A can access only the fixed variables of MAIN.
  1569.  
  1570.   3.  PROCEDURE B can access only the variables of PROCEDURE A and MAIN.
  1571.       PROCEDURE B cannot access the variables of PROCEDURE C or PROCEDURE D.
  1572.  
  1573.   4.  PROCEDURE C can access only the variables of PROCEDURE A and MAIN.
  1574.       PROCEDURE C cannot access the variables of PROCEDURE B or PROCEDURE D.
  1575.  
  1576.   5.  PROCEDURE D can access the variables of PROCEDURE C, PROCEDURE A, and
  1577.       MAIN. PROCEDURE D cannot access the variables of PROCEDURE B.
  1578.  
  1579. ENTER at the beginning of the MAIN PROGRAM creates dynamic storage space
  1580. for MAIN but copies no pointers. The first and only word in the display
  1581. points to itself because there is no previous value for LEAVE to return to
  1582. EBP. See Figure 3-18.
  1583.  
  1584. After MAIN calls PROCEDURE A, ENTER creates a new display for PROCEDURE A
  1585. with the first word pointing to the previous value of EBP (BPM for LEAVE to
  1586. return to the MAIN stack frame) and the second word pointing to the current
  1587. value of EBP. Procedure A can access variables in MAIN since MAIN is at
  1588. level 1. Therefore the base for the dynamic storage for MAIN is at [EBP-2].
  1589. All dynamic variables for MAIN are at a fixed offset from this value. See
  1590. Figure 3-19.
  1591.  
  1592. After PROCEDURE A calls PROCEDURE B, ENTER creates a new display for
  1593. PROCEDURE B with the first word pointing to the previous value of EBP, the
  1594. second word pointing to the value of EBP for MAIN, and the third word
  1595. pointing to the value of EBP for A and the last word pointing to the current
  1596. EBP. B can access variables in A and MAIN by fetching from the display the
  1597. base addresses of the respective dynamic storage areas. See Figure 3-20.
  1598.  
  1599. After PROCEDURE B calls PROCEDURE C, ENTER creates a new display for
  1600. PROCEDURE C with the first word pointing to the previous value of EBP, the
  1601. second word pointing to the value of EBP for MAIN, and the third word
  1602. pointing to the EBP value for A and the third word pointing to the current
  1603. value of EBP. Because PROCEDURE B and PROCEDURE C have the same lexical
  1604. level, PROCEDURE C is not allowed access to variables in B and therefore
  1605. does not receive a pointer to the beginning of PROCEDURE B's stack frame.
  1606. See Figure 3-21.
  1607.  
  1608. LEAVE (Leave Procedure) reverses the action of the previous ENTER
  1609. instruction. The LEAVE instruction does not include any operands. LEAVE
  1610. copies EBP to ESP to release all stack space allocated to the procedure by
  1611. the most recent ENTER instruction. Then LEAVE pops the old value of EBP from
  1612. the stack. A subsequent RET instruction can then remove any arguments that
  1613. were pushed on the stack by the calling program for use by the called
  1614. procedure.
  1615.  
  1616.  
  1617. Figure 3-16.  Formal Definition of the ENTER Instruction
  1618.  
  1619. The formal definition of the ENTER instruction for all cases is given by the
  1620. following listing. LEVEL denotes the value of the second operand.
  1621.  
  1622. Push EBP
  1623. Set a temporary value FRAME_PTR := ESP
  1624. If LEVEL > 0 then
  1625.       Repeat (LEVEL-1) times:
  1626.           EBP :=EBP - 4
  1627.           Push the doubleword pointed to by EBP
  1628.       End repeat
  1629.       Push FRAME_PTR
  1630. End if
  1631. EBP := FRAME_PTR
  1632. ESP := ESP - first operand.
  1633.  
  1634.  
  1635. Figure 3-17.  Variable Access in Nested Procedures
  1636.  
  1637.       ╔════════════════════════════════════════════════════════════════╗
  1638.       ║                MAIN PROCEDURE (LEXICAL LEVEL 1)                ║
  1639.       ║   ╔════════════════════════════════════════════════════════╗   ║
  1640.       ║   ║              PROCEDURE A (LEXICAL LEVEL 2)             ║   ║
  1641.       ║   ║  ╔══════════════════════════════════════════════════╗  ║   ║
  1642.       ║   ║  ║           PROCEDURE B (LEXICAL LEVEL 3)          ║  ║   ║
  1643.       ║   ║  ╚══════════════════════════════════════════════════╝  ║   ║
  1644.       ║   ║                                                        ║   ║
  1645.       ║   ║  ╔══════════════════════════════════════════════════╗  ║   ║
  1646.       ║   ║  ║           PROCEDURE C (LEXICAL LEVEL 3)          ║  ║   ║
  1647.       ║   ║  ║  ╔════════════════════════════════════════════╗  ║  ║   ║
  1648.       ║   ║  ║  ║        PROCEDURE D (LEXICAL LEVEL 4)       ║  ║  ║   ║
  1649.       ║   ║  ║  ╚════════════════════════════════════════════╝  ║  ║   ║
  1650.       ║   ║  ║                                                  ║  ║   ║
  1651.       ║   ║  ╚══════════════════════════════════════════════════╝  ║   ║
  1652.       ║   ║                                                        ║   ║
  1653.       ║   ╚════════════════════════════════════════════════════════╝   ║
  1654.       ║                                                                ║
  1655.       ╚════════════════════════════════════════════════════════════════╝
  1656.  
  1657.  
  1658. Figure 3-18.  Stack Frame for MAIN at Level 1
  1659.  
  1660.                                       31          0 
  1661.                 D  O                 ║               ║
  1662.                 I  F              ┌─ ╠═══════╪═══════╣
  1663.                 R                 │  ║    OLD ESP    ║
  1664.                 E  E     DISPLAY ─┤  ╠═══════╪═══════╣──EBP FOR
  1665.                 C  X              │  ║      EBPM
  1666. EBPM = EBP VALUE FOR MAIN    ║    MAIN
  1667.                 T  P              ╞═ ╠═══════╪═══════╣
  1668.                 I  A              │  ║               ║
  1669.                 O  N              │  ╠═══════╪═══════╣
  1670.                 N  S     DYNAMIC ─┤  ║               ║
  1671.                    I     STORAGE  │  ╠═══════╪═══════╣
  1672.                  │ O              │  ║               ║
  1673.                  │ N              └─ ╠═══════╪═══════╣──ESP
  1674.                  │                   ║               ║
  1675.                                                    
  1676.  
  1677.  
  1678. Figure 3-19.  Stack Frame for Procedure A
  1679.  
  1680.                                       31          0 
  1681.                 D  O                 ║               ║
  1682.                 I  F                 ╠═══════╪═══════╣
  1683.                 R                    ║    OLD ESP    ║
  1684.                 E  E                 ╠═══════╪═══════╣
  1685.                 C  X                 ║      EBPM
  1686. EBPM = EBP VALUE FOR MAIN    ║
  1687.                 T  P                 ╠═══════╪═══════╣
  1688.                 I  A                 ║               ║
  1689.                 O  N                 ╠═══════╪═══════╣
  1690.                 N  S                 ║               ║
  1691.                    I                 ╠═══════╪═══════╣
  1692.                  │ O                 ║               ║
  1693.                  │ N              ┌─ ╠═══════╪═══════╣
  1694.                  │                │  ║      EBPM     ║
  1695.                                  │  ╠═══════╪═══════╣──EBP FOR A
  1696.                          DISPLAY ─┤  ║      EBPM     ║
  1697.                                   │  ╠═══════╪═══════╣
  1698.                                   │  ║      EBPA
  1699. EBPA = EBP VALUE FOR PROCEDURE A    ║
  1700.                                   ╞═ ╠═══════╪═══════╣
  1701.                                   │  ║               ║
  1702.                                   │  ╠═══════╪═══════╣
  1703.                          DYNAMIC ─┤  ║               ║
  1704.                          STORAGE  │  ╠═══════╪═══════╣
  1705.                                   │  ║               ║
  1706.                                   └─ ╠═══════╪═══════╣──ESP
  1707.                                      ║               ║
  1708.                                                     
  1709.  
  1710.  
  1711. Figure 3-20.  Stack Frame for Procedure B at Level 3 Called from A
  1712.  
  1713.                                       31          0 
  1714.                 D  O                 ║               ║
  1715.                 I  F                 ╠═══════╪═══════╣
  1716.                 R                    ║    OLD ESP    ║
  1717.                 E  E                 ╠═══════╪═══════╣
  1718.                 C  X                 ║      EBPM
  1719. EBPM = EBP VALUE FOR MAIN    ║
  1720.                 T  P                 ╠═══════╪═══════╣
  1721.                 I  A                 ║               ║
  1722.                 O  N                 ╠═══════╪═══════╣
  1723.                 N  S                 ║               ║
  1724.                    I                 ╠═══════╪═══════╣
  1725.                  │ O                 ║               ║
  1726.                  │ N                 ╠═══════╪═══════╣
  1727.                  │                   ║      EBPM     ║
  1728.                                     ╠═══════╪═══════╣
  1729.                                      ║      EBPM     ║
  1730.                                      ╠═══════╪═══════╣
  1731.                                      ║      EBPA     ║
  1732.                                      ╠═══════╪═══════╣
  1733.                                      ║               ║
  1734.                                      ╠═══════╪═══════╣
  1735.                                      ║               ║
  1736.                                      ╠═══════╪═══════╣
  1737.                                      ║               ║
  1738.                                   ┌─ ╠═══════╪═══════╣
  1739.                                   │  ║      EBPA     ║
  1740.                                   │  ╠═══════╪═══════╣──EBP
  1741.                                   │  ║      EBPM     ║
  1742.                          DISPLAY ─┤  ╠═══════╪═══════╣
  1743.                                   │  ║      EBPA     ║
  1744.                                   │  ╠═══════╪═══════╣
  1745.                                   │  ║      EBPB
  1746. EBPB = EBP VALUE FOR PROCEDURE B    ║
  1747.                                   ╞═ ╠═══════╪═══════╣
  1748.                                   │  ║               ║
  1749.                                   │  ╠═══════╪═══════╣
  1750.                          DYNAMIC ─┤  ║               ║
  1751.                          STORAGE  │  ╠═══════╪═══════╣
  1752.                                   │  ║               ║
  1753.                                   └─ ╠═══════╪═══════╣──ESP
  1754.                                      ║               ║
  1755.                                                     
  1756.  
  1757.  
  1758. Figure 3-21.  Stack Frame for Procedure C at Level 3 Called from B
  1759.  
  1760.                                       31          0 
  1761.                 D  O                 ║               ║
  1762.                 I  F                 ╠═══════╪═══════╣
  1763.                 R                    ║    OLD ESP    ║
  1764.                 E  E                 ╠═══════╪═══════╣
  1765.                 C  X                 ║      EBPM
  1766. EBPM = EBP VALUE FOR MAIN    ║
  1767.                 T  P                 ╠═══════╪═══════╣
  1768.                 I  A                 ║               ║
  1769.                 O  N                 ╠═══════╪═══════╣
  1770.                 N  S                 ║               ║
  1771.                    I                 ╠═══════╪═══════╣
  1772.                  │ O                 ║               ║
  1773.                  │ N                 ╠═══════╪═══════╣
  1774.                  │                   ║      EBPM     ║
  1775.                                     ╠═══════╪═══════╣
  1776.                                      ║      EBPM     ║
  1777.                                      ╠═══════╪═══════╣
  1778.                                      ║      EBPA
  1779. EBPA = EBP VALUE FOR PROCEDURE A    ║
  1780.                                      ╠═══════╪═══════╣
  1781.                                      ║               ║
  1782.                                      ╠═══════╪═══════╣
  1783.                                      ║               ║
  1784.                                      ╠═══════╪═══════╣
  1785.                                      ║               ║
  1786.                                   ┌─ ╠═══════╪═══════╣
  1787.                                   │  ║      EBPA     ║
  1788.                                   │  ╠═══════╪═══════╣──EBP
  1789.                                   │  ║      EBPM     ║
  1790.                          DISPLAY ─┤  ╠═══════╪═══════╣
  1791.                                   │  ║      EBPA     ║
  1792.                                   │  ╠═══════╪═══════╣
  1793.                                   │  ║      EBPB
  1794. EBPB = EBP VALUE FOR PROCEDURE B    ║
  1795.                                   ╞═ ╠═══════╪═══════╣
  1796.                                   │  ║               ║
  1797.                                   │  ╠═══════╪═══════╣
  1798.                          DYNAMIC ─┤  ║               ║
  1799.                          STORAGE  │  ╠═══════╪═══════╣
  1800.                                   │  ║               ║
  1801.                                   └─ ╠═══════╪═══════╣──ESP
  1802.                                      ║               ║
  1803.                                                     
  1804.  
  1805.  
  1806. 3.8  Flag Control Instructions
  1807.  
  1808. The flag control instructions provide a method for directly changing the
  1809. state of bits in the flag register.
  1810.  
  1811.  
  1812. 3.8.1  Carry and Direction Flag Control Instructions
  1813.  
  1814. The carry flag instructions are useful in conjunction with
  1815. rotate-with-carry instructions RCL and RCR. They can initialize the carry
  1816. flag, CF, to a known state before execution of a rotate that moves the carry
  1817. bit into one end of the rotated operand.
  1818.  
  1819. The direction flag control instructions are specifically included to set or
  1820. clear the direction flag, DF, which controls the left-to-right or
  1821. right-to-left direction of string processing. If DF=0, the processor
  1822. automatically increments the string index registers, ESI and EDI, after each
  1823. execution of a string primitive. If DF=1, the processor decrements these
  1824. index registers. Programmers should use one of these instructions before any
  1825. procedure that uses string instructions to insure that DF is set properly.
  1826.  
  1827. Flag Control Instruction                  Effect
  1828.  
  1829. STC (Set Carry Flag)                      CF  1
  1830. CLC (Clear Carry Flag)                    CF  0
  1831. CMC (Complement Carry Flag)               CF  NOT (CF)
  1832. CLD (Clear Direction Flag)                DF  0
  1833. STD (Set Direction Flag)                  DF  1
  1834.  
  1835.  
  1836. 3.8.2  Flag Transfer Instructions
  1837.  
  1838. Though specific instructions exist to alter CF and DF, there is no direct
  1839. method of altering the other applications-oriented flags. The flag transfer
  1840. instructions allow a program to alter the other flag bits with the bit
  1841. manipulation instructions after transferring these flags to the stack or the
  1842. AH register.
  1843.  
  1844. The instructions LAHF and SAHF deal with five of the status flags, which
  1845. are used primarily by the arithmetic and logical instructions.
  1846.  
  1847. LAHF (Load AH from Flags) copies SF, ZF, AF, PF, and CF to AH bits 7, 6, 4,
  1848. 2, and 0, respectively (see Figure 3-22). The contents of the remaining bits
  1849. (5, 3, and 1) are undefined. The flags remain unaffected.
  1850.  
  1851. SAHF (Store AH into Flags) transfers bits 7, 6, 4, 2, and 0 from AH into
  1852. SF, ZF, AF, PF, and CF, respectively (see Figure 3-22).
  1853.  
  1854. The PUSHF and POPF instructions are not only useful for storing the flags
  1855. in memory where they can be examined and modified but are also useful for
  1856. preserving the state of the flags register while executing a procedure.
  1857.  
  1858. PUSHF (Push Flags) decrements ESP by two and then transfers the low-order
  1859. word of the flags register to the word at the top of stack pointed to by ESP
  1860. (see Figure 3-23). The variant PUSHFD decrements ESP by four, then
  1861. transfers both words of the extended flags register to the top of the stack
  1862. pointed to by ESP (the VM and RF flags are not moved, however).
  1863.  
  1864. POPF (Pop Flags) transfers specific bits from the word at the top of stack
  1865. into the low-order byte of the flag register (see Figure 3-23), then
  1866. increments ESP by two. The variant POPFD transfers specific bits from the
  1867. doubleword at the top of the stack into the extended flags register (the RF
  1868. and VM flags are not changed, however), then increments ESP by four.
  1869.  
  1870.  
  1871. Figure 3-22.  LAHF and SAHF
  1872.  
  1873.                      7    6    5    4    3    2    1    0
  1874.                    ╔════╦════╦════╦════╦════╦════╦════╦════╗
  1875.                    ║ SF ║ ZF ║ UU ║ AF ║ UU ║ PF ║ UU ║ CF ║
  1876.                    ╚════╩════╩════╩════╩════╩════╩════╩════╝
  1877.  
  1878.      LAHF LOADS FIVE FLAGS FROM THE FLAG REGISTER INTO REGISTER AH. SAHF
  1879.      STORES THESE SAME FIVE FLAGS FROM AH INTO THE FLAG REGISTER. THE BIT
  1880.      POSITION OF EACH FLAG IS THE SAME IN AH AS IT IS IN THE FLAG REGISTER.
  1881.      THE REMAINING BITS (MARKED UU) ARE RESERVED; DO NOT DEFINE.
  1882.  
  1883.  
  1884. 3.9  Coprocessor Interface Instructions
  1885.  
  1886. A numerics coprocessor (e.g., the 80387 or 80287) provides an extension to
  1887. the instruction set of the base architecture. The coprocessor extends the
  1888. instruction set of the base architecture to support high-precision integer
  1889. and floating-point calculations. This extended instruction set includes
  1890. arithmetic, comparison, transcendental, and data transfer instructions. The
  1891. coprocessor also contains a set of useful constants to enhance the speed of
  1892. numeric calculations.
  1893.  
  1894. A program contains instructions for the coprocessor in line with the
  1895. instructions for the CPU. The system executes these instructions in the same
  1896. order as they appear in the instruction stream. The coprocessor operates
  1897. concurrently with the CPU to provide maximum throughput for numeric
  1898. calculations.
  1899.  
  1900. The 80386 also has features to support emulation of the numerics
  1901. coprocessor when the coprocessor is absent. The software emulation of the
  1902. coprocessor is transparent to application software but requires more time
  1903. for execution. Refer to Chapter 11 for more information on coprocessor
  1904. emulation.
  1905.  
  1906. ESC (Escape) is a 5-bit sequence that begins the opcodes that identify
  1907. floating point numeric instructions. The ESC pattern tells the 80386 to send
  1908. the opcode and addresses of operands to the numerics coprocessor. The
  1909. numerics coprocessor uses the escape instructions to perform
  1910. high-performance, high-precision floating point arithmetic that conforms to
  1911. the IEEE floating point standard 754.
  1912.  
  1913. WAIT (Wait) is an 80386 instruction that suspends program execution until
  1914. the 80386 CPU detects that the BUSY pin is inactive. This condition
  1915. indicates that the coprocessor has completed its processing task and that
  1916. the CPU may obtain the results.
  1917.  
  1918.  
  1919. Figure 3-23.  Flag Format for PUSHF and POPF
  1920.  
  1921.                                 PUSHFD/POPFD
  1922.      ┌───────────────────────────────┴────────────────────────────────┐
  1923.                                                   PUSHF/POPF
  1924.                                      ┌────────────────┴───────────────┐
  1925.       31              23               15                7           0
  1926.      ╔═══════════════╪═══════════╤═╤═╤═╤═╤════╤═╤═╤═╤═╤═╤═╤═╤═╤═╤═╤═╤═╗
  1927.      ║                           │V│R│ │N│ID  │O│D│I│T│S│Z│ │A│ │P│ │C║
  1928.      ║0 0 0 0 0 0 0 0 0 0 0 0 0 0│ │ │0│ │    │ │ │ │ │ │ │0│ │0│ │1│ ║
  1929.      ║                           │M│F│ │T│  PL│F│F│F│F│F│F│ │F│ │F│ │F║
  1930.      ╚═══════════════╪═══════════╧═╧═╧═╧═╧════╧═╧═╧═╧═╧═╧═╧═╧═╧═╧═╧═╧═╝
  1931.  
  1932.      BITS MARKED 0 AND 1 ARE RESERVED BY INTEL. DO NOT DEFINE.
  1933.  
  1934.    SYSTEMS FLAGS (INCLUDING THE IOPL FIELD, AND THE VM, RF, AND IF FLAGS)
  1935.    ARE PUSHED AND ARE VISIBLE TO APPLICATIONS PROGRAMS. HOWEVER, WHEN AN
  1936.    APPLICATIONS PROGRAM POPS THE FLAGS, THESE ITEMS ARE NOT CHANGED,
  1937.    REGARDLESS OF THE VALUES POPPED INTO THEM.
  1938.  
  1939.  
  1940. 3.10  Segment Register Instructions
  1941.  
  1942. This category actually includes several distinct types of instructions.
  1943. These various types are grouped together here because, if systems designers
  1944. choose an unsegmented model of memory organization, none of these
  1945. instructions is used by applications programmers. The instructions that deal
  1946. with segment registers are:
  1947.  
  1948. 1.  Segment-register transfer instructions.
  1949.  
  1950.    MOV SegReg, ...
  1951.    MOV ..., SegReg
  1952.    PUSH SegReg
  1953.    POP SegReg
  1954.  
  1955. 2.  Control transfers to another executable segment.
  1956.  
  1957.    JMP far    ; direct and indirect
  1958.    CALL far
  1959.    RET far
  1960.  
  1961. 3.  Data pointer instructions.
  1962.  
  1963.    LDS
  1964.    LES
  1965.    LFS
  1966.    LGS
  1967.    LSS
  1968.  
  1969. Note that the following interrupt-related instructions are different; all
  1970. are capable of transferring control to another segment, but the use of
  1971. segmentation is not apparent to the applications programmer.
  1972.  
  1973. INT n
  1974. INTO
  1975. BOUND
  1976. IRET
  1977.  
  1978.  
  1979. 3.10.1  Segment-Register Transfer Instructions
  1980.  
  1981. The MOV, POP, and PUSH instructions also serve to load and store segment
  1982. registers. These variants operate similarly to their general-register
  1983. counterparts except that one operand can be a segment register. MOV cannot
  1984. move segment register to a segment register. Neither POP nor MOV can place a
  1985. value in the code-segment register CS; only the far control-transfer
  1986. instructions can change CS.
  1987.  
  1988.  
  1989. 3.10.2  Far Control Transfer Instructions
  1990.  
  1991. The far control-transfer instructions transfer control to a location in
  1992. another segment by changing the content of the CS register.
  1993.  
  1994. Direct far JMP. Direct JMP instructions that specify a target location
  1995. outside the current code segment contain a far pointer. This pointer
  1996. consists of a selector for the new code segment and an offset within the new
  1997. segment.
  1998.  
  1999. Indirect far JMP. Indirect JMP instructions that specify a target location
  2000. outside the current code segment use a 48-bit variable to specify the far
  2001. pointer.
  2002.  
  2003. Far CALL. An intersegment CALL places both the value of EIP and CS on the
  2004. stack.
  2005.  
  2006. Far RET. An intersegment RET restores the values of both CS and EIP which
  2007. were saved on the stack by the previous intersegment CALL instruction.
  2008.  
  2009.  
  2010. 3.10.3  Data Pointer Instructions
  2011.  
  2012. The data pointer instructions load a pointer (consisting of a segment
  2013. selector and an offset) to a segment register and a general register.
  2014.  
  2015. LDS (Load Pointer Using DS) transfers a pointer variable from the source
  2016. operand to DS and the destination register. The source operand must be a
  2017. memory operand, and the destination operand must be a general register. DS
  2018. receives the segment-selector of the pointer. The destination register
  2019. receives the offset part of the pointer, which points to a specific location
  2020. within the segment.
  2021.  
  2022. Example: LDS ESI, STRING_X
  2023.  
  2024. Loads DS with the selector identifying the segment pointed to by a
  2025. STRING_X, and loads the offset of STRING_X into ESI.  Specifying ESI as the
  2026. destination operand is a convenient way to prepare for a string operation on
  2027. a source string that is not in the current data segment.
  2028.  
  2029. LES (Load Pointer Using ES) operates identically to LDS except that ES
  2030. receives the segment selector rather than DS.
  2031.  
  2032. Example: LES EDI, DESTINATION_X
  2033.  
  2034. Loads ES with the selector identifying the segment pointed to by
  2035. DESTINATION_X, and loads the offset of DESTINATION_X into EDI. This
  2036. instruction provides a convenient way to select a destination for a string
  2037. operation if the desired location is not in the current extra segment.
  2038.  
  2039. LFS (Load Pointer Using FS) operates identically to LDS except that FS
  2040. receives the segment selector rather than DS.
  2041.  
  2042. LGS (Load Pointer Using GS) operates identically to LDS except that GS
  2043. receives the segment selector rather than DS.
  2044.  
  2045. LSS (Load Pointer Using SS) operates identically to LDS except that SS
  2046. receives the segment selector rather than DS.  This instruction is
  2047. especially important, because it allows the two registers that identify the
  2048. stack (SS:ESP) to be changed in one uninterruptible operation.  Unlike the
  2049. other instructions which load SS, interrupts are not inhibited at the end
  2050. of the LSS instruction.  The other instructions (e.g., POP SS) inhibit
  2051. interrupts to permit the following instruction to load ESP, thereby forming
  2052. an indivisible load of SS:ESP.  Since both SS and ESP can be loaded by LSS,
  2053. there is no need to inhibit interrupts.
  2054.  
  2055.  
  2056. 3.11  Miscellaneous Instructions
  2057.  
  2058. The following instructions do not fit in any of the previous categories,
  2059. but are nonetheless useful.
  2060.  
  2061.  
  2062. 3.11.1  Address Calculation Instruction
  2063.  
  2064. LEA (Load Effective Address) transfers the offset of the source operand
  2065. (rather than its value) to the destination operand.  The source operand must
  2066. be a memory operand, and the destination operand must be a general register.
  2067. This instruction is especially useful for initializing registers before the
  2068. execution of the string primitives (ESI, EDI) or the XLAT instruction (EBX).
  2069. The LEA can perform any indexing or scaling that may be needed.
  2070.  
  2071. Example: LEA EBX, EBCDIC_TABLE
  2072.  
  2073. Causes the processor to place the address of the starting location of the
  2074. table labeled EBCDIC_TABLE into EBX.
  2075.  
  2076.  
  2077. 3.11.2  No-Operation Instruction
  2078.  
  2079. NOP (No Operation) occupies a byte of storage but affects nothing but the
  2080. instruction pointer, EIP.
  2081.  
  2082.  
  2083. 3.11.3  Translate Instruction
  2084.  
  2085. XLAT (Translate) replaced a byte in the AL register with a byte from a
  2086. user-coded translation table. When XLAT is executed, AL should have the
  2087. unsigned index to the table addressed by EBX. XLAT changes the contents of
  2088. AL from table index to table entry. EBX is unchanged. The XLAT instruction
  2089. is useful for translating from one coding system to another such as from
  2090. ASCII to EBCDIC.  The translate table may be up to 256 bytes long.  The
  2091. value placed in the AL register serves as an index to the location of the
  2092. corresponding translation value.
  2093.  
  2094.  
  2095.                         PART II  SYSTEMS PROGRAMMING                       
  2096.  
  2097.  
  2098.