-
-
[旧帖] [原创]一个堆排序的简单实现(masm版本) 0.00雪花
-
发表于: 2012-6-6 21:07 2015
-
前几天发了同题的一篇帖子,给出了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
[注意]传递专业知识、拓宽行业人脉——看雪讲师团队等你加入!
赞赏
- [求助]科摩多的病毒分析师职位怎么样? 11963
- [求助]病毒分析师和安全开发工程师哪个靠谱? 12385
- [求助]信息安全专业毕业生找什么单位比较靠谱? 1662
- [求助]有人能在装有比特梵德的机子上写一个后台程序吗 1411
- [原创]自主设计贪吃蛇游戏源码 2829