User Tools

Site Tools


base:more_hexadecimal_to_decimal_conversion

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
base:more_hexadecimal_to_decimal_conversion [2015-04-17 04:33] – external edit 127.0.0.1base:more_hexadecimal_to_decimal_conversion [2019-08-16 01:44] (current) verz
Line 58: Line 58:
 BCD .DS  2 BCD .DS  2
 </code> </code>
 +
 +Here a stripped down version to compute a BCD number not larger than 99 (single byte). You send and receive the value in A. The intermediate variables are in page zero.
 +<code>
 +; Convert a 7 bit binary value to BCD
 +;
 +; This routine converts a binary value to BCD; the value cannot be larger than 99.
 +; Same working principle of the other routines.
 +; The value to convert is sent in A, and the result will be in A. X value is destroyed.
 +; The routine executes in 124 cycles (+rts). 
 +;
 +; Verz!!! 18-Mar-2017
 +
 +BinBcd_sb ldx #$7 ; The number of source bits     2c
 + asl ; 2c
 +; ldx #$6 ; you can replace the  LDX#$7/ASL with LDX#$6/ASL/ASL
 +; asl ; to compute values up to 63 (as for a clock)
 +; asl
 + sta <bin ; 3c
 + lda #$0 ; Ensure the result is clear    2c
 + sed ; Switch to decimal mode 2c
 +CnvBit_sb asl <BIN ; Shift out one bit 5c |
 + sta <BCD0 ; 3c |
 + adc <BCD0 ; 3c | 16c
 + dex ; And repeat for next bit 2c |
 + bne CnvBit_sb ; 3c |
 + cld ; Back to binary 2c
 + rts  ; All Done.
 +
 +BIN .equ  $fc
 +BCD0 .equ  $fb
 +</code>
 +
 Here is an equivalent routine for converting 16-bit numbers: Here is an equivalent routine for converting 16-bit numbers:
 <code> <code>
Line 104: Line 136:
 BIN .DW  12345 BIN .DW  12345
 BCD .DS  3 BCD .DS  3
 +</code>
 +
 +This is the same routine, just unrolled. It gains 250 cycles for that (30%).
 +Using zero page variables the gain is of 116 cycles more.
 +<code>
 +;-------------------------------
 +; Converts a 16bit number in BCD
 +;-------------------------------
 +;
 +;  call it with the value in bin
 +;-------------------------------
 +BINBCD16
 +        SED             ; Switch to decimal mode        2
 +        LDA #0          ; Ensure the result is clear    2
 +        STA bcd+0;                                      4
 +        STA bcd+1;                                      4
 +        STA bcd+2;                                      4       16
 + 
 +        LDX #6;                                               2
 +CNVBIT1          
 +        ASL bin+0       ; Shift out one bit             6
 +        ROL bin+1       ;                               6
 +;        LDA bcd+0      ; And add into result          
 +        ADC bcd+0       ;                               4
 +        STA bcd+0       ;                               4
 +;        LDA bcd+1      ; propagating any carry
 +;        ADC bcd+1
 +;        STA bcd+1
 +;        LDA bcd+2      ; ... thru whole result
 +;        ADC bcd+2
 +;        STA bcd+2
 +        DEX             ; And repeat for next bit       2
 +        BNE CNVBIT1     ;                                     25*6-1=149
 + 
 +        LDX #7;                                               2
 +CNVBIT2
 +        ASL bin+0      ; Shift out one bit              6
 +        ROL bin+1       ;                               6
 +        LDA bcd+0      ; And add into result            4
 +        ADC bcd+0       ;                               4
 +        STA bcd+0       ;                               4
 +        LDA bcd+1      ; propagating any carry          4
 +        ADC bcd+1       ;                               4
 +        STA bcd+1       ;                               4
 +;        LDA bcd+2      ; ... thru whole result
 +;        ADC bcd+2
 +;        STA bcd+2
 +        DEX             ; And repeat for next bit       2
 +        BNE CNVBIT2     ;                                     41*7-1=286
 + 
 +        LDX #3;                                               2
 +CNVBIT3
 +        ASL bin+0       ; Shift out one bit             6
 +        ROL bin+1       ;                               6
 +        LDA bcd+0      ; And add into result            4
 +        ADC bcd+0       ;                               4
 +        STA bcd+0       ;                               4
 +        LDA bcd+1      ; propagating any carry          4
 +        ADC bcd+1       ;                               4
 +        STA bcd+1       ;                               4
 +        LDA bcd+2      ; ... thru whole result          4
 +        ADC bcd+2       ;                               4
 +        STA bcd+2       ;                               4
 +        DEX             ; And repeat for next bit       2
 +        BNE CNVBIT3     ;                                     53*3-1=158
 + 
 +        CLD             ; Back to binary                2       2; tot 615
 +       
 +        rts             ; All Done.                            
 </code> </code>
base/more_hexadecimal_to_decimal_conversion.1429237982.txt.gz · Last modified: 2015-04-17 04:33 by 127.0.0.1