On 24 Oct 2009, at 20:08, Thomas Harte wrote:

Anyway, if some of you z80 experts could have a quick look and tell me
if I'm making any obvious style errors or otherwise missing obvious
optimisations — even if only on a peephole level — I'd be infinitely
grateful.

One trick I almost always use, is instead of:

NUMVERTS:
        db 0
...
        ld a, (NUMVERTS)

I would write:

NUMVERTS: equ $+1
            ld a,00

i.e. the data byte of the instruction is overwritten when the symbol is used (other code can write the symbol as normal). You can safely do this even if you're writing to the next consecutive instruction (i.e. there are no pipelining issues to be concerned with. Naturally, I would be shot for proposing this on any processor newer than the Z80)

This is a byte smaller, and 8 t-states faster (not much, but it can be useful if the value is used frequently. Of course, if you read the variable in several places only one of them can be modified in this way. Choose the one which is executed most often).

You can do this for any instruction which takes a literal data byte or word (use EQU $+2 for an instruction which uses the index registers). The only gotcha is that you must be careful to write the correct data size back, i.e. never write a double-register to storage allocated by 'ld a,00' otherwise you will corrupt the following instruction.

By the way:

        ld d, (NUMVERTS)

I don't think you can do this?
If you've managed to persuade pyz80 to accept that, I'd be interested to see what opcode it generated...

NB. the transformed alternative *is* available. i.e.:
NUMVERTS: equ $+1
            ld d,00

HTH,
Andrew

--
http://www.intensity.org.uk/



Reply via email to