SlideShare a Scribd company logo
1 of 27
STACK AND
SUBROUTINE
1SSP/EC-502/2020
STACK
 The stack is an area of memory
identified by the programmer for
temporary storage of information.
 Address of the stack is stored into the
stack pointer register.
 The stack is a LIFO structure. – Last In
First Out.
 The starting location of the stack is
defined by loading a 16 bit address into
the stack pointer that spaced is
reserved, usually at the top of the
memory map.
2SSP/EC-502/2020
STACK Cont…
 The stack normally grows backwards into
memory. – i.e. the programmer defines the
bottom of the stack and the stack grows up
into reducing address range.
 The stack can be initialized
anywhere in the user memory
map , but stack is initialized at
the highest memory location
so that there will not be any
interface with the program.
3SSP/EC-502/2020
STACK Cont…
o The Size of the stack is limited only
by the available memory
o In the 8085, the stack is defined by
setting the SP (Stack Pointer) register.
LXI SP, F653H
o The LXI SP,F653 state that load the 16
bit address into the stack pointer
register.
4SSP/EC-502/2020
Information is stored and retrieved from
the stack
• The 8085 provide two instruction PUSH & POP for
storing information on the stack and retrieving it back.
• Information in the register pairs stored on the stack in
reverse order by using the instruction PUSH.
• Information retrieved from the stack by using the
instruction POP.
• PUSH & POP both instruction works with register pairs
only.
• The storage and retrieval of the content of registers on
the stack follows the LIFO(Last-In-First-Out)
sequence.
• Information in the stack location may not be destroyed
until new information is stored in that memory5SSP/EC-502/2020
Operation of stack by PUSH and POP
instruction
2000 LXI SP,2099H
2003 LXI H ,42F2H
2006 PUSH H
2007 DELAY COUNTER
200F
2010 POP H
Load the stack pointer
register with the
address 2099.
Loads data in the HL
register pair.
The content of the HL
register pair pushed
into stack.
Saved data in stack
pointer register to HL
register pair. 6SSP/EC-502/2020
PUSH Instruction
 The stack pointer is
decremented by one to 2098 H ,
and the contents of the H
register are copied to memory
location 2098H.
 The stack pointer register is
again decremented by one to
2097H,and the contents of the L
register are copied to memory
location 2097H.
 The contents of the register pair
HL are not destroyed ; however
HL is made available for delay
counter.
8085 Register
A
B
D
H
S
p
F
C
E
L
Memory
2097
2098
2099
Contents on the stack &in the register
after the PUSH instruction
Sp
42 F2
2099
X
42
F2
7SSP/EC-502/2020
POP Instruction
 The contents of the top of the
stack location shown by the
stack pointer are copied in the L
register and the stack pointer
register is incremented by one to
2098 H.
 The contents of the top of the
stack (now it is 2098H) are
copied in the H register, and the
stack pointer is incremented by
one.
 The contents of memory location
2097H and 2098 are not
destroyed until some other data
8085 Register
A
B
D
S
p
F
C
E
Memory
2097
2098
2099
H L
Contents on the stack and in the
registers after the POP instruction
Sp
2097
42 F2
X
42
F2
8SSP/EC-502/2020
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 stack and then the pointer is
incremented.
 The SP pointer always points to “the top of
the
9SSP/EC-502/2020
LIFO Function
 The order of PUSHs and POPs must be
opposite of each other in order to
retrieve information back into its original
location.
 PUSH H
 PUSH L
 ...
 POP L
 POP H
10SSP/EC-502/2020
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.
11SSP/EC-502/2020
PUSH PSW Register Pair
 PUSH PSW (1 Byte
Instruction)
 Decrement SP
 Copy the contents of
register A to the memory
location pointed to by SP
 Decrement SP.
 Copy the contents of Flag
register to the memory
location pointed to by SP.
12 80
A Flag
FFFF
FFFE
FFFD
FFFC
FFFB
12
80
12SSP/EC-502/2020
POP PSW Register Pair
 POP PSW (1 Byte
Instruction)
 Copy the contents of the
memory location pointed
to by the SP to Flag
register.
 Increment SP
 Copy the contents of the
