[Return to Main Page]

BCD to Binary Conversion (32-bit) from 6502 Software Design, Expanded by Greg
[Up to Source Code Repository]


BCD to Binary Conversion (32-bit)

These are routines that I expanded from the 6502 Software Design book by Leo J. Scanlon. Anyone may contact me for questions or comments at greg@ntown.com.

;This routine converts a packed 8 digit BCD value in memory loactions
;BINARY to BINARY+3 to a binary value with the dp value in location
;EXP and stores it in locations BCD to BCD+3. It Then packs the dp value
;in the MSBY high nibble location BCD+3.
BCD_BIN:   stz     BINARY+3 ;Reset MSBY
           jsr     NXT_BCD  ;Get next BCD value
           sta     BINARY   ;Store in LSBY
           ldx     #$07
GET_NXT:   jsr     NXT_BCD  ;Get next BCD value
           jsr     MPY10
           dex
           bne     GET_NXT
           asl     EXP      ;Move dp nibble left
           asl     EXP
           asl     EXP
           asl     EXP
           lda     BINARY+3 ;Get MSBY and filter it
           and     #$0f
           ora     EXP      ;Pack dp
           sta     BINARY+3
           rts
NXT_BCD:   ldy     #$04
           lda     #$00
MV_BITS:   asl     BCD
           rol     BCD+1
           rol     BCD+2
           rol     BCD+3
           rol     a
           dey
           bne     MV_BITS
           rts

;Conversion subroutine for BCD_BIN
MPY10:     sta     TEMP2    ;Save digit just entered
           lda     BINARY+3 ;Save partial result on
           pha              ;stack
           lda     BINARY+2
           pha
           lda     BINARY+1
           pha
           lda     BINARY
           pha
           asl     BINARY   ;Multiply partial
           rol     BINARY+1 ;result by 2
           rol     BINARY+2
           rol     BINARY+3
           asl     BINARY   ;Multiply by 2 again
           rol     BINARY+1
           rol     BINARY+2
           rol     BINARY+3
           pla              ;Add original result
           adc     BINARY
           sta     BINARY
           pla
           adc     BINARY+1
           sta     BINARY+1
           pla
           adc     BINARY+2
           sta     BINARY+2
           pla
           adc     BINARY+3
           sta     BINARY+3
           asl     BINARY   ;Multiply result by 2
           rol     BINARY+1
           rol     BINARY+2
           rol     BINARY+3
           lda     TEMP2    ;Add digit just entered
           adc     BINARY
           sta     BINARY
           lda     #$00
           adc     BINARY+1
           sta     BINARY+1
           lda     #$00
           adc     BINARY+2
           sta     BINARY+2
           lda     #$00
           adc     BINARY+3
           sta     BINARY+3
           rts



;This program takes a 4 byte binary value from locations BINARY to
;BINARY+3, converts it to packed BCD and stores it in locations BCD
;to BCD+3 and puts the dp value in location EXP.
BIN_BCD:   lda     BINARY+3 ;Get MSBY
           and     #$f0     ;Filter out low nibble
           lsr     a        ;Move hi nibble right (dp)
           lsr     a
           lsr     a
           lsr     a
           sta     EXP      ;store dp
           lda     BINARY+3
           and     #$0f     ;Filter out high nibble
           sta     BINARY+3
BCD_DP:    ldy     #$00     ;Clear table pointer
NXTDIG:    ldx     #$00     ;Clear digit count
SUB_MEM:   lda     BINARY   ;Get LSBY of binary value
           sec
           sbc     SUBTBL,y ;Subtract LSBY + y of table value
           sta     BINARY   ;Return result
           lda     BINARY+1 ;Get next byte of binary value
           iny
           sbc     SUBTBL,y ;Subtract next byte of table value
           sta     BINARY+1
           lda     BINARY+2 ;Get next byte
           iny
           sbc     SUBTBL,y ;Subtract next byte of table
           sta     BINARY+2
           lda     BINARY+3 ;Get MSBY of binary value
           iny
           sbc     SUBTBL,y ;Subtract MSBY of table
           bcc     ADBACK   ;If result is neg go add back
           sta     BINARY+3 ;Store MSBY then point back to LSBY of table
           dey
           dey
           dey
           inx
           bra     SUB_MEM  ;Go subtract again
ADBACK:    dey              ;Point back to LSBY of table
           dey
           dey
           lda     BINARY   ;Get LSBY of binary value and add LSBY
           adc     SUBTBL,y ;of table value
           sta     BINARY
           lda     BINARY+1 ;Get next byte
           iny
           adc     SUBTBL,y ;Add next byte of table
           sta     BINARY+1
           lda     BINARY+2 ;Next byte
           iny
           adc     SUBTBL,y ;Add next byte of table
           sta     BINARY+2
           txa              ;Put dec count in acc
           jsr     BCDREG   ;Put in BCD reg
           iny
           iny
           cpy     #$20     ;End of table?
           bcc     NXTDIG   ;No? go back with next dec weight
           lda     BINARY   ;Yes? put remainder in acc and put in BCD reg
BCDREG:    asl     a
           asl     a
           asl     a
           asl     a
           ldx     #$04
SHFT_L     asl     a
           rol     BCD
           rol     BCD+1
           rol     BCD+2
           rol     BCD+3
           dex
           bne     SHFT_L
           rts

SUBTBL:    DB      $00,$e1,$f5,$05
           DB      $80,$96,$98,$00
           DB      $40,$42,$0f,$00
           DB      $a0,$86,$01,$00
           DB      $10,$27,$00,$00
           DB      $e8,$03,$00,$00
           DB      $64,$00,$00,$00
           DB      $0a,$00,$00,$00
Last page update: August 12, 1999.