• 沒有找到結果。

# 2's column4's column8's column1's columnnnnn

N/A
N/A
Protected

Share "2's column4's column8's column1's columnnnnn"

Copied!
66
0
0

(1)

### Codes and number systems

Introduction to Computerp Yung-Yu Chuang

with slides by Nisan & Schocken (www.nand2tetris.org) and Harris & Harris (DDCA)

(2)

### Coding

• Assume that you want to communicate with your friend with a flashlight in a night what your friend with a flashlight in a night, what will you do?

light painting?

What’s the problem?

(3)

### Solution #1

• A: 1 blink B 2 bli k

• B: 2 blinks

• C: 3 blinks :

• Z: 26 blinks

• Z: 26 blinks

Wh t’ th bl ? What’s the problem?

• How are you? = 131 blinks

(4)

Hello

(5)

### Lookup

• It is easy to translate into Morse code than reverse Why?

reverse. Why?

(6)

(7)

### Lookup

Useful for

checking the checking the correctness/

d d

redundency

(8)

(9)

(10)

### What’s common in these codes?

• They are both binary codes.

(11)

### Binary representations

• Electronic Implementation

E t t ith bi t bl l t – Easy to store with bistable elements

– Reliably transmitted on noisy and inaccurate wires

0 1 0

2.8V 3.3V

0.0V 0.5V

(12)

(13)

## Number Systems

### • Decimal numbers

10's colum100's colum

1000's colu 1's column

537410 =

nmn

umn

### • Binary numbers

2's column4's column

8's column 1's column

11012 =

nn

n n

Chapter 1 <13>

(14)

## Number Systems

### • Decimal numbers

10's colum100's colum

1000's colu 1's column

537410 = 5 ? 103 + 3 ? 102 + 7 ? 101 + 4 ? 100

five

nmn

umn

three seven four

thousands hundreds tens ones

### • Binary numbers

2's column4's column

8's column 1's column

11012 = 1 ? 23 + 1 ? 22 + 0 ? 21 + 1 ? 20 = 1310

one eight

nn

n

one four

no two

one one

n

Chapter 1 <14>

(15)

### Binary numbers

• Digits are 1 and 0

( bi di it i ll d bit) (a binary digit is called a bit) 1 = true

0 = false

• MSB –most significant bit

• LSB –least significant bit

MSB LSB

• Bit numbering: 1 0 1 1 0 0 1 0 1 0 0 1 1 1 0 0

MSB LSB

A bit string could have different interpretations

0 15

• A bit string could have different interpretations

(16)

0

8

1

2

9

10

3

11

4

5

12

13

5

6

13

14

7

15

Chapter 1 <16>

(17)

## Powers of Two

0

8

1

2

9

10

3

11

4

5

12

13

5

6

13

14

7

9

15

Chapter 1 <17>

9

(18)

### Unsigned binary integers

• Each digit (bit) is either 1 or 0

• Each bit represents a power of 2: 1 1 1 1 1 1 1 1

27 26 25 24 23 22 21 20

Every binary number is a

f

sum of powers of 2

(19)

### Translating binary to decimal

Weighted positional notation shows how to Weighted positional notation shows how to

calculate the decimal value of each binary bit:

d (D 2n 1) (D 2n 2) (D 21) (D dec = (Dn-1  2n-1)  (Dn-2  2n-2)  ...  (D1  21)  (D0

 20)

D = binary digit

binary 00001001 = decimal 9:

(1  23) + (1  20) = 9

(20)

### binary

• Repeatedly divide the decimal integer by 2. Each remainder is a binary digit in the translated value:

remainder is a binary digit in the translated value:

37 = 100101 37 = 100101

(21)

## Number Conversion

### • Decimal to binary conversion: y

– Convert 100112 to decimal

### • Decimal to binary conversion:

– Convert 4710 to binary

Chapter 1 <21>

(22)

## Number Conversion

### • Decimal to binary conversion: y

– Convert 100112 to decimal

– 16×1 + 8×0 + 4×0 + 2×1 + 1×1 = 1910

### • Decimal to binary conversion:

– Convert 4710 to binary

32 1 + 16 0 + 8 1 + 4 1 + 2 1 + 1 1 101111 – 32×1 + 16×0 + 8×1 + 4×1 + 2×1 + 1×1 = 1011112

Chapter 1 <22>

(23)

## Binary Values and Range

### • N‐digit decimal number  g

– How many values?

– Range?Range?

– Example: 3‐digit decimal number:

### • N‐bit binary number

– How many values?

– Range:

– Example: 3‐digit binary number:

Chapter 1 <23>

(24)

## Binary Values and Range

### • N‐digit decimal number  g

How many values? 10N Range?  [0, 10N ‐ 1]

E l 3 di it d i l b

Example: 3‐digit decimal number:

103 = 1000 possible values

Range: [0, 999]

### • N‐bit binary number

How many values? 2N How many values? 2N Range: [0, 2N ‐ 1]

Example: 3‐digit binary number:p g y

23 = 8 possible values

Range: [0, 7] = [0002 to 1112]

Chapter 1 <24>

(25)

### Integer storage sizes

byte

16 8

Standard sizes: 16

32 word

doubleword

Standard sizes:

Practice: What is the largest unsigned integer that may be stored in 20 bits?

Practice: What is the largest unsigned integer that may be stored in 20 bits?

(26)

## Bits, Bytes, Nibbles…

### • Bits 10010110 10010110

least significant

bit most

significant bit

byte

bit bit

nibble

### • Bytes y CEBF9AD7

least significant

byte most

significant byte

Chapter 1 <26>

byte byte

(27)

10

20

30

30

Chapter 1 <27>

(28)

24

Chapter 1 <28>

(29)

## Estimating Powers of Two

24

4

20

2

30

Chapter 1 <29>

(30)

### Large measurements

• Kilobyte (KB), 210 bytes M b (MB) 220 b

• Megabyte (MB), 220 bytes

• Gigabyte (GB), 230 bytes

• Terabyte (TB), 240 bytes

• Petabyte

• Petabyte

• Exabyte Z tt b t

• Zettabyte

• Yottabyte

(31)

Hex Digit Decimal Equivalent Binary Equivalent

0 0

1 1

2 2

3 3

3 3

4 4

5 5

6 6

6 6

7 7

8 8

9 9

A 10

B 11

C 12

D 13

E 14

Chapter 1 <31>

F 15

(32)

Hex Digit Decimal Equivalent Binary Equivalent

0 0 0000

1 1 0001

2 2 0010

3 3 0011

3 3 0011

4 4 0100

5 5 0101

6 6 0110

6 6 0110

7 7 0111

8 8 1000

9 9 1001

A 10 1010

B 11 1011

C 12 1100

D 13 1101

E 14 1110

Chapter 1 <32>

F 15 1111

(33)

Chapter 1 <33>

(34)

## Translating binary to hexadecimal Translating binary to hexadecimal

• Each hexadecimal digit corresponds to 4 binary bits.

• Example: Translate the binary integer

• Example: Translate the binary integer

(35)

### power of 16:

dec = (D3  163) + (D2  162) + (D1  161) + (D0  160)

H 1234 l (1 163) + (2 162) + (3 161) + (4

• Hex 1234 equals (1  163) + (2  162) + (3  161) + (4

 160), or decimal 4,660.

• Hex 3BA4 equals (3Hex 3BA4 equals (3  16 ) + (11 16 ) + (10  16 )  163) + (11 * 162) + (10  161) + (4  160), or decimal 15,268.

(36)

## Hexadecimal to Binary Conversion

### • Hexadecimal to binary conversion: y

– Convert 4AF16 (also written 0x4AF) to binary

### • Hexadecimal to decimal conversion:

– Convert 0x4AF to decimal

Chapter 1 <36>

(37)

## Hexadecimal to Binary Conversion

### • Hexadecimal to binary conversion: y

– Convert 4AF16 (also written 0x4AF) to binary – 0100 1010 11112

### • Hexadecimal to decimal conversion: Hexadecimal to decimal conversion:

– Convert 4AF16 to decimal

– 162×4 + 161×10 + 160×15 = 119910

Chapter 1 <37>

(38)

### Powers of 16

Used when calculating hexadecimal values up to 8 digits long:

(39)

### Converting decimal to hexadecimal

decimal 422 = 1A6 hexadecimal

(40)

carries

11

3734 5168 +

carries 11

5168 +

8902

11 carries

1011 0011 +

1110

Chapter 1 <40>

(41)

Chapter 1 <41>

(42)

Chapter 1 <42>

(43)

## Overflow

### • See previous example of 11 + 6

Chapter 1 <43>

(44)

Divide the sum of two digits by the number base (16) Th ti t b th l d (16). The quotient becomes the carry value, and the remainder is the sum digit.

36 28 28 6A

1 1

36 28 28 6A

42 45 58 4B

78 6D 80 B5

78 6D 80 B5

Important skill: Programmers frequently add and subtract the addresses of variables and instructions

addresses of variables and instructions.

(45)

## Signed Binary Numbers

Chapter 1 <45>

(46)

### Signed integers

The highest bit indicates the sign. 1 = negative, 0 i i

0 = positive

sign bit sign bit

1 1 1 1 0 1 1 0

Negative

0 0 0 0 1 0 1 0 Positive

If the highest digit of a hexadecmal integer is > 7, the value is negative Examples: 8A C5 A2 9D

negative. Examples: 8A, C5, A2, 9D

(47)

## Sign/Magnitude Numbers

• 1 sign bit, N-1 magnitude bits

• Sign bit is the most significant (left-most) bit

– Positive number: sign bit = 0os t ve u be : s g b t 0 A:

### 

aN1,aN2,a a a2, ,1 0

### 

– Negative number: sign bit = 1

1

1 2 2 1 0

2

( 1) n 2

N N

n

a i

A ai

 

### 

• Example, 4-bit sign/mag representations of ± 6:

0 i

+6 = - 6 =

R f N bit i / it d b

• Range of an N-bit sign/magnitude number:

Chapter 1 <47>

(48)

## Sign/Magnitude Numbers

• 1 sign bit, N-1 magnitude bits

• Sign bit is the most significant (left-most) bit

– Positive number: sign bit = 0os t ve u be : s g b t 0 A:

### 

aN1,aN2,a a a2, ,1 0

### 

– Negative number: sign bit = 1

1

1 2 2 1 0

2

( 1) n 2

N N

n

a i

A ai

 

### 

• Example, 4-bit sign/mag representations of ± 6:

0 i

+6 = 0110 - 6 = 1110

R f N bit i / it d b

• Range of an N-bit sign/magnitude number:

[-(2N-1-1), 2N-1-1]

Chapter 1 <48>

(49)

## Sign/Magnitude Numbers

### • Problems:

– Addition doesn’t work, for example -6 + 6:

### 10100 (wrong!)

– Two representations of 0 (± 0):

Chapter 1 <49>

(50)

## Two’s Complement Numbers

### • Don’t have same problems as sign/magnitude p g g numbers:

– Single representation for 0

Chapter 1 <50>

(51)

### Two's complement notation

Steps:

Complement (reverse) each bit – Complement (reverse) each bit – Add 1

Note that 00000001 + 11111111 = 00000000

(52)

## “Taking the Two’s Complement”

### • Method:

1 Invert the bits 1. Invert the bits 2. Add 1

10

2

Chapter 1 <52>

(53)

## “Taking the Two’s Complement”

### • Method:

1 Invert the bits 1. Invert the bits 2. Add 1

10

2

1. 1100 2. + 1

1101 = -310

Chapter 1 <53>

(54)

1010

22

2

Chapter 1 <54>

(55)

## Two’s Complement Examples

1010

22

1. 1001 2. + 1

10102 = -610

2

### ?

1. 0110 2. + 1

01112 = 710, so 10012 = -710

Chapter 1 <55>

2 10, 2 10

(56)

### Binary subtraction

• When subtracting A – B, convert B to its two's complement

complement

• Add A to (–B)

0 1 0 1 0 0 1 0 1 0 – 0 1 0 1 1 1 0 1 0 0 1 1 1 1 1 Advantages for 2’s complement:

Advantages for 2’s complement:

• No two 0’s

• Sign bit

• Remove the need for separate circuits for add and sub

(57)

Chapter 1 <57>

(58)

Chapter 1 <58>

(59)

## Increasing Bit Width

### • Extend number from N to M bits (M > N) :

– Sign-extension – Zero-extensionZero extension

Chapter 1 <59>

(60)

## Sign‐Extension

### • Example 1:

4 bit representation of 3 = 0011 4-bit representation of 3 = 0011

8-bit sign-extended value: 00000011

### • Example 2:

4-bit representation of -5 = 1011

8-bit sign-extended value: 11111011

Chapter 1 <60>

(61)

## Zero‐Extension

### • Example 1:

4 bit value = 0011 = 3

4-bit value = 00112 = 310 8-bit zero-extended value: 00000011 = 310

### • Example 2:

4-bit value = 1011 = -510 8-bit zero-extended value: 00001011 = 1110

Chapter 1 <61>

(62)

## Number System Comparison

Number System Range

Unsigned [0, 2N-1]

Sign/Magnitude [-(2N-1-1), 2N-1-1]

1 1

Two’s Complement [-2N-1, 2N-1-1]

For example 4-bit representation:

-8 -7 -6 -5 -4 -3 -2 -1 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

For example, 4-bit representation:

1000 1001 1010 1011 1100 1101 1110 1111 0000 0001 0010 0011 0100 0101 0110 0111 Two's Complement

1000 1001 1010 1011 1100 1101 1110 1111 0000 0001 0010 0011 0100 0101 0110 0111

Unsigned

1000 1001 1010 1011 1100 1101 1110 1111 0000 0001 0010 0011 0100 0101 0110 0111 Two s Complement

1001 1000 1010 1011 1100 1101 1110

1111 0000

0001 0010 0011 0100 0101 0110 0111 Sign/Magnitude

Chapter 1 <62>

(63)

### Ranges of signed integers

The highest bit is reserved for the sign. This limits the range:

the range:

(64)

### Character

• Character sets

St d d ASCII(0 127) – Standard ASCII(0 – 127) – Extended ASCII (0 – 255)

ANSI (0 255) – ANSI (0 – 255)

– Unicode (0 – 65,535)

• Null-terminated String

– Array of characters followed by a null byte

• Using the ASCII table

– back inside cover of book

(65)
(66)

### Representing Instructions

int sum(int x, int y)

{ Alpha sum Sun sum PC sum

{

return x+y;

}

55 89 00

00 p

81 C3

– For this example, Alpha &

Sun use two 4-byte

E5 8B 45 30

42 01

E0 08 90

instructions

• Use differing numbers of instructions in other cases

0C 03 45 80

FA 6B

02 00 instructions in other cases 09

– PC uses 7 instructions with lengths 1, 2, and 3

08 89 EC

Diff t hi t t ll diff t

g , ,

bytes

• Same for NT and for Linux

EC 5D C3 Different machines use totally different instructions and encodings

• NT / Linux not fully binary compatible

3. Show the remaining statement on ad h in Proposition 5.27.s 6. The Peter-Weyl the- orem states that representative ring is dense in the space of complex- valued continuous

In addition that the training quality is enhanced with the improvement of course materials, the practice program can be strengthened by hiring better instructors and adding

Courtesy: Ned Wright’s Cosmology Page Burles, Nolette &amp; Turner, 1999?. Total Mass Density

The case where all the ρ s are equal to identity shows that this is not true in general (in this case the irreducible representations are lines, and we have an inﬁnity of ways

maintenance and repair works should be carried out by school and her maintenance agent(s) to rectify defect(s) as identified in routine and regular inspections. Examples of works

We may observe that the Riemann zeta function at integer values appears in the series expansion of the logarithm of the gamma function.. Several proofs may be found

• If the same monthly prepayment speed s is maintained since the issuance of the pool, the remaining principal balance at month i will be RB i × (1 − s/100) i. • It goes without

• If the same monthly prepayment speed s is maintained since the issuance of the pool, the remaining principal balance at month i will be RB i × (1 − s/100) i.. • It goes