Number output in 6502 machine code ================================== Print 8-bit Hexadecimal ======================= Print value in A in hexadecimal padded with zeros. \ On entry, A=value to print \ On exit, A corrupted .PrHex PHA :\ Save A LSR A:LSR A:LSR A:LSR A :\ Move top nybble to bottom nybble JSR PrNybble :\ Print this nybble PLA :\ Get A back and print bottom nybble .PrNybble AND #15 :\ Keep bottom four bits CMP #10:BCC PrDigit :\ If 0-9, jump to print ADC #6 :\ Convert ':' to 'A' .PrDigit ADC #ASC"0":JMP OSWRCH :\ Convert to character and print Print 8-bit Hexadecimal (more cunning) ====================================== Print value in A in hexadecimal padded with zeros. (With reference to http://www.obelisk.demon.co.uk/6502/algorithms.html) \ On entry, A=value to print \ On exit, A corrupted .PrHex PHA :\ Save A LSR A:LSR A:LSR A:LSR A :\ Move top nybble to bottom nybble JSR PrNybble PLA AND #15 :\ Mask out original bottom nybble .PrNybble SED CLC ADC #&90 :\ Produce &90-&99 or &0-&05 ADC #&40 :\ Produce &30-&39 or &41-&46 CLD JMP OSWRCH :\ Print it Print 8-bit Decimal =================== Print value in A in decimal padded with zeros. \ On entry, A=value to print \ On exit, A corrupted .PrDec LDX #&FF:SEC :\ Prepare for subtraction .PrDec100 INX:SBC #100:BCS PrDec100 :\ Count how many 100s ADC #100:JSR PrDecDigit :\ Print the 100s LDX #&FF:SEC :\ Prepare for subtraction .PrDec10 INX:SBC #10:BCS PrDec10 :\ Count how many 10s ADC #10:JSR PrDecDigit :\ Print the 10s TAX :\ Pass 1s into X .PrDecDigit PHA:TXA :\ Save A, pass digit to A ORA #ASC"0":JSR OSWRCH :\ Convert to character and print it PLA:RTS :\ Restore A and return Print 32-bit Decimal ==================== Print 32-bit value in decimal with no padding or specified character padding. \ On entry, num..num+3=number to print \ pad =character to pad with or zero for no padding \ sub..sub+3=subtraction workspace .PrDec32 LDX #&3B:LDA #&9A:LDY #&CA:JSR PrDecDigit32 :\ 1,000,000,000s LDX #&05:LDA #&F5:LDY #&E1:JSR PrDecDigit32 :\ 100,000,000s .PrDec24 LDA #&00:STA sub+3 :\ Subtractant is &00aaxxyy STA num+3 :\ Ensure 24-bit number LDA #&98:LDY #&96:LDX #&80:JSR PrDecDigit :\ 10,000,000s LDA #&0F:LDY #&42:LDX #&40:JSR PrDecDigit :\ 1,000,000s LDA #&01:LDY #&86:LDX #&A0:JSR PrDecDigit :\ 100,000s .PrDec16 LDA #&00:STA sub+3 :\ Subtractant is &00aaxxyy STA num+3:STA num+2 :\ Ensure 16-bit number LDY #&27:LDX #&10:JSR PrDecDigit16 :\ 10,000s LDY #&03:LDX #&E8:JSR PrDecDigit16 :\ 1000s .PrDec8 LDA #&00:STA sub+3 :\ Subtractant is &00aaxxyy STA num+3:STA num+2:STA num+1 :\ Ensure 8-bit number LDX #&64:JSR PrDecDigit8 :\ 100s LDX #&0A:JSR PrDecDigit8 :\ 10s LDA num+0:BPL PrDecPrint :\ 1s : .PrDecDigit32 STX sub+3:LDX #0:BEQ PrDecDigit .PrDecDigit8 LDY #0 :\ Use &0000xx .PrDecDigit16 LDA #0 :\ Use &00yyxx .PrDecDigit STX sub+0:STY sub+1:STA sub+2 :\ Set up subtractant LDX #&FF :\ Initialise digit counter .PrDecLp SEC:LDY #0:PHP :\ Prepare for 4-byte subtraction .PrDecSubLp PLP:LDA num,Y:SBC sub,Y:STA num,Y :\ Subtract byte at Y PHP:INY:CPY #4:BNE PrDecSubLp :\ Step through 4 bytes PLP:INX:BCS PrDecLp :\ Loop until subtracted too much LDY #0:PHP .PrDecAddLp PLP:LDA num,Y:ADC sub,Y:STA num,Y :\ Add final subtraction back PHP:INY:CPY #4:BNE PrDecAddLp PLP:TXA:BNE PrDecPrint :\ If not zero, print it LDA pad:BEQ PrDecZero :\ Exit if pad is null .PrDecPrint ORA #ASC"0":JSR OSWRCH :\ Print this digit LDA #ASC"0":STA pad :\ Set pad to '0' for other digits .PrDecZero RTS