Ask Question, Ask an Expert

+61-413 786 465

info@mywordsolution.com

Ask C/C++ Expert


Home >> C/C++

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.

C/C++, Programming

  • Category:- C/C++
  • Reference No.:- M9523837

Have any Question?


Related Questions in C/C++

Assignment word matchingwhats a six-letter word that has an

Assignment: Word Matching What's a six-letter word that has an e as its first, third, and fifth letter? Can you find an anagram of pine grave. Or how about a word that starts and ends with ant (other than ant itself, of ...

Question 1find the minimum and maximum of a list of numbers

Question: 1. Find the Minimum and Maximum of a List of Numbers: 10 points File: find_min_max.cpp Write a program that reads some number of integers from the user and finds the minimum and maximum numbers in this list. Th ...

1 implement the binary search tree bst in c using the node

1. Implement the Binary Search Tree (BST) in C++, using the Node class template provided below. Please read the provided helper methods in class BST, especially for deleteValue(), make sure you get a fully understanding ...

Project - space race part a console Project - Space Race Part A: Console Implementation

Project - Space Race Part A: Console Implementation INTRODUCTION This assignment aims to give you a real problem-solving experience, similar to what you might encounter in the workplace. You have been hired to complete a ...

Assign ment - genetic algorithmin this assignment you will

ASSIGN MENT - GENETIC ALGORITHM In this assignment, you will use your C programming skills to build a simple Genetic Algorithm. DESCRIPTION OF THE PROGRAM - CORE REQUIREMENTS - REQ1: Command-line arguments The user of yo ...

What are the legal requirements with which websites must

What are the legal requirements with which websites must comply in order to meet the needs of persons with disabilities? Why is maximizing accessibility important to everyone?

Software development fundamentals assignment 1 -details amp

Software Development Fundamentals Assignment 1 - Details & Problems - In this assignment, you are required to answer the short questions, identify error in the code, give output of the code and develop three C# Console P ...

There are several ways to calculate the pulse width of a

There are several ways to calculate the pulse width of a digital input signal. One method is to directly read the input pin and another method (more efficient) is to use a timer and pin change interrupt. Function startTi ...

Why do researcher drop the ewaste and where does it end

Why do researcher drop the ewaste and where does it end up?

  • 4,153,160 Questions Asked
  • 13,132 Experts
  • 2,558,936 Questions Answered

Ask Experts for help!!

Looking for Assignment Help?

Start excelling in your Courses, Get help with Assignment

Write us your full requirement for evaluation and you will receive response within 20 minutes turnaround time.

Ask Now Help with Problems, Get a Best Answer

Why might a bank avoid the use of interest rate swaps even

Why might a bank avoid the use of interest rate swaps, even when the institution is exposed to significant interest rate

Describe the difference between zero coupon bonds and

Describe the difference between zero coupon bonds and coupon bonds. Under what conditions will a coupon bond sell at a p

Compute the present value of an annuity of 880 per year

Compute the present value of an annuity of $ 880 per year for 16 years, given a discount rate of 6 percent per annum. As

Compute the present value of an 1150 payment made in ten

Compute the present value of an $1,150 payment made in ten years when the discount rate is 12 percent. (Do not round int

Compute the present value of an annuity of 699 per year

Compute the present value of an annuity of $ 699 per year for 19 years, given a discount rate of 6 percent per annum. As