;#Mode=Dos;Title:数学表达式计算;Declare:仅支持 整数(0-65535),+,-,*,/,()运算符;Date:2012-11-15;author:53101512 ZhongQiangdata segment expression db 50,?,40 dup(0),'$';表达式数组 message db 50 dup('*'),0dh,0ah,'Please Input Expression:',0dh,0ah,'$';输入提示 message2 db 0dh,0ah,50 dup('*'),0dh,0ah,'The Answer is: ',0dh,0ah,'$';输出提示 top db 50 dup('*'),0dh,0ah, '*',' Integer(0-65535),Support +,-,*,/,() ','*',0dh,0ah, '*',' example input (45+32)/7-10 ','*',0dh,0ah, '*',' Date: 2012-11-15 ','*',0dh,0ah, '*',' Id: 53101512 ','*',0dh,0ah, '*',' Name: ZhongQiang ','*',0dh,0ah, 50 dup('*'),0dh,0ah,'$' token db ? arg db ? result dw ? ;结果 nozero db 0 ;十进制输出辅助data ends code segment assume cs:code, ds:data start: mov ax,data mov ds,ax;说明 lea dx,top call printstring;输入提示信息 lea dx,message call printstring;输入算数表达式 lea dx,expression mov ah,0ah int 21h ;表达式解析 lea si,expression add si,2 mov al,[si] inc si mov [token],al call exp mov [result],dx;输出提示及结果输出 lea dx,message2 call printstring mov bx,result call outputdec;按任意键返回 mov ah,1 int 21h ;返回DOS mov ah, 4ch int 21hexp proc local temp:WORD ;定义局部变量 call term mov temp,dxwhile1: cmp al,'+' je add_ cmp al,'-' je sub_ jmp end1 add_: mov cl,'+' mov [arg],cl call match call term add temp,dx jmp while1 sub_: mov cl,'-' mov [arg],cl call match call term sub temp,dx jmp while1 end1: mov dx,temp retexp endp term proc local temp:WORD ;定义局部变量 call factor mov temp,dxwhile2: cmp al,'*' je mul_ cmp al,'/' je div_ jmp end_ mul_: mov cl,'*' mov [arg],cl call match call factor mov cx,ax mov ax,temp mul dx mov [temp],ax mov ax,cx jmp while2div_: mov cl,'/' mov [arg],cl call match call factor mov cx,ax mov ax,temp div dl xor ah,ah mov [temp],ax mov ax,cx jmp while2end_: mov dx,temp retterm endpfactor proc local temp:WORD ;定义局部变量 cmp al,'(' je lef xor cx,cx mov cl,al xor ax,axwhile3: sub cl,'0' mov BL,10 imul BL add ax,cx mov cl,[si] inc si cmp cl,'0' jb end3 cmp cl,'9' ja end3 jmp while3 lef: mov cl,'(' mov [arg],cl call match call exp mov temp,dx mov cl,')' mov [arg],cl call match jmp endx end3: mov dx,ax dec si mov al,[si] inc si retendx: mov dx,temp retfactor endpmatch proc cmp al,arg je true true: mov al,[si] inc si retmatch endp;十进制输出子程序outputdec proc push cx mov nozero,0 mov cx,10000 call main mov cx,1000 call main mov cx,100 call main mov cx,10 call main mov cx,1 call main cmp nozero,0 jne ll mov dl,30h call printchar ll: pop cx ret main proc mov ax,bx mov dx,0 div cx mov bx,dx mov dl,al cmp dl,0 jne ll1 cmp nozero,0 jne ll1 jmp ll2 ll1: mov nozero,1 add dl,30h call printchar ll2: ret main endpoutputdec endp;字符串输出子程序printstring proc mov ah,9 int 21h retprintstring endp;字符输出子程序printchar proc mov ah,2 int 21h retprintchar endpcode ends end start