home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 13 / CDA13.ISO / cdactual / demobin / share / program / Pascal / ASSEM120.ZIP / ASSEM120.DOC next >
Encoding:
Text File  |  1991-08-22  |  10.9 KB  |  211 lines

  1.  
  2.               Instruction assembly code, by Joe Tamburino
  3.               -------------------------------------------
  4.  
  5.                  Version 1.20 / Release Date:  8/22/91
  6.  
  7.  
  8.                         Joseph J. Tamburino
  9.                          7 Christopher Rd.
  10.                          Westford, MA 01886
  11.                         Home: (508) 692-7756
  12.                         Work: (603) 880-1322                             
  13.                      Toll Free:  (800) 247-2476
  14.                        CompuServe:  70033,107
  15.                          User Fee:  $15.00
  16.  
  17.  
  18.  
  19.      I had the need to write an 8088/86 instruction assembler, so I did, and 
  20. here is version 1.20 of the results.  Being such a low version number, this 
  21. program is far from perfect.  However, it should be sufficient for anyone
  22. wanting to implement an instruction assembler for their programs.
  23.  
  24.      This particular version of ASSEMBLE is written in Turbo Pascal, and 
  25. tested with Turbo Pascal, versions 5.0-6.0.  Here are the files included with 
  26. the archived version of ASSEMBLE:
  27.  
  28.              ASSEM120.PAS:  The whole entire assembler unit which defines,
  29.                             among other things, the procedure "Assemble"
  30.                             which does the assembling.
  31.              ASSEMBLE.PAS:  A program used to demonstrate the use of
  32.                             ASSEM120.  Uses a debugger to verify the
  33.                             output of procedure Assemble.
  34.              ASSEMBLE.EXE:  The compiled version of ASSEMBLE.PAS.
  35.              MNEMONIC.LST:  The data file for ASSEM120.
  36.              TRANSLAT.PAS:  Translation program to convert MNEMONIC.LST to
  37.                             MNEMONIC.BIN.
  38.              TRANSLAT.EXE:  The compiled version of TRANSLAT.PAS
  39.              MNEMONIC.BIN:  The translated version of MNEMONIC.LST
  40.              ASSEMBLE.DOC:  This document
  41.  
  42.  
  43.                        Operation of ASSEM120
  44.                        ---------------------
  45.  
  46.      The following paragraphs describe a brief description of ASSEM120, its 
  47. features, and how it works.
  48.  
  49.      Use the Assemble procedure from the ASSEM120 unit whenever you need to 
  50. get the machine language equivalent of an assembly language line of code.  For 
  51. instance, if you need to get the machine language form of "mov al,[bx+9]" you 
  52. would use, from your program, the line:  "Assemble('mov al,[bx+9]')" and it 
  53. will be assembled automatically for you.  Please note that the case of the 
  54. characters fed to Assemble is unimportant as there is a Capitalize procedure 
  55. in the ASSEM120 unit.
  56.  
  57.      The ASSEM120 unit makes four objects available to the program calling 
  58. it.  One of them, obviously (I hope!), is the Assemble procedure itself which 
  59. is defined this way:
  60.  
  61.                   Procedure Assemble(Instruct:  string);
  62.  
  63.              ASSEM120 also makes these things available:
  64.  
  65.                   Var
  66.                        BytePtr:  integer;
  67.  
  68.                        Bytes:  array[1..7] of byte;
  69.                        InstructionAddr:  word;
  70.  
  71.      The Bytes[] array will contain the bytes from your assembled instruction 
  72. after Assemble is finished.  BytePtr will point to the last byte that the 
  73. instruction uses PLUS ONE.  InstructionAddr is set during unit initialization 
  74. to be 0.  It reflects where the next instruction to be assembled will reside.  
  75. At any time, you may alter its contents to point to any location in a current 
  76. segment.
  77.  
  78.      For instance, it you wanted to assemble an instruction that resides at 
  79. location XXXX:0100, you would put $100 into InstructionAddr.
  80.  
  81.      When Assem120 initializes (it will initialize every time the program 
  82. starts) it will open and read its data file called "MNEMONIC.BIN".  This file 
  83. is derived from the file "MNEMONIC.LST" by the program "TRANSLATE.EXE".  
  84. "MNEMONIC.LST" is a text file that can be edited with a standard file editor.  
  85. It currently contains every mnemonic (that I know of) of the 8088/86 micro-
  86. processor.  Use the existing file as a guide for entering in your own instruc-
  87. tions (such as from an 80286 or 80386, or from a math coprocessor, etc.) if 
  88. you wish.
  89.  
  90.      The "MNEMONIC.LST" file is divided into a series of fields, including 
  91. the mnemonic, first byte, second byte, operand type, and class field.  I am 
  92. not going to delve into the specifics here.  If you really need to know them, 
  93. the ASEM120.PAS program code illustrates the use of all of the fields.  Feel 
  94. free to get in touch with me if you need further assistance.  But for now, the 
  95. mnemonic field contains the instruction.  The first byte field contains either 
  96. a hexadecimal number (proceeded by a "h") which represents the first byte that 
  97. the instruction generates or a binary number (proceeded by a "b").  If the 
  98. number is binary, it may have embedded characters to represent variable bit-
  99. fields.  These characters are case insensitive.  See the DecodeByte procedure 
  100. for details.  The second byte represents an actual second byte if the instruc-
  101. tion takes no operands (such as AAD).  Otherwise, it represents the REG field 
  102. of the second byte for operand types that use a specific 3-digit binary number 
  103. for the REG field (it might be handy to be referencing an instruction encoding 
  104. guide while reading this).  The operand type field represents the method in 
  105. which the instruction decodes its operands.  Again, an encoding guide would be 
  106. helpful here.  See the CONST section of ASEM120 for details on this.  Lastly, 
  107. the class field is my own personal breakdown of the 8088/86 instructions into
  108. classes.  See the comments in the GetOperandType procedure to see which in-
  109. structions each class includes.  When implementing your own instructions, you 
  110. may have to add your own class and/or operand type if you don't find any 
  111. existing ones that are the same.
  112.  
  113.      ASEM120 can handle most of the instructions that debuggers such as 
  114. Symdeb and Debug can handle.  I am not going to include a complete discussion 
  115. of what will and will not work, but in general, most everything that SYMDEB 
  116. can handle, ASEM120 can also handle.  Since ASEM120 is not a symbolic assem-
  117. bler, it will not handle labels.  Segments overrides are not treated as sepa-
  118. rate instructions.  So, to use them, you must prefix them to a memory location 
  119. like you would in a debugger (such as ES:[BX]).  Also, the REP instruction 
  120. will take another instruction as its argument (such as REP MOVSB) or it can be 
  121. used alone.  Since ASEM120 does not use labels, it is sometimes necessary to 
  122. specify the explicitly specify the size of data you are using.  Here are some 
  123. examples:
  124.  
  125.              mov [bx+9],byte ptr 8
  126.              mov [bx+si],word ptr 67h
  127.              call far [8]
  128.              call near [bx]
  129.              call far 0abcdh    (generates call 0000:abcd)
  130.              jmp short 97h      (SHORT is necessary for 8-bit relatives)
  131.              jmp near 97h
  132.              retf               (a FAR return)
  133.              retf 6             (a FAR return, pop 6 bytes)
  134.  
  135.  
  136.      One of the poorer aspects of ASSEM120 is its lack of error handling.  
  137. Because of this, if you make an error, you can't rely on ASEM120 to notice.  
  138. The algorithm ASEM120 is based on defaults everything.  That is, if you fail 
  139. to specify something important, or specify something incorrectly, ASEM120 will 
  140. may return incorrect results.  For instance, here are some possible invalid 
  141. entries, and what it will interpret those entries to be:
  142.  
  143.               mov al,bx        - - >       mov ax,bx
  144.               mov al,9876h     - - >       mov ax,9876h
  145.               push byte ptr [5] - ->       push word ptr [5]
  146.               push bl          - - >       push bx
  147.  
  148.      In some aspects, this lack of error checking is nice.  For instance if 
  149. you accidentally specify AL when you meant AX, it will automatically use AX.
  150. But what if you had some important data in AH?  Well then, you're in trouble 
  151. -- until I ever write an assembler with full error checking (don't hold your 
  152. breath!)
  153.  
  154.  
  155.  
  156.                         Running ASSEMBLE.EXE
  157.                         --------------------
  158.  
  159.      This is a demo program which demonstrates the use of ASSEM120.  Before 
  160. you run it, make sure you have a command.com program in the root directory of 
  161. drive C:, and that you have a SYMDEB program somewhere in your search directo-
  162. ry.  If you don't, just modify those entries in the CONST section of 
  163. ASSEMBLE.PAS, and run it.  DEBUG can be used in place of SYMDEB if you happen 
  164. to have this debugger.  Also, make sure that MNEMONIC.BIN is in the current 
  165. directory so that ASSEM120 knows where to find it.  One you have it running, 
  166. enter the offset address you wish to begin assembling in. After that, the 
  167. program continuously prompts you for instructions to assemble and assembles 
  168. it.  After each assembly, it ports the bytes into an intermediate script file 
  169. which it creates, and executes your debugger program.  The debugger then dis-
  170. assembles all the instructions from the starting address that you specified
  171. until the last instruction you have entered.  Since this all this is done each 
  172. and every time you enter an instruction, your previous instructions may not 
  173. stay in memory by the time the debugger starts to disassemble them the next 
  174. time.  If you enter the instructions starting at offset 100h, you shouldn't 
  175. have much of a problem, however.
  176.  
  177.  
  178.                            TRANSLAT.PAS
  179.                            ------------
  180.  
  181.      This quick, little, and virtually undocumented program simply converts 
  182. the MNEMONIC.LST file into the MNEMONIC.BIN file.  It doesn't ask any ques-
  183. tions, and I don't think it will even tell you "goodbye" when it leaves -- but 
  184. what it will do is strip a few of the text-oriented fields of MNEMONIC.LST 
  185. into single byte fields by the time it gets to MNENONIC.BIN.  The fields are 
  186. made up of the exact information that is defined in the EncodeRec type of 
  187. ASSEM120.  If the number of records (or lines in the .LST file) goes beyond 
  188. 160, you must increase it's max in both TRANSLAT.PAS and ASSEM120.PAS.  Do 
  189. this by updating the line "ChartType = array [1..160] of EncodeRec" which 
  190. resides in the TYPE section of both programs.
  191.  
  192.  
  193.                          In Conclusion ...
  194.                          -----------------
  195.  
  196.      Well I guess that's all I really have to say about ASSEM120.  Please
  197. remember:  if you have any questions, please get in touch with me.  I have
  198. provided my name, address, phone numbers, and CompuServe account number on the
  199. first page.
  200.  
  201.  
  202.                            Using my code
  203.                            -------------
  204.  
  205.      If you wish to develop ASSEM into your own program, either as is, or
  206. modified, I would appreciate some compensation for my time and effort.  The
  207. one-time, flat-fee for use of this copyrighted code is $15.00.  Please make
  208. all payments to the address at the top of this file.  Also, if anyone wants
  209. the code to be revised in some way, feel free to get in touch with me, and
  210. we'll talk about it.
  211.