A simple expression calculator written in 32-bit Assembly Language

Source: Internet
Author: User

The main content, which can be supplemented later.

First paste the C language code

# Include <stdio. h> # include <stdlib. h> // define status # define st_nil 0 // initial status # define st_num 1 // operand status # define st_op 2 // operator status # define st_end 3 // end status int calc (INT N1, int N2, char sign) {Switch (sign) {Case '+': Return N1 + N2; Case '-': Return n1-n2; Case '*': Return N1 * N2; case '/': Return N1/N2; default: Return 0 ;}} int num_arr [2]; // operand array int op_arr [2]; // operator array int num_count; // Number of operands int op_count; // number of operators // mode = 0. When an operator is encountered, the expression in the calculation part is // mode = 1. When the expression is an Terminator, the entire expression is calculated. // use the global variable num_arr and op_arrvoid do_calc (INT mode) {Switch (op_count) {Case 0: break; // you do not need to calculate Case 1: // at this time, there should be two operands and one operation, the expression may be in the form of "1 + 2" If (mode = 1) {num_arr [0] = calc (num_arr [0], num_arr [1], op_arr [0]); num_count = 1; op_count = 0;} break; Case 2: // at this time, there should be two operands and two operation symbols, the expression may be in the form of "1 + 2 *" num_arr [0] = calc (num_arr [0], num_arr [1], op_arr [0]); op_arr [0] = op_arr [1]; num _ Count --; op_count --; break; default: printf ("error !!! \ N "); // It should not be break here;} int calc_expression (char * szstring) {int curr_num; // number of input in progress. When an operator or string ends, copy curr_num to num_arr int pre_status, // The Last curr_status, // The Current Status st_comb; // the combination of the last status and Current Status char * P = szstring; pre_status = st_nil; num_count = 0; // Number of operands obtained op_count = 0; // number of operators obtained curr_num = 0; while (1) {char CH = * P ++; If (CH> = '0' & Ch <= '9') curr_status = st_num; else if (CH = '+' | CH = '-' | CH = '*' | CH = '/') curr_status = st_op; else curr_status = st_end; // There are four possible values for the previous status, and four possible values for the current status. There are 16 combinations of these values, among them, only 5 statuses are valid st_comb = pre_status * 4 + curr_status; Switch (st_comb) {Case st_nil * 4 + st_num: // The last status is st_nil, the current status is st_num case st_op * 4 + st_num: // The last status is st_op, and the current status is st_num case st_num * 4 + st_num: // The last status is st_num, the current status is st_num curr_num = curr_num * 10 + CH-'0'; break; Case st_num * 4 + st_op: // The last status is st_num, the current status is st_op num_arr [num_count ++] = curr_num; // the operand is saved to the array curr_num = 0; // prepares for the next input op_arr [op_count ++] = CH; // The operator stores the array do_calc (0); break; Case st_num * 4 + st_end: // The last status is st_num, and the current status is st_end num_arr [num_count ++] = curr_num; // curr_num = 0; // prepare do_calc (1) for the next input; return num_arr [0]; default: printf ("the % DTH char is invalid \ n ", p-szstring); Return 0;} pre_status = curr_status;} int main () {// char * string = "1234 + 5678-1111*9876/4321 "; char buff [256]; printf ("Please input an expression:"); gets (buff); int r = calc_expression (buff ); printf ("% s = % d \ n", buff, R); Return 0 ;}

 

Then paste the assembly language code

