Chapter 6
Chapter 6
(MPI)
GTU # 3160712
Unit-6:
Stack and
Subroutine
Looping
Topics to be covered
Introduction to Stack
Introduction to Subroutine
Counters and Time Delays
Time Delay Routines
Time Delay using a Register Pair
Counter design with time delay
Examples
Assembly language programs
Introduction to Stack
Introduction to Stack
Stack is a group of memory location in the R/W memory that is used for
temporary storage of binary information during execution of a program.
The starting memory location of the stack can be defined in program and
space is reserved usually at the high end of memory map.
E.g.: LXI SP,FFF8; loads 16-bit memory address in stack pointer
Instruction necessary for stack are as follows:
LXI SP, 2095 Load the stack pointer register with a 16-bit
PUSH B/D/H address.
It copies contents of register pair on the stack
PUSH PSW Operand PSW represents Program status word
meaning. i.e. content of accumulator and flags.
POP B/D/H It copies content of top two memory locations
of the stack in to specified register pair
POP PSW It copies content of top two memory locations
of the stack in to accumulator and flags
respectively.
Introduction to
Subroutine
Introduction to Subroutine
A subroutine is a group of instruction that performs a subtask of repeated
occurrence.
A subroutine can be used repeatedly in different locations of the program.
Advantage of using Subroutine
Rather than repeating same instructions several times, they can be
grouped into a subroutine that can be called from the different locations.
Where to write Subroutine?
In Assembly language, a subroutine can exist
anywhere in the code.
However, it is customary to place subroutines
separately from the main program.
The 8085 has two instructions for dealing with
subroutines
The CALL instruction is used to redirect program
execution to the subroutine.
Introduction to Subroutine
Instruction Description Example
Opcode Operand
Stack
03 2151
06 2150
Register 21 2149
s
W Z SP 50 2148
SP 24 2147
SP 00 2146
2145
PC
2144
Things to be considered in Subroutine
Number of PUSH and POP instruction used in the subroutine must be
same, otherwise, RET instruction will pick wrong value of the return
address from the stack and program will fail.
Counters and Time
Delays
Counters and Time Delays
Counters and Time Delays are important techniques.
Applications of Counters and Time Delays
They are commonly used in
1. Traffic Signal
2. Digital Clocks
3. Process Control
4. Serial data transfer
• A counter is designed simply by loading appropriate
number into one of the registers and using INR or DCR
instructions.
• Loop is established to update the count.
• Each count is checked to determine whether it has
reached final number ;if not, the loop is repeated.
Example 1: Create Counters and Time Delays
3. OUT 01
Update
4. DCR C
Is this
NO
Final
Count
5. JNZ LOOP
YES
6. HLT
End
Example 1: Create Counters and Time Delays
CRJNZ
C LOOP
MVI C, 05h
(true) JNZ LOOP (false)
achine Cycle:
Machine
Machine Cycle:F +FR=+1
Cycle: FR+=3
R Machine Cycle: F + R = 2
States:
T-States:
=2 = 4T 4T + T-States: 4T +
3T + 3T=10T 4T + 3T =
T-States: 3T =7T
7T
Example 1: Create Counters and Time Delays
Instruction MVI C, 05h requires 7 T-States to execute.
Assuming, 8085 Microprocessor with 2MHz clock frequency.
How much time it will take to execute above instruction?
Clock frequency of the system (f)= 2 MHz
Clock period (T) = 1/f = ½ * 10-6 = 0.5 s
Time to execute MVI = 7 T-states * 0.5 s
= 3.5 μs
How much time it will take to execute above instruction with 1 MHz
clock frequency?
Example 1: Create Counters and Time Delays
FF h
Yes
Yes
Example 5: Hexadecimal counter program
Write a program to count Assembly Language Program
continuously in
1. MVI B,FF
hexadecimal from FFh to
00h with 0.5 s clock 2. LOOP:MOV A,B
period. Use register C to 3. OUT 01
set up 1ms delay 4. MVI C, COUNT; need to calculate delay
between each count and
count
display the number at
one of the output port. 5. DELAY: DCR C
6. JNZ DELAY
Given:
7. DCR B
Counter= FF h
8. JNZ LOOP
Clock Period T=0.5 s
9. HLT
Total Delay = 1ms
Output:
To find value of delay
Example 5: Hexadecimal counter program
Delay Calculations Instruction T-States
DCR C 4
1. MVI B,FF;
JNZ DELAY 10
2. LOOP:MOV A,B Total 14 T
3. OUT 01
4. MVI C, COUNT
Calculate Delay for
5. DELAY: DCR C
Internal Loop
6. JNZ DELAY TI = T-States * Clock Period
7. DCR B
* COUNT
8. JNZ LOOP = 14 * 0.5 * 10-6 * COUNT
9. HLT TI = (7.0 * 10-6 )* COUNT
Example 6: 0-9 up/down counter program
Write an 8085 Instructio T-States
Program n
assembly language DCX 6
program to generate a 1. START: MVI B,00H MOV A,L 4
decimal counter (which 2. DISPLAY:OUT 01 ORA H 4
counts 0 to 9 3. LXI H, COUNT JNZ 10
continuously) with a 4. LOOP: DCX H TOTAL 24 T
one second delay in
between. The counter 5. MOV A,L
should reset itself to
6. ORA H
zero and repeat
continuously. Assume a 7. JNZ LOOP
Clock frequency of 8. INR B
1MHz. 9. MOV A,B
10. CPI 0A
11. JNZ DISPLAY
12.JZ START
Example 6: 0-9 up/down counter program
Delay Calculation:
As show in previous program of register pair, assuming
T-states = 24
Loop Delay TL = 1 sec
Clock Period T =1 * 10-6 sec
Find Count
TL= T * Loop T-states * Count
1 sec = (1.0 * 10-6 sec) * 24 * Count
Count =
= (41666)10
= (A2C2)16 =A2C2 h
Exercise
Calculate delay in following loop, assuming clock period = 0.33s
Ans=102ms
TL= T * T-states * Count
Exercise
Write a set of 8085 assembly language instructions to generate a 1 second
delay, if the crystal frequency is 6 MHz.
Note:
Clock Period = 1/Clock Frequency
Example 7:Square wave program
Write a program to generate a continuous square wave with the
period of 500 s with clock period of 325ns, and use bit D0 to
output square wave.Problem Analysis
• In this problem, the period of square wave is 500
s; therefore, the pulse should be ON(logic 1) for
250 s and OFF(logic 0) for remaining 250 s.
• Therefore, the alternate pattern of 0/1 bits can be
provided by loading Accumulator with AA h (1010
1010).
• Now rotating the pattern once through each delay
loop.
500 s
• Bit D0 of output port is used to provide logic 0
and 1.
• The delay of 250s can be easily obtained with an
8-bit delay count and one register.
Example 7:Square wave program
Program Logic
1. MVI D, AA 1
(A) 1 0 1 0
2. ROTATE: MOV A,D
1 0 1 0
3. RLC
4. MOV D,A After RLC 0 1 0 1
5. ANI 01 0 1 0 1
6. OUT 01 ANI 01h 0 0 0 0 0
Logic
7. MVI B, 00 0 1
COUNT (A) 0 1 0 1
8. DELAY: DCR B After
0 1 0AND
1 0 0 0 0
9. JNZ DELAY 0 0 RLC
After 0 1 1 0 1 0
10. JMP ROTATE 1 0 1 0
ANI 01h 0 0 0 0 0
Example 7:Square wave program
Delay Calculation:
In this problem, the pulse width is relatively small (250 s); therefore, to
obtain accurate output pulse, we should take into account for all the T-
states.
The total delay should include the delay in the loop and execution time of
the instruction outside the loop.
Example 7:Square wave program
Delay Calculation:
No. of instruction outside the loop is seven.
Delay outside the loop TO=46 T-states * 325 ns =14.95s
Delay Loop includes two instruction, with 14 T-States, except for last cycle
11 T-States
Loop Delay TL= 14 T * 325ns * (COUNT -1) + [ 11 T *325ns ]
TL = 4.5 s (COUNT -1) + 3.575 s
Total delay required = 250 s
TD = TO + TL
250 s = 14.95s + 4.5 s (COUNT -1) + 3.575 s
Count = (52)10
= (34)16
Exercise
Write a program to generate a square wave with the period of 400s with
clock period of 325ns. Use bit D0 to output square wave.
ANS = (42)10
GTU Exam Questions
Sr Questions Marks Year
.
1. Write a set of 8085 assembly language instructions to generate a 0.5 second 4 W’17
delay, if
the crystal frequency is 4 MHz.
2. Explain stack and subroutine with suitable example. 4 S’18
3. Design an up-down counter to count from 0 to 7 and 7 to 0 continuously with a 1- 4 W’18
second delay between each count, and display the count at one of the output
ports. Show the delay calculation
4. Define the concepts of stack and subroutine. Explain the PUSH and POP 3 S’19
instructions of an 8085 microprocessor with example.
References
Book: Microprocessor Architecture, Programming, and Applications with the
8085, Ramesh S. Gaonkar Pub: Penram International
Thank You