Professional Documents
Culture Documents
UNIT 3 Microprocessor
UNIT 3 Microprocessor
UNIT 3
Stack and Subroutines
2
The
Stack
• The stack is an area of memory identified by the
programmer for temporary storage of
information.
• The stack is a LIFO structure.
– Last In First Out.
• The stack normally grows backwards
into memory.
– In other words, the programmer
Memory
FFFC F3
FFFD 12 SP
FFFE
FFFF
6
The POP Instruction
• POP D (1 Byte Instruction)
– Copy the contents of the memory location pointed
to by the SP to register E
– Increment SP
– Copy the contents of the memory location pointed
to by the SP to register D
– Increment SP
D E
12 F3
FFFB
FFFC SP
F3
FFFD 12
FFFE
FFFF
7
Operation of the
Stackthe stack operates in
• During pushing,
a “decrement then store” style.
– The stack pointer is decremented first, then
the information is placed on the stack.
PUSH B
PUSH
D
...
POP
D
POP B
• Reversing the order of the POP instructions will result in
the exchange of the contents of BC and DE.
9
The PSW Register
• The 8085Pair
recognizes one additional register pair
called the PSW (Program Status Word).
– This register pair is made up of the
Accumulator and the Flags registers.
FFFC 80
12
FFFD SP
FFFE
FFFF
11
Pop PSW Register
• POP PSWPair (1 Byte Instruction)
– Copy the contents of the memory location pointed
to by the SP to Flag register
– Increment SP
– Copy the contents of the memory location pointed
to by the SP to register A
– Increment SP
A Flag
12 80
FFFB
FFFC SP
80
FFFD 12
FFFE
FFFF
12
Modify Flag Content using PUSH/POP
• Let, We want to Reset the Zero Flag
• 7 6 5 4 3 21 0
• 8085 Flag : S | Z | X |AC|X |P |X |
• Program: Cy
– LXI SP
FFFF
– PUSH PSW
– POP H
MOVBFH
– ANI A L (BFH= 1011 1111) * Masking
– MOV L A
– PUSH H
– POP
PSW
13
Subroutines
• A subroutine is a group of instructions that will be
used repeatedly in different locations of the
program.
– Rather than repeat the same instructions several
times, they can be grouped into a subroutine that
is called from the different locations.
PC 2003
FFFB
FFFC 03
20
FFFD SP
FFFE
FFFF
16
The CALL
Instruction
– MP Reads the subroutine address from the next
two memory location and stores the higher order
8bit of the address in the W register and stores the
lower order 8bit of the address in the Z register
– Pushe the address of the instruction immediately
following the CALL onto the stack [Return
address]
– Loads the program counter with the 16-bit address
supplied with the CALL instruction from WZ
register.
17
The RET
• RET Instruction
(1 byte instruction)
– Retrieve the return address from the top of
the stack
– Load the program counter with the return
address.
PC 2003
FFFB
4014 ...
4015 RET FFFC SP
03
FFFD 20
FFFE
FFFF
18
Things to be considered in Subroutine
• The CALL instruction places the return address
at the two memory locations immediately before
where the Stack Pointer is pointing.
– You must set the SP correctly BEFORE using the
CALL instruction.
Time Delay in
Loop TL= T*Loop T states * N10
=
MVI B,00H
NEXT: DCR B
MVI
C,8CH
DELAY: DCR C
JNZ DELAY
MOV A,B
OUT PORT#
JMP NEXT
Illustrative Program: Zero to nine (Modulo
ten) Counter Start
START: MVI B,00H
MOV A,B Initialize counter
DSPLAY: OUT
PORT # Display Output
LOOP: DCX H LXI
H,16-bit
MOV A,L Load Delay register
ORA H
JNZ LOOP Decrement Delay register
INR B
MOV A,B Is Delay register=0?
CPI 0AH
JNZ DSPLAY Next Count
JZ START
Is count =0AH?
If yes, Initialize
counter If no, Display
Output