Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- void insert_sort(int *array, int size) {
- asm(".intel_syntax noprefix\n"
- //Rejestry, które powinny być przywrócone po zakończeniu działania programu
- "push RBP\n"
- "mov RBP, RSP\n"
- "push RBX\n"
- "push RSI\n"
- "push RDI\n"
- //EBP + 8 tablica
- //EBP + 12 liczba elementów
- //ustawienie ECX by wskazywało liczbę elementów
- //mnożymy ECX (liczbę elementów) x 4 by ustawić na koniec tablicy
- "mov RAX, [RBP+12]\n"
- "mov RCX, 4\n"
- "mul RCX\n"
- "mov RCX, RAX\n"
- //We will move 'i' and 'j' in increments and decrements of 4,
- //which is the size of the elements
- "mov RAX, 4\n" //EAX będzie naszym 'i'
- "xor RBX, RBX\n" //EBX będzie naszym 'j' (ustawiamy na 0)
- "mov RSI, [RBP+8]\n" //ESI tablica
- "for:\n"
- //Jeśli i >= liczbie elementów -> koniec pętli
- "cmp RAX, RCX\n"
- "jge end_for\n"
- //Save our number of items value, we ll restore it later
- "push RCX\n"
- //ECX = array[i]
- "mov RCX, [RSI+RAX]\n"
- //j = i-1
- "mov RBX, RAX\n"
- "sub RBX, 4\n"
- "while:\n"
- //WARUNKI WHILE
- //j < 0 -> przewij pętlę
- "cmp RBX, 0\n"
- "jl end_while\n"
- //array[j] <= k -> przerwij pętlę
- "cmp [RSI+RBX], RCX\n"
- "jle end_while\n"
- //array[j+1] = array[j]
- "push [RSI+RBX]\n"
- "pop qword ptr [RSI+RBX+4]\n"
- //j--
- "sub RBX, 4\n"
- //Kolejna iteracja pętli while
- "jmp while\n"
- "end_while:\n"
- //array[j+1] = key
- "mov [RSI+RBX+4], RCX\n"
- //i++
- "add RAX, 4\n"
- //przywrócenie ilości elementów
- "pop RCX\n"
- //kolejna iteracja pętli for
- "jmp for\n"
- "end_for:\n"
- //Przywrócenie rejestrów
- "pop RDI\n"
- "pop RSI\n"
- "pop RBX\n"
- "pop RBP\n"
- ".att_syntax \n");
- }
- int main(void) {
- int array[] = {6,5,3,1,8,7,2,4};
- int size = 8;
- //Sortowanie przez scalanie
- insert_sort(array, size);
- int i;
- for(i=0;i<8;++i) {
- printf("%d\n", array[i]);
- }
- printf("\n\n");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement