本人上学时间做的一个计算器作业.发上来以备以后查看,写的方法有点幼稚,见笑.
XIAN MACRO XIANSHI;显示大写字母
LEA BX,XIANSHI
LOCAL C1,C2
C1: CMP [BX],'A'
JS C2
CMP [BX],'Z'
JNS C2
MOV DL,[BX]
MOV AH,2
INT 21H
INC BX
JMP C1
C2: MOV DL,"!"
MOV AH,2
INT 21H
MOV DL,0AH
MOV AH,2
INT 21H
MOV DL,0DH
MOV AH,2
INT 21H
ENDM
XIAN1 MACRO XIANSHI;显示非零位
LEA BX,XIANSHI
LOCAL C1,C2
C1:
CMP [BX],'0'
JZ C2
MOV DL,[BX]
MOV AH,2
INT 21H
INC BX
JMP C1
C2: MOV DL,"!"
MOV AH,2
INT 21H
MOV DL,0AH
MOV AH,2
INT 21H
MOV DL,0DH
MOV AH,2
INT 21H
ENDM
XIAN0 MACRO XIANSHI0;显示四位以内数字
LOCAL D1,D2,D21,D3,D4,D5,D51,D6,D7,D8,D81,D9,D10,D11,D111,D12
MOV BX,XIANSHI0
MOV DL,-1
MOV FLAG,0
D1: SUB BX,10000
INC DL
JC D2
JMP D1
D2: CMP DL,1
JNS D21
CMP FLAG,0
JZ D3
D21: MOV FLAG,1
OR DL,30H
MOV AH,2
INT 21H
D3: ADD BX,10000
MOV DL,-1
D4: SUB BX,1000
INC DL
JC D5
JMP D4
D5: CMP DL,1
JNS D51
CMP FLAG,0
JZ D6
D51: MOV FLAG,1
OR DL,30H
MOV AH,2
INT 21H
D6: ADD BX,1000
MOV DL,-1
D7: SUB BX,100
INC DL
JC D8
JMP D7
D8: CMP DL,1
JNS D81
CMP FLAG,0
JZ D9
D81: MOV FLAG,1
OR DL,30H
MOV AH,2
INT 21H
D9: ADD BX,100
MOV DL,-1
D10: SUB BX,10
INC DL
JC D11
JMP D10
D11: CMP DL,1
JNS D111
CMP FLAG,0
JZ D12
D111: MOV FLAG,1
OR DL,30H
MOV AH,2
INT 21H
D12: ADD BX,10
MOV DL,BL
OR DL,30H
MOV AH,2
INT 21H
ENDM
SHURU MACRO STATE;输入四位之内数字
LOCAL S1
S1: MOV AH,1
INT 21H
CMP AL,30H
JC STATE
CMP AL,3AH
JNC STATE
MOV FLAGE,01H
ADD CX,CX
MOV BX,CX
ADD CX,CX
ADD CX,CX
ADD CX,BX
AND AX,0FH
ADD CX,AX
JMP S1
ENDM
DATA SEGMENT
ERROR0 DB 'ERROR0'
TUICHU DB 'TUICHU0'
OUTT DB 'OUT'
KUOHAO DB 'SOME ( HAS NOT BEEN ENDDED0'
KUOHAO1 DB 'THIS ) IS WRONG0'
SHUZI1 DW ?
SHUZI2 DW ?
FUHAO1 DW ?
FUHAO2 DW ?
FUHAO DW ?
;HUANHANG DB ' '
FLAGE DB 00H
FLAG DB ?
FLA DW -1
XI DW 9899
DATA ENDS
CODE SEGMENT
ASSUME CS:CODE,DS:DATA
START: MOV AX,DATA
MOV DS,AX
YUNSUAN PROC NEAR
PUSH SHUZI1
PUSH SHUZI2
PUSH FUHAO1
PUSH FUHAO2
PUSH FUHAO
INC FLA
;XIAN0 FLA
;PUSH AX
;PUSH BX
;PUSH DX
;XIAN0 XI
STATE1: MOV CX,0000H
SHURU STATE2;读第一个数字
STATE2: SUB FLAGE,01H
JZ S21
CMP AL,'('
JNZ S24
CALL YUNSUAN
JMP S21
S24: JMP ERROR
S21: ;XIAN0 CX
MOV SHUZI1,CX;保存cx中的第一个数字并且读第一个符号
CMP AL,'+'
JZ STATE3
CMP AL,'-'
JZ STATE3
CMP AL,'*'
JZ S22
CMP AL,'/'
JZ S22
CMP AL,'='
JZ S23
CMP AL,')'
JZ S23
JMP ERROR
S23: JMP OK
S22: JMP STATE4
STATE3: MOV CX,0000H
MOV AH,0H
MOV FUHAO1,AX;第一个符号是+-时,保存第一个符号,读入第二个数字
SHURU STATE5
STATE5: SUB FLAGE,01H
JZ S51
CMP AL,'('
JNZ S53
CALL YUNSUAN
JMP S51
S53: JMP ERROR
S51: MOV SHUZI2,CX;保存第二个数字,读入第二个符号
CMP AL,'+'
JZ STATE6
CMP AL,'-'
JZ STATE6
CMP AL,'*'
JZ STATE7
CMP AL,'/'
JZ STATE7
CMP AL,'='
JZ S52
CMP AL,')'
JZ S52
JMP ERROR
S52: JMP OK1
STATE6: CMP FUHAO1,'+';第二个符号是+-时,将返回到状态3
JZ STATE8
CMP FUHAO1,'-'
JZ STATE9
STATE8: MOV BX,SHUZI2
ADD SHUZI1,BX
JMP STATE3
STATE9: MOV BX,SHUZI2
SUB SHUZI1,BX
JMP STATE3
STATE7: MOV CX,0000H
MOV AH,0H
MOV FUHAO2,AX;第二个符号是*/时,保存第二个符号,读入下一个数字
SHURU STATE10
STATE10:SUB FLAGE,01H
JZ S101
CMP AL,'('
JNZ S102
CALL YUNSUAN
JMP S101
S102: JMP ERROR
S101: MOV AH,0H
MOV FUHAO,AX
CMP FUHAO2,'*';读入第三个数字并先完成乘除法计算,将返回状态5
JZ STATE11
CMP FUHAO2,'/'
JZ STATE12
STATE11:MOV AX,SHUZI2
MUL CX
MOV CX,AX
MOV AX,FUHAO
MOV FLAGE,01H
JMP STATE5
STATE12:MOV AX,SHUZI2
XOR DX,DX
IDIV CX
MOV CX,AX
MOV AX,FUHAO
MOV FLAGE,01H
JMP STATE5
STATE4: MOV CX,0000H
MOV AH,0H
MOV FUHAO1,AX;第一个符号是*/时将返回状态2
SHURU STATE13
STATE13:SUB FLAGE,01H
JZ S131
CMP AL,'('
JNZ S132
CALL YUNSUAN
JMP S131
S132: JMP ERROR
S131: MOV AH,0H
MOV FUHAO ,AX
CMP FUHAO1,'*'
JZ STATE14
CMP FUHAO1,'/'
JZ STATE15
STATE14:MOV AX,SHUZI1
MUL CX
MOV CX,AX
MOV AX,FUHAO
MOV FLAGE,01H
JMP STATE2
STATE15:MOV AX,SHUZI1
XOR DX,DX
IDIV CX
MOV CX,AX
MOV AH,0H
MOV AX,FUHAO
MOV FLAGE,01H
JMP STATE2
OK1: MOV BX,SHUZI2
CMP FUHAO1,'-'
JZ O12
;ADD AX,SHUZI1
ADD SHUZI1,BX
JMP O1
O12: SUB SHUZI1,BX
;XIAN OUTT
;XIAN HANHANG
JMP O1
ERROR1: MOV DL,0AH
MOV AH,2
INT 21H
MOV DL,0DH
MOV AH,2
INT 21H
XIAN1 KUOHAO
JMP ERROR
ERROR2: MOV DL,0AH
MOV AH,2
INT 21H
MOV DL,0DH
MOV AH,2
INT 21H
XIAN1 KUOHAO1
ERROR: XIAN ERROR0
XIAN TUICHU
JMP EENNDD
OK: ;XIAN OUTT
;XIAN HUANHANG
JMP O1
O1: CMP AL,')'
JNZ O2
CMP FLA,0H
JS O3
MOV CX,SHUZI1
JMP ED
O3: JMP ERROR2
O2: CMP FLA,0H
JZ O4
JMP ERROR1
O4: XIAN0 SHUZI1
MOV DL,0DH
MOV AH,2
INT 21H
MOV DL,0AH
MOV AH,2
INT 21H
MOV FLA,-1
JMP START
EENNDD: MOV AH,4CH
INT 21H
ED: MOV FLAGE,01H
;POP DX
;POP BX
;POP AX
MOV AH,1
INT 21H
POP FUHAO
POP FUHAO2
POP FUHAO1
POP SHUZI2
POP SHUZI1
DEC FLA
;XIAN0 FLA
RET
YUNSUAN ENDP
;CALL YUNNSUAN
CODE ENDS
END START