Docsity

Solved Exam 1 for Mechanisms Computing System | ECE 3035, Exams of Electrical and Electronics Engineering

Toggle sidebar
bg1
ECE 3035 12:00 p.m. Computing Mechanisms Spring 2007
4 problems, 4 pages Exam One Solution 21 February 2007
1
Problem 1 (4 parts, 25 points) Edge Detection Baseline
Suppose $3 contains the base address of a 32x32 pixel subarray within an image. Each pixel is a
packed RGB value contained in the lower 24 bits of a 32-bit word. Suppose $1 contains the
address of a pixel in the 32x32 subarray.
Part A (7 points) Write a MIPS code fragment that tests whether the pixel is on the top edge of
the subarray and if so, branches to the label Target.
Label Instruction Comment
sub $2, $1, $3 # subtract array base
slti $2, $2, 128 # is pixel in first row?
bne $2, $0, Target # then skip to target
Part B (7 points) Write code that test whether the pixel is on the right boundary of the subarray
and if so, branches to the label Target.
Label Instruction Comment
sub $2, $1, $3 # subtract array base
addi $2, $2, 4 # compute next pixel position
andi $2, $2, 127 # is next pixel on left edge?
beq $2, $0, Target # then skip to target
Part C (4 points) Suppose $8 holds variable x and $11 holds variable y. What is the
mathematical expression computed by the following code fragment (e.g., “y = 2*y”)?
slt $5, $8, $11
beq $5, $0, Next
add $8, $11, $0
Next: …
Answer: y = min(x, y)
Part D (7 points) Registers $13, $14, and $15 each contain a color pixel value (R, G, B,
respectively) in their lower 8 bits (with 0’s in their upper 24 bits). Write code to pack them into
an RGB color pixel representation (in the lower 24 bits of a word) and store it in an output image
array whose base address is labeled Edges at the pixel offset stored in $2. You may modify
registers $2, $13, $14, $15.
Label Instruction Comment
sll $13, $13, 8 # shift red left one byte
or $13, $13, $14 # combine red and green
sll $13, $13, 8 # shift red & green left one byte
or $13, $13, $15 # add blue to red & green
sw $13, Edges($2) # store packed word in to memory
pf3
pf4

Partial preview of the text

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

4 problems, 4 pages Exam One Solution 21 February 2007

Problem 1 (4 parts, 25 points) Edge Detection Baseline

Suppose $3 contains the base address of a 32x32 pixel subarray within an image. Each pixel is a

packed RGB value contained in the lower 24 bits of a 32-bit word. Suppose $1 contains the

address of a pixel in the 32x32 subarray.

Part A (7 points) Write a MIPS code fragment that tests whether the pixel is on the top edge of

the subarray and if so, branches to the label Target.

Label Instruction Comment sub $2, $1, $3 # subtract array base

slti $2, $2, 128 # is pixel in first row?

bne $2, $0, Target # then skip to target

Part B (7 points) Write code that test whether the pixel is on the right boundary of the subarray

and if so, branches to the label Target.

Label Instruction Comment sub $2, $1, $3 # subtract array base

addi $2, $2, 4 # compute next pixel position

andi $2, $2, 127 # is next pixel on left edge?

beq $2, $0, Target # then skip to target

Part C (4 points) Suppose $8 holds variable x and $11 holds variable y. What is the

mathematical expression computed by the following code fragment (e.g., “y = 2*y”)?

slt $5, $8, $

beq $5, $0, Next

add $8, $11, $

Next: …

Answer : y = min(x, y)

Part D (7 points) Registers $13, $14, and $15 each contain a color pixel value (R, G, B,

respectively) in their lower 8 bits (with 0’s in their upper 24 bits). Write code to pack them into

an RGB color pixel representation (in the lower 24 bits of a word) and store it in an output image

array whose base address is labeled Edges at the pixel offset stored in $2. You may modify

registers $2, $13, $14, $15.

Label Instruction Comment sll $13, $13, 8 # shift red left one byte

or $13, $13, $14 # combine red and green

sll $13, $13, 8 # shift red & green left one byte

or $13, $13, $15 # add blue to red & green

sw $13, Edges($2) # store packed word in to memory

4 problems, 4 pages Exam One Solution 21 February 2007

Problem 2 (3 parts, 27 points) Pointers and Structs

Part A (8 points) The following variables are allocated in memory beginning at address 5000.

Complete the memory map below with the variable names at each word or byte in memory. Do

not include the variable’s value.

int A; int B[] = {1, 2}; char C = ‘z’; char D[] = “Hello”; int *E; char *F;

5000 A → → →

5004 B[0] → → →

5008 B[1] → → →

5012 C D[0] D[1] D[2]

5016 D[3] D[4] D[5] slack

5020 E → → →

5024 F → → →

Part B (10 points) Suppose the following code follows the definitions in part A. List the values

of the listed variables following execution of this code.

E = B;

A = *E + E[1];

F = &(D[1]);

C = *F;

A 3 C ‘e’ D 5013 E 5004 F 5014

Part C (9 points) Consider the following struct declaration.

struct State { int Num; char Name[NAMELENGTH]; int YearPop[NUMYEARS]; float Growth; };

Suppose S is a pointer to a previously allocated and initialized State object. Write proper C statements to

perform the following struct accesses.

Store initial year population in integer P (^) P = S->YearPop[0];

Store first character of Name in character C C = S->Name[0];

Set Growth to percentage growth between years 0 and 1. (Don’t worry if it’s the max.)

S->Growth = 100.0 * (S->YearPop[1] – S->YearPop[0]) / S->YearPop[0];

4 problems, 4 pages Exam One Solution 21 February 2007

Problem 4 (2 parts, 28 points) Activation Frames

Part A (24 points) The function Bar (below left) calls function Foo after completing code block

1. Write MIPS assembly code that properly calls Foo. You should include all instructions

between code block 1 and code block 2. List the symbolic names and values for each stack

entry (below right). Bar is called by a jal instruction at address 2000. Foo returns 55.

int Bar() {

int A = 21, B = 33; char C[] = “Hi!”; int Foo(int *, char *);

(code block 1)

B = Foo(&A, C)

(code block 2)

Bar’s FP 9900 Bar’s RV 9896 A = 21 9892 B = 33 9888 C = “Hi!”

(^9884) Bar’s RA, 2004

(^9880) Bar’s FP, 9900

(^9876) &A, 9896

(^9872) C, 9888

SP, Foo’s FP → 9868 Foo’s RV, 55

label instruction comment

addi $29, $29, -20 # allocate stack space for Foo call

sw $31, 16($29) # preserve Bar’s return address

sw $30, 12($29) # preserve Bar’s frame pointer

addi $2, $30, -4 # compute address of A

sw $2, 8($29) # store &A

addi $2, $30, -12 # compute address of C[]

sw $2, 4($29) # store C

jal Foo # call Foo

lw $31, 16($29) # restore Bar’s return address

lw $30, 12($29) # restore Bar’s frame pointer

lw $2, 0($29) # load Foo’s return value

sw $2, -8($30) # store in B

addi $29, $29, 20 # deallocate stack space for Foo call

Part B (4 points) short answer:

Explain why an activation frame cannot be fully allocated by a called function.

An activation frame must be partially created by the caller so it can initialize

input parameters to the called function and access the returned value.