## Which word to bcd subroutine is faster?

Moderator: BigEvilCorporation

Scorpion Illuminati
Interested
Posts: 28
Joined: Fri Oct 02, 2015 4:58 pm

### Which word to bcd subroutine is faster?

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
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
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

Sik
Very interested
Posts: 709
Joined: Thu Apr 10, 2008 3:03 pm
Contact:

### Re: Which word to bcd subroutine is faster?

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: 330
Joined: Sat Jul 30, 2016 12:33 am

### Re: Which word to bcd subroutine is faster?

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.
Perhaps the sunny thing was an excuse, but the “flashes” were real, did you see them? Afterwards, next day or so, a spaceship from the same people send something to us, a radio message I believe.

### Who is online

Users browsing this forum: No registered users and 1 guest