SlideShare a Scribd company logo
1 of 20
Download to read offline
Stack
The stack is a group of memory location in the R/W memory defined by
the programmer that is used for temporary storage of binary
information during the execution of a program.
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, FF00H
Stack can be initialized at any where in the memory location within the
R/W memory using above procedure. But, As data are filled into the
stack memory in reverse direction, it is better to initialize start of the
stack memory at the highest memory location in order to get maximum
stack memory and avoid overlapping the stack memory with the
program.
So, Stack is initialized at FFFFH which is the last memory in the R/W.
LXI SP, FFFFH.
One stack is initialized in the R/W memory, storing data into the stack, start
from previous memory location of the initialization value.
That is if stack is initialized as LXI SP, FFFF So first data is stored into FFFE
location, next data is stored in FFFD location and so on in reverse order.
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
The 8085 microprocessor has two instructions PUSH and POP for storing
data into stack memory and retrieving it back respectively in LIFO. Both
instruction works with register pair only.
PUSH: It is used to store the content of register pair onto the stack
memory in reverse order (decreasing memory address)
POP: It is used to transfer data bytes from stack memory into the
respective register pair.
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
FFFFSP
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
FFFFSP
LXI SP, FFFF
A F
B 11 C 22
D 33 E 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
FFFFSP
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
A subroutine is group of instruction written separately from the main
program to perform a function that occurs repeatedly in the main
program
When a main program calls a subroutine the pro gram execution is
transferred to the subroutine.
After the completion of the subroutine ,the program execution returns to
the main program.
So, The microprocessor uses the stack to store the return address of the
subroutine.
The 8085 has two instructions for dealing with subroutines.
o The CALL instruction is used in the main program to redirect program
execution to the subroutine.
o The RET instruction is used at the last of the subroutine to return the
execution to the calling routine.
When a subroutine is called, address of the next instruction following to
the call instruction is PUSHed into the stack automatically and program
execution is transferred to the subroutine address.
When RET instruction is executed at the end of the subroutine, the
returned address which was stored into the stack memory during CALL, is
retrieved into PC and execution is resumed in the main program.
CALL instruction must be used in conjunction with the RET instruction in
the subroutine. During Subroutine Stack must be initialized.
CALL 4000H
o 3-byte instruction.
o Push the address of the instruction immediately following the CALL
onto the stack and decrement the stack pointer register by two.
o Load the program counter with the 16-bit address supplied with the
CALL instruction.
o Jump Unconditionally to memory location.
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:
Call is a three byte instruction. So, depending upon the byte size of the
instruction it has opcode fetch, Memory read and memory read. Again,
before jumping to the subroutine, return address (i.e. content of PC value
which is the address of next instruction following to the call is to be
stored onto the stack memory). So, along with the above three M/C, it
has also mem. Write ( storing higher address onto stack) and mem. Write
( storing lower address onto the stack) M/C.
CALL 9000
8020: opcode of call (OF)
8021: 00 (MR)
8022: 90 (MR)
PC 8023: Next Instruction
Value of PC is the return address after completing the subroutine. So, PC
value is to be stored into the stack memory before going to the
subroutine using automatic PUSH operation.
M/C SP
FFFF
Add. Bus
AB
PC
PCH PCL
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 90 00
M4
MW
FFFD
SP-2
FFFE 8023 80
PCH
90 00
M5
MW
FFFD FFFD 8023 23
PCL
90 00
OF
Of 1st inst.
In
Subroutine
FFFD 9000 9001 90 00
Mem
Add.
Code
8020 CD
8021 00
8022 90
8023 Opcode
of next
PC
23
80
----------
FFFD
FFFE
FFFF
Stack
RET instruction sequence:
RET is a 1-byte instruction. So, depending upon the byte size, it has only OF M/C. But
when RET instruction is executed, program sequence is transferred to the main
program by POPing the return address to PC from stack memory which was saved
during call. Then, execution resume from this address. So, Two extra memory read is
required to get return address.
M/C of RET are OF, MR (reading lower value of return address from stack), MR
(reading higher value of return address from stack)
900F: RET instruction opcode (OF)
PC 9010:
M/C
SP
FFFD AB
PC
PCH PCL
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
OF of
inst.
Next to
CALL
FFFF 8023 8024 opcode
80 23
900F C9
FFFD 23
FFFE 80
FFFF -------
Stack
SP
Restart and Conditional CALL and Return Instruction:
In addition to the unconditional call and return instruction , the 8085 instruction set
has eight restart instruction and eight conditional call and return instruction.
Restart (RST) instruction:
RST instruction are 1-byte call instruction that transfer the program Sequence to a
specific location on ooH memory page. They are executed the same way as CALL
instruction. When RST instruction is executed, the 8085 stores the content of the PC
(i.e. address of the next instruction) on the top of the stack and transfers the program
sequence to the fixed restart location. These instructions are generally used in
conjunction with the interrupt process.
RST 0 CALL 0000
RST 1 CALL 0008
RST 2 CALL 0010
RST 3 CALL 0018
RST 4 CALL 0020
RST 5 CALL 0028
RST 6 CALL 0030
RST 7 CALL 0038
Conditional CALL and RET instruction:
Conditional call and return instruction are based on the four conditional flag bits: CY, P,
Z, S.
In case of conditional CALL, program is transferred to the subroutine if the condition is
met, otherwise main program execution continue. In case of conditional RET, the
sequence returns to the main program if the condition is met, otherwise execution in
the subroutine continued. Last instruction of any subroutine must be unconditional RET
otherwise sequence can’t return to the main program if condition not met.
Unconditional CALL: Unconditional RET:
CC Call subroutine if CY=1 RC Return from subroutine if CY=1
CNC Call subroutine if CY=0 RNC Return from subroutine if CY=0
CZ Call subroutine if Z=1 RZ Return from subroutine if Z=1
CNZ Call subroutine if Z=0 RNZ Return from subroutine if Z=0
CM Call subroutine if S=1 RM Return from subroutine if S=1
CP Call subroutine if S=0 RP Return from subroutine if S=0
CPE Call subroutine if P=1 RPE Return from subroutine if P=1
CPO Call subroutine if P=0 RPO Return from subroutine if P=0
Multiple Calling and Nesting of a subroutine
Multiple Ending of a subroutine

More Related Content

What's hot

Microcontroller pic 16f877 addressing modes instructions and programming
Microcontroller pic 16f877 addressing modes instructions and programmingMicrocontroller pic 16f877 addressing modes instructions and programming
Microcontroller pic 16f877 addressing modes instructions and programmingNilesh Bhaskarrao Bahadure
 
Architecture of 8051
Architecture of 8051Architecture of 8051
Architecture of 8051hello_priti
 
Instruction sets of 8086
Instruction sets of 8086Instruction sets of 8086
Instruction sets of 8086Mahalakshmiv11
 
Branch instructions in 8051 micrcocontroller
Branch instructions in 8051 micrcocontrollerBranch instructions in 8051 micrcocontroller
Branch instructions in 8051 micrcocontrollerUshaRani289
 
register file structure of PIC controller
register file structure of PIC controllerregister file structure of PIC controller
register file structure of PIC controllerNirbhay Singh
 
Addressing modes of 8051
Addressing modes of 8051Addressing modes of 8051
Addressing modes of 8051SARITHA REDDY
 
PIC18 TIMER PROGRAMMING IN ASSEMBLY AND C
PIC18 TIMER PROGRAMMING IN ASSEMBLY AND CPIC18 TIMER PROGRAMMING IN ASSEMBLY AND C
PIC18 TIMER PROGRAMMING IN ASSEMBLY AND C raosandy11
 
Stack in 8085 microprocessor
Stack in 8085 microprocessorStack in 8085 microprocessor
Stack in 8085 microprocessorhepzijustin
 
Microcontroller 8051 and its interfacing
Microcontroller 8051 and its interfacingMicrocontroller 8051 and its interfacing
Microcontroller 8051 and its interfacingAnkur Mahajan
 
Solution manual 8051 microcontroller by mazidi
Solution manual 8051 microcontroller by mazidiSolution manual 8051 microcontroller by mazidi
Solution manual 8051 microcontroller by mazidiMuhammad Abdullah
 
Assembler directives and basic steps ALP of 8086
Assembler directives and basic steps ALP of 8086Assembler directives and basic steps ALP of 8086
Assembler directives and basic steps ALP of 8086Urvashi Singh
 
Relay interfacing with 8051
Relay interfacing with 8051Relay interfacing with 8051
Relay interfacing with 8051Pratik Phadte
 

What's hot (20)

Microcontroller pic 16f877 addressing modes instructions and programming
Microcontroller pic 16f877 addressing modes instructions and programmingMicrocontroller pic 16f877 addressing modes instructions and programming
Microcontroller pic 16f877 addressing modes instructions and programming
 
Instruction formats-in-8086
Instruction formats-in-8086Instruction formats-in-8086
Instruction formats-in-8086
 
Architecture of 8051
Architecture of 8051Architecture of 8051
Architecture of 8051
 
Instruction sets of 8086
Instruction sets of 8086Instruction sets of 8086
Instruction sets of 8086
 
Interrupts in pic
Interrupts in picInterrupts in pic
Interrupts in pic
 
8051 ch9
8051 ch98051 ch9
8051 ch9
 
Branch instructions in 8051 micrcocontroller
Branch instructions in 8051 micrcocontrollerBranch instructions in 8051 micrcocontroller
Branch instructions in 8051 micrcocontroller
 
register file structure of PIC controller
register file structure of PIC controllerregister file structure of PIC controller
register file structure of PIC controller
 
Interfacing LCD with 8051 Microcontroller
Interfacing LCD with 8051 MicrocontrollerInterfacing LCD with 8051 Microcontroller
Interfacing LCD with 8051 Microcontroller
 
Addressing modes of 8051
Addressing modes of 8051Addressing modes of 8051
Addressing modes of 8051
 
Interfacing Stepper motor with 8051
Interfacing Stepper motor with 8051Interfacing Stepper motor with 8051
Interfacing Stepper motor with 8051
 
PIC18 TIMER PROGRAMMING IN ASSEMBLY AND C
PIC18 TIMER PROGRAMMING IN ASSEMBLY AND CPIC18 TIMER PROGRAMMING IN ASSEMBLY AND C
PIC18 TIMER PROGRAMMING IN ASSEMBLY AND C
 
boolean 8051
boolean 8051boolean 8051
boolean 8051
 
Programming with 8085
Programming with 8085Programming with 8085
Programming with 8085
 
Stack in 8085 microprocessor
Stack in 8085 microprocessorStack in 8085 microprocessor
Stack in 8085 microprocessor
 
Microcontroller 8051 and its interfacing
Microcontroller 8051 and its interfacingMicrocontroller 8051 and its interfacing
Microcontroller 8051 and its interfacing
 
Solution manual 8051 microcontroller by mazidi
Solution manual 8051 microcontroller by mazidiSolution manual 8051 microcontroller by mazidi
Solution manual 8051 microcontroller by mazidi
 
Assembler directives and basic steps ALP of 8086
Assembler directives and basic steps ALP of 8086Assembler directives and basic steps ALP of 8086
Assembler directives and basic steps ALP of 8086
 
Relay interfacing with 8051
Relay interfacing with 8051Relay interfacing with 8051
Relay interfacing with 8051
 
program status word
program status wordprogram status word
program status word
 

Similar to Stack and subroutine

B sc e 5.2 mp unit 2 soft ware(alp)
B sc e 5.2 mp unit 2 soft ware(alp)B sc e 5.2 mp unit 2 soft ware(alp)
B sc e 5.2 mp unit 2 soft ware(alp)MahiboobAliMulla
 
Microprocessors-based systems (under graduate course) Lecture 7 of 9
Microprocessors-based systems (under graduate course) Lecture 7 of 9 Microprocessors-based systems (under graduate course) Lecture 7 of 9
Microprocessors-based systems (under graduate course) Lecture 7 of 9 Randa Elanwar
 
Lecture 05 NOP and Stack Group of Instructions
Lecture 05 NOP and Stack Group of InstructionsLecture 05 NOP and Stack Group of Instructions
Lecture 05 NOP and Stack Group of InstructionsZeeshan Ahmed
 
Microprocessor Part 4
Microprocessor    Part 4Microprocessor    Part 4
Microprocessor Part 4Sajan Agrawal
 
Https _doc-0o-c4-apps-viewer.googleusercontent
Https  _doc-0o-c4-apps-viewer.googleusercontent Https  _doc-0o-c4-apps-viewer.googleusercontent
Https _doc-0o-c4-apps-viewer.googleusercontent vijaydeepakg
 
Programming basic computer
Programming basic computerProgramming basic computer
Programming basic computerMartial Kouadio
 
(a)Suppose the main memory of the Pep8 were completely filled with .docx
(a)Suppose the main memory of the Pep8 were completely filled with .docx(a)Suppose the main memory of the Pep8 were completely filled with .docx
(a)Suppose the main memory of the Pep8 were completely filled with .docxajoy21
 
Stack and subroutine
Stack and subroutineStack and subroutine
Stack and subroutineAshim Saha
 
Micro controller(pratheesh)
Micro controller(pratheesh)Micro controller(pratheesh)
Micro controller(pratheesh)Pratheesh Pala
 
