SlideShare a Scribd company logo
1 of 35
Stacks &
Subroutines
o The stack is an area of memory identified
by the programmer for temporary storage
of information.
o The stack is a LIFO (Last In First Out. )
structure.
o The stack normally grows backwards
into memory.
o In other words, the programmer
defines the bottom of the stack
and the stack grows up into
reducing address range.
The Stack
o 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.
o In the 8085, the stack is defined by setting the SP
(Stack Pointer) register.
LXI SP, FFFFH
o This sets the Stack Pointer to location FFFFH (end of
memory for the 8085).
The Stack
o Information is saved on the stack by PUSHing
it on.
o It is retrieved from the stack by POPing it
off.
o The 8085 provides two instructions: PUSH
and POP for storing information on the stack
and retrieving it back.
o Both PUSH and POP work with register
pairs ONLY.
Saving Information on the
Stack
The PUSH Instruction
● PUSH B/D/H/PSW
o Decrement SP
o Copy the contents of register B to the memory location
pointed to by SP
o Decrement SP
o Copy the contents of register C to the memory location
pointed to by SP
The POP Instruction
● POP B/D/H/PSW
o Copy the contents of the memory location pointed to by the
SP to register E
o Increment SP
o Copy the contents of the memory location pointed to by the
SP to register D
o Increment SP
o During pushing, the stack operates in a “decrement
then store” style.
o The stack pointer is decremented first, then the
information is placed on the stack.
o During poping, the stack operates in a “use then
increment” style.
o The information is retrieved from the top of the the
stack and then the pointer is incremented.
o The SP pointer always points to “the top of the stack”.
Operation of the Stack
● 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.
LIFO
The PSW Register Pair
o The 8085 recognizes one additional register pair
called the PSW (Program Status Word).
o This register pair is made up of the Accumulator
and the Flags registers.
o It is possible to push the PSW onto the stack, do
whatever operations are needed, then POP it off of the
stack.
o 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.
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.
Program to Reset and display
Flags
oClear all Flags.
oLoad 00H in the accumulator, and
demonstrate that the zero flag is not affected
by data transfer instruction.
oLogically OR the accumulator with itself to
set the Zero flag, and display the flag at
PORT1 or store all flags on the stack.
Program to Reset and display
Flags
●XX00 LXI SP, XX99H Initialize the stack
●03 MVI L, 00H Clear L
●05 PUSH H Place (L) on stack
●06 POP PSW Clear Flags
●07 MVI A, 00H Load 00H
●09 PUSH PSW Save Flags on stack
●0A POP H Retrieve flags in L
●0B MOV A, L
●0C OUT PORT0 Display Flags (00H)
●0E MVI A, 00H Load 00H Again
Program to Reset and display
Flags
●XX10 ORA A Set Flags and reset
CY, AC
●11 PUSH PSW Save Flags on Stack
●12 POP H Retrieve Flags in L
●13 MOV A, L
●14 ANI 40H Mask all Flags except Z
●16 OUT PORT1 Displays 40H
●18 HLT End of Program
o A subroutine is a group of instructions that will be
used repeatedly in different locations of the program.
o Rather than repeat the same instructions several
times, they can be grouped into a subroutine that is
called from the different locations.
o In Assembly language, a subroutine can exist
anywhere in the code.
o However, it is customary to place subroutines
separately from the main program.
Subroutines
o The 8085 has two instructions for dealing with
subroutines.
o The CALL instruction is used to redirect
program execution to the subroutine.
o The RTE instruction is used to return the
execution to the calling routine.
Subroutines
The CALL Instruction
● 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.
The CALL Instruction
● RTE
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.
The RTE Instruction
Illustrates the exchange of information between stack
and Program Counter
Program Execution
CALL Execution
● Instruction requires five machine cycles and eighteen T-
states: Call instruction is fetched, 16-bit address is read during M2 and M3 and
stored temporarily in W/Z registers. In next two cycles content of program counter are
stored on the stack (address from where microprocessor continue it execution of
program after completion of the subroutine.)
RET Execution
● Program execution sequence is transferred to the memory location 2043H
location.M1 is normal fetch cycle during M2 contents of stack pointer are placed on
address bus so 43H data is fetched and stored on Z register and SP is upgraded.
Similarly for M3. Program sequence is transfered to2043H by placing contents of
W/Z on address bus.
Passing Data to a
Subroutine
o In Assembly Language data is passed to a
subroutine through registers.
o The data is stored in one of the registers by the
calling program and the subroutine uses the value from
the register.
o The other possibility is to use agreed upon memory
locations.
o The calling program stores the data in the memory
location and the subroutine retrieves the data from the
location and uses it.
RESTART, CONDITIONAL CALL
& RETURN INSTRUCTIONS
RST Instruction
RESTART, CONDITIONAL CALL
& RETURN INSTRUCTIONS
Conditional CALL
RESTART, CONDITIONAL CALL
& RETURN INSTRUCTIONS
Conditional RETURN
A Proper Subroutine
o According to Software Engineering practices, a
proper subroutine:
o Is only entered with a CALL and exited with an
RTE
o Has a single entry point
o Do not use a CALL statement to jump into
different points of the same subroutine.
Writing Subroutines
Write a Program that will display FF and 11 repeatedly on the
seven segment display. Write a ‘delay’ subroutine and Call it as
necessary.
C000: LXI SP, FFFF
C003: MVI A, FF
C005: OUT 00
C007: CALL C014
C00A: MVI A, 11
C00C: OUT 00
C00E: CALL 1420
C011: JMP C003
Writing Subroutines
DELAY: C014: MVIB, FF
C016: MVIC, FF
C018: DCR C
C019: JNZ C018
C01C: DCR B
C01D: JNZ C016
C020: RET
Nesting Subroutines
Problem Statement
Problem Statement
Problem Statement
Problem Statement
Problem Statement

