Files
pokegold-spaceworld/home/print_bcd.asm
luckytyphlosion e1659ecd41 Introduce linkerscript.
Addresses of sections will now be added to the linkerscript via `org`, and the section name will be the path/to/file. If there is more than one section in the file, then add a @SectionName after the path/to/file to describe the section.
2018-07-03 17:07:05 -04:00

63 lines
1.6 KiB
NASM
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

INCLUDE "constants.asm"
; if DEBUG
SECTION "home/print_bcd.asm", ROM0
; else
; SECTION "BCD Functions", ROM0[$3A76]
; endc
; 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
; bits 0-5: 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:: ; 3ab2 (0:3ab2)
ld b, c ; save flags in b
res 7, c
res 6, c ; c now holds the length
.loop
ld a, [de]
swap a
call PrintBCDDigit
ld a, [de]
call PrintBCDDigit
inc de
dec c
jr nz, .loop
bit 7, b ; were any non-zero digits printed?
jr z, .done
.numberEqualsZero ; if every digit of the BCD number is zero
bit 6, b
jr nz, .skipRightAlignmentAdjustment
dec hl ; if the string is right-aligned, it needs
.skipRightAlignmentAdjustment ;to be moved back one space
ld [hl], ""
call PrintLetterDelay
inc hl
.done
ret
PrintBCDDigit:: ; 3ad5 (0:3ad5)
and $0f
and a
jr z, .zeroDigit
res 7, b ; unset 7 to indicate that a nonzero
.outputDigit ; digit has been reached
add ""
ld [hli], a
jp PrintLetterDelay
.zeroDigit
bit 7, b ; either printing leading zeroes or
jr z, .outputDigit ; already reached a nonzero digit?
bit 6, b
ret nz ; left-align, don't pad with space
ld a, " "
ld [hli], a
ret
; 0x3aed