Title: Lecture 07
1Lecture 07 October 11, 2001
- Programming our toy machine
- Review of hardware
- Storing images and sounds
- Compression
- Administrivia
- send questions to cs111_at_cs.princeton.edu
- not cs111_at_princeton.edu
- NO lab sessions on Fridays
- (do the lab before Friday if you want help from
the lab TAs) - Collaboration policy
- Everyone does their own writeup
2CPU
Memory Registers
Control Unit(State Machine)
3Our Example CPU
- Well look at a toy CPUs Machine Language
- Our CPU has
- 8 Registers each holds 16 bits (2 bytes)
- Our RAM
- Works in blocks of 16 bits (2 bytes)
- Has 28 256 addresses
- Total Memory 512 bytes
4Machine Core
- Everything is in binary (hex)
Registers
R0 0000 R1 0CA8 R2 A9DB R3 0705 R4 1011 R5
90A0 R6 0807 R7 00A0
5Representing Instructions
- Machine instructions will also be representedby
16 bits. - We divide those bits into 4 groups - 4 bits
each - The first group of 4 bits is called the Op-Code
- Tells what instruction it is.
6 C A 5
6Instructions
- 16 possible Op-Codes
- Well only look at a fewin detail
0 halt 1 add 2 subtract 3 multiply 4 bus
output 5 jump 6 jump if positive 7 jump
count 8 bus input 9 load A store B load
direct/addr. C NAND D AND E Shift Right F
Shift Left
7Halt Instruction
- 0 Halt
- This just tells the machineto stop.
- Other 12 bits are ignored
- All have same effect.
0 halt 1 add 2 subtract 3 multiply 4 bus
output 5 jump 6 jump if positive 7 jump
count 8 bus input 9 load A store B load
direct/addr. C NAND D AND E Shift Right F
Shift Left
0 0 0 0
0 F F F
0 9 A C
8Data Instructions
- B Load Direct / Address
- Sets a Register to a fixedSpecified Value
- Encoding
- Effect Register A becomesthe specified value
0 halt 1 add 2 subtract 3 multiply 4 bus
output 5 jump 6 jump if positive 7 jump
count 8 bus input 9 load A store B load
direct/addr. C NAND D AND E Shift Right F
Shift Left
B regA value (8 bits)
9Arithmetic/Logic Instructions
- 1 Add
- Adds contents of two registerstogether and puts
result in thirdregister - Encoding
- Effect Register A becomesRegister B Register C
0 halt 1 add 2 subtract 3 multiply 4 bus
output 5 jump 6 jump if positive 7 jump
count 8 bus input 9 load A store B load
direct/addr. C NAND D AND E Shift Right F
Shift Left
1 regA regB regC
10Adding Numbers
- Simple Program to calculate1 2 3 4 5 6
21 15 (hex)
10 Load R0 ? 0001 (always 1) 11 Load R2 ?
0000 (running total) 12 Load R1 ? 0001
(current number) 13 Add R2 ? R2 R1
(R21) 14 Add R1 ? R1 R0 (R12) 15 Add
R2 ? R2 R1 (R23) 16 Add R1 ? R1 R0
(R13) 17 Add R2 ? R2 R1 (R26) 18 Add
R1 ? R1 R0 (R14) 19 Add R2 ? R2 R1
(R2A) 1A Add R1 ? R1 R0 (R15) 1B Add
R2 ? R2 R1 (R2F) 1C Add R1 ? R1 R0
(R16) 1D Add R2 ? R2 R1 (R215) 1E halt
11Adding Numbers
- Simple Program to calculate1 2 3 4 5 6
21 15 (hex) - Lets watch it execute
12Arithmetic/Logic Instructions
- 2 Subtract
- Similar to Add
- Encoding
- Effect Register A becomesRegister B - Register C
0 halt 1 add 2 subtract 3 multiply 4 bus
output 5 jump 6 jump if positive 7 jump
count 8 bus input 9 load A store B load
direct/addr. C NAND D AND E Shift Right F
Shift Left
2 regA regB regC
13Control Instructions
- 6 Jump if Positive
- Jump to a different placein memory if Register
is gt 0 - Encoding
- Effect If Register A gt 0,Go To Instruction at
specifiedaddress
0 halt 1 add 2 subtract 3 multiply 4 bus
output 5 jump 6 jump if positive 7 jump
count 8 bus input 9 load A store B load
direct/addr. C NAND D AND E Shift Right F
Shift Left
6 regA address (8 bits)
14Adding Numbers
- Use a Loop to calculate1 2 3 4 5 N
10 Load R1 ? 0006 (N) 11 Load R2 ?
0000 (running total) 12 Load R0 ? 0001
(always 1) 13 Add R2 ? R2 R1
(add in N) 14 Sub R1 ? R1 - R0 (N N-1)
15 Jump to 13 if (R1gt0) (If N isnt 0
yet, go back) 16 halt (N is
now 0, and R2 12N)
15Adding Numbers
- Use a Loop to calculate1 2 3 4 5 N
- Execution
16Control Instructions
- 7 Jump and count (backwards)
- Jump to a different placein memory if Register
is gt 0,AND decrement by 1 - Encoding
- Effect If Register A gt 0,Go To Instruction at
specifiedaddress, and A A - 1
0 halt 1 add 2 subtract 3 multiply 4 bus
output 5 jump 6 jump if positive 7 jump
count 8 bus input 9 load A store B load
direct/addr. C NAND D AND E Shift Right F
Shift Left
7 regA address (8 bits)
17Adding Numbers
- Alternate Loop using Jump Count for1 2 3
4 5 N
10 Load R1 ? 0006 (N) 11 Load R2 ?
0000 (running total) 12 Add R2 ? R2
R1 (add in N) 13 Jump to 12 and
(If N isnt 0 yet, decrease R1 if (R1gt0)
Let NN-1, and go back) 14 halt
(N is now 0, and R2 12N)
18Adding Numbers
- Alternate Loop using Jump Count for1 2 3
4 5 N - Execution
19Memory Instructions
- 9 Load (from memory)
- Load from Memory into Register
- Encoding
- Effect Load from RAM atspecified address
intoRegister A
0 halt 1 add 2 subtract 3 multiply 4 bus
output 5 jump 6 jump if positive 7 jump
count 8 bus input 9 load A store B load
direct/addr. C NAND D AND E Shift Right F
Shift Left
9 regA address (8 bits)
20Memory Instructions
- A Store (into memory)
- Store Register into Memory
- Encoding
- Effect Store contents ofRegister A into
memoryat specified address
0 halt 1 add 2 subtract 3 multiply 4 bus
output 5 jump 6 jump if positive 7 jump
count 8 bus input 9 load A store B load
direct/addr. C NAND D AND E Shift Right F
Shift Left
A regA address (8 bits)
21Memory Instructions Indirect
- 9 Load (from memory)
- Load from Memory into Register
- Encoding
- Effect Load from RAM ataddress BC into
Register A
0 halt 1 add 2 subtract 3 multiply 4 bus
output 5 jump 6 jump if positive 7 jump
count 8 bus input 9 load A store B load
direct/addr. C NAND D AND E Shift Right F
Shift Left
9 regA regB regC
22Memory Instructions Indirect
- How can CPU tell these apart?
- Sneaky trick
- CPU has 8 Registers,so only need 3 bits to
specifyRegister A - Use extra bit to tell which type of LOAD
0 halt 1 add 2 subtract 3 multiply 4 bus
output 5 jump 6 jump if positive 7 jump
count 8 bus input 9 load A store B load
direct/addr. C NAND D AND E Shift Right F
Shift Left
9 regA address (8 bits)
9 regA regB regC
23Hacks
- This is called a HACK!
- Hacks are terrible! Generally should be avoided.
- Sadly, they are everywhere.
- Some CS people get perversepleasure out of
hacks. - This is a major source of bugs!
0 halt 1 add 2 subtract 3 multiply 4 bus
output 5 jump 6 jump if positive 7 jump
count 8 bus input 9 load A store B load
direct/addr. C NAND D AND E Shift Right F
Shift Left
24A Virus!
- Copies itself on top of other programs!
10 Load R1 ? 0006 (Length of
Virus) 11 Load R2 ? 001F (Memory
Location to copy self To -1) 12 Load R3 ? 000F
(Memory Location to copy self From
-1) 13 Load R0 ? MemoryR3R1 (Load
instruction from source) 14 Store MemoryR2R1
? R0 (Store instruction in destination) 15
Jump to 13 and (If havent copied
whole virus, decrease R1 if (R1gt0)
decrease R1, and go back) 16 halt
(Virus has replicated) ...
__________________________________________________
____________ 20 . . . .
(Program about to be destroyed.) 21 . . . .
...
25A Virus!
- Copies itself on top of other programs!
- Execution
26Summary
- We looked at a typical Machine Languageand how
instructions are represented. - Halt Instruction
- Data Instructions
- Arithmetic/Logic Instructions
- Control Instructions
- Memory Instructions
27Summary (cont.)
- We saw a few simple examples of programsyou can
write in Machine Language. - You now have a pretty solid understandingof how
computers really work !
28Hardware Wrap-up
29Layers of Abstraction
- Build more and more powerful tools out of simpler
ones.
Really Simple Stuff
Computers
30Logic Gates
X
Z
Y
X
Z
Y
Y
X
X
Y
31Universal Method
First, make circuitsfor each row in Truth Table
witha 1 in the output.
A B C F 0 0 0 0 0 0 1 1 0 1 0 1 0 1
1 0 1 0 0 0 1 0 1 0 1 1 0 1 1 1 1 0
A
B
C
A
B
C
A
B
C
32Universal Method (cont.)
A
Finally, combine themwith an OR gate.
B
C
A
F
B
C
A
B
C
33Universality
- For ANY Truth Table we can write down,we can
make a circuit for it using only 3 Logic Gates
AND, OR, NOT - In real life, make AND/OR/NOT gatesout of
Transistors.
34Representing Information
- Information in the world comes in manyforms
letters, numbers, pictures, sounds - How can we represent these thingswith 0s and
1s? - Start with numbers.
35The Flip-Flop
Set
M
Reset
- M becomes 1 if Set is turned on
- M becomes 0 if Reset is turned on
- Otherwise (if Set and Reset are both 0), M just
remembers its value
36Snapshot Memory
D
M
Write
- When Write changes from 1 to 0,the value of D
gets stored in M. (snapshot) - Otherwise, M just remembers its value.(It
ignores D.)
37Design for Any State Machine
Current State
Memory Register
Logic For Next State Output
Clock
Inputs
Outputs
38Review
On our way to building a computer
Represent Manipulate Info. with 0s 1s (aka
bits)
Memory Circuits
State Machines
Computers
We are here
39Putting it all together
- Computer has many parts, connected by a Bus
- Each part is a state machine.
CPU
Keyboard
Display
Bus
Hard Disk
CD-ROM
RAM
40Putting it all together
- This cycle is orchestrated by the Control Unitin
the CPU.
Memory Registers
Control Unit(State Machine)
41Instructions
0 halt 1 add 2 subtract 3 multiply 4 bus
output 5 jump 6 jump if positive 7 jump
count 8 bus input 9 load A store B load
direct/addr. C NAND D AND E Shift Right F
Shift Left
42Adding Numbers
- Simple Program to calculate1 2 3 4 5 6
21 15 (hex)
10 Load R0 ? 0001 (always 1) 11 Load R2 ?
0000 (running total) 12 Load R1 ? 0001
(current number) 13 Add R2 ? R2 R1
(R21) 14 Add R1 ? R1 R0 (R12) 15 Add
R2 ? R2 R1 (R23) 16 Add R1 ? R1 R0
(R13) 17 Add R2 ? R2 R1 (R26) 18 Add
R1 ? R1 R0 (R14) 19 Add R2 ? R2 R1
(R2A) 1A Add R1 ? R1 R0 (R15) 1B Add
R2 ? R2 R1 (R2F) 1C Add R1 ? R1 R0
(R16) 1D Add R2 ? R2 R1 (R215) 1E halt
43Adding Numbers
- Use a Loop to calculate1 2 3 4 5 N
10 Load R1 ? 0006 (N) 11 Load R2 ?
0000 (running total) 12 Load R0 ? 0001
(always 1) 13 Add R2 ? R2 R1
(add in N) 14 Sub R1 ? R1 - R0 (N N-1)
15 Jump to 13 if (R1gt0) (If N isnt 0
yet, go back) 16 halt (N is
now 0, and R2 12N)
44A Virus!
- Copies itself on top of other programs!
10 Load R1 ? 0006 (Length of
Virus) 11 Load R2 ? 001F (Memory
Location to copy self To -1) 12 Load R3 ? 000F
(Memory Location to copy self From
-1) 13 Load R0 ? MemoryR3R1 (Load
instruction from source) 14 Store MemoryR2R1
? R0 (Store instruction in destination) 15
Jump to 13 and (If havent copied
whole virus, decrease R1 if (R1gt0)
decrease R1, and go back) 16 halt
(Virus has replicated) ...
__________________________________________________
____________ 20 . . . .
(Program about to be destroyed.) 21 . . . .
...
45Further Directions
- Weve covered the basics of computers.
- What weve left out
- How to make computers faster.
- How to make circuits more efficient
- Large machine languages
- Pipelining make the computer execute more
than one instruction at once. - Parallelism having many CPUs work together
- To learn more, take CS courses (217,306,471)
46Sounds and Images
- How do we represent sounds?
- How do we represent images?
- These representations are too big, can we
compress? - How do we manipulate them?
- Next few lectures
- Next 2 labs
47Representing sounds
- Start from waveform of the sound
- Then digitize
- Eventually, consider frequencies
48The waveform
49Sampling
50The approximation
51Finer sampling
52Even finer sampling
53Analog vs. digital
- In analog technology, a wave is recorded or used
in its original form. For example, in an analog
tape recorder, a signal is taken straight from
the microphone and laid onto tape. The wave is
analog. It can be read, amplified and sent to a
speaker to produce the sound. - In digital technology, the analog wave is sampled
at some interval, and then turned into numbers
that can be stored digitally.
54Digital sound
- On a CD, the sampling rate is 44,100 samples per
second. Each sample is quantized into a number in
the range from 0 to 65,535, So a CD has 44,100
numbers (each 2 bytes) per second of music. - Numbers are used to make a voltage wave that
approximates the original wave. - Advantages of digital technology
- Recording does not degrade over time.
- Groups of numbers can often be compressed by
finding patterns in them. - It is easy to use special computers called
digital signal processors (DSPs) to process
streams of numbers.
55Numeracy
- CD has 44,100 2 byte samples per second.
- 44,100 x 2 x 60 x 2 (channels) 10.09 megabytes
per CD minute - A CD is 74 minutes (746 megabytes) long.
- MP3 players typically use memory 32 or 64
megabytes in size. (or 3-6 minutes) - Compression is needed
56Frequency domain
- Sounds are made up of frequencies.
- We often want to filter out certain frequencies
scratches, background noise, or because we are
unable to hear them. - How do we do such things?
57Enter Jean Baptiste Joseph Fourier
He lived 1768-1830 in France Developed the idea
of building trigonometric series for turning
signals measured over time into signals measured
by frequency His ideas are the basis of
digital signal processing (DSP)
58Demonstration
59Representing Images
- Start from analog representation (waveform) of
the image - Then digitize
- Eventually, consider frequencies
60Digital resolution
- A printer will display 300 or 600 or 1200 dots
per inch (dpi). - Visually
- Less than 150 dots per inch is tiring
- 200 dots per inch makes a very crisp screen
display - 600 dots per inch is now the norm
- 1200 dots per inch was what phototypesetters did
- Photograph negatives typically have 2000 dots per
inch - Each dot can have 256 shades of selected colors.
- Printers use Cyan, Yellow, Magenta and sometimes
blacK - Monitors use Red, Green and Blue .
61Numeracy
- An 8x10 picture at 600 dpi has
- 80 square inches
- 360,000 dots per square inch
- 28,800,000 dots per picture
- Each dots can be 3 bytes
- 86.4 Megabytes of storage
- So, compression is needed
62Representing Images
- Each dot is a picture element (or a pixel)
- So, a picture is just a stream of bytes.
- When we process the picture (as we will in the
next lab) we manipulate the bytes.
63Frequency domain for images
- Images are made up of frequencies.
- We often want to filter out certain frequencies
scratches, background of image, - By checking for frequencies, we can find places
where the image changes. - How do we do such things?
64The need for compression
- A CD is 746 megabytes long
- A printed page requires 86.4 Megabytes of storage
65Compression
- Start with text
- Eliminate duplication
- Be more clever
- Move to sounds and images
- Remove frequencies that wont be heard/seen
- Apply text ideas
- Be willing to lose part of the image/sound that
doesnt matter
66Simple compression
- A sentence to compress
- "Ask not what your country can do for you -- ask
what you can do for your country."
67Simple compression (cont.)
- "Ask not what your country can do for you -- ask
what you can do for your country." - The quote has 17 words, made up of 61 letters, 16
spaces, one dash and one period. Storing this,
takes 79 units of memory.
68How to shrink
- Use redundancy
- Immediately, we notice that
- "ask" , "what" , "your" , "country" , "can" ,
"do", "for" , and "you" appear two times.
69Shrink via redundancy
- Immediately, we notice that
- "ask" , "what" , "your" , "country" , "can" ,
"do", "for" , and "you" appear two times. - Build a dictionary
- 1 gt ask 2 gt what 3 gt your
- 4 gt country 5 gt can 6 gt do
- 7 gt for 8 gt you
- Our sentence now reads
- "1 not 2 3 4 5 6 7 8 -- 1 2 8 5 6 7 3 4
70Using redundancy
- Build a dictionary
- 1 gt ask 2 gt what 3 gt your
- 4 gt country 5 gt can 6 gt do
- 7 gt for 8 gt you
- Our sentence now reads
- "1 not 2 3 4 5 6 7 8 -- 1 2 8 5 6 7 3 4
- Ask what your country can do for you1 not 2 3 4
5 6 7 8 -- 1 2 8 5 6 7 3 4 - Memory usage
- 37 units and 37 units for dictionary 74 units
- Need a symbol to tell when the dictionary ends
- Need to know that there are no numbers or in
text
71Use patterns to further shrink
- New dictionary
- Ask_
- what_
- you
- r_country
- _can_do_for_you
- New encoding of sentence
- 1not_2345_--_12354"
- Ask_what_your_country _can_do_for_you1not_234
5_--_12354 - Memory usage
- 18 units and 41 units for dictionary 59 units
72Typical savings
- Text files (unstructured)
- Usually can compress by half
- Text files (structured e.g. computer programs)
- Compression is greater
- Music/Image files
- Can usually reduce by factor of 10 with some loss
- Jpeg, mp3, files
- Usually no savings theyre already compresed
73More complex compression
- Lossy compression
- Can save space by eliminating parts of the
image/sound that dont matter very much - Big savings compared to lossless
- Ideas
- Dropping frequencies you cant hear
- Dropping small differences you wont notice
- Generally large savings compared to lossless
74Formats
- GIF original graphics compression format
- Can be lossy or lossless
- Limits numbers of colors to 256 (28 vs. 224)
- Can be progressive
- JPEG (Joint Photographic Experts Group)
- Can adjust level of lossy-ness
- Most popular format
- MPEG (Moving Picture Experts Group)
- Built on JPEG
- Works also in the time dimension
75MPEG-3 Audio layer
- This is MP3
- Compresses sound by about a factor of 10
- Preserves CD quality sound