bubblesort.asm
来自「一个演示了用汇编语言编写的数据排序算法,源代码非常详细」· 汇编 代码 · 共 128 行
ASM
128 行
; Date: 10.03.2004
; last modification: 28.03.2004
.code
IFNDEF PURE_ALGORITHM
BubbleSort PROTO :DWORD, :DWORD
BubbleSort PROC Arr:DWORD, count:DWORD
mov esi, Arr
mov ebx, esi
lea edi, [ebx - 4]
mov ecx, count
lea ebx, [ebx + ecx*4]
@Loop1:
sub ebx, 4
cmp ebx, esi
jna @Return
;-------------------------------------------------------------------------------
DelayAfterPass
;-------------------------------------------------------------------------------
mov eax, edi
@Loop2:
add eax, 4
cmp eax, ebx
jnb @Loop1
;-------------------------------------------------------------------------------
IncCMPCounter
push ebx
lea ebx, [eax + 4]
DelayAfterComparison eax, ebx
pop ebx
;-------------------------------------------------------------------------------
mov edx, DWORD PTR [eax + 4]
cmp DWORD PTR [eax], edx
jna @Loop2
mov ecx, DWORD PTR [eax]
mov DWORD PTR [eax], edx
mov DWORD PTR [eax + 4], ecx
;-------------------------------------------------------------------------------
IncMOVCounter
RefreshElement eax
push eax
add eax, 4
RefreshElement eax
pop eax
DelayAfterExchange
;-------------------------------------------------------------------------------
jmp @Loop2
@Return:
ret
BubbleSort ENDP
ELSE
BubbleSort_PURE PROTO :DWORD, :DWORD
BubbleSort_PURE PROC Arr:DWORD, count:DWORD
mov esi, Arr
mov ebx, esi
lea edi, [ebx - 4]
mov ecx, count
lea ebx, [ebx + ecx*4]
@Loop1:
sub ebx, 4
cmp ebx, esi
jna @Return
mov eax, edi
@Loop2:
add eax, 4
cmp eax, ebx
jnb @Loop1
;-------------------------------------------------------------------------------
IncCMPCounter
;-------------------------------------------------------------------------------
mov edx, DWORD PTR [eax + 4]
cmp DWORD PTR [eax], edx
jna @Loop2
mov ecx, DWORD PTR [eax]
mov DWORD PTR [eax], edx
mov DWORD PTR [eax + 4], ecx
;-------------------------------------------------------------------------------
IncMOVCounter
;-------------------------------------------------------------------------------
jmp @Loop2
@Return:
ret
BubbleSort_PURE ENDP
ENDIF
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?