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

Info iconThis 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. if(d<10) cout<<d; else cout<<char('A'+d-10); } digit(out,op); digit(out,add/16); digit(out,add%16); PC++; if(PC>255){ cerr<<"Code overflow"; exit(1); } out<<endl; } 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
Background image of page 1

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

View Full DocumentRight 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> for(int j=0;j<hi;j++) ray[j]=p.ray[j]; } template <class T,int lo, int hi>
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 03/09/2008 for the course CSE 109 taught by Professor Kay during the Spring '08 term at Lehigh University .

Page1 / 12

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

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online