Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
37 views

Lecture 10 Programming Constructs

The document discusses various programming constructs including sequential flow, if-then, if-then-else, multiple if-then-else, do-while loops, and programmed delays. It provides examples of how each construct can be used and sample code to illustrate them. It also discusses calculating delay times for programmed delay loops and includes an exercise for students to design delay routines of different durations.

Uploaded by

falconblock
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views

Lecture 10 Programming Constructs

The document discusses various programming constructs including sequential flow, if-then, if-then-else, multiple if-then-else, do-while loops, and programmed delays. It provides examples of how each construct can be used and sample code to illustrate them. It also discusses calculating delay times for programmed delay loops and includes an exercise for students to design delay routines of different durations.

Uploaded by

falconblock
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 25

Lecture 10

Programming
Constructs
Lecture 10(a)

Programming Constructs
Programming Constructs
Programming constructs describe the general flow
of instruction execution in portion of a program.

A program may have several constructs depending


on required function.

It is important to study constructs so as to be able


to choose best program flows for situations as
desired.
Sequential Flow
The program flows
without branching. Start

An example to find Get first number

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 Number N


AND 10000000b JZ negative?
next

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

Copy-paste Example DE = 4000h


HL = 8000h
BC = 1000h

MOVE_NEXT

Move contents of memory pointed by DE


to memory pointed to by HL

START: LXI D,4000H Increment DE


LXI H,8000H Increment HL
LXI B,1000H
Decrement BC
MOVE_NEXT: LDAX D
MOV M,A Code section
BC = 0? N
INX DE executed
INX HL repeatedly
Y
DCX BC
JNZ MOVE_NEXT End
HLT
Tutorial Exercise 7
1. Question 5
2. Question 6
Lecture 10(b)

Programming Constructs
Programmed Delays
A programmed delay is a program that executes so
as to give a time delay.

Basic idea is to repeatedly decrement a register to


zero in a DO-WHILE, WHILE-DO or FOR loop.

By the time the loop is exited, a time delay would


have elapsed.

Register decrements may be nested to increase


the delay period
Programmed Delays
Start

Load BC with number y


D1 DELAY: LXI B,0800h
Decrement BC D1: DCX B
JNZ D1
N
Is DE = 0?

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

Number of executions of LXI D,x = 1 10 cycles


Number of executions of LXI B,x = x 10x cycles
Number of executions of DCX B = xy 6xy cycles
Number of executions of JNZ D1 = xy 12xy cycles
Number of executions of DCX D = x 6x cycles
Number of executions of JNZ D2 = x 12x cycles
TOTAL CYCLES = 10 + 28x +18xy cycles
Class Exercise
Design delay routines of the following durations:
5 seconds
10 seconds
20 seconds
Individual Researches
The WHILE-DO LOOP

The FOR LOOP


MAY DO IF TIME
AVAILABLE ELSE
TO DO
Extra AS
Works
INDIVDUAL
WORKS
The WHILE-DO Loop
A section of a program
is executed repeatedly
until some condition is LOOP
met.
Test condition checked
Write Aah
before executing code
Pin 7
high? N
Example:
A program to write the
code AAh to port 07h Y NEXT

until pin 7 of the port


06h goes high.
The WHILE-DO Loop

LOOP
LOOP: IN 06h
ANI 10000000b
JNZ NEXT Write Aah

MVI A,0AAh Pin 7


OUT 07h high? N

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

A section of a program is executed repeatedly


until some condition is met.

You might also like