aboutsummaryrefslogtreecommitdiffstats
path: root/home/print_bcd.asm
diff options
context:
space:
mode:
authorRangi <35663410+Rangi42@users.noreply.github.com>2020-07-04 16:19:15 -0400
committerGitHub <noreply@github.com>2020-07-04 16:19:15 -0400
commit2718c36065e7eb201a149938bcdd51987c4e56b6 (patch)
tree4a1c0294193a497e15ec8e6be59390eb1f84b5d3 /home/print_bcd.asm
parentMerge pull request #256 from Rangi42/master (diff)
parentUPDATE_PARTY_MENU_BLK_PACKET -> SET_PAL_PARTY_MENU_HP_BARS, and introduce SET... (diff)
downloadpokeyellow-2718c36065e7eb201a149938bcdd51987c4e56b6.tar.gz
pokeyellow-2718c36065e7eb201a149938bcdd51987c4e56b6.tar.xz
pokeyellow-2718c36065e7eb201a149938bcdd51987c4e56b6.zip
Merge pull request #257 from Rangi42/master
More reorganization
Diffstat (limited to 'home/print_bcd.asm')
-rw-r--r--home/print_bcd.asm77
1 files changed, 77 insertions, 0 deletions
diff --git a/home/print_bcd.asm b/home/print_bcd.asm
new file mode 100644
index 00000000..38aedd3c
--- /dev/null
+++ b/home/print_bcd.asm
@@ -0,0 +1,77 @@
+; function to print a BCD (Binary-coded decimal) number
+; de = address of BCD number
+; hl = destination address
+; c = flags and length
+; bit 7: if set, do not print leading zeroes
+; if unset, print leading zeroes
+; bit 6: if set, left-align the string (do not pad empty digits with spaces)
+; if unset, right-align the string
+; bit 5: if set, print currency symbol at the beginning of the string
+; if unset, do not print the currency symbol
+; bits 0-4: length of BCD number in bytes
+; Note that bits 5 and 7 are modified during execution. The above reflects
+; their meaning at the beginning of the functions's execution.
+PrintBCDNumber::
+ ld b, c ; save flags in b
+ res 7, c
+ res 6, c
+ res 5, c ; c now holds the length
+ bit 5, b
+ jr z, .loop
+ bit 7, b
+ jr nz, .loop
+ ld [hl], "¥"
+ inc hl
+.loop
+ ld a, [de]
+ swap a
+ call PrintBCDDigit ; print upper digit
+ ld a, [de]
+ call PrintBCDDigit ; print lower digit
+ inc de
+ dec c
+ jr nz, .loop
+ bit 7, b ; were any non-zero digits printed?
+ jr z, .done ; if so, we are done
+.numberEqualsZero ; if every digit of the BCD number is zero
+ bit 6, b ; left or right alignment?
+ jr nz, .skipRightAlignmentAdjustment
+ dec hl ; if the string is right-aligned, it needs to be moved back one space
+.skipRightAlignmentAdjustment
+ bit 5, b
+ jr z, .skipCurrencySymbol
+ ld [hl], "¥"
+ inc hl
+.skipCurrencySymbol
+ ld [hl], "0"
+ call PrintLetterDelay
+ inc hl
+.done
+ ret
+
+PrintBCDDigit::
+ and $f
+ and a
+ jr z, .zeroDigit
+.nonzeroDigit
+ bit 7, b ; have any non-space characters been printed?
+ jr z, .outputDigit
+; if bit 7 is set, then no numbers have been printed yet
+ bit 5, b ; print the currency symbol?
+ jr z, .skipCurrencySymbol
+ ld [hl], "¥"
+ inc hl
+ res 5, b
+.skipCurrencySymbol
+ res 7, b ; unset 7 to indicate that a nonzero digit has been reached
+.outputDigit
+ add "0"
+ ld [hli], a
+ jp PrintLetterDelay
+.zeroDigit
+ bit 7, b ; either printing leading zeroes or already reached a nonzero digit?
+ jr z, .outputDigit ; if so, print a zero digit
+ bit 6, b ; left or right alignment?
+ ret nz
+ inc hl ; if right-aligned, "print" a space by advancing the pointer
+ ret