Hey guys...
Is there an opcode which calculates the remainder a.k.a MOD of a division or does anyone know of a trick to perform this...i can code this using standard div/mul/sub but is there a clever! way of doing it?

eg.
two integers INT1 INT2
INT1 mod INT2=INT1 - ((INT1/INT2)*INT2);/= interger division

so if INT1=5 and INT2 =3 then:

mov edx,0 ;setup for division
mov eax,INT1 ; eax =5
mov ecx,INT2 ;ecx =3
div ecx ;eax=1
mul ecx ;eax=3
mov ecx,INT1 ;ecx=5
sub ecx,eax ;ecx=2, remainder

thanks all

The Martial
Posted on 2001-10-16 11:27:16 by MArtial_Code
im not sure but isnt the remainder in (e)dx or ah after div?

japheth
Posted on 2001-10-16 11:33:39 by japheth
if the operand is a dword, the quotient will be in eax and the remainder in edx.
Posted on 2001-10-16 11:43:20 by Tola
If INT2 is a constant, there are a couple of options!



IF INT2 is a power of 2, simply:
mask = NOT(INT2 - 1)
and INT1, mask

IF INT1 is a word in ax, and the divisor is a byte:

db 0D4h, INT2
;An undocumented, un mneumoniced instruction
;based on AAM. It'll take quite a few clocks, but
;marginally better than a divide


Other than that, divide is a nasty thing to code (it sucks in logic too, and it takes too many gates to speed it up :( ), its difficult to do a better "general" solution, if you have a specific subset of numbers in mind, then there can be cheats & hacks.

div is the best general rule way of doing things though.

Mirno
Posted on 2001-10-16 12:02:42 by Mirno
Cheers Guys...just checked up on the div instruction and indeed the remainder is in edx

the Martial
Posted on 2001-10-16 17:41:57 by MArtial_Code
Yea, DIV is a hog but it's nice to have. Ever work on a CPU that didn't have hardware MUL/DIV? The 6502 didn't. You had to code compare/subtract/shift loops to divide. :)
Posted on 2001-10-16 20:57:34 by S/390