memory location pointed
to by the SP to register A.
 Increment SP
FFFF
FFFE
FFFD
FFFC
FFFB
80
12
A Flag
12 80
13SSP/EC-502/2020
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.
14SSP/EC-502/2020
Subroutines
 A subroutine is group of instruction
written separately from the main
program to perform a function that
occurs repeatedly in the main
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.
15SSP/EC-502/2020
Subroutines Cont…
When a main program calls a
subroutine the program execution is
transferred to the subroutine, after
the completion of the subroutine ,the
program execution returns to the
main program.
The microprocessor uses the stack
to store the return address of the
subroutine.
16SSP/EC-502/2020
Subroutines Cont…
 The 8085 has two instructions for
dealing with subroutines.
– The CALL instruction is used to
redirect program execution to the
subroutine.
– The RET instruction is used to
return to the main program at the
end of the subroutine .
17SSP/EC-502/2020
The CALL instruction
 CALL ,16 bit
Call subroutine in conditionally
located at the memory address
specified by the 16 bit operand.
This instruction places the address of
the next instruction on the stack and
transfer the program execution to the
subroutine address.
18SSP/EC-502/2020
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.
FFFF
FFFE
FFFD
FFFC
20 03
40 00
CALL 4000
[W] [Z] Register
PC
03
20
19SSP/EC-502/2020
400
0
The CALL instruction cont…
 MP reads the subroutine address from the
next two memory location and store the
higher order 8 bit of the address in the W
register and stores the lower order 8 bit of
the address in the Z register.
 Push the address of the instruction
immediately following the CALL onto the
stack [ Return address].
 Load the program counter with the 16-bit
address supplied with the CALL
instruction from WZ register.
20SSP/EC-502/2020
The RTE 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.
FFFE
FFFF
FFFD
FFFC
20
03
20PC
21SSP/EC-502/2020
032003
CALL and RET Function
 Main prog.
◦ . F1 F2
◦ . . .
◦ CALL F1 . .
◦ RET 1 CALL F2 RET
2
◦ . RET 2
◦ . .
◦ . .
◦ RET 1 X
RET 1
RET 2
22SSP/EC-502/2020
Stack
Nesting Subroutines
SSP/EC-502/2020 23
Conditional CALL and RTE 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.
24SSP/EC-502/2020
Passing Data to a Subroutine
 Data is passed to a subroutine through
registers.
 Call by Reference:
◦ The data is stored in one of the registers by the
calling program and the subroutine uses the
value from the register.The register values get
modified within the subroutine. Then these
modifications will be transferred back to the
calling program upon returning from a subroutine
 Call by Value:
◦ The data is stored in one of the registers, but the
subroutine first PUSHES register values in the
stack and after using the registers, it POPS the
previous values of the registers from the stack
while exiting the subroutine. i.e. the original
values are restored before execution returns to
the calling program. SSP/EC-502/2020 25
Passing Data to a Subroutine
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.
SSP/EC-502/2020 26
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
SSP/EC-502/2020 27

More Related Content

What's hot (20)

Assembly Language Programming Of 8085
Assembly Language Programming Of 8085Assembly Language Programming Of 8085
Assembly Language Programming Of 8085
 
program status word
program status wordprogram status word
program status word
 
8051 ch9-950217
8051 ch9-9502178051 ch9-950217
8051 ch9-950217
 
SHLD and LHLD instruction
SHLD and LHLD instructionSHLD and LHLD instruction
SHLD and LHLD instruction
 
Instruction formats-in-8086
Instruction formats-in-8086Instruction formats-in-8086
Instruction formats-in-8086
 
8085 interfacing with memory chips
8085 interfacing with memory chips8085 interfacing with memory chips
8085 interfacing with memory chips
 
8086 alp
8086 alp8086 alp
8086 alp
 
Microprogrammed Control Unit
Microprogrammed Control UnitMicroprogrammed Control Unit
Microprogrammed Control Unit
 
Addressing modes 8085
Addressing modes 8085Addressing modes 8085
Addressing modes 8085
 
8085 branching instruction
8085 branching instruction8085 branching instruction
8085 branching instruction
 