More Related Content

What's hot

Instruction set of 8086
Instruction set of 8086Instruction set of 8086
Instruction set of 8086
9840596838
 
8086-instruction-set-ppt
 8086-instruction-set-ppt 8086-instruction-set-ppt
8086-instruction-set-ppt
jemimajerome
 
Architecture of 8086 Microprocessor
Architecture of 8086 Microprocessor  Architecture of 8086 Microprocessor
Architecture of 8086 Microprocessor
Mustapha Fatty
 
Memory interfacing
Memory interfacingMemory interfacing
Memory interfacing
mahalakshmimalini
 
8237 dma controller
8237 dma controller8237 dma controller
8237 dma controller
Tech_MX
 

What's hot (20)

Assembly Language Programming Of 8085
Assembly Language Programming Of 8085Assembly Language Programming Of 8085
Assembly Language Programming Of 8085
 
Subroutine
SubroutineSubroutine
Subroutine
 
8255 PPI
8255 PPI8255 PPI
8255 PPI
 
Instruction set of 8086
Instruction set of 8086Instruction set of 8086
Instruction set of 8086
 
8086-instruction-set-ppt
 8086-instruction-set-ppt 8086-instruction-set-ppt
8086-instruction-set-ppt
 
Architecture of 8086 Microprocessor
Architecture of 8086 Microprocessor  Architecture of 8086 Microprocessor
Architecture of 8086 Microprocessor
 
Memory interfacing
Memory interfacingMemory interfacing
Memory interfacing
 
Instruction cycle
Instruction cycleInstruction cycle
Instruction cycle
 
Instruction Set of 8051 Microcontroller
Instruction Set of 8051 MicrocontrollerInstruction Set of 8051 Microcontroller
Instruction Set of 8051 Microcontroller
 
Serial Communication in 8051
Serial Communication in 8051Serial Communication in 8051
Serial Communication in 8051
 
ARM Processors
ARM ProcessorsARM Processors
ARM Processors
 
8086 microprocessor-architecture
8086 microprocessor-architecture8086 microprocessor-architecture
8086 microprocessor-architecture
 
INTERRUPTS OF 8086 MICROPROCESSOR
INTERRUPTS OF 8086 MICROPROCESSORINTERRUPTS OF 8086 MICROPROCESSOR
INTERRUPTS OF 8086 MICROPROCESSOR
 
8237 dma controller
8237 dma controller8237 dma controller
8237 dma controller
 
3.programmable interrupt controller 8259
3.programmable interrupt controller 82593.programmable interrupt controller 8259
3.programmable interrupt controller 8259
 
Memory & I/O interfacing
Memory & I/O  interfacingMemory & I/O  interfacing
Memory & I/O interfacing
 
Branching instructions in 8086 microprocessor
Branching instructions in 8086 microprocessorBranching instructions in 8086 microprocessor
Branching instructions in 8086 microprocessor
 
Architecture of 8051
Architecture of 8051Architecture of 8051
Architecture of 8051
 
System bus timing 8086
System bus timing 8086System bus timing 8086
System bus timing 8086
 
Difference b/w 8085 & 8086
Difference b/w 8085 & 8086Difference b/w 8085 & 8086
Difference b/w 8085 & 8086
 

Viewers also liked

Architecture of 8051 microcontroller))
Architecture of 8051 microcontroller))Architecture of 8051 microcontroller))
Architecture of 8051 microcontroller))
Ganesh Ram
 
Stack and subroutine
Stack and subroutineStack and subroutine
Stack and subroutine
Ashim Saha
 
Stack in microprocessor 8085(presantation)
Stack in microprocessor 8085(presantation)Stack in microprocessor 8085(presantation)
Stack in microprocessor 8085(presantation)
Safin Biswas
 
8051 serial communication
8051 serial communication8051 serial communication
8051 serial communication
asteriskbimal
 

Viewers also liked (20)

Architecture of 8051 microcontroller))
Architecture of 8051 microcontroller))Architecture of 8051 microcontroller))
Architecture of 8051 microcontroller))
 
8051 interrupts
8051 interrupts8051 interrupts
8051 interrupts
 
8051 Microcontroller ppt
8051 Microcontroller ppt8051 Microcontroller ppt
8051 Microcontroller ppt
 
Interrupt
InterruptInterrupt
Interrupt
 
8 interrupt 8051
8 interrupt 80518 interrupt 8051
8 interrupt 8051
 
Stack and subroutine
Stack and subroutineStack and subroutine
Stack and subroutine
 
Interrupt
InterruptInterrupt
Interrupt
 
Chapter 9
Chapter 9Chapter 9
Chapter 9
 
Lica 7th chapter slides
Lica 7th chapter slidesLica 7th chapter slides
Lica 7th chapter slides
 
1206 Interrupts Of 8085
1206 Interrupts Of 80851206 Interrupts Of 8085
1206 Interrupts Of 8085
 
Stack in microprocessor 8085(presantation)
Stack in microprocessor 8085(presantation)Stack in microprocessor 8085(presantation)
Stack in microprocessor 8085(presantation)
 
Uart
UartUart
Uart
 
8051 serial communication-UART
8051 serial communication-UART8051 serial communication-UART
8051 serial communication-UART
 
8259 a
8259 a8259 a
8259 a
 
8251 USART
8251 USART8251 USART
8251 USART
 
Interrupts of microprocessor 8085
Interrupts of microprocessor  8085Interrupts of microprocessor  8085
Interrupts of microprocessor 8085
 
Interrupt programming with 8051 microcontroller
Interrupt programming with 8051  microcontrollerInterrupt programming with 8051  microcontroller
Interrupt programming with 8051 microcontroller
 
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
 

Similar to Stacks & subroutines 1

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
 
Basic programming of 8085
Basic programming of 8085 Basic programming of 8085
Basic programming of 8085
vijaydeepakg
 
Instruction set of 8086
Instruction set of 8086Instruction set of 8086
Instruction set of 8086
aviban
 

Similar to Stacks & subroutines 1 (20)

Stacks & Subroutines.ppt.pptx
Stacks & Subroutines.ppt.pptxStacks & Subroutines.ppt.pptx
Stacks & Subroutines.ppt.pptx
 
Stack and subroutine
Stack and subroutineStack and subroutine
Stack and subroutine
 
