You are on page 1of 2

EEC 70: Computer Structures and Assembly Programming

Winter Quarter 2013

Laboratory Exercise 3: Arithmetic Expression Evaluator Due Date: January 31, 2013, 11:55PM Full Points 100

Purpose of the Exercise


The goal of this exercise is to learn how computers deal with numbers. You will learn that all input is done using characters. The characters are then converted into the appropriate representation. Specically, you will see how real numbers are read by the SAL program using the get ch command. This program will further enhance your understanding of procedures in SAL and representation of numbers and ASCII characters. Also as a bonus you will learn how to write a simple parser which is an important and essential component of any language translation program like assemblers, compilers, interpreters etc.

Detailed Description of the Problem


For a computer to process an arithmetic expression the syntax of the expression should be PRECISE. We specify the syntax precisely by a set rules also known as the grammar for the arithmetic expression. EXPR Num2 Num1 OP :: :: :: :: Num1 Num2CR | -Num1 Num2CR OP Num1 | Num2 real number + | - | * | /

where | stands for OR and things in single quotes are literals and CR stands for the return key on your keyboard and :: stands for is dened by. Note that there is EXACTLY ONE space between components of the expression and there is NO space after the last number and the PRECEDENCE of the operators is left to right. Also note that there is no space after the optional sign in the rst operand. So, the grammar above tells us that the following are valid expressions 1.34 -2.3 -2.3 1.34 0.34 + + + / * 1.0 4.56 4.56 * 3.5 2.3 * 3.5356445 + 3.3 - 18.39394 4.31 - 3.443

and expressions listed below are INVALID and the reason is given in parenthesis 1 + 3.2 1.0+2.034 0.1345 + 34.3 (all numbers have to be floats. Integers are not allowed) (there has to be a space after 1.0 and +) (too many spaces after 0.1345)

1.0 + -2.34 39586.0 .394 * 34.34 / 4.0 30. / 3.0

(only the first number can have a sign) (an expression is more than one number) (.394 is not correct, it has to be 0.394) (It should be 30.0)

You should write a SAL program that evaluates the expression and outputs the answer followed by newline. After evaluating one expression, your program should prompt the user to enter another expression. The program should terminate if the input is Q or X. If the input does not meet the rules specied by the expression grammar above, your program should report an error (with a friendly message) and should give the user a chance to enter a new expression from the beginning.

Useful Hints
1. The key idea in this program is to read character inputs from the keyboard using the get ch instruction and convert them into a real number. You should consult the example in Figure 4.7 on page 103 of your textbook to get an idea of how to do that. Note that the example converts ASCII characters into integers. You have to convert them into a real number. 2. The grammar for the expression is also good way to structure your program and you should identify the repetitive parts of the program and encapsulate them into procedures. 3. You will need the SAL instruction cvt x, y, which converts the value in y into the type of x. This is useful to convert an integer into a real number.

What should you do?


Write the SAL assembly program and verify its operation using the SPIMSAL simulator. Verify the operation of your program with valid and invalid inputs and boundary conditions. Make sure that you check syntax errors. Inputs that do not match the specication described above should be reported as errors. One of the reasons people write assembly programs is for eciency. So, you should make sure that your program does not have any redundant data or instructions. Add sucient comments to your code so that the TAs can understand your program. Make sure you put your name and ID and your partners name and ID as a comment at the top of the program. Submit the veried program using SmartSite

You might also like