Download presentation
Presentation is loading. Please wait.
Published byShanon Watkins Modified over 8 years ago
1
Computers
2
Light A Review: State Machine IF A=1 AND B=0 Always IF A=0 AND B=1 Otherwise Light B Otherwise Always
3
Design for Any State Machine Memory Register Clock Inputs Logic For Next State & Output Outputs Current State Many bits
4
Review In designing state machines, we used: Binary Representation (to represent states) Memory (to store the current state) Logic Circuits & Universal Method (to determine the next state) In this way, we learned how to build special-purpose digital systems (e.g. traffic lights)
5
General Purpose Computers What makes a PC different? Today your PC can’t play Final Fantasy IX. Tonight, you buy the Final Fantasy IX CD-ROM Tomorrow your PC can play the game! PC’s are general-purpose computers. Realize state machines for traffic lights, digital cameras, VCR’s, … You can write a program (software) to control the circuitry (hardware) of a general purpose computer
6
The Stored Program Idea is simple: Build a machine that can execute certain instructions. Then, write down different lists of instructions (programs) to accomplish different things. One program plays an adventure game Another program lets you write papers Another lets you surf the Internet
7
Programming Using software to control hardware –Hardware is fixed circuitry –Software is instructions to the processor to control the inputs to the fixed circuitry Inputs are of 2 types –Data –Program We don’t differentiate
8
History of Programmable Machines First “programmable system” was the early printing process developed in China circa 800 C.E. First “program” was perhaps Chinese translation of Buddhist Canon (the Tipitaka)
9
History (cont.) Gutenberg’s Printing Press (circa 1450) Main Contribution: Just a few “basic instructions” (smaller alphabet size) suffice.
10
History (cont.) Jacquard’s Loom (circa 1810) Punched Cards stored program for weaving patterns.
11
History (cont.) Charles Babbage (1822-64): Input -- Punched Cards Hardware -- general-purpose mechanical mathematical system (Analytical Engine) -- never built Could be programmed punched card could say: “Go back 5 punched cards” Instructions could be Executed repeatedly, or in different order.
12
The Modern Computer Basic Idea still the same: A machine that can execute certain instructions. Machine instructions represented by sequences of 0’s and 1’s (Machine Language) Instructions stored in Memory von Neumann (1945) Princeton, NJ
13
Memory We’ve seen that memory can be used to build State Machines. In modern computers, memory is also used in a different way: Big arrays of Memory called RAM : Random Access Memory RAM is a place to store information while the computer is running.
14
RAM Take a single bit of memory… D Write M
15
RAM (cont.) Group 8 of them together. 8 bits (b) is called a byte (B). Most RAM is arranged in bytes. D M W D M W D M W D M W D M W D M W D M W D M W
16
RAM (cont.) A byte of memory (a.k.a. an 8-bit register) 8 bits (1 byte) of Memory 8 bits Output 8 bits input Write
17
RAM (cont.) We want a HUGE number of such 1 byte memory cells. 1B
18
RAM (cont.) We want a HUGE number of such 1 byte memory cells. Problem: How do we indicate which byte of memory we want to use at any given time? 1B
19
RAM (cont.) Solution: Assign each byte an address. We can number all the bytes in binary. Each byte’s assigned number is called the byte’s address in memory. Then, we can ask the RAM: What is byte number 011010101010? Or tell the RAM: Write “01101100” into memory at address 011010101010
20
RAM (cont.) 2 20 bytes of RAM (1 Mega-byte) Write Address Data input Data Output
21
RAM (cont.) 2 20 bytes of RAM (1 Mega-byte) Write Address Data input Data Output 20 bits of address
22
RAM (cont.) 2 20 bytes of RAM (1 Mega-byte) Write Address Data input Data Output 20 bits of address 8 bits (1 byte) of data
23
RAM (cont.) When you talk about the “memory” of a computer, most often you’re talking about its RAM. If a program is stored in RAM, that means that a sequence of instructions are stored in consecutively addressed bytes in the RAM. RAM is one (crucial) part of the computer’s overall architecture
24
Computer Architecture Bus CPU RAM Input/ Output Devices Central Processing Unit
26
Computer Architecture Bus CPU RAM Keyboard Hard Disk Display CD-ROM
27
The Bus Bus What is a bus? It is a simplified way for many devices to communicate to each other. Looks like a “highway” for information. Actually, more like a “basket” that they all share. CPU Keyboard Display
28
The Bus Bus CPU Keyboard Display
29
The Bus Bus Suppose CPU needs to check to see if the user typed anything. CPU Keyboard Display
30
The Bus Bus CPU puts “Keyboard, did the user type anything?” (represented in some way) on the Bus. CPU Keyboard Display “Keyboard, did the user type anything?”
31
The Bus Bus Each device (except CPU) is a State Machine that constantly checks to see what’s on the Bus. CPU Keyboard Display “Keyboard, did the user type anything?”
32
The Bus Bus Keyboard notices that its name is on the Bus, and reads info. Other devices ignore the info. CPU Keyboard Display “Keyboard, did the user type anything?”
33
The Bus Bus Keyboard then writes “CPU: Yes, user typed ‘a’.” to the Bus. CPU Keyboard Display “CPU: Yes, user typed ‘a’.”
34
The Bus Bus At some point, CPU reads the Bus, and gets the Keyboard’s response. CPU Keyboard Display “CPU: Yes, user typed ‘a’.”
35
Computer Architecture Bus CPU RAM Keyboard Hard Disk Display CD-ROM
36
The CPU The CPU is the brain of the computer. It is the part that actually executes the instructions. Let’s take a look inside.
37
The CPU (cont.) Memory Registers Register 0Register 1Register 2Register 3 Temporary Memory
38
The CPU (cont.) Memory Registers Register 0Register 1Register 2Register 3 Arithmetic / Logic Unit For doing basic Arithmetic / Logic Operations on Values stored in the Registers
39
The CPU (cont.) Memory Registers Register 0Register 1Register 2Register 3Instruction Register Arithmetic / Logic Unit To hold the current instruction
40
The CPU (cont.) Memory Registers Register 0Register 1Register 2Register 3Instruction RegisterInstr. Pointer (IP) Arithmetic / Logic Unit To hold the address of the current instruction in RAM
41
The CPU (cont.) Memory Registers Register 0Register 1Register 2Register 3Instruction RegisterInstr. Pointer (IP) Arithmetic / Logic Unit Control Unit (State Machine)
42
The Control Unit It all comes down to the Control Unit. This is just a State Machine. How does it work?
43
The Control Unit Control Unit State Machine has very simple structure: 1) Fetch: Ask the RAM for the instruction whose address is stored in IP. 2) Execute: There are only a small number of possible instructions. Depending on which it is, do what is necessary to execute it. 3) Repeat:Add 1 to the address stored in IP, and go back to Step 1 !
44
The Control Unit State Machine … … … … … Add Load Store Goto … Add 1 to IP Fetch
45
Putting it all together Bus CPU RAM Keyboard Hard Disk Display CD-ROM Computer has many parts, connected by a Bus:
46
Putting it all together Bus CPU RAM Keyboard Hard Disk Display CD-ROM The RAM is the computer’s main memory. This is where programs are stored.
47
Putting it all together Bus CPU RAM Keyboard Hard Disk Display CD-ROM The CPU goes in a never-ending cycle, reading instructions from RAM and executing them.
48
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)
49
Putting it all together It simply looks at where IP is pointing, reads the instruction there from RAM, and executes it. Memory Registers Register 0Register 1Register 2Register 3Instruction RegisterInstr. Pointer (IP) Arithmetic / Logic Unit Control Unit (State Machine)
50
Putting it all together In the process of executing the instruction, the Control Unit uses the Registers & the ALU. Memory Registers Register 0Register 1Register 2Register 3Instruction RegisterInstr. Pointer (IP) Arithmetic / Logic Unit Control Unit (State Machine)
51
Typical Instructions Some common instructions include: 1) “Load Direct” – Put a fixed value in one of theregisters (as specified) 2) “Load” – Load a value from RAM into one of the registers 3) “Store” -Store the value in a specified register to the RAM 4) “Add” - Add the contents of Registers 1 and 2 and put the result in Register 0
52
Typical Instructions Some common instructions include: 5) “Compare” - If the value in Register 1 is larger than the value in Register 2, put a “0” in Register 0 6) “Jump” -If the value of Register 0 is “0”, change IP to the value in Register 1 7) “Bus” - Put the value in Register 0 on the Bus.
53
Important Point One of the important features of the von Neumann model is the fact that: The instructions themselves And The data the instructions manipulate are all stored in the same RAM. This was one of the revolutionary features of the modern computer, totally unlike Punch-Card computers proposed in the past.
54
Important Point This innovation is crucial to modern computing. It even allows for the possibility of programs that change themselves as they are executed. With great power comes great risk…
55
Important Point This innovation is crucial to modern computing. It even allows for the possibility of programs that change themselves as they are executed. With great power comes great risk… Computer Viruses!
56
Machine Language We now have a machine that can execute instructions. Basic Questions: What instructions? How do computers understand these instructions? (Representation) What does software look like to a computer?
57
Machine Languages Different types of CPU’s understand different instructions Pentium / Pentium II / Pentium III Celeron / AMD K6 / Cyrix … (i86 family) PowerPC (Mac) DragonBall (Palm Pilot) Many Others… All have different representations of instructions
58
Simplicity Computers used to have very complicated instruction sets – this was known as: CISC = Complex Instruction Set Computer Almost all computers 15 years ago were CISC. Recently, trend toward: RISC = Reduced Instruction Set Computer
59
Simplicity RISC = Reduced Instruction Set Computer Fewer, Less powerful instructions But Simpler, Faster, Easier to design CPU’s Can make “powerful” instructions by combining several wimpy ones Now widely accepted to be a better idea than Complex Instruction Set Computer (CISC) for everyday PC’s.
60
Simplicity Nevertheless, Pentium is actually CISC ! Why?
61
Simplicity Nevertheless, Pentium is actually CISC ! Why: Compatibility with older software But Pentiums are gradually moving towards simpler RISC model Certain complex old instructions are scheduled to be eliminated one day.
62
Our Example CPU We’ll look at a toy CPU’s Machine Language: Our CPU has: 8 Registers – each holds 16 bits (2 bytes)
63
CPU Memory Registers Register 0Register 1Register 2Register 3Instruction RegisterInstr. Pointer (IP) Arithmetic / Logic Unit Control Unit (State Machine)
64
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
65
Writing it down We are going to be looking at lots of 16-bit sequences. E.g. 0110110010100101 It can very tiring / confusing to read so many bits. We’ll use a shorthand notation.
66
Shorthand: Hexadecimal 0123456701234567 0000000100100011010001010110011100000001001000110100010101100111 89ABCDEF89ABCDEF 1000100110101011110011011110111110001001101010111100110111101111
67
Hex Shorthand e.g. 0123456701234567 0000000100100011010001010110011100000001001000110100010101100111 89ABCDEF89ABCDEF 1000100110101011110011011110111110001001101010111100110111101111 0110 1100 1010 0101
68
Hex Shorthand e.g. 0123456701234567 0000000100100011010001010110011100000001001000110100010101100111 89ABCDEF89ABCDEF 1000100110101011110011011110111110001001101010111100110111101111 0110 1100 1010 0101 6 C A 5
69
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
70
Representing Instructions Machine instructions will also be represented by 16 bits. We’ll divide those bits up into groups: The first 4 bits is called the Op-Code: This indicates what type of instruction it is. How many possibilities does 4 bits give us? 6 C A 5
71
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
72
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
73
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)
74
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
75
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
76
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
77
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)
78
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)
79
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)
80
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)
81
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)
82
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)
83
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
84
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)
85
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
86
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:.......
87
We looked at a typical Machine Language and how instructions are represented. Halt Instruction Data Instructions Arithmetic/Logic Instructions Control Instructions Memory Instructions Summary
88
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.)
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.