SHARE
TWEET

Untitled

a guest Oct 22nd, 2019 79 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. .global mergesort
  2.     .global mergesort_
  3.     .global merge
  4.     .intel_syntax noprefix
  5.     .text
  6.  
  7. merge:
  8.     push ebp
  9.     mov ebp, esp
  10.  
  11.     push eax
  12.     push ebx
  13.     push ecx
  14.     push edx
  15.  
  16.    
  17.  
  18.     mov ecx, 0      /* i = 0 */
  19.     mov edx, 0      /* j = 0  */
  20.  
  21. merge_stage:
  22.  
  23.     mov eax, [ebp + 12]     /* first array size */
  24.     mov ebx, [ebp + 20]     /* second array size */
  25.  
  26.     cmp ecx, eax        /*  if (i < a.sz)  */
  27.     jl second_check
  28.     jmp full_first
  29.  
  30.  
  31.  
  32. second_check:
  33.     cmp edx, ebx
  34.     jl compare      /* if (j < b.sz ) */
  35.     jmp full_second
  36.  
  37. compare:
  38.     mov eax, [ebp + 8]          /* get a[i] */
  39.     mov eax, [eax + 4 * ecx]
  40.     mov ebx, [ebp + 16]
  41.     mov ebx, [ebx + 4 * edx]        /* get b[j */
  42.  
  43.     mov edi, [ebp + 24]         /* *c   */
  44.     cmp eax, ebx
  45.     jl first_min        /* if (a[i] < b[j])  */
  46.     jmp second_min
  47.  
  48.  
  49. full_first:
  50.     cmp edx, ebx
  51.     jl continue_s           /* if (j < b.sz ) */
  52.     jmp end
  53.  
  54. continue_s:
  55.     mov eax, [ebp + 16]
  56.     mov eax, [eax + 4 * edx]        /* get b[j]  */
  57.  
  58.     mov edi, [ebp + 24]
  59.  
  60.  
  61.     mov esi, 4
  62.     imul esi, ecx
  63.     add edi, esi
  64.  
  65.     mov esi, 4
  66.     imul esi, edx
  67.     add edi, esi
  68.  
  69.     mov [edi], eax
  70.  
  71.     add edx, 1
  72.     jmp full_first
  73.    
  74.  
  75. full_second:
  76.     cmp ecx, eax
  77.     jl continue_f
  78.     jmp end
  79.  
  80. continue_f:
  81.     mov ebx, [ebp + 8]
  82.     mov ebx, [ebx + 4 * ecx]        /* get a[i]  */
  83.     mov edi, [ebp + 24]
  84.  
  85.     mov esi, 4
  86.     imul esi, ecx
  87.     add edi, esi
  88.  
  89.     mov esi, 4
  90.     imul esi, edx
  91.     add edi, esi
  92.     mov [edi], ebx
  93.  
  94.     add ecx, 1
  95.     jmp full_second
  96.  
  97. first_min:
  98.     mov esi, 4
  99.     imul esi, ecx
  100.     add edi, esi
  101.  
  102.     mov esi, 4
  103.     imul esi, edx
  104.     add edi, esi
  105.  
  106.     mov [edi], eax
  107.     add ecx, 1
  108.     jmp merge_stage
  109.  
  110.  
  111. second_min:
  112.     mov esi, 4
  113.     imul esi, ecx
  114.     add edi, esi
  115.     mov esi, 4
  116.     imul esi, edx
  117.     add edi, esi
  118.     mov [edi], ebx
  119.     add edx, 1
  120.     jmp merge_stage
  121.  
  122. end:
  123.     pop edx
  124.     pop ecx
  125.     pop ebx
  126.     pop eax
  127.     pop ebp
  128.     ret
  129.  
  130.  
  131. mergesort_:
  132.     push ebp
  133.     mov ebp, esp
  134.     push ebx
  135.  
  136.     mov eax, [ebp + 12]
  137.     sub eax, [ebp + 8]
  138.  
  139.  
  140.  
  141.  
  142.     cmp eax, 2              /* eax = size */
  143.     jl end_merge
  144.  
  145.     xor edx, edx
  146.     mov ebx, 2
  147.  
  148.     idiv ebx
  149.  
  150.     add eax, [ebp + 8]
  151.     push eax            /* eax = border */
  152.  
  153.  
  154.     mov ebx, eax       
  155.     sub ebx, [ebp + 8]
  156.     push ebx
  157.  
  158.    
  159.     push [ebp + 20]
  160.     push [ebp + 16]
  161.     push eax
  162.     push [ebp + 8]
  163.     call mergesort_
  164.    
  165.     pop esi
  166.     pop eax
  167.     pop esi
  168.     pop esi
  169.  
  170.  
  171.     push [ebp + 20]
  172.     push [ebp + 16]
  173.     push [ebp + 12]
  174.     push eax
  175.  
  176.     call mergesort_
  177.  
  178.     pop eax
  179.     pop esi
  180.     pop esi
  181.     pop esi
  182.  
  183.     pop edx
  184.     pop eax
  185.  
  186.  
  187.     mov ebx, [ebp + 12]    
  188.     sub ebx, eax            /* ebx - b.sz */
  189.  
  190.  
  191.  
  192.     mov esi, 4
  193.     imul esi, ebx
  194.     sub esp, esi
  195.     mov esi, 4
  196.  
  197.     imul esi, edx
  198.     sub esp, esi
  199.  
  200.     push esp
  201.     push ebx
  202.  
  203.     mov edi, [ebp + 20]
  204.     mov esi, 4
  205.     imul esi, eax
  206.     add edi, esi
  207.  
  208.     push edi
  209.     push edx
  210.  
  211.     mov edi, [ebp + 20]
  212.     mov esi, 4
  213.     imul esi, [ebp + 8]
  214.     add edi, esi
  215.     push edi
  216.  
  217.  
  218.     call merge
  219.  
  220.     pop edx
  221.     pop ecx
  222.     pop ebx
  223.     pop eax
  224.     pop esp
  225.  
  226.  
  227.     mov edx, [ebp + 20]
  228.     mov esi, 0
  229.  
  230.     add eax, ecx    /* full size */
  231.  
  232. fill:
  233.     mov ebx, [ebp + 8]
  234.     cmp esi, eax
  235.     jl copy
  236.     jmp end_merge
  237.  
  238.  
  239. copy:
  240.     add ebx, esi
  241.     mov ecx, [esp]
  242.     mov [edx + 4 * ebx], ecx
  243.     add esp, 4
  244.     add esi, 1
  245.     jmp fill
  246.    
  247.  
  248.  
  249. end_merge:
  250.     pop ebx
  251.     pop ebp
  252.     ret
  253.  
  254.  
  255. mergesort:
  256.     push ebp
  257.     mov ebp, esp
  258.     push ebx
  259.  
  260.     mov eax, [ebp + 8]
  261.     mov ebx, [ebp + 12]
  262.     mov ecx, [ebp + 16]
  263.     mov edx, [ebp + 20]
  264.  
  265.     mov esi, 0
  266.     sub ebx, eax
  267.  
  268. while:
  269.     cmp esi, ebx
  270.     jl nfull
  271.     jmp next_step
  272.  
  273. nfull:
  274.     mov edi, eax
  275.     add edi, esi
  276.     mov edi, [ecx + 4 * edi]
  277.     mov [edx + 4 * esi], edi
  278.     add esi, 1
  279.     jmp while
  280.  
  281. next_step:
  282.     push [ebp + 20]
  283.     push [ebp + 16]
  284.     push ebx
  285.     push 0
  286.  
  287.     call mergesort_
  288.  
  289.     pop eax
  290.     pop eax
  291.     pop eax
  292.     pop eax
  293.    
  294.     pop ebx
  295.     pop ebp
  296.     ret
  297.  
  298.  
  299.  
  300. scanf_and_printf_format_ptr:
  301.     .long scanf_and_printf_format
  302.  
  303. scanf_and_printf_format:
  304.     .string "%d %d\n"
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top