EASy68K
http://www.easy68k.com/EASy68Kforum/

Binary to BCD Help!
http://www.easy68k.com/EASy68Kforum/viewtopic.php?f=6&t=960
Page 1 of 1

Author:  \\Phoenix// [ Wed Feb 16, 2011 1:46 am ]
Post subject:  Binary to BCD Help!

Hi, im new to Easy68k.

I found this code that does BCD to binary. I am trying to figure out how to do the opposite, binary to BCD.
I found a couple of algorithms on google how to do this but i am having trouble implementing it on Easy68k

Here is the code:
Code:
;D0=Four digit BCD number.
;D2=BCD number converted to binary.
;
    moveq   #0,d2       ;Clear conversion register.
    moveq   #3,d7       ;Number of BCD digits-1.
.loop
    rol.w   #4,d0       ;Move top digit to bottom.
    move.w  d0,d1       ;Copy BCD number.
    and.w   #15,d1      ;Keep only bottom digit.
    mulu.w  #10,d2      ;Make room to add digit to binary number.
    add.w   d1,d2       ;Add digit to binary number.
    dbra    d7,.loop


If anyone can help me understand and show me how to do binary to BCD it is greatly appreciated.

Author:  clive [ Thu Feb 17, 2011 6:13 pm ]
Post subject: 

Divide the source by 10
Add 0x30 ('0') to the remainder (ie 0-9) if you want ASCII BCD
Shift the destination by 8, inserting the result from above (by 4 for just digits)
Repeat until the source (quotient) is consumed, or 4 times

Two possible implementations
Code:
        move    #1234,d0

;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

Code:
        move    #5678,d0

;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

Author:  \\Phoenix// [ Fri Feb 18, 2011 1:52 am ]
Post subject: 

Great! Thank you!

Page 1 of 1 All times are UTC
Powered by phpBB® Forum Software © phpBB Group
http://www.phpbb.com/