Computer Systems: A programmers perspective

February 10, 2017 | Author: Saurabh Kapadia | Category: N/A
Share Embed Donate


Short Description

Download Computer Systems: A programmers perspective...

Description

Exam 1 Solutions 15-213 / 18-213 Fall 2012 ********* Problem 1 ********* 1-a 2-c 3-d 4-c 5-a 6-b 7-c 8-(b or d) 9-c 10-d The correct answer for 8 was initially listed as d) temporal locality, but the correct answer is actually spatial locality. While it's true that blocking in things like matmult primarily exploits temporal locality, blocking is effective for transpose because it exploits spatial locality by effectively using the entries in each cache line; there is no reuse. ********* Problem 2 ********* Expression 4b decimal 4b binary 6b decimal 6b binary -----------------------------------------------------------------8 | -8 | 1000 | -8 | 11 1000 -TMin | -8 | 1000 | -32 | 10 0000 x >> 1 | -3 | 1101 | -3 | 11 1101 (-x ^ -1) >> 2 | -2 | 1110 | -2 | 11 1110 ---------------------------------------------------------------********* Problem 3 ********* | A | One | 0 011 00 | 1/2 | 0 010 00 | 11/8 | 0 011 10 |

B 0 01 000 0 00 100 0 01 011

Exact in both formats Exact in both formats, norm in A, denorm in B Format A round to even, format B exact

