Machine language

advertisement
Chapter 6
Programming Languages
Chapter 6: Programming
Languages







6.1 Historical Perspective
6.2 Traditional Programming Concepts
6.3 Procedural Units
6.4 Language Implementation
6.5 Object Oriented Programming
6.6 Programming Concurrent Activities
6.7 Declarative Programming
2
Programming Languages




Complex software system like OS would be developed
impossibly using machine instructions directly.
Programming languages are designed for humans to express
the algorithm easily, yet are convertible to the machine
instructions on the target platform.
As such, the details of registers, memory addresses, and even
machine cycles are out of our focus.
We may then concentrate on the properties of the problems in
question.
3
Historical Perspective



Evolution of computer languages:
Micro-instructions: Inside CPU, they implement each machine
instruction.
Machine language: binary form direct controls the hardware.
E.g., “4032”: R3⇐R2, very hard to read & write the programs.
Assembly language: mnemonic form of the machine language.
E.g., “MV R3 R2”is used to refer to “4032”.
Descriptive name like “Price” is used to represent some memory
address.
“LD R5, Price”(156C) load the content in the address
“Price”0X6C to R5.
Mnemonic ADDI & ADDF refer to integer & floating-point
additions.
4
Historical Perspective
This is a major syntax of revolution in
programming.
Assemblers are the programs which
translate the program written in an
assembly language to the target
machine language.
Shortages: still machine-dependent, and
too naïve to develop large-scaled
programs.
5
Historical Perspective
High-level language: English like language, for more
productive development.
FORTRAN (Formula Translator) for scientific and
engineering applications.
COBOL (Common Business-Oriented Language) from the
U.S. Navy.
The approach is to identify a collection of high-level
programming primitives to construct the software blocks.
[in essentially the same spirit with pseudo codes]
Translators or compilers(initiated by Grace Hopper) are
used to compile several machine instructions in the target
machines to simulate the activity represented by a single
high-level primitive. [interpreters]
6
Historical Perspective
Interpreters—similar to translators except they executed the
instructions as they were translated
i.e. rather than producing a machine language copy of a
program that would be executed later, an interpreter
actually executed a program from its high level form
Programming languages are constructed around a small set
of primitives that could be expressed in a variety of natural
languages with only simple modification to the translators.
Natural language(English, German,.) evloved over time
without formal grammatical analysis
Formal language(programming language)—are precisely
defined by grammars
7
Figure 6.1 Generations of
programming languages
8
Second-generation:
Assembly language

A mnemonic system for representing
programs



Mnemonic names for op-codes
Names for all registers
Identifiers = descriptive names for memory
locations, chosen by the programmer
9
Assembly language
characteristics

One-to-one correspondence between
machine instructions and assembly
instructions



Programmer must think like the machine
Inherently machine-dependent
Converted to machine language by a
program called an assembler
10
Assembly language example
Machine language
156C
166D
5056
30CE
C000
Assembly language
LD R5, Price
LD R6,
ShippingCharge
ADDI R0, R5 R6
ST R0, TotalCost
HLT
11
Third generation language

Uses high-level primitives





Similar to our pseudocode in Chapter 5
Machine independent (mostly)
Examples: FORTRAN, COBOL
Each primitive corresponds to a short
sequence of machine language instructions
Converted to machine language by a program
called a compiler
12
Machine Independence and Beyond






Through compiling, programs can be portedeasily to any
other machine.
The efficiency of compilers also determines the quality of
executable programs.
A programming language may have several dialects over
different machines.
Standardization: ANSI and ISO have adopted and
published several standards for several popular
programming languages. [to improve compatibility]
Human Centric: To achieve true machine independence,
the machines need to be taught to ultimately understand
the abstract concepts used in humans.
Creatively, the machines can learn to automate the
algorithm discovery process.
13
Figure 6.2 The evolution of
programming paradigms
14
Software Development Paradigms







Functional: construct “black boxes”from elementary functions.
Goal: decompose the entire transformation with initial input and
final output into nested complexes of simpler functions. [modular
approach, support OO]
LISP: Average ≡(Divide (Sum Numbers) (Count Numbers));
Min ≡(First (Sort List)).
Object-oriented: OOP uses active objects ≡data + methods.
Merits: modular design, message passing (good for CORBA),
client/server (distributed) over the networks.
Imperative(procedural): the traditional approach to expand the
CPU cycle for a more powerful sequence of commands (code
blocks = macro instructions).
Goal: find an algorithm for the problem.
15
Software Development Paradigms


Declarative: emphasizes more on what the problem
is than how to solve it.
Goal: discover/implement a general problem-solving
algorithm, and develop/write a precise statement of
the problem. [SQL, formal logic, logic programming]
16
Figure 6.3 A function for checkbook
balancing constructed from simpler
functions
17
Software Development Paradigms
Which of the following is an example of a language that is based on
the object-oriented paradigm?


A. LISP
B. PROLOG
C. C
D. C++

Most machine languages are based on the

A. Imperative paradigm
B. Declarative paradigm

C. Functional paradigm
D. Object-oriented
paradigm
Which of the following is not a type of statement found in a typical
high-level imperative programming language?




A. Imperative statement
C. Declarative statement
B. Exclamatory statement
D. Comment statement
18
Traditional Programming Concepts
Generally, programming languages contains:
Declarative statements: define customized names to
be used later in the program.
Imperative statements: describe the steps in the
underlying algorithms.
Comments: enhance the readability of a program for
handy explanation.
19
Traditional Programming Concepts
Declarative statements:
Variables are identifiers or customized names defined to refer
some memory locations.
Literals are fixed, predetermined values.
Constants are customized names initiated with fixed values.
(meaningful literals)
Data type: variables or constants are defined with both names and
data types.
Primitive data types: integer, real, character, Boolean. (predefined
in languages)
Derived data types: are customized data types, combinations of
primitive ones.
20
Traditional Programming Concepts
Declarative statements:
Homogeneous– a block of values of the same types
heterogeneous –a block of data in which different
elements can have different data type
array (structure), queues, objects, or the hybrid
21
Traditional Programming Concepts
22
Figure 6.4 The composition of a typical
imperative program or program unit
23
Constants and literals
EffectiveAlt <- alitmeter + 645;
In C
const int Airaltitude = 645;
In Java
final int Airaltitude = 645;
EffectiveAlt <- alitmeter + Airaltitude;
24
Figure 6.5 Variable declarations in C,
C++, C#, and Java
25
Figure 6.6 A two-dimensional array with
two rows and nine columns
26
Figure 6.7 Declaration of heterogeneous
array
27
Figure 6.8 Control
structures
and their
representations in C,
C++, C#, and Java
28
Figure 6.9 The for loop structure and its
representation in C++, C#, and Java
29
Traditional Programming Concepts
Which of the following does not require a Boolean structure?
A. If-then-else statement
C. Assignment statement
B. While loop statement
D. For loop statement
Which of the following is not a control statement?
A. If-then-else statement
C. Assignment statement
B. While loop statement
D. For loop statement
30
Procedural Unit
A program = code blocks in a concatenated or
hierarchical fashion.
Procedures are some code blocks (or programming
units).
Procedure’s header = the procedure name + other
details:
Declaration statements to declare local variables used
within the procedure.With the same names, local
variables in different procedures are distinct.
Global variables in the parent block are unique in all
children blocks.
31
Procedural Unit
Imperative statements to describe the steps performed
in the procedure.
Invoking the procedure: FORTRAN: “CALL
GetNames”; in Ada/C/C++/Java/Pascall,
“GetNames;”.
32
Figure 6.10 The flow of control involving a procedure
33
Figure 6.11 The procedure Project Population written
in the programming language C
34
Procedural Unit
Parameters are additional information passed to the
procedure upon invocation.
Callers use the actual parameters, each of whose
values mapped orderly to the corresponding one of
formal parameters in the procedure.
“Passed by value”-copy: protect the data in callers, but
inefficient if data is large. The changes on formal
parameters do not reflect on actual parameters.
“Passed by reference”: the same data of callers are
used directly in the procedure. It is favorable to sort
the long list in place.
35
Procedure Demo(Formal)
{Formal<-Formal +1
print formal}
actual = 5
apply Demo to actual
print actual
Figure 6.12 Executing
the procedure Demo and
passing parameters by
value
36
Figure 6.13 Executing
the procedure Demo and
passing parameters by
reference
37
Procedural Unit
Function—behaves like variables
--similar to a procedure except that a
value is transferred back to the calling
program init as “ the value of the function”.
38
Figure 6.14 The function CylinderVolume
written in the programming language C
39
Procedural Unit
Input/Output Statements: call the default procedures for the
program I/O
In Pascal, “ readln(Value) ”retrieves a value from keyboard to
the variable Value;“ writeln(Value) ”outputs the value of
the variable Value to the monitor
In C, “ scanf ”& “ printf(“The price is %d.\n”,
Price);”functions are used instead
In C++, ready-made “ cin >> value ”& “ cout<<
Price; ”objects represent the I/O devices.
40
Language Implementation
The code blocks are ultimately translated into the
sequence of machine instructions.
 Translation Process:
The source program is the program in the original
programming language.
The object program is the translated version resulted
from the translation process:

41
Figure 6.15 The translation process
42
Language Implementation






Lexical analysis by lexical analyzer:
Recognize which strings of symbols
representing a single entity
E.X. const alt = 153;
Classify entity by numeric values, words,
arithmetic operators, etc
Generate a bit pattern known as tokens
The entire program byte streams are recognized as
a series of tokens of various types.( if a < b then a++;
else b++;)
43
Language Implementation

Parsing by parser:

