汇编排序算法代码总结

1 冒泡排序

http://blog.csdn.net/a123443/article/details/6779137

;冒泡排序
;author JRH
;2011.7.10
assume ds:data
data segment
  a dw 1,4,2,5,7,9,6,3
data ends
code segment
start:
     mov ax,data
     mov ds,ax
     mov cx,8 
     dec cx
    lop1:
         push cx
         mov dx,0
         mov si,0
         lop2:  
         mov bp,a[si]
         cmp bp,a[si+2]
         jnb go_on
         xchg bp,a[si+2]
         mov a[si],bp
         mov dx,1 ;标志位
      go_on:
         add si,2
         loop lop2
         pop cx
         cmp dx,0 
         jz over
    loop lop1
      over:
      mov ax,4c00h
      int 21h
code ends
end start

以下代码来自RadASM自带库;

2 递归排序

assort.asm

    .486                      ; force 32 bit code
    .model flat, stdcall      ; memory model & calling convention
    option casemap :none      ; case sensitive

    asqsort PROTO :DWORD,:DWORD,:DWORD,:DWORD,:DWORD
    acisort PROTO :DWORD,:DWORD

    .code

assort proc arr:DWORD,cnt:DWORD,rdi:DWORD

    .if rdi == 0
      mov rdi, 100                      ; set default maximum recursion depth
    .endif

    mov eax, cnt
    sub eax, 1

    invoke asqsort,arr,0,eax,0,rdi
    xor eax, eax                        ; exit with EAX = 0 is quick sorted
    test edx, edx                       ; if EDX = 0 quick sort failed by
    jnz @F                              ; exceeding maximum recursion depth
    invoke acisort,arr,cnt
    mov eax, 1                          ; exit with EAX = 1 is CISORTed
  @@:

    ret

assort endp

end

rdi为最大递归深度

本过程调用了asqsort和acisort

3 快速排序

asqsort.asm

    .486                      ; force 32 bit code
    .model flat, stdcall      ; memory model & calling convention
    option casemap :none      ; case sensitive
 
    .code

asqsort proc arr:DWORD,i:DWORD,j:DWORD,rec:DWORD,ind:DWORD

    test edx, edx
    jz quit

    mov eax, i
    cmp eax, j
    jge quit

    add rec, 1                  ; increment recursion depth indicator

    mov eax, rec
    cmp eax, ind
    jl @F
    xor edx, edx                ; set bailout indicator
    jmp quit


  @@:
    mov [esp-8], ebx
    mov [esp-12], esi
    mov [esp-16], edi

    mov edi, arr
    mov esi, i
    mov ebx, j
    mov [esp-20], ebp           ; save EBP
    mov edx, [edi+ebx*4]        ; pivot is from higher index value
    sub ebx, 1
    cmp esi, ebx
    jg lbl5

  lbl0:
    sub esi, 1
  align 4
  lbl1:
    add esi, 1                  ; increment lower index
  ; ***************************
    mov ecx, [edi+esi*4]        ; load lower index string
    mov ebp, -1
  sts0:
    add ebp, 1
    mov al, [ecx+ebp]           ; compare lower and pivot strings
    cmp al, [edx+ebp]
    jg nxt0
    jl lbl1
    test al, al
    jnz sts0
  nxt0:
  ; ***************************
  align 4
  lbl2:
  ; ***************************
    mov ecx, [edi+ebx*4]        ; load upper index string
    mov ebp, -1
  sts1:
    add ebp, 1
    mov al, [ecx+ebp]           ; compare upper and pivot strings
    cmp al, [edx+ebp]
    jg nxt1
    jl lbl3
    test al, al
    jnz sts1
  nxt1:
  ; ***************************
    sub ebx, 1                  ; decrement upper index
    cmp ebx, esi
    jge lbl2

  lbl3:
    cmp esi, ebx
    jg lbl5
    mov eax, [edi+esi*4]        ; swap pointers
    mov ecx, [edi+ebx*4]
    mov [edi+esi*4], ecx
    mov [edi+ebx*4], eax
    add esi, 1
    sub ebx, 1
    cmp esi, ebx
    jle lbl0

  lbl5:
    mov ebp, [esp-20]           ; restore EBP before using LOCAL
    mov eax, j
    mov edx, [edi+esi*4]
    mov ecx, [edi+eax*4]
    mov [edi+esi*4], ecx
    mov [edi+eax*4], edx
    mov eax, esi

    mov ebx, [esp-8]
    mov esi, [esp-12]
    mov edi, [esp-16]

    push eax
    sub eax, 1
    invoke asqsort,arr,i,eax,rec,ind

  @@:
    pop eax
    add eax, 1
    invoke asqsort,arr,eax,j,rec,ind

  quit:

    ret

asqsort endp

end

快速排序(Quicksort)是对冒泡排序的一种改进。

它的基本思想是:通过一趟排序将要排序的数据分割成独立的两部分,其中一部分的所有数据都比另外

