Chapter1 - CPU
Chapter1 - CPU
Chapter1 - CPU
Chapter
1-1
Syllabus :
Programming Languages : Machine language, Assembly language, High level language, Compilers and
interpreters
Problem Solving using computers : Algorithms and flowcharts
1.1
Introduction :
GTU - June 2010.
A Computer is divided into 2 categories - software and hardware. Hardware is the physical
component of the computer, where as software is the logical component of the computer. The
computer is intelligent device, where intelligence depends on the software. The comparison
between hardware and software is given below.
Hardware Vs Software
Sr.
No.
Hardware
component
Software
1.
It is a physical
computer.
of
2.
User can see and feel the hardware User cannot see the logical components but
components.
can feel it.
3.
Hardware is divided into 3 part that are Software is divided into two categories that
microprocessor,
mother
board,
and are system software and application
peripherals.
software.
4.
5.
Sr.
No.
Hardware
1-2
Software
6.
For design of hardware, it requires special For design of the software, programmer
software(chip designing software).
requires set of system software.
7.
8.
Example are keyboard mouse, screen Software are Microsoft word, paint brush ,
,CPU , light pen, printer etc
loader, linker etc.
1.1.1
Computer Hardware :
GTU - June 2009, Jan. 2010, June 2010.
It includes all physical components which are touchable and sensible. It is mainly divided
into 4 parts - CPU, input device, output device and auxiliary memory.
CPU is the Central Processing Unit, which contains electronics circuits and used to execute
instructions and also controls the operations of other input output devices. CPU is again divided
into three parts ALU, CU, and memory .
ALU (Arithmetic and Logical Unit) performs arithmetical operations like addition,
subtraction, multiplication, division as well as logical operations like AND, OR,NOT
operations.
1-3
CU (Control Unit) controls each and every activity of processing of all connected devices. It
acts as a brain. Its main function is to fetch instructions from main memory and execute it.
Normally it executes instructions sequentially. But again this depends on specified
instructions.
RAM (Random Access Memory) can hold data till power is on. So it is called as a
volatile memory (that means temporary memory).
ROM (Read Only Memory) can hold data permanently. Ordinary user can not store his
information into the ROM only manufacturer of the ROM chip can store data into it. It
consist of programs which can control IO devices (BIOS), booting procedures, programs
of some mathematical functions like sin, cos, tan etc.
EPROM (Erasable Programmable Read Only Memory) can hold data permanently.
User can store his information into the EPROM but only once. After that bit
modification is not allowed. User can delete all the contents of the chip using ultra
violet rays and again rewrite information on the EPROM chip. So it is called as a non
volatile memory (that means permanent memory).
EEPROM (Electrically Erasable Programmable Read Only Memory) can hold data
permanently. User can store his information into the EEPROM and bit modification is
allowed. So it is called as a non volatile memory (that means permanent memory). But
till now it is not used as a secondary memory because of its cost.
CPU accepts information from user through input device. There are many input devices like
keyboard, mouse, mice, tracker ball, touch screens etc.
CPU conveys information to user through output device. There are many output devices like
monitor, LCD, printers, plotters, speakers etc.
1.1.2
1-4
Computer Software :
GTU - June 2009.
Program is the collection of instructions, which performs particular task, and collection of
programs which accomplishes application (that means number of tasks) is called as a software.
This software is classified into two categories - System software and Application software. System
software directly interacts with computer hardware, whereas Application software is only
designed to solve user problems as per users requirement. Following block diagram describes the
roll of computer software and hardware.
System Software :
System software is classified into 3 categories : (1) Operating system (2) System support
software and (3) System development software.
Operating system manages hardware as well as interacts with user, and provides different
services to users. There are many operating systems like DOS, Windows-XP,VISTA,
LINUX etc.
System support software manages hardware more efficiently, for example drivers of the IO
devices or antivirus softwares
Drivers of the IO device consists of interfacing programs which can interacts with IO
device. Every IO devices has its own driver programs.
Viruses are the programs which can start misuse of the resources thus there is
mismanagement in the system. Virus infected systems lose their data as well as
systems start doing some illegal operations.
Antivirus programs remove viruses from the systems and some time recover systems
from major data loss.
1-5
Editor is the software , which is used to create programs as well as it is used for
modification of existing programs.
Pre-processor is the software which is used before translators (that means compiler or
editors) , which is used to replace some segments of code with some another segment. It
is also called as an expansion.
Compilers are the software which can translate high level programs into low level
programs.
Interpreters are the software which can translate line by line high level programs into
low level programs.
Loaders are the software which can load object codes into the main memory and
execute it.
Compilers Vs Interpreters
Sr.
No.
Interpreter
Compiler
1.
Translates and executes line by line of Translates whole source code into object
source code.
code.
2.
User can see the result of program at User cannot see the result of program at
translation time.
translation time.
3.
4.
5.
6.
It cannot produce any object code. Thus, it Once object code is created then does not
needs translation process for every require translation, it only requires to load
execution.
object code into the memory. That is
loading process.
7.
So this process
consuming.
8.
Debugging facilities can be easily provided When compiler provides debugging facility
by the interpreter.
then logic becomes more complicated.
9.
Size of Interpreter program is larger than Size of compiler program is smaller than
compiler because it contain two tasks interpreter because it contain only
translation as well as loading.
translation procedure.
10.
Now a days, there are two types of Here portability can be achieved through
interpreter that is pure interpreter and bootstrapping process i.e. cross compilation
impure interpreter. Impure interpreter of the object code.
becomes more popular because of their
portability.
11.
Interpreters
is
easily
lengthy
supports
and
Sr.
No.
1-6
Interpreter
Compiler
allocation of memory.
12.
Examples :
interpreter.
Basic
memory.
interpreter,
Application Software :
Application software is classified into two categories : General purpose software and Special
purpose software.
General purpose software is used for many number of tasks, and provides many features for
example word processor, oracle, excel etc.
Special purpose software is designed for specific purposes only. User programs comes under
special purpose software for example pay roll system for specific company, tax calculation
software etc.
Sr.
No.
System software
Application software
1.
Is primarily concerned with the efficient Is primarily concerned with the solution of
management of the computer system.
some problems, using the computer as a
tool.
2.
However system programs are intended to The focus is on the application, not on the
support the operation and use of the computing system.
computer itself, rather than any particular
application.
3.
Is usually related to the architecture of the Is usually related to the detailed knowledge
machine on which that is to run.
of the Language and environment.
system programmer should have knowledge So that Application programmer should
of computer architecture.
have detailed knowledge of high level
programming language which is used to
develop
application
program
and
environment i.e. operating system on which
they are to run.
4.
Is machine dependent.
Is machine independent.
5.
Used to develop new system programs and Used to develop new application programs
using Bootstrapping we can make them and using cross compiler i.e. again system
portable.
programs, we can make them portable.
6.
1.2
1-7
Computer Languages :
Language is the media of communication. Natural languages contains many words with
specific meaning and sentences with grammatical rules. Languages are used on computer system
called as computer languages. Computer languages are divided into 2 categories - low level
language, and high level language. Low level language is further divided into 2 types - machine
language and assembly language.
1.2.1
Machine Language :
Computer directly understands this language. It is the language of 0s and 1s (which is also
called as a binary number system i.e. binary pattern). Every instruction consists of order, data,
address and all are in terms of 0s and 1s, thus it is very difficult to understand as well as
remember.
1.2.1.1 Advantages of Machine Level Language :
The programs are stored in very less memory. Programmer can take care of each and every
bits and bytes.
Once programs are entered into main memory computer can directly execute it without using
any translator.
Suitable for low volume application, small simple program where the prototype is the final
product.
The programs are difficult to understand or debug. (Address, instruction, and operands all
are in machine language i.e. in binary pattern)
The programs are slow to enter since you must enter each bit individually
The programs do not describe the task, which you want to perform in anything resembling a
human readable format.
The programmer often makes careless error. Which are subsequently very difficult to find
(binary pattern all look the same.)
1.2.2
Assembly Language :
Every machine language instruction is assigned to English word mnemonic such that it
should describe function of instruction. Such language of mnemonics is called as Assembly
language. Programs written in the assembly languages is called as an assembly language
programs. Computer requires assembler to translate assembly level programs into machine level
and then only it is become executable.
1.2.2.1
1-8
The assembly language instruction is called as mnemonic or memory jogger, which should
describe in some way what the instruction does. (if we compare it with machine instruction.)
1.2.2.2
Programmers need to learn structure of assembly language program and syntax of every
statement.
1.2.3
This language contains set of statements with each having their own syntactical
(grammatical) rules. It is very similar to natural languages. Thus it is very easy for program
development purpose. They requires set of system program for the execution of high level
language program.
Example :- C, C++, JAVA, .NET (VB.Net, ASP.NET, C#.Net)
1.2.3.1
The High level instructions are easy sentences so it is very easy to learn. (if we compare it
with machine instruction.)
1.2.3.2
High level language program requires compiler or interpreter to translate high level
language program to machine level that means object code.
Object code needs loader to load code into the main memory and execute that program.
Programmers need to learn structure of high level language program and syntax of every
statement.
Some high level languages are environment dependent (That means dependent on operating
system.)
1.3
1-9
programs, so now a days only high level languages are used to develop a program. But, directly
writing programs in the high level language is also a tedious work as they may produce wrong
result. To overcome such situation we need to follow program development process called as a
system development life cycle. This process is distributed in 6 phases, it is also called as a water
fall model which is shown in Fig. 1.3.
System Requirement : This phase includes understanding the problem, and think on what
exactly has to be done. Gather all information which is essential to solve problem.
Design : In this phase, start to solve problem using various tools like flowcharts and
algorithms which are explained in the section 1.4. It is very important step which helps in
showing the flow of the program as well as detecting mistakes if any.
Coding : Finally all the design is to be converted into the coding (i. e. programs).
Testing : It is again very important phase, which is used to test whether output of the
program is correct or not? This can be tested with various set of inputs.
Maintenance : In this phase, program is compared with initial requirement, and as per
requirements, new features are added into it.
1.4
Algorithms :
.GTU Jan. 2010, June 2010.
Note :
1.4.1
1-10
Always remember that algorithm and flowcharts are to be developed first and then programs are to be
written.
Algorithm has four properties, that are finiteness, definiteness, effectiveness, input and
output. They are explained below.
Input and output : The algorithm must accept zero or more input and must produce at least
one output.
1.4.2
Advantages :
1.4.3
Disadvantages :
Time consuming.
1.4.4
Examples of Algorithm :
Simple Flow Problem : These are very simple problems, and the procedure of flow is in
sequential manner, means after first step, it automatically goes to second step.
Example 1 :
Algorithm
Step 1 : Accept the value of length i.e. l
Step 2
Step 3
Step 4
Step 5
: Stop procedure
Example 2 :
Algorithm
Step 1 : Accept the name and marks in 6 subject, i.e. n,m1,m2,m3,m4,m5,m6
Step 2 : Total=m1+m2+m3+m4+m5+m6
1-11
Branching Flow problems : These sentences are not simple. It consists of condition, it tests
that condition , according to it transfers the flow. Here flow is not sequential that means
after branching step, it automatically goes to step mentioned in the statement.
Example 3 :
Algorithm
Step 1 : Accept the number i.e. n
Step 2 : Check if n<0 ? If yes go to Step 3 otherwise go to Step 5
Step 3 : Print n is negative
Step 4 : Stop procedure
Step 5 : Print n is positive
Step 6 : Stop procedure
Example 4 :
Algorithm
Step 1 : Accept two numbers i.e. a , b
Step 2 : Check if a<b ? If yes go to Step 3 otherwise go to Step 5
Step 3 : Print a is smallest number
Step 4 : Stop procedure
Step 5 : Print b is smallest number
Step 6 : Stop procedure
Example 5 :
Algorithm
Step 1 : Accept four numbers i.e. a , b , c ,d
Step 2 : Check if a>b ? If yes Go to Step 3 otherwise go to Step 14
Step 3 : Check if a>c ? If yes Go to Step 4 otherwise go to Step 9
Step 4 : Check if a>d ? If yes Go to Step 5 otherwise go to Step 7
Step 5 : Print a is largest number
Step 6 : Stop procedure
Step 7 : Print d is largest number
Step 8 : Stop procedure
Step 9 : Check if c>d ? If yes Go to Step 10 otherwise go to Step 12
1-12
Above algorithm is structured algorithm (that means nesting). We can eliminate repetitive
steps using unstructured way, which is described in following algorithm.
1-13
Dear students, You can see that both algorithms produce accurate and same result but
algorithm 2 requires less number of steps. When you are solving any problem first time then do
not think about steps only try to produce accurate result and then try to reduce unnecessary as
well as repetitive steps.
LOOPS : There are 2 types of loops : Finite loop and Infinite loops
When number of steps are repeated many number of times using same sequence is called as
a loop and set of repeated steps are called as a body of loop.
Finite Loops : When body of loop is executed finite number of times called as finite loop. For
construction of finite loops following steps are important
When initial value is less than final value then step value should be positive.
Note :
When initial value is greater than final value then step value should be negative.
Note :
Note :
Running variable should not be initialised by some constant within the body of loop.
Note :
Running variable should not be same in nested loops (i.e. loop within loop)
Step 1 : I=1
Step 1 : I=1
Step 2 : ODD=1
Step 2 : Print I
Step 3 : I=I+2
Step 4 : ODD=ODD+2
Step 5 : I=I+1
1-14
Algorithm
Algorithm
Step 1 : Accept N
Step 1 : Accept N
Step 2 : I=1
Step 2 : I=1
Step 3 : SUM=0
Step 3 : FACT=1
Step 4 : FACT=FACT*I
Step 5 : SUM=SUM+A
Step 5 : I=I+1
Step 6 : I=I+1
Step 7 : AVG=SUM/N
Infinite Loop : When the body of loop is executed finite number of time called as finite loop.
Following algorithm is the example of infinite loop.
Example 10 :
1-15
Algorithm
Step 1 : Print Tech Max Publication Jindabad
Step 2 : Go to Step 1
Step 3 : Stop procedure
Here step 3 never executes, so it is called as dead step
1.5
Flowcharts :
.GTU Jan. 2010, June 2010.
Name
Uses
Input /Output
Process
Decision
Flow lines
Indicates flow
Loop
1-16
Symbol
Name
Uses
Connector
Multiple selection
Block
Module call
1.5.1
Advantages :
Easy to draw.
1.5.2
Disadvantages :
Time consuming.
1.5.3
1-17
Examples of Flowchart :
Flowchart :
Fig. 1.5
Fig. 1.6
Example 13 : Draw a flowchart to check whether Example 14 : Draw a flowchart to find out
the given number is positive or negative ?
smallest number out of two numbers.
Flowchart :
Flowchart :
Fig. 1.7
Example 15 :
Fig. 1.8
1-18
Flowchart :
Fig. 1.9
Example 16: Draw a flowchart to find out sum and Example 17 : Draw a flowchart to find
average of given N numbers.
.GTU - Dec. 2008. out factorial of given N numbers.
GTU - April 2010.
Flowchart :
Flowchart :
Fig. 1.10
Fig. 1.11
1-19
1-x+x2-x3 +---xn
Flowchart :
Flowchart :
Fig 1.13
Fig. 1.12
Fig 1.14
Example 21 :
1-20
Write an algorithm and draw flowchart for finding sum digits of a number.
Algorithm :
Flowchart :
Step 1 : Accept N
Step 2 : N1=N,SUM=0
Step 3 : D=N%10
Step 4 : SUM=SUM*D
Step 5 : N=N/10
Step 6 : If N !=0 go to Step 3
Step 7 : Print SUM
Step 8 : Stop procedure
Fig. 1.15
Example 22 :
1-21
Write an algorithm and draw the flowchart of a program to find the maximum of
'n' numbers.
Algorithm :
Flowchart :
Step 1 : Accept N
Step 2 : Accept FIRSTNUM
Step 3 : I=2 , MAX=FIRSTNUM
Step 4 : Accept any number i.e. A
Step 5 : Check if A>MAX Then MAX=A
Step 6 : I=I+1
Step 7 : If I<=N go to Step 4
Step 8 : Print Maximum number is MAX
Step 9 : Stop procedure
Fig. 1.16
Example 23 :
Write an algorithm and draw the flowchart to accept N numbers and count how
many of them where odd and also compute sum of all these odd..GTU June 2010.
Ans. :
Step 1 :
Step 2 :
Accept N
Step 3 :
Accept A
Step 4 :
If A%2==1
Then
SUM=SUM+A
1-22
ODD=ODD+1
Step 5 :
COUNT=COUNT+1
Step 6 :
Step 7 :
Step 8 :
Stop Procedure
Flowchart :
Exercise
Q. 1
Q. 2
Q. 3
Q. 4
Q. 5
Q. 6
Q. 7
Q. 8
1.6
1-23
Dec. 2008
Q. 1
Q. 2
Draw a flow chart to find the maximum number out of three given numbers.
(Pg. 1-23, Example 15 : Say d = 32768 (Smallest integer number))
(5 Marks)
Draw a flow chart to do the sum of 10 elements read from the user.
(Pg. 1-24, Example 16 : say n = 10)
(5 Marks)
June 2009
Q. 3
Draw block diagram of computer and explain each block. (Section 1.1.1)
Q. 4
Define following :
(i)
Operating system
(ii)
Compiler
(iii)
Application software
(iv)
(7 Marks)
(4 Marks)
Jan. 2010
Q. 5
Q. 6
Draw the Block Diagram of Computer Architecture and explain each block with its functions.
(Section 1.1.1)
(7 Marks)
Give comparison of Flow charts and Algorithms (Sections 1.4 and 1.5)
(4 Marks)
April 2010
Q. 7
(5 Marks)
June 2010
Q. 8
Q. 9
(Section 1.1)
Name the major components of computer and give their function in brief. (Section 1.1.1)
(2 Marks)
Q. 10 Write an algorithm and draw the flowchart to accept N numbers and count how many of them where odd
and also compute sum of all these odd. (Example 23)
(10 Marks)
1-24
Note