Summary Files Commits Branches @commit @diff @stats
path: root / catalan / catalan.a65
blob: 53d30eba3354a68c316d2af18b20c04f9635235f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#include "basic_starter.a65"
#include "kernal.a65"

hello	.byt "HELLO, WORLD!",0

// accumulator = a * b
#define multiply(a, b) .(                     :\
	lda a       ; if (a == 0)             :\
	beq return  ;   return 0              :\
	sta buffer  ; buffer = a              :\
                                               \
	lda b       ; if (b == 0)             :\
	beq return  ;   return 0              :\
	tax         ; x = b                   :\
                                               \
	lda #0      ; a = 0                   :\
loop	clc         ; do  carry = 0           :\
	adc buffer  ;     a += buffer + carry :\
	dex         ;     --x                 :\
	cpx #0      ; while (x > 0)           :\
	bne loop                              :\
return                                        :\
.)

// accumulator = a / b
#define divide(a, b) .(                        :\
	lda b       ; if (b == 0)              :\
	beq return  ;   return 0               :\
	sta buffer  ; buffer = b               :\
                                                \
	lda a       ; if (a == 0)              :\
	beq return  ;   return 0               :\
	ldx #0      ; x = 0                    :\
                                                \
loop	sec         ; do  carry = 1            :\
	sbc buffer  ;     a -= buffer + !carry :\
	inx         ;     ++x                  :\
	cmp buffer  ; while (a >= buffer)      :\
	bcs loop                               :\
return  txa                                    :\
.)

cat_n	.byt 1
n	.byt 0

main
	lda #9
	sta diva_arg1
	lda #144
	jsr diva
	clc
	adc #49
	putchar()
	rts

// a *= multa_arg1
multa .(
	beq return	; if (a == 0) return a
	cmp #1		; if (a == 1)
	beq r_arg1	;   return arg1

	ldx arg1	; x = arg1
	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_arg1	lda arg1
	rts

+multa_arg1
arg1		.byt 0
addend		.byt 0
.)

// a /= diva_arg1
diva .(
	ora #0		; Update flags, so they're accurate with the accumulator value
	beq return	; if (a == 0) return a

	ldx arg1	; x = arg1
	cpx #0		; if (x == 0)
	beq r_zero	;   return 0
	cpx #1		; if (x == 1)
	beq return	;   return a

	stx subtrahend	; subtrahend = x
	ldx #0		; x = 0
loop	sec		; do  carry = 1
	sbc subtrahend	;     a -= addend + !carry
	inx		;     ++x
	cmp subtrahend	; while (a >= subtrahend)
	bcs loop

return	txa
	rts

r_zero	lda #0
	rts

+diva_arg1
arg1		.byt 0
subtrahend	.byt 0
.)

buffer	.byt 0

// vim: noet ts=8 sw=0 lcs=tab\:\ \