Professional Documents
Culture Documents
It111 Lecture02
It111 Lecture02
It111 Lecture02
LECTURE 02
1
Center for Information and Communication Technology
Programming Languages
Computer programs, known as software, are instructions
that tell a computer what to do.
Computers do not understand human languages, so,
programs must be written in languages computers can
understand.
There are hundreds of programming languages, developed
to make the programming process easier for people.
All programs must be converted into instructions that a
computer can execute.
2
Center for Information and Communication Technology
4
Center for Information and Communication Technology
Machine Language
A computer’s native language, which differs among
different types of computers, is its machine language - a
set of built-in primitive instructions.
These instructions are in the form of binary code, so if
you want to give a computer an instruction in its native
language, you have to enter the instruction as binary code.
For example, to add two numbers, you might have to
write an instruction in binary code, as follows:
1101101010011010
5
Center for Information and Communication Technology
Machine Language
To see how instructions are written in machine language,
Suppose you want to use the equation
wages = rate x hours to calculate weekly wages.
Suppose that the binary code 100100 stands for load, 100110
stands for multiplication, and 100010 stands for store.
In machine language, you might need the following sequence of
instructions to calculate weekly wages
100100 010001
100110 010010
100010 010011
6
Center for Information and Communication Technology
Machine Language
To represent the weekly wages equation in machine
language, the programmer had to remember the machine
language codes for various operations.
To manipulate data, the programmer had to remember
the locations of the data in the main memory.
This need to remember specific codes made
programming not only very difficult, but also error prone.
Assembly languages were developed to make the
programmer’s job easier.
7
Center for Information and Communication Technology
Assembly Language
Programming in machine language is a tedious process.
Programs written in machine language are very difficult to read
and modify.
For this reason, assembly language was created in the early days
of computing as an alternative to machine languages.
Assembly language uses a short descriptive word, known as a
mnemonic, to represent each of the machine-language
instructions.
For example, the mnemonic add typically means to add numbers
and sub means to subtract numbers.
8
Center for Information and Communication Technology
Assembly Language
A mnemonic is a term, symbol or name used to define or
specify a computing function.
Mnemonics are used in computing to provide users with
a means to quickly access a function, service or process,
bypassing the actual more lengthy method used to
perform or achieve it.
9
Center for Information and Communication Technology
Assembly Language
To add the numbers 2 and 3 and get the result, you might
write an instruction in assembly language like this:
ADD 2, 3, RESULT
Assembly languages were developed to make
programming easier.
Computer cannot execute assembly language, another
program called an assembler - is used to translate
assembly-language programs into machine code
10
Center for Information and Communication Technology
Assembly Language
Assembly Language
Using assembly language instructions, you can write the equation to
calculate the weekly wages in previous example (slide 6) as follows:
LOAD rate
MULT hours
STOR wages
As you can see, it is much easier to write instructions in assembly
language.
However, a computer cannot execute assembly language
instructions directly. The instructions first have to be translated
into machine language
12
Center for Information and Communication Technology
Assembly Language
Writing code in assembly language is easier than in
machine language.
However, it is still tedious to write code in assembly
language.
Writing in assembly requires that you know how the CPU
works.
Assembly language is referred to as a low-level
language, because assembly language is close in nature to
machine language and is machine dependent.
13
Center for Information and Communication Technology
High-Level Language
In the 1950s, a new generation of programming languages known as high-level
languages emerged.
They are platform-independent, which means that you can write a program in
a high level language and run it in different types of machines.
High-level languages are English-like and easy to learn and use.
The instructions in a high-level programming language are called statements.
High-level language statement that computes the area of a circle with a radius
of 5:-
area = 5 * 5 * 3.1415
There are many high-level programming languages, and each was designed for
a specific purpose.
14
Center for Information and Communication Technology
Language Description
Ada The Ada language was developed for the Department of Defense and is
used mainly in defense projects.
BASIC Beginner’s All-purpose Symbolic Instruction Code. It was designed to be
learned and used easily by beginners.
C Developed at Bell Laboratories. C combines the power of an assembly
language with the ease of use and portability of a high-level language.
C++ C++ is an object-oriented language, based on C.
C# Pronounced “C Sharp.” It is a hybrid of Java and C++ and was developed by
Microsoft.
Python A simple general-purpose scripting language good for writing short
programs.
15
Center for Information and Communication Technology
Language Description
COBOL COmmon Business Oriented Language. It is used for business
applications.
FORTRAN FORmula TRANslation. It is popular for scientific and mathematical
applications.
Java Developed by Sun Microsystems, now part of Oracle. It is widely used
for developing platform-independent Internet applications.
Pascal Named for Blaise Pascal, who pioneered calculating machines in the
seventeenth century. It is a simple, structured, general-purpose
language primarily used for teaching programming.
Visual Basic Visual Basic was developed by Microsoft and it enables the
programmers to rapidly develop graphical user
16
Center for Information and Communication Technology
High-Level Language
A computer cannot execute a source program, a source
program must be translated into machine code for execution.
The translation can be done using another programming tool
called an interpreter or compiler
An interpreter reads one statement from the source code,
translates it to the machine code or virtual machine code, and
then executes it immediately.
17
Center for Information and Communication Technology
High-Level Language
Compiler: A program that translates instructions written
in a high-level language into the equivalent machine
language.
A compiler translates the entire source code into a
machine-code file, and the machine code file is then
executed
18
Center for Information and Communication Technology
High-Level Language
In C++, you write the weekly wages equation as follows:
wages = rate * hours;
The instruction written in C++ is much easier to understand and is
self-explanatory to a novice user who is familiar with basic
arithmetic.
As in the case of assembly language, the computer cannot directly
execute instructions written in a high-level language.
To execute on a computer, this C++ instruction first need to be
translated into machine language.
A compiler translates instructions written in high-level languages
into machine code. 19
Center for Information and Communication Technology
21
Center for Information and Communication Technology
32
Center for Information and Communication Technology
33
Center for Information and Communication Technology
Language Implementation
Programming languages can be implemented in three
ways;
A language can be interpreted.
A language can be compiled to a machine language
A language can be implemented by some combination
of the first two method
Machine language is the language of the Central
Processing Unit (CPU) and is very simple.
36
Center for Information and Communication Technology
Language Implementation
For instance, typical instructions are fetch this value into
the CPU, store this value into memory from the CPU, add
these two values together, and compare these two
values and if they are equal, jump here next.
The goal of any programming language implementation
is to translate a source program into simpler machine
language so it can be executed by the CPU.
37
Center for Information and Communication Technology
Language Implementation
All language implementations translate
a source program to some intermediate
representation before translating the
intermediate representation to machine
language.
Exactly how these two translations are
packaged varies significantly from one
programming language to the next, but
luckily most language implementations
follow one of a few methodologies.
38
Center for Information and Communication Technology
Compilation
The most direct method of translating a program to machine
language is called compilation.
Internally, a compiler is composed of several parts. The parser
reads a source program and translates it into an intermediate
form called an abstract syntax tree (AST).
An AST is a tree like data structure that internally represents the
source program.
The code generator then traverses the AST and produces another
intermediate form called an assembly language program.
This program is not machine language, but it is much closer.
39
Center for Information and Communication Technology
Compilation
Finally, an assembler and linker
translate an assembly language program
to machine language making the
program ready to execute.
This whole process is encapsulated by a
tool called a compiler.
In most instances, the assembler and
linker are separate from the compiler
Normally the compiler runs the
assembler and linker automatically when
a program is compiled.
40
Center for Information and Communication Technology
Compilation
Programming in a compiled language is a three-step process
First, you write a source program
Then you compile the source program, producing an
executable program
Then you run the executable program
The source program and the machine language program are
not in sync.
After making changes to the source program you must
remember to recompile before running the executable
program again
41
Center for Information and Communication Technology
Compilation
Machine language is specific to a CPU architecture and
operating system.
Compiling a source program on Linux means it will run on
most Linux machines with a similar CPU.
However, you cannot take a Linux executable and put it on
a Microsoft Windows machine and expect it to run, even if
the two computers have the same CPU.
The Linux and Windows operating systems each have their
own format for executable machine language programs.
42
Center for Information and Communication Technology
Interpretation
Interpreter is the machine language program that
executes all the programs written in the interpreted
language.
An interpreter is written in some other language and
compiled into machine language.
The interpreter itself is the machine language program.
The interpreter itself is written to read source programs
from the interpreted language and interpret them.
For instance, Python is an interpreted language. The Python
interpreter is written in C and is compiled for a particular
platform like Linux, Mac OS X, or Microsoft Windows.
43
Center for Information and Communication Technology
Interpretation
To run a Python program, you must download and install the
Python interpreter that goes with your operating system and
CPU.
When you run an interpreted source program, you are actually
running the interpreter. representation before executing it.
Programming in an interpreted language is a two step process.
First you write a source program.
Then you execute the source program by running the
interpreter.
Each time your program is executed it is translated into an AST
by a part of the interpreter called the parser.
44
Center for Information and Communication Technology
Interpretation
There may be an additional step that translates the AST
to some lower-level representation, often called
bytecode.
In an interpreter, this lower-level representation is still
internal to the interpreter program.
Then a part of the interpreter, often called a virtual
machine, executes the byte code instructions. Not every
interpreter translates the AST to bytecode.
Sometimes the interpreter directly interprets the AST
but it is often convenient to translate the source
program’s AST to some simpler representation before
executing it.
Eliminating the compile step has a few implications.
45
Center for Information and Communication Technology
Virtual Machines
A virtual machine is a program that provides insulation from the
actual hardware and operating system of a machine while supplying
a consistent implementation of a set of low-level instructions, often
called bytecode.
Figure on next slide shows how a virtual machine sits on top of the
operating system/CPU to act as this insulator
There is no one specification for bytecode instructions.
They are specific to the virtual machine being defined.
Python has a virtual machine buried within the interpreter.
Prolog is another interpreter that uses a virtual machine as part of
its implementation
46
Center for Information and Communication Technology
Virtual Machines
Java has a virtual machine that executes
bytecode instructions as does Python.
The creators of Java separated the virtual
machine from the compiler.
Instead of storing the bytecode instructions
internally as in an interpreter, the Java
compiler, called javac, compiles a Java source
code program to a bytecode file.
This file is not machine language so it cannot
be executed directly on the hardware.
It is a Java bytecode file which is interpreted by
the Java virtual machine
47
Center for Information and Communication Technology
Virtual Machines
Languages that fall into this virtual machine category include
Java
ML
Python
C#
Visual Basic
.NET
JScript
other .NET platform languages
Both ML and Python include interactive interpreters as well as the
ability to compile and run low-level bytecode programs
48