Evolution of Programming Languages Generations of PLs.

Post on 29-Dec-2015

216 views 1 download

Tags:

Transcript of Evolution of Programming Languages Generations of PLs.

Evolution of Programming Languages

Generations of PLs

First generation computers relied on machine language, the instructions of which are long strings of binary codes.

http://fahmirahman.wordpress.com/2011/01/04/five-generations-of-computer/

Generations of PLs1GL – Machine language

. String of 0’s and 1’s

. Directly executed by CPU2GL – Assembly language

. Symbolic equivalent of machine language3GL – Procedural language

. Give how to do/ algorithm. E.g. Pascal

4GL – Declarative language. Specify what to do. E.g. SQL

5GL – Related to artificial intelligence

Low Level and High Level PLs • Low Level PLs (1-2GLs)

– Instructions directly operate computer hardware– Need to have machine level knowledge in order to

write programs– Machine dependent– Machine language and assembly language

• High Level PLs (3-5 GLs)– More English like– Closer to human language

1 GL – Machine language

• A low level language• Consists of strings of 0’s and 1’s• Directly executed by the CPU• Difficult and tedious to write and maintain• Quickly replaced by assembly language (2GL)

2 GL – Assembly Language• Instruction has a one-to-one correspondence

with its machine language equivalent• Instruction consists of opcode = operation code

Action Memory Address, register or value

e.g. ADD AX, 3 ; increase AX by 3

Opcode Operand(s)

• Machine instructions are symbolized using mnemonics into assembly language instructions

A sample machine language program and the corresponding assembly language program

2 GL – Assembly Language

• A low level language• Translated into machine language for

execution• Manipulate computer down to register level• Hardware dependent• Difficult to learn and debug• Directly control of CPU operations, therefore

the codes produce are more efficient

Extended Reading

Microsoft Windows DEBUG Textbook P.6 Act. 1 - Writing a real assembly language program

General registers

AX

BX

CX

DX

The 80x86 CPU has four general registers

The accumulator AX stores the results of math operations.

The base register BX stores the address or pointer.

The counting register CX is used in counting and looping.

The data register DX stores data.

Commands of DEBUG

Command/Prompt

Operation

- A prompt for accepting command

r Display the contents of CPU registers.

rax Display the contents of AX.

: A prompt for accepting value to the register being worked with.

a Start assembling (translating) symbolic instructions into machine codes. i.e. Start to accept assembly language program.

t Trace the program. i.e. Execute the program and show the contents of the register.

q Quit DEBUG.

A sample assembly language program explained

Assembly Instruction Explanation

mov ax, 5 Move the value 5 (hex) to register AX.

mov bx, 3 Move the value 3 (hex) to register BX.

add ax, bx Add the values stored in register AX and BX and the result back to register AX.

int 20 Call the DOS interrupt 20 to terminate the program.

Choose run > cmd to open the command prompt window

Enter debug to the command prompt to run debug

Want to try it out yourself?

P.6 Activity 1 Writing a simple assembly language program using DEBUG

5 minutes

Typo in textbook P.8 Step 9: the first -t should be -r

3GLs (high level)• Close to human language, more English like• E.g. Pascal, FORTRAN, BASIC, COBOL, C,...• Programmers can focus on solving problems

rather than manipulating registers and memory addresses

• Easy to write, debug and maintain• Loss of direct control of CPU operations and

memory addresses• Programs more portable (machine independent)

Assembly language Pascal

185B:0100 mov ax, 5185B:0103 mov bx, 3185B:0106 add ax, bx185B:0108 int 20185B:010A

var a, b : integer;begin a := 5; b := 3; a := a + bend.

The same program written in assembly language (2GL) and Pascal (3GL). The use of variables, data types and mathematical expression syntax in 3GL hides the complexities of CPU’s internal operations from the programmer.

4GLs• Declarative / non-procedural• Specify what to do/ not how to do• In 3GLs, detail steps in solving a problem has to

be coded, in 4GL fewer statements are necessary• More towards solving specific problems

- SQL to make queries on relational database - SPSS to perform data analysis

SQL statement for outputting the number of female students in each class from the database table student:

SELECT class, COUNT(*)

FROM student

WHERE sex = "F"

GROUP BY class;

5GLs• Related to artificial intelligence (AI)• Able to search for solution of the problem by

itself• Based on constraints and conditions given• Programmers can focus on what problems

need to be solved and what constraints and conditions are to be satisfied, rather then algorithm to solve the problem

• E.g. Prolog

Prolog (Program Logic)

In Prolog, the program logic is represented by relations,

and queries can be constructed and run on these relations.

Relations are defined by clauses

and there are two types of clauses: facts and rules

Facts in Prolog loves(peter, snacks).

- a fact clause that defined the relation loves between two

objects peter and snacks

- note that a clause must end with a full stop

- object names must be in lower case (upper-case is for variable)

- the fact defined can be interpreted as ‘Peter loves snacks’

(not the other way round)

  The following 5 clauses define 5 facts:

loves(peter, snacks).

loves(peter, mary).

loves(mary, fruit).

loves(mary, snacks).

loves(mary, tom).

Queries in Prolog

loves(peter,tom).

can be interpreted as ‘Does Peter love tom?’

loves(peter,X).

can be interpreted as ‘What/ who does Peter love?’

loves(X,snacks).

can be interpreted as ‘Who loves snacks?’

Notes:

- X is in upper case to indicate that it is a variable

- loves(peter,tom). can be a fact clause or a query,

depend on usage

Query Result loves(peter,tom). no

loves(peter,X). X = snacksX =

loves(X,snacks). X =X =

Queries in Prolog

Fill in the table based on the following facts:

loves(peter, snacks).loves(peter, mary).loves(mary, fruit).loves(mary, snacks).loves(mary, tom).

Petermary

mary

Rules in Prolog

loves(peter, X) :- loves(mary, X). means Peter loves X if Mary loves X

lovers(X, Y) :- loves(X, Y), loves(Y, X). means X and Y are lovers if X loves Y and Y loves X

Given the 5 facts and the ruleloves(peter, snacks).

loves(peter, mary).

loves(mary, fruit).

loves(mary, snacks).

loves(mary, tom).

loves(peter, X) :- loves(mary, X).

The query loves(peter, X) returns X = snacks

X =

(and more ...)

Given the 5 facts and the ruleloves(peter, snacks).

loves(peter, mary).

loves(mary, fruit).

loves(mary, snacks).

loves(mary, tom).

loves(peter, X) :- loves(mary, X).

The query loves(peter, X) returns X = snacks

X = mary

X = fruit

X = snacks

X = tom

Given the 3 facts and the ruleloves(tom, mary).

loves(peter, mary).

loves(mary, tom).

lovers(X, Y) :- loves(X, Y), loves(Y,X).

The query lovers(peter, mary). returns The query lovers(mary, tom). returns The query lovers(tom, mary). returns

no

true

true

Your turn now (P.15 Activity 2)

Step1: Create a text file named loves.pl storing all the relations (facts and rules)

Step2: Open GNU Prolog enter the command consult('s:/loves.pl').

Step3: Make queries

Generations of PLs1GL – Machine language

. String of 0’s and 1’s

. Directly executed by CPU2GL – Assembly language

. Symbolic equivalent of machine language3GL – Procedural language

. Give how to do/ algorithm. E.g. Pascal

4GL – Declarative language. Specify what to do. E.g. SQL

5GL – Related to artificial intelligence . E.g. Prolog

Hierarchy of programming languages

Try it out with GNU Prolog

Step1: Create a text file named loves.pl storing all the relations (facts and rules) Step2: Open GNU Prolog enter the command consult('s:/loves.pl').Step3: Make queries

Reminders: - don’t leave out the full stop - place loves.pl in your S drive ~ simpler path