Timing diagram 8085 microprocessor
Timing diagram 8085 microprocessorTiming diagram 8085 microprocessor
Timing diagram 8085 microprocessor
 
Module 1 8086
Module 1 8086Module 1 8086
Module 1 8086
 
Time delay programs and assembler directives 8086
Time delay programs and assembler directives 8086Time delay programs and assembler directives 8086
Time delay programs and assembler directives 8086
 
Programming with 8085
Programming with 8085Programming with 8085
Programming with 8085
 
Subroutine in 8051 microcontroller
Subroutine in 8051 microcontrollerSubroutine in 8051 microcontroller
Subroutine in 8051 microcontroller
 
8086 modes
8086 modes8086 modes
8086 modes
 
8085 alp programs
8085 alp programs8085 alp programs
8085 alp programs
 
Microprocessor lab manual
Microprocessor lab manualMicroprocessor lab manual
Microprocessor lab manual
 
Subroutine
SubroutineSubroutine
Subroutine
 
8255 PPI
8255 PPI8255 PPI
8255 PPI
 

Similar to Stack and subroutine

Stack and subroutine
Stack and subroutineStack and subroutine
Stack and subroutinemilandhara
 
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
 
Lecture 05 NOP and Stack Group of Instructions
Lecture 05 NOP and Stack Group of InstructionsLecture 05 NOP and Stack Group of Instructions
Lecture 05 NOP and Stack Group of InstructionsZeeshan Ahmed
 
Stack in microprocessor 8085(presantation)
Stack in microprocessor 8085(presantation)Stack in microprocessor 8085(presantation)
Stack in microprocessor 8085(presantation)Safin Biswas
 
Stacks & Subroutines.ppt.pptx
Stacks & Subroutines.ppt.pptxStacks & Subroutines.ppt.pptx
Stacks & Subroutines.ppt.pptxssuserdcfc6d
 
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
 
Instruction set of 8086
Instruction set of 8086Instruction set of 8086
Instruction set of 8086aviban
 
80 c51 family programmer’s guide
80 c51 family programmer’s guide80 c51 family programmer’s guide
80 c51 family programmer’s guidePratheesh Pala
 
Microprocessor and Microcontroller lec4
Microprocessor and Microcontroller lec4Microprocessor and Microcontroller lec4
Microprocessor and Microcontroller lec4Ameen San
 
Microprocessor Part 4
Microprocessor    Part 4Microprocessor    Part 4
Microprocessor Part 4Sajan Agrawal
 
Basic programming of 8085
Basic programming of 8085 Basic programming of 8085
Basic programming of 8085 vijaydeepakg
 
Stack and subroutine
Stack and subroutineStack and subroutine
Stack and subroutineAshim Saha
 
8085 microprocessor lab manual
8085 microprocessor lab manual8085 microprocessor lab manual
8085 microprocessor lab manualNithin Mohan
 

Similar to Stack and subroutine (20)

Stack and subroutine
Stack and subroutineStack and subroutine
Stack and subroutine
 
Chapter 9
Chapter 9Chapter 9
Chapter 9
 
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)
 
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
 
Stack in microprocessor 8085(presantation)
Stack in microprocessor 8085(presantation)Stack in microprocessor 8085(presantation)
Stack in microprocessor 8085(presantation)
 
Lec04
Lec04Lec04
Lec04
 
Lec04
Lec04Lec04
Lec04
 
Stacks & Subroutines.ppt.pptx
Stacks & Subroutines.ppt.pptxStacks & Subroutines.ppt.pptx
Stacks & Subroutines.ppt.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
 
Instruction set of 8086
Instruction set of 8086Instruction set of 8086
Instruction set of 8086
 
80 c51 family programmer’s guide
80 c51 family programmer’s guide80 c51 family programmer’s guide
80 c51 family programmer’s guide
 
Microprocessor and Microcontroller lec4
Microprocessor and Microcontroller lec4Microprocessor and Microcontroller lec4
Microprocessor and Microcontroller lec4
 
Chapter 5 notes
Chapter 5 notesChapter 5 notes
Chapter 5 notes
 
Microprocessor Part 4
Microprocessor    Part 4Microprocessor    Part 4
Microprocessor Part 4
 