Lec04
Lec04Lec04
Lec04
 
Lec04
Lec04Lec04
Lec04
 
Microprocessor Part 4
Microprocessor    Part 4Microprocessor    Part 4
Microprocessor Part 4
 
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 and Microcontroller lec4
Microprocessor and Microcontroller lec4Microprocessor and Microcontroller lec4
Microprocessor and Microcontroller lec4
 
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
 
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
 
8051 microcontroller
8051 microcontroller8051 microcontroller
8051 microcontroller
 
(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
 
Basic programming of 8085
Basic programming of 8085 Basic programming of 8085
Basic programming of 8085
 
MPMC UNIT-2.pdf
MPMC UNIT-2.pdfMPMC UNIT-2.pdf
MPMC UNIT-2.pdf
 
1347 Assembly Language Programming Of 8051
1347 Assembly Language Programming Of 80511347 Assembly Language Programming Of 8051
1347 Assembly Language Programming Of 8051
 
Real Time Embedded System
Real Time Embedded SystemReal Time Embedded System
Real Time Embedded System
 
Instruction set of 8086
Instruction set of 8086Instruction set of 8086
Instruction set of 8086
 
Introduction to 8085 & it's description(includes basic lab experiments)
Introduction to 8085 & it's description(includes basic lab experiments)Introduction to 8085 & it's description(includes basic lab experiments)
Introduction to 8085 & it's description(includes basic lab experiments)
 
module-2.pptx
module-2.pptxmodule-2.pptx
module-2.pptx
 
Lecture5
Lecture5Lecture5
Lecture5
 
Lecture5(1)
Lecture5(1)Lecture5(1)
Lecture5(1)
 

More from deval patel (10)

8085 interrupts
8085 interrupts8085 interrupts
8085 interrupts
 
8254 PIT
8254 PIT8254 PIT
8254 PIT
 
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
 
Interrupts
InterruptsInterrupts
Interrupts
 
Chapter 8
Chapter 8Chapter 8
Chapter 8
 
Lecture 1
Lecture 1Lecture 1
Lecture 1
 

Recently uploaded

Integrated Test Rig For HTFE-25 - Neometrix
Integrated Test Rig For HTFE-25 - NeometrixIntegrated Test Rig For HTFE-25 - Neometrix
Integrated Test Rig For HTFE-25 - Neometrix
Neometrix_Engineering_Pvt_Ltd
 
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak HamilCara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Kandungan 087776558899
 

Recently uploaded (20)

Thermal Engineering Unit - I & II . ppt
Thermal Engineering  Unit - I & II . pptThermal Engineering  Unit - I & II . ppt
Thermal Engineering Unit - I & II . ppt
 
FEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced Loads
FEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced LoadsFEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced Loads
FEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced Loads
 
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
 
Theory of Time 2024 (Universal Theory for Everything)
Theory of Time 2024 (Universal Theory for Everything)Theory of Time 2024 (Universal Theory for Everything)
Theory of Time 2024 (Universal Theory for Everything)
 
Online electricity billing project report..pdf
Online electricity billing project report..pdfOnline electricity billing project report..pdf
Online electricity billing project report..pdf
 
Integrated Test Rig For HTFE-25 - Neometrix
Integrated Test Rig For HTFE-25 - NeometrixIntegrated Test Rig For HTFE-25 - Neometrix
Integrated Test Rig For HTFE-25 - Neometrix
 
COST-EFFETIVE and Energy Efficient BUILDINGS ptx
COST-EFFETIVE  and Energy Efficient BUILDINGS ptxCOST-EFFETIVE  and Energy Efficient BUILDINGS ptx
COST-EFFETIVE and Energy Efficient BUILDINGS ptx
 
Learn the concepts of Thermodynamics on Magic Marks
Learn the concepts of Thermodynamics on Magic MarksLearn the concepts of Thermodynamics on Magic Marks
Learn the concepts of Thermodynamics on Magic Marks
 
DC MACHINE-Motoring and generation, Armature circuit equation
DC MACHINE-Motoring and generation, Armature circuit equationDC MACHINE-Motoring and generation, Armature circuit equation
DC MACHINE-Motoring and generation, Armature circuit equation
 
Hostel management system project report..pdf
Hostel management system project report..pdfHostel management system project report..pdf
Hostel management system project report..pdf
 
Bhubaneswar🌹Call Girls Bhubaneswar ❤Komal 9777949614 💟 Full Trusted CALL GIRL...
Bhubaneswar🌹Call Girls Bhubaneswar ❤Komal 9777949614 💟 Full Trusted CALL GIRL...Bhubaneswar🌹Call Girls Bhubaneswar ❤Komal 9777949614 💟 Full Trusted CALL GIRL...
Bhubaneswar🌹Call Girls Bhubaneswar ❤Komal 9777949614 💟 Full Trusted CALL GIRL...
 
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
 
Thermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.pptThermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.ppt
 
Jaipur ❤CALL GIRL 0000000000❤CALL GIRLS IN Jaipur ESCORT SERVICE❤CALL GIRL IN...
Jaipur ❤CALL GIRL 0000000000❤CALL GIRLS IN Jaipur ESCORT SERVICE❤CALL GIRL IN...Jaipur ❤CALL GIRL 0000000000❤CALL GIRLS IN Jaipur ESCORT SERVICE❤CALL GIRL IN...
Jaipur ❤CALL GIRL 0000000000❤CALL GIRLS IN Jaipur ESCORT SERVICE❤CALL GIRL IN...
 
HAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKAR
HAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKARHAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKAR
HAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKAR
 
HOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptx
HOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptxHOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptx
HOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptx
 
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak HamilCara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
 
Employee leave management system project.
Employee leave management system project.Employee leave management system project.
Employee leave management system project.
 
💚Trustworthy Call Girls Pune Call Girls Service Just Call 🍑👄6378878445 🍑👄 Top...
💚Trustworthy Call Girls Pune Call Girls Service Just Call 🍑👄6378878445 🍑👄 Top...💚Trustworthy Call Girls Pune Call Girls Service Just Call 🍑👄6378878445 🍑👄 Top...
💚Trustworthy Call Girls Pune Call Girls Service Just Call 🍑👄6378878445 🍑👄 Top...
 
Introduction to Serverless with AWS Lambda
Introduction to Serverless with AWS LambdaIntroduction to Serverless with AWS Lambda
Introduction to Serverless with AWS Lambda
 

Stacks & subroutines 1

  • 2. o The stack is an area of memory identified by the programmer for temporary storage of information. o The stack is a LIFO (Last In First Out. ) structure. o The stack normally grows backwards into memory. o In other words, the programmer defines the bottom of the stack and the stack grows up into reducing address range. The Stack
  • 3. o 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. o In the 8085, the stack is defined by setting the SP (Stack Pointer) register. LXI SP, FFFFH o This sets the Stack Pointer to location FFFFH (end of memory for the 8085). The Stack
  • 4. o Information is saved on the stack by PUSHing it on. o It is retrieved from the stack by POPing it off. o The 8085 provides two instructions: PUSH and POP for storing information on the stack and retrieving it back. o Both PUSH and POP work with register pairs ONLY. Saving Information on the Stack
  • 5. The PUSH Instruction ● PUSH B/D/H/PSW o Decrement SP o Copy the contents of register B to the memory location pointed to by SP o Decrement SP o Copy the contents of register C to the memory location pointed to by SP
  • 6. The POP Instruction ● POP B/D/H/PSW o Copy the contents of the memory location pointed to by the SP to register E o Increment SP o Copy the contents of the memory location pointed to by the SP to register D o Increment SP
  • 7. o During pushing, the stack operates in a “decrement then store” style. o The stack pointer is decremented first, then the information is placed on the stack. o During poping, the stack operates in a “use then increment” style. o The information is retrieved from the top of the the stack and then the pointer is incremented. o The SP pointer always points to “the top of the stack”. Operation of the Stack
  • 8. ● 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. LIFO
  • 9. The PSW Register Pair o The 8085 recognizes one additional register pair called the PSW (Program Status Word). o This register pair is made up of the Accumulator and the Flags registers. o It is possible to push the PSW onto the stack, do whatever operations are needed, then POP it off of the stack. o 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. 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.
  • 11. Program to Reset and display Flags oClear all Flags. oLoad 00H in the accumulator, and demonstrate that the zero flag is not affected by data transfer instruction. oLogically OR the accumulator with itself to set the Zero flag, and display the flag at PORT1 or store all flags on the stack.
  • 12. Program to Reset and display Flags ●XX00 LXI SP, XX99H Initialize the stack ●03 MVI L, 00H Clear L ●05 PUSH H Place (L) on stack ●06 POP PSW Clear Flags ●07 MVI A, 00H Load 00H ●09 PUSH PSW Save Flags on stack ●0A POP H Retrieve flags in L ●0B MOV A, L ●0C OUT PORT0 Display Flags (00H) ●0E MVI A, 00H Load 00H Again
  • 13. Program to Reset and display Flags ●XX10 ORA A Set Flags and reset CY, AC ●11 PUSH PSW Save Flags on Stack ●12 POP H Retrieve Flags in L ●13 MOV A, L ●14 ANI 40H Mask all Flags except Z ●16 OUT PORT1 Displays 40H ●18 HLT End of Program
  • 14. o A subroutine is a group of instructions that will be used repeatedly in different locations of the program. o Rather than repeat the same instructions several times, they can be grouped into a subroutine that is called from the different locations. o In Assembly language, a subroutine can exist anywhere in the code. o However, it is customary to place subroutines separately from the main program. Subroutines
  • 15. o The 8085 has two instructions for dealing with subroutines. o The CALL instruction is used to redirect program execution to the subroutine. o The RTE instruction is used to return the execution to the calling routine. Subroutines
  • 16. The CALL Instruction ● 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.
  • 18. ● RTE 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. The RTE Instruction
  • 19. Illustrates the exchange of information between stack and Program Counter
  • 21. CALL Execution ● Instruction requires five machine cycles and eighteen T- states: Call instruction is fetched, 16-bit address is read during M2 and M3 and stored temporarily in W/Z registers. In next two cycles content of program counter are stored on the stack (address from where microprocessor continue it execution of program after completion of the subroutine.)
  • 22. RET Execution ● Program execution sequence is transferred to the memory location 2043H location.M1 is normal fetch cycle during M2 contents of stack pointer are placed on address bus so 43H data is fetched and stored on Z register and SP is upgraded. Similarly for M3. Program sequence is transfered to2043H by placing contents of W/Z on address bus.
  • 23. Passing Data to a Subroutine o In Assembly Language data is passed to a subroutine through registers. o The data is stored in one of the registers by the calling program and the subroutine uses the value from the register. o The other possibility is to use agreed upon memory locations. o The calling program stores the data in the memory location and the subroutine retrieves the data from the location and uses it.
  • 24. RESTART, CONDITIONAL CALL & RETURN INSTRUCTIONS RST Instruction
  • 25. RESTART, CONDITIONAL CALL & RETURN INSTRUCTIONS Conditional CALL
  • 26. RESTART, CONDITIONAL CALL & RETURN INSTRUCTIONS Conditional RETURN
  • 27. A Proper Subroutine o According to Software Engineering practices, a proper subroutine: o Is only entered with a CALL and exited with an RTE o Has a single entry point o Do not use a CALL statement to jump into different points of the same subroutine.
  • 28. Writing Subroutines Write a Program that will display FF and 11 repeatedly on the seven segment display. Write a ‘delay’ subroutine and Call it as necessary. C000: LXI SP, FFFF C003: MVI A, FF C005: OUT 00 C007: CALL C014 C00A: MVI A, 11 C00C: OUT 00 C00E: CALL 1420 C011: JMP C003
  • 29. Writing Subroutines DELAY: C014: MVIB, FF C016: MVIC, FF C018: DCR C C019: JNZ C018 C01C: DCR B C01D: JNZ C016 C020: RET