SlideShare a Scribd company logo
1 of 22
Chapter 9
Stack and Subroutines
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
defines the bottom of the stack
and the stack grows up into
reducing address range.
Memory
Bottom
of the
Stack
The Stack
grows
backwards
into memory
The Stack
• Given that the stack grows backwards into
memory, it is customary to place the bottom of
the stack at the end of memory to keep it as far
away from user programs as possible.
• In the 8085, the stack is defined by setting the
SP (Stack Pointer) register.
LXI SP, FFFFH
• This sets the Stack Pointer to location FFFFH
(end of memory for the 8085).
Saving Information on the Stack
• Information is saved on the stack by PUSHing it
on.
– It is retrieved from the stack by POPing it off.
• The 8085 provides two instructions: PUSH and
POP for storing information on the stack and
retrieving it back.
– Both PUSH and POP work with register pairs
ONLY.
The PUSH Instruction
• PUSH B
– Decrement SP
– Copy the contents of register B to the memory
location pointed to by SP
– Decrement SP
– Copy the contents of register C to the memory
location pointed to by SP
B C
SPFFFF
FFFE
FFFD
FFFC
FFFB
F312
F3
12
The POP Instruction
• POP D
– 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
SP
FFFF
FFFE
FFFD
FFFC
FFFB
F312
F3
12
Operation of the Stack
• 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 the
stack and then the pointer is incremented.
• The SP pointer always points to “the top of the
stack”.
LIFO
• The order of PUSHs and POPs must be opposite of each
other in order to retrieve information back into its original
location.
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.
The PSW Register Pair
• The 8085 recognizes one additional register pair
called the PSW (Program Status Word).
– This register pair is made up of the Accumulator
and the Flags registers.
• It is possible to push the PSW onto the stack, do
whatever operations are needed, then POP it off
of the stack.
– The result is that the contents of the Accumulator
and the status of the Flags are returned to what
they were before the operations were executed.
Problem Statement
•Clear all the flags,
•Load 00h in the accumulator and
demonstrate that the zero flag is
not affected by the data transfer
instruction
•Logically OR the accumulator with
itself to set the Zero flag, and
display the flag at 01H or store
all the flags on the stack
Program
•LXI SP 0100h
•MVI L 00h
•PUSH H
•POP PSW
•MVI A 00h
•PUSH PSW
•POP H
•MOV A L
•OUT 01h
•MVI A 00h
•ORA A
•PUSH PSW
•POP H
•MOV A L
•ANI 40h
•OUT 01h
•HLT
STACK USAGE
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.
• In Assembly language, a subroutine can exist
anywhere in the code.
– However, it is customary to place subroutines
separately from the main program.
Subroutines
• The 8085 has two instructions for dealing with
subroutines.
– The CALL instruction is used to redirect program
execution to the subroutine.
– The RTE instruction is used to return the
execution to the calling routine.
The CALL Instruction
• CALL 4000H
– Push the address of the instruction immediately
following the CALL onto the stack
– Load the program counter with the 16-bit
address supplied with the CALL instruction.
PC
SPFFFF
FFFE
FFFD
FFFC
FFFB
2 0 0 3
03
20
2000 CALL 4000
2003
The RTE Instruction
• RTE
– Retrieve the return address from the top of the
stack
– Load the program counter with the return
address.
PC
FFFF
FFFE
FFFD
FFFC
FFFB
2 0 0 3
03
20
4014 . . .
4015 RTE SP
Cautions
• 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.
• The RTE instruction takes the contents of the two
memory locations at the top of the stack and
uses these as the return address.
– Do not modify the stack pointer in a subroutine.
You will loose the return address.
Passing Data to a Subroutine
• In Assembly Language data is passed to a
subroutine through registers.
– The data is stored in one of the registers by the
calling program and the subroutine uses the value
from the register.
• The other possibility is to use agreed upon
memory locations.
– The calling program stores the data in the memory
location and the subroutine retrieves the data from
the location and uses it.
Call by Reference and Call by Value
• If the subroutine performs operations on the
contents of the registers, then these
modifications will be transferred back to the
calling program upon returning from a
subroutine.
– Call by reference
• If this is not desired, the subroutine should PUSH
all the registers it needs on the stack on entry
and POP them on return.
– The original values are restored before execution
returns to the calling program.
Cautions with PUSH and POP
• PUSH and POP should be used in opposite
order.
• There has to be as many POP’s as there are
PUSH’s.
– If not, the RET statement will pick up the wrong
information from the top of the stack and the
program will fail.
• It is not advisable to place PUSH or POP inside a
loop.
Conditional CALL and RET Instructions
• The 8085 supports conditional CALL and
conditional RTE instructions.
– The same conditions used with conditional JUMP
instructions can be used.
– CC, call subroutine if Carry flag is set.
– CNC, call subroutine if Carry flag is not set
– RC, return from subroutine if Carry flag is set
– RNC, return from subroutine if Carry flag is not set
– Etc.
A Proper Subroutine
• According to Software Engineering practices, a
proper subroutine:
– Is only entered with a CALL and exited with an
RTE
– Has a single entry point
• Do not use a CALL statement to jump into different
points of the same subroutine.
– Has a single exit point
• There should be one return statement from any
subroutine.
• Following these rules, there should not be any
confusion with PUSH and POP usage.

