Docsity

Exam 2 Solutions - Mechanisms-Computing System | ECE 3035, Exams of Electrical and Electronics Engineering

Toggle sidebar
bg1
ECE 3035 A & B Computing Mechanisms Spring 2008
4 problems, 5 pages Exam Two Solutions 9 April 2008
1
Problem 1 (3 parts, 22 points) Path Search
Part A (6 points) The diamond search map above list the cost of visiting each square (in black)
and the position of the square (in blue). Answer the following questions (show work).
Cost to square 55 47 – 38 + 89 – 79 + 60 = 79
Cost to square 48 -75 + 37 = -38
Cost to square 25 -75 + 37 – 90 – 79 – 74 = -281
Part B (11 points) Write a MIPS subroutine that computes (in general) the path cost from the
starting square to square 115. Assume $3 is initialized to the address of square 71. Return the
path cost in register $2.
label instruction comment
CostTo115: addi $5, $0, 4 # count equals run - 1
lw $2, Array($3) # load initial weight
Loop: add $3, $3, 44 # adjust for next square
lw $1, Array($3) # load weight
add $2, $2, $1 # add to sum
addi $5, $5, -1 # next loop?
bne $5, $0, Loop # loop until done
jr $31 # return to caller
Part C (5 points) Explain the flaw in creating a minimum cost path search program with no
limits on path length.
If the path length is unbound, the search algorithm will enter an infinite loop
stepping in and out of negative squares as the cost moves towards negative
infinity.
pf3
pf4
pf5

Partial preview of the text

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

4 problems, 5 pages Exam Two Solutions 9 April 2008

Problem 1 (3 parts, 22 points) Path Search

Part A (6 points) The diamond search map above list the cost of visiting each square (in black)

and the position of the square (in blue). Answer the following questions (show work).

Cost to square 55 47 – 38 + 89 – 79 + 60 = 79

Cost to square 48 -75 + 37 = -

Cost to square 25 -75 + 37 – 90 – 79 – 74 = -

Part B (11 points) Write a MIPS subroutine that computes (in general) the path cost from the

starting square to square 115. Assume $3 is initialized to the address of square 71. Return the

path cost in register $2.

label instruction comment

CostTo115: addi $5, $0, 4 # count equals run - 1

lw $2, Array($3) # load initial weight

Loop: add $3, $3, 44 # adjust for next square

lw $1, Array($3) # load weight add $2, $2, $1 # add to sum addi $5, $5, -1 # next loop? bne $5, $0, Loop # loop until done jr $31 # return to caller

Part C (5 points) Explain the flaw in creating a minimum cost path search program with no

limits on path length.

If the path length is unbound, the search algorithm will enter an infinite loop

stepping in and out of negative squares as the cost moves towards negative

infinity.

4 problems, 5 pages Exam Two Solutions 9 April 2008

Problem 2 (2 parts, 30 points) Activation Frames

Consider the following C fragment: int main() { int X = 100, Y = 255, Z = 1, A[] = {3, 5, 7}; int Foo(int, int *, int *); Z = Foo(X, &Y, A); printf("X = %d, Y = %d, A[0]=%d, A[1]=%d, A[2]=%d\n", X, Y, A[0], A[1], A[2]); return (Y); } int Foo(int P, int *Q, int V[]) { int Z = 35, W[] = {2, 4}; V[2] = W[0]; *Q = W[1]; P = P + Z; return(P); } Part A (15 points) Describe the current state of the stack before Foo returns to main. Fill in the unshaded boxes to show main’s and Foo’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 main’s RV (^9888) A[2] 7 2 (^9884) A[1] 5 (^9880) A[0] 3 (^9876) Z 1 (^9872) Y 255 4 (^9868) X 100 (^9864) RA of main (^9860) FP of main 9892 (^9856) V 9880 (^9852) Q 9872 (^9848) P 100 135

Foo’s FP 9844 Foo’s RV 135 (^9840) W[1] 4 (^9836) W[0] 2

SP 9832 Z 35 9828 9824

4 problems, 5 pages Exam Two Solutions 9 April 2008

Problem 3 (6 parts, 30 points) Heap Management

Below is a snapshot of heap storage. Values that are pointers are denoted with a “$”. The heap pointer is

$6168 and the free pointer is $6100. 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 0 6096 16 6128 12 6160 0 6004 $6036 6036 0 6068 4 6100 $6080 6132 8000 6164 0 6008 0 6040 24 6072 $6148 6104 16 6136 2525 6168 0 6012 16 6044 $6036 6076 8 6108 5 6140 0 6172 0 6016 3 6048 16 6080 $6072 6112 $6004 6144 20 6176 0 6020 8 6052 20 6084 0 6116 8 6148 0 6180 0 6024 25 6056 $6120 6088 4 6120 $6132 6152 8 6184 0 6028 52 6060 0 6092 $6052 6124 24 6156 26 6188 0

Part A (4 points) Assuming the heap has been allocated contiguously beginning at 6000, with no

gaps between objects, circle all object size words in the map above.

Part B (8 points) List the address of the first data word in each object in the free list in order, starting with the head of the free list.

Free List : 6100, 6080, 6072, 6148

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

Address : 6172

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?

Address : 6100

Part E (5 points) If the next instruction is free(ptr)

where ptr = $6024, what changes to storage will result?

FP: $6024; 6024: $

Part F (5 points) How would free(ptr) where ptr = $6056, a pointer not returned from malloc, cause

errors?

The data word above $6056 would be interpreted as the size of the object (20) and 20 bytes

would be freed, but they are not part of 1 contiguous object. They overlap with an object that’s

already on the free list. Reuse/changes to 1 object would cause unintended side effects to the other.

4 problems, 5 pages Exam Two Solutions 9 April 2008

Problem 4 (3 parts, 18 points) Short Answer

Part A (8 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 b) { int foo(int b) {

int x = 32; int temp = (b-82);

int a, n, z = 0; int a = (temp*temp)<<5;

for (n=100; n > 0; n--) { Ö int n, z = 0;

a = (b-82)*(b-82); for (n=100; n > 0; n--)

z = bar(z, a*x);} z = bar(z, a);

return z;} return z;}

Briefly describe which standard compiler optimizations you applied:

Common subexpression elimination (b-82), constant propagation (32);

dead-code elimination (int x = 32); strength reduction (“*32” replaced w/ “<<5”);

loop invariant removal (a = (b-82)(b-82) and ax).

Part B (5 points) Why would a program need to allocate data on the heap rather than on the

stack?

If the data needs to persist across procedure invocation and/or if the number of data

objects to be allocated cannot be determined at compile-time (e.g., depends on the input

data set), then the data should be allocated on the heap.

Part C (5 points) Define “memory leak” and explain why they commonly occur.

A memory leak occurs when memory is allocated but not freed up even when it’s no longer

used. They occur when the program loses track of the pointer to the allocated storage (e.g.,

when the pointer is stored as a local variable and is popped from the stack on procedure

return.