This preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
Unformatted text preview: 1 ECE 190 Midterm Exam 3 Spring 2011 Practice Exam Notes: This exam will consist entirely of five programming problems. Each problem will be graded separately. We will provide skeleton codes for each problem. Problem 1 (5 points): Warmup. In this assignment, you are asked to implement a function to compute the dot product between two ndimensional vectors. The function accepts two vectors, vec1 and vec2 , and number of dimensions, num_dims , and returns the dot product: float dot_product(float vec1, float vec2, int num_dims); Example run: float vec1 = { 1, 2, 3, 4, 5 }; float vec2 = { 5, 4, 3, 2, 1 }; float dp = dot_product(vec1, vec2, 5); After this code executes, dp should be set to 35. 2 Problem 2 (20 points): C to LC3 assembly conversion. In this assignment, you are asked to rewrite a function for computing factorial written in C into LC3 assembly language. int factorial(int n) { int fn; if (n > 1) fn = n * factorial(n1); else fn = 1; return fn; } The function call should be implemented using the runtime stack. Assume caller’s save R0R3. (in other words, you do not need to save/restore them, the calling function with do this for you.) As a reminder, the factorial function's activation record is provided below: Fn caller’s frame pointer caller’s return address return value n To get you started, we provide a skeleton LC3 assembly code that should give you some structure and hints to what we expect you to write: FACTORIAL: ; push callee’s bookkeeping info onto the runtime stack ; allocate space in the runtime stack for return value __________________ ; one or more LC3 instructions ; store caller’s return address and frame pointer __________________ ; one or more LC3 instructions ; allocate memory for local variable fn __________________ ; one or more LC3 instructions ; if (n>1) LDR R1, R5, #4 ADD R2, R1, #1 BRnz ELSE ; compute fn = n * factorial(n1) ; callerbuilt stack for factorial(n1) function call ; push n1 onto runtime stack __________________ ; one or more LC3 instructions 3 ; call factorial subroutine JSR FACTORIAL ; pop return value from runtime stack (to R0) __________________ ; one or more LC3 instructions ; pop function argument from the runtime stack __________________ ; one or more LC3 instructions ; multiply n by the return value (already in R0) LDR R1, R5, #4 MUL R0, R0, R1 ; R0 < n * factorial(n1)...
View
Full
Document
This note was uploaded on 03/08/2012 for the course ECE 190 taught by Professor Hutchinson during the Spring '08 term at University of Illinois, Urbana Champaign.
 Spring '08
 Hutchinson

Click to edit the document details