12302010004 火雨辰. it’s a pair of unsigned multiplication, and each value is represented by...

27
HOMEWORK1 12302010004 火火火

Upload: aldous-gibson

Post on 19-Jan-2016

227 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 12302010004 火雨辰.  It’s a pair of unsigned Multiplication, and each value is represented by 4-bits unsigned integer. Please solve the equation step by

HOMEWORK112302010004火雨辰

Page 2: 12302010004 火雨辰.  It’s a pair of unsigned Multiplication, and each value is represented by 4-bits unsigned integer. Please solve the equation step by

Q1 BINARY CALCULATION

It’s a pair of unsigned Multiplication, and each value is represented by 4-bits unsigned integer. Please solve the equation step by step! Or you can’t get marks of this question.

0011 * x + 0100 * y = 1001 (a) 0100 * x + 0011 * y = 1000 (b)

Page 3: 12302010004 火雨辰.  It’s a pair of unsigned Multiplication, and each value is represented by 4-bits unsigned integer. Please solve the equation step by

Q1 BINARY CALCULATION

3x+4y=9 4x+3y=8 x=5/7,y=12/7

Page 4: 12302010004 火雨辰.  It’s a pair of unsigned Multiplication, and each value is represented by 4-bits unsigned integer. Please solve the equation step by

Q1 BINARY CALCULATION

Solution1:(b)–(a) 0001*x+1111*y=1111

(c)(c)*2 0010*x+1110*y=1110 (d)(c)+(d) 0011*x+1101*y=1101

(e)(e) –(a) 1001*y=0100 y=0100

0011*x=1001 x=0011

Page 5: 12302010004 火雨辰.  It’s a pair of unsigned Multiplication, and each value is represented by 4-bits unsigned integer. Please solve the equation step by

Q1 BINARY CALCULATION

Solution2:x=abcd(2) y=efgh(2)

(a) (a+b+e)(b+c+h)(a+d)d = 1001(b) (e+f+c)(f+g+d)(g+h)h = 1000From right to left solve the function

Page 6: 12302010004 火雨辰.  It’s a pair of unsigned Multiplication, and each value is represented by 4-bits unsigned integer. Please solve the equation step by

Q1 BINARY CALCULATION

Solution3:(3x+4y) mod 16= 9(4x+3y) mod 16= 83x + 4y = 9 + 16a4x + 3y = 8 + 16bx,y ϵ [0,15]x = 3, y = 4

Page 7: 12302010004 火雨辰.  It’s a pair of unsigned Multiplication, and each value is represented by 4-bits unsigned integer. Please solve the equation step by

Q2 OPERATIONS

Given A and B with hexadecimal expression 0xDC and 0xE3 respectively.Calculate the values of the following expressions:A & B 0xC0A && B 0x01A | B 0xFFA || B 0x01~ A |~ B 0x3F! A || B 0x01A&&~ B 0x01

Page 8: 12302010004 火雨辰.  It’s a pair of unsigned Multiplication, and each value is represented by 4-bits unsigned integer. Please solve the equation step by

Q2 OPERATIONS

X X<<4 X>>3 (Logical) X>>3 (Arithmetic)

Hex Hex Hex Hex

0xF3 0x30 0x1E 0xFE

0x0E 0xE0 0x01 0x01

0xCD 0xD0 0x19 0xF9

0xA9 0x90 0x15 0xF5

Page 9: 12302010004 火雨辰.  It’s a pair of unsigned Multiplication, and each value is represented by 4-bits unsigned integer. Please solve the equation step by

Q3 ENDIAN

Consider the following calls to show_bytes() from Figure2.4 page 76 (English text book)int val = 0x1234ABCD;byte_pointer valp = (byte_pointer) &val;

show_bytes(valp,1); /* A. */show_bytes(valp,2) /* B. */show_bytes(valp,3) /* C. */

Page 10: 12302010004 火雨辰.  It’s a pair of unsigned Multiplication, and each value is represented by 4-bits unsigned integer. Please solve the equation step by

Q3 ENDIAN

Indicate which of the values would be printed by each call on a little-endian and on a big-endian machine:A.Little endian: _CD_____; Big endian:

__12____;B.Little endian: _CDAB___; Big endian:

__1234_;C.Little endian: _CDAB34_; Big endian:

_1234AB;

Page 11: 12302010004 火雨辰.  It’s a pair of unsigned Multiplication, and each value is represented by 4-bits unsigned integer. Please solve the equation step by

Q3 ENDIAN

Try to write an C function to determine whether the machine running this code is big endian or small endian. The function should have no more than 2 lines. (It’s okey to do this step by step, ignore the restriction when you start)Int BIG_small(){_________(1)___________;_________(2)___________;} // return 0 if small, 1 for big

Page 12: 12302010004 火雨辰.  It’s a pair of unsigned Multiplication, and each value is represented by 4-bits unsigned integer. Please solve the equation step by

Q3 ENDIAN

Int i = 1; Return !(*((char *)&i));

Page 13: 12302010004 火雨辰.  It’s a pair of unsigned Multiplication, and each value is represented by 4-bits unsigned integer. Please solve the equation step by

Q3 ENDIAN

Fault 1: Byte_pointer y = &0x01000000; return y[0];

Fault 2: Char *p = &0x1100; return p;

