Download Final Exam Review - Computing Mechanisms | ECE 3035 and more Exams Electrical and Electronics Engineering in PDF only on Docsity!
5 problems, 8 pages Final Exam 4 May 2007
Instructions: This is a closed book, closed note exam. Calculators are not permitted. If you have
a question, raise your hand and I will come to you. Please work the exam in pencil and do not
separate the pages of the exam. For maximum credit, show your work.
Good Luck!
Your Name ( please print ) ________________________________________________
A
B
C
D total
E
5 problems, 8 pages Final Exam 4 May 2007
Problem 1 (2 parts, 30 points) Compiling MIPS
Part A (20 points) Write an efficient MIPS program fragment that is equivalent to the C program
fragment below. Assume x is in register $3 and sum is in register $4.
int x = 0; int sum = 0; do { sum =+ x; x++; } while (x < 16) sum = x * sum;
label instruction comment
Part B (10 points) Assume the following C declarations:
double A[] = {3.14159, 1.67, 1.932};
double *DP;
Assume A is stored on the stack at an offset of -24 bytes from the frame pointer $30. Write an efficient
MIPS program fragment that is equivalent to the C program fragment below. Use $1 to maintain DP.
DP = A;
DP++;
label data/instruction comment
5 problems, 8 pages Final Exam 4 May 2007
Problem 3 (5 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. 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
Part A (8 points) Suppose the stack holds a local variable whose value is the memory address $6052 and
register $3 holds the address $6148. 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 :
Part B (8 points) Create a free list by scanning the memory for garbage, starting at address $6000 and
pushing each reclaimed object on the front of the free list. List the addresses of the objects (in order) on
the free list at the end of the scan.
Free List :
Part C (3 points) Based on the free list created in part B, if an object of size 13 bytes is allocated, what
address will be returned using a best-fit allocation strategy?
Address :
Part D (3 points) Based on the free list created in part B, if an object of size 9 bytes is allocated, what
address will be returned using a best-fit allocation strategy?
Address :
Part E (8 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:
Mark and Sweep:
Old-New Space (copying):
5 problems, 8 pages Final Exam 4 May 2007
Problem 4 (1 part, 25 points) Activation Frames
Part A (25 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!”;
(code block 1)
A = Foo(C, &B, 125);
(code block 2)
Bar’s FP 9900 9896 A = 21 9892 B = 33 9888 C = “Hi!” 9884
9880
9876
9872
9868
9864
label instruction comment
5 problems, 8 pages Final Exam 4 May 2007
Part D (15 points) Write MIPS assembly code to implement Child_IQ(), shown below.
Assume standard activation frame conventions for the caller and callee. Set $30 as the frame
pointer. Also label all slots in the stack diagram.
int Child_IQ(struct Newt *Mom, struct Newt *Dad) { return ((Mom->IQ + Dad->IQ) / 2); }
Label Instruction Comment
Child_IQ:
stack diagram ( label all slots )
SP-> 9980
5 problems, 8 pages Final Exam 4 May 2007
MIPS Instruction Set (core)
instruction example meaning arithmetic add add $1,$2,$3 $1 = $2 + $ subtract sub $1,$2,$3 $1 = $2 - $ add immediate addi $1,$2,100 $1 = $2 + 100 add unsigned addu $1,$2,$3 $1 = $2 + $ subtract unsigned subu $1,$2,$3 $1 = $2 - $ add immediate unsigned addiu $1,$2,100 $1 = $2 + 100 set if less than slt $1, $2, $3 if ($2 < $3), $1 = 1 else $1 = 0 set if less than immediate slti $1, $2, 100 if ($2 < 100), $1 = 1 else $1 = 0 set if less than unsigned sltu $1, $2, $3 if ($2 < $3), $1 = 1 else $1 = 0 set if < immediate unsigned sltui $1, $2, 100 if ($2 < 100), $1 = 1 else $1 = 0 multiply mult $2,$3 Hi, Lo = $2 * $3, 64-bit signed product multiply unsigned multu $2,$3 Hi, Lo = $2 * $3, 64-bit unsigned product divide div $2,$3 Lo = $2 / $3, Hi = $2 mod $ divide unsigned divu $2,$3 Lo = $2 / $3, Hi = $2 mod $3, unsigned transfer move from Hi mfhi $1 $1 = Hi move from Lo mflo $1 $1 = Lo
load upper immediate lui $1,100 $1 = 100 x 2 16
logic and and $1,$2,$3 $1 = $2 & $ or or $1,$2,$3 $1 = $2 | $ and immediate andi $1,$2,100 $1 = $2 & 100 or immediate ori $1,$2,100 $1 = $2 | 100 nor nor $1,$2,$3 $1 = not($2 | $3) xor xor $1, $2, $3 (^) $1 = $2 ⊕ $ xor immediate xori $1, $2, 255 (^) $1 = $2 ⊕ 255 shift shift left logical sll $1,$2,5 $1 = $2 << 5 (logical) shift left logical variable sllv $1,$2,$3 $1 = $2 << $3 (logical), variable shift amt shift right logical srl $1,$2,5 $1 = $2 >> 5 (logical) shift right logical variable srlv $1,$2,$3 $1 = $2 >> $3 (logical), variable shift amt shift right arithmetic sra $1,$2,5 $1 = $2 >> 5 (arithmetic) shift right arithmetic variable srav $1,$2,$3 $1 = $2 >> $3 (arithmetic), variable shift amt memory load word lw $1, 1000($2) $1 = memory [$2+1000] store word sw $1, 1000($2) memory [$2+1000] = $ load byte lb $1, 1002($2) $1 = memory[$2+1002] in least sig. byte load byte unsigned lbu $1, 1002($2) $1 = memory[$2+1002] in least sig. byte store byte sb $1, 1002($2) memory[$2+1002] = $1 (byte modified only) branch branch if equal beq $1,$2,100 if ($1 = $2), PC = PC + 4 + (1004) branch if not equal bne $1,$2,100 (^) if ($1 ≠ $2), PC = PC + 4 + (1004) jump jump j 10000 PC = 10000* jump register jr $31 PC = $ jump and link jal 10000 $31 = PC + 4; PC = 10000*