Skip to content

Instantly share code, notes, and snippets.

@digarok
Created May 23, 2017 14:39
Show Gist options
  • Save digarok/12ce4a12993c4c4444c0819e9fcc752a to your computer and use it in GitHub Desktop.
Save digarok/12ce4a12993c4c4444c0819e9fcc752a to your computer and use it in GitHub Desktop.
org $300 ;Tiny program so we'll use the page at $300
COUT equ $FDED ;equates for two firmware routines we'll be using
PRHEX equ $FDDA ;COUT prints a character, PRHEX prints a 2-digit hex value
Init lda #0 ;starting number (will increment at beginning of loop)
sta $0
MainLoop sed ;use decimal mode to increment
lda $0
clc
adc #1 ;increment our number using addition
sta $0
cld ;return to hex
lda $0 ;superfluous?
bne :notDone
jmp Done
:notDone jsr PRHEX ;print our number
lda #" " ;load a space
jsr COUT
lda #3
sta $1 ;store fizz number
jsr Modulus
bne :noFizz ;not %3 (non-zero result)
:fizz lda #5 ;fizz check 5
sta $1
jsr Modulus
cmp #0
beq :fizzBuzz ;wow! both fizz and buzz
lda #<fizzS ;load low byte of string address
sta $2 ;store as zero-page pointer
lda #>fizzS ;load high byte of string address
sta $3 ;store as zero-page pointer
jsr PrintStr ;print fizz
jmp MainLoop ;loop
:fizzBuzz lda #<fizzbuzzS ;load low byte of string address
sta $2 ;store as zero-page pointer
lda #>fizzbuzzS ;load high byte of string address
sta $3 ;store as zero-page pointer
jsr PrintStr ;print fizzbuzz
jmp MainLoop ;loop
:noFizz lda #5 ;but we still need to check for a buzz
sta $1 ;now check 5
jsr Modulus
bne :noFizzOrBuzz
:buzz lda #<buzzS ;load low byte of string address
sta $2 ;store as zero-page pointer
lda #>buzzS ;load high byte of string address
sta $3 ;store as zero-page pointer
jsr PrintStr ;print fizzbuzz
;FALL THROUGH TO jmp
:noFizzOrBuzz jmp MainLoop ;What a letdown!
Done rts
* Print a zero terminated string... pointer to string in $02,$03
PrintStr ldy #0
:loop lda ($02),y
beq :endStr
jsr COUT
iny
bra :loop
:endStr rts
fizzS asc "Fizz! "
db 0
buzzS equ *+4 ;this is just a pointer to the middle of the FizzBuzz! string... ha!
fizzbuzzS asc "FizzBuzz! "
db 0
* Modulus function in decimal mode
* variables are in direct page locations $00 and $01
Modulus sed
lda $00 ;load our main number
sec
:modloop sbc $01 ;subract our mod number
bcs :modloop ;over and over until we underflow
adc $01 ;reverse our last subtraction to see where we ended up
cld
rts ;and return that value in A (the modulus)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment