This document discusses computer programming languages and number systems. It begins by defining programming languages and categorizing them into machine language, assembly language, and high-level languages. It then covers number systems used in computers like binary, octal, hexadecimal, and decimal. Binary arithmetic operations like addition, subtraction, multiplication, and division are explained. Negative numbers in binary are also discussed.
4. Unit 2
Meaning: Programming Language
•
A language that is acceptable to a computer system is called a computer language or programming language,
and the process of writing instructions in such a language for an already planned program is called
programming or coding.
•
All computer languages can be broadly classified into the following three categories:
Machine language
Assembly language
High- level language
Programming Language
5. Unit 2
Machine Language
•
Machine language is understood by the computer without using a translation program.
•
The machine language of a computer is normally written as strings of binary 1s and 0s.
•
A machine language instruction normally has a two-part format
I. Operation code: which tells the computer what function to perform
II. Operand: which tells the compute where to find or store the data or other instructions, which
are to be manipulated.
•
Every computer has a set of operation codes, called as instruction set which are meant for performing a
specific operation or function
•
Typical operations included in the instruction set of a computer are as follows:
Arithmetic operations
Logical operations
Branch operations (either conditional or unconditional) for transfer of control to the address
given in the operand field
Data movement operations for moving data between memory locations and registers
Programming Language
6. Unit 2
Typical Single-Address Machine Language Instructions
Programming Language
Memory
Instruction
Operand
OP-CODE Address A
7. Unit 2
Advantages of Machine Language
It makes fast and efficient use of the computer.
It requires no translator to translate the code i.e. Directly understood by the computer
Disadvantages of Machine Language
Machine dependent
The internal design of every type of computer is different from every other type of computer, the machine
language also differs from computer to computer.
Difficult to program
Machine language programs are directly and efficiently executed by the computer, it is difficult to program in
machine language. It is necessary for the programmer either to memorise the dozens of operation code
numbers.
Error prone
A programmer has to remember the opcodes, and must keep track of the storage locations of data and
instructions, it becomes very difficult for him/her to concentrate fully on the logic of the problem.
Difficult to modify
Checking machine instructions to locate errors is very difficult and time consuming.
Programming Language
8. Unit 2
Assembly Language
•
An assembly language is a low level programming language for computers, microprocessors, microcontrollers
and other programmable devices.
•
It implements a symbolic representation of machine codes and other constants needed to program a given CPU
architecture.
•
A program written in an assembly language is called an ‘assembly language program’
or a ‘symbolic program’.
Assembler
•
An assembler of a computer is system software, supplied by the computer manufacturer, which translates an
assembly language program into an equivalent machine language program of a computer
•
It is so called because, in addition to translating an assembly language program into its equivalent machine
language program, it also, ‘assembles’ the machine language program in the main memory of the computer
and makes it ready for execution.
Programming Language
9. Unit 2
Advantages of Assembly Language over Machine Language
Easier to understand and use
Easier to locate and correct errors
Easier to modify
No worry about addresses
Easily re-locatable
Efficiency of machine language
Limitations of Assembly Language
Machine dependent
Assembly language differs from computer to computer and an assembly language program can be executed
only on the computer in whose assembly language it has been written.
Knowledge of hardware required
An assembly language programmer must have a good knowledge of the characteristics and the logical
structure of the computer to write good assembly language programs.
Machine level coding
Writing assembly language programs is still time-consuming and not very easy.
Programming Language
10. Unit 2
High-Level Language
•
Machine and assembly languages are often referred to as low-level programming languages. High-level
programming languages were designed to overcome the limitations of low-level programming languages.
•
The advent of high-level language has enabled the use of computers to solve problems even by non-expert
users.
High-Level Languages are Characterised:
•
A program written in a high-level language can be easily ported and executed on any computer, which has the
translator software for the high-level language.
•
High-level languages are machine independent, a programmer writing a program in a high-level language may
not even know on which computer will his/her program be executed. This allows the programmers to mainly
concentrate on the logic of the problem.
•
They deal with high-level coding, enabling the programmers to write instructions using English words and
familiar mathematical symbols and expressions.
Programming Language
11. Unit 2
Compiler
•
A compiler is a translator program (much more sophisticated than an assembler), which translates a high-
level language program into its equivalent machine language program.
•
A compiler is so called, because it compiles a set of machine language instructions for every program
instruction of a high-level language.
Linker
•
A program called a ‘linker’ is used to properly combine all the object program files (modules) of the
software, and to convert them into the final executable program, which is sometimes called a ‘load
module’.
•
That is, a linker takes object program files (modules), and fits them together to assemble them into the
program’s final executable form.
Interpreter
•
A program that executes instructions written in a high-level language. An interpreter translates high-level
instructions into an intermediate form, which it then executes.
Programming Language
12. Unit 2
Advantages and Limitations of High-level Language
Machine independence
Easier to learn and use
Fewer errors
Lower program preparation cost
Better documentation
Easier to machine
Limitations of High-Level Language
Lower efficiency
Program written in high-level languages take more time to execute and require more main memory space.
Less flexibility
High-level languages are less flexible than assembly languages because they do not normally have instructions
or mechanism to control the computer’s CPU, memory and registers.
Programming Language
13. Unit 2
Object-Oriented Programming Languages
•
A programming language that enables the programmer to associate a set of procedures with each type of data
structure.
•
A type of programming in which programmers define not only the data type of a data structure, but also the
types of operations (functions) that can be applied to the data structure. In this way, the data structure
becomes an object that includes both data and functions. In addition, programmers can create relationships
between one object and another.
•
A couple of examples of object oriented programming languages are C++ and Java.
Programming Language
14. Unit 2
Features of Object Oriented Programming
Objects and classes
Object is an instance of a class. It has a state, behaviour and identity. It is also called as an instance of a class.
A class is a template for a set of objects that share a common structure and a common behaviour.
Inheritance
Inheritance is a relationship among classes, wherein one class shares the structure or behaviour defined in
another class. This is called Single Inheritance. If a class shares the structure or behaviour from multiple classes,
then it is called Multiple Inheritance.
Polymorphism and Dynamic Binding
Polymorphism is a characteristic of being able to assign a different behavior or value in a subclass, to
something that was declared in a parent class.
Dynamic binding is a binding in which the class association is not made until the object is created at execution
time.
Programming Language
16. Unit 2
Problem Solving
Process
• Problem identification and analysis
• Breaking down the solution in a sequence
of elementary tasks
• Algorithm/Computer program
formulation
• Program execution
Problem Solving
17. Unit 2
Algorithm
An algorithm may be formally defined as a sequence of instructions, designed in a manner that, if the
instructions are executed in the specified sequence, the desired results will be obtained.
Characteristics of an Ideal Algorithm
•
Each and every instruction should be precise and unambiguous.
•
Each instruction should be such that it can be performed in a finite time.
•
One or more instructions should not be repeated infinitely. This ensures that the algorithm will ultimately
terminate.
•
After performing the instructions, that is, after the algorithm terminates, the desired results must be obtained.
Representation of Algorithms
Algorithms may be represented in the form of programs, flowcharts or pseudo codes.
Problem Solving
18. Unit 2
Flowchart
•
A flowchart is a pictorial representation of an
algorithm.
•
It is often used by programmers as a program-
planning tool for organising a sequence of steps
necessary to solve a problem by a computer.
•
It uses boxes of different shapes to denote
different types of instructions.
Problem Solving
19. Unit 2
Advantages of Flowcharts
•
Conveys better meaning
•
Analyses the problem effectively
•
Efficient Coding
•
Systematic debugging
Limitations of Flowcharts
•
Takes a significant amount of time to draw
•
Difficult to make changes
•
Non-standardisation
Problem Solving
20. Unit 2
Pseudocode
It is an outline of a program , written in a form that
can easily be converted into real programming
statements.
A pseudocode is made up of the following basic logic
structures that have been proved to be sufficient for
writing any computer program:
•
Sequence
•
Selection (if...then...else or if....then)
•
Iteration(do...while or repeat...until)
Problem Solving
21. Unit 2
Advantages of Pseudocodes
•
Converting a pseudocode to a programming language is much easier as compared to converting a flowchart.
•
As compared to a flowchart, it is easier to modify the pseudocode of program logic when program
modifications are necessary.
•
Writing of pseudocode involves much less time and effort than drawing an equivalent flowchart. Pseudocode is
easier to write than an actual programming language because it has only a few rules to follow, allowing the
programmer to concentrate on the logic of the program.
Limitations of Pseudocodes
•
In case of pseudocode, a graphic representation of program logic is not available.
•
There are no standard rules to follow in using pseudocode. Different programmers use their own style of
writing pseudocodes and hence communication problems occur due to lack of standardisation.
•
For a beginner, it is more difficult to follow the logic or write the pseudocode, as compared to flowcharting.
Problem Solving
23. Unit 2
Number Systems used in Computers
The common number systems used in computers are:
•
Binary
•
Octal
•
Hexadecimal
•
Decimal
Number Systems and Codes
24. Unit 2
Decimal System
The decimal system is the system which is used in everyday counting. The number system includes the ten
digits from 0 through 9. These digits are recognised as the symbols of the decimal system. Each digit in a base
ten number represents units ten times the units of the digit to its right.
Binary System
Computers do not use the decimal system for counting and arithmetic. Their CPU and memory are made up of
millions of tiny switches that can be either in ON or OFF states. 0 represents OFF and 1 represents ON. Binary
system has two numbers 0 and 1. Binary system has base 2 therefore the weight of nth bit of the number from
Right Hand Side is nth bit × 2n-1.
Octal System
The octal system is commonly used with computers. The octal number system with its 8 digit 0,1,2,3,4,5,6, and
7 has base 8. The octal system uses a power of 8 to determine the digit of a number’s position.
Hexadecimal System
Hexadecimal is another number system that works exactly like the decimal, binary and octal number systems,
except that the base is 16. Each hexadecimal represents a power of 16. The system uses 0 to 9 numbers and A
to F characters to represent 10 to 15 respectively.
Number Systems and Codes
25. Unit 2
Binary Arithmetic
Addition
•
The simplest arithmetic operation in binary is addition. Adding two single-digit binary numbers is relatively
simple, using a form of carrying:
0 + 0 → 0
0 + 1 → 1
1 + 0 → 1
1 + 1 → 10, carry 1 (since 1 + 1 = 0 + 1 × binary 10)
•
Adding two “1” digits produces a digit “0”, while 1 will have to be added to the next column. This is similar to
what happens in decimal when certain single-digit numbers are added together. If the result equals or exceeds
the value of the radix (10), the digit to the left is incremented:
5 + 5 → 0, carry 1 (since 5 + 5 = 0 + 1 × 10)
7 + 9 → 6, carry 1 (since 7 + 9 = 6 + 1 × 10)
•
This is known as carrying. When the result of an addition exceeds the value of a digit, the procedure is to
“carry” the excess amount divided by the radix (that is, 10/10) to the left, adding it to the next positional value.
This is correct since the next position has a weight that is higher by a factor equal to the radix. Carrying works
the same way in binary.
Number Systems and Codes
26. Unit 2
Subtraction
•
Subtraction works in much similar way:
0 − 0 → 0
0 − 1 → 1, borrow 1
1 − 0 → 1
1 − 1 → 0
•
Subtracting a “1” digit from a “0” digit produces the digit “1”, while 1 will have to be subtracted from the next
column. This is known as borrowing. The principle is the same as for carrying. When the result of a subtraction
is less than 0, the least possible value of a digit, the procedure is to “borrow” the deficit divided by the radix
(that is, 10/10) from the left, subtracting it from the next positional value.
•
Subtracting a positive number is equivalent to adding a negative number of equal absolute value. Computers
typically use 2’s complement notation to represent negative values. This notation eliminates the need for a
separate “subtract” operation.
•
The most common way of subtracting binary numbers is done by first taking the second value (the number to
be subtracted) and apply what is known as 2’s complement, this is done in two steps:
•
complement each digit in turn (change 1 for 0 and 0 for 1)1
•
add 1 (one) to the result
Number Systems and Codes
27. Unit 2
Negative numbers
•
The above example is subtracting a smaller number from a larger number, if one wants to subtract a larger number
from a smaller number (giving a negative result), then the process is slightly different.
•
Usually, to indicate a negative number, the most significant bit (left hand bit) is set to 1 and the remaining 7 digits are
used to express the value. In this format the MSB is referred to as the sign bit.
•
Here are the steps for subtracting a large number from a smaller one (negative result)
Apply 2’s complement to the larger number.
Add this value to the smaller number.
Change the sign bit (MSB) to zero.
Apply 2’s complement to the value to get final result.
The most significant bit (sign bit) now indicates the value is negative.
Multiplication
•
Multiplication in binary is similar to its decimal counterpart. Two numbers A and B can be multiplied by partial products.
For each digit in B, the product of that digit in A is calculated and written on a new line, shifted leftward so that its
rightmost digit lines up with the digit in B that was used. The sum of all these partial products gives the final result.
•
Since there are only two digits in binary, there are only two possible outcomes of each partial multiplication:
If the digit in B is 0, the partial product is also 0.
If the digit in B is 1, the partial product is equal to A.
Number Systems and Codes
28. Unit 2
Division
In binary division the divisor is 1012, or 5 decimal, while the dividend is 110112. The procedure is the same as
that of decimal long division. Here, the divisor 1012 goes into the first three digits 1102 of the dividend one
time, so a “1” is written on the top line.
This result is multiplied by the divisor, and subtracted from the first three digits of the dividend; the next digit
(a “1”) is included to obtain a new three-digit sequence:
______1______
101 ) 1 1 0 1 1
- 1 0 1
0 1 1
The procedure is then repeated with the new sequence, continuing until the digits in the dividend have been
exhausted.
Number Systems and Codes
29. Unit 2
Hamming Error-Correction Codes
•
Messages that are transmitted over a communication channel can be damaged; their bits can be masked
or inverted by noise.
•
Detecting and correcting these errors is important. Some simple codes can detect but not correct errors,
others can detect and correct one or more errors.
Parity checking
One simple way to detect errors is:
•
Count the number of ones in the binary message
•
Append one more bit, called the parity bit, to the message
•
Set the parity bit to either 0 or 1 so that the number of ones in the result is even.
•
Count the number of ones in the received message, including the parity bit. The result will always be even
if no errors were encountered.
Hamming code approach
Hamming codes are an extension of this simple method that can be used to detect and correct a larger set of
errors. The basic idea is to have several parity bits (called check bits in Hamming codes) and assign
different bits to several overlapping groups. If some parity bits are correct and others are not, the bit in
error can be deduced.
Number Systems and Codes
30. Unit 2
Alphanumeric Codes
An alphanumeric character is any element in the set that includes the upper and lower case alphabetic letters
(A-Z, a-z) and decimal numerals (0-9). It is good and useful to group these letters and numerals together as
many computer programs treat them differently from punctuation characters.
Two well-known alphanumeric codes are:
•
EBCDIC (Extended Binary Coded Decimal Interchange Code)
•
ASCII (The American Standard Code for Information Interchange)
Number Systems and Codes