8086汇编,实现冒泡排序并做到100以内的数打印
assume cs:code, ds:data, ss:stack
data segment
arr db 2, 4, 9, 1, 7, 10, 12, 35
len dw $ - arr
data ends
stack segment
db 10 dup(0)
stack ends
code segment
start:
;; 初始化
mov ax, data
mov ds, ax
mov ax, stack
mov ss, ax
mov bp, 0
mov sp, 10
call bubble_sort
call println
mov ah, 4ch
int 21h
bubble_sort:
mov ax, len
dec ax
mov cx, ax
;; 外循环
fori:
push cx
mov si, 0
;; 内循环
forj:
;; if arr[si] > arr[si + 1]
mov ah, arr[si]
cmp ah, arr[si + 1]
jle j1
;; swap arr[si], arr[si + 1]
mov al, arr[si + 1]
mov arr[si], al
mov arr[si + 1], ah
j1:
;; end loop, update si
inc si
loop forj
pop cx
loop fori
;; return
ret
println:
mov cx, len
mov si, 0
j2:
mov al, arr[si]
mov ah, 00h
mov bl, 10
div bl
cmp al, 0
je j3
add al, 30h
mov dl, al
push ax
mov ah, 02h
int 21h
pop ax
j3:
add ah, 30h
mov dl, ah
mov ah, 02h
int 21h
;; print ' '
mov dl, 0
int 21h
inc si
loop j2
;; print '\n'
mov dx, 0ah
int 21h
ret
code ends
end start