programming_exam3_practice

# programming_exam3_practice - 1 ECE 190 Midterm Exam 3...

This preview shows pages 1–4. Sign up to view the full content.

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the 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): Warm-up. In this assignment, you are asked to implement a function to compute the dot product between two n-dimensional 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 LC-3 assembly conversion. In this assignment, you are asked to rewrite a function for computing factorial written in C into LC-3 assembly language. int factorial(int n) { int fn; if (n > 1) fn = n * factorial(n-1); else fn = 1; return fn; } The function call should be implemented using the run-time stack. Assume caller’s save R0-R3. (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 LC-3 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 run-time stack ; allocate space in the run-time stack for return value __________________ ; one or more LC-3 instructions ; store caller’s return address and frame pointer __________________ ; one or more LC-3 instructions ; allocate memory for local variable fn __________________ ; one or more LC-3 instructions ; if (n>1) LDR R1, R5, #4 ADD R2, R1, #-1 BRnz ELSE ; compute fn = n * factorial(n-1) ; caller-built stack for factorial(n-1) function call ; push n-1 onto run-time stack __________________ ; one or more LC-3 instructions 3 ; call factorial subroutine JSR FACTORIAL ; pop return value from run-time stack (to R0) __________________ ; one or more LC-3 instructions ; pop function argument from the run-time stack __________________ ; one or more LC-3 instructions ; multiply n by the return value (already in R0) LDR R1, R5, #4 MUL R0, R0, R1 ; R0 <- n * factorial(n-1)...
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.

### Page1 / 7

programming_exam3_practice - 1 ECE 190 Midterm Exam 3...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online