I started learning Z80 assembly several years ago but I didn't really do anything with it apart from a few flashy screen effects. So now I've decided to come back to it and attempt to make an actual game, starting with this text routine. It's optimized to be as small as possible (34/35 bytes), with the font using 7 bytes for every pair of characters. I thought I'd share it here in the hope that others might find it useful, so feel free to use it, modify it, whatever.
And I just wanted to say thanks for all the hard work that goes into maintaining ZXDB and this site, it's really appreciated
Here's a quick demo of the routine. It's not really this slow, I just put in a few HALTs so you can see it properly in action.
Code: Select all
; Prints up to 4 lines of text using a 4x8 character font.
;
; IX = points to one beyond last character of string
; HL = screen address to print at
; C = number of characters to print, maximum 256 (0)
;
; Notes: The left edge of the string is always aligned
; with a character block. If you want a string to start
; halfway along a block, it must begin with a space.
;
; The font is stored with two graphic rows packed into
; a byte, with each of 7 rows stored on a separate
; 256-byte page.
;
; preserves: IY, alternate registers
charloop:
ld d, font_hi ; construct font address
push hl ; save screen pos
ld a, c ; number of chars left to print
dec a ; we want to round downwards
or a ; clear carry for scroll
rra ; divide by two (64 chars to 32)
add a, l ; calculate new screen address
ld l, a
ld b, 7 ; loop 7 rows
dec ix ; read string backwards
if 1
ld e, (ix+0) ; get character
srl e ; check value of LSB
else
db $DD, $CB, $00, $3B ; ld e, srl (ix+0)
; saves one byte and 4 Ts
; Only use for temporary strings
; (string becomes corrupted)
endif
rowloop:
inc h ; next screen line (skipping first row)
ld a, (de) ; get graphic
rrd ; rotate onto screen
jr c, nextrow ; done printing?
rrd ; rotate again into A
ld (hl), a ; display on screen
nextrow:
inc d ; next graphic
djnz rowloop ; loop for all rows
nextchar:
pop hl ; restore screen pos
dec c ; any more chars?
jr nz, charloop ; loop back round
ret ; done