 
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:
; ==========================================================
; From: Scott Dattalo
;
; This version saves one cycle.
;
        swapf   bcd, W
        andlw   0x0F            ; W=tens
        movwf   temp
        addwf   temp, W         ; W=2*tens
        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
Comments:
RRCF BCD, W ; BCD/2 in WREG+
RRCF WREG, W ; BCD/2/2 in WREG
ANDLW 0x3C ; mask upper nibble as 4 * tens
SUBWF BCD, F ; BCD = (16 * tens) - (4 * tens) = 12 * tens + ones
RRNCF WREG, W ; BCD/2/2/2 in WREG = (2 * tens)
SUBWF BCD, F ; BCD = (12 * tens) - (2 * tens) = 10 * tens + ones
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
addwf PRODL,w ;add to multiply result
| file: /Techref/microchip/math/radix/bp2b-2d8b.htm, 4KB, , updated: 2017/1/6 02:47, local time: 2025/10/31 08:38, 
 
216.73.216.87,10-2-37-96:LOG IN | 
| ©2025 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.piclist.com/Techref/microchip/math/radix/bp2b-2d8b.htm"> PIC Microcontoller Radix Math Method BCD packed to binary 2 digit to 8 bit</A> | 
| Did you find what you needed? | 
|  PICList 2025 contributors: o List host: MIT, Site host massmind.org, Top posters @none found - Page Editors: James Newton, David Cary, and YOU! * Roman Black of Black Robotics donates from sales of Linistep stepper controller kits. * Ashley Roll of Digital Nemesis donates from sales of RCL-1 RS232 to TTL converters. * Monthly Subscribers: Gregg Rew. on-going support is MOST appreciated! * Contributors: Richard Seriani, Sr. | 
| Ashley Roll has put together a really nice little unit here. Leave off the MAX232 and keep these handy for the few times you need true RS232! | 
.