home *** CD-ROM | disk | FTP | other *** search
/ Simtel MSDOS - Coast to Coast / simteldosarchivecoasttocoast2.iso / asmutil / a86v322.zip / A05.DOC < prev    next >
Text File  |  1990-01-25  |  11KB  |  241 lines

  1. CHAPTER 5   SOME EXCLUSIVE FEATURES OF A86
  2.  
  3.  
  4. The IF Statement
  5.  
  6. As a "nudge" in the direction of structured programming, A86
  7. offers the IF statement.  Suppose you want to conditionally skip
  8. around just one instruction.  Ordinarily, this would require, for
  9. example:
  10.  
  11.   JNZ >L1    ; skip the following move if NZ
  12.   MOV AX,BX  ; make this move only if Z
  13. L1:          ; this label exists only for the above skip
  14.  
  15. You may replace the above code with the single line:
  16.  
  17.   IF Z MOV AX,BX
  18.  
  19. The above line generates exactly the same code as the previous 3
  20. lines-- a conditional jump of the opposite condition, around the
  21. statement given in the tail of the IF statement.  The statement
  22. can be a macro call, giving you the opportunity to skip something
  23. more complicated.
  24.  
  25. You may use any condition that would follow the "J" in a
  26. conditional jump instruction, except CXZ, which does not have a
  27. reverse condition.  The assembler interprets the condition by
  28. appending a "J" to the beginning of the condition; so that the
  29. symbols "C", "NC", "Z", "NZ", etc. are not reserved by the
  30. assembler, and can be defined in other contexts.
  31.  
  32.  
  33. Multiple operands to PUSH, POP, INC, DEC
  34.  
  35. A86 will accept any number of register operands for the
  36. instructions PUSH, POP, INC, and DEC; it will generate the
  37. appropriate machine instruction for each operand.  For example,
  38. the statement PUSH AX,BX is the same as the two statements PUSH
  39. AX and PUSH BX.
  40.  
  41. A numeric operand appearing in an INC or DEC statement will cause
  42. the previous INC(s) or DEC(s) to be propagated that number of
  43. times.  For example, the statement INC AX,4 will generate 4 INC
  44. AX instructions.  The statement DEC AL,BX,2 will generate DEC AL,
  45. DEC BX, DEC AL, DEC BX.  Sorry, numeric operands are not allowed
  46. if any of the operands affected was a forward reference or
  47. relocatable quantity; e.g., INC FOO,2 where FOO is undefined.  In
  48. most such cases, you'll want to code the more efficient ADD FOO,2
  49. anyway.
  50.                                                               5-2
  51.  
  52. Conditional Return Instructions
  53.  
  54. Programmers accustomed to the conditional return instructions of
  55. the 8080/Z80 will appreciate the following feature: A86 allows
  56. the operand to a conditional jump instruction to be one of the
  57. three RET instructions RET, RETF, or IRET.  The assembler will
  58. find a nearby return instruction of the indicated flavor, and use
  59. that as the target for the conditional jump.  For example, JZ RET
  60. is the replacement for the 8080's RZ return-if-zero instruction.
  61. In other 8086 assembly languages, you have to find the nearby
  62. instruction yourself, attach a label to it, and use that label.
  63. Note that it does not suffice to attach a label to a single RET
  64. instruction and use that label throughout the program: the range
  65. of conditional jumps is only 128 bytes in either direction.
  66.  
  67. What happens if A86 does not find a nearby return instruction? In
  68. that case, A86 issues an error, "02 Jump > 128", for the next
  69. matching return instruction in the program.  If there is no
  70. subsequent return instruction, the return mnemonic will appear as
  71. an undefined symbol at the end of the program.   In either case,
  72. you correct the problem by inserting a free-standing return
  73. instruction at some nearby point in the program, where it will
  74. not affect the existing code (typically following an
  75. unconditional JMP instruction).  If there is no good place to
  76. insert a return instruction, you can always replace the "Jcond
  77. RET" with an "IF cond RET".
  78.  
  79.  
  80. A86 extensions to the MOV and XCHG instructions
  81.  
  82. There are a number of MOV and XCHG instructions available in A86
  83. that are not a part of the machine instruction set.
  84.  
  85. First, moves between segment registers, and of immediate
  86. constants into segment registers are allowed.  For example, if
  87. you code MOV ES,DS , the assembler will generate a PUSH DS
  88. followed by a POP ES; which will effect the move that you
  89. intended.   If you code MOV DS,0 , the assembler will generate
  90. PUSH AX; MOV AX,0; MOV DS,AX; POP AX.  This is mainly a
  91. convenience for D86 users to load segment registers manually.
  92.  
  93. Second, MOV allows 3 operands.  A statement MOV x,y,z is
  94. equivalent to the two statements MOV y,z followed by MOV x,y.
  95. Sorry, but segment overrides are not allowed in conjunction with
  96. 3-operand MOVs.  The override preceding the MOV is ambiguous in
  97. its meaning; and overrides within operands cannot be handled
  98. correctly by A86.  You'll have to code two MOV instructions if
  99. you want either or both to have a segment override.
  100.  
  101. Third, A86 accepts a MOV of a word-sized memory operand into
  102. another word-sized memory operand.  A86 handles this the same way
  103. it handles a MOV of segment registers: it generates a PUSH of the
  104. source followed by a POP of the destination.
  105.                                                               5-3
  106.  
  107. Finally, A86 allows the XCHG of a segment register (except CS)
  108. with any other word-sized quantity, as well as the XCHG of two
  109. word-sized memory quantities.  If there is no machine instruction
  110. available for XCHG a,b, then A86 generates PUSH a followed by MOV
  111. a,b followed by POP b.
  112.  
  113.  
  114. Local Symbols
  115.  
  116. If you examine most assembly language program symbol tables, you
  117. will find that the symbols can be partitioned into two levels of
  118. significance.   About half the symbols are the names of
  119. procedures and variables having global significance.  If the
  120. names of these symbols are chosen intelligently and carefully,
  121. the program's readability improves drastically. (They usually
  122. aren't chosen well, most often because the assembler restricts
  123. symbols to 6 letters, or because the programmer's habits are
  124. influenced by such assemblers.)
  125.  
  126. The other half of the symbols in a program have a much lower,
  127. local significance.  They are only place markers used to
  128. implement small loops and local branching (e.g., "skip the next 2
  129. instructions if the Z-flag is set").  Assigning full-blown names
  130. to these symbols reduces the readability of your program in two
  131. ways:  First, it is harder to recognize local jumps for what they
  132. are-- they are usually the assembly language equivalent of high
  133. level language constructs like IF statements and WHILE loops.
  134.  
  135. Second, it is harder to follow the global, significant symbols
  136. because they are buried in a sea of the place marker symbols in
  137. the symbol table.
  138.  
  139. A86 solves this problem with local symbols.  If a symbol in your
  140. program consists of a single letter followed by one or more
  141. decimal digits (L3, X123, Y37, etc.), then the symbol is a local
  142. symbol.  Local symbols do not appear in the A86 XREF
  143. cross-reference listing.  They can also be redefined to something
  144. completely different later in the program.  Local symbols can be
  145. of any type: labels, memory variables, etc.
  146.  
  147. Because local symbols can be redefined, you must take care to
  148. specify which one you are referring to in your program.  If your
  149. reference is a forward reference (the label occurs further down
  150. in the program from the reference), then the reference must be
  151. preceded by a ">".  For example,
  152.  
  153. L2:
  154.   MOVSB
  155.   INC BX
  156.   LOOP L2    ; lack of ">" means L2 is above this statement
  157.   .
  158.   .
  159.   JNZ >L2    ; ">" indicates L2 is below this statement
  160.   .
  161.   JMP >L2    ; JMP L2 is disallowed here: cannot overlap ranges
  162.   .
  163. L2:
  164.                                                               5-4
  165.  
  166. I recommend that you assign all your local labels the names L0
  167. through L9.  If your program is so complex that it needs more
  168. than 10 place holders in any one stretch of code, then that
  169. stretch needs to be rewritten.
  170.  
  171.  
  172.  
  173. Operands to AAM and AAD Instructions
  174.  
  175. Those of you who have examined 86 family opcodes with an eagle
  176. eye will have noticed a somewhat spurious "0A" opcode generated
  177. after every AAM or AAD instruction.  The opcode is there to
  178. provide the constant divisor or multiplicand for the instruction.
  179. Believe it or not, there wasn't enough room in the microcode of
  180. the original 8086 to hold this constant!  Although Intel has
  181. never announced the generality of AAM and AAD, it is there: you
  182. can substitute any other constant for 0A (decimal 10), and that
  183. constant will be used.  A86 supports this by letting you give a
  184. constant byte-sized operand to AAM or AAD.  Particularly useful
  185. are the instructions AAM 16, which unpacks AL into nibbles AH and
  186. AL; and AAD 16, which reverses the process, packing nibbles AH
  187. and AL into AL.
  188.  
  189. WARNING: A couple of my users point out to me that the AAD
  190. instruction with a general operand won't work on the NEC V20 and
  191. V30 chips.  The operand is assumed to be 10 no matter what it
  192. really is.  Since a large number of PC "speed up" kits involve
  193. switching to NEC chips, this will be seen on many PC's.  You
  194. should not use AAD with an operand if you want your program to
  195. run on everybody's machine.  Too bad.  AAM works fine, though.
  196.  
  197.  
  198. Single-Operand Forms of the TEST Instruction
  199.  
  200. A86 allows the TEST instruction to have a single operand, to set
  201. the flags according to the value of the operand.  If the operand
  202. is a register, A86 generates a TEST of the register with itself.
  203. If the operand is a memory quantity, A86 generates a TEST of the
  204. memory with the constant -1 (i.e., the quantity will be ANDed
  205. with an all 1's constant).  For example, instead of TEST DL,DL,
  206. you can code simply TEST DL.  Instead of TEST WVAR,0FFFF, you can
  207. code simply TEST WVAR.
  208.  
  209.  
  210. Optimized LEA Instruction
  211.  
  212. Many assembly-language programmers are in the habit of using, for
  213. example, LEA SI,MEMLOC instead of the equivalent MOV SI,OFFSET
  214. MEMLOC to load an immediate value that represents the pointer to
  215. a memory location.  However, the LEA instruction form generates
  216. one more byte of object code than the MOV form. A86 recognizes
  217. this situation and generates the more-efficient MOV instruction
  218. when it can.  This also applies to register moves: MOV AX,BX
  219. instead of LEA AX,[BX].
  220.                                                               5-5
  221.  
  222. I've gotten a little flak from some users about this feature.
  223. They claim it violates my policy against "behind your back"
  224. actions.  But I feel that this feature is completely equivalent
  225. to code optimizations in other situations: the short JMP form
  226. instead of the equivalent near JMP; a byte operand to ADD SI,4
  227. instead of a word operand; the one-byte XCHG AX,BX instead of the
  228. general XCHG rw,ew form; etc, etc, etc. In situations where there
  229. is absolute functional equivalence between forms, A86 tries to
  230. generate the most efficient form.  But for those who are not
  231. convinced, I offer the +L2 switch, described in Chapter 3.
  232.  
  233. Some users have also gotten the mistaken impression, from reading
  234. Intel's confusing specs, that the longer LEA is sometimes faster
  235. than the shorter MOV.  This is never the case-- those users are
  236. reading the clock counts for the memory-fetch forms of MOV, not
  237. the register-only or immediate-value forms.  If you don't believe
  238. it, try timing 1000 consecutive LEA's in a loop that executes
  239. 50000 times, vs. a similar loop with the equivalent MOV.
  240.  
  241.