Email: Password: Remember Me | Create Account (Free)

Back to Subject List

Old thread has been locked -- no new posts accepted in this thread
???
05/02/11 18:22
Read: times


 
Msg Score: +1
 +1 Good Answer/Helpful
#182157 - Either work with full byte "digits" or do shift/subtract
Responding to: ???'s previous message
How do you divide two numbers with pen and paper?

Remember that you can do the same with a computer. If the computer have a division operator but can't handle the full size of the numbers, then you can treat the number as many "digits" of the size the processor can handle.

So you may see a 16-bit number as two 8-bit "digits", and perform the same job as you would do with pen and paper when performing a division with two 2-digt numbers.

Too many different national and historical ways exists how to perform manual division so I can't really give an example and hope you would recognize it.

The other alternative is to use shift and subtract.

1000 is not larger than 60000. So you can shift left (multiply with two). 2000 is not larger than 60000, so you can shift left (multiply with two).

After having shifted 5 times you get 32000 which is not larger. But 6 times is 64000 which is larger.
So you know that 2^5 (2*2*2*2*2) = 32 times 1000 is ok.

Subtract 32000 from 60000 while remembering your 32.

Now you have 28000 and your original number 1000. Repeat again.
16000 fits but 32000 doesn't fit. and 16000 is 16*1000. So for your result, add 16 to your previous 32 - now 48. And remove 16000 from your 28000 - now 12000 remains.

Repeat again. This time 8000 is <= 12000 so 12000-8000 = 4000. And 48 + 8 = 56.

Repeat again. This time 4000 is <= 4000 so 4000-4000 = 0. And 56 + 4 = 60.

There you go - the integer division of 60000 with 1000 resulted in the (potentially truncated) answer 60.

The shift and subtract method is great for processors that doesn't have any division instruction since it guarantees quick progress with simple code and subtract, compare and shift are very cheap processor instructions - even when cascaded for numbers spanning multiple word sizes.

I leave it to you to try the old school-book style manual division but performed with 8-bit digits spanning 0..255 instead of our "normal" decimal digits spanning just 0..9.

List of 8 messages in thread
TopicAuthorDate
16 bit divison in 89s52 with assembly language            01/01/70 00:00      
   Either work with full byte "digits" or do shift/subtract            01/01/70 00:00      
      I am way more lazy than Per and give only a link :-)            01/01/70 00:00      
      excellent ...            01/01/70 00:00      
      Appreciate ur description            01/01/70 00:00      
   Right here!            01/01/70 00:00      
   24-bit / 16-bit            01/01/70 00:00      
   It's that time of year again, isn't it?            01/01/70 00:00      

Back to Subject List