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.
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:
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.
I strongly recommend that you program this assignment in a high level language before you attempt assembly language code. It is tricky to keep track of all the cases in parsing a line, and you will find life is easier if you already have a working algorithm in hand.
You will have to convert numbers, character by character from decimal to binary and vice verse. Thus a good place to start is to write the getchar, putchar, and printnum subprograms. With reliable I/O you can go on to write and debug the rest. Remember that the character code of the decimal digit k is k+'0'.
To read and covert a number, use syscall number 8 with an argument of 2 in $a1 to read one character at a time. Remember that syscall 8 returns its result in the memory indicated by the contents of $a0 and not directly in $a0. As you read the number, accumulate it in a register. After each digit, you can multiply by ten and add the new digit to the result.
Your program should be turned in via the submit program as program number 3.