Test Exam Solutions Problem 1. a) 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 +---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+--- | c X X X p p p p i i i i X X X X +---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+--- 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 +---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+ d d d d d d d d s s X X X X X X +---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+ b) 32 c) 8 d) 5 e) 8 f) 8 Problem 2. 1) f 2) b 3) a 4) c 5) e 6) h In all the reasoning we examplify with 4-bit numbers a) MIN_INT + MAX_INT = 1000 + 0111 = 1111 = -1 Now we only have to look at one bit: b ^ 1 = ~b so we get ~(~a | ~b) = ~~a & ~~b = a & b Calculates a & b b) Look at one bit. Two cases b = 1 and b = 0 b = 1) ((a ^ 1) & ~1) | (~(a ^ 1) & 1) = (~a & 0) | (~~a) = a b = 0) ((a ^ 0) & ~0) | (~(a ^ 0) & 0) = (a & 1) | (~a & 0) = a Calculates a c) 1 + (a << 3) + ~a = (a << 3) + ~a + 1 = a * 8 - a = a * 7 Calculates a * 7 d) (a << 4) + (a << 2) + (a << 1) = a * 16 + a * 4 + a * 2 = a * 22 Calculates a * 22 e) Two cases a < 0 and a >= 0 a < 0) Need to add a bias 2^2 - 1 = 3 to get closest integer >= a / 4 (a + 3) >> 2 = a / 4 a >= 0) a >> 2 = a / 4 Calculates a / 4 f) See a) We get a ^ 1 = ~a Calculates ~a g) ~a + 1 = -a Two cases a = 0 and a != 0 a = 0) ~((0 | -0) >> W) & 1 = ~(0 >> W) & 1 = ~0 & 1 = 1 a != 0) a is of form b 1 z where b are m arbitrary bits and z are n zeros where m>=0, n>=0 and m+n=W. Then ~a is ~b 0 k where k are n ones and we get ~b 0 k + 1 = ~b 1 z which gives b 0 z | ~b 1 z = d z where d is m+1 ones so ~(d z) = p k where p are m+1 zeros (p k) >> W = 0 and 0 & 1 = 0 So a = 0 gets 1 and a != 0 gets 0. Calculates !a h) Two cases a < 0 and a >= 0 a < 0) a >> W = -1 and -1 << 1 = -2 but ~x = -x - 1 so ~(-2) = -(-2) - 1 = 2 - 1 = 1 a >= 0) a >> W = 0 and 0 << 1 = 0 and ~0 = -1 Calculates (a < 0) ? 1 : -1 i) Calculates a >> 2 Problem 3. 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 4. M=15 N=9 Problem 5. fun1 Problem 6. A) yes, -4, Need to pass pointer to it to recursive call B) no C) the value of %ebx is saved here, because %ebx is a callee-save register. D) nothing is stored here. Problem 7. A) 1 as we remove the jle instruction. B) if %eax <= 0 we gain 0 as we remove the jle instruction but we execute the rrmovlg which does nothing if %eax > 0 we gain 3 as we remove the jle instruction and avoid 2 bubbles C)