-
-
[旧帖] [原创]一个堆排序的简单实现(masm版本) 0.00雪花
-
2012-6-6 21:07 1725
-
前几天发了同题的一篇帖子,给出了c语言版本的堆排序,今天给出masm版本,聊作练习
.386
.model flat, stdcall
include windows.inc
include user32.inc
include kernel32.inc
includelib kernel32.lib
includelib user32.lib
includelib msvcrt.lib
printf PROTO C :VARARG
getchar proto C
.data
array dd 0 ,4,1,3,2,16,9,10,14,8,7
szMessage db "%d ",0
.code
start:
call main
call getchar
main proc
LOCAL i:DWORD
LOCAL j:DWORD
LOCAL len:DWORD
xor edx, edx
mov eax, sizeof array
mov bx, sizeof DWORD
div bx
mov len, eax
mov eax, len
mov bx, 2
div bx
mov i, eax
.while i >= 1
push len
push i
call HeapAdjust
dec i
.endw
mov eax, len
dec eax
mov j, eax
.while j>=1
invoke printf, offset szMessage, array[4]
mov ecx, j
mov eax, array[ecx*4]
mov array[4], eax
push j
push 1
call HeapAdjust
dec j
.endw
ret
main endp
HeapAdjust proc i:DWORD, len:DWORD
LOCAL left:DWORD
LOCAL right:DWORD
LOCAL max:DWORD
mov eax, i
shl eax,1
mov left, eax
mov ebx,i
shl ebx,1
add ebx,1
mov right, ebx
mov eax, left
mov ebx, i
shl ebx, 2
mov ecx, array[ebx]
.if ((eax < len) && (array[eax*4] > ecx))
mov eax, left
mov max, eax
.else
mov eax, i
mov max, eax
.endif
mov eax, right
mov ebx, max
mov ecx, array[ebx*4]
.if ((eax < len) && (array[eax*4] > ecx))
mov eax, right
mov max, eax
.endif
mov eax, max
.if eax != i
mov ecx, i
mov eax, array[ecx*4]
mov edx, max
mov ebx, array[edx*4]
mov array[ecx*4], ebx
mov array[edx*4], eax
push len
push max
call HeapAdjust
.endif
ret
HeapAdjust endp
end start
.386
.model flat, stdcall
include windows.inc
include user32.inc
include kernel32.inc
includelib kernel32.lib
includelib user32.lib
includelib msvcrt.lib
printf PROTO C :VARARG
getchar proto C
.data
array dd 0 ,4,1,3,2,16,9,10,14,8,7
szMessage db "%d ",0
.code
start:
call main
call getchar
main proc
LOCAL i:DWORD
LOCAL j:DWORD
LOCAL len:DWORD
xor edx, edx
mov eax, sizeof array
mov bx, sizeof DWORD
div bx
mov len, eax
mov eax, len
mov bx, 2
div bx
mov i, eax
.while i >= 1
push len
push i
call HeapAdjust
dec i
.endw
mov eax, len
dec eax
mov j, eax
.while j>=1
invoke printf, offset szMessage, array[4]
mov ecx, j
mov eax, array[ecx*4]
mov array[4], eax
push j
push 1
call HeapAdjust
dec j
.endw
ret
main endp
HeapAdjust proc i:DWORD, len:DWORD
LOCAL left:DWORD
LOCAL right:DWORD
LOCAL max:DWORD
mov eax, i
shl eax,1
mov left, eax
mov ebx,i
shl ebx,1
add ebx,1
mov right, ebx
mov eax, left
mov ebx, i
shl ebx, 2
mov ecx, array[ebx]
.if ((eax < len) && (array[eax*4] > ecx))
mov eax, left
mov max, eax
.else
mov eax, i
mov max, eax
.endif
mov eax, right
mov ebx, max
mov ecx, array[ebx*4]
.if ((eax < len) && (array[eax*4] > ecx))
mov eax, right
mov max, eax
.endif
mov eax, max
.if eax != i
mov ecx, i
mov eax, array[ecx*4]
mov edx, max
mov ebx, array[edx*4]
mov array[ecx*4], ebx
mov array[edx*4], eax
push len
push max
call HeapAdjust
.endif
ret
HeapAdjust endp
end start
[培训]《安卓高级研修班(网课)》月薪三万计划,掌握调试、分析还原ollvm、vmp的方法,定制art虚拟机自动化脱壳的方法
赞赏
他的文章
看原图