diff --git a/HW3/InsertionSort/src/README.md b/HW3/InsertionSort/src/README.md new file mode 100644 index 0000000..5770761 --- /dev/null +++ b/HW3/InsertionSort/src/README.md @@ -0,0 +1,41 @@ +# Number Sorter / Сортировка чисел + +Program for sorting integers with counting of moved elements. +Программа для сортировки целых чисел с подсчётом перемещённых элементов. + +## Build / Сборка + +```bash +gcc main.c insertionSort.s -o sorter +``` + +## Run / Запуск + +```bash +# Keyboard input / Ввод с клавиатуры +./sorter +# Then enter numbers separated by spaces, press Enter +# Затем введите числа через пробел, нажмите Enter + +# Direct input via echo / Прямой ввод через echo +echo "5 2 8 1 3" | ./sorter +``` + +## Check result / Проверка результата + +```bash +echo "5 2 8 1 3" | ./sorter +echo $? # Shows count of moved elements / Покажет количество перемещённых элементов +``` + +## File structure / Структура файлов + +- `main.c` - main C code (I/O) / основной код на C (ввод/вывод) +- `insertionSort.s` - sorting algorithm in assembly / алгоритм сортировки на ассемблере +- `README.md` - this instruction / данная инструкция + +## Requirements / Требования + +- GCC compiler / компилятор GCC +- Linux/Unix system / Linux/Unix система + diff --git a/HW3/InsertionSort/src/insertionSort.s b/HW3/InsertionSort/src/insertionSort.s new file mode 100644 index 0000000..ce64275 --- /dev/null +++ b/HW3/InsertionSort/src/insertionSort.s @@ -0,0 +1,79 @@ + .file "insertionSort.c" + .text + .globl insertionSort + .type insertionSort, @function +insertionSort: +.LFB0: + .cfi_startproc + pushq %rbp + .cfi_def_cfa_offset 16 + .cfi_offset 6, -16 + movq %rsp, %rbp + .cfi_def_cfa_register 6 + movq %rdi, -24(%rbp) + movl %esi, -28(%rbp) + movl $1, -12(%rbp) + jmp .L2 +.L6: + movl -12(%rbp), %eax + cltq + leaq 0(,%rax,4), %rdx + movq -24(%rbp), %rax + addq %rdx, %rax + movl (%rax), %eax + movl %eax, -4(%rbp) + movl -12(%rbp), %eax + subl $1, %eax + movl %eax, -8(%rbp) + jmp .L3 +.L5: + movl -8(%rbp), %eax + cltq + leaq 0(,%rax,4), %rdx + movq -24(%rbp), %rax + addq %rdx, %rax + movl -8(%rbp), %edx + movslq %edx, %rdx + addq $1, %rdx + leaq 0(,%rdx,4), %rcx + movq -24(%rbp), %rdx + addq %rcx, %rdx + movl (%rax), %eax + movl %eax, (%rdx) + subl $1, -8(%rbp) +.L3: + cmpl $0, -8(%rbp) + js .L4 + movl -8(%rbp), %eax + cltq + leaq 0(,%rax,4), %rdx + movq -24(%rbp), %rax + addq %rdx, %rax + movl (%rax), %eax + cmpl %eax, -4(%rbp) + jl .L5 +.L4: + movl -8(%rbp), %eax + cltq + addq $1, %rax + leaq 0(,%rax,4), %rdx + movq -24(%rbp), %rax + addq %rax, %rdx + movl -4(%rbp), %eax + movl %eax, (%rdx) + addl $1, -12(%rbp) +.L2: + movl -12(%rbp), %eax + cmpl -28(%rbp), %eax + jl .L6 + nop + nop + popq %rbp + .cfi_def_cfa 7, 8 + ret + .cfi_endproc +.LFE0: + .size insertionSort, .-insertionSort + .ident "GCC: (GNU) 15.1.1 20250729" + .section .note.GNU-stack,"",@progbits + diff --git a/HW3/InsertionSort/src/main.c b/HW3/InsertionSort/src/main.c new file mode 100644 index 0000000..688b855 --- /dev/null +++ b/HW3/InsertionSort/src/main.c @@ -0,0 +1,41 @@ +#include +extern void insertionSort(int *changeArr, int size); + +int compareArrays(int *arr1, int *arr2, int size); +int fillArray(int *arr); + +int main() { + int changeArr[100]; + int origArr[100]; + + int elNumber = fillArray(changeArr); + + for (int i = 0; i < elNumber; i++) + origArr[i] = changeArr[i]; + + insertionSort(changeArr, elNumber); + int changeCount = compareArrays(origArr, changeArr, elNumber); + + return changeCount; +} + +int compareArrays(int *arr1, int *arr2, int size) { + int changes = 0; + for (int i = 0; i < size; i++) + if (arr1[i] != arr2[i]) + changes++; + + return changes; +} + +int fillArray(int *arr) { + int count = 0; + printf("Enter numbers:\n"); + + do { + scanf("%d", &arr[count++]); + } while (getchar() != '\n' && count < 100); + + return count; +} +