Z80-ASM - the Z80 assembler

0. Content

1. How it works

The z80-asm is a double pass syntax controlled compiler. All is controlled from compile function.

This function first calls lexical_analysis which parses another line from the source and fills global structure lex. It performs lexical analysis and syntactical analysis. Syntactical analysis is very simple because of simple assembler syntax (line oriented source, max. one instruction per line, fixed order of label, instruction and arguments) so syntactical analysis is done at the same time as lexical analysis.

After return from lexical analysis the compile function calls special translating function according to the instruction body. Each translating function can translate group of instructions (e.g. arithmetical instructions, load instructions, bit instructions ...) and it also performs semantical analysis. It tests if there is a correct number of arguments, it tests types of arguments and so on. When everything's OK translaing instruction translates instruction and returns. Translating functions can be found in files compile.c and compile.h (where headers are).

Compiler has 64Kb memory buffer representing Z80's memory. Translating functions write translated code to this buffer. This buffer is in variable unsigned char memory[65536]; in z80-asm.c file.

2. Labels

Because labels can be used before they're defined this compiler passes through the source code twice. During first pass are labels collected to a table, where label text and label address is stored. This table is hashed so access is immediate and adding is quite simple. I think it's the best way to store labels. If labels were found binary it would be in logarithmic time. And storing labels as they come has linear complexity for searching.

During first pass nothing's put to memory but translating functions don't know it. They "translate" normally. So possible errors are revealed already in first pass. What happens when an instruction uses label during first pass? Hashing function replies current address on a query, so everything's OK.

During second pass instructions are completely translated. If a label occurs it's searched in the table and address of label is used. If there's not such label error message is generated.

3. Functions

4. Data

Other compiler variables are here.

5. The compiler library

Main compiler functions are made as a library functions so they can be simply called from a separate program.

To use compiler as a library you must compile the asm.a library typing make asm.a, include file asm.h to your source and define following functions:

There are some variables for controlling the compiler, these variables must be set before calling the compiler:

And now to start compiling: