@ Binary to Binary coded decimal (BCD)
@ Originally posted by Kay on the gbadev.org forum
@ Re arranged by isildur and optimized by FluBBa
@ January 2005
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

.GLOBAL Bin2BCD

.section .iwram, "xw", %progbits
.arm
.align 2


@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@
@ u32 Bin2BCD(u32 hexVal);
@
@ in  r0 => binary value [26 bits max]
@ out r0 <= BCD value [8 digits 0 - 99999999]

Bin2BCD:
    stmfd   sp!, {r4, r5}

    mov   r1, #0
    ldr   r3, =BCD_conversion_table      @ BCD
    ldr   r4, =0x06666666            @ carry care overflow value
    ldr   r5, =0x11111110            @ carry count mask value

_Binary_to_BCD_conversion_loop:
    ldr   r2, [r3], #4               @ BCD (read BCD table)
    movs   r0, r0, lsr #1               @ test bit

    bcc   _Binary_to_BCD_conversion_loop_check
_Binary_to_BCD_calc:
    add   r1, r4, r1               @ t1 = $06666666 + a
    add   r12, r1, r2              @ t2 = t1 + BCD
    eor   r2, r1, r2               @ t3 = t1 XOR BCD
    eor   r2, r12, r2              @ t3 = t2 XOR t3
    bic   r2, r5, r2               @ t3 = $11111110 AND ~t3
    orr   r1, r2, r2, lsr #1       @ t1 = t3 OR t3 LSR 1
    sub   r1, r12, r1, lsr #2      @ a = t2 - (t1 LSR 2)

_Binary_to_BCD_conversion_loop_check:
    bne   _Binary_to_BCD_conversion_loop
    mov r0, r1

    ldmfd   sp!, {r4, r5}
    bx lr


    .pool

BCD_conversion_table:
    .word 0x00000001
    .word 0x00000002
    .word 0x00000004
    .word 0x00000008
    .word 0x00000016
    .word 0x00000032
    .word 0x00000064
    .word 0x00000128
    .word 0x00000256
    .word 0x00000512
    .word 0x00001024
    .word 0x00002048
    .word 0x00004096
    .word 0x00008192
    .word 0x00016384
    .word 0x00032768
    .word 0x00065536
    .word 0x00131072
    .word 0x00262144
    .word 0x00524288
    .word 0x01048576
    .word 0x02097152
    .word 0x04194304
    .word 0x08388608
    .word 0x16777216
    .word 0x33554432

    Source: geocities.com/v_d_d/gba

               ( geocities.com/v_d_d)