【实验题目】
简易计算器设计 【实验目的】
综合测试学生微机接口技术及应用能力,包括系统构思设计、电路设计搭建、软件调试等;
结合应用实际,培养学生运用微机技术服务应用、服务实际的能力。 【基本要求】
1) 利用实验箱上的4x4键盘及6位数码管,实现两个16位宽的非负整数(0~65535)进行+、-、
×运算,计算结果限制在范围-65535~65535,超过范围在数码管最低位显示E; 2) 16个按键的分配可以自行指定;
【扩展要求】
1) 按基本要求保持输入的范围不变(16位宽),扩展计算结果的范围到用足6位数码管,当计算结果
超过-65535~999999时,显示E;
2) 增加÷的功能,有小数显示;
【实验程序】
;该程序实现了基本要求及扩展要求的2)
DSEG SEGMENT BUFF DB 6 DUP(?) LED_7 DB 3FH,06H,5BH,4FH,66H,6DH,7DH,07H,7FH,6FH,77H,7CH,39H,5EH,79H,71H,00H,40H
POSITION DB 0DFH,0EFH,0F7H,0FBH,0FDH,0FEH UNIT10 DW 10000,1000,100,10,1 NEWNUM DB 0 COUNT DB 0
FLAG DB 0 ;是否有键按下的标志 NEGTI DB 0 ;是否为负数的标志 NUM DW 0 NUM1 DW 0 NUM2 DW 0
TAG DB 0 ;运算种类标志
POINT DB 0 ;除法结果添加小数点标志 RESULT DW 0 DSEG ENDS
CSEG SEGMENT
ASSUME DS:DSEG,CS:CSEG START:
MOV AX,DSEG MOV DS,AX
MOV DX,300CH ;8255初始化 MOV AL,81H OUT DX,AL LEA SI,BUFF
MOV CX,6 NEXT:
MOV BYTE PTR[SI],16 INC SI
LOOP NEXT
CALL SHOW ;将显示缓冲区中内容在LED上一次显示出来
MOV COUNT,0 ;记按下了几位数
NEXT2: CALL SHOW
CALL SCAN ;判断是否有按键按下 CMP FLAG,1 JZ OK
JMP NEXT2 OK:
MOV FLAG,0 MOV POINT,0
MOV DX,3000H ;判断是哪一个键被按下 MOV AL,0FFH OUT DX,AL
MOV CH,-1 ;CH用于保存当前被扫描的列号 MOV CL,07FH XL:
ROL CL,1 INC CH
MOV DX,3000H MOV AL,CL
1 / 7
OUT DX,AL
MOV DX,3008H
IN AL,DX
AND AL,0FH
CMP AL,0FH
JZ XL ;选中下一列被扫描
CMP AL,0EH
JNZ FOUR
MOV CL,0 ;CL保存行号
JMP FREE
FOUR: MOV AL,0FFH OUT DX,AL CALL DELAY2 JMP NEXT2 JIAFA: MOV TAG,1 ;置加法标志 CALL TO_DEC ;将输入的数转化为十进制数保存 MOV AX,NUM MOV NUM1,AX JMP START ;继续输入第二个按键数 CMP AL,0DH JIANFA: JNZ BA MOV TAG,2 MOV CL,4 CALL TO_DEC JMP FREE MOV AX,NUM BA: MOV NUM1,AX CMP AL,0BH JMP START JNZ SHIER CHENGFA: MOV CL,8 MOV TAG,3 JMP FREE CALL TO_DEC SHIER: MOV AX,NUM CMP AL,07H MOV NUM1,AX JNZ XL JMP START MOV CL,12 CHUFA: FREE: MOV TAG,4 ADD CH,CL ;行列值相加为按键代表的值 CALL TO_DEC MOV AX,NUM CMP CH,9 MOV NUM1,AX JBE SHUZI JMP START CMP CH,12 ;加法 CLEAR: JZ JIAFA JMP START CMP CH,13 ;减法
JZ JIANFA YUNSUAN:
CMP CH,14 ;乘法 CALL TO_DEC JZ CHENGFA 数保存 CMP CH,15 ;除法 MOV AX,NUM JZ CHUFA MOV NUM2,AX CMP CH,11 ;等号时进行运算 CMP TAG,1 JZ YUNSUAN JZ SJIAF CMP CH,10 ;此键清屏重新输入 CMP TAG,2 JZ CLEAR JZ SJIANF SHUZI: CMP TAG,3 INC COUNT JZ SCHENGF MOV NEWNUM,CH CMP TAG,4 JZ SCHUF CALL COPY ;将新输入的按键值送入显
示缓冲区的最末位 YEAH:
MOV DX,3000H ;将LED关显示 CALL CHULI
2 / 7
;将第二次输入的数转化为十进制;将结果中零的位置16关显示
MOV TAG,0 MOV NEGTI,0 CALL SHOW JMP NEXT2
SJIAF: CALL JIA JMP YEAH SJIANF: CALL JIAN JMP YEAH SCHENGF: CALL CHENG JMP YEAH SCHUF: CALL CHU JMP YEAH
MOV AH,4CH INT 21H
JIA PROC NEAR PUSH AX PUSH CX PUSH SI
MOV AX,NUM1 ADD AX,NUM2
JC YC1 ;溢出MOV RESULT,AX CALL TO_SHOWSHU JMP OVER1 YC1:
LEA SI,BUFF
MOV BYTE PTR[SI],14 INC SI MOV CX,5 SS0:
MOV BYTE PTR[SI],16 INC SI LOOP SS0 OVER1: POP SI POP CX POP AX RET
JIA ENDP
JIAN PROC NEAR
PUSH AX PUSH CX PUSH SI
MOV AX,NUM1 CMP AX,NUM2 JA POSITIVE
MOV NEGTI,1 ;结果为负数 MOV AX,NUM2 SUB AX,NUM1 JC YC2 JMP OV POSITIVE:
MOV AX,NUM1 SUB AX,NUM2 JC YC2 OV:
MOV RESULT,AX CALL TO_SHOWSHU JMP OVER2 YC2:
LEA SI,BUFF
MOV BYTE PTR[SI],14 INC SI MOV CX,5 SS2:
MOV BYTE PTR[SI],16 INC SI LOOP SS2 OVER2: POP SI POP CX POP AX RET
JIAN ENDP
CHENG PROC NEAR PUSH AX PUSH CX PUSH SI
MOV AX,NUM1 MUL NUM2 JC YC3
MOV RESULT,AX CALL TO_SHOWSHU JMP OVER3 YC3:
LEA SI,BUFF
MOV BYTE PTR[SI],14
3 / 7
INC SI MOV CX,5 SS3:
MOV BYTE PTR[SI],16 INC SI LOOP SS3 OVER3: POP SI POP CX POP AX RET
CHENG ENDP
CHU PROC NEAR PUSH AX PUSH CX PUSH DX PUSH SI XOR DX,DX MOV AX,NUM1 DIV NUM2 PUSH DX
MOV RESULT,AX CALL TO_SHOWSHU MOV POINT,1 LEA SI,BUFF ADD SI,5 MOV CX,5 PEA:
MOV AL,[SI-1] MOV [SI],AL DEC SI LOOP PEA POP DX MOV AX,10 MUL DX XOR DX,DX DIV NUM2 MOV [SI],AL POP SI POP DX POP CX POP AX RET
CHU ENDP
CHULI PROC NEAR PUSH SI
PUSH CX LEA SI,BUFF ADD SI,5 XX:
CMP BYTE PTR[SI],0 JNZ NOZERO
MOV BYTE PTR[SI],16 DEC SI JMP XX NOZERO: CMP NEGTI,1 JNZ ZHENG INC SI
MOV BYTE PTR[SI],17 ZHENG: POP CX POP SI
CHULI ENDP
TO_DEC PROC NEAR PUSH SI PUSH AX PUSH BX PUSH CX LEA SI,BUFF MOV AL,COUNT XOR AH,AH ADD SI,AX DEC SI XOR AX,AX XOR CH,CH MOV CL,COUNT AGAIN: ADD AX,AX MOV BX,AX ADD AX,AX ADD AX,AX ADD AX,BX MOV BH,0 MOV BL,[SI] ADD AX,BX DEC SI
LOOP AGAIN MOV NUM,AX POP CX POP BX POP AX POP SI
4 / 7
RET
TO_DEC ENDP
TO_SHOWSHU PROC NEAR PUSH SI JNZ NOPOINT MOV CH,0FFH AA3:
LEA SI,BUFF LEA DI,POSITION PUSH DI PUSH AX PUSH BX PUSH CX LEA DI,BUFF ADD DI,5
LEA SI,UNIT10 MOV AX,RESULT MOV BYTE PTR[DI],0 DEC DI LOP0: XOR CL,CL MOV BX,[SI] LOP1: SUB AX,BX JB LOP3 INC CL JMP LOP1 LOP3:
ADD AX,BX MOV [DI],CL INC SI INC SI DEC DI CMP BX,1 JNZ LOP0 POP CX POP BX POP AX POP DI POP SI RET
TO_SHOWSHU ENDP
SHOW PROC NEAR PUSH AX PUSH BX PUSH CX PUSH DX PUSH SI PUSH DI
CMP POINT,1 ;是否需添加小数点
MOV AL,[SI] LEA BX,LED_7 XLAT
MOV DX,3004H OUT DX,AL MOV DX,3000H MOV AL,[DI] OUT DX,AL
CALL DELAY INC SI INC DI
MOV AL,[SI] LEA BX,LED_7 XLAT
OR AL,80H MOV DX,3004H OUT DX,AL MOV DX,3000H MOV AL,[DI] OUT DX,AL
CALL DELAY INC SI INC DI MOV CL,4 AA2:
MOV AL,[SI] LEA BX,LED_7 XLAT
MOV DX,3004H OUT DX,AL MOV DX,3000H MOV AL,[DI] OUT DX,AL
CALL DELAY INC SI INC DI DEC CH
JZ REALSTOP DEC CL JNZ AA2 JMP AA3
NOPOINT:
5 / 7
;延时,使一位稳定显示;填小数点 ;延时,使一位稳定显示;延时,使一位稳定显示
MOV CH,0FFH DELAY2 PROC NEAR AA1:
LEA SI,BUFF LEA DI,POSITION MOV CL,6 AA:
MOV AL,[SI] LEA BX,LED_7 XLAT
MOV DX,3004H OUT DX,AL MOV DX,3000H MOV AL,[DI] OUT DX,AL
CALL DELAY ;延时,使一位稳定显示 INC SI INC DI DEC CH
JZ REALSTOP DEC CL JNZ AA JMP AA1
REALSTOP: POP DI POP SI POP DX POP CX POP BX POP AX RET
SHOW ENDP
DELAY PROC NEAR PUSH BX PUSH CX
MOV BX,3FFH DEL1:
MOV CX,0FFFH DEL2:
LOOP DEL2 DEC BX JNZ DEL1 POP CX POP BX RET
DELAY ENDP
PUSH BX PUSH CX MOV BX,3FH DEL3:
MOV CX,0FFH DEL4:
LOOP DEL4 DEC BX JNZ DEL3 POP CX POP BX RET
DELAY2 ENDP
SCAN PROC NEAR PUSH AX PUSH DX KS:
MOV DX,3000H MOV AL,0 OUT DX,AL MOV DX,3008H IN AL,DX
AND AL,0FH CMP AL,0FH JZ JIESHU MOV FLAG,1 JIESHU: POP DX POP AX RET
SCAN ENDP
COPY PROC NEAR PUSH SI PUSH CX PUSH AX MOV CX,5 LEA SI,BUFF ADD SI,5 NEXT1:
MOV AL,[SI-1] MOV [SI],AL DEC SI
LOOP NEXT1
6 / 7
;屏蔽高四位
MOV AL,NEWNUM MOV [SI],AL POP AX POP CX POP SI
RET COPY ENDP CSEG ENDS END START 7 / 7
百度搜索“爱华网”,专业资料,生活学习,尽在爱华网