assembly x86 (nasm) Selection Sort

A first address word NUM N disorder unsigned integer array, programming using a selection method the number of sorting the array in an ascending order of sort output.

 

 

Select Sort:

 

 

data segment
message        db    'This is a program of Selection sort',0dh,0ah,'$'
NUM         dw     12,78,55,4,125,96    ;0ch,4eh,37h,4h,7dh,60h
count         db     $-NUM
data ends
code segment
    assume cs:code,ds:data
start:
        mov ax,data
        mov ds,ax
        mov    dx,offset message            
        mov    ah,9                        
        int    21h    
        xor cx,cx
        mov cl,count
        shr cl,1
        dec cx                    ;比较n-1次
        mov bx,0
loop1:
        push cx                    ;cx入栈,避免内循环改变cx的值
        mov si,bx                ;si为内层循环的初始值,相当于下标
        mov ax,NUM[si]            ;ax为关键字
loop2:
        cmp ax,NUM[si+2]
        jg exchange             ;ax>NUM[si+1]则NUM[si+1]设为当前最小值,交换
        jmp done
exchange:
        xchg ax,NUM[si+2]        ;暂时存在ax中
done:
        add si,2
        loop loop2
        mov NUM[bx],ax            ;得到一个最小值, 前面的已经排好序
        add bx,2
        pop cx
        loop loop1
        xor cx,cx
        xor si,si
print:                            ;输出
        mov ax,NUM[si]
loopb:
        xor dx,dx
        inc cl
        mov bx,10
        div bx                    ;ax商,dx余
        push dx                    ;余数入栈
        cmp ax,0
        jne loopb
loopc: 
        pop dx
        or  dl,00110000b
        mov    ah,2                            
        int    21h
        loop loopc
        add si,2
        mov dl,20h
        mov ah,2h
        int 21h
        mov bx,word ptr count
        cmp si,bx
        jb print
exit:
        mov ah,4ch
        int 21h
code ends
end start

 

Guess you like

Origin www.cnblogs.com/lanclot-/p/10956989.html