0% found this document useful (0 votes)
5 views52 pages

CSC 409 Unit 6

The document outlines the course CSC 409, focusing on structured programming, its paradigms, and the differences between machine, assembly, and high-level languages. It details the roles of compilers and interpreters in translating programming languages, along with the advantages and limitations of each language type. The course aims to equip students with the skills to design, implement, and understand structured programming methodologies, as well as to write and debug programs in various languages.

Uploaded by

dfwsarahhhh
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)
5 views52 pages

CSC 409 Unit 6

The document outlines the course CSC 409, focusing on structured programming, its paradigms, and the differences between machine, assembly, and high-level languages. It details the roles of compilers and interpreters in translating programming languages, along with the advantages and limitations of each language type. The course aims to equip students with the skills to design, implement, and understand structured programming methodologies, as well as to write and debug programs in various languages.

Uploaded by

dfwsarahhhh
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/ 52

Course code: CSC 409

Credit Units: 3
Lecturers/Course coordinator Dr. E. O. Oyekanmi
Office: Computer Science Department
Phone: 08036427123
Email: e.oyekanmi@achievers.edu.ng
Class Hours: (Monday, from 8.00 am to 10.00 am)
Office Hours: M, T, W, Th (from 2.00 pm to 4.00 pm)
Online Hours: M, T, W, Th, F, Sa (from 6.00 pm to 8.00 pm)
There are several programming languages before now, however not all of them are
structured. The purpose of the structured ones are:
 improve programme dependability and programmers' understanding.
Three hows of programming:
 how to develop the solution to a given problem,
 how to organise a programme, and
 how to effectively use the standard techniques that represent the "tricks" of the
programming trade.
Three basic building blocks of structured programming
 Sequential, conditional, and repeating structured codes which are the are discussed in
details.
 These three components replace the need for GO TO commands to control the flow of
a programme.
Overall, this course will ensure that a programmer has greater confidence on a developed software code in
terms of simplicity to grasp and reduction of logical errors, because the programme is built of highly
independent modules.
At the end of this course, students should be able to:
 explain the rationale behind structured programming paradigms.
 differentiate among three main approaches for writing a computer program
 compare and contrast between structured programming and procedural
coding.
 Interpret programming language with built-in structured programming styles
 implement programming methodology.
 Design language construct using C++.
 describe discipline of programming.
 demonstrate the use of structure programming in database.
At the end of this unit, students should be able to:
 describe machine language, its prons and cons
 understand the architecture of the processor
 know some commands available in assembly language
 write program with assembly language
At the end of this unit, students should be able to:
 describe machine language, its prons and cons
 understand the architecture of the processor
 know some commands available in assembly language
 write program with assembly language
– 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, . . .
 Python, Java, C, and C++ are, indeed, examples of high-level languages

 Strictly speaking, computer hardware can only understand a very low-level language
known as machine language

 If you want a computer to add two numbers, the instructions that the CPU will carry out
might be something like this:

Load the number from memory location 2001 into the CPU
Load the number from memory location 2002 into the CPU A Lot of
Add the two numbers in the CPU Work!
Store the result into location 2003
Machine Language
 Fundamental language understood by computer.
 Also known as Machine Code.
 Written as strings of binary 0’s and 1’s.
 No translation program is required.
 Not really easy to learn.
 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
Instruction Format
Opcode Operand
(Operation Code) (Address)

 Instruction prepared in machine language has 2 parts


 Operation Code: Specifies operation to be performed.
 Operand: specifies address of operand.
Advantages of Machine Language
 Programs written in machine language are very fast to execute as instructions
written in Machine language are directly understood by CPU and no translation
program is required.
Limitations of Machine Language
 Machine dependent.
 Difficult to program
 Error prone.
Assembly Language
 To simplify the program writing process used in machine language, the numeric
operation codes of Machine language were substituted by letter symbols(Mnemonics).
 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
Mnemonics
 It is any kind of mental trick we use to help us remember.
 For example: machine code of 111 can be interpreted as subtract but it is still easier
