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

unit 1.1.ppt

Uploaded by

Kathan Patel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

unit 1.1.ppt

Uploaded by

Kathan Patel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 35

Introduction to Computer

Programming
Software Categories

• System SW
– Programs written for computer systems
• Compilers, operating systems, …

• Application SW
– Programs written for computer users
• Word-processors, spreadsheets, & other
application packages
A Layered View of the Computer
Application Programs
Word-Processors, Spreadsheets,
Database Software, IDEs,
etc…
System Software
Compilers, Interpreters,Preprocessors,
etc.
Operating System, Device Drivers
Machine with all its hardware
Operating System (OS)
∙ Provides several essential services:
– Loading & running application programs
– Allocating memory & processor time
– Providing input & output facilities
– Managing files of information
Programs
• Programs are written in programming languages
– PL = programming language
– Pieces of the same program can be written in different
PLs
• Languages closer to the machine can be more efficient
• As long as they agree on how to communicate
• A PL is
– A special purpose and limited language
– A set of rules and symbols used to construct a
computer program
– A language used to interact with the computer
Computer Languages
– Machine Language
• Uses binary code
• Machine-dependent
• Not portable
• Assembly Language
– Uses mnemonics
– Machine-dependent
– Not usually portable
• High-Level Language (HLL)
– Uses English-like language
– Machine independent
– Portable (but must be compiled for different platforms)
– Examples: Pascal, C, C++, Java, Fortran, . . .
Machine Language
• The representation of a computer program which is
actually read and understood by the computer.
– A program in machine code consists of a sequence of machine
instructions.
• Instructions:
– Machine instructions are in binary code
– Instructions specify operations and memory cells involved in the
operation
Example: Operation Address

0010 0000 0000 0100


0100 0000 0000 0101

0011 0000 0000 0110


Assembly Language
• A symbolic representation of the machine language of a
specific processor.
• Is converted to machine code by an assembler.
• Usually, each line of assembly code produces one
machine instruction (One-to-one correspondence).
• Programming in assembly language is slow and
error-prone but is more efficient in terms of hardware
performance.
• Mnemonic representation of the instructions and data
• Example:
Load Price
AddTax
Store Cost
Low-level language
• It is a machine-friendly language, i.e., the
computer understands the machine language,
which is represented in 0 or 1.
• The low-level language takes more time to
execute.
• It requires the assembler to convert the
assembly code into machine code.
• The machine code cannot run on all machines,
so it is not a portable language.
• It is memory efficient.
• Debugging and maintenance are not easier in a
low-level language.
High-level language
• A programming language which use statements
consisting of English-like keywords such as "FOR",
"PRINT" or “IF“, ... etc.
• Each statement corresponds to several machine
language instructions (one-to-many correspondence).
• Much easier to program than in assembly language.
• Data are referenced using descriptive names
• Operations can be described using familiar symbols
• Example:
Cost := Price + Tax
High-level language
• It is a user-friendly language as this language is
written in simple English words, which can be
easily understood by humans.
• It executes at a faster.
• It requires the compiler to convert the high-level
language instructions into machine code.
• The high-level code can run all the platforms, so
it is a portable language.
• Debugging and maintenance are easier in a
high-level language.
Compilers & Programs
• Compiler
– A program that converts another program from
some source language (or high-level
programming language / HLL) to machine
language (object code).
– Some compilers output assembly language
which is then converted to machine language by
a separate assembler.
– Is distinguished from an assembler by the fact
that each input statement, in general,
correspond to more than one machine
instruction.
Compilation into Assembly L

Source Assembly
Program Compiler Language

Assembly Assembler Machine


Language Language
Compilers & Programs
• Source program
– The form in which a computer program,
written in some formal programming
language, is written by the programmer.
– Can be compiled automatically into object
code or machine code or executed by an
interpreter.
– Pascal source programs have extension
‘.pas’
Compilers & Programs
• Object program
– Output from the compiler
– Equivalent machine language translation of the
source program
– Files usually have extension ‘.obj’

• Executable program
– Output from linker/loader
– Machine language program linked with necessary
libraries & other files
– Files usually have extension ‘.exe’
Program Execution
• Steps taken by the CPU to run a program
(instructions are in machine language):
1. Fetch an instruction
2. Decode (interpret) the instruction
3. Retrieve data, if needed
4. Execute (perform) actual processing
5. Store the results, if needed
Program Errors
• Syntax Errors:
– Errors in grammar of the language
• Runtime error:
– When there are no syntax errors, but the program
can’t complete execution
• Divide by zero
• Invalid input data
• Logical errors:
– The program completes execution, but delivers
incorrect results
– Incorrect usage of parentheses
Compilation
Source Target
Program Compiler Program

Input Target Program Output

• Compiler translates source into target (a machine


language program)
• Compiler goes away at execution time
• Compiler is itself a machine language program,
presumably created by compiling some other high-level
program
• Machine language, when written in a format understood
by the OS is object code
Interpretation

Source
Program

Interpreter Output

Input

• The interpreter stays around during execution


• It reads and executes statements one at a time
Compilation vs. Interpretation
• Compilation:
– Syntax errors caught before running the program
– Better performance
– Decisions made once, at compile time
• Interpretation:
– Better diagnostics (error messages)
– More flexibility
– Supports late binding (delaying decisions about
program implementation until runtime)
• Can better cope with PLs where type and size of
variables depend on input
– Supports creation/modification of program code on
the fly (e.g. Lisp, Prolog)
Mixture of C & I
Source Intermediate
Program Translator Program

Intermediate
Program
VM Output
Input

• Many programming languages implement this

• Interpreter implements a Virtual Machine (VM).


JAVA
For portability:

Java Compiler

bytecode
ML Interpreter

For flexibility: Just In Time (JIT) compiler translates


bytecode into ML just before execution
Features of C Language
• 1) Simple
• C is a simple language in the sense that it
provides a structured approach (to break the
problem into parts), the rich set of library
functions, data types, etc.
• 2) Machine Independent or Portable
• Unlike assembly language, c programs can be
executed on different machines with some
machine specific changes. Therefore, C is a
machine independent language.

Features of C Language
• 3) Mid-level programming language
• Although, C is intended to do low-level programming.
It is used to develop system applications such as kernel,
driver, etc. It also supports the features of a high-level
language. That is why it is known as mid-level language.
• 4) Structured programming language
• C is a structured programming language in the sense
that we can break the program into parts using
functions. So, it is easy to understand and modify.
Functions also provide code reusability.
Features of C Language
• 5) Rich Library
• C provides a lot of inbuilt functions that make
the development fast.
• 6) Memory Management
• It supports the feature of dynamic memory
allocation. In C language, we can free the
allocated memory at any time by calling
the free() function.
Features of C Language
• 7) Speed
• The compilation and execution time of C
language is fast since there are lesser inbuilt
functions and hence the lesser overhead.
• 8) Pointer
• C provides the feature of pointers. We can
directly interact with the memory by using the
pointers. We can use pointers for memory,
structures, functions, array, etc.
Features of C Language
• 9) Recursion
• In C, we can call the function within the
function. It provides code reusability for every
function. Recursion enables us to use the
approach of backtracking.
• 10) Extensible
• C language is extensible because it can easily
adopt new features.
C as a procedural language
• A procedure is known as a function, method,
routine, subroutine, etc. A procedural
language specifies a series of steps for the
program to solve the problem.
• A procedural language breaks the program into
functions, data structures, etc.
• C is a procedural language. In C, variables and
function prototypes must be declared before
being used.
Linker and Loader
Linker
• The main function of the linker is to generate executable
files.
• The linker takes the input as the object code which would
be generated by a compiler/assembler.
• The process of linking can be understood as a method to
combine different snippets of code in order to obtain
executable code.
• There are two types of linkers available: Linkage Editor
and Dynamic Linker.
• Linker also helps combine all the object modules.
• Linker is responsible to arrange the objects in the
program’s address space.
Linker
• A linker is special program that combines the object files,
generated by compiler/assembler and other pieces of
code to originate an executable file has .exe extension.
In the object file, linker searches and append all libraries
needed for execution of file. It regulates the memory
space that will hold the code from each module. It also
merges two or more separate object programs and
establishes link among them.
Linker
• The linker performs several tasks, including:
• Symbol resolution: The linker resolves symbols in the
program that are defined in one module and referenced
in another.
• Code optimization: The linker optimizes the code
generated by the compiler to reduce code size and
improve program performance.
• Memory management: The linker assigns memory
addresses to the code and data sections of the program
and resolves any conflicts that arise.
• Library management: The linker can link external
libraries into the executable file to provide additional
functionality.
Loader
• The main function of a loader is to load executable files
to the main memory.
• It takes the executable files (generated by linker) as its
input.
• It can be understood as a process of loading the
executable codes into main memory where it is execute
further.
• There are 3 types of loaders: Absolute loading, Relocate
loading and Dynamic run-time loading.
• It helps allocate the addresses to executable codes or
files.
• It is also responsible to adjust the references that are
used within the program.
Loader
• It is special program that takes input of executable files
from linker, loads it to main memory, and prepares this
code for execution by computer. Loader allocates
memory space to program. Even it settles down symbolic
reference between objects. It is in charge of loading
programs and libraries in operating system. The
embedded computer systems don’t have loaders. In
them, code is executed through ROM. There are
following various loading schemes:
Loader
• The loader performs several tasks, including:
• Loading: The loader loads the executable file into
memory and allocates memory for the program.
• Relocation: The loader adjusts the program’s memory
addresses to reflect its location in memory.
• Symbol resolution: The loader resolves any unresolved
external symbols that are required by the program.
• Dynamic linking: The loader can dynamically link libraries
into the program at runtime to provide additional
functionality.

You might also like