Stack
ThestackisagroupofmemorylocationintheR/Wmemorydefinedby
theprogrammerthatisusedfortemporarystorageofbinary
informationduringtheexecutionofaprogram.
The starting location of the stack is defined in the program by loading a
16 bit memory address into the stack pointer (SP) register as follow:
LXI SP, FF00
H
Stackcanbeinitializedatanywhereinthememorylocationwithinthe
R/Wmemoryusingaboveprocedure.But,Asdataarefilledintothe
stackmemoryinreversedirection,itisbettertoinitializestartofthe
stackmemoryatthehighestmemorylocationinordertogetmaximum
stackmemoryandavoidoverlappingthestackmemorywiththe
program.
So,StackisinitializedatFFFF
HwhichisthelastmemoryintheR/W.
LXI SP, FFFF
H.
Storing data into stack memory
The8085microprocessorhastwoinstructionsPUSHandPOPforstoring
dataintostackmemoryandretrievingitbackrespectivelyinLIFO.Both
instructionworkswithregisterpaironly.
PUSH:Itisusedtostorethecontentofregisterpairontothestack
memoryinreverseorder(decreasingmemoryaddress)
POP:Itisusedtotransferdatabytesfromstackmemoryintothe
respectiveregisterpair.
PUSH Instruction POP Instruction Operation with
PUSH B POP B B –C Register pair
PUSH D POP D D –E Register pair
PUSH H POP H H –L Register pair
PUSH PSW POP PSW Accumulator and Flag
PUSH B
Decrement SP by 1
Copy the contents of register B to the memory location pointed by SP
Decrement SP by 1
Copy the contents of register C to the memory location pointed by SP
So, SP is decremented by two.
LXI SP,FFFF
PUSH B
11
22
R/W memory
FFF9
FFFA
FFFB
FFFC
FFFD
FFFE
FFFF
SP
LXI SP, FFFF
A F
B 11 C 22
D E
H L
SP
PUSH D
Decrement SP by 1
Copy the contents of register D to the memory location pointed by SP
Decrement SP by 1
Copy the contents of register E to the memory location pointed by SP
So, SP is decremented by two.
LXI SP,FFFF
PUSH B
PUSH D
PUSH PSW
11 (B)
22 (C)
33 (D)
44 (E)
A
F
R/W memory
FFF9
FFFA
FFFB
FFFC
FFFD
FFFE
FFFF
SP
LXI SP, FFFF
A F
B 11 C 22
D 33E 44
H L
SP
POP H
▪Copy the contents of the memory location pointed to by the SP to
register L.
▪Increment SP by 1
▪Copy the contents of the memory location pointed to by the SP to
register H
▪Increment SP by 1
▪SP is total incremented by two.
LXI SP, FFFF
PUSH B
POP H
A F
B 11 C 22
D E
H 11 L 22
11 (B)
22 (C)
R/W memory
FFF9
FFFA
FFFB
FFFC
FFFD
FFFE
FFFF
SP
LXI SP, FFFF
Conclusion:
During pushing, the stack operates in a “decrement then store” style.
The stack pointer is decremented first, then the information is placed on
the stack.
During poping, the stack operates in a “use then increment” style.
The information is retrieved from the top of the stack and then the
pointer is incremented.
The SP pointer always points to “the top of the stack’’.
Subroutine
Asubroutineisgroupofinstructionwrittenseparatelyfromthemain
programtoperformafunctionthatoccursrepeatedlyinthemain
program
Whenamainprogramcallsasubroutinetheprogramexecutionis
transferredtothesubroutine.
Afterthecompletionofthesubroutine,theprogramexecutionreturnsto
themainprogram.
So,Themicroprocessorusesthestacktostorethereturnaddressofthe
subroutine.
RET
o 1-byte instruction
o Retrieve the return address from the top of the stack and increments stack
pointer register by two.
o Load the program counter with the return address.
o Unconditionally returns from a subroutine.
RET
Main Program
8000: LXI SP,FFFF
8020: CALL 9000 (opcode of CALL)
8021: lower byte operand (00)
8022: Higher byte operand (90)
8023: Next instruction following to the call
802F: RST 1
Subroutine
9000:First subroutine Inst.
900F:RET
CALL and RET Sequence
Call instruction sequence:
Callisathreebyteinstruction.So,dependinguponthebytesizeofthe
instructionithasopcodefetch,Memoryreadandmemoryread.Again,
beforejumpingtothesubroutine,returnaddress(i.e.contentofPCvalue
whichistheaddressofnextinstructionfollowingtothecallistobe
storedontothestackmemory).So,alongwiththeabovethreeM/C,it
hasalsomem.Write(storinghigheraddressontostack)andmem.Write
(storingloweraddressontothestack)M/C.
CALL 9000
8020: opcode of call (OF)
8021: 00 (MR)
8022: 90 (MR)
PC 8023: Next Instruction
ValueofPCisthereturnaddressaftercompletingthesubroutine.So,PC
valueistobestoredintothestackmemorybeforegoingtothe
subroutineusingautomaticPUSHoperation.
M/C SP
FFFF
Add. Bus
AB
PC
PC
H PC
L
Data Bus
DB
Int. Reg.
W Z
M1
OF
FFFE
SP-1
8020 8021 Opcode
CD
---------
M2
MR
FFFE 8021 8022 00 ---00
M3
MR
FFFE 8022 8023 90 9000
M4
MW
FFFD
SP-2
FFFE 8023 80
PC
H
9000
M5
MW
FFFD FFFD 8023 23
PC
L
9000
OF
Of 1
st
inst.
In
Subroutine
FFFD 9000 9001 9000
Mem
Add.
Code
8020CD
802100
802290
8023
Opcode
of next
PC
23
80
----------
FFFD
FFFE
FFFF
Stack
RET instruction sequence:
RETisa1-byteinstruction.So,dependinguponthebytesize,ithasonlyOFM/C.But
whenRETinstructionisexecuted,programsequenceistransferredtothemain
programbyPOPingthereturnaddresstoPCfromstackmemorywhichwassaved
duringcall.Then,executionresumefromthisaddress.So,Twoextramemoryreadis
requiredtogetreturnaddress.
M/CofRETareOF,MR(readinglowervalueofreturnaddressfromstack),MR
(readinghighervalueofreturnaddressfromstack)
900F: RET instruction opcode (OF)
PC 9010:
M/C
SP
FFFD AB
PC
PC
H PC
L
DB W Z
M1
OF
FFFD 900F 9010 C9 -----------
M2
MR
FFFE
SP+1
FFFD 9010
23
From
top of
stack
----23
M3
FFFF
Sp+2
FFFE 9010
80
80 23
M1
OFof
inst.
Next to
CALL
FFFF 8023 8024 opcode
80 23
900FC9
FFFD23
FFFE80
FFFF-------
Stack
SP