970 likes | 1.08k Views
Learn about binary arithmetic, signed integer representation, bit shifting, and coding rules for calculations. Understand how to perform operations and handle overflows. Practice swap and abs operations in binary.
E N D
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; unsigned int ux = x; Problem-1 9 = 001 001 -9 = 110 110 + 1 = 110 111
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
Problem-2 • Ref: lab-1 • Conditions • The coding rules are same as lab1 • Legal ops: ! ~ & ^ | + << >> • Constants: <= 8 bits (0x0 ~ 0xFF)
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 }
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
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
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) }
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..
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))
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) }
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
Problem-4 • Ref: section 3.3~3.6, 3.7 • Conditions • 32-bit little endian machine
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
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