Lecture 10 Programming Constructs
Lecture 10 Programming Constructs
Programming
Constructs
Lecture 10(a)
Programming Constructs
Programming Constructs
Programming constructs describe the general flow
of instruction execution in portion of a program.
average of two
Get second number
numbers
MVI A,05H ; get first number Add
MVI B,07H ; get second number
ADD B ; add the two Divide by 2
STC ; set carry flag
CMC ; complement (clear) Cy
RAR A ; divide by two End
The IF-THEN Construct
A section of the code is only
executed when some condition is
met.
In other cases, it is skipped over.
Number N
negative?
Example:
Program to calculate magnitude
of an 8-bit signed numbers in the
2’s complement form. Y
Magnitude is same as number if
number is positive Evaluate 2’s
Magnitude is equal the 2’s complement
complement of the input number
if the number is negative. NEXT
2’s complement evaluated only
when number is negative.
The IF-THEN Construct
MOV A,M Y
CMA Sequence
INR A Evaluate 2’s 2’s
calculates
complement
complement
next: INX H value
NEXT
The IF-THEN-ELSE Construct
Two sections of code that
are executed on alternate
conditions. Temp
N
above
threshold HEAT
Example: value?
A program to turn on or
off a heaters and fans Y
depending on whether Heat
temperature is above or Cool
below a threshold value.
NEXT
The IF-THEN-ELSE Construct
IN 00H
CPI 56H
Temp
JC HEAT above
N
MVI A,F0H threshold HEAT
OUT 01H value?
JMP NEXT
Y
HEAT: MVI 0FH Heat
OUT 01H Cool
NEXT: -------
NEXT
Multiple IF-THEN-ELSE Construct
Several sections of code
each one of which is
executed when one of Below Y
multiple conditions is met. LOW
50?
B=0
N
Example:
A program that reads the Below Y
port 08H and writes to 150? MID
register B:
0 if number is less than 50 N B = 50
50 if the number is in B = 150
range 50 to 150
150 if the number is above NEXT
150
Multiple IF-THEN-ELSE Construct
IN 08 ; read in value
CPI 50 ; compare to lowest range
JNC LOW ; branch to code for low range
CPI 150 ; compare to mid range
JNC MID ; branch to code for mid range
MVI B,150 ; code for high range
JMP NEXT ; jump code for alternate range
LOW: MVI B,0 ; code for low range
JMP NEXT ; jump code for alternate range
MID: MVI B,50 ; code for mid range
NEXT: ------- ; program continuation
The DO-WHILE Loop
A section of a program LOOP
is executed repeatedly
until some condition is
Read port
met.
Example:
A program to check the
value on an input port Value is Y
zero?
and only proceed when
a non-zero value is
read. N
The DO-WHILE Loop
LOOP
Read port
LOOP: IN 05H
JZ LOOP
------
Value is Y
zero?
N
Start
MOVE_NEXT
Programming Constructs
Programmed Delays
A programmed delay is a program that executes so
as to give a time delay.
End
Start
ProgrammedNested
Delaysloops
Load DE with number x
D2
Load BC with number y
DELAY: LXI D,067h
D1
D2: LXI B,0FFFFh
Decrement BC
D1: DCX B
N
JNZ D1
Is BC = 0?
DCX D
Y
JNZ D2
Decrement DE
N
Is DE = 0?
Y
End
Delay Calculations
Knowing durations of the
DELAY: LXI D,x
individual instructions, one can
D2: LXI B,y
evaluate total duration and hence
D1: DCX B
delay offered by the program
JNZ D1
segment.
DCX D
JNZ D2
LOOP
LOOP: IN 06h
ANI 10000000b
JNZ NEXT Write Aah
JMP LOOP
NEXT: - - - - - - - - Y NEXT
Code to be
repeated if till
condition met
The FOR Loop
Has same basic operation as DO-WHILE or
WHILE-DO LOOPs