I found this nice Assembler routine here:
http://www.piclist.com/techref/micro...b-5d16b-ph.htm
And best of all, he explains it all for people like me.He boasts it takes only 33 cycles in all to complete, which I consider fantastic compared to any of the alternatives I can possibly write in BASIC Pro (after it converts into Assembler).
He says it's free, I'm using it as an include and keeping all his credits; he deserves it. I don't like using someone else's free code, I like using my 'own stuff'', but this routine is just awesome. 5 bytes in, 2 bytes out, and no temporary variables, pretty sleek stuff.
And for those like me that don't know how to tie this into our BASIC programs, chapter 8.2 is where it's at.
; 5 digit decimal to 16 (17) bit binary. By Peter Hemsley, March 2003.
; Input decimal digits in D0 (LSD) to D4 (MSD)
; Output 16 bit binary in NUMHI and NUMLO
; No temporary variables required
; Code size: 33 instructions
; Execution time: 33 cycles (excluding Call and Return)
; Returns carry set if > 65535 (and NUMHI-LO MOD 65536)
dec2bin16
movf D1,W ; (D1 + D3) * 2
addwf D3,W
movwf NUMLO
rlf NUMLO,F
swapf D2,W ; + D2 * 16 + D2
addwf D2,W
addwf NUMLO,F
rlf D4,W ; + (D4 * 2 + D3) * 256
addwf D3,W
movwf NUMHI
rlf NUMLO,F ; * 2
rlf NUMHI,F
swapf D3,W ; - D3 * 16
subwf NUMLO,F
skpc
decf NUMHI,F
swapf D2,W ; + D2 * 16 + D1
addwf D1,W
addwf NUMLO,F
skpnc
incf NUMHI,F
swapf D4,W ; + D4 * 16 + D0
addwf D0,W
rlf NUMLO,F ; * 2
rlf NUMHI,F
addwf NUMLO,F
skpnc
incf NUMHI,F
movf D4,W ; - D4 * 256
subwf NUMHI,F
swapf D4,W ; + D4 * 16 * 256 * 2
addwf NUMHI,F
addwf NUMHI,F
return ; Q.E.D.
Bookmarks