to the syntax of bool expression the ">

c109sp01f - CSc 109 Final Examination SUGGESTED ANSWERS 1...

Info icon This preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
CSc 109 Final Examination Friday 18 May 2001 SUGGESTED ANSWERS 1. Suppose we added the symbol ">>" to the syntax of bool expression in the Simple Language. In particular, <exp1> >> <exp2> would be true if the absolute value of <exp1> is >= the absolute value of <exp2). Also suppose that the virtual stack starts at FF and "goes down" in memory. Finally, suppose that the values of <exp1> and <exp2> are stored on the virtual stack, <exp1> at StkPtr+2 and <exp2> at StkPtr+1. Write the function GREATABS() which stores in a file the Simple Machine code which should be generated for the expression IF <exp1> >> <exp2> GOTO ONE where the code for evaluating and storing <exp1> and <exp2>) on the virtual stack has already been generated. The call to GREATABS() would take the form GREATABS(out,StkPtr,PC,ONE_ADD); where "out" is of type ostream, StkPtr, PC, and ONE_ADD are integers. PC gives the current value of the program counter (the number of words of Simple Machine code generated so far), and ONE_ADD is the address corresponding to the Simple Language variable "ONE". This question is similar to question 4 on test 2. There you were asked to write the sasm code; here you are asked to write the C++ code which generates the simple machine code. Hint: You would be wise to write some functions to call. void digit(ostream &out,int d){ if(d<10) cout<<d; else cout<<char('A'+d-10); } void genCode(ostream &out,int &PC, int op, int add){ digit(out,op); digit(out,add/16); digit(out,add%16); PC++; if(PC>255){ cerr<<"Code overflow"; exit(1); } out<<endl; } void GREATABS(ostream &out,int &StkPtr, int &PC,int add){ const int LOAD=3,MULT=7, SUB=6, BRANCHGE=10; genCode(out,PC,LOAD,StkPtr+2); genCode(out,PC,LOAD,StkPtr+2); genCode(out,PC,MULT,0); genCode(out,PC,LOAD,StkPtr+1); genCode(out,PC,LOAD,StkPtr+1); genCode(out,PC,MULT,0); genCode(out,PC,SUB,0); genCode(out,PC,BRANCHGE,add); StkPtr+=2; //pop values of <exp1> and <exp2> off the virtual stack } 2. (This question and question 3 are related. Design your answer to this question so that it can be used in question 3.) Write a template class PRay for "protected arrays." A protected array does not let you access
Image of page 1

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
entries outside the preset bounds of the array. For such a class the following code should compile, but should crash at runtime, because it accesses entries outside the bounds of the array. PRay<double,-3,10> x; //only allow x[-3], x[-2], ... x[10] for(int j=5; j<9;j++) x[j]=j*j+0.01; for(int j=7; j<12; j++) cout<<x[j]<<endl; //CRASH HERE WHEN J==11 #include <fstream.h> #include <stdlib.h> #include <math.h> template <class T,int lo, int hi> class PRay{ public: PRay(); PRay(const PRay & p); T & operator[] (int n); protected: T ray[hi-lo+1]; static void check(bool ok,char *mess); }; template <class T,int lo, int hi> PRay<T,lo,hi>::PRay(){ } template <class T,int lo, int hi> PRay<T,lo,hi>::PRay(const PRay<T,lo,hi> & p){ for(int j=0;j<hi;j++) ray[j]=p.ray[j]; } template <class T,int lo, int hi> T & PRay<T,lo,hi>::operator[](int n){ check(n>=lo && n<=hi,"([]) Range error"); return ray[n-lo]; } template <class T, int lo, int hi> void PRay<T,lo,hi>::check(bool ok,char *mess){ if(!ok){ cerr<<"ERROR: "<<mess<<endl; exit(1); } } 3. Write a template subclass, OPRa, of the class PRay above. An instance of OPRa stores the number of actual entries in the array, starting at the lower bound.
Image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern