Scott Dattalo wrote: ... >> ab-cde+fgh >> a-2*bcd+efgh >> -abcd+efgh > Actually, the last one should be > -2*abcd + efgh Yes, thanks for noticing. > This is the one I was thinking that would be most optimal. That's why I > said hint 16 = 18 - 2. > Another way to look at this is to write the number explicitly: > abcdefgh = (abcd)*16^1 + (efgh)*16^0 > If you want to know if this is divisible by 9, then: > abcdefgh % 9 = (abcd)*16 % 9 + efgh % 9 > = (abcd)*(-2) % 9 + efgh % 9 > because, 16 is congruent to -2 mod 9. It's also congruent to 7, so we > could write: > abcdefgh % 9 = abcd*7 % 9 + efgh % 9 > if you know 'a' is 0, then you can determine if the number is divisible in > 11 instructions using this trick: > movwf temp > swapf temp,w > subwf temp,f > skpdc > addlw 7 > subwf temp,w > skpdc > addlw 9 > andlw 0x0f > skpz > addlw -9 This calculates -2*abcd + efgh, doesn't it? I can't see a multiplication by 7. But this is a neat idea! Using the DC flag for keeping the sum in the lower 4 bits. Extending it further: > movwf temp > swapf temp,w > subwf temp,f > skpdc ;> addlw 7 - comment this out addlw -2 ;equivalent to adding 7, but gives us a ;convenient DC flag polarity, as after subtraction skpdc ;if overflowed again, subtract 16 % 9 = 7 or addlw 2 ;add 2, which is the same in that case. > subwf temp,w > skpdc > addlw 9 > andlw 0x0f > skpz > addlw -9 That's 1 instruction too much though. Nikolai -- http://www.piclist.com hint: PICList Posts must start with ONE topic: [PIC]:,[SX]:,[AVR]: ->uP ONLY! [EE]:,[OT]: ->Other [BUY]:,[AD]: ->Ads