一部分的所有数据都要小,然后再按此方法对这两部分数据分别进行快速排序,整个排序过程可以递归

进行,以此达到整个数据变成有序序列。

输入参数为i,j,rec,ind;四个DWORD类型;

如果edx为0直接退出;

i小于j直接退出;

JGE:如果大于或等于(>=)则跳转

rec:递归深度指示变量

首先把ebx,esi,edi的值存入堆栈;

数组首址是arr,放入edi;

在lbl5段和@@段中调用了自身;

4 acisort

acisort.asm

    .486                      ; force 32 bit code
    .model flat, stdcall      ; memory model & calling convention
    option casemap :none      ; case sensitive

    include \masm32\macros\macros.asm

    aissort PROTO :DWORD,:DWORD

    .code

align 4

acisort proc arr:DWORD,cnt:DWORD

    LOCAL gap   :DWORD
    LOCAL sflag :DWORD
    LOCAL hflag :DWORD
    LOCAL eflag :DWORD

    push ebx
    push esi
    push edi

    mov eax, cnt
    mov gap, eax                ; copy cnt to gap
    mov ebx, arr                ; address of 1st element
    dec cnt
  ; --------------------------------------------------
  ;        bi-directional COMB preordering pass
  ; --------------------------------------------------
  setgap:
    fild gap                    ; load integer memory operand to divide
    fdiv FP8(1.35)              ; divide gap by constant
    fistp gap                   ; store result back in integer memory operand
    sub gap, 1                  ; round down by 1

    cmp gap, 10
    jg @F
    cmp gap, 9                  ; comb 11 code
    jl @F
    mov gap, 11
  @@:

    cmp gap, 1
    jle nxt                     ; exit when gap <= 1
    mov edi, cnt
    sub edi, gap
    mov eflag, edi
    xor ecx, ecx                ; low value index
  inner:
    mov edx, ecx
    add edx, gap                ; high value index
    push ebp
    mov ebp, [ebx+ecx*4]        ; lower value
    mov esi, [ebx+edx*4]        ; swap values
    mov edi, -1
  ; ===========================================
  cmpstrings:
    add edi, 1
    mov al, [ebp+edi]           ; compare both strings
    cmp al, [esi+edi]
    jl noswap                   ; ascending sort
    jg swap                     ; swap these two labels for descending
    test al, al
    jnz cmpstrings
  ; ===========================================
    jmp noswap
  swap:
    mov [ebx+edx*4], ebp
    mov [ebx+ecx*4], esi
  noswap:
    pop ebp
    add ecx, 1
    cmp ecx, eflag
    jle inner
  ; *******************************
    fild gap                    ; load integer memory operand to divide
    fdiv FP8(1.4)               ; divide gap by constant
    fistp gap                   ; store result back in integer memory operand
    sub gap, 1                  ; round down by 1

    cmp gap, 10
    jg @F
    cmp gap, 9                  ; comb 11 code
    jl @F
    mov gap, 11
  @@:

    cmp gap, 1
    jle nxt                     ; exit when gap <= 1
    mov ecx, cnt
    sub ecx, gap                ; calculate ECX as cnt - gap
  rinner:
    mov edx, ecx
    add edx, gap                ; high value index
    push ebp
    mov ebp, [ebx+ecx*4]        ; lower value
    mov esi, [ebx+edx*4]        ; swap values
    mov edi, -1
  ; ===========================================
  rcmpstrings:
    add edi, 1
    mov al, [ebp+edi]           ; compare both strings
    cmp al, [esi+edi]
    jl rnoswap                  ; ascending sort
    jg rswap                    ; swap these two labels for descending
    test al, al
    jnz rcmpstrings
  ; ===========================================
    jmp rnoswap
  rswap:
    mov [ebx+edx*4], ebp
    mov [ebx+ecx*4], esi
  rnoswap:
    pop ebp
    sub ecx, 1
    jnz rinner
    jmp setgap
  nxt:

    inc cnt
    invoke aissort,arr,cnt      ; call the insertion sort to clean up the rest

  done:
    pop edi
    pop esi
    pop ebx

    ret

acisort endp

end

输入参数:arr,cnt;数组和个数;

本地变量:gap,sflag,hflag,eflat;三个标志变量;

调用了插入排序(aissort)来清除rest;

rcmpstrings段:

比较ebp+edi指向的值和esi+edi指向的值,

小于转到rnoswap

大于转到rswap

rswap-交换:

把ebp存入ebx+edx*4的地址;

把esi存入ebx+ecx*4的地址;

5 插入排序(升序)

aissort.asm

    .486                      ; force 32 bit code
    .model flat, stdcall      ; memory model & calling convention
    option casemap :none      ; case sensitive

    .code

OPTION PROLOGUE:NONE 
OPTION EPILOGUE:NONE 

align 4

