0
\$\begingroup\$

I am currently working on a 8051 assembly program which receives a set of BCD inputs and calculates the mean of this set. However, I couldn't figure out a decent way to divide 24bit BCD number. For instance, say that the sum of the series is 154128 and I am storing this data in 3 bytes: 15H, 41H, 28H and need to divide this by 13. Any ideas how should I proceed? Thanks already.

\$\endgroup\$
3
  • \$\begingroup\$ Why not just implement the algorithm you used when you were a kid? \$\endgroup\$
    – dim
    Commented Nov 7, 2017 at 12:22
  • \$\begingroup\$ @dim actually I ve never used that one so.. \$\endgroup\$ Commented Nov 7, 2017 at 12:25
  • \$\begingroup\$ In which grade are you? If you program 8051 assembly, I bet you have been taught how to make a division by hand when you were about 10, didn't you? I'm talking about the long division algorithm. You can totally implement that in a microcontroller. \$\endgroup\$
    – dim
    Commented Nov 7, 2017 at 12:28

1 Answer 1

1
\$\begingroup\$

There are two very simple methods. The simplest is to just successively subtract 13 from 15 41 28 and count the number of times before the result goes negative. It's horribly inefficient but it will eventually yield the correct answer for the integer part of the quotient give or take one. An O(n) algorithm so it can be very slow for large dividend and small divisor.

The second is to use the subtract and shift algorithm you were taught in elementary school. You can do this with BCD numbers. Subtract up to 9x and if the result goes negative then add it back again, so you have as many as 10 operations per digit of result-- so O(log(n)). Pocket calculators will use something like this algorithm. You should be able to design, implement and test this algorithm in a couple hours at most.

A third approach would be to convert the number to binary, do the calculations, then convert back again, which might make sense if there are many calculations to be done, but would be slower than BCD division.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.