用汇编语言编写程序,统计某门课程考试成绩各分数段的人数,然后按照一定格式显示出总人数,各分数段人数及所占百分比。用子程序结构写
时间: 2023-12-21 19:07:41 浏览: 102
汇编语言用于统计分数段
以下是用汇编语言编写程序,统计某门课程考试成绩各分数段的人数,然后按照一定格式显示出总人数,各分数段人数及所占百分比的代码,使用了子程序结构:
```
.MODEL SMALL
.STACK 100H
.DATA
; 定义考试成绩的数组
SCORES DB 60, 70, 80, 90, 100
; 定义各分数段人数的变量
UNDER60 DB ?
BETWEEN60AND69 DB ?
BETWEEN70AND79 DB ?
BETWEEN80AND89 DB ?
BETWEEN90AND100 DB ?
; 定义总人数、百分比的变量
TOTAL DB ?
PERCENT60 DB ?
PERCENT70 DB ?
PERCENT80 DB ?
PERCENT90 DB ?
PERCENT100 DB ?
; 定义输出格式的字符串
MSG1 DB 'Total number of students: $'
MSG2 DB 'Number of students under 60: $'
MSG3 DB 'Number of students between 60 and 69: $'
MSG4 DB 'Number of students between 70 and 79: $'
MSG5 DB 'Number of students between 80 and 89: $'
MSG6 DB 'Number of students between 90 and 100: $'
MSG7 DB 'Percentage of students under 60: $'
MSG8 DB 'Percentage of students between 60 and 69: $'
MSG9 DB 'Percentage of students between 70 and 79: $'
MSG10 DB 'Percentage of students between 80 and 89: $'
MSG11 DB 'Percentage of students between 90 and 100: $'
.CODE
MAIN PROC
MOV AX, @DATA
MOV DS, AX
; 初始化各分数段人数的变量
MOV UNDER60, 0
MOV BETWEEN60AND69, 0
MOV BETWEEN70AND79, 0
MOV BETWEEN80AND89, 0
MOV BETWEEN90AND100, 0
; 统计各分数段人数
CALL COUNT_SCORES
; 计算总人数和百分比
CALL CALC_PERCENTAGE
; 输出结果
CALL DISPLAY_RESULT
MOV AH, 4CH
INT 21H
MAIN ENDP
; 统计各分数段人数的子程序
COUNT_SCORES PROC
MOV CX, 5 ; 考试成绩的数量
MOV SI, OFFSET SCORES
MOV BX, OFFSET UNDER60
LOOP_START:
CMP [SI], 60
JB UNDER60_SCORE
CMP [SI], 70
JB BETWEEN60AND69_SCORE
CMP [SI], 80
JB BETWEEN70AND79_SCORE
CMP [SI], 90
JB BETWEEN80AND89_SCORE
JMP BETWEEN90AND100_SCORE
UNDER60_SCORE:
INC BX
JMP LOOP_END
BETWEEN60AND69_SCORE:
ADD BX, 2
JMP LOOP_END
BETWEEN70AND79_SCORE:
ADD BX, 3
JMP LOOP_END
BETWEEN80AND89_SCORE:
ADD BX, 4
JMP LOOP_END
BETWEEN90AND100_SCORE:
ADD BX, 5
LOOP_END:
ADD SI, 1 ; 跳到下一个考试成绩
LOOP LOOP_START
RET
COUNT_SCORES ENDP
; 计算总人数和百分比的子程序
CALC_PERCENTAGE PROC
MOV AL, UNDER60
ADD AL, BETWEEN60AND69
ADD AL, BETWEEN70AND79
ADD AL, BETWEEN80AND89
ADD AL, BETWEEN90AND100
MOV TOTAL, AL
MOV BL, 100
DIV BL ; AL = 总人数,BL = 100
MOV PERCENT60, AL
MOV AL, BETWEEN60AND69
ADD AL, BETWEEN70AND79
ADD AL, BETWEEN80AND89
ADD AL, BETWEEN90AND100
MOV BL, TOTAL
DIV BL ; AL = 60-100分数段的总人数,BL = 总人数
MOV PERCENT70, AL
MOV AL, BETWEEN70AND79
ADD AL, BETWEEN80AND89
ADD AL, BETWEEN90AND100
MOV BL, TOTAL
DIV BL ; AL = 70-100分数段的总人数,BL = 总人数
MOV PERCENT80, AL
MOV AL, BETWEEN80AND89
ADD AL, BETWEEN90AND100
MOV BL, TOTAL
DIV BL ; AL = 80-100分数段的总人数,BL = 总人数
MOV PERCENT90, AL
MOV AL, BETWEEN90AND100
MOV BL, TOTAL
DIV BL ; AL = 90-100分数段的总人数,BL = 总人数
MOV PERCENT100, AL
RET
CALC_PERCENTAGE ENDP
; 输出结果的子程序
DISPLAY_RESULT PROC
; 输出总人数
MOV AH, 9
MOV DX, OFFSET MSG1
INT 21H
MOV DL, TOTAL
ADD DL, 48
INT 21H
MOV AH, 2
MOV DL, 0DH
INT 21H
MOV DL, 0AH
INT 21H
; 输出各分数段人数
MOV AH, 9
MOV DX, OFFSET MSG2
INT 21H
MOV DL, UNDER60
ADD DL, 48
INT 21H
MOV AH, 2
MOV DL, 0DH
INT 21H
MOV DL, 0AH
INT 21H
MOV AH, 9
MOV DX, OFFSET MSG3
INT 21H
MOV DL, BETWEEN60AND69
ADD DL, 48
INT 21H
MOV AH, 2
MOV DL, 0DH
INT 21H
MOV DL, 0AH
INT 21H
MOV AH, 9
MOV DX, OFFSET MSG4
INT 21H
MOV DL, BETWEEN70AND79
ADD DL, 48
INT 21H
MOV AH, 2
MOV DL, 0DH
INT 21H
MOV DL, 0AH
INT 21H
MOV AH, 9
MOV DX, OFFSET MSG5
INT 21H
MOV DL, BETWEEN80AND89
ADD DL, 48
INT 21H
MOV AH, 2
MOV DL, 0DH
INT 21H
MOV DL, 0AH
INT 21H
MOV AH, 9
MOV DX, OFFSET MSG6
INT 21H
MOV DL, BETWEEN90AND100
ADD DL, 48
INT 21H
MOV AH, 2
MOV DL, 0DH
INT 21H
MOV DL, 0AH
INT 21H
; 输出百分比
MOV AH, 9
MOV DX, OFFSET MSG7
INT 21H
MOV DL, PERCENT60
ADD DL, 48
INT 21H
MOV AH, 2
MOV DL, '%'
INT 21H
MOV DL, 0DH
INT 21H
MOV DL, 0AH
INT 21H
MOV AH, 9
MOV DX, OFFSET MSG8
INT 21H
MOV DL, PERCENT70
ADD DL, 48
INT 21H
MOV AH, 2
MOV DL, '%'
INT 21H
MOV DL, 0DH
INT 21H
MOV DL, 0AH
INT 21H
MOV AH, 9
MOV DX, OFFSET MSG9
INT 21H
MOV DL, PERCENT80
ADD DL, 48
INT 21H
MOV AH, 2
MOV DL, '%'
INT 21H
MOV DL, 0DH
INT 21H
MOV DL, 0AH
INT 21H
MOV AH, 9
MOV DX, OFFSET MSG10
INT 21H
MOV DL, PERCENT90
ADD DL, 48
INT 21H
MOV AH, 2
MOV DL, '%'
INT 21H
MOV DL, 0DH
INT 21H
MOV DL, 0AH
INT 21H
MOV AH, 9
MOV DX, OFFSET MSG11
INT 21H
MOV DL, PERCENT100
ADD DL, 48
INT 21H
MOV AH, 2
MOV DL, '%'
INT 21H
MOV DL, 0DH
INT 21H
MOV DL, 0AH
INT 21H
RET
DISPLAY_RESULT ENDP
END MAIN
```
注释已经在代码中给出。
阅读全文