Programs, Languages, and Compilers
The sequence of instructions that the computer can perform (or execute) to accomplish a task is called a program. For every task we want to perform, we need an appropriate program. And in order for the program to run a program must be stored (or loaded) in the computer’s memory. Computers are built to execute instructions written in what is called machine language. In every machine language, everything is expressed in terms of binary number system – 1s and 0s. Each computer has its own machine language and the computer can execute instructions written in that language only.
Machine language is considered a low – level programming language. In the early days of computing (1940s and ’50s) programmers had to write programs in machine language, that is, express all their instructions using 1s and 0s. To make a life little easier for them, assembly language was developed. This was closely related to machine language but it allowed the programmer to use mnemonic instruction codes such as ADD and names for storage locations ( such as sum ) rather than strings of binary digits ( bits ). For instance, a programmer could refer to a number by sum rather than have to remember that the number was stored in memory location 1000011101101011.
A program called an assembler is used to convert an assembly language program into machine language. Still, programming this way had several drawbacks:
- It was very tedious and error prone.
- It forced the programmer to think in terms of the machine rather than in terms of his problem.
- A program written in the machine language of one computer could not be run on a computer with a different machine language. Changing your computer could mean having to rewrite all your programs.
To overcome these problems, high – level or problem – oriented languages were developed in late 1950s and ’60s. The most popular of these were FORTRAN ( FORmula TRANslation ) and COBOL ( COmmon Business Oriented Language ). FORTRAN was designed for solving scientific and engineering problems that involved a great deal of numerical computation. COBOL was designed to solve the data – processing problems of the business community. This enabled the programmer to concentrate on solving the problem without the added burden of worrying about the idiosyncrasies of a particular machine. However, the computer still could only execute instructions written in machine language. A program called a compiler is used to translate a program written in a high – level language to machine language.
How a Computer Solves a Problem
Solving a problem involves following activities:
Define the Problem
Suppose we need to add two numbers. This defines a problem to be solved. However, this brief information is not enough to proceed with developing a program. Further information could be to take to two numbers from the user to add and then print the result.
Analyse the Problem
We further analyse the problem to
- Ensure that we have the clearest possible understanding of it.
- Determine general requirements such as main inputs to the program and the main outputs from the program. For more complex programs, we would, for instance, also need to decide on the kinds of files that may be needed.
- If there are several ways of solving the problem, we should consider the alternatives and choose the best or most appropriate one.
Develop an Algorithm to Solve the Problem
An algorithm is a set of instructions that, if faithfully followed, will produce a solution to a given problem or perform some specified task. For any problem, there will be more than one algorithm to solve it. Each algorithm will have its own advantages and disadvantages. In any situation, a programmer would usually have a choice of algorithms, and it is one of her more important jobs to decide which algorithm is the best, and why this is so. Computer instructions fall into three main categories :
- Input instructions, used for supplying data from the “outside world” to a program ; this is usually done via the keyboard or a file.
- Processing instructions, used for manipulating data inside the computer. These instructions allow us to add, subtract, multiply, and divide; they also allow us to compare two values, and act according to the result of the comparison. Also, we can move data from one location in the computer’s memory to another location.
- Output instructions, used for getting information out of the computer to the outside world.
Data and Variables
All computer programs, except the most trivial, are written to operate on data. A program must be stored in the computer’s memory for it to be run. When data is supplied to a program, that data is also stored in memory. Thus we think of memory as a place for holding programs and data. One of the nice things about programming in a high level language is that you don’t have to worry about which memory locations are used to store your data. Think of memory as a set of boxes. Each box can hold one items of data. We can give a name to a box, and we will be able to refer to that box by the given name. A variable ( or assume it as a box ) is a name associated with a particular memory location. Important points to remember are :
- A variable (or assume it as a box) can hold only one value at a time, if we put in a new value, the old one is lost.
- We must assume that a variable (or assume it as a box) contains any value unless we specifically store a value in the variable. In particular, in must not assume that the box contains zero.
Develop an Algorithm
When an algorithm is developed, it must be checked to make sure that it is doing its intended job correctly. We can test an algorithm by executing instructions by hand, using appropriate data values. This process is called dry running or desk checking the algorithm. It is used to pinpoint any errors in logic before the computer program is actually written. We should never start to write programming codes unless we are confident that the algorithm is correct.
Write the Program for the Algorithm
In order to write the computer program from the algorithm, a suitable programming language must be chosen. We can think of a program as a set of instructions, written in a programming language, which, when executed, will produce a solution to a given problem or perform some specified task.
The major difference between an algorithm and a program is that an algorithm can be written using informal language without having to follow any special rules whereas a program is written in a programming language and must follow all the rules of the language. A program is written in a high – level language is usually referred to as a source code or source program.
Test and Debug the Program
After writing the program, the next job is to test it to find out whether it is doing its intended job. Testing a program involves following steps:
- Compile the program: Computer can execute a program written in machine language only. Before the computer can run our program the latter must be converted to machine language. We say that the source code must be converted to object code or machine code. The program that does this job is called compiler.
- A compiler will check the source code for Syntax error – error that arise from breaking the rules for writing statements in the language.
- If the program contains syntax errors, these must be corrected before compiling it again. When the program is free from syntax errors, the compiler will convert it to machine language and we can go to the next step.
- Run the program: We request the computer to execute the program and we supply the data to program for which we know the answer. Such data is called test data.
- If the program does not give us the right answers then we know that the program contains at least one logic error. A logic error is one that causes a program to give incorrect results for valid data. A logic error may also cause the program to crash.
- If a program contains logic errors, we must debug the program; we must find and correct any errors that are causing the program to produce wrong answers.
Document the Program
The final job is to complete the documentation of the program. Documentation includes the following:
- The statement of the problem.
- The algorithm for solving the problem.
- The program listing.
- Test data and the result produced by the program.
These are some of the items that make up the technical documentation of the program. This is documentation that is useful to a programmer, perhaps for modifying the program at the later stage. The other kind of documentation that must be written is user documentation. This enables a nontechnical person to use the program without needing to know about the internal workings of the program.
Maintain the Program
Programs are normally meant to be used over a long time. During this time, errors may be discovered that previously went unnoticed. Errors may also surface because of conditions or data that never arose before. Such errors must be corrected.
But a program may need to be modified for other reasons. Perhaps the assumptions made when the program was written have now changed due to changed company policy or even due to a change in government regulations. Perhaps the company is changing its computer system and program needs to be ” migrated ” to the new system. We say the program must be “maintained”.
If it was well-designed and properly documented, then the job of the maintenance programmer would be so much easier.
How a Computer Executes a Program
A computer can execute a program written in machine language only. For the computer to execute the instructions of such a program, those instructions must be loaded into the computer’s memory also called as primary storage.
You can think of a memory as a series of storage locations, numbered consecutively starting at 0. The number associated with a memory location is called its address. A computer runs a program by executing its first instruction, then the second, then the third, and so on. It is possible that one instruction might say to jump over several instructions to a particular one and continue executing from there. Another might say to go back to a previous instruction and execute it again.
No matter what the instructions are, the computer faithfully executes them exactly as specified. The computer cannot know what you intend, it can only execute what you actually write.
There are two types of data with which we interact, “names” and “numbers”. Also numbers are also divided into two parts, ” whole number or integers” and ” real or floating-point numbers”.
In computer terminology, we use the term character to refer to any one of the following:
- A digit from 0 to 9.
- An uppercase letter from A to Z.
- A lowercase letter from a to z.
- A special symbol such as ( ), $, =, <, >, +, -, /, *, etc.