Exam 1 Solutions: 6:30 - 9:30 section CS 213 Spring 2009 ********* Problem 1 ********* Description Decimal Bits ---------------------------------------------------------------- Bias 3 ........ Smallest positive number 1/16 0 000 01 Lowest finite -14 1 110 11 Smallest positive normalized 1/4 0 001 00 .... -6/16 1 001 10 .... 6/4 0 011 10 .... -3/4 1 010 10 .... 11 (12) 0 110 10 ********* Problem 2 ********* a) 36 bytes b) 8 bytes c) fun1, fun4, fun3, fun2 d) 0xf, 0xbfdbdc320, 0xc, 0x12, 0xf3, 0xd5, 0xc, 0x5 ********* Problem 3 ********* switch (y) { case 0: case 6: x=x+1; break; case -1: y=x; break; case 4: y=y / 2; // y >> 1; case 3: y = y + 5; break; default: y = y * 9; } return x[y]; ********* Problem 4 ********* do { a = 0; for (b = len - 1; b > 0; b--) { if (array[b - 1] > array[b]) { tmp = array[b - 1]; array[b - 1] = array[b]; array[b] = tmp; a++; } } } while (a > 0); ********* Problem 5 ********* i. (c) ii. (a) iii. (a) iv. (c) v. (d) vi. (a) vii. (a) viii.(a) ix. (a) x. (b) xi. (b) ********* Problem 6 ********* a) Save %ebp onto the stack so foo() can use it, as %ebp is a callee-saved register. Then allocate 64-bytes for buf. b) 8, 12, 16, 20 c) before +------+ | arg4 | +20 +------+ | arg3 | +16 +------+ | arg2 | +12 +------+ | arg1 | +8 +------+ |return| | addr | +4 +------+ | saved| | ebp | ebp +------+ | buf | esp +------+ after +------+ | arg4 | +80 +------+ | arg3 | +76 +------+ | arg2 | +72 +------+ | arg1 | +68 +------+ |return| | addr | +64 +------+ | buf | esp +------+ d) 68, 72, 76, 80 e) It reduces the overhead associated with making a function call by eliminating 2 memory access and a register copy. It also frees %ebp for use as a general register. Drawback (Not asked of students): This technique can, in certain circumstances, make accessing the arguments more difficult, because without the %ebp, there is no fixed point of reference. Their offset from the %esp can change if things are pushed onto the stack. This also makes debugging more difficult.