0% found this document useful (0 votes)
14 views

cs101 Lecture2

The document discusses how computers work by representing real-world problems as operations on numeric data. It explains how pictures, weather prediction, and language can be converted to sequences of numbers and analyzed using algorithms. Basic computer hardware components like circuits, memory, and processors are also introduced at a high level.

Uploaded by

Rushin Shah
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

cs101 Lecture2

The document discusses how computers work by representing real-world problems as operations on numeric data. It explains how pictures, weather prediction, and language can be converted to sequences of numbers and analyzed using algorithms. Basic computer hardware components like circuits, memory, and processors are also introduced at a high level.

Uploaded by

Rushin Shah
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 54

CS 101:

Computer Programming and


Utilization
About These Slides

• Based on Chapter 2 of the book


An Introduction to Programming Through C++
by Abhiram Ranade (Tata McGraw Hill, 2014)

• Original slides by Abhiram Ranade


– First update by Varsha Apte
– Second update by Uday Khedker
How Does A Computer Work
Very simply:
A computer is a large* circuit with parts that

• read numbers from external world (Input)


• store numbers (Storage)
• perform arithmetic on numbers (Processing)
• send numbers to external world (Output)

*How large can large be?


• Physically small: ~1000 mm2
• Logically large: ~109 transistors
How To Use A Computer To
Solve Real-Life Problems?
1. Express the problem as calculations on numbers
2. Think of a solution in terms of the computations need
to be performed, possibly repeatedly and
conditionally
= program
3. Feed the
• the data in term of numbers, and
• the program to the computer
4. Get the output on a screen or elsewhere (?)
Outline

• Examples of expressing real life problems as numerical


problems
− Picture processing
− Predicting the weather
− Understanding and processing language
• Algorithms and Programs
• High level design of a computer
− Digital circuits
− Parts of a computer
− Stored program, compilation
“What is in this picture?”

Can we ask this question to a computer and get an answer?


Start With Baby Steps

• Black and white picture representation and


comprehension
How to represent black and white
pictures?

• Suppose the picture is 10cm x 10cm


• Divide it into 0.1 mm x 0.1 mm squares
• The number of squares (or pixels) is 1000 x 1000
• If a square is mostly white, represent it by 0
• If a square is mostly black, represent it by 1
• Thus, our picture = 1 million numbers!
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)


Image Recognition
Is this a picture of a vertical line?
• Input:
A sequence of 1 million numbers (0 or 1) representing a
10cm x 10cm black & white picture
• What property does the sequence need to have if it is to
contain a vertical line somewhere?
• All 0s, except for 1s at positions
i, i+1000, i+2000, i+3000, i+4000,…
for some i

A question about a picture converted into a question


about numbers!
Remarks
• Better representation if picture divided into
more cells.
• Pictures with different “gray levels”: use
numbers 0, 0.1, …, 1.0 to represent level of
darkness rather than just 0, 1.
• Pictures with colours: picture = 3 sequences
– sequence for red component,
– sequence for blue component,
– sequence for green component
• Add up the colours to get the actual colour.
More Recognition problems

• Is the picture largely red in colour?

• Is there a square in the picture?

• Are two pictures similar?

• ...
Coming back to: Does the picture
contain a Chameleon?
This question will need to be expressed as:

• Does the sequence of numbers representing the picture


contain a subsequence satisfying certain properties?
• Which properties?
− Enormous ingenuity needed to specify.
− Very difficult problem
− Main concern of a deep subject called Computer
Vision
Weather Prediction
Divide the surface of the earth into small regions (like
pixels)
For each region i, and each time t, let pi,t, ci,t, hi,t represent
pressure, temperature, humidity
Laws of physics will tell us what relationships pi,t, ci,t, hi,t
must satisfy across (i,t)
We can measure some pressure, humidity, temperature
values for past times, and predict for the future
The details are complicated.
Representing A Language Using
Numbers (1)
Define a numeric code for representing letters
• ASCII (American Standard Code for Information
Interchange) is the commonly used code
• Letter ‘a’ = 97 in ASCII, ‘b’ = 98, …
• Uppercase letters, symbols, digits also have codes
• Code also for space character
• Words = sequences of ASCII codes of letters in the word
‘computer’ = 99, 111,109,112,117,116,101,114
Representing A Language Using
Numbers (2)

• Sentences/paragraphs = larger sequences


• Does the word “computer” occur in a paragraph?
• Does a certain sequence of numbers occur inside another
sequence of numbers?
A Summary

• Questions about pictures, weather, documents can be


converted to questions about properties of number
sequences
• Finding answers requires solving interesting math
problems
• How will you represent Chess playing as a question on
numbers?
Outline

Examples of expressing real life problems as numerical


problems
− Picture processing
− Predicting the weather
− Understanding and processing language
Algorithms and Programs
High level design of a computer
− Digital circuits
− Parts of a computer
− Stored program, compilation
Algorithm

• A precise sequence of mathematical operations using


which a given (INPUT) sequences of numbers is
changed into another (OUTPUT) sequence of numbers
• Allowed mathematical operations: arithmetic operations
• Possible to have conditions selection
add 1 to x if y is greater than 0
• Repetition is also allowed iteration
Repeat the following operations n times
Algorithms You Already Know

• The procedures you have learnt in primary school for


arithmetic on numbers with many digits are really
algorithms.
• Primary school algorithms contain all ingredients
described earlier
1. First add the least significant digit of the first number to the least
significant digit of the second number
2. If the sum is greater than 9 then carry the most significant digits
3. Repeat as many times as there are digits
• Algorithms for determining whether a number is prime,
finding the greatest common divisor of two numbers
More About Algorithms
• Early algorithms were invented for computing using
paper and pencil
eg. Babylonian algorithm for square root
• Many such algorithms are useful even today with
computers
• More general notion of algorithms:
− Precise description of steps needed to perform any
clearly defined task, not necessarily computational
An Algorithm to cook rice
− s = number of servings
− Put s/2 cups of rice and s cups of water into a pot
− Put on stove and bring the pot contents to boil
− …
Programs

• Algorithms written using a precise notation

• Many different notations/languages possible

• C++ is one such language

• Other languages: C, Java, Python, Basic, Lisp, …


Outline

Examples of expressing real life problems as numerical


problems
− Picture processing
− Predicting the weather
− Understanding and processing language
Algorithms and Programs
High level design of a computer
− Digital circuits
− Parts of a computer
− Stored program, compilation
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?
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 =
Copyright © 1999-2000 Michael Stutz [email protected]
number 0
− Circuit designed so that voltage will
An inverter circuit never be between 0.2 and 5 volt, hence
no ambiguity.
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)
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"
• Details on conversion in next lecture
– For now assume that all decimal numbers can be
converted into binary numbers...i.e. into a sequence
of 1' s and 0's
Representing Numbers
Key idea:
Store each bit of the number on a
separate capacitor
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
Bit = wire/capacitor, depending upon context
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
Organization of a computer
Memory
Organized into bytes (groups of 0 0 0 0 1 1 0 1 0
8 capacitors) 1
2
Memories of present day
computers contain few 3

Gigabytes, Giga=230 4
5 1 0 1 1 1 1 0 1
Each byte in the memory is
6
assigned a distinct number, or
7
an address.
8
In a memory with N bytes, the
9
addresses go from 0 to N-1
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.
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)
Yes, it is possible to build
circuits that can do this!
Write Operation
10010

11101000

"Write the number


11101000 (232) into
0 1 the location number
10010 (18)
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)
Read Operation
10010

11101000

"Read from the


location number
1 0 10010 (18)
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, …
Peripherals: keyboard, screen, disk…
Also have control port data
and data port like
organization.
control
Depending upon value
data
placed on control port,
the peripheral decides control
what to do with the
data
value on the data port,
or itself places values
control
on the data port.
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.
Machine language progam
• 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.
– Subsquent numbers are OPERANDS. These are "arguments" to the operation.

OPCODE OPERAND1 OPERAND2 OPERAND3


