2019-04-16 13:36:57 -04:00
|
|
|
# z80 assembler
|
|
|
|
|
2019-10-04 20:26:21 -04:00
|
|
|
This is probably the most critical part of the Collapse OS project because it
|
|
|
|
ensures its self-reproduction.
|
2019-04-16 13:36:57 -04:00
|
|
|
|
2019-11-15 09:57:53 -05:00
|
|
|
## Invocation
|
|
|
|
|
|
|
|
`zasm` is invoked with 2 mandatory arguments and an optional one. The mandatory
|
|
|
|
arguments are input blockdev id and output blockdev id. For example, `zasm 0 1`
|
|
|
|
reads source code from blockdev 0, assembles it and spit the result in blockdev
|
|
|
|
1.
|
|
|
|
|
|
|
|
Input blockdev needs to be seek-able, output blockdev doesn't need to (zasm
|
|
|
|
writes in one pass, sequentially.
|
|
|
|
|
|
|
|
The 3rd argument, optional, is the initial `.org` value. It's the high byte of
|
|
|
|
the value. For example, `zasm 0 1 4f` assembles source in blockdev 0 as if it
|
|
|
|
started with the line `.org 0x4f00`. This also means that the initial value of
|
|
|
|
the `@` symbol is `0x4f00`.
|
|
|
|
|
2019-04-16 13:36:57 -04:00
|
|
|
## Running on a "modern" machine
|
|
|
|
|
|
|
|
To be able to develop zasm efficiently, [libz80][libz80] is used to run zasm
|
|
|
|
on a modern machine. The code lives in `emul` and ran be built with `make`,
|
|
|
|
provided that you have a copy libz80 living in `emul/libz80`.
|
|
|
|
|
|
|
|
The resulting `zasm` binary takes asm code in stdin and spits binary in stdout.
|
|
|
|
|
2019-05-27 10:38:29 -04:00
|
|
|
## Literals
|
|
|
|
|
2019-11-22 14:01:16 -05:00
|
|
|
See "Number literals" in `apps/README.md`.
|
2019-05-27 10:38:29 -04:00
|
|
|
|
2019-11-22 14:01:16 -05:00
|
|
|
On top of common literal logic, zasm also has string literals. It's a chain of
|
|
|
|
characters surrounded by double quotes. Example: `"foo"`. This literal can only
|
|
|
|
be used in the `.db` directive and is equivalent to each character being
|
|
|
|
single-quoted and separated by commas (`'f', 'o', 'o'`). No null char is
|
|
|
|
inserted in the resulting value (unlike what C does).
|
2019-05-27 10:38:29 -04:00
|
|
|
|
|
|
|
## Labels
|
|
|
|
|
|
|
|
Lines starting with a name followed `:` are labeled. When that happens, the
|
|
|
|
name of that label is associated with the binary offset of the following
|
|
|
|
instruction.
|
|
|
|
|
|
|
|
For example, a label placed at the beginning of the file is associated with
|
|
|
|
offset 0. If placed right after a first instruction that is 2 bytes wide, then
|
|
|
|
the label is going to be bound to 2.
|
|
|
|
|
2019-10-09 11:12:08 -04:00
|
|
|
Those labels can then be referenced wherever a constant is expected. They can
|
2019-05-27 10:38:29 -04:00
|
|
|
also be referenced where a relative reference is expected (`jr` and `djnz`).
|
|
|
|
|
|
|
|
Labels can be forward-referenced, that is, you can reference a label that is
|
|
|
|
defined later in the source file or in an included source file.
|
|
|
|
|
|
|
|
Labels starting with a dot (`.`) are local labels: they belong only to the
|
|
|
|
namespace of the current "global label" (any label that isn't local). Local
|
|
|
|
namespace is wiped whenever a global label is encountered.
|
|
|
|
|
|
|
|
Local labels allows reuse of common mnemonics and make the assembler use less
|
|
|
|
memory.
|
|
|
|
|
|
|
|
Global labels are all evaluated during the first pass, which makes possible to
|
|
|
|
forward-reference them. Local labels are evaluated during the second pass, but
|
|
|
|
we can still forward-reference them through a "first-pass-redux" hack.
|
|
|
|
|
|
|
|
Labels can be alone on their line, but can also be "inlined", that is, directly
|
|
|
|
followed by an instruction.
|
|
|
|
|
|
|
|
## Constants
|
|
|
|
|
|
|
|
The `.equ` directive declares a constant. That constant's argument is an
|
|
|
|
expression that is evaluated right at parse-time.
|
|
|
|
|
|
|
|
Constants are evaluated during the second pass, which means that they can
|
|
|
|
forward-reference labels.
|
|
|
|
|
|
|
|
However, they *cannot* forward-reference other constants.
|
|
|
|
|
2019-11-14 21:16:36 -05:00
|
|
|
When defining a constant, if the symbol specified has already been defined, no
|
|
|
|
error occur and the first value defined stays intact. This allows for "user
|
|
|
|
override" of programs.
|
|
|
|
|
|
|
|
It's also important to note that constants always override labels, regardless
|
|
|
|
of declaration order.
|
2019-05-27 10:38:29 -04:00
|
|
|
|
|
|
|
## Expressions
|
|
|
|
|
2019-11-22 14:01:16 -05:00
|
|
|
See "Expressions" in `apps/README.md`.
|
2019-05-27 10:38:29 -04:00
|
|
|
|
|
|
|
## The Program Counter
|
|
|
|
|
|
|
|
The `$` is a special symbol that can be placed in any expression and evaluated
|
|
|
|
as the current output offset. That is, it's the value that a label would have if
|
|
|
|
it was placed there.
|
|
|
|
|
2019-10-04 20:26:21 -04:00
|
|
|
## The Last Value
|
|
|
|
|
|
|
|
Whenever a `.equ` directive is evaluated, its resulting value is saved in a
|
|
|
|
special "last value" register that can then be used in any expression. This
|
2019-11-15 08:59:26 -05:00
|
|
|
last value is referenced with the `@` special symbol. This is very useful for
|
|
|
|
variable definitions and for jump tables.
|
|
|
|
|
|
|
|
Note that `.org` also affect the last value.
|
2019-10-04 20:26:21 -04:00
|
|
|
|
2019-05-27 10:38:29 -04:00
|
|
|
## Includes
|
|
|
|
|
2019-10-06 14:32:23 -04:00
|
|
|
The `.inc` directive is special. It takes a string literal as an argument and
|
2019-05-27 10:38:29 -04:00
|
|
|
opens, in the currently active filesystem, the file with the specified name.
|
|
|
|
|
|
|
|
It then proceeds to parse that file as if its content had been copy/pasted in
|
|
|
|
the includer file, that is: global labels are kept and can be referenced
|
|
|
|
elsewhere. Constants too. An exception is local labels: a local namespace always
|
|
|
|
ends at the end of an included file.
|
|
|
|
|
|
|
|
There an important limitation with includes: only one level of includes is
|
2019-10-06 14:32:23 -04:00
|
|
|
allowed. An included file cannot have an `.inc` directive.
|
2019-05-27 10:38:29 -04:00
|
|
|
|
|
|
|
## Directives
|
|
|
|
|
|
|
|
**.db**: Write bytes specified by the directive directly in the resulting
|
|
|
|
binary. Each byte is separated by a comma. Example: `.db 0x42, foo`
|
|
|
|
|
|
|
|
**.dw**: Same as `.db`, but outputs words. Example: `.dw label1, label2`
|
|
|
|
|
|
|
|
**.equ**: Binds a symbol named after the first parameter to the value of the
|
|
|
|
expression written as the second parameter. Example:
|
2019-11-14 21:16:36 -05:00
|
|
|
`.equ foo 0x42+'A'`. See "Constants" above.
|
2019-07-23 16:01:23 -04:00
|
|
|
|
2019-05-27 10:38:29 -04:00
|
|
|
**.fill**: Outputs the number of null bytes specified by its argument, an
|
|
|
|
expression. Often used with `$` to fill our binary up to a certain
|
|
|
|
offset. For example, if we want to place an instruction exactly at
|
2019-10-09 11:12:08 -04:00
|
|
|
byte 0x38, we would precede it with `.fill 0x38-$`.
|
2019-05-27 10:38:29 -04:00
|
|
|
|
2019-11-13 22:24:58 -05:00
|
|
|
The maximum value possible for `.fill` is `0xd000`. We do this to
|
|
|
|
avoid "overshoot" errors, that is, error where `$` is greater than
|
|
|
|
the offset you're trying to reach in an expression like `.fill X-$`
|
|
|
|
(such an expression overflows to `0xffff`).
|
|
|
|
|
2019-05-27 10:38:29 -04:00
|
|
|
**.org**: Sets the Program Counter to the value of the argument, an expression.
|
|
|
|
For example, a label being defined right after a `.org 0x400`, would
|
|
|
|
have a value of `0x400`. Does not do any filling. You have to do that
|
|
|
|
explicitly with `.fill`, if needed. Often used to assemble binaries
|
|
|
|
designed to run at offsets other than zero (userland).
|
|
|
|
|
2019-06-03 11:13:31 -04:00
|
|
|
**.out**: Outputs the value of the expression supplied as an argument to
|
|
|
|
`ZASM_DEBUG_PORT`. The value is always interpreted as a word, so
|
|
|
|
there's always two `out` instruction executed per directive. High byte
|
|
|
|
is sent before low byte. Useful or debugging, quickly figuring our
|
|
|
|
RAM constants, etc. The value is only outputted during the second
|
|
|
|
pass.
|
|
|
|
|
2019-10-06 14:32:23 -04:00
|
|
|
**.inc**: Takes a string literal as an argument. Open the file name specified
|
2019-05-27 10:38:29 -04:00
|
|
|
in the argument in the currently active filesystem, parse that file
|
|
|
|
and output its binary content as is the code has been in the includer
|
|
|
|
file.
|
|
|
|
|
2019-07-21 12:58:02 -04:00
|
|
|
**.bin**: Takes a string literal as an argument. Open the file name specified
|
|
|
|
in the argument in the currently active filesystem and outputs its
|
|
|
|
contents directly.
|
2019-06-19 13:34:06 -04:00
|
|
|
|
2019-10-17 15:52:59 -04:00
|
|
|
## Undocumented instructions
|
|
|
|
|
|
|
|
`zasm` doesn't support undocumented instructions such as the ones that involve
|
|
|
|
using `IX` and `IY` as 8-bit registers. We used to support them, but because
|
|
|
|
this makes our code incompatible with Z80-compatible CPUs such as the Z180, we
|
|
|
|
prefer to avoid these in our code.
|
|
|
|
|
2019-12-13 17:38:40 -05:00
|
|
|
## AVR assembler
|
|
|
|
|
|
|
|
`zasm` can be configured, at compile time, to be a AVR assembler instead of a
|
|
|
|
z80 assembler. Directives, literals, symbols, they're all the same, it's just
|
|
|
|
instructions and their arguments that change.
|
|
|
|
|
|
|
|
Instructions and their arguments have a ayntax that is similar to other AVR
|
|
|
|
assemblers: registers are referred to as `rXX`, mnemonics are the same,
|
|
|
|
arguments are separated by commas.
|
|
|
|
|
|
|
|
To assemble an AVR assembler, use the `gluea.asm` file instead of the regular
|
|
|
|
one.
|
|
|
|
|
2019-04-16 13:36:57 -04:00
|
|
|
[libz80]: https://github.com/ggambetta/libz80
|