aissort proc arr:DWORD,cnt:DWORD

  ; -------------------------------
  ; ascending insertion string sort
  ; -------------------------------
    mov [esp-8],  ebx
    mov [esp-12], esi
    mov [esp-16], edi
    mov [esp-20], ebp

    mov edi, [esp+4]            ; array address
    mov edx, 1
    cmp edx, [esp+8]            ; compare count to EDX
    jge quit
    xor eax, eax                ; clear EAX of previous content

  entry:
    mov ebx, [edi+edx*4]
    mov esi, edx

  inner:
    mov ecx, [edi+esi*4-4]
    mov ebp, -1
  stcmp:                        ; string comparison loop
    add ebp, 1
    mov al, [ecx+ebp]
    cmp al, [ebx+ebp]
    jl outer
    jg swap
    test al, al
    jnz stcmp

    jmp outer

  swap:
    mov [edi+esi*4], ecx
    sub esi, 1
    jnz inner

  outer:
    mov [edi+esi*4], ebx
    add edx, 1
    cmp edx, [esp+8]            ; compare count to EDX
    jl entry

  quit:
    mov ebx, [esp-8]
    mov esi, [esp-12]
    mov edi, [esp-16]
    mov ebp, [esp-20]

    ret 8

aissort endp

OPTION PROLOGUE:PrologueDef 
OPTION EPILOGUE:EpilogueDef 

end
    .486                      ; force 32 bit code
    .model flat, stdcall      ; memory model & calling convention
    option casemap :none      ; case sensitive

    .code

OPTION PROLOGUE:NONE 
OPTION EPILOGUE:NONE 

align 4

aissort proc arr:DWORD,cnt:DWORD

  ; -------------------------------
  ; ascending insertion string sort
  ; -------------------------------
    mov [esp-8],  ebx
    mov [esp-12], esi
    mov [esp-16], edi
    mov [esp-20], ebp

    mov edi, [esp+4]            ; array address
    mov edx, 1
    cmp edx, [esp+8]            ; compare count to EDX
    jge quit
    xor eax, eax                ; clear EAX of previous content

  entry:
    mov ebx, [edi+edx*4]
    mov esi, edx

  inner:
    mov ecx, [edi+esi*4-4]
    mov ebp, -1
  stcmp:                        ; string comparison loop
    add ebp, 1
    mov al, [ecx+ebp]
    cmp al, [ebx+ebp]
    jl outer
    jg swap
    test al, al
    jnz stcmp

    jmp outer

  swap:
    mov [edi+esi*4], ecx
    sub esi, 1
    jnz inner

  outer:
    mov [edi+esi*4], ebx
    add edx, 1
    cmp edx, [esp+8]            ; compare count to EDX
    jl entry

  quit:
    mov ebx, [esp-8]
    mov esi, [esp-12]
    mov edi, [esp-16]
    mov ebp, [esp-20]

    ret 8

aissort endp

OPTION PROLOGUE:PrologueDef 
OPTION EPILOGUE:EpilogueDef 

end

数组地址存入edi;

比较次数存入edx;

stcmp-字符串比较循环:比较的是ecx+ebp地址处的值和ebx+ebp地址处的值

6 插入排序(降序)

    .486                      ; force 32 bit code
    .model flat, stdcall      ; memory model & calling convention
    option casemap :none      ; case sensitive

    .code

OPTION PROLOGUE:NONE 
OPTION EPILOGUE:NONE 

align 4

dissort proc arr:DWORD,cnt:DWORD

  ; --------------------------------
  ; descending insertion string sort
  ; --------------------------------
    mov [esp-8],  ebx
    mov [esp-12], esi
    mov [esp-16], edi
    mov [esp-20], ebp

    mov edi, [esp+4]            ; array address
    mov edx, 1
    cmp edx, [esp+8]            ; compare count to EDX
    jge quit
    xor eax, eax                ; clear EAX of previous content

  entry:
    mov ebx, [edi+edx*4]
    mov esi, edx

  inner:
    mov ecx, [edi+esi*4-4]
    mov ebp, -1
  stcmp:                        ; string comparison loop
    add ebp, 1
    mov al, [ecx+ebp]
    cmp al, [ebx+ebp]
    jg outer
    jl swap
    test al, al
    jnz stcmp

    jmp outer

  swap:
    mov [edi+esi*4], ecx
    sub esi, 1
    jnz inner

  outer:
    mov [edi+esi*4], ebx
    add edx, 1
    cmp edx, [esp+8]            ; compare count to EDX
    jl entry

  quit:
    mov ebx, [esp-8]
    mov esi, [esp-12]
    mov edi, [esp-16]
    mov ebp, [esp-20]

    ret 8

dissort endp

OPTION PROLOGUE:PrologueDef 
OPTION EPILOGUE:EpilogueDef 

end

    原文作者:排序算法
    原文地址: https://blog.csdn.net/bcbobo21cn/article/details/51602570
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