Machine Problem 3
22C:18, Spring 1996
Due Tuesday Mar. 12, 1995, in discussion
Douglas W. Jones
Given the M68000 program that emulates an RPN calculator, assigned in machine
problem 2, add the following new single-character commands:
Each character on the line of input has meaning as an instruction to the
calculator:
- % -- the mod operator:
- As with division, this combines the top two elements on the stack,
but it returns the remainder, not the quotient.
- L -- the load operation:
- This uses the top element on the stack as a memory address to select
from one of 1000 memory locations, indexed by the numbers 0 to 999.
If the top item on the stack is outside this range, it is an error.
Otherwise, the top item on the stack is replaced by the value from
memory. The locations in memory each hold one stack item.
- S -- the store operation:
- This uses the top element on the stack as a memory address to select
from one of 1000 memory locations, indexed by the numbers 0 to 999.
If the top item on the stack is outside this range, it is an error.
Otherwise, the item under the top item on the stack stored in the
indicated memory location, and the top two items on the stack are
popped.
- J -- the jump operation:
- The top item on the stack is popped and used as a branch displacement,
and popped off the stack. If it is zero, it is an error (the branch
is to the J instruction itself). If negative, the branch goes backwards.
If positive, the branch goes forward.
- < = > -- the conditional jump operations:
- These are identical to the J operation, except that they test the sign
of the item below the branch displacement and only branch if the item
is less than, equal to, or greater than zero. In addition, they always
pop the item off the stack, whether or not they branch.
Here is an example of the operation of this calculator for one line of input:
Input: E12ESELEL+P ELE1-ES ELP ELEE19->
Output: 24
Output: 11
Output: 10
The first sequence of instructions stores 12 in location zero, then
loads from location zero twice, adds and prints the result. The
second third and forth sequenes make up a loop that repeatedly
decrements and prints the contents of location zero.