本程序是从键盘输入学生姓名和成绩,按成绩升序排序,并显示结果
DATAS SEGMENT
INFOR0 DB 0AH,0DH,'SORT=$'
INFOR1 DB 0AH,0DH,'INPUT NAME:$'
INFOR2 DB 0AH,0DH,'SCORE:$'
N EQU 8 ;姓名长度
M EQU 4 ;成绩长度(3位+回车符)
P EQU 3 ;输入的人数
Q EQU 3 ;成绩位数
BUFF1 DB N,?,N DUP(0),'$'
BUFF2 DB M,?,M DUP(0),'$'
SNAME DB P DUP(N+1 DUP(0)) ;保存姓名
SCORE1 DW P DUP(M+1 DUP(0)) ;保存成绩
SCORE2 DW P DUP(M+1 DUP(0))
CIXU DB P DUP(0) ;输入次序
X DW ?
SIGN1 DW 0
SIGN2 DW 0
CONT DB '1'
DATAS ENDS
CODES SEGMENT
ASSUME CS:CODES,DS:DATAS
MAIN PROC FAR
START:
MOV AX,DATAS
MOV DS,AX
MOV ES,AX
MOV BX,0
MOV CX,0
CALL INPUT
CALL SORT
CALL PRINT
MOV AH,4CH
INT 21H
MOV AH,4CH
INT 21H
MAIN ENDP
;输入姓名成绩
INPUT PROC
INC BX
CMP BX,P
JA EXIT
LEA DX,INFOR1
MOV AH,9
INT 21H
LEA DX,BUFF1
MOV AH,10
INT 21H
MOV AL,BUFF1+1 ;实际输入个数
ADD AL,2 ;加2,包含buff1的0,1号单元
MOV AH,0
MOV SI,AX
MOV BUFF1[SI],0 ;回车0d换为0,便于输出显示
LEA DX,INFOR2
MOV AH,9
INT 21H
MOV AL,BUFF2+1
ADD AL,2
MOV AH,0
MOV SI,AX
MOV BUFF2[SI],0
MOV CIXU[BX-1],BL
CMP BX,1
JZ LET1
ADD SIGN1,N+1
ADD SIGN2,Q
LET1:
CALL COPY
JMP INPUT
EXIT:
RET
INPUT ENDP
;数据转存
COPY PROC
MOV CX,N+1
LEA SI,BUFF1+2
LEA DI,SNAME
ADD DI,SIGN1
CLD
REP MOVSB
MOV CX,N
MOV AX,'$'
LEA DI,BUFF1+2
REP STOSB
MOV CX,M+1
LEA SI,BUFF2+2
LEA DI,SCORE1
ADD DI,SIGN2
CLD
REP MOVSB
LEA SI,BUFF2+2
MOV DI,SIGN2
CALL CHANGE
RET
COPY ENDP
;十进制数ASCII→二进制
CHANGE PROC
MOV X,0
MOV CX,[SI-1]
AND CX,000FH
REPT2:
MOV AL,[SI]
CMP AL,30H
JL EXIT1
CMP AL,39H
JG EXIT1
AND AX,000FH
XCHG AX,X
MOV DX,10
MUL DX
ADD X,AX
INC SI
LOOP REPT2
MOV AX,X
MOV SCORE2[DI],AX
MOV X,0
ADD SIGN2,2
EXIT1:RET
CHANGE ENDP
;排序
SORT PROC
MOV CX,P
DEC CX
LOOP1:PUSH CX
MOV BX,0
MOV SI,0
LOOP2:MOV AX,SCORE2[BX]
CMP AX,SCORE2[BX+M+1]
JGE NEXT
XCHG AX,SCORE2[BX+M+1]
MOV SCORE2[BX],AX
MOV AL,CIXU[SI]
XCHG AL,CIXU[SI+1]
MOV CIXU[SI],AL