EASy68K  
It is currently Fri Apr 10, 2020 2:48 am

All times are UTC




Post new topic Reply to topic  [ 3 posts ] 
Author Message
 Post subject: Binary to BCD Help!
PostPosted: Wed Feb 16, 2011 1:46 am 
Offline

Joined: Tue Feb 15, 2011 4:43 am
Posts: 2
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.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 17, 2011 6:13 pm 
Offline

Joined: Mon Jul 26, 2010 11:43 pm
Posts: 198
Location: Aurora, IL
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


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 18, 2011 1:52 am 
Offline

Joined: Tue Feb 15, 2011 4:43 am
Posts: 2
Great! Thank you!


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 7 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group