Computer Science 301 - Translators


Tutorial 2 - Stack Machine Code, T-diagrams - Solutions

(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);
          }

     +++++++++++++++++ Prac question - solution will be supplied later


     (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 and why?

      ┌──────────────────────────┐          ┌──────────────────────────┐
      │                          │          │                          │
      │   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 Parva2ToCSharp translator. This was developed at Rhodes by using a compiler generator program called CoCo to produce its C# source code from a so-called "Attribute Grammar" written in a specification language called Cocol, which you will meet in a few weeks' time. This C# code was then compiled with the C# compiler available for the PC. Draw T- diagrams that describe this development.

The Parva2ToCSharp program was defined in Cocol. Coco.EXE took this as its input, and generated a Parva to C# high level compiler written in C# as its output (left part of the diagram).

This was compiled with the CSC compiler to produce the Intel based version P2C#.EXE (right part of the diagram).

  ┌──────────────────────────┐          ┌──────────────────────────┐          ┌──────────────────────────┐
  │         P2C#.Cocol       │          │         P2C#.CS          │          │         P2C#.EXE         │
  │ Parva  ──────────>  C#   │          │ Parva ───────────>  C#   │          │ Parva ───────────>  C#   │
  │                          │          │                          │          │                          │
  └───────┐          ┌───────┴──────────┴───────┐          ┌───────┴──────────┴───────┐          ┌───────┘
          │          │         Coco.EXE         │          │         CSC.EXE          │          │
          │   Cocol  │ Cocol  ────────>     C#  │    C#    │   C#   ────────>   Intel │  Intel   │
          │          │                          │          │                          │          │
          └──────────┴───────┐          ┌───────┴──────────┴───────┐          ┌───────┼──────────┤
                             │          │                          │          │       │          │
                             │  Intel   │                          │  Intel   │       │  Intel   │
                             │          │                          │          │       │          │
                             └──────────┘                          └──────────┘       └──────────┘
                             ┌──────────┐                          ┌──────────┐
                             │  Intel   │                          │  Intel   │
                             │          │                          │          │
                             └──────────┘                          └──────────┘

Using Parva2ToC# can be exemplified as follows. (Not asked in the tutorial.) We write our factorial program in Parva and translate it to C# (left part of the diagram). We then feed this version to the C# compiler and produce the Intel machine code version (right part of the diagram). Ths can be fed to the Intel machine to produce the factorials.

  ┌──────────────────────────┐          ┌──────────────────────────┐          ┌──────────────────────────┐
  │         NFact.pav        │          │        NFact.cs          │          │         NFact.exe        │
  │    N   ──────────>   N!  │          │   N   ───────────>  N!   │          │   N   ───────────>  N!   │
  │                          │          │                          │          │                          │
  └───────┐          ┌───────┴──────────┴───────┐          ┌───────┴──────────┴───────┐          ┌───────┘
          │          │         P2C#.EXE         │          │         CSC.EXE          │          │
          │   Parva  │ Parva  ────────>     C#  │    C#    │   C#   ────────>   Intel │  Intel   │
          │          │                          │          │                          │          │
          └──────────┴───────┐          ┌───────┴──────────┴───────┐          ┌───────┴──────────┘
                             │          │                          │          │       ┌──────────┐
                             │  Intel   │                          │  Intel   │       │  Intel   │
                             │          │                          │          │       │          │
                             └──────────┘                          └──────────┘       └──────────┘
                             ┌──────────┐                          ┌──────────┐
                             │  Intel   │                          │  Intel   │
                             │          │                          │          │
                             └──────────┘                          └──────────┘
                          (produced as above)


Home  © P.D. Terry