学习来自:《汇编语言:基于x86处理器(原书第7版)》第9章
.386
.model flat,stdcall
option casemap:none
.data
arr dword 10h,97h,96h,65h,66h,15h,88h,18h,7h,22h
.stack
db 128 dup (0)
.code
main proc
push offset arr
push sizeof arr /type arr
call mySort
ret
main endp
;-------------------------------------
;mySort
;使用冒泡排序,将一个32位有符号整数数组按升序进行排序
;接收:数组指针、数组大小
;返回:无
;------------------------------------
mySort proc
push ebp
mov ebp ,esp
sub esp,040h
push esi
push edi
push edx
push ecx
mov ecx ,10h
mov eax,0cccccccch
lea edi,dword ptr ss:[ebp-040h]
cld
rep stosd
mov ecx ,dword ptr ss:[ebp+8h];外层循环次数
mov edi,dword ptr ss:[ebp+0ch];外层edi地址--i
l1:
mov eax,dword ptr ds:[edi];eax为外层循环的值:eax=arr[i]
mov esi,edi;内层esi--j
add esi ,4h
mov edx,ecx
dec edx
cmp edx, 0
je l4
l2:
cmp eax,dword ptr ds:[esi] ;arr[i]与arr[j]比较
jl l3
xchg eax,dword ptr ds:[esi]
mov dword ptr ds:[edi] ,eax
l3:
add esi ,4h
dec edx
cmp edx ,0
jg l2
l4:
dec ecx
add edi ,4h
cmp ecx ,0
jg l1
pop ecx
pop edx
pop edi
pop esi
mov esp,ebp
pop ebp
ret
mySort endp
end main
;-------------------------------------
;mySort2
;使用冒泡排序,将一个32位有符号整数数组按升序进行排序(前后比较)
;接收:数组指针、数组大小
;返回:无
;------------------------------------
mySort2 proc
push ebp
mov ebp ,esp
sub esp,040h
push esi
push edi
push edx
push ecx
mov ecx ,10h
mov eax,0cccccccch
lea edi,dword ptr ss:[ebp-040h]
cld
rep stosd
mov ecx ,dword ptr ss:[ebp+8h]
mov esi,dword ptr ss:[ebp+0ch]
dec ecx
l1:
push ecx
push esi
l2:
mov eax,dword ptr ds:[esi]
cmp dword ptr ds:[esi+4h],eax
jg l3
xchg eax,dword ptr ds:[esi+4h]
mov dword ptr ds:[esi] ,eax
l3:
add esi ,4h
loop l2
pop esi
pop ecx
loop l1
pop ecx
pop edx
pop edi
pop esi
mov esp,ebp
pop ebp
ret
mySort2 endp