CSE 109   Test 1  Wednesday 9 October 2002
 ====================SUGGESTED ANSWERS================================
1.  For each of (a), (b), and (c), provide fragments of OP2 code (lines
of instructions) to accomplish the given task.  In each case, assume the
first line of the fragment is placed in memory location 35.  Recall the
op-codes: read (0), write (1), load immediate (2), store (3), add (4),
subtract (5), multiply (6), divide (7), jump (8), compare and jump (9).
(a) halt if the contents of location 3 is greater than 0
(b) halt if the contents of location 3 is greater than 2
(c) write out the number 1 if the contents of location 3 is the number 3
----------------------------------------------------------------------
(a)  2000000  [35] clear
     4003000  [36] load contents of 3
     9038000  [37] jump to 0 if positive, otherwise goto next line [38]
(b)  2000002  [35] put 2 in accumulator
     5003000  [36] subtract 2 from contents of location 3
     9038000  [37] go to 0 if number is > 0
(c)  2000003  [35] put 3 in the accumulator
     5003000  [36] subtract 3 from contents of location 3
     9040040  [37] skip next 2 instructions if not 0
     2000001  [38] put 1 in accum
     1000000  [39] write contents

2.  Write a program which does nothing unless it is called with command
line arguments in which case is displays the message
   Usage: <progName>
where <progName> is replaced by the name of the executable file containing
the program.  For example, if the executable file is called a.out, then the
command
a.out
would do nothing, while the command
a.out hello there
would lead to the output
Usage: a.out
---------------------------------------------------------------------
#include <fstream.h>

int main(int ct,char *arg[])
{if(ct>1)
  cout<<"Usage: "<<arg[0]<<endl;
}

3.  Assume the files a.h, b.h, and prog.cc, respectively, contain the class
declarations of A and B and the main program below, assume that the
definitions for the classes A and B are containied in files a.cc and b.cc,
respectively.  Write a Makefile for separate compilation of a.cc, b.cc,
and prog.cc so that the executable program ends up in the file prog.
Include a command for "cleaning up."
#ifndef A_H         #ifndef B_H       #include "a.h"
#define A_H         #define B_H       #include "b.h"
#include "b.h"      class B           int main()
class A             {public:          {A a;
{public:               int g;          B b;
   B b;                void t();       a.s();
   void s();        };                 b.t();
};                  #endif            }
#endif
-------------------------------------------------------------------
# Makefile for problem 3 on first test
prog: a.o b.o prog.o
     g++ -o prog a.o b.o prog.o

prog.o: a.h b.h prog.cc
     g++ -c prog.cc

a.o: a.h b.h a.cc
     g++ -c a.cc

b.o: b.h b.cc
     g++ -c b.cc

clean:
     rm -f *~ *.o prog

4. Write a template for the class Bracket which is used to displaying
output in square brackets ([]).  For such a templated class the following
code should produce the output indicated in the comments, where the string
"OUTPUT:" does not appear as part of the bracketed output.
  Bracket <int> x;
  Bracket <char> y;
  Bracket <char*> z; //if the first two work, so will this. Cute, huh?
  x.data=7;
  y.data='a';
  z.data="hello";
  x.display(cout);  //OUTPUT: [7]
  y.display(cout);  //OUTPUT: [a]
  z.display(cout);  //OUTPUT: [hello]
---------------------------------------------------
#include <fstream.h>

template <typename T>
class Bracket
{public:
   T data;
   void display(ostream & out);
};

template <typename T>
void Bracket<T>::display(ostream &out)
{out<<"["<<data<<"]";}
