The MASM Forum Archive 2004 to 2012

Miscellaneous Forums => 16 bit DOS Programming => Topic started by: ilian007 on November 10, 2009, 10:54:28 PM

Title: Calculate Modulo ?
Post by: ilian007 on November 10, 2009, 10:54:28 PM
Hello, I need to calculate modulo and trying to find best and short way to do it for example:

9%5=4 beacause 9=1*5+4
8%4=0 because 8=2*4+0

So far I found that way:

Given 11 % 3 how would you find the remainder? Well how would you do divide?
11 - 3 = +1 cycle with 8 left over
8 is > 3 so keep going
8 - 3 = +1 cycle with 5 left over
5 > 3 so...
5 - 3 = +1 cycle with 2 left over
2 is < 3 and > 0 so the remainder is 2 after 11 % 3

Is there any better way ?

Thank you
Title: Re: Calculate Modulo ?
Post by: oex on November 10, 2009, 11:10:36 PM
mov eax, 9
mov ecx, 5
div ecx
modulous in edx
Title: Re: Calculate Modulo ?
Post by: ilian007 on November 10, 2009, 11:56:04 PM
It works thank you :) just needed to zero the DX prior dividing :)
Title: Re: Calculate Modulo ?
Post by: oex on November 10, 2009, 11:59:30 PM
xor dx, dx