Professional Documents
Culture Documents
(MPI)
GTU # 3160712
Unit-6:
Stack and
Subroutine
Positive Vibes:MPI is the interesting, easiest and scoring subject.
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
Positive Vibes:MPI is the interesting, easiest and scoring subject.
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
Positive Vibes:MPI is the interesting, easiest and scoring subject.
Introduction to Subroutine
Introduction to Subroutine
A subroutine is a group of instruction that performs a subtask of repeated occurrence.
Positive Vibes:MPI is the interesting, easiest and scoring subject.
Opcode Operand
CALL 4 0 5 0
40 50 Stack
03
06
Registers 21
W Z SP 50
SP
SP
PC 24 00
RET
• Retrieve the return address from the top of the stack
• Load the program counter with the return address.
Stack
03 2151
06 2150
Registers 21 2149
W Z SP 50 2148
SP 24 2147
SP 00 2146
2145
PC
2144
instruction will pick wrong value of the return address from the stack and program will fail.
3. OUT 01
Update
4. DCR C
Is this
NO
Final
Count
5. JNZ LOOP
YES
6. HLT
End
DCRLOOP
MVI
JNZ C 05h
C, (true) JNZ LOOP (false)
MachineCycle:
Machine Cycle: FF ++=RR
1+=R2=3 Machine Cycle: F+R =2
T-States:
T-States: = 4T
4T
4T ++3T
3T+=3T=10T
7T T-States: 4T + 3T =7T
Now to calculate time delay in loop, we must account for the T-states required for each
instruction, and for the number of times instructions are executed in the loop.
The for the next two instructions:
DCR: 4 T-States
JNZ : 10 T-States
14 T-States
Here, the loop is repeated for 5 times.
instruction
i.e If JNZ = true, then T-States = 10
FF h
Is Delay
No Is Count Register = 0?
Complete?
Yes
Yes
continuously in hexadecimal
1. MVI B,FF
from FFh to 00h with 0.5 s
clock period. Use register C to 2. LOOP:MOV A,B
set up 1ms delay between each 3. OUT 01
count and display the number at 4. MVI C, COUNT; need to calculate delay count
one of the output port.
5. DELAY: DCR C
Given: 6. JNZ DELAY
Counter= FF h 7. DCR B
Clock Period T=0.5 s 8. JNZ LOOP
Total Delay = 1ms 9. HLT
Output:
To find value of delay counter
3. OUT 01
4. MVI C, COUNT
Calculate Delay for Internal Loop
5. DELAY: DCR C
TI = T-States * Clock Period * COUNT
6. JNZ DELAY
= 14 * 0.5 * 10-6 * COUNT
7. DCR B
TI = (7.0 * 10-6 )* COUNT
8. JNZ LOOP
9. HLT
Prof. Swati R Sharma Unit 6 – Stack and Subroutine 40
Example 5: Hexadecimal counter program
Calculate Delay for Outer Loop: Instruction T-States
Positive Vibes:MPI is the interesting, easiest and scoring subject.
MOV A,B 4
TO = T-States * Clock Period
OUT 01H 10
= 35 * 0.5 * 10 -6
MVI C, COUNT 7
TO = 17.5 s DCR B 4
JNZ LOOP 10
TOTAL 35 T
Calculate Total Time Delay:
TD = TO + TL
1 ms = 17.5 * 10-6 + (7.0 * 10-6 )* COUNT
1 * 10-3 = 17.5 * 10-6 + (7.0 * 10-6 )* COUNT
COUNT
(140)10 = (8C)16
frequency is 6 MHz.
Note:
Clock Period = 1/Clock Frequency
1. MVI D, AA (A) 1 0 1 0 1 0 1 0
2. ROTATE: MOV A,D
After RLC 0 1 0 1 0 1 0 1
3. RLC
4. MOV D,A ANI 01h 0 0 0 0 0 0 0 1
5. ANI 01 After AND 0 0 0 0 0 0 0 1
6. OUT 01
Logic 0
7. MVI B, COUNT
(A) 0 1 0 1 0 1 0 1
8. DELAY: DCR B
After RLC 1 0 1 0 1 0 1 0
9. JNZ DELAY
10. JMP ROTATE ANI 01h 0 0 0 0 0 0 0 1
After AND 0 0 0 0 0 0 0 0
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.
ANS = (42)10
Thank You