Advertisement
kerelius

homework_Q2

Oct 18th, 2019
2,756
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. arrayDisplay    macro   Array,   numOfElemnts
  2.     push    ebx
  3.     mov     ebx,    offset Array
  4.  
  5.     i = 0
  6.     while i LT numOfElemnts
  7.         movsd   xmm0,   real8 ptr [ebx + i * type real8]
  8.         sub     esp,    8
  9.         movsd   real8 ptr [esp],    xmm0
  10.         push    offset  outputFormatString
  11.         call    printf_s
  12.         add     esp,    12
  13.  
  14.         i = i + 1
  15.     endm
  16.     pop     ebx
  17. endm
  18.  
  19. .686
  20. .xmm
  21. .model flat, c
  22.  
  23. includelib libcmt.lib
  24. includelib libvcruntime.lib
  25. includelib libucrt.lib
  26. includelib legacy_stdio_definitions.lib
  27.  
  28. extern printf_s : proc
  29.  
  30. .data
  31. array                   real8   3.0, 5.0, 4.0, 8.0, 12.0, 4.50, 8.45, 8.10
  32. outputFormatString      byte    "%.2lf, ",0
  33. beforeSorting           byte    "The array before sorting = [", 0
  34. afterSorting            byte    "The array after sorting = [", 0
  35. closingBracket          byte    8,8,"]", 13, 10, 0
  36. .code
  37. main    proc
  38.     push    offset      beforeSorting   ;   printf_s("The array before sorting = [")
  39.     call    printf_s
  40.     add     esp,    4
  41.  
  42.     arrayDisplay    array,  8   ;   a macro to display the array before sorting
  43.  
  44.     push    offset      closingBracket  ;   printf_s("]")
  45.     call    printf_s
  46.     add     esp,    4
  47.  
  48.  
  49.  
  50.     push    sizeof      array   ;   pushing the array size into the stack
  51.     push    offset      array   ;   pushing the array starting address into the stack
  52.     call    selectionSort       ;   sorting the array
  53.     add     esp,    8           ;   clearing the stack
  54.  
  55.     push    offset      afterSorting   ;   printf_s("The array after sorting = [")
  56.     call    printf_s
  57.     add     esp,    4
  58.  
  59.     arrayDisplay    array,  8   ;   a macro to display the array after sorting
  60.  
  61.     push    offset      closingBracket  ;   printf_s("]")
  62.     call    printf_s
  63.     add     esp,    4
  64.  
  65.     xor     eax, eax
  66.     ret
  67. main    endp
  68.  
  69. selectionSort   proc, x: ptr dword, sizeOfX: dword
  70.     pushad                      ;   save all registers
  71.     mov     eax,    x           ;   eax <------ offset of array x
  72.  
  73.     xor     ebx,    ebx         ;   ebx <------ k = 0
  74.     xor     ecx,    ecx         ;   ecx <------ j = 0
  75.     xor     edx,    edx         ;   edx <------ minpos = 0
  76.  
  77.     mov     edi,    sizeOfX     ;   edi <------ size - 2
  78.     sub     edi,    type x * 2
  79.  
  80.     mov     esi,    sizeOfX     ;   esi <------ size - 1
  81.     sub     esi,    type x
  82.     Loop_k:                     ;   for(k = 0; k <= size - 2; k++)
  83.         cmp     ebx,    edi
  84.         ja      End_Loop_k
  85.  
  86.         mov     edx,    ebx ;   minpos = k
  87.  
  88.         mov     ecx,    ebx ;   j = k + 1
  89.         add     ecx,    type real8
  90.         Loop_j:                 ;   for(j = k + 1; j <= size - 1; j++)
  91.             cmp     ecx,    esi
  92.             ja      End_Loop_j
  93.  
  94.             movsd   xmm0,   real8 ptr [eax + ecx]   ;   xmm0 <---- x[j]
  95.             movsd   xmm1,   real8 ptr [eax + edx]   ;   xmm1 <---- x[minpos]
  96.  
  97.             comisd  xmm0,   xmm1                    ;   if(x[j] < x[minpos])
  98.             jae     No_Swap
  99.  
  100.             mov     edx,    ecx     ;   minpos = j
  101.  
  102.             No_Swap:
  103.             add     ecx,    type real8  ;   j++
  104.             jmp     Loop_j
  105.         End_Loop_j:
  106.         movsd   xmm0,   real8 ptr [eax + ebx]   ;   x[minpos] <---- x[k]
  107.         movsd   xmm1,   real8 ptr [eax + edx]   ;   x[k] <---- x[minpos]
  108.         movsd   real8 ptr [eax + edx],  xmm0
  109.         movsd   real8 ptr [eax + ebx],  xmm1
  110.  
  111.         add     ebx,    type real8  ;   k++
  112.         jmp     Loop_k
  113.     End_Loop_k:
  114.  
  115.     popad                           ;   restore all registers
  116.     ret
  117. selectionSort   endp
  118.  
  119. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement