Advertisement
rudolf222222

Untitled

Oct 21st, 2022 (edited)
989
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. .intel_syntax noprefix
  2. .text
  3. .global mergesort
  4.  
  5. // ; rdi, rsi, rdx, rcx, r8, r9
  6. // ; void Merge(std::vector<Segment>& segments, int from, int middle, int to)
  7. // ; extern void mergesort(int from, int to, const int *in, int *out);
  8.  
  9. mergesort:
  10.     push rdx
  11.     push rcx
  12.     push r10
  13.     push r12
  14.     cmp r11d, 0
  15.     je .fill_out
  16.        .fill_out:
  17.             mov r11d, 1
  18.             mov r12d, [rdx]
  19.             mov [rcx], r12d
  20.             add rcx, 4
  21.             add rdx, 4
  22.             add r10d, 1
  23.             cmp r10d, esi
  24.             jl .fill_out
  25.  
  26.     pop r12
  27.     pop r10
  28.     pop rcx
  29.     pop rdx
  30.     push rsi // ; saving to
  31.     push rdi //; saving from
  32.     cmp edi, esi //; if from > to, return
  33.         jg .return
  34.  
  35.     mov r12d, esi // writing in 12 register second argument a.k.a to
  36.     add r12d, edi // add from
  37.     mov eax, r12d
  38.     mov r9d, 2
  39.     div r9d // dividing whats in eax by 2
  40.     mov esi, eax // wiritng eax in second argument
  41.     call mergesort
  42.  
  43.     pop rdi
  44.     pop rsi
  45.     push rsi //  saving from
  46.     push rdi // saving to
  47.     mov edi, eax // writing middle in from
  48.     add edi, 1 // adding 1 to get middle + 1
  49.     call mergesort // calling mergesort with rest default arguments
  50.  
  51.    
  52.     pop rdi
  53.     pop rsi             //  returning values to their places
  54.                        //; void Merge(std::vector<Segment>& segments, int from, int middle, int to)
  55.                        //; extern void mergesort(int from, int to, const int *in, int *out);
  56.     push rsi
  57.     push rcx
  58.     push rdi
  59.     push rdx
  60.     mov r14d, edi // writing from to 14-th register
  61.     mov edi, ecx // writing in first argument pointer to out, where elems are already copied
  62.     mov r13d, esi // writing in 13-th register second argument a.k.a to
  63.     mov esi, r14d // writing in second argument 14-th register where is placed "from"
  64.     mov ecx, r13d // writing to last argument 'to'
  65.     mov edx, eax // writing in register that holds pointer to const middle, previously pushed him to stack
  66.     call merge
  67.     pop rdx
  68.     pop rdi
  69.     pop rcx
  70.     pop rsi
  71.     jmp .return
  72.    .return:
  73.     ret
  74.  
  75.  
  76. merge:
  77.    // ; rdx = middle, rsi = from, rcx = to
  78.     mov r9d, esi // ; int i = from
  79.     mov r10d, edx // ; int j = middle
  80.     add r10d, 1 //; j += 1
  81.  
  82.     .while: // ; while(i <= middle && j <= to)
  83.     cmp r9d, edx //;  r9 <= middle
  84.         jle .fill_first //; if i(r9) <= rdx(middle) -> state to compare r10(j) and rcx(to)
  85.         jg .fill_the_rest_with_second //; if r > middle, then we jump into state where we fill the rest with second part, if there are any left
  86.    
  87.     .fill_the_rest_with_second:
  88.         cmp r10d, ecx // ; if j is greater than to, then we finish all this stuff
  89.             jg .refill_array // ; refill with sorted elems
  90.         push [rdi+r10*4] //; pushing segments[j] to stack
  91.         add r10d, 1 //; j++
  92.             jmp .fill_the_rest_with_second
  93.    
  94.     .fill_first: // ; here i(r9) <= middle
  95.         cmp r10d, ecx //; if r10 <= rcx -> do the comparing while loop
  96.             jle .fill_both //; -> state for comparing while loop
  97.         push [rdi+r9*4] //; vector.push([segments[i]])
  98.         add r9d, 1 //; i++
  99.             jmp .while //; jump back to while
  100.    
  101.     .fill_both: //; we already checked that this is the case we need
  102.         mov r8d, [rdi+r9*4]
  103.         cmp r8d, [rdi+r10*4] //; if(arr[i] < arr[j]){ input in resullt array i-th the smaller element}
  104.             jl .add_from_first //; push to stack the smaller if segments[i] < segments[j]
  105.             jge .add_from_second //; else push segments[j]
  106.    
  107.     .add_from_first:
  108.         push [rdi+r9*4] //; pushing segments[i]
  109.         add r9d, 1 //; i++
  110.         jmp .while //; jump back to while start
  111.    
  112.     .add_from_second:
  113.         push [rdi+r10*4] //; pushing segments[j]
  114.         add r10d, 1 //; j++
  115.         jmp .while //; jumping back to while
  116.    
  117.     .refill_array:
  118.         mov r9d, ecx //; i = to
  119.         mov r10d, 0 // counter = 0
  120.         loop:
  121.             cmp r9d, esi // if i < from
  122.                 jl .end // end
  123.             pop r10 // pop last pushed element
  124.             mov [rdi+r9*4], r10d // place it from the back in out array
  125.             sub r9d, 1
  126.             jmp loop  
  127.    
  128.         .end:
  129.         ret
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement