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

C program to demonstrate pointer to variable, C program to demonstrate Poin...

C program to demonstrate Pointer to variable: void p2a(int *); void main() {                 int x=10, *a,**b;                 int arr[5];                 //poin

Write a program read words from file, Write a program IdentyfyWordsMain.jav...

Write a program IdentyfyWordsMain.java reading a fi le (like HistoryOfProgramming) and divide the text into a sequence of words (word=sequence of letters). Save the result in a new

Do friends break encapsulation?, A: No. If they're utilized properly, they ...

A: No. If they're utilized properly, they increase encapsulation. You frequently require splitting a class in half while the two halves will have distinct numbers of instances o

Blanche has a fashion design company called BLB_Best_Clothin, Blanche has a...

Blanche has a fashion design company called BLB_Best_Clothing Pty. That she has just opened recently

Abstract array - c program, Abstract array - c program: AbstractArray:...

Abstract array - c program: AbstractArray::AbstractArray( int anUpper, int aLower, sizeType aDelta ) {     PRECONDITION( anUpper >= aLower );     lastElementIndex = a

Demonstration using moss open source machine translation, Demonstration usi...

Demonstration using Moss Open Source Machine Translation Project Description: Moses is an open source Statistical Machine Translation System. I need someone to provide me a d

Setup and source code with nice user interface, Virtual Webcam effects - Se...

Virtual Webcam effects - Setup and Source code with Nice User Interface Project Description: I want a similar webcam effects application which can apply effects to webcam fee

Function declarations and function definitions in cpp, FUNCTIONS It refe...

FUNCTIONS It refers to a subprogram that is meant to do a certain task. It is basically used to execute a set of operations and return information to the main or calling functio

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