Computer Science 301 - 2014


Tutorial 2 - Solutions - Stack Machine Code and T diagrams

(a) Develop stack machine code equivalents of the following simple programs:

     (1)  void main () { // print numbers 0 through 10
            for (int i = 0; i <= 10; i++) write(i, "\n")
          }

          0 DSP    1
          2 LDA    0
          4 LDC    0
          6 STO
          7 LDA    0
          9 LDV
         10 LDC    10
         12 CLE
         13 BZE    32
         15 LDA    0
         17 LDV
         18 PRNI
         19 PRNS   "\n"
         21 LDA    0
         23 LDA    0
         25 LDV
         26 LDC    1
         28 ADD
         29 STO
         30 BRN    7
         32 HALT

     (2)  void main() {  // factorials
            int n, f;
            read(n);
            write(n, "! =");
            f = 1;
            while (n > 0) {
              f = f * n;
              n = n - 1;
            }
            write(f);
          }

          0 DSP      2
          2 LDA      0
          4 INPI
          5 LDA      0
          7 LDV
          8 PRNI
          9 PRNS     "! ="
         11 LDA      1
         13 LDC      1
         15 STO
         16 LDA      0
         18 LDV
         19 LDC      0
         21 CGT
         22 BZE      45
         24 LDA      1
         26 LDA      1
         28 LDV
         29 LDA      0
         31 LDV
         32 MUL
         33 STO
         34 LDA      0
         36 LDA      0
         38 LDV
         39 LDC      1
         41 SUB
         42 STO
         43 BRN      16
         45 LDA      1
         47 LDV
         48 PRNI
         49 HALT

     (3)  void main () { // simple array handling
            int i = 1;
            int[] list = new int[11];
            list[0] = 5;
            while (i <= 10) {
              list[i] = 2 - list[i-1];
              i++;
            }
          }

          0 DSP      2
          2 LDA      0
          4 LDC      1
          6 STO
          7 LDA      1
          9 LDC      11
         11 ANEW
         12 STO
         13 LDA      1
         15 LDV
         16 LDC      0
         18 LDXA
         19 LDC      5
         21 STO
         22 LDA      0
         24 LDV
         25 LDC      10
         27 CLE
         28 BZE      57
         30 LDA      1
         32 LDV
         33 LDA      0
         35 LDV
         36 LDXA
         37 LDC      2
         39 LDA      1
         41 LDV
         42 LDA      0
         44 LDV
         45 LDC      1
         47 SUB
         48 LDXA
         49 LDV
         50 SUB
         51 STO
         52 LDA      0
         54 INC
         55 BRN      22
         57 HALT


(b) Consider the following general form of T diagram. Although it uses the letters A through I in the various arms of the Ts, one could draw the diagram with fewer letters. How?

      ----------------------------          ----------------------------
      |                          |          |                          |
      |   A    ---------->  B    |          |   C   ----------->    D  |
      |                          |          |                          |
      ---------          ----------------------------          ---------
              |          |                          |          |
              |     E    |    F   -------->     G   |     H    |
              |          |                          |          |
              --------------------          --------------------
                                 |          |
                                 |     I    |
                                 |          |
                                 ------------
Because of the rule that the "input" is semantically the same as the "output" it follows that we can draw this
      ----------------------------          ----------------------------
      |                          |          |                          |
      |   A    ---------->  B    |          |   A   ----------->    B  |
      |                          |          |                          |
      ---------          ----------------------------          ---------
              |          |                          |          |
              |     F    |    F   -------->     G   |     G    |
              |          |                          |          |
              --------------------          --------------------
                                 |          |
                                 |     I    |
                                 |          |
                                 ------------


(c) In the practical sessions you should have used the Parva2ToJava translator. This was developed at Rhodes by using the C# compiler available for the PC. Draw T-diagrams showing the process used to produce this system, and go on to draw T-diagrams showing how you managed to take the program SIEVE.PAV and convert it and run it on the PC using the Java compiler.

The Parva2ToJava program was written in C# and compiled:

      ----------------------------          ----------------------------
      |           P2J.CS         |          |          P2J.EXE         |
      |  Parva ----------> Java  |          | Parva ----------->  Java |
      |                          |          |                          |
      ---------          ----------------------------          ---------
              |          |          CSC.EXE         |          |
              |    C#    |   C#   -------->   Intel |   Intel  |
              |          |                          |          |
              --------------------          --------------------
                                 |          |
                                 |  Intel   |
                                 |          |
                                 ------------
                                 |          |
                                 |  Intel   |
                                 |          |
                                 ------------

Using Parva2ToJava can be exemplified as follows:

  ----------------------------          ----------------------------          ----------------------------
  |         SIEVE.PAV        |          |        SIEVE.JAVA        |          |         SIEVE.CLASS      |
  |   N    ----------> Primes|          |   N   -----------> Primes|          |   N   -----------> Primes|
  |                          |          |                          |          |                          |
  ---------          ----------------------------          ----------------------------          ---------
          |          |         P2J.EXE          |          |          JAVAC           |          |
          |   Parva  | Parva  -------->    Java |   Java   |  Java  -------->    JVM  |   JVM    |
          |          |                          |          |                          |          |
          --------------------          ----------------------------          --------------------
                             |          |                          |          |       |   JVM    |
                             |  Intel   |                          |   JVM    |       | Interpr  |
                             |          |                          |          |       |  Intel   |
                             ------------                          ------------       ------------
                             |          |                          |   JVM    |       |          |
                             |  Intel   |                          | Interpr  |       |  Intel   |
                             |          |                          |  Intel   |       |          |
                             ------------                          ------------       ------------
                                                                   |          |
                                                                   |  Intel   |
                                                                   |          |
                                                                   ------------