Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- int* input(char* filename, int* n) {
- FILE* file = fopen(filename, "r");
- if (file == NULL) {
- printf("failed opening file");
- exit(1);
- }
- int nums = 0, count = 0;
- while (fscanf(file, "%d", &nums) == 1)
- ++count;
- rewind(file);
- int* arr = (int*)malloc(count * sizeof(int));
- int i = 0;
- while (fscanf(file, "%d", &nums) == 1)
- arr[i++] = nums;
- fclose(file);
- *n = count;
- return arr;
- }
- void output(int* arr, int n) {
- FILE* file = fopen("sorted.txt", "w");
- if (file == NULL) {
- printf("failed opening file");
- exit(1);
- }
- for (int i = 0; i < n; i++) {
- fprintf(file, "%d ", arr[i]);
- }
- fclose(file);
- }
- void sort(int* arr, int n) {
- __asm {
- start:
- mov ecx, 0;
- mov esi, arr;
- loop_start:
- cmp ecx, n;
- je end_loop;
- jmp init_j_and_cond;
- jmp loop_start;
- init_j_and_cond:
- mov eax, ecx;
- inc ecx;
- another_loop:
- cmp eax, 0;
- je loop_start;
- mov edx, [esi + eax * 4];
- cmp edx, [esi + eax * 4 - 4];
- jge loop_start;
- mov ebx, [esi + eax * 4 - 4];
- mov [esi + eax * 4 - 4], edx;
- mov [esi + eax * 4], ebx;
- dec eax;
- jmp another_loop;
- end_loop:
- }
- }
- int main(int argc, char** argv) {
- int n;
- int* arr = input(argv[1], &n);
- sort(arr, n);
- output(arr, n);
- free(arr);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment