Machine Problem 2

22C:18, Spring 1996

Due Tuesday Feb. 27, 1995, in discussion

Douglas W. Jones

Notice: Exam I will be given in class on Friday Feb. 23, Open Book!

Write an M68000 program that emulates an RPN calculator, repeatedly reading a line of input from the keyboard, and performing the computations and output operations directed by the characters on that line, interpreted from left to right, starting with an empty stack.

Each character on the line of input has meaning as an instruction to the calculator:

0-9 -- the digits:
The item on the stack top is multiplied by 10 and then the corresponding integer is added to the stack top. If the top item is initially zero, the result of reading a string of digits is to enter the corresponding number.
+-*/ -- the operators:
The two topmost items on the stack are popped, combined as indicated by the operator symbol, and then the result is pushed. In the case of subtraction and division, the top item on the stack is subtracted from or divided into the item below it.
E -- the enter operation:
A zero is pushed onto the stack.
P -- the print operation:
The top item on the stack is popped off the stack and printed, as a decimal number.
B -- the odd-base print operation:
The top item on the stack is used as the number base for printing the item below it on the stack. Both are then popped. An error message should be printed if the number base cannot be supported.
_ -- space
Spaces in the input are ignored.
Here is an example of the operation of this calculator for one line of input:
    Input: E12E6-P E12E7E6-*E12B E160E2*E10/P
    Output: 6
    Output: 10
    Output: 32
Your program should detect stack underflow and print an error message. Your program need not process input lines longer than 80 characters. Your program should use a procedure that prints output in any number base, most likely a procedure based on your solution to MP1.