


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, 4 pages Exam One Solution 16 February 2005
Problem 1 (2 parts, 25 points) VotingMap Implementation
Consider the following fragment from the VotingMap baseline implementation: slt $1, $3, $ bne $1, $0, GBtest slt $1, $3, $ bne $1, $0, Bwinner lui $3, 255 beq $0, $0, Update Bwinner: addi $3, $0, 255 beq $0, $0, Update GBtest: slt $1, $4, $ bne $1, $0, Bwinner ori $3, $0, 65280 Update: sw $3, Array($2) Part A (15 points) Draw the flow diagram for the fragment above. Be sure to label blocks with the corresponding code from the fragment.
Red 16M Blue 255 (^) Green 64K
sw result
Part B (10 points) Now re-express the fragment above as a C fragment. Assume R is in $3, G is in $4, B is in $5, and N is in $2. Further assume that Array is an integer array.
if (R >= G) if (R >= B) Result = 16,711,680; /* Red wins / else Result = 255; / Blue wins / else if (G >= B) Result = 65280; / Green wins / else Result = 255; / Blue wins */ Array[N] = Result;
4 problems, 4 pages Exam One Solution 16 February 2005
Problem 2 (2 parts, 31 points) Array and Pointers
Part A (15 points) Write the MIPS code implementation of the dynamically allocated array access below. A pointer to the array (declared below) is stored in $3. Variables A, B, and C reside in $4, $5, and $6 respectively. Modify only $1 and $2 and the indexed memory location.
int Array[8][12][4]; /* array declaration */
Array[A][B][C]= 375; /* implement this */ label instruction comment addi $1, $0, 48 load subarray size
mul $1, $4 A * 48
mflo $2 starting EA
sll $1, $5, 2 B * 4
add $2, $2, $1 add column offset to EA
add $2, $2, $6 add row offset
sll $2, $2, 2 multiple EA by word size, 4
add $2, $3, $2 add base address
addi $1, $0, 375 load constant
sw $1, 0($2) store 375 at Array[A][B][C]
Part B (16 points) The following static variables are allocated in memory beginning at address
int A = 13; int *B = &A; int C[10] = {00, 11, 22, 33, 44, 55, 66, 77, 88, 99};
4 problems, 4 pages Exam One Solution 16 February 2005
Problem 4 (1 part, 15 points) C Code
Write a C code fragment that best expresses the following control flow diagram.
switch (N) { case 2: A = 3 / B; case 3: C = 4 + D; break; case 4: 5 * D; break; case 5: C = 9 โ B; default: A = B + D; }