From 2197d80685a7dfc6c2c0e17970987de0a759d23a Mon Sep 17 00:00:00 2001 From: Dan Date: Mon, 6 Oct 2025 13:31:45 +0300 Subject: [PATCH 1/5] Add insetion sort algorithm --- HW3/InsertionSort/src/insertionSort.s | 78 +++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 HW3/InsertionSort/src/insertionSort.s diff --git a/HW3/InsertionSort/src/insertionSort.s b/HW3/InsertionSort/src/insertionSort.s new file mode 100644 index 0000000..6141447 --- /dev/null +++ b/HW3/InsertionSort/src/insertionSort.s @@ -0,0 +1,78 @@ + .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 From 74536366f699d9f409c2e93a6591e42d4627712a Mon Sep 17 00:00:00 2001 From: Dan Date: Mon, 6 Oct 2025 13:33:23 +0300 Subject: [PATCH 2/5] Add main program with I/O handling --- HW3/InsertionSort/src/main.c | 41 ++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 HW3/InsertionSort/src/main.c 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; +} + From c411ee9b798469bce2268ba1091099e734350a1c Mon Sep 17 00:00:00 2001 From: Dan Date: Mon, 6 Oct 2025 13:34:50 +0300 Subject: [PATCH 3/5] Add README with build and usage instructions --- HW3/InsertionSort/src/README.md | 40 +++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 HW3/InsertionSort/src/README.md diff --git a/HW3/InsertionSort/src/README.md b/HW3/InsertionSort/src/README.md new file mode 100644 index 0000000..2600ca0 --- /dev/null +++ b/HW3/InsertionSort/src/README.md @@ -0,0 +1,40 @@ +# 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 система From 32ab294b36fedd0c39b805ef20b02e8a7901f40f Mon Sep 17 00:00:00 2001 From: Dan Date: Mon, 6 Oct 2025 13:40:02 +0300 Subject: [PATCH 4/5] Add final newline --- HW3/InsertionSort/src/README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/HW3/InsertionSort/src/README.md b/HW3/InsertionSort/src/README.md index 2600ca0..5770761 100644 --- a/HW3/InsertionSort/src/README.md +++ b/HW3/InsertionSort/src/README.md @@ -38,3 +38,4 @@ echo $? # Shows count of moved elements / Покажет количество - GCC compiler / компилятор GCC - Linux/Unix system / Linux/Unix система + From 9cf13b5bd01e388b5bf5074213981dacba9586f8 Mon Sep 17 00:00:00 2001 From: Dan Date: Mon, 6 Oct 2025 13:40:11 +0300 Subject: [PATCH 5/5] Add final newline --- HW3/InsertionSort/src/insertionSort.s | 1 + 1 file changed, 1 insertion(+) diff --git a/HW3/InsertionSort/src/insertionSort.s b/HW3/InsertionSort/src/insertionSort.s index 6141447..ce64275 100644 --- a/HW3/InsertionSort/src/insertionSort.s +++ b/HW3/InsertionSort/src/insertionSort.s @@ -76,3 +76,4 @@ insertionSort: .size insertionSort, .-insertionSort .ident "GCC: (GNU) 15.1.1 20250729" .section .note.GNU-stack,"",@progbits +