Which word to bcd subroutine is faster?

Ask anything your want about Megadrive/Genesis programming.

Moderator: BigEvilCorporation

Post Reply
User avatar
Scorpion Illuminati
Interested
Posts: 28
Joined: Fri Oct 02, 2015 4:58 pm

Which word to bcd subroutine is faster?

Post by Scorpion Illuminati » Wed Oct 12, 2016 4:48 am

Which word to bcd subroutine is faster?

Code: Select all

;D0=binary number (0-9999)
;D2=binary number converted to four digit BCD

    moveq   #0,d2       ;Clear conversion register.
    moveq   #3,d7       ;Number of BCD digits-1.
loop2
    divu    #10,d0      ;D0:LOW = D0/10, D0:HIGH = D0%10
    move.l  d0,d1       ;Copy to split quotient and remainder
    and.l   #$FFFF,d0   ;D0:HIGH = 0
    clr.w   d1          ;D1:LOW = 0, D1:HIGH = 0..9
    ror.l   #4,d1       ;Align new digit to the end (div goes backward)
    ror.w   #4,d2       ;Make room to add digit to BCD number
    add.w   d1,d2       ;Add digit to binary number.
    dbra    d7,loop2
or

Code: Select all

;D0=binary number (0-9999)
;D2=binary number converted to four digit BCD

    moveq   #0,d2       ;Clear conversion register.
    moveq   #3,d7       ;Number of BCD digits-1.
loop3
    divu    #10,d0      ;D0:LOW = D0/10, D0:HIGH = D0%10
    move.l  d0,d1       ;Copy to split quotient and remainder
    and.l   #$FFFF,d0   ;D0:HIGH = 0
    clr.w   d1          ;D1:LOW = 0, D1:HIGH = 0..9
    add.l   d1,d2       ;Add digit to binary number.
    ror.l   #4,d2       ;Pull it back into 16-bit window
    dbra    d7,loop3
Any assistance in this matter would be greatly appreciated.

Sincerely,

Scorpion Illuminati
Scorpion Illuminati - An open source rhythm game for the Sega Genesis
http://www.scorpionilluminati.tk

User avatar
Sik
Very interested
Posts: 591
Joined: Thu Apr 10, 2008 3:03 pm
Contact:

Re: Which word to bcd subroutine is faster?

Post by Sik » Wed Oct 12, 2016 3:44 pm

If you can spare the ROM, one idea I had was to take a group of bits (say 6 bits), then use them as an index to a look-up table of BCD values (e.g. $00 to $63). Then take more bits and do the same with another table (e.g. $0000 to $4095 in steps of $64 for the next six bits) and so on. Then add all the BCD values.

I originally came up with this idea for 32-bit values (where division would be hell and indeed eat up a significant amount of CPU time just for converting one number) but this should work here too if you're willing to try. Then again, ask yourself whether it's really worth avoiding the division in the first place... all games I've seen converting 16-bit to BCD just use divisions. Not to mention the last digit doesn't need its own division (because it gives you both the last and next to last digits already).

EDIT: what comes to mind at the moment, albeit didn't test it. d7 takes a number on input and has BCD on output. d6 and d5 get clobbered.

Code: Select all

    moveq   #0, d6
    moveq   #0, d5
    and.l   #$FFFF, d7
    divu.w  #10, d7
    move.w  d7, d6
    divu.w  #10, d6
    move.w  d6, d5
    divu.w  #10, d5
    swap    d7
    swap    d6
    lsl.w   #4, d6
    or.w    d6, d7
    lsl.l   #8, d5
    lsl.w   #4, d5
    or.w    d5, d7
    swap    d5
    or.w    d5, d7
Sik is pronounced as "seek", not as "sick".

Miquel
Very interested
Posts: 178
Joined: Sat Jul 30, 2016 12:33 am

Re: Which word to bcd subroutine is faster?

Post by Miquel » Thu Oct 13, 2016 4:33 pm

They are almost the same, aren't they? Just 1st routine has one more instruction (then slower), or am I missing something?

Anyway, the fastest way to do something is not doing it. In this case you can have duplicate values: one in binary, the other in a kind of bcd form, and make the same operations on both.
Confused? Just ask!

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest