学生成绩由姓名,成绩A,成绩B以及合计总分四项组成,其中,合计总分=成绩A+成绩B。从键盘输入1个学生的成绩记录后,
输出学生成绩。
• 要求:输入、计算、输出为子程序。
data segment
prompt1 db "Please input a name:$"
prompt2 db "Please input a scoreA:$"
prompt3 db "Please input a scoreB:$"
result1 db "name:$"
result2 db "ScoreA:$"
result3 db "ScoreB:$"
result4 db "ScoreSum:$"
cr equ 0dh
lf equ 0ah
input_str db 80 dup ('$')
ScoreA dw ?
ScoreB dw ?
ScoreSum dw ?
data ends
code segment
assume cs:code,ds:data
main proc far
start:
mov ax,data
mov ds,ax
CALL Input;调用输入函数
CALL Calculate;调用计算函数
CALL Output;调用输出函数
mov ax,4c00h
int 21h
main endp
;----------------------
;输入函数
Input proc near
; 提示输入字符串
mov ah, 09h
lea dx,prompt1
int 21h
mov di, 0 ; 设置存储位置
; 从键盘读入一个字符(包括回车符)
readchar:
mov ah, 01h ; 功能号:读入一个字符
int 21h ; 调用21h中断输入字符
cmp al, cr ; 判断是否是回车符
je end_input ; 如果是回车符,则结束输入
; 存储输入的字符
mov input_str[di], al ; 存储字符
inc di;
jmp readchar ; 继续输入下一个字符
end_input:
mov byte ptr input_str[di], 0 ;为字符串添加结束符
jmp readscoreA;跳转到输入成绩A
readscoreA:
; 提示输入字符串
mov ah, 09h
lea dx,prompt2
int 21h
; 从键盘读入一个字符(包括回车符)
mov bx,0
s1: mov ah, 01h ; 功能号:读入一个字符
int 21h ; 调用21h中断输入字符
sub al, 30h;转换为数字
jl readscoreB;不是数的话,说明输入完成,跳到输入成绩B上
cmp al,9
jg readscoreB
xchg ax,bx;交换寄存器
mov cx,10
mul cx
xchg ax,bx
add bx,ax
jmp s1 ; 继续输入下一个字符
readscoreB:
mov si,offset ScoreA
mov [si],bx;
; 提示输入字符串
mov ah, 09h
lea dx,prompt3
int 21h
mov bx,0
; 从键盘读入一个字符(包括回车符)
s2: mov ah, 01h ; 功能号:读入一个字符
int 21h ; 调用21h中断输入字符
sub al, 30h;转换为数字
jl exit;不是数的话,说明输入完成,跳到输入成绩B上
cmp al,9
jg exit
xchg ax,bx;交换寄存器
; 存储输入的字符
mov cx,10
mul cx
xchg ax,bx
add bx,ax
jmp s2 ; 继续输入下一个字符
exit:
mov si,offset ScoreB
mov [si],bx;
ret
Input endp
;-------------
Calculate proc near
mov si,offset ScoreA
mov di,offset ScoreB
mov ax,[si]
mov bx,[di]
add ax,bx
mov si,offset ScoreSum
mov [si],ax
ret
Calculate endp
;---------------
Output proc near
mov ah,09h
lea dx,result1;姓名
int 21h
lea dx,input_str
mov ah,09h
int 21h
mov ah,02h
mov dl,10
int 21h
mov ah,09h
lea dx,result2;成绩A
int 21h
mov si,offset ScoreA;输出成绩A
mov bl,[si]
mov bh,0
Call Both;
mov ah,02h
mov dl,10
int 21h
mov ah,09h
lea dx,result3;成绩B
int 21h
mov si,offset ScoreB;输出成绩B
mov bl,[si]
mov bh,0
Call Both;
mov ah,02h
mov dl,10
int 21h
mov ah,09h
lea dx,result4;总成绩
int 21h
mov si,offset ScoreSum;输出总成绩
mov bl,[si]
mov bh,0
Call Both;
ret
Output endp
;--------------
Both proc near
mov ch,4;数字转换为4位16进制
shift: mov cl,4
rol bx,cl
mov al,bl
and al,0fh
add al,30h
cmp al,39h
jle dig
add al,7
dig: mov dl,al
mov ah,2
int 21h
dec ch
jnz shift
ret
Both endp
;---------------
code ends
end start
运行结果: