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

📄 sorting.asm

📁 一个演示了用汇编语言编写的数据排序算法,源代码非常详细
💻 ASM
📖 第 1 页 / 共 3 页
字号:
      .ENDIF

      invoke szCopy, ebx, eax
      invoke MessageBox, hCurrentDialog, ADDR localBuffer, lpSTR_TITLE_ERROR,
                         MB_OK or MB_ICONEXCLAMATION

  .ENDIF


@Return:
  ret
StartAlgorithm ENDP



;-----------------------------------------------------------------------------------------
; Handles the exceptions provoked by the sorting algorithm.
;-----------------------------------------------------------------------------------------

ExceptionHandler PROC C lpException:DWORD, lpFrame:DWORD, lpContext:DWORD, lpDispatch:DWORD

  push   ebx

  mov    eax, lpContext
  mov    ebx, SEH.SaveEip
  mov    [eax][CONTEXT._Eip], ebx
  mov    ebx, SEH.OrgEsp
  mov    [eax][CONTEXT._Esp], ebx
  mov    ebx, SEH.OrgEbp
  mov    [eax][CONTEXT._Ebp], ebx

  mov    eax, lpException
  mov    eax, [eax][EXCEPTION_RECORD.ExceptionCode]
  .IF eax == EXCEPTION_STACK_OVERFLOW

      mov    SortInfo.errorId, ERROR_STACK_OVERFLOW
      xor    eax, eax

  .ELSE

      mov    eax, EXCEPTION_CONTINUE_EXECUTION

  .ENDIF

@Return:
  pop    ebx
  ret
ExceptionHandler ENDP



;-----------------------------------------------------------------------------------------
; Purpose:     Ascertains and executes the selected sorting algorithms and displays
;              the results in the result list view if the active dialog is the tabular
;              dialog.
;
; Inputs:      lpParam - TRUE = execute the algorithm(s); FALSE = restore all texts and
;                        free memory that has been allocated (this value is given when
;                        a sorting process is canceled)
;
; Outputs:     none
;
; Notes:       none
;-----------------------------------------------------------------------------------------

