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

Assignment Help:

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:

.data

;

; 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.


Related Discussions:- develop a calculator in masm

Explain in brief about the one-definition rule, Explain one-definition rule...

Explain one-definition rule (ODR). According to one-definition rule, C++ constructs should be identically defined in each compilation unit they are used in. As per ODR, two

Sparce matrices, Write an algorithm for multiplication of two sparse matric...

Write an algorithm for multiplication of two sparse matrices using Linked Lists.

String routines in c, Available in the string.h header file are the followi...

Available in the string.h header file are the following routines         strcat(string1,string2);   This joins string2 to string1; care must be taken to allocate enough space for

Explain public and private members, Public, Private and Protected members: ...

Public, Private and Protected members: Class members can either be declared in public','protected' or in the 'private' sections of the class. But as one of the features of OOP i

Sort wars, Explain each of the algorithms in a way that would be understand...

Explain each of the algorithms in a way that would be understandable to an intelligent person who is not familiar with programming. You should not use any code (or even pseudo code

Arrays and pointers, i have an array of structs, and am trying to make a po...

i have an array of structs, and am trying to make a pointer that can point to a certain struct within the array

Explain the symbolic constants in c language, Explain the Symbolic Constant...

Explain the Symbolic Constants in c language? Symbolic constants are the constants of any type that declared by using the #define compiler directive and it is a preprocessor di

Opp, what is oops

what is oops

Write Your Message!

Captcha
Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd