Page 20 - Incorrect Arithmatic

Problem:
The sums on page 20 just don't add up.

Solution:
Division

There is no easy way to divide numbers with a PIC. So 250/5 = ? The easiest way to achieve the same result is to turn the question around and ask “how many times do I have to multiply the 5 to get 250. If the program is working with 8 bit numbers, the procedure is as follows:
  • Multiply the lower number by 128.
  • Check if the total is higher or lower that the higher number.
  • If the total is lower or the same, add 128 number to the results register and subtract the total from the higher number.
  • If the total is higher, add nothing to the results register and do not subtract anything from the higher number
  • Repeat this procedure with 64, 32, 16, 8, 4, 2 and 1.

With the above figures of 250/5:
  • 5 times 128 is 640. This is higher than 250 so take no action.
  • 5 times 64 is 320. This is higher than 250 so take no action.
  • 5 times 32 is 160. This is lower than 250 so add 32 to result and subtract 160 from 250 to leave 90.
  • 5 times 16 is 80. This is lower than 90 so add 16 to result and subtract 80 from 90 to leave 10.
  • 5 times 8 is 40. This is higher than 10 so take no action.
  • 5 times 4 is 20. This is higher than 10 so take no action.
  • 5 times 2 is 10. This is the same as 10 so add 2 to result and subtract 10 from 10 to leave zero.
  • 5 times 1 is 5. This is higher than zero, so take no action.

The result register contains 32 plus 16 plus 2, which equals 50 and 250/5 also equals 50. It should be noted that this method will always round down when an exact division is not possible.

Further information:
I hope that this page helps you with your projects but if you have found any other problems or if you have any further questions please contact me though the email address of gares-AT-g4aym-DOT-org-DOT-uk.

Sorry about the email address but it seems to be the only way to avoid the spammers



Click here to return to the PIC Basics Main Page

Click here for the Index of Errors and Ommissions