Andrew login ID Full Name Recitation Section CS 15 213 Spring 2009 Exam 1 Tuesday February 24 2009 Instructions Make sure that your exam is not missing any sheets then write your full name Andrew login ID and recitation section A J on the front Write your answers in the space provided for the problem If you make a mess clearly indicate your final 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 easy points 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 other electronic devices are allowed Good luck 1 16 2 22 3 13 4 13 5 22 6 14 TOTAL 100 Page 1 of 17 Problem 1 16 points Consider a new floating point format that follows the IEEE spec you should be familiar except with 3 exponent bits and 2 fraction bits and 1 sign bit Fill in all blank cells in the table below If in the process of converting a decimal number to a float you have to round write the rounded value next to the original decimal as well Description Decimal Bias Binary Representation Smallest positive number Lowest finite Smallest positive normalized 7 16 5 4 1 010 01 13 Page 2 of 17 Problem 2 22 points Consider the C code written below and compiled on a 32 bit Linux system using GCC struct s1 short x int y struct s2 struct s1 a struct s1 b int x char c int y char e 3 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 17 a 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 17 c Which of the following correspond to functions fun1 fun2 fun3 and fun4 push mov mov add pop ret ebp esp ebp 0x8 ebp eax 0x1c eax ebp ANSWER push mov mov mov movswl pop ret ebp esp ebp 0x8 ebp eax 0x8 eax eax eax eax ebp ANSWER push mov mov mov pop ret ebp esp ebp 0x8 ebp eax 0x1c eax eax ebp ANSWER push mov mov movswl pop ret ebp esp ebp 0x8 ebp eax eax eax ebp ANSWER Page 5 of 17 d Assume a variable is declared as struct s2 myS2 and the storage for this variable begins at address 0xbfb2ffc0 gdb x 20w 0xbfb2ffc0 0xbfb2ffd0 0xbfb2ffe0 0xbfb2fff0 0xbfb30000 myS2 0x0000000f 0xb7f173ff 0xb7e2dfd5 0xb7e2e0b9 0x00000001 0x000000d5 0x0000012c 0xb7f15ff4 0xb7f15ff4 0xbfb30084 0xbfb2ffe8 0xbf030102 0xbfb30012 0xbfb30058 0xbfb3008c 0x00000000 0x0000000c 0x000000f3 0xb7e1adce 0xbfb30010 Fill 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 17 Problem 3 13 points Given the memory dump and disassembly from GDB on the next page fill in the C skeleton of the function switchfn int switchfn int a long b int y 0 x switch a b case 1 return 24 case 6 a return a case 0 return a b case 4 x a y b break case a y x case 3 b y x case 5 return a b return x y Page 7 of 17 There may be a few instructions you haven t seen before in this assembly dump data16 is functionally equivalent to nop setcc functions similarly to jcc except it will set its operand to 1 or 0 instead of jumping or not jumping respectively cqto is the 64 bit equivalent of cltd gdb x 7xg 0x4005c0 0x4005c0 IO stdin used 8 0x4005d0 IO stdin used 24 0x4005e0 IO stdin used 40 0x4005f0 IO stdin used 56 0x0000000000400476 0x000000000040047b 0x0000000000400480 0x0000000000400483 0x0000000000400487 0x000000000040048b 0x000000000040048d 0x0000000000400494 0x0000000000400499 0x000000000040049a 0x000000000040049d 0x000000000040049e 0x000000000040049f 0x00000000004004a0 0x00000000004004a1 0x00000000004004a4 0x00000000004004a5 0x00000000004004a7 0x00000000004004aa 0x00000000004004ac 0x00000000004004ae 0x00000000004004b1 0x00000000004004b4 0x00000000004004b6 0x00000000004004b9 0x00000000004004bc 0x00000000004004bf 0x00000000004004c1 0x00000000004004c4 0x00000000004004c5 0x00000000004004c7 0x00000000004004ca 0x00000000004004cd 0x00000000004004a1 0x00000000004004ac 0x00000000004004a5 0x000000000040049a switchfn 0 switchfn 5 switchfn 10 switchfn 13 switchfn 17 switchfn 21 switchfn 23 switchfn 30 switchfn 35 switchfn 36 switchfn 39 switchfn 40 switchfn 41 switchfn 42 switchfn 43 switchfn 46 switchfn 47 switchfn 49 switchfn 52 switchfn 54 switchfn 56 switchfn 59 switchfn 62 switchfn 64 switchfn 67 switchfn 70 switchfn 73 switchfn 75 switchfn 78 switchfn 79 switchfn 81 switchfn 84 switchfn 87 Page 8 of 17 mov mov movslq imul cmp ja jmpq mov retq lea data16 data16 nop retq lea retq mov imul jmp cmp sete movzbl cmp setl movzbl movslq cqto idiv retq cmp sete movzbl retq 0x0000000000400494 0x00000000004004b4 0x00000000004004bc 0x0 ecx 0xdeadbeef edx edi rax rsi rax 0x6 rax 0x4004c5 switchfn 79 0x4005c0 rax 8 0x18 eax rdx rsi 4 eax rdi rsi 1 eax edi edx esi ecx 0x4004c5 switchfn 79 edx ecx al al edi edx ecx al al esi edi rax rsi ecx edx al al eax Problem 4 13 points The function below is hand written assembly code for a sorting algorithm Fill in the blanks on the next page by converting this assembly to C code globl mystery sort mystery sort jmp exports the symbol so other c files can call the function loop1 check loop1 xor mov jmp rdx rdx rsi rcx loop2 check mov cmp jg mov rdi rcx 8 rax rax rdi rdx 8 loop2 check rcx rdx loop2 loop2 check dec test jnz dec mov mov mov mov loop1 check test jnz rcx rcx rcx loop2 rsi rdi rsi rdi rdx rcx rdi rax rdi 8 rax 8 rcx rsi 8 rdx 8 rsi rsi loop1 ret Page 9 of 17 void mystery sort long array long len long a b tmp while a for b b b if array array len tmp array array array array tmp Page 10 of 17 Problem 5 22 points Circle the correct answer 1 What sequence of operations does the leave instruction execute a mov ebp esp pop ebp b pop ebp mov ebp esp c pop esp mov ebp esp d push ebp mov esp ebp 2 Who is responsible for storing the return address of a function call a the caller b the callee c the kernel d the CPU 3 On what variable types does C perform logical right shifts a signed types b unsigned types c signed and unsigned types d C does not perform logical right shifts 4 What is the difference between the rbx and the ebx register on an x86 64 machine a nothing they are the same register b ebx refers to only the low order 32 bits of the rbx register
View Full Document