Computer Science 301 - 2012


Tutorial for week 20 - 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 Extacy Modula-2 to C translator. This was developed in Russia by a team who used the JPI Modula-2 compiler available for the PC. But the demonstration system we downloaded from the Internet came with the file XC.EXE and a few other modules written in Modula-2 (but not the source of the XC executable itself). Draw T-diagrams showing the process the Russians must have used to produce this system, and go on to draw T-diagrams showing how you managed to take the program SIEVE.MOD and run it on the PC using the Borland C++ system as your time-waster of choice.

The XC program was written in Modula-2 and compiled:

      ----------------------------          ----------------------------
      |           XC.MOD         |          |           XC.EXE         |
      |   M-2  ---------->   C   |          |  M-2  ----------->   C   |
      |                          |          |                          |
      ---------          ----------------------------          ---------
              |          |          JPI.EXE         |          |
              |    M-2   |   M-2  -------->    8086 |    8086  |
              |          |                          |          |
              --------------------          --------------------
                                 |          |
                                 |   8086   |
                                 |          |
                                 ------------
                                 |          |
                                 |   8086   |
                                 |          |
                                 ------------

Using XC.EXE can be depicted as follows:

  ----------------------------          ----------------------------          ----------------------------
  |         SIEVE.MOD        |          |         SIEVE.C          |          |         SIEVE.EXE        |
  |   N    ----------> Primes|          |   N   -----------> Primes|          |   N   -----------> Primes|
  |                          |          |                          |          |                          |
  ---------          ----------------------------          ----------------------------          ---------
          |          |          XC.EXE          |          |         BCC.EXE          |          |
          |    M-2   |   M-2  -------->     C   |     C    |    C   -------->   8086  |   8086   |
          |          |                          |          |                          |          |
          --------------------          ----------------------------          --------------------
                             |          |                          |          |       |          |
                             |   8086   |                          |   8086   |       |   8086   |
                             |          |                          |          |       |          |
                             ------------                          ------------       ------------
                             |          |                          |          |
                             |   8086   |                          |   8086   |
                             |          |                          |          |
                             ------------                          ------------