



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




4 problems, 5 pages Exam Two Solutions 8 November 2006
Problem 1 (4 parts, 28 points) Puzzle Match Baseline
In the puzzle match project, dynamic execution time can be significantly reduced by employing sub-word parallelism. Rather than match patterns one color at a time, all eight colors are compared simultaneously in the packed reference pattern and candidates. Write the following code fragments to implement parts of this approach. $1 and $5 may be used as temporaries.
Part A (6 points) In order to generate all possible sequences within the reference pattern, the pattern must be replicated in the upper 16 bits. Write MIPS code that copies the lower 16 bits of $2 into its upper 16 bits. label instruction comment sll $5, $2, 16 # copy lower 16 to upper
or $2, $2, $5 # combine lower and upper
Part B (10 points) A flipped reference will include the colors in reverse order. Write MIPS code that reverses the colors in the reference pattern (stored in $3), placing the results in $7. label instruction comment srl $7, $3, 14 # init $7 to Ref color 7
addi $1, $0, 7 # init loop counter
Reverse: sll $7, $7, 2 # make room on right end
andi $5, $3, 3 # extract rightmost color
or $7, $7, $5 # add color into $
srl $3, $3, 2 # shift out of Ref
addi $1, $1, -1 # decrement loop counter
bne $1, $0, Reverse # loop until done
Part C (6 points) Using the technique described above, all eight colors of a candidate are tested in a single word-wide comparison. In the worst-case, how many of these comparisons are needed to identify the matching candidate in the 8-candidate puzzle? Show all work.
1 compare/permutation x 8 permutations/candidate x 8 candidates = 64 compares
Part D (6 points) Suppose the choice of possible colors (e.g., red, yellow, green, blue) for each of the eight squares was increased from four to eight. Using the described algorithm, how would the dynamic instruction length be affected? For maximum credit, show all work.
Since all colors are compared in parallel (as described in part C), and the larger
patterns (24 bits versus 16 bits) fit in a word, there would be little effect on
dynamic execution.
4 problems, 5 pages Exam Two Solutions 8 November 2006
Problem 2 (3 parts, 20 points) Activation Frames
Consider the following C fragment: int main() { int X = 33; int Y = 55; int Z = 44; int P = 1; int Conditional_Exchange(int *, int *, int); Conditional_Exchange(&Y, &Z, P); printf("X = %d, Y = %d, Z = %d\n", X, Y, Z); return Z; } int Conditional_Exchange(int *A, int *B, int C) { int Temp; if (C) { Temp = *A; *A = B; B = Temp; } return(C); } Part A (10 points) Describe the current state of the stack before Conditional_Exchange returns to main. Fill in the unshaded boxes to show main’s and Conditional_Exchanges’s activation frames. Include a symbolic description and the actual value (in decimal). For return addresses, show only the symbolic description; do not include a value. Label the frame pointer and stack pointer. address description value 9900 RA of main’s caller 9896 FP of main’s caller SP, main’s FP 9892 (^) return value (^9888) X 33 (^9884) Y 55 44 (^9880) Z 44 55 (^9876) P 1 (^9872) return address (^9868) frame pointer 9892 (^9864) &Y (A) 9884 (^9860) &Z (B) 9880 (^9856) P (C) 1 C_E FP 9852 (^) return value 1 SP 9848 (^) temp 55 9844 9840 9836 9832 9828 9824
4 problems, 5 pages Exam Two Solutions 8 November 2006
Problem 3 (1 part, 30 points) Hash Tables
Consider an open hash table composed of a four-bucket table, with each bucket containing a variable length list. Each list entry has three slots corresponding to the three word groupings in the entries section. The hash function is key mod four. Inserted entries are appended to the end of a bucket list. Deallocated entries are maintained on a LIFO free list. When the free list is empty, new entry objects are allocated from heap memory. Accesses are listed as . Simulate the access list below and draw the ending state. Assume the hash table is initially empty, the heap pointer is initially 5016 and the free pointer is initially
Heap Pointer
5016 5028 5040 5052 (^5066) Free List
Buckets
5000 5040 5004 5016 5008 5028 5012
Entries
Hash Table Access Trace
1 insert 1001 111 5 insert 1018 555 2 insert 1002 222 6 remove 1010 n/a 3 insert 1010 333 7 insert 1000 666 4 insert 1001 444
4 problems, 5 pages Exam Two Solutions 8 November 2006
Problem 4 (3 parts, 22 points) Short Answer Consider the C code below: struct State { int Num; char Name[NAMELENGTH]; int YearPop[NUMYEARS]; float Growth; };
int main(int argc, char *argv[]) { struct State States[NUMSTATES]; int NumStates; struct State *Rank[NUMSTATES]; int I; … NumStates = Load_Census_Data(States, NUMSTATES, ++argv); … for (I = 0; I < NUMSTATES; I++) { States[I].Growth = (States[I].YearPop[5] - States[I].YearPop[0]) * 100.0 / States[I].YearPop[5]; Insert(&(States[I]), I, Rank); } … Part A (6 points) In the function Load_Census_Data(), strcpy is called on the state’s name before it is added to the State structure. Explain why. strcpy is required since the storage for the input string is originally on the stack and will be deallocated when the function returns. (strcpy copies in the heap) Part B (10 points) Complete the Insert() function below. / This routine performs an insertion sort of a State object into a sorted array of pointers. */
void Insert(struct State *New, int I, struct State *Rank[]) { while ((I > 0) && (New->Growth > Rank[I-1]->Growth)) { Rank[I] = Rank[I-1]; I--; }
Part C (6 points) List three differences between heap and stack data storage.