⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.asm

📁 一个演示了用汇编语言编写的数据排序算法,源代码非常详细
💻 ASM
字号:
;-----------------------------------------------------------------------------------------
;
;  Title : Sorting algorithms in Assembly
;  Author: Marty Winkler
;
;  Date  : 20.03.2004
;  last modification : 13.06.2004
;
;
;  Copyright 2004 by Marty Winkler
;
;-----------------------------------------------------------------------------------------
;
;  Your feedback is welcome!
;
;  Contact:
;           E-Mail:  marty@codingcrew.de
;
;           Website: http://www.codingcrew.de
;
;-----------------------------------------------------------------------------------------
;
;  Purpose: With this program you can sort elements (presorting can be optionally
;           ascending, descending or unsorted) with the use of different sorting
;           algorithms. The program counts the needed comparisons, exchange operations,
;           additionally allocated memory and the elapsed time.
;           Furthermore, this program can display the working method of the sorting
;           algorithms in a simplified form.
;
;-----------------------------------------------------------------------------------------
;
;  Hint:    For building a working program from this source code you need to have Steve
;           Hutchesson's MASM32 package installed.
;
;           Download from: http://www.masm32.com
;
;-----------------------------------------------------------------------------------------


  .386
  .model flat, stdcall
  option casemap:none


;-----------------------------------------------------------------------------------------
; Include the data and other code files.
;-----------------------------------------------------------------------------------------

  include include.inc


;-----------------------------------------------------------------------------------------
; The code.
;-----------------------------------------------------------------------------------------

.code
Start:

  ;---------------------------------------------------------------------------------------
  ;-- Load the common control dll (if not already loaded).
  invoke InitCommonControlsEx, OFFSET ICCE


  ;---------------------------------------------------------------------------------------
  ;-- Save the module handle of the current module.
  invoke GetModuleHandle, NULL
  mov    hInstance, eax


  ;---------------------------------------------------------------------------------------
  ;-- Create the frame dialog.
  invoke DialogBoxParam, eax, IDD_FRAME, 0, ADDR FrameDialogProc, NULL


  ;---------------------------------------------------------------------------------------
  ;-- End the process.
  invoke ExitProcess, eax



;-----------------------------------------------------------------------------------------
; Converts a QWORD to a NULL-terminated ASCII string.
;-----------------------------------------------------------------------------------------

QW2ASCII PROC lpQWORD:DWORD, lpBuffer:DWORD

  LOCAL HiDWORD:DWORD
  LOCAL LoDWORD:DWORD

  mov    edi, lpBuffer
  mov    BYTE PTR [edi], NULL

  mov    ebx, lpQWORD
  mov    eax, DWORD PTR [ebx]
  mov    LoDWORD, eax
  mov    eax, DWORD PTR [ebx + 4]
  mov    HiDWORD, eax
  lea    ebx, HiDWORD
  lea    esi, LoDWORD

  mov    ecx, 10

@Loop1:
    mov    eax, DWORD PTR [esi]
    xor    edx, edx
    div    ecx
    mov    DWORD PTR [esi], eax

    mov    eax, DWORD PTR [ebx]
    div    ecx
    mov    DWORD PTR [ebx], eax

    or     dl, '0'
    inc    edi
    mov    BYTE PTR [edi], dl

    mov    eax, DWORD PTR [ebx]
    or     eax, DWORD PTR [esi]
    jnz    @Loop1

    mov    esi, lpBuffer
@Loop2:
    mov    ah, BYTE PTR [esi]
    mov    al, BYTE PTR [edi]
    mov    BYTE PTR [edi], ah
    mov    BYTE PTR [esi], al
    inc    esi
    dec    edi
    cmp    esi, edi
    jb     @Loop2

@Return:
    ret
QW2ASCII ENDP



;-----------------------------------------------------------------------------------------
; Gets the text of the window specified window and saves it's value in the randInitValue
; of the SortInfo-structure. When this procedure fails it returns 0 in EAX.
;-----------------------------------------------------------------------------------------

GetInitializationValue PROC hParent:HWND, hWnd:HWND, fSaveValue:DWORD

  invoke SendMessage, hWnd, WM_GETTEXTLENGTH, NULL, NULL
  test   eax, eax
  jnz    @GetValue


  ;---------------------------------------------------------------------------
  ;-- No value is specified, use the return value of GetTickCount instead.
  invoke GetTickCount
  mov    SortInfo.randInitValue, eax
  jmp    @ReturnWithoutError


