Jump to content
  • entries
    17
  • comments
    28
  • views
    32,103

Hex to Decimal (0-255) Pt II


Omegamatrix

1,458 views

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. :)

  • Like 2

0 Comments


Recommended Comments

There are no comments to display.

Guest
Add a comment...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...