Computer Science 3 - 2008 - Test on Prac 20

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]

Pat Terry 63T0844

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, 2008
         int n = 0;
         char ch;
         char[] sentence = new char[1000];
         repeat
           read(ch);
           write(toUpperCase(ch));
           sentence[n] = ch;
           n++;
         until (ch == '.');
       }

           0     DSP      3
           2     LDC      0
           4     STL      0       ; n = 0
           6     LDC      1000
           8     ANEW
           9     STL      2       ; char [] sentence = ...
          11     LDA      1
          13     INPC             ; read(ch);
          14     LDL      1
          16     CAP
          17     PRNC             ; write(toUpperCase(ch));
          18     LDL      2
          20     LDL      0
          22     LDXA
          23     LDL      1
          25     STO              ; sentence[n] = ch;
          26     LDA      0
          28     INC              ; n++
          29     LDL      1
          31     LDC      46      ; ch == "."
          33     CEQ
          34     BZE      11
          36     HALT

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]

There are various strategies we could employ. The simplest is probably to have a variation on the STO opcode (and its derivatives like STL) and on INC and DEC that will perform range checks:

            case PVM.stoc:          // character checked store
              tos = pop(); adr = pop();
              if (inBounds(adr))
                if (tos >= 0 && tos <= maxChar) mem[adr] = tos;
                else ps = badVal;
              break;

            case PVM.stlc:        // character checked pop to local variable
              tos = pop(); adr = cpu.fp - 1 - next();
              if (inBounds(adr))
                if (tos >= 0 && tos <= maxChar) mem[adr] = tos;
                else ps = badVal;
              break;

            case PVM.incc:        // ++ (characters)
              adr = pop();
              if (inBounds(adr))
                if (mem[adr]) < maxChar) mem[adr]++;
                else ps = badVal;
              break;
            case PVM.decc:       // -- (characters)
              adr = pop();
              if (inBounds(adr))
                if (mem[adr]) > 0) mem[adr]--;
                else ps = badVal;
              break;

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]

One would need to modify the INPx instructions so that they did not store, but push the value read onto the stack:

         case PVM.inpi:          // integer input
              push(data.readInt());
              if (data.error()) ps = badData;
              break;
            case PVM.inpb:          // boolean input
              push(data.readBool() ? 1 : 0);
              if (data.error()) ps = badData;
              break;
            case PVM.inpc:          // character input
              push(data.readChar());
              if (data.error()) ps = badData;
              break;

The source above would then have to be translated to

            LDA  x
            INPI
            INPI
            ADD
            STO

and

            LDL  sentence
            LDL  i
            LDXA
            INPC
            STOC

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 62 - 65 of the text) to support these new opcodes. [6]

            case PVM.min:          // minimum
              tos = pop();
              sos = pop();
              push(tos < sos? tos : sos);
              break;
            case PVM.sqr:          // square
              tos = pop();
              push(tos * tos);
              break;

(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]

            LDL  a
            LDL  b
            LDL  c
            MIN
            MIN
            SQR
            PRNI

(c) Briefly indicate where, if anywhere, you would have to make other changes to the PVM.java file to handle these new opcodes [2].

We should have to add internal values for min and sqr to the "enumeration" at the start of the file, and we should have to add the corresponding initialisation of the elements of the mnemonic look-up array (at the end of the file). Since these are "zero address" instructions, they do not give rise to further changes in the parts of the file responsible for tracing, assembling or for listing code.


Home  © P.D. Terry