Stack and subroutine

1,395 views 20 slides Aug 22, 2020
Slide 1
Slide 1 of 20
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20

About This Presentation

8085 Microprocessor


Slide Content

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.

OnestackisinitializedintheR/Wmemory,storingdataintothestack,start
frompreviousmemorylocationoftheinitializationvalue.
ThatisifstackisinitializedasLXISP,FFFFSofirstdataisstoredintoFFFE
location,nextdataisstoredinFFFDlocationandsooninreverseorder.
11
22
33
44
55
66
77
R/W memory
FFF9
FFFA
FFFB
FFFC
FFFD
FFFE
FFFFSP
LXI SP, FFFF

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.

The8085hastwoinstructionsfordealingwithsubroutines.
oTheCALLinstructionisusedinthemainprogramtoredirectprogram
executiontothesubroutine.
oTheRETinstructionisusedatthelastofthesubroutinetoreturnthe
executiontothecallingroutine.
Whenasubroutineiscalled,addressofthenextinstructionfollowingto
thecallinstructionisPUSHedintothestackautomaticallyandprogram
executionistransferredtothesubroutineaddress.
WhenRETinstructionisexecutedattheendofthesubroutine,the
returnedaddresswhichwasstoredintothestackmemoryduringCALL,is
retrievedintoPCandexecutionisresumedinthemainprogram.
CALLinstructionmustbeusedinconjunctionwiththeRETinstructionin
thesubroutine.DuringSubroutineStackmustbeinitialized.

CALL 4000H
o3-byteinstruction.
oPushtheaddressoftheinstructionimmediatelyfollowingtheCALL
ontothestackanddecrementthestackpointerregisterbytwo.
oLoadtheprogramcounterwiththe16-bitaddresssuppliedwiththe
CALLinstruction.
oJumpUnconditionallytomemorylocation.

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

Restart and Conditional CALL and Return Instruction:
Inadditiontotheunconditionalcallandreturninstruction,the8085instructionset
haseightrestartinstructionandeightconditionalcallandreturninstruction.
Restart(RST)instruction:
RSTinstructionare1-bytecallinstructionthattransfertheprogramSequencetoa
specificlocationonoo
Hmemorypage.TheyareexecutedthesamewayasCALL
instruction.WhenRSTinstructionisexecuted,the8085storesthecontentofthePC
(i.e.addressofthenextinstruction)onthetopofthestackandtransferstheprogram
sequencetothefixedrestartlocation.Theseinstructionsaregenerallyusedin
conjunctionwiththeinterruptprocess.
RST0CALL0000
RST1CALL0008
RST2CALL0010
RST3CALL0018
RST4CALL0020
RST5CALL0028
RST6CALL0030
RST7CALL0038

Conditional CALL and RET instruction:
Conditionalcallandreturninstructionarebasedonthefourconditionalflagbits:CY,P,
Z,S.
IncaseofconditionalCALL,programistransferredtothesubroutineiftheconditionis
met,otherwisemainprogramexecutioncontinue.IncaseofconditionalRET,the
sequencereturnstothemainprogramiftheconditionismet,otherwiseexecutionin
thesubroutinecontinued.LastinstructionofanysubroutinemustbeunconditionalRET
otherwisesequencecan’treturntothemainprogramifconditionnotmet.
UnconditionalCALL: UnconditionalRET:
CC CallsubroutineifCY=1 RC ReturnfromsubroutineifCY=1
CNC CallsubroutineifCY=0 RNC ReturnfromsubroutineifCY=0
CZ CallsubroutineifZ=1 RZ ReturnfromsubroutineifZ=1
CNZ CallsubroutineifZ=0 RNZ ReturnfromsubroutineifZ=0
CM CallsubroutineifS=1 RM ReturnfromsubroutineifS=1
CP CallsubroutineifS=0 RP ReturnfromsubroutineifS=0
CPE CallsubroutineifP=1 RPE ReturnfromsubroutineifP=1
CPO CallsubroutineifP=0 RPO ReturnfromsubroutineifP=0

Multiple Calling and Nesting of a subroutine

Multiple Ending of a subroutine
Tags