Michael Watterson wrote: > Vasile can you or someone else explain > http://www.piclist.com/techref/piclist/jal/bin2bcd3.htm > > -- -------------------------------------------------- > > -- Bin2Bcd3(var bcd2..bcd0 , bin1..bin0) > > -- converts BIN number in bin1 .. bin0 > > -- to BCD out bcd2 .. bcd0 > > -- > > -- bcd2:msd, bcd1:isd, bcd0:lsd, bin1:msb, bin0:lsb > > -- -------------------------------------------------- > > Procedure AdjBcd (Byte in out digit) is > > Var byte temp > > var bit hc at temp : 3 > > var bit fc at temp : 7 > >=20 > > temp =3D digit + 3 > > if hc then digit =3D temp end if > > temp =3D digit + 0x30 > > if fc then digit =3D temp end if > > End procedure > >=20 > > procedure bin2bcd3 (byte out bcd2, byte out bcd1, byte out bcd0, > > Byte in bin1, Byte in bin0) is > > bcd2 =3D 0 > > bcd1 =3D 0 > > bcd0 =3D 0 > > for 16 loop -- all (16) bits of bin0..bin2 > > AdjBcd (bcd0) > > AdjBcd (bcd1) > > AdjBcd (bcd2) > > Assembler > > rlf bin0,f > > rlf bin1,f > > rlf bcd0,f > > rlf bcd1,f > > rlf bcd2,f > > End Assembler > > End loop > > End procedure >=20 > Is result only up to 999 (three digits)? No, the result is three bytes of packed BCD, or 6 decimal digits. > But I don't understand it. It's relatively straightforward. The combination of the AdjBcd()* calls and= the left shift constitutes multiplying a 3-byte (6-digit) number by two in BCD. The original sixteen bit number is converted to decimal one bit at a time starting with the MSB, by multiplying the BCD result by two and then adding the next bit from the original binary number. If you work through an example that has just one bit set in the binary numb= er, you'll see its decimal "weight" computed in the result. If multiple bits ar= e set, their weights are combined in the proper way. The algorithm is perfectly general, you can convert any number of binary by= tes to the corresponding number of decimal digits: Binary bytes: 1 2 3 4 ... Loop iterations: 8 16 24 32 ... Decimal digits: 3 5 8 10 ... BCD bytes: 2 3 4 5 ... -- Dave Tweed * AdjBcd() is equivalent to the x86 instruction DAA, except that the adjust= ment is being done before the shift, which is perfectly valid. --=20 http://www.piclist.com PIC/SX FAQ & list archive View/change your membership options at http://mailman.mit.edu/mailman/listinfo/piclist .