Computer Science 3 - 2014 - Test for week 2

This should take no longer than 30 minutes and is simply intended to probe whether you have understood the material in the prac. Use your textbook if you think it will help. Answer on the question sheet (3 sides).)

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

2. Translate the following program into PVM code, making use of the LDL, STL, INC, DEC and CAP opcodes that you should have introduced into the system during the course of the week. [10]

       void main () {
       // Reads and stores a simple sentence
       // Ima Jeenyus, 2014
         int n = 0;
         char ch;
         char[] sentence = new char[1000];
         repeat
           read(ch);
           write(upper(ch));
           sentence[n] = ch;
           n++;
         until (ch == '.');
       }

3. Having a character data type is all very well, but it could get you into trouble. Consider code like

        int i = 0;
        char c = 'a';
        while (i < 1000) {
          i++; c++;
        }

Sooner or later (but, of course, when you least wanted trouble) this sort of code would cause trouble. What extensions would you suggest be made to the opcode set and/or the interpreter to allow it to catch such errors? [4]

4. 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 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? [4]

5. Suppose the PVM were to be extended to provide a few more "functions", specifically MIN and SQR - the first for selecting the lesser of the two values currently on the top of the stack, and the second for squaring the value on the top of the stack.

(a) Show the additions that would be needed to the switch statement in the emulator (as it appears on pages 37 - 39 of the text) to support these new opcodes. [6]

(b) Show how they could be used in a translation of the statement

           write(sqr(min(a, min(b, c)));

(Hint - you should be able to do this in about 7 lines of PVM code). [3]

(c) Briefly indicate where, if anywhere, you would have to make other changes to the PVM.java or PVM.cs file to handle these new opcodes (don't worry too much about the niceties of C#/Java) [2]


Home  © P.D. Terry