Example
57 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
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.
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.
Putting it together
Arthimetic 1. Control unit is told
Unit
the address of the
0
1
instruction to fetch
.. (e.g. instruction is at
10 57 100 200 300 location 10)
...
10
100 289 2. A read operation
.... is performed on
345
memory location 10
200 Bus
(network)
... 3. instruction
57 100 200 300
300 57 100 200 300
Control Unit
is now "loaded" into
Memory the control unit
1 0

Read Write
Putting it together
Arthimetic 1. Now control unit
Unit
performs a read
0
1
operation of address
.. 100
10 57 100 200 300
...
100
100 289
2. The number 289
.... is sent to input 1 of
arithmetic unit
200 345 Bus
289 (network)
...
300
Control Unit

Memory

1 0

Read Write
Putting it together
Arthimetic 1. Now control unit
Unit
performs a read
0
1
operation of address
.. 200
10 57 100 200 300
...
200 2. The number 345
100 289
is sent to input 2 of
.... arithmetic unit
200 345 Bus
345 (network)
...
300
Control Unit

Memory

1 0

Read Write
Putting it together
Arthimetic 1. Now control unit
Unit
instructs the
0 99705
1
arithmetic unit to
.. x perform a multiply
10 57 100 200 300 operation (the
...
"control" wires are
100 289
set to the operation
.... code of "multiply")
200 345 Bus
(network)
2. Control unit
... stores the product
300 99705 (289 x 345 = 99705)
x Control Unit temporarily inside its
Memory own unit
0 0

Read Write
Putting it together
Arthimetic 1. Now control unit
Unit
performs a write
0
1
operation on
.. address 300
10 57 100 200 300
...
300 2. The number
100 289
99705 is sent on the
.... data port of memory
and 300 is sent on
200 345 Bus
99705
(network)
the address port of
... the memory
300 99705
Control Unit

Memory

0 1
Instruction
execution is
Read Write COMPLETE
Announcements
• Quiz: CS101:520
• Lab today: Check email for lab and JTA
assignment.
• Bugzilla
• Help session
– Some of you will get email for compulsory
attendance
– Others need advance sign up
Machine language program example
Example:

57, 100, 100, 100


57, 100, 100, 100
This contains two instructions.
Both instructions cause the word at address 100 to be multiplied by
itself and the result stored back in address 100.
After executing the first instruction, address 100 would contain the
square of the number that was present before.
The second operation would repeat the squaring operation.
Thus this is a machine language program to compute the fourth power
of a number.
More complex instructions
Example 1: operation code 59 might mean: “Shut down the computer”
Example 2: operation code 60 might mean:
Interpret the next number in the program as an address
Instead of next executing the instruction following the current one, execute
the instruction starting at this address.
Example 3: operation code 61 might mean:
Interpret the next number in the program as an address.
If the last result produced by the arithmetic unit was 0, then execute the
instruction starting at this address.
If the last result produced by the arithmetic unit was not 0, then execute the
instruction following the current one.
Analogous to the repeat statement of chapter 1.
Using such instructions, we will be able to perform an operation 100s of
times without making our machine language very long.
Machine language programs and C++ programs

On early computers, you would have to write machine


language programs.
Find out what operation you want.
Look up the manual and find its code.
Enter the code into the memory of the computer.
Repeat.
Process is laborious, error-prone.
Modern computers also need machine language programs.
You write C++ program.
A prewritten program, “compiler”, translates your C++ program to a
machine language program.
Another program, “loader”, will load it into the memory and start its
execution.
Concluding Remarks
Key idea 1: use numerical codes to
represent non numerical entities
letters and other symbols: ASCII code
operations to perform on the computer:
Operation codes
Key idea 2: Current/charge/voltage
values in the computer circuits
represent bits (0 or 1).
Concluding Remarks
Memory is organized into bytes. Each byte has an
address.
What the computer does is determined by a
machine language program that must be stored in
the memory.
Computer users do not need to themselves write a
machine language program, but can write a C++
program which is then compiled by a compiler.

You might also like