CSC 409 Unit 6
CSC 409 Unit 6
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
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.
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.
Source
Machine
Code Compiler
Code
(Program)
Running
Inputs Outputs
Program
Source Target
Program Compiler Program
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.
Source
Code
(Program) Computer
Running An Outputs
Interpreter
Inputs
E.g., Python is an interpreted language
Source
Program
Interpreter Output
Input
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
Java Compiler
bytecode
ML Interpreter
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