@GetValue:
  ;---------------------------------------------------------------------------
  ;-- Get the specified string and convert it to an integer value.
  invoke GetIntegerFromWindowText, hParent, hWnd,
                                   lpSTR_ERR_TOLARGEINITVALUE,
                                   lpSTR_ERR_INITVALUEINVALID
  jc     @ReturnWithError
  mov    SortInfo.randInitValue, eax


  .IF fSaveValue

      ;---------------------------------------------------------------------------
      ;-- Convert this integer to an ASCII string.
      invoke dw2a, eax, OFFSET Buffer
      
      
      ;---------------------------------------------------------------------------
      ;-- Is this string already contained in the combo box?
      invoke SendMessage, hWnd, CB_FINDSTRINGEXACT, -1, OFFSET Buffer
    
      .IF eax == CB_ERR
    
          ;-----------------------------------------------------------------------
          ;-- No. Insert the string.
          invoke SendMessage, hWnd, CB_INSERTSTRING, 0, OFFSET Buffer
    
      .ELSE
    
          ;-----------------------------------------------------------------------
          ;-- Yes. Move the string to the top.
          invoke SendMessage, hWnd, CB_DELETESTRING, eax, NULL
          invoke SendMessage, hWnd, CB_INSERTSTRING, 0, OFFSET Buffer
    
      .ENDIF
    
      invoke SendMessage, hWnd, CB_SETCURSEL, 0, NULL
      
  .ENDIF

  mov    eax, 1


@ReturnWithoutError:
  ret

@ReturnWithError:
  xor eax, eax
  ret
GetInitializationValue ENDP



;-----------------------------------------------------------------------------------------
; Gets the text of a window and converts it to an integer value. When this
; procedure fails the carry flag is set, otherwise it's cleared.
;-----------------------------------------------------------------------------------------

GetIntegerFromWindowText PROC hParent:HWND, hWnd:HWND, lpErrStr_ValueToLarge:DWORD,
                              lpErrStr_InvalidValue:DWORD

  LOCAL tempBuffer[11]:BYTE


  ;---------------------------------------------------------------------------------------
  ;-- Get the text of the window.
  invoke SendMessage, hWnd, WM_GETTEXT, 11, ADDR tempBuffer


  ;---------------------------------------------------------------------------------------
  ;-- Check the number of bytes copied.
  cmp    eax, 10
  jae    @ERR_ValueToLarge


  ;---------------------------------------------------------------------------------------
  ;-- Does the string contain invalid chars?
  lea    eax, tempBuffer
  dec    eax

@NextChar:
    inc    eax
    mov    bl, BYTE PTR [eax]

    ;-------------------------------------------------------------------------------------
    ;-- The char has to be from '0' to '9' to be valid (except the
    ;-- NULL-byte).
    cmp    bl, '0'
    jb     @OutOfRange
    cmp    bl, '9'
  jbe    @NextChar


  ;---------------------------------------------------------------------------------------
  ;-- Is the char the NULL-byte or an invalid char?
@OutOfRange:
  test   bl, bl
  jnz    @ERR_InvalidValue


  ;---------------------------------------------------------------------------------------
  ;-- The char is the NULL-byte. Convert the string to a 32-bit integer.
  invoke ustr2dw, ADDR tempBuffer


  ;---------------------------------------------------------------------------------------
  ;-- The value must not be 0.
  test   eax, eax
  jz     @ERR_InvalidValue


  clc
  ret


@ERR_ValueToLarge:
  ;---------------------------------------------------------------------------------------
  ;-- The string is to long, that means the number is to large.
  invoke MessageBox, hParent, lpErrStr_ValueToLarge, lpSTR_TITLE_ERROR, MB_OK or MB_ICONHAND

  stc
  ret

@ERR_InvalidValue:
  ;---------------------------------------------------------------------------------------
  ;-- The string is invalid.
  invoke MessageBox, hParent, lpErrStr_InvalidValue, lpSTR_TITLE_ERROR, MB_OK or MB_ICONHAND

  stc
  ret
GetIntegerFromWindowText ENDP


END Start

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -