Computer Science 3 - 2017 - Test on Week 2

This should take no longer than 30 minutes and is intended to probe whether you have understood the PVM. 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 Ερατοσθενes and his σιεϕ and cannot remember - remind me of your first name, (surname) and student number (in that format) [1]

2. In the prac you were invited to note that sequences like

         LDA   X          and   LDA   Y
         LDV                    <code>
                                STO

occurred so frequently that it might 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. (Character manipulations) Here is an algorithm that hopefully is familiar:

     void main() {
     // Read a sequence of digit characters and convert this to the corresponding integer
       int n = 0;
       char ch;
       read(ch);
       while (n < 214748364 && 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 the character handling function upperCase() and the I/O functions read() and write().

(a) What changes would you have to make to the switch statement in the PVM emulator in either of the Assembler systems you have been using, to support additional opcodes isdig and digv that achieve the effect of character handling functions isDigit(ch) and digitValue(ch)? [6]

(Hint: the Char class has a method Char.IsDigit(c) which might prove to be useful.)

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

(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. You should also demonstrate a "short-circuit" approach to the expression controlling the while loop. (Hint: this can be done in under 28 lines.) [8]

4. (Array handling) Translate the following program into PVM code, making use of the INC, LDL and STL opcodes studied in the prac. [10] (Hint: this can be done in under 24 lines.)

     void main () {
       int i;
       bool[] odd = new bool[1000];
       for (i = 0; i < 1000; i++) odd[i] = i % 2 != 0;
     }

5. The PVM currently supports the INPI, INPB and INPC operations that expect to find a destination address on the top of the stack, as you should know, favouring the translation of code like

          read(x);
          read(sentence[i]);

Suppose instead that the system were to be changed to support a style of programming like

          x = readInt() + readInt();  // read and add two integers
          sentence[i] = readChar();   // read and store a single character

What implications would this have for the PVM, and what code sequences would be needed to correspond to these fragments? [5]


Home  © P.D. Terry