Computer Science 301 - 2012

Tutorial for week 20 - Stack Machine Code, 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")
          }

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

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

(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    |
                                 |          |
                                 `----------'

(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.EXE 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 compiler of choice.


Home  © P.D. Terry