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