UNIT2 COMPUTER
PROGRAMMING
LESSON1
What is a Computer?
An electronic device that:
• accepts data (input),
• manipulates the data
(process),
• provides output (output) and
• stores the results (storage).
HE COMPUTER INFORMATION
ROCESSING CYCLE
Input c l
C y
ng
s i
e s
c
Output
P ro Process
on
ti
a
r m
Memory/Storage
f o
In
COMPUTER
PROGRAMMING?
Is the process of developing and
implementing various sets of
instructions to enable a computer
to do a certain task.
These instructions are considered
computer programs and help the
computer to operate smoothly.
WHAT IS A COMPUTER
PROGRAM?
A computer program is a
sequence of instructions
written using a Computer
Programming Language
to perform a specified
task by the computer.
WHAT IS A Computer
Program?
A computer program is a collection
of step-by-step instructions that tell
the computer how to perform a
particular task or group of related
tasks.
PROGRAM IS ALSO KNOWN AS
SOFTWARE
MAIN TYPES OF
Software(PROGRAMS)
There are two general types
of software.
•System software &
•Application software
SYSTEM SOFTWARE:
Operating Systems
The two operating systems most
users encounter are Microsoft
Windows and Mac OS X.
Operating systems contain the
instructions that allow the parts of a
computer to work together by
performing most, if not all, of the
“behind the scenes” tasks such
SYSTEM SOFTWARE:
Device
Device Drivers
drivers contain the
instructions which tell the computer
how to interact with that particular
device.
Many device drivers come
preinstalled on the operating
system but devices such as printers
and scanners come with a device
driver to install on the computer if
necessary.
Application Software
Software the computer uses to
carry out a specific task as specified
by the user.
Word Processing
Software that enables the user to
enter, edit, format and print
documents containing primarily, but
not limited to text.
Spreadsheets
Software that enables the user to
enter, edit, format, print, sort and
calculate primarily numeric data.
Computer Programming Languages:
A programming language is an
artificial language that can be used to
control the behavior of a machine,
particularly a computer.
Programming languages, like human languages,
are defined through the use of syntactic and
semantic rules, to determine structure and
meaning respectively.
Computer Programming Languages:
A PL is
A special purpose and limited
language
A set of rules and symbols
used to construct a
computer program
A language used to interact
with the computer
Computer Programming Languages
(Contd…):
Programming languages
are used to facilitate
communication about the
task of organizing and
manipulating information,
and to express algorithms
precisely.
Computer Programming Languages
(Contd…): binary form
In order for a computer to be able to execute a
program, the program must first be present in
binary form within the computer's memory.
However, writing a program directly in binary
form is completely out of the question.
The solution to this conflict is for the
programmer to write a program in a more
suitable language and then to use a special
computer program to translate it into the binary
machine code necessary for the target computer.
Levels of Programming
Languages
Programming languages are
said to be "lower" or "higher,"
depending on how close they
are to the language the
computer itself uses (Os and 1s
= low) or to the language
people use (more English-like-
high).
Levels of Programming
Languages
We will consider five levels of language.
In terms of ease of use and capabilities,
each generation is an improvement over its
predecessors.
The five generations of languages are:
Machine language
Assembly languages
High-level languages
Very high-level languages
Natural languages
Machine Languages, Assembly
Languages, and High-level Languages
Three types of
programming languages
Machine languages
Strings of numbers giving
machine specific instructions
Example:
+1300042774 (these would really
be in binary)
+1400593419
+1200274027
20
2000 Prentice Hall, Inc. All rights
Machine language:
While easily understood by computers,
machine languages are almost impossible for
humans to use because they consist entirely of
numbers.
For example, an x86/IA-32 processor can
execute the following binary instruction as
expressed in machine language:
Binary: 10110000 01100001 (Hexadecimal: 0xb061)
Machine language:
A computer is a binary machine.
The signals used by the various parts of the
machine to communicate with each other
must be in binary form.
A program shown in binary form (or in hex
for ease of reading) so as to reflect these
values is said to be in machine language.
Machine language is the most basic and
elementary form in which any program can
exist
Machine Language :
Machine Language is the only language that is
directly understood by the computer.
It does not need any translator program.
We also call it machine code and it is written as
strings of 1's (one) and 0’s (zero).
When this sequence of codes is fed to the
computer, it recognizes the codes and converts it
in to electrical signals needed to run it. For
example, a program instruction may look like this:
1011000111101
Advantage Machine
Language :
The only advantage is that
program of machine
language run very fast
because no translation
program is required for the
CPU.
Disadvantages Machine
Language:
1. It is very difficult to program in machine
language.
The programmer has to know details of
hardware to write program.
2. The programmer has to remember a lot
of codes to write a program which results in
program errors.
3. It is difficult to debug the program.
Assembly Level Language:
An assembly language is a low-level
language for programming computers.
The word "low" does not imply that the
language is inferior to high-level
programming languages but rather
refers to the small or nonexistent
amount of abstraction between the
language and machine language,
because of this, low-level languages are
sometimes described as being "close to
the hardware."
It implements a symbolic representation
of the numeric machine codes and other
Levels of Programming
Languages
Assembly languages
English-like abbreviations
representing elementary
computer operations
(translated via assemblers)
Example:
LOAD BASEPAY
ADD OVERPAY
STORE GROSSPAY
Assembly Level
Language (contd…):
A utility program called an assembler, is used to
translate assembly language statements into the
target computer's machine code.
The assembler performs a more or less isomorphic
translation (a one-to-one mapping) from mnemonic
statements into machine instructions and data.
Example: Assembly language representation is
easier to remember (more mnemonic)
mov al, 061h
This instruction means:
Move the hexadecimal value 61 (97 decimal) into
the processor register named "al".
The mnemonic "mov" is an operation code or
opcode, A comma-separated list of arguments or
Assembly Language
A symbolic representation of the machine
language of a specific processor.
Is converted to machine code by an assembler.
Usually, each line of assembly code produces one
machine instruction (One-to-one correspondence).
Programming in assembly language is slow and
error-prone but is more efficient in terms of
hardware performance.
Mnemonic representation of the instructions and
data
Example:
Load Price
Add Tax
Store Cost
Example (Adds 2 numbers):
name "add"
mov al, 5 ; bin=00000101b
mov bl, 10 ; hex=0ah or bin=00001010b
add bl, al ; 5 + 10 = 15 (decimal) or
hex=0fh or
bin=00001111b
Advantages Assembly
Language:
1.The symbolic programming of Assembly Language is easier
to understand and saves a lot of time and effort of the
programmer.
2.It is easier to correct errors and modify program instructions.
3.Assembly Language has the same efficiency of execution as
the machine level language.
Because this is one-to-one translator between assembly
language program and its corresponding machine language
program.
.
Disadvantages Assembly
Language:
1.One of the major disadvantages is that
assembly language is machine dependent.
A program written for one computer might
not run in other computers with different
hardware configuration
Machine Languages, Assembly
Languages, and High-level Languages
High-level languages
Instructions closer to everyday English
English is a natural language. Although high
level programming languages are closer to
natural languages, it is difficult to get too
close due to the ambiguities in natural
languages (a statement in English can mean
different things to different people –
obviously that is unacceptable for computer
programming).
Use mathematical notations (translated
via compilers)
Example:
grossPay = basePay + overTimePay
33
High-level language
A programming language which use
statements consisting of English-like keywords
such as "FOR", "PRINT" or “IF“, ... etc.
Each statement corresponds to several
machine language instructions (one-to-many
correspondence).
Much easier to program than in assembly
language.
Data are referenced using descriptive names
Operations can be described using familiar
symbols
Example:
Cost := Price + Tax
Advantages of High Level
Languages
Higher level languages have a major
advantage over machine and assembly
languages that higher level languages are
easy to learn and use.
It is because that they are similar to the
languages used by us in our day to day life.
Advantages of High Level
Languages
High-level language programs are
portable.
(Although some programs could
still have a few machine-dependent
details, they can be used with little
or no modifications on other types
of machines.)
Program development is faster
Fewer lines of code
Program maintenance is easier
Low vs High level language
Low Level Language:
The Low Level language are those
languages which are near to computer
language.
It is hard to understand by Human and easy
to understand by Computer itself.
Example:
Machine Language (1's and 0's)
Assembly Language (Key words and
symbols)
Advantages and Disadvantages
of Low Level Languages:
It consists of binary digits and some symbols.
It depends on machine.
Machine language directly executes on
computer.
No need of language translator for machine
language.
Program written in low level language is
called object code.
Example of low level languages
are “MACHINE” and “ASSEMBLY” Language.
disadvantages of Low Level languages
It is not easy to
understand.
It is difficult to
modify.
High Level Languages:
The high level languages which are close to human languages.
they are easy to understand by Humans but difficult for
Computers to understand.
Computer needs some mechanism to understand them.
Example:
BASIC
PASCAL
FORTRAN
C/C++
python
JAVA etc.
Advantages and disadvantages of
High Level Languages
advantages of high level languages:
they are close to human being
they are easy to understand
They consist of English language like
structure
It does not depend upon machine
they are easy to modify
disadvantages of high level languages:
they need language translator to translate
into program in to machine language.
They do not execute directly on computer.
Programming Language levels
summary:
•.
•Machine languages are the only languages
understood by computers.
Programming Language levels
summary:Computer Languages
– Machine Language
• Uses binary code
• Machine-dependent
• Not portable
Assembly Language
Uses mnemonics
Machine-dependent
Not usually portable
High-Level Language (HLL)
Uses English-like language
Machine independent
Portable (but must be compiled for different
platforms)
Examples: Pascal, C, C++, Java, Fortran, . . .
Syntax & Semantics in
programming
Syntax:
A language's syntax is the
grammar of the language.
Semantics:
The meanings of words
and symbols used in the
language
Compilers & Programs
Source code or Source program
The form in which a computer
program, written in some formal
programming language, is
written by the programmer.
Can be compiled automatically
into object code or machine code
or executed by an interpreter.
Compilers & Programs
Compiler
A program that converts
another program from
some source language (or
high-level programming
language / HLL) to machine
language (object code).
Compilation into Assembly L
Source Assembly
Program Compiler Language
Assembly Assembler Machine
Language Language
Compilers & Programs
Object code or Object program
Output from the compiler
Equivalent machine language translation of the
source program
Files usually have extension ‘.obj’
Executable program
Output from linker/loader
Machine language program linked with necessary
libraries & other files
Files usually have extension ‘.exe’
Program Execution
Steps taken by the CPU to run a program
(instructions are in machine language):
1. Fetch an instruction
2. Decode (interpret) the instruction
3. Retrieve data, if needed
4. Execute (perform) actual processing
5. Store the results, if needed
Program Errors
Syntax Errors:
Errors in grammar of the language
Runtime error:
When there are no syntax errors, but the
program can’t complete execution
Divide by zero
Invalid input data
Logical errors:
The program completes execution, but delivers
incorrect results
Incorrect usage of parentheses
Compilation
Source Target
Program Compiler Program
Input Target Program Output
Compiler translates source into target (a machine
language program)
Compiler goes away at execution time
Compiler is itself a machine language program,
presumably created by compiling some other
high-level program
Machine language, when written in a format
understood by the OS is object code
Interpretation
Source
Program
Interpreter Output
Input
• The interpreter stays around during
execution
• It reads and executes statements one at a
time
Compilation vs. Interpretation
Compilation:
Syntax errors caught before running the
program
Better performance
Decisions made once, at compile time
Interpretation:
Better diagnostics (error messages)
More flexibility
Supports late binding (delaying decisions
about program implementation until runtime)
Can better cope with PLs where type and size of
variables depend on input
Supports creation/modification of program
code on the fly (e.g. Lisp, Prolog)
JAVA
For portability:
Java Compiler
bytecode
ML Interpreter
For flexibility: Just In Time (JIT) compiler
translates bytecode into ML just before
execution