programming_exam3_practice - 1 Spring 2011 Practice Exam No...

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

View Full Document Right Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 1 Spring 2011 Practice Exam No t e s : This exam will consist entirely of five programming problems. Each problem will be graded separately. We will provide skeleton codes for each problem. Probl e m 1 (5 point s ): 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); Exampl e 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 Probl e m 2 (20 point s ): C to L C-3 a ss e mbly c onv e r s ion . 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. $VVXPH FDOOHUV VDYH 5-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 FDOOHUV IUDPH SRLQWHU FDOOHUV UHWXUQ DGGUHVV 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: SXVK FDOOHHV ERRNNHHSLQJ LQIR RQWR WKH UXQ-time stack ; allocate space in the run-time stack for return value __________________ ; one or more LC-3 instructions VWRUH FDOOHUV UHWXUQ DGGUHVV DQG IUDPH SRLQWHU __________________ ; 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

Page1 / 7

programming_exam3_practice - 1 Spring 2011 Practice Exam No...

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

View Full Document Right Arrow Icon
Ask a homework question - tutors are online