0% found this document useful (0 votes)
3 views

2.Programming Languages

The document covers the fundamentals of computer programming, including the differences between low-level and high-level programming languages, the roles of compilers and interpreters, and the stages of program execution. It details the characteristics, advantages, and disadvantages of machine language, assembly language, and various high-level languages. Additionally, it explains the processes involved in compilation, linking, and loading programs into memory.

Uploaded by

Anand Duraiswamy
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

2.Programming Languages

The document covers the fundamentals of computer programming, including the differences between low-level and high-level programming languages, the roles of compilers and interpreters, and the stages of program execution. It details the characteristics, advantages, and disadvantages of machine language, assembly language, and various high-level languages. Additionally, it explains the processes involved in compilation, linking, and loading programs into memory.

Uploaded by

Anand Duraiswamy
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 26

Fundamentals of Computer

Programming
Topics
 Introduction to Programming Languages
 Compliers/Interpreters
 Major differences between Machine Language and High Level
Language
 Major differences between Assembly Language and High
Level Language
 Execution of a Program
 Compilation Stages
 Working of Assembler
 Object file format
 Symbol table
 Linker
 Loader
Introduction to Programming Languages
Programming language is a language used for expressing a set of
computer instructions.
It consists of necessary symbols, characters and grammar rules which
allows programmers to communicate with computers.
Programming
Languages

Low Level High Level


Language (LLL) Language (HLL)

Machine Assembly General Specific


Language Language purpose HLL purpose HLL
Introduction to Programming Languages
Low Level Languages
Characteristics:
• Easily understood by computers
• m/c dependent
• Computer specific
• First generation computer language
e.g. m/c language, assembly language

Machine language (m/c Language)


Characteristics:
• Written in the form of 0’s and 1’s
• Instructions have a specific format consisting of two fields
Opcode Address

Opcode - operation to be done


Address – memory location from where data is referenced
Introduction to Programming Languages
Machine language

Advantages:
• Can be typed directly and executed
• No translation program is required

Disadvantages:
• Difficult to remember m/c instructions
• Difficult to understand, modify and debug errors
• Difficult to remember addresses of data and instructions
• Lack of portability
Introduction to Programming Languages
Assembly Language:
Characteristics:
• Uses symbolic instructions (mnemonics)
• Second generation programming language

Assembler
Source program Object program
(Assembly Language) (Machine Language)

Some mnemonics used are:


ADD - Addition
SUB - Subtraction
MUL - Multiplication
HALT – Stop
JUMP – Jump to the specified memory location
INR - Increment
Introduction to Programming Languages
Example of Assemblers:
TASM (Turbo Assembler), MASM (Macro Assembler)

Advantages:
• Easy to remember opcodes
• Easy to understand program
• Easy to write, modify and debug programs as compared to
machine language
• Need not remember addresses of operands and instruction
locations

Disadvantages:
• Not standardized
• Less efficient than machine language
• Mnemonics are machine dependent
Introduction to Programming Languages
High level language
Characteristics:
• Third generation language
• Machine independent
• Easily understood by programmer

Process of translating HLL to m/c language requires


translator

Translator
Source program (Interpreter/Compiler) Object program
(HLL) (Machine Language)
Introduction to Programming Languages
Examples of Specific purpose HLL are:

COBOL – for business data processing


FORTRAN – for scientific applications
C++ - for object oriented programming
Prolog – for Artificial Intelligence
Java – for Internet programming
C# - for .NET Applications

Examples of General purpose HLL:

BASIC, Pascal, C – for teaching/training, etc.


Introduction to Programming Languages
High level language

Advantages:
Easy to understand
Portable
Standardized
m/c independent
Easy to write, modify and debug

Disadvantages:
Less efficient than low level languages
Compiler and Interpreter
Major differences between compiler and interpreter
are tabulated below:

Compiler Interpreter
Takes entire HLL Takes one statement at a
program and translates time as input and
to m/c language translates to m/c language
All errors are listed and Errors are displayed only
displayed for the current statement
Debugging is faster Debugging is slower
Requires more memory Requires less memory
Costlier Cheaper
Major Differences between Machine
Language and High Level Language (HLL)
Machine Language HLL
Uses 0 and 1 Uses English alphabets
Instruction is sequence of 0’s Instruction is English like
and 1’s statement
m/c dependent m/c independent
Not standardized Standardized
Difficult to understand, write, Easy to understand, write,
debug and modify debug and modify
Efficient Less efficient
Code written in m/c language Code written in HLL cannot
can be directly executed be directly executed
Major Differences between Assembly
Language and High Level Language (HLL)

Assembly Language HLL


Mnemonics Uses English alphabets
m/c dependent m/c independent
One step process Two step process
Assembly language => m/c HLL => assembly language=>m/c
language language
Assembler is used in translation Interpreter/Compiler is used in
process translation process
Efficient Less efficient
Execution of a program
Source program (HLL)

Compiler

Assembly language program

Assembler

Object code: m/c language module Object code: Library routine (m/c language)

Linker

Executable: m/c language program

Loader

Memory (m/c language program loaded)


Compilation Stages
Sequence of Characters
Lexical Analysis

Sequence of Lexical Elements


Parsing
Syntax tree
Semantic Analysis
Annotated Syntax tree
Generation of Intermediate code
Sequence of IL
Optimization of Intermediate code
IL – Intermediate
Language Sequence of IL
e.g. three address Pseudo Code
code Assembly program
Three Address Code (TAC or 3AC)
# Calculate one solution to the quadratic equation.
x = (-b + sqrt (b^2 - 4*a*c)) / (2*a)

t1 := b * b In three-address code, this would be


t2 := 4*a broken down into several separate
t3 := t2 * c instructions.These instructions
t4 := t1 - t3 translate easier to assembly
t5 := sqrt (t4) language. It is also easier to detect
t6 := 0 - b common sub-expressions for
t7 := t5 + t6 shortening the code.
t8 := 2 * a
t9 := t7 / t8
x := t9
Compilation Stages
The lexical analysis stage
transforms a sequence of characters to a sequence of lexical
elements. These lexical entities correspond principally to
integers, floating point numbers, characters, strings of characters
and identifiers.

Note: The message Illegal character might be generated by this analysis.

The parsing stage


constructs a syntax tree and verifies that the sequence of lexical
elements is correct with respect to the grammar of the language.

Note: The message Syntax error indicates that the phrase analyzed does not
follow the grammar of the language.
Compilation Stages
The semantic analysis stage
traverses the syntax tree, checking another aspect of program
correctness. The analysis consists principally of type inference,
which if successful, produces the most general type of an
expression or declaration.

Note: Type error messages may occur during this phase. This stage also
detects whether any members of a sequence are not of type unit. Other
warnings may result, including pattern matching analysis (e.g. pattern matching is
not exhaustive, part of pattern matching will not be used).

Generation and the optimization of intermediate code


These stages does not produce errors or warning messages.

The final step in the compilation process is the generation of a


program binary. Details differ from compiler to compiler.
Compilation Stages
Sum = 4 + 5;
Assignment Statement
=

Identifier Expression
Sum +

Identifier (num 1) Identifier (num 2)

Semantic Analysis compiler will check:


1. Data type of First operand
2. Data type of Second operand
3. Check whether ‘+’ is binary or unary
4. Check for number of operands supplied to operator depending on the type
of operator (unary, binary, ternary)
Working of Assembler

Typically, an assembler makes two passes over the


assembly language file.

First pass: reads each line and record labels in a symbol


table.
Second pass: uses information in symbol table to
produce actual m/c code for each line
Working of Assembler
Assembler:
Performs the following operations:
• Translates assembly language instructions and pseudo
instructions into m/c language
• Converts decimal numbers etc. specified by
programmer into binary.

Object file consists of:


• m/c instructions
• data
• information needed to place instructions properly in
memory
Object File Format
Object File Text Data Relocation Symbol Debugging
Header Segment Segment Information Table Information
Object File Header :
describes the size and position of the other pieces of object file

Text Segment :
contains m/c instructions

Data Segment :
contains binary representation of data in assembly file

Relocation Information:
identifies instructions and data that depend on absolute addresses

Symbol Table:
Associates addresses with external labels and lists unresolved references

Debugging Information:
to debug the errors
Symbol Table
In a compiler:
a data structure used by the compiler to keep track of identifiers
used in the source program. This is a compile-time data structure.
Not used at run time.

In object files:
a symbol table (mapping var name to address) can be build into
object programs, to be used during linking of different object
programs to resolve reference.

In executables:
a symbol table (again mapping name to address) can be included in
executables, to recover variable names during debugging.
Execution of a program
Source File 1 Source File 2 … Source File n

Assembler Assembler Assembler

Object File 1 Object File 2 … Object File n

Linker

Executable Program
File Library

Loader

Memory
Linker
Tools that merges the object files produced by separate
compilation or assembly and creates an executable file

Three tasks of a linker:


• Searches the program to find library routines used by
program.
e.g. printf, math routines etc.

• Determine the memory locations that code from each


module will occupy and relocates its instructions by adjusting
absolute references

• Resolves references among files


Loader
Part of Operating System that brings an executable file residing
on disk into memory and starts its running.

Steps:
• Read executable file’s header to determine the size of text
and data segments
• Creates a new address space for the program
• Copies instructions and data into address space
• Copies arguments passed to the program on the stack
• Initializes the m/c registers including stack pointer
• Jumps to a startup routine that copies the program’s
arguments from the stack to registers and calls the
program’s main routine

You might also like