Dave's Free Press

Technology: CPU-Z80-Assembler-1.03


Older stuff

CPU::Z80::Assembler - a Z80 assembler


CPU::Z80::Assembler - a Z80 assembler


    use CPU::Z80::Assembler;
    my $binary = z80asm(q{
        ORG 0x1000
        LD A, 1


This module provides a single subroutine which implements a Z80 assembler.


By default the 'z80asm' subroutine is exported. To disable that, do:

    use CPU::Z80::Assembler ();



This takes one parameter, a string of Z80 assembler source. It returns the assembled version as a string. If you set the $verbose variable it will also spit out an assembler listing:

    $CPU::Z80::Assembler::verbose = 1;


Instructions are seperated by new lines, and have the following format. They must be ASCII:

    INSTRUCTION [; optional comments]

or $label [= ...] [; ...]

or # comment beginning with # as first char on a line


Numbers can be supplied in either decimal, hexadecimal, or binary. Hex numbers have a leading 0x, binary numbers have a leading 0b.


DEFB 0x12
A byte of data

DEFW 0x1234
A 16-bit word of data, in little-endian order. So the example would actually insert 0x34 followed by 0x12.

DEFT ``literal text'', 0x00
A literal string, either single- or double-quoted. Can optionally be followed by a comma-seperated list of bytes. Quoted text can not include the quotes surrounding it or newlines.

ORG 0x4567
Tell the assembler to start building the code at this address. Must be the first instruction and can only appear once. If absent, defaults to 0x0000. This value is available in an assembler label called 'org'.

INCLUDE ``filename''
Include another file. This is not recursive. If you want recursion, use the C pre-processor.


Standard Z80 mnemonics are used. The ``unofficial'' Z80 instructions are not yet implemented.

RST vectors

The RST instruction takes as its parameter either the address to jump to or the reset vector number - this is just the address / 8.

This means that, for example, RST 0x28 == RST 5.


The DJNZ and JR instructions take an address as their destination, not an offset. If you need to use an offset, do sums on $$. Note that $$ is the address of the *current* instruction. The offset needs to be calculated from the address of the *next* instruction, which for these instructions is always $$ + 2.


This extra instruction (which assembles to 0xDD 0xDD 0x00) is provided for the convenience of those using the CPU::Emulator::Z80 module.


Labels are preceded by a dollar sign, must start with a letter or underscore, and consist solely of letters, underscores and numbers. They default to having the value of the address they are created at. If you want to assign another value, then you can say:

    $label = 0x1234

You can use $$ to refer to the current address. Mathemagical operations are allowed too - the value is parsed as perl, and you can refer to other labels as $name:

    $label      = $$ + 8
    $otherlabel = $label / 2 + 3


Macros are created thus. This example creates an ``instruction'' called MAGIC that takes two parameters:

    MACRO MAGIC param1, param2 {
        LD $param1, 0
        BIT $param2, L
        $label = 0x1234
        ... more real instructions go here.

Within the macro, $param1, $param2 etc will be replaced with whatever parameters you pass to the macro. So, for example, this:

    MAGIC HL, 2

Is the same as:

    LD HL, 0
    BIT 2, L

Any labels that you define inside a macro are local to that macro. Actually they're not but they get renamed to $_macro_$$_... so that they effectively *are* local.

See the test suite for examples.


The ``unofficial'' instructions aren't supported.

I welcome feedback about my code, including constructive criticism. Bug reports should be made using http://rt.cpan.org/ or by email.




Paulo Custodio for numerous bugfixes




Copyright 2008 David Cantrell <david@cantrell.org.uk>

This software is free-as-in-speech software, and may be used, distributed, and modified under the terms of either the GNU General Public Licence version 2 or the Artistic Licence. It's up to you which one you use. The full text of the licences can be found in the files GPL2.txt and ARTISTIC.txt, respectively.


This software is also free-as-in-mason.