Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pret
GitHub Repository: pret/pokered
Path: blob/master/home/print_bcd.asm
1270 views
1
; function to print a BCD (Binary-coded decimal) number
2
; de = address of BCD number
3
; hl = destination address
4
; c = flags and length
5
; bit 7: if set, do not print leading zeroes
6
; if unset, print leading zeroes
7
; bit 6: if set, left-align the string (do not pad empty digits with spaces)
8
; if unset, right-align the string
9
; bit 5: if set, print currency symbol at the beginning of the string
10
; if unset, do not print the currency symbol
11
; bits 0-4: length of BCD number in bytes
12
; Note that bits 5 and 7 are modified during execution. The above reflects
13
; their meaning at the beginning of the functions's execution.
14
PrintBCDNumber::
15
ld b, c ; save flags in b
16
res BIT_LEADING_ZEROES, c
17
res BIT_LEFT_ALIGN, c
18
res BIT_MONEY_SIGN, c ; c now holds the length
19
bit BIT_MONEY_SIGN, b
20
jr z, .loop
21
bit BIT_LEADING_ZEROES, b
22
jr nz, .loop
23
ld [hl], "¥"
24
inc hl
25
.loop
26
ld a, [de]
27
swap a
28
call PrintBCDDigit ; print upper digit
29
ld a, [de]
30
call PrintBCDDigit ; print lower digit
31
inc de
32
dec c
33
jr nz, .loop
34
bit BIT_LEADING_ZEROES, b
35
jr z, .done ; if so, we are done
36
.numberEqualsZero ; if every digit of the BCD number is zero
37
bit BIT_LEFT_ALIGN, b
38
jr nz, .skipRightAlignmentAdjustment
39
dec hl ; if the string is right-aligned, it needs to be moved back one space
40
.skipRightAlignmentAdjustment
41
bit BIT_MONEY_SIGN, b
42
jr z, .skipCurrencySymbol
43
ld [hl], "¥"
44
inc hl
45
.skipCurrencySymbol
46
ld [hl], "0"
47
call PrintLetterDelay
48
inc hl
49
.done
50
ret
51
52
PrintBCDDigit::
53
and $f
54
and a
55
jr z, .zeroDigit
56
.nonzeroDigit
57
bit BIT_LEADING_ZEROES, b
58
jr z, .outputDigit
59
; if bit 7 is set, then no numbers have been printed yet
60
bit BIT_MONEY_SIGN, b
61
jr z, .skipCurrencySymbol
62
ld [hl], "¥"
63
inc hl
64
res BIT_MONEY_SIGN, b
65
.skipCurrencySymbol
66
res BIT_LEADING_ZEROES, b
67
.outputDigit
68
add "0"
69
ld [hli], a
70
jp PrintLetterDelay
71
.zeroDigit
72
bit BIT_LEADING_ZEROES, b
73
jr z, .outputDigit ; if so, print a zero digit
74
bit BIT_LEFT_ALIGN, b
75
ret nz
76
inc hl ; if right-aligned, "print" a space by advancing the pointer
77
ret
78
79