CMSC 311- Computer Organization

Fall 1995 - Programming Assignment Part "B"

Due (at 10:30 AM) Tuesday, December 12

A Reverse Polish Notation Calculator

The EZ LITE 95 calculator (EZ for short) manipulates integers on a stack. It is run from a terminal. EZ prompts you with ez> The input is either a number or an operation followed by a line feed. A line feed alone just causes the prompt to be printed out.

Numbers are entered as a string of numeric characters optionally preceded by + or - and followed by a line feed. Numbers may have any number of leading zeros, but only numbers less that 10000 in absolute value are permitted. The format for printing numbers is +dddd for nonnegative numbers and -dddd for negative numbers (1 is printed as +0001). Only one number to a line.

The stack has up to 128 entries. Initially the stack is empty. Numbers are pushed onto the stack by entering them from the keyboard. An attempt to enter an illegal number (e.g., 34295 or 43a2) causes EZ to print Bad number and leave the stack unaltered. An attempt to push an item onto a full stack should print Stack Overflow and not change the stack. An attempt to pop an item from an empty stack should result in an Stack Underflow message and not change the stack.

An operation consists of a single nonnumeric character. The arithmetic operations are +, -, *, /. Each of these operations pops two entries from the stack, computes second op top, pushes the result back on the stack, and prints out the result. Division returns the integer part of the quotient. On overflow (i.e., a result greater in absolute value than 9999) or divide by zero, EZ prints out Overflow and pushes nothing on the stack.

The operation c changes the sign of the top entry and prints it.

The operation x exchanges the top and second entry of the stack.

The operation p prints the stack from bottom to top.

The operation k (for kill) pops the top item from the stack and prints it.

The operation q quits the calculator.

An illegal operation causes EZ to print Illegal operation. The stack is unchanged. An illegal operation is any input line that is not a valid operation and does not start with a digit, plus, or minus.

Implementation

To insure some uniformity, you must observe the following conventions.

There are two types of subprograms: formal and informal. A formal subprogram follows the stack-frame conventions. An informal subprogram is one that will never call another subroutine. In addition to the procedure main (which is formal), you will use four informal procedures, getchar, putchar, push, and pop, and two formal procedures, parse and printnum. Here is a brief description of each:

• push: Pushes an number in \$a0 onto the stack. The stack resides in a global array dataStack. Numbers on the stack are machine integers. If a stack overflow occurs, \$v0 should be set to a 1, otherwise it should be set to 0.
• pop: Pops an item from the stack and puts in \$v1. If a stack underflow occurs, a 1 should be put in \$v0, otherwise it should be set 0.
• getchar: Gets the next character from the input stream and returns it in the low order byte of \$v0. Uses syscall to read input.
• putchar: Prints the character in the low order byte of \$a0. Uses syscall.
• printnum: Prints the number in \$a0 in the format described above.
• parse: Prompts for and parses a line of input, returns the following in \$v0.

• ```Code                    Result

-2    Invalid operation

-1    Error in converting a number.

0    The line contains an operation.  The
character code for the operation is
returned in the low order byte of
\$v1.

1    The line contains a legal number,
which is contained in \$v1.

```

Thus the main program consists of a loop which calls parse and takes appropriate actions.