#ifndef CATALAN_MATH #define CATALAN_MATH #define starg(name, value) .( :\ pha ; push a :\ lda value ; a = value :\ sta name ; name = a :\ pla ; pop a :\ .) #define applya(subroutine, arg, value) .( :\ starg(arg, value) :\ jsr subroutine :\ .) ; a *= multa_arg multa .( ora #0 ; Update flags, so they're accurate with the accumulator value beq return ; if (a == 0) return a cmp #1 ; if (a == 1) beq r_arg ; return arg ldx arg ; x = arg cpx #0 ; if (x == 0) beq r_zero ; return 0 cpx #1 ; if (x == 1) beq return ; return a dex ; --x // accumulator already has the first value sta addend ; addend = a loop clc ; do carry = 0 adc addend ; a += addend + carry dex ; --x cpx #1 ; while (x >= 1) bcs loop return ora #0 ; Update flags, so they're accurate with the accumulator value rts r_zero lda #0 rts r_arg lda arg rts +multa_arg arg .byt 0 addend .byt 0 .) ; a /= diva_arg diva .( ora #0 ; Update flags, so they're accurate with the accumulator value beq return ; if (a == 0) return a ldx arg ; x = arg cpx #0 ; if (x == 0) beq r_zero ; return 0 cpx #1 ; if (x == 1) beq 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 txa rts r_zero lda #0 rts +diva_arg 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\:\ \