組合語言:統計學生成績小程式(增加了邊界判斷)
阿新 • • 發佈:2019-02-05
從鍵盤錄入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退出: