Explanation why adding 3 in Binary to BCD conversion

Thread Starter

roxi60

Joined Sep 2, 2018
73
Hello.

Can anyone explain to me why in this simple routine in BCD adjustment we need to add 3 and check for >7?
I don't reallly understand why I have to add 3...

Thank you

Code:
;***************************************************************************
    ; Bin2BCD16 - Converts a 16-bit binary number in TEMPH:TEMPL into a 3 byte packed
 ; BCD number in R0:R1:R2.
 ; R0 holds the 10 thousands digit
 ; R1 holds the hundreds and thousands digits
 ; R2 holds the ones and tens digits
 ;***************************************************************************
 ; Inputs: TEMPH, TEMPL (16-bit binary number)
 ; Outputs: R0, R1, R2 (5 digit BCD number)
 ; Used: COUNT

Bin2BCD16
 bcf STATUS,C ; clear carry bit
 movlw .16
 movwf COUNT ; init bit counter
 clrf R0 ; clear output
 clrf R1 ; clear output
 clrf R2 ; clear output
Loop16a2
 rlcf TEMPL,F ; mult by 2, shift MSb to TEMPH
 rlcf TEMPH,F ; mult by 2, shift MSb to R2
 rlcf R2,F ; mult by 2, shift MSb to R1
 rlcf R1,F ; mult by 2, shift MSb to R0
 rlcf R0,F ; mult by 2, shift MSb to Carry
 decfsz COUNT,F ; decrement bit counter
 goto AdjDec2
 return
AdjDec2
 LFSR FSR0,R2
 call AdjBCD2
    LFSR FSR0,R1
 call AdjBCD2
    LFSR FSR0,R0
 call AdjBCD2
 goto Loop16a2
AdjBCD2
 movlw 3
 addwf INDF0,W ; W = 3 + Rn
 movwf TEMP ; TEMP = 3+Rn
 btfsc TEMP,3 ;
 movwf INDF0 ; Rn=TEMP
 movlw 30 ; decimal adjust?
 addwf INDF0,W ; W=30+Rn
 movwf TEMP ; TEMP=30+Rn
 btfsc TEMP,7
 movwf INDF0 ; Rn=TEMP
 return
 

Thread Starter

roxi60

Joined Sep 2, 2018
73
Thank you jpanhalt.
I saw that, but it seemed different to me.
What I posted is from a Microchip application note, and it adds always 3 and checked if greater than 7 (that's what I do'nt understand).
Thank you again.
 

WBahn

Joined Mar 31, 2012
26,398
Thank you jpanhalt.
I saw that, but it seemed different to me.
What I posted is from a Microchip application note, and it adds always 3 and checked if greater than 7 (that's what I do'nt understand).
Thank you again.
The reason for the three is that you are about to left shift the value, which double it making it effectively adding six.

So why six?

That is what you need to add to go from 9 to 0 since you have to skip over A,B,C,D,E, and F.
 
Top