I'm new to asm and I was wondering what the quickest way to find the mod (modulus) of an integer is?
Posted on 2001-03-25 10:28:00 by kaytan
The easiest would be the div instruction - it returns the remainder in AH, DX, EDX for byte, word, and dword size operands respectively. bitRAKE
Posted on 2001-03-25 12:06:00 by bitRAKE
Come on... someone gotta have a cool hack for this one... ?? I racked my brain on it all afternoon, figuring out shr's, not's, add's etc. but in the end i couldnt get something that would converge to every case. Svin HELP!! heehehe NaN
Posted on 2001-03-25 23:17:00 by NaN
Here is a link: Integer division and modulus by constants. Hope it's helpful :) bitRAKE This message was edited by bitRAKE, on 3/26/2001 12:28:50 AM
Posted on 2001-03-25 23:28:00 by bitRAKE
Hope this helps... I created a simple VC++ app and viewed it's dissassembly... had to do it many times because it uses a different algorithm for anything that's power of 2, IE 2, 4, 8, 16... and then a second formula (which is much simpler) for anything else... in all, it appears to use two different algorithms, based on whether it's power of 2 or not... ---


--- main.cpp ---


#include 
#include 


int main(void)
{
   int a;
   int b;

   a = 17;
	
   b = (a % 4);

   cout << "Total: " << b << '\n' << '\n' << endl;

   return(0);
}

--- End ---




10:      int a;
11:
12:      a = 4;
00401058   mov         dword ptr ,4
13:      a++;
0040105F   mov         eax,dword ptr 
00401062   add         eax,1
00401065   mov         dword ptr ,eax
14:
15:      a = (a % 3);
00401068   mov         eax,dword ptr 
0040106B   cdq
0040106C   mov         ecx,3
00401071   idiv        eax,ecx
00401073   mov         dword ptr ,edx
16:
17:      cout << "Total: " << a << '\n' << '\n' << endl;




10:      int a;
11:
12:      a = 4;
00401058   mov         dword ptr ,4
13:      a++;
0040105F   mov         eax,dword ptr 
00401062   add         eax,1
00401065   mov         dword ptr ,eax
14:
15:      a = (a % 2);
00401068   mov         eax,dword ptr 
0040106B   and         eax,80000001h
00401070   jns         main+37h (00401077)
00401072   dec         eax
00401073   or          eax,0FFFFFFFEh
00401076   inc         eax
00401077   mov         dword ptr ,eax
16:
17:      cout << "Total: " << a << '\n' << '\n' << endl;




10:      int a;
11:      int b;
12:
13:      a = 17;
00401058   mov         dword ptr ,11h
14:
15:      b = (a % 5);
0040105F   mov         eax,dword ptr 
00401062   cdq
00401063   mov         ecx,5
00401068   idiv        eax,ecx
0040106A   mov         dword ptr ,edx
16:
17:      cout << "Total: " << b << '\n' << '\n' << endl;



10:      int a;
11:      int b;
12:
13:      a = 17;
00401058   mov         dword ptr ,11h
14:
15:      b = (a % 6);
0040105F   mov         eax,dword ptr 
00401062   cdq
00401063   mov         ecx,6
00401068   idiv        eax,ecx
0040106A   mov         dword ptr ,edx
16:
17:      cout << "Total: " << b << '\n' << '\n' << endl;



10:      int a;
11:      int b;
12:
13:      a = 17;
00401058   mov         dword ptr ,11h
14:
15:      b = (a % 3);
0040105F   mov         eax,dword ptr 
00401062   cdq
00401063   mov         ecx,3
00401068   idiv        eax,ecx
0040106A   mov         dword ptr ,edx
16:
17:      cout << "Total: " << b << '\n' << '\n' << endl;



10:      int a;
11:      int b;
12:
13:      a = 17;
00401058   mov         dword ptr ,11h
14:
15:      b = (a % 4);
0040105F   mov         eax,dword ptr 
00401062   and         eax,80000003h
00401067   jns         main+2Eh (0040106e)
00401069   dec         eax
0040106A   or          eax,0FFFFFFFCh
0040106D   inc         eax
0040106E   mov         dword ptr ,eax
16:
17:      cout << "Total: " << b << '\n' << '\n' << endl;
Posted on 2001-03-26 00:29:00 by _Shawn