Computer Science 3 - 2015 - Test on Week 2

This should take no longer than 30 minutes and is intended to probe whether you have understood prac 2. Use your textbook - but not your notes or solutions - if you think it will help. Answer on the question sheet (4 sides).)

1. I have a memory like my friend Eratosqenes and his sief and cannot remember - remind me of your first name (surname) and student number (in that format) [2]

2. In the prac you were should have noticed that sequences like

         LDA   X          and   LDA   Y
         LDV                    <code>
                                STO

occur so frequently that it might well be worth introducing special opcodes LDL X and STL Y instead. By now you will recognise something of the Terry perversity - maybe the machine is defined to have LDA, LDV and STO opcodes rather than simply having only LDL and STL opcodes for no other reason than that it makes for lots of fun late at night to add these opcodes to the machine. But maybe not? If you have LDL and STL, could you completely remove LDA, LDV and STO from the opcode list, or would you still need to have them available? Justify your answer. [2]

3. (a) What do you understand by "short circuit evaluation of Boolean expressions" [2]

(b) Translate the following program into PVM code, making use of the opcodes you have now encountered, and using short circuit evaluation, but not adding any other opcodes. [10] (Hint: this can be done in under 24 lines.)

     void Main () {
       int n;
       bool a, b, c, d;
       bool[] list = new bool[n];
       if (a && b) write("both");
       list[n] = a == b || c != d;
     }

4. The following represents the C# translation of the encoding method that you (should have) translated to PVM code in this practical.

     public static void Main(string[] args) {
     // rot13 encryption of a text terminated with a period
     // P.D. Terry, Rhodes University, 2015

       char ch;
       do {
         ch = IO.ReadChar();
         ch = Char.ToLower(ch);
         if (Char.IsLetter(ch)) ch = (char) ('a' + (ch - 'a' + 13) % 26);
         IO.Write(ch);
       }
       while (ch != '.');
     } // Main

(a) How would you decode a text that had been supplied to you after being encrypted in this way? [2]

(b) You will note the presence of the (char) cast in this source code. Given that characters are simply represented by integer values at the machine level, and that C# permits you to add character literals to integers in any case (as you can also see exemplified here), what purpose (if any) does the (char) casting operation serve? Could it simply have been omitted? Discuss. [3]

(c) When you translated the algorithm to PVM code you may (or may not) have added an opcode to perform the casting operation, which it now seems might be superfluous. Think some more. Would such an opcode be a good idea, and if so, can you suggest how the interpreter "case arm" would deal with it? (use either PVM style, as you wish) [3]

5. Here is another algorithm that should be familiar from your first steps in progrramming:

     void Main() {
     // Read a sequence of digit characters and convert
     // this to the corresponding integer
       int n = 0;
       char ch;
       read(ch);
       while (IsDigit(ch)) {
         n = 10 * n + DigitValue(ch);
         read(ch);
       }
       write(n);
     } // Main

In the practical you (should have) introduced opcodes to achieve the effect of character handling functions lowerCase() and isLetter().

(a) What changes would you have to make to the switch statement in the PVM emulator in the Assembler system you have been using, to support additional opcodes that achieve the effect of character handling functions like IsDigit() and DigitValue()? [3]

(b) Would you have to make any other changes to the PVMxxxx.cs file and PVMAsm file to support these extensions? Explain your thinking! [2]

[part c is on the next page]

(c) Translate the algorithm above into PVM code, making use of your new opcodes, and also of the STL and LDL opcodes discussed in question 2. (Hint: this can be done in under 20 lines.) [8]


Home  © P.D. Terry