+ All Categories
Home > Documents > Computer Systems: A programmers perspective

Computer Systems: A programmers perspective

Date post: 02-Oct-2015
Category:
Upload: saurabh-kapadia
View: 57 times
Download: 1 times
Share this document with a friend
Description:
Exam solutions
52
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 | B One | 0 011 00 | 0 01 000 Exact in both formats 1/2 | 0 010 00 | 0 00 100 Exact in both formats, norm in A, denorm in B 11/8 | 0 011 10 | 0 01 011 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;
Transcript

Exam 1 Solutions15-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 truethat blocking in things like matmult primarily exploits temporallocality, blocking is effective for transpose because it exploitsspatial 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 0000x >> 1 | -3 | 1101 | -3 | 11 1101(-x ^ -1) >> 2 | -2 | 1110 | -2 | 11 1110----------------------------------------------------------------

*********Problem 3********* | A | B One | 0 011 00 | 0 01 000 Exact in both formats1/2 | 0 010 00 | 0 00 100 Exact in both formats, norm in A, denorm in B11/8 | 0 011 10 | 0 01 011 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 bc c c c d d d X e e e e e e e ef f f f f f f f

Part 2.f f f f f f f f b b b b b b b be 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 be e e e e e e e f f f f f f f f

*********Problem 6*********A: phdB: bachelorsC: 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 The diagram starts with the addresss 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 | 0xffff1008 +--------------------------------+ | 18 | 0xffff1004 +--------------------------------+ | 213 | 0xffff1000 +--------------------------------+ | unknown | 0xffff0ffc +--------------------------------+ | unknown | 0xffff0ff8 +--------------------------------+ | unknown | 0xffff0ff4 +--------------------------------+ | unknown | 0xffff0ff0 +--------------------------------+ | 15 | 0xffff0fec +--------------------------------+ | 18 | 0xffff0fe8 +--------------------------------+ | 0x080483b7 | 0xffff0fe4 +--------------------------------+ | 0xffff0ff8 | 0xffff0fe0 +--------------------------------+ | unknown | 0xffff0fdc +--------------------------------+ | unknown | 0xffff0fd8 +--------------------------------+ | 3 | 0xffff0fd4 +--------------------------------+ | 15 | 0xffff0fd0 +--------------------------------+ | 0x080483b7 | 0xffff0fcc +--------------------------------+ | 0xffff0fe0 | 0xffff0fc8 +--------------------------------+ | unknown | 0xffff0fc4 +--------------------------------+ | unknown | 0xffff0fc0 +--------------------------------+ | 0 | 0xffff0fbc +--------------------------------+ | 3 | 0xffff0fb8 +--------------------------------+ | unknown | 0xffff0fb4 +--------------------------------+ | unknown | 0xffff0fb0 +--------------------------------+

Part B:esp: 0xffff0fccebp: 0xffff0fe0

Grading Rubric:

Part A (8):Let m be the number of mistakes. The following is anon-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 thissubproblem. However, at the grader's discretion, one or twoextra points can be awarded for solutions that display someknowledge of x86 stack conventions, or one or two extra pointscan be deducted for solutions that betray only feeble knowledgeof x86 stack conventions.

Part B (2):One point each for %esp and %ebp. The addresses must beconsistent 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 fooDump of assembler code for function foo:0x00000000004004e4 : sub $0x8,%rsp0x00000000004004e8 : movb $0x65,(%rdi)0x00000000004004eb : movq $0x0,0x18(%rdi)0x00000000004004f3 : movw $0x213,0x10(%rdi)0x00000000004004f9 : movzbl 0x29(%rdi),%ecx0x00000000004004fd : lea 0x2c(%rdi),%rdx0x0000000000400501 : mov 0x8(%rdi),%rsi0x0000000000400505 : mov $0x40062c,%edi0x000000000040050a : mov $0x0,%eax0x000000000040050f : callq 0x4003e0 0x0000000000400514 : add $0x8,%rsp0x0000000000400518 : 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 = x; break; case 5: result = 2 * x; case 4: result = result + y; break; default: result = y; } return result;}

