Docsity

Exam Solutions for ECE 3035B Computing Mechanisms (Fall 2004) - Problems 1-3, Exams of Electrical and Electronics Engineering

Toggle sidebar
bg1
ECE 3035B Computing Mechanisms Fall 2004
4 problems, 5 pages Exam One Solutions 16 September 2004
1
Problem 1 (2 parts, 25 points)
Part A (15 points) For the following C code fragment, write the MIPS implementation. Assume the
variables a, b, and c are in $1, $2, and $3, respectively. You may use additional registers if needed.
if (a <= 5)
b = c โ€“ a - 25;
else
c = a/5;
a = b + c;
Label Instruction Comment
addi $4, $0, 5
slt $5, $4, $1 Is 5 < a?
bne $5, $0, Else If so, branch to Else
sub $6, $3, $1 $6: c-a
addi $2, $6, -25 b = c-a-25
j Continue
Else: div $1, $4 Lo = a/5
mflo $3 c = a/5
Continue: add $1, $2, $3 a = b + c
Part B (10 points) Consider the following MIPS code.
.data
V: .word 33, 44, 55, -9999, 100, 3, 22
.text
Start: addi $2, $0, 0 # sum = 0;
addi $5, $0, 0 # i = 0;
lw $3, V($5) # elt = V[i];
addi $4, $0, -9999
Loop: add $2, $2, $3
addi $5, $5, 1
sll $1, $5, 2
lw $3, V($1)
bne $3, $4, Loop
div $2, $5 # Lo = sum/I;
mflo $6 # $6 = avg;
jr $31
Assume that registers $2 and $5 contain the values of variables sum and i, respectively. Complete the
following C code fragment by filling in the dashed box with the appropriate C iteration construct.
sum = 0;
i = 0;
elt = V[0];
do {
sum += elt;
i++;
elt = V[i];
} while (elt != -9999);
avg = sum/i;
pf3
pf4

Partial preview of the text

Download Exam Solutions for ECE 3035B Computing Mechanisms (Fall 2004) - Problems 1-3 and more Exams Electrical and Electronics Engineering in PDF only on Docsity!

4 problems, 5 pages Exam One Solutions 16 September 2004

Problem 1 (2 parts, 25 points)

Part A (15 points) For the following C code fragment, write the MIPS implementation. Assume the

variables a, b, and c are in $1, $2, and $3, respectively. You may use additional registers if needed.

if (a <= 5) b = c โ€“ a - 25; else c = a/5; a = b + c;

Label Instruction Comment

addi $4, $0, 5 slt $5, $4, $1 Is 5 < a? bne $5, $0, Else If so, branch to Else sub $6, $3, $1 $6: c-a addi $2, $6, -25 b = c-a- j Continue Else: div $1, $4 Lo = a/ mflo $3 c = a/ Continue: add $1, $2, $3 a = b + c

Part B (10 points) Consider the following MIPS code.

.data V: .word 33, 44, 55, -9999, 100, 3, 22 .text Start: addi $2, $0, 0 # sum = 0; addi $5, $0, 0 # i = 0; lw $3, V($5) # elt = V[i]; addi $4, $0, - Loop: add $2, $2, $ addi $5, $5, 1 sll $1, $5, 2 lw $3, V($1) bne $3, $4, Loop div $2, $5 # Lo = sum/I; mflo $6 # $6 = avg; jr $

Assume that registers $2 and $5 contain the values of variables sum and i, respectively. Complete the

following C code fragment by filling in the dashed box with the appropriate C iteration construct.

sum = 0; i = 0; elt = V[0];

do { sum += elt; i++; elt = V[i]; } while (elt != -9999);

avg = sum/i;

4 problems, 5 pages Exam One Solutions 16 September 2004

Problem 2 (2 parts, 25 points) Control Flow Graphs

Part A (10 points) Partition the following MIPS code into basic blocks. For each basic block, fill in a box to the right with the address range of the instructions in the basic block. For example, if instructions at addresses 2000 through 2004 form a basic block, write โ€œ2000-2004โ€ in a box. Fill in one box for each basic block. You may not need to use all of the boxes. Box B1 has already been filled in as an example.

Address Label Instruction

1000 Minmax: addi $4, $0, 396 1004 lw $1, Array($4) 1008 lw $2, Array($4) 1012 Loop: lw $3, Array($4) 1016 slt $5, $3, $ 1020 beq $5, $0, Skip 1024 add $1, $3, $ 1028 Skip1: slt $5, $2, $ 1032 beq $5, $0, Skip 1036 add $2, $3, $ 1040 Skip2: addi $4, $4, - 1044 slt $5, $4, $ 1048 beq $5, $0, Loop 1052 jr $

Part B (15 points) Draw the control flow graph of the MIPS code in Part A, using the boxes you filled in. Label the edges of the control flow graph with โ€œTโ€ and โ€œFโ€ where appropriate. You do not need to fill in the boxes with addresses or instructions. The box B1 is already placed below to get you started.

B

T

F

T

F

B

B

B

B

F T

B

B

B1:

B2:

B5:

B6:

B9:

B10:

B8:

B7:

B3:

B4:

4 problems, 5 pages Exam One Solutions 16 September 2004

Problem 4 (3 parts, 25 points) Arrays

For each of the following C code fragments, list the MIPS implementation of the array reference. Assume

the arrays are allocated in static memory space, and W, X, Y, and Z currently reside in $1, $2, $3, and $

respectively. Assume the base address of each array can be referenced symbolically using the array name

(e.g., Array1, Array2, etc.). You can use $5 also if needed.

Part A (4 points)

int Array1[15]; W = Array1[X];

Label Instruction Comment

sll $1, $2, 2 # 4 * X (byte addr)

lw $1, Array1($1) # EA: Array1 + 4*X

Part B (9 points)

int Array2[10][50]; W = Array2[Y][X];

Label Instruction Comment

addi $1, $0, 50 # $1: Lx

mult $3,$1 # Lo: Y*Lx

mflo $1 # $1: Y*Lx

add $1, $1, $2 # $1: Y*Lx + X

sll $1, $1, 2 # convert to byte addr

lw $1, Array2($1) # EA: Array2 + 4(Y*Lx+X)

Part C (12 points)

int Array3[100][20][30]; W = Array3[Z][Y][X];

Label Instruction Comment

addi $5, $0, 600 # $5: LxLy

mult $4,$5 # Lo: Z*LxLy

mflo $5 # $5: Z*LxLy

addi $1, $0, 30 # $1: Lx

mult $3,$1 # Lo: Y*Lx

mflo $1 # $1: Y*Lx

add $1, $1, $2 # $1: Y*Lx + X

add $1, $1, $5 # $1: ZLxLy + YLx + X

sll $1, $1, 2 # convert to byte addr

lw $1, Array3($1) # look up Array3 elt