Coding • Assume that you want to communicate with your friend with a flashlight in a night, what will you do? Codes and number systems

Introduction to Computer Yung-Yu Chuang light painting? What’s the problem?

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

Solution #1 Solution #2: Morse code • A: 1 blink • B: 2 blibliknks • C: 3 blinks : • Z: 26 blinks

Wha t’s the pro blem ? • How are you? = 131 blinks

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

Lookup Braille Useful for checking the correctness/ redddundency Braille What’s common in these codes? • They are both binary codes.

Binary representations Number systems • Electronic Implementation – Easy to store w ith bitblbistable elemen ts – Reliably transmitted on noisy and inaccurate wires

0 1 0 3.3V 2.8V

0.5V 0.0V Number Systems Number Systems

numbers • Decimal numbers 1000's col 1000's colum10's column 1's 1000's col 1000's colum 10's column 1's 100's colu100's 100's colu 100's u u m m n n mn mn n n

3 2 1 0 537410 = 537410 = 5 ? 10 + 3 ? 10 + 7 ? 10 + 4 ? 10 five three seven four thousands hundreds tens ones • Binary numbers • Binary numbers 8's colum 8's colum 2's colum 1's 8's colum 8's colum 2's colum 1's 4's colum 4's colum 4's n n n n n n n n

3 2 1 0 11012 = 11012 = 1 ? 2 + 1 ? 2 + 0 ? 2 + 1 ? 2 = 1310 one one no one eight four two one

Chapter 1 <13> Chapter 1 <14>

Binary numbers Powers of Two • Digits are 1 and 0 •20 =•28 = (a binary dig it is ca lle d a ) •21 = •29 = 1 = true • 22 = • 210 = 0 = false 3 11 • MSB –most significant bit •2= •2 = • LSB –least significant bit •24 = •212 = 5 13 MSB LSB • 2 = • 2 = • : 1 0 1 1 0 0 1 0 1 0 0 1 1 1 0 0 •26 = •214 = 15 0 •27 =•215 = • A bit string could have different interpretations

Chapter 1 <16> Powers of Two Unsigned binary integers

•20 = 1 • 28 = 256 • Each digit (bit) is either 1 or 0 •21 = 2 •29 = 512 • Each bit represents a power of 2: 1 1 1 1 1 1 1 1 27 26 25 24 23 22 21 20 • 22 =4= 4 • 210 = 1024 •23 = 8 •211 = 2048 •24 = 16 •212 = 4096 Every is a 5 13 • 2 ==32 32 • 2 = 8192 sum of powers •26 = 64 •214 = 16384 of 2 •27 = 128 •215 = 32768 • HandHandy to memoriz e u p to 29

Chapter 1 <17>

Translating unsigned decimal to Translating binary to decimal binary • Repeatedly divide the decimal integer by 2. Each Weighted shows how to remainder is a binary digit in the translated value: calculate the decimal value of each binary bit: n-1 n-2 1 dec = (Dn-1  2 )  (Dn-2  2 )  ...  (D1  2 )  (D0  20) 37 = 100101 D = binary digit

binary 00001001 = decimal 9: (1  23) + (1  20) = 9 Number Conversion Number Conversion

• Decimal to binary conversion: • Decimal to binary conversion:

– Convert 100112 to decimal – Convert 100112 to decimal

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

• Decimal to binary conversion: •Decilbiimal to binary conversi on: – Convert 47 to binary – Convert 4710 to binary 10 – 32×11+16 + 16×00+8 + 8×1 ++4 4×11+2 + 2×11+1 + 1×1 = 101111 2

Chapter 1 <21> Chapter 1 <22>

Binary Values and Range Binary Values and Range

• N‐digit decimal number • N‐digit decimal number N – How many values? – How many values? 10 – N – Range? [0, 10 ‐ 1] Range? – ElExample: 3‐dig it ddiecima l number: – Example: 3‐digit decimal number: • 103 = 1000 possible values • Range: [0, 999]

• N‐bit binary number • N‐bit binary number – How many values? 2N – How many values? – Range: [0, 2N ‐ 1] – Range: – Example: 3‐digit binary number: – Example: 3‐digit binary number: • 23 = 8 possible values • Range: [0, 7] = [0002 to 1112]

Chapter 1 <23> Chapter 1 <24> Integer storage sizes , ,

8 • Bits 10010110 Standard sizes: word 16 most least doubleword 32 significant significant quadword 64 bit bit • Bytes & Nibbles byte 10010110

• Bytes CEBF9AD7 most least significant significant Practice: What is the largest unsigned integer that may be stored in 20 bits? bits? byte byte

Chapter 1 <26>

Large Powers of Two Estimating Powers of Two

• 210 = 1 kilo ≈ 1000 (1024) • What is the value of 224?

• 220 = 1 mega ≈ 1 million (1,048,576)

• 230 = 1 giga ≈ 1 billion (1,073 ,741 ,824) • How many values can a 32‐bit variable represent?

Chapter 1 <27> Chapter 1 <28> Estimating Powers of Two Large measurements

• What is the value of 224? •Kilobyte (KB),210 bytes 20 ‐ 24 × 220 ≈ 16 million • MbMegabyte (MB), 2 bytes • Gigabyte (GB), 230 bytes • Terabyte (TB), 240 bytes • How many values can a 32‐bit variable • Petabyte represent? •Exabyte 2 30 ‐2 × 2 ≈ 4 billion • ZttbtZettabyte • Yottabyte

Chapter 1 <29>

Hexadecimal Numbers Hexadecimal Numbers

Hex Digit Decimal Equivalent Binary Equivalent Hex Digit Decimal Equivalent Binary Equivalent 00 0 0 0000 11 1 1 0001 22 2 2 0010 3 3 3 3 0011 44 4 4 0100 55 5 5 0101 6 6 6 6 0110 77 7 7 0111 88 8 8 1000 99 9 9 1001 A10 A 10 1010 B11 B 11 1011 C12 C 12 1100 D13 D 13 1101 E14 E 14 1110 F15 F 15 1111

Chapter 1 <31> Chapter 1 <32> Hexadecimal Numbers Translating binary to hexadecimal • Base 16 • Each hexadecimal digit corresponds to 4 binary bits. • Shorthand for binary • Example: Translate the binary integer 000101101010011110010100 to hexadecimal:

Chapter 1 <33>

Converting hexadecimal to decimal Hexadecimal to Binary Conversion

• Multiply each digit by its corresponding • Hexadecimal to binary conversion:

power of 16: – Convert 4AF16 (also written 0x4AF) to binary 3 2 1 0 dec = (D3  16 ) + (D2  16 ) + (D1  16 ) + (D0  16 )

• Hex 1234 equa ls (1  163)+(2) + (2  162)+(3) + (3  161)+(4) + (4 • Hexadecimal to decimal conversion: 0  16 ), or decimal 4,660. – Convert 0x4AF to decimal

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

Chapter 1 <36> Hexadecimal to Binary Conversion Powers of 16

• Hexadecimal to binary conversion: Used when calculating hexadecimal values up to

– Convert 4AF16 (also written 0x4AF) to binary 8 digits long:

– 0100 1010 11112

• Hexadecimal to decimal conversion:

– Convert 4AF16 to decimal 2 1 0 –16×4 + 16 ×10 + 16 ×15 = 119910

Chapter 1 <37>

Converting decimal to hexadecimal Addition

• Decimal 11 carries 3734 + 5168 8902

•Binary 11 carries 1011 decimal 422 = 1A6 hexadecimal + 0011 1110

Chapter 1 <40> Binary Addition Examples Binary Addition Examples

• Add the following • Add the following 1 1001 1001 4-bit binary 4-bit binary + 0101 + 0101 numbers numbers 1110

111 • Add the following 1011 • Add the following 1011 4-bit binary + 0110 4-bit binary + 0110 numbers numbers 10001 Overflow!

Chapter 1 <41> Chapter 1 <42>

Overflow Hexadecimal addition

•Diggyital systems o perate on a fixed number of Divide the sum of two digits by the number base (16). The quoti ent becomes the carry va lue, an d bits the remainder is the sum digit. • Overflow: when result is too big to fit in the available number of bits 1 1 • See previous example of 11 + 6 36 28 28 6A 42 45 58 4B 78 6D 80 B5

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

Chapter 1 <43> Signed Binary Numbers Signed integers

•Sigggn/Magnitude Numbers The highest bit indicates the . 1 = negative, 0 = pos it ive • Two’s Complement Numbers 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

Chapter 1 <45>

Sign/Magnitude Numbers Sign/Magnitude Numbers

• 1 sign bit, N-1 magnitude bits • 1 sign bit, N-1 magnitude bits • Sign bit is the most significant (left-most) bit • Sign bit is the most significant (left-most) bit

– Posi tive nu mbe r: s ig n b it = 0 A:,,,,aaNN12 aaa 210 – Posi tive nu mbe r: s ig n b it = 0 A:,,,,aaNN12 aaa 210 – Negative number: sign bit = 1 n2 – Negative number: sign bit = 1 n2 an1 i an1 i Aa(1) i 2 Aa(1) i 2 i0 i0 • Example, 4-bit sign/mag representations of ± 6: • Example, 4-bit sign/mag representations of ± 6: +6 = +6 = 0110 - 6 = - 6 = 1110 • RfRange of an N-bit si gn/ magnit ud e numb er: • RfRange of an N-bit si gn/ magnit ud e numb er: [-(2N-1-1), 2N-1-1]

Chapter 1 <47> Chapter 1 <48> Sign/Magnitude Numbers Two’s Complement Numbers

• Problems: • Don’t have same ppggroblems as sign/magnitude – Addition doesn’t work, for example -6 + 6: numbers: – Addition works 1110 – Single representation for 0 + 0110 10100 (wrong!)

– Two representations of 0 (± 0): 1000 0000

Chapter 1 <49> Chapter 1 <50>

Two's complement notation “Taking the Two’s Complement” Steps: •Flippg the sign of a two’s comp lement number – Complement (reverse) each bit – Add 1 • Method: 1. Invert the bits 2. Add 1

• Exampllihifle: Flip the sign of 310 = 00112

Note that 00000001 + 11111111 = 00000000

Chapter 1 <52> “Taking the Two’s Complement” Two’s Complement Examples

•Flippg the sign of a two’s comp lement number • Take the two’s complement of 610 = 01102 • Method: 1. Invert the bits 2. Add 1

• Exampllihifle: Flip the sign of 310 = 00112 1. 1100 • What is the decimal value of 10012? 2. + 1

1101 = -310

Chapter 1 <53> Chapter 1 <54>

Two’s Complement Examples Binary subtraction

• Take the two’s complement of 6 = 0110 • When subtracting A – B, convert B to its two's 10 2 complement 1. 1001 • Add A to (–B) 2. + 1 0 1 0 1 0 0 1 0 1 0 10102 = -610 – 0 1 0 1 1 1 0 1 0 0 1 1 1 1 1 • What is the decimal value of the two’s Advantages for 2’s complement: complement number 10012? 1. 0110 • No two 0’s 2. + 1 • Sign bit

01112 = 710, so 10012 = -710 • Remove the need for separate circuits for add and sub Chapter 1 <55> Two’s Complement Addition Two’s Complement Addition

• Add 6 + (-6))g using two’s comp lement • Add 6 + (-6))g using two’s comp lement numbers numbers 111 0110 0110 + 1010 + 1010 10000 • Add -2 + 3 usingpg two’s complement numbers • Add -2 + 3 usingpg two’s complement numbers 111 1110 1110 + 0011 + 0011 10001 Chapter 1 <57> Chapter 1 <58>

Increasing Bit Width Sign‐Extension

• Extend number from N to M bits (M > N): •Sign bit copied to msb’s – Sign-extension • Number value is same – Zero-extension • Example 1: – 4-bit representation of 3 = 0011 – 8-bit sign-extended value: 00000011 • EElxample 2: – 4-bit representation of -5 = 1011 – 8-bit sign-extended value: 11111011

Copyright © 2012 Elsevier Chapter 1 <59> Chapter 1 <60> Zero‐Extension Number System Comparison

• Zeros copied to msb’s Number System Range N • Value changes for negative numbers Unsigned [0, 2 -1] Sign/Magnitude [-(2N-1-1), 2N-1-1] Two’s Complement [-2N-1, 2N-1-1] • Example 1:

– 4-bit value = 00112 = 310 For example , 4-bit representation:

– 8-bit zero-extended value: 00000011 = 310 -8 -7 -6 -5 -4 -3 -2 -1 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 • EElxample 2: Unsigned 0000 0001 0010 0011 0100 0101 0110 0111 1000 1001 1010 1011 1100 1101 1110 1111 – 4-bit value = 1011 = -510 1000 1001 1010 1011 1100 1101 1110 1111 0000 0001 0010 0011 0100 0101 0110 0111 Two' s Complement – 8-bit zero-extended value: 00001011 = 1110 0000 1111 1110 1101 1100 1011 1010 1001 0001 0010 0011 0100 0101 0110 0111 Sign/Magnitude 1000

Chapter 1 <61> Chapter 1 <62>

Ranges of signed integers Character The highest bit is reserved for the sign. This limits •Character sets the range: – Stand ar d ASCII(0 – 127) – Extended ASCII (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 Representing Instructions int sum(int x, int y) { Alpha sum Sun sum PC sum return x+y; 00 81 55 } 00 C3 89 30 E0 E5 – For this example, Alpha & 42 08 8B Sun use two 4-byte 01 90 45 instructions 80 02 0C • Use differing numbers of FA 00 03 instructions in other cases 6B 09 45 08 – PC uses 7 instructions 89 with lengg,,ths 1, 2, and 3 EC bytes 5D • Same for NT and for Linux C3 • NT / Linux not fully binary Differen t mac hines use to ta lly different compatible instructions and encodings