36 byte Snowflake in Assembler
Author: Serato / Finnish Gold
Category: Christmas Challenge
System: ZX81
Language: ZX81 Assembler
Len source code: 1231
Len exe file:
Len code only: 36
Instructions:
Navigate to https://weggetjes.nl/jszeddy/jszeddy.html and paste source into ASM
notepad and press Assemble. Then enter "1 rand usr 30473" into the BASIC
notepad and press Compile, then press r(un) enter.
Techniques that save bytes:
- Simple iteration, no subroutines
- Program flow automatically renders the central + (no cost)
- Only draws stars (not spaces) so no character code calculation
- Early exit after plot reaches left column, so no empty bitmap byte needed
- E is unaffected by PRINT AT so used as a sentinel by 8-bit rotate
- Start address of $7709 is in BC, so E can be loaded with $77 in 1 byte
- and also C is initialised to 9
- which also aligns bitmap bytes at $7700 + col, so low byte = C
- Modifies the bitmap in place so no need to keep value in register
- branch on carry set is both conditional plot and also rotation loop exit flag
- jp (hl) saves a byte over jr, only possible because of in-place bitmap shift
More detail:
This routine draws the pattern from the inside out, by first drawing the stars
that form the + and then right shifting bits out of the bitmap from preceding
bytes one at a time. The first iteration plots the bitmap along the same row and
column as the + so the contents of the pattern byte do not matter. This bit
shifting is done in place turning each byte of the bitmap to 0. The first byte
to be consumed is the first instruction "ld e, b" which must only run once.
The last iteration plots the outermost stars of the + and then immediately exits
before reading a bitmap byte. The code is placed immediately after the bitmap
and the branch in the code back to start each row is implemented as "jp (hl)".
As HL is the pointer into the bitmap, and the byte pointed to has just been
zeroed, the branched-to location consists of a string of NOP's preceding the
intended branch destination. "jp (hl)" is a 1-byte instruction, saving a byte
over the conventional "jr xx".