410 likes | 743 Views
Evolution of Programming Languages. Generations of PLs. http://fahmirahman.wordpress.com/2011/01/04/five-generations-of-computer/. First generation computers relied on machine language, the instructions of which are long strings of binary codes. Generations of PLs. 1GL – Machine language
E N D
Evolution of Programming Languages Generations of PLs
http://fahmirahman.wordpress.com/2011/01/04/five-generations-of-computer/http://fahmirahman.wordpress.com/2011/01/04/five-generations-of-computer/ First generation computers relied on machine language, the instructions of which are long strings of binary codes.
Generations of PLs 1GL – Machine language . String of 0’s and 1’s . Directly executed by CPU 2GL – Assembly language . Symbolic equivalent of machine language 3GL – 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 • 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
Microsoft Windows DEBUG Textbook P.6 Act. 1 - Writing a real assembly language program General registers 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.
A sample assembly language program explained 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)
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
Queries in Prolog mary Peter mary Fill in the table based on the following facts: loves(peter, snacks). loves(peter, mary). loves(mary, fruit). loves(mary, snacks). loves(mary, tom).
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 rule loves(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 rule loves(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 rule loves(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 PLs 1GL – Machine language . String of 0’s and 1’s . Directly executed by CPU 2GL – Assembly language . Symbolic equivalent of machine language 3GL – 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
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