Professional Documents
Culture Documents
Introduction to Programming Languages - Full Lessons
Introduction to Programming Languages - Full Lessons
Programming Languages
Lesson 1
Introduction
Examples:
Creating arrays, strings, lists, records.
Using recursions, object classes, etc.
Lesson 2
Principles of Programming
Languages
movl #0x1,n
• Of course a compare:
program written
in machine code, cmpl #oxa,n
in order to “run”, cgt
must first be end_of_loop
translated
(assembled) into acddl #0x1,n
machine code.
bra compare
end_of_loop:
ASSEMBLY LANGUAGE (2)
An assembly language is a low-level language for
programming computers.
name "add"
mov al, 5 ; bin=00000101b
mov bl, 10 ; hex=0ah or bin=00001010b
add bl, al ; 5 + 10 = 15 (decimal) or hex=0fh or
bin=00001111b
LOAD A
ADD B
STORE C
Lesson 3
Programming Languages Paradigm
Discussions
Principles of PLP
Concepts of PLP
program = algorithms +
Imperative style: data
good for decomposition
program = functions o
Other processing paradigms were invented that went away from the von
Neumann model, an examples are Dataflow machines and LISP
Assembly was, and still is, used for time-critical systems and
frequently in embedded systems.
Specifying the steps the program must take to reach the desired state.
Often a better choice than simple sequential or unstructured programming in many situations
The ability to re-use the same code at different places in the program without copying it.
An easier way to keep track of program flow than a collection of "GOTO" or "JUMP"
statements (which can turn a large, complicated program into spaghetti code).
Procedures
Procedures are convenient for making pieces of code written by different people
or groups represented in programming libraries.
specify a simple interface
these languages, instructions were very simple, which made hardware implementation easier, but
FORTRAN (1954) was the first major programming language to remove through abstraction
FORTRAN was a compiled language that allowed named variables, complex expressions,
In the late 1950s and 1960s, ALGOL was developed in order to allow mathematical
In the 1970s, Pascal was developed by Niklaus Wirth, and C was created by Dennis Ritchie for
the needs of the United States Department of Defense, Jean Ichbiah and a team at Honeywell
Determine;
the output desired
the required input
processing that will occur
The set of values of the attributes of a particular object forms its state.
The object consists of the state and the behavior that's defined in the
object's class.
In the case of a regular field (also called instance variable), for each instance
of the object there is an instance variable.
A static field (also called class variable) is one variable shared by all instances.
A class is a data structure that may contain different fields, defined to contain
the procedures that act upon it. It represents an abstract data type.
Constructors: Creation and initialization of the state of an object. Constructors are called
Retrieval and modification of state: accessor methods are used to access the value of a
class may also define private methods visible from the internal perspective of the object.
Destructor: When an object goes out of scope, or destroyed, its destructor is called by the
run-time system. This frees the memory and resources used during its execution.
Object-Oriented PLD – Inheritance (6)
Inheritance is a way to compartmentalize and reuse code by creating collections
of attributes and behaviors (classes) based on previously created classes.
The new classes, known as subclasses (or derived classes), inherit attributes and
behavior of the pre-existing classes, which are referred to as superclass.
Multiple inheritance can be defined whereas a class can inherit from more than
one superclass. A single class can then inherit from two classes that have members
bearing the same names, but yet have different meanings.
Abstract inheritance can be defined whereas abstract classes can declare member
functions that have no definitions and are expected to be defined in all of its
subclasses.
For example, a class Car would be made up of an Engine, Gearbox, Steering objects, and
many more components. To build the Car class, one does not need to know how the
different components work internally, but only how to interface with them, i.e., send
messages to them, receive messages from them, and perhaps make the different objects
The reason for encapsulation is to prevent clients of an interface from depending on those parts of the
implementation that are likely to change in the future, thereby allowing those changes to be made
If a Dog is commanded to speak(), this may elicit a bark(). However, if a Pig is commanded to
speak(), this may elicit an oink(). They both inherit speak() from Animal, but their derived class
methods override the methods of the parent class. This is overriding polymorphism.
The "+" operator, for example, may be used to perform integer addition, float addition, list
Languages that are "pure" OO languages treat everything in them consistently as an object,
from primitives such as characters and punctuation, all the way up to whole classes,
prototypes, blocks, modules, etc. They were designed specifically to facilitate, even
Languages designed mainly for OO programming, but with some procedural elements.
Languages that are historically procedural languages, but have been extended with some
OO features. Examples: VB.NET (derived from VB), Fortran 2003, Perl, COBOL 2002,
PHP.
Languages with abstract data type support, but not all features of object-orientation,
Constraint programming
Logic programming
Functional programming
The reflection-oriented programming paradigm, however, adds that program instructions can be
modified dynamically at runtime and invoked in their modified state.
That is, the program architecture itself can be decided at runtime based upon the data, services,
and specific operations that are applicable at runtime.
It requires the translation process to retain in the executable code much of the higher-
level information present in the source code, thus leading to more bloated executables.
"Scripts" are distinct from the core code of the application, as they are
usually written in a different language and are often created by the end-user.
Scripts are most often interpreted from source code, whereas application
software is typically first compiled to a native machine code or to an
intermediate code.
Early mainframe computers (in the 1950s) were non-interactive and instead
used batch processing. IBM's Job Control Language (JCL) is the archetype of
scripting language used to control batch processing.
Languages of this sort are designed for a single application; and, while they may
superficially resemble a specific general-purpose language (e.g. QuakeC,
modeled after C), they have custom features that distinguish them.
Analysis-Coding-Execution
Algorithm :
Analysis and
algorithm design
done apart from any
specific programming
Processing of the high-
language
level programming
language
Lesson 4
Introducing Programming Languages
Other considerations:
efficiency
compatibility with existing code
availability of translators
Common Constructs:
basic data types (numbers, etc.); variables; expressions;
statements; keywords; control constructs; procedures;
comments; errors ...
Uncommon Constructs:
type declarations; special types (strings, arrays,
matrices, ...); sequential execution; concurrency
constructs; packages/modules; objects; general
functions; generics; modifiable state; ...
Status
Still the dominant language for AI
COMMON LISP and Scheme are contemporary dialects
ML, Miranda, and Haskell are related languages
IBM’s goal:
• Develop a single computer (IBM 360) and a single programming
language (PL/I) that would be good for scientific and business
applications.
• Eventually grew to include virtually every idea in current practical
programming languages.
END HELLO;
Begin
while 1 = 1 do begin
outtext ("Hello World!");
outimage;
end;
End;
BASIC
Developed at Dartmouth College in mid 1960s
Minimal; easy to learn
Incorporated basic O/S commands (NEW, LIST, DELETE, RUN, SAVE)
...
'HELLO WORLD'
...
(DEFUN HELLO-WORLD ()
(PRINT (LIST 'HELLO 'WORLD)))
Successes
Unix Shell, awk, emacs, HyperTalk, AppleTalk, TCL, Python, Perl,
VisualBasic ...
Lesson 5
Statements and Constructs
Start
Print answer
Get 2 numbers
End
Add them
Pseudocode:
Get yr
◦ Start
◦ Get year born Calc age
◦ Calculate age
◦ Print age Print age
◦ If age > 50 print OLD
◦ End Y
OLD Age>50?
N
End
Data Types
Integer: Whole numbers
Real (float): Numbers with fractions
Character: Symbols
Boolean: True/false
Inheritance and
polymorphism
combined allow the
programmer to build
useful hierarchies of
classes that can be
reused in different
applications
Count-controlled loops
◦ Repeat a specified number of times
◦ Use of a special variable called a loop control variable
goto next
L(n-1): code for Stmt(n-1)
goto next
L(n): code for Stmt(n)
goto next
test: if (t=V1) goto L1
if (t=V2) goto L2
… if (t=V(n-1)) goto L(n-1) else goto L(n)
L1: if c goto L2
goto L3
L2: x= y+1
L3:
N Y
Print “K” X > 5? Print “1st”
K will be printed. The answer to the question “Is X greater than 5?” is NO, since X is equal to (not greater than) 5.
◦ End
END
Lesson 6
Application Domains of
Programming Languages
Simplicity
Orthogonality
Control Statements
Data types and Structures
Syntax
P --> D; E
D--> D;D
D--> id :T { addtype(id.entry,T.type);}
T--> char {T.type= char;}
T--> integer {T.type=integer;}
T-->*T_1 {T.type=pointer(T_1.type);}
T--> array[num] of T_1 { T.type
=array(1..num.val,T_1.type); }
E.g. The type of an array includes the index set of an array, so a function
with an array argument can only be applied to arrays with that index set.
Lesson 7
Mathematical Expressions
Analog Signal
Digital systems use digital circuits that process digital signals which
can take on one of two values, we call:
0 and 1 (digits of the binary number system)
187
Dr Joe Essien - Computer Architecture
Boolean Algebra
e.g. 0 OR 1 = 1 0 OR 0 = 0
1 AND 1 = 1 1 AND 0 = 0
NOT 0 = 1 NOT 1 = 0
188
Dr Joe Essien - Computer Architecture
Digital (logic) Elements: Gates
Digital devices or gates have one or more inputs and produce an output
that is a function of the current input value(s).
All inputs and outputs are binary and can only take the values 0 or 1
189
Dr Joe Essien - Computer Architecture
Boolean Algebra and Logic Gates
Set of Elements: {0,1}
Set of Operations: {., + , ¬ }
x y x.y x y x+y x ¬x
0 0 0 0 0 0 0 1
0 1 0 0 1 1 1 0
1 0 0 1 0 1
1 1 1 1 1 1 NOT
AND OR
x x
x.y x+y x x'
y y
a a
OR a+b + a+b
b b
NOT a 1 a'
a a'
a a
(a.b)' & (a.b)'
NAND b b
a a
NOR (a+b)' 1 (a+b)'
b b
a a
EXCLUSIVE OR ab =1 ab
b b
INPUTS OUTPUTS
… …
… …
Truth table
Example (2 inputs, 2 outputs)
inputs outputs
x y x.y x+y inputs outputs
0 0 0 0 x x.y
Digital
0 1 0 1 y circuit x+y
1 0 0 1
1 1 1 1
192
Dr Joe Essien - Computer Architecture
Logic Gates: The AND Gate
A
A.B
B A B A.B
0 0 0
Vcc
12 11 10 9 8
0 1 0
14 13
1 0 0
1 1 1
Truth table
1 2 3 4 5 6 7
Ground
Top View of a TTL 74LS family 74LS08 Quad 2-input AND Gate IC Package
193
Dr Joe Essien - Computer Architecture
Logic Gates: The OR Gate
A A B A+B
A+B
B 0 0 0
0 1 1
1 0 1
1 1 1
Truth table
Top View of a TTL 74LS family 74LS08 Quad 2-input OR Gate IC Package
194
Dr Joe Essien - Computer Architecture
Logic Gates: The NAND Gate
A
(A.B)'
A
(A.B)'
B B
NAND gate is self-sufficient (can build any logic circuit with it).
Can be used to implement AND/OR/NOT.
Implementing an inverter using NAND gate: x x'
A B (A.B)'
0 0 1
0 1 1
1 0 1
1 1 0
Truth table
Top View of a TTL 74LS family 74LS00 Quad 2-input NAND Gate IC Package
195
Dr Joe Essien - Computer Architecture
Logic Gates: The NOR Gate
A
B
(A+B)' A
(A+B)'
B
NOR gate is also self-sufficient (can build any logic circuit with it).
Can be used to implement AND/OR/NOT.
A B (A+B)'
0 0 1
0 1 0
1 0 0
1 1 0
Truth table
Top View of a TTL 74LS family 74LS02 Quad 2-input NOR Gate IC Package
196
Dr Joe Essien - Computer Architecture
Logic Gates: The XOR Gate
A B AB
A
AB 0 0 0
B
0 1 1
Vcc 1 0 1
14 13 12 11 10 9 8
1 1 0
Truth table
1 2 3 4 5 6 7
Ground
Top View of a TTL 74LS family 74LS86 Quad 2-input XOR Gate IC Package
197
Dr Joe Essien - Computer Architecture
Drawing Logic Circuits
When a Boolean expression is provided, we can
easily draw the logic circuit.
Examples:
F1 = xyz'
(note the use of a 3-input AND gate)
x
y F1
z z'
198
Dr Joe Essien - Computer Architecture
Analyzing Logic Circuits
F4 = (A'B'+C)'
A'
B'
F4
C
A'B' A'B'+C (A'B'+C)'
199
Dr Joe Essien - Computer Architecture
Programming Languages
Implementation
Lesson 8
Runtime Considerations
Fragmentation
◦ Internal, block allocated is larger than
needed by the allocation request
◦ External, storage available, but not
contiguous.
BinaryAllocator Performance
Issues
Internal Fragmentation
◦ Need 40 bytes, get 64 bytes, 24 wasted
External Fragmentation
◦ May have 2 non-contiguous 128 byte
blocks, but cannot allocate 256 bytes
Performance
◦ Bounded (at most K allocation steps)
Controlled Allocation
In C++, constructors automatically allocate
storage, destructors free storage.
Replacement semantics
Raise/Throw an exception
Abandons current execution
Strips stack frames
Till exception is handled/caught
Calendar/Time Issues
Match of semantics
Time changes, daylight saving
Resolution
Special target dependent capabilities
E.g. high resolution timer on NT
Certification Issues
Safety-Critical certification
Requires complete testing
And documentation of process
For a run-time
Must provide testing materials
Certify the process
One approach: no run-time at all!
Applies to OS as well
WRS new certifiable kernel
In preparation for a follow up of this course
you will install the following Programming
Languages and tools on your Laptops: