BCD to Binary ============= Convert BCD to BIN, corrupting registers, but using no memory. \ On entry A=BCD number &00-&99 \ On exit X=binary number 0-99 \ A corrupted \ Y=preserved \ Size 11 bytes .BCDtoBIN LDX #&FF :\ Start with result=-1 SEC :\ Prepare for subtraction SED :\ Switch to Decimal arithmetic .BCDtoBINlp INX :\ Add 1 to result SBC #1 :\ Subtract 1 with BCD arithmetic BCS BCDtoBINlp :\ Loop until BCD value < 0 CLD :\ Switch back to Binary arithmetic RTS :\ Returns with X=binary number Convert BCD to BIN without looping, preserving registers, but using temporary memory location. \ On entry A=BCD number &00-&99 \ On exit A=binary number 0-99 \ X,Y preserved \ tmp corrupted \ Size 18 bytes .BCDtoBIN PHA :\ Save starting value AND #&F0 :\ Keep top nybble LSR A:STA tmp :\ tmp=num/2 = (num/16)*8 LSR A:LSR A :\ A=num/8 = (num/16)*2 ADC tmp:STA tmp :\ tmp = num/2+num/8 = (num/16)*10 PLA:AND #&0F :\ Get bottom nybble ADC tmp :\ Add bottom nybble RTS :\ Returns with A=binary number BCDtoBIN can be used to scan a decimal number using hex scanning code: JSR ScanHex :\ Scan 8-bit hex to A JMP BCDtoBIN :\ Treat as BCD and convert to binary Binary to BCD ============= Convert BIN to BCD, corrupting registers, but using no memory. \ On entry A=binary number 0-99 \ On exit A=BCD number &00-&99 \ X=corrupted \ Y preserved \ Size 12 bytes .BINtoBCD TAX:LDA #&99 :\ Move value to X, start at -1 in BCD SED :\ Switch to decimal arithmetic .BINtoBCDlp CLC:ADC #1 :\ Add one in BCD mode DEX:BPL BINtoBCDlp :\ Loop for all of source number CLD :\ Switch back to binary arithmetic RTS Convert BIN to BCD, preserving registers, but using temporary memory location. \ On entry A=binary number 0-99 \ On exit A=BCD number &00-&99 \ tmp corrupted \ X,Y preserved \ Size 23 bytes .BINtoBCD PHA:LDA #10:STA tmp:PLA .BINtoBCDlp CMP tmp:BCC BINtoBCDdone ADC #5:PHA:LDA tmp ADC #16:STA tmp:PLA BCC BINtoBCDlp .BINtoBCDdone RTS BINtoBCD can be used to print decimal numbers using a hex printout routine: JSR BINtoBCD :\ Convert A to BCD JMP PrHex :\ Print as 8-bit hex