for us to remember it as SUB.
Assembly Language

 The language which substitutes letters and symbols for the numbers in the machine
language program is called an ASSEMBLY LANGUAGE or SYMBOLIC LANGUAGE.
Addition of two
numbers
 Machine Language  Assembly Language
0001100100 CLA A
………… ADD B
………… STA C
………… TYP C
………….. HLT
Assembler
 The translator program that translates an assembly code into
machine code is called an Assembler.

Assembly Machine Language


Language Program Assembler Program
(Source Program) (Object Program)
Advantages of Assembly Language
over Machine Language

 Easier to understand and use.


 Easy to locate and correct errors.
 Easier to modify.
 No worry about addresses.
Limitations of Assembly
Language
 Machine dependent.
 Knowledge of hardware required.

Machine and Assembly Languages being machine dependent are called as Low
Level Languages.
Compiler
 The translator program that translates the instructions of HLL into
Machine Language is called Compiler.

High Level Machine Language


Language Program Compiler Program
(Source Program) (Object Program)
 A compiler is a complex software that takes a program written in a high-level language
and translates it into an equivalent program in the machine language of some
computer

Source
Machine
Code Compiler
Code
(Program)

Running
Inputs Outputs
Program
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
 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.
Source Assembly
Program Compiler Language

Assembly Machine
Assembler
Language Language
 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’
 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’
Interpreter
 An Interpreter is a type of translator used for translating HLL into
Machine Code.
 It takes one statement of HLL and translates it into a Machine
instruction which is immediately executed.

High Level Machine Language


Language Program Interpreter Program
(Source Program) (Object Program)
 An interpreter is a software that analyzes and executes the source code instruction-by-
instruction (on-the-fly) as necessary

Source
Code
(Program) Computer
Running An Outputs
Interpreter
Inputs
 E.g., Python is an interpreted language
Source
Program

Interpreter Output

Input

• The interpreter stays around during execution


• It reads and executes statements one at a time
Difference between
Interpreter and a Compiler
 In case of Compiler, whole source program is translated into equivalent machine
language program. The object code thus obtained is permanently saved for future
use. So, repeated compilation is not necessary whereas in Interpreter no object code
is saved because translation and execution process alternate.
 Advantage of an Interpreter over Compiler is that it responses fast to changes in
source program.
 Interpreters are easy to write and do not require large memory space.
 Disadvantage of interpreter over compiler is that interpreter is a time consuming
translation method because each statement must be translated every time it is
executed from source program.
 Compiling is a static (i.e., pre-execution), one-shot translation
 Once a program is compiled, it may be run over and over again without further need for the
compiler or the source code

 Interpreting is dynamic (i.e., happens during execution)


 The interpreter and the source code are needed every time the program runs

 Compiled programs tend to be faster, while interpreted ones lend themselves to a more
flexible programming environments (they can be developed and run interactively)
 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)
 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
Source Intermediate
Program Translator Program

Intermediate
Program
VM Output

Input

• Many programming languages implement this

• Interpreter implements a Virtual Machine (VM).


For portability:

Java Compiler

bytecode

ML Interpreter

For flexibility: Just In Time (JIT) compiler translates


bytecode into ML just before execution
 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.
• 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
 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
 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
 It is important to adopt a clear and consistent style in assembly
language programming.
 This is particularly important when one is working as part of a team,
since individuals must be able to read and understand each other's
programs.
 The assembly language solutions to problems up to this point have
been deliberately sketchy.
 For larger programming tasks, however, a more critical approach is
required.
 The following tips are offered to help improve assembly language
programming style.
41/4
9
 Use labels that are descriptive of the destination they represent.
 For example, when branching back to repeatedly perform an
operation, use a label such as "LOOP," "BACK," "MORE," etc.
 When skipping over a few instructions in the program, use a label
such as "SKIP" or "AHEAD."
 When repeatedly checking a status bit, use a label such as "WAIT"
or "AGAIN."
 The choice of labels is restricted somewhat when one is using a
