汇编语言:统计学生成绩小程序(增加了边界判断)

从键盘录入10个学生成绩(0-100),录入完毕自动统计各分段的学生数。如果录入的不是0-100,则报错并重新输入。而且,按下ESC直接结束程序。之前没有对边界进行判断,结果造成了统计人数超过9以后就不能正常显示了此次加了边界判断,如果人数更大的话以此类推,若超过100则再另行处理。

include 一套工具.mac
data segment
	buf db 10 dup(?)
	w db 3 dup (?)
	s6 db ?
	s7 db ?
	s8 db ?
	s9 db ?
	show db "Please input ten scores:$"
	show1 db "The number of 0-59:$"
	show2 db "The number of 60-79:$"
	show3 db "The number of 80-89:$"
	show4 db "The number of 90-100:$"
	error1 db "Input error!$"
data ends

code segment
	assume cs:code,ds:data
start:
	mov ax,data
	mov ds,ax
	oustring show
	mov bx,0
	mov di,0
set1:
	input
	escexit al
	ldjudge al
	cmpzn al
	and al,0fh
	cmp bx,2
	jg error
	mov w[bx],al
	inc bx
	jmp set1
judge:
	cmp bx,0
	jz error
	cmp bx,1
	jz onebit
	cmp bx,2
	jz twobit
	cmp w[0],1
	jnz error
	threedigit w
	cmp al,100
	jg error
	jmp ret1
onebit:
	onedigit w
	jmp ret1
twobit:
	twodigit w
	jmp ret1
error:
	ld
	oustring error1
	mov bx,0
	mov cx,3
	jmp clear
ret1:
	mov buf[di],al
	inc di
	cmp di,9
	jg compare
	mov bx,0
	mov cx,3
clear:
	mov w[bx],0
	inc bx
	loop clear
	ld
	oustring show
	mov bx,0
	jmp set1
compare:
	mov cx,10
	mov bx,0
ret2:
	cmp buf[bx],60
	jl s1
	cmp buf[bx],80
	jl s2
	cmp buf[bx],90
	jl s3
	inc s9
	jmp s4
s1:
	inc s6
	jmp s4
s2:
	inc s7
	jmp s4
s3:
	inc s8
s4:
	inc bx
	loop ret2
	ld
	oustring show1
	cmp s6,10
	jge s6ab
	add s6,30h
	output s6
ss1:
	ld
	oustring show2
	cmp s7,10
	jge s7ab
	add s7,30h
	output s7
ss2:
	ld
	oustring show3
	cmp s8,10
	jge s8ab
	add s8,30h
	output s8
ss3:
	ld
	oustring show4
	cmp s9,10
	jge s9ab
	add s9,30h
	output s9
	ld
	jmp exit

s6ab:
	mov al,s6
	cbw
	mov cl,10
	idiv cl
	mov bl,ah
	add al,30h
	mov dl,al
	mov ah,2
	int 21h
	add bl,30h
	mov dl,bl
	int 21h
	jmp ss1
s7ab:
	mov al,s7
	cbw
	mov cl,10
	idiv cl
	mov bl,ah
	add al,30h
	mov dl,al
	mov ah,2
	int 21h
	add bl,30h
	mov dl,bl
	int 21h
	jmp ss2
s8ab:
	mov al,s8
	cbw
	mov cl,10
	idiv cl
	mov bl,ah
	add al,30h
	mov dl,al
	mov ah,2
	int 21h
	add bl,30h
	mov dl,bl
	int 21h
	jmp ss3
s9ab:
	mov al,s9
	cbw
	mov cl,10
	idiv cl
	mov bl,ah
	add al,30h
	mov dl,al
	mov ah,2
	int 21h
	add bl,30h
	mov dl,bl
	int 21h
	
exit:
	mov ah,4ch
	int 21h
code ends
end start

运行结果示意:

先来一个极端的:


正常的:



按ESC退出:



猜你喜欢

转载自blog.csdn.net/baidu_38760069/article/details/80370985