SortingThread PROC lpParam:DWORD

  LOCAL itemID  :DWORD
  LOCAL counter :DWORD
  LOCAL localBuffer[256] :BYTE

  ;---------------------------------------------------------------------------------------
  ;-- Initialize the SortInfo-structure if the algorithm(s) shall be executed.
  .IF lpParam
      invoke InitSortInfoStruct, TRUE
  .ENDIF


  .IF SortInfo.fGraphic
      ;-----------------------------------------------------------------------------------
      ;-- The graphic dialog is active.

  cmp lpParam, FALSE
  je  @CleanUpGraphicDialog


      ;-----------------------------------------------------------------------------------
      ;-- Change the text of the start button.
      invoke SendMessage, hGraphicDialog_B_Start, WM_SETTEXT, NULL, lpSTR_B_CANCEL


      ;-----------------------------------------------------------------------------------
      ;-- Deactivate some combo boxes.
      invoke EnableWindow, hGraphicDialog_CB_Algorithm, FALSE
      invoke EnableWindow, hGraphicDialog_CB_Elements, FALSE
      invoke EnableWindow, hGraphicDialog_CB_Data, FALSE
      invoke EnableWindow, hGraphicDialog_CB_InitValue2, FALSE


      ;-----------------------------------------------------------------------------------
      ;-- Deactivate the button that is used to generate new elements.
      invoke EnableWindow, hGraphicDialog_B_Generate, FALSE


      ;-----------------------------------------------------------------------------------
      ;-- Get the selected item of the algorithm list view.
      invoke SendMessage, hGraphicDialog_CB_Algorithm, CB_GETCURSEL, NULL, NULL


      ;-----------------------------------------------------------------------------------
      ;-- Show the algorithm title in the status bar.
      push   eax
        mov    eax, DWORD PTR [eax*8 + OFFSET AlgorithmsInfo]
        invoke SendMessage, hGraphicDialog_SB, SB_SETTEXT, SB_PART_ALGORITHM, eax
      pop    eax


      ;-----------------------------------------------------------------------------------
      ;-- Load the algorithm procedure offset.
      mov    eax, DWORD PTR [eax*8 + 4 + OFFSET AlgorithmsInfo]


      ;-----------------------------------------------------------------------------------
      ;-- Start the algorithm.
      invoke StartAlgorithm, eax


      ;-----------------------------------------------------------------------------------
      ;-- Paint the elements.
    @CleanUpGraphicDialog:
      invoke PaintElements, -1, NULL, FALSE


      ;-----------------------------------------------------------------------------------
      ;-- Change the text of the start button.
      invoke SendMessage, hGraphicDialog_B_Start, WM_SETTEXT, NULL, lpSTR_B_SORT


      ;-----------------------------------------------------------------------------------
      ;-- Activate some combo boxes.
      invoke EnableWindow, hGraphicDialog_CB_Algorithm, TRUE
      invoke EnableWindow, hGraphicDialog_CB_Elements, TRUE
      invoke EnableWindow, hGraphicDialog_CB_Data, TRUE
      invoke EnableWindow, hGraphicDialog_CB_InitValue2, TRUE


      ;-----------------------------------------------------------------------------------
      ;-- Activate the button that is used to generate new elements.
      invoke EnableWindow, hGraphicDialog_B_Generate, TRUE

  .ELSE
      ;-----------------------------------------------------------------------------------
      ;-- The tabular dialog is active.

  cmp lpParam, FALSE
  je  @CleanUpTableDialog


      ;-----------------------------------------------------------------------------------
      ;-- Change the text of the start button.
      invoke SendMessage, hTableDialog_B_Start, WM_SETTEXT, NULL, lpSTR_B_CANCEL


      ;-----------------------------------------------------------------------------------
      ;-- Deactivate some combo boxes.
      invoke EnableWindow, hTableDialog_CB_Elements, FALSE
      invoke EnableWindow, hTableDialog_CB_Data, FALSE
      invoke EnableWindow, hTableDialog_CB_InitValue, FALSE


      ;-----------------------------------------------------------------------------------
      ;-- Get the number of list view items.
      invoke SendMessage, hTableDialog_LV_Algorithms, LVM_GETITEMCOUNT, NULL, NULL
      mov    ecx, eax


      ;-----------------------------------------------------------------------------------
      ;-- Search the checked items.
      mov    itemID, 0
      mov    counter, 0
    @NextItem:
        push   ecx

          ;-------------------------------------------------------------------------------
          ;-- Get the status of the current item.
          invoke SendMessage, hTableDialog_LV_Algorithms, LVM_GETITEMSTATE, itemID, 8192
          test   eax, eax
          jz     @F

          ;-------------------------------------------------------------------------------
          ;-- This algorithm is checked. Signalize that at least one algorithm is checked.
          mov    counter, 1


          ;-------------------------------------------------------------------------------
          ;-- Initialize the SortInfo-structure.
          invoke InitSortInfoStruct, TRUE


          ;-------------------------------------------------------------------------------
          ;-- Generate the elements and show a hint in the status bar.
          invoke SendMessage, hCurrentDialog, WM_APP + 1, itemID, NULL
          invoke CreateElements


          ;-------------------------------------------------------------------------------
          ;-- No. Load the algorithm procedure offset.
          mov    eax, itemID
          mov    eax, DWORD PTR [eax*4 + OFFSET AlgorithmsInfo_PURE]


          ;-------------------------------------------------------------------------------
          ;-- Start this algorithm.
          invoke StartAlgorithm, eax


          ;-------------------------------------------------------------------------------
          ;-- No. Get the number of items in the result list view.
          invoke SendMessage, hTableDialog_LV_Result, LVM_GETITEMCOUNT, NULL, NULL


          ;-------------------------------------------------------------------------------
          ;-- Insert some informations in the result list view.

            ;-----------------------------------------------------------------------------
            ;-- The algorithm title.
            mov    LVitem.imask, LVIF_TEXT
            mov    LVitem.iItem, eax
            mov    LVitem.iSubItem, NULL

            AddErrorString
            mov    ebx, eax
            mov    eax, itemID
            mov    eax, DWORD PTR [eax*8 + OFFSET AlgorithmsInfo]
            invoke szCopy, eax, ebx
            lea    eax, localBuffer
            mov    LVitem.pszText, eax

            invoke SendMessage, hTableDialog_LV_Result, LVM_INSERTITEM, NULL, OFFSET LVitem


            ;-----------------------------------------------------------------------------
            ;-- The number of elements.
            push eax
              mov    LVitem.imask, LVIF_TEXT
              mov    LVitem.iItem, eax
              mov    eax, LVcolIndex_Count
              mov    LVitem.iSubItem, eax

              AddErrorString
              invoke wsprintf, eax, OFFSET FCS_Integer, SortInfo.numElements
              lea    eax, localBuffer
              mov    LVitem.pszText, eax

              invoke SendMessage, hTableDialog_LV_Result, LVM_SETITEM, NULL, OFFSET LVitem
            pop eax


            ;-----------------------------------------------------------------------------
            ;-- The value of the comparison counter.
            push eax
              mov    LVitem.imask, LVIF_TEXT
              mov    LVitem.iItem, eax
              mov    eax, LVcolIndex_CMPCounter
              mov    LVitem.iSubItem, eax

              AddErrorString
              invoke QW2ASCII, OFFSET SortInfo.cmpCounter, eax
              lea    ebx, localBuffer
              mov    LVitem.pszText, ebx

              invoke SendMessage, hTableDialog_LV_Result, LVM_SETITEM, NULL, OFFSET LVitem
            pop eax


            ;-----------------------------------------------------------------------------
            ;-- The value of the exchange counter.
            push eax
              mov    LVitem.imask, LVIF_TEXT
              mov    LVitem.iItem, eax
              mov    eax, LVcolIndex_MOVCounter
              mov    LVitem.iSubItem, eax

              AddErrorString
              invoke QW2ASCII, OFFSET SortInfo.movCounter, eax
              lea    ebx, localBuffer
              mov    LVitem.pszText, ebx

              invoke SendMessage, hTableDialog_LV_Result, LVM_SETITEM, NULL, OFFSET LVitem
            pop eax


            ;-----------------------------------------------------------------------------
            ;-- The elapsed time.
            push eax
              mov    LVitem.imask, LVIF_TEXT
              mov    LVitem.iItem, eax
              mov    eax, LVcolIndex_Time
              mov    LVitem.iSubItem, eax

              AddErrorString
              invoke wsprintf, eax, OFFSET FCS_Integer, SortInfo.elapsedTime
              lea    ebx, localBuffer
              mov    LVitem.pszText, ebx

              invoke SendMessage, hTableDialog_LV_Result, LVM_SETITEM, NULL, OFFSET LVitem
            pop eax


            ;-----------------------------------------------------------------------------
            ;-- The additionally used memory.
            push eax
              mov    LVitem.imask, LVIF_TEXT
              mov    LVitem.iItem, eax
              mov    eax, LVcolIndex_Memory
              mov    LVitem.iSubItem, eax

              AddErrorString
              invoke wsprintf, eax, OFFSET FCS_Integer, SortInfo.usedMemory
              lea    ebx, localBuffer
              mov    LVitem.pszText, ebx

              invoke SendMessage, hTableDialog_LV_Result, LVM_SETITEM, NULL, OFFSET LVitem
            pop eax


            ;-----------------------------------------------------------------------------
            ;-- The presorting.
            push eax
              mov    LVitem.imask, LVIF_TEXT
              mov    LVitem.iItem, eax
              mov    eax, LVcolIndex_Data
              mov    LVitem.iSubItem, eax

              AddErrorString
              mov    bl, SortInfo.direction
              .IF bl == 1
                  ;-----------------------------------------------------------------------
                  ;-- Sorted ascending.
                  mov    ebx, lpSTR_SORTEDASC
              .ELSEIF bl == 2
                  ;-----------------------------------------------------------------------
                  ;-- Sorted descending.
                  mov    ebx, lpSTR_SORTEDDESC
              .ELSE
                  ;-----------------------------------------------------------------------
                  ;-- Unsorted.
                  mov    ebx, lpSTR_UNSORTED
              .ENDIF
              invoke szCopy, ebx, eax
              lea    eax, localBuffer
              mov    LVitem.pszText, eax

              invoke SendMessage, hTableDialog_LV_Result, LVM_SETITEM, NULL, OFFSET LVitem
            pop eax


            ;-----------------------------------------------------------------------------
            ;-- The initialiszation value for the random number generator.
            push eax
              mov    LVitem.imask, LVIF_TEXT
              mov    LVitem.iItem, eax
              mov    eax, LVcolIndex_InitValue
              mov    LVitem.iSubItem, eax

              AddErrorString
              invoke wsprintf, eax, OFFSET FCS_Integer, SortInfo.randInitValue
              lea    eax, localBuffer
              mov    LVitem.pszText, eax

              invoke SendMessage, hTableDialog_LV_Result, LVM_SETITEM, NULL, OFFSET LVitem
            pop eax


            ;-----------------------------------------------------------------------------
            ;-- Esure that the last entry is visible.
            invoke SendMessage, hTableDialog_LV_Result, LVM_ENSUREVISIBLE, eax, FALSE


      @@:
        pop    ecx

        ;---------------------------------------------------------------------------------
        ;-- Have all list view items been processed?
        inc    itemID
        cmp    itemID, ecx
        jb     @NextItem


      ;-----------------------------------------------------------------------------------
      ;-- Yes. Display an error message if no algorithm was selected.
      .IF !counter
          invoke MessageBox, hDialogTable, lpSTR_ERR_NOALGORITHMSELECTED, lpSTR_TITLE_ERROR, MB_OK or MB_ICONWARNING
      .ENDIF


      @fCancel:


      ;-----------------------------------------------------------------------------------
      ;-- Delete all texts in the status bar.
    @CleanUpTableDialog:
      invoke SendMessage, hCurrentDialog, WM_APP + 2, NULL, NULL


      ;-----------------------------------------------------------------------------------
      ;-- Free the allocated memory.
      invoke SysFreeString, SortInfo.lpElements


      ;-----------------------------------------------------------------------------------
      ;-- Change the text of the start button.
      invoke SendMessage, hTableDialog_B_Start, WM_SETTEXT, NULL, lpSTR_B_SORT


      ;-----------------------------------------------------------------------------------
      ;-- Activate some combo boxes.
      invoke EnableWindow, hTableDialog_CB_Elements, TRUE
      invoke EnableWindow, hTableDialog_CB_Data, TRUE
      invoke EnableWindow, hTableDialog_CB_InitValue, TRUE

  .ENDIF


  ;---------------------------------------------------------------------------------------
  ;-- The sorting process(es) is/are finished.
  invoke InitSortInfoStruct, FALSE


@Return:
  ret
SortingThread ENDP

⌨️ 快捷键说明

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