More Related Content

What's hot (20)

8086 micro processor
8086 micro processor8086 micro processor
8086 micro processor
 
8085 instruction set
8085 instruction set8085 instruction set
8085 instruction set
 
Architecture of 8086 Microprocessor
Architecture of 8086 Microprocessor  Architecture of 8086 Microprocessor
Architecture of 8086 Microprocessor
 
Unit 2 mpmc
Unit 2 mpmcUnit 2 mpmc
Unit 2 mpmc
 
Memory organization of 8051
Memory organization of 8051Memory organization of 8051
Memory organization of 8051
 
8085 microprocessor
8085 microprocessor8085 microprocessor
8085 microprocessor
 
8051 timers
8051 timers8051 timers
8051 timers
 
8051 Microcontroller PPT's By Er. Swapnil Kaware
8051 Microcontroller PPT's By Er. Swapnil Kaware8051 Microcontroller PPT's By Er. Swapnil Kaware
8051 Microcontroller PPT's By Er. Swapnil Kaware
 
Lecture 3 instruction set
Lecture 3  instruction setLecture 3  instruction set
Lecture 3 instruction set
 
Cache memory
Cache memoryCache memory
Cache memory
 
8085 MICROPROCESSOR ARCHITECTURE AND ITS OPERATIONS
8085 MICROPROCESSOR ARCHITECTURE AND ITS OPERATIONS8085 MICROPROCESSOR ARCHITECTURE AND ITS OPERATIONS
8085 MICROPROCESSOR ARCHITECTURE AND ITS OPERATIONS
 
8085 microprocessor ramesh gaonkar
8085 microprocessor   ramesh gaonkar8085 microprocessor   ramesh gaonkar
8085 microprocessor ramesh gaonkar
 
Microprocessor 80386
Microprocessor 80386Microprocessor 80386
Microprocessor 80386
 
Stack in 8085 microprocessor
Stack in 8085 microprocessorStack in 8085 microprocessor
Stack in 8085 microprocessor
 
Control Memory
Control MemoryControl Memory
Control Memory
 
Instruction Set of 8051 Microcontroller
Instruction Set of 8051 MicrocontrollerInstruction Set of 8051 Microcontroller
Instruction Set of 8051 Microcontroller
 
LOGICAL OPERATIONS IN 8085 MICROPROCESSOR
LOGICAL OPERATIONS IN 8085 MICROPROCESSORLOGICAL OPERATIONS IN 8085 MICROPROCESSOR
LOGICAL OPERATIONS IN 8085 MICROPROCESSOR
 
Timers
TimersTimers
Timers
 
8051 Timers / Counters
8051 Timers / Counters8051 Timers / Counters
8051 Timers / Counters
 
Registers
RegistersRegisters
Registers
 

Viewers also liked

Viewers also liked (13)

Interrupt
InterruptInterrupt
Interrupt
 
1206 Interrupts Of 8085
1206 Interrupts Of 80851206 Interrupts Of 8085
1206 Interrupts Of 8085
 
Lica 7th chapter slides
Lica 7th chapter slidesLica 7th chapter slides
Lica 7th chapter slides
 
