Lecture 35
Lecture 35
FUNDAMENTALS OF PROGRAMMING:
It is very difficult for the user to write his program directly in machine
code. It is more common to write the program in Assembly Language
and then translate the assembly language program into machine
language either by hand coding or using an assembler program.
The best method to introduce software program is to take few
examples first and solve.
SUM= i
i=1
First we should note down all the constraints. In this problem since
we have to use only 8-bit registers, SUM cannot exceed 255D.
Working back we can find out the constraint on N. We know SUM of
N natural number is given by
N (N+1)
SUM=
2
Therefore, for SUM ≤ 255 the value of N ≤ 22.
The second stage is to list down the algorithm to be used to solve the
problem and draw the flow chart of the algorithm. For this problem,
the flowchart is shown in fig.6.1. Fig.6.2 gives better picture in terms
of FORTRAN flow chart.
START
START
SUM = 0
COUNTER = N SUM = 0
I=0 COUNTER = N
I=0
GENERATE NEXT
NATURAL NUMBER I = I +1
HAS
ALL THE
NATURAL NUMBERS No IS
No
ADDEDD COUNTER
? =0?
Yes Yes
STOP STOP
(A) 0
(C) N
(B) (A)
(B) (B) + 1
(B) (A)
(C) (C) - 1
IS No
Z =1 ?
Yes
STOP
Having decided the variables in the register we draw the macro RTL
flow chart. While drawing this flow chart we take the help of FRTRAN
flow chart drawn earlier and also the instruction set. Every block in
the macro RTL flow chart must be implemented using one instruction
at this stage. For the given problem this is shown in fig.6.3..
Having written the macro RTL flow chart we can directly write ALP.
The preliminary ALP for fig.6.3 is shown in fig.6.4.
NSUM: XRA A ; Clear Accumulator
MVI C, N ; Initialize the Counter with Last Natural Number
MOV B, A ; Initialize Natural Number in (B) to zero
NEXT: INR B ; Generate Next Natural Number
ADD B ; Obtain running SUM in Accumulator
DCR C ; Has all Natural Number added?
JNZ NEXT; No, Go Back to Generate Next Natural Number
HLT ; Yes, stop.
Fig.6.4 Assembly Language program for SUM of Natural Numbers
1) Label field: The first field of ALP statement is the label field. A label is
a symbol used to represent an address that is not specifically known
or it is a name to be assigned to an instruction’s location. The lebel
field is usually ended with a colon (:).
2) Mnemonic field: This field contains the mnemonic for the instruction
to be performed. Sometimes mnemonics are referred to as operation
codes or opcode. It may consist of pseudo mnemonics.
3) Operand field: Operand field consists of operand and operands -
either constants or variables with reference to the instruction in the
mnemonic field. It may be any register, data, or address on which the
instruction is to be performed. Depending upon the instruction, the
operand filed may be absent, may contain one operand or two
operands separated by a comma. A comma is required between
register initials or between register initial and a data byte.
4) Comment field: A very important part of an ALP is the comment field.
For most assemblers, the comment field is started with a semicolon
(;). Comments do not become part of the machine program. They are
written for the reference of the user. If you write a program without
comment and set it aside for 6 months it may be very difficult for you
to understand the program again when you look back to it. It may
even appear that someone else has written it. Comments should be
written to explain in detail what each instruction or group of
instructions is doing. Comments should not just describe the
mnemonic, but also the function of the instruction in the particular
routine. For best result, write comments as if you are trying to explain
the program to someone who initially knows nothing about the
program’s purpose.
In addition to the comments, a program or subroutine should
start with a series of comments describing what the program is
supposed to do. The staring comments should also include a list of
parameters, registers and memory locations used.