A simple expression calculator assembler program that uses the masm32 syntax for programming to generate a 32-bit console program running in windows. Functions and restrictions; only supports the input of 10-digit integers and four-digit operators, negative numbers are not supported. The intermediate results and final results of the calculation process can be negative numbers. They are calculated from left to right, and the operator priority is not supported; the intermediate and final results in the calculation process do not exceed the 32-Bit Signed Integer Range. For example, input "1234 + 5678-1111*9876/4321" and output "13258 "; how to compile and link this program; this program requires masm32 for compilation. If your masm32 is installed on the C drive, set the following environment variables; set include = c: \ masm32 \ include; set Lib = c: \ masm32 \ Lib; Set Path = c: \ masm32 \ bin; % PATH %; if the program name is test. ASM, then run the following command to compile and link and run; ml/C/coff Calc. ASM; Link/subsystem: Console test. Calc; calc.exe. 386. Model flat, stdcall; 32 bit memory modeloption Casemap: none; Case sensitiveinclude windows. incinclude login user32.libinclude login kernel32.lib. Data? Inbuffer dB 256 DUP (?); Input buffer outbuffer dB 32 DUP (?); Output buffer houtput dd? Hinput dd? Num_arr DD 2 DUP (?); Operand array op_arr DD 2 DUP (?); Operator number array num_count dd?; Number of operands op_count dd?; Number of operators: curr_num dd? Pre_status dd? Curr_status dd? St_comb dd? Tlen dd?; Only used for API function calls. const szfmtout1 dB '% d', 0 szoutstring dB 'Please input a expression :'. codest_nil equ 0; initial state st_num equ 1; operand state st_op equ 2; operator state st_end equ 3; end state; 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 EBX, var_n2 mov ECx, var_signcheck_01: CMP Cl, '+' jnz check_02 Dd eax, ebx jmp this_exitcheck_02: CMP Cl, '-' jnz check_03 sub eax, ebx jmp this_exitcheck_03: CMP Cl, '*' jnz check_04 imul ebx jmp limit: CMP Cl, '/'jnz err XOR edX, EDX idiv ebx jmp this_exiterr: XOR eax, eax this_exit: retcalc endp; var_mode: input parameter,; mode = 1, expression analysis completed, 0: there are currently four Arithmetic Operators; Return Value: none; this procedure work on global array num_arr, op_arr, num_count, op_countdo_calc Proc far C uses EBX ECx ESI, var_mode mov ESI, op_count check_0:; op_count = 0? Cmp esi, 0 JZ this_exit; do nothingcheck_1:; op_count = 1? Cmp esi, 1 jnz check_2 mov edX, var_mode; If mode = 0, do nothing or edX, edx jz this_exit mov ECx, op_arr [0] invoke calc, num_arr [0], num_arr [1*4], op_arr [0] mov num_arr [0], eax mov num_count, 1 mov op_count, 0 JMP this_exit check_2:; op_count = 2? Cmp esi, 2 jnz err_input invoke calc, num_arr [0], num_arr [1*4], op_arr [0] mov num_arr [0], eax mov eax, op_arr [1*4]; op_arr [0] = op_arr [1] mov op_arr [0], eax dec num_count; num_count -- dec op_count; op_count -- JMP this_exit; op_count = 1? Err_input:; do nothing, only returnthis_exit: retdo_calc endp; var_string: input parameter, the expression; eax: output parameter, the resultcalc_exp proc far C uses esi edi ebx ECx, var_string mov ESI, var_string init_vars: XOR ECx, ECx XOR eax, eax mov num_count, eax; num_cont = 0 mov op_count, eax; op_count = 0 mov curr_num, eax; curr_num = 0 mov pre_status, st_nil; pre_status = st_nil loop_start:; the loop entrycheck_0to9: mov Cl, [esi] Inc esi cmp Cl, '0' JL check_add_sign CMP Cl, '9' JG Protocol: mov curr_status, st_num JMP Protocol: CMP Cl, '+ 'jz do_ch_is_op check_sub_sign: CMP Cl,'-'JZ do_ch_is_op check_mul_sign: CMP Cl, '* 'jz ready check_div_sign: CMP Cl,'/'JZ do_ch_is_op JMP ready: mov curr_status, st_op JMP next_02 ready: mov curr_status, st_end JMP next_02 next_02: mov eax, pre_status SHL eax, 2 add eax, curr_status mov st_comb, eax; st_comb = pre_status * 4 + curr_statuscheck_01: CMP st_comb, st_nil * 4 + st_num; last status is st_nul, curr status is st_num, JZ case_01 check_02: CMP st_comb, st_op * 4 + st_num; last status is st_op, curr status is st_num JZ metadata: CMP st_comb, st_num * 4 + st_num; last status is st_num, curr status is st_num JZ case_01check_04: CMP st_comb, st_num * 4 + st_op; last status is st_num, curr status is st_op JZ case_02 check_05: CMP st_comb, st_num * 4 + st_end; last status is st_num, curr status is st_end JZ case_03 JMP case_error; invalid status, jump to case_errorcase_01:; st_comb is st_nil * 4 + st_num, st_op * 4 + st_num or st_num * 4 + st_num mov edX, curr_num SHL edX, 1; edX = curr_num * 2 Lea eax, [edX + EDX * 4]; eax = curr_num * 10 sub Cl, '0' movzx edX, CL add eax, EDX mov curr_num, eax; curr_num = curr_num + Cl-'0' JMP next_loopcase_02 :; st_comb is st_num * 4 + st_op mov EBX, num_count mov eax, curr_num mov num_arr [EBX * 4], eax; num_arr [num_count ++] = curr_num Inc EBX; num_count ++ mov num_count, EBX mov curr_num, 0 mov EBX, op_count movzx eax, Cl; op_arr [op_count ++] = curr char mov op_arr [EBX * 4], eax Inc EBX; op_count ++ mov op_count, EBX invoke do_calc, 0 JMP next_loop case_03:; st_comb is st_num * 4 + st_end mov eax, curr_num mov EBX, num_count mov num_arr [EBX * 4], eax; num_arr [num_count ++] = curr_num Inc EBX; num_count ++ mov num_count, EBX; MoV curr_num, 0 invoke do_calc, 1 JMP this_exit; return num_arr [0] case_error: XOR eax, eax mov num_arr [0], eax JMP this_exit; invliad case, return 0 next_loop: mov edX, curr_status; pre_status = curr_status mov pre_status, edx jmp ready: mov eax, num_arr [0] retcalc_exp endp main proc farstart: invoke getstdhandle, std_output_handle; get the handle of the console output device mov houtput, eax invoke writeconsole, houtput, ADDR szoutstring, sizeof szoutstring, ADDR tlen, 0 invoke getstdhandle, role; get the handle of input devices in the console mov hinput, eax invoke readfile, hinput, ADDR inbuffer, sizeof inbuffer, ADDR tlen, null invoke calc_exp, ADDR inbuffer; Calculation Expression inbuff, mov num_arr [0], eax; save this number to number invoke wsprintf, ADDR outbuffer, ADDR szfmtout1, num_arr [0] invoke writeconsole, houtput, ADDR outbuffer, eax, ADDR tlen, 0 XOR eax, eax invoke exitprocess, eax; exit process RET main endpend main

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.