原本想寫個完整的學生成績管理系統,不過想想還是算了,所以此程序就沒有使用文件及鏈表什麼的,僅作爲基本練習,演示了成績管理基本功能、分頁顯示數據、文本選單。
.model small
;***************************************
;定義學生結構體類型
student struc
id dw 0 ;學號
sname db 16 dup('$') ;姓名
score dw 0 ;分數
order dw 0 ;排名
student ends
;***************************************
;***************************************
;回車換行
newline macro
push ax
push dx
mov ah,2
mov dl,0dh
int 21h
mov ah,2
mov dl,0ah
int 21h
pop dx
pop ax
endm
;***************************************
COUNT equ 30 ;學生人數
LIMIT equ 300 ;分數上限
.stack 200h
.data
str1 db "student (ID: ",'$'
str2 db ") score: ",'$'
str3 db "ID name score order",0dh,0ah,'$'
space db " ",'$'
tip1 db "Student ID: ",'$'
tip2 db "Name: ",'$'
tip3 db "Score: ",'$'
error1 db "Not found! Please input again.",0dh,0ah,'$'
error2 db "Too high. Please input again.",0dh,0ah,'$'
menu1 db " ***************************************",0dh,0ah
db " * *",0dh,0ah
db " * 1.input score *",0dh,0ah
db " * 2.modify *",0dh,0ah
db " * 3.display *",0dh,0ah
db " * 4.quit *",0dh,0ah
db " ***************************************",0dh,0ah,'$'
buf db 16
len db 0
buffer db 16 dup('$')
temp student <>
array student <1,"Tom">
student <2,"Jack">
student <3,"Rose">
student <4,"Lusy">
student <5,"Lily">
student <6,"David">
student <7,"Tim">
student <8,"Ana">
student <9,"Ivy">
student <10,"Joe">
student <11,"John">
student <12,"Joson">
student <13,"Lulu">
student <14,"Kitty">
student <15,"Kite">
student <16,"Joy">
student <17,"Jill">
student <18,"Bily">
student <19,"Bill">
student <20,"Jane">
student <21,"Bean">
student <22,"Iris">
student <23,"Ishara">
student <24,"Jamie">
student <25,"Jenny">
student <26,"Judy">
student <27,"June">
student <28,"Kate">
student <29,"Linda">
student <30,"Lisa">
.CODE
;***************************************
;讀取正整數子程序
;功能描述:進行過濾性輸入,非法字符不會接收及被顯示,以回車鍵結束輸入;
; 按退格鍵可以刪除之前輸入的數字,按ESC鍵則結束子程序並置DX爲1
;入口參數:無
;出口參數:AX=所接收的整數值,DX=0|1(1表示輸入了ESC字符,0表示沒有)
read proc
local flag: WORD
push di
push si
push bx
push cx
xor di,di
a00:
mov flag,0
a10:
mov ah,8
int 21h
inc flag
cmp al,1Bh
je a50
cmp al,8
jne a01
cmp flag,1
je a00
test di,di
jz a00
dec di
pop ax
mov ah,2
mov dl,8
int 21h
mov ah,2
mov dl,' '
int 21h
mov ah,2
mov dl,8
int 21h
jmp a10
a01:
cmp al,0Dh
jne a11
cmp flag,1
je a00
jmp a20
a11:
cmp al,30h
jb a10
cmp al,39h
ja a10
mov ah,2
mov dl,al
int 21h
sub al,30h
xor ah,ah
push ax
inc di
jmp a10
a20:
xor bx,bx
mov si,10
xor cx,cx
a21:
pop ax
push cx
test cx,cx
jz a40
a30:
mul si
loop a30
a40:
pop cx
inc cx
add bx,ax
dec di
test di,di
jnz a21
xor dx,dx
mov ax,bx
jmp a60
a50:
xor ax,ax
mov dx,1
a60:
pop cx
pop bx
pop si
pop di
ret
read endp
;***************************************
;***************************************
;顯示正整數子程序
;功能描述:以十進制方式輸出任意的16位正整數
;入口參數:AX=要輸出的整數
write proc
push si
push bx
push cx
push dx
xor si,si
mov bx,10
tostring:
xor dx,dx
div bx
add dx,30h
push dx
inc si
test ax,ax
jnz tostring
mov cx,si
display:
pop ax
mov ah,2
mov dl,al
int 21h
loop display
pop dx
pop cx
pop bx
pop si
ret
write endp
;***************************************
;***************************************
;清屏子程序
cls proc
push ax
push bx
push cx
push dx
mov ax,3
int 10h
mov ah,6
xor al,al
mov bh,1fh
xor cx,cx
mov dx,184fh
int 10h
pop dx
pop cx
pop bx
pop ax
ret
cls endp
;***************************************
;***************************************
;輸入分數子程序
input proc
push si
push di
push cx
push dx
call cls
xor si,si
mov di,1
mov cx,COUNT
b10:
lea dx,str1
mov ah,9
int 21h
mov ax,di
call write
inc di
lea dx,str2
mov ah,9
int 21h
call read
cmp dx,1
je over
cmp ax,LIMIT
jb b11
newline
dec di
lea dx,error2
mov ah,9
int 21h
newline
jmp b10
b11:
mov array[si].score, ax
newline
add si,size student
loop b10
over:
pop dx
pop cx
pop di
pop si
ret
input endp
;***************************************
;***************************************
;顯示記錄子程序
;功能描述:顯示一個結構體變量的值
;入口參數:BX=結構體變量的地址
ShowMsg proc
push ax
push dx
mov ax,(student ptr [bx]).id
call write
lea dx,space
mov ah,9
int 21h
lea dx,(student ptr [bx]).sname
mov ah,9
int 21h
lea dx,space
mov ah,9
int 21h
mov ax,(student ptr [bx]).score
call write
lea dx,space
mov ah,9
int 21h
mov ax,(student ptr [bx]).order
call write
pop dx
pop ax
ret
ShowMsg endp
;***************************************
;***************************************
;輸出子程序
;功能描述:在屏幕上顯示最多20條記錄
;入口參數:SI=結構數組起始下標
PageScreen proc
push si
push ax
push bx
push cx
push dx
cmp si,COUNT
jae quit
call cls
lea dx,str3
mov ah,9
int 21h
mov ax,si
mov si,size student
mul si
mov si,ax
mov cx,20
disp:
lea bx,array[si]
call ShowMsg
newline
add si,size student
cmp si,size student*(COUNT-1)
jg quit
loop disp
quit:
pop dx
pop cx
pop bx
pop ax
pop si
ret
PageScreen endp
;***************************************
;***************************************
;分屏顯示子程序
;功能描述:分屏顯示數據,按'a'鍵向上翻頁,'d'向下翻頁,'w'上移一條記錄,'s'下移一條記錄
output proc
push si
push ax
xor si,si
call PageScreen
last0:
mov ah,7
int 21h
cmp al,1Bh
je last4
cmp al,'d'
jne last1
add si,20
cmp si,COUNT
jle c00
mov si,COUNT
c00:
call PageScreen
jmp last0
last1:
cmp al,'a'
jne last2
sub si,20
cmp si,0
jge c01
xor si,si
c01:
call PageScreen
jmp last0
last2:
cmp al,'s'
jne last3
inc si
cmp si,COUNT
jle c03
mov si,COUNT
c03:
call PageScreen
jmp last0
last3:
cmp al,'w'
jne last0
mov bx,si
dec si
cmp si,0
jge c04
xor si,si
c04:
call PageScreen
jmp last0
last4:
pop ax
pop si
ret
output endp
;***************************************
;***************************************
;結構體值對換子程序
;功能描述:交換兩個結構變量的值
;入口參數:SI=結構變量1的地址,DI=結構變量2的地址
swap proc
push ax
push bx
push dx
mov ax,si
mov bx,di
cld
lea di,temp
mov cx,size student
rep movsb
mov si,bx
mov di,ax
mov cx,size student
rep movsb
lea si,temp
mov di,bx
mov cx,size student
rep movsb
pop dx
pop bx
pop ax
ret
swap endp
;***************************************
;***************************************
;排序子程序
;功能描述:根據成績對學生結構數組進行排序
sort proc
local m: WORD
mov m,size student
push ax
push bx
push cx
push dx
push si
push di
xor cx,cx
outer:
mov bx,cx
inc bx
inner:
mov ax,cx
mul m
mov si,ax
mov ax,bx
mul m
mov di,ax
mov ax,array[si].score
cmp ax,array[di].score
jbe next
push si
push di
lea si,array[si]
lea di,array[di]
call swap
pop di
pop si
next:
inc bx
cmp bx,COUNT
jb inner
inc cx
cmp cx,COUNT
jb outer
pop di
pop si
pop dx
pop cx
pop bx
pop ax
ret
sort endp
;***************************************
;***************************************
;排名子程序
;功能描述:根據學生成績進行排名
ranking proc
push si
push ax
push bx
mov si,(COUNT-1)*size student
mov bx,1
mov ax,array[si].score
lop:
cmp ax,array[si].score
je step
inc bx
step:
mov array[si].order,bx
mov ax,array[si].score
sub si,size student
cmp si,0
jge lop
pop bx
pop ax
pop si
ret
ranking endp
;***************************************
;***************************************
;修改學生信息子程序
modify proc
push ax
push bx
push dx
push si
push di
call cls
again1:
lea dx,tip1
mov ah,9
int 21h
call read
newline
cmp dx,1
je overall
test ax,ax
jz b00
cmp ax,COUNT
jbe b01
b00:
lea dx,error1
mov ah,9
int 21h
newline
jmp again1
b01:
; dec ax
; mov si,size student
; mul si
call search
mov si,ax
lea dx,tip2
mov ah,9
int 21h
lea dx,buf
mov ah,0ah
int 21h
mov cl,len
xor di,di
xor bx,bx
copy1:
mov al,buffer[di]
mov array[si].sname[bx],al
inc di
inc bx
loop copy1
newline
again2:
lea dx,tip3
mov ah,9
int 21h
call read
newline
cmp dx,1
je overall
cmp ax,LIMIT
ja b02
mov array[si].score,ax
jmp overall
b02:
lea dx,error2
mov ah,9
int 21h
newline
jmp again2
overall:
pop di
pop si
pop dx
pop bx
pop ax
ret
modify endp
;***************************************
;***************************************
;搜索子程序
;入口參數:AX=學生學號
;出口參數:AX=所找到的學生記錄在數組中的索引
search proc
push si
xor si,si
tofind:
cmp ax,array[si].id
je found
add si,size student
cmp si,size student*COUNT
jb tofind
found:
mov ax,si
pop si
ret
search endp
;***************************************
;***************************************
;主菜單程序
menu proc
push ax
push dx
step0:
call cls
lea dx,menu1
mov ah,9
int 21h
mov ah,8
int 21h
cmp al,'1'
jne step1
call input
jmp step0
step1:
cmp al,'2'
jne step2
call modify
jmp step0
step2:
cmp al,'3'
jne step3
call sort
call ranking
call output
jmp step0
step3:
pop dx
pop ax
ret
menu endp
;***************************************
START:
mov ax,@data
mov ds,ax
mov es,ax
call menu
mov ah,4ch
int 21h
END START