\ Print arbitary size decimal number \ ================================== \ Prints decimal number by pushing digits in reverse order onto stack. \ \ See forum.6502.org/viewtopic.php?f=2&t=4894 \ and groups.google.com/g/comp.sys.apple2/c/_y27d_TxDHA \ \ On entry X=>base of four zero page locations \ Y= number of digits to pad to, 0 for no padding \ On exit 0,X to 3,X are set to zero \ X preserved \ A,Y corrupted \ Uses PRPAD = to hold pad count \ PRTEMP = bit counter \ Size 55 bytes \ \ Example code \ LDX #OSNUM \ X=>four bytes in zero page \ LDY #8 \ Y=pad to 8 digits \ JSR PRINTDEC \ Print number at !OSNUM \ \ Can print fewer than 32 bits by setting higher bytes \ to zero and setting Y appropriately, for example \ LDX #OSNUM \ X=>four bytes in zero page \ LDY #0 \ STY OSNUM+2 \ Ensure 16-bit number \ STY OSNUM+3 \ LDY #5 \ Y=pad to 5 digits \ JSR PRINTDEC \ Print number at OSNUM+0,OSNUM+1 \ \ This can display an arbitary number of digits from an arbitary-sized \ number by just increasing or decreasing the number of zero page \ locations used at label PRDEC10 and the bit count at PRDECDIGIT. \ .PRINTDEC STY PRPAD \ Number of padding+digit characters LDY #0 \ Digit counter .PRDECDIGIT LDA #32 \ 32-bit divide STA PRTEMP LDA #0 \ Remainder=0 CLV \ V=0 means divide result = 0 .PRDECDIV10 CMP #10/2 \ Calculate OSNUM/10 BCC PRDEC10 SBC #10/2+&80 \ Remove digit & set V=1 to show div result > 0 SEC \ Shift 1 into div result .PRDEC10 ROL 0,X \ Shift /10 result into OSNUM ROL 1,X \ The number of locations must match the bits ROL 2,X \ set at PRDECDIGIT ROL 3,X \ Can continue to arbitary size by adding more zero page locations \ ROL A \ Shift bits of input into acc (input mod 10) DEC PRTEMP BNE PRDECDIV10 \ Continue 32-bit divide ORA #ASC"0" \ Convert to ASCII character PHA \ Push low digit 0-9 to print INY \ Increase number of digits BVS PRDECDIGIT \ If V=1, result of /10 was >0, do next digit LDA #ASC" " \ Pad with spaces .PRDECLP1 CPY PRPAD BCS PRDECLP2 \ Enough padding pushed PHA \ Push leading space characters INY BNE PRDECLP1 .PRDECLP2 PLA \ Pop character left to right JSR OSWRCH \ Print it DEY BNE PRDECLP2 RTS \ Print arbitary size decimal number \ ================================== \ Prints decimal number by pushing digits in reverse order onto stack. \ This version uses a fixed location for the number, instead of an \ offset from X. This allows X to be used in the loop. \ \ On entry OSNUM+0..OSNUM+3 = locations holding number to print \ Y= number of digits to pad to, 0 for no padding \ On exit OSNUM+0..OSNUM+3 are set to zero \ A,X,Y corrupted \ Uses PRPAD = to hold pad count \ Size 51 bytes \ \ Example code \ LDY #8 \ Y=pad to 8 digits \ JSR PRINTDEC \ Print number at !OSNUM \ \ This can display an arbitary number of digits from an arbitary-sized \ number by just increasing or decreasing the number of location \ used at label PRDEC10 and the bit count at PRDECDIGIT. \ .PRINTDEC STY PRPAD \ Number of padding+digit characters LDY #0 \ Digit counter .PRDECDIGIT LDX #32 \ 32-bit divide LDA #0 \ Remainder=0 CLV \ V=0 means divide result = 0 .PRDECDIV10 CMP #10/2 \ Calculate OSNUM/10 BCC PRDEC10 SBC #10/2+&80 \ Remove digit & set V=1 to show div result > 0 SEC \ Shift 1 into div result .PRDEC10 ROL OSNUM+0 \ Shift /10 result into OSNUM ROL OSNUM+1 \ The number of locations must match the bits ROL OSNUM+2 \ set at PRDECDIGIT ROL OSNUM+3 ROL A \ Shift bits of input into acc (input mod 10) DEX BNE PRDECDIV10 \ Continue 32-bit divide ORA #ASC"0" \ Convert to ASCII character PHA \ Push low digit 0-9 to print INY \ Increase number of digits BVS PRDECDIGIT \ If V=1, result of /10 was >0, do next digit LDA #ASC" " \ Pad with spaces .PRDECLP1 CPY PRPAD BCS PRDECLP2 \ Enough padding pushed PHA \ Push leading space characters INY BNE PRDECLP1 .PRDECLP2 PLA \ Pop character left to right JSR OSWRCH \ Print it DEY BNE PRDECLP2 RTS \ Print arbitary size decimal number \ ================================== \ Prints decimal number by pushing digits in reverse order onto stack \ This version uses a fixed location for the number and doesn't \ implement padding. \ \ On entry OSNUM+0..OSNUM+3 = locations holding number to print \ Y= number of digits to pad to, 0 for no padding \ On exit OSNUM+0..OSNUM+3 are set to zero \ A,X,Y corrupted \ Size 40 bytes, 36 bytes for 16-bit decimal \ \ Example code \ JSR PRINTDEC \ Print number at !OSNUM \ \ This can display an arbitary number of digits from an arbitary-sized \ number by just increasing or decreasing the number of locations \ used at label PRDEC10 and the bit count at PRDECDIGIT. \ .PRINTDEC LDY #0 \ Digit counter .PRDECDIGIT LDX #32 \ 32-bit divide LDA #0 \ Remainder=0 CLV \ V=0 means divide result = 0 .PRDECDIV10 CMP #10/2 \ Calculate OSNUM/10 BCC PRDEC10 SBC #10/2+&80 \ Remove digit & set V=1 to show div result > 0 SEC \ Shift 1 into div result .PRDEC10 ROL OSNUM+0 \ Shift /10 result into OSNUM ROL OSNUM+1 \ The number of locations must match the bits ROL OSNUM+2 \ set at PRDECDIGIT ROL OSNUM+3 ROL A \ Shift bits of input into acc (input mod 10) DEX BNE PRDECDIV10 \ Continue 32-bit divide ORA #ASC"0" \ Convert to ASCII character PHA \ Push low digit 0-9 to print INY \ Increase number of digits BVS PRDECDIGIT \ If V=1, result of /10 was >0, do next digit .PRDECLP2 PLA \ Pop character left to right JSR OSWRCH \ Print it DEY BNE PRDECLP2 RTS \ Print arbitary decimal number \ ============================= \ The code can be optimised further to preserve Y. \ \ On entry OSNUM+0..OSNUM+3 = locations holding number to print \ On exit OSNUM+0..OSNUM+3 are set to zero \ A,X corrupted \ Y preserved \ Size 40 bytes, 36 bytes for 16-bit decimal \ \ Example code \ JSR PRINTDEC \ Print number at !OSNUM \ .PRINTDEC LDA #255 \ bit 7 set = terminator PHA .PRDECDIGIT LDX #31 \ 31+1 bits to divide LDA #0 \ Remainder=0 CLV \ V=0 means divide result = 0 .PRDECDIV10 CMP #10/2 \ Calculate OSNUM/10 BCC PRDEC10 SBC #10/2+&80 \ Remove digit & set V=1 to show div result > 0 SEC \ Shift 1 into div result .PRDEC10 ROL OSNUM+0 \ Shift /10 result into OSNUM ROL OSNUM+1 \ Number of locations must match initial X ROL OSNUM+2 ROL OSNUM+3 ROL A \ Shift bits of input into acc (input mod 10) DEX BNE PRDECDIV10 \ Continue 32-bit divide PHA \ Push low digit 0-9 to print BVS PRDECDIGIT \ If V=1, result of /10 was >0, do next digit PLA \ Get last digit back .PRDECLP2 ORA #ASC"0" \ Convert to ASCII character JSR OSWRCH \ Print it PLA \ Get next digit BPL PRDECLP2 \ Loop until bit 7 terminator popped RTS