CS 101: Computer Programming and Utilizationcs101/2019.1/lectures/... · 2019-08-27 · Autumn 2019...

59
Autumn 2019 CS101@CSE IIT Bombay CS 101: Computer Programming and Utilization Puru with CS101 TAs and Staff Course webpage: https://www.cse.iitb.ac.in/~cs101/ Lecture 9: Number representation

Transcript of CS 101: Computer Programming and Utilizationcs101/2019.1/lectures/... · 2019-08-27 · Autumn 2019...

Page 1: CS 101: Computer Programming and Utilizationcs101/2019.1/lectures/... · 2019-08-27 · Autumn 2019 CS101@CSE IIT Bombay Representing Numbers • Example: 25 Decimal = 11001 binary

Autumn 2019 CS101@CSE IIT Bombay

CS 101:Computer Programming and

Utilization

Puruwith

CS101 TAs and Staff

Course webpage: https://www.cse.iitb.ac.in/~cs101/

Lecture 9: Number representation

Page 2: CS 101: Computer Programming and Utilizationcs101/2019.1/lectures/... · 2019-08-27 · Autumn 2019 CS101@CSE IIT Bombay Representing Numbers • Example: 25 Decimal = 11001 binary

Autumn 2019 CS101@CSE IIT Bombay

Number representation and variables

02/08/19 2

Page 3: CS 101: Computer Programming and Utilizationcs101/2019.1/lectures/... · 2019-08-27 · Autumn 2019 CS101@CSE IIT Bombay Representing Numbers • Example: 25 Decimal = 11001 binary

Autumn 2019 CS101@CSE IIT Bombay

why numbers?• problem solving using computers is about

problem solving on numbers• examples

– salaries, temperature, length, distance, voltage …– picture, language, characters, …

02/08/19 3

Page 4: CS 101: Computer Programming and Utilizationcs101/2019.1/lectures/... · 2019-08-27 · Autumn 2019 CS101@CSE IIT Bombay Representing Numbers • Example: 25 Decimal = 11001 binary

Autumn 2019 CS101@CSE IIT Bombay

Picture Representation and Reconstruction

0 0 0 1 1 1 0 0 0 0

0 0 1 0 0 0 1 1 0 0

0 1 0 0 0 0 0 0 1 0

1 0 0 0 0 0 0 0 1 0

1 0 1 0 0 0 1 0 0 1

1 0 0 0 0 0 0 0 0 1

1 0 0 1 1 1 0 0 1 0

0 1 0 0 0 0 0 0 1 0

0 0 1 0 0 0 1 1 0 0

0 0 0 1 1 1 0 0 0 0

(a) (b) (c)

Page 5: CS 101: Computer Programming and Utilizationcs101/2019.1/lectures/... · 2019-08-27 · Autumn 2019 CS101@CSE IIT Bombay Representing Numbers • Example: 25 Decimal = 11001 binary

Autumn 2019 CS101@CSE IIT Bombay

radix-based number systems

• Number systems with radix r has r symbols (including 0)• (x)r x is a string of symbols, r is the radix/base• key idea: position of a symbol determines it's value!

PLACE VALUE– Multiply from right to left by: r0, r1, r2, r3

, ... and then add

• decimal number system (r=10)• hexa-decimal number system (r=16)• binary (r=2)

02/08/19 5

Page 6: CS 101: Computer Programming and Utilizationcs101/2019.1/lectures/... · 2019-08-27 · Autumn 2019 CS101@CSE IIT Bombay Representing Numbers • Example: 25 Decimal = 11001 binary

Autumn 2019 CS101@CSE IIT Bombay

examplesdecimal hexa-decimal binary

26

0xf

255

16

0xffff

02/08/19 6

Page 7: CS 101: Computer Programming and Utilizationcs101/2019.1/lectures/... · 2019-08-27 · Autumn 2019 CS101@CSE IIT Bombay Representing Numbers • Example: 25 Decimal = 11001 binary

Autumn 2019 CS101@CSE IIT Bombay

examplesdecimal hexa-decimal binary

26 1a (0x1a) 10110

15 0xf 1111

255 0xff 11111111

16 0x10 00010000

65535 0xffff 1111111111111111

02/08/19 7

Page 8: CS 101: Computer Programming and Utilizationcs101/2019.1/lectures/... · 2019-08-27 · Autumn 2019 CS101@CSE IIT Bombay Representing Numbers • Example: 25 Decimal = 11001 binary

Autumn 2019 CS101@CSE IIT Bombay

number representation with computers

• all numbers represented in binary form• data types:

– int (32 bits)– unsigned int (32 bits)– float (32 bits)– double (64 bits)

• binary sequences in each have different formats

02/08/19 8

Page 9: CS 101: Computer Programming and Utilizationcs101/2019.1/lectures/... · 2019-08-27 · Autumn 2019 CS101@CSE IIT Bombay Representing Numbers • Example: 25 Decimal = 11001 binary

Autumn 2019 CS101@CSE IIT Bombay

two’s complement representation for integers

• For a n-bit integer (numbers to be stored in n bits)

• If x is positive: (0 <= x <= 2n-1 – 1)– Binary form of x

• If x is negative ( -2n-1 <= x < 0)– one’s complement of positive number + 1– E.g. -9 in 2's complement:

one’s complement (00001001) + 1 = 11110110 + 1 = 11110111– Binary form of 2n + x

100000000 - 000001001 = 11110111 = 247 decimal

Page 10: CS 101: Computer Programming and Utilizationcs101/2019.1/lectures/... · 2019-08-27 · Autumn 2019 CS101@CSE IIT Bombay Representing Numbers • Example: 25 Decimal = 11001 binary

Autumn 2019 CS101@CSE IIT Bombay

the two’s complement wrap-around

02/08/19 10

Zero is one position to the right of center

-1=1…1 0=0…0 Max=01…1Min=10…0

Page 11: CS 101: Computer Programming and Utilizationcs101/2019.1/lectures/... · 2019-08-27 · Autumn 2019 CS101@CSE IIT Bombay Representing Numbers • Example: 25 Decimal = 11001 binary

Autumn 2019 CS101@CSE IIT Bombay

why two’s complement?• single representation for zero• addition is same as subtraction!• examples:

8 + 4

8 - 4

4 - 8

02/08/19 11

Page 12: CS 101: Computer Programming and Utilizationcs101/2019.1/lectures/... · 2019-08-27 · Autumn 2019 CS101@CSE IIT Bombay Representing Numbers • Example: 25 Decimal = 11001 binary

Autumn 2019 CS101@CSE IIT Bombay

The bool type

• Old C++ used int to store Boolean values• But ANSI standard C++ offers a type called bool• bool tval = true, fval = false;• int ival = int(tval);• However, old bad habits still allowed

– if (37) { … }– bool bval = 37;

• Overall value unclear

02/08/19 12

Page 13: CS 101: Computer Programming and Utilizationcs101/2019.1/lectures/... · 2019-08-27 · Autumn 2019 CS101@CSE IIT Bombay Representing Numbers • Example: 25 Decimal = 11001 binary

Autumn 2019 CS101@CSE IIT Bombay

Bit array manipulation

• Fixed size integers are arrays of bits

• C++ lets you do bitwise Boolean algebra

• a & b (and), a | b (or), a^b (exor), ~b (not)

02/08/19 13

Page 14: CS 101: Computer Programming and Utilizationcs101/2019.1/lectures/... · 2019-08-27 · Autumn 2019 CS101@CSE IIT Bombay Representing Numbers • Example: 25 Decimal = 11001 binary

Autumn 2019 CS101@CSE IIT Bombay

Bitwise operators

• a & b (and), a | b (or), a^b (exor), ~b (not)

02/08/19 14

101101101001010110010100

&

101101101001010100100011

^

101101101001010110110111

|

0010001111011100~

Page 15: CS 101: Computer Programming and Utilizationcs101/2019.1/lectures/... · 2019-08-27 · Autumn 2019 CS101@CSE IIT Bombay Representing Numbers • Example: 25 Decimal = 11001 binary

Autumn 2019 CS101@CSE IIT Bombay

Left shift operator

• int c = 5; cout << (c << 2);

• Bits lost from the left (msb)• Zero bits inserted from the right (lsb)• Result is 20 (= 5 ´ 22)• Cheap way to multiply by powers of two

02/08/19 15

00000000,00000000,00000000,00010100

00000000,00000000,00000000,00000101

Page 16: CS 101: Computer Programming and Utilizationcs101/2019.1/lectures/... · 2019-08-27 · Autumn 2019 CS101@CSE IIT Bombay Representing Numbers • Example: 25 Decimal = 11001 binary

Autumn 2019 CS101@CSE IIT Bombay

Right shift operator

• c >> 2• Bits discarded to the right (lsb)• If msb of c was 0, then 0 bits injected from left

(msb)– 5 >> 2 gives 1

• If msb of c was 1 (c was negative) then 1 bits injected from left– -5 >> 2 gives -2 (work it out)– 0xfffffffb >> 2 gives 0xfffffffe

• Preserves sign of number02/08/19 16

Page 17: CS 101: Computer Programming and Utilizationcs101/2019.1/lectures/... · 2019-08-27 · Autumn 2019 CS101@CSE IIT Bombay Representing Numbers • Example: 25 Decimal = 11001 binary

Autumn 2019 CS101@CSE IIT Bombay

examples• generate powers of 2 using shift operator

• outputs of …int a = 1;cout << a;a = a << 31;cout << a;a = a >> 31;cout << a;

02/08/19 17

Page 18: CS 101: Computer Programming and Utilizationcs101/2019.1/lectures/... · 2019-08-27 · Autumn 2019 CS101@CSE IIT Bombay Representing Numbers • Example: 25 Decimal = 11001 binary

Autumn 2019 CS101@CSE IIT Bombay

Some applications of bit operations

• Is an int x odd or even?

• Remainder when divided by 8•

How many one bits in a 32-bit int?

02/08/19 18

Page 19: CS 101: Computer Programming and Utilizationcs101/2019.1/lectures/... · 2019-08-27 · Autumn 2019 CS101@CSE IIT Bombay Representing Numbers • Example: 25 Decimal = 11001 binary

Autumn 2019 CS101@CSE IIT Bombay

Some applications of bit operations

• Is an int x odd or even?– int isOdd = (x & 1);

• Remainder when divided by 8– int remain = (x & 7);– Faster than x % 8

• How many one bits in a 32-bit int?• Repeat 32 times:

– numOnes = numOnes + (x & 0x8000000);– x = x << 1;

02/08/19 19

In binary this looks like a one followed by 31 zeros

Page 20: CS 101: Computer Programming and Utilizationcs101/2019.1/lectures/... · 2019-08-27 · Autumn 2019 CS101@CSE IIT Bombay Representing Numbers • Example: 25 Decimal = 11001 binary

Autumn 2019 CS101@CSE IIT Bombay

fractions in binary

8 4 2 1 1/2 1/4 1/8 1/16

02/08/19 20

• powers on the right side of the point are negative:

• binary 0.1 = 0 + 1 x 2-1 = 0.5 in decimal

• in binary 0.11 = 0x 1 + 1 x 2-1 + 1 x 2-2

= 0.5 + 0.25 = 0.75 in decimal

Page 21: CS 101: Computer Programming and Utilizationcs101/2019.1/lectures/... · 2019-08-27 · Autumn 2019 CS101@CSE IIT Bombay Representing Numbers • Example: 25 Decimal = 11001 binary

Autumn 2019 CS101@CSE IIT Bombay

fractions and precision• represent decimal number 1.45 using 3 bits and

4 bits

02/08/19 21

Page 22: CS 101: Computer Programming and Utilizationcs101/2019.1/lectures/... · 2019-08-27 · Autumn 2019 CS101@CSE IIT Bombay Representing Numbers • Example: 25 Decimal = 11001 binary

Autumn 2019 CS101@CSE IIT Bombay

fractions and precision• represent decimal number 1.45 using 3 bits and

4 bits

• with 3 bits: 1.11 => 1.375• with 4 bits: 1.111 => 1.4375

• for actual storage as numbers all factions converted to mantissa + exponent form

• length of mantissa determines precission!

02/08/19 22

Page 23: CS 101: Computer Programming and Utilizationcs101/2019.1/lectures/... · 2019-08-27 · Autumn 2019 CS101@CSE IIT Bombay Representing Numbers • Example: 25 Decimal = 11001 binary

Autumn 2019 CS101@CSE IIT Bombay

Representing floating point numbers• Use an analogue of scientific notation:

• significand * 10exponent, e.g. 6.022 * 1022

• Significand (with a bit for sign) and exponent are in binary• significand * 2exponent

• Actual representation: more complex. “IEEE Floating Point Standard”

Costs how many bits to store

No of bits for mantissa

No of bits for exponent

float 32 24 8double 64 52 12

Page 24: CS 101: Computer Programming and Utilizationcs101/2019.1/lectures/... · 2019-08-27 · Autumn 2019 CS101@CSE IIT Bombay Representing Numbers • Example: 25 Decimal = 11001 binary

Autumn 2019 CS101@CSE IIT Bombay

example• representation of number 65 as a float• 65 in binary is 1000001• 65 in significand—exponent form

– 6.5 x 101

– 1000001– 1.000001 x 2110

– binary point moved 6 places to the right – 6 is 110 in binary

02/08/19 25

Page 25: CS 101: Computer Programming and Utilizationcs101/2019.1/lectures/... · 2019-08-27 · Autumn 2019 CS101@CSE IIT Bombay Representing Numbers • Example: 25 Decimal = 11001 binary

Autumn 2019 CS101@CSE IIT Bombay

example, number 65 as a float• 65 decimal in binary signifand-exponent form

– 1.000001 x 2110

• For float, – 23 bits for mantissa (+1 bit for sign)– 7 bits for exponent (+1 bit for sign)

• 65 decimal is stored as a float as follows,– decimal point is assumed after 2nd bit– 01000001000000000000000000000110

02/08/19 26

sign of mantissa sign of exponent

Page 26: CS 101: Computer Programming and Utilizationcs101/2019.1/lectures/... · 2019-08-27 · Autumn 2019 CS101@CSE IIT Bombay Representing Numbers • Example: 25 Decimal = 11001 binary

Autumn 2019 CS101@CSE IIT Bombay

more examples• 36 as a float

• 3.75 as float

02/08/19 27

Page 27: CS 101: Computer Programming and Utilizationcs101/2019.1/lectures/... · 2019-08-27 · Autumn 2019 CS101@CSE IIT Bombay Representing Numbers • Example: 25 Decimal = 11001 binary

Autumn 2019 CS101@CSE IIT Bombay

more examples• 36 as a float• 36 is 100010 in binary• 1.00010 x 2101

in binary significand (mantissa)-exponent form• 01000100000000000000000000000101

02/08/19 28

Page 28: CS 101: Computer Programming and Utilizationcs101/2019.1/lectures/... · 2019-08-27 · Autumn 2019 CS101@CSE IIT Bombay Representing Numbers • Example: 25 Decimal = 11001 binary

Autumn 2019 CS101@CSE IIT Bombay

more examples• 3.75 as a float• integral part is 3: 011 in binary• fractional part is .75 which is .11 in binary

– 1*(1/2) + 1 * (1/4) = 0.5 + 0.25 = 0.75• 3.75 decimal is 11.11 binary• convert to significand-exponent form

– 1.111 x 21

– Store in 32-bit float

02/08/19 29

Page 29: CS 101: Computer Programming and Utilizationcs101/2019.1/lectures/... · 2019-08-27 · Autumn 2019 CS101@CSE IIT Bombay Representing Numbers • Example: 25 Decimal = 11001 binary

Autumn 2019 CS101@CSE IIT Bombay

more examples• 3.76

• 0.75

• 3450– 1.10101111010 x 21011

02/08/19 30

Page 30: CS 101: Computer Programming and Utilizationcs101/2019.1/lectures/... · 2019-08-27 · Autumn 2019 CS101@CSE IIT Bombay Representing Numbers • Example: 25 Decimal = 11001 binary

Autumn 2019 CS101@CSE IIT Bombay

more examples

• Let us represent the number 3450 = 3.45 x 103

• First: Convert to binary:

• 3450 = 211+ 210+ 28 + 26+ 25+24 +23 + 21

• Thus 3450 in binary = 110101111010

• 3450 in significand-exponent notation: how?

• 1.10101111010 x 21011

− 10 in binary is 2 in decimal

− 1011 in binary is 11 in decimal, we have to move the

"binary point" 11 places to the right

− as float

01101011110100000000000000001011

11 10 9 8 7 6 5 4 3 2 1 0

1 1 0 1 0 1 1 1 1 0 1 0

Page 31: CS 101: Computer Programming and Utilizationcs101/2019.1/lectures/... · 2019-08-27 · Autumn 2019 CS101@CSE IIT Bombay Representing Numbers • Example: 25 Decimal = 11001 binary

Autumn 2019 CS101@CSE IIT Bombay

precision, big numbers• int, unsigned int, long, long unsigned int

• float, double, long double

• why does double have higher precision than float?

02/08/19 32

Page 32: CS 101: Computer Programming and Utilizationcs101/2019.1/lectures/... · 2019-08-27 · Autumn 2019 CS101@CSE IIT Bombay Representing Numbers • Example: 25 Decimal = 11001 binary

Autumn 2019 CS101@CSE IIT Bombay

different Needs, different Variable Types

• The keyword long : says, I need to store bigger or more precise numbers, so give me more than usual space.

• long unsigned int: Likely 64 bits will be allocated

• long double: likely 96 bits will be allocated

unsigned int telephone_number;

float velocity;

float mass, acceleration;

long unsigned int crypto_password;

long double more_precise_vaule;

02/08/19 33

Page 33: CS 101: Computer Programming and Utilizationcs101/2019.1/lectures/... · 2019-08-27 · Autumn 2019 CS101@CSE IIT Bombay Representing Numbers • Example: 25 Decimal = 11001 binary

Autumn 2019 CS101@CSE IIT Bombay

the Const Keyword

• const double pi = 3.14;

• The keyword const means : value assigned once cannot be

changed

• Useful in readability of a program

• area = pi * radius * radius;

• reads better than

• area = 3.14 * radius * radius;

Page 34: CS 101: Computer Programming and Utilizationcs101/2019.1/lectures/... · 2019-08-27 · Autumn 2019 CS101@CSE IIT Bombay Representing Numbers • Example: 25 Decimal = 11001 binary

Autumn 2019 CS101@CSE IIT Bombay02/08/19 35

Page 35: CS 101: Computer Programming and Utilizationcs101/2019.1/lectures/... · 2019-08-27 · Autumn 2019 CS101@CSE IIT Bombay Representing Numbers • Example: 25 Decimal = 11001 binary

Autumn 2019 CS101@CSE IIT Bombay

The Hardware(A very high level glimpse)

• How do we store numbers in hardware?• How is an instruction expressed in hardware?• How is it executed?• How do we output the numbers?

Page 36: CS 101: Computer Programming and Utilizationcs101/2019.1/lectures/... · 2019-08-27 · Autumn 2019 CS101@CSE IIT Bombay Representing Numbers • Example: 25 Decimal = 11001 binary

Autumn 2019 CS101@CSE IIT Bombay

Digital Circuits - Operations

• Building blocks of computers• Circuits have wires that carry current,

and are at a certain electric potential.

• Digital circuits: interpret electrical potential/voltage as numbers.

• Simplest convention− Voltage above 5 volt = number 1

− Voltage between 0 and 0.2 volt = number 0

− Circuit designed so that voltage will never be between 0.2 and 5 volt, hence no ambiguity.

Copyright © 1999-2000 Michael Stutz [email protected]

An inverter circuit

Page 37: CS 101: Computer Programming and Utilizationcs101/2019.1/lectures/... · 2019-08-27 · Autumn 2019 CS101@CSE IIT Bombay Representing Numbers • Example: 25 Decimal = 11001 binary

Autumn 2019 CS101@CSE IIT Bombay

Digital Circuits - Storage

• Capacitors (like batteries) can store electrical charges• Charge stored on a capacitor may also denote numbers

− Capacitor has low charge = value 0 stored on it.− Capacitor has high charge = value 1 stored on it.− Once charge is stored on a capacitor, it persists. Memory

• Building blocks of DRAMs (Dynamic Random Access Memory)

Page 38: CS 101: Computer Programming and Utilizationcs101/2019.1/lectures/... · 2019-08-27 · Autumn 2019 CS101@CSE IIT Bombay Representing Numbers • Example: 25 Decimal = 11001 binary

Autumn 2019 CS101@CSE IIT Bombay

Representing Numbers• How to represent numbers using this capability?• Key idea : Binary number system

– Represent all numbers using only 1's and 0's– Also called "Bits": "Binary digits"

Page 39: CS 101: Computer Programming and Utilizationcs101/2019.1/lectures/... · 2019-08-27 · Autumn 2019 CS101@CSE IIT Bombay Representing Numbers • Example: 25 Decimal = 11001 binary

Autumn 2019 CS101@CSE IIT Bombay

Representing Numbers

• Example: 25 Decimal = 11001 binary

– Use 5 capacitors

– Store high charge on 1st, 2nd, 5th,

and low charge on 3rd, 4th

• To transmit 25 from one part of the computer

to another

– Use 5 wires and raise the wires to appropriate

voltages at one end.

– Sense the voltages at the other end

Key idea:Store each bit of the number on a separate capacitor

Page 40: CS 101: Computer Programming and Utilizationcs101/2019.1/lectures/... · 2019-08-27 · Autumn 2019 CS101@CSE IIT Bombay Representing Numbers • Example: 25 Decimal = 11001 binary

Autumn 2019 CS101@CSE IIT Bombay

Bits, bytes, half-words, words

• Bit = 1 capacitor/wire• byte = 8 capacitors/wires• half-word = 16 capacitors/wires• word = 32 capacitors/wires• double word = 64 capacitors/wires

Page 41: CS 101: Computer Programming and Utilizationcs101/2019.1/lectures/... · 2019-08-27 · Autumn 2019 CS101@CSE IIT Bombay Representing Numbers • Example: 25 Decimal = 11001 binary

Autumn 2019 CS101@CSE IIT Bombay

Organization of a computer

Page 42: CS 101: Computer Programming and Utilizationcs101/2019.1/lectures/... · 2019-08-27 · Autumn 2019 CS101@CSE IIT Bombay Representing Numbers • Example: 25 Decimal = 11001 binary

Autumn 2019 CS101@CSE IIT Bombay

Memory

• Organized into bytes (groups of 8 capacitors)

• Memories of present day computers contain few Gigabytes, Giga=230

• Each byte in the memory is assigned a distinct number, or an address

• In a memory with N bytes, the addresses go from 0 to N-1

0 0 0 0 1 1 0 1 01

2

3

4

5 1 0 1 1 1 1 0 1

6

7

8

9

Page 43: CS 101: Computer Programming and Utilizationcs101/2019.1/lectures/... · 2019-08-27 · Autumn 2019 CS101@CSE IIT Bombay Representing Numbers • Example: 25 Decimal = 11001 binary

Autumn 2019 CS101@CSE IIT Bombay

Memory Ports

• Memory has 3 ports: address port, data port, control port.

• Address port consists of log N wires. (N = number of bytes in memory)

• You can place numbers 0..N-1 on address port

• Control Port may be just 1 wire. – Wire = 0: Memory to perform read

operation.– Wire = 1: Memory to perform write

operation.• Data port will have w wires, where

w is a multiple of 8. Say w=8m.

Page 44: CS 101: Computer Programming and Utilizationcs101/2019.1/lectures/... · 2019-08-27 · Autumn 2019 CS101@CSE IIT Bombay Representing Numbers • Example: 25 Decimal = 11001 binary

Autumn 2019 CS101@CSE IIT Bombay

Write Operation• Control Port must be set to 1.• If A is placed on the address

port, and D on data port, then D will be stored in the m bytes starting at byte A.

• (Remember that the data port had 8m wires, and so m bytes are available on the data port)

Page 45: CS 101: Computer Programming and Utilizationcs101/2019.1/lectures/... · 2019-08-27 · Autumn 2019 CS101@CSE IIT Bombay Representing Numbers • Example: 25 Decimal = 11001 binary

Autumn 2019 CS101@CSE IIT Bombay

Write Operation

0 1

10010

11101000

"Write the number 11101000 (232) into the location number 10010 (18)

Page 46: CS 101: Computer Programming and Utilizationcs101/2019.1/lectures/... · 2019-08-27 · Autumn 2019 CS101@CSE IIT Bombay Representing Numbers • Example: 25 Decimal = 11001 binary

Autumn 2019 CS101@CSE IIT Bombay

Read Operation

• Control Port must be set to 0.

• If A is placed on the address port, the m bytes starting at byte A will appear on the data port

• (Data port has 8m wires, and so m bytes will fit on the data port)

Page 47: CS 101: Computer Programming and Utilizationcs101/2019.1/lectures/... · 2019-08-27 · Autumn 2019 CS101@CSE IIT Bombay Representing Numbers • Example: 25 Decimal = 11001 binary

Autumn 2019 CS101@CSE IIT Bombay

Read Operation

1

10010

11101000

"Read from the location number 10010 (18)0

Page 48: CS 101: Computer Programming and Utilizationcs101/2019.1/lectures/... · 2019-08-27 · Autumn 2019 CS101@CSE IIT Bombay Representing Numbers • Example: 25 Decimal = 11001 binary

Autumn 2019 CS101@CSE IIT Bombay

Arithmetic Unit

• Ports: Input1, Input2, Output, Control• Typically Input1, Input2, Output will consist of w wires,

w = size of memory data port• Control = several wires. Number appearing on the control

wires will say what operation should be performed• 1 cycle after values are placed on Control, the Output will take

the commanded value: sum, product, …

Page 49: CS 101: Computer Programming and Utilizationcs101/2019.1/lectures/... · 2019-08-27 · Autumn 2019 CS101@CSE IIT Bombay Representing Numbers • Example: 25 Decimal = 11001 binary

Autumn 2019 CS101@CSE IIT Bombay

Peripherals: keyboard, screen, disk…

• Also have control port and data port like organization.

• Depending upon value placed on control port, the peripheral decides what to do with the value on the data port, or itself places values on the data port.

data

control

data

control

control

data

Page 50: CS 101: Computer Programming and Utilizationcs101/2019.1/lectures/... · 2019-08-27 · Autumn 2019 CS101@CSE IIT Bombay Representing Numbers • Example: 25 Decimal = 11001 binary

Autumn 2019 CS101@CSE IIT Bombay

Control Unit• Tells other parts of the computer what to

do.– Sends numbers on control wires of each unit

• The control unit decides what to tell other units by reading a “machine language program” from the memory of the computer.

Page 51: CS 101: Computer Programming and Utilizationcs101/2019.1/lectures/... · 2019-08-27 · Autumn 2019 CS101@CSE IIT Bombay Representing Numbers • Example: 25 Decimal = 11001 binary

Autumn 2019 CS101@CSE IIT Bombay

Machine language program

OPCODE OPERAND1 OPERAND2 OPERAND3

• Program = Sequence of instructions• Instruction = sequence of numbers

– First number is OPERATION CODE (OPCODE). This is the code that tells the Control Unit what OPERATION should do.

– Subsequent numbers are OPERANDS. These are "arguments" to the operation

• i386• x86_64• PowerPC

Page 52: CS 101: Computer Programming and Utilizationcs101/2019.1/lectures/... · 2019-08-27 · Autumn 2019 CS101@CSE IIT Bombay Representing Numbers • Example: 25 Decimal = 11001 binary

Autumn 2019 CS101@CSE IIT Bombay

Example57 100 200 300

• operation code 57 might mean:– Interpret the 3 numbers following 57 as addresses.– Read the words at the first two addresses and send them

to the Arithmetic unit.– Command the arithmetic unit to perform multiplication by

sending appropriate number on its control wires.– Store the result from the arithmetic unit into the word at

the third address

Page 53: CS 101: Computer Programming and Utilizationcs101/2019.1/lectures/... · 2019-08-27 · Autumn 2019 CS101@CSE IIT Bombay Representing Numbers • Example: 25 Decimal = 11001 binary

Autumn 2019 CS101@CSE IIT Bombay

Machine language progam• Program = Sequence of instructions• Instruction = sequence of numbers, first of which is an �operation code� which denotes what operation to perform

• Example: operation code 57 might mean:– Interpret the 3 numbers following 57 as addresses.– Read the words at the first two addresses and send them to the Arithmetic unit.– Command the arithmetic unit to perform multiplication by sending appropriate number on its control wires.– Store the result from the arithmetic unit into the word at the third address

• The sequence 57, 100, 200, 300 is an instruction that would cause the product of the numbers stored in addresses 100, 200 to be stored in the address 300.

• The operation codes are defined by the computer designer.

– She will assign a different code for each operation that she would like the computer to perform.– Example: 58 might mean the same thing as above, except that the numbers would be added.

Page 54: CS 101: Computer Programming and Utilizationcs101/2019.1/lectures/... · 2019-08-27 · Autumn 2019 CS101@CSE IIT Bombay Representing Numbers • Example: 25 Decimal = 11001 binary

Autumn 2019 CS101@CSE IIT Bombay

Control unit operation• Control unit must be told where the machine

language program is in memory

• The control unit then fetches the instructions constituting the program, interprets the codes, and performs the required operation

• After one instruction is fetched and executed, it fetches the next instruction and repeats the process

Page 55: CS 101: Computer Programming and Utilizationcs101/2019.1/lectures/... · 2019-08-27 · Autumn 2019 CS101@CSE IIT Bombay Representing Numbers • Example: 25 Decimal = 11001 binary

Autumn 2019 CS101@CSE IIT Bombay

Putting it together

Memory

Arthimetic Unit

Control Unit

Bus (network)

01..10...

100....

200

...300

57 100 200 300

10

1. Control unit is told the address of the instruction to fetch (e.g. instruction is at location 10)

2. A read operation is performed on memory location 10

3. instruction57 100 200 300is now "loaded" into the control unit

57 100 200 300

289

345

Read Write

1 0

Page 56: CS 101: Computer Programming and Utilizationcs101/2019.1/lectures/... · 2019-08-27 · Autumn 2019 CS101@CSE IIT Bombay Representing Numbers • Example: 25 Decimal = 11001 binary

Autumn 2019 CS101@CSE IIT Bombay

Putting it together

Memory

Arthimetic Unit

Control Unit

Bus (network)

01..10...

100....

200

...300

57 100 200 300

100

1. Now control unit performs a read operation of address 100

2. The number 289 is sent to input 1 of arithmetic unit

289

289

345

Read Write

1 0

Page 57: CS 101: Computer Programming and Utilizationcs101/2019.1/lectures/... · 2019-08-27 · Autumn 2019 CS101@CSE IIT Bombay Representing Numbers • Example: 25 Decimal = 11001 binary

Autumn 2019 CS101@CSE IIT Bombay

Putting it together

Memory

Arthimetic Unit

Control Unit

Bus (network)

01..10...

100....

200

...300

57 100 200 300

200

1. Now control unit performs a read operation of address 200

2. The number 345 is sent to input 2 of arithmetic unit

345

289

345

Read Write

1 0

Page 58: CS 101: Computer Programming and Utilizationcs101/2019.1/lectures/... · 2019-08-27 · Autumn 2019 CS101@CSE IIT Bombay Representing Numbers • Example: 25 Decimal = 11001 binary

Autumn 2019 CS101@CSE IIT Bombay

Putting it together

Memory

Arthimetic Unit

Control Unit

Bus (network)

01..10...

100....

200

...300

57 100 200 300

1. Now control unit instructs the arithmetic unit to perform a multiply operation (the "control" wires are set to the operation code of "multiply")

2. Control unitstores the product (289 x 345 = 99705) temporarily inside its own unit

289

345

Read Write

0 0

x

x 99705

99705

Page 59: CS 101: Computer Programming and Utilizationcs101/2019.1/lectures/... · 2019-08-27 · Autumn 2019 CS101@CSE IIT Bombay Representing Numbers • Example: 25 Decimal = 11001 binary

Autumn 2019 CS101@CSE IIT Bombay

Putting it together

Memory

Arthimetic Unit

Control Unit

Bus (network)

01..10...

100....

200

...300

57 100 200 300

300

1. Now control unit performs a write operation on address 300

2. The number 99705 is sent on the data port of memory and 300 is sent on the address port of the memory

99705

289

345

Read Write

0 1Instruction execution is COMPLETE

99705