new 'print_integer' routine, nearly 120 bytes shorter

This commit is contained in:
FeralChild64 committed 2020-07-12 08:44:08 +02:00
1 parent 02f532e2ec
commit 2b5c01d3bb
4 files changed
+103 -127

No files matched your search

+55 -127
View File
@@ -2,147 +2,75 @@
// #LAYOUT# * BASIC_0 #TAKE
// #LAYOUT# * * #IGNORE
//
// Works similar to $BDCD in original C64 BASIC
// XXX Should eventually get replaced by a more flexible and optimal routine.
// Temp result gets written to $0100-$0104
//
print_integer:
pha
phx_trash_a
// Tested with original C64 ROMs, that it trashes FAC1 too
// Clear temporary output
lda #$00
ldy #5
!: sta $0100,y
dey
bpl !-
pla
ldy #23 // 8 x 3 - 1 = offset of last digit of 128
lo_loop:
cmp #$7f
bcc bit_clear
sta FAC1_exponent+1
stx FAC1_exponent+0
// Bit set, so add the digits
pha
ldx #2
clc
lo_add_loop:
lda $0102,x
adc number_table_lo,y
sta $0102,x
// Check for 0
ora FAC1_exponent+0
bne print_integer_not_0
// Just print '0' and quit
lda #$30
jmp JCHROUT
print_integer_not_0:
// Try to find the index of the first digit
ldy #$04
!:
jsr print_integer_compare
bcs print_integer_digit
dey
dex
bpl lo_add_loop
pla
jmp next_lo_bit
bit_clear:
dey
dey
dey
next_lo_bit:
asl
cpy #$7f
bcc lo_loop
bpl !- // branch always
print_integer_digit:
// Now do the same for upper byte
pla
ldy #39 // 8 x 5 - 1 = offset of last digit of 32768
hi_loop:
cmp #$7f
bcc bit_clear_hi
// Determine digit to print
// Bit set, so add the digits
pha
ldx #4
clc
hi_add_loop:
lda $0100,x
adc number_table_hi,y
sta $0100,x
dey
dex
bpl hi_add_loop
pla
jmp next_hi_bit
bit_clear_hi:
dey
dey
dey
dey
dey
next_hi_bit:
asl
cpy #$7f
bcc hi_loop
fix_carry:
// Deal with any carries
ldx #3
carry_fix_loop:
lda $0101,x
cmp #10
ldx #$00
!:
jsr print_integer_compare
bcc !+
inc $0100,x
// Raise digit, lower the integer
inx
sec
sbc #10
sta $0101,x
lda FAC1_exponent+0
sbc print_integer_tab_lo, y
sta FAC1_exponent+0
lda FAC1_exponent+1
sbc print_integer_tab_hi, y
sta FAC1_exponent+1
bcs !- // branch always
!:
// Convert digit in .X to PETSCII in .A and print it out
jmp carry_fix_loop
txa
clc
adc #$30
!: dex
bpl carry_fix_loop
post_carry:
// Got digits.
// Skip leading zeros, and print the resulting number
// Skip leading zeros
ldy #0
!: lda $0100,y
bne found_start_of_number
iny
cpy #4
bne !-
found_start_of_number:
// Print digits
!: lda $0100,y
ora #$30
jsr JCHROUT
iny
cpy #5
bne !-
// Check if next iteration is needed
dey
bpl print_integer_digit
// The end
rts
number_table_lo:
.byte 0,0,1
.byte 0,0,2
.byte 0,0,4
.byte 0,0,8
.byte 0,1,6
.byte 0,3,2
.byte 0,6,4
.byte 1,2,8
number_table_hi:
.byte 0,0,2,5,6
.byte 0,0,5,1,2
.byte 0,1,0,2,4
.byte 0,2,0,4,8
.byte 0,4,0,9,6
.byte 0,8,1,9,2
.byte 1,6,3,8,4
.byte 3,2,7,6,8
+18
View File
@@ -0,0 +1,18 @@
// #LAYOUT# STD * #TAKE
// #LAYOUT# * BASIC_0 #TAKE
// #LAYOUT# * * #IGNORE
//
// Helper compare subroutine for 'print_integer'
//
print_integer_compare:
lda FAC1_exponent+1
cmp print_integer_tab_hi, y
bne !+
lda FAC1_exponent+0
cmp print_integer_tab_lo, y
!:
rts
+15
View File
@@ -0,0 +1,15 @@
// #LAYOUT# STD * #TAKE
// #LAYOUT# * BASIC_0 #TAKE
// #LAYOUT# * * #IGNORE
//
// Helper power of 10s table (high buytes) for 'print_integer'
//
print_integer_tab_hi:
.byte >1
.byte >10
.byte >100
.byte >1000
.byte >10000
+15
View File
@@ -0,0 +1,15 @@
// #LAYOUT# STD * #TAKE
// #LAYOUT# * BASIC_0 #TAKE
// #LAYOUT# * * #IGNORE
//
// Helper power of 10s table (low buytes) for 'print_integer'
//
print_integer_tab_lo:
.byte <1
.byte <10
.byte <100
.byte <1000
.byte <10000