21,500
社区成员
发帖
与我相关
我的任务
分享
;>>>>>>>>>>>>>>assume伪指令>>>>>>>>>>>>>>>>>>>>>>
assume cs:codesg,ds:datasg
;>>>>>>>>>>>>>>datasg段>>>>>>>>>>>>>>>>>>>>>>>>>>>
datasg segment
array db 3,2,5,6,1,9,7,4,0,8
LEN equ $-array
info db 'Please Input a Key,0 for asc,1 for desc,other for again!'
db 0ah,0dh,'$'
datasg ends
;>>>>>>>>>>>>>>codesg段>>>>>>>>>>>>>>>>>>>>>>>>>>>>
codesg segment
start:
mov dx,datasg
mov ds,dx
again:
mov dx,offset info ;显示提示信息
mov ah,9
int 21h
mov ah,1 ;从键盘输入一个字符,放在al中
int 21h
cmp al,'0' ;判断,输入为0或1就排序,否则重新输入
je sort
cmp al,'1'
je sort
mov ah,2 ;打印一个回车换行
mov dl,0ah
int 21h
mov dl,0dh
int 21h
jmp again
sort:
lea bx,array ;送入数组首地址在数据段的偏移
mov cx,LEN-1 ;送入数组长度减1
call selectionSort
inc cx ;恢复数组长度
mov si,0 ;si做变址寻址
mov ah,2 ;打印一个回车换行
mov dl,0ah
int 21h
mov dl,0dh
int 21h
show:
mov dl,[bx + si] ;把数组一个分量送dl
add dl,30h ;转换为ASCII码
int 21h
mov dl,20h ;输出一个空格
int 21h
inc si
loop show
mov ax,4c00h
int 21h
;>>>>>>>>>>>>>>>>>>选择排序子程序>>>>>>>>>>>>>>>>>>
;>>>>>>>>>>>输入参数cx:要排序的长度-1>>>>>>>>>>>>>
;>>>>>>>>>>>输入参数bx:数组首地址偏移>>>>>>>>>>>>
;>>>>>>>>>>>al为0则升序,al为1则降序>>>>>>>>>>>>>>>>
selectionSort proc
push bx
push si
push dx
push cx
cmp al,'0'
jne s00
s0:
mov si,1
push cx
s1:
mov dl,[bx]
cmp dl,[bx + si]
jbe next1
xchg dl,[bx + si]
mov [bx],dl
next1:
inc si
loop s1
inc bx
pop cx
loop s0
jmp exit
s00:
mov si,1
push cx
s11:
mov dl,[bx]
cmp dl,[bx + si]
jge next2
xchg dl,[bx + si]
mov [bx],dl
next2:
inc si
loop s11
inc bx
pop cx
loop s00
exit:
pop cx
pop dx
pop si
pop bx
ret
selectionSort endp
codesg ends
end start