当前位置:文档之家› 实验5 子程序设计

实验5 子程序设计

实验五子程序结构设计
一、实验目的
1、加深对子程序的理解,掌握子程序的结构。

2、掌握子程序的设计、编写及调试方法。

二、实验内容
1、已知在内存数据段中GRADE开始单元连续存放着10个学生的成绩,试
采用子程序结构编程实现以下功能:
(1)找到最高成绩,将其偏移地址存放在内存单元MAX中。

(2)统计不及格的人数,存放在内存单元COUNT中。

(3)计算平均成绩(只取整数部分)。

(4)在屏幕上显示平均成绩。

要求:将以上功能分别写成子程序,数据区定义如下
DSEG SEGMENT
GRADE DB 76,68,54,80,45,92,63,58,94,85
MAX DW ?
COUNT DB ?
DSEG ENDS
答:源程序:
DATAS SEGMENT
GRADE DB 76,68,54,80,45,92,63,58,94,85
MAX DW?
COUNT DB?
DATAS ENDS
CODES SEGMENT
ASSUME CS:CODES,DS:DATAS
MAIN PROC FAR
START: PUSH DS
MOV AX,0
PUSH AX
MOV AX,datas
MOV DS,AX
CALL MAXGRADE
CALL BUJIGE
CALL AVGRADE
RET
MAIN ENDP
MAXGRADE PROC FAR
push ax
push cx
push si
mov cx,10
mov ax,0
mov si,ax
start_loop:
mov ax,word ptr grade[si]
cmp ax,word ptr grade[si+1] jge big
mov ax,word ptr grade[si+1] big: inc si
loop start_loop
mov max,ax
pop si
pop cx
pop ax
ret
MAXGRADE ENDP
BUJIGE PROC FAR
PUSH SI
PUSH CX
PUSH AX
MOV AX,0
MOV COUNT,0
MOV CX,10
LOOP_START2:
MOV AX,WORD PTR GRADE[SI]
CMP AX,60
JG SMALL2
INC COUNT
SMALL2:
INC SI
LOOP LOOP_START2
POP AX
POP CX
POP SI
RET
BUJIGE ENDP
AVGRADE PROC FAR
push ds
sub ax,ax
push ax
mov ax,DATAS
mov ds,ax
mov cx,10
mov bx,0
mov dx,0
avg_grade_1:
mov al,grade[bx] cbw
add dx,ax
inc bx
loop avg_grade_1
mov ax,dx
mov bl,10
div bl
sub ah,ah
div bl
push ax
add al,30h
mov dl,al
mov ah,02
int 21h
pop ax
add ah,30h
mov dl,ah
mov ah,02
int 21h
mov ah,4ch
int 21h
ret
AVGRADE ENDP CODES ENDS
END
运行结果:
2、编写程序实现十进制数的加减运算。

数据段可照如下设置(也可自己定义格式):DA TA SEGMENT
mes1 db 'please input the first operand:$' ;请输入第一个数
mes2 db 'please input the second operand:$' ;请输入第二个数
mes3 db 'please input the operator:$' ;请输入操作符
mes4 db 'output the Calculated Results:$' ;输出计算结果
DA TA ENDS
下面的结果截图仅供参考:(以输入任意非十进制字符作为输入结束)
(做减法若结果为负数要输出负号)
答:源程序:
datas segment
x1 dw 0
y1 dw 0
z1 dw 0
mess1 db' please input the first operand:$ ' mess2 db' please input the second operand:$ ' mess3 db' pleade input the operaor:$ '
mess4 db' output the Calculated Result:$ ' datas ends
codes segment
assume cs:codes,ds:datas
main proc far
push ds
sub ax,ax
push ax
START:
MOV AX,DATAS
MOV DS,AX
call input
call crlf
call input2
call crlf
call jiajian
call crlf
call jieguo
ret
main endp
multily macro mess
local newchar,exit1
lea dx,mess
mov ah,09
int 21h
newchar:
mov ah,1
int 21h
mov dl,al
sub al,30
jl exit1
cmp al,9d
jg exit1
cbw
xchg ax,bx
add bx,ax
jmp newchar
exit1:
ret
endm
input proc near
multily mess1
mov x1,bx
ret
input endp
input2 proc far
multily mess2
mov y1,bx
ret
input2 endp
defmac macro fuhao,operator fuhao macro x,y,z
push ax
mov ax,x
operator ax,y
mov z,ax
pop ax
endm
endm
jia proc near
defmac addition,add
ret
jia endp
jian proc near
defmac subtract,sub
ret
jian endp
jiajian proc near
lea dx,mess3
mov ah,09
int 21h
mov ah,1
int 21h
mov ax,bx
cmp al,2bh
je add1
cmp al,2dh
je sub1
jmp next
add1:
call jia
addition x1,y1,z1 mov bx,z1
jmp next
sub1:
call jian
subtract x1,y1,z1 mov bx,z1
mov ax,bx
int 21h
next:
ret
jiajian endp jieguo proc near
lea dx,mess4
mov ah,09
int 21h
mov al,bl
and al,0fh
add al,30h
cmp al,3ah
jl print
add al,7h
print:
mov dl,al
mov ah,2
int 21h
ret
jieguo endp
crlf proc near
mov dl,0dh
mov ah,2
int 21h
mov dl,0ah
mov ah,02h
int 21h
ret
crlf endp
mov ah,4ch
int 21h
codes ends
end main
运行结果:
三、实验报告要求:
1、画出程序框图说明程序的基本结构,写出各个模块说明。

2、写出调试过程,并分析结果,说明程序存在的问题。

3、说明子程序结构的使用场合。

相关主题