




Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Main points of this past exam are: Statement, Multiply a Number, Branche, Multiplied, Executed, Simple Operation, Twenty Words
Typology: Exams
1 / 8
This page cannot be seen from the preview
Don't miss anything!





Part A (8 points). The C statement to multiply a number by 10 can be done in the LC-2 using only 5 instructions, none of which are branches. Assuming that the number to be multiplied is in R1, write a sequence of 5 LC-2 instructions or less that will compute R1*10 and store the result into R0. Do not use any branches. Part B (5 points). Consider the C statement, printf(“The value of x is %d”, x);. Say the value of x is 6372 when this statement is executed. If this code is run on an LC-2, how many calls are made to TRAP x21? Part C (7 points). The following function performs a simple operation. Describe it in twenty words or less. int PropertyX(int number) { int x = 0; while (number > 0) { x = x + (number % 10); number = number / 10; } return x; }
Part A (10 points): What shape is printed out by the following function? Be specific. void PrintShape(int height) { int i, j; for (i = 1; i <= height; i++) { printf(“”); for (j = 1; j <= (height – 2); j++) { if ( (i == 1) || (i == height) ) printf(“”); else printf(“ ”); } printf(“\n”); } } *****Part B (5 points):* The following function takes an integer height input and prints the letter ‘V’ using the asterisk character (‘’). Fill in the missing code. Three example outputs are shown below. Assume that height > 1 and that the leftmost ‘’ appears in the leftmost column on your monitor. PrintV(2); PrintV(3); PrintV(4);
void PrintV(int height) { int i, j; for(i = 0; i< height; i++) { for(j = height; j > (height - i); j--) printf(" "); printf(""); for(j = 0; j < ____________________ ; j++) printf(" "); if (i < __________ ) printf(""); printf("\n"); }
Part C (5 points): The function below takes an integer height input and prints the letter ‘W’ using the ‘*’ character. The code provided is taken from the PrintV function of Part B (note that the last printf(“\n”) statement was not included). You must finish the function using compilable C code. There is no need to fill in the blank lines as they are the same as from Part B. Example outputs for PrintW are provided below. You may assume that height > 1 and that the output is left-justified. PrintW(2); PrintW(3); PrintW(4);
void PrintW(int height) { int i, j; for (i=0; i < height; i++) { for(j = height; j > (height - i); j--) printf(" "); printf(""); for(j = 0; j < ___________________ ; j++) printf(" "); if (i < _________) printf(""); /* Your code begins here; do not change anything above this line.*/ } }
Part A (15 points). The following C code implements a basic logic component that we’ve discussed in class. Name the component and be sure to explain its inputs and outputs. Assume ‘a’ can only take on the values 0, 1, 2, or 3. int UnknownComponent(int a) { if (a == 0) { return 0x01; } else if (a == 1) { return 0x02; } else if (a == 2) { return 0x04; } else if (a == 3) { return 0x08; } } Part B (5 points). Now, write C code to implement the logic diagram below. You must write and use all of the functions below. Additionally, complete main() using the functions you wrote. e f e AND f g m n m[15:8] @ n[7:0] o a b (^) c d output a[0] c[0] g[0] Mux21 (^) Mux Mux BitwiseAnd Join
int Mux21( int x, int y, int select ) { } int BitwiseAnd( int e, int f ) { } int Join( int m, int n ) { } int main() { int a, b, c, d, output; scanf( “%d %d %d %d”, &a, &b, &c, &d ); printf( “Result: %d\n”, output ); return 0; }