8085_LAB_PROGRAMS.pdf
8085_LAB_PROGRAMS.pdf8085_LAB_PROGRAMS.pdf
8085_LAB_PROGRAMS.pdf
 
Basic programming of 8085
Basic programming of 8085 Basic programming of 8085
Basic programming of 8085
 
Stack and subroutine
Stack and subroutineStack and subroutine
Stack and subroutine
 
Lab manual
Lab manualLab manual
Lab manual
 
8085 microprocessor lab manual
8085 microprocessor lab manual8085 microprocessor lab manual
8085 microprocessor lab manual
 
8085 alp programs
8085 alp programs8085 alp programs
8085 alp programs
 

Recently uploaded

UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfNirmal Dwivedi
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsKarakKing
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - Englishneillewis46
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...ZurliaSoop
 
REMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxREMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxDr. Ravikiran H M Gowda
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.pptRamjanShidvankar
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...Nguyen Thanh Tu Collection
 
How to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxHow to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxCeline George
 
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptxOn_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptxPooja Bhuva
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Pooja Bhuva
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxDr. Sarita Anand
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfAdmir Softic
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxEsquimalt MFRC
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Pooja Bhuva
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxAreebaZafar22
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentationcamerronhm
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...pradhanghanshyam7136
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.christianmathematics
 
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptxCOMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptxannathomasp01
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfSherif Taha
 

Recently uploaded (20)

UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - English
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
REMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxREMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptx
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
How to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxHow to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptx
 
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptxOn_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptx
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptxCOMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdf
 

