From: John Payson via Scott Dattalo
see
http://www.dattalo.com/technical/software/pic/bcd.txt
for notes on how this works. Plan on a headache. <GRIN>
[ed: quick guess at speed is that about 200 instructions will be executed
and 50 instructions + 7 registers used]
;Takes hex number in NumH:NumL Returns decimal in ;TenK:Thou:Hund:Tens:Ones ;written by John Payson ;input ;=A_{3}*16^{3} + A_{2}*16^{2} + A_{1}*16^{1} + A_{0}*16^{0} ;=A_{3}*4096 + A_{2}*256 + A_{1}*16 + A_{0} NumH DS 1 ;A3*16+A2 NumL DS 1 ;A1*16+A0 ;output ;=B_{4}*10^{4} + B_{3}*10^{3} + B_{2}*10^{2} + B_{1}*10^{1} + B_{0}*10^{0} ;=B_{4}*10000 + B_{3}*1000 + B_{2}*100 + B_{1}*10 + B_{0} TenK DS 1 ;B_{4} Thou DS 1 ;B_{3} Hund DS 1 ;B_{2} Tens DS 1 ;B_{1} Ones DS 1 ;B_{0} mov W, <>NumH ;w = A_{2}*16+A_{3} or W, #$F0 ;w = A_{3}16 mov Thou, W ;B_{3} = A_{3}16 add Thou, W ;B_{3} = 2*(A_{3}16) = 2A_{3}  32 mov Hund, W mov W, #$E2 add Hund, W ;B_{2} = A_{3}16  30 = A_{3}46 mov W, #$32 add W, Hund mov Ones, W ;B_{0} = A_{3}46 + 50 = A_{3}+4 mov W, NumH ;w = A_{3}*16+A_{2} and W, #$0F ;w = A_{2} add Hund, W ;B_{2} = A_{3}46 + A_{2} = A_{3}+A_{2}46 add Hund, W ;B_{2} = A_{3}+A_{2}46 + A_{2} = A_{3}+2A_{2}46 add Ones, W ;B_{0} = A_{3}+4 + A_{2} = A_{3}+A_{2}+4 mov Tens, W mov W, #$E9 add Tens, W ;B_{1} = A_{2}23 mov W, Tens add Tens, W ;B_{1} = 2*(A_{2}23) add Tens, W ;B_{1} = 3*(A_{2}23) = 3A_{2}69 (Doh! thanks NG) mov W, <>NumL ;w = A_{0}*16+A_{1} and W, #$0F ;w = A_{1} add Tens, W ;B_{1} = 3A_{2}69 + A_{1} = 3A_{2}+A_{1}69 range 69...9 add Ones, W ;B_{0} = A_{3}+A_{2}+4 + A_{1} = A_{3}+A_{2}+A_{1}+4 and Carry = 0 (thanks NG) rl Tens ;B_{1} = 2*(3A_{2}+A_{1}69) + C = 6A_{2}+2A_{1}138 and Carry is now 1 as tens register had to be negitive rl Ones ;B_{0} = 2*(A_{3}+A_{2}+A_{1}+4) + C = 2A_{3}+2A_{2}+2A_{1}+9 (+9 not +8 due to the carry from prev line, Thanks NG) not Ones ;B_{0} = ~(2A_{3}+2A_{2}+2A_{1}+9) = 2A_{3}2A_{2}2A_{1}10 (ones complement plus 1 is twos complement. Thanks SD) ;;Nikolai Golovchenko [golovchenko at MAIL.RU] says: complement [not Ones] can be regarded like: ;; not Ones ;; inc Ones ;; dec Ones ;;First two instructions make up negation. So, ;;Ones = Ones  1 ;; =  2 * (A3 + A2 + A1)  9  1 ;; =  2 * (A3 + A2 + A1)  10 rl Ones ;B_{0} = 2*(2A_{3}2A_{2}2A_{1}10) = 4A_{3}4A_{2}4A_{1}20 mov W, NumL ;w = A_{1}*16+A_{0} and W, #$0F ;w = A_{0} add Ones, W ;B_{0} = 4A_{3}4A_{2}4A_{1}20 + A_{0} = A_{0}4(A_{3}+A_{2}+A_{1})20 range 215...5 Carry=0 rl Thou ;B_{3} = 2*(2A_{3}  32) = 4A_{3}  64 mov W, #$07 ;w = 7 mov TenK, W ;B_{4} = 7 ;B_{0} = A_{0}4(A_{3}+A_{2}+A_{1})20, 5...200 ;B_{1} = 6A_{2}+2A_{1}138, 18...138 ;B_{2} = A_{3}+2A_{2}46, 1...46 ;B_{3} = 4A_{3}64, 4...64 ;B_{4} = 7, 7 ; At this point, the original number is ; equal to TenK*10000+Thou*1000+Hund*100+Tens*10+Ones ; if those entities are regarded as two's compliment ; binary. To be precise, all of them are negative ; except TenK. Now the number needs to be normal ; ized, but this can all be done with simple byte ; arithmetic. mov W, #$0A ;w = 10 Lb1: ;do add Ones, W ; B_{0} += 10 dec Tens ; B_{1} = 1 sb 3.0 ;skip no carry jmp Lb1 ; while B_{0} < 0 ;jmp carry Lb2: ;do add Tens, W ; B_{1} += 10 dec Hund ; B_{2} = 1 sb 3.0 jmp Lb2 ; while B_{1} < 0 Lb3: ;do add Hund, W ; B_{2} += 10 dec Thou ; B_{3} = 1 sb 3.0 jmp Lb3 ; while B_{2} < 0 Lb4: ;do add Thou, W ; B_{3} += 10 dec TenK ; B_{4} = 1 sb 3.0 jmp Lb4 ; while B_{3} < 0 ret
See also:
file: /Techref/scenix/lib/math/radix/b2bu16b5d_sx.htm, 6KB, , updated: 2004/6/10 13:40, local time: 2019/12/10 16:04,

©2019 These pages are served without commercial sponsorship. (No popup ads, etc...).Bandwidth abuse increases hosting cost forcing sponsorship or shutdown. This server aggressively defends against automated copying for any reason including offline viewing, duplication, etc... Please respect this requirement and DO NOT RIP THIS SITE. Questions? <A HREF="http://www.sxlist.com/techref/scenix/lib/math/radix/b2bu16b5d_sx.htm"> SX Microcontroller Radix Math Method Binary to BCD unpacked 16 bit to 5 digit</A> 
Did you find what you needed? 
Welcome to sxlist.com!sales, advertizing, & kind contributors just like you! Please don't rip/copy (here's why Copies of the site on CD are available at minimal cost. 
Welcome to www.sxlist.com! 
.