## Department of Electrical and Computer Engineering

### The University of Texas at Austin

EE 306, Fall 2006
Instructor: Yale N. Patt
TAs: Aseem Bathla, Cameron Davison, Lisa de la Fuente, Phillip Duran, Jose Joao,
Jasveen Kaur, Rustam Miftakhutdinov, Veynu Narasiman, Nady Obeid, Poorna Samanta.
Supplemental Note: A stack machine
November 27, 2006
```Let's assume we add opcodes SUB, MUL, and DIV to the LC-3 (making
it the LC-3.5, perhaps).  And, assume we replace the register file with
a stack.  The instructions would work as follows:

* Loads would take from memory and PUSH on the stack
* Stores would POP the stack and write to memory
* Operates would POP, POP, operate, and PUSH

Note that no addresses are needed for operates since the computer knows
EXACTLY where to find the sources and where to write the destination.

With such a machine, a high-level language programmer could write

A=(((B*C)+(D*E))/F)-G

and the LC-3.5 Compiler would translate this to:

PUSH B
PUSH C
MUL
PUSH D
PUSH E
MUL
PUSH F
DIV
PUSH G
SUB
POP  A

Actually, in mathematics, we call this representation Postfix or Polish
Notation:

A=BC*DE*+F/G-

Suppose I do not have an LC-3.5, and am stuck with just the LC-3, and
I wish to Simulate a Stack machine (as in the Calculator program in Chap.10).

1. I could write subroutines to ADD, SUB, MUL, and DIV.

Each subroutine would POP the top two elements from the stack, perform
the operation, and PUSH the result onto the stack.

2. Then my main routine to perform the above assignment statement would be:

LD  R0,B
JSR PUSH
LD  R0,C
JSR PUSH
JSR MUL
LD  R0,D
JSR PUSH
LD  R0,E
JSR PUSH
JSR MUL
JSR SUM
LD  R0,F
JSR PUSH
JSR DIV
LD  R0,G
JSR PUSH
JSR SUB
JSR POP
ST  R0,A

```