Just a little bragadosio...

Coding and general discussion relating to the compiler

Moderators: David Barker, Jerry Messina

Post Reply
xor
Posts: 286
Joined: Sun Nov 05, 2006 1:15 pm
Location: NYC
Contact:

Just a little bragadosio...

Post by xor » Fri Dec 15, 2006 3:19 am

I thought I would share a personal milestone after programming PIC's for the past 1-1/2 years...matching Scott Dattalo for a piece of optimized code that he wrote 6 years ago on PICList:
http://www.piclist.com/techref/microchi ... b-2d8b.htm

User avatar
David Barker
Swordfish Developer
Posts: 1214
Joined: Tue Oct 03, 2006 7:01 pm
Location: Saltburn by the Sea, UK
Contact:

Post by David Barker » Fri Dec 15, 2006 2:44 pm

For those who would like to use this routine in a function...

Code: Select all

function access BCDToDec(pBCD as byte) as WREG
   asm
      rrcf       pBCD, W      ; BCD/2 in WREG
      rrcf       WREG, W      ; BCD/2/2 in WREG
      andlw      0x3C         ; mask upper nibble as 4 * tens
      subwf      pBCD, F      ; BCD = (16 * tens) - (4 * tens) = 12 * tens + ones
      rrncf      WREG, W      ; BCD/2/2/2 in WREG = (2 * tens)
      subwf      pBCD, W      ; BCD = (12 * tens) - (2 * tens) = 10 * tens + ones
   end asm
end function
For those of you interested in such things...

(a) the 'access' keyword is used to guarantee pBCD is located in access RAM - so no bank switching is ever required
(b) the function returns in WREG, thus removing the need to assign and allocate 'result'

xor
Posts: 286
Joined: Sun Nov 05, 2006 1:15 pm
Location: NYC
Contact:

Post by xor » Fri Dec 15, 2006 3:18 pm

Thanks Dave....I was thinking about writing a function but it was getting late. I have especially learned something new from your demonstration of code. You have created a very powerful compiler.
:D

I was trying to think of where such a routine might be useful and I believe that the DS1307 uses a packed BCD format.

Post Reply