Necto

qs in inline asm

May 6th, 2023 (edited)
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.57 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. int* input(char* filename, int* n) {
  5.     FILE* file = fopen(filename, "r");
  6.  
  7.     if (file == NULL) {
  8.         printf("failed opening file");
  9.         exit(1);
  10.     }
  11.  
  12.     int nums = 0, count = 0;
  13.    
  14.     while (fscanf(file, "%d", &nums) == 1)
  15.         ++count;
  16.     rewind(file);
  17.  
  18.     int* arr = (int*)malloc(count * sizeof(int));
  19.  
  20.     int i = 0;
  21.     while (fscanf(file, "%d", &nums) == 1)
  22.         arr[i++] = nums;
  23.    
  24.     fclose(file);
  25.     *n = count;
  26.     return arr;
  27. }
  28.  
  29. void output(int* arr, int n) {
  30.     FILE* file = fopen("sorted.txt", "w");
  31.  
  32.     if (file == NULL) {
  33.         printf("failed opening file");
  34.         exit(1);
  35.     }
  36.  
  37.     for (int i = 0; i < n; i++) {
  38.         fprintf(file, "%d ", arr[i]);
  39.     }
  40.  
  41.     fclose(file);
  42. }
  43.  
  44. void sort(int* arr, int n) {
  45.  
  46.     __asm {
  47.     start:
  48.         mov ecx, 0;
  49.         mov esi, arr;
  50.     loop_start:
  51.         cmp ecx, n;
  52.         je end_loop;
  53.         jmp init_j_and_cond;
  54.         jmp loop_start;
  55.  
  56.     init_j_and_cond:
  57.         mov eax, ecx;
  58.         inc ecx;
  59.     another_loop:
  60.         cmp eax, 0;
  61.         je loop_start;
  62.         mov edx, [esi + eax * 4];
  63.         cmp edx, [esi + eax * 4 - 4];
  64.         jge loop_start;
  65.         mov ebx, [esi + eax * 4 - 4];
  66.         mov [esi + eax * 4 - 4], edx;
  67.         mov [esi + eax * 4], ebx;
  68.         dec eax;
  69.         jmp another_loop;
  70.  
  71.     end_loop:
  72.  
  73.     }
  74. }
  75.  
  76.  
  77. int main(int argc, char** argv) {
  78.     int n;
  79.     int* arr = input(argv[1], &n);
  80.  
  81.     sort(arr, n);
  82.     output(arr, n);
  83.  
  84.     free(arr);
  85.     return 0;
  86. }
  87.  
Advertisement
Add Comment
Please, Sign In to add comment