Advertisement
prz-emo

Untitled

Nov 30th, 2015
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.42 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3.  
  4. void insert_sort(int *array, int size) {
  5. asm(".intel_syntax noprefix\n"
  6.         //Rejestry, które powinny być przywrócone po zakończeniu działania programu
  7.     "push RBP\n"
  8.     "mov  RBP, RSP\n"
  9.     "push RBX\n"
  10.     "push RSI\n"
  11.     "push RDI\n"
  12.    
  13.     //EBP + 8    tablica
  14.     //EBP + 12   liczba elementów
  15.  
  16.     //ustawienie ECX by wskazywało liczbę elementów
  17.     //mnożymy ECX (liczbę elementów) x 4 by ustawić na koniec tablicy
  18.     "mov RAX, [RBP+12]\n"
  19.     "mov RCX, 4\n"
  20.     "mul RCX\n"
  21.     "mov RCX, RAX\n"
  22.    
  23.     //We will move 'i' and 'j' in increments and decrements of 4,
  24.     //which is the size of the elements
  25.     "mov RAX, 4\n"          //EAX będzie naszym 'i'
  26.     "xor RBX, RBX\n"        //EBX będzie naszym 'j' (ustawiamy na 0)
  27.     "mov RSI, [RBP+8]\n"    //ESI tablica
  28.  
  29.     "for:\n"
  30.             //Jeśli i >= liczbie elementów -> koniec pętli
  31.         "cmp RAX, RCX\n"
  32.         "jge end_for\n"
  33.        
  34.         //Save our number of items value, we ll restore it later
  35.         "push RCX\n"
  36.        
  37.         //ECX = array[i]
  38.         "mov RCX, [RSI+RAX]\n"
  39.        
  40.         //j = i-1
  41.         "mov RBX, RAX\n"
  42.         "sub RBX, 4\n"
  43.        
  44.         "while:\n"
  45.                 //WARUNKI WHILE
  46.             //j < 0 -> przewij pętlę
  47.             "cmp RBX, 0\n"
  48.             "jl end_while\n"
  49.            
  50.             //array[j] <= k -> przerwij pętlę
  51.             "cmp [RSI+RBX], RCX\n"
  52.             "jle end_while\n"
  53.            
  54.             //array[j+1] = array[j]
  55.             "push [RSI+RBX]\n"
  56.  
  57.             "pop qword ptr [RSI+RBX+4]\n"
  58.            
  59.             //j--
  60.             "sub RBX, 4\n"
  61.            
  62.             //Kolejna iteracja pętli while
  63.             "jmp while\n"
  64.            
  65.         "end_while:\n"
  66.        
  67.         //array[j+1] = key
  68.         "mov [RSI+RBX+4], RCX\n"
  69.        
  70.         //i++
  71.         "add RAX, 4\n"
  72.        
  73.         //przywrócenie ilości elementów
  74.         "pop RCX\n"
  75.        
  76.         //kolejna iteracja pętli for
  77.         "jmp for\n"
  78.        
  79.     "end_for:\n"
  80.    
  81.     //Przywrócenie rejestrów
  82.     "pop RDI\n"
  83.     "pop RSI\n"
  84.     "pop RBX\n"
  85.     "pop RBP\n"
  86.  
  87.         ".att_syntax \n");  
  88. }
  89.  
  90. int main(void) {
  91.    
  92.     int array[] = {6,5,3,1,8,7,2,4};
  93.     int size = 8;
  94.  
  95.     //Sortowanie przez scalanie
  96.     insert_sort(array, size);
  97.  
  98.     int i;
  99.     for(i=0;i<8;++i) {
  100.         printf("%d\n", array[i]);
  101.     }
  102.     printf("\n\n");
  103.  
  104.     return 0;
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement