CS计算机代考程序代写 mips Integer Numbers

Integer Numbers

Representing Multi-bit Values
■Number bits from right (0) to left (n-1)
◆ just a convention — could be left to right, but must be consistent
■Use brackets to denote range:
D[l:r] denotes bit l to bit r, from left to right
MSB (Most
LSB (Least
significant bit)
15 0
A = 0101001101010101
significant bit)
A[2:0] = 101 May also see A<14:9>, especially in hardware block
diagrams.
A[14:9] = 101001
CSE12 Winter 2021
2

Hexadecimal Number System
■ Base (radix): 16
■ Digits (symbols): 0-9, A–F (a-f)
■ In C/MIPS: leading “0x” (e.g., 0xA3)
■ Sometimes: leading “x” (e.g., “x3000”)
■ Hexadecimal is also known as “hex” for short
CSE12 Winter 2021
Hex
A
B
C
D
E
F
Decimal (unsigned format)
10
11
12
13
14
15
3

Examples of Converting Hex to Decimal
■ 0xA316 = A*161 + 3*160 = 10*16 + 3*1
= 160 + 3
= 163
■ 0x3E816 = Do it at Home!! (answer is 1000)
CSE12 Winter 2021
4

Decimal To Binary Conversion: Method 1
■ Divide decimal value by 2 until the value is 0
■ Write the remainders starting from the least significant position (the right to
the left)
CSE12 Winter 2021
5

Decimal To Binary Conversion: Method 1
■ Divide decimal value by 2 until the value is 0
■ Write the remainders starting from the least significant position (the right to
the left)
CSE12 Winter 2021 6

Knowing The Powers Of Two
MUST MEMORIZE ATLEAST TILL 210!
20
21
22
23
24
25
26
27
28
29
210
1
2
4
8
16
32
64
128
256
512
1024
CSE12 Winter 2021
7

Binary to Octal Conversion
■ Group into 3 starting at least significant bit ◆ Why 3?
◆ Add leading 0 as needed
■ Write one octal digit for each group
CSE12 Winter 2021
8

Binary to Octal Conversion: Examples
■ 100010 42
■ 10101
Octal
0
1
Binary
000
CSE12 Winter 2021
111 (binary) 7 (octal)
110 (binary) (octal)
2
3
4
5
6
7
9
001
010
011
100
101
110
111

Binary to Octal Conversion: Examples
■ 100010 42
■ 010101 25
Octal
0
1
Binary
000
CSE12 Winter 2021
111 (binary) 7 (octal)
110 (binary) 6 (octal)
2
3
4
5
6
7
10
001
010
011
100
101
110
111

Octal to Binary Conversion
■ Write down the 3-bit binary code for each octal digit
■ Example: ◆ 047
Octal
0
1
2
3
4
5
6
7
Binary
000
001
010
011
100
101
110
111
CSE12 Winter 2021
11

Octal to Binary Conversion
■ Write down the 3-bit binary code fo◆r each octal digit
■ Example: 000 100 111
Octal
0
0
4
7
1
2
3
4
5
6
7
Binary
000
001
010
011
100
101
110
111
CSE12 Winter 2021
12

Binary to Hex Conversion
■ Group into 4 starting at least significant bit ◆ Why 4?
◆ Add leading 0 if needed
■ Write one hex digit for each group
CSE12 Winter 2021
13

Binary to Hex Conversion: Examples
1001 1110 0111 0000 (binary) 9 E 7 0 (hex)
1 1111 1010 0011 (binary) (hex)
Hex
0
1
2
3
4
5
6
7
Bin
0000
0001
0010
0011
0100
0101
0110
0111
Hex
8
9
A
B
C
D
E
F
14
Bin
1000
1001
1010
1011
1100
1101
1110
1111
CSE12 Winter 2021

Binary to Hex Conversion: Examples
1001 1110 0111 0000 (binary) 9 E 7 0 (hex)
0001 1111 1010 0011 (binary) 1 F A 3 (hex)
Hex
0
1
2
3
4
5
6
7
Bin
0000
0001
0010
0011
0100
0101
0110
0111
Hex
8
9
A
B
C
D
E
F
15
Bin
1000
1001
1010
1011
1100
1101
1110
1111
CSE12 Winter 2021

Hex to Binary Conversion
■ Write down the 4-bit binary code for each hex digit
■ Example:
◆ 0x 3 9 C 8
0011 1001 1100 1000
Know this in your sleep
Hex
0
1
2
3
4
5
6
7
Bin
0000
0001
0010
0011
0100
0101
0110
0111
Hex
8
9
A
B
C
D
E
F
16
Bin
1000
1001
1010
1011
1100
1101
1110
1111
CSE12 Winter 2021

Hex to Binary Conversion
■ Write down the 4-bit binary code for each hex digit
■ Example:
◆0x3 C8
0011 1001 1000
9
1100
0
1
2
3
4
5
6
7
Bin
0000
0001
0010
0011
0100
0101
0110
0111
Hex
8
9
A
B
C
D
E
F
17
Bin
1000
1001
1010
1011
Know this in y ur sleep
1100
1101
1110
1111
CSE12 Winter 2021
Hex
o

Conversion Table
Decimal
(unsigned format)
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
Hexadecimal
0
1
2
3
4
5
6
7
8
9
A
B
C
D
E
F
0
1
2
3
4
5
6
7
10
11
12
13
14
15
16
17
0000
0001
0010
0011
0100
0101
0110
0111
1000
1001
1010
1011
1100
1101
1110
CSE12 Winter 2021
15
Octal
Binary
1111
18

A practical example: HTML Color Codes
RGB
2 digit Hex value for Red, 2 digit Hex value for Green, 2 digit Hex value for Blue
CSE12 Winter 2021
19

Remember Base Conversion
Three cases:
I. From any base b to base 10
II. From base 10 to any base b
III. From any base b to any other base c
CSE12 Winter 2021
20

More Conversions
■ Hex → Octal
◆ Doitin2steps
◆ Hex → binary → octal
■ Decimal → Hex
◆ Doitin2steps
◆ Decimal → binary → hex
■ So why use hex and octal and not just binary and decimal?
CSE12 Winter 2021
21

Some calculations…
◆ Inbase10?10 -1 e.g.n=2,100-1=99 n
■ What is the largest unsigned number that we can represent in n digits… n
◆ Inbase2?2 -1 ◆ In octal? 8 -1
n
◆ In hex? 16n -1
◆ Inbase7?7 -1 n
◆ Inbaseb?bn -1
■ How many different unique numbers can we represent with n digits in base
b?
bn (remember 0 is a number 2, inclusion)
CSE12 Winter 2021
22

How many bits are necessary?
■ How many bits are necessary? ◆ log2(444) = 8.8…
◆ ceil(8.8) = 9
■ Where is log2 on my calculator? logy(x) = log(x)/log(y)
log2(x) = log(x)/log(2)
CSE12 Winter 2021
23

Floating Point, FP Addition, FP Multiplication

(Unsigned) Fixed Point Numbers
 With binary integers, we assume each position is a power of the base 2  … 8’s, 4’s, 2’s, 1’s
 This is actually: …23, 22, 21, 20
 What if we extend this with a negative power?  2-1, 2-2, 2-3, etc.
� Negative exponent means it is denominator
� 1⁄2, 1⁄4, 1⁄8, etc.
 0.5’s, 0.25’s, 0.125’s, etc.
CSE12 Winter 2021
25

Fixed Point Example 1
 Convert to  12.75
CSE12 Winter 2021
a 4+4 bit fixed point number
26

Fixed Point Example 1
 Convert to a 4+4 bit fixed point number
? ? ? ?. ? ? ? ?
3 2 1 0. -1 -2 -3 -4
 0.75=3⁄4=1⁄2+1⁄4 = .11
 
CSE12 Winter 2021
27

Fixed Point Example 1
 Convert to  12 = 8 + = 11
CSE12 Winter 2021
a 4+4 bit fixed point number 4
00
28

Alternative way for finding out Binary of Fractions from a given Decimal value
■ Split the value into 2 parts: Integer part + Fractional part
■ Find the binary representation of
the integral part by repeatedly divide the value by 2 (to obtain the powers of 2n)
■ Find the binary representation of
the fractional part by repeatedly multiply the value by 2 (to obtain the powers of 2-n)
CSE12 Winter 2021
29

Fixed Point Example 1:Alternative solution
 Convert to a 4+4 bit fixed point number  12=8+4=11002
CSE12 Winter 2021 30

Fixed Point Example 2
 Given 4+4 bit fixed point number, what is the decimal value?
 0110.1010
CSE12 Winter 2021
31

Fixed Point Example 2
 Given 4+4 bit fixed point number, what is the decimal value?
 0110.1010 = 4 + 2 + 1⁄2 +1⁄8
CSE12 Winter 2021
32

Fixed Point Example 2
 Given 4+4 bit fixed point number, what is the decimal value?  0110.1010 = 4 + 2 + 1⁄2 +1⁄8
= 6.625
CSE12 Winter 2021
33

(Unsigned) Fixed Point Precision
 This assumes that the fraction point begins at a fixed bit location  Example, 32-bit number, 8-bits decimal
� 24 bits are used for the integer part
� 8 bits are used for the decimal part
 You can think of each number as multiplied by a scale (28), shifts left 8
bits
 What is the most accurate you can represent this fixed-point number(precision)?
 Example, 2-8 = 1/256 = 0.00390625