Uart
UartUart
Uart
 
8051 serial communication-UART
8051 serial communication-UART8051 serial communication-UART
8051 serial communication-UART
 
Stack in microprocessor 8085(presantation)
Stack in microprocessor 8085(presantation)Stack in microprocessor 8085(presantation)
Stack in microprocessor 8085(presantation)
 
8259 a
8259 a8259 a
8259 a
 
Interrupts of microprocessor 8085
Interrupts of microprocessor  8085Interrupts of microprocessor  8085
Interrupts of microprocessor 8085
 
8251 USART
8251 USART8251 USART
8251 USART
 
8259 Programmable Interrupt Controller by vijay
8259 Programmable Interrupt Controller by vijay8259 Programmable Interrupt Controller by vijay
8259 Programmable Interrupt Controller by vijay
 
USART
USARTUSART
USART
 
8051 serial communication
8051 serial communication8051 serial communication
8051 serial communication
 
UART
UARTUART
UART
 

Similar to Chapter 9

Microprocessor and Microcontroller lec4
Microprocessor and Microcontroller lec4Microprocessor and Microcontroller lec4
Microprocessor and Microcontroller lec4Ameen San
 
Stacks & Subroutines.ppt.pptx
Stacks & Subroutines.ppt.pptxStacks & Subroutines.ppt.pptx
Stacks & Subroutines.ppt.pptxssuserdcfc6d
 
8085-Programming-II-mod-1.pptx
8085-Programming-II-mod-1.pptx8085-Programming-II-mod-1.pptx
8085-Programming-II-mod-1.pptxAsmita Bhagdikar
 
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
 
Microprocessor Part 4
Microprocessor    Part 4Microprocessor    Part 4
Microprocessor Part 4Sajan Agrawal
 
Stack and subroutine
Stack and subroutineStack and subroutine
Stack and subroutinemilandhara
 
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
 
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
 
(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
 
Computer Architecture - Program Execution
Computer Architecture - Program ExecutionComputer Architecture - Program Execution
Computer Architecture - Program ExecutionVarun Bhargava
 
lec15_x86procedure_4up.pdf
lec15_x86procedure_4up.pdflec15_x86procedure_4up.pdf
lec15_x86procedure_4up.pdfhasan58964
 
Central processing unit
Central processing unitCentral processing unit
Central processing unitHeman Pathak
 

Similar to Chapter 9 (20)

Microprocessor and Microcontroller lec4
Microprocessor and Microcontroller lec4Microprocessor and Microcontroller lec4
Microprocessor and Microcontroller lec4
 
Stacks & Subroutines.ppt.pptx
Stacks & Subroutines.ppt.pptxStacks & Subroutines.ppt.pptx
Stacks & Subroutines.ppt.pptx
 
Lec04
Lec04Lec04
Lec04
 
Lec04
Lec04Lec04
Lec04
 
8085-Programming-II-mod-1.pptx
8085-Programming-II-mod-1.pptx8085-Programming-II-mod-1.pptx
8085-Programming-II-mod-1.pptx
 
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)
 
Microprocessor Part 4
Microprocessor    Part 4Microprocessor    Part 4
Microprocessor Part 4
 
class-Stacks.pptx
class-Stacks.pptxclass-Stacks.pptx
class-Stacks.pptx
 
Stack and subroutine
Stack and subroutineStack and subroutine
Stack and subroutine
 
module-2.pptx
module-2.pptxmodule-2.pptx
module-2.pptx
 
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
 
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...
 
Topic 1
Topic 1Topic 1
Topic 1
 
Subroutine
SubroutineSubroutine
Subroutine
 
Uc 2(vii)
Uc 2(vii)Uc 2(vii)
Uc 2(vii)
 
(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
 
Unit 4.pptx
Unit 4.pptxUnit 4.pptx
Unit 4.pptx
 
Computer Architecture - Program Execution
Computer Architecture - Program ExecutionComputer Architecture - Program Execution
Computer Architecture - Program Execution
 
lec15_x86procedure_4up.pdf
lec15_x86procedure_4up.pdflec15_x86procedure_4up.pdf
lec15_x86procedure_4up.pdf
 
Central processing unit
Central processing unitCentral processing unit
Central processing unit
 

More from deval patel

More from deval patel (12)

8085 interrupts
8085 interrupts8085 interrupts
8085 interrupts
 
8254 PIT
8254 PIT8254 PIT
8254 PIT
 
8255 PPI
8255 PPI8255 PPI
8255 PPI
 
8085 intro
8085 intro8085 intro
8085 intro
 
8155 GPPI
8155 GPPI8155 GPPI
8155 GPPI
 
8279 PKDI
8279 PKDI8279 PKDI
8279 PKDI
 
8085 Microprocessor Architecture
8085 Microprocessor Architecture8085 Microprocessor Architecture
8085 Microprocessor Architecture
 
8085 Architecture
8085 Architecture8085 Architecture
8085 Architecture
 
Memory & I/O interfacing
Memory & I/O  interfacingMemory & I/O  interfacing
Memory & I/O interfacing
 
Interrupts
InterruptsInterrupts
Interrupts
 
Chapter 8
Chapter 8Chapter 8
Chapter 8
 
Lecture 1
Lecture 1Lecture 1
Lecture 1
 

Recently uploaded

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
 
(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
 
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
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Dr.Costas Sachpazis
 
Online banking management system project.pdf
Online banking management system project.pdfOnline banking management system project.pdf
Online banking management system project.pdfKamal Acharya
 
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...ranjana rawat
 
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
Introduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxIntroduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxupamatechverse
 
Porous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingPorous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingrakeshbaidya232001
 
Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)simmis5
 
AKTU Computer Networks notes --- Unit 3.pdf
AKTU Computer Networks notes ---  Unit 3.pdfAKTU Computer Networks notes ---  Unit 3.pdf
AKTU Computer Networks notes --- Unit 3.pdfankushspencer015
 
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and workingUNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and workingrknatarajan
 
Russian Call Girls in Nagpur Grishma Call 7001035870 Meet With Nagpur Escorts
Russian Call Girls in Nagpur Grishma Call 7001035870 Meet With Nagpur EscortsRussian Call Girls in Nagpur Grishma Call 7001035870 Meet With Nagpur Escorts
Russian Call Girls in Nagpur Grishma Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Christo Ananth
 
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Dr.Costas Sachpazis
 
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
 
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 

Recently uploaded (20)

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
 
(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...
 
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
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
 
Online banking management system project.pdf
Online banking management system project.pdfOnline banking management system project.pdf
Online banking management system project.pdf
 
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
 
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
 
Introduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxIntroduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptx
 
Porous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingPorous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writing
 
Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)
 
AKTU Computer Networks notes --- Unit 3.pdf
AKTU Computer Networks notes ---  Unit 3.pdfAKTU Computer Networks notes ---  Unit 3.pdf
AKTU Computer Networks notes --- Unit 3.pdf
 
Roadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and RoutesRoadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and Routes
 
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and workingUNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
 
Russian Call Girls in Nagpur Grishma Call 7001035870 Meet With Nagpur Escorts
Russian Call Girls in Nagpur Grishma Call 7001035870 Meet With Nagpur EscortsRussian Call Girls in Nagpur Grishma Call 7001035870 Meet With Nagpur Escorts
Russian Call Girls in Nagpur Grishma Call 7001035870 Meet With Nagpur Escorts
 
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
 
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
 
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
 
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...
 
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
 

Chapter 9

  • 1. Chapter 9 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 defines the bottom of the stack and the stack grows up into reducing address range. Memory Bottom of the Stack The Stack grows backwards into memory
  • 3. The Stack • Given that the stack grows backwards into memory, it is customary to place the bottom of the stack at the end of memory to keep it as far away from user programs as possible. • In the 8085, the stack is defined by setting the SP (Stack Pointer) register. LXI SP, FFFFH • This sets the Stack Pointer to location FFFFH (end of memory for the 8085).
  • 4. Saving Information on the Stack • Information is saved on the stack by PUSHing it on. – It is retrieved from the stack by POPing it off. • The 8085 provides two instructions: PUSH and POP for storing information on the stack and retrieving it back. – Both PUSH and POP work with register pairs ONLY.
  • 5. The PUSH Instruction • PUSH B – Decrement SP – Copy the contents of register B to the memory location pointed to by SP – Decrement SP – Copy the contents of register C to the memory location pointed to by SP B C SPFFFF FFFE FFFD FFFC FFFB F312 F3 12
  • 6. The POP Instruction • POP D – 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 SP FFFF FFFE FFFD FFFC FFFB F312 F3 12
  • 7. Operation of the Stack • 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 the stack and then the pointer is incremented. • The SP pointer always points to “the top of the stack”.
  • 8. LIFO • The order of PUSHs and POPs must be opposite of each other in order to retrieve information back into its original location. 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 Pair • The 8085 recognizes one additional register pair called the PSW (Program Status Word). – This register pair is made up of the Accumulator and the Flags registers. • It is possible to push the PSW onto the stack, do whatever operations are needed, then POP it off of the stack. – The result is that the contents of the Accumulator and the status of the Flags are returned to what they were before the operations were executed.
  • 10. Problem Statement •Clear all the flags, •Load 00h in the accumulator and demonstrate that the zero flag is not affected by the data transfer instruction •Logically OR the accumulator with itself to set the Zero flag, and display the flag at 01H or store all the flags on the stack
  • 11. Program •LXI SP 0100h •MVI L 00h •PUSH H •POP PSW •MVI A 00h •PUSH PSW •POP H •MOV A L •OUT 01h •MVI A 00h •ORA A •PUSH PSW •POP H •MOV A L •ANI 40h •OUT 01h •HLT
  • 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. • In Assembly language, a subroutine can exist anywhere in the code. – However, it is customary to place subroutines separately from the main program.
  • 14. Subroutines • The 8085 has two instructions for dealing with subroutines. – The CALL instruction is used to redirect program execution to the subroutine. – The RTE instruction is used to return the execution to the calling routine.
  • 15. The CALL Instruction • CALL 4000H – Push the address of the instruction immediately following the CALL onto the stack – Load the program counter with the 16-bit address supplied with the CALL instruction. PC SPFFFF FFFE FFFD FFFC FFFB 2 0 0 3 03 20 2000 CALL 4000 2003
  • 16. The RTE Instruction • RTE – Retrieve the return address from the top of the stack – Load the program counter with the return address. PC FFFF FFFE FFFD FFFC FFFB 2 0 0 3 03 20 4014 . . . 4015 RTE SP
  • 17. Cautions • 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. • The RTE instruction takes the contents of the two memory locations at the top of the stack and uses these as the return address. – Do not modify the stack pointer in a subroutine. You will loose the return address.
  • 18. Passing Data to a Subroutine • In Assembly Language data is passed to a subroutine through registers. – The data is stored in one of the registers by the calling program and the subroutine uses the value from the register. • The other possibility is to use agreed upon memory locations. – The calling program stores the data in the memory location and the subroutine retrieves the data from the location and uses it.
  • 19. Call by Reference and Call by Value • If the subroutine performs operations on the contents of the registers, then these modifications will be transferred back to the calling program upon returning from a subroutine. – Call by reference • If this is not desired, the subroutine should PUSH all the registers it needs on the stack on entry and POP them on return. – The original values are restored before execution returns to the calling program.
  • 20. Cautions with PUSH and POP • PUSH and POP should be used in opposite order. • There has to be as many POP’s as there are PUSH’s. – If not, the RET statement will pick up the wrong information from the top of the stack and the program will fail. • It is not advisable to place PUSH or POP inside a loop.
  • 21. Conditional CALL and RET Instructions • The 8085 supports conditional CALL and conditional RTE instructions. – The same conditions used with conditional JUMP instructions can be used. – CC, call subroutine if Carry flag is set. – CNC, call subroutine if Carry flag is not set – RC, return from subroutine if Carry flag is set – RNC, return from subroutine if Carry flag is not set – Etc.
  • 22. A Proper Subroutine • According to Software Engineering practices, a proper subroutine: – Is only entered with a CALL and exited with an RTE – Has a single entry point • Do not use a CALL statement to jump into different points of the same subroutine. – Has a single exit point • There should be one return statement from any subroutine. • Following these rules, there should not be any confusion with PUSH and POP usage.