0% found this document useful (0 votes)
35 views57 pages

A Programmingfundamental

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)
35 views57 pages

A Programmingfundamental

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/ 57

Creating Computer

Programs

Dr. Md. Fazlul Kader


Professor
Dept. of EEE, University of Chittagong

McGraw-Hill Technology Education Copyright © 2006 by The McGraw-Hill Companies, Inc. All rights reserved.
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
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
Operating System (OS)
 Provides several essential services:
– Loading & running application programs
– Allocating memory & processor time
– Providing input & output facilities
– Managing files of information
What Is a Computer Program?
• Computer programs
– Also called software
– Are a list of instructions
– Instructions are called code
– CPU performs the instructions
– Three types
• Operating system
• Utility
• Application

13A-5
Software Is Stored In Many Files
• Executable files
– Contain the instructions for the CPU
– Have extensions of .exe, or .com

13A-6
Software Is Stored In Many Files
• Dynamic link libraries
– Partial executable file
– Used to support executable files
– Have .dll extensions

13A-7
Software Is Stored In Many Files
• Initialization files
– Contain configuration settings for software
– Have a .ini extension
– Modern programs use the registry

13A-8
Software Is Stored In Many Files
• Help files
– Contain information about the software
– Information is indexed and searchable
– Provides an online manual
– Have a .chm or .hlp extension

13A-9
Software Is Stored In Many Files
• Batch files
– Used to automate tasks
– Hold a series of OS commands
– Have a .bat extension

13A-10
Hardware/Software Interaction
• Program execution
– Software executes at the CPU level
– Code to play a sound
• Code generates an interrupt
• CPU tells the sound card to play
• Sound card plays the file
– Programmer creates the code

13A-11
Hardware/Software Interaction
• Code
– Statements written in a programming
language
– Writing code can be tedious
• Code must be perfect
• Order of steps must be exact
– Writing code is quite exciting
• Problems are solved
• New ideas are formed

13A-12
Writing Code

13A-13
Hardware/Software Interaction
• Machine code
– Recall that computers think in binary
– Code is translated into machine code
• CPU executes the machine code
– CPUs have a unique machine code

13A-14
Hardware/Software Interaction
• Programming languages
– Simplifies the writing of code
• English is used to describe the binary
– Original code is called source code
– Several hundred languages exist

13A-15
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
Add Tax
Store Cost
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
Syntax & Semantics
• Syntax:
– The structure of strings in some language. A
language's syntax is described by a grammar.
• Semantics:
– The meaning of the language
Hardware/Software Interaction
• Compilers and interpreters
– Converts source code into binary
• Allows code to execute
– Checks source code for correctness

13A-21
Hardware/Software Interaction
• Compiler
– Creates an executable file
• Contents are called object code
– Executable can run on its own
– Each language has its own compiler
– C++ and Java are compiled languages

13A-22
Hardware/Software Interaction
• Interpreter
– Runs program one line at a time
– More flexible than compilers
– Slower than compilers
– Always needed to execute program
– Visual Basic and Perl are interpreted

13A-23
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 Machine
Assembler
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’
What is a Linker?
• A program that pulls other programs together
so that they can run.
• Most programs are very large and consist of
several modules.
• Even small programs use existing code
provided by the programming environment
called libraries.
• The linker pulls everything together, makes
sure that references to other parts of the
program (code) are resolved.
Running Programs
• Steps that the computer goes through to run a
program:
Memory

Machine language
program
(executable file)
Input Data Data entered CPU
during execution

Computed results
Program Output
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
Program Errors
• There are 5 types of error in C:
– Syntax Errors
– Runtime Errors
– Logical Errors
– Linked Errors
– Semantic Errors
Program Errors
• Syntax Errors
• These are also referred to as compile-time errors.
• These errors have occurred when the rule of C writing
techniques or syntaxes has been broken.
• These types of errors are typically flagged by the
compiler prior to compilation.
Program Errors
• Syntax Errors

Program Errors
• Runtime Errors
• This type of error occurs while the program is running.
• Because this is not a compilation error, the compilation
will be completed successfully.
• These errors occur due to segmentation fault when a
number is divided by division operator or modulo division
operator.
Program Errors
• Runtime Errors
Program Errors
• Runtime Errors
Program Errors
• Logical Errors
• Even if the syntax and other factors are correct,
we may not get the desired results due to logical
issues.
• These are referred to as logical errors.
• We sometimes put a semicolon after a loop,
which is syntactically correct but results in one
blank loop.
Program Errors
• Logical Errors
Program Errors
• Logical Errors
Program Errors
• Linker Errors
• When the program is successfully compiled and
attempting to link the different object files with the main
object file, errors will occur.
• When this error occurs, the executable is not generated.
• This could be due to incorrect function prototyping, an
incorrect header file, or other factors.
• If main() is written as Main(), a linked error will be
generated.
Program Errors
• Linker Errors
Program Errors
• Semantic Errors
• When a sentence is syntactically correct but has
no meaning, semantic errors occur.
• This is similar to grammatical errors.
• If an expression is entered on the left side of the
assignment operator, a semantic error may
occur.
Program Errors
• Semantic Errors
Planning a Computer Program
• Plans
– The steps to solve a problem
– Describe the expected results
– Programming without a plan is difficult

13A-45
Planning Tools
• Pseudo code
– Natural language statements that
resemble code
– Describes what must be done
– Can be written by non programmers
– Programmers develop unique versions

13A-46
Planning Tools
• Input-processing-output (IPO) charts
– Determines what is needed
– Input column
• Data inputted by the user
– Processing column
• Pseudo code describing the problem solution
– Output column
• Desired output from the program

13A-47
IPO Chart

13A-48
How Programs Solve Problems
• Program control flow
– Order program statements are executed
– Typically executed in order
– Constructs can change the flow
• Decision statements
• Loops

13A-49
How Programs Solve Problems
• Algorithm
– Set of steps
– Always leads to a solution
– Steps are always the same
– Flowcharts can describe algorithms
• Structured tool for drawing algorithms
– Algorithms appear in all programs

13A-50
Planning Tools
• Pseudo code

13A-51
Flowchart

13A-52
Flowchart

13A-53
Structured Programming
• Programming using defined structures
• Creates easy to read code
• Programs are efficient and run fast
• Several defined structures

13A-54
Structured Programming
• Sequence structure
– Describes the flow of the program
– Typically executed in order
– Branching statements allow multiple flows

13A-55
Structured Programming
• Selection statement
– Also called conditional statement
– Performs a true or false test
– Determines which code to execute next

13A-56
Structured Programming
• Repetition statements
– Also called looping structures
– Repeats a section of code
• Until an exit condition is reached

13A-57

You might also like