simple memory-resident or absolute assembler. These assemblers
treat the entire program as a unit, thus limiting the use of common
labels. Several techniques circumvent this problem.
 Common labels can be sequentially numbered, such as SKIP1,
SKIP2, SKIP3, etc.; or perhaps within subroutines all labels can use
the name of the subroutine followed by a number, such as SEND,
SEND2, SEND3, etc.
 There is an obvious loss of clarity here.
 More sophisticated assemblers, such as ASM51, allow each
subroutine (or a common group of subroutines) to exist as a
separate file that is assembled independent of the main program.
This type of assembler, usually called a "relocatable" assembler,
allows the same label to appear in different files. 42/4
9
 The use of comments cannot be overemphasized,
particularly in assembly language programming, which is
inherently abstract.
 All lines of code, except those with truly obvious actions,
should include a comment.
 Conditional jump instructions are effectively commented
using a question similar to the flowchart question for a similar
operation.
 The "yes" and "no" answers to the question should appear in
comments at the lines representing the `jump" and "no jump"
actions.
43/4
9
 Comment lines are essential at the beginning of each
subroutine.
 Since subroutines perform well-defined tasks commonly
needed throughout a program, they should be general-
purpose and well documented.
 Each subroutine is preceded by a comment block, a
series of comment lines that explicitly state:
 The name of the subroutine
 The operation performed
 Entry conditions
 Exit conditions
 Name of other subroutines used (if any)
 Name of registers affected by the subroutine (if any)
44/4
9
 As applications grow in size and complexity,
new subroutines are often written that build
upon and use existing subroutines. Thus,
subroutines are calling other subroutines,
which in turn call other subroutines, and so on.
These are called "nested subroutines." There
is no danger in nesting subroutines so long as
the stack has enough room to hold the return
addresses. This is not a problem, since
nesting beyond several levels is rare.
 A potential problem, however, lies in the use of
registers within subroutines.
 A solid programming practice is to save
registers on the stack that are altered by a
subroutine, and then restore them at the end
of the subroutine. 46/4
9
 Defining constants with equate statements
makes programs easier to read and maintain.
 Equates appear at the beginning of a program
to define constants such as carriage return
(<CR>) and line feed (<LF>), or addresses of
registers inside peripheral ICs such as
STATUS or CONTROL.
 A generous use of equates makes a program
more maintainable, as well as more readable.
 If a constant must be changed, only one line
needs changing-the line where the symbol is
equated. 47/4
9
 As programs grow in size, it is important to "divide and
conquer"; that is, subdivide large and complex operations
into small and simple operations. These small and simple
operations are programmed as subroutines. Subroutines
are hierarchical in that simple subroutines can be used by
more complex subroutines, and so on.
 A flowchart references a subroutine using the "predefined
process" box. The use of this symbol indicates that another
flowchart elsewhere describes the details of the operation.
 Subroutines are constructed in pseudo code as complete
sections of code, beginning with their names and
parentheses. Within the parentheses are the names or
values of parameters passed to the subroutine (if any).
Each subroutine ends with the keyword RETURN followed
by parentheses containing the name or value of parameters
returned by the subroutine (if any).
48/4
9
 Although programs are often written piecemeal (i.e., subroutines are
written separately from the main program), all programs should be
consistent in their final organization. In general, the sections of a
program are ordered as follows:
 Equates
 Initialization instructions
 Main body of program
 Subroutines
 Data constant definitions (DB and DW)
 RAM data locations defined using the DS directive
 All but the last item above are called the "code segment," and the
RAM data locations are called the "data segment."
 Code and data segments are traditionally separate, since code is often
destined for ROM or EPROM, whereas RAM data are always destined
for RAM.
 Note that data constants and strings defined using the DB or DW
directives are part of the code segment (not the data segment), since
these data are unchanging constants and, therefore, are part of the
program. 51/4
9
 Write an assembly language program for the Intel 8086
microprocessor that adds two 16-bit words in the memory
locations called ADD1 and ADD2 respectively, and stores the
result in a memory location SUM

52/4
9

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy