Please answer all questions in the spaces provided (two sides). Max 20
This test is intended to check your ability to understand code generation involving branches, and how multifunction programs are compiled and executed. It should only take you about 30 minutes at the most. Textbooks may not be used this time.
1. I'll forget again by next week, but we can keep trying. What are your surname (and initials)?
Terry (P.D.)
2. The following is the source code of an absurd program whose only purpose is to test whether you have understood scope and existence concepts for a multifunction program.
bool confused = true; int function(int x, int y) { int result; if (confused) result = x + y; else result = x - y; return result; // point (a) } void main() { int i = 20, j = 30; int k = function(i + j, 10); write(k); // point (b) }
(a) If this program were to be compiled and executed, what value would be displayed?
60
(b) Which identifiers would be "in scope" at compile time when the compilation reached
point (a) confused function x y result (people tended to ignore "function")
point (b) confused function main i j k (people tended to ignore "function" and "main")
(c) Indicate on the memory map below what you believe the run-time stack would look like execution reached point (a) (that is, immediately before the "return" statement transferred control back to the main routine). Mark where the variables would be found, and also indicate where the stack pointer cpu.sp and the frame pointer cpu.fp would "point" at that instant. Part of the map has been suggested for you.
function main global confused .-----------------------------------------------------------------------------------------------------------. | | | | | | | | | | | | | | | | | | | | | | 60 | 10 | 50 | 511 | ? | 511 | 60 | 504 | ? | 30 | 20 | 512 | ? | 512 | ? |true | | | | res | y | x | SM | RA | DL | RV | ADR | k | j | i | SM | RA | DL | RV |true | | | | | | | | | | | k | | | | | | | | | `-----------------------------------------------------------------------------------------------------------' 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 ^ ^ ^ sp (496) fp (503) gp (512)
3. The following gives the outline of a piece of Parva source code incorporating looping and decision structures. S1 through S8 denote "other" statements whose details need not concern us, and C1 through C5 denote Boolean conditions whose details likewise are unimportant. A skeleton for the compiled code appears on the right, but this has omitted all the labels and branch instructions that would be required to complete the code (other than the label L1 at the start of the repeat loop and the BZE instruction at the end of this loop).
Annotate the code to show where further labels L2, L3 ... would be needed, and insert the appropriate BRN and/or BZE instructions needed to complete the code generation
S1; <code for S1> repeat S2; L1: <code for S2> if (C1) { S3; <code for C1> break; } // then <code for S3> elsif (C2) { while (C3) { <code for C2> S4; if (C4) <code for C3> S5; else <code for S4> break; S6; <code for C4> } // while break; <code for S5> } // elsif else <code for S6> S7; break; <code for S7> until (C5); S8; <code for C5> BZE L1 <code for S8>
This turned out to be rather a nasty question to have set, and few people got off the ground. It would have helped if I had included a few comments, perhaps, as I have done here (I have not tried a question like this before!)
S1; repeat S2; if (C1) { // if1 S3; break; // from repeat loop } // then elsif (C2) { // elsif1 while (C3) { // start while S4; if (C4) // if2 S5; else // else2 break; // from while loop S6; // end if2 } // end while break; // from repeat loop } // elsif1 else // else1 S7; // endif1 break; // from repeat until (C5); // until S8;
and the code, including the branches and especially the break statement becomes
<code for S1> L1: // start repeat loop <code for S2> <code for C1> // start of if1-elsif1-else1 BZE L2 <code for S3> BRN L9 // break from repeat loop BRN L8 // to end if1 L2: <code for C2> BZE L7 // to else1 L3: // start while loop <code for C3> BZE L6 // to loop exit <code for S4> <code for C4> // start if2 BZE L4 // to else2 <code for S5> BRN L5 // to end if2 L4: BRN L6 // break from while loop L5: <code for S6> BRN L3 // to start while loop L6: // exit point of while loop BRN L9 // break from repeat loop BRN L8 // to end if1 L7: <code for S7> L8: // end if1 BRN L9 // break from repeat loop <code for C5> BZE L1 // to start repeat L9: <code for S8>