1 / 97

2011 ICS Middle Examination

2011 ICS Middle Examination. PROBLEM-1. Problem-1. Ref: section 2.2, 2.3 Conditions 6-bits machine Two’s complement arithmetic Signed integer Right shift is performed arithmetically Short is encoded using 3 bits. short sy = -4; unsigned short usy = sy; int y = sy; int x = -30;

adora
Download Presentation

2011 ICS Middle Examination

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 2011 ICS Middle Examination

  2. PROBLEM-1

  3. Problem-1 • Ref: section 2.2, 2.3 • Conditions • 6-bits machine • Two’s complement arithmetic • Signed integer • Right shift is performed arithmetically • Short is encoded using 3 bits

  4. short sy = -4; unsigned short usy = sy; int y = sy; int x = -30; unsigned int ux = x; Problem-1 9 = 001 001 -9 = 110 110 + 1 = 110 111

  5. short sy = -4; unsigned short usy = sy; int y = sy; int x = -30; unsigned int ux = x; Problem-1 9 = 001 001 -9 = 110 110 + 1 = 110 111

  6. short sy = -4; unsigned short usy = sy; int y = sy; int x = -30; unsigned int ux = x; Problem-1 x = -30 30 = 011 110 -30 = 100 001 + 1 = 100 010 ux = x = 100 010

  7. short sy = -4; unsigned short usy = sy; int y = sy; int x = -30; unsigned int ux = x; Problem-1 x = -30 30 = 011 110 -30 = 100 001 + 1 = 100 010 ux = x = 100 010

  8. short sy = -4; unsigned short usy = sy; int y = sy; int x = -30; unsigned int ux = x; Problem-1 sy = -4 short 4 = 100 -4 = 011 + 1 = 100 y = sy = 111 100 sign extension

  9. short sy = -4; unsigned short usy = sy; int y = sy; int x = -30; unsigned int ux = x; Problem-1 sy = -4 short 4 = 100 -4 = 011 + 1 = 100 y = sy = 111 100 sign extension

  10. short sy = -4; unsigned short usy = sy; int y = sy; int x = -30; unsigned int ux = x; Problem-1 sy = -4 short 4 = 100 -4 = 011 + 1 = 100 usy = sy = 100

  11. short sy = -4; unsigned short usy = sy; int y = sy; int x = -30; unsigned int ux = x; Problem-1 sy = -4 short 4 = 100 -4 = 011 + 1 = 100 usy = sy = 100

  12. short sy = -4; unsigned short usy = sy; int y = sy; int x = -30; unsigned int ux = x; Problem-1 x = 100 010 x>>2 = 11100 010 = 111 000arithmetically (x>>2)<<1 = 111 0000 = 110 000

  13. short sy = -4; unsigned short usy = sy; int y = sy; int x = -30; unsigned int ux = x; Problem-1 x = 100 010 x>>2 = 11100 010 = 111 000arithmetically (x>>2)<<1 = 111 0000 = 110 000

  14. short sy = -4; unsigned short usy = sy; int y = sy; int x = -30; unsigned int ux = x; Problem-1 x = 100 010 y = 111 100 x + y = 100 010 + 111 100 = 1011 110overflow

  15. short sy = -4; unsigned short usy = sy; int y = sy; int x = -30; unsigned int ux = x; Problem-1 x = 100 010 y = 111 100 x + y = 100 010 + 111 100 = 1011 110overflow

  16. short sy = -4; unsigned short usy = sy; int y = sy; int x = -30; unsigned int ux = x; Problem-1 x = 100 010 y = 111 100 !y = 000 000 x & !y = 100 010 & 000 000 = 000 000

  17. short sy = -4; unsigned short usy = sy; int y = sy; int x = -30; unsigned int ux = x; Problem-1 x = 100 010 y = 111 100 !y = 000 000 x & !y = 100 010 & 000 000 = 000 000

  18. short sy = -4; unsigned short usy = sy; int y = sy; int x = -30; unsigned int ux = x; Problem-1 UMax = 2^6 – 1 = 111 111 TMax = 2^5 – 1 = 011 111 -TMin = -2^5 = -(100 000) = 011 111 + 1 = 100 000

  19. short sy = -4; unsigned short usy = sy; int y = sy; int x = -30; unsigned int ux = x; Problem-1 UMax = 2^6 – 1 = 111 111 TMax = 2^5 – 1 = 011 111 -TMin = -2^5 = -(100 000) = 011 111 + 1 = 100 000

  20. PROBLEM-2

  21. Problem-2 • Ref: lab-1 • Conditions • The coding rules are same as lab1 • Legal ops: ! ~ & ^ | + << >> • Constants: <= 8 bits (0x0 ~ 0xFF)

  22. Problem-2 /* [absValue] * calculate the abs value of x * ex: absValue(5) = 5 * absValue(-29) = 29 * legal ops: ~ & ^ | - << >> * #ops: 8 */ int absValue (int x) { /* fill your code */ return }

  23. Problem-2 /* [absValue] * calculate the abs value of x * ex: absValue(5) = 5 * absValue(-29) = 29 * legal ops: ~ & ^ | - << >> * #ops: 8 */ int absValue (int x) { /* fill your code */ return } if x >= 0 abs = x = x^(0)-(0) if x < 0 abs = x^0xF.. + 1 = x^(-1)+1 = x^(-1)–(-1) abs = x^mask-mask mask = x>>31

  24. Problem-2 /* [absValue] * calculate the abs value of x * ex: absValue(5) = 5 * absValue(-29) = 29 * legal ops: ~ & ^ | - << >> * #ops: 8 */ int absValue (int x) { int mask = x >> 31; abs = x ^ mask – mask; return abs; } ex: x=5 mask = 5>>31 = 0 abs = 5^0 – 0 = 5 ex: x=-5= 0xF..FB mask = 0xF..FF abs = 0xF..FB ^ 0xF..FF - (-1) = 0x0..04 + 0x0..01 = 0x5

  25. Problem-2 /* [swapBits] * swap n bits started from i * with n bits started from j * ex: swapBits(0x2F,1,5,3) = 0xE3 * legal ops: ~ & ^ | - << >> */ intswapBits (int x, int i, int j, int n) { int mask = (1 << ?) - 1; intxorTemp = ((x >> ? ) ^ (x >> ? ))&? ; return x ? ((xorTemp << i) ? (xorTemp << j) }

  26. Problem-2 x = 0x2F, i = 1, j = 5, n = 3 0x2F = 0010 1111 swapBits(0x2F) = 1110 0011 = E3 x = 0x..A..B.. swapBits(x) = 0x..B..A.. A = A ^ B ^ B = (A ^ B) ^ B B = B ^ A ^ A = (B ^ A) ^ A x = 0x..A..B.. ^ 0x..T..T.. (T = A ^ B) swapBits(x) = 0x..B..A..

  27. Problem-2 x = 0x..A..B.. T = A ^ B = 0x0..0..A ^ 0x0..A..B & 0x0....0M {M = (1<<n)-1} = ((x >> j) ^ (x >> i)) & M swapBits(x) = 0x..A..B.. ^ 0x..T..T.. {0x..T.. | 0x....T..} = x ^ ((T << j) | (T << i))

  28. Problem-2 /* [swapBits] * swap n bits started from i * with n bits started from j * ex: swapBits(0x2F,1,5,3) = 0xE3 * legal ops: ~ & ^ | - << >> */ intswapBits (int x, int i, int j, int n) { int mask = (1 << n) - 1; intxorTemp = ((x >> i) ^ (x >> j))& mask; return x^((xorTemp << i)|(xorTemp << j) }

  29. PROBLEM-3

  30. Problem-3 • Ref: section 3.3, 3.4, 3.5 • Conditions • 32-bit little endian machine • 4 byte size and hex • Each operation take effect on the memory and register

  31. Problem-3

  32. Problem-3

  33. Problem-3

  34. Problem-3

  35. Problem-3

  36. Problem-3

  37. Problem-3

  38. Problem-3

  39. Problem-3

  40. Problem-3

  41. Problem-3

  42. Problem-3

  43. Problem-3

  44. Problem-3

  45. Problem-3

  46. Problem-3

  47. PROBLEM-4

  48. Problem-4 • Ref: section 3.3~3.6, 3.7 • Conditions • 32-bit little endian machine

  49. int mystery(int i) { if(i != 0) return i + mystery(i-1) return i; } int main(void) { return mystery(10); } Problem-4 08048324 <mystery> L1 8048324: 55 L2 : 89 e5 L3 : 83 ec 08 L4 ? : 83 7d 08 00 00

  50. int mystery(int i) { if(i != 0) return i + mystery(i-1) return i; } int main(void) { return mystery(10); } Problem-4 08048324 <mystery> L1 8048324: 55 L2 8048325: 89 e5 L3 8048327: 83 ec 08 L4 804832a: 83 7d 08 00 00

More Related