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

Chapter 8
Chapter 8Chapter 8
Chapter 8deval patel
ย 
8051 Addressing Modes
8051 Addressing Modes8051 Addressing Modes
8051 Addressing ModesSenthil Kumar
ย 
LOGICAL OPERATIONS IN 8085 MICROPROCESSOR
LOGICAL OPERATIONS IN 8085 MICROPROCESSORLOGICAL OPERATIONS IN 8085 MICROPROCESSOR
LOGICAL OPERATIONS IN 8085 MICROPROCESSORRamaPrabha24
ย 
8051 memory
8051 memory8051 memory
8051 memoryMayank Garg
ย 
Stack in 8085 microprocessor
Stack in 8085 microprocessorStack in 8085 microprocessor
Stack in 8085 microprocessorhepzijustin
ย 
Addressing modes 8085
Addressing modes 8085Addressing modes 8085
Addressing modes 8085ShivamSood22
ย 
8085 micro processor
8085 micro processor8085 micro processor
8085 micro processorPoojith Chowdhary
ย 
8051 Microcontroller I/O ports
8051 Microcontroller I/O ports8051 Microcontroller I/O ports
8051 Microcontroller I/O portsanishgoel
ย 
program status word
program status wordprogram status word
program status wordsheetalverma38
ย 
programming techniques
programming techniquesprogramming techniques
programming techniquesRamaPrabha24
ย 
Interrupts in pic
Interrupts in picInterrupts in pic
Interrupts in picv Kalairajan
ย 
Counters & time delay
Counters & time delayCounters & time delay
Counters & time delayHemant Chetwani
ย 
Interrupts of 8085
Interrupts of 8085Interrupts of 8085
Interrupts of 8085ShivamSood22
ย 
Chapter 7 - Programming Techniques with Additional Instructions
Chapter 7 - Programming Techniques with Additional InstructionsChapter 7 - Programming Techniques with Additional Instructions
Chapter 7 - Programming Techniques with Additional Instructionscmkandemir
ย 
Programmable Peripheral Interface 8255
 Programmable Peripheral Interface   8255 Programmable Peripheral Interface   8255
Programmable Peripheral Interface 8255Dr.P.Parandaman
ย 

What's hot (20)

Chapter 8
Chapter 8Chapter 8
Chapter 8
ย 
8051 Addressing Modes
8051 Addressing Modes8051 Addressing Modes
8051 Addressing Modes
ย 
8051 serial communication-UART
8051 serial communication-UART8051 serial communication-UART
8051 serial communication-UART
ย 
LOGICAL OPERATIONS IN 8085 MICROPROCESSOR
LOGICAL OPERATIONS IN 8085 MICROPROCESSORLOGICAL OPERATIONS IN 8085 MICROPROCESSOR
LOGICAL OPERATIONS IN 8085 MICROPROCESSOR
ย 
8051 memory
8051 memory8051 memory
8051 memory
ย 
Stack in 8085 microprocessor
Stack in 8085 microprocessorStack in 8085 microprocessor
Stack in 8085 microprocessor
ย 
Programming with 8085
Programming with 8085Programming with 8085
Programming with 8085
ย 
Addressing modes 8085
Addressing modes 8085Addressing modes 8085
Addressing modes 8085
ย 
8085 micro processor
8085 micro processor8085 micro processor
8085 micro processor
ย 
8051 Microcontroller I/O ports
8051 Microcontroller I/O ports8051 Microcontroller I/O ports
8051 Microcontroller I/O ports
ย 
program status word
program status wordprogram status word
program status word
ย 
Addressing modes
Addressing modesAddressing modes
Addressing modes
ย 
programming techniques
programming techniquesprogramming techniques
programming techniques
ย 
Interrupts in pic
Interrupts in picInterrupts in pic
Interrupts in pic
ย 
Counters & time delay
Counters & time delayCounters & time delay
Counters & time delay
ย 
8085 instruction set
8085 instruction set8085 instruction set
8085 instruction set
ย 
8259 a
8259 a8259 a
8259 a
ย 
Interrupts of 8085
Interrupts of 8085Interrupts of 8085
Interrupts of 8085
ย 
Chapter 7 - Programming Techniques with Additional Instructions
Chapter 7 - Programming Techniques with Additional InstructionsChapter 7 - Programming Techniques with Additional Instructions
Chapter 7 - Programming Techniques with Additional Instructions
ย 
Programmable Peripheral Interface 8255
 Programmable Peripheral Interface   8255 Programmable Peripheral Interface   8255
Programmable Peripheral Interface 8255
ย 

Viewers also liked

Interrupt
InterruptInterrupt
Interruptroshan_rawat
ย 
1206 Interrupts Of 8085
1206 Interrupts Of 80851206 Interrupts Of 8085
1206 Interrupts Of 8085techbed
ย 
Lica 7th chapter slides
Lica 7th chapter slidesLica 7th chapter slides
Lica 7th chapter slidesSIVA NAGENDRA REDDY
ย 
Uart
UartUart
Uartcs1090211
ย 
Stack in microprocessor 8085(presantation)
Stack in microprocessor 8085(presantation)Stack in microprocessor 8085(presantation)
Stack in microprocessor 8085(presantation)Safin Biswas
ย 
Interrupts of microprocessor 8085
Interrupts of microprocessor  8085Interrupts of microprocessor  8085
Interrupts of microprocessor 8085mujeebkhanelectronic
ย 
8251 USART
8251 USART8251 USART
8251 USARTcoolsdhanesh
ย 
8259 Programmable Interrupt Controller by vijay
8259 Programmable Interrupt Controller by vijay8259 Programmable Interrupt Controller by vijay
8259 Programmable Interrupt Controller by vijayVijay Kumar
ย 
8051 serial communication
8051 serial communication8051 serial communication
8051 serial communicationasteriskbimal
ย 

Viewers also liked (11)

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
ย 
Stack in microprocessor 8085(presantation)
Stack in microprocessor 8085(presantation)Stack in microprocessor 8085(presantation)
Stack in microprocessor 8085(presantation)
ย 
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
ย 
class-Stacks.pptx
class-Stacks.pptxclass-Stacks.pptx
class-Stacks.pptxssuser2efca7
ย 
Stack and subroutine
Stack and subroutineStack and subroutine
Stack and subroutinemilandhara
ย 
module-2.pptx
module-2.pptxmodule-2.pptx
module-2.pptxAmbika Naik
ย 
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
ย 
Topic 1
Topic 1Topic 1
Topic 1nishantiitp
ย 
(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
ย 
Unit 4.pptx
Unit 4.pptxUnit 4.pptx
Unit 4.pptxBLACKSPAROW
ย 
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

8085 interrupts
8085 interrupts8085 interrupts
8085 interruptsdeval patel
ย 
8254 PIT
8254 PIT8254 PIT
8254 PITdeval patel
ย 
8255 PPI
8255 PPI8255 PPI
8255 PPIdeval patel
ย 
8085 intro
8085 intro8085 intro
8085 introdeval patel
ย 
8155 GPPI
8155 GPPI8155 GPPI
8155 GPPIdeval patel
ย 
8279 PKDI
8279 PKDI8279 PKDI
8279 PKDIdeval patel
ย 
8085 Microprocessor Architecture
8085 Microprocessor Architecture8085 Microprocessor Architecture
8085 Microprocessor Architecturedeval patel
ย 
8085 Architecture
8085 Architecture8085 Architecture
8085 Architecturedeval patel
ย 
Memory & I/O interfacing
Memory & I/O  interfacingMemory & I/O  interfacing
Memory & I/O interfacingdeval patel
ย 
Interrupts
InterruptsInterrupts
Interruptsdeval patel
ย 
Lecture 1
Lecture 1Lecture 1
Lecture 1deval patel
ย 

More from deval patel (11)

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
ย 
Lecture 1
Lecture 1Lecture 1
Lecture 1
ย 

Recently uploaded

Thermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.pptThermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.pptDineshKumar4165
ย 
Unit 1 - Soil Classification and Compaction.pdf
Unit 1 - Soil Classification and Compaction.pdfUnit 1 - Soil Classification and Compaction.pdf
Unit 1 - Soil Classification and Compaction.pdfRagavanV2
ย 
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
ย 
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXssuser89054b
ย 
Double rodded leveling 1 pdf activity 01
Double rodded leveling 1 pdf activity 01Double rodded leveling 1 pdf activity 01
Double rodded leveling 1 pdf activity 01KreezheaRecto
ย 
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...roncy bisnoi
ย 
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance BookingCall Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance Bookingroncy bisnoi
ย 
data_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdfdata_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdfJiananWang21
ย 
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
ย 
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...ranjana rawat
ย 
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...SUHANI PANDEY
ย 
Thermal Engineering Unit - I & II . ppt
Thermal Engineering  Unit - I & II . pptThermal Engineering  Unit - I & II . ppt
Thermal Engineering Unit - I & II . pptDineshKumar4165
ย 
Double Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torqueDouble Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torqueBhangaleSonal
ย 
NFPA 5000 2024 standard .
NFPA 5000 2024 standard                                  .NFPA 5000 2024 standard                                  .
NFPA 5000 2024 standard .DerechoLaboralIndivi
ย 
Thermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - VThermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - VDineshKumar4165
ย 
University management System project report..pdf
University management System project report..pdfUniversity management System project report..pdf
University management System project report..pdfKamal Acharya
ย 
chapter 5.pptx: drainage and irrigation engineering
chapter 5.pptx: drainage and irrigation engineeringchapter 5.pptx: drainage and irrigation engineering
chapter 5.pptx: drainage and irrigation engineeringmulugeta48
ย 
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
ย 

Recently uploaded (20)

Thermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.pptThermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.ppt
ย 
Unit 1 - Soil Classification and Compaction.pdf
Unit 1 - Soil Classification and Compaction.pdfUnit 1 - Soil Classification and Compaction.pdf
Unit 1 - Soil Classification and Compaction.pdf
ย 
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, ...
ย 
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
ย 
Double rodded leveling 1 pdf activity 01
Double rodded leveling 1 pdf activity 01Double rodded leveling 1 pdf activity 01
Double rodded leveling 1 pdf activity 01
ย 
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
ย 
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance BookingCall Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
ย 
data_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdfdata_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdf
ย 
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)
ย 
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
ย 
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
ย 
Thermal Engineering Unit - I & II . ppt
Thermal Engineering  Unit - I & II . pptThermal Engineering  Unit - I & II . ppt
Thermal Engineering Unit - I & II . ppt
ย 
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
ย 
Double Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torqueDouble Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torque
ย 
NFPA 5000 2024 standard .
NFPA 5000 2024 standard                                  .NFPA 5000 2024 standard                                  .
NFPA 5000 2024 standard .
ย 
Thermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - VThermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - V
ย 
University management System project report..pdf
University management System project report..pdfUniversity management System project report..pdf
University management System project report..pdf
ย 
chapter 5.pptx: drainage and irrigation engineering
chapter 5.pptx: drainage and irrigation engineeringchapter 5.pptx: drainage and irrigation engineering
chapter 5.pptx: drainage and irrigation engineering
ย 
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
ย 
Water Industry Process Automation & Control Monthly - April 2024
Water Industry Process Automation & Control Monthly - April 2024Water Industry Process Automation & Control Monthly - April 2024
Water Industry Process Automation & Control Monthly - April 2024
ย 

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.