Summary Files Commits Branches @commit @diff @stats
path: root / catalan / print.a65
blob: 655c16264a1a5503b9b9c5bdf37f24ec66a39930 (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
#ifndef CATALAN_PRINT
#define CATALAN_PRINT

#include "kernal.a65"
#include "math.a65"

#define print(str) .(                          :\
	ldx #0       ; x = 0                   :\
loop 	lda str, x   ; while (_1[x] != 0)      :\
	beq end                                :\
	putchar()    ;    putchar(_1[x])       :\
	inx          ;    ++x                  :\
	bne loop     ;    if (x == 0) break    :\
end                                            :\
.)

#define puta() .(                          :\
	pha		; push a           :\
	clc		; carry = 0        :\
	adc #"0"	; a += '0' + carry :\
	putchar()                          :\
	pla		; pop a            :\
.)

; print decimal value of a
printa .(
	ora #0		; Make sure flags correspond to accumulator value
	cmp #10		; if (a < 10)
	bcc digit1
	cmp #100	; if (a < 100)
	bcc digit2

digit3	pha			; push a
	starg(diva_arg, #100)	; a /= 100
	jsr diva
	puta()
	pla			; pop a
	starg(moda_arg, #100)	; a %= 100
	jsr moda

digit2	pha			; push a
	starg(diva_arg, #10)	; a /= 10
	jsr diva
	puta()
	pla			; pop a
	starg(moda_arg, #10)	; a %= 10
	jsr moda

digit1	puta()
	rts
.)

#endif /* CATALAN_PRINT */
// vim: noet ts=8 sw=0 lcs=tab\:\ \