Wednesday, May 11, 2011

COMPUTER PROGRAMMING: FUNDAMENTALS PART4

HIGH-LEVEL LANGUAGE

The machine and assembly languages are referred to as low-level languages because they are closest to hardware. To create programs that are hardware independent, programmers have developed a new type of programming language called the high-level language. A high-level language is user-friendly because it is similar to the English language and uses words, such as PRINT and GOTO to print and jump to a particular line, respectively. Some examples of a high-level language are Basic, COBOL, C, and Pascal.
High-level languages are also called third-generation languages.
Each instruction in a high-level language is translated into multiple machine-level instructions. High-level languages have made programming an easy task, which has increased the usage of high-level languages by programmers.
Some advantages of using a high-level language are as follows:
·         Learning a high-level language is easier than learning a machine or assembly language because their instructions are more English like.
·         A programmer can easily read, write, and maintain a program written in a high-level language. This is because high-level languages vocabularies are similar to the English language.
·         The high-level language programs being hardware independent provide the feature of portability. The high-level language program written in one type of computer can easily be used on another type of computer.
Compiler
To execute a program written in a high-level language, a computer needs translation software called a compiler.
A compiler is always language-specific which means each high-level language has its own compiler, which translates the source code of that language into the object code. For example, a compiler for COBOL compiles only the programs written in COBOL. It cannot compile programs written in any other high-level language, such as FORTRAN.

The following figure shows how a compiler works:
The preceding figure shows two programs written in two different high-level languages. The programs are compiled using the respective compilers of the languages and are converted into corresponding object code.
When a compiler program translates a source code, it checks the syntax of the statements. If the compiler finds an error in the source code, it generates a list of errors. The compiler does not generate the object code until the errors are removed.

Linker
When a program is written in any programming language, it uses predefined keywords, such as Print or Accept, whose functionalities are defined in the libraries. A library is the collection of various keywords and their functionalities, which are written by experienced programmers and stored for reuse by other programmers. These predefined keywords can be reused by giving the references of libraries in the programs.
On compilation, the compiler generates the object code. To generate the executable code from the object code the functionality of the keywords also needs to be attached. The linker performs the task of linking the functionality of the keywords with the object code.
The process of linking is shown in the following figure:

Interpreter
Some high-level languages use a different type of translator program called an interpreter. An interpreter takes each instruction of the source code, converts it to a machine language instruction, executes it and does not save the generated object code. This process is repeated each time before execution. Therefore, executing the program using an interpreter is time-consuming.
Debugging is easier using an interpreter because interpreter error messages are usually specific and point out the line on which the error occurs.
PERL, BASIC, and Visual Basic languages typically use interpreter.

Examples of High-level Languages
A large number of high-level languages have been developed since the first high-level language, FLOWMATIC, was developed in 1952. Some of the common high-level languages are Beginners All Purpose Symbolic Instruction Code (BASIC), Formula Translation (FORTRAN), Common Business Oriented Language (COBOL), Pascal, and C.


BASIC
BASIC was developed in 1964 by Professor John Kemny and Thomas Kurtz at Darmouth College in the USA.
Basic is an interpreter-based language that is easy and simple to use. BASIC was one of the earliest high-level languages to be implemented on personal computers.

FORTRAN
FORTRAN is one of the oldest high-level languages. It was developed in 1957 by John Backus and his team at International Business Machines (IBM) Corporation. FORTRAN was designed to solve scientific and engineering problems. After the first version was launched, various versions of FORTRAN have been developed, such as FORTRAN II, FORTRAN IV, and FORTRAN 77. FORTRAN IV was the first language that was standardized by American National Standards Institute (ANSI).

COBOL
COBOL was initially developed by the mathematician, Grace Hopper, in 1959. It was later modified in 1959-60 by a committee of the Conference on Data Systems Languages (CODASYL). This committee defined the language specifications for COBOL.
COBOL was standardized by ANSI in 1968. Revised versions of this standard were published in 1974 and 1985, which are known as COBOL 74 and COBOL 85, respectively. Currently, the latest version of COBOL is known as COBOL 2002.
COBOL was designed to solve business data processing problems.

Pascal
Pascal was designed in 1971 by Professor Nicklaus Wirth of the Federal Institute of Technology in Zurich, Switzerland. He named it after the famous French mathematician, Blaise Pascal.
Pascal was designed to help beginners to learn good problem solving and programming practices. Today, Pascal is recognized as a language that allows programmers to write structured and modular programs. It is suitable for scientific as well as business applications.
Pascal was standardized by ANSI in 1983. Since then, different versions of Pascal are available.

C
C was developed in 1972 by Dennis Ritchie and Brian Kernighan at AT&T’s Bell Laboratories, USA. They designed C in such a way that it incorporated the features of a high-level language along with the efficiency of a low-level language.
C was standardized by ANSI in 1989. C is a compiler-based language. Therefore, C programs can be easily transferred to other computers equipped with a C compiler. It is widely used for writing word-processing programs, compilers, and operating systems. For example, the UNIX operating system is written in C.

Selecting a Programming Language
With the availability of a wide variety of programming languages, the selection of an appropriate programming language is an important issue today. The following factors should be considered when selecting a programming language:
·         The first criterion for selecting a language is the type of application that is to be developed. For example, COBOL is for solving business data processing problems.
·         If multiple languages are suitable for an application, programmers should select a language in which they are proficient.
·         If the programmers are not familiar with any language, they should select a language that is easy to learn and use

Tuesday, May 10, 2011

COMPUTER PROGRAMMING: FUNDAMENTALS PART3

ASSEMBLY LANGUAGE

To overcome the limitations of machine language, assembly language was introduced in 1952. Assembly language uses alphanumeric code and symbols instead of binary digits to represent instructions and memory addresses. For example, it uses ADD for addition and SUB for subtraction.
 NB: Alphanumeric code uses a set of letters and numbers to represent an instruction.


   These alphanumeric code and symbols make the program shorter and easier to write than machine language. They also reduce the possibility of errors. Additionally, the programs can be modified easily.

An assembly language is called a second-generation language.
A sample program snippet in an assembly language to add two numbers is shown below:
1. LD Ax, 9
            2. LD Bx, 10
3. ADD Ax,Bx
4. LD (100),Ax
5. JMP Bx
6. HLT
Here, Ax and Bx are registers. Registers are nothing but the memory locations inside a microprocessor. Every instruction and data needs to be loaded from main memory to registers before the CPU can process it.
In the above program:
The line number one loads register Ax with the value, 9.
The line number two loads register Bx with the value, 10.
The line number three adds the value of register Bx to the value of register Ax.
The line number four stores the value of register Ax in the main memory location, 100.
The line number five uses JMP to jump to register Bx to transfer the control to register Bx.
The line number six stops the program execution.

As mentioned earlier, a computer cannot understand any language other than machine language. As a result, a translation program is needed to interpret assembly language code into machine language code. This translation program is called an assembler. A code written in any programming language, which requires translation for execution, is known as source code.

Functioning of an Assembler

The assembler takes each program statement in the source code in assembly language and generates a corresponding bit stream or pattern (a series of 0s and 1s of a given length). This set of instructions in a bit stream is the output of the assembler and is called the object code. The object code can then be executed whenever required. The assembler generates a list of errors instead of object code if it finds errors in the source code.

The functioning of an assembler is illustrated in the following figure:

Functioning Of An Assembler


Advantages of the Assembly Language

Assembly language programs are easier to write and understand than machine language because they use alphanumeric code instead of binary numbers. The programmer can easily remember the opcode and alphanumeric names for the addresses of data and instructions. This saves time and effort in developing and modifying assembly language programs.

Limitations of the Assembly Language
Assembly language also has a few limitations. These are as follows:
  •  The programs created in assembly language are slower than those created in the machine language. This is because extra time is required to translate assembly language instructions into machine language instructions.
  • Programs created in assembly language are dependent on processors because the assembly language vocabulary varies for different processors. Therefore, a program written in assembly language for one processor might not run on another processor.  

    

COMPUTER PROGRAMMING: FUNDAMENTALS PART2

MACHINE LANGUAGE

All computers use the binary number system, comprising the binary digits, 0 and 1 for performing internal operations. Machine language is the only language that uses binary digits, to represent an instruction. Therefore, the computer can directly process the instructions written in machine language.
A number of languages can be used to write programs. However, the computer understands only machine language. The programs written in other programming languages need to be translated to machine language for execution.
The machine language program being written in binary digits is difficult to learn because it is difficult to read and understand. For example, a program instruction to print a number might be
1011001111101001   1110110011001111
NB: The binary number system uses the base of 2. For example, 101 in the binary system is equal to 5 in the decimal system.
The conversion can be done as:
101=1*22 + 0*21+ 1* 20 =1*4 + 0*2 + 1*1 = 4 + 0 + 1=5
Machine language is also called a first-generation language.

Advantage of the Machine Language
The main advantage of the machine language is that computer programs written in a machine language execute fast. This is possible because machine instructions are in binary form, which are directly understood by the computer and no translation of the instructions is required.

Limitations of the Machine Language
Machine language has the following limitations:
·         Machine language programs are difficult to write because the programmer either needs to remember all the operation code or refer constantly to the reference card of the computer. The programmer also needs to remember the memory locations of the data.
NB:A reference card is a documented form of machine instructions for a computer. It lists the binary form   of various machine language instructions such as print and read.

·         The machine language code differs from one type of computer to another. This is because the internal design of each type of computer varies from other types and needs different signals to operate.
·         Machine language programs are error-prone because it is difficult to write a machine language program. A single error of a 0 or 1 can change the interpretation of the program.
Programs written in a machine language are difficult to modify because of their complexity.

COMPUTER PROGRAMMING: FUNDAMENTALS PART1

INTRODUCING PROGRAMMING LANGUAGES

A language is a means of communication between people. Similarly, a programming language is a means of communication between a programmer and a computer. A programmer writes a set of instructions in a programming language to instruct the computer to perform a task. A set of logically related instructions is called a program.
Every language is nothing but a collection of words and symbols, which are understood by users of the language. This collection of words and symbols is called the vocabulary of a language.
The vocabulary of a programming language is not as vast as the vocabulary of a commonly spoken communication language. Like any other communication language, all programming languages have a set of rules, called the syntax of the language.
Programming languages can be classified into three broad categories:
·         Machine language                                             
·         Assembly language
·         High-level language
As stated earlier, a program written in any programming language is a set of logically related instructions. These instructions have two parts, as shown in the following figure:
Format Of A Programming Language

·         Operation code (opcode): This part instructs a computer about the operation to be performed.
·         Operand: This part instructs the computer about the location of the data on which the operation specified by the opcode is to be performed.
For example, in the instruction Add A and B, Add is the Opcode and A and B are operands. 
Each computer has its own set of opcode. The opcode in the instruction set can perform the following operations:
·         Logical operations: These operations involve the comparison of data and the result is either True or False.
·         Arithmetic operations: These operations involve mathematical calculations.
·         Branch operations: These operations involve transfer of control a memory location given in an operand field.
·         Data movement operations: These operations involve data transfer between the memory and the processor or between the input/output devices and the computer.

It must be understood here that the objective of writing a program is to generate the predefined output with the use of computers. However, programs can be long and complex. There are tools available to programmers that help them minimize the time taken to get a program up and running. Depending on the standards followed by their organization, programmers use flowcharts or pseudocode during the planning phase of problem solving. There are also specialized tools available for representing complex logic like decision tables and decision trees.