Download Lecture 2. How computer work? - Department of Computer Science

Document related concepts

Addition wikipedia , lookup

Elementary mathematics wikipedia , lookup

Arithmetic wikipedia , lookup

Location arithmetic wikipedia , lookup

Approximations of π wikipedia , lookup

Positional notation wikipedia , lookup

Transcript
Lecture 2. How does the
computer work?
Prof. Sin-Min Lee
Department of Computer Science
‘Computer” of the day
Jacquard loom
late 1700’s
for weaving silk
“Program” on punch cards
“Microcode”: each hole
lifts a set of threads
“Or gate”: thread lifted if
any controlling hole punched
Card Punch
• Early “programmers” were well-paid (compared to loom operators)
Computer Architecture =
Instruction Set Architecture + Machine Organization + …
Aug. 31, 2004, 12:05AM
Big gain in small package
Intel test chip boasts technology to add to
speed
Intel, the world's biggest semiconductor maker, said Monday
it built a test chip with a new process that creates faster
circuits by packing 10 million transistors into an area the size
of the tip of a ballpoint pen.
Intel, based in Santa Clara, Calif., has made the first working
memory chip that uses so-called 65-nanometer technology to
shrink the circuits inside chips, Intel researcher Mark Bohr
said during a conference call.
Bit
so a k is...
k in computing is short for kb or kilobyte
where kilo means the same as anywhere else in our measurement system:
1000
so a k is 1000 bytes
how about Mb?
b for byte again, and M for mega: megabyte
big M for mega, small m means milli, or one thousandth, 1/1000
as in mm, thousandths of a metre
mega in our measurement system means one million, 1 000 000
or a thousand thousand, so a megabyte is a thousand kilobytes or one million
bytes
how about GB?
you can now buy hard disk drives in Gb, gigabytes, thousands of Mb or
thousands of millions of bytes, so a 5Gb HDD holds 5000 Mb of data
with some large computers storing up to Tb, terabytes...
yup, thousands of Gb...
so one terabyte (Tb) = 1000 gigabytes (Gb) = 1000 000 megabytes (Mb) =
1000 000 000 kilobytes (kb) = 1 000 000 000 000 bytes or one thousand
billion bytes of information
What is a Computer?
•
•
•
•
•
•
Data
Primitive Operations
Sequence Control
Data Access
Storage Management
Operating
Environment
May be actual hardware
computer or softwaresimulated computer.
Most computers a
combination of the
two.
In mathematics you don't understand things. You just get
used to them.
John von Neumann
Typical Machine Layout
Two cycles:
•Fetch cycle - get instruction
•Execute cycle - do operation
Typical Machine Translation
Instruction format:
Opcode
register, index, offset
load
R1, R2, 24
For example: As we see later, memory for data in blocks of storage
pointed to by a register:
X=Y+Z
could be translated as:
load R1, R2, 28
[Location of Y]
add R1, R2, 40
[Location of Z]
store
R1, R2, 24
[Location of X]
Ways to Construct a Computer
• Hardware Realization
– Any precisely defined algorithm or data
structure may be realized in hardware.
• Firmware Realization
– Microprogramming or emulation
• Virtual Machine
– Provided by a language environment
• Combination
Von Neumann Model of Execution
Instruction
Obtain instruction from program storage
Fetch
Instruction
Determine required actions and instruction size
Decode
Operand
Locate and obtain operand data
Fetch
Execute
Result
Store
Next
Instruction
Compute result value or status
Almost all Modern
Computers are
Von Neumann
machines
Deposit results in storage for later use
Determine successor instruction
Data Representation
• Data comes in different forms such as numbers, text,
images, audio, video, …
• How does the computer handle all the different data types?
• The most efficient solution is to use a uniform
representation of data. All data from outside a computer
are transformed into this uniform representation.
• The universal format is called a bit pattern (a sequence of
bits).
Computer Memory
• Memory is comprised of a large collection of bi-state
(off/on) electrical devices called bits (binary digits)
• A single bit can assume the value 0 or 1.
• A single bit is not sufficient to represent all data ;
therefore, it is necessary to use a sequence of bits.
Basic Addressable Unit of Memory
• A byte is the basic addressable unit of memory.
• In virtually all modern computers, a byte consists of 8 bits.
• As indicated on the previous slide, an 8-bit byte can
encode 256 distinct values.
Binary integer arithmetic
• Integers are stored in a computer using a fixed number of
binary digits to encode each value.
Decimal system
Binary system
• The binary system is based on 2.
• There are only two digits: 0 and 1
• We convert a number from binary to decimal by
multiplying each binary digit by its corresponding power of
2. i.e. Multiply the bit at position n (n = 0, 1, 2, …) by
2n
Binary system
Binary to Decimal Conversion
Binary number
Exercises
1.
Convert the binary number 10011 to decimal.
2.
Convert the binary number 1110101 to decimal.
1
100
6
10
Decimal
review
2
.
3
7
5
1
1/10
1/100
Digits
1/1000 Weights
• Numbers consist of a bunch of digits, each
with a weight:
1
102
6
101
2
100
.
3
10-1
7
10-2
5
10-3
Digits
Weights
(1 x 102) + (6 x 101) + (2 x 100) + (3 x 10-1) + (7 x 10-2) + (5 x 10-3) = 162.375
• The weights are all powers of the base,
which is 10. We can rewrite the weights like
this:
Decimal to Binary Conversion
To convert a decimal to a binary number, use repetitive
division.
• Divide the number by 2. Write the remainder as the least
significant digit (rightmost digit).
• The quotient becomes the dividend in the next stage.
Continue to divide by 2 until the quotient is zero, writing
each remainder from right to left.
Decimal to binary conversion
Decimal to Binary Conversion
Example:
Write 18 in base 2.
18 / 2 = 9 r 0
9/2=4r1
4/2=2r0
2/2=1r0
1/2=0r1
18 base 10 = 10010 base 2
Converting binary to decimal
1
23
1
22
0
21
1
20
.
0
2-1
1
2-2
Binary digits, or bits
Weights (in base 10)
• We can use the same trick to convert binary,
or base 2, numbers to decimal. The only
difference is that the weights are powers of
2.(1 x 2 ) + (1 x 2 ) + (0 x 2 ) + (1 x 2 ) + (0 x 2 ) + (1 x 2 ) =
8
+
4
+
0
+
1
+
0
+ 0.25 = 13.25
• For example, here is 1101.01 in binary:
3
2
1
0
Powers of 2:
20 = 1
21 = 2
22 = 4
23 = 8
24
25
26
27
=
=
=
=
16
32
64
128
• The decimal value is:
28 = 256
29 = 512
210 = 1024
-1
-2
Converting decimal to binary
• To convert a decimal integer into binary, keep dividing by
2 until the quotient is 0. Collect the remainders in reverse
order.
• To convert a fraction, keep multiplying the fractional part
by 2 until it becomes 0. Collect the integer parts in forward
order.
• Example: 162.375:
162 / 2
81 / 2
40 / 2
20 / 2
10 / 2
5/2
2/2
1/2
= 81
= 40
= 20
= 10
=5
=2
=1
=0
rem 0
rem 1
rem 0
rem 0
rem 0
rem 1
rem 0
rem 1
0.375 x 2 = 0.750
0.750 x 2 = 1.500
0.500 x 2 = 1.000
Why does this work?
162 / 10 = 16
16 / 10 = 1
1 / 10 = 0
rem 2
rem 6
rem 1
• This works for converting from
decimal to any base
• Why? Think about converting
162.375 from decimal to
0.375 x 10 = 3.750
decimal.0.750 x 10 = 7.500
0.500 x 10 = 5.000
Exercises
1. Convert the decimal number 35 to binary.
2. Convert the decimal number 327 to binary.
Bases other than 2 and 10
• Any positive integer can be used as a base.
• For base 5
– The digits are {0, 1, 2, 3, 4}
– In a number, such as 1203, each digit is implicitly multiplied
by a positional power of 3.
– 1203 base 5 is
1* 5  2 * 5  0 * 5  3 * 5  178 base 10
3
2
1
0
Bases that are Powers of 2
• Bit patterns are designed to represent data when stored
inside a computer.
• People find it difficult to manipulate bit patterns.
Writing a long stream of 0s and 1s is tedious and errorprone.
• Bases which are powers of 2 are useful in simplifying
notation.
Bases that are Powers of 2
• A single digit of a base
bits of a base 2 system.
Base
4
8
16
Number of bits
2
3
4
2n
system represents exactly n
Converting from base 2 to base 4
•
•
Valid digits in base 4 are 0, 1, 2, 3
To convert from base 2 to base 4
1.
2.
Organize the stream of binary digits into groups of two.
Find the base 4 value for each group of 2 bits.
11110010
11 11 00 10
3 3 0 2
Converting from base 4 to base 2
•
To convert from base 4 to base 2
Convert each digit to its 2-bit equivalent.
3 3 0 2
11 11 00 10
3302 base 4 is 11110010 base 2
Base 8 (Octal )
Valid digits: 0, 1, 2, 3, 4, 5, 6, 7
Bit Pattern
-----------000
001
010
011
100
101
110
111
Octal Digit
-----------0
1
2
3
4
5
6
7
Converting from base 2 to base 8
•
To convert from base 2 to base 8
1.
2.
Organize the stream of binary digits into groups of three.
Find the octal value for each group of 3 bits.
111100101
111 100 101
7 4 5
111100101 base 2 is 745 base 8
Converting from octal to binary
•
To convert from octal to binary
Convert each digit to its 3-bit equivalent.
3 5 7
011 101 111
357 base 8 is 011101111 base 2
Binary to octal and
octal to binary transformation
Exercises
1.
Show the octal equivalent of the bit pattern 101110010.
2.
Show the octal equivalent of the bit pattern 100010.
3.
What is the bit pattern for 24 base 8?
Base 16 is useful too
• The hexadecimal system
uses 16 digits:
0 1 23 4 56 7 89AB C D
EF
• You can convert between
base 10 and base 16 using
techniques like the ones
we just showed for
converting between
Decimal
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Binary
0000
0001
0010
0011
0100
0101
0110
0111
1000
1001
1010
1011
1100
1101
1110
1111
Hex
0
1
2
3
4
5
6
7
8
9
A
B
C
D
E
F
Converting from base 2 to hexadecimal
•
To convert from base 2 to hexadecimal
1.
2.
Organize the stream of binary digits into groups of four.
Find the hexadecimal value for each group of 4 bits.
10010010111000011010
1001 0010 1110 0001 1010
9
2 E
1
A
Converting from hexadecimal to base 2
•
To convert from hexadecimal (base 16) to base 2
Convert each digit to its 4-bit equivalent.
9
2
E
1
A
1001 0010 1110 0001 1010
Binary to hexadecimal and
hexadecimal to binary transformation
Exercises
1. Show the hexadecimal equivalent of the bit pattern
1100 1110 0010.
2.
Show the hexadecimal equivalent of the bit pattern
0011100010.
3.
What is the bit pattern for 2675 base 16?
4.
What is the bit pattern for B51E base 16?
Binary and hexadecimal
conversions
• Converting from hexadecimal to binary is easy: just replace each hex
digit with its equivalent 4-bit binary sequence.
261.3516 =
2
6
1
.
3
516
= 0010 0110 0001 . 0011 01012
10110100.0010112 = 1011
=
B
0100 . 0010 11002
4
.
2
C16
• To convert from binary to hex, make groups of 4 bits, starting from the
binary point. Add 0s to the ends of the number if needed. Then, just
convert each bit group to its corresponding hex digit.
H
0
ex
1
2
Bina
0000
ry
0001
0010
H
4
ex
5
6
Bina
0100
ry
0101
0110
H
8
ex
9
A
Bina
1000
ry
1001
1010
H
C
ex
D
E
Bina
1100
ry
1101
1110
Class Exercise
1.
2.
3.
4.
5.
6.
Bin to dec: 1001 0011
Dec to bin: 105
Bin to hex: 0010 1110 1000 1011
Hex to bin: 0xFEDC
Hex to dec: 0x10A
Dec to hex: 165
Other Bases
• Base 8 (octal number system)
– 123 = 1 * 82 + 2 * 81 + 3 * 160
= 1 * 64 + 2 * 8 + 3 * 1
= 64 + 16 + 3
= 83
• Base 13
– 123 = 1 * 132 + 2 * 131 + 3 * 130
= 1 * 169 + 2 * 13 + 3 * 1
= 169 + 26 + 3
= 198
<B; +, *,’, 0,1> Algebraic System
Binary operations: +,*
Unary operation: ‘
Properties of Boolean Algebra