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

CH 2

The document discusses how problems can be expressed numerically and solved on computers. It describes how pictures, weather prediction, and language can be represented using numbers. It also outlines key aspects of algorithms, programs, digital circuits, number representation, and computer organization.

Uploaded by

Random Swag
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

CH 2

The document discusses how problems can be expressed numerically and solved on computers. It describes how pictures, weather prediction, and language can be represented using numbers. It also outlines key aspects of algorithms, programs, digital circuits, number representation, and computer organization.

Uploaded by

Random Swag
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 41

An Introduction to

Programming through C++


Abhiram G. Ranade

Ch. 2: A bird’s eye view


A computer can do many things
• Predict the weather
• Make railway bookings
• Play chess and beat human world
champions
• Control machinery in large factories

How can a single machine do all this?


Some basic questions
• How do you solve problems on a computer?
• How does a computer work?

This chapter gives very high level answers to


the above questions.

The answers provide background for future


chapters.
How to solve problems on a computer..

• Express the problem as a problem on


numbers.
• Decide what computations need to be
performed.
• List of required computations = “program”
• Feed the numbers and the program to the
computer.
How a computer works
How a computer works: A computer is a
giant circuit having parts that
• “store” numbers.
• perform arithmetic on numbers.
• read numbers from the external world.
• send numbers to the external world.
• somehow “understand” the program.
Outline
• How to express real life problems as numerical
problems.
– Picture processing
– Predicting the weather
– “understanding” language
• Algorithms and Programs
• How a computer solves (numerical) problems
– Digital circuits
– How numbers are represented
– Parts of a computer
– Machine language program, compilation.
“What is in this picture?”

https://en.wikipedia.org/wiki/File:Jackson%27s_Chameleon_2_edit1.jpg
How to represent black and white
pictures
• Suppose picture is 10cm x 10cm.
• Break it up into 0.1 mm x 0.1 mm squares
• 1000 x 1000 squares. “pixel”s
• If square is mostly white, represent it as
0.
• If square is mostly black, represent it as
1.
• Picture = 1 million numbers!
Picture, Representation, 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)


Remarks
• Better representation if picture divided into
more cells.
• Pictures with different “gray levels”: sequence
of numbers indicating degree of darkness
• 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.
Is there a vertical line in this picture?

• Input: sequence of 1 million numbers (0 or 1)


representing a 10cm x 10cm black and white
picture
• What property does the sequence need to
have if it is to contain a vertical line?
• All 0s, except for 1s at positions
i, i+1000, i+2000, i+3000, i+4000,…
Question about picture converted into question
about numbers.
Does the picture contain a chameleon?

• Question expressed as:


– Does the sequence of numbers representing
the picture contain a subsequence satisfying
certain properties?
• “certain properties”: Enormous ingenuity
needed to specify.
• Main concern of the deep subject
“Computer Vision”
Weather prediction
• Divide the surface of the earth into small regions
(like pixels).
• For each region i, let pi, ti, hi represent pressure,
temperature, humidity in region i.
• Laws of physics tell us how the values will change
with time if we know current values.
• We can measure current pressure, humidity,
temperature values, and calculate what will
happen tomorrow!
• Smaller the regions, better will be the accuracy.
(Smaller the pixels, better will be the picture
representation).
Language using numbers
• Define a code for representing letters.
• ASCII (American Standard Code for Information
Interchange) is the commonly used code on computers.
• 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.
• Sentences/paragraphs = larger sequences.
• Does the word “computer” occur in a paragraph?
– Does a certain sequence of numbers occur inside another
sequence of numbers?
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?
Algorithm
• Precise sequence of mathematical operations using
which given one set of numbers (INPUT) numbers
we can calculate another (OUTPUT/SOLUTION) set.
• In an algorithm we may perform arithmetic
operations.
• We may also have conditional operations, e.g.
– “add 1 to x if y is greater than 0”
• Repetition is also allowed
– “Repeat the following operations z times”
• Man invented algorithms well before computers.
• Earliest algorithms were for pencil and paper
calculations.
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
– “First add the least significant digit of the first number to the least
significant digit of the second number”
– “Repeat as many times as there are digits”
– “Add 1 if the previous sum was greater than 10”
• Algorithms for determining whether a number is prime,
finding the greatest common divisor are actually very tricky
algorithms, and you already know them, even though you
might not have explicitly written down the precise steps
required.
Programs
• Algorithms written in precise notation
• Many different notations/languages
possible.
• C++ is one such language.
• Other languages: Fortran, Basic, Lisp, …
• Rest of the course: more about C++
Outline
• How to express real life problems as numerical
problems.
– Picture processing
– Predicting the weather
– “understanding” language
• Algorithms and Programs
• How a computer solves (numerical) problems
– Digital circuits
– How numbers are represented
– Parts of a computer
– Machine language program, compilation.
Digital circuits: building blocks of
computers
• Digital circuits: interpret electrical potential/voltage as numbers.
• Simplest convention
– Voltage above 1 volt = number 1, Voltage between 0 and 0.2 volt =
number 0
– Circuit designed so that voltage will never be between 0.2 and 1 volt,
hence no ambiguity.
• Current may also be used, e.g. current < some value represents 0…
• Charge stored on a capacitor may also denote numbers
– Capacitor has low charge = number 0, High charge = number 1
– Once charge is stored on a capacitor, it persists. “Memory”
• Once you can represent 0 and 1, you can represent anything:
NEXT
• We can design circuits which perform arithmetic: if you feed sets of
voltages representing two numbers to the circuit, it will generate
voltages representing their sum!
– Or product, or quotient …
Standard representation for non-
negative numbers
• Use many capacitors to store a single number
• Number of capacitors allowed : 8, 16, 32, 64
• Example: To store 25 using 32 bits:
– Represent 25 in binary.
25 Decimal = 00000000000000000000000000011001
• So store the following charge pattern (H=High,
L=Low)
LLLLLLLLLLLLLLLLLLLLLLLLLLLHHLLH
• Range stored: 0 to 232 – 1. If your numbers are
likely to be larger, then use 64 bits.
• Transmitting numbers: send high or low voltages
on as many wires.
Representing integers that be positive
and negative
• One of the bits is used to indicate sign
• Sign bit = 0 (low charge/voltage) means
positive number, = 1 means negative number.
• To store -25 use
10000000000000000000000000011001
• Leftmost bit = sign bit
• Range stored: -231 – 1 to 231 – 1.
• Actual representation used: more complex.
“Two’s complement”.
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
Representing Real numbers
• Use analogue of “scientific notation”:
significand * 10exponent, e.g. 6.022 * 1022
• Same idea, but significand, exponent are in
binary, thus number is:
significand * 2exponent
• “Single precision”: store significand in 24 bits,
exponent in 8 bits. Fits in one word!
• “Double precision”: store significand in 53 bits,
exponent in 11 bits. Fits in a double word!
• Actual representation: more complex. “IEEE
Floating Point Standard”.
Example
Let us represent Avogadro’s number 6.022 x 1023
• Convert to binary: 1.11111110001010111 x 21001110
• Use 23 bits for magnitude of fraction, 1 bit for
sign of fraction. Equivalent of 7-8 decimal digits.
• Use 7 bits for magnitude of exponent, 1 bit for
sign of exponent
• 0111111110001010111000001001110
• Decimal point is assumed after 2nd bit.
• Inherently imprecise: fraction represented only to
certain finite number of bits.
• IEEE Floating Point Representation: more
complex.
Organization of a computer
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. Much like houses on a
street!
• In a memory with N bytes, the addresses go
from 0 to N-1
• The memory circuit communicates with the
rest of the world through 3 sets of wires or
“ports”: address port, data port, control port.
Storing/writing data into memory
Suppose we want to store a number D in the byte with
address A.
• Place the number A on the address port.
– Address port consists of several wires, place bits of A on
respective wires.
• Place the number D on the data port.
• Place the number 1 on the control port.
• If the values on the ports are held steady for 1 “clock
cycle”, then the capacitors at address A will get data
D!
• Clock cycle = how long? memory designer will tell
you..
Reading what is stored
• Suppose we wish to know what is stored in
address A of the memory
• Place A on address port
• Place 0 on control port.
• After 1 clock cycle, the values in the capacitors at
address A will appear on the data port.
• The data port is connected to the rest of the
world by wires, and through them the rest of the
world will know what was in address A.
• Reading the value at an address does not destroy
it.
Remarks
• You are not expected to understand what
circuitry is present in a memory.
• Instead of reading or writing a byte at a time,
entire word starting at given address A may be
read or written into.
– “Word starting at address A”: data stored in
address A, A+1, A+2, A+3.
– The data port will have 32 wires.
• Similarly we can have “double word oriented
memory”…
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 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.
– Screen: data port value will be suitably interpreted
and shown on the screen.
– Keyboard: some value corresponding to what user
types will be placed 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.
• Yes, the data and the program reside in the
same memory.
• Machine language program = sequence of
machine language instructions
NEXT
Machine language progam
• Machine language instruction = sequence of
numbers
• First word = “operation code”
• Operation code decides how to interpret the
remaining numbers in the instruction and what
operation to perform
• 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.
A Fictitious Example
• Hypothetical instruction: 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
• This instruction causes the product of the numbers
stored in addresses 100, 200 to be stored in the address
300.
• 58 might mean the same thing as above, except perhaps
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.
• Yes, the circuitry in the control unit is very
tricky, but it can be built!
Machine language program
(hypothetical) 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.
Remarks
• We said 57 is the code for multiplication – the
actual code used on different machines will be
different.
– You do not need to remember that 57 = multiplication.
• Actual machine languages are more complex, will
have many more instructions.
• 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.
Machine language programs and C++
programs
• On a modern computer you write C++ program.
• A prewritten program, “compiler”, translates your
C++ program to a machine language program.
• When you type s++ square.cpp, the compiler
is called upon to compile your file square.cpp.
It creates the “machine language program” which
by default is called a.out on unix.
• When you type ./a.out the program a.out
gets loaded into memory and starts execution.
For this a “loader” program works behind the
scenes.
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).
• Key idea 3: Larger numbers can be represented using
sequence of bits.
– In a fixed number of bits you can represent numbers in a fixed
range.
– If you dedicate a bit to representing the sign, the range of
representable numbers changes.
– Real numbers are represented approximately. If you want more
precision or greater range, you need to use larger number of bits.
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