Docsity

Final Exam Solution for Mechanisms-Computing System | ECE 3035, Exams of Electrical and Electronics Engineering

Toggle sidebar
bg1
ECE 3035 B 1 -2 p.m. Computing Mechanisms Spring 2008
5 problems, 8 pages Final Exam Solutions 28 April 2008
Problem 1 (5 parts, 30 points) Compiler Optimization
Consider the following program fragment.
a = f(50);
b = g(0);
if (a < 0) {
if (b > 100)
e = h(a, b);
else
e = 0;
b = e * a;
} else {
g = g(1);
h = g * 4;
h = h + g;
}
printf(“answer: %d\n”, b);
Part A (5 points) The control flow graph for this
fragment is shown below. Fill in the definitions
and uses of the variables (e.g., “def x” and “use w”)
in the basic blocks of the graph.
Part B (7 points) Identify the webs (of def-use
chains) in the control flow graph by drawing a
region for each variable and labeling each web S1,
S2, etc.
1
FT
F
T
def a
def b
use a
use b def g
use g
def h
use h
use g
def h
use a
use b
def e
def e
use e
use a
def b
use b
s1
s2
s3
s4
s5
s6
pf3
pf4
pf5

Partial preview of the text

Download Final Exam Solution for Mechanisms-Computing System | ECE 3035 and more Exams Electrical and Electronics Engineering in PDF only on Docsity!

5 problems, 8 pages Final Exam Solutions 28 April 2008

Problem 1 (5 parts, 30 points) Compiler Optimization

Consider the following program fragment. a = f(50); b = g(0); if (a < 0) { if (b > 100) e = h(a, b); else e = 0; b = e * a; } else { g = g(1); h = g * 4; h = h + g; } printf(“answer: %d\n”, b);

Part A (5 points) The control flow graph for this fragment is shown below. Fill in the definitions and uses of the variables (e.g., “def x” and “use w”) in the basic blocks of the graph.

Part B (7 points) Identify the webs (of def-use chains) in the control flow graph by drawing a region for each variable and labeling each web S1, S 2, etc.

T F

F

T

def a

def b

use a

use b def g

use g

def h

use h

use g

def h

use a

use b

def e

def e

use e

use a

def b

use b

s

s

s

s

s

s

5 problems, 8 pages Final Exam Solutions 28 April 2008

Part C (5 points) Draw the interference graph using the nodes to represent the webs identified in part B.

Part D (4 points) What is the minimum number of registers needed to cover all the variables in this code? Show work for maximum credit.

3 Registers

Part E (9 points) Perform at least three standard compiler optimizations on the following C code fragment by writing the optimized version (in C) to the right.

int foo(int x) { int foo(int x) {

int i, d, a = 64; int i;

int z = 0;

int c = a * x;

int c = a << 6;

int temp = x + 25;

for (i=0; i < c; i++) { Ö int d = temp * temp;

d = (x + 25)*(x + 25); for (i=0; i < c; i++)

z = bar(z, d);} z = bar(z, d);

return z;} return z;}

Briefly describe which standard compiler optimizations you applied:

Constant propagation (a = 64), dead code elimination, strength

reduction (a * 64 = a << 6), common subexpression elimination (x + 25),

loop invariant removal (calculation of d).

s4 s

s

s

s6 s

5 problems, 8 pages Final Exam Solutions 28 April 2008

Problem 3 (3 parts, 30 points) C Baselines

Part A (10 points) The crypto program encodes a ASCII text file into code output using a Dictionary array. Complete the missing lines below. void EncodeStream(char *Dictionary) { int Ch, I; int getchar(); Ch = getchar(); while (Ch != EOF) { if (Ch >= 'A' && Ch <= 'Z') putchar(Dictionary[Ch - 'A']); else if (Ch >= 'a' && Ch <= 'z') putchar(Dictionary[Ch - 'a'] - 'A' + 'a'); else putchar(Ch); Ch = getchar(); } } Part B (10 points) In the edge detection program, each component is compared to a current Min and Max. Write a C function Compare that compares a pixel value against the current Min and Max. If the value exceeds the min-max range, the appropriate limit is updated. Value, Min, and Max are parameters for this function. Hint: Compare is a void function.

void Compare(unsigned char Value, unsigned char *Min, unsigned char *Max) { if (Value < *Min) *Min = Value; if (Value > *Max) *Max = Value; }

Part C (10 points) Explain the difference between int People[100]; and extern int People[100];.

The first statement declares the name, size, and type and allocates storage. The second statement declares the name, size, and type; it is allocated elsewhere.

Regarding implementation, explain the differences between an array and a struct.

Arrays entry types are homogeneous, so indexing is implemented as address arithmetic. Struct entry types are heterogeneous, so field offsets are irregular (compiler computed).

In a makefile, what does the line beginning with .c.o: express?

This rule shows how a file with .c extension can produce a file with a .o extension. This rule calls the compiler to produce an object file.

5 problems, 8 pages Final Exam Solutions 28 April 2008

Problem 4 (5 parts, 35 points) Heap Management

Below is a snapshot of heap storage. Values that are pointers are denoted with a “$”. The heap pointer is $6168. The heap has been allocated contiguously beginning at $6000, with no gaps between objects. addr value addr value addr value addr value addr value addr value 6000 16 6032 12 6064 $6004 6096 16 6128 12 6160 0 6004 33 6036 28 6068 4 6100 8024 6132 $6120 6164 0 6008 $6072 6040 6056 6072 $6100 6104 16 6136 0 6168 0 6012 16 6044 60 6076 8 6108 $6004 6140 0 6172 0 6016 680 6048 16 6080 80 6112 6156 6144 12 6176 0 6020 8 6052 $6128 6084 $6120 6116 4 6148 16 6180 0 6024 25 6056 $6080 6088 4 6120 600 6152 8 6184 0 6028 6092 6060 0 6092 6028 6124 20 6156 6052 6188 0

Part A (9 points) Suppose the stack holds a local variable whose value is the memory address $6052. No other registers or static variables currently hold heap memory addresses. List the addresses of all objects in the heap that are not garbage.

Addresses of Non-Garbage Objects : (^) 6052, 6128, 6120, 6080, 6004, 6072, 6100

Part B (9 points) Create a sorted (by size) free list by scanning the memory for garbage, starting at address $6000 and inserting each garbage object into the free list in increasing size order. List the addresses of the objects (in order) on the free list at the end of the scan.

Free List : (^) 6092, 6024, 6036, 6152

Part C (4 points) Based on the free list created in part B, if an object of size 19 bytes is allocated, what address will be returned using a first-fit allocation strategy? How many bytes of slack (if any) will result?

Address : (^6172)

Slack: (^) 1 byte

Part D (4 points) Based on the free list created in part B, if an object of size 7 bytes is allocated, what address will be returned using a first-fit allocation strategy? How many bytes of slack (if any) will result?

Address : (^6024)

Slack: (^) 1 byte

Part E (9 points) If the local variable whose value is the address $6052 is popped from the stack, which addresses will be reclaimed by each of the following strategies? If none, write “none.” (You do not have to list addresses already on the free list from part B.)

Reference Counting: (^) 6052, 6128, 6080, 6120

Mark and Sweep: (^) 6052, 6128, 6080, 6120, 6004, 6072, 6100

Old-New Space (copying): (^) 6052, 6128, 6080, 6120, 6004, 6072, 6100