*********Problem 8*********Answer: (B) 8.For (A) the sequence of accesses will result in M H M M M M, hit rate = 0.16For (B) the sequence of accesses will result in M H H M M M, hit rate = 0.33For (C) the sequence of accesses will result in M H H H M M, hit rate = 0.5Hence, the answer is (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 statethis assumption clearly. Thus we will accept a miss rate of either 1/2or 1/4 for part A.

Part A:16 floats in a blockFor 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 onlyaccesses 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 toinclude the accesses to C in the miss rate calulation: Thus, the missrate = 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 is2/32 = 1/16.

If you assumed that C[i][j] is not in a register, then the miss rateis 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. b3. d 4. b5. c6. b7. d8. c9. b10. a11. a12. 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 | |

*********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 h h h h h h h m h h h h h h h m h h h h h h h m 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*********ValueFP bitsRounded 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:abccddddeeeefXXXgggg

Part B:There are multiple correct answers. In general, putthe largest data items first. E.g.,

ddddeeeeffffccabfXXX

*********Problem 5*********A. ClydeB. InkyC. PinkyD. Blinky

*********Problem 6*********Cases 210, 214, and 218 should have "break".

0x400590: 0x4004700x400598: 0x40048a0x4005a0: 0x40048a0x4005a8: 0x4004770x4005b0: 0x40047c0x4005b8: 0x40048a0x4005c0: 0x4004820x4005c8: 0x40048a0x4005d0: 0x4004820x4005d8: 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 == 5C. string "0123456" is stored at 0x80484d0D. buf[0] = 0x33323130 buf[1] = 0x00363534 buf[2] = 0xffffd3e8 buf[3] = 0x080483fc buf[4] = 0x080484d0E. Value at %ebp is 0xffffd3e8F. 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 1.Part 1: cPart 2: aPart 3: a,b,cPart 4: aPart 5: aPart 6: bPart 7: 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 actuallyexecuted everytime 0x8048364 is.

Problem 3.

Part 1:0xffd3d208

Part 2: 0x080483cd

Part 3 (table):

arg1: 0xffd3d1f0 15arg2: 0xffd3d1f4 7/64arg3: 0xffd3d1f8 NaNarg4: 0xffd3d1fc 2^(-149)

-------------------------------------------------------------------------------Problem 4:

Part 1:Key: _ unshaded = shaded

bb==xxxxss==zzzzccccc===aaaaaaaaq===____

Part 2:ms.b = 0x00bbms.x = 0x0001d9f9ms.s = 0x3b6dms.z = 0xbeefbabems.c = 0x68, 0x6c, 0x70, 0x6d, 0x65ms.a = 0xdeafelffledfablems.q = 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:aaaaaaaaxxxxzzzzbbsscccccq==____

size = 28

-------------------------------------------------------------------------------Problem 5:

cbaret Addr to callerold ebpeax (c)ecx (b)edx (a)0xdeold ebpeax (c)ecx (b)edx (a)0xdeold ebp 6 a == 1 => -5 a == 2 => 0 a == 3 => 6 o.w. => 5

*********Problem 5*********

odin bdva etri achetyre d

*********Problem 6*********

a) ecx = rax + r8b) eax = *(rdi + rax) // rdi is int * eax = *(rdi + 4 * rax) // rdi is char *c) no stack to return from, no push %ebpd) x86_6432-bit has les registers than 64-bit mode, so local variableswould need to be stored on the stack. Stack accessing takesextra 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. b2. c3. c4. b5. b6. a7. a8. b9. d

*********Problem 8*********

H = 28J = 15

Exam 1 Solutions CS 213 Spring 2009

*********Problem 1*********

Description Decimal BinaryBias 3 ------Smallest Positive positive 1/16 000001Lowest finite -14 111011Smallest positive normalized 1/4 000100------ -7/16 100111------ 5/4 001101------ -5/8 101001------ 13(12) 011010

*********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;

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*********aabba

ddbbac

Update 10/6/11 by Taerim Kim:Although C was accepted as the solution in Spring 2009, none of the choicesfor Problem 5 Question 11 are correct.

*********Problem 6*********a)

Enter new stack frame by saving old base pointer andallocating space by subtracting from stack pointer

b)Fill into dissasembly of foo

0x8, 0xc, 0x10, 0x14or8, 12, 16, 20

c)The new foo is not saving the old ebp. This makes the stack frame 4 bytes shorter

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

d)

0x44, 0x48, 0x4c, 0x50or68, 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 GPRDrawbacks: 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 1001sa | -6 | 1010b | -12 | 1111 0100sc | 4 | 0100ux | 192 | 1100 0000TMax | 127 | 0111 1111TMax-TMin | -1 | 1111 1111-----------------------------------

*********Problem 2********

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

*********Problem 3********

110 11100 | 15 | 10010 111 | 15 100 10101 | 53/16 | 10000 101 | 13/4 111 00000 | +Inf | 10100 110 | 56 000 00001 | 1/128 | 01000 000 | 1/128

*********Problem 4********

Blanks should be filled in as:

int ** i < n j 0 i 0

*********Problem 5********

H = 6J = 31

*********Problem 6********

CDAE

*********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 ofthe return address being overwritten with a '\0'byte, which changes the return address from 0x400f7c to0x400f00, coincidently, the address of smoke().

