CS 201 Winter 2014 (Karavanic) Final Exam Your Name: (1 point) Instructions: - Be sure to write your name on the first sheet. - All answers, and all work submitted in support of answers, should be written on this exam paper. - You may not use lecture notes, books, or any other written or electronic material for this exam, EXCEPT for one 8.5 x 11 inch sheet of paper full of notes. You may not use a calculator or any other electronic tools. - Do not begin the test until instructed to do so. Stop working on the test when instructed to do so. You will have 2 hours to complete the test. - Work individually. Do not discuss the test with anyone, do not copy answers from other students, and do not allow others to copy your work. - In parts B and C, if you arrive at an incorrect answer, showing your work may result in partial credit. Good luck!
Part A. Multiple Choice (3 points each; 30 points total) Circle the one best choice for each of these 10 questions. 1. How does x86 assembly store the return value when a function is finished? (a) The ret instruction stores it in a special retval register. (b) By convention, it is always in %eax. (c) It is stored on the stack just above the (%ebp) of the callee. (d) It is stored on the stack just above all the arguments to the function. 2. In IEEE floating point, what would be an effect of allocating more bits to the exponent part by taking them from the fraction part? (a) You could represent fewer numbers, but they could be much larger. (b) You could represent the same numbers, but with more decimal places. (c) You could represent both larger and smaller numbers, but with less precision. (d) Some previously representable numbers would now round to infinity 3. Consider the following two blocks of code, found in separate files: /* main.c */ int i=0; int main() { foo(); exit(0); /* foo.c */ int i = 1; void foo() { printf( %d, i); What will happen when you attempt to compile, link, and run this code? (a) It will fail to compile. (b) It will fail to link. (c) It will raise a segmentation fault. (d) It will print 0. (e) It will print 1. (f) It will sometimes print 0 and sometimes print 1.
4. Consider the following C functions and assembly code: int fun7(int a) { return a * 30; int fun8(int a) { return a * 34; int fun9(int a) { return a * 18; pushl %ebp movl %esp,%ebp movl 8(%ebp),%eax sall $4,%eax addl 8(%ebp),%eax addl %eax,%eax movl %ebp,%esp popl %ebp ret Which of the functions compiled into the assembly code shown? (a) fun7 (b) fun8 (c) fun9 (d) None 5. To delete symbol table information from an object file I should: (a) compile with the - g option (b) use GPROF (c) use STRIP (d) use LDD 6. Which of these uses of caching is not crucial to program performance? (a) Caching portions of physical memory (b) Caching virtual address translations (c) Caching virtual addresses (d) Caching virtual memory pages (e) None of the above (that is, they are all crucial)
7. Suppose a local variable int myint is declared in a function named func. Which of the following is considered safe in C? (a) func returns &myint and the caller dereferences the returned pointer. (b) func returns &myint and the caller prints the returned pointer to the screen (c) func sets the value of a global variable to &myint and returns. The global variable is unchanged up to the point another function dereferences the global variable. (d) None of the above 8. I can get a listing of all of the system calls made by a program using: (a) STRACE (b) PS (c) TOP (d) OBJDUMP 9. Within a loop, my code accesses the same loop index variable repeatedly. This is an example of: (a) temporal locality (b) spatial locality (c) recursion (d) onomatopoeia 10. Before executing a function's instructions we need to: (a) add function arguments to the stack (b) create a new stack frame (c) save register values (d) all of the above
Part B. Short answers (5 points each; 25 points total) 1. The smallest, fastest memory at the top of the memory hierarchy is the: 2. A #define in your C program will be handled by the: 3. Suppose you are given the task of improving the performance of a program consisting of 3 parts. Part A requires 20% of the overall run time, part B requires 30%, and part C requires 50%. You determine that for $1000 you could either speed up part B by a factor of 3.0 or part C by a factor of 1.5. Which choice would maximize performance? Circle one: B C 4. What call is used by a debugger to halt a running program and change some of its current variable values?
5. Consider the C program below. (For space reasons, we are not checking error return codes, so assume that all functions return normally.) main() { if (fork() == 0) { if (fork() == 0) { printf("3"); else { pid_t pid; int status; if ((pid = wait(&status)) > 0) { printf("4"); else { if (fork() == 0) { printf("1"); exit(0); printf("2"); printf("0"); return 0; Out of the 5 outputs listed below, circle only the valid outputs of this program. Assume that all processes run to normal completion. A. 2030401 B. 1234000 C. 2300140 D. 2034012 E. 3200410
Part C. Longer Answers C.1. (2 pts each; 12 points total) The following table gives the parameters for a number of different caches, where is the number of physical address bits, is the cache size (number of data bytes), is the block size in bytes, and is the number of lines per set. For each cache, determine the number of cache sets (S), tag bits (t), set index bits (s), and block offset bits (b). Cache m C B E S t s b 1. 32 1024 4 4 2. 32 1024 4 256 3. 32 1024 8 1 4. 32 1024 8 128 5. 32 1024 32 1 6. 32 1024 32 4
C.2. (8 points): Consider the source code below, where M and N are constants declared with #define. int array1[m][n]; int array2[n][m]; int copy(int i, int j) { array1[i][j] = array2[j][i]; Suppose the above code generates the following assembly code: copy: pushl %ebp movl %esp,%ebp pushl %ebx movl 8(%ebp),%ecx movl 12(%ebp),%ebx leal (%ecx,%ecx,8),%edx sall $2,%edx movl %ebx,%eax sall $4,%eax subl %ebx,%eax sall $2,%eax movl array2(%eax,%ecx,4),%eax movl %eax,array1(%edx,%ebx,4) popl %ebx movl %ebp,%esp popl %ebp ret What are the values of M and N? M= N=
C.3. (2 points each; 8 points) The heart of the game SimAquarium is a tight loop that calculates the average position of 256 algae. You are evaluating its cache performance on a machine with a 1024-byte direct-mapped data cache with 16-byte blocks (B = 16). You are given the following definitions: struct algae-position { int x; int y; ; struct algae_position grid[16][16]; int total_x = 0, total_y = 0; int i, j; Assume the following: sizeof(int) == 4. grid begins at memory address 0 The cache is initially empty The only memory accesses are to the entries of the array grid. Variables i, j, total_x and total_y are stored in registers. Your job is to determine the cache performance for the following code: for (i = 0; i < 16; i++ ) { for (j = 0; j < 16; j++) { total_x += grid[j][i].x; total_y += grid[j][i].y; A. What is the total number of reads? B. What is the total number of reads that miss in the cache? C. What is the miss rate? D. What would the miss rate be if the cache were twice as big?
C.4. (2 points each; 16 points total): Dr. Evil has returned! He has placed a binary bomb in this exam! Once again, Dr. Evil has made the disastrous mistake of leaving behind some of his source code. Can you save all of mankind (or at least your grade on this question), and tell us what this bomb does? The C source code Dr. Evil forgot to erase: /* bomb.c: Use new computer technology to blow up exams! -- Dr. Evil */ #include <stdio.h> #include <stdlib.h> extern long secret_unsolvable_puzzle_fn(long input); void explode_bomb() { printf("you fail! Mwhahahahaha!!!\n"); exit(8); int main(int argc,char *argv[]) { if(argc!= 2) { printf("usage: %s <magic password>\n", argv[0]); explode_bomb(); if(secret_unsolvable_puzzle_fn(atol(argv[1])) == 0) explode_bomb(); printf("curses, foiled again! You get paid one MEEELLION DOLLARS!\n"); return 0; The IA32 disassembly for the stuff Dr. Evil did erase: secret_unsolvable_puzzle_fn: 80485c0: 55 pushl %ebp 80485c1: 89 e5 movl %esp,%ebp 80485c3: 8b 45 08 movl 0x8(%ebp),%eax 80485c6: 85 c0 testl %eax,%eax 80485c8: 74 1d je 80485e7 80485ca: 8d 14 00 leal (%eax,%eax,1),%edx 80485cd: 81 fa 42 53 00 cmpl $21314,%edx 80485d2: 00 80485d3: 77 0a ja 80485df 80485d5: 01 c2 addl %eax,%edx 80485d7: 81 fa 42 53 00 cmpl $21314,%edx 80485dc: 00 80485dd: 76 f6 jbe 80485d5 80485df: 81 fa 43 53 00 cmpl $21315,%edx 80485e4: 00 80485e5: 74 02 je 80485e9 80485e7: 31 c0 xorl %eax,%eax 80485e9: 89 ec movl %ebp,%esp 80485eb: 5d popl %ebp 80485ec: c3 ret
A. Does the function secret_unsolvable_puzzle_fn() contain any of the following (circle either yes or no): loops: yes no if statements: yes no function calls: yes no recursion: yes no B. For each of the following input values, circle whether it defuses or explodes the bomb: input = 0: defuses explodes input = 1: defuses explodes input = 7105: defuses explodes input = 10657: defuses explodes