home *** CD-ROM | disk | FTP | other *** search
/ Simtel MSDOS - Coast to Coast / simteldosarchivecoasttocoast2.iso / asmutil / a86v322.zip / A04.DOC < prev    next >
Text File  |  1988-11-11  |  11KB  |  252 lines

  1. CHAPTER 4   ELEMENTS OF THE A86 LANGUAGE
  2.  
  3. This chapter begins the description of the A86 language.  It's a
  4. bit more tutorial in nature than the rest of the manual.  I'll
  5. start by describing the elementary building blocks of the
  6. language.
  7.  
  8.  
  9. The A86 Language and the A86 Program
  10.  
  11. First, let's establish what we mean when we say A86.  On one
  12. hand, A86 is the name for my assembly language for the Intel 86
  13. family of (IBM-PC and compatible) computers. Statements written
  14. in this language are used to specify machine instructions for the
  15. 86 family and to allocate memory space for program data.  On the
  16. other hand, A86 is the name for a program called an assembler,
  17. that translates these human readable statements into a machine
  18. readable form. The input to the assembler is a source file (or a
  19. list of source files) containing assembly language statements.
  20. The output of the assembler is a file containing binary program
  21. code that can either be run as a program on the PC, or combined
  22. with other modules (using a linker) to make a program.
  23.  
  24.  
  25. General Categories of A86 Elements
  26.  
  27. The statements in an A86 source file can be classified in three
  28. general categories: instruction statements, data allocation
  29. statements, and assembler directives.  An instruction statement
  30. uses an easily remembered name (a mnemonic) and possibly one or
  31. more operands to specify a machine instruction to be generated. A
  32. data allocation statement reserves, and optionally initializes,
  33. memory space for program data.  An assembler directive is a
  34. statement that gives special instructions to the assembler.
  35. Directives are unlike the instruction and data allocation
  36. statements in that they do not specify the actual contents of
  37. memory.  Examples of the three types of A86 statements are given
  38. below.  These are provided to give you a general idea of what the
  39. different kinds of statements look like.
  40.  
  41. Instruction Statements
  42.  
  43. MOV AX,BX
  44. CALL SORT_PROCEDURE
  45. ADD AL,7
  46.  
  47. Data Allocation Statements
  48.  
  49. A_VARIABLE DW 0
  50. DB 'HELLO'
  51.  
  52. Assembler Directives
  53.  
  54. CODE SEGMENT
  55. ITEM_COUNT EQU 5
  56.                                                               4-2
  57.  
  58. The statements in an A86 source file are made up of keywords,
  59. identifiers, numbers, strings, special characters, and comments.
  60. A keyword is a symbol that has special meaning to the assembler,
  61. such as an instruction mnemonic (MOV, CALL) or some other
  62. reserved word in the assembly language (DB, SEGMENT, EQU).
  63. Identifiers are programmer-defined symbols, used to represent
  64. such things as variables, labels in the code, and numerical
  65. constants.  Identifiers may contain letters, numbers, and the
  66. characters _, @, $, and ?, but must begin with a letter, _, or @.
  67. The identifier name is considered unique up to 127 characters,
  68. but it can be of any length (up to 255 characters).   Examples of
  69. identifiers are: COUNT, L1, and A_BYTE.
  70.  
  71. Numbers in A86 may be expressed as decimal, hexadecimal, octal,
  72. or binary.  These must begin with a decimal digit and, except in
  73. the case of a decimal or hexadecimal number, must end with "x"
  74. followed by a letter identifying the base of the number.  A
  75. number without an identifying base is hexadecimal if the first
  76. digit is 0; decimal if the first digit is 1 through 9.  Examples
  77. of A86 numbers are: 123 (decimal), 0ABC (hexadecimal), 1776xQ
  78. (octal), and 10100110xB (binary).
  79.  
  80. Strings are characters enclosed in single quotes.  Examples of
  81. strings are: '1st string' and 'SIGN-ON MESSAGE, V1.0'.  The
  82. single quote is one of many special characters used in the
  83. assembly language.  Others, run together in a list, are: ! $ ? ;
  84. : = , [ ] . + - ( ) * / > ".  The space and tab characters are
  85. also special characters, used as separators in the assembly
  86. language.
  87.  
  88. For compatibility with other assemblers, I now also accept double
  89. quotes for strings.
  90.  
  91. A comment is a sequence of characters used for program
  92. documentation only; it is ignored by the assembler.  Comments
  93. begin with a semicolon (;) and run to the end of the line on
  94. which they are started.  Examples of lines with comments are
  95. shown below:
  96.  
  97. ; This entire line is a comment.
  98. MOV AX,BX  ; This is a comment next to an instruction statement.
  99.  
  100. Alternatively, for compatibility with other assemblers, I provide
  101. the COMMENT directive.  The next non-blank character after
  102. COMMENT is a delimiter to a comment that can run across many
  103. lines; all text is ignored, until a second instance of the
  104. delimiter is seen.  For example,
  105.  
  106. COMMENT 'This comment
  107. runs across two lines'
  108.                                                               4-3
  109.  
  110. I don't like COMMENT, because I think it's very dangerous.  If,
  111. for example, you have two COMMENTs in your program, and you
  112. forget to close the first one, the assembler will happily ignore
  113. all source code between the comments.  If that source code does
  114. not happen to contain any labels referenced elsewhere, the error
  115. may not be detected until your program blows up.  For multiline
  116. comments, I urge you to simply start each line with a semicolon.
  117.  
  118. Statements in the A86 are line oriented, which means that
  119. statements may not be broken across line boundaries.  A86 source
  120. lines may be entered in a free form fashion; that is, without
  121. regard to the column orientation of the symbols and special
  122. characters.
  123.  
  124. PLEASE NOTE: Because an A86 line is free formatted, there is no
  125. need for you to put the operands to your instructions in a
  126. separate column.  You organize things into columns when you want
  127. to visually scan down the column; and you practically never scan
  128. operands separate from their opcodes.  The only reason that 99%
  129. of the assembly-language programs out there in the world have
  130. operands in a separate column is that some IBM assembler written
  131. back in 1953 required it.  It makes no sense to have operands in
  132. a separate column, so STOP DOING IT!
  133.  
  134.  
  135. Operand Typing and Code Generation
  136.  
  137. A86 is a strongly typed assembly language.   What this means is
  138. that operands to instructions (registers, variables, labels,
  139. constants) have a type attribute associated with them which tells
  140. the assembler something about them.  For example, the operand 4
  141. has type number, which tells the assembler that it is a numerical
  142. constant, rather than a register or an address in the code or
  143. data.  The following discussion explains the types associated
  144. with instruction operands and how this type information is used
  145. to generate particular machine opcodes from general purpose
  146. instruction mnemonics.
  147.  
  148. Registers
  149.  
  150. The 8086 has 8 general purpose word (two-byte) registers:
  151. AX,BX,CX,DX,SI,DI,BP, and SP.  The first four of those registers
  152. are subdivided into 8 general purpose one-byte registers
  153. AH,AL,BH,BL,CH,CL,DH, and DL.  There are also 4 16-bit segment
  154. registers CS,DS,ES, and SS, used for addressing memory; and the
  155. implicit instruction-pointer register (referred to as IP,
  156. although "IP" is not part of the A86 assembly language).
  157.  
  158. Variables
  159.  
  160. A variable is a unit of program data with a symbolic name,
  161. residing at a specific location in 8086 memory.  A variable is
  162. given a type at the time it is defined, which indicates the
  163. number of bytes associated with its symbol.  Variables defined
  164. with a DB statement are given type BYTE (one byte), and those
  165. defined with the DW statement are given type WORD (two bytes).
  166. Examples:
  167.                                                               4-4
  168.  
  169. BYTE_VAR DB 0   ; A byte variable.
  170. WORD_VAR DW 0   ; A word variable.
  171.  
  172. Labels
  173.  
  174. A label is a symbol referring to a location in the program code.
  175. It is defined as an identifier, followed by a colon (:), used to
  176. represent the location of a particular instruction or data
  177. structure.  Such a label may be on a line by itself or it may
  178. immediately precede an instruction statement (on the same line).
  179. In the following example, LABEL_1 and LABEL_2 are both labels for
  180. the MOV AL,BL instruction.
  181.  
  182. LABEL_1:
  183. LABEL_2: MOV AL,BL
  184.  
  185. In the A86 assembly language, labels have a type identical to
  186. that of constants.  Thus, the instruction MOV BX,LABEL_2 is
  187. accepted, and the code to move the immediate constant address of
  188. LABEL2 into BX, is generated.
  189.  
  190. IMPORTANT: you must understand the distinction between a label
  191. and a variable, because you may generate a different instruction
  192. than you intended if you confuse them.  For example, if you
  193. declare  X: DW ?, the colon following the X means that X is a
  194. label; the instruction MOV SI,X moves the immediate constant
  195. address of X into the SI register.  On the other hand, if you
  196. declare X DW ?, with no colon, then X is a word variable; the
  197. same instruction MOV SI,X now does something different: it loads
  198. the run-time value of the memory word X into the SI register.
  199.  
  200. Constants
  201.  
  202. A constant is a numerical value computed from an assembly-time
  203. expression.  For example, 123 and 3 + 2 - 1 both represent
  204. constants.  A constant differs from an a variable in that it
  205. specifies a pure number, known by the assembler before the
  206. program is run, rather than a number fetched from memory when the
  207. program is running.
  208.  
  209.  
  210. Generating Opcodes from General Purpose Mnemonics
  211.  
  212. My A86 assembly language is modeled after Intel's ASM86 language,
  213. which uses general purpose mnemonics to represent classes of
  214. machine instructions rather than having a different mnemonic for
  215. each opcode.  For example, the MOV mnemonic is used for all of
  216. the following: move byte register to byte register, load word
  217. register from memory, load byte register with constant, move word
  218. register to memory, move immediate value to word register, move
  219. immediate value to memory, etc.  This feature saves you from
  220. having to distinguish "move" from "load," "move constant" from
  221. "move memory," "move byte" from "move word," etc.
  222.                                                               4-5
  223.  
  224. Because the same general purpose mnemonic can apply to several
  225. different machine opcodes, A86 uses the type information
  226. associated with an instruction's operands in determining the
  227. particular opcode to produce.  The type information associated
  228. with instruction operands is also used to discover programmer
  229. errors, such as attempting to move a word register to a byte
  230. register.
  231.  
  232. The examples that follow illustrate the use of operand types in
  233. generating machine opcodes and discovering programmer errors.  In
  234. each of the examples, the MOV instruction produces a different
  235. 8086 opcode, or an error.  The symbols used in the examples are
  236. assumed to be defined as follows: BVAR is a byte variable, WVAR
  237. is a word variable, and LAB is a label.  As you examine these MOV
  238. instructions, notice that, in each case, the operand on the right
  239. is considered to be the source and the operand on the left is the
  240. destination.  This is a general rule that applies to all
  241. two-operand instruction statements.
  242.  
  243. MOV AX,BX     ; (8B) Move word register to word register.
  244. MOV AX,BL     ; ERROR: Type conflict (word,byte).
  245. MOV CX,5      ; (B9) Move constant to word register.
  246. MOV BVAR,AL   ; (A0) Move AL register to byte in memory.
  247. MOV AL,WVAR   ; ERROR: Type conflict (byte,word).
  248. MOV LAB,5     ; ERROR: Can't use label/constant as dest. to MOV.
  249. MOV WVAR,SI   ; (89) Move word register to word in memory.
  250. MOV BL,1024   ; ERROR: Constant is too large to fit in a byte.
  251.  
  252.