zmac - Z-80 Macro Cross Assembler
zmac has been around for a long time. My version runs under Windows (XP or Vista and probably older versions) and has quite a number of improvements:
- Cycle-counting with sett, setocf pseudo-ops and t, tilo, tihi and ocf operators.
- Flexible label format and new pseudo-ops making it compatible with MRAS and MACRO-80.
- Outputs .hex, .cmd, .cas, .lcas, .rel, .cim and .ams files.
- $ and 0x prefixes for hex constants
- 8080 mnemonics supported
- Undocumented Z-80 instructions
- JR promotion
- Full C expressions and parenthesis allowed.
- Include data files directly (incbin)
- 32 bit constants (defd)
- -I to specify include directories
- rept, irp and irpc in-line macros.
- Ever improving compatibility with MAC, MACRO-80 and MRAS.
You may also want the ld80 linker if you're creating ".rel" files or need to link with existing ".rel" files.
The best description of the T-state counting macros can be found at the end of my cycle counting tutorial. Might also explain why you would want such a feature. setocf and ocf() are newer features not covered there. They're needed when counting clock cycles on the TRS-80 Model 4 when it runs in high speed (4 MHz) mode. While the Z-80 mostly runs twice as fast in that the opcode fetch portion of each instruction is slowed down by 2 wait states. Code on the Model 1 or Model 3 (or Model 4 running in Model 3 mode) can be timed like this:
code: ld a,(de) ld b,(hl) add a,b time equ t($)-t(code)The first two instructions take 7 T-states, the 3rd 4 T-states so zmac will set time equal to 18. But if you need cycle-accurate counts on a Model 4 you'll have to do this:
code: ld a,(de) ld b,(hl) add a,b time equ t($)-t(code)+2*(ocf($)-ocf(code))As before we get a base time of 18 T-states but in this case each instruction also takes an opcode fetch thus the equation will add an additional 6 T-states and zmac will set time to 24.
While most instructions require only one opcode fetch many others take more. All instructions require at least one opcode fetch so ocf() will be at least a count of instructions.
Just drop me a line at the e-mail address at the bottom of this page if there's any features you'd like to see in zmac, have bugs to report or even just to say "hi".
zmac has all its documentation built-in (zmac -h and zmac --doc). Here's the output of --doc for convenient reference.
Overview of zmaczmac is a Z-80 macro cross-assembler. It has all the features you'd expect. It assembles the specified input file (with a '.z' extension if there is no pre-existing extension and the file as given doesn't exist) and produces program output in many different formats. It also produces a nicely-formatted listing of the machine code and cycle counts alongside the source in a ".lst" file.
To reduce clutter and command line options, all zmac output is put into an (auto-created) zout subdirectory. For file.z the listing will be in zout/file.lst, the TRS-80 executable format in zout/file.cmd and so on.
Undocumented Z-80 instructions are supported as well as 8080 code.
zmac strives to be a powerful assembler with expressions familiar to C programmers while providing good backward compatibility with original assemblers such as Edtasm, MRAS and Macro-80.
Usagezmac [ --help ] [ --version ] [ --dep ] [ --mras ] [ --rel ] [ --rel7 ] [ --doc ] [ --zmac ] [ -8bcefghijJlLmnopstz ] [ filename[.z] ]
|Display a list of options and a terse description of what the options do.|
|Print zmac version name.|
|MRAS compatibility mode. Any ? in a label will be expanded to the current module identifier as set by *mod. Operator precedence and results are changed.|
|Output ".rel" (relocatable object file) format only. Exported symbols are truncated to length 6.|
|Output ".rel" (relocatable object file) format only. Exported symbols are truncated to length 7.|
|zmac compatibility mode. defl labels are undefined after each pass. Quotes and double quotes are stripped from macro arguments before expansion. $ is ignored in identifiers allowing foo$bar to construct identifiers in macro expansions. Use ` (backquote) instead in normal mode. Labels starting with "." are temporary and are reset whenever a non-temporary label is defined (thus they may be reused). Labels starting with "_" are local to their file thus avoid multiple definition when brought in with include.|
|Print all files read by include and incbin.|
|Print this documentation in HTML format to standard output.|
|Set @@k to the given numeric value before assembly. Up to 10 parameters can be set from 0 though 9. -Pk is shorthand for -Pk=-1. For example, P4=$123 effectively puts @@4 equ $123 at the top of the first file.|
|Accept 8080 mnemonics preferentially. Equivalent to .8080 pseudo-op.|
|Don't generate any machine code output at all.|
|Make the listing continuous, i.e., don't generate any page breaks or page headers. Can make things less confusing if you're going to consult the listing online rather than printing it. This is the default.|
|Omit the "error report" section in the listing.|
|List instructions not assembled due to "if" expressions being false. (Normally these are not shown in the listing.)|
|List only the first line of equivalent hex for a source line.|
|Display a list of options and a terse description of what the options do. (same as --help)|
|Don't list files included with include or read.|
|Add dir to the end of the include file search path.|
|Promote relative jumps and DJNZ to absolute equivalents as needed.|
|Error if an absolute jump could be replaced with a relative jump.|
|Don't generate a listing at all.|
|Generate listing no matter what. Overrides any conflicting options.|
|List macro expansions.|
|Omit line numbers from listing.|
|List to standard output.|
|Use a few linefeeds for page break in listing rather than ^L.|
|Output listing for a printer with headers, multiple symbols per column, etc.|
|Omit the symbol table from the listing.|
|Only output number of errors instead list of each one.|
|Accept Z-80 mnemonics preferentially. Equivalent to .z80 pseudo-op.|
zmac uses the standard Zilog mnemonics, and the pseudo-ops are also largely as you'd expect.
A "." may optionally preceeed any psuedo-op. For example, ".org" and "org" are treated as equivalent.
Input can be upper or lowercase.
Comments start with ; and carry on to the end of the line.
Number constants can take a trailing h or a leading $ or 0x for hex, a trailing b for binary, a trailing o or q for octal, or a trailing d for decimal.
'LH' (any length 2 string) can be treated as a number whose value is 'H' * 256 + 'L'.
Labels are declared with label: or just label - indentation is unimportant. Labels can be up to 40 chars long. They can start with and contain letters, digits, $, ., ?, @ and _. Ambiguous identifiers like $FCB will be treated as hex constants unless defined as a label. Labels declared with two colons (label::) make the label public.
Single quotes are ignored at the end of identifiers allowing non-binding notation indicating alternate register use during heavy applications of exx and ex.
Here is how other things work. Numbers are used as examples, but a full expression can be used in their place.
A byte. ascii, byte, db, defm and text are synonyms.
An ASCII character string (not NUL-terminated). Double quotes can also be used.
defb 'Who needs anything more than CP/M?',13,10,'$'
Strings and bytes can mix together.
A word (16 bits). word and dw are synonyms.
A double word (32 bits). dword is a synonym.
Skip output ahead 500 bytes. This will insert 500 zeros in the ".ams" and ".cim" output files or if inside a ".phase" section. block, ds and rmem are synonyms.
Like ascii but accepts only a single string and the high bit of the last character will be set.
Repeat the byte value a total of count times. Similar to defs except that memory is always filled with value.
Inserts the raw contents of the file into the assembly. Simpler for large amounts of data.
label equ 100
Define a symbol to have a fixed value. The symbol can be used before it is defined. A symbol defined with equ or as a label can be defined only once, except that a symbol defined with equ may be redefined to the same value.
varname defl 200
Define a symbol to have a changeable value. The symbol cannot be used before it is defined, and it can be redefined to a different value later with another defl. aset, set and = are synonyms (despite set also being a Z-80 mnemonic).
varname OP = expression
Shorthand for varname defl varname OP expression. Allows for C-like handling of variable such as var += 5. OP can be +, -, *, /, %, &, |, ^, <<, >>, && or ||.
Shorthand for varname defl varname + 1
Shorthand for varname defl varname - 1
Same as defl except that the symbol is defined as the smaller or bigger of two comma-separated expressions.
Increment the internal module name string. The first time this results in "a". Then "b", "c", ... "z". Then "aa", "ab", "ac", etc. all the way up to "zzzz". The module name string is used in --mras mode where "?" in label names is replaced with the current module name.
The listed labels are defined in an external module for later linking. No effect unless zmac is producing ".rel" output. ext and extrn are synonyms.
The given labels will be visible to external modules when linking. No effect unless zmac is producing ".rel" output. global and entry are synonyms.
Equivalent to label defl label + 1.
Equivalent to label defl label - 1.
label += 10
label -= 10
Equivalent to label defl label + 10 and label defl label - 10 respectively. Also works for *=, /=, %=, |=, &=, ^=, <<= and >>=.
Set the address to assemble to 0x9000.
Continue to produce code and data for loading at the current address but assemble instructions and define labels as if they originated at the given address. Useful when producing code that will be copied to a different location before being executed (e.g., an overlay).
End phase mode assembly.
aseg cseg dseg
Switch to the absolute, code and data segments respectively. No effect unless zmac is producing ".rel" output.
Ends the input. Any lines after an end are silently ignored. If an arg is given, it declares the entry address for the program. This has no effect in ".cim" output. In ".hex" output it generates an S-record directing 0 bytes of data to be loaded at the given address.
if ... [ else ... ] endif
For conditional assembly. If you do if foo and foo evaluates to zero, all the lines up until the next corresponding else or endif are completely ignored. Conversely, if foo evaluates to non-zero, any lines from a corresponding else to the endif are ignored. Ifs can be nested. cond/endc are synonyms for if/endif.
Like if, but tests if symbol has been defined. Declaring a symbol as external counts as it being defined.
Like if, but tests if symbol has not yet been defined.
Include a file. Like C's (well, cpp's) #include and follows the same include path search rules, but the filename arg lacks the angle brackets or quotes (though single or double quotes may be used). read is a synonym. *include file also works if started in the first column. In --mras mode ".asm" will be added if file has no suffix.
Like include but adds .lib to the file name so includes file.lib.
Suspend assembly until the next occurence of character X on a line. The rest of the line will be ignored. A multi-line comment.
Stop assembly if expr is non-zero.
Set the current T-state count to expr. tstate is a synonym.
Set the current opcode fetch count to expr.
Make cycle counting operators return 8080 cycle counts and interpret any ambiguous assembly statements as Intel 8080 mnemonics. CP will be interpreted as "call on positive" and JP as "jump on positive".
Make cycle counting operators return Z-80 cycle counts and interpret any ambiguous assembly statements as Zilog Z-80 mnemonics. CP will be interpreted as "compare accumulator" and JP as "jump unconditionally".
If enable is non-zero, turn on errors when JR instructions could be used instead of JP, off otherwise. Used to check existing code for situations where shorter code could be generated. Same as -J option. No effect if in 8080 mode.
If enable is non-zero, JR and DJNZ instructions will be promoted to equivalent JP and DEC B, JP NZ instructions if the relative branch offset is out of range. If enable is zero, promotion is disabled. Same as the -j option. No effect if in 8080 mode.
Most Z-80 chips support a number of undocumented instructions that were part of the original design but not made an offical part of the Zilog specification. These instructions may not be supported by all Z-80 chips, especially licensed variants, but are fairly widely available nonetheless.
Same as sla r but shifts a 1 into the lower bit of r rather than a 0.
Inputs a byte from port c but does not store the value. Flags are still set as with the normal in r,(c) instruction.
Outputs a zero to port c.
Same as the corresponding operation on just (ix+d) or (iy+d) but with the result being stored both into (ix+d) and register r. Except for bit which has no effect on r. zmac supports the syntax to allow those instruction patterns to be generated.
The upper and lower bytes of the ix and iy can be used in a number of instructions much in the same way as d and e correspond to the upper and lower bytes of de. zmac names these ixh, ixl, iyh and iyl and are referred to generically as ixylh here.
Arithmetic or logical operation on ix or iy high or low byte.
Load register with ix or iy high or low byte.
Load ix or iy high or low byte with register.
Output $DD and $FD prefix bytes. The Z-80 allows multiple prefix bytes for IX and IY instructions. This allows you to specify them abstractly. There is little purpose except for delaying an interrupt or confusing disassemblers.
Set the name of the output module to str. For compatibility reasons str may be parenthesized (e.g., "name ('foo')"). Not all output formats support an internal name and many have severe length limits.
rsym and wsym
Read/write a symbol file. These simply load/save the currently defined symbols from/to the file specified (in a non-portable format). rsym takes place at the point it is encountered in the file (on the first pass); wsym is delayed until assembly has finished.
There are several pseudo-ops for controlling the listing. None of these ops appear in the listing themselves:
Start a new listing page.
Do nothing. This can be used to have a comment in the source but not the listing, I suppose.
elist, flist, glist, mlist
These have the same effect as the similarly-named command-line options, though possibly with the sense reversed depending on the default. Use an arg >0 (or no arg) to enable, and an arg <0 to disable.
Turns output to listing file (.list) off if arg < 0 or on if arg > 0. If no arg supplied then listing is enabled. Use this to avoid listing certain parts of the source. In --mras mode arg must be either on or off and *list can be used if started in the first column.
Set title (used in listing and symbol file).
Output arg blank lines in the listing, or one line if no arg is given.
Expressions feature a full set of C operators with the same precedence rules and some common assembler extensions and names. Here is the complete list of operators, highest-precedence first. Operators separated only by a space are synonyms; for example, ~ is the same as not.
! (logical), ~ not (bitwise), + (unary), - (unary), low, high, t, tilo, tihi, ocf
*, /, % mod
<< shl, >> shr
< lt, > gt, <= le, >= ge
== = eq, != <> ne
& and (bitwise)
^ xor (bitwise)
| or (bitwise)
? : (ternary choice operator)
Expressions change significantly in --mras mode:
Evaluation is strictly left to right. Except for and, or, xor and =. This doesn't break compatibility as original MRAS source code only allows .and., .or. and .xor. but the precedence difference may surprise if code is added.
! is bitwise OR instead of logical not.
< is left shift (or right shift when shift amount is negative)
MRAS operators (.and. .eq. .ge. .gt. .high. .le. .low. .lt. .mod. .ne. .not. .or. .shl. .shr. .xor.) are recognized even if apparently in identifers. (e.g., a.or.b is seen as a .or. b).
Logical operators return -1 for true and 0 for false. Normally zmac, like C, uses 1 for true.
You can use normal parentheses or square brackets to override the precedence rules. Square brackets can be used where parentheses would conflict with Z-80 mnemonic syntax, but this is not necessary in any practical case.
The ? may need spaces around it to distinguish it from a label that has ? in it.
The unary operators not familiar to C programmers:
|Returns low 8 bits of expr|
|Returns high 8 bits of expr|
|Current count of T-states up to memory location expr|
|Low count of T-states used by instruction at memory location expr|
|High count of T-states used by instruction at memory location expr|
|Current count of opcode fetches up to memory location expr|
MacrosThe following defines a macro named m with zero or more formal parameters p1, p2, ..., pn, zero or more local symbols ?s1, ?s2, ..., ?sm, and body b1, b2, ...:
m macro p1, p2, ..., pn, ?s1, ?s2, ..., ?sm
The macro is called by writing:
m v1, v2, ..., vn
A macro call expands to the text of the macro's body, with each occurrence of a formal parameter pk replaced by the corresponding value vk, and with each local symbol ?sk replaced by a new, unique symbol invented for this call. Invented symbols begin with ?, so you should avoid using such symbols elsewhere in your program.
zmac currently does not check that you have provided the right number of parameters when calling a macro. If you provide too few, unmatched formals are replaced with the empty string. If you provide too many, the additional values begin to replace local symbols as if they were ordinary parameters. (This could be considered a feature.) After the local symbols are all replaced, additional parameters are silently ignored.
For compatibility with Macro-80, the first line of a macro definition can list other labels that will be treated locally:
Each time the macro is expanded the local labels are replaced with unique names thus avoiding multiple definition problems.
For compatability with MRAS, macro arguments may be preceeded by # in their definition and use.
Any ` (backquote) in a macro is ignored thus allowing a macro to construct identifiers. For example:
move macro dir
Invoking move i will construct a ldir block move instruction.
For compatibility, & can also be used as in MAC to concatenate macro parameters. This conflicts with zmac's bitwise and operator but you can use the and synonym in macros to avoid the conflict.
In --mras mode arguments will be expanded even if they are inside other identifiers. The move could be written:
move macro dir
Macro definitions can contain macro definitions which will be defined when the outer macro is first exapnded. Macros can be redefined as well.
Macro expansion continues to the endm directive but can be stopped prematurely by the exitm directive. Typically the exitm is inside some conditional part of the macro.
Parameters passed to a macro can be empty and are tested with the nul operator:
if nul &par
Macro parameters can contain commas if grouped inside < and >. Or a comma can be escaped with ^ which can also escape spaces and other special characters. It is also be put in front of a macro parameter name inside the expansion to suppress the replacement by its value.
Expansion of parameters in a macro body is purely textual. This can lead to surprises in complex situations. The % character can be used to force a macro parameter to be replaced with the evaluation of it as an expression.
zmac supports the commonly available rept, irp and irpc inline macros
rept repeats its block the given number of times. This will output 10 nop instructions:
irpc runs through a string of letters assigned them to a variable and expanding the macro block each time. For example, this will load 7 into registers b, d and h:
irp runs through a list of parameters assiging each entry to a variable and expanding the macro block. Here we load bc, de and hl with 0:
Lists can be nested. Here's an example of and irp passing lists on down to another irp:
zmac is broadly compatible with many original Z-80 and 8080 assemblers because it accepts many different names for common operations and has liberal identifier and numeric formats. It also accepts most simple usage of macros.
When assembling old code keep these portability problems in mind.
Expression order of evaluation may be different. zmac uses C semantics more order of evaluation but assemblers often used simple left to right ordering. zmac will evaluate 2+2*3 as 8 where other assemblers will yield 12. However, in --mras mode expressions are evaluated strictly left-to-right for compatibility.
zmac has no support operating on strings in macros. Assemblers like Macro-80 could perform conditional tests on strings.
Advanced macros are unlikely to work. zmac hasn't advanced to the state where all the possible ways of substituting parameters are supported.
Consult the original assembler manual. zmac error messages won't help you figure out what an unknown assembler command is supposed to do.
Compare against original output. The very safest thing to do when porting assembly code is to compare the binary output of zmac against that produced by the original assembler. This way you can ensure everything has been interpreted correctly. Only once that has been achieved should you modify the code.
Errors and Warnings
Any errors or warnings encountered during assembly are reported to standard error and in the listing file. The errors output immediately give the source file and line number containing the error. In listings the error letter and message appear just after the line containing the error.
|A string is missing an closing quote or an if is missing an endif|
|An expression did not parse or attempts a divide or modulus by 0.|
|General problem with the syntax on a line. Sometimes extra explanation will be printed on standard output.|
|A numeric constant has too many digits to be represented as a 32 bit number.|
|Mult. def. error|
|A symbol has been defined more than once and those values differ.|
|On the second or subsequent assembly passes the assembly has changed significantly. Most commonly it means an if has changed conditions but can also happen when labels or equated values do not converge to a fixed value.|
|An undeclared symbol was used in an expression or public statement.|
|An invalid value was given to a statement. Often this means using less than -128 or greater then 255 in a defb or less than -32768 or greater than 65535 in a defw. Or similar invalid values used Z-80/8080 opcodes requiring an 8 or 16 bit value (and other restrictions like 0 to 7 for BIT). Also if a relative jump is out of range or if a negative value is given in defs or dc.|
|phase was used within another phase or dephase without phase. Or if org is used within phase.|
|Assertion failure error|
|An assert statement evaluated to zero.|
|Use JR error|
|An absolute jump instruction was used where relative jump was in range of the destination address. Only generated if -j or jrpromote is in effect.|
|Not relocatable error|
|An expression was used that must be generated at link time but cannot be handled by the ".rel" format. For instance, an org to a symbol in the data segment when in the code segment. Or a relative jump to a different segment. The ".rel" format can evaluate expressions at link time using the high, low, not, -, +, *, / and % operators. zmac is clever enough to use high or low in place of & $ff00 and & 255. But it won't replace a shl with a multiply.|
|Register usage error|
|A invalid register was given to an instruction. For example, LD B,(DE) or ADD HL,IX.|
|Z-80 instruction in 8080 mode error|
|An instruction was assembled that is only valid on the Z-80 but .8080 (or -8) mode is in effect. However, use use of Z-80 mnemonics that output valid 8080 instructions is always OK.|
|$hex constant interpreted as symbol warning|
|A symbol such as $FCB has been defined even though it could appear to be a hexadecimal constant. zmac will treat $FCB as symbol for the entire assembly which could be rather surprising if that were not the intent.|
|Not implemented warning|
|For statements that have been added as parse rules but have no effect. The only current example is subttl which sets the sub title of a listing in certain assemblers.|
Except for ".rel", zmac writes every known output when assembling. This is no burden on modern computers and saves having to come up with options to select a particular output format.
".rel" is a special case since that format is intended for linking and can have undefined external symbols which would be errors in the other formats which do not support them.
|AMSDOS executable format for Amstrad computers.|
|For source-level debugging in trs80gp|
|TRS-80 high-speed (1500 baud) cassette SYSTEM file. The internal name of the file is the source file name shortened to 6 characters with suffixes removed.|
|Core In-Memory image. A raw binary format with the first byte corresponding to the lowest generated code or data and proceeding contiguously until the highest address generated. Any gaps are filled with zeros. Typically used for CP/M where all executables start at address 256 or for ROM images.|
|TRS-80 DOS executable file format as used by all major DOSes on the TRS-80 (TRS-DOS, LDOS, MULTIDOS, NEWDOS, etc.)|
|Intel hex record format.|
|TRS-80 low-speed (500 baud) cassette SYSTEM file. The internal name of the file is the source file name shortened to 6 characters with suffixes removed.|
|Relocatable object module format as produced by MACRO-80 and other assemblers.|
|ZX Spectrum cassette tape format.|
MiscellaneousIn the symbol table listing, the = prefix is given for those symbols defined by equ or defl.
The .rel file format can store symbol names of up to 7 characters in length. However, MACRO-80 truncates symbols to 6 characters so that it has one character in reserve for extending linking operations such as subtracting two externals from each other. To be compatible (and sensible), --rel truncates externals to 6 characters. For MRAS compatibility, --mras truncates symbols to 7 characters. This is not a problem for MRAS as it doesn't support extended linking. But necessary if you want zmac to produce .rel files that will link with MRAS generated .rel files. The --rel7 option sets symbol truncation to 7 characters so you can assemble files that will link with MRAS output. However, it will break extended linking on labels longer than 6 characters.
The ignoring of single quotes can be handy for tracking alternate register usage. Consider the following code fragment:
Although zmac does nothing but ignore the single quotes they are useful for indicating which register we're talking using. A more advanced mode where zmac pays attention to the trailing quotes and emits exchange instrucitons as needed has been considered.
|One or more errors were found during assembly, or zmac exited with a fatal error.|
CreditsBruce Norskog originally wrote zmac in 1978.
Updates and bugfixes over the years by John Providenza, Colin Kelley, and more recently by Russell Marks, Mark RISON, Chris Smith, Matthew Phillips and Tim Mann.
Extensive modifications for cycle counting, multiple output formats, ".rel" output, 8080 mode and older assembler compatibilty were written by George Phillips.
This document was based on Russell Marks zmac man page which had tweaks by Mark RISON and Tim Mann. George Phillips converted it to HTML and documented the new features and some older ones (e.g., phase/dephase).
George Phillips, December 26, 2016. gp2000 -at- shaw.ca