1.2 What is a Computer? Computer –Device capable of performing computations and making logical...

50
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.” 1

Transcript of 1.2 What is a Computer? Computer –Device capable of performing computations and making logical...

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.”

1

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

HardwarePhysical Devices

SoftwareInstructions & Data

1.3 Computer Organization

• Six logical units in every computer:1. Input unit

• Obtains information from input devices (keyboard, mouse)2. Output unit

• Outputs information (to screen, to printer, to control other devices)3. Memory unit

• Rapid access, low capacity, stores input information4. Arithmetic and logic unit (ALU)

• Performs arithmetic calculations and logic decisions5. Central processing unit (CPU)

• Supervises and coordinates the other sections of the computer6. Secondary storage unit

• Cheap, long-term, high-capacity storage• Stores inactive programs

3

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

Arithmetic/Logic UnitControl Unit Registers

Fetch Next Instruction Add Sub

Mult Div

And Or

Not …

Integer

Floating Point

Fetch Data Store Data

Increment Instruction Ptr

Execute Instruction

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

Cache

253 GB/sec (72%) on a1.83 GHz Pentium4 Core Duo

351 GB/sec on a1.83 GHz Pentium4 Core Duo

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.

Speed, Price, Size

Medium Speed (MB/sec)

Size (MB)

Media Type

Can write to it?

Port-able?

Pop-ular?

Drive cost ($)

Media cost ($/MB)

Cache 269,257 12 L2/L3 Y N Req’d $5

RAM 21,328 32,768 DDR2 Y N Req’d $0.03

Hard Disk 100 1,500,000 Mag Y N Req’d $0.0001

Blu-ray 17 25,000 Opt Y Y Soon $120 $0.0002

DVD+RW 16 8500 Opt Y Y Y $24 $0.00003

CD-RW 7.6 700 Opt Y Y Y $14 $0.0002

Mag tape 15 800,000 Mag Y Y N $2000 $0.00006

Floppy 0.03 1.44 Mag Y Y Y $9 $0.09

Cassette << 1 << 1 Mag Y Y Historical

Paper tape << 1 << 1 Paper Y Y Historical

Punch card << 1 << 1 Paper Y Y Historical

Input/Output (I/O ): Input Devices• Input Devices transfer 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 language is 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 code is a sequence of instructions, written in a human-readable programming 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 system is 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

22

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)

23

1.6 Machine Languages, Assembly Languages, and High-level Languages

Three types of programming languages1. Machine languages

• Strings of numbers giving machine specific instructions• Example:

+1300042774+1400593419+1200274027

2. Assembly languages• English-like abbreviations representing elementary computer

operations (translated via assemblers)• Example:

LOAD BASEPAYADD OVERPAYSTORE GROSSPAY

24

1.6 Machine Languages, Assembly Languages, and High-level Languages

Three types of programming languages (continued)3. High-level languages

• Codes similar to everyday English• Use mathematical notations (translated via

compilers)• Example:

– grossPay = basePay + overTimePay

25

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

26

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

27

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

28

2007 Pearson Education, Inc. All rights reserved.

29

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

30

31

Languages

• What is a language?• Kinds of languages

– Natural languages

– Programming languages (also known as Formal languages)

• Converting between programming languages– Compilers

– Interpreters

– Assemblers

32

Ingredients of a Language

• Symbols: a set of words and 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 meaning of a grammatically valid statement

33

Kinds of Languages

• Natural languages: used in human communication

• Programming languages (also known as formal languages): used by computers (among others)

34

Programming 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 EXACTLY correct in every way

Keywords

auto double int struct

break else long switch

case enum register typedef

char extern return union

const float short unsigned

continue for signed void

default goto sizeof volatile

do if static while

35

Programming Language Hierarchy

• High Level Languages

• Assembly Languages

• Machine Languages

36

High Level Languages

• 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.

37

Assembly 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

38

Machine 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

39

Converting Between Languages

Compilers, interpreters and assemblers are programs that convert human-readable source code into machine-readable executable code.

40

Compiler

• Converts a human-readable high level language source code of a program into a machine language executable program

• Converts an entire source code all at once

• Must be completed before executing the program

• Examples: Fortran 90, C, C++, Pascal

41

Interpreter

• 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)

42

Assembler

• Converts a human-readable CPU-specific assembly code into CPU-specific, non-human-readable machine language

• Like a compiler, but for a low level assembly language instead of for a high level language

43

hello_world.c

/* ************************************************* *** Program: hello_world *** *** Author: ProgrammerName([email protected]) *** *** 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 */

44

Elements of a Compiler #1

• 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: && || !

45

Elements of a Compiler #2

• 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

46

Phases of Compiling

• 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.

1.14 Typical C Program Development Environment

•Phases of C++ Programs:–Edit–Preprocess–Compile–Link–Load–Execute

47

Fig. 1.1 | Typical C development environment.

48

The Programming Process

Formulate Problem

Construct Algorithm

Choose Programming Language

Write Program

Compile

Run

Debug

Get an A/Impress Your Boss/Sell for Zillions!

Bugs?

Bugs?

No

Yes

No

Yes

49

What is an Algorithm?

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).

50

Algorithms

An algorithm is a language-independent way 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.