500 likes | 624 Views
1.2 What is a Computer?. Computer Device capable of performing computations and making logical decisions Computers process data under the control of sets of instructions called computer programs programmable electronic device that can store, retrieve and process data.”.
E N D
1.2 What is a Computer? • Computer • Device capable of performing computations and making logical decisions • Computers process data under the control of sets of instructions called computer programs • programmable electronic device that can store, retrieve and process data.”
Components of a Computer • Hardware • Various devices comprising a computer • Keyboard, screen, mouse, disks, memory, CD-ROM, and processing units • Software • Programs that run on a computer Computer Hardware Physical Devices Software Instructions & Data
1.3 Computer Organization • Six logical units in every computer: • Input unit • Obtains information from input devices (keyboard, mouse) • Output unit • Outputs information (to screen, to printer, to control other devices) • Memory unit • Rapid access, low capacity, stores input information • Arithmetic and logic unit (ALU) • Performs arithmetic calculations and logic decisions • Central processing unit (CPU) • Supervises and coordinates the other sections of the computer • Secondary storage unit • Cheap, long-term, high-capacity storage • Stores inactive programs
Central Processing Unit (CPU) • The Central Processing Unit(CPU), also called the processor, is the “brain” of the computer • Examples • Intel Pentium 4/AMD Athlon (Windows PCs) • Intel Itanium2 (servers) • Qualcomm MSM (cell phones) • IBM POWER6 (servers) • Sun UltraSPARC (servers)
CPU Parts • The CPU consists of three main parts: • Control Unit • Arithmetic/Logic Unit • Registers Registers Control Unit Arithmetic/Logic Unit Fetch Next Instruction Add Sub Integer Fetch Data Store Data Mult Div Increment Instruction Ptr Floating Point And Or Execute Instruction … Not …
CPU: Control Unit • Control Unit : decides what to do next. • For example: • memory operations: for example, • load data from main memory (RAM) into the registers; • store data from the registers into main memory; • arithmetic/logical operations: e.g., add, multiply; • branch: choose among several possible courses of action.
CPU: Arithmetic/Logic Unit • Arithmetic/Logic Unit(ALU): performs arithmetic and logical operations. • Arithmetic operations: • add, subtract, multiply, divide, square root, cosine, etc. • Logical operations: • compare two numbers to see which is greater, check whether a true/false statement is true, etc.
CPU: Registers • Register : • memory-like location inside the CPU where data and instructions reside that are being used right now. • hold the operands being used by the current arithmetic or logical operation, or the result of the arithmetic or logical operation that was just performed. • For example • if the CPU is adding two numbers, then the addend is in some register, the augend is in another register, and after the addition is performed, the sum shows up in yet another register. • A typical CPU has only a few hundred to a few thousand bytes of registers.
Storage There are two major categories of storage: • Primary : • Cache • Main memory (RAM) • Secondary • Hard disk • Removable (e.g., CD, floppy)
Primary Storage • is where data and instructions reside when they’re being used by a program that is currently running. • Typically is volatile: The data disappear when the power is turned off. • Cache • Main memory (RAM)
Cache • is where data and instructions reside when they are going to be used very very soon, or have just been used. • Cache is very • fast(typically 20% - 100% of the speed of the registers). • expensive (e.g., $5 per MB) http://www.pricewatch.com/ • small (e.g., 1/4 MB to 12 MB) … but still much bigger than registers.
CPU 351 GB/sec on a 1.83 GHz Pentium4 Core Duo 253 GB/sec (72%) on a 1.83 GHz Pentium4 Core Duo Cache
Main Memory (RAM) • is where data and instructions reside when a program that is currently running is going to use them at some point during the run (whether soon or not). • Much • slower than cache - about 1-5% of CPU speed for RAM, vs 20-100% of CPU speed for cache) • cheaper than cache - $0.03/MB for RAM vs $5/MB for cache) • larger than cache - 1-32 GB for RAM vs 1/4 MB to 12 MB for cache)
Secondary Storage • Where data and instructions reside that are going to be used in the future • Nonvolatile: data don’t disappear when power is turned off. • Much • slower than RAM • Cheaper • larger. • Other than hard disk, most are portable: they can be easily removed from your computer and taken to someone else’s.
Input/Output (I/O ): Input Devices • Input Devicestransfer data into computer (e.g., from a user into memory). • Keyboard • Mouse • Scanner • Microphone • Touchpad • Joystick • Output Devices transfer data out of computer (e.g., from memory to a user). • Monitor • Printer • Speakers
What is Software? A Program? Data? • Software: • for our purposes, is just a word that means “programs.” • A program: • is a collection of data – on RAM, disk, etc – and a sequence of actions on those data. • The actions in a program are known as instructions. • In computing, data : • are values stored in storage locations: RAM, disk, etc.
What are Instructions? • The actions in a program are known as instructions. • Examples: • Arithmetic/Logical calculation: e.g., add, subtract, multiply, divide, square root, cosine, etc. • Memory operations: load from or store into RAM • I/O: read from or write to secondary storage • Branch: jump to an instruction that is out of sequence • Repetition • Allocation of resources … and many more.
What is a Programming Language? A programming languageis a well-defined set of rules for specifying a program’s collection of data and sequence of instructions. Examples: C, C++, Fortran 90, Java, Basic, HTML, Perl, Haskell, Prolog, Pascal, Unix shell, SAS, Pentium4 assembly language, etc.
What is Source Code? What is a Source File? Source codeis a sequence of instructions, written in a human-readableprogramming language, that constitutes a program, or a piece of a program. #include <stdio.h> int main () { printf("Hello, world!\n"); } A source file is a file of source code.
What is an Operating System? An operating systemis a program that manages interactions between: users and hardware; users and software; hardware and software; ... and so much more. • MS Windows/MS-DOS • MacOS • PalmOS • Unix • Linux (portable) • FreeBSD (portable, underlies MacOS X) • Solaris (Sun Microsystems) • AIX (IBM) • IRIX (SGI) • Tru64 (Hewlett-Packard) • HP-UX (Hewlett-Packard) • Unicos (Cray)
1.4 Early Operating Systems • Batch processing • Do only one job or task at a time • Operating systems • Manage transitions between jobs • Increased throughput • Amount of work computers process • Multitasking • Computer resources are shared by many jobs or tasks • Timesharing • Computer runs a small portion of one user’s job then moves on to service the next user
1.5 Personal Computing, Distributed Computing, and Client/Server Computing • Personal computers • Economical enough for individual • Distributed computing • Computing distributed over networks • Client/server computing • Sharing of information across computer networks between file servers and clients (personal computers)
1.6 Machine Languages, Assembly Languages, and High-level Languages Three types of programming languages • Machine languages • Strings of numbers giving machine specific instructions • Example: +1300042774 +1400593419 +1200274027 • Assembly languages • English-like abbreviations representing elementary computer operations (translated via assemblers) • Example: LOAD BASEPAY ADD OVERPAY STORE GROSSPAY
1.6 Machine Languages, Assembly Languages, and High-level Languages Three types of programming languages (continued) • High-level languages • Codes similar to everyday English • Use mathematical notations (translated via compilers) • Example: • grossPay = basePay + overTimePay
1.7 Fortran, COBOL, Pascal and Ada • Fortran • developed by IBM Corporation in the 1950s • used for scientific and engineering applications that require complex mathematical computations • COBOL • developed in 1959 by computer manufacturers, the government and industrial computer users • used for commercial applications that require precise and efficient manipulation of large amounts of data
1.7 Fortran, COBOL, Pascal and Ada • Pascal • Developed by Professor Niklaus Wirth in 1971 • Designed for teaching structured programming • Ada • Developed under the sponsorship of the U.S. Department of Defense (DOD) during the 1970s and early 1980s • Able to perform multitasking
1.8 History of C • C • Evolved by Ritchie from two previous programming languages, BCPL and B • Used to develop UNIX • Used to write modern operating systems • Hardware independent (portable) • By late 1970's C had evolved to "Traditional C" • Standardization • Many slight variations of C existed, and were incompatible • Committee formed to create a "unambiguous, machine-independent" definition • Standard created in 1989, updated in 1999
Portability Tip 1.1 • Because C is a hardware-independent, widely available language, applications written in C can run with little or no modifications on a wide range of different computer systems.
1.9 C Standard Library • C programs consist of pieces/modules called functions • A programmer can create his own functions • Advantage: the programmer knows exactly how it works • Disadvantage: time consuming • Programmers will often use the C library functions • Use these as building blocks • Avoid re-inventing the wheel • If a pre-made function exists, generally best to use it rather than write your own • Library functions carefully written, efficient, and portable
What is a language? Kinds of languages Natural languages Programming languages (also known as Formal languages) Converting between programming languages Compilers Interpreters Assemblers Languages
Symbols: a set of wordsand punctuation(in computing, words and punctuation are together known as tokens) Grammar(also known as syntax): a set of rules for putting tokens together to get valid statements Semantics: a set of rules for interpreting the meaningof a grammatically valid statement Ingredients of a Language
Natural languages: used in human communication Programming languages (also known as formal languages): used by computers (among others) Kinds of Languages
Examples: C, Java, HTML, Haskell, Prolog, SAS Also known as formal languages Completely described and rigidly governed by formal rules Cannot mix the words of multiple languages, or the syntax of multiple languages, in the same program Cannot be ambiguous Words and syntax must be EXACTLYcorrect in every way Programming Languages
High Level Languages Assembly Languages Machine Languages Programming Language Hierarchy
Human-readable Most are standardized, so they can be used on just about any kind of computer. Examples: C, Fortran 90, Java, HTML, Haskell, SAS Typically they are designed for a particular kind of application; for example: C for operating system design Fortran 90 for scientific & engineering applications Java for web applets and embedded systems HTML for hypertext (webpages) SAS for statistics But often, their uses in real life are broader their original purpose. High Level Languages
Human-readable Specific to a particular CPU family; for example: Intel Pentium4/AMD (PC) IBM PowerPC (Macintosh until recently) Qualcomm MSM (cell phones) So, for example, a program in Pentium4 assembly language cannot be directly run on a PowerPC machine. Set of simple commands; for example: Load a value from a location in main memory Add two numbers Branch to an instruction out of sequence Assembly Languages
Not human-readable, except with immense effort Binary code that the CPU family understands directly Binary representation of the CPU family’s assembly language Machine Languages
Compilers, interpreters and assemblers are programs that convert human-readable source code into machine-readable executable code. Converting Between Languages
Converts a human-readable high level language source code of a program into a machine language executableprogram Converts an entire source code all at once Must be completed before executing the program Examples: Fortran 90, C, C++, Pascal Compiler
Converts a human-readable high level language source code into actions that are immediately performed Converts and executes one statement at a time Conversion and execution alternate Examples: Perl, HTML, SAS, Mathematica, Unix “shell” (interactive system within Unix) Interpreter
Converts a human-readable CPU-specific assembly codeinto CPU-specific, non-human-readable machine language Like a compiler, but for a low level assembly language instead of for a high level language Assembler
hello_world.c /* ************************************************* *** Program: hello_world *** *** Author: ProgrammerName(Pr@students.edu) *** *** Course: CSCS1304 *** *** Lab: Sec 101 / 102 Sat 10:00am *** *** Description: Prints the sentence *** *** "Hello, world!" to standard output. *** ************************************************* */ #include <stdio.h> int main () { /* main */ /* ******************************** *** Execution Section (body) *** ******************************** * * Print the sentence to standard output * (i.e., to the terminal screen). */ printf("Hello, world!\n"); rteurn 0; } /* main */
Lexical Analyzer: identifies program’s “word” elements Comments (ignored by compiler) Keywords(e.g., int, while) Constants(e.g., 5, 0.725, "Hello, world!") User-defined Identifiers (e.g., addend) Operators; for example: Arithmetic: + - * / % Relational: == != < <= > >= Logical: && || ! Elements of a Compiler #1
Parser: determines the program’s grammar Semantic Analyzer: determines what the program does Intermediate Code Generator: expresses, as an assembly-like program, what the program does Optimizer: makes code more efficient (faster) Assembly Code Generator: produces the final assembly code that represents what the program does Elements of a Compiler #2
Compiler Assembler: turns assembly code into machine code Linker/loader: turns machine code into an executable file Both the assembler and the linker/loader are invoked automatically by the compiler, so you don’t have to worry about them. Phases of Compiling
1.14 Typical C Program Development Environment • Phases of C++ Programs: • Edit • Preprocess • Compile • Link • Load • Execute Fig. 1.1 | Typical C development environment.
Bugs? Bugs? Get an A/Impress Your Boss/Sell for Zillions! The Programming Process Compile Formulate Problem Yes Debug Construct Algorithm No Choose Programming Language Run Yes Write Program No
An algorithm is: a step-by-step method that is written in a natural language (e.g., English) or in pseudocode(something that sort of looks like a programming language but isn’t as precise), rather than in a programming language, that solves a well-defined (but not necessarily useful) problem, on a well-defined set of inputs (which may be empty), using finite resources(e.g., computing time and memory), and that produces a well-defined set of outputs (which may be empty). What is an Algorithm?
An algorithm is a language-independentway of expressing the method of solving a problem; that is, an algorithm could be expressed in two different languages (e.g., English and Japanese) and still be the same algorithm. A program, by contrast, is a language-dependent implementation of the method of solving a problem; that is, the same set of steps expressed in two different programming languages would be two different programs, even if the two programs accomplished exactly the same result. Many programs, but not all, implement algorithms. Algorithms