800 likes | 814 Views
Computer Programming. CS 1 Introduction to Computers and Computer Technology Rick Graziani Fall 2015. Computer Architecture. Central Processing Unit (CPU) or processor Arithmetic/Logic unit (ALU) Performs operations on data such as addition and subtraction Control unit
E N D
Computer Programming CS 1 Introduction to Computers and Computer Technology Rick Graziani Fall 2015
Computer Architecture • Central Processing Unit (CPU) or processor • Arithmetic/Logic unit (ALU) • Performs operations on data such as addition and subtraction • Control unit • Coordinating the CPU’s activities • Holds input and results (output) for the ALU • Registers • Temporary storage for the CPU • General registers • Special purpose registers Rick Graziani graziani@cabrillo.edu
A CPU can be: A CPU can be: 1. A series of integrated circuits (chips) on one or more circuit boards • Older mainframe and minicomputers 2. On a single integrated circuit known as a microprocessor microprocessor = a CPU on a single chip microcomputer = older term for a computer with a microprocessor(s) (PC, Macintosh) Rick Graziani graziani@cabrillo.edu
Computer Architecture • Bus • Used to transfer bits between the CPU and RAM (main memory) Rick Graziani graziani@cabrillo.edu
User interface User Types (Input) 2 + = 3 Computer Outputs 5 Rick Graziani graziani@cabrillo.edu
Computer Architecture • Task: Add two values stored in main memory (RAM) • Data (two values) must be transferred from main memory to registers within the CPU • ALU: Values are added • Result stored in main memory (RAM) Value = 5 2 + 3 = 5 Value = 2 Value = 3 Rick Graziani graziani@cabrillo.edu
Computer Architecture • Task: Add two values stored in main memory (RAM) • Data (two values) must be transferred from main memory to registers within the CPU • ALU: Values are added • Result stored in main memory (RAM) 2 + 3 = 5 5 2 2 3 3 2 + 3 = 5 Rick Graziani graziani@cabrillo.edu
Our CPU and the Pentium CPU Rick Graziani graziani@cabrillo.edu
Stored Program Concept • Stored program concept: A program can be encoded as bit patterns and stored in main memory. • CPU can then: • extract the instructions as needed (copy them into its registers) • execute them Program instruction Rick Graziani graziani@cabrillo.edu
Terminology • Machine instruction: An instruction (or command) encoded as a bit pattern recognizable by the CPU • Machine language: The set of all instructions recognized by a machine (CPU) Op-code Operand Description 1 RXY LOAD reg. R from cell XY. 2 RXY LOAD reg. R with XY. 3 RXY STORE reg. R at XY. 4 0RS MOVE R to S. 5 RST ADD S and T into R. (2’s comp.) 6 RST ADD S and T into R. (floating pt.) 7 RST OR S and T into R. 8 RST AND S and T into R. 9 RST XOR S and T into R. A R0X ROTATE reg. R X times. B RXY JUMP to XY if R = reg. 0. C 000 HALT. Rick Graziani graziani@cabrillo.edu
Machine Instruction Types • Machine Instruction Types • Data Transfer: Copy data from one location to another • Arithmetic/Logic: Use existing bit patterns to compute a new bit patterns • Control: Direct the execution of the program • More in a moment Op-code Operand Description 1 RXY LOAD reg. R from cell XY. 2 RXY LOAD reg. R with XY. 3 RXY STORE reg. R at XY. 4 0RS MOVE R to S. 5 RST ADD S and T into R. (2’s comp.) 6 RST ADD S and T into R. (floating pt.) 7 RST OR S and T into R. 8 RST AND S and T into R. 9 RST XOR S and T into R. A R0X ROTATE reg. R X times. B RXY JUMP to XY if R = reg. 0. C 000 HALT. Rick Graziani graziani@cabrillo.edu
Adding values stored in memory 2 3 2+3=5 5 Rick Graziani graziani@cabrillo.edu
Using machine language Rick Graziani graziani@cabrillo.edu
Computer Architecture 2 + 3 = 5 6E 5 0 5 6C 2 5 2 6 3 6D 3 Rick Graziani graziani@cabrillo.edu
Adding values stored in memory Step 1 2 + 3 = 5 6E 5 0 5 Step 2 6C 2 5 2 6 3 6D 3 Step 3 Step 4 Step 5 • Data Transfer • Instructions that request the movement (copying) of data from one location to another Rick Graziani graziani@cabrillo.edu
Adding values stored in memory Step 1 2 + 3 = 5 6E 5 0 5 Step 2 6C 2 5 2 6 3 6D 3 Step 3 Step 4 Step 5 • ALU • Instructions that tells the control unit to request an activity within the ALU • Capable of performing operations other than basic arithmetic operations, including: AND, OR, XOR. Rick Graziani graziani@cabrillo.edu
Remember our half-adder? Adding two bits • Adding two, eight bit values involves a similar process, just more gates (2 + 3 = 5): • 00000010 + 00000011 = 00001001 XOR 0 Inputs: A, B S = Sum C = Carry 1 1 0 AND C S 0 + 1 ---- A + B = 2’s1’s 0 1 = 0 1 1 Rick Graziani graziani@cabrillo.edu
Adding values stored in memory Step 1 Step 2 Step 3 Step 4 Step 5 • Control • Instructions that direct the execution of the program Jump or Branch instructions: Directs the CPU to execute an instruction other than the next one in the list. • Unconditional Jump: Skip to Step 6 • Condition Jump: If the value is 0 then Skip to Step 6 Rick Graziani graziani@cabrillo.edu
The architecture of our machine Address 00000000 00000001 00000010 00000011 11111111 • Main Memory • 256 cells: 00 through FF (Hex) • 0000 0000 through 1111 1111 • Storage: 8 bits per cell • CPU • 16 registers: 0 through F (Hex); 8 bits per cell • Program counter: (Address of) Keeps track of the next instruction • Instruction register: Contains the current instruction to be executed by the ALU. Rick Graziani graziani@cabrillo.edu
Converting Decimal, Hex, and Binary Dec. Hex. Binary Dec. Hex. Binary 0 0 0000 8 8 1000 1 1 0001 9 9 1001 2 2 0010 10 A 1010 3 3 0011 11 B 1011 4 4 0100 12 C 1100 5 5 0101 13 D 1101 6 6 0110 14 E 1110 7 7 0111 15 F 1111 1 Hex digit = 4 bits Rick Graziani graziani@cabrillo.edu
Loading instructions into the computer Rick Graziani graziani@cabrillo.edu
Op-code Operand Description 1 LOAD reg. R from cell XY. 2 LOAD reg. R with XY. 3 STORE reg. R at XY. 4 MOVE R to S. 5 ADD S and T into R. (2’s comp.) 6 ADD S and T into R. (floating pt.) 7 OR S and T into R. 8 AND S and T into R. 9 XOR S and T into R. A ROTATE reg. R X times. B JUMP to XY if R = reg. 0. C HALT. Parts of a Machine Instruction Machine Language • Each instruction involves two parts: • Op-code: Specifies which operation to execute • LOAD, ADD, STORE, etc. • Operand: Gives more detailed information about the operation • Interpretation of operand varies depending on op-code 0011 0101 1010 0111 Binary (16 bits) 1 Hex digit = 4 bits Rick Graziani graziani@cabrillo.edu
Op-code Operand Description 1 LOAD reg. R from cell XY. 2 LOAD reg. R with XY. 3 STORE reg. R at XY. 4 MOVE R to S. 5 ADD S and T into R. (2’s comp.) 6 ADD S and T into R. (floating pt.) 7 OR S and T into R. 8 AND S and T into R. 9 XOR S and T into R. A ROTATE reg. R X times. B JUMP to XY if R = reg. 0. C HALT. Parts of a Machine Instruction Store the bits found in register 5 in main memory cell A7 A 7 8 8 5 1010 0111 Rick Graziani graziani@cabrillo.edu
The machine cycle Rick Graziani graziani@cabrillo.edu
Program Execution Rick Graziani graziani@cabrillo.edu
The program is stored in main memory ready for execution 1 Hex digit = 4 bits Hard Disk Drive 00 FF • The program is copied put into main memory. • Typically from permanent storage. • Requires two memory cells per instruction: • Instructions are 16 bits; memory cells are 8 bits • Program counter contains first instruction: A0 Rick Graziani graziani@cabrillo.edu
Performing the fetch step of the machine cycle Rick Graziani graziani@cabrillo.edu
Program Counter Registers • At the beginning of the first fetch: • Program Counter = A0 A0 0 5 Instruction Register 6 … 02 6C 6D 03 6E Rick Graziani graziani@cabrillo.edu
Program Counter Registers • At the beginning of the first fetch: • Instruction at A0 (and A1) loaded into Instruction register • Program Counter = A2 0 A2 A0 5 Instruction Register 6 156C … 6C 02 6D 03 6E Rick Graziani graziani@cabrillo.edu
Program Counter Registers • CPU analyzes the instruction • Loads Register 5 with the contents of memory cell address 6C. A2 0 5 02 Instruction Register 6 156C … 6C 02 Loadregister 5 in main memory cell 6C 6D 03 6E Rick Graziani graziani@cabrillo.edu
Program Counter Registers • At the beginning of the next fetch: • Program Counter = A2 A2 0 5 02 Instruction Register 6 … 6C 02 6D 03 6E Rick Graziani graziani@cabrillo.edu
Program Counter Registers • Instruction at A2 (and A3) loaded into Instruction register • Program Counter incremented: A4 • CPU analyzes the instruction • Loads Register 6 with the contents of memory cell address 6D. A4 A2 0 5 02 Instruction Register 6 03 166D … 6C 02 Loadregister 6 in main memory cell 6D 6D 03 6E Rick Graziani graziani@cabrillo.edu
Program Counter Registers • At the beginning of the next fetch: • Program Counter = A4 A4 0 2 02 Instruction Register 3 03 … 6C 02 6D 03 6E Rick Graziani graziani@cabrillo.edu
Program Counter Registers • Instruction at A4 (and A5) loaded into Instruction register • Program Counter incremented: A6 • CPU analyzes the instruction • Adds contents of register 5 and register 6, storing the result into register 0. A6 A4 0 05 5 02 Instruction Register 6 03 5056 … 6C 02 Add: result into register 0, adding contents ofregister 5 and register 6 6D 03 6E Adds contents of register 5 and 6, placing result into register 0. Rick Graziani graziani@cabrillo.edu
Program Counter Registers • At the beginning of the next fetch: • Program Counter = A6 A6 0 05 2 02 Instruction Register 3 03 … 6C 02 6D 03 6E Rick Graziani graziani@cabrillo.edu
Program Counter Registers • Instruction at A6 (and A7) loaded into Instruction register • Program Counter incremented: A8 • CPU analyzes the instruction • Stores contents of register 0 in the memory cell at address 6E. A8 0 05 A6 5 02 Instruction Register 6 03 306E … 6C 02 Store contents of register 0, in memory celladdress 6E 6D 03 6E 05 Rick Graziani graziani@cabrillo.edu
Program Counter Registers • At the beginning of the next fetch: • Program Counter = A8 A8 0 05 5 02 Instruction Register 6 03 … 6C 02 6D 03 6E 05 Rick Graziani graziani@cabrillo.edu
Program Counter Registers • Instruction at A8 (and A9) loaded into Instruction register • Program Counter incremented: AA • CPU analyzes the instruction • Halts the program. (Program ends.) AA A8 0 05 5 02 Instruction Register 6 03 C000 … 6C 02 Halt the program 6D 03 6E 05 Rick Graziani graziani@cabrillo.edu
Algorithm • An algorithm is an ordered set of unambiguous, executable steps that defines a terminating process. • Algorithms are part of many activities, even mundane ones. • Note: Researchers believe that the human mind including imagination, creativity, and decision making, is actually the result of algorithm execution. • This is used in artificial intelligence Rick Graziani graziani@cabrillo.edu
Example • Obtain a basket of unshelled peas and an empty bowl. • As long as there are unshelled peas in the basket continue to execute the following steps: a. Take a pea from the basket b. Break open the pea pod c. Dump the peas from the pod into the bowl d. Discard the pod Rick Graziani graziani@cabrillo.edu
Defining the Algorithm Non-terminating sequence • An algorithm is an ordered set of unambiguous, executable steps that defines a terminating process. • Steps do not have to be executed in sequence. • Non-Terminating Sequence: • Make a list of positive integers. • The above requirement could not be performed in an algorithm, because it does not terminate (it is infinite). • Unambiguous • The instructions must be clear, specific and direct • No room for creativity or interpretation 1 2 3 4 5 6 7 8 9 10 11 … (this could go on for ever!) Ambiguous Organize the CDs (By title? By artist? By genre?) Rick Graziani graziani@cabrillo.edu
Abstract Nature of Algorithms • An algorithm can represented in several ways. • Example: Algorithm to convert temperatures from Celsius to Fahrenheit: • As an algebraic formula: • F = (9/5)C + 32 • As a written instruction: • Multiply the temperature reading in Celsius by 9/5 and then add 32 Rick Graziani graziani@cabrillo.edu
Algorithm Representation • Algorithm requires some form of a language. • Algorithm is a form of communication • Don’t want misunderstandings • Proper level of detail • Proper level of difficulty • Problems arise when: • Steps not precisely defined • Not enough detail Rick Graziani graziani@cabrillo.edu
Algorithm Representation Op-code Description 1 LOAD reg. R from cell XY. 2 LOAD reg. R with XY. 3 STORE reg. R at XY. 4 MOVE R to S. 5 ADD S and T into R. (2’s comp.) 6 ADD S and T into R. (floating pt.) 7 OR S and T into R. 8 AND S and T into R. 9 XOR S and T into R. A ROTATE reg. R X times. B JUMP to XY if R = reg. 0. C HALT. • Primitive – A well defined set of building blocks (terms) used in computer science. • Arithmetic and logic operations built into the language • Removes any ambiguity • Includes its own syntax • Programming language – A collection of primitives (terms) and the rules that state how the primitives can be combined to represent more complex ideas. LOAD reg. R from cell XY Rick Graziani graziani@cabrillo.edu
Op-code Operand Description 1 LOAD reg. R from cell XY. 2 LOAD reg. R with XY. 3 STORE reg. R at XY. 4 MOVE R to S. 5 ADD S and T into R. (2’s comp.) 6 ADD S and T into R. (floating pt.) 7 OR S and T into R. 8 AND S and T into R. 9 XOR S and T into R. A ROTATE reg. R X times. B JUMP to XY if R = reg. 0. C HALT. • Machine language uses primitives • High-level programming languages (C++, Java) use higher-level primitives, constructed from the lower-level machine language primitives. • This results in an easier set of instructions to write. • More later. Store the bits found in register 5 in main memory cell A7 Rick Graziani graziani@cabrillo.edu
Pseudocode • Pseudocode – A notational system in which ideas can be expressed informally during the algorithm development process. (written sentence) • Used independently of the programming language. • Each programming language has its own primitives and rules. Pseudocode Enter two numbers. Add the numbers together. Display the result. Rick Graziani graziani@cabrillo.edu
Pseudocode Conditional selection • The selection of one of two possible activities depending upon the truth or falseness of some condition ifconditionthenaction or ifconditionthen(activity) else (activity) • If this condition is true, perform this activity. If (sunny) then (put on sunscreen) • If this condition is true, perform this activity, otherwise perform a different activity. If (sunny) then (go swimming) else (go bowling) Rick Graziani graziani@cabrillo.edu
Repeating structure • Another common semantic structure is the repeated execution of a statement or sequence of statements as long as some condition remains true. whileconditiondoactivity • Also known as a while loop • Examples: while (tickets remain to be sold) do (sell a ticket) Rick Graziani graziani@cabrillo.edu
Repeating structure Task: Write Hello 500 times. Pseudocode Counter = 1 While counter is less than or equal to 500, write the word “Hello” and add 1 to Counter. Programming Code Counter = 1 While (counter <= 500) do (print “Hello”; Counter Counter + 1) Hello Counter Hello 500 501 4 2 3 1 Hello Hello … <End of loop> Hello Rick Graziani graziani@cabrillo.edu
For loop • A for loop can be used to accomplish the same thing as a while loop. • Note: There are some differences between while and for loops. Counter = 1 For (Counter <= 500) do (print the message “Hello”; Counter Counter + 1) Hello Counter Hello 500 501 4 2 3 1 Hello Hello … <End of loop> Hello Rick Graziani graziani@cabrillo.edu