It is currently Fri Jun 05, 2020 6:22 pm

 All times are UTC

 Page 1 of 1 [ 3 posts ]
 Print view Previous topic | Next topic
Author Message
 Post subject: Binary to BCD Help!Posted: Wed Feb 16, 2011 1:46 am

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.
dbra    d7,.loop

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

Top

 Post subject: Posted: Thu Feb 17, 2011 6:13 pm

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
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
ror.l   #4,d2       ;Pull it back into 16-bit window
dbra    d7,loop3

Top

 Post subject: Posted: Fri Feb 18, 2011 1:52 am

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

Top

 Display posts from previous: All posts1 day7 days2 weeks1 month3 months6 months1 year Sort by AuthorPost timeSubject AscendingDescending
 Page 1 of 1 [ 3 posts ]

 All times are UTC

#### Who is online

Users browsing this forum: No registered users and 44 guests

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

Search for:
 Jump to:  Select a forum ------------------ EASy68K Forum    FAQ    Latest Features    EASy68K Questions    68000 Programming Questions    Wish List    Wishes Granted    Undocumented Features    Documented Features    General Discussion    EASy68K Projects