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.