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

📄 createelements.asm

📁 一个演示了用汇编语言编写的数据排序算法,源代码非常详细
💻 ASM
字号:

;-----------------------------------------------------------------------------------------
; Prototypes.
;-----------------------------------------------------------------------------------------

  CreateUnsortedElements PROTO :DWORD, :DWORD, :DWORD, :DWORD, :DWORD
  CreateAscElements      PROTO :DWORD, :DWORD
  CreateDescElements     PROTO :DWORD, :DWORD
  CreateElements         PROTO


.code

;-----------------------------------------------------------------------------------------
; Purpose:     Fills an array with double word values according to the current settings
;              in the SortInfo-structure.
;
; Inputs:      none
;
; Outputs:     an array of double word values (pointer is SortInfo.lpElements)
;
; Notes:       The members of the SortInfo-structure have to be valid.
;-----------------------------------------------------------------------------------------

CreateElements PROC

  ;---------------------------------------------------------------------------------------
  ;-- Shall the elements be created with a certain presorting?
  mov    al, SortInfo.direction
  .IF al == 1

      ;-----------------------------------------------------------------------------------
      ;-- Sorted ascending.
      invoke CreateAscElements, SortInfo.lpElements, SortInfo.numElements

  .ELSEIF al == 2

      ;-----------------------------------------------------------------------------------
      ;-- Sorted descending.
      invoke CreateDescElements, SortInfo.lpElements, SortInfo.numElements

  .ELSE

      ;-----------------------------------------------------------------------------------
      ;-- Save the correct value for 'unsorted elements'.
      mov    SortInfo.direction, 0


      ;-----------------------------------------------------------------------------------
      ;-- The elements shall be unsorted.
      invoke CreateUnsortedElements, SortInfo.randInitValue,
                                     SortInfo.lpElements, SortInfo.numElements,
                                     SortInfo.minValue, SortInfo.maxValue

  .ENDIF

@Return:
  ret
CreateElements ENDP



;-----------------------------------------------------------------------------------------
; Purpose:     Fills an array with random double word values according to the current
;              settings in the SortInfo-structure.
;
; Inputs:      initValue - initialization value for the random number generator
;              lpArray   - offset to an array of double words to save the generated values
;              countElements - the size of the array in double words (= number of
;                          double word values to generate)
;              min, max  - the range for the random values
; 
; Outputs:     an array of random double word values
;
; Notes:       The members of the SortInfo-structure have to be valid.
;-----------------------------------------------------------------------------------------

CreateUnsortedElements PROC initValue:DWORD, lpArray:DWORD, countElements:DWORD,
                            min:DWORD, max:DWORD

  LOCAL maxRelative :DWORD
  LOCAL elementsArr[128]:BYTE

  ;---------------------------------------------------------------------------------------
  ;-- Initialize the random number generator.
  invoke nseed, initValue


  ;---------------------------------------------------------------------------------------
  ;-- countElements has to be greater than 0.
  mov    ebx, countElements
  dec    ebx
  js     @Return


  ;---------------------------------------------------------------------------------------
  ;-- If min is greater than max return.
  mov    eax, min
  cmp    eax, max
  ja     @Return


  ;---------------------------------------------------------------------------------------
  ;-- If the current dialog is the graphic dialog then each value can exist only once.
  .IF lpArray == OFFSET ElementsArr
      lea    edi, elementsArr
      mov    ecx, 127/4
    @@:
        mov    DWORD PTR [edi + ecx*4], NULL
        dec    ecx
        jns    @B
  .ENDIF


  ;---------------------------------------------------------------------------------------
  ;-- Generate the values and store them in the array.
  mov    ecx, lpArray


  ;---------------------------------------------------------------------------------------
  ;-- Compute a relative max value (sub min from max).
  mov    edx, max
  inc    edx
  sub    edx, min
  mov    maxRelative, edx

  @nextElement:

    push   ebx
    push   ecx

      ;-----------------------------------------------------------------------------------
      ;-- Generate a value (has to be <= maxRelative).
      invoke nrandom, maxRelative


      ;-----------------------------------------------------------------------------------
      ;-- Add min to get the real value.
      add    eax, min

    pop    ecx
    pop    ebx


    ;-------------------------------------------------------------------------------------
    ;-- If the current dialog is the graphic dialog then each value can exist only once.
    cmp    lpArray, OFFSET ElementsArr
    jne    @F

    lea    edi, elementsArr
    cmp    BYTE PTR [edi + eax], NULL
    jnz    @nextElement
    
    mov    BYTE PTR [edi + eax], TRUE


    ;-------------------------------------------------------------------------------------
    ;-- Store the generated value.
  @@:
    mov    DWORD PTR [ecx + ebx*4], eax


    ;---------------------------------------------------------------------------------------
    ;-- Otherwise decrement the counter and create the next value if the counter is >= 0.
    dec    ebx
    jns    @nextElement


@Return:
  ret
CreateUnsortedElements ENDP



;-----------------------------------------------------------------------------------------
; Purpose:     Fills an array with ascending double word values. The first value
;              is 0, the last one is countElements minus 1.
;
; Inputs:      lpArray   - offset to an array of double words to save the generated values
;              countElements - the size of the array in double words (= number of
;                          double word values to generate)
; 
; Outputs:     an array of ascending double word values
;
; Notes:       none
;-----------------------------------------------------------------------------------------

CreateAscElements PROC lpArray:DWORD, countElements:DWORD,

  ;---------------------------------------------------------------------------------------
  ;-- countElements has to be greater than 0.
  mov    eax, countElements
  dec    eax
  js     @Return


  ;---------------------------------------------------------------------------------------
  ;-- Generate the values and store them in the array.
  mov    ebx, lpArray

  @nextElement:
    ;-------------------------------------------------------------------------------------
    ;-- Store the value.
    mov    DWORD PTR [ebx + eax*4], eax


    ;---------------------------------------------------------------------------------------
    ;-- Otherwise decrement the counter and store the next value if the counter is >= 0.
    dec    eax
    jns    @nextElement


@Return:
  ret
CreateAscElements ENDP



;-----------------------------------------------------------------------------------------
; Purpose:     Fills an array with descending double word values. The first value
;              is countElements minus 1, the last one is 0.
;
; Inputs:      lpArray   - offset to an array of double words to save the generated values
;              countElements - the size of the array in double words (= number of
;                          double word values to generate)
; 
; Outputs:     an array of descending double word values
;
; Notes:       none
;-----------------------------------------------------------------------------------------

CreateDescElements PROC lpArray:DWORD, countElements:DWORD

  ;---------------------------------------------------------------------------------------
  ;-- countElements has to be greater than 0.
  mov    eax, countElements
  dec    eax
  js     @Return


  ;---------------------------------------------------------------------------------------
  ;-- Generate the values and store them in the array.
  mov    ebx, lpArray
  xor    edx, edx


  @nextElement:
    ;-------------------------------------------------------------------------------------
    ;-- Store the value.
    mov    DWORD PTR [ebx + eax*4], edx


    ;---------------------------------------------------------------------------------------
    ;-- Otherwise increment the value, decrement the counter and store the next value if
    ;-- the counter is >= 0.
    inc    edx
    dec    eax
    jns    @nextElement


@Return:
  ret
CreateDescElements ENDP

⌨️ 快捷键说明

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