(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 () { // demonstrate hailstone series int m; read("what is the first term of the series? ", m); write(m); int n = 1; while (m != 1) { if (m % 2 == 0) m = m / 2; else m = 3 * m + 1; n = n + 1; write(m); } write("\n", n , " terms in that sequence"); }
(b) The following code is supposed to check to see whether the elements of an array form a non-decreasing sequence. Does it do so correctly? If not, how would you fix it, and what would the resulting PVM code look like?
void main () { // check to see whether an array has elements in ascending order const size = 20; int[] list = new int[size]; list[5] = -1; int i = 0; bool inOrder; while (i < size) { if (list[i] > list[i+1]) inOrder = false; else inOrder = true; i = i + 1; } write(inOrder); }
(c) 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 | | | `----------'
(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.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.