From 8dfce9012a7f28485ea9d4b2391c7352ce60a3f7 Mon Sep 17 00:00:00 2001 From: Syndamia Date: Sun, 3 Aug 2025 11:36:39 +0300 Subject: feat(catalan): Implement modulo --- catalan/catalan.a65 | 24 +++++++++++++++++------- catalan/math.a65 | 29 +++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 7 deletions(-) diff --git a/catalan/catalan.a65 b/catalan/catalan.a65 index c51f040..dbc4fd9 100644 --- a/catalan/catalan.a65 +++ b/catalan/catalan.a65 @@ -4,21 +4,31 @@ hello .byt "HELLO, WORLD!",0 +#define store_in_memory(memory, value) .( :\ + pha ; push a :\ + lda value ; a = value :\ + sta memory ; memory = a :\ + pla ; pop a :\ +.) + +#define applya1(subroutine, valuea, arg1, value1) .( :\ + lda valuea :\ + store_in_memory(arg1, value1) :\ + jsr subroutine :\ +.) main .( + applya1(moda, #34, moda_arg, #5) + clc + adc #"0" + putchar() + jsr catalan lda cat_n putchar() rts .) -#define store_in_memory(memory, value) .( :\ - pha ; push a :\ - lda value ; a = value :\ - sta memory ; memory = a :\ - pla ; pop a :\ -.) - /* a = x * a *= 4 * a -= 2 diff --git a/catalan/math.a65 b/catalan/math.a65 index 434fd8d..7bd850a 100644 --- a/catalan/math.a65 +++ b/catalan/math.a65 @@ -66,5 +66,34 @@ arg .byt 0 subtrahend .byt 0 .) +; a %= moda_arg +moda .( + ldx arg ; x = arg + cpx #1 ; if (x <= 1) + bcc r_zero ; return 0 + beq r_zero + + ora #0 ; Update flags, so they're accurate with the accumulator value + beq return ; if (a == 0) return a + + cmp arg ; if (a < arg) + bcc return ; return a + ldx #0 ; x = 0 + +loop sec ; do carry = 1 + sbc arg ; a -= arg + !carry + inx ; ++x + cmp arg ; while (a >= arg) + bcs loop + +return rts + +r_zero lda #0 + rts + ++moda_arg +arg .byt 0 +.) + #endif /* CATALAN_MATH */ // vim: noet ts=9 sw=0 lcs=tab\:\ \ -- cgit v1.3.1-23-gd82d