Page 14: 12302010004 火雨辰.  It’s a pair of unsigned Multiplication, and each value is represented by 4-bits unsigned integer. Please solve the equation step by

Q3 ENDIAN

Fault 3: Unsigned char* x = 0x100; Return (int)x[0];

Fault 4: Int a = 0x0100; Return ((char *)&a)[0];

Page 15: 12302010004 火雨辰.  It’s a pair of unsigned Multiplication, and each value is represented by 4-bits unsigned integer. Please solve the equation step by

Q3 ENDIAN

Fault 5: Int x = 0xFF; Return !(&x)[0];

Fault 6: Unsigned short x = 0x00010000; Return (unsigned char)x;

Page 16: 12302010004 火雨辰.  It’s a pair of unsigned Multiplication, and each value is represented by 4-bits unsigned integer. Please solve the equation step by

Q3 ENDIAN

Fault 7: Unsigned int x =1; Return (int)((char*)&x);

Fault 8: Int x = 0x01; Return !(x ^ show_byte(x,3));

Page 17: 12302010004 火雨辰.  It’s a pair of unsigned Multiplication, and each value is represented by 4-bits unsigned integer. Please solve the equation step by

Q3 ENDIAN

Fault 9: Unsigned char c = 0x1200; Retur c[0];

Fault 10: Short int a = 0x1122; Return (char)a == 0x11;

Page 18: 12302010004 火雨辰.  It’s a pair of unsigned Multiplication, and each value is represented by 4-bits unsigned integer. Please solve the equation step by

Q3 ENDIAN

Fault 11: Int x = 0x12345678; Return (&x)[0]==0x12;

Fault 12: Int x = 1; Return ((char*) &x)[0];

Page 19: 12302010004 火雨辰.  It’s a pair of unsigned Multiplication, and each value is represented by 4-bits unsigned integer. Please solve the equation step by

Q4 ENCODINGS

Expression 2’s-complement Signed 8-bit Decimal

3+2 0000 0101 5 99/2

(-23)/2 round to zero-127-1 125+3

125>>3

Suppose all the numbers are 8‐bit long. Give the result of the following expressions of C language in full 8‐bit 2’s complement form and signed decimal form.

Page 20: 12302010004 火雨辰.  It’s a pair of unsigned Multiplication, and each value is represented by 4-bits unsigned integer. Please solve the equation step by

Q5 SIGN VS. UNSIGN

So easy

(unsigned)-7 -8

-7 (unsigned)-8

Page 21: 12302010004 火雨辰.  It’s a pair of unsigned Multiplication, and each value is represented by 4-bits unsigned integer. Please solve the equation step by

Q6 CASTING

The following C code pieces are executed on a typical 32‐bit big endian machine with 2’s complement encoding. Please give the output.

int main() { int x = 257; char y = -10; int z = 128; char a = (char)x; short b =(short)y; unsigned short ud = (unsigned short)b; char c = (char)z; unsigned int w = (c > 0) ? 0 : 1; printf("a=%d,b=%d,ud=%x,c=%d,w=%d\n",a,b,ud,c,w); }

Page 22: 12302010004 火雨辰.  It’s a pair of unsigned Multiplication, and each value is represented by 4-bits unsigned integer. Please solve the equation step by

Q6 CASTING

Correct: A=1, b=-10, ud=0xfff6,c =-128,w=1

Incorrect: Ud = 65526 Ud = 0xfffffff6

What about little endian?

Page 23: 12302010004 火雨辰.  It’s a pair of unsigned Multiplication, and each value is represented by 4-bits unsigned integer. Please solve the equation step by

Q7 INTEGER ARITHMETIC

easy

Page 24: 12302010004 火雨辰.  It’s a pair of unsigned Multiplication, and each value is represented by 4-bits unsigned integer. Please solve the equation step by

Q8 FLOAT NUM

Normalized: V = (-1)sign * (1.fraction) * 2e-bias Where bias = 2K-1-1

Denormalized: V = (-1)sign * (0.fraction) * 2E where E = 1 – (2K-1-1)

Page 25: 12302010004 火雨辰.  It’s a pair of unsigned Multiplication, and each value is represented by 4-bits unsigned integer. Please solve the equation step by

Q8 FLOAT NUM

Convert decimal fraction into binary form 0.09375 * 2 = 0.1875 ------- 0 0.1875 * 2 = 0.375 ------- 0 0.375 * 2 = 0.75 ------- 0 0.75 * 2 = 1.5 ------- 1 0.5 * 2 = 1 ------- 1 0.09375 = (0.00011)2

Page 26: 12302010004 火雨辰.  It’s a pair of unsigned Multiplication, and each value is represented by 4-bits unsigned integer. Please solve the equation step by

Q8 FLOAT NUM

(12.625)10 = (1100.101)2

= 1.100101 * 23

(0x4c18)16 = (10000110)2

= 1.000011 * 27

Page 27: 12302010004 火雨辰.  It’s a pair of unsigned Multiplication, and each value is represented by 4-bits unsigned integer. Please solve the equation step by

Q8 FLOAT NUM

Sum = 1.000011 * 27 + 1.100101 * 23 = 1.0010010101 * 27

= 1.00101 * 27 = 0x4C50 Mul = 1.000011 * 1.100101 * 210

= 1.101001101111* 210 = 1.10101 * 210 = 0x5350

or=12.625 * 134 = 1691.75 =

0x5350