Assembly Language Programming By Ytha Yu, Charles Marut Chap 8 (The Stack and...
Assembly Language Programming By Ytha Yu, Charles Marut Chap 8 (The Stack and...Assembly Language Programming By Ytha Yu, Charles Marut Chap 8 (The Stack and...
Assembly Language Programming By Ytha Yu, Charles Marut Chap 8 (The Stack and...Bilal Amjad
 

Similar to Stack and subroutine (20)

B sc e 5.2 mp unit 2 soft ware(alp)
B sc e 5.2 mp unit 2 soft ware(alp)B sc e 5.2 mp unit 2 soft ware(alp)
B sc e 5.2 mp unit 2 soft ware(alp)
 
Stack and subroutine
Stack and subroutineStack and subroutine
Stack and subroutine
 
Microprocessors-based systems (under graduate course) Lecture 7 of 9
Microprocessors-based systems (under graduate course) Lecture 7 of 9 Microprocessors-based systems (under graduate course) Lecture 7 of 9
Microprocessors-based systems (under graduate course) Lecture 7 of 9
 
Chapter 5 notes
Chapter 5 notesChapter 5 notes
Chapter 5 notes
 
6.pptx
6.pptx6.pptx
6.pptx
 
Chapter 9
Chapter 9Chapter 9
Chapter 9
 
Lec04
Lec04Lec04
Lec04
 
Lec04
Lec04Lec04
Lec04
 
Lecture 05 NOP and Stack Group of Instructions
Lecture 05 NOP and Stack Group of InstructionsLecture 05 NOP and Stack Group of Instructions
Lecture 05 NOP and Stack Group of Instructions
 
branch ins 8051
branch ins 8051branch ins 8051
branch ins 8051
 
Microprocessor Part 4
Microprocessor    Part 4Microprocessor    Part 4
Microprocessor Part 4
 
Https _doc-0o-c4-apps-viewer.googleusercontent
Https  _doc-0o-c4-apps-viewer.googleusercontent Https  _doc-0o-c4-apps-viewer.googleusercontent
Https _doc-0o-c4-apps-viewer.googleusercontent
 
Lecture5
Lecture5Lecture5
Lecture5
 
Lecture5(1)
Lecture5(1)Lecture5(1)
Lecture5(1)
 
Programming basic computer
Programming basic computerProgramming basic computer
Programming basic computer
 
MPMC UNIT-2.pdf
MPMC UNIT-2.pdfMPMC UNIT-2.pdf
MPMC UNIT-2.pdf
 
(a)Suppose the main memory of the Pep8 were completely filled with .docx
(a)Suppose the main memory of the Pep8 were completely filled with .docx(a)Suppose the main memory of the Pep8 were completely filled with .docx
(a)Suppose the main memory of the Pep8 were completely filled with .docx
 
Stack and subroutine
Stack and subroutineStack and subroutine
Stack and subroutine
 
Micro controller(pratheesh)
Micro controller(pratheesh)Micro controller(pratheesh)
Micro controller(pratheesh)
 
Assembly Language Programming By Ytha Yu, Charles Marut Chap 8 (The Stack and...
Assembly Language Programming By Ytha Yu, Charles Marut Chap 8 (The Stack and...Assembly Language Programming By Ytha Yu, Charles Marut Chap 8 (The Stack and...
Assembly Language Programming By Ytha Yu, Charles Marut Chap 8 (The Stack and...
 

Recently uploaded

Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxAsutosh Ranjan
 
Introduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxIntroduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxupamatechverse
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Christo Ananth
 
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICSHARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICSRajkumarAkumalla
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxJoão Esperancinha
 
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...ranjana rawat
 
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Serviceranjana rawat
 
Introduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxIntroduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxupamatechverse
 
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSMANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSSIVASHANKAR N
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130Suhani Kapoor
 
Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxupamatechverse
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...
IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...
IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...RajaP95
 
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCall Girls in Nagpur High Profile
 
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Call Girls in Nagpur High Profile
 
Porous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingPorous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingrakeshbaidya232001
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVRajaP95
 

Recently uploaded (20)

Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptx
 
Introduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxIntroduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptx
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
 
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICSHARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
 
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
 
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
 
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
 
Introduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxIntroduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptx
 
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINEDJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
 
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSMANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
 
Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptx
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
 
IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...
IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...
IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...
 
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
 
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
 
Porous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingPorous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writing
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
 

Stack and subroutine

  • 1. Stack The stack is a group of memory location in the R/W memory defined by the programmer that is used for temporary storage of binary information during the execution of a program. 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, FF00H Stack can be initialized at any where in the memory location within the R/W memory using above procedure. But, As data are filled into the stack memory in reverse direction, it is better to initialize start of the stack memory at the highest memory location in order to get maximum stack memory and avoid overlapping the stack memory with the program. So, Stack is initialized at FFFFH which is the last memory in the R/W. LXI SP, FFFFH.
  • 2. One stack is initialized in the R/W memory, storing data into the stack, start from previous memory location of the initialization value. That is if stack is initialized as LXI SP, FFFF So first data is stored into FFFE location, next data is stored in FFFD location and so on in reverse order. 11 22 33 44 55 66 77 R/W memory FFF9 FFFA FFFB FFFC FFFD FFFE FFFFSP LXI SP, FFFF
  • 3. Storing data into stack memory The 8085 microprocessor has two instructions PUSH and POP for storing data into stack memory and retrieving it back respectively in LIFO. Both instruction works with register pair only. PUSH: It is used to store the content of register pair onto the stack memory in reverse order (decreasing memory address) POP: It is used to transfer data bytes from stack memory into the respective register pair. 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
  • 4. 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 FFFFSP LXI SP, FFFF A F B 11 C 22 D E H L SP
  • 5. 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 FFFFSP LXI SP, FFFF A F B 11 C 22 D 33 E 44 H L SP
  • 6. 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 FFFFSP LXI SP, FFFF
  • 7. 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’’.
  • 8. Subroutine A subroutine is group of instruction written separately from the main program to perform a function that occurs repeatedly in the main program When a main program calls a subroutine the pro gram execution is transferred to the subroutine. After the completion of the subroutine ,the program execution returns to the main program. So, The microprocessor uses the stack to store the return address of the subroutine.
  • 9. The 8085 has two instructions for dealing with subroutines. o The CALL instruction is used in the main program to redirect program execution to the subroutine. o The RET instruction is used at the last of the subroutine to return the execution to the calling routine. When a subroutine is called, address of the next instruction following to the call instruction is PUSHed into the stack automatically and program execution is transferred to the subroutine address. When RET instruction is executed at the end of the subroutine, the returned address which was stored into the stack memory during CALL, is retrieved into PC and execution is resumed in the main program. CALL instruction must be used in conjunction with the RET instruction in the subroutine. During Subroutine Stack must be initialized.
  • 10. CALL 4000H o 3-byte instruction. o Push the address of the instruction immediately following the CALL onto the stack and decrement the stack pointer register by two. o Load the program counter with the 16-bit address supplied with the CALL instruction. o Jump Unconditionally to memory location.
  • 11. 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
  • 12. 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
  • 13. Call instruction sequence: Call is a three byte instruction. So, depending upon the byte size of the instruction it has opcode fetch, Memory read and memory read. Again, before jumping to the subroutine, return address (i.e. content of PC value which is the address of next instruction following to the call is to be stored onto the stack memory). So, along with the above three M/C, it has also mem. Write ( storing higher address onto stack) and mem. Write ( storing lower address onto the stack) M/C. CALL 9000 8020: opcode of call (OF) 8021: 00 (MR) 8022: 90 (MR) PC 8023: Next Instruction Value of PC is the return address after completing the subroutine. So, PC value is to be stored into the stack memory before going to the subroutine using automatic PUSH operation.
  • 14. M/C SP FFFF Add. Bus AB PC PCH PCL 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 90 00 M4 MW FFFD SP-2 FFFE 8023 80 PCH 90 00 M5 MW FFFD FFFD 8023 23 PCL 90 00 OF Of 1st inst. In Subroutine FFFD 9000 9001 90 00 Mem Add. Code 8020 CD 8021 00 8022 90 8023 Opcode of next PC 23 80 ---------- FFFD FFFE FFFF Stack
  • 15. RET instruction sequence: RET is a 1-byte instruction. So, depending upon the byte size, it has only OF M/C. But when RET instruction is executed, program sequence is transferred to the main program by POPing the return address to PC from stack memory which was saved during call. Then, execution resume from this address. So, Two extra memory read is required to get return address. M/C of RET are OF, MR (reading lower value of return address from stack), MR (reading higher value of return address from stack) 900F: RET instruction opcode (OF) PC 9010:
  • 16. M/C SP FFFD AB PC PCH PCL 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 OF of inst. Next to CALL FFFF 8023 8024 opcode 80 23 900F C9 FFFD 23 FFFE 80 FFFF ------- Stack SP
  • 17. Restart and Conditional CALL and Return Instruction: In addition to the unconditional call and return instruction , the 8085 instruction set has eight restart instruction and eight conditional call and return instruction. Restart (RST) instruction: RST instruction are 1-byte call instruction that transfer the program Sequence to a specific location on ooH memory page. They are executed the same way as CALL instruction. When RST instruction is executed, the 8085 stores the content of the PC (i.e. address of the next instruction) on the top of the stack and transfers the program sequence to the fixed restart location. These instructions are generally used in conjunction with the interrupt process. RST 0 CALL 0000 RST 1 CALL 0008 RST 2 CALL 0010 RST 3 CALL 0018 RST 4 CALL 0020 RST 5 CALL 0028 RST 6 CALL 0030 RST 7 CALL 0038
  • 18. Conditional CALL and RET instruction: Conditional call and return instruction are based on the four conditional flag bits: CY, P, Z, S. In case of conditional CALL, program is transferred to the subroutine if the condition is met, otherwise main program execution continue. In case of conditional RET, the sequence returns to the main program if the condition is met, otherwise execution in the subroutine continued. Last instruction of any subroutine must be unconditional RET otherwise sequence can’t return to the main program if condition not met. Unconditional CALL: Unconditional RET: CC Call subroutine if CY=1 RC Return from subroutine if CY=1 CNC Call subroutine if CY=0 RNC Return from subroutine if CY=0 CZ Call subroutine if Z=1 RZ Return from subroutine if Z=1 CNZ Call subroutine if Z=0 RNZ Return from subroutine if Z=0 CM Call subroutine if S=1 RM Return from subroutine if S=1 CP Call subroutine if S=0 RP Return from subroutine if S=0 CPE Call subroutine if P=1 RPE Return from subroutine if P=1 CPO Call subroutine if P=0 RPO Return from subroutine if P=0
  • 19. Multiple Calling and Nesting of a subroutine
  • 20. Multiple Ending of a subroutine