Machine Problem 3

22C:18, Fall 1996

Due Thursday July 10, 1997, in class

Douglas W. Jones

Write a program that operates as an RPN calculator. An RPN calculator uses a stack for its operands. We'll use the operations push(i) and pop() to push and pop from this stack in the following discussion, but in solving this problem, you will probably find no value in packaging these operations as functions. All of our calculator's commands are one character each:

R - clears the system to its initial state
_ - blank is a no-op, useful to make things readable
E - push( 0 )
d - push( pop()*10 + value(d) ) [d is any decimal digit]
+ - push( pop2() + pop1() )
- - push( pop2() - pop1() )
Note that the + and - operators involve two pops and a push. The subscripts on the pops indicate the order in which they are done. For the - command, this means that the top item on the stack is subtracted from the item under it!

The calculator should operate by repeatedly reading a line of commands from the keyboard, performing those commands, and then printing the entire contents of the stack. The program should stop when a 0 is entered. The following example illustrates the use of this command language:

input: E
stack: 0
input: E
stack: 0 0
input: 123
stack: 0 123
input: +
stack: 123
input: R
stack:
input: E 12 E 123 + E 12 E 123 -
stack: 135 -111
input: +
stack: 24
input:
The above listing is not entirely right -- if we used printing computer terminals, that is how we might do it, but on a windowing terminal, the R command should reset the display window as well as clearing the stack. The following control structure for your main program is strongly suggested. It's given here in pseudocode:
DSPINI()
line = 0
loop
    DSPAT(line,0)
    DSPSTR("input: ")
    KBGETS(line)
    if line is empty, exit loop
    for each character ch in the line
        select the command
            blank - do nothing
            enter - push(0)
            etc
        endselect
    endloop
    line = line + 1
    DSPAT(line,0)
    DSPSTR("stack:")
    for each number n on the stack
        DSPCH(' ')
        if n<0 DSPCH('-')
        DSPDEC(n,1)
    endloop
    line = line + 1
endloop
A very serious debugging suggestion: Add features one at a time! First, get the main loop working, reading lines from the keyboard one at a time and ending the program when an empty line is encountered. Then, make it process the blank (no-op command) and complain about any other command it encounteres. Then make it handle the E command and print the contents of the stack, then add support for the R command, and only add the code to reset the display window only after you test it properly reseting the stack, and finally begin adding other commands.

As usual, your code should be well commented, and your program structure should correspond in a clean way to the kind of structure you would have used had you been writing the program in a high level language.

Turn in:

  1. An assembly listings of your main program and any subprograms.
  2. The linker's map of the combined results.