;对string字符串中的字符进行冒泡法排序
;BY 方剑冰 2009-5-9
;BY 方剑冰 2009-5-9
data segment
string db "ecdab$"
length equ $-string
infor db "result:$"
data ends
code segment
main proc far
ASSUME CS:CODE,DS:DATA
START:
PUSH DS
sub ax,ax
sub cx,cx
push ax
mov ax,data
MOV DS,AX
mov SI,00H
MOV cl,LENGTH
;打印排列前结果
lea dx,string
mov ah,09h
int 21h
call crlf
dec cl
cmp cl,0
JE EXIT
dec cl
;=======================
;第一层循环
ACTION1:
CMP cl,0
JE exit
mov ch,0
mov SI,00h
;=======================
;第二层循环
ACTION2:
;mov ch,0
CMP ch,cl
JE NEXT
mov bl,string[SI]
INC SI
cmp bl,string[SI]
jg swap
main proc far
ASSUME CS:CODE,DS:DATA
START:
PUSH DS
sub ax,ax
sub cx,cx
push ax
mov ax,data
MOV DS,AX
mov SI,00H
MOV cl,LENGTH
;打印排列前结果
lea dx,string
mov ah,09h
int 21h
call crlf
dec cl
cmp cl,0
JE EXIT
dec cl
;=======================
;第一层循环
ACTION1:
CMP cl,0
JE exit
mov ch,0
mov SI,00h
;=======================
;第二层循环
ACTION2:
;mov ch,0
CMP ch,cl
JE NEXT
mov bl,string[SI]
INC SI
cmp bl,string[SI]
jg swap
return:
INC ch
jmp action2
;=======================
NEXT:
DEC cl
jmp action1
;=======================
INC ch
jmp action2
;=======================
NEXT:
DEC cl
jmp action1
;=======================
swap:
mov bh,string[SI]
mov string[SI],bl
DEC SI
mov string[SI],bh
INC SI
jmp return
mov bh,string[SI]
mov string[SI],bl
DEC SI
mov string[SI],bh
INC SI
jmp return
EXIT:
;打印提示信息
lea dx,infor
mov ah,09h
int 21h
call crlf
;打印出排列后的结果
lea dx,string
mov ah,09h
int 21h
call crlf
lea dx,infor
mov ah,09h
int 21h
call crlf
;打印出排列后的结果
lea dx,string
mov ah,09h
int 21h
call crlf
;将控制权交还操作系统
mov ah,4ch
int 21h
main endp
crlf proc near
;回车换行
mov dl,0dh
mov ah,02h
int 21h
mov dl,0ah
mov ah,02h
int 21h
ret
crlf endp
code ends
end start
mov ah,4ch
int 21h
main endp
crlf proc near
;回车换行
mov dl,0dh
mov ah,02h
int 21h
mov dl,0ah
mov ah,02h
int 21h
ret
crlf endp
code ends
end start