CSE12 Winter 2021
34

Repeating Fractions
 Convert to  5.2
CSE12 Winter 2021
a 4+4 bit fixed point number
35

Repeating Fractions
 Convert to  5 = 101
CSE12 Winter 2021
a 4+4 bit fixed point number
36

Repeating Fractions
 Convert to a 4+4 bit fixed point number  .2 = ????
1⁄2 = 0.5
1⁄4 = 0.25
1⁄8 = 0.125 1/16 = 0.0625
CSE12 Winter 2021
37

Repeating Fractions
 Convert to a 4+4 bit fixed point number  .2=???
CSE12 Winter 2021 38

Repeating Fractions
 We saw that 0.210 cannot be exactly expressed in 4+4 fixed point since we will lose precision
 So what is the most accurate we can represent it then in binary 4+4 ?
 We know for certain, .00112 =0.187510< 0.210  Next largest possible binary number in 4+4 is .0011+ .0001 =0.0100= 0.2510  But 0.2-0.1875 < 0.25 – 0.2  Thus 0.0011 is the closest we can accurately convey 0.210 in binary 4+4  Therefore, 5.210= 0101.0011 in 4+4 CSE12 Winter 2021 39 Repeating Fractions  Convert to a 4+4 bit fixed point number  5.2 ≈ 0101.0011 CSE12 Winter 2021 40 (Unsigned) Fixed Point Range  Consider again this 24 + 8 bit fixed point number.  What is the maximum value?  All 1’s for integer, all 1’s for fraction  223 <– >2-8
 What is the minimum value?
0
CSE12 Winter 2021
41

What about signed fixed point?
 It is possible to have a fixed-point two’s complement number  Will not cover in CSE12
 Could also have a signed-magnitude fixed-point number  MSB represents positive (0) or negative (1)
CSE12 Winter 2021
42

How does arithmetic work with fixed point?
 Addition is the same!
 If the two numbers have the same scale
 Subtraction is the same!
 If the two numbers have the same scale
CSE12 Winter 2021
43

Example adding two fixed point numbers
 0011.1100 + 0000.0110  What are the values?
 What is the result?
CSE12 Winter 2021
44

Example adding two fixed point numbers
 0011.1100 + 0000.0110  What are the values?
 0011.1100 = 1+2+1⁄2 +1⁄4
 What is the result?
CSE12 Winter 2021
45

Example adding two fixed point numbers
 0011.1100 + 0000.0110
 What are the values?
 0011.1100 = 1+2+1⁄2 +1⁄4
= 3.75
 What is the result?
CSE12 Winter 2021
46

Example adding two fixed point numbers
 0011.1100 + 0000.0110
 What are the values?
 0011.1100 = 1+2+1⁄2 +1⁄4
= 3.75 0000.0110 = 0 + 1⁄4 +1⁄8 = .375
 What is the result?
CSE12 Winter 2021
47

Example adding two fixed point numbers
 0011.1100 + 0000.0110
 What are the values?
 0011.1100 = 1+2+1⁄2 +1⁄4
= 3.75 0000.0110 = 0 + 1⁄4 +1⁄8 = .375
 What is the result? 0011.1100
+ 0000.0110 100.0010
CSE12 Winter 2021
48

Example adding two fixed point numbers
 0011.1100 + 0000.0110
 What are the values?
 0011.1100 = 1+2+1⁄2 +1⁄4
= 3.75 0000.0110 = 0 + 1⁄4 +1⁄8 = .375
 What is the result? 0011.1100
+ 0000.0110
100.0010 = 4 + 1⁄8 = 4.125
CSE12 Winter 2021
49

Floating Point Numbers
Consider: A x 10B, where A is one digit
A B
0 any 1..9 0 1..9 1 1..9 2
1..9 -1 1..9 -2
A x 10B
0
1 .. 9
10 .. 90 100 .. 900
0.1 .. 0.9 0.01 .. 0.09
How to do scientific notation in binary? Standard: IEEE 754 Floating-Point
CSE12 Winter 2021
50

Real numbers
 Our decimal system handles non-integer real numbers by adding yet another symbol – the decimal point (.) to make a fixed point notation:
 e.g. 3,456.78 = 3*103 + 4*102 + 5*101 + 6*100 + 7*10-1 + 8.10-2
 The floating point, or scientific, notation allows us to represent very large and very small numbers (integer or real), with as much or as little precision as needed:
 Unit of electric charge e = 1.602 176 462 * 10-19 Coul.  Volume of universe = 1 * 1085 cm3
� the two components of these numbers are called the mantissa and the exponent
CSE12 Winter 2021
51

Real numbers in floating point

We mimic the decimal floating point notation to create a “hybrid” binary floating point number:
 We first use a “binary point” to separate whole numbers from fractional numbers to make a fixed point notation:
