Summary Files Commits Branches @commit @diff @stats
diff options
context:
space:
mode:
Commit8dfce9012a7f28485ea9d4b2391c7352ce60a3f7 (patch)
Author
 Syndamia <kamen@syndamia.com>
2025-08-03 11:36:39 +0300
Committer
 Syndamia <kamen@syndamia.com>
2025-08-03 11:36:39 +0300
Filesde033a3c00afd1d1076d8c58c0dc4385b8bac725
c64-programs-8dfce9012a7f28485ea9d4b2391c7352ce60a3f7.tar
c64-programs-8dfce9012a7f28485ea9d4b2391c7352ce60a3f7.tar.gz
c64-programs-8dfce9012a7f28485ea9d4b2391c7352ce60a3f7.zip
Parentef864ce4111fc204f74f490957bd85ec02aafc05 (diff)
feat(catalan): Implement modulo
2 files changed, 46 insertions, 7 deletions
-rw-r--r--catalan/catalan.a6524
-rw-r--r--catalan/math.a6529
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\:\ \