---------- >Dudes: > >As many of you know, I periodically post programming "challenges" >here... Usually, they're of the "Tell me how this code fragment >works" form. > >This time, it's a little different... >The following PIC 16C5x code tests for divisibility by 3, using an >algorithm credited to Lewis Carroll, the dope-smoking mathematician >and author of "Alice in Wonderland". Carroll's algorithm, of course, >was designed for base 10, so it tested for divisibility by 11. > >; >; Test for divisibility by 3. >; Enter with the number to be tested in "TEST". >; Exits with W = 0 if the number's not divisible by 3, >; W = 1 if the number IS divisible by 3. >; The following is what I call the "one potato, two potato" method. ; ********************************************* list p=16c54 TEST equ 9 STATUS equ 3 CBIT equ 0 ZBIT equ 2 WREG equ 0 TRUE equ 1 FALSE equ 0 start call divby3 nop nop ; start of routine divby3 movf TEST,WREG ;into w reg btfsc STATUS,ZBIT ;test for inital zero retlw FALSE ;yes, can't devide zero ; main loop aa10 is if no need to clear carry loop bcf STATUS,CBIT ;clear carry aa10 movf TEST,WREG ;see if done btfsc STATUS,ZBIT ;is w zerO retlw TRUE ;yes, so good and done rrf TEST ;look for next bit btfss STATUS,CBIT ;not the bit? goto aa10 ;yes, didn't find first bit ; got a bit, look for another good bit aa20 bcf STATUS,CBIT ;clear carry aa22 rrf TEST ;look for good bit btfsc STATUS,CBIT ;found bit? goto loop ;yes ; no good bit, look for bad bit aa30 movf TEST,WREG ;see if done btfsc STATUS,ZBIT ;is w zerO retlw FALSE ;yes, so bad and done rrf TEST ;look for bad bit btfss STATUS,CBIT ;not a bad bit? goto aa20 ;yes, so go back for good bit ; got a bad bit, now next bit is bad, then good bcf STATUS,CBIT ;clear carry goto aa30 ;look for offsetting bad bit end ; ***************************************************************** --------- Bill Cornutt billcorn@infoserv.com Located in Ione California USA. A small town in Northern California. Sitting against the foothills of the Mother Lode. ----------------------------------------------------