� e.g. 00011001.110 = 1*24 + 1*23 + 1*20 + 1*2-1 + 1*2-2 => 25.75 (2-1 = 0.5 and 2-2 = 0.25, etc.)
 We then “float” the binary point:
� 00011001.110 => 1.1001110 x 24
mantissa = 1.1001110, exponent = 4
 Now we have to express this without the extra symbols ( x, 2, . ) � by convention, we divide the available bits into three fields:
sign, mantissa, exponent
CSE12 Winter 2021
52

IEEE-754 fp numbers Single Precision
32 bits: 1 8 bits
23 bits
s
biased exp.
fraction
N = (-1)s x 1.fraction x 2(biased exp. – 127)
 Sign: 1 bit
 Mantissa: 23 bits
 We “normalize” the mantissa by dropping the leading 1 and recording only its
fractional part
 Exponent: 8 bits
 In order to handle both +ve and -ve exponents, we add 127 to the actual exponent to create a “biased exponent”:
� 2-127 => biased exponent = 0000 0000 (= 0)
� 20 => biased exponent = 0111 1111 (= 127)
� 2+127 => biased exponent = 1111 1110 (= 254)
CSE12 Winter 2021
53

IEEE-754 fp numbers
 Example:
� 25.75 => 00011001.110 => 1.1001110 x 24
� sign bit = 0 (+ve)
� normalized mantissa (fraction) = (1.)100 1110 0000 0000 0000 0000
� biased exponent = 4 + 127 = 131 => 1000 0011
� so 25.75 => 0 1000 0011 100 1110 0000 0000 0000 0000 =>
0x41CE0000
CSE12 Winter 2021
54

How to convert 64.2 into IEEE SP
 Get a binary representation for 64.2
 Binary of left of radix/decimal point is: 1000000
 Binary of right of radix/decimal:
� Successively multiply value by 2 and compare to 1 • 0.2×2=0.4lessthan1so… 0
• 0.4×2=0.8lessthan1so… 0
• 0.8×2=1.6g.t.1so… 1
• 0.6×2=1.2g.t.1so… 1 • 0.2×2=0.4 0 • 0.4×2=0.8 0 • 0.8×2=1.6 1 • 0.6×2=1.2 1
CSE12 Winter 2021
55

(continued)
 Binary for .2: .0011 0011 0011 0011
 64.2 is: 1000000.0011001100110011…
 Normalize binary form
 Produces: 1.0000000011 X 26
CSE12 Winter 2021
56

(continued)
 3. Turn true exponent into bias 127  E = 6 + 127 = 133 = 10000101
 4. Put it together:
 23-bit F is: (1.)0000000011 0011 0011 0011 0
 SEFis: S=0
 E = 10000101
 F = 00000000110011001100110
 In hex:
 0x42806666
0
100 0010 1
000 0000 0110 0110 0110 0110
CSE12 Winter 2021
57

Convert IEEE SP to real
 What is the decimal value for this SP FP number 0xC228 0000?
 Convert to binary
 Break into S, E, F:
 E is 10000100 = 132 decimal: 132 – 127= 5
 F is (1.)0101000…
 Move decimal over 5: 101010.000…  S E F is -42!
1
100 0010 0
010 1000 0000 0000 0000 0000
CSE12 Winter 2021
58

Convert IEEE SP to Real
 0x3F800000
CSE12 Winter 2021
59

Convert IEEE SP to Real
 0x3F800000
0011 1111 1000 0000 0000 0000 0000 0000
CSE12 Winter 2021
60

Convert IEEE SP to Real
 0x3F800000
0
011 1111 1
S=0
000 0000 0000 0000 0000 0000
E = 0111 1111 = 127 –
F = 1.0
0x3F8 = 1 in single precision floating point CSE12 Winter 2021
127
61

Take Home Practice
 What is 47.62510 in SP FP format?
 What is 0x44ed8000 as real number?
CSE12 Winter 2021
62

Check your Practice
 http://www.h-schmidt.net/FloatConverter/IEEE754.html
CSE12 Winter 2021
63

IEEE-754 Double Precision
 Double precision (64 bit) floating point 64 bits: 1 11 bits 52 bits
N = (-1)s x 1.fraction x 2(biased exp. – 1023)
biased
exp.
fraction
CSE12 Winter 2021
s
64

How to represent 0, NaN, +/- Infinity?
 Values represented by convention:
 Infinity (+ and -): exponent = 255 (1111 1111) and
fraction = 0
 NaN (not a number): exponent = 255 and fraction ≠ 0
 Zero (0): exponent = 0 and fraction = 0
� Note: exponent = 0 => fraction is de-normalized (i.e. no
hidden 1)
CSE12 Winter 2021
65