Tokens regarding to comments are skipped.Parser groups
tokens together into statements according to the grammatical
rules(syntax).
Early programming languages (fixed-format languages) insist
the particular positions for statements; most languages today
are free-format using delimiters like semicolons, key words,
reserved words, etc. to delineate statements.
Grammatical rules can be illustrated by syntax diagrams.

Code generation by code generator:
44
Figure 6.16 A syntax diagram
of our if-then-else pseudocode statement
45
Figure 6.19
Two distinct
parse trees for
the statement
if B1 then if B2 then S1 else S2
46
Ambiguities Avoidance

Ambiguities rise when the syntax rules yields
two or more distinct parse trees for the same
series of tokens.
The single statement “if B1 then ifB2 thenS1
elseS2”reveals such flaw.
 Avoidance: (uniqueness)
Using parentheses to specify the precise join.
[programmers]
Adjusting the syntax rules for default join (nearest).
[languagedesigners]

47
Figure 6.17 Syntax diagrams describing the
structure of a simple algebraic expression
48
Figure 6.18 The parse tree for the string x + y x z based on the
syntax diagrams in Figure 6.17
49
Code Generation
To analyze imperative statements, the
parser first processes the declarative
statements and records each userdefined identifier in the symbol table.
For variables, their data types and data structures are
booked.
For procedures, their return types and lists of formal
parameters are stored.
50
Code Generation
As an example, “Total ←Price + Tax”is translated into
integer or floating-point additions according to the
types of variables Price and Tax.
An error is raised or additional type conversion codes
are prefixed when both data types are
different(incompatible or compatible).
Such implicit conversion is called coercion.
Strongly typed languages disallow coercion and treat
them as errors.
51
Figure 6.20 An object-oriented approach to the
translation process
52
Linking and Loading
One or more object programs need to be
connected together as an executable. It is
done by the linker to produce the executable
file (load module).
Object programs can be developed by another
teams, system library, or any third party.
53
Figure 6.21 The complete program preparation
process
54
Objects and Classes


Object = active program unit
containing both data and procedures
Class = a template for all objects of
the same type
An Object is often called an instance of
the class.
55
Components of an object


Instance variable = variable within
an object
Method = function or procedure within
an object

Can manipulate the object’s instance
variables
56
Figure 6.22 The structure of a class describing a laser
weapon in a computer game
57
Class & Object

Create an object of “type” and assign it to
Laser 1.
LaserClass Laser1 = new LaserClass();
--declare variable Laser1 to be of the type
LaserClass but also creates a new object
using the LaserClass template

Activate the approriate methods
Laser1.fire();
58
Constructor =
special method to initialize a new object instance
LaserClass Laser1(50), Laser2(100);
59
Encapsulation

Encapsulation = a way of restricting
access to the internal components of an
object


Private—only object itself can access it
Public—accessible from outside of objects
60
Figure 6.24 Our LaserClass definition using
encapsulation as it would appear in a Java or C#
program
61
Additional object-oriented concepts
Inheritance: allows new classes to be defined in
terms of previously defined classes
 Class RechargeableLaser extends LaserClass
{ void fire ()
}
RechargeableLaser Laser3, Laser4;


Polymorphism: allows method calls to be
interpreted by the object that receives the
call
Laser3.fire();
62
Programming concurrent
activities

Parallel or concurrent processing =
simultaneous execution of multiple processes




True concurrent processing requires multiple CPUs
Can be simulated using time-sharing with a single
CPU
To facilitate such demands, modern programming
languages provide syntax for expressing the
semantic structures.
In Ada, an activation is called a task, while in Java,
it is a thread.
63
Figure 6.25 Spawning processes
64
Interaction between processes



Mutual exclusion = a method for
ensuring that data can be accessed by
only one process at a time
Approach:From process side
Monitor = a data item augmented with
the ability to control access to itself
65
Declarative programming


provides a general problem-solving algorithm
around which declarative programming can
be constructed over the logic deduction.
Resolution = combining two or more statements to produce a
new, logically equivalent statement
 Example: (P OR Q) AND (R OR Q)
resolves to (P OR R)
 Resolvent = a new statement deduced by resolution
 Clause form = statement whose elementary components
are connected by the Boolean operation OR
E.X.: P OR Q
P->Q
 Q OR ~P
66
Figure 6.26 Resolving the statements (P OR Q)
and (R OR ¬Q) to produce (P OR R)
If the original statement is true, then the resolvent must also
Be true. If Q is true, then R must be true, but if Q is false then
P must be true. Regardless Q is true or false, either P or R must
67
Be true
Figure 6.27 Resolving the statements (P OR
Q), (R OR ¬Q), ¬R, and ¬P
68
Prolog(Programming in Logic)

Fact = predicateName(arguments).


Example: parent(bill, mary).
Rule = conclusion :- premise.


:- means “if”
Example: wise(X) :- old(X).
old(x)-> wise(x)


Example: faster(X,Z) :- faster(X,Y),

faster(Y,Z).
((faster(x,y) and faster(Y,z))-> faster(x,z)
All statements must be fact or rules.
69
Download