Computer Programs

Parts of a Program

A program has the following parts. Some of the parts are optional. A program written in a high level language like Python has to be converted to machine language of the CPU that this program will run on.

Translating Programs to Machine Code

The CPU understands machine language. The machine language instructions are encoded as strings of bits. The instruction set is dependent on the type of CPU. The instructions are primitive like load, add, store, jump.

The machine instruction that adds 3 to register D1 might be encoded as the bit string:

0101 011 000000 001

Writing and understanding machine language code is error prone. The first step was to have short mnemonics for the primitive operations instead of binary strings. The above instruction could be written as:

ADD 3, D1

This is assembly language programming. An assembler translates the mnemonics to machine code. But even the assembly language code is unwieldy. The time to write, debug, understand, and maintain a piece of code is proportional to the number of lines of instruction.

A high level language allows common operations such as expression evaluation, repetition, assignment, and conditional action to be invoked in a single high-level statement. Examples of high level languages - Fortran, Pascal, C, C++, Lisp, SmallTalk, Basic, Java, Perl, PHP, Python, Ruby.

A computer does not understand a program that is written in a high level language. A compiler translates a high level program into machine code for that particular CPU and saves it to a file known as object code or executable code. To run the program the executable has to be loaded and then run. The advantage of compiled code is that the compiler can optimize the code for you. The disadvantage of compiled code is that this code is specific only for a particular type of processor and will not run on any processor.

There is another way to run the code. A special program called the interpreter can take the source code and translate each line of code to machine language and run it for you. The disadvantage is that you have to have the source code available each time the code is run and it is slow. The advantage, however, is that since you are working from the source code each time, the source code can be ported from one machine to another since it is just text file. Python is an interpreted language.