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

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

im not sure but isnt the remainder in (e)dx or ah after div?

japheth

japheth

if the operand is a dword, the quotient will be in eax and the remainder in edx.

If INT2 is a constant, there are a couple of options!

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

```
```

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

Cheers Guys...just checked up on the div instruction and indeed the remainder is in edx

the Martial

the Martial

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. :)