## BCD packed to binary 2 digit to 8 bit

From: Scott Dattalo

```         rrf   bcd, W
andlw 01111000b  ;W = tens*8
movwf temp
clrc
rrf   temp, F    ;temp = tens*4
rrf   temp, F    ;temp = tens*2
subwf bcd, W     ;W = tens*16 + ones - tens*8
;W = tens*8 + ones
addwf temp, W    ;W = tens*10 + ones
```

Code:

• Silvan Minghetti shares this code:
```; ==========================================================
; From: Scott Dattalo
;
; This version saves one cycle.
;
swapf   bcd, W
andlw   0x0F            ; W=tens
movwf   temp
addwf   temp, F         ; temp=3*tens (note carry is cleared)
rlf     temp, W         ; W=6*tens
subwf   bcd, W          ; W = 16*tens+ones - 6*tens

; ==========================================================
; From: Silvan Minghetti
;
; Does not require an additional temp register, but trashes
; the content of the bcd register.
;
; Expects bcd value in 'bcd', returns binary value in WREG.
; To return the binary value in 'bcd', change the last line
; to subwf bcd, F
;
swapf   bcd, F
rlf     bcd, W
andlw   b'00011110'     ; WREG = tens * 2
swapf   bcd, F

subwf   bcd, F          ; bcd  = (tens * 16) - (tens * 2) + ones
subwf   bcd, F          ; bcd  = (tens * 14) - (tens * 2) + ones
subwf   bcd, W          ; WREG = (tens * 12) - (tens * 2) + ones

; ==========================================================
; From: Scott Dattalo
;
; The upper method adapted for the PIC18 Family, saves one
; more cycle.
;
swapf   bcd, W
andlw   0x0F            ; W= tens
rlncf   WREG, W         ; W= 2*tens
subwf   bcd, F          ; 16*tens + ones - 2*tens
subwf   bcd, F          ; 14*tens + ones - 2*tens
subwf   bcd, W          ; 12*tens + ones - 2*tens
```
+

Tomas Kocian Says:

Another method, using 6 cycles without additional registers:
swapf bcd,w ;separate tenths
andlw 0F
mullw 0A ;multiply by 10
movf bcd,w
andlw 0F ;separate below 10
+
 file: /Techref/microchip/math/radix/bp2b-2d8b.htm, 4KB, , updated: 2017/1/6 01:47, local time: 2023/2/7 06:19, TOP NEW HELP FIND:  34.225.194.102:LOG IN

 ©2023 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?Please DO link to this page! Digg it! / MAKE! PIC Microcontoller Radix Math Method BCD packed to binary 2 digit to 8 bit

After you find an appropriate page, you are invited to your to this massmind site! (posts will be visible only to you before review) Just type a nice message (short messages are blocked as spam) in the box and press the Post button. (HTML welcomed, but not the <A tag: Instead, use the link box to link to another page. A tutorial is available Members can login to post directly, become page editors, and be credited for their posts.

Attn spammers: All posts are reviewed before being made visible to anyone other than the poster.
 Did you find what you needed? "No. I'm looking for: " "No. Take me to the search page." "No. Take me to the top so I can drill down by catagory" "No. I'm willing to pay for help, please refer me to a qualified consultant"

.