********* Problem 4 ********* unsigned transform(unsigned n) { int b, m; for(m = 0; n != 0; n >>= 1) { // (or) for(m = 0; n > 0; n = n/2) b = n & 1; // (or) b = n % 2; if(b == 0) { continue; } m = 2*m + 1; // (or) m = m + m + 1; (or) m = m 1; } }

return m;

********* Problem 5 ********* Part 1. a X X X X X X X b b b b b b b b c c c c d d d X e e e e e e e e f f f f f f f f Part 2. f f f f f f f f b b b b b b b b e e e e e e e e c c c c d d d a or a d d d c c c c b b b b b b b b e e e e e e e e f f f f f f f f ********* Problem 6 ********* A: phd B: bachelors C: masters ********* Problem 7 ********* int result = 4; switch(a){ case 0: case 1: c = c - 5; case 2: result = 4 * c; //or result *= c break; case 5: result = 86547; //or 0x15213 break; case 3:

}

c = 2; case 7: b = b & c; default: result += b; // or result = b + 4

return result; } ********* Problem 8 ********* Stack addresss

The diagram starts with the arguments for foo() +-----------------------------------+ 0xffffd850| 5 | +-----------------------------------+ 0xffffd84c| 4 | +-----------------------------------+ 0xffffd848| 3 | +-----------------------------------+ 0xffffd844| caller ra: 0x080483c9 | +-----------------------------------+ 0xffffd840| old ebp: ffffd858 | 1 or n / 2 ********* Problem 5 ********* Part A: +--------------------------------+ | unknown | +--------------------------------+ | 18 | +--------------------------------+ | 213 | +--------------------------------+ | unknown | +--------------------------------+ | unknown | +--------------------------------+ | unknown |

0xffff1008 0xffff1004 0xffff1000 0xffff0ffc 0xffff0ff8 0xffff0ff4

+--------------------------------+ | unknown | +--------------------------------+ | 15 | +--------------------------------+ | 18 | +--------------------------------+ | 0x080483b7 | +--------------------------------+ | 0xffff0ff8 | +--------------------------------+ | unknown | +--------------------------------+ | unknown | +--------------------------------+ | 3 | +--------------------------------+ | 15 | +--------------------------------+ | 0x080483b7 | +--------------------------------+ | 0xffff0fe0 | +--------------------------------+ | unknown | +--------------------------------+ | unknown | +--------------------------------+ | 0 | +--------------------------------+ | 3 | +--------------------------------+ | unknown | +--------------------------------+ | unknown | +--------------------------------+

0xffff0ff0 0xffff0fec 0xffff0fe8 0xffff0fe4 0xffff0fe0 0xffff0fdc 0xffff0fd8 0xffff0fd4 0xffff0fd0 0xffff0fcc 0xffff0fc8 0xffff0fc4 0xffff0fc0 0xffff0fbc 0xffff0fb8 0xffff0fb4 0xffff0fb0

Part B: esp: 0xffff0fcc ebp: 0xffff0fe0 Grading Rubric: Part A (8): Let m be the number of mistakes. The following is a non-exhaustive list of possible mistakes: -

An entry is left blank, although it should contain a value. An entry is filled in, although it should remain blank. An entry contains an incorrect value. The stack is shifted up or down by four bytes. (If the stack is shifted by more than four bytes, count the number of times it is shifted.)

In general, 8 - ceil(m / 2) points should be awarded for this subproblem. However, at the grader's discretion, one or two extra points can be awarded for solutions that display some knowledge of x86 stack conventions, or one or two extra points

can be deducted for solutions that betray only feeble knowledge of x86 stack conventions. Part B (2): One point each for %esp and %ebp. The addresses must be consistent with the values provided in the stack diagram. ********* Problem 6 ********* Part A: +----+----+----+----+----+----+----+----+ | a | XX | XX | XX | XX | XX | XX | XX | +----+----+----+----+----+----+----+----+ | b | b | b | b | b | b | b | b | +----+----+----+----+----+----+----+----+ | c | c | XX | XX | XX | XX | XX | XX | +----+----+----+----+----+----+----+----+ |d[0]|d[0]|d[0]|d[0]|d[0]|d[0]|d[0]|d[0]| +----+----+----+----+----+----+----+----+ |d[1]|d[1]|d[1]|d[1]|d[1]|d[1]|d[1]|d[1]| +----+----+----+----+----+----+----+----+ |e[0]|e[1]|e[2]| XX | f | f | f | f |end +----+----+----+----+----+----+----+----+ Part B: (gdb) disassemble foo Dump of assembler code for function foo: 0x00000000004004e4 : sub $0x8,%rsp 0x00000000004004e8 : movb $0x65,(%rdi) 0x00000000004004eb : movq $0x0,0x18(%rdi) 0x00000000004004f3 : movw $0x213,0x10(%rdi) 0x00000000004004f9 : movzbl 0x29(%rdi),%ecx 0x00000000004004fd : lea 0x2c(%rdi),%rdx 0x0000000000400501 : mov 0x8(%rdi),%rsi 0x0000000000400505 : mov $0x40062c,%edi 0x000000000040050a : mov $0x0,%eax 0x000000000040050f : callq 0x4003e0 0x0000000000400514 : add $0x8,%rsp 0x0000000000400518 : retq End of assembler dump. Part C: Most compact ordering is 40 bytes. for example: b, d, f, e, a, c ********* Problem 7 ********* int test(int x, int y, int z) { int result = 3; switch(z) { case 0: x = x & 25; case 3: case 7:

result break; case 5: result case 4: result break; default: result

}

= x; = 2 * x; = result + y;

= y; } return result;

********* Problem 8 ********* Answer: (B) 8. For (A) the sequence For (B) the sequence For (C) the sequence Hence, the answer is

of accesses will result in M H M M M M, hit rate = 0.16 of accesses will result in M H H M M M, hit rate = 0.33 of accesses will result in M H H H M M, hit rate = 0.5 (B)

********* Problem 9 ********* The unstated assumption is that C[i][j] is stored in a register. However, the question as written is ambiguous because it doesn't state this assumption clearly. Thus we will accept a miss rate of either 1/2 or 1/4 for part A. Part A: 16 floats in a block For row-wise A: Pattern is 1 miss, 15 hits, 1 miss, 15 hits, ... For col-wise B: Pattern is miss, miss, miss, ... For C[i][j] not in register: pattern is miss, hit, hit, hit, hit, ... If C[i][j] in register, then there are zero memory accesses to C[i][j] during each inner loop. If you assumed that C[i][j] is in a register, then there are only accesses to A and B in the inner loop. Thus, the miss rate = 17/32 ~ 1/2 If you assumed that C[i][j] is not in a register, then we have to include the accesses to C in the miss rate calulation: Thus, the miss rate = 17/48, which is closer to 1/4 than 1/2. Part B: For row-wise A, B: Pattern is 1 miss, 15 hits, 1 miss, 15 hits, ... If you assumed that C[i][j] is in a register, then the miss rate is 2/32 = 1/16. If you assumed that C[i][j] is not in a register, then the miss rate is 2/48 = 1/24, and the closest rate on the answer key is still 1/16.

Exam 1 Version 1 Solutions CS 213 Spring 2011 ********* Problem 1 ********* 1. c 2. b 3. d 4. b 5. c 6. b 7. d 8. c 9. b 10. a 11. a 12. T ********* Problem 2 ********* A.

67 = 0100 0011 -35 = 1101 1101

B.

/* * reverseBytes - reverse bytes * Example: reverseBytes(0x12345678) = 0x78563412 * Legal ops: ! ~ & ^ | + > */ int reverseBytes(int x) { int newbyte0 = (x >> 24) & 0xff; int newbyte1 = (x >> 8) & 0xff00; int newbyte2 = (x | |

0x1000

********* Problem 7 ********* A.

m m m m

m m m m

m m m m

m m m m

m m m m

m m m m

m m m m

m m m m

Miss rate = 1 B.

m m m m

h h h h

h h h h

h h h h

h h h h

h h h h

h h h h

h h h h

Miss rate = 1/8 C.

The second sample performs better than the first because it makes better use of the cache. In the first, heavy conflict results in a large number of evictions. In the second, the entire array fits in the cache, and the only misses are cold misses.

Exam 1 Solutions 15-213 / 18-243 Fall 2010 ********* Problem 1 ********* 1-b 2-a 3-c 4-b 5-a 6-d 7-b 8-a 9-a 10-a 11-b 12-a 13-IA32: 1, 4, 4, 4 x86-64: 1, 4, 8, 8 14-a 15-a 16-a 17-a 18-a 19-elided (ambiguous) 20-a ********* Problem 2 ********* Value FP bits Rounded value 1 011 00 1 easy normalized, exact (1 pt) 12 110 10 12 normalized exact (1 pt) 11 110 10 12 round up to 12 (because 10 is odd) (1 pt) 1/8 000 10 1/8 denorm, exact (2 pt) 7/32 001 00 1/4 straddles norm/denorm boundary, round up ********* Problem 3 ********* Version A: H = 15 J = 3 Version B: H = 5 J = 7 ********* Problem 4 ********* Part A: abccddddeeeefXXX gggg Part B: There are multiple correct answers. In general, put the largest data items first. E.g., ddddeeeeffffccab fXXX ********* Problem 5 ********* A. Clyde B. Inky C. Pinky D. Blinky *********

Problem 6 ********* Cases 210, 214, and 218 should have "break". 0x400590: 0x400598: 0x4005a0: 0x4005a8: 0x4005b0: 0x4005b8: 0x4005c0: 0x4005c8: 0x4005d0: 0x4005d8:

0x400470 0x40048a 0x40048a 0x400477 0x40047c 0x40048a 0x400482 0x40048a 0x400482 0x400487

********* Problem 7 ********* A. (a) Call pushes the return address on the stack, jmp does not. (b) Ret gets the return address from the top of the stack. B. x == 5 C. string "0123456" is stored at 0x80484d0 D. buf[0] = 0x33323130 buf[1] = 0x00363534 buf[2] = 0xffffd3e8 buf[3] = 0x080483fc buf[4] = 0x080484d0 E. Value at %ebp is 0xffffd3e8 F. Value at %esp is 0x080483fc G. No segfault. "0123456" is only 8 bytes including '\0', and int[2] can store 8 bytes.

Solution Key to 15213-s10 Exam 1 Problem Part 1: Part 2: Part 3: Part 4: Part 5: Part 6: Part 7:

1. c a a,b,c a a b d

-----------------------------------------------------------------------------Problem 2. Part 1: int mystery(int i) { if ( i!= 0 ) return i + mystery(i-1); return i; } Part 2: cmp $0x0, %eax

Part 3: There is a jump to line 0x8048364 so 0x8048361 is not actually executed everytime 0x8048364 is. Problem 3. Part 1: 0xffd3d208 Part 2: 0x080483cd Part 3 (table): arg1: arg2: arg3: arg4:

0xffd3d1f0 0xffd3d1f4 0xffd3d1f8 0xffd3d1fc

15 7/64 NaN 2^(-149)

-----------------------------------------------------------------------------Problem 4: Part 1: Key: _ unshaded = shaded bb==xxxx ss==zzzz ccccc=== aaaaaaaa q===____ Part ms.b ms.x ms.s ms.z ms.c ms.a ms.q

2: = 0x00bb = 0x0001d9f9 = 0x3b6d = 0xbeefbabe = 0x68, 0x6c, 0x70, 0x6d, 0x65 = 0xdeafelffledfable = 0x21

Part 3: (many possible solutions) One example: struct my_compressed_struct { long long a; long z; int x; short b; short s; char[5] c;

}

char q;

Part 4: Depends on previous solution. From our example above: aaaaaaaa xxxxzzzz bbsscccc cq==____ size = 28 -----------------------------------------------------------------------------Problem 5: c b a ret Addr to caller old ebp eax (c) ecx (b) edx (a) 0xde old ebp eax (c) ecx (b) edx (a) 0xde old ebp 6 1 => -5 2 => 0 3 => 6 => 5

********* Problem 5 ********* odin dva tri chetyre

b e a d

********* Problem 6 ********* a) ecx = rax + b) eax = *(rdi eax = *(rdi c) no stack to d) x86_64

r8 + rax) // rdi is int * + 4 * rax) // rdi is char * return from, no push %ebp

