Where can I find soft-multiply and divide algorithms?

前端 未结 7 2034
萌比男神i
萌比男神i 2021-02-19 09:00

I\'m working on a micro-controller without hardware multiply and divide. I need to cook up software algorithms for these basic operations that are a nice balance of compact siz

7条回答
  •  忘掉有多难
    2021-02-19 09:55

    Here's a simple multiplication algorithm:

    1. Start with rightmost bit of multiplier.

    2. If bit in multiplier is 1, add multiplicand

    3. Shift multiplicand by 1

    4. Move to next bit in multiplier and go back to step 2.

    And here's a division algorithm:

    1. If divisor is larger than dividend, stop.

    2. While divisor register is less than dividend register, shift left.

    3. Shift divisor register right by 1.

    4. Subtract divisor register from dividend register and change the bit to 1 in the result register at the bit that corresponds with the total number of shifts done to the divisor register.

    5. Start over at step 1 with divisor register in original state.

    Of course you'll need to put in a check for dividing by 0, but it should work.

    These algorithms, of course, are only for integers.

提交回复
热议问题