Download ECE 190 Exam 3 - Problem Solutions and more Exams Introduction to Aerospace Engineering in PDF only on Docsity!
ECE 190 - Exam 3
Monday, April 16, 2012
Name: NetID: Discussion Section (Friday Lab Section): (circle one) AD1 AD2 AD3 AD4 AD5 AD6 AD7 AD8 AD9 ADA
- Be sure that your exam booklet has 8 pages.
- This is a closed book exam.
- You are allowed two 8.5x11-inch sheets of handwritten notes.
- Absolutely no interaction between students is allowed.
- Do your best!
- Appendix A and Appendix D from the textbook are provided for you on the desktop.
- Write your code in the files indicated by the problem statements. Do not rename the files. If you create new files, the contents of those files will not be graded.
- Hint files are available for some problems. The hints are intended to help you if you get stuck and don’t know how to start a problem. If you choose to view the hint file for a problem, the maximum amount of points you can earn on that problem will be reduced. The point reduction is 7 points on a 20 - point problem (i.e., a perfect answer would earn 13 out of 20 points).
- We are NOT using a subversion repository for the exam. We will grade the saved files that you leave in your student directory.
Problem 1: C to LC-3 Conversion ( 20 points) Convert the C function below to an LC-3 assembly language subroutine. While converting from C to LC-3, use the run-time stack and activation record implementation described in the textbook (R5 is the frame pointer, R6 is the stack pointer, etc). You may assume that the input values for parameters alpha and beta are positive integers (greater than zero). You may also assume that the passed parameters are already saved in the activation record on the run-time stack (this was done by the calling program). int greek(int alpha, int beta) { int gamma, delta; gamma = 3; delta = alpha * beta + gamma; return delta; } Files in your problem1 directory:
- main.asm – This is a main program that allows you to test your subroutine. THIS FILE SHOULD NOT BE EDITED with the exception of the lines (specified in the file) where you can change the values of parameters alpha and beta.
- greek.asm – This is where you will write the LC- 3 subroutine that corresponds to the C function above. The subroutine must start at memory address x3000. R5 holds the frame pointer. R6 holds the stack pointer. You can assume that the function arguments have already been pushed onto the run-time stack, according to the C to LC-3 convention described in the textbook. The return value must be saved at the appropriate place on the run-time stack. Your code will be graded by an autograder for the functionality of the subroutine as well as the correct construction of the activation record in the run-time stack. Consequently, you MAY NOT perform any optimizations that impact the contents of the stack. For example, you MUST save R7, even when the function does not make a subroutine call. You may receive zero points if your code does not assemble or behave as specified. Helpful Reminder: ALL local variables used in C should be stored onto the run-time stack appropriately. The local variables may NOT be stored locally in memory reserved by .FILL, .BLKW, etc. HINT OPTION: The point reduction is 7 points if you view the hint for this problem. To view the hint, type ./hint from your problem 1 directory. This hint includes the format of the activation records on the runtime stack.
Problem 3: Image Processing (20 points) Given two image files (Image1.ppm and Image2.ppm), write the C code to generate a new image file (alternate.ppm) such that the new image has alternating horizontal bands from Image1 and Image2. For example, if Image1 is entirely red and Image2 is entirely blue, then the final image will have horizontal strips of red and blue, with each strip is one pixel wide. Input1 Input2 Output The maximum size of an image will not exceed 200 by 200 pixels. The two images will be the same size. Files in your problem 3 directory:
- alternate_bands.c - You will implement the C function alternate in this file.
- Image1.ppm – image file for test input.
- Image2.ppm – image file for test input. In the code provided for you, please note the usage of the following variables:
- image1: three-dimensional array holding the first image as read from file
- image2: three-dimensional array holding the second image as read from file
- x: x-dimension of the image
- y: y-dimension of the image
- max: maximum pixel value as read from file
- alternate.ppm : name of the new image file Do not change the names of the variables provided for you. Your code will be graded by an autograder for functionality. You may receive zero points if your code does not compile or behave as specified. HINT OPTION: The point reduction is 7 points if you view the hint for this problem. To view the hint, type ./hint from your problem 3 directory.
Problem 4: Sting Manipulation (20 points) Implement a C function to perform basic string compression using the counts of repeated characters. For example, the string “aabccccaaa” would become “a2b1c5a3”. If the “compressed string” would not become smaller than the original string, your method should return the original string. File in your problem 4 directory:
- compression.c - You will implement the C function compress in this file. Do not change the names of the variables provided for you. The prototype for compress is char *compress(char *str, char *buf) where str is the string to be compressed, and buf is a buffer that has already been allocated for you and is of size sufficient to hold the compressed string. (For testing purposes, note that the compressed version of string is at most twice the length of the original string). Your code will be graded by an autograder for functionality. You may receive zero points if your code does not compile or behave as specified. HINT OPTION: The point reduction is 7 points if you view the hint for this problem. To view the hint, type ./hint from your problem 4 directory.
Problem 5, continued Example You do not need to implement the queue in this manner; however, this example is provided for your contemplation. The queue is initially empty. isEmpty() returns 1; isFull() returns 0 Head Tail After calling enqueue(10): 10 Head Tail After calling enqueue(20): 10 20 Head Tail After calling enqueue(30): 10 20 30 Head Tail dequeue(&x) sets x to 10 and returns 1 20 30 Head Tail dequeue(&x) sets x to 2 0 and returns 1 30 Head Tail dequeue(&x) sets x to 30 and returns 1 Head Tail dequeue(&x) returns 0 Head Tail Free Hints Consider what happens when you enqueue 2 elements and dequeue 1 and repeat 50 times. At some point, if you don't copy, your tail pointer will leave the end of the array. There are three ways to deal with this: (1) make a circular array (harder), or (2) make the array bigger than it needs to be, and occasionally copy the array on dequeue (faster but takes some thought), or (3) always copy the array on dequeue (slower but easier and still worth full credit).
Reference information LC-3 assembly language problems:
- To begin work on problem X, use the cd command to get to the problemX directory.
- To edit the file, type gedit givenfilename.asm & (Replace “ givenfilename ” with the name of the file where you will add your code.)
- Save your work.
- To assemble your code and produce the object file, type lc3as givenfilename.asm
- To run the LC-3 graphical simulator, type lc3sim-tk givenfilename.obj OR To run the LC-3 command-line simulator, type lc3sim givenfilename.obj C language problems:
- To begin work on problem X, use the cd command to get to the problemX directory.
- To edit the file, type gedit givenfilename.c &
- Save your work.
- To compile your code, type ./compile (this script uses the clang compiler with the C standard). You must compile your code in this way.
- To run your code, type ./givenfilename