8-bit number output in 6502 machine code ======================================== Print 8-bit hexadecimal ======================= Print value in A in hexadecimal padded with zeros to two characters. \ On entry A=value to print \ On exit A corrupted \ Size 22 bytes .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 to two characters. (With reference to http://www.obelisk.me.uk/6502/algorithms.html) \ On entry A=value to print \ On exit A corrupted \ Size 21 bytes .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 :\ Switch to decimal arithmetic CLC ADC #&90 :\ Produce &90-&99+CC or &00-&05+CS ADC #&40 :\ Produce &30-&39 or &41-&46 CLD :\ Switch back to binary arithmetic JMP OSWRCH :\ Print it Print 8-bit decimal 0-255 ========================= Print value in A in decimal padded with zeros to three characters. \ On entry A=value to print 0-255 \ On exit A,X corrupted \ Size 35 bytes .PrDec LDX #ASC"0"-1:SEC :\ Prepare for subtraction .PrDec100 INX:SBC #100:BCS PrDec100 :\ Count how many 100s ADC #100:JSR PrDecDigit :\ Print the 100s LDX #ASC"0"-1:SEC :\ Prepare for subtraction .PrDec10 INX:SBC #10:BCS PrDec10 :\ Count how many 10s ADC #10:JSR PrDecDigit :\ Print the 10s ORA #ASC"0":TAX :\ Pass 1s into X .PrDecDigit PHA:TXA:JSR OSWRCH :\ Print digit PLA:RTS :\ Restore A and return Print 8-bit decimal 0-99 ======================== Print value in A in decimal padded with zeros to two characters. Works by converting into a BCD number and printing in hex. \ On entry A=value to print 0-99 \ On exit A,X corrupted \ Size 11 bytes + size of PrHex .PrDec TAX:LDA #&99 :\ Move value to X, start at -1 in BCD SED :\ Switch to decimal arithmetic .PrDecLp CLC:ADC #1 :\ Add one in BCD mode DEX:BPL PrDecLp :\ Loop for all of source number CLD :\ Switch back to binary arithmetic : \ Fall through into PrHex .PrHex