最近得补补16, 32, 64位的汇编,还是16位的汇编好玩尤其是使用debug调试的时候:)
选择排序
;把数据从大到小排列
;选择法排序
assume cs:code,ds:data
data segment
BUFFER db 1 dup(12h,31h,22h,44h,18h,99h,10h,2h,64h)
data ends
code segment
start:mov ax,data
mov ds,ax
mov si,offset BUFFER
mov cx,9
dec cx
s0:push cx
mov di,si
mov al,[di]
mov bx,di
s1:inc di
cmp al,[di]
jna p
mov al,[di]
mov bx,di
p:loop s1
xchg [si],al
xchg [bx],al
inc si
pop cx
loop s0
mov ax,4c00h
int 21h
code ends
end start
冒泡排序
;冒泡法排序
assume cs:code,ds:data
data segment
BUFFER db 12h,31h,22h,44h,18h,99h,10h,2h,64h
data ends
code segment
start:mov ax,data
mov ds,ax
mov si,offset BUFFER
mov cx,9
dec cx
s:push cx
mov di,si
s0:mov bl,[di]
cmp bl,[di+1]
jna s1
xchg bl,[di+1]
xchg bl,[di]
s1:inc di
loop s0
pop cx
loop s
mov ax,4c00h
int 21h
code ends
end start
插入排序
;插入法排序
;(有序系列扩大排序)
assume cs:code,ds:data
data segment
BUFFER db 12h,31h,22h,44h,18h,99h,10h,2h,64h
data ends
code segment
start:mov ax,data
mov ds,ax
mov si,offset BUFFER
inc si
mov cx,8
s1:mov bx,si ;随着循环的进行有序序列不断扩大
s:mov al,[si]
cmp al,[si-1]
ja s0
xchg al,[si-1]
xchg al,[si]
cmp si,1
jna s0
dec si
jmp s
s0:mov si,bx
inc si
loop s1
mov ax,4c00h
int 21h
code ends
end start