Principles of Programming Languages Tasks.
There are two tasks, both entry level, which are,
Assignment 0 – Some Sample Programs in Shank and Task 1 – The Lexer.
Procedural Programming
From the beginning, programming was hard
Programming was done,
at first, by re-wiring.
Machine Language
With the adoption
of computer
memory as a way
to store programs,
we moved to
machine language.
Machine Language – First Generation Language
Machine language is made up of instructions, stored in bits.
1001 1100 1010 0011 (for example)
These bits might indicate that the computer should add two numbers
together (stored at addresses 14 and 10) and saving them into another
address (3).
No loops or advanced flow control
You can jump to a different instruction by comparing two values:
Is the first less than the second?
Greater than the second?
Equal?
Not Equal?
There were also no variable names – everything was by address in the
computer.
Clearly this was difficult…
To write a program, you had to:
Understand the algorithm
Break it down to the instructions that the computer could do
Manage your resources (memory, input, output)
Deal with infinite loops
All with no debugging capability
And if the program is wrong, you have to fix and re-enter by hand!
Asssembly – Second Generation Language
The most pressing need (and it was done fairly quickly) was to get out
of flipping switches and entering bit values.
Assembly language is a textual representation of machine language.
It is a 1-1 translation – one line of assembly is one machine language
instruction.
1001 1100 1010 0011 ➔ ADD R14, R10, R3
Assembler
Assembly Language is the language. An assembler is the program that
takes the text (ADD R14, R10, R3) and converts it into bits.
This is still slow and arduous, but easier than working in bits.
Assembly language is still used today by people who work close to the
hardware!
Often game developers will do some level of high performance work in
assembly.
Growth of computing
While assembly language was a big jump, it still presents a number of
problems that started to be pressing.
Computers were getting cheaper and more of them were being made.
Companies started looking at how computers could help them with
handling their exploding data needs.
What they found, at first, was not an easy path.
Problems with working in Assembly
Assembly language is
machine (brand) specific
labor-intensive (and therefore expensive)
arduous (tedious and difficult to write long programs)
unintelligible (hard for others to read what you wrote)
highly detailed (even things we don’t care about)
hard to reuse any existing work
Begin the abstraction…
While the 1st and 2nd generation languages were all very tied to the
machine design, the 3rd generation were far less so – they were more
abstract.
By taking a step back from the hardware and writing programs based
on what one wants to do, a number of the issues with assembly were
addressed.
Procedural languages solve issues!
Assembly procedural language(s) is(are):
machine (brand) specific “portable”
labor-intensive (and therefore expensive) Quick to develop in
arduous (tedious and difficult to write long programs) Easy
unintelligible (hard for others to read what you wrote) Clear
highly detailed (even things we don’t care about) Abstract
hard to reuse any existing work Encourage reuse
Procedural Language Innovations
Named variables
Loops (for, while)
If
abstractions for reading/writing
Functions/Procedures/Subroutines (parameters and return values!)
Data structures (Cobol records)
Scoped variables
Enforcement of data types
A hint of the flavor – Fortran
In 1956, John Backus (you will hear his name later!) and his team
created a language called Fortran (FORmula TRANslator).
A team at IBM spent 25 person-years to build a language designed to
take scientific formulae and convert them (nearly) 100% efficiently to
machine language.
Fortran is still used in scientific computing because of its high
performance and wide-spread use.
C AREA OF A TRIANGLE – HERON’S FORMULA
C INPUT – CARD READER UNIT 5, INTEGER INPUT
C OUTPUT – LINE PRINTER UNIT 6, REAL OUTPUT
C INPUT ERROR DISPAY ERROR OUTPUT CODE 1 IN JOB CONTROL LISTING
INTEGER A,B,C
READ(5,501) A,B,C
501 FORMAT(3I5)
IF(A.EQ.0 .OR. B.EQ.0 .OR. C.EQ.0) STOP 1
S = (A + B + C) / 2.0
AREA = SQRT( S * (S – A) * (S – B) * (S – C) )
WRITE(6,601) A,B,C,AREA
601 FORMAT(4H A= ,I5,5H B= ,I5,5H C= ,I5,8H AREA=
,F10.2,12HSQUARE UNITS)
STOP
END
A hint of the flavor – Algol 60
The ACM (Association for Computing Machinery) worked together to
design a language that was “Fortran for algorithms” (and not tied to
IBM hardware).
A number of features were included in Algol that Fortran did not have,
including local variables, dynamic arrays and recursion.
FOR i := i WHILE i0 THEN 150
90 LET X=XD
100 GOTO 160
150 LET X = X – D
160 IF D < .0001 THEN 200
170 LET D=D/2
180 GO TO 60
200 PRINT “ROOT = “ X
999 END
A hint of the flavor – Pascal (1968-1972)
Designed by Niklaus Wirth, a Swiss computer scientist. We wanted to
simplify ALGOL and focus on education.
Simple highly structured conditionals and loops
The ability to make enumerations
More data structures (records, files, sets)
Included pointers
Used “P-Code” – a stack based virtual machine
Program TowersOfHanoi(input,output);
Var
disks: integer;
Procedure Hanoi(source, temp, destination: char; n: integer);
begin
if n > 0 then
begin
Hanoi(source, destination, temp, n – 1);
writeln(‘Move disk ‘,n:1,’ from peg ‘,source,’ to peg ‘,destination);
Hanoi(temp, source, destination, n – 1);
end;
end;
begin
write(‘Enter the number of disks: ‘);
readln(disks);
writeln(‘Solution:’);
Hanoi(‘A’,’B’,’C’,disks);
end.
C, Shell, UNIX
The C programming language, the Bash (and other) shell language and
UNIX all came from Bell Labs in the early 1970’s. These are so critical to
our understanding of Computer Science that they get their own course.
Of all of the procedural programming languages, C is, by far, the most
popular today. It is highly portable and is particularly designed for
“systems programming” – writing operating systems and other low
level software.
/* add.c
* a simple C program
*/
#include
#define LAST 10
int main()
{
int i, sum = 0;
for ( i = 1; i