《【精编版】汇编语言实现四则运算.pdf》由会员分享,可在线阅读,更多相关《【精编版】汇编语言实现四则运算.pdf(6页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、DISP MACRO VAR MOV AH,2 MOV DL,VAR INT 21H ENDM DATAS SEGMENT DATA0 DB PLEASE INPUT A NUMBER(0-999):,$DATA1 DB over flow input again:,$DATA2 DB PLEASE INPUT ANOTHER NUMBER(EXP:123),$DATA3 DB PLEASE INPUT AN OPREATOR:,$DATA4 DB THE OPERATOR IS WRONG,INPUT AGAIN:,$OPER DB?FLAG DB 0 FLAG1 DB 0 DATA DW
2、3 DUP(?)DATAS ENDS STACKS SEGMENT DW 256 DUP(?)STACKS ENDS CODES SEGMENT ASSUME CS:CODES,DS:DATAS,SS:STACKS START:MOV AX,DATAS MOV DS,AX MOVDI,OFFSET DATA0;提示信息CALL DISPL MOVSI,0;SI作为数组下标CALL INPUT CALL NEWLINE MOVDI,OFFSET DATA3;提示信息CALL DISPL AGAIN:MOVAH,1;1号功能输入四则运算符INT 21H CMPAL,+;判断输入的是不是+,-,*,
3、/JE NEXT0 CMPAL,-JE NEXT0 CMPAL,*JE NEXT0 CMPAL,/JE NEXT0 CALL NEWLINE;输入的运算符不合法,重新输入MOVDI,OFFSETDATA4 CALL DISPL JMP AGAIN NEXT0:MOVOPER,AL;保存运算符CALL NEWLINE MOV DI,OFFSETDATA2;输入第二个操作数CALL DISPL MOVSI,2 CALL INPUT MOVAX,DATA0 MOVBX,DATA2 CMPOPER,+;判断是那种操作,进行运算JE NEXT1 CMPOPER,-JE NEXT2 CMPOPER,*JE
4、 NEXT3 CMPOPER,/JE NEXT4 NEXT1:;加法运算CLC ADCAX,BX JNC N1 MOVFLAG,1;判断是否进位N1:MOVDATA4,AX;运算结果放入内存JMP OUTPUT NEXT2:;减法运算CLC SBBAX,BX JNC N2 MOVFLAG,1;判断是否借位NEGAX N2:MOVDATA4,AX;运算结果放入内存JMP OUTPUT NEXT3:;乘法运算CLC MOVDX,0 MULBX MOVDATA4,AX;运算结果放入内存JMP OUTPUT NEXT4:;除法运算CLC MOVDX,0 CMPBX,0 JNZ N4;判断除数是否为 0
5、 CALL NEWLINE CALL ERROR CALL NEWLINE MOVDI,OFFSET DATA2 CALL DISPL MOVSI,2 CALL INPUT JMP NEXT4 N4:DIVBX MOVDATA4,AX;运算结果放入内存OUTPUT:CALL NEWLINE MOVDI,0;输出第一个操作数CALL PRINT DISP OPER;输出运算符MOVDI,2 CALL PRINT;输出第二个操作数DISP=CMPFLAG,1 JNZ NEXT5 DISP-NEXT5:MOVDI,4 CALL PRINT;输出结果MOV AH,4CH INT 21H;-输入函数-I
6、NPUT PROC NEAR PUSH AX PUSH BX PUSH CX PUSH DX MOV BX,0;BX存放十进制数CLC MOV DX,0 L0:;-输入数字-MOVAH,1;判断数字是否合法INT 21H CMPAL,0DH JE L2 SUBAL,30H JL L1 CMPAL,9 JG L1;-转换成十进制数-MOVAH,0;转换成 10进制数放入内存XCHG AX,BX MOVCX,10 MULCX ADDAX,BX JC L1 XCHG AX,BX JMP L0 L1:MOVDX,0 MOVBX,0 CALL NEWLINE CALL ERROR JMP L0 L2:C
7、MPDX,0 JNZ L1 MOVDATASI,BX MOVDX,0 POPDX POPCX POPBX POPAX RET INPUT ENDP;-提示信息-DISPL PROC NEAR MOV AH,9 MOV DX,DI INT 21H RET DISPL ENDP;-回车换行-NEWLINE PROC NEAR PUSH AX PUSH DX MOVDL,0AH MOVAH,2 INT 21H MOVDL,0DH MOVAH,2 INT 21H POPDX POPAX RET NEWLINE ENDP;-错误提示-ERROR PROC NEAR PUSH AX PUSH DX MOV
8、DX,OFFSET DATA1 MOVAH,9 INT 21H POPDX POPAX RET ERROR ENDP;-显示函数-PRINT PROC NEAR PUSH AX PUSH BX PUSH CX PUSH DX MOVCX,0 MOVBX,10 MOVAX,DATADI LAST:MOVDX,0;除10压栈DIVBX PUSH DX INCCX CMPAX,0 JNZ LAST AGE:POPDX;出栈输出ORDX,30H MOVAH,2 INT 21H LOOP AGE POPDX POPCX POPBX POPAX RET PRINT ENDP CODES ENDS END START