32位masm汇编—冒泡排序

 学习来自:《汇编语言:基于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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值