Andrew login ID:Full Name:Recitation Section:CS 15-213, Spring 2009Exam 1Tues., February 24, 2009Instructions:• Make sure that your exam is not missing any sheets, then write your full name, Andrew login ID, andrecitation section (A–J) on the front.• Write your answers in the space provided for the problem. If you make a mess, clearly indicate yourfinal answer.• The exam has a maximum score of 100 points.• The problems are of varying difficulty. The point value of each problem is indicated. Pile up the easypoints quickly and then come back to the harder problems.• This exam is OPEN BOOK. You may use any books or notes you like. No calculators or otherelectronic devices are allowed.• Good luck!1 (16):2 (22):3 (13):4 (13):5 (22):6 (14):TOTAL (100):Page 1 of 17Problem 1. (16 points):Consider a new floating point format that follows the IEEE spec you should be familiar, except with 3exponent bits and 2 fraction bits (and 1 sign bit). Fill in all blank cells in the table below. If, in the processof converting a decimal number to a float, you have to round, write the rounded value next to the originaldecimal as well.Description Decimal Binary RepresentationBias -----Smallest positive numberLowest finiteSmallest positive normalized----- −616-----64----- 1 010 10----- 11Page 2 of 17Problem 2. (22 points):Consider the C code written below and compiled on a 32-bit Linux system using GCC.struct s1{int y;short x;};struct s2{struct s1 a;struct s1*b;int x;char c;int y;char e[5];int z;};short fun1(struct s2*s){return s->a.x;}void*fun2(struct s2*s){return &s->z;}int fun3(struct s2*s){return s->z;}short fun4(struct s2*s){return s->b->x;}Page 3 of 17a) What is the size of struct s2?b) How many bytes are wasted for padding?You may use the rest of the space on this page for scratch space to help with the rest of this problem.Nothing written below this line will be graded.Page 4 of 17c) Which of the following correspond to functions fun1, fun2, fun3, and fun4?push %ebpmov %esp,%ebpmov 0x8(%ebp),%eaxmovswl 0x4(%eax),%eaxpop %ebpretANSWER: ________push %ebpmov %esp,%ebpmov 0x8(%ebp),%eaxmov 0x8(%eax),%eaxmovswl 0x4(%eax),%eaxpop %ebpretANSWER: ________push %ebpmov %esp,%ebpmov 0x8(%ebp),%eaxmov 0x20(%eax),%eaxpop %ebpretANSWER: ________push %ebpmov %esp,%ebpmov 0x8(%ebp),%eaxadd $0x20,%eaxpop %ebpretANSWER: ________Page 5 of 17d) Assume a variable is declared as struct s2 myS2; and the storage for this variable begins at ad-dress 0xbfbdc300.(gdb) x/20w &myS20xbfbdc300: 0x000000d5 0x0000000f 0xbfbdc338 0x000000000xbfbdc310: 0x000000ff 0x0000012c 0x01020501 0xb7f0a6030xbfbdc320: 0x0000000c 0x080496a0 0xbfbdc338 0x0804828d0xbfbdc330: 0xb7ed9fd5 0xb7fc1ff4 0x000000f3 0x080400120xbfbdc340: 0xb7eda0b9 0xb7fc1ff4 0xbfbdc3a8 0xb7ec6dceFill in all the blanks below.HINTS: Label the fields. Not all 20 words are used. Remember endianness!What would be returned by:fun1(&myS2) = 0x___________fun2(&myS2) = 0x___________fun3(&myS2) = 0x___________fun4(&myS2) = 0x___________What is the value of:myS2.b->y = 0x___________myS2.a.y = 0x___________myS2.z = 0x___________myS2.e[1] = 0x___________Page 6 of 17Problem 3. (13 points):This problem concerns assembly code generated by GCC for a function containing a switch statement onan x86-64 machine.Below is the entire assembly dump of the function selector, whose C skeleton is given subsequently.0000000000400470 <selector>:400470: 8d 46 01 lea 0x1(%rsi),%eax400473: 83 f8 06 cmp $0x6,%eax400476: 77 1a ja 400492 <selector+0x22>400478: 89 c0 mov %eax,%eax40047a: ff 24 c5 a0 05 40 00 jmpq*0x4005a0(,%rax,8)400481: 48 83 c7 04 add $0x4,%rdi400485: eb 0e jmp 400495 <selector+0x25>400487: 8b 37 mov (%rdi),%esi400489: eb 0a jmp 400495 <selector+0x25>40048b: d1 fe sar %esi40048d: 83 c6 05 add $0x5,%esi400490: eb 03 jmp 400495 <selector+0x25>400492: 8d 34 f6 lea (%rsi,%rsi,8),%esi400495: 48 63 c6 movslq %esi,%rax400498: 8b 04 87 mov (%rdi,%rax,4),%eax40049b: c3 retqThe code at line 40047a uses an indirect jump to index into a jump table:40047a: ff 24 c5 a0 05 40 00 jmpq*0x4005a0(,%rax,8)In GDB, we examine the memory dump at address 0x4005a0 which produces the following output:(gdb) x /8g 0x4005a00x4005a0: 0x0000000000400487 0x00000000004004810x4005b0: 0x0000000000400492 0x000000000040048d0x4005c0: 0x000000000040048b 0x00000000004004920x4005d0: 0x0000000000400481 0x0000002c3b031b01Page 7 of 17Fill in the blank portions of C code below to reproduce the function corresponding to this object code.int selector (int*x, int y) {switch (y) {case ____:case ____:____________;break;case ____:y = ________;break;case ____:y = ________;case ____:y = ________;break;default:y = ________;}return ________;}Page 8 of 17Problem 4. (13 points):The function below is hand-written assembly code for a sorting algorithm. Fill in the blanks on the nextpage by converting this assembly to C code..globl mystery_sort # exports the symbol so other .c files# can call the functionmystery_sort:dec %rsixor %rdx, %rdxinc %rdxjmp loop1_checkloop1:xor %rdx, %rdxmov %rsi, %rcxjmp loop2_checkloop2:dec %rcxmov 8(%rdi, %rcx, 8), %r8mov (%rdi, %rcx, 8), %r9cmp %r8, %r9jle loop2_checkmov %r8, (%rdi, %rcx, 8)mov %r9, 8(%rdi, %rcx, 8)inc %rdxloop2_check:test %rcx, %rcxjnz loop2loop1_check:test %rdx, %rdxjnz loop1retPage 9 of 17void mystery_sort (long*array, long len){long a, b, tmp;do{a = _____;for (b = _____; b > _____; b--){if (array[_____] > array{_____]){tmp = array[_____];array[_____] = array[_____];array[_____] = tmp;a++;}}} while (a > _____);}Page 10 of 17Problem 5. (22 points):Circle the correct answer.1. What register is the return value from a function stored in? (Assume 32 bit return value)(a) eip(b) ebp(c) eax(d) esp2. The pushl instruction does what to the stack pointer?(a) decrements the stack pointer by 4 bytes(b) decrements the stack pointer by 1 byte(c) increments the stack pointer by 4 bytes(d) increments the stack pointer by 1 byte3. What does the test instruction do before setting condition flags?(a) bitwise and(b) subtraction(c) bitwise xor(d) bitwise bang4. On the x8664 fish machines, what is the size of an int?(a) 1 byte(b) 32 bytes(c) 4 bytes(d) 8 bytes5. Which of the following represents the order of the registers that store the first four arguments to afunction in x8664?(a) rdi, rsi, rcx, rdx(b) rax, rbx, rsi, rdi(c) rsi, rdi, rdx, rbx(d) rdi, rsi, rdx, rcxPage 11 of 176. The ˜ operator performs what operation on a value?(a) bitwise complement(b) logical complement(c) reverses the order of
View Full Document