From 64b6fca5f33b619e9c38778000f5192521a57cf6 Mon Sep 17 00:00:00 2001 From: Mark Wooding Date: Sun, 14 Nov 2021 13:55:21 +0000 Subject: [PATCH] fizzbuzz.s: Abstract out a number of system dependencies. Maybe you can see where this is going. --- Makefile | 2 +- fizzbuzz.s | 52 ++++++++++++++++++---------------------------------- spectrum.s | 29 +++++++++++++++++++++++++++++ 3 files changed, 48 insertions(+), 35 deletions(-) create mode 100644 spectrum.s diff --git a/Makefile b/Makefile index 9bef433..0af35b7 100644 --- a/Makefile +++ b/Makefile @@ -7,7 +7,7 @@ TARGETS = CLEANFILES = $(TARGETS) CLEANFILES += spectrum-fb.img -spectrum-fb.img: fizzbuzz.s +spectrum-fb.img: spectrum.s fizzbuzz.s z80asm -o $@ $^ TARGETS += spectrum-fizzbuzz.tap diff --git a/fizzbuzz.s b/fizzbuzz.s index 58d4928..8521554 100644 --- a/fizzbuzz.s +++ b/fizzbuzz.s @@ -2,21 +2,6 @@ ;;; ;;; Best not to ask why. - org 0xf000 - -print_a: equ 0x10 -tvflag: equ 0x5c3c - -entry: - ;; Initialize the buffer. - ld a, 1 - ld (buf), a - ld (len), a - - ;; Use the main screen. - xor a - ld (tvflag), a - ;; Look at the buffer and decide what to do. again: ld e, 0 @@ -42,7 +27,7 @@ mod3_1: djnz mod3 call squish call squish - cp 0 + and a jr z, prfizz cp 3 jr z, prfizz @@ -68,7 +53,7 @@ prbuzz: ld hl, buzz ;; Not a multiple of five. Skip ahead if it was a multiple of three. nobuzz: ld a, e - cp 0 + and a jr nz, prnl ;; OK, so just print the value. @@ -86,12 +71,12 @@ nobuzz: ld a, e srl a srl a jr z, skiplz - or 0x30 - rst print_a + fixdig + print_a skiplz: ld a, d and 0x0f - or 0x30 - rst print_a + fixdig + print_a dec b jr z, prnl @@ -102,17 +87,17 @@ prdig: dec hl srl a srl a srl a - or 0x30 - rst print_a + fixdig + print_a ld a, d and 0x0f - or 0x30 - rst print_a + fixdig + print_a djnz prdig ;; Print the newline. -prnl: ld a, ' ' - rst print_a +prnl: ld a, spc + print_a ;; Increment the counter. ld hl, buf @@ -153,14 +138,13 @@ squish: print: ;; Print the string at hl. ld a, (hl) - cp 0 + endstrp ret z - rst print_a + print_a inc hl jr print -fizz: defb "fizz", 0 -buzz: defb "buzz", 0 - -len: defb 0 -buf: defs 256 + ;; Initial state. The buffer notionally continues for another 254 + ;; bytes, but there's no point in including them in the image. +len: db 1 +buf: db 1 diff --git a/spectrum.s b/spectrum.s new file mode 100644 index 0000000..01c29f8 --- /dev/null +++ b/spectrum.s @@ -0,0 +1,29 @@ +;;; -*-asm-*- + + org 0x7000 + +spc: equ ' ' + +tvflag: equ 0x5c3c + +fixdig: macro + or 0x30 + endm + +print_a: macro + rst 0x10 + endm + +endstrp: macro + and a + endm + + jr setup + +fizz: db "fizz", 0 +buzz: db "buzz", 0 + +setup: + ;; Use the main screen. + xor a + ld (tvflag), a -- 2.11.0