home *** CD-ROM | disk | FTP | other *** search
/ CP/M / CPM_CDROM.iso / zsys / znode-12 / a / disa-rel.lbr / DISASM.DOC < prev    next >
Encoding:
Text File  |  1993-06-12  |  13.8 KB  |  341 lines

  1.                     DOCUMENTATION FOR DISASM
  2.  
  3.                          by Ronald Bruck
  4.  
  5. This program accepts as input a  .ERL or .REL  (Microsoft-format 
  6. relocatable)  file  and  disassembles  it  to  the  output  file.  
  7. The original file may very well be a library file, containing more 
  8. than one module.  The result is almost in a form for assembly via 
  9. M80 or RMAC -- see the example below.
  10.  
  11.                               USAGE
  12.  
  13. In addition to the DISASM.COM file, you must have the file
  14. OPCODES.TXT on drive A:.
  15.  
  16. The program is invoked by
  17.  
  18.     DISASM inputfile.ext outputfile.ext
  19.        (for translating inputfile to the textfile outputfile)
  20.     or
  21.  
  22.     DISASM inputfile.ext
  23.        (for translating inputfile to the default CON:)
  24.  
  25. It  is an error to invoke DISASM without any parameters,  and  the 
  26. user is given a message to that effect.
  27.  
  28. For example, consider the very simple (if useless) program
  29.  
  30. PROGRAM dummy;
  31. VAR
  32.   col, line : INTEGER;
  33.  
  34. EXTERNAL PROCEDURE gotoxy ( col, line );
  35.  
  36. BEGIN
  37.   gotoxy ( col, line );
  38. END.
  39.  
  40. After compilation by MT+ version 5.6 and disassembly of the .ERL file
  41. by DISASM we obtain the following actual output:
  42.  
  43.                       NAME      ('DUMMY  ')
  44.                       EXTRN     @INI,@HLT,GOTOXY
  45.                       PUBLIC    LINE,COL
  46.                       CSEG
  47. 0000'  00             NOP
  48. 0001'  00             NOP
  49. 0002'  00             NOP
  50. 0003'  00             NOP
  51. 0004'  00             NOP
  52. 0005'  00             NOP
  53. 0006'  00             NOP
  54. 0007'  00             NOP
  55. 0008'  00             NOP
  56. 0009'  00             NOP
  57. 000A'  00             NOP
  58. 000B'  00             NOP
  59. 000C'  00             NOP
  60. 000D'  00             NOP
  61. 000E'  00             NOP
  62. 000F'  00             NOP
  63. 0010'  C3             JMP  C$0013
  64. 0013'  2A    C$0013:  LHLD 0006h
  65. 0016'  F9             SPHL
  66. 0017'  CD             CALL @INI
  67. 001A'  2A             LHLD COL
  68. 001D'  E5             PUSH H
  69. 001E'  2A             LHLD LINE
  70. 0021'  E5             PUSH H
  71. 0022'  CD             CALL GOTOXY
  72. 0025'  CD             CALL @HLT
  73.                       DSEG
  74. 0000"          LINE:  DS   2
  75. 0002"           COL:  DS   2
  76.                       END
  77.  
  78. Note that the EXTERNAL declaration in the original program is mirrored
  79. by the EXTRN declaration in the disassembled file.  Note also that
  80. PUBLICLY declared items -- arising from the global variables and global 
  81. procedures/functions in the MT+ program -- are declared in a PUBLIC
  82. statement in the disassembled file.  Finally, internal entrypoints and
  83. data are denoted by C$nnnn and D$nnnn, respectively.
  84.  
  85. The disassembler does not construct DB's in the DSEG, only DS's, so it
  86. may fail on certain .REL files.  In the MT+ environment this is not a
  87. problem, since LINKMT does not accept DB's in the DSEG.
  88.  
  89. The first five characters at the left of the disassembly specify whether 
  90. the bytes are in the code-relative or data-relative sections (a single 
  91. quote stands for code-relative, a double-quote for data-relative).  
  92. DISASM does not support COMMON.
  93.  
  94. The FIRST BYTE of each item is given because the disassembly is not 
  95. perfect -- I have not attempted to have it disassemble embedded strings 
  96. or data in the code-relative segment.  It will treat this as code as
  97. long as it can, otherwise will use DB.  Since the first byte is available,
  98. you can manually change these items to DB.  
  99.  
  100. Note that CSEG and DSEG are specified, and that in contrast to the output
  101. of DIS8080, all reference chains have been filled in.  If there is no data,
  102. the DSEG statement will not be generated.
  103.  
  104.  
  105. The first 12 columns are always present -- they may be blanks (no tabs are
  106. used) -- so you can go into ED and do a macro:  BM12DL  to remove all of
  107. that information.  The resulting file can be reassembled by M80 to give
  108. a perfect copy of the original .ERL or .REL file.  (There may be a
  109. problem with seven-character names which coincide in the first six
  110. characters.)  Also, if the original file was a library file, the first
  111. END encountered will terminate assembly.
  112.  
  113. The companion program BREAKLIB can be used on the OUTPUT of DISASM.  
  114. (Strange things will happen if you use it on other files!)  It is expecially
  115. intended for use on files generated from DISASM applied to libraries.
  116. (In particular, PASLIB.ERL.)  When invoked by
  117.  
  118.                      BREAKLIB  filename.ext,
  119.  
  120. the following are created:
  121.  
  122.     (1)  The file  filename.REF, which contains (for each module
  123. in the original file) the NAME of the module, followed by (indented)
  124. lines identifying the PUBLIC entrypoints of those modules.
  125.  
  126.     (2)  For each module in the original file, having name "NAME",
  127. a file of name  NAME.MAC, containing the lines of the original file
  128. pertaining to that module, eliminating the first 7 characters, but
  129. leaving the first byte of the item so you can manually edit strings.
  130.  
  131. For safety's sake, BREAKLIB will never destroy an existing file.  When
  132. you apply it to PASLIB, expect LOTS of text, and a two-inch printout.
  133.  
  134.                        WHY A DISASSEMBLER
  135.  
  136. (a)  A disassembler and a code optimizer together can give more compact,
  137. faster programs.  Now all we're lacking is the optimizer!
  138.  
  139. (b)  You can disassemble files such as REALIO.ERL, for which source is
  140. not available from DRI, and write your own modules based on what you find.
  141. For example, I plan to write a "rational arithmetic" module which will
  142. replace the 80-bit BCD form of real numbers with an exact rational 
  143. arithmetic format.  This will allow, for example, exact 73-bit integer
  144. arithmetic (including the sign bit).  
  145.  
  146. (c)  When ordinary single-precision reals are used, the compiler allocates
  147. 4 bytes per real number.  When BCD reals are used, it allocates 10 bytes
  148. per real.  Disassembling the code, we can replace these 10 bytes by 8.
  149. (To do this automatically will require a special program which parses the 
  150. original source file in parallel with the disassembled code.)  This is a
  151. kludge to overcome DRI's negligence in not providing double-precision
  152. arithmetic.
  153.  
  154. I intend to write a double-precision real number package using the Hudson 
  155. 8087 board with my Compupro 8085/8088 board.  8080 MT+ will then be able 
  156. to do double-precision reals.  (I own MT+86 for the 8088 side of my machine, 
  157. but unfortunately DRI modified the compiler so it will no longer run in 
  158. 128K.  My money wasn't exactly wasted -- eventually I'll get more 
  159. memory -- but it is certainly lying fallow.)
  160.  
  161. (d)  You can disassemble PASLIB and rewrite the parts that you need
  162. rewritten.  I have done a similar job with BLAZEIO, trying to correct
  163. a bug and provide file-locking.
  164.  
  165.  
  166.                  RENAMING RELOCATABLE FILE ITEMS
  167.  
  168. Also provided is a program (RENREL) to RENAME the entrypoints of a .ERL 
  169. or .REL file.  You can also use this to remove the NAME FOR SEARCH items
  170. (unless your file is a library -- leave them in in that case!)
  171.  
  172. The main advantage of this is that M80 output files are limited to six-
  173. character names, whereas MT+ may emit up to seven-character names.
  174. Using RENREL, you can change the six-character names to seven.
  175.  
  176.                 A RELOCATABLE FILE READING MODULE
  177.  
  178. Also provided is MSMODULE (and MSTYPE) for sequentially reading Microsoft-
  179. format items from the bit-stream of a .ERL or .REL file and translating
  180. them to a format more easily used in Pascal.  (If I were more of a Pascal
  181. purits, I would have written the type "ms_item" found in MSTYPE in terms
  182. of variant records.  In fact, I did.  Keeping track of the variants was
  183. such a headache I went to the type declared here.)
  184.  
  185. MSTYPE is separated out of MSMODULE since any program which uses the
  186. "get_ms" and "put_ms" procedures is also going to need the type declarations.
  187. Just $I MSTYPE.PAS somewhere in the types of your main program.
  188.  
  189. DISASM was written before MSMODULE, and I never got around to rewriting it
  190. to use the module.
  191.  
  192.  
  193.                A TUTORIAL ON THE MICROSOFT FORMAT
  194.  
  195. A relocatable format is one which contains assembled code and
  196. relocation information.  In contrast with the 8086, 8080 code is
  197. not naturally relocatable:  JMP's and CALL's are to ABSOLUTE
  198. addresses, and if the code location is shifted all of these addresses
  199. become invalid.  One way to overcome this is to assemble the code
  200. as if it started at $0000, then store (in addition to the code)
  201. one bit for each byte of code, indicating whether the item is
  202. relocatable or not.
  203.  
  204. The Microsoft format goes several steps further:  it contains not 
  205. only relocation information, but also a good deal of symbolic 
  206. information (external references and names of entry-points).  The
  207. format is intended to be used with a linker.  
  208.  
  209. The  Microsoft relocatable-file format consists of a bit  stream:  
  210. individual fields are not aligned on byte boundaries,  except  as 
  211. noted  below.   There  are  two types  of  items:   ABSOLUTE  and 
  212. RELOCATABLE.   The  first bit of an item distinguishes the  type:  
  213. if a 0,  the next 8 bits are loaded as ABSOLUTE; if a 1, the next 
  214. 2 bits are used to determine the relocatable TYPE:
  215.  
  216.      00 = special item (see below);
  217.  
  218.      01 = Code relative.  Load the next 16 bits after adding
  219.           the current Program base.
  220.  
  221.      10 = Data  relative.   Load  the next 16 bits  after  adding           
  222.           the current Data base.
  223.  
  224.      11 = Common  relative.   Load the next 16 bits after  adding 
  225.           the current Common base.
  226.  
  227. A special item has a bit stream looking like this:
  228.  
  229. 100       xxxx      yy bb     zzz + symbol name
  230.           ----      -----     -----------------
  231.          Control   A-field         B-field
  232.           Field
  233.  
  234. where both the A-field and B-field are optional.
  235.  
  236. The A-field  yy  bb  consists of a two-bit address yy similar  to 
  237. the relocatable type code above,  except that 00 denotes ABSOLUTE 
  238. address; followed by the 16-bit field bb described by yy.
  239.  
  240. The  B-field  consists of a 3- bit field zzz  which defines 
  241. the length of a string, and as many bytes thereafter (up to 7) as
  242. are needed to define the symbol name.
  243.  
  244. The following types specified by the control field have a B-FIELD ONLY:
  245.      xxxx                 Type
  246.      ----      ------------------------------
  247.      0000      Entry symbol (name for search)
  248.      0001      Select COMMON block
  249.      0010      Program name
  250.      0011      Request library search
  251.      0100      Reserved (future expansion)
  252.  
  253. The following types have both an A-field and a B-field:
  254.  
  255.      xxxx                 Type
  256.      ----      ------------------------------
  257.      0101      Define COMMON size
  258.      0110      Chain external (A is head of address chain,
  259.                  B is name of external symbol)
  260.      0111      Define entry point (A=address, B=name)
  261.  
  262. The following types have an A-field only:
  263.  
  264.      xxxx                 Type
  265.      ----      ------------------------------
  266.      1000      External  - offset.   Used  for JMP and  CALL  to 
  267.                externals.
  268.      1001      External  + offset.   A value will be added to the 
  269.                two bytes starting at the current location counter 
  270.                immediately before execution.
  271.      1010      Define size of Data area = A.
  272.      1011      Set loading location counter to A
  273.      1100      Chain  address.   A=head  of  chain,  replace  all 
  274.                entries  in chain with current  location  counter.  
  275.                Last  entry  in  chain  has an  address  field  of 
  276.                absolute 0.
  277.      1101      Define program size = A.
  278.      1110      End program -- forces to BYTE BOUNDARY.
  279.  
  280. The following type has neither an A nor a B field:
  281.  
  282.      xxxx                 Type
  283.      ----      ------------------------------
  284.      1111      End of file.
  285.  
  286.  
  287.  
  288.      A  library  will contain one "End program" for  each  module 
  289. assembled into it; and only one End of file.
  290.  
  291.                           FINAL REMARKS
  292.  
  293. I have provided source code for DISASM although I intend to
  294. eventually modify and improve it, making it more interactive.
  295. Users are advised to get a copy of Ward Christiansen's public-
  296. domain RESOURCE (from CPMUG) if they want to disassemble .COM
  297. files.  (This is much more difficult, because you have NO symbol
  298. information available.)
  299.  
  300. A companion module, MSMODULE, contains procedures to read/write
  301. Microsoft-format files.  This was (essentially) extracted from
  302. DISASM.
  303.  
  304. The .ERL or .REL file is limited to 10K bytes.  Any more and an
  305. error message is displayed.
  306.  
  307. When DISASM is tricked in the CSEG and must produce a DB, it issues
  308. a SYNCHRONIZATION ERROR line following the DB -- since this is a comment
  309. line, it will not interfere with the assembler.
  310.  
  311. If the output file is not CON:, you will be shown a running summary
  312. of MEMAVAIL.
  313.  
  314. DISASM is not entirely bug-free. In particular, sometimes it 
  315. hangs -- I have no idea why.  It checks MEMAVAIL before every use 
  316. of NEW, and it never seems to be anywhere near the stack.  Despite
  317. the new manual's promise about "@HERR", I can't get the compiler or
  318. linker to recognize it.  
  319.  
  320. Also, since I make only one pass through the file, and don't back up 
  321. when a synchronization error is found (e.g., an opcode which requires 
  322. an absolute byte as the next byte, but has instead a relocatable word), 
  323. some address references are missed.  I arrange for these with ORG's 
  324. near the end of the CSEG.
  325.  
  326. Finally, when the output file is not CON:, it finishes with a spurious
  327. display of memory remaining.  This is such a minor bug I haven't 
  328. bothered to try to squash it.
  329.  
  330.  
  331.                       COMPLAINT DEPARTMENT
  332.  
  333. Please address all remarks, queries, complaints, suggestions for
  334. improvement, etc. to:
  335.  
  336.                     Professor Ronald E. Bruck
  337.                     Department of Mathematics
  338.                     University of Southern California
  339.                     Los Angeles, CA 90089
  340.  
  341.