home *** CD-ROM | disk | FTP | other *** search
/ CP/M / CPM_CDROM.iso / beehive / program / zsm.arc / ZSM.DOC < prev   
Encoding:
Text File  |  1991-08-11  |  33.3 KB  |  909 lines

  1.         ZSM, ZLINK, ZLIB, ZPATCH, ZC - Z80 assembler package
  2.  
  3. Background.
  4.  
  5.     I originally wrote this assembler with a view to using it as part
  6. of a C compiler, however I don't know when, if ever, I'll write the compiler.
  7. As for the assembler, I chose to write my own because the only other one I've
  8. worked with is Microsoft's M80, and while this is a good assembler, I find
  9. that it is a bit to restrictive in the way it deals with external labels. In
  10. addition, I wanted to make library management easy, hence the production of
  11. ZLIB, and the .lib directive.
  12.  
  13.  
  14. Usage.
  15.  
  16.     Assuming that you have used an editor to produce your source file,
  17.  
  18. A>ZSM FILENAME.Z -LSX -TB
  19.  
  20. is the command that will turn it into an object file. Source files must have
  21. the .Z extension, and object files will have a .O extension (or .L: see
  22. libraries and the .lib directive). Since the .Z must be on the source file,
  23. it is optional on the command line.
  24.  
  25.     The only four flags recognised by ZSM are -L -S -X and -TX. As
  26. implied above, they can be handed to in groups: -LSX has the same effect
  27. as -L -S -X. The -T flag can be given in a group, but if this is done is
  28. is best made the last one, as it requires a following character.
  29.  
  30.     The -L flag generates a list file: in the above example it would have
  31. the name FILENAME.LST. List files are full of all sorts of goodies: in
  32. addition to the source, a fairly comprehensive copy of the object code is
  33. produced; ZSM also inserts page breaks in the right places, so you don't get a
  34. line of listing over the place where the paper tears; finally a cross
  35. reference page is produced at the end showing all the labels, variables and
  36. macros defined in the assembly.
  37.  
  38.     The -S flag generates a symbol table file called FILENAME.SYM, in the
  39. right format for use with ZSID (Digital Research's Z80 Symbolic Interactive
  40. Debugger). These symbols will only be correct if FILENAME.O is linked on it's
  41. own because, as will be seen later, linking in other files will throw some of
  42. the references off. Note also that since ZSID hasn't latched onto the fact
  43. that there is such a thing as lower case, all labels are converted to upper
  44. case before being written to the .SYM file. As a result, a little care and
  45. attention with labels will make work with ZSID a lot easier.
  46.  
  47.     The -X flag is used when a single source file is being assmbled
  48. and linked, this causes a linker built into ZSM to produce a .COM file
  49. directly, in addition to the .O file. This can represent a fair saving
  50. of time on a big file (source in the order of 32K), since ZLINK is a two
  51. pass linker (1st. pass sets up certain addresses and values); whereas
  52. the linker in ZSM only needs one pass as it already has evaluated the
  53. necessary addresses and values. The -X flag will be ignored if -
  54. A: the source contains external references (except #end - see ZLINK below),
  55. B: there are any errors in the source,
  56. C: the source produces a library.
  57.  
  58.     The -TX flag tells ZSM to place it's temporary files on drive X:
  59. usually ZSM places it's temporary files on the same drive as the source,
  60. by doing this, the temporary files are placed elsewhere. This sometimes
  61. allows assemblies to be done on otherwise almost full disks, or by putting
  62. the temporary files on a ramdisk, speed of assembly can be improved.
  63.  
  64.     As an alternative to supplying the arguments on the command line
  65. that invokes ZSM, it is possible to assemble several files with ZSM, start
  66. by giving it no arguments, then after it has signed on, it will prompt
  67. with a '=', and a command line typed in will be processed, after which
  68. ZSM will prompt again for another command line. This process will repeat
  69. until an empty line is given, allowing multiple assemblies without the
  70. overhead of loading ZSM each time.
  71.  
  72.     Now that FILENAME.O has been produced:
  73.  
  74. A>ZLINK FILENAME.O -O OUTNAME LIB.L -S -TX
  75.  
  76. is the sort of thing that will link it. FILENAME.O is a single object file,
  77. and always gets linked in. In the absence of a -O flag ZLINK gives the
  78. output .COM file the same name as the first file it encounters on the
  79. command line, however in the above example the output file will be called
  80. OUTNAME.COM, because of the -O OUTNAME present in the command line. Note
  81. that the name referenced by the -O flag can be a .O or .L file:
  82.  
  83. A>ZLINK ARX.O -O PROGRAM.O LIB.L -S
  84.  
  85. is perfectly acceptable, giving PROGRAM.COM from ARX.O & PROGRAM.O etc.
  86. The -TX flag works just like in ZSM - it places ZLINK's temporary files
  87. on drive X.
  88.  
  89.     LIB.L is a library: this is best considered as a list of .O
  90. files (or modules) all joined together, however instead of linking them all
  91. in, as happens with .O files in the command line, a .O module in a library
  92. only gets linked in if it will resolve a currently undefined label. Note also
  93. that the following is possible: if LIB1.L and LIB2.L both contain .O modules
  94. that would resolve the undefined external label "input", then the linkage:
  95.  
  96. A>ZLINK PROGRAM.O LIB1.L LIB2.L
  97.  
  98. would link in the module from LIB1.L to resolve "input", and leave the
  99. corresponding module in LIB2.L out.
  100.  
  101.     The -S flag has a similar effect to ZSM: it generates a symbol
  102. table file for ZSID, however it only contains labels defined as external
  103. in the assembly, internal labels will not show up. Note that the same
  104. restrictions apply regarding upper & lower case.
  105.  
  106.     ZLINK also recognises three other flags: -CXXXX, -DXXXX, -UXXXX.
  107. Under normal conditions, ZLINK puts all the code first, then the data
  108. immediately following the code, then the uninitialised data areas; and the
  109. addresses are set up so that the code starts at 100 hex: for a standard CP/M
  110. program. However by using the -C, -D and -U flags, it is possible to move
  111. these sections around, so to give an example:
  112.  
  113. A>ZLINK PROM.O -C0 -U4000
  114.  
  115. would produce a .COM file where the data followed right after the code (no -D)
  116. but the code started at 0 hex, not 100 hex, and the uninitialised data started
  117. at 4000 hex, typically this would be used to produce code to burn into a prom
  118. in a dedicated environment, where the prom code starts at zero, and there is
  119. a ram chip at address 0x4000. Alternatively:
  120.  
  121. A>ZLINK BIOS.O -CF400
  122.  
  123. would link up code for a CP/M bios, starting at 0xf400, with data and
  124. uninitialised data following right after.
  125.  
  126.     Note also that ZLINK creates one external label itself when linking
  127. code: "#end": this is defined as the address of the first byte after the end
  128. of the uninitialised data.
  129.  
  130.     Like ZSM, ZLINK can be made to prompt for its arguments, however the
  131. action taken by ZLINK in response to multiple line input is a little
  132. different. Instead of processing each line on it's own, ZLINK internally
  133. joins everything together and does one big linkage. This is especially useful
  134. when large numbers of .O and .L files are being linked, as it allows the files
  135. to be entered in groups. Note that several files can be given in response
  136. to one '=' prompt. While doing the linkage, in regards to order of files,
  137. the files are scanned from left to right on a given line, and first line
  138. first. One important thing to note in both ZSM and ZLINK: termination of
  139. input occurs when a line containing no arguments is given, in particular
  140. a single space is considered as no arguments, the practical upshot of which
  141. is that exit from ZSM and ZLINK under SUBMIT and XSUB can be achieved
  142. gracefully (c.f. PIP).
  143.  
  144.     ZSM itself (as will be noted) does not complain about undefined
  145. labels, it just notes them and passes them right along to ZLINK, however if
  146. ZLINK cannot resolve a reference, then it will complain, in addition it will
  147. complain if a label is defined more than once.
  148.  
  149.  
  150. Libraries.
  151.  
  152.     ZLIB, the librarian is invokes as follows:
  153.  
  154. A>ZLIB LIBRARY.L
  155.  
  156. and as in ZSM, since the .L is mandatory on the file being processed, it is
  157. optional on the command line. If LIBRARY.L does not exist, it is created, and
  158. if it does, then it is saved in LIBRARY.LBK to provide a backup. Once ZLIB has
  159. signed on, it will prompt for each command, once a command is given, ZLIB will
  160. prompt further for the other arguments necessary.
  161.  
  162.     Commands available to ZLIB are:
  163.  
  164.      1. L(ist) - Print a list of all .O modules currently in the LIBRARY file.
  165.     .O modules are tagged, depending on what action is to be taken: 'k'
  166.     means the file will be killed, '>' means it will be output to a .O
  167.     disk file, '<' means it has been input from a disk .O file, '<FILE.L'
  168.     means it has been copied over from another library. Finally, if you
  169.     append another library file, the name of the second .L file shows up
  170.     surrounded by '--'s, see the example below
  171.  
  172.     1:    MODULE1.O        ; Left alone - gets copied over
  173.     2:    MODULE2.O   k        ; killed: will be removed from .L file
  174.     3:    MODULE3.O   >        ; output: will create MODULE.O on disk
  175.     4:    MODULE4.O   k>        ; extract: does both of the above
  176.     5:    MODULE5.O   <        ; input disk file MODULE5.O to library
  177.     6:    MODULE6.O   <FILE1.L    ; search MODULE6.O in from FILE1.L
  178.     7:    -- FILE2.L --        ; append library file FILE2.L
  179.     8:    MODULE7.O        ; left alone
  180.     9:    [end]
  181.  
  182.      2.    K(ill) - asks for a module: you can specify it by name (MODULE2
  183.     in the above example, or by number (i.e. place in the library file)
  184.     when all the work is finished, this module will be removed from the
  185.     library file.
  186.  
  187.      3. O(utput) - asks for a module as in kill, when all work is finished
  188.     a .O file is created on disk containing the appropriate module.
  189.  
  190.      4. E(xtract) - asks for a module, then does both a kill and an output to
  191.     it.
  192.  
  193.      5. I(nput) - asks first where the new module is to be input, note that
  194.     the new module will be placed immediately before the one given, in
  195.     the above example, if the input command (working on MODULE5.O) is the
  196.     last command given, then MODULE5.O would be input immediately before
  197.     MODULE6.O. In this case it is permissible to specify [end] as a
  198.     module, which would add the new module right on the end of the
  199.     library. Once the position has been determined, ZLIB asks for the
  200.     name of the .O file to be input.
  201.  
  202.      6. S(earch) - this command behaves in a similar manner to input: however
  203.     instead of asking for a .O file on disk, it asks for a .L file, and
  204.     when one is given, it is listed, and a module from the .L file is
  205.     chosen: it will be placed in the output .L file at the point chosen.
  206.  
  207.      7. A(ppend) - asks for the name of a library file on disk, and simply
  208.     appends the given file to the end of the current work file.
  209.  
  210.      8. D(one) - Commands which have an effect (2 - 7) are not implemented
  211.     as they are typed, instead a note is made to execute each command,
  212.     and when the done command is given, all the commands are executed.
  213.     Note that the file being processed gets backed up in a .LBK file, in
  214.     case you want to keep your original.
  215.  
  216.      9. Q(uit) - leaves ZLIB without doing any of the changes specified.
  217.  
  218.     As implied above, all of the commands are recognised by their initial
  219. letter, so kill, k, krunch all work equally well.
  220.  
  221.  
  222. Patching.
  223.  
  224.     ZPATCH.COM is a small program designed for doing "Hot Patches" to
  225. .COM files that already exist without needing to mess around with ZSID and
  226. SAVE.
  227.  
  228. A>ZPATCH PROGRAM.COM PATCHES.O
  229.  
  230. is a typical command line invoking ZPATCH. PROGRAM.COM is the file being
  231. patched, and PATCHES.O is the patch file - like ZLIB, since there is no
  232. ambiguity of file extensions they are optional on the command line: if not
  233. present then they will be added by ZPATCH.
  234.     PATCHES.O would be created by assembling a .Z source file with ZSM,
  235. and there are several limitations imposed on the source: first there can be
  236. no .dseg or .useg code or references (see section on directives for meaning
  237. of .dseg & .useg); also there can be no external references.
  238.     The behavior of ZPATCH depends on what it has just encountered in
  239. the .O file: the only two significant items are .org directives (including
  240. ds opcodes) and "real" code (i.e. instructions or db / dw opcodes). If
  241. ZPATCH finds a .org directive (or a ds opcode), then it will start copying
  242. bytes over without modification, however when it encounters code or data
  243. in the .O file it replaces the bytes in the old version of the file with
  244. the bytes specified in the .O file.
  245.     To give an example, consider the following .Z source file:
  246.  
  247. .org    0x0100
  248. start:    jp    patch
  249. .org    0x0862
  250. entry:
  251. .org    0x2000
  252. patch:    ld    de,string
  253.     ld    c,9
  254.     call    5
  255.     ld    hl,entry
  256.     ld    (start + 1),hl
  257.     jp    start
  258. .org    0x2860
  259. string:    db    'Signon message\r\n$'
  260.  
  261. The first line sets the label start: at 100 hex, which is the start of a
  262. CP/M program. The second line is a jump that will replace the original
  263. jump so that the code at patch: gets executed on startup. The third and
  264. fourth lines set the label entry: to 862 hex which was the original entry
  265. point of the program. The fifth line sets 2000 hex as being the address
  266. where part of the patch will go - this simply prints a signon message
  267. resets the jump at the start of the program and starts again at 100 hex.
  268. The final .org might be there because there isn't enough room at 2000 hex
  269. to include the signon string, so another patch area has to be used: 2860
  270. hex in this case.
  271.  
  272.     If the .O file finishes it's work before the end of the .COM file
  273. then the only changes will be the inclusion of the patches, however if the
  274. .org's in the .O file extend beyond the end of the .COM file then the
  275. intervening spaces will be filled with zero bytes.
  276.  
  277.     When ZPATCH has finished it's work, it will have created a new .COM
  278. file, however it leaves the original in a .CBK file to provide a backup.
  279.  
  280.  
  281. Doing it all at once:
  282.  
  283.     ZC is based loosely on UNIX CC: the C compiler, with some MAKE
  284. thrown in (make is a program that does the minimum amount of work needed
  285. to bring a multi source executable up to date when you edit one or more
  286. of the sources). In particular, on the same disk as ZC.COM you should have
  287. ZSM.COM, ZLINK.COM, ARX.O and friends (see ARX.DOC), and LIB.L. A typical
  288. usage would be:
  289.  
  290. A>ZC B:FILE.Z
  291.  
  292. which would firstly invoke ZSM to assemble B:FILE.Z to B:FILE.O, and then
  293. invoke ZLINK to link ARX.O, B:FILE.O, and LIB.L together to give B:FILE.COM.
  294. By specifying several sources on the command line, ZC can be made to glue
  295. them all together into one:
  296.  
  297. A>ZC B:FILE1.Z B:FILE2.Z
  298.  
  299. would assemble the two sources, and then link them with ARX.O and LIB.L.
  300.  
  301.     As well as handling .Z files, ZC knows about .O files and .L
  302. libraries, so:
  303.  
  304. A>ZC B:FILE3.Z C:FILE4.O C:LIBRARY.L
  305.  
  306. is fair game: it would assemble B:FILE3.Z to B:FILE3.O, and then link
  307. everything together.
  308.  
  309.     ZC has a whole host of options:
  310.  
  311. -S: generate a .SYM symbol table file - this is just passed along to ZLINK.
  312.  
  313. -X: alter which ARX.O header is used: by saying -XSTR, STRARX.O is used
  314.     instead of the regular ARX.O (for a list of the ARX.O files available
  315.     and what they do see ARX.DOC). As a special case, just saying -X
  316.     with no following string causes no ARX.O module to be used at all.
  317.     When a string is provided, ARX.O is added to the end of it, and the
  318.     resulting filename is used, so -XB: would use B:ARX.O, and -XC:Q
  319.     would use C:QARX.O etc.
  320.  
  321. -L: add extra libraries, or disable LIB.L. This works much like -X, except
  322.     that these are additive: so -LB:X would include B:XLIB.L as well
  323.     as LIB.L. Note that this option is position sensitive, in effect
  324.     -LX is a shorthand that gets expanded to XLIB.L, in the same place
  325.     as the -LX was in the command line. Again -L is a special case, it
  326.     causes LIB.L not to be included, but any other libraries selected
  327.     with other -L options still remain.
  328.  
  329.     Note that in normal usage, the selected ARX.O file always is the
  330.     first thing to be linked, and LIB.L always follows all other files
  331.     and libraries.
  332.  
  333. -TX: move temporary files around - this is simply passed straight along to
  334.     ZSM and ZLINK, and acts as explained above.
  335.  
  336. -PX: look elsewhere for programs and ZLINK files: normally ZC looks on the
  337.     current drive for programs (ZSM & ZLINK) and ZLINK files (ARX.O etc.
  338.     & LIB.L etc.). This option tells it to look on drive X instead - so
  339.     if B: were the current drive:
  340.  
  341.     B>A:ZC XYZ.Z -PA
  342.  
  343.     would cause ZSM etc. to be pulled from A: as opposed to the default
  344.     drive B:
  345.  
  346. -O: behaves just like the -O flag in ZLINK - it alters the name of the output
  347.     .COM executable. As in ZLINK, the -O can be followed by a plain
  348.     FILENAME, in which case the name is used, or it can be placed in
  349.     front of a .Z, .O or .L file, in which case the name is transferred
  350.     to the output file, as well as using the specified file for the
  351.     assembly / linkage.
  352.  
  353. -B: in a case such as:
  354.  
  355.     A>ZC FILE1.Z FILE2.Z FILE3.Z -B
  356.  
  357.     the -B causes the .Z files to assembled only if corresponding .BAK
  358.     files exist. This is the 'Make' portion of ZC: by using this
  359.     command option, only those files that need assembly have ZSM invoked
  360.     for them. As an addition, -BE has the same effect, but in addition
  361.     to doing the assembly, if the assembly succedes, the .BAK file is
  362.     erased.
  363.  
  364. -A: when ZC is invoked, it usually links the output .O files together. This
  365.     causes the ZLINK phase to be bypassed, stopping with production
  366.     of the .O files only. This is most useful for assembling a collection
  367.     of .Z files, without linking them:
  368.  
  369.     A>ZC B:FILE1.Z B:FILE2.Z B:FILE3.Z -A
  370.  
  371.     just assembles the three .Z files and stops.
  372.  
  373. -CXXXX, -DXXXX, -UXXXXX: these options are simply passed straight through
  374.     to ZLINK, where they have the effects noted above.
  375.  
  376.  
  377. Features.
  378.  
  379.     ZSM recognises standard ZILOG neumonics, along the lines of
  380.  
  381.     ld    a,(hl)
  382.  
  383. or
  384.  
  385.     inc    (ix+0x76)
  386.  
  387. etc. etc. - these are not covered here as there are many good text books
  388. covering writing assembly language on the Z80. Comments start with a ';'
  389. and continue to the end of the line, to define a label, put the label name
  390. at the start of the line and follow it with a ':'. A typical line of code
  391. might then be:
  392.  
  393. setup:    add    a,17            ; add the offset to a
  394.  
  395. Line length is limited to 100 characters - anything else is simply truncated,
  396. the only time this is likely to cause an assembly error is if a long string
  397. is given to a 'db' statement.
  398.  
  399. Case is not significant in opcodes, reserved operands or directives, so
  400.  
  401.     ld    a,(hl)
  402.     LD    A,(HL)
  403.     Ld    a,(hL)
  404.  
  405. are all legal and all produce the same thing.
  406.  
  407. Targets of jp and call opcodes are covered later, but in the case of jr and
  408. djnz opcodes, the target can be one of three things:
  409.  
  410. 1.    jr    label
  411.  
  412. simply jump to the specified label;
  413.  
  414. 2.    jr    label + expression
  415.  
  416. jump to expression bytes offset from label: to give an example
  417.  
  418.     jr    input + 3
  419.  
  420.     .......
  421.  
  422. input:    ld    hl,buffer
  423.     call    getdata
  424.  
  425. in this case, the jr would jump to the call, bypassing the ld hl instruction;
  426.  
  427. 3.    jr    expression
  428.  
  429. jump the specified number of bytes, for example
  430.  
  431.     dec    a
  432.     jr    nz,-3
  433.  
  434. would implement a timing loop. In addition the '@' term is allowed in jr
  435. targets (see expressions) so the above loop could also be implemented as
  436.  
  437.     dec    a
  438.     jr    nz,@ - 1
  439.  
  440.  
  441. db:    define a list of bytes, note that strings enclosed in quotes are
  442.     legal, and as in C, the backslash is used as an escape character:
  443.     to give an example:
  444.  
  445.     string:    db    'Can\'t find file'
  446.  
  447.     by preceding the quote in the string with a backslash, it becomes a
  448.     character in the string rather than a string delimiter. There are a
  449.     total of 9 cases where the backslash has a significant effect:
  450.  
  451.     \r    generates a carriage return (0x0d)
  452.     \n    generates a linefeed (0x0a)
  453.     \b    generates a backspace (0x08)
  454.     \t    generates a tab (0x09)
  455.     \f    generates a formfeed (0x0c)
  456.     \e    generates an escape (0x1b)
  457.     \\    generates a backslash (0x5c)
  458.     \'    generates a single quote (0x27)
  459.     \OOO    generates the octal character OOO
  460.  
  461.     in this last case OOO can be at most 3 octal digits, to give a few
  462.     examples: '\033' generates an escape, as does '\33', '\0' generates
  463.     a null, '\177' generates a delete char. To briefly explain, when
  464.     ZSM finds a backslash followed by an octal digit ('0' to '7') it
  465.     will continue scanning until either it finds a character which is
  466.     not a legal octal digit, or it has scanned three characters: so
  467.     '\0034' will generate 0x03 0x34, the \003 gives the 0x03, and the
  468.     4 gives the 0x34. Note that a single backslash preceding any other
  469.     character is ignored:
  470.  
  471.     'stringX'
  472.  
  473.     and
  474.  
  475.     'string\X'
  476.  
  477.     generate exactly the same thing. Typical use of the \r type sequences
  478.     might be in a case such as
  479.  
  480.     db    'Data error:\r\nTry again please\r\n'
  481.  
  482.     which is somewhat easier than
  483.  
  484.     db    'Data error:',0xd,0xa,'Try again please',0xd,0xa
  485.  
  486.     although both would generate the same thing. Alternatively, to ring
  487.     the bell on a video screen a string such as
  488.  
  489.     db    'ERROR\007'
  490.  
  491.     or
  492.  
  493.     db    'ERROR\7'
  494.  
  495.     could be used. As is shown above, multiple strings can be given, they
  496.     are simply concatenated and output in the order given. Note also that
  497.     expressions are legal, so
  498.  
  499.     db    'This "', 'c' & 0x1f, '" is a control C'
  500.  
  501.     would be perfectly legal, generating a control C in the middle of the
  502.     text (even though ' ..."\003"... ' would do the same thing)
  503.  
  504. dw: generates words of data, arranged in the right order (low byte first)
  505.  
  506.     dw    0x1234, label
  507.     dw    label - 0x1000
  508.  
  509.     unlike db's, dw "operands" can be relocatable, so a jump table might
  510.     be implemented as follows:
  511.  
  512.     dw    l1,l2
  513.     dw    l3,l4
  514.     dw    0
  515.  
  516.     with the zero word flagging the end of the table. Note also that while
  517.     up to 100 bytes can be generated with just one db statement, the limit
  518.     is 2 words (4 bytes) with one dw statement - the table above could not
  519.     be implemented as:
  520.  
  521.     dw    l1,l2,l3,l4,0
  522.  
  523. ds: generate a space of the given size:
  524.  
  525.     ds    0x1000
  526.  
  527.     if given in the .cseg or .dseg sections of a program, then the space
  528.     is guaranteed to be zero filled, if given in the .useg section, then
  529.     the contents will be garbage at the start of program execution (see
  530.     section on directives for an explanation of .cseg, .dseg, .useg)
  531.  
  532. Finally, ZSM does not need a .end directive to make it stop (indeed there is
  533. no provision for recognising such a thing) - it simply keeps on going till it
  534. falls off the end of the source file.
  535.  
  536.  
  537. Identifiers.
  538.  
  539.     An identifier is a collection of seven or fewer characters, given that
  540. the first must be a letter (ZSM considers the underscore and hash characters
  541. '_' and '#' as letters), and the remaining six must be letters or numbers. ZSM
  542. is case sensitive regarding identifiers, but all characters following the
  543. seventh are ignored, so
  544.  
  545. Foo:
  546. foo:
  547.  
  548. are different labels, whereas
  549.  
  550. long_label
  551. long_label1
  552.  
  553. are not. Opcodes, reserved operands & directives cannot be used as
  554. identifiers: you cannot have a variable called 'hl' etc. etc.
  555.  
  556.  
  557. Expressions.
  558.  
  559.     ZSM recognises two types of expression: absolute or relocatable.
  560. An absolute expression is one composed of variables and constants, i.e. one
  561. that can be evaluated fully by the assembler during pass 1 - these can be
  562. used anywhere. In addition, if an opcode has a word operand such as
  563.  
  564.     call    address
  565.     ld    hl,(address)
  566.     dw    address1, address2
  567.  
  568. then the address can be considered as a relocatable expression, which is
  569. simply a label, followed by an optional absolute expression: for example
  570.  
  571.     call    setup
  572.     ld    hl,(table + 0x0100)
  573.     dw    swtab - 2, 0x1234
  574.  
  575. In the first of the above examples 'setup' is a label, in addition to using
  576. a label, the term '@' refers to the start of the current instruction: so
  577.  
  578.     jp    p,@ + 6
  579.     ld    hl,.....
  580.  
  581. will only do the 'ld hl' if the 'm' flag is set. In absolute expressions,
  582. there are seven levels of operator precedence:
  583.  
  584.     1.    || &&: logical or / logical and. x && y is true (1) iff
  585.         both x and y are true (non-zero), x || y is true iff
  586.         either x or y or both are non-zero.
  587.  
  588.     2.    == != >= <= > <: relational operators: in order shown
  589.         equal, not equal, greater than or equal, less than or equal
  590.         greater than, less than. Note that expressions are unsigned,
  591.         so -1 < 0 is false as -1 is considered to be 65535
  592.  
  593.     3.    >> <<: right shift, left shift: 1 << 3 is 8, 0x8000 >> 15 is
  594.         1, and not 0xffff: right shift is zero fill
  595.  
  596.     4.    & | ^: bitwise and, bitwise or, bitwise exclusive or.
  597.         Note that 1 & 2 is zero, whereas 1 && 2 is 1
  598.  
  599.     5.    + -: binary addition and subtraction
  600.  
  601.     6.    * / %: binary multiply, divide, modulus. Note that division by
  602.         zero does not generate an error: it just returns zero, and
  603.         the % operator gives remainder on division: so 10 % 3 is 1,
  604.         again anything % 0 gives zero, without an error
  605.  
  606.     7.    ! - + ~ [] {}: unary logical not, unary minus, unary plus,
  607.         unary bitwise not, brackets to force order of evaluation,
  608.         braces to evaluate a relocatable difference.
  609.  
  610.         ! 0 is 1
  611.         ! 1 == ! 76 == 0 as is ![any non-zero value]
  612.         ~ 0 == 0xffff
  613.         ~ 0xc000 == 0x3fff
  614.  
  615.         Square brackets are used to force order of evaluation as they
  616.         solve problems in potentially ambiguous cases such as
  617.  
  618.         ld    hl,('0' << 8) + ' '
  619.  
  620.         In an expression, a term with the form
  621.  
  622.         {label1 - label2}
  623.  
  624.         will be evaluated as the difference between label1 and label,
  625.         typically it can be used to determine the size of a table, or
  626.         the length of a section of code. Certain restrictions apply to
  627.         the use of such terms: they can be used in both relocatable
  628.         and absolute expressions:
  629.  
  630.         ld     bc,{endtbl - table}    ; get size of table to bc
  631.  
  632.         in this case the only limitation on endtbl and table is that
  633.         they be defined in the current source code file, and that they
  634.         be in the same segment. However in a case such as
  635.  
  636.         .var    ident    {label - base}
  637.  
  638.         or
  639.  
  640.         ds    {end - start}
  641.  
  642.         these are absolute expressions, and as such they must be
  643.         evaluated immediately on being encountered, hence the labels
  644.         they contain must already have been defined otherwise an
  645.         error will result. This is to prevent paradoxes such as this
  646.         ocurring
  647.  
  648.         table:
  649.  
  650.             ......
  651.  
  652.  
  653.             ds    {endtbl - table}
  654.  
  655.             .......
  656.  
  657.         endtbl:
  658.  
  659.         Note also that '@' can be used as a legal "label" in { }
  660.         terms, so code such as
  661.  
  662.         label:
  663.  
  664.             .........
  665.  
  666.  
  667.         .var    ident    {@ - label} * 2
  668.  
  669.         or
  670.  
  671.         ld    bc,{foo - @}
  672.  
  673.         are both legal.
  674.  
  675.     Constants are assumed in base 10, unless there is a leading 0X / 0x
  676. for hexadecimal (both 'A' - 'F' and 'a' - 'f' are allowed), 0 for
  677. octal, or 0B / 0b for binary, in addition a single character enclosed in
  678. quotes is considered a legal constant by the expression parser, so
  679.  
  680.     db    0x4D        ; hexadecimal
  681.     db    77        ; decimal
  682.     db    0113        ; octal
  683.     db    0b1001011    ; binary
  684.     db    '\113'        ; octal character
  685.     db    'M'        ; ascii character
  686.  
  687. all produce the same thing
  688.  
  689.  
  690. Directives
  691.  
  692.     There are 14 directives: they are introduced by starting a line
  693. with a period:
  694.  
  695. 1. .cseg, .dseg, .useg: define code, data or uninitialised data segments.
  696.     These are the segment directives that are used when generating
  697.     standard CP/M .COM files: .cseg is aimed to hold all the executable
  698.     code, .dseg holds initialised data, and .useg holds uninitialised
  699.     data. These can be freely intermixed in the source file: by the
  700.     time ZLINK has finished all the .cseg code will be contiguous in
  701.     the .COM file, followed by the .dseg data. After the .dseg data
  702.     is finished, .useg space begins. Two things to note: ZSM starts
  703.     assuming .cseg; and .useg space can only contain label definitions,
  704.     ds opcodes, and .org directives (q.v.) .useg is best used for
  705.     allocating space for large tables that are to be filled in at run
  706.     time. To give a couple of examples:
  707.  
  708.     .cseg
  709.         ld    de,string
  710.     .dseg
  711.     string:    db    'Hello World!\r\n$'
  712.     .cseg
  713.         ld    c,9
  714.         call    5
  715.         jp    0
  716.  
  717.     will print 'Hello World!' because the string is pulled out of the
  718.     middle of the code; similarly
  719.  
  720.     .cseg
  721.         ld    hl,table
  722.     .useg
  723.     table:    ds    0x0100
  724.     .cseg
  725.         ld    de,table+1
  726.         ld    bc,0x00ff
  727.         ld    (hl),0xff
  728.         ldir
  729.  
  730.     will fill a table with 256 bytes all -1, further the table will
  731.     not occupy any space in the .COM file, .useg can only be used to
  732.     create labels for reference purposes: before the ldir is executed
  733.     the table will be full of junk.
  734.  
  735. 2. .org: origin, which allows holes to be left in code, which will be
  736.     zero filled in .cseg & .dseg, junk filled in .useg. Note that
  737.     .org's refer to the base of the current segment, and .org's
  738.     only move forward, so
  739.  
  740.     .org    0x4000
  741.  
  742.     followed by
  743.  
  744.     .org    0x3000
  745.  
  746.     will create a hole of 60K in the output file. In the absence of any
  747.     .orgs, ZLINK puts the .COM file together to run at 0x0100 so it will
  748.     work right under CP/M. The main use for these is putting code in the
  749.     right place with ZPATCH, because results of using this directive in
  750.     multi-source files is not particularly useful, and moving things
  751.     around with ZLINK is far easier with the -C, -D and -U flags.
  752.  
  753. 3. .extern: define a label as external:
  754.  
  755.     .extern    label
  756.  
  757.     By default all labels are defined internal, unless a .extern is used,
  758.     however when used, if a label is not defined in the current file, it
  759.     is automatically assumed to be defined externally in another module
  760.     which will be linked in by ZLINK. The exception to this is jr targets:
  761.     these must (a) be defined in the current file; and (b) exist in the
  762.     same segment. As a result of all of this, ZSM will never complain
  763.     about undefined labels, it just notes them and leaves it for ZLINK to
  764.     sort it all out (in much the same way as a C compiler handles
  765.     procedure definition and usage).
  766.  
  767. 4. .incl: include a .I file:
  768.  
  769.     .incl    "file"
  770.  
  771.     causes FILE.I to be included: these are most useful to hold macro
  772.     definitions and variable declarations (qq. v.), see STDHDR.I and
  773.     BDOS.I for typical examples.
  774.  
  775. 5. .macro, .endm: macro definition:
  776.  
  777.     .macro    load    r1,r2,offset
  778.         ld    r2,(ix+offset)
  779.         ld    r1,(ix+[offset]+1)
  780.     .endm
  781.  
  782.     is a typical macro definition: in usage a line of the type:
  783.  
  784.         load    h,l,10
  785.  
  786.     generates the following:
  787.  
  788.         ld    l,(ix+10)
  789.         ld    h,(ix+11)
  790.  
  791.     As regards macro arguments, when the macro is defined, they must
  792.     be legal identifiers, however when the macro is used they can be
  793.     any eleven characters except ',' and ';' as these two are reserved
  794.     for delimiting. A macro may have any number of arguments up to
  795.     eight, if a macro is invoked with too many arguments, then the
  796.     extra ones are ignored, and if there are too few arguments, then
  797.     the undefined ones are left blank. Macro usages can nest up to a
  798.     maximum depth of five, and recursive macros are only detected by
  799.     the excessive nesting depth.
  800.  
  801. 6. .var: set a variable:
  802.  
  803.     .var    variable    expression
  804.  
  805.     the above line defines variable if it doesn't already exist, and
  806.     assigns it the value of the expression. To give a real life example:
  807.  
  808.     .var    bdos    5
  809.  
  810.     allows a line of the type
  811.  
  812.         call    bdos
  813.  
  814.     to get into CP/M. There can be no duplication of names, i.e. no two
  815.     of:
  816.  
  817.     foo:
  818.     .var    foo    ....
  819.     .macro    foo
  820.  
  821.     can appear in the same .Z file, however the line
  822.  
  823.     .var    foo    expression
  824.  
  825.     can be included many times, each occurence changing the value of foo.
  826.  
  827. 7. .lib: direct library production - to give an example:
  828.  
  829.     .lib    "strcpy"    ; generating STRCPY.O in the output .L file
  830.     .extern    _strcpy        ; _strcpy is the only external label
  831.     _strcpy:        ; here it is - note that it could be anywhere
  832.         call    #arg2    ; from here to the next .lib
  833.     l1:    ld    a,(de)    ; and the code ....
  834.  
  835.     etc. etc.
  836.  
  837.     is the start of a typical library module source: in the above example
  838.     ZSM will produce a .L file directly, and the code segment given
  839.     would behave just as if ZLIB had been used to insert a file STRCPY.O.
  840.     Note that ALL .externs in a library source must come immediately
  841.     after a .lib, and that .lib automatically does a .cseg. .lib has to
  842.     must be the first thing in the file (with the exception of .macro's
  843.     and .var's) so that ZSM latches onto the notion of producing a .L
  844.     file before it actually does anything.
  845.  
  846. 8. .if, .else, .elif, .endif: conditional assembly. These directives allow
  847.     assembly of different sections of code depending on the value of
  848.     expressions:
  849.  
  850.     .if    test1
  851.  
  852.         code1        ; assembled iff test1 is true
  853.  
  854.     .elif    test2
  855.  
  856.         code2        ; assembled iff test1 false and test2 true
  857.  
  858.     .else
  859.  
  860.         code3        ; assembled iff both test1 and test2 false
  861.  
  862.     .endif
  863.  
  864.     To explain more fully, .if starts a section of conditional code,
  865.     .else (optional) "reverses" the test, only code on one side of the
  866.     .else is used, and .endif closes the .if down. .elif has exactly
  867.     the same result as
  868.  
  869.     .else
  870.     .if    test2
  871.  
  872.     except that using .elif saves a level of nesting. Only one .elif is
  873.     shown above, they can be chained together as many times as are needed
  874.     following an initial .if. As with macro usage, .if's nest, up to a
  875.     maximum depth of five.
  876.  
  877.  
  878. Errors
  879.  
  880.     There are 16 error codes: when an error occurs, ZSM prints the
  881. offending line, including (among other things) a letter code in the thirty
  882. third character explaining what went wrong:
  883.  
  884. a    bad arguments in macro invocation
  885. c    constant error: illegal digit in constant (e.g. 059 or 0b011020)
  886. d    directive error: e.g. misplaced .lib's, .endm's, etc., or bad syntax
  887. e    expression error: something wrong with expression syntax
  888. f    .incl file not found
  889. g    segment error: typically code in .useg
  890. i    .if error: misplaced .else, .elif, .endif
  891. l    jr length error or jr to undefined label
  892. m    multiply defined label / macro
  893. n    macro's / if's nested too deep
  894. o    opcode error: unknown opcode
  895. p    error with formal parameters in macro definition
  896. q    missing trailing ' in db string
  897. s    syntax error: something wrong with the operands
  898. u    undefined variable in expression
  899. v    value error e.g. rst 77 or im 10
  900.  
  901. Only the first error on a line will be reported, for example in the case of
  902. a multiply defined label AND a syntax error, only the 'm' error will get
  903. reported.
  904.  
  905.     In addition ZSM will exit if there is a symbol table overflow, or
  906. macro / symbol definitions run it out of memory, but as there are some
  907. 1000 symbol table slots, it requires typically in excess of 64K of source
  908. to approach these kinds of limits.
  909.