On Thu, 7 Jun 2001, Drew Vassallo wrote: > Scott, if you can't offer a solution to your own question, especially in > this area, then we're all in trouble :) I was guessing it would take 12 instructions based of course on Dmitry's Law (all complicated, highly optimized snippets take exactly 12 instructions). After getting it down to 12, I quit working on it. ;;---------------------------------------------------- ;; Check to see if a number is evenly divisible by 9 ;; (e.g. N%9 == 0) ;; ;; ;; An 8-bit number can be written like: ;; ;; N = abcdefgh ;; = a*128 + bcde*8 + fgh ;; = a*128 + bcde*(9-1) + fgh ;; = a*128 + bcde*9 - bcde + fgh ;; ;; Evaluate N%9: ;; ;; N%9 = (a*128 + bcde*9 - bcde + fgh) % 9 ;; ;; Let's look at the first tow terms in the expression: ;; ;; a*128 % 9 = a*2 % 9 ( because 128 = 126 +2) ;; ;; (bcde * 9) % 9 = 0 ;; ;; re-writing: ;; ;; N%9 = (a*2 - bcde +fgh) ;; ;; ;; Enter: W contains the number to test ;; Exit: Z is set if the number is evenly divisble by 9 movwf temp ;shift left by 2 addwf temp,f ;by adding. puts 'a' into carry ;and bcde into the upper nibble andlw 7 ;grab the lower three bits skpnc ;if the MSB ('a') was set then addlw 2 ;a*2 == 2, W = a*2 + fgh. swapf temp,f ;put bcde into the lower nibble subwf temp,w ;W = bcde - (a*2 +fgh) ;at this point, if N%9 is zero, then W contains -9,0,9 ;so just check these three cases skpdc ;If W is less than 0 addlw 9 ; then add 9 andlw 0x0f ;If W was/is zero then set Z skpz ;If W is greater than 0 addlw -9 ; then subtract 9 Works in gpsim. Scott -- http://www.piclist.com hint: The PICList is archived three different ways. See http://www.piclist.com/#archives for details.