要求:
输入一个表达式,输出十进制结果,例如:
输入1234+5678-1111*9876/4321
输出13258
注意:表达式中只允许出现+-*/,没有其它运算符,表达式总长度不超过80字符;
运算过程不考虑优先级,一律从左到右;
计算除法时只要商,不要余数;
计算过程中算得的中间值以及最终结果都假定不会超过32位;
程序中可以使用32位寄存器及386指令。
初学汇编,实在是编不粗来,求解答求源码。。。~~~~(>_<)~~~~
------解决方案--------------------------------------------------------
编出来了在masm32下编译通过,测试通过,代码见下
- Assembly code
;要求:;输入一个表达式,输出十进制结果,例如:;输入1234+5678-1111*9876/4321;输出13258;注意:表达式中只允许出现+-*/,没有其它运算符,表达式总长度不超过80字符;;运算过程不考虑优先级,一律从左到右;;计算除法时只要商,不要余数;;计算过程中算得的中间值以及最终结果都假定不会超过32位;;程序中可以使用32位寄存器及386指令。 ; 如何编译链接该程序; 本程序需使用masm32来编译,若你的masm32安装在c盘,请设置如下环境变量; set include=c:\masm32\include; set lib=c:\masm32\lib; set PATH=c:\masm32\bin;%PATH%; 若本程序名为test.asm,则运行如下命令编译和链接和运行;ml /c /coff test.asm; link /subsystem:console test.obj; test.exe.386.model flat,stdcall ; 32 bit memory modeloption casemap :none ; case sensitiveinclude windows.incinclude user32.incincludelib user32.libinclude kernel32.incincludelib kernel32.lib.data inBuffer db 256 dup(?) ;输入缓冲区 outBuffer db 256 dup(?) ;输出缓冲区 hOutput dd ? hInput dd ? num1 dd ? ;被分解的数 num2 dd ? ;被分解的数的平方 result dd ? count dd ? pre_status dd ? curr_status dd ? curr_num dd ? st_comb dd ? dwLen dd ? tLen dd ? curr_sign db ? pre_sign db ? .const szFmtout1 db '%d',0 szOutString db 'Please input a expression',13,10 .codeST_NIL equ 0 ;初始状态ST_NUM equ 1 ;操作数状态ST_OP equ 2 ;操作符状态ST_END equ 3 ;结束状态;var_N1: input parameter, the first number;var_N2: input paramenter, the second number;var_sign: op signcalc proc far C uses ebx ecx, var_N1,var_N2, var_sign mov eax,var_N1 mov edx,var_N2 mov ecx,var_sign mov ebx,edxcheck_01: cmp cl, '+' jnz check_02do_add: add eax,ebx jmp this_exitcheck_02: cmp cl, '-' jnz check_03do_sub: sub eax,ebx jmp this_exitcheck_03: cmp cl, '*' jnz check_04do_mul: imul ebx jmp this_exitcheck_04: cmp cl, '/' jnz errdo_div: xor edx,edx idiv ebx jmp this_exiterr: xor eax,eax this_exit: retcalc endp ;var_string: input parameter,the expression address;eax: out parameter, the resultcalc_exp proc far C USES esi edi ebx ecx ,var_string mov esi,var_string xor eax,eax xor ecx,ecx mov count,eax mov pre_status,ST_NIL loop_start: mov cl,[esi] inc esicmp_01: cmp cl,'0' jl cmp_11 cmp_02: cmp cl,'9' jg cmp_11case_01: mov curr_status,ST_NUM jmp next_02 cmp_11: cmp cl,'+' jz case_02cmp_12: cmp cl,'-' jz case_02cmp_13: cmp cl,'*' jz case_02cmp_14: cmp cl,'/' jz case_02 jmp case_03case_02: mov curr_status,ST_OP jmp next_02case_03: mov curr_status,ST_END jmp next_02 next_02: mov eax,pre_status shl eax,2 add eax,curr_status mov st_comb,eaxcheck_01: cmp st_comb,ST_NIL*4+ST_NUM ; 上次状态是ST_NIL,当前状态是ST_NUM jnz check_02case1_01: sub cl,'0' mov curr_num, ecx jmp next_loopcheck_02: cmp st_comb,ST_OP*4+ST_NUM ; 上次状态是ST_OP,当前状态是ST_NUM jnz check_03case1_02: sub cl,'0' mov curr_num, ecx jmp next_loopcheck_03: cmp st_comb,ST_NUM*4+ST_OP ; 上次状态是ST_NUM,当前状态是ST_OP jnz check_04case1_03: mov curr_sign,cl cmp count,0 jnz case1_03_02case1_03_01: mov eax,curr_num mov num1,eax jmp case1_03_nextcase1_03_02: mov edx,curr_num mov num2,edx mov eax,num1 mov cl,pre_sign invoke calc,eax,edx,ecx mov num1,eaxcase1_03_next: inc count mov cl,curr_sign mov pre_sign,cl jmp next_loop check_04: cmp st_comb,ST_NUM*4+ST_END ; 上次状态是ST_NUM,当前状态是ST_END jnz check_05 cmp count,0 jnz case1_04_02case1_04_01: mov eax,curr_num mov num1,eax jmp case1_04_nextcase1_04_02: mov edx,curr_num mov num2,edx mov eax,num1 mov cl,pre_sign invoke calc,eax,edx,ecx mov num1,eaxcase1_04_next: inc count jmp this_exitcheck_05: cmp st_comb,ST_NUM*4+ST_NUM ; 上次状态是ST_NUM,当前状态是ST_NUM jnz check_06case1_05: mov edx,curr_num shl edx,1 lea eax,[edx+edx*4] sub cl,'0' movzx edx,cl add eax,edx mov curr_num,eax jmp next_loopcheck_06: ; 无效的状态,直接退出 jmp this_exit next_loop: mov edx,curr_status mov pre_status,edx jmp loop_startthis_exit: mov eax,num1 retcalc_exp endp main proc farstart: invoke GetStdHandle,STD_OUTPUT_HANDLE ; 获取控制台输出设备的句柄 mov hOutput,eax invoke WriteConsole,hOutput,addr szOutString,sizeof szOutString,addr tLen,0 invoke GetStdHandle,STD_INPUT_HANDLE ; 获取控制台输入设备的句柄 mov hInput, eax invoke ReadFile,hInput,addr inBuffer,sizeof inBuffer,ADDR tLen,NULL lea esi, inBuffer invoke calc_exp,esi ;转化字符串为一个数 mov result, eax ;将这个数存入number invoke wsprintf, addr outBuffer , addr szFmtout1,result mov dwLen, eax invoke WriteConsole,hOutput,addr outBuffer,dwLen,addr tLen,0 xor eax, eax invoke ExitProcess,eax ;退出进程 ret main endpend main