对300,250,280,240,270五个数进行冒泡排序
代码:
assume cs:cseg, ds:dseg, ss:sseg
sseg segment stack
dw 5H dup (?);开辟个五字节的空,额数组吧
sseg ends
dseg segment
dw 300,250,280,240,270
dseg ends
cseg segment
start: mov ax, dseg
mov ds, ax
mov cx, 4;五个数循环四次即可
sort: push cx ;将cx入栈
mov si, 0 ;将si指针归零 si 源变址寄存器
inner: mov ax, [si]
cmp ax, [si+2] ;栈里面是先进后出,先高后低,比较两个数
AH | ||
AL | AX | |
AH |
jbe noswap ;如果ax小于等于则跳转
xchg ax, [si+2];xchg交换两数,不能直接操作段寄存器,所以用ax过渡
mov [si], ax ;
noswap: add si, 2
loop inner ;loop根据cx数跳转,执行loop时先将cx减一如果cx不为零再跳转
pop cx ;pop出栈,退出循环
loop sort
mov ax, 4c00h
int 21h
cseg ends
end start