Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jul 15th, 2012  |  syntax: None  |  size: 1.26 KB  |  hits: 13  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #include <stdio.h>
  2.  
  3. void sort_array(int n, int target[])
  4. {
  5.         // Variable init.
  6.         int i;
  7.  
  8.         // Preliminary implementation in C:
  9.         /*int temp;
  10.         int location;
  11.        
  12.         for (i = 1; i < n; i++)
  13.         {
  14.                 temp = target[i];
  15.                 location = i;
  16.                 while (location >= 1 && target[location - 1] > temp)
  17.                 {
  18.                         target[location] = target[location - 1];
  19.                         location--;
  20.                 }
  21.                 target[location] = temp;
  22.         }*/
  23.  
  24.         // Assemblin' time!
  25.         __asm{
  26.                 mov     i, 1
  27. top1:   mov     eax, i
  28.                 cmp     eax, n
  29.                 jge     done1
  30.                 mov     edx, 0
  31.                 mov ebx, 4
  32.                 mul     ebx
  33.                 mov     ebx, target[eax]
  34. top2:   cmp eax, 4
  35.                 jl      done2
  36.                 cmp     target[eax] - 4, ebx
  37.                 jle     done2
  38.                 mov     edx, target[eax] - 4
  39.                 mov target[eax], edx
  40.                 sub eax, 4
  41.                 jmp top2
  42. done2:  mov target[eax] - 4, ebx
  43.                 inc     i
  44.                 jmp     top1
  45. done1:  nop
  46.         }
  47.  
  48.         // Print out the array.
  49.         for (int i = 0; i < n; i++)
  50.                 printf("%d ", target[i]);
  51.         printf("\n");
  52. }
  53.  
  54. void main()
  55. {
  56.         // Initialize the arrays.
  57.         int array1[] = {100, 99, 97, 95, 90, 87, 86, 83, 81, 77, 74, 69, 63, 50, 44, 43, 39, 31, 29, 12};
  58.         int array2[] = {123456, 342100, 87539, 606006, 443322, 198371, 99109, 88018, 707007};
  59.         int size1 = 20;
  60.         int size2 = 9;
  61.  
  62.         // Do the sort.
  63.         sort_array(size1, array1);
  64.         sort_array(size2, array2);
  65.  
  66.         // Wait for keypress because Windows consoles disappear.
  67.         getchar();
  68. }