ASCII Conversion Routine

A very compact and obscure looking routine. But a clever use of the Carry flag underneath!

0673   7358 18          ASCII  CLC
0674   7359 69 06              ADC #6
0675   735B 69 F0              ADC #$F0
0676   735D 90 02              BCC ASC1
0677   735F 69 06              ADC #$06
0678   7361             ;
0679   7361 69 3A       ASC1   ADC #$3A
0680   7363 48                 PHA           ; TEST FOR LETTER B IN ADR DURING WBNPF
0681   7364 C9 42              CMP #'B'
0682   7366 D0 0A              BNE ASCX
0683   7368 A5 FD              LDA SAVX
0684   736A C9 07              CMP #NCMDS
0685   736C D0 04              BNE ASCX      ; NOT WB CMD
0686   736E 68                 PLA
0687   736F A9 20              LDA #' '      ; FOR WB, BLANK 0'S IN ADR
0688   7371 48                 PHA
0689   7372 68          ASCX   PLA
0690   7373 60                 RTS

ASCII
Convert the low nibble in A to ASCII hex. Call it with the upper nibble containing 0.
A returned has the ASCII equivalent of the byte value.

What is happening here?

  • clear the Carry flag (673)
  • A is shifted into a range that later allows the Carry flag to distinguish between numeric digits (0–9) and alphabetic digits (A–F) (674)
  • Adding $F0 causes only values A–F to generate a carry.(675)
    Input Accumulator Carry
    0–9 $F6–$FF 0
    A–F $00–$05 1
  • Carry is clear, the value was 0–9. Otherwise an extra AD $06 is executed before the final addition.(677)
  • Add $3A to make it ASCII ((679)
  • if the character is ‘B” and the current command is WH BNPF repalce with ‘ ‘
    to suppress illegal characters in the BNPF format

Note that suppressing ‘F’ is aslo aprt of the BNPF format definition. IT is not done on the TIM for unknown reasons.

Examples

Input = 5

5 + 6      = 11
11 + $F0   = $FB   Carry=0
$FB + $3A  = $35   ASCII '5'

Input = A (10)

10 + 6     = 16
16 + $F0   = $00   Carry=1
00 + 6 + C = $07
07 + $3A   = $41   ASCII 'A'