32-bit has les registers than 64-bit mode, so local variables would need to be stored on the stack. Stack accessing takes extra instructions and extra time. e) int mystery (int * array, size_t size, int e){ int a; int max = size; int min = 0; while (max - min > 0) { a = (max - min) / 2 + 1; if (array[a] < e) { max = a; } else if (array[a] < e) { min = a; } else { return a } } return -1; } f) Binary Search ********* Problem 7 ********* 1. 2. 3. 4. 5. 6. 7. 8. 9.

b c c b b a a b d

********* Problem 8 ********* H = 28 J = 15

Exam 1 Solutions CS 213 Spring 2009 ********* Problem 1 ********* Description Bias Smallest Positive positive Lowest finite Smallest positive normalized

Decimal 3 1/16 -14 1/4

Binary -----000001 111011 000100

--------------------********* Problem 2 ********* a) 32 bytes b) 6 bytes c) fun2 fun4 fun3 fun1 d)0x000f 0xbfb2ffdc 0x0000000c 0x0012 0x000000f3 0x000000d5 0x000000c 0x01

********* Problem 3 ********* int switchfn(inta, longb) { int y=0,x=)xdeadbeef; switch(A*b) { case 1: return 24; case 6: a= x + b*4; return a; case 0: return a+b; case 4: x=a; y*=b; break; case 2: a= y==x;

-7/16 5/4 -5/8 13(12)

100111 001101 101001 011010

case 3: b = y0) { a = 0; for(b=len-1; b>0; b--) { if(array[b]>array[a]) { a=b; } } len--; tmp = array[len]; array[len] = array[a]; array[a] = tmp; } ********* Problem 5 ********* a a b b a d d b b a