Stack and subroutine

  • 2. STACK  The stack is an area of memory identified by the programmer for temporary storage of information.  Address of the stack is stored into the stack pointer register.  The stack is a LIFO structure. – Last In First Out.  The starting location of the stack is defined by loading a 16 bit address into the stack pointer that spaced is reserved, usually at the top of the memory map. 2SSP/EC-502/2020
  • 3. STACK Cont…  The stack normally grows backwards into memory. – i.e. the programmer defines the bottom of the stack and the stack grows up into reducing address range.  The stack can be initialized anywhere in the user memory map , but stack is initialized at the highest memory location so that there will not be any interface with the program. 3SSP/EC-502/2020
  • 4. STACK Cont… o The Size of the stack is limited only by the available memory o In the 8085, the stack is defined by setting the SP (Stack Pointer) register. LXI SP, F653H o The LXI SP,F653 state that load the 16 bit address into the stack pointer register. 4SSP/EC-502/2020
  • 5. Information is stored and retrieved from the stack • The 8085 provide two instruction PUSH & POP for storing information on the stack and retrieving it back. • Information in the register pairs stored on the stack in reverse order by using the instruction PUSH. • Information retrieved from the stack by using the instruction POP. • PUSH & POP both instruction works with register pairs only. • The storage and retrieval of the content of registers on the stack follows the LIFO(Last-In-First-Out) sequence. • Information in the stack location may not be destroyed until new information is stored in that memory5SSP/EC-502/2020
  • 6. Operation of stack by PUSH and POP instruction 2000 LXI SP,2099H 2003 LXI H ,42F2H 2006 PUSH H 2007 DELAY COUNTER 200F 2010 POP H Load the stack pointer register with the address 2099. Loads data in the HL register pair. The content of the HL register pair pushed into stack. Saved data in stack pointer register to HL register pair. 6SSP/EC-502/2020
  • 7. PUSH Instruction  The stack pointer is decremented by one to 2098 H , and the contents of the H register are copied to memory location 2098H.  The stack pointer register is again decremented by one to 2097H,and the contents of the L register are copied to memory location 2097H.  The contents of the register pair HL are not destroyed ; however HL is made available for delay counter. 8085 Register A B D H S p F C E L Memory 2097 2098 2099 Contents on the stack &in the register after the PUSH instruction Sp 42 F2 2099 X 42 F2 7SSP/EC-502/2020
  • 8. POP Instruction  The contents of the top of the stack location shown by the stack pointer are copied in the L register and the stack pointer register is incremented by one to 2098 H.  The contents of the top of the stack (now it is 2098H) are copied in the H register, and the stack pointer is incremented by one.  The contents of memory location 2097H and 2098 are not destroyed until some other data 8085 Register A B D S p F C E Memory 2097 2098 2099 H L Contents on the stack and in the registers after the POP instruction Sp 2097 42 F2 X 42 F2 8SSP/EC-502/2020
  • 9. 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 stack and then the pointer is incremented.  The SP pointer always points to “the top of the 9SSP/EC-502/2020
  • 10. LIFO Function  The order of PUSHs and POPs must be opposite of each other in order to retrieve information back into its original location.  PUSH H  PUSH L  ...  POP L  POP H 10SSP/EC-502/2020
  • 11. 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. 11SSP/EC-502/2020
  • 12. PUSH PSW Register Pair  PUSH PSW (1 Byte Instruction)  Decrement SP  Copy the contents of register A to the memory location pointed to by SP  Decrement SP.  Copy the contents of Flag register to the memory location pointed to by SP. 12 80 A Flag FFFF FFFE FFFD FFFC FFFB 12 80 12SSP/EC-502/2020
  • 13. POP PSW Register Pair  POP PSW (1 Byte Instruction)  Copy the contents of the memory location pointed to by the SP to Flag register.  Increment SP  Copy the contents of the memory location pointed to by the SP to register A.  Increment SP FFFF FFFE FFFD FFFC FFFB 80 12 A Flag 12 80 13SSP/EC-502/2020
  • 14. 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. 14SSP/EC-502/2020
  • 15. Subroutines  A subroutine is group of instruction written separately from the main program to perform a function that occurs repeatedly in the main 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. 15SSP/EC-502/2020
  • 16. Subroutines Cont… When a main program calls a subroutine the program execution is transferred to the subroutine, after the completion of the subroutine ,the program execution returns to the main program. The microprocessor uses the stack to store the return address of the subroutine. 16SSP/EC-502/2020
  • 17. Subroutines Cont…  The 8085 has two instructions for dealing with subroutines. – The CALL instruction is used to redirect program execution to the subroutine. – The RET instruction is used to return to the main program at the end of the subroutine . 17SSP/EC-502/2020
  • 18. The CALL instruction  CALL ,16 bit Call subroutine in conditionally located at the memory address specified by the 16 bit operand. This instruction places the address of the next instruction on the stack and transfer the program execution to the subroutine address. 18SSP/EC-502/2020
  • 19. 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. FFFF FFFE FFFD FFFC 20 03 40 00 CALL 4000 [W] [Z] Register PC 03 20 19SSP/EC-502/2020 400 0
  • 20. The CALL instruction cont…  MP reads the subroutine address from the next two memory location and store the higher order 8 bit of the address in the W register and stores the lower order 8 bit of the address in the Z register.  Push the address of the instruction immediately following the CALL onto the stack [ Return address].  Load the program counter with the 16-bit address supplied with the CALL instruction from WZ register. 20SSP/EC-502/2020
  • 21. The RTE 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. FFFE FFFF FFFD FFFC 20 03 20PC 21SSP/EC-502/2020 032003
  • 22. CALL and RET Function  Main prog. ◦ . F1 F2 ◦ . . . ◦ CALL F1 . . ◦ RET 1 CALL F2 RET 2 ◦ . RET 2 ◦ . . ◦ . . ◦ RET 1 X RET 1 RET 2 22SSP/EC-502/2020 Stack
  • 24. Conditional CALL and RTE 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. 24SSP/EC-502/2020
  • 25. Passing Data to a Subroutine  Data is passed to a subroutine through registers.  Call by Reference: ◦ The data is stored in one of the registers by the calling program and the subroutine uses the value from the register.The register values get modified within the subroutine. Then these modifications will be transferred back to the calling program upon returning from a subroutine  Call by Value: ◦ The data is stored in one of the registers, but the subroutine first PUSHES register values in the stack and after using the registers, it POPS the previous values of the registers from the stack while exiting the subroutine. i.e. the original values are restored before execution returns to the calling program. SSP/EC-502/2020 25
  • 26. Passing Data to a Subroutine 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. SSP/EC-502/2020 26
  • 27. 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 SSP/EC-502/2020 27