*********Problem 4*********

L = 9M = 33N = 2

*********Problem 5*********0x400690: 0x00000000004004d1 0x00000000004004b80x4006a0: 0x00000000004004bb 0x00000000004004c00x4006b0: 0x00000000004004d4 0x00000000004004c3

*********Problem 6*********

int bar2(int x){ int y = 0; int z = x/4;

for( ; z > 0 ; y++) z = z/4; return y; }}

*********Problem 7*********

aprjanfebmar

*********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. 1...1 for x < 0, 0...0 for x >= 0 B. -2 for x < 0, 0 for x >= 0 C. x >= 0 D. opt_abs(x) == abs(x) - 1 E. Compute return value as: comp-mask F. 0 and TMin

3. A. Register Variable %eax l, A[j], j %ebx r, j %ecx i %edx A[i], t %esi A %edi x %esp stack pointer %ebp 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 |+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ ^ | domesticB. typedef struct { short WID; char name[5]; char domestic; Location address; double balance; char *note; } Compact;C. 24D. 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 0010ux | 47 | 10 1111y | -3 | 11 1101x >> 1 | -9 | 11 0111TMax | 31 | 01 1111-TMin | -32 | 10 0000TMin+TMin | 0 | 00 0000

*********Problem 2*********Part IA. [1,2): 2^7B. [2,3): 2^6

Part IIA. 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*********fallsummerspringwinter

*********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] = 0x64636261buf[1] = 0x68676665buf[2] = 0x08040069

B.ebp = 0x68676665

C.eip = 0x08040069

*********Problem 8*********%eax = 0x400446e8

EXAM ! SOLUTIONS SPRING 03Problem 1=========17. This answer was given at the very start of class as a rewardto those students who had showed up ontime.

Problem 2========= Expression decimal binary hex--------------------------------------------Zero | 0 | 0000 0000 | 00--- | -3 | 1111 1101 | fdi | -11 | 1111 0101 | f5i >> 4 | -1 | 1111 1111 | ffui | 245 | 1111 0101 | f5s | -2 | 1111 1110 | fes ^ 7 | -7 | 1111 1001 | f9us | 14 | 0000 1110 | 0eTMax | 127 | 0111 1111 | 7fTMin | -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 -> Xdooby -> 1,4doo -> 3scrappy-> 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.16Exam 1 Solutions CS 213 Fall 2002Problem 1*********

Expression decimal binary-----------------------------------Zero | 0 | 0 0000 --- | -5 | 1 1011--- | -14 | 1 0010y | -9 | 1 0111z | 23 | 1 0111y-z | 0 | 0 0000TMax | 15 | 0 1111TMin | -16 | 1 0000-----------------------------------

*********Problem 2*********

Part IA. 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=3N=9

*********Problem 4*********

A.

Product_Struct1: 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 |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) = 24sizeof(Product_Struct2) = 16sizeof(Product_Union) = 24

C.(a) p->product_id = 0x64636261(b) p->two.name = 0x64636261(c) p->two.type = 0x6665(d) p->two.color = 0x00(e) p->two.model = 0x2ace

*********Problem 5*********(c) fun6

*********Problem 6*********A. return addr: 0x04saved %ebp : 0x00&buf : 0xf8saved %ebx : 0xd8%esp : 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 = a & b; break; case 1: result = a | b; break; case 2: result = a ^ b; break; case 3: result = ~a ; break; case 4: result = a + b; 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 -- save callee-save register mov 0x8(%ebp), %ecx -- ecx = A mov 0xc(%ebp), %edx -- edx = size mov (%ecx), %eax -- eax = min = A[0] mov $0x0, %ebx -- ebx = i cmp %edx, %ebx -- cmp size, i jge .done .loop cmp %edx, (%ecx, %ebx, 4) -- cmp min, A[i] jge .incr mov (%ecx, %ebx, 4), %eax -- eax = min = A[i] .incr inc %ebx -- incr i cmp %edx, %ebx -- cmp size, i jl .loop .done pop %ebx -- restore callee-save reg. mov %ebp, %esp pop %ebp ret

Exam 1 Solutions CS 213 Fall 2001

*********Problem 1*********

Expression decimal binary-----------------------------------Zero | 0 | 00 0000 --- | -6 | 11 1010--- | 18 | 01 0010ux | 47 | 10 1111y | -3 | 11 1101x >> 1 | -9 | 11 0111TMax | 31 | 01 1111-TMin | -32 | 10 0000TMin+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=15N=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 = 0xff00newData->raw[0] = 0xb8newData->raw[2] = 0x50newData->raw[4] = 0xe1newData->sense = 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


Recommended