c Update 10/6/11 by Taerim Kim: Although C was accepted as the solution in Spring 2009, none of the choices for Problem 5 Question 11 are correct. ********* Problem 6 ********* a) Enter new stack frame by saving old base pointer and allocating space by subtracting from stack pointer b)Fill into dissasembly of foo 0x8, 0xc, 0x10, 0x14 or 8, 12, 16, 20 c) The new foo is not saving the old ebp. This makes the stack frame 4 bytes shorter old -----| args | | ret | | ebp | ebp | | | | | ... | | | |______| esp

new -----| args | | ret | | | | | | | | ... | | | |______| esp

d) 0x44, 0x48, 0x4c, 0x50 or 68, 72, 74, 80 e) Advantages: save space (4 bytes per stack frame) save time (don't execute enter leave instructions) %ebp can now be used as GPR Drawbacks: makes debugging "impossible" as you can no longer easily get a stack trace by climbing the stack with the base pointer

Exam 1, Version 1 CS 213 Fall 2008

********* Problem 1 ******** Expression Decimal Binary ------------------------------------| -17 | 1110 1111 --| 41 | 0010 1001 sa | -6 | 1010 b | -12 | 1111 0100 sc | 4 | 0100 ux | 192 | 1100 0000 TMax | 127 | 0111 1111 TMax-TMin | -1 | 1111 1111 ----------------------------------********* Problem 2 ******** A. B. C. D. E.

True. False. False. True. False.

It yields i when i < 0, and ~i when i >= 0 For 0 and TMin For large values of i, e.g., TMax, the addition can overflow Will use floating point arithmetic, which does not overflow Converting i to floating point can cause rounding. E.g., for TMax

********* Problem 3 ******** 110 100 111 000

11100 10101 00000 00001

| | | |

15 53/16 +Inf 1/128

| | | |

10010 10000 10100 01000

111 101 110 000

********* Problem 4 ******** Blanks should be filled in as: int ** i < n j 0 i 0 ********* Problem 5 ******** H = 6 J = 31

| | | |

15 13/4 56 1/128

********* Problem 6 ******** C D A E ********* Problem 7 ******** long test(long a, long b, long c) { long answer = 5; switch(a) { case 5: c = b ^ 15; /* Fall through */ case 7: (or 0) case 0: (or 7) answer = c + 112; break; case 2: (or 4) answer = (c + b) Norm ------------------------------------------------------------------********* Problem 3 ********* A 40-byte string results in the low order byte of the return address being overwritten with a '\0' byte, which changes the return address from 0x400f7c to 0x400f00, coincidently, the address of smoke(). ********* Problem 4 ********* L = 9 M = 33 N = 2 ********* Problem 5 ********* 0x400690: 0x4006a0: 0x4006b0:

0x00000000004004d1 0x00000000004004bb 0x00000000004004d4

********* Problem 6 ********* int bar2(int x) { int y = 0; int z = x/4; for( ; z > 0 ; y++) z = z/4; return y; } } ********* Problem 7 ********* apr

0x00000000004004b8 0x00000000004004c0 0x00000000004004c3

jan feb mar ********* Problem 8 ********* A. 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ |id| X|code | amount | name |X X X X X | +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ | data |in| X X X X X X X| +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ | next | address |X X X +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ B. size(Union_1) = 48 bytes C. (a) Output #1 is u->value = 0x6c6c6548 (b) Output #2 is u->buf = Hello WoSM (c) Output #3 is u->buf = He

15-213 Fall, 2004Exam 1 SOLUTIONS 1. Intended Solution (Bias = +7) Entries: -0, 15/1024, -17/1024, 13, -248, +infty Solution for Bias = -7 Entries: -0, 240, -272, 212,992, -24,063,232, +infty 2. A. B. C. D. E. F. 3. A.

1...1 for x < 0, 0...0 for x >= 0 -2 for x < 0, 0 for x >= 0 x >= 0 opt_abs(x) == abs(x) - 1 Compute return value as: comp-mask 0 and TMin

Register %eax %ebx %ecx %edx %esi

Variable l, A[j], j r, j i A[i], t A

%edi %esp %ebp

x stack pointer stack frame pointer

B. These are callee save registers. C. static int bunny(int l, int r, int *A) { int x = A[l]; int i = l - 1; int j = r + 1; while(i < j) { do { j--; } while(A[j] > x); do { i++; } while(A[i] < x); if(i < j) { int t = A[i]; A[i] = A[j]; A[j] = t; } } return j; } D. draft_horse() { if() { bunny(); draft_horse(); draft_horse(); } return; } E. This is the code for quicksort. 4.

A. In the original code, strlen(str) must be called on every iteration due to potential side-effects. Moving it to line 3 reduces the number of calls from 'n' calls to a single call. B. Yes. In this code, the length of str is never changed, therefore strlen(str) will return the same value for each iteration of the loop. Additionally, strlen(str) has no side-effects. These two properties make it safe to move. C. In the original code, each iteration of the loop has a multiplication that depends on the single variable hash. By unrolling the loop, each iteration can perform two multiplications in parallel because they depend on separate variables. Note that in modern processors, accurate branch prediction will make loop overheads negligable in both cases (as the increment and jump instructions can be performed in parallel to the ongoing multiplication). D. Yes. Many compilers have an option to perform loop unrolling. In this case there are no potential side effects of a loop iteration, and thus loop unrolling would be safe to perform.

E. Another optimization is replacing the * 32 with >> 5, a form of strength reduction. Because the shift operation is faster than multiply, this should improve performance. 5. A.

|| +----+----+----+----+----+----+----+----+----+----+----+----+----+ | | 69 | 6c | 75 | 76 | 32 | 31 | 33 | 00 | | | | | +----+----+----+----+----+----+----+----+----+----+----+----+----+ ^ %ebp B.

|| +----+----+----+----+----+----+----+----+----+----+----+----+----+ | | xx | xx | xx | xx | xx | xx | xx | xx | st | qr | op | mn | +----+----+----+----+----+----+----+----+----+----+----+----+----+ || || +----+----+----+----+----+----+----+----+----+----+----+----+----+ | e2 | 83 | 04 | 08 | xx | xx | xx | xx | 13 | 52 | 01 | 00 | | +----+----+----+----+----+----+----+----+----+----+----+----+----+ 6. A. 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+-+--+--+ | WID | name |//| address | balance | |////////| note | +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+-+--+--+ ^ | domestic B. typedef struct { short WID; char name[5]; char domestic; Location address; double balance; char *note; } Compact; C. 24 D. typedef struct { short WID; char name[5]; char domestic; double balance; Location address;

char *note; } Win_Compact; E.

0x004e4143

7. T, F, T, F, F, T, F

Exam 1 Solutions CS 213 Fall 2003 ********* Problem 1 ********* --------------------------------Expression decimal binary --------------------------------Zero | 0 | 00 0000 --| -6 | 11 1010 --| 18 | 01 0010 ux | 47 | 10 1111 y | -3 | 11 1101 x >> 1 | -9 | 11 0111 TMax | 31 | 01 1111 -TMin | -32 | 10 0000 TMin+TMin | 0 | 00 0000 ********* Problem 2 ********* Part I A. [1,2): 2^7 B. [2,3): 2^6 Part II A. Denormalized numbers (a) E = -6 (b) M = 127/128 B. Normalized numbers (a) E = -6 (b) E = +7 (c) M = 255/128 Part III ----------------------------------------------------------Description | E | M | V | Binary ----------------------------------------------------------Zero | -6 | 0 | 0 | 0 0000 00000 ----------------------------------------------------------Smallest positive | -6 | 1/128 | 1/8192 | 0 0000 00001 ----------------------------------------------------------Largest denorm | -6 | 127/128 | 127/8192| 0 0000 11111 ----------------------------------------------------------Smallest pos norm | -6 | 1 | 128/8192| 0 0001 00000 -----------------------------------------------------------

********* Problem 3 ********* fun6 ********* Problem 4 ********* fall summer spring winter ********* Problem 5 ********* IA-32 Windows: 1 2 3 || 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 || c - - - - - - - d d d d d d d d s s - - p p p p f f f f p p p p || || IA-32 Linux: 1 2 || 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 || c - - - d d d d d d d d s s - - p p p p f f f f p p p p || || ********* Problem 6 ********* int mystery(int a, int b, int c) { int x, y; y = c; for (x = a + b; x > 0; x--) y++; return y; } ********* Problem 7 ********* A. buf[0] = 0x64636261 buf[1] = 0x68676665 buf[2] = 0x08040069 B. ebp = 0x68676665 C. eip = 0x08040069 ********* Problem 8 *********

%eax = 0x400446e8

EXAM ! SOLUTIONS SPRING 03 Problem 1 ========= 17. This answer was given at the very start of class as a reward to those students who had showed up ontime. Problem 2 ========= Expression decimal binary hex -------------------------------------------Zero | 0 | 0000 0000 | 00 --| -3 | 1111 1101 | fd i | -11 | 1111 0101 | f5 i >> 4 | -1 | 1111 1111 | ff ui | 245 | 1111 0101 | f5 s | -2 | 1111 1110 | fe s ^ 7 | -7 | 1111 1001 | f9 us | 14 | 0000 1110 | 0e TMax | 127 | 0111 1111 | 7f TMin | -128 | 1000 0000 | 80 Problem 3 ========= Description Binary M E V ---------------------------------------0 010 010 5/4 -1 5/8 2 3/8 0 100 010 5/4 1 5/2 -infinity 1 111 000 most neg norm 1 110 111 15/8 3 -15 smal pos dnrm 0 000 001 1/8 -2 1/32 Problem 4 ========= func1 Problem 5 ========= scooby -> dooby -> doo -> scrappy->

X 1,4 3 2

Problem 6 ========= 1. 0xbfffb30c 2.

68 fc b2 ff bf c3 xx xx xx xx xx xx pq rs tu vw

fc b2 ff bf 00 where xx can be anything except 00, and pqrstuvw can be any valid hexademical values which aren't zeros 3.

0xvwturspq (matching the pqrstuvw above)

Problem 7 ========= #define M 11 //two points for each index #define N 3 int arr1[M][N]; int arr2[M][N]; int moo(int x) { int i; for(i = 0; i < M; i++) //1 point for this line { arr1[i][0] = arr2[i][2]+4*x; //a point for each index, plus 2 for the product } return i; //1 point for this line } Problem 8 ========= A. 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ | ID |XXXXX| weight |components |mom |XXXXX| | +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ B. typedef struct { double weight; double *components; short momentum; char ID[2]; } Modified; C. 16

Exam 1 Solutions CS 213 Fall 2002 Problem 1 ********* Expression decimal binary ----------------------------------Zero | 0 | 0 0000 --| -5 | 1 1011 --| -14 | 1 0010 y | -9 | 1 0111 z | 23 | 1 0111

y-z | 0 | 0 0000 TMax | 15 | 0 1111 TMin | -16 | 1 0000 ----------------------------------********* Problem 2 ********* Part I A. Denormalized numbers (a) E = -6 (b) M = 31/32 B. Normalized numbers (a) E = -6 (b) E = +7 (c) M = 63/32 Part II ----------------------------------------------------------Description | E | M | V | Binary ----------------------------------------------------------Zero | -6 | 0 | 0 | 0 0000 00000 ----------------------------------------------------------Smallest positive | -6 | 1/32 | 1/2048 | 0 0000 00001 ----------------------------------------------------------Largest denorm | -6 | 31/32| 31/2048 | 0 0000 11111 ----------------------------------------------------------Smallest pos norm | -6 | 1 | 32/2048 | 0 0001 00000 ----------------------------------------------------------One | 0 | 1 | 1 | 0 0111 00000 ----------------------------------------------------------Largest odd integer | 5 | 63/32| 63 | 0 1100 11111 ----------------------------------------------------------Largest finite number| 7 | 63/32| 252 | 0 1110 11111 ----------------------------------------------------------Positive infinity | - | - | +inf | 0 1111 00000 ----------------------------------------------------------********* Problem 3 ********* M=3 N=9 ********* Problem 4 ********* A. Product_Struct1: 0

1

2

3

4

5

6

7

8

end of struct | 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23

+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ |name |XX|type |model |c |XXXXXXXX|price | +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ Product_Struct2: end of struct | 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ |name |type |c |XX|model|XXXXX|price | +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ B. sizeof(Product_Struct1) = 24 sizeof(Product_Struct2) = 16 sizeof(Product_Union) = 24 C. (a) (b) (c) (d) (e)

p->product_id = 0x64636261 p->two.name = 0x64636261 p->two.type = 0x6665 p->two.color = 0x00 p->two.model = 0x2ace

********* Problem 5 ********* (c) fun6 ********* Problem 6 ********* A. return addr: saved %ebp : &buf : saved %ebx : %esp :

0x04 0x00 0xf8 0xd8 0xd8

B. (%ebp) = 0x6874726f C. 0x080484e3 ********* Problem 7 ********* int foo(int x, int y, int z) { int i, result; result = y; for (i = x; i >= z; i--) { result = result + i; } return result;

}

15-213 Exam 1 Spring 2002 Solutions Question 1: Expression decimal binary ----------------------------------Zero | 0 | 00 0000 --| -3 | 11 1101 --| -14 | 11 0010 ux | 51 | 11 0011 y | -3 | 11 1101 x >> 2 | -4 | 11 1100 TMax | 31 | 01 1111 ----------------------------------Question 2: Description | Binary | M | E | Value -----------------------------------------------------------------Positive Zero | 0 000 0000 | 0 | -2 | 0.0 -------------------------------------------------------------------| 0 000 0101 | 5/16 | -2 | 5/64 -----------------------------------------------------------------Largest Denormalized | 0 000 1111 | 15/16 | -2 | 15/64 -----------------------------------------------------------------Smallest Normalized | 1 001 0000 | 8/8 | -2 | -1/4 -----------------------------------------------------------------Negative Two | 1 100 0000 | 1 | 1 | -2.0 -------------------------------------------------------------------| 1 110 1101 | 29/16 | 3 | -14.5 -----------------------------------------------------------------Negative infinity | 1 111 0000 | --| --- | -infty -----------------------------------------------------------------Question 3: A. 4 B.

0 1 2 4 8 14 16 24 26 32 |--------------------------------------------------------| | c[0] | c[1] | pad | intp | union1 | pad | d | s | pad | |--------------------------------------------------------|

C. 32 bytes D. 24 bytes Question 4: int foo (int op, int a, int b)

{

int result = 0; switch (op) { case 0: result case 1: result case 2: result case 3: result case 4: result }

= = = = =

a & a | a ^ ~a a +

b; b; b; ; b;

break; break; break; break; break;

return result;

}

Question 5: A.

-- alloc -Uses the next free BP register to save the number of the last used stack register. Why don't we have to save the 'old base pointer'? There is a separate register for each stack frame. Hence, there's no need to save any values - they are automatically saved by the BPn registers. -- push -Uses the next free STK register to save data on the 'stack' (and increments/decrements some index into the STK registers) -- pop -Takes the value from the last used register and decrements/increments some index into the STK registers. -- call -Uses the next free RET register to save the return address. Also jumps to the procedure

B. What is the problem with using registers in this fashion? Registers only offer a limited, static amount of space. C. Why is the notion of a base pointer still required? If the call stack gets too large, the data in these registers still must be saved in a stack-ish area. Hence, the notion of a base pointer is still required because we may need to save these registers out to a area

of memory. It's not really a base pointer in the same sense as we know, and the solution to the problem at hand (putting the 'base pointer' in the BP registers) isn't necessarily good either, but at some point, system software will have to save out these registers to a memory area, since the stack must remain valid Many people thought that we would still need some memory address to serve as the base pointer because we need it to index on the stack. However, imagine the case where the STK, BP, and RET sets of registers are infinitely large. Following the above solution for the operation of each instruction, it is possible to see that there is no reason to keep a base pointer (since there is a BP register for each stack frame and all the BP registers do is store an index - not a pointer) to be able to index into data. Question 6: Part A: Send some negative number for index such that cmds[index] is a bad address. Part B: Overflow buffer to set the return address to point into the buffer you sent, which contains the code to execute. Part C: The head of the buffer should contain the /etc/passwd entry. Overflow buffer to set return address into the buffer, which sets result_fname to point to "/etc/passwd" (stored in the sent buffer), and then code to jump to the line with open(). Question 7: FindMin: push %ebp push %ebp mov %esp, %ebp push %ebx mov 0x8(%ebp), %ecx mov 0xc(%ebp), %edx mov (%ecx), %eax mov $0x0, %ebx cmp %edx, %ebx jge .done .loop cmp %edx, (%ecx, %ebx, 4) jge .incr mov (%ecx, %ebx, 4), %eax .incr inc %ebx cmp %edx, %ebx jl .loop .done pop %ebx mov %ebp, %esp

-------

save callee-save register ecx = A edx = size eax = min = A[0] ebx = i cmp size, i

-- cmp min, A[i] -- eax = min = A[i] -- incr i -- cmp size, i -- restore callee-save reg.

pop %ebp ret

Exam 1 Solutions CS 213 Fall 2001 ********* Problem 1 ********* Expression decimal binary ----------------------------------Zero | 0 | 00 0000 --| -6 | 11 1010 --| 18 | 01 0010 ux | 47 | 10 1111 y | -3 | 11 1101 x >> 1 | -9 | 11 0111 TMax | 31 | 01 1111 -TMin | -32 | 10 0000 TMin+TMin | 0 | 00 0000 ----------------------------------********* Problem 2 ********* -----------------------------------------------------------------Description | Binary | M | E | Value -----------------------------------------------------------------Minus Zero | 1 000 0000 | 0 | -2 | -0.0 -------------------------------------------------------------------| 0 100 0101 | 21/16 | 1 | 21/8 -----------------------------------------------------------------Smallest Denormalized| 1 000 1111 | 15/16 | -2 | -15/64 -----------------------------------------------------------------Largest normalized | 0 110 1111 | 31/16 | 3 | 31/2 -----------------------------------------------------------------One | 0 011 0000 | 1 | 0 | 1.0 -------------------------------------------------------------------| 0 101 0110 | 11/8 | 2 | 5.5 -----------------------------------------------------------------Positive infinity | 0 111 0000 | --| --- | +\infty -----------------------------------------------------------------********* Problem 3 ********* fun8 ********* Problem 4 ********* fun6 *********

Problem 5 ********* M=15 N=9 ********* Problem 6 ********* int foo(int a) { int i; int result = a + 2; for (i=0; i < a; i++) { result += (i + 5); result *= (i + 3); } return result; } ********* Problem 7 ********* A. OldSensorData:

end of struct | 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ |code |XXXXX| start | raw |XX| data | | +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ NewSensorData: end of struct | 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ |code |start| raw |XX|sense| ext |XXXXX| data | +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ B. newData->start newData->raw[0] newData->raw[2] newData->raw[4] newData->sense

= = = = =

0xff00 0xb8 0x50 0xe1 0x008f

********* Problem 8 ********* A.

+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ | |64|72|2e|65|76|69|6c|00| | | +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ ^ ebp ---------- addresses increase this way ------> The printf inside main prints 0x6c6976 (or 0x006c6976 is OK too) B. (a) buf[0] = 0x652e7264 buf[3] = 0x08040073 (b) %ebp = 0x6576696c

View more...

Comments

Copyright ©2017 KUPDF Inc.
SUPPORT KUPDF