Download How Computers Work

Document related concepts
no text concepts found
Transcript
How Computers Work
… and how you can work them.
Art 315 Lecture 03
Dr. J Parker
Fall 2010
What is a Computer?
There are many types of computer,
but the typical PC or MAC or Linux
computer is what we are going to
discuss. It is vastly the most
common type.
It is referred to as a stored
program digital computer (von
Neumann machine)
Properties of a computer
1. Computers can only
manipulate numbers.
To get a computer to create a picture or
music, we must device some sort of
code that allows pictures and music to
be made into numbers.
This is an encoding, and computers use
many of them
Computers can only manipulate numbers.
A simple encoding: characters.
On a computer, the letter ‘A’ is stored as
the number 65, ‘B’ is 66 …
The number ‘0’ is 48, ‘1’ is 49. and so on.
This is the ASCII (American Standard
Code for Information Interchange)
code. There are others.
ASCII
Computers can only manipulate numbers.
Strings are sequences of characters.
E.G. “This is a string”
Is stored as a set of adjacent numbers:
84 104 105 115 32 105 115 32 97 32 115 116
114 105 110 103
This is called the representation of the string,
and we’ll talk about that sort of thing in the
next class.
Computers can only manipulate numbers.
Computers can do the following things to
numbers:
Add them (and subtract)
Store in memory
Get from memory
Some can multiply/divide
Logical: AND, OR, NOT etc
Computers can only manipulate numbers.
Memory is a place where numbers can
be stored for later use.
Each place in memory can hold a
number.
Each place in memory has an address
by which the location is known. To
store/retrieve a number in memory one
needs and address.
Address
0
1
2
3
4
5
6
7
8
9
10
Memory
1
2
4
8
16
32
64
128
256
512
1024
Accessing memory in a computer
Ask: Give me the contents of
location number 8 (address 8).
Then it can be changed (EG ‘add
to 7’)
Ask: Store 15 into location 8
Accessing memory in a computer
Most times one has to get the
contents of memory before doing
something to it:
Get location 6 (=64)
Get location 5 (=32)
Add them together (=96)
Store result in location 7
(This is a computer program!)
Properties of computers
2. Computers operate using
electricity.
Electronic computers must use some
electronic means to store and retrieve
numbers.
There must be a way to represent
numbers using electricity – voltages,
current, etc.
What do you know about electricity?
Electricity
Not everyone is good with electricity, so here’s
a quick simple summary:
Electricity is a motion of electrons through a
conductor. Common analogy is that of water
through a pipe.
Water pressure = voltage (difference between
the two ends).
Current = amount of water flow (amps)
Resistance = friction with the pipe. (Ohms)
Electricity
Electrons have a negative charge. If
electrons are like ‘water’ then here
is a visual analogy:
-- - - - - -- - -- - -- ------ - --
-- - - - - ------ - - -
Flow is from more electrons to fewer (more
‘negative’ to less ‘negative’ (more ‘positive’)
Electricity
For electricity to flow there needs to
be a complete loop (unlike water)
Flow is from negative to positive.
Complete loop is a conductor from
the –ve pole of the source of
electricity to the +ve.
+
Arrows show the direction
Of electron motion.
Electricity
For electricity to flow there needs to be a
complete loop (unlike water)
That’s why a switch can turn off a light –
it breaks the circuit and there is no
longer a loop.
+
-
Arrows show the direction
Of electron motion (or not).
Electricity
So how can we represent numbers?
Hmm, 10 digits. Maybe 10 voltages?
Leads to complex and slow circuits.
ENIAC
Properties of computers
2. Computers use binary
numbers, not decimal.
Electronic computers must use some
electronic means to store and retrieve
numbers, and binary (base 2) is much
more effective in electronic circuits.
But what are binary numbers?
Binary/Decimal numbers
Grade 3 math coming up …
Our standard number system is
Base 10, meaning that there are
10 digits 0, 1, …9
10 is the base of the number system, meaning
that the representation depends on it.
We probably use 10 as a base because we have 10 fingers
Binary/Decimal numbers
The number 342 in base 10 is really
3 x 100 + 4 x10 + 2
or
3x102 + 4x101 + 2x100
The exponent is the digit position, counted right to left starting at
0.
Position
3
2
1
0
103=1000 102=100 101=10 100=1
x
x
x
x
0
3
4
2
300 + 40
+ 2
173 = 100 + 70 + 3 = 1x102 + 7x101 + 3x100
= 342
Binary/Decimal numbers
Why does the base have to be 10? It does not.
How about base 8?
82 = 64
81 = 8 80=1
So 342 in base 8 is:
3x82 + 4x81 + 2x80
or
3 x 64 + 4 x8 + 2
or
192 + 32 + 2 = 226 (if it were in
base 10)
Base is shown as a subscript:
342 base 10 is 34210
342 base 8 is 3428
Binary/Decimal numbers
Numbers always represent the same things
so 1210 means this many:
| | | | | | | | | | | |
148 = 1210 and means this many:
| | | | | | | | | | | |
The value of the number 12 stays the same,
symbolic representation can vary depending
on symbols set and base.
Babylonians used base 60!
Binary/Decimal numbers
We can do math in much the same
way irrespective of base.
Listen carefully:
Binary/Decimal numbers
342
- 1 73
Starting at the right do 2-3.
Can’t do that since 2 < 3, so we borrow one (which is
101 = 10) from the next place left.
12 – 3 = 9
3 3 12
-17 3
9
Binary/Decimal numbers
332
- 1 73
Now the next column is 3 - 7.
As before, 3 < 7, so we borrow one (which is 102=100)
from the next place left – it was 3, now becomes 2
13 – 7 = 6
2 13 12
-1 7 3
6 9
Binary/Decimal numbers
232
- 173
Now the next column is 2 – 1 = 1.
As before, 3 < 7, so we borrow one (which is 102=100)
from the next place left – it was 3, now becomes 2
13 – 7 = 6
2 13 12
-1 7 3
1 6 9
Base 8 (octal) numbers
342
- 1 73
Starting at the right do 2-3.
Can’t do that since 2 < 3, so we borrow one (which is
81 = 810 or 108) from the next place left.
128 – 38 = 78
3 3 12
-17 3
7
Base 8 (octal) numbers
332
- 1 73
Now do 3 - 7. Borrow from the 82 place (64??)
That is, borrow one from the leftmost position, making
the 3 into 2 and making the problem 138 – 78.
138 – 78 = 1110 – 710 = 4
3 13 1 2
-1 7 3
4 7
Base 8 (octal) numbers
232
- 173
Now do 2 - 1. Still 2 – 1 = 1
That’s it! the 3 into 2 and making the problem 138 – 78.
So 3428 - 1738 = 1478
3 13 1 2
-1 7 3
1 4 7
Why Did We Do All Of That?
Because positional number systems like these prevail
in most human systems, and in computers too.
In particular, we want to learn about binary (base 2)
numbers because computers use binary to store all
numbers, all data, all programs. Everything.
The last question asked about 11 slides ago was:
‘What Are Binary Numbers?’
Why Did We Do All Of That?
Binary allows electrical circuits to be simple. Lights,
buzzers, toasters, TV sets can be on or off. Two
states can be used easily to represent 2 digits:
0 = OFF
1 = ON.
So a number can be stored as the settings (states) of a
set of switches:
0
1
1
0
0
1
0 = 50
Binary Numbers Are Everything
Everything on a computer is coded as numbers, and all
numbers are binary.
Even commands for the computer to execute.
Computers have a memory, a circuit that
does calculations, a circuit that controls
overall operations, and lots of extra
stuff for communications and long term
storage.
Stored Program Computer
However:
Computers store the commands to be
executed (the program) as numbers in
its memory.
It fetches the next command (instruction)
from memory, then executes it.
This is the fetch-execute cycle.
4. Instructions are Binary Numbers
Principle 4 is:
All Computer Instructions are Binary
Numbers.
Instructions in memory can be executed.
Those not in memory have to be put
there before they can be executed.
4. Instructions are Binary Numbers
Why does this matter?
A good artist (or programmer I guess)
needs to be able to have control over
their computer.
To be a really good media artist you need
to be able to make the machine do what
you want (or at least have it make
interesting errors)
How a computer works - instructions
Here is how the Fetch-execute cycle works
000
Load 04
001
Add 05
002
Store 06
003
HALT
004
005
005
009
006
000
Here is a sample program in memory.
We’ll use decimal at first, and
symbolic instructions.
007
008
009
010
Assume that we always start executing
a program at memory location 0.
How a computer works - instructions
The address of the instruction changes every time
000
Load 04
001
Add 05
002
Store 06
003
HALT
004
005
005
009
006
000
007
000
It usually increases by 1.
008
009
010
We’ll use a special place to store the
address of the next instruction, called the
Program counter.
It will initially contain 0.
How a computer works - instructions
We also need a place where we can do the math
000
Load 04
001
Add 05
002
Store 06
003
HALT
004
005
005
009
006
000
007
000
000
It’s like the display on a calculator.
008
009
010
We’ll call it the accumulator
We can add to is, load it, store it,
take away from it, etc etc.
How a computer works - instructions
We finally need a place to store the instruction
PC
000
Load 04
001
Add 05
002
Store 06
003
HALT
004
005
005
009
006
000
000
IR
ACC
000
… because we increment the program
counter.
007
008
009
010
We’ll call it the instruction register
OK, now let’s execute.
How a computer works - instructions
PC
000
Load 04
001
Add 05
002
Store 06
003
HALT
004
005
005
009
006
000
007
008
009
010
000
IR
Load 004
ACC
000
FETCH.
Get the instruction at the address indicated
by the PC.
Store it in the IR
Add 1 to the PC
How a computer works - instructions
PC
000
Load 04
001
Add 05
002
Store 06
003
HALT
004
005
005
009
006
000
007
008
009
010
001
IR
Load 004
ACC
000
FETCH.
Get the instruction at the address indicated
by the PC.
Store it in the IR
Add 1 to the PC
How a computer works - instructions
PC
000
Load 04
001
Add 05
002
Store 06
003
HALT
004
005
005
009
006
000
007
008
009
010
001
IR
Load 004
ACC
005
EXECUTE.
Execute the instruction in the IR
In this case, Load 4 means:
‘Take the contents of memory location
4 and move a copy of it to the
accumulator”
How a computer works - instructions
PC
000
Load 04
001
Add 05
002
Store 06
003
HALT
004
005
005
009
006
000
007
008
009
010
001
IR
Add 05
ACC
005
FETCH.
Get the instruction at the address
indicated by the PC.
Store it in the IR
Add 1 to the PC
How a computer works - instructions
PC
000
Load 04
001
Add 05
002
Store 06
003
HALT
004
005
005
009
006
000
007
008
009
010
002
IR
Add 05
ACC
005
FETCH.
Get the instruction at the address
indicated by the PC.
Store it in the IR
Add 1 to the PC
How a computer works - instructions
PC
000
Load 04
001
Add 05
002
Store 06
003
HALT
004
005
005
009
006
000
007
008
009
010
002
IR
Add 05
ACC
014
EXECUTE.
Execute the instruction in the IR
In this case, Add 5 means:
‘Take the contents of memory location
5 and add it to the accumulator”
How a computer works - instructions
PC
000
Load 04
001
Add 05
002
Store 06
003
HALT
004
005
005
009
006
000
002
IR
Store 06
ACC
014
FETCH.
007
008
009
010
Get the instruction at the address
indicated by the PC.
Store it in the IR
Add 1 to the PC
How a computer works - instructions
PC
000
Load 04
001
Add 05
002
Store 06
003
HALT
004
005
005
009
006
000
003
IR
Store 06
ACC
014
FETCH.
007
008
009
010
Get the instruction at the address
indicated by the PC.
Store it in the IR
Add 1 to the PC
How a computer works - instructions
PC
000
Load 04
001
Add 05
002
Store 06
003
HALT
004
005
005
009
006
000
003
IR
Srore 06
ACC
014
EXECUTE.
007
008
009
010
Execute the instruction in the IR
In this case, STORE 06 means:
‘Take the contents of the accumulator
and store it in memory location 6”
How a computer works - instructions
PC
000
Load 04
001
Add 05
002
Store 06
003
HALT
004
005
005
009
006
014
003
IR
Srore 06
ACC
014
EXECUTE.
007
008
009
010
Execute the instruction in the IR
In this case, STORE 06 means:
‘Take the contents of the accumulator
and store it in memory location 6”
How a computer works - instructions
PC
000
Load 04
001
Add 05
002
Store 06
003
HALT
004
005
005
009
006
014
003
IR
Halt
ACC
014
FETCH.
007
008
009
010
Get the instruction at the address
indicated by the PC.
Store it in the IR
Add 1 to the PC
How a computer works - instructions
PC
000
Load 04
001
Add 05
002
Store 06
003
HALT
004
005
005
009
006
014
004
IR
005
ACC
014
FETCH.
007
008
009
010
Get the instruction at the address
indicated by the PC.
Store it in the IR
Add 1 to the PC
How a computer works - instructions
PC
000
Load 04
001
Add 05
002
Store 06
003
HALT
004
005
005
009
006
014
004
IR
005
ACC
014
EXECUTE.
007
008
009
010
Execute the instruction in the IR
In this case, Halt means:
‘Stop the computer; cease executing
this program”
How a computer works - instructions
PC
000
Load 04
001
Add 05
002
Store 06
003
HALT
004
005
005
009
006
014
007
004
IR
005
ACC
014
The program has
completed.
008
009
010
This program was intended to add two
numbers. Because we wrote it, we know
where to find the answer: memory
location 6
Instructions are coded as numbers
000
Load 04
001 00000100
001
Add 05
Add 05
002
Store 06
Store 06
003
HALT
HALT
004
005
005
005
009
009
006
014
014
007
008
009
010
Load is code 001 address is 8 bits
so LOAD 4 is 001 00000100
Instructions are coded as numbers
000
Load 04
001 00000100
001
Add 05
101 00000101
002
Store 06
Store 06
003
HALT
HALT
004
005
005
005
009
009
006
014
014
000
LOAD 001
010
011
100
ADD 101
007
008
009
010
Load is code 001 address is 8 bits
so LOAD 4 is 001 00000100
Add is code 101 address is 8 bits
so ADD 5 is 101 00000101
0
1
2
3
4
5
Instructions are coded as numbers
000
Load 04
001 00000100
001
Add 05
101 00000101
002
Store 06
010 00000110
003
HALT
HALT
004
005
005
005
009
009
006
014
014
000
LOAD 001
STORE 010
011
100
ADD 101
007
008
009
010
Add is code 101 address is 8 bits
so ADD 5 is 101 00000101
Store is code 010 address is 8 bits
so Store 6 is 010 00000110
0
1
2
3
4
5
Instructions are coded as numbers
000
Load 04
001 00000100
001
Add 05
101 00000101
002
Store 06
010 00000110
003
HALT
000 00000000
004
005
005
005
009
009
006
014
014
HALT 000
LOAD 001
STORE 010
011
100
ADD 101
007
008
009
010
Store is code 010 address is 8 bits
so Store 6 is 010 00000110
Halt is code 000 no address
so Halt is 000 00000000
0
1
2
3
4
5
Instructions are coded as numbers
000
Load 04
001 00000100
001
Add 05
101 00000101
002
Store 06
010 00000110
003
HALT
000 00000000
004
005
005
009
000 00000101
000 00001001
006
014
000 00001110
HALT 000
LOAD 001
STORE 010
011
100
ADD 101
007
008
009
010
Halt is code 000 no address
so Halt is 000 00000000
Data are simply stored as numbers
0
1
2
3
4
5
Instructions are coded as numbers
000
Load 04
001 00000100
001
Add 05
101 00000101
002
Store 06
010 00000110
003
HALT
000 00000000
004
005
005
009
000 00000101
000 00001001
006
014
000 00001110
HALT 000 0
LOAD 001 1
STORE 010 2
BZA
011 3
BRA
100 4
ADD 101 5
NEG 110 6
AND 111 7
007
008
009
010
There are many other instructions:
Branch
Branch on zero accumulator
Negate
And
Instructions are coded as numbers
So, a computer program is really
HALT 000 0
just a sequence LOAD 001 1
STORE 010 2
BZA
011 3
of numbers,
BRA
100 4
ADD 101 5
each of which
NEG 110 6
AND 111 7
represents
a command or
instruction.
Next …
How can we represent things that
don’t seem to be numbers?