Hi I have a 14 bit number I'm right shift this number 6 times (divide by 64).
But when i right shift i need to make a fraction byte which is goes to the carry (without discarding LSB).
Can somebody help me to make a fraction byte using this system.
I'm doing this to detect the final result is a half divide or fully divide one.
So looking at the fraction byte i can detect that.
Ex:26.5,31.5 etc.....
Code:
Divide bcf STATUS,C
rrf TotalH,F
rrf TotalL,F ;divide by 2
bcf STATUS,C
rrf TotalH,F
rrf TotalL,F ;divide by 4
bcf STATUS,C
rrf TotalH,F
rrf TotalL,F ;divide by 8
bcf STATUS,C
rrf TotalH,F
rrf TotalL,F ;divide by 16
bcf STATUS,C
rrf TotalH,F
rrf TotalL,F ;divide by 32
bcf STATUS,C
rrf TotalH,F
rrf TotalL,F ;divide by 64
Hi kchriste Thanks for your coding.So how can I detect whether it is a halfly divided one Ex 26.5 etc....Only one bit will set in the Fractionbyte isn't it.So what bit to check in the Fractionbyte?.
When the fraction byte equals 128 (0x80) then the result is ending in 0.5 decimal. The fraction byte is really called the "remainder" in math and in your case will contain a number between 0-255 depending on what was in TotalH and TotalL when you started.
Because you are rotating right with the rrf instruction, the carry bit (From the previous rrf TotalL,F instruction) goes into bit7 of Fractionbyte. Bit1 of Fractionbyte moves into bit0 of Fractionbyte during the rrf Fractionbyte,F instruction. If you think of TotalH:TotalL:Fractionbyte as a 24bit number being rotated right, by the three consecutive rrf instructions, you'll be on the right track.
For example
If the total value let say 1769 then the binary = b’11011101001’.
When I right shift this number 6 times divide by 64.
Then the fraction byte = decimal 164 = b’10100100’
The bit 7 of fraction byte already is set then what about the rest of the bits do I need to check them as well or do I need to check only bit 7 of fraction byte to determine is it a halfly divide one?
If the entire Fractionbyte is equal to 0x80 then the number is an exact multiple of 0.5. In other words, if bit7 of fraction byte is set and bits 0-6 are all clear, then the number in Fractionbyte equals 0.5. ie: the decimal equivalent of the fraction byte is Fractionbyte/256.
Ok guys, I have a question. If we only want to look at the most significant bit of the 6-bit fractional portion of this 14-bit number wouldn't it be easier to simply multiply by 4 (shift the word 2-bits to the left) which would leave the 8-bit integer portion of the number in the hi byte and the most significant bit of the 6-bit fractional portion of the number in bit 7 of the lo byte?
Code:
;
; multiply result by 4
;
; --IIIIII IIFFFFFF -> IIIIIIII FFFFFF--
;
; 8-bit Integer result in TotalH
; 6-bit Fractional result in TotalL (fraction = TotalL << 2)
;
clrc ;
rlf TotalL,F ;
rlf TotalH,F ;
clrc ;
rlf TotalL,F ;
rlf TotalH,F ;