;----------------------------------
;冒泡排序
;负数显示补码如-1显示65535
;如想增加数据只需在a变量中添加,
;同时长度len要相应的更改
;----------------------------------
data segment
len dw 10
a dw -1,2,100,-100,99,-120,15,46,411,102
b dw 0
data ends
code segment
main proc far
assume cs:code,ds:data
start:
push ds
sub ax,ax
push ax
mov ax,data
mov ds,ax
mov cx,len
dec cx
loop1:
mov di,cx
mov bx,0
loop2:
mov ax,a[bx]
cmp a[bx+2],ax
jge cotinue
xchg ax,a[bx+2]
mov a[bx],ax
cotinue:
add bx,2
loop loop2
mov cx,di
loop loop1
mov si,0
loop3:
mov bx,a[si]
call bini
call crlf
add si,2
mov di,len
add di,len
cmp si,di
jl loop3
ret
main endp
bini proc near
mov cx,10000d
call bin
mov cx,1000d
call bin
mov cx,100d
call bin
mov cx,10d
call bin
mov cx,1d
call bin
ret
bini endp
bin proc near
mov ax,bx
mov dx,0
div cx
mov bx,dx
mov dl,al
add dl,30h
mov ah,02h
int 21h
ret
bin endp
crlf proc near
mov dl,0ah
mov ah,02h
int 21h
mov dl,0dh
mov ah,02h
int 21
ret
crlf endp
code ends
end start
楼上的兄弟,比较数而已嘛,用得着写那么长吗,看得好累;不知道我这个如何:
data segment
dw 6666h,7777h,8888h,9999h,0aaaah,0bbbbh,0cccch,0ddddh,0eeeeh,0ffffh
data ends
code segment
assume cs:code,ds:data
start:mov ax,data
mov ds,ax
mov cx,9 ;我这里是10个数 所以第一次大循环需要比较9次
l1: mov dx,cx
mov bx,0
l2: mov ax,[bx]
inc bx
inc bx
cmp ax,[bx]
jle done ;有符号数的小于等于跳转
xchg [bx],ax
dec bx
dec bx
mov [bx],ax
inc bx
inc bx
done: dec dx
jne l2 ;与下一个比较
loop l1 ;外套循环,比较得出第二大的,放到倒数第二位置
mov ax,4c00h
int 21h
code ends
end start