Advertisement
davispuh

Selection Sort

Apr 16th, 2013
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ; ----------------------------------------------------------------------------
  2. ; SelectionSort.asm
  3. ;
  4. ; Selection sort is a sorting algorithm, specifically an in-place comparison sort.
  5. ;
  6. ; ----------------------------------------------------------------------------
  7.  
  8. ;
  9. ;      10 000 =   0.040820039808750 sec
  10. ;      50 000 =   0.907835304737091 sec
  11. ;     100 000 =   4.108306407928467 sec
  12. ;     200 000 =  16.423355102539062 sec
  13. ;   1 000 000 = 421.7449951171875 sec (7min)
  14. ;
  15. ; -------- Constants -------
  16. ;
  17.     MAX_NUMBERS equ 50000
  18.  
  19.     %define SORTDESC 0
  20.     %if SORTDESC > 0
  21.         CMPMASK equ 0xFFFF
  22.     %else
  23.         CMPMASK equ 0x0000
  24.     %endif
  25.     NULL equ 0
  26.     STD_OUTPUT_HANDLE equ -11
  27.     MB_OK equ 0x00000000
  28.     INVALID_HANDLE_VALUE equ -1
  29.     CRYPT_NEWKEYSET equ 0x8
  30.     PROV_RSA_FULL equ 1
  31.     CREATE_ALWAYS equ 2
  32.     GENERIC_WRITE equ 0x40000000
  33. ;
  34. ; ---- End of Constants ----
  35. ;
  36.  
  37.     global _main
  38.  
  39.     extern  _GetStdHandle@4
  40.     extern  _WriteFile@20
  41.     extern _WriteConsoleA@20
  42.     extern  _ExitProcess@4
  43.     extern  _MessageBoxA@16
  44.     extern _GetLastError@0
  45.     extern _AllocConsole@0
  46.     extern _AttachConsole@4
  47.     extern _CryptGenRandom@12
  48.     extern _CryptAcquireContextA@20
  49.     extern _CryptReleaseContext@8
  50.     extern _CreateFileA@28
  51.     extern _CloseHandle@4
  52.     extern _QueryPerformanceCounter@4
  53.     extern _QueryPerformanceFrequency@4
  54.  
  55. %macro MessageBox 2
  56.     ; int WINAPI MessageBox(HWND hWnd, LPCTSTR lpText, LPCTSTR lpCaption, UINT uType );
  57.     push MB_OK
  58.     push %2
  59.     push %1
  60.     push 0
  61.     call _MessageBoxA@16
  62. %endmacro
  63.  
  64. %macro WriteFile 3
  65. ;HANDLE WINAPI CreateFile( LPCTSTR lpFileName, DWORD dwDesiredAccess, DWORD dwShareMode, LPSECURITY_ATTRIBUTES lpSecurityAttributes, DWORD dwCreationDisposition, DWORD dwFlagsAndAttributes, HANDLE hTemplateFile);
  66. push NULL
  67. push NULL
  68. push CREATE_ALWAYS
  69. push NULL
  70. push NULL
  71. push GENERIC_WRITE
  72. push %1
  73. call _CreateFileA@28
  74.  
  75. push eax
  76.  
  77. ; BOOL WINAPI WriteFile(HANDLE hFile, LPCVOID lpBuffer,DWORD nNumberOfBytesToWrite, LPDWORD lpNumberOfBytesWritten, LPOVERLAPPED lpOverlapped);
  78. push NULL
  79. push Temp
  80. push %3
  81. push %2
  82. push eax
  83. call _WriteFile@20
  84.  
  85. ; BOOL WINAPI CloseHandle( HANDLE hObject );
  86. call _CloseHandle@4
  87. %endmacro
  88.  
  89. %macro Print 2
  90.     ; BOOL WINAPI WriteConsole( HANDLE hConsoleOutput, const VOID *lpBuffer, DWORD nNumberOfCharsToWrite, LPDWORD lpNumberOfCharsWritten, LPVOID lpReserved );
  91.     push    0
  92.     push    NumberOfCharsWritten
  93.     push    %2
  94.     push    %1
  95.     push    DWORD [Handle]
  96.     call    _WriteConsoleA@20
  97. %endmacro
  98.  
  99. %macro PrintLn 0
  100.     Print NewLine, 2
  101. %endmacro
  102.  
  103. %macro FillRandom 0
  104.     mov byte [Numbers], 65
  105.     push Numbers
  106.     push MAX_NUMBERS
  107.     push dword [CryptProv]
  108.     call _CryptGenRandom@12
  109. %endmacro
  110.  
  111. ;  EDX - address to memory
  112. ;  ECX - byte count
  113. PrintReversedHex:
  114.     push edx
  115.     push ecx
  116.     Print HEXStart, 2
  117.     pop ecx
  118.     pop edx
  119.     mov ebx, ecx
  120.     .loop:
  121.         push ecx
  122.         ;mov eax, ecx
  123.         ;sub eax, ecx
  124.         mov al, byte [edx+ecx-1]
  125.         and eax, 0xFF
  126.         div byte [m16]
  127.         xor ecx, ecx
  128.         mov cl, al
  129.         add ecx, HEX
  130.         push edx
  131.         push eax
  132.         Print ecx, 1
  133.         pop eax
  134.         xor ecx, ecx
  135.         mov cl, ah
  136.         add ecx, HEX
  137.         Print ecx, 1
  138.         pop edx
  139.         pop ecx
  140.     loop .loop
  141. ret
  142.  
  143. MakeHex:
  144.     mov ecx, MAX_NUMBERS
  145.     .loop:
  146.         xor eax, eax
  147.         mov al, byte [Numbers+ecx-1]
  148.         xor ebx, ebx
  149.         div byte [m16]
  150.         mov bl, al
  151.         mov al, byte [HEX+ebx]
  152.         xor ebx, ebx
  153.         mov bl, ah
  154.         mov ah, byte [HEX+ebx]
  155.         push eax
  156.         mov eax, ecx
  157.         sub eax, 1
  158.         xor edx, edx
  159.         mul dword [m6]
  160.         mov ebx, eax
  161.         pop eax
  162.         mov byte [NumbersHEX+ebx], '0'
  163.         mov byte [NumbersHEX+1+ebx], 'x'
  164.         mov byte [NumbersHEX+2+ebx], al
  165.         mov byte [NumbersHEX+3+ebx], ah
  166.         mov byte [NumbersHEX+4+ebx], 13
  167.         mov byte [NumbersHEX+5+ebx], 10
  168.     loope .loop
  169. ret
  170.  
  171. %macro Fill_xmm1_With_al 0
  172.     PINSRB xmm0, al, 0
  173.     PINSRB xmm0, al, 1
  174.     PINSRB xmm0, al, 2
  175.     PINSRB xmm0, al, 3
  176.     PSHUFD xmm1, xmm0, 0
  177.     PADDB xmm1, xmm4
  178.     ;mov edi, esi
  179. %endmacro
  180.  
  181. %macro CheckReg 1
  182.         PEXTRB eax, xmm0, %1
  183.         cmp eax, CMPMASK
  184.         jne .not%1
  185.             PEXTRB eax, xmm2, %1
  186.             cmp esi, eax
  187.             %if SORTDESC > 0
  188.                 jae .not%1
  189.             %else
  190.                 jb .not%1
  191.             %endif
  192.             mov edx, ebx
  193.             add edx, %1
  194.             mov esi, eax
  195.         .not%1:
  196. %endmacro
  197.  
  198. %macro Swap 0
  199.     mov al, byte [Numbers+ecx]
  200.     xchg byte [Numbers+edx], al
  201.     mov byte [Numbers+ecx], al
  202. %endmacro
  203.  
  204. ;
  205. ;  Sort - Selection sort
  206. ;
  207. ;  ecx - i, position in array
  208. ;  ebx - j, position in array
  209. ;  edx - min or max index
  210. ;
  211. Sort:
  212.     xor eax,eax
  213.     xor ecx, ecx
  214.     xor edx, edx
  215. .loop:
  216.     mov ebx, ecx
  217.     add ebx, 1
  218.     mov al, byte [Numbers+ecx]
  219.     .loop2:
  220.         cmp byte [Numbers+ebx], al
  221.         %if SORTDESC > 0
  222.           jb .not
  223.         %else
  224.           jae .not
  225.         %endif
  226.             mov edx, ebx
  227.             mov al, byte [Numbers+edx]
  228.         .not:
  229.         add ebx, 1
  230.         cmp ebx, MAX_NUMBERS
  231.     jb .loop2
  232.     cmp edx, ecx
  233.     je .continue
  234.         Swap
  235.     .continue:
  236.     add ecx, 1
  237.     cmp ecx, MAX_NUMBERS
  238.     jbe .loop
  239. ret
  240.  
  241. TimeElapsed:
  242. ; BOOL WINAPI QueryPerformanceCounter( LARGE_INTEGER *lpPerformanceCount );
  243.     MOVQ xmm2, qword [Frequency]
  244.     PEXTRD eax, xmm2, 1
  245.     PEXTRD ebx, xmm2, 0
  246.     or  eax,ebx
  247.     jz .skip
  248.       push CurrentCounter
  249.       call _QueryPerformanceCounter@4
  250.       MOVQ xmm0, qword [CurrentCounter]
  251.       MOVQ xmm1, qword [LastCounter]
  252.       MOVQ qword [LastCounter], xmm0
  253.       PSUBQ xmm0, xmm1
  254.       MOVQ qword [Temp], xmm0
  255.       FILD qword [Frequency]
  256.       FST ST1
  257.       FILD qword [Temp]
  258.       FTST
  259.       jbe .skip
  260.       FDIV ST1
  261.       FST dword [DeltaTime]
  262.     .skip:
  263. ret
  264.  
  265.  
  266. %macro Header 0
  267.     mov     ebp, esp
  268.     sub     esp, 4
  269.  
  270.     ; BOOL WINAPI AttachConsole( DWORD dwProcessId );
  271.     push -1
  272.     call _AttachConsole@4
  273.     cmp eax, 0
  274.     jz .error
  275.     jmp .no_error
  276.     .error:
  277.         ; BOOL WINAPI AllocConsole(void);
  278.         call _AllocConsole@0
  279.     .no_error:
  280.     ; HANDLE WINAPI GetStdHandle( DWORD nStdHandle );
  281.     push    STD_OUTPUT_HANDLE
  282.     call    _GetStdHandle@4
  283.     cmp eax, NULL
  284.     je LabelError
  285.     cmp eax, INVALID_HANDLE_VALUE
  286.     je LabelError
  287.     mov [Handle], eax
  288.  
  289.     push NULL
  290.     push PROV_RSA_FULL
  291.     push NULL
  292.     push Key
  293.     push CryptProv
  294.     call _CryptAcquireContextA@20
  295.     cmp eax,0
  296.     jne success
  297.     push CRYPT_NEWKEYSET
  298.     push PROV_RSA_FULL
  299.     push NULL
  300.     push Key
  301.     push CryptProv
  302.     call _CryptAcquireContextA@20
  303.     success:
  304.     ; BOOL WINAPI QueryPerformanceFrequency( LARGE_INTEGER *lpFrequency );
  305.     push Frequency
  306.     call _QueryPerformanceFrequency@4
  307.     push LastCounter
  308.     call _QueryPerformanceCounter@4
  309. %endmacro
  310.  
  311. %macro Footer 0
  312.     push 0
  313.     push dword [CryptProv]
  314.     call _CryptReleaseContext@8
  315.     jmp Exit
  316.     LabelError:
  317.         MessageBox TextExit, TextError
  318.     Exit:
  319.     call FunctionExit
  320. %endmacro
  321.  
  322.  
  323. section .text
  324.  
  325. FunctionExit:
  326.     ; ExitProcess(0)
  327.     push    0
  328.     call    _ExitProcess@4
  329.  
  330.     ; never here
  331.     hlt
  332.  
  333. NewLine: db 10,10
  334. m6: dd 6
  335. m16: db 16
  336. align 16
  337. n128: db 80h, 80h, 80h, 80h, 80h, 80h, 80h, 80h, 80h, 80h, 80h, 80h, 80h, 80h, 80h, 80h
  338. HEXStart: db '0x'
  339. HEX: db '0123456789ABCDEF'
  340. Key: db 'ThisKey',0
  341. FileName: db 'File.dat',0
  342. FileNameSorted: db 'FileSorted.dat',0
  343. FileNameHEX: db 'FileHex.txt',0
  344. FileNameSortedHEX: db 'FileSortedHex.txt',0
  345. Author:  db 'davispuh',10
  346. AuthorLength equ $-Author
  347. Tittle:  db 'Selection Sort',10
  348. TittleLength equ $-Tittle
  349. Done: db 'Done!',0
  350. TextError: db      'Error!', NULL
  351. TextExit:  db      'Error occurred program will exit.', NULL
  352.  
  353. _main:
  354.     Header
  355.     PrintLn
  356.     Print Author, AuthorLength
  357.     Print Tittle, TittleLength
  358.  
  359.     %if SORTDESC > 0
  360.         mov eax, 0x00000000
  361.     %else
  362.         mov eax, 0xFFFFFFFF
  363.     %endif
  364.  
  365.     mov dword [padding], eax
  366.     mov dword [padding+4], eax
  367.     mov dword [padding+8], eax
  368.     mov dword [padding+12], eax
  369.  
  370.     FillRandom
  371.    
  372.     WriteFile FileName, Numbers, MAX_NUMBERS
  373.     call MakeHex
  374.     WriteFile FileNameHEX, NumbersHEX, MAX_NUMBERS*6
  375.     call Sort
  376.  
  377.     WriteFile FileNameSorted, Numbers, MAX_NUMBERS
  378.     call MakeHex
  379.     WriteFile FileNameSortedHEX, NumbersHEX, MAX_NUMBERS*6
  380.     mov ecx, 10
  381.     .repeat:
  382.         push ecx
  383.         FINIT
  384.         FillRandom
  385.         call TimeElapsed
  386.         call Sort
  387.         call TimeElapsed
  388.         mov ecx, 4
  389.         mov edx, DeltaTime
  390.         call PrintReversedHex
  391.         Print NewLine, 1
  392.         pop ecx
  393.         sub ecx, 1
  394.         cmp ecx, 0
  395.         ja .repeat
  396.    
  397.     PrintLn
  398.     MessageBox Done, Done
  399.     Footer
  400.  
  401. section .bss
  402.  
  403. Handle: resd 1
  404. NumberOfCharsWritten: resd 1
  405. alignb 16
  406. padding2: resb 16
  407. Numbers: resb MAX_NUMBERS
  408. padding: resb 16
  409. NumbersHEX: resb MAX_NUMBERS*6
  410. Frequency: resq 1
  411. CurrentCounter: resq 1
  412. LastCounter: resq 1
  413. DeltaTime: resq 2
  414. Temp: resb 100
  415. CryptProv: resd 1
  416. resb 1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement