2020-07-04 02:57:43 +00:00
|
|
|
PrintNumber::
|
|
|
|
; Print the c-digit, b-byte value at de.
|
|
|
|
; Allows 2 to 7 digits. For 1-digit numbers, add
|
|
|
|
; the value to char "0" instead of calling PrintNumber.
|
|
|
|
; Flags LEADING_ZEROES and LEFT_ALIGN can be given
|
|
|
|
; in bits 7 and 6 of b respectively.
|
|
|
|
push bc
|
|
|
|
xor a
|
2020-07-06 23:03:05 +00:00
|
|
|
ldh [hPastLeadingZeros], a
|
|
|
|
ldh [hNumToPrint], a
|
|
|
|
ldh [hNumToPrint + 1], a
|
2020-07-04 02:57:43 +00:00
|
|
|
ld a, b
|
|
|
|
and $f
|
|
|
|
cp 1
|
|
|
|
jr z, .byte
|
|
|
|
cp 2
|
|
|
|
jr z, .word
|
|
|
|
.long
|
|
|
|
ld a, [de]
|
2020-07-06 23:03:05 +00:00
|
|
|
ldh [hNumToPrint], a
|
2020-07-04 02:57:43 +00:00
|
|
|
inc de
|
|
|
|
ld a, [de]
|
2020-07-06 23:03:05 +00:00
|
|
|
ldh [hNumToPrint + 1], a
|
2020-07-04 02:57:43 +00:00
|
|
|
inc de
|
|
|
|
ld a, [de]
|
2020-07-06 23:03:05 +00:00
|
|
|
ldh [hNumToPrint + 2], a
|
2020-07-04 02:57:43 +00:00
|
|
|
jr .start
|
|
|
|
|
|
|
|
.word
|
|
|
|
ld a, [de]
|
2020-07-06 23:03:05 +00:00
|
|
|
ldh [hNumToPrint + 1], a
|
2020-07-04 02:57:43 +00:00
|
|
|
inc de
|
|
|
|
ld a, [de]
|
2020-07-06 23:03:05 +00:00
|
|
|
ldh [hNumToPrint + 2], a
|
2020-07-04 02:57:43 +00:00
|
|
|
jr .start
|
|
|
|
|
|
|
|
.byte
|
|
|
|
ld a, [de]
|
2020-07-06 23:03:05 +00:00
|
|
|
ldh [hNumToPrint + 2], a
|
2020-07-04 02:57:43 +00:00
|
|
|
|
|
|
|
.start
|
|
|
|
push de
|
|
|
|
|
|
|
|
ld d, b
|
|
|
|
ld a, c
|
|
|
|
ld b, a
|
|
|
|
xor a
|
|
|
|
ld c, a
|
|
|
|
ld a, b
|
|
|
|
|
|
|
|
cp 2
|
|
|
|
jr z, .tens
|
|
|
|
cp 3
|
|
|
|
jr z, .hundreds
|
|
|
|
cp 4
|
|
|
|
jr z, .thousands
|
|
|
|
cp 5
|
|
|
|
jr z, .ten_thousands
|
|
|
|
cp 6
|
|
|
|
jr z, .hundred_thousands
|
|
|
|
|
2022-06-06 21:25:31 +00:00
|
|
|
MACRO print_digit
|
2020-07-04 02:57:43 +00:00
|
|
|
|
2022-06-06 21:25:31 +00:00
|
|
|
IF (\1) / $10000
|
|
|
|
ld a, \1 / $10000 % $100
|
|
|
|
ELSE
|
|
|
|
xor a
|
|
|
|
ENDC
|
2020-07-06 23:03:05 +00:00
|
|
|
ldh [hPowerOf10 + 0], a
|
2020-07-04 02:57:43 +00:00
|
|
|
|
2022-06-06 21:25:31 +00:00
|
|
|
IF (\1) / $100
|
|
|
|
ld a, \1 / $100 % $100
|
|
|
|
ELSE
|
|
|
|
xor a
|
|
|
|
ENDC
|
2020-07-06 23:03:05 +00:00
|
|
|
ldh [hPowerOf10 + 1], a
|
2020-07-04 02:57:43 +00:00
|
|
|
|
|
|
|
ld a, \1 / $1 % $100
|
2020-07-06 23:03:05 +00:00
|
|
|
ldh [hPowerOf10 + 2], a
|
2020-07-04 02:57:43 +00:00
|
|
|
|
|
|
|
call .PrintDigit
|
|
|
|
call .NextDigit
|
2020-07-06 16:28:31 +00:00
|
|
|
ENDM
|
2020-07-04 02:57:43 +00:00
|
|
|
|
|
|
|
.millions print_digit 1000000
|
|
|
|
.hundred_thousands print_digit 100000
|
|
|
|
.ten_thousands print_digit 10000
|
|
|
|
.thousands print_digit 1000
|
|
|
|
.hundreds print_digit 100
|
|
|
|
|
|
|
|
.tens
|
|
|
|
ld c, 0
|
2020-07-06 23:03:05 +00:00
|
|
|
ldh a, [hNumToPrint + 2]
|
2020-07-04 02:57:43 +00:00
|
|
|
.mod
|
|
|
|
cp 10
|
|
|
|
jr c, .ok
|
|
|
|
sub 10
|
|
|
|
inc c
|
|
|
|
jr .mod
|
|
|
|
.ok
|
|
|
|
|
|
|
|
ld b, a
|
2020-07-06 23:03:05 +00:00
|
|
|
ldh a, [hPastLeadingZeros]
|
2020-07-04 02:57:43 +00:00
|
|
|
or c
|
2020-07-06 23:03:05 +00:00
|
|
|
ldh [hPastLeadingZeros], a
|
2020-07-04 02:57:43 +00:00
|
|
|
jr nz, .past
|
|
|
|
call .PrintLeadingZero
|
|
|
|
jr .next
|
|
|
|
.past
|
|
|
|
ld a, "0"
|
|
|
|
add c
|
|
|
|
ld [hl], a
|
|
|
|
.next
|
|
|
|
|
|
|
|
call .NextDigit
|
|
|
|
.ones
|
|
|
|
ld a, "0"
|
|
|
|
add b
|
|
|
|
ld [hli], a
|
|
|
|
pop de
|
|
|
|
dec de
|
|
|
|
pop bc
|
|
|
|
ret
|
|
|
|
|
|
|
|
.PrintDigit:
|
|
|
|
; Divide by the current decimal place.
|
|
|
|
; Print the quotient, and keep the modulus.
|
|
|
|
ld c, 0
|
|
|
|
.loop
|
2020-07-06 23:03:05 +00:00
|
|
|
ldh a, [hPowerOf10]
|
2020-07-04 02:57:43 +00:00
|
|
|
ld b, a
|
2020-07-06 23:03:05 +00:00
|
|
|
ldh a, [hNumToPrint]
|
|
|
|
ldh [hSavedNumToPrint], a
|
2020-07-04 02:57:43 +00:00
|
|
|
cp b
|
|
|
|
jr c, .underflow0
|
|
|
|
sub b
|
2020-07-06 23:03:05 +00:00
|
|
|
ldh [hNumToPrint], a
|
|
|
|
ldh a, [hPowerOf10 + 1]
|
2020-07-04 02:57:43 +00:00
|
|
|
ld b, a
|
2020-07-06 23:03:05 +00:00
|
|
|
ldh a, [hNumToPrint + 1]
|
|
|
|
ldh [hSavedNumToPrint + 1], a
|
2020-07-04 02:57:43 +00:00
|
|
|
cp b
|
|
|
|
jr nc, .noborrow1
|
|
|
|
|
2020-07-06 23:03:05 +00:00
|
|
|
ldh a, [hNumToPrint]
|
2020-07-04 02:57:43 +00:00
|
|
|
or 0
|
|
|
|
jr z, .underflow1
|
|
|
|
dec a
|
2020-07-06 23:03:05 +00:00
|
|
|
ldh [hNumToPrint], a
|
|
|
|
ldh a, [hNumToPrint + 1]
|
2020-07-04 02:57:43 +00:00
|
|
|
.noborrow1
|
|
|
|
|
|
|
|
sub b
|
2020-07-06 23:03:05 +00:00
|
|
|
ldh [hNumToPrint + 1], a
|
|
|
|
ldh a, [hPowerOf10 + 2]
|
2020-07-04 02:57:43 +00:00
|
|
|
ld b, a
|
2020-07-06 23:03:05 +00:00
|
|
|
ldh a, [hNumToPrint + 2]
|
|
|
|
ldh [hSavedNumToPrint + 2], a
|
2020-07-04 02:57:43 +00:00
|
|
|
cp b
|
|
|
|
jr nc, .noborrow2
|
|
|
|
|
2020-07-06 23:03:05 +00:00
|
|
|
ldh a, [hNumToPrint + 1]
|
2020-07-04 02:57:43 +00:00
|
|
|
and a
|
|
|
|
jr nz, .borrowed
|
|
|
|
|
2020-07-06 23:03:05 +00:00
|
|
|
ldh a, [hNumToPrint]
|
2020-07-04 02:57:43 +00:00
|
|
|
and a
|
|
|
|
jr z, .underflow2
|
|
|
|
dec a
|
2020-07-06 23:03:05 +00:00
|
|
|
ldh [hNumToPrint], a
|
2020-07-04 02:57:43 +00:00
|
|
|
xor a
|
|
|
|
.borrowed
|
|
|
|
|
|
|
|
dec a
|
2020-07-06 23:03:05 +00:00
|
|
|
ldh [hNumToPrint + 1], a
|
|
|
|
ldh a, [hNumToPrint + 2]
|
2020-07-04 02:57:43 +00:00
|
|
|
.noborrow2
|
|
|
|
sub b
|
2020-07-06 23:03:05 +00:00
|
|
|
ldh [hNumToPrint + 2], a
|
2020-07-04 02:57:43 +00:00
|
|
|
inc c
|
|
|
|
jr .loop
|
|
|
|
|
|
|
|
.underflow2
|
2020-07-06 23:03:05 +00:00
|
|
|
ldh a, [hSavedNumToPrint + 1]
|
|
|
|
ldh [hNumToPrint + 1], a
|
2020-07-04 02:57:43 +00:00
|
|
|
.underflow1
|
2020-07-06 23:03:05 +00:00
|
|
|
ldh a, [hSavedNumToPrint]
|
|
|
|
ldh [hNumToPrint], a
|
2020-07-04 02:57:43 +00:00
|
|
|
.underflow0
|
2020-07-06 23:03:05 +00:00
|
|
|
ldh a, [hPastLeadingZeros]
|
2020-07-04 02:57:43 +00:00
|
|
|
or c
|
|
|
|
jr z, .PrintLeadingZero
|
|
|
|
|
|
|
|
ld a, "0"
|
|
|
|
add c
|
|
|
|
ld [hl], a
|
2020-07-06 23:03:05 +00:00
|
|
|
ldh [hPastLeadingZeros], a
|
2020-07-04 02:57:43 +00:00
|
|
|
ret
|
|
|
|
|
|
|
|
.PrintLeadingZero:
|
|
|
|
bit BIT_LEADING_ZEROES, d
|
|
|
|
ret z
|
|
|
|
ld [hl], "0"
|
|
|
|
ret
|
|
|
|
|
|
|
|
.NextDigit:
|
|
|
|
; Increment unless the number is left-aligned,
|
|
|
|
; leading zeroes are not printed, and no digits
|
|
|
|
; have been printed yet.
|
|
|
|
bit BIT_LEADING_ZEROES, d
|
|
|
|
jr nz, .inc
|
|
|
|
bit BIT_LEFT_ALIGN, d
|
|
|
|
jr z, .inc
|
2020-07-06 23:03:05 +00:00
|
|
|
ldh a, [hPastLeadingZeros]
|
2020-07-04 02:57:43 +00:00
|
|
|
and a
|
|
|
|
ret z
|
|
|
|
.inc
|
|
|
|
inc hl
|
|
|
|
ret
|