develop a calculator in masm, C/C++ Programming

Part 1

Assignment:  develop a calculator in MASM.

Text chapters covered:  1 through 4, 5.4, 5.5, 6.3, 7.4

You will develop a "calculator" algorithm in MASM using reverse-polish notation (RPN).  RPN is an extremely useful technique to calculate algebraic expressions.  It is used by many compilers to process algebraic statements.  Your calculator will evaluate RPN expressions using INTEGER values only.

RPN is a postfix expression, meaning that the operation (add, subtract, etc.) follows the variables.  Contrast this to an algebraic expression where the operations are situated between the variables (this is called infix notation).  Here's an example of an RPN expression and its algebraic equivalent:

                        Algebraic equation:     E(A+B)/C-D

                        RPN equivalent:          AB+E*CD-/

To compute an RPN expression is a straightforward process using stacks.  Assume that the RPN expression is always presented as a character string.  The algorithm to do this is shown below.

The assignment: apply this algorithm to your MASM program

1.      If the character encountered is a variable, push the variable on the stack.

2.      If the character encountered is a operator (+,-,*,/), pop the top two variables off the stack, perform the operation on them, then push the result back on the stack.

3.      Continue steps 1 and 2 until the string is exhausted.  The last value on the stack is the final result.

Taking a simple algebraic expression, (5+6)*(3-1), and its RPN equivalent, 56+31-*, the following is an example of how the algorithm works.

Input RPN string: 56+31-*

 Step 1: get first character, 5.  It's a variable, push on stack.              Stack contents:  5.

Step 2: get second character, 6.  Push on stack.                                 Stack contents: 6, 5.

Step 3: get third character, +.  It's an operator.  Pop top two values from stack, perform operation, push result back on stack.                                              Stack contents:  11.

Step 4: get fourth character, 3.  Push on stack.                                  Stack contents:  3, 11.

Step 5: get fifth character, 1.  Push on stack.                                     Stack contents: 1, 3, 11.

Step 6: get sixth character, -.  Pop top two values from the stack, subtract them, push result back on stack.  Note that you subtract the first popped value from the stack from the second.  In this case, it's 3-1 = 2.  Push result back on stack.                                       Stack contents:  2, 11.

Step 7: get seventh character, *.  Multiply top two values on stack.  Push result back on stack.

                                                                                                            Stack contents:  22.

Step 8: end of string, stop.  This can be determined by a special character at the end of the string, or through knowing the length of the string to control a loop counter.  Result is at top of stack.


1)      Declare the following variables in your data segment:



; These are the values of the variables


Variables               DWORD         17                ; A

DWORD         12                                            ; B

DWORD         4                                              ; C

DWORD         10                                            ; D

DWORD         5                                              ; E


; This is the expression to evaluate . . .


Expression BYTE              "AB-C*DE-/"                                      ; expression to evaluate

2)      Create two procedures.  The first procedure should be the RPN calculator and is called from your main program procedure.  The parameters you pass to the RPN procedure from the main program are:

a)      The address of the source RPN string.

b)      The address of your variables list.

c)      The length of our source RPN string.

You may choose any registers to use for passing parameters.

The RPN procedure returns the calculated result in the EAX register.

The RPN procedure must use the stack as discussed in Chapter 5 to compute the result.

3)      The second procedure is called from your RPN procedure.  It determines if the character the RPN procedure is looking at is a variable or operator.  The parameters you pass to the second procedure are:

a)      The character being examined.

b)      Return code in EAX.  Zero if a variable, one if an operator.

4)      Both procedures should be called using the CALL instruction ONLY.

5)      When processing the RPN expression, your variable references should map to the variables array.  In other words, your program logic should locate the first DWORD value in the variables list from the letter "A", the second DWORD value from the variable "B", and so on.  You CANNOT hardcode numeric values in the RPN string!

6)      Create a second set of variables and an associated RPN string of your choosing.  The second set must consist of AT LEAST four variables.  I can provide examples if you need them.

7)      Run your program ONCE with both sets of variables and RPN strings.  Your main program should call the RPN procedure once with the first string, then a second time immediately thereafter with the second string.  Save both results in your data segment.

8)      Your main program and all procedures should be in the same .asm file.

9)      Submit the following for grading:

a)      Your source code (softcopy).

b)      A listing of your source code and a memory dump showing the results of both calls to the RPN procedure.

Posted Date: 2/23/2013 12:01:54 AM | Location : United States

Related Discussions:- develop a calculator in masm, Assignment Help, Ask Question on develop a calculator in masm, Get Answer, Expert's Help, develop a calculator in masm Discussions

Write discussion on develop a calculator in masm
Your posts are moderated
Related Questions

(a) Verify the values of the resistors with the following colour coding: (i) red-red-orange-silver (ii) orange-orange-black-blue-green (b) What is the value of a resistor

Byteland county is very famous for luminous jewels. Luminous jewels are used in making beautiful necklaces. A necklace consists of various luminous jewels of particular colour. Nec

The STL details are described in many places online (see the CS377 webpage for some links), and there's a very quick introduction in Section A.14. Here are just a few additional no


c++ coding decode the letters from numbers

#include void func(int num, b=5) { auto int total=0; static int sum=0; for ( int i=num; i>0 ; i--) total+=i; sum+=total; cout

find the area of a curve y=f(x) between x=a and x=b integrate f(x) between limits a and b   #include float start_point, /* GLOBAL VARIABLES */