简单计算器程序设计2010-01-20 16:441、编写一个程序实现十进制加减乘除运算2、在命令提示后输入算式,运算符号不超过四个3、回车后进行计算并以十进制显示结果;能做带括号的运算,输入错误能报警data segmentmsg1 db 'Please Input an Expression:',0AH,0DH,'$' msg2 db 'the result is:$'db '('num db 200 dup (0)temp1 db 200 dup(0),13temp dw 0data endsstack segmentdb 200 dup (?)stack endscode segmentassume cs:code,ds:data,ss:stackstart:mov ax,datamov ds,axlea dx,msg1mov ah,9int 21hcall inputagain2:lea si,numagain1:inc sicmp byte ptr[si],13je outtcmp byte ptr[si],')'jne again1call chasc1call countpush axjmp again2outt:pop bxmov ah,2mov dl,10int 21hmov dl,13int 21hlea dx,msg2mov ah,9int 21hcall chaoumov ah,8int 21hmov ah,4chint 21h;************************************** input proc;输入子程序能纠错push axpush bxpush cxpush dxpush sipush dixor si,sixor bh,bhmov bl,0ffhxor cx,cxtop:mov ah,8int 21hcmp al,8jne next1cmp si,0jne ne1mov bl,0ffhjmp next6ne1:mov ah,2mov dl,8int 21hmov dl,0int 21hmov dl,8int 21hdec sicmp byte ptr[num+si],'(' jne ndec clmov bl,0ffhjmp nextn:cmp byte ptr[num+si],')' jne ne2dec chxor bl,bljmp nextne2:cmp byte ptr[num+si-1],')' jne ne5xor bl,bljmp nextne5:xor bl,blcmp byte ptr[num+si-1],30h jae nextmov bl,0ffhnext:jmp topnext1:cmp al,30hjae next2cmp al,'*'je next7cmp al,'/'je next7cmp al,'-'je next7cmp al,'+'je next7cmp al,'('je next5cmp al,')'je next4cmp al,13je end1next6:mov ah,2mov dl,7int 21hjmp topnext7:cmp bl,0ffhje next6xor bl,0ffhjmp next3next2:cmp al,39hja next6xor bl,blcmp byte ptr[num+si-1],')' je next6jmp next3next4:inc chcmp ch,clja next0cmp byte ptr[num+si-1],')' je next3cmp byte ptr[num+si-1],30h jb next0jmp next3next0:dec chjmp next6next5:cmp si,0je next8cmp byte ptr[num+si-1],')' je next6cmp byte ptr[num+si-1],30h jae next6next8:inc clnext3:mov dl,almov ah,2int 21hmov [num+si],alinc sijmp topend1:cmp ch,cljne next6cmp byte ptr[num+si-1],')'je ou1cmp byte ptr[num+si-1],30hjb next6ou1:mov byte ptr[num+si],')'mov byte ptr[num+si+1],13pop dipop sipop dxpop sipop dxpop cxpop bxpop axretinput endp;**************************************;***************************************** chasc1 proc;提取括号中算式;si为')'的地址往前扫“(”为止pop bplea bx,word ptr[temp1+198]mov byte ptr[si],0n5:mov di,sin4:cmp byte ptr[si],'('je n3n7:dec sicmp byte ptr[si],0je n1cmp byte ptr[si],30hjb n2jmp n7n2:call chasc2mov word ptr[bx],axdec bxn9:cmp byte ptr[si],2ahjb n8mov al,byte ptr[si]mov byte ptr[bx],aldec bxdec bxn8:jmp n5n1:pop word ptr[bx]dec bxn6:mov byte ptr[si],2dec sicmp byte ptr[si],1jne n6mov byte ptr[si],2dec sijmp n9n3:mov byte ptr[si],1inc bxpush bpretchasc1 endp;************************************* ;*********************************** chasc2 proc;ASCII转为十进制push bxpush cxpush dxpush sipush dimov cx,disub cx,sidec cxmov si,1xor bx,bxaga:dec dimov al,byte ptr[di]sub al,30hmul siadd bx,axmov ax,simov si,10mul simov si,axloop agamov ax,bxot:pop dipop sipop dxpop cxpop bxretchasc2 endp;**************************************;*********************************** count proc;四则运算;入口参数:bxxor si,simov di,bxcmul:cmp byte ptr[bx+2],'+'je resccmp byte ptr[bx+2],'-'je rescinc sipush word ptr[bx]resc:inc bxinc bxcmp byte ptr[bx],'*'je nexcmp byte ptr[bx],'/'je nex0cmp byte ptr[bx],13inc bxjmp cmulnex:inc bxpop axmov cx,word ptr[bx] imul cxpush axjmp rescnex0:inc bxpop axxor dx,dxmov cx,word ptr[bx] idiv cxpush axjmp resccadd:mov bx,dishl si,1add sp,simov bp,spsub bp,2mov ax,word ptr[bx] inc bxinc bxcso:cmp byte ptr[bx],'+' je nex1cmp byte ptr[bx],'-' je nex1cmp byte ptr[bx],13 je overmov ax,word ptr[bp] dec bpdec bpabo:inc bxinc bxinc bxcmp byte ptr[bx],'*'je abocmp byte ptr[bx],'/'je abojmp csonex1:mov cl,byte ptr[bx]cmp byte ptr[bx+3],'*'je nex2cmp byte ptr[bx+3],'/'je nex2inc bxmov dx,word ptr[bx]inc bxinc bxjmp nex3nex2:mov dx,word ptr[bp]sub bp,2abo1:inc bxinc bxinc bxcmp byte ptr[bx],'*'je abo1cmp byte ptr[bx],'/'je abo1nex3:cmp cl,'+'jne suadd ax,dxjmp add1su:sub ax,dxadd1:jmp csoover:retcount endp;*********************************;************************************* chaou proc;以十进制形式显示结果xor bp,bpmov ax,bxshl bx,1jnc cupush axmov ah,2mov dl,'-'int 21hpop axxor bx,bxsub bx,axmov ax,bxcu:mov cx,4mov di,10mov bx,10000ga:xor dx,dxdiv bxpush dxcmp bp,0jne bacmp ax,0je zroba:add al,30hmov dl,almov ah,2int 21hinc bpzro:mov ax,bxxor dx,dxdiv dimov bx,axpop axloop gaadd al,30hmov dl,almov ah,2int 21hretchaou endp;******************************************code ends end start。