Jump to content





Hex to Decimal (0-255) Pt II

Posted by Omegamatrix, in 6502 Math Routines 08 June 2014 · 1,090 views

hex to decimal
I had another go at an one byte 'hex to decimal' conversion routine. This time I kept it simple. It's a much better effort, and much quicker then before! :)
 
Boring stats:
;--------------------------------
;0-255 conversion stats
;--------------------------------
;cycles   occurances
    ;47 - 20
    ;48 - 0
    ;49 - 10
    ;50 - 36
    ;51 - 0
    ;52 - 40
    ;53 - 10
    ;54 - 10
    ;55 - 40
    ;56 - 0
    ;57 - 40
    ;58 - 10
    ;59 - 10
    ;60 - 20
    ;61 - 0
    ;62 - 10

  ;average execution is 54.10 cycles
It looks like a nice even spread, not really weighted to one side or the other.
 
 
Here is the code:
;---------------------------------------
;  0-255 Hex to Decimal conversion
;  47-62 cycles, 59 bytes
;---------------------------------------

    ldx    #1                    ;2  @2
    lda    hexValue              ;3  @5     A = 0-255
    cmp    #200                  ;2  @7
    bcs    .sub200               ;2³ @9/10
    cmp    #100                  ;2  @11
    bcs    .sub100               ;2³ @13/14
    dex                          ;2  @15
    bpl    .storeHundreds        ;3  @18    always branch

.sub200:
    inx                          ;2  @12
    sbc    #100                  ;2  @14
.sub100:
    sbc    #100                  ;2  @16
.storeHundreds:
    stx    decHundreds           ;3  @21

    ldx    #0                    ;2  @23
    cmp    #50                   ;2  @25    A = 0-99
    bcc    .try20                ;2³ @27/28
    sbc    #50                   ;2  @29
    ldx    #5                    ;2  @31
.try20:
    cmp    #20                   ;2  @33
    bcc    .try20b               ;2³ @35/36
    inx                          ;2  @37
    inx                          ;2  @39
    sbc    #20                   ;2  @41
.try20b:
    cmp    #20                   ;2  @43
    bcc    .try10                ;2³ @45/46
    sbc    #20                   ;2  @47
    inx                          ;2  @49
    inx                          ;2  @51
.try10:
    cmp    #10                   ;2  @53
    bcc    .storeResult          ;2³ @55/56
    sbc    #10                   ;2  @57
    inx                          ;2  @59
.storeResult:
    stx    decTens               ;3  @62
    sta    decOnes               ;3  @65
There are lots of branches in there, and I found the worst time to be 62 cycles (not 65). I also like the approach of this routine better. It reduces the problem at the start, and doesn't go through the hassle of splitting the digits later. :)




November 2018

S M T W T F S
    123
45678910
11121314151617
18 192021222324
252627282930 

Recent Comments

Latest Visitors

0 user(s) viewing

0 members, 0 guests, 0 anonymous users