DOC PREVIEW
UCLA COMSCI 33 - CS33-midterm-2011_v1

This preview shows page 1-2-3-4-5 out of 14 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 14 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 14 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 14 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 14 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 14 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 14 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

UNIVERSITY OF CALIFORNIA, LOS ANGELES UCLA CS 33 Exam #1 The first page of the exam is an answer key – all answers must be here for grading purposes. Show your work on the remaining pages of the exam, use the backs of pages if needed. An ASCII table is on the last page if you need it. This is an open book, open notes quiz – but you cannot share books or notes. I will follow the guidelines of the university in reporting academic misconduct – please do not cheat. NAME: ________________________________________________________________ ID: ____________________________________________________________________ Problem 1: ___________ Problem 2: ___________ Problem 3: ___________ Problem 4: ___________ Problem 5: ___________ Problem 6: ___________ Problem 7: ___________ Total: ________________ ∞ BERKELEY • DAVIS • IRVINE • LOS ANGELES • RIVERSIDE • SAN DIEGO • SAN FRANCISCO SANTA BARBARA • SANTA CRUZ1. Y N Y N Y N Y N Y N 2. a. b. c. d. e. f. 3. a. b. c. d. ∞ 4. a. b. 5. a. b. 6. Starting address for the table: ___________ 7. Answer Sheet11. Lost at C? (15 points): The following problem assumes the following declarations: int x = random(); // assume random() returns a random 32-bit value int y = random(); float f = random(); // assume f is not NaN float g = random(); // assume g is not NaN unsigned ux = (unsigned) x; For the following C expressions, circle either Y or N (but not both). If you circle the right answer, you get +2 points. If you circle the wrong answer, you get -1 point. If you do not circle anything, you get 0 points. So do not just guess wildly. Always True? a. f+(g+f) == g+2*f Y N b. ((float) ((int) f <<2) >>2) == f Y N c. (int)((float) x + f) == ((int) f + x) Y N d. ((x>>30)&1) && (x≥0) ⇒⇒⇒⇒ (x+x) < 0 Y N e. (int) ux < 0 ⇒⇒⇒⇒ ux > x Y N Note that “⇒⇒⇒⇒” represents an implication. A ⇒⇒⇒⇒ B means that you assume A is true, and your answer should indicate whether B should be implied by A – i.e. given that A is true, is B always true?2 2. Give Me Strength (12 points): Consider the following sequence of operations on x (defined as int x): x = a%8; if (x%2 == 0) y = 1; else if (x==5 || x==7) y=2; else y=3; We will use strength reduction to use a less expensive operator in place of the modulo (%) operator here. Fill in the blanks in the following code so that it is equivalent to the above code: x = a _ _; if (!(x_ _)) y = 1; else if (x _ _) y=2; else y=3; Each of the 6 blanks above (labeled a-f) will hold a single character (operator or numeric value). Write your answers on the answer key at the appropriate labels. a b c d e f3 3. Bytes So Bad it Hertz (12 points): Consider the code fragment below: union { int i; short x; unsigned short u; float f; } testout; testout.i=0xC0208000; This will be executed on an IA32 machine. What would be printed for each of the following statements (please answer in decimal): a. printf("%d", testout.x); b. printf("%u", testout.u); c. printf("%.1f", testout.f); (note that the %.1f formatting means the answer should only have one digit of precision after the . – i.e. 4.0 or 2.3) How many bytes would testout occupy in memory?: d. # of bytes: ______________4 4. Complete Dis-Array (12 points): Consider the following array reference: array5[i][j] On an IA32 machine, the following code fragment was generated for this reference: 804863b: 8b 45 e4 mov 0xffffffe4(%ebp),%eax 804863e: c1 e0 02 shl $0x2,%eax 8048641: 03 45 e0 add 0xffffffe0(%ebp),%eax 8048644: 8b 10 mov (%eax),%edx 8048646: 8b 45 e8 mov 0xffffffe8(%ebp),%eax 8048649: c1 e0 02 shl $0x2,%eax 804864c: 8d 04 02 lea (%edx,%eax,1),%eax 804864f: 8b 00 mov (%eax),%eax The first mov instruction moves the value of i into %eax. The add instruction adds the base address of array5 to %eax. The third mov instruction moves the value of j into %eax. Based on this code fragment, and on your understanding of how the compiler optimizes different types of arrays, answer the following questions: a. Is this a nested array (different row vectors are always contiguously allocated in memory) or a multilevel array (different row vectors have no guarantees of contiguous allocation)? (answer nested or multilevel) b. How big is each element of the array? (answer in bytes)5 5. I Cannot Function in this Environment (15 points): The following function small() is a part of a program compiled on an IA32 architecture: 0804840d <small>: 804840d: 55 push %ebp 804840e: 89 e5 mov %esp,%ebp 8048410: 53 push %ebx 8048411: 83 ec 14 sub $0x14,%esp 8048414: 8b 45 10 mov 0x10(%ebp),%eax 8048417: 89 44 24 08 mov %eax,0x8(%esp) 804841b: 8b 45 0c mov 0xc(%ebp),%eax 804841e: 89 44 24 04 mov %eax,0x4(%esp) 8048422: 8b 45 08 mov 0x8(%ebp),%eax 8048425: 89 04 24 mov %eax,(%esp) 8048428: e8 24 00 00 00 call 8048451 <bar> 804842d: 89 c3 mov %eax,%ebx 804842f: 8b 45 08 mov 0x8(%ebp),%eax 8048432: 89 44 24 08 mov %eax,0x8(%esp) 8048436: 8b 45 0c mov 0xc(%ebp),%eax 8048439: 89 44 24 04 mov %eax,0x4(%esp) 804843d: 8b 45 10 mov 0x10(%ebp),%eax 8048440: 89 04 24 mov %eax,(%esp) 8048443: e8 09 00 00 00 call 8048451 <bar> 8048448: 8d 04 03 lea (%ebx,%eax,1),%eax 804844b: 83 c4 14 add $0x14,%esp 804844e: 5b pop %ebx 804844f: 5d pop %ebp 8048450: c3 ret Using gdb, we set a breakpoint and run the program: (gdb) break *0x8048428 Breakpoint 1 at 0x8048428: file func.c, line 17. (gdb) run Starting program Breakpoint 1,


View Full Document

UCLA COMSCI 33 - CS33-midterm-2011_v1

Download CS33-midterm-2011_v1
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view CS33-midterm-2011_v1 and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view CS33-midterm-2011_v1 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?