Docsity

Final Exam Solutions for ECE 3035B - Computing Mechanisms, Spring 2006, Exams of Electrical and Electronics Engineering

Toggle sidebar
bg1
ECE 3035B 1:00pm Computing Mechanisms Spring 2006
5 problems, 7 pages Final Exam Solutions 3 May 2006
1
Problem 1 (1 part, 20 points) Iteration and Control Flow Graphs
Draw the control flow graph for the body of the following procedure.
int Foo (int S, int N, int Array[][N]) {
int I, J;
int Sum = 0;
for (I = 0; I < N; I++) {
J = I;
while (Array[I][J] != S) {
Sum += Array[I][J];
J++;}
return Sum;}
T
F
I < N
int I, J;
int Sum = 0;
I = 0;
J = I;
return Sum;
Array[I][J] != S
T
F
Sum += Array[I][J];
J++;
I++;
pf3
pf4
pf5

Partial preview of the text

Download Final Exam Solutions for ECE 3035B - Computing Mechanisms, Spring 2006 and more Exams Electrical and Electronics Engineering in PDF only on Docsity!

5 problems, 7 pages Final Exam Solutions 3 May 2006

Problem 1 (1 part, 20 points) Iteration and Control Flow Graphs

Draw the control flow graph for the body of the following procedure.

int Foo (int S, int N, int Array[][N]) {

int I, J;

int Sum = 0;

for (I = 0; I < N; I++) {

J = I;

while (Array[I][J] != S) {

Sum += Array[I][J];

J++;}

return Sum;}

F T

I < N

int I, J;

int Sum = 0;

I = 0;

J = I;

return Sum;

Array[I][J] != S

F T

Sum += Array[I][J];

J++;

I++;

5 problems, 7 pages Final Exam Solutions 3 May 2006

Problem 2 (4 parts, 34 points) Baseline Understanding

Part A (10 points) In Word Search, SWI 524 returns the packed(Y:X)letter map size in $01.

Write assembly code that places the size of X in $03, the size of Y in $04, and the number of

letters in the map in $01.

label instruction comment

swi 524 # create letter map

andi $03, $01, 15 # load X size

srl $04, $01, 4 # load Y size

mult $03, $04 # compute number of letters

mflo $01 # store in $

For parts B – D, refer to the following code fragment from Diamond Sort. Inputs $11, $12 and

$13 are Sptr, Num, and Step respectively; $04 = 4 (constant).

L01 : mult $12, $13 # I- mflo $20 # I- add $20, $20, $11 # I- add $14, $11, $13 # I- L02 : lw $15, 0($14) # I- sub $17, $14, $13 # I- srl $16, $15, 6 # I- bne $13, $04, L03 # I- andi $16, $15, 63 # I- L03 : lw $18, 0($17) # I- srl $19, $18, 6 # I- bne $13, $04, L04 # I- andi $19, $18, 63 # I- L04 : slt $02, $16, $19 # I- beq $02, $00, L05 # I- add $02, $17, $13 # I- sw $18, 0($02) # I- sub $17, $17, $13 # I- slt $02, $17, $11 # I- beq $02, $00, L03 # I- L05 : add $02, $17, $13 # I- sw $15, 0($02) # I- add $14, $14, $13 # I- bne $14, $20, L02 # I- jr $31 # I-

Part B (8 points) How many times is the loop beginning at L02 : executed?

Part C (8 points) Describe the behavior of the code changes if Step is equal to four versus not

equal to four. State what the code does rather than how it does it. Don’t transliterate.

Step == 4:

Step <> 4:

Part D (8 points) This routine operates on a list of values in memory beginning at Sptr. Define

the quantitative ordering of these values that will result in the worst case execution time.

5 problems, 7 pages Final Exam Solutions 3 May 2006

Part B (8 points) Given the state of the stack you computed in Part A, write a MIPS code

fragment to implement the statement from Baz: “Sum += Values[X];” Do not use absolute

addresses in your code; instead, access variables relative to the frame pointer.

instruction comment

lw $1, 4($30) load Array

lw $2, -4($30) load X

sll $2, $2, 2 adjust X by word size (in bytes)

add $1, $1, $2 effective address of Values[X]

lw $1, 0($1) load Values[X]

lw $2, -8($30) load Sum

add $2, $1, $2 Sum + Value[X]

sw $2, -8($30) Sum += Value[X]

Part C (8 points) Given the state of the stack you computed in Part A, write a MIPS code

fragment to implement the statement from Baz: “*End += 1;” Do not use absolute addresses in

your code; instead, access variables relative to the frame pointer.

instruction comment

lw $1, 8($30) load &End

lw $2, 0($1) dereference End

addi $2, $2, 1 increment bound

sw $2, 0($1) *End += 1;

5 problems, 7 pages Final Exam Solutions 3 May 2006

Problem 4 (2 parts, 24 points) Hash Table Implementation

Part A (12 points) Write a C function that

allocates LINKBLOCKSIZE new links from the

heap, initializes them (Key and Value = -1),

and pushes them onto the head of the hash

table’s FreeLinks list. Your code should print

an error when insufficient heap space is

available.

#define LINKBLOCKSIZE 16

typedef struct Link { int Key; int Value; struct Link *Next; } Link;

typedef struct { Link **Buckets; int NumBuckets; Link *FreeLinks; int Size; } HashTable;

void Add_New_Links(HashTable *HT) {

Link *NewLinks;

int Index;

void *malloc(unsigned int);

NewLinks = (Link *) malloc(LINKBLOCKSIZE * sizeof(Link));

if (NewLinks == NULL)

printf("Out of heap memory\n");

else

for (Index = 0; Index < LINKBLOCKSIZE; Index++) {

NewLinks[Index].Key = -1;

NewLinks[Index].Value = -1;

NewLinks[Index].Next = HT.FreeLinks;

HT.FreeLinks = &(NewLinks[Index]);

Part B (12 points) Write a C fragment that reinserts hash table entries (Link) from an old

bucket array (Link **OldBuckets) of size OldNumBuckets into a newly resized hash

table (HashTable *HT). Assume the resized hash table is already correctly initialized and

sized. You may call only the hash function: int Hash(HashTable *HT, int Key).

for (OldBucket = 0; OldBucket < OldNumBuckets; OldBucket++) {

ThisLink = OldBuckets[OldBucket];

while (ThisLink != NULL) {

NextLink = ThisLink->Next;

Bucket = Hash(HT, ThisLink->Key);

ThisLink->Next = HT->Buckets[Bucket];

HT->Buckets[Bucket] = ThisLink;

ThisLink = NextLink;

5 problems, 7 pages Final Exam Solutions 3 May 2006

Part C (8 points) Consider a multitasking system with four tasks T A, TB , TC , and TD. TA and TB

execute at equal priority. TC gets twice as much processor time as TA while TD gets a third of the

processor time of TA. What percentage of the processor time does TD receive? Ignore external

factors (page faults, interrupts, waits for I/O, etc.) Show work.

TA = TB = X; TC = 2X; TD = X/3;

(X/3) / (X + X + 2X + X/3) = 1/13 = 7.7%

Part D (6 points) Explain three differences between the implementation of interrupt handling

and multitasking.

1. interrupt handlers use memory stack from current task

2. multitasking employs a clock to divide processor time

3. multitasking swaps task’s IP, register file, VM xlate table

Part E (6 points) List three differences between the heap and stack allocated data storage.

1. heap: manually allocated and deallocated. stack: automatic

2. heap: data persists across procedure invocation. stack: data lifetime = procedure

3. heap: all elements always accessible. stack: only top elements accessible.