8086
汇编经典例程
1
/
7
==========================================
5.
编写一个数组排序
(从小到大
)
的小程序,
数组在程序
中定义,程序先将原数组显示一遍,然后排序,再将排
好序的数组显示一遍。
==========================================
AAA SEGMENT
BUF DB '6455412345$'
L = $ - BUF
ary dw -897,345,6789,8654,-1234,
-3456,-99,-678,987,567,
32762,-12121,0,3434,4645,-234,
23455,-100,100,1000,-1000
dw
572,-2345,-6543,-1234,9999
len dw ($-ary)/2
ASSUME CS:AAA, DS:AAA
MAIN PROC FAR
PUSH CS
POP DS
LEA SI,ARY
MOV CX,LEN
NEXT3:
MOV BX,[SI]
CALL DISP
CALL SPACE
ADD SI,2
LOOP NEXT3
CALL CRLF
CALL CRLF
LEA SI,ARY
MOV CX,LEN
CALL SORT
LEA SI,ARY
MOV CX,LEN
NEXT5:
MOV BX,[SI]
CALL DISP
CALL SPACE
ADD SI,2
LOOP NEXT5
MOV AH,4CH
INT 21H
MAIN
ENDP
DISP PROC
PUSH SI
PUSH AX
PUSH BX
PUSH DX
PUSH CX
MOV CX,0
CMP BX,0
JNL NEXT1
NEG BX
MOV CX,1
NEXT1:
LEA SI,BUF+l-1
MOV AX,BX
MOV BX,10
NEXT:
MOV DX,0
DIV BX
ADD DL,30H
DEC SI
MOV [SI],DL
CMP AX,0
JNE NEXT
CMP CX,0
JE NEXT2
DEC SI
MOV BYTE PTR [SI],'-'
NEXT2:
MOV DX,SI
MOV AH,9
INT 21H
POP CX
POP DX
POP BX
POP AX
POP SI
RET
DISP
ENDP
SORT PROC
NEXTT: PUSH CX
MOV DI,SI
MOV AX,[SI]
NEXT0: CMP AX,[DI]
JL NEXTQ
MOV BX,AX
MOV AX,[DI]
MOV [DI],BX
NEXTQ: ADD DI,2
LOOP NEXT0
MOV [SI],AX
ADD SI,2
POP CX
LOOP NEXTT
RET
SORT ENDP
CRLF PROC
PUSH AX
PUSH DX
MOV DL,13
MOV AH,2
INT 21H
MOV DL,10
INT 21H
POP DX
POP AX