Develop stack machine code equivalents of the following simple programs:
(a) void main () { 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 (b) void main () { int i = 0; int[] list = new int[11]; while (i <= 10) { list[i] = 2 * i; i++; } } 0 DSP 2 2 LDA 0 4 LDC 0 6 STO 7 LDA 1 9 LDC 11 11 ANEW 12 STO 13 LDA 0 15 LDV 16 LDC 10 18 CLE 19 BZE 46 21 LDA 1 23 LDV 24 LDA 0 26 LDV 27 LDXA 28 LDC 2 30 LDA 0 32 LDV 33 MUL 34 STO 35 LDA 0 37 LDA 0 39 LDV 40 LDC 1 42 ADD 43 STO 44 BRN 13 46 HALT (c) void main () { int[] list = new int[100]; int i, j; read(i, j, list[i+j]); list[list[j]] = list[j + 2 * i]; } 0 DSP 3 2 LDA 0 4 LDC 100 6 ANEW 7 STO 8 LDA 1 10 INPI 11 LDA 2 13 INPI 14 LDA 0 16 LDV 17 LDA 1 19 LDV 20 LDA 2 22 LDV 23 ADD 24 LDXA 25 INPI 26 LDA 0 28 LDV 29 LDA 0 31 LDV 32 LDA 2 34 LDV 35 LDXA 36 LDV 37 LDXA 38 LDA 0 40 LDV 41 LDA 2 43 LDV 44 LDC 2 46 LDA 1 48 LDV 49 MUL 50 ADD 51 LDXA 52 LDV 53 STO 54 HALT
(a) 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 | | | ------------
(b) A pretty printer is a program that will take the source code of a program written in some language and essentially simply reproduce it, but making it look "pretty", for example adjusting all the indentation to be consistent, placing each statement on a new line, adding blank lines between functions and so on.
Suppose you have available a Java compiler JIKES.EXE for the PC, and that you have just been introduced to the language Parva. Use T diagrams to show in broad detail how you would (a) develop a pretty printer PRETTY.EXE for Parva that would run on a Windows XP system in the Hamilton Compass Laboratories, and (b) use this pretty printer on that system to beautify a messy program MESSY.PAV written in Parva.
We write the pretty-printer in Java and compile it:
---------------------------- ---------------------------- | PRETTY.JAVA | | PRETTY.CLASS | | PARVA ----------> PARVA | | PARVA -----------> PARVA | | | | | --------- ---------------------------- --------- | | JIKES.EXE | | | Java | Java --------> JVM | JVM | | | | | -------------------- -------------------- | | | 8086 | | | ------------ | | | 8086 | | | ------------
Running the pretty-printer is depicted by
---------------------------- ---------------------------- | MESSY.PAV | | BETTER.PAV | | DATA ----------> JUNK | | DATA -----------> JUNK | | | | | --------- ---------------------------- --------- | | PRETTY.CLASS | | | PARVA | PARVA --------> PARVA | PARVA | | | | | -------------------- -------------------- | | | JVM | | | ------------ | JVM | | Emulator | ------------ | | | 8086 | | | ------------
We can try to depict this all in one diagram as follows:
----------------------- ----------------------- | MESSY.PAV | | BETTER.PAV | | DATA -------> JUNK | | DATA ------> JUNK | | | | | --------- --------- --------- --------- ---------------------------- | |----------------------------| | | PRETTY.JAVA | |PARVA|| PRETTY.CLASS ||PARVA| | PARVA ----------> PARVA | | || PARVA -----------> PARVA || | | | -------| |------- --------- ---------------------------- --------- | | JIKES.EXE | | | Java | Java --------> JVM | JVM | | | | | -------------------- -------------------- | | | | | 8086 | | JVM | | | | Emulator | ------------ ------------ | 8086 | | 8086 | | | | | ------------ ------------Note that we are not using or writing a Parva compiler here!
(d) 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 | | | | | ------------ ------------