Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 07 – October 11, 2001 Programming our toy machine Review of hardware Storing images and sounds Compression Administrivia –send questions to

Similar presentations


Presentation on theme: "Lecture 07 – October 11, 2001 Programming our toy machine Review of hardware Storing images and sounds Compression Administrivia –send questions to"— Presentation transcript:

1 Lecture 07 – October 11, 2001 Programming our toy machine Review of hardware Storing images and sounds Compression Administrivia –send questions to cs111@cs.princeton.edu not cs111@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

2 CPU Memory Registers Register 0Register 1Register 2Register 3Instruction RegisterInstr. Pointer (IP) Arithmetic / Logic Unit Control Unit (State Machine)

3 Our Example CPU We’ll look at a toy CPU’s Machine Language: Our CPU has: 8 Registers – each holds 16 bits (2 bytes) Our RAM: Works in blocks of 16 bits (2 bytes) Has 2 8 = 256 addresses Total Memory: 512 bytes

4 Machine “Core” Everything is in binary (hex) Registers R0: 0000 R1: 0CA8 R2: A9DB R3: 0705 R4: 1011 R5: 90A0 R6: 0807 R7: 00A0 00: 0000000000000000 04: 0000000000000000 08: 0000000000000000 0C: 0000000000000000 10: B106B200B0011221 … F8: 0000000000000000 FC: 0000000000000000 Memory

5 Representing Instructions Machine instructions will also be represented by 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

6 Instructions 16 possible Op-Codes: We’ll only look at a few in 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

7 Halt Instruction 0 : Halt This just tells the machine to 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 F F F 0 9 A C

8 Data Instructions B : Load Direct / Address Sets a Register to a fixed Specified Value Encoding: Effect: Register A becomes the 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)

9 Arithmetic/Logic Instructions 1 : Add Adds contents of two registers together and puts result in third register Encoding: Effect: Register A becomes Register 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

10 Adding Numbers Simple Program to calculate 1 + 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 (R2=1) 14: Add R1  R1 + R0 (R1=2) 15: Add R2  R2 + R1 (R2=3) 16: Add R1  R1 + R0 (R1=3) 17: Add R2  R2 + R1 (R2=6) 18: Add R1  R1 + R0 (R1=4) 19: Add R2  R2 + R1 (R2=A) 1A: Add R1  R1 + R0 (R1=5) 1B: Add R2  R2 + R1 (R2=F) 1C: Add R1  R1 + R0 (R1=6) 1D: Add R2  R2 + R1 (R2=15) 1E: halt

11 Adding Numbers Simple Program to calculate 1 + 2 + 3 + 4 + 5 + 6 = 21 = 15 (hex) Let’s watch it execute

12 Arithmetic/Logic Instructions 2 : Subtract Similar to Add… Encoding: Effect: Register A becomes Register 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

13 Control Instructions 6 : Jump if Positive Jump to a different place in memory if Register is > 0 Encoding: Effect: If Register A > 0, Go To Instruction at 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 6 regA address (8 bits)

14 Adding Numbers Use a Loop to calculate 1 + 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 (R1>0) (If N isn’t 0 yet, go back) 16: halt (N is now 0, and R2 = 1+2+…+N)

15 Adding Numbers Use a Loop to calculate 1 + 2 + 3 + 4 + 5 + … + N Execution

16 Control Instructions 7 : Jump and count (backwards) Jump to a different place in memory if Register is > 0, AND decrement by 1 Encoding: Effect: If Register A > 0, Go To Instruction at specified address, 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)

17 Adding Numbers Alternate Loop using Jump & Count for 1 + 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 isn’t 0 yet, decrease R1 if (R1>0) Let N=N-1, and go back) 14: halt (N is now 0, and R2 = 1+2+…+N)

18 Adding Numbers Alternate Loop using Jump & Count for 1 + 2 + 3 + 4 + 5 + … + N Execution

19 Memory Instructions 9 : Load (from memory) Load from Memory into Register Encoding: Effect: Load from RAM at specified address 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 address (8 bits)

20 Memory Instructions A : Store (into memory) Store Register into Memory Encoding: Effect: Store contents of Register A into memory at 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)

21 Memory Instructions: Indirect 9 : Load (from memory) Load from Memory into Register Encoding: Effect: Load from RAM at address [B+C] 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

22 How can CPU tell these apart? Sneaky trick: CPU has 8 Registers, so only need 3 bits to specify Register A Use extra bit to tell which type of LOAD Memory Instructions: Indirect 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 9 regA address (8 bits)

23 This is called a HACK! Hacks are terrible! Generally should be avoided. Sadly, they are everywhere. Some CS people get perverse pleasure out of hacks. This is a major source of bugs! Hacks 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

24 A 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  Memory[R3+R1] (Load instruction from source) 14: Store Memory[R2+R1]  R0 (Store instruction in destination) 15: Jump to 13 and (If haven’t copied whole virus, decrease R1 if (R1>0) decrease R1, and go back) 16: halt (Virus has replicated)... ______________________________________________________________ 20:.... (Program about to be destroyed.) 21:.......

25 A Virus! Copies itself on top of other programs! Execution

26 We looked at a typical Machine Language and how instructions are represented. Halt Instruction Data Instructions Arithmetic/Logic Instructions Control Instructions Memory Instructions Summary

27 We saw a few simple examples of programs you can write in Machine Language. You now have a pretty solid understanding of how computers really work ! Summary (cont.)

28 Hardware Wrap-up

29 Layers of Abstraction Build more and more powerful tools out of simpler ones. Really Simple Stuff Computers

30 Logic Gates AND X Y Z OR X Y Z X Y NOT X Y

31 Universal Method AND A C B A C A C B B A B C F 0000 0011 0101 0110 1000 1010 1101 1 110 First, make circuits for each row in Truth Table with a 1 in the output.

32 OR Universal Method (cont.) AND A C B A C A C B B Finally, combine them with an OR gate. F

33 Universality 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 gates out of Transistors.

34 Representing Information Information in the world comes in many forms – letters, numbers, pictures, sounds… How can we represent these things with 0’s and 1’s? Start with numbers.

35 The Flip-Flop OR AND Set Reset M 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

36 “Snapshot” Memory 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.) D Write M

37 Design for Any State Machine Memory Register Clock Inputs Logic For Next State & Output Outputs Current State

38 Review On our way to building a computer… Computers We are here … Represent & Manipulate Info. with 0’s & 1’s (aka bits) Memory Circuits State Machines

39 Putting it all together Bus CPU RAM Keyboard Hard Disk Display CD-ROM Computer has many parts, connected by a Bus: Each part is a state machine.

40 Putting it all together This cycle is orchestrated by the Control Unit in the CPU. Memory Registers Register 0Register 1Register 2Register 3Instruction RegisterInstr. Pointer (IP) Arithmetic / Logic Unit Control Unit (State Machine)

41 Instructions 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

42 Adding Numbers Simple Program to calculate 1 + 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 (R2=1) 14: Add R1  R1 + R0 (R1=2) 15: Add R2  R2 + R1 (R2=3) 16: Add R1  R1 + R0 (R1=3) 17: Add R2  R2 + R1 (R2=6) 18: Add R1  R1 + R0 (R1=4) 19: Add R2  R2 + R1 (R2=A) 1A: Add R1  R1 + R0 (R1=5) 1B: Add R2  R2 + R1 (R2=F) 1C: Add R1  R1 + R0 (R1=6) 1D: Add R2  R2 + R1 (R2=15) 1E: halt

43 Adding Numbers Use a Loop to calculate 1 + 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 (R1>0) (If N isn’t 0 yet, go back) 16: halt (N is now 0, and R2 = 1+2+…+N)

44 A 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  Memory[R3+R1] (Load instruction from source) 14: Store Memory[R2+R1]  R0 (Store instruction in destination) 15: Jump to 13 and (If haven’t copied whole virus, decrease R1 if (R1>0) decrease R1, and go back) 16: halt (Virus has replicated)... ______________________________________________________________ 20:.... (Program about to be destroyed.) 21:.......

45 We’ve covered the basics of computers. What we’ve 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) Further Directions

46 Sounds 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

47 Representing sounds Start from waveform of the sound Then digitize Eventually, consider frequencies

48 The waveform

49 Sampling

50 The approximation

51 Finer sampling

52 Even finer sampling

53 Analog 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.

54 Digital 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.

55 Numeracy 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

56 Frequency 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?

57 Enter 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)

58 Demonstration Fourier transform

59 Representing Images Start from analog representation (waveform) of the image Then digitize Eventually, consider frequencies

60 Digital 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.

61 Numeracy An 8”x10” 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

62 Representing 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.

63 Frequency 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?

64 The need for compression A CD is 746 megabytes long A printed page requires 86.4 Megabytes of storage

65 Compression Start with text –Eliminate duplication –Be more clever Move to sounds and images –Remove frequencies that won’t be heard/seen –Apply text ideas –Be willing to lose part of the image/sound that doesn’t matter

66 Simple compression A sentence to compress –"Ask not what your country can do for you - - ask what you can do for your country."

67 Simple 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.

68 How to shrink Use redundancy Immediately, we notice that: "ask", "what", "your", "country", "can", "do", "for", and "you" appear two times.

69 Shrink via redundancy Immediately, we notice that: "ask", "what", "your", "country", "can", "do", "for", and "you" appear two times. Build a dictionary: 1 => ask 2 => what3 => your 4 => country5 => can6 => do 7 => for8 => you Our sentence now reads: "1 not 2 3 4 5 6 7 8 -- 1 2 8 5 6 7 3 4“

70 Using redundancy Build a dictionary: 1 => ask 2 => what3 => your 4 => country5 => can6 => do 7 => for8 => 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 you*1 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

71 Use patterns to further shrink New dictionary 1.Ask_ 2.what_ 3.you 4.r_country 5._can_do_for_you New encoding of sentence –“1not_2345_--_12354" Ask_#what_#you#r_country# _can_do_for_you*1not_2345_--_12354 Memory usage –18 units and 41 units for dictionary = 59 units

72 Typical 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 – they’re already compresed

73 More complex compression Lossy compression –Can save space by eliminating parts of the image/sound that don’t matter very much –Big savings compared to lossless –Ideas Dropping frequencies you can’t hear Dropping small differences you won’t notice –Generally large savings compared to lossless

74 Formats GIF – original graphics compression format –Can be lossy or lossless –Limits numbers of colors to 256 (2 8 vs. 2 24 ) –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

75 MPEG-3 Audio layer This is MP3 Compresses sound by about a factor of 10 Preserves CD quality sound


Download ppt "Lecture 07 – October 11, 2001 Programming our toy machine Review of hardware Storing images and sounds Compression Administrivia –send questions to"

Similar presentations


Ads by Google