## Code optimization challenge

Any discussions related to the creation of new hardware or software for the ZX80 or ZX81
marste
Posts: 100
Joined: Sun Aug 10, 2014 8:58 pm
Location: Italy
Contact:

### Code optimization challenge

As the last time with "waiting some time" solution (viewtopic.php?t=1448), I hope to receive interesting idea to optimize a piece of code:

The objective is to do some instructions optimizing space (and time also where possible),

In particular DO "dec d" and "ld c',l" (the alternate c register with the standard l, that require to make additional ld),
JUST IF "(b=x1xx1xxx and l=z010xxxx) or (b=x1xx0xxx and l=z101xxxx)" - where x means whatever value, z means that the value there is known a priori and is zero, and 0 and 1 means that that bits should be exactly that values

Register a and flags can be destroied together with hl' (the alternate one), the others should keep the original values.

Note: I can swap the usage of the 6th bit of b with one of the other of the higher nibble (7th-4th) if useful for the solution.

Initial attempt pretty long is:

Code: Select all

``````
bit 6,b
jr z,skip
ld a,l
and \$70
bit 3,b
jr nz,check2
sub \$50
jr z,doit
jr skip
check2:
sub \$20
jr nz,skip

doit:
ld a,l
exx
ld c,a
exx
dec d

skip:
``````
Happy hacking!

marste
Posts: 100
Joined: Sun Aug 10, 2014 8:58 pm
Location: Italy
Contact:

### Re: Code optimization challenge

Some improvements:

1 byte less with

Code: Select all

``````    ld a,b
and 01001000b
xor 01001000b
jr z,point1
xor 00001000b
jr nz,skip
ld a,01110000b
point1:
xor 01010000b
xor l
and 01110000b
jr nz,skip
doit:
ld a,l
exx
ld c,a
exx
dec d
skip:
``````
Changing the b 6th bit with 7th, 4 byte less:

Code: Select all

``````    ld a,b
rla
jr nc,skip
and 00010000b
jr nz,point1
ld a,01100000b ; a was 0
point1:
xor 01010000b ; a was 00010000b from jump
xor l
and 01110000b
jr nz,skip
doit:
ld a,l
exx
ld c,a
exx
dec d
skip:
``````
I'm sure it can be done better...

### Who is online

Users browsing this forum: No registered users and 2 guests