Develop stack machine code equivalents of the following simple programs:
(a) void main () { for (int i = 0; i <= 10; i++) printf("%d\n", i) } DSP 1 ADR -1 LIT 0 STO ADR -1 VAL LIT 10 LEQ BZE 30 ADR -1 VAL PRN NLN ADR -1 ADR -1 VAL LIT 1 ADD STO BRN 7 HLT (b) void main () { int i, list[11]; i = 0; while (i <= 10) { list[i] = 2 * i; i++; } } DSP 12 ADR -1 LIT 0 STO ADR -1 VAL LIT 10 LEQ BZE 41 ADR -2 ADR -1 VAL LIT 11 IND LIT 2 ADR -1 VAL MUL STO ADR -1 ADR -1 VAL LIT 1 ADD STO BRN 7 HLT (c) PROGRAM Silly; VAR List[10], I, J; BEGIN Read(I, J, List[I+J]); List[List[J]] := List[J + 2 * I] END. DSP 13 ADR -12 INN ADR -13 INN ADR -1 ADR -12 VAL ADR -13 VAL ADD LIT 11 IND INN ADR -1 ADR -1 ADR -13 VAL LIT 11 IND VAL LIT 11 IND ADR -1 ADR -13 VAL LIT 2 ADR -12 VAL MUL ADD LIT 11 IND VAL STO HLT
(a) Consider the following general form of T diagram. Although it uses the letters A through I in the various arms of the T's, 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 C++ compiler CC.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 2000 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 C++ and compile it:
---------------------------- ---------------------------- | PRETTY.CPP | | PRETTY.EXE | | PARVA ----------> PARVA | | PARVA -----------> PARVA | | | | | --------- ---------------------------- --------- | | BCC.EXE | | | C++ | C++ --------> 8086 | 8086 | | | | | -------------------- -------------------- | | | 8086 | | | ------------ | | | 8086 | | | ------------
Running the pretty-printer is depicted by
---------------------------- ---------------------------- | MESSY.PAV | | BETTER.PAV | | DATA ----------> JUNK | | DATA -----------> JUNK | | | | | --------- ---------------------------- --------- | | PRETTY.EXE | | | PARVA | PARVA --------> PARVA | 8086 | | | | | -------------------- -------------------- | | | 8086 | | | ------------ | | | 8086 | | | ------------
We can try to depict this all in one diagram as follows:
----------------------- ----------------------- | MESSY.PAV | | BETTER.PAV | | DATA -------> JUNK | | DATA ------> JUNK | | | | | --------- --------- --------- --------- ---------------------------- | |----------------------------| | | PRETTY.CPP | |PARVA|| PRETTY.EXE ||PARVA| | PARVA ----------> PARVA | | || PARVA -----------> PARVA || | | | -------| |------- --------- ---------------------------- --------- | | BCC.EXE | | | C++ | C++ --------> 8086 | 8086 | | | | | -------------------- -------------------- | | | | | 8086 | | 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 MicroSoft 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 | | CL.EXE | | | M-2 | M-2 --------> C | C | C --------> 8086 | 8086 | | | | | | | -------------------- ---------------------------- -------------------- | | | | | | | 8086 | | 8086 | | 8086 | | | | | | | ------------ ------------ ------------ | | | | | 8086 | | 8086 | | | | | ------------ ------------