69
COL100: Introduction to Computers and Programming: Lecture 1: Introduction Instructor : Prof Saroj Kaushik Institure : IIT Delhi (CSE) Email : saroj@ cse.iitd.ernet.in Web Link : http://www.cse.iitd.ac.in/~saroj Lecture Module 1

Lecture Module 1 - Department of Computer Science and

  • Upload
    others

  • View
    1

  • Download
    0

Embed Size (px)

Citation preview

COL100: Introduction to Computers and Programming: Lecture 1: Introduction

Instructor : Prof Saroj Kaushik Institure : IIT Delhi (CSE)Email : saroj@ cse.iitd.ernet.inWeb Link : http://www.cse.iitd.ac.in/~saroj

Lecture Module 1

Introduction to Computers, memory, ALU, CPU, I/O devices Data representations, Number System Conversion from one base to another Compliment representations of negative integers, Floating point representationIntroduction to system software; operating systems, compilers and multi-user environments,Introduction to Computer Problem Solving, Fundamental algorithms, Program Design methods

Course Contents

Prof Saroj Kaushik, CSE, IIT Delhi

• Concept of an algorithm; Algorithms to programs: specification, top-down development and stepwise refinement.

• Use of high level programming language for the systematic development of programs.

• Introduction to the design and implementation of correct, efficient and maintainable programs using some high level programming language

• Array techniques, Multidimensional arrays, Structured Data types, Merging, Sorting & searching, Dynamic Data structure algorithms using pointers, Functions & procedures & recursive algorithms,

• Introduction to Object oriented programming language (C++)

Course Contents - Cont

Prof Saroj Kaushik, CSE, IIT Delhi

TEXT BOOKS

◦ R.G. Dromey, "How to Solve it by Computer", Prentice Hall of India, 1992.

◦ E Balagurusamy, "Programming in ANSI C", edition 2.1, Tata McGraw-Hill, reprint 2002.

◦ B.W. Kernighan & D.M. Ritchie, "The C Programming Language", Prentice Hall of India, 1989.

◦ N Barkakati, "Object-Oriented Programming in C++", Prentice Hall

Books

Prof Saroj Kaushik, CSE, IIT Delhi

• REFERENCE BOOKS– Cooper,Mullish, "The Spirit of C", Jaico Publishing

House, New Delhi, 1987.– Richard Johnson-Baugh & Martin Kalin,

"Application Programming in C", Macmillan International editions, 1990.

– Herbert Schildt, "The complete Reference C, Fourth Edition", Tata McGraw-Hill

– N. A. Solter and S. J. Kleper, Professional C++, Wiley India

Books – Contd..

Prof Saroj Kaushik, CSE, IIT Delhi

Course Grading Structure

• These weights are indicative, and may change as semester progresses– Minor 1 & 2, each with 15-20% weightage– Major, 30-35% weightage– Quizzes and home assignments, 5-10%

weightage– Programming exercises, 20-25% weightage

Prof Saroj Kaushik, CSE, IIT Delhi

Lab Schedule• Lab Schedule : Lab groups: E1 and E2• Lab sessions are primarily for learning coding of

programs. • Testing of programming skill will be tested and graded in

labs only• Labs will start from 13th August onwards• E1 and E2 swap alternate week

Lectures Sat- (10am-12pm) Sun – (8-10am)Lab Gps Days Timings

E1 Monday 1 pm - 4 pmE2 Saturday/

Sunday3 pm - 5 pm1 pm - 3 pm

Prof Saroj Kaushik, CSE, IIT Delhi

Introduction to Computers

What is a Computer?

• Electronic device• Capable of performing

– arithmetic operations on numbers– logical operations on bits (binary digit)– decision test– retrieval, manipulation and storage of large

amount of data at a very high speed

Prof Saroj Kaushik, CSE, IIT Delhi

Generation of Computers• Generation in computer terminology is a change in

technology a computer is/was being used. – Initially, the generation term was used to distinguish

between varying hardware technologies. – But nowadays, generation includes both hardware and

software, which together make up an entire computer system.

• There are totally five computer generations known till date.

Prof Saroj Kaushik, CSE, IIT Delhi

Summary of Generations

Characteristics Gen I Gen II Gen III Gen IV Gen V Period 1946-1958 1959-1964 1965-1970 1971-1980 1981-till

date Components The

Vacuum Tube

Transistors Integrated circuits

LSI (Large Scale Integration

VLSI (Very large scale integration

Complexity of circuits

One circuit component

Few circuits components

80-100 circuits components

Over 100 circuits components

Over 1000 circuits components

Relative failure rate

1 0.01 0.001 0.0001 Very low

Languages used

Machine language

Assembly language, FORTRAN, COBOL

User oriented Languages

Online commands

Object oriented languages

Type of system Single user Single user Multi user Remote user

Remote user on internet

What Does A Computer Do?

• Computers can perform four generaloperations, which comprise the informationprocessing cycle.– Input– Process– Output– Storage

• To do each of these computer has devices.

Prof Saroj Kaushik, CSE, IIT Delhi

13

Devices that comprise a computer system

Printer (output)

Monitor (output)

Speaker (output)

Scanner (input)

Mouse (input)

Keyboard (input)

System unit(processor, memory…)

Storage devices(CD‐RW, Floppy, Hard disk, zip,…)

Prof Saroj Kaushik, CSE, IIT Delhi

14

Why Is a Computer So Powerful?

• The ability to perform the informationprocessing cycle with amazing speed.

• Reliability (low failure rate).• Accuracy.• Ability to store huge amounts of data and

information.• Ability to communicate with other

computers.Prof Saroj Kaushik, CSE, IIT Delhi

• By 1980, computers were classified into three main types.• Main Frame:

– Traditionally large computers, often containing thousands of IC’s and costing millions of dollars

– High speed, large storage & highly accurate– can be shared by group of users

• Mini Computers:– Smaller in size, slow in speed, low cost and can be shared by group of

users

• Micro Computers (Personal computer (PC)):– New class of general purpose machines intended for single users. – Small in size and inexpensive

Types of Computers

Developments in microprocessor technology however means that today'sdesktop/laptops/tablets etc.Now a days microcomputer are available with more RAM and disk storage.The differences between mainframe, minicomputers and a desktop modelmay more likely be in the software that is being used as opposed to thehardware.For example, a multi-user operating system such as Unix, would typicallybe used, as opposed to the Windows systems of PCs in mainframe andminicomputer.Mainframe computers are still powerful machines (physically muchsmaller than their ancestors) with hundreds of megabytes of RAM andterabytes of disk storage.They have very powerful CPU's that allow them cope with large numbersof users.WorkStation It is also a single user computer system which is similar topersonal computer but have more powerful microprocessor.

Contd…

The following schematic diagram gives the layout of a digital computer.

The current architecture of PC (personal Computer) is as follows (major hardware components )

• The bus is a group of wires on the main circuit board of the computer through which electrical signal passes.

• It is a pathway for data flowing between components. • Buses are of three types:

– Address bus: • used to transmit address of memory location

– Data bus:• Used to transmit data from/to memory

– Control bus:• Supervises reading/writing of data

• Most devices are connected to the bus through a controller • Controller coordinates the activities of the device and the bus.

Contd..

Supercomputer is the term used for the most powerful computer available at any time. These are typically tailored for very fast processing of what are known as number crunching applications. Such applications require a tremendous number of arithmetic calculations to be carried out. Weather forecasting is the classic example of such an application, where equations taking account of huge numbers of observations have to be solved. Other applications are to be found in astronomy and some branches of chemical analysis and modeling. Supercomputers at the moment can carry out trillions of operations per second! The Cray is perhaps the most well known supercomputer named after its designer, Seymour Cray.

Super Computer

21

How Does a Computer Know what to do?

• It must be given a detailed list of instructions,called a computer program or software, that tellswhat exactly it has to do.

• Before processing a specific job, the computerprogram corresponding to that job must be createdand stored in memory.

• Once the program is stored in memory thecomputer can start the operation by executing theprogram instructions one after the other.

Prof Saroj Kaushik, CSE, IIT Delhi

Computer System• A computer system is made up of both

– Hardware electronic components

– Software Program consisting of set of instruction

• Example: Some of the Anology:– Mobile is hardware and corresponding

facilities available is software.– CD is hardware and music on it is software.

Prof Saroj Kaushik, CSE, IIT Delhi

Hardware Components Of A Computer ?

• Input devices.• Output devices.• Central Processing

Unit• Memory.• Storage devices.

Prof Saroj Kaushik, CSE, IIT Delhi

The Keyboard

• The most commonly used input device is the keyboard onwhich data is entered by manually keying in or typingcertain keys.

• A keyboard typically has 101 or 105 keys.

Prof Saroj Kaushik, CSE, IIT Delhi

The Mouse

• Is a pointing device which is used to control the movement of a mouse pointer on the screen to make selections from the screen.

• A mouse has one to five buttons. The bottom of the mouse is flat and contains a mechanism that detects movement of the mouse.

Prof Saroj Kaushik, CSE, IIT Delhi

Output Devices• Output devices make the information resulting from the

processing available for use.• The two output devices more commonly used are the

printer and the computer screen.• The printer produces a hard copy of your output, and the

computer screen produces a soft copy of your output.

Prof Saroj Kaushik, CSE, IIT Delhi

The Central processing Unit, CPU• The CPU contains electronic circuits that cause processing

to occur. • Does the `work‘ of fetching, storing and manipulating

values that are stored in the computers memory.• It is considered the “brain” of the computer.

Prof Saroj Kaushik, CSE, IIT Delhi

Main Components of CPU

• Arithmetic Logic Unit (ALU)– The ALU carries out arithmetic operations (e.g.

addition and subtraction) and logical operations (e.g. and, or, not)

• Control Unit (CU)– The CU controls the execution of instructions.

Prof Saroj Kaushik, CSE, IIT Delhi

The processor as its name suggests is the unit that does thework of the computer system i.e.◦ it executes computer programs.

Instructions in the program tell the processor◦ when and what to read from a keyboard;◦ what to display on a screen;◦ what to store and retrieve from a disk drive and so on.

It can do arithmetic, compare numbers and performinput/output. (read information and display or store it).It has no magical powers. It is instructive to bear in mind thatall computer programs are constructed from sequences ofinstructions based on such primitive operations.

CPU-Contd…

• The clock rate typically refers to the frequency at which CPU, is running and is used as an indicator of the processor's speed.

• It is measured in clock cycles per second or its equivalent, the SI unit hertz (Hz).

• The clock rate of the first generation of computers was measured in hertz or kilohertz (kHz),

• In 21st century the speed of modern CPUs is commonly advertised in GigaHertz (GHz).

• Modern Computer performance is primarily affected by the processor and memory.

Processor’s speed

Memory• Memory consists of electronic components that

store data including numbers, characters, graphicsand sound.

• It has different types of memory.– Random Access Memory (RAM)– Read only Memory (ROM)– MROM (Masked ROM)– PROM (Programmable Read only Memory)– EPROM(Erasable and Programmable Read Only

Memory)

Prof Saroj Kaushik, CSE, IIT Delhi

Random Access Memory – RAM• RAM is where programs and data are kept during

execution when the processor is actively usingthem.

• RAM is called Main memory or Primary memory.• Main memory is:

• very closely connected to the processor.• the contents are quickly and easily changed.• holds the programs and data that the processor is

actively working with.• interacts with the processor millions of times per

second.Prof Saroj Kaushik, CSE, IIT Delhi

RAM – Contd…

• RAM– Is Volatile - temporary– Has random access to any memory

location– Read & write access– Program + data reside in this memory

Prof Saroj Kaushik, CSE, IIT Delhi

Read only Memory - ROM

• Read only Memory (ROM)– Non volatile– Only read access– Permanent programs reside

Prof Saroj Kaushik, CSE, IIT Delhi

Types of Memory – Contd..• MROM (Masked ROM)

– The very first ROMs were hard-wired devices that contained a pre-programmed set of data or instructions.

– These kind of ROMs are known as masked ROMs which are inexpensive.

• PROM (Programmable Read only Memory)– PROM is read-only memory that can be modified only

once by a user. – The user buys a blank PROM and enters the desired

contents using a PROM program.

Prof Saroj Kaushik, CSE, IIT Delhi

Types of Memory – Contd..• EPROM(Erasable and Programmable Read Only

Memory)– The EPROM can be erased by exposing it to ultra-

violet light for a duration of up to 40 minutes. – During programming, an electrical charge is trapped in

an insulated gate region. – For erasing this charge, ultra-violet light is passed

through a quartz crystal window(lid). – This exposure to ultra-violet light dissipates the charge.

During normal use the quartz lid is sealed with a sticker.

Prof Saroj Kaushik, CSE, IIT Delhi

Cache Memory

• A special and very high speed memory used toincrease the speed of processing.

• Cache is sometimes called CPU cache• Transfer from cache to CPU is very fast as

compared to from main memory to CPU.

Main Memory

CPU

ALU + CU

Cache

Prof Saroj Kaushik, CSE, IIT Delhi

Contd….

• It lies between CPU and main memory.• The cache is usually filled from main memory

when instructions or data are fetched into the CPU.• Often the main memory will supply a wider data

word to the cache than the CPU requires, to fill thecache more rapidly.

• It is expensive and usually small in size.• Stores only segments of programs currently being

executed by CPU.• It is also sometimes called buffer.

Prof Saroj Kaushik, CSE, IIT Delhi

Storage Devices- Secondary memory

• Connected to main memory• The contents are easily changed, but this is very slow

compared to main memory.• It is used for long-term storage of programs and data.• The processor only occasionally interacts with

secondary memory.• The most common types of secondary storage used are

– Hard disks– CD-ROM drives– Flash Drives (USB, pen drives)

Prof Saroj Kaushik, CSE, IIT Delhi

Hard Disk

• The hard disk has enormous storagecapacity compared to main memory– 300 times the amount of storage in

main memory

• The hard disk is usually contained inthe systems unit of a computer.

Prof Saroj Kaushik, CSE, IIT Delhi

Reasons for having two types of storage

Primary memory

1. Fast 2. Expensive 3. Low capacity 4. Connects directly to

the processor

Secondary memory

1. Slow 2. Cheap 3. Large capacity 4. Not connected

directly to the processor

Prof Saroj Kaushik, CSE, IIT Delhi

What is a Port

• A port is a physical docking point using which an external device can be connected to the computer.

• Information flows from a program to computer or over the internet using port.

Prof Saroj Kaushik, CSE, IIT Delhi

Characteristics of a Port• A port has the following characteristics:

– External devices are connected to a computer using cables and ports.

– Ports are slots on the motherboard into which a cable of external device is plugged in.

– Examples of external devices attached via ports are mouse, keyboard, monitor, microphone, speakers etc.

Prof Saroj Kaushik, CSE, IIT Delhi

Data Representation in Memory

• Data is stored inside the memory in the form of 1’s and 0’s, Bits (Binary Digits) as shown below (ON/OFF positions). – For example

1100 0111 0011 11110101 1110

• One byte consists of 8 bits.Prof Saroj Kaushik, CSE, IIT Delhi

Contd…

• How many distinct values can be stored in one byte?

• Answer: 28

• Minimum Value: 0 0000 0000

• Maximum value: 28–1 1111 1111

Prof Saroj Kaushik, CSE, IIT Delhi

Organization of Main Memory• Main memory consists of a very long list of bytes. • In most modern computers, each byte has an address that

is used to locate it. • The picture shows a small part of main memory:

Prof Saroj Kaushik, CSE, IIT Delhi

Contd…• Each row is called memory location that is a single

byte and has an address.• The addresses are the integers to the left of the boxes:

0, 1, 2, 3, 4, ... 255 for location of one byte.• The addresses for most computer memory start at 0

and go up in sequence until each byte has an address.• The bits at a memory location are called the contents

of that location.• Each location contains a pattern of eight bits, each bit

is either 0 or 1.• When people say that a computer has "128 MB of

RAM" they are talking about how big its mainmemory is.

Prof Saroj Kaushik, CSE, IIT Delhi

Units of Memory

• 1 KB (1 KiloByte) contains 1024 Bytes. • 1 MB (1 MegaByte) consists of 1024 KB.• 1 GB (1 GigaByte) has 1024 MB.• 1 TB (1 Terabyte consists of 1024 GB.

Prof Saroj Kaushik, CSE, IIT Delhi

Table of units of measurementName Equivalent Number of Bytes power of 2

Byte 8 bits 1 20

Kilobyte (KB)

1024 bytes 1024 210

Megabyte (MB)

1024 KB 1,048,576 220

Gigabyte (GB)

1024 MB 1,073,741,824 230

Terabyte 1024 GB 1,099,511,627,776 240

Prof Saroj Kaushik, CSE, IIT Delhi

Software• Computer programmers/users write the codes/

instructions that make-up softwareapplications/programs.

• Set of instructions is also referred to as lines ofcode.

• Without software a computer is useless, just as acar without someone to drive it.

• To get a computer to perform a specific task, itmust be given a sequence of unambiguousinstructions called a program written in someprogramming language.

Prof Saroj Kaushik, CSE, IIT Delhi

Types of Programs• There are two categories of programs.

– Application programs (usually called just"applications") that people use to get their work done.

– Systems programs keep all the hardware and softwarerunning together smoothly.

• The difference between "application program" and"system program" is fuzzy.

• Often it is more a matter of marketing than oflogic.

Prof Saroj Kaushik, CSE, IIT Delhi

Application Programs

• Word processors• Game programs• Spreadsheets • Data base packages• Graphics programs • Web browsers

Systems Programs

• Operating system. • Networking system. • Database system. • Compilers • Web site server. • Data backup.

Prof Saroj Kaushik, CSE, IIT Delhi

Example of Types of Software

Application Software

Operating system Software

Prof Saroj Kaushik, CSE, IIT Delhi

Operating Systems (OS)• OS coordinates the operation of all the hardware and

software components of the computer system.• The operating system is responsible for starting

application programs running and finding theresources that they need.

• When an application program is running, the operatingsystem manages the details of the hardware for it.

• It is there in the background managing resources,doing input and output for the application, andkeeping everything else running.

• The operating system is always present when thecomputer is running.

Prof Saroj Kaushik, CSE, IIT Delhi

Resources managed by OS• OS is a complex collection of many programs that

governs the control of various resources such as:– Processor– Main Memory– Secondary storage– I/O devices– Files

• The operating system is software; the samehardware can be used with many differentoperating systems (although only one at a time.)

Prof Saroj Kaushik, CSE, IIT Delhi

Various Modules of OS• There are various modules of OS

– Processor management– Memory management– Device management– Information management

• These modules resolve conflicts, optimizeperformance and acts as an interface between theuser’s program and computer hardware.

• Modern operating systems usually come with auser interface that enables users to easily interactwith application programs by using windows,buttons, menus, icons, the mouse, and thekeyboard.

Prof Saroj Kaushik, CSE, IIT Delhi

• Examples of operating systems are– DOS, Unix, Windows 98, Windows NT, Linux,

Solaris, etc.• There are different OS for different kinds of

machines.– Single user machine– Batch processing– Multiprogramming– Time sharing– Real time machines

Prof Saroj Kaushik, CSE, IIT Delhi

Programming LanguagesMachine Lang Assembly Lang High level Lang

Consists of 0 & 1 Pseudo instAdd 2 5

English like instsInput A, B

Comp System directly executes it

Assembler translates to M/L

Compiler translates to M/L

Difficult to write Simple to write Simpler to write

Highly error prone Less error prone Lesser error prone

Machine dependent Machine dependent Machine independent

Prof Saroj Kaushik, CSE, IIT Delhi

Machine Language• Consider machine of 16-bits

Opcode (4-bits) Address (12-bits)

• There are 16 possible opcodes (Hypothetical example)Opcode Action0000 Load accumulator (ACC)from memory0001 Store accumulator in memory0010 Add the contents of Acc with specified

location and leave the result in ACC

1111 stopProf Saroj Kaushik, CSE, IIT Delhi

M/L program

Instructions

• Load ACC from Loc 10

• Add ACC with contents of Loc 20

• Store ACC contents in Loc 30

• Stop

Machine language

• 0000 0000 0000 1010

• 0010 0000 0001 0100

• 0001 0000 0001 1110

• 1111 0000 0000 0000

Prof Saroj Kaushik, CSE, IIT Delhi

Assembly Language

• Load X• Add Y• Store Z• Print Z• Stop

High level Language (pascal, C, C++, Java)

• Input X, Y• Z = X+Y• Output Z• Stop

• One assembly instruction is equivalent to machine instruction

• One high level inst corresponds to many machine instruction Prof Saroj Kaushik, CSE, IIT Delhi

• All CPUs have an instruction set (or language) that they understand.

• Eventually all assembly level / high level programs must be translated (or compiled) into instructions from this set.

• Roughly speaking, all processors have the same sort of instructions available to them.

• An executable program is machine language program that runs on a CPU and is always represented as a series of binary digits.

• This is achieved by compiling (translating) a high-level program with a special piece of software called a compiler.

• Compilers are incredibly complicated programs that accept other programs as input and generate a binary executable object file as output.

Prof Saroj Kaushik, CSE, IIT Delhi

Types of Files• The OS can take a

– program file, copy it into main memory, and start it running. – supplies data files and its information to a running program

when it asks. • Last part of a file's name (the extension) shows what

the file is expected to be used for. For example, – "mydata.txt" the ".txt" means that the file is expected to be

used as a collection of text, that is, characters.– “netscape.exe" the ".exe" means that the file is an

"executable," that is, a program that is ready to run. – "program1.py" the ".py" means that the file is a source

program in the language PYTHON

Prof Saroj Kaushik, CSE, IIT Delhi

Interpreter• There are some language which are not compiled but

interpreted by interpreter (Python, BASIC, SML, …).• An interpreter is a program that acts like a processor that

can directly execute a high level language.

file_name.pyPython Interpreter

Processor

Interpreting a Source Program

Prof Saroj Kaushik, CSE, IIT Delhi

• Here the source program “file_name.py" has beenwritten in Python (a programming language) by aprogrammer with a text editor i.e.,gedit file_name.py.

• It is being interpreted by the PYTHON interpreter,which is running on the processor.

• The PYTHON interpreter will read each command inthe source program and do what it says.

• When an interpreter is running a PYTHON sourceprogram, both the interpreter and the source programare in main memory.

• The interpreter consists of machine instructions thatthe hardware can execute directly.

Prof Saroj Kaushik, CSE, IIT Delhi

Translator verses Interpreter

• Translator– Takes a complete document in one language

and creates a complete document in anotherlanguage, which can then be used at any time.

– Translator is called compiler.• Interpreter

– Acts as an intermediate between sourcelanguage and machine language and convertsinstruction wise.

Prof Saroj Kaushik, CSE, IIT Delhi

Compiler• A source program is set of instructions written in a high level

language.. • It is translated into a machine language program by a system

software called compiler.• It takes a source file as input and produces an executable

program (machine language program) as output.

Prof Saroj Kaushik, CSE, IIT Delhi

Contd..• The source program is stored in a file created using a text

editor.• The source file is kept on the hard disk.• When you have to run your program (Source program),

first compile by compiler and form an executable file.• The source file remains unchanged; a new executable file

is created.• The executable file is also kept on hard disk.• Compilers are specific to high level languages (like "C")

and a specific to processor type (like "Pentium"), andonly runs under a specific operating system (like"Windows".)

• The above is what goes on with most languages: Ada,Pascal, C, C++, FORTRAN and others.

Prof Saroj Kaushik, CSE, IIT Delhi

Example Problem :

Consider the problem of converting F (Fahrenheit) to C(Centigrade) which is suitable for solution by computer.

Formula: C = 5 * (F – 32) / 9• Set of possible high level instructions:Input : F (given parameters)Output: C (output parameters)

read FC = 5 * (F – 32 ) / 9print Cend

Prof Saroj Kaushik, CSE, IIT Delhi