Epso

Asm ex Sort

Jun 1st, 2012
39
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.52 KB | None | 0 0
  1. -=-=-=-=-ASM EXTERN SORT-=-=-=-=-
  2.  
  3.  
  4.  
  5. #include <conio.h>
  6. #include <stdio.h>
  7. #include <iostream>
  8. #include <time.h>
  9. #include <Windows.h>
  10.  
  11.  
  12. int main()
  13. {
  14.   int again = 4;
  15. start:
  16.  
  17.  
  18.     int N = 30000;
  19.     srand((time(NULL)) );
  20.  
  21.     int *a = new int[N];
  22.  
  23.     for(int i = 0; i < N; i++)
  24.     a[i] = rand()%100 + 1;
  25.  
  26.   int *aa = new int[N];
  27.  
  28.   for (int i = 0; i < N; i++)
  29.     aa[i] = a[i];
  30.  
  31.  
  32.     double time = GetTickCount();
  33.    
  34.  
  35.   for (int i = 0; i < N-1; i++)
  36.     for (int j = 1; j < N; j++)
  37.       if (aa[j-1] > aa[j])
  38.       {
  39.         int tmp = aa[j-1];
  40.         aa[j-1] = aa[j];
  41.         aa[j] = tmp;
  42.       }
  43.  
  44.     printf("C++: %f\n", GetTickCount() - time);
  45.  
  46.   time = GetTickCount();
  47.  
  48.   __asm
  49.   {
  50.   xor ecx, ecx // ecx - i
  51.   xor edx, edx // edx - j
  52.   mov esi, a; // esi - a[0];
  53.   mov ecx, [N] // i = n;
  54.   dec ecx; // i = n-1
  55. c1: // for i
  56.   mov edx, [N]; // j = n;
  57.   dec ecx; // i--
  58.   test ecx, ecx // if i == 0 goto endAsm
  59.   jz endAsm;
  60.  
  61. c2: // for j
  62.   dec edx // j--
  63.   cmp edx, 1;  // if j-1 == 0 goto c1;
  64.   jz c1;
  65.   mov eax, [esi + edx * 4] // eax = a[j]
  66.   mov ebx, [esi + edx * 4 - 4]
  67.   cmp eax, ebx// cmp a[j] - a[j-1]
  68.   jns c2; // if a[j-1] > a[j]  
  69.  
  70. c3: // if
  71.   mov dword ptr [esi + edx * 4], ebx; //a[j] = a[j-1]
  72.   mov dword ptr [esi + edx * 4 - 4], eax; // a[j-1] = a[j]
  73.   jmp c2
  74.  
  75. endAsm:
  76.   }
  77.  
  78.     printf("ASM: %f\n", GetTickCount() - time);
  79.      
  80.  
  81.     delete []a;
  82.   delete []aa;
  83.   printf("\n");
  84.   again--;
  85.   if (again != 0) goto start;
  86.  
  87.   printf("\n-=-=end=-=-");
  88.   getch();
  89.   return 0;
  90. }
Advertisement
Add Comment
Please, Sign In to add comment