Z80 Assembler/Disassembler

The Z80 CPU was widely used in the 80s in many home computer. Even today it is often used as a cheap embedded CPU. If you need more information can get one of the following books:

Z80 Disassembler

I created this small disassembler for a Z80 cpu at one afternoon. It is a commandline tool. The size of the ROM and entry points have to be coded directly in the sourcecode.

Every ANSI C compiler should compile this program. It is tested with Think C 5.0 on a Macintosh. It only uses come ANSI functions (look into “main()”) for loading a file called “EPROM”.

The program has two parts:

The subroutine “OpcodeLen()” returns the size of one opcode in bytes. It is called while parsing and while disassembling.

The disassembler recognizes no hidden opcodes (the assembler does!). I didn’t had a table for them while writing the disassembler and they were not needed anyway.

If a routine wanted an “address” to the Z80 code, it is in fact an offset to the array of code. No pointers! Longs are not necessary for a Z80, because the standard Z80 only supports 64k.

In “main()” is a switch for disassembly with address and hexdump instead of disassembly with labels. This is useful for findings bugs in the disassembler and creating a list of variables.

This program is freeware. It is not allowed to be used as a base for a commercial product!

Z80 Assembler

I created the assembler for the Z80 a few days later to compile the changes code from the disassembler into an EPROM image and build a new firmware for my aquarium computer. I needed almost two days for the assembler, this means: commandline only… If you want to change the filename of the sourcefile, you have to change main().

This small assembler has some nice gadgets: it is a quite fast tokenizing single-pass assembler with backpatching. It knows all official Z80 opcodes and some undocumented opcodes (mainly with “IX” and “IY”). The Z80 syntax is documented in the Zilog documentation.

The assembler allows mathematical expressions in operands: “+”, “-”, “*“, “/”, “%” (modulo), “&” (and), “|” (or), “!” (not), “^” (xor), “<<” (shift left) and “>>” (shift right). Brackets are also available. The expression parser is located in [[Z80 Calc.c]]. Number can be postpended by a “D”, “H” or “B” for decimal, hexadecimal and binary numbers.

The assembler also knows the most commend pseudo opcodes (look into the sourcefile ‘Z80 Tokenize.c’):

The Sourcecode

Previous post:
Next post: