SlideShare ist ein Scribd-Unternehmen logo
1 von 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

Weitere ähnliche Inhalte

Was ist angesagt?

Microprocessor 8085 complete
Microprocessor 8085 completeMicroprocessor 8085 complete
Microprocessor 8085 completeShubham Singh
 
8085 microprocessor Architecture and Pin description
8085 microprocessor Architecture and Pin description 8085 microprocessor Architecture and Pin description
8085 microprocessor Architecture and Pin description Vijay Kumar
 
Logical instruction of 8085
Logical instruction of 8085Logical instruction of 8085
Logical instruction of 8085vishalgohel12195
 
Assembly Language Programming Of 8085
Assembly Language Programming Of 8085Assembly Language Programming Of 8085
Assembly Language Programming Of 8085techbed
 
Addressing modes 8085
Addressing modes 8085Addressing modes 8085
Addressing modes 8085ShivamSood22
 
8085 microproceesor ppt
8085 microproceesor ppt8085 microproceesor ppt
8085 microproceesor pptRJ Aniket
 
4.programmable dma controller 8257
4.programmable dma controller 82574.programmable dma controller 8257
4.programmable dma controller 8257MdFazleRabbi18
 
8254 Programmable Interval Timer by vijay
8254 Programmable Interval Timer by vijay8254 Programmable Interval Timer by vijay
8254 Programmable Interval Timer by vijayVijay Kumar
 
8085 microprocessor ramesh gaonkar
8085 microprocessor   ramesh gaonkar8085 microprocessor   ramesh gaonkar
8085 microprocessor ramesh gaonkarSAQUIB AHMAD
 
8259 Programmable Interrupt Controller
8259 Programmable Interrupt Controller8259 Programmable Interrupt Controller
8259 Programmable Interrupt Controllerabhikalmegh
 
Presentation on 8086 microprocessor
Presentation on 8086 microprocessorPresentation on 8086 microprocessor
Presentation on 8086 microprocessorDiponkor Bala
 
Addressing modes of 8051
Addressing modes of 8051Addressing modes of 8051
Addressing modes of 8051SARITHA REDDY
 
8051 Microcontroller I/O ports
8051 Microcontroller I/O ports8051 Microcontroller I/O ports
8051 Microcontroller I/O portsanishgoel
 
8085 interfacing with memory chips
8085 interfacing with memory chips8085 interfacing with memory chips
8085 interfacing with memory chipsSrikrishna Thota
 

Was ist angesagt? (20)

Microprocessor 8085 complete
Microprocessor 8085 completeMicroprocessor 8085 complete
Microprocessor 8085 complete
 
Instruction set of 8086
Instruction set of 8086Instruction set of 8086
Instruction set of 8086
 
8085 microprocessor Architecture and Pin description
8085 microprocessor Architecture and Pin description 8085 microprocessor Architecture and Pin description
8085 microprocessor Architecture and Pin description
 
SHLD and LHLD instruction
SHLD and LHLD instructionSHLD and LHLD instruction
SHLD and LHLD instruction
 
Logical instruction of 8085
Logical instruction of 8085Logical instruction of 8085
Logical instruction of 8085
 
Assembly Language Programming Of 8085
Assembly Language Programming Of 8085Assembly Language Programming Of 8085
Assembly Language Programming Of 8085
 
Timing diagram 8085 microprocessor
Timing diagram 8085 microprocessorTiming diagram 8085 microprocessor
Timing diagram 8085 microprocessor
 
Addressing modes 8085
Addressing modes 8085Addressing modes 8085
Addressing modes 8085
 
8085 instruction set
8085 instruction set8085 instruction set
8085 instruction set
 
8085 microproceesor ppt
8085 microproceesor ppt8085 microproceesor ppt
8085 microproceesor ppt
 
4.programmable dma controller 8257
4.programmable dma controller 82574.programmable dma controller 8257
4.programmable dma controller 8257
 
8254 Programmable Interval Timer by vijay
8254 Programmable Interval Timer by vijay8254 Programmable Interval Timer by vijay
8254 Programmable Interval Timer by vijay
 
8086
8086 8086
8086
 
8085 microprocessor ramesh gaonkar
8085 microprocessor   ramesh gaonkar8085 microprocessor   ramesh gaonkar
8085 microprocessor ramesh gaonkar
 
8086
80868086
8086
 
8259 Programmable Interrupt Controller
8259 Programmable Interrupt Controller8259 Programmable Interrupt Controller
8259 Programmable Interrupt Controller
 
Presentation on 8086 microprocessor
Presentation on 8086 microprocessorPresentation on 8086 microprocessor
Presentation on 8086 microprocessor
 
Addressing modes of 8051
Addressing modes of 8051Addressing modes of 8051
Addressing modes of 8051
 
8051 Microcontroller I/O ports
8051 Microcontroller I/O ports8051 Microcontroller I/O ports
8051 Microcontroller I/O ports
 
8085 interfacing with memory chips
8085 interfacing with memory chips8085 interfacing with memory chips
8085 interfacing with memory chips
 

Ähnlich wie 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
 
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
 

Ähnlich wie 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
 
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
 
Lecture5
Lecture5Lecture5
Lecture5
 

Kürzlich hochgeladen

THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONTHEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONHumphrey A Beña
 
Activity 2-unit 2-update 2024. English translation
Activity 2-unit 2-update 2024. English translationActivity 2-unit 2-update 2024. English translation
Activity 2-unit 2-update 2024. English translationRosabel UA
 
4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptxmary850239
 
Presentation Activity 2. Unit 3 transv.pptx
Presentation Activity 2. Unit 3 transv.pptxPresentation Activity 2. Unit 3 transv.pptx
Presentation Activity 2. Unit 3 transv.pptxRosabel UA
 
Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4JOYLYNSAMANIEGO
 
Textual Evidence in Reading and Writing of SHS
Textual Evidence in Reading and Writing of SHSTextual Evidence in Reading and Writing of SHS
Textual Evidence in Reading and Writing of SHSMae Pangan
 
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...Nguyen Thanh Tu Collection
 
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Celine George
 
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxMULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxAnupkumar Sharma
 
Integumentary System SMP B. Pharm Sem I.ppt
Integumentary System SMP B. Pharm Sem I.pptIntegumentary System SMP B. Pharm Sem I.ppt
Integumentary System SMP B. Pharm Sem I.pptshraddhaparab530
 
How to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPHow to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPCeline George
 
ICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdfICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdfVanessa Camilleri
 
The Contemporary World: The Globalization of World Politics
The Contemporary World: The Globalization of World PoliticsThe Contemporary World: The Globalization of World Politics
The Contemporary World: The Globalization of World PoliticsRommel Regala
 
Transaction Management in Database Management System
Transaction Management in Database Management SystemTransaction Management in Database Management System
Transaction Management in Database Management SystemChristalin Nelson
 
Active Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfActive Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfPatidar M
 
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfInclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfTechSoup
 
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSGRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSJoshuaGantuangco2
 

Kürzlich hochgeladen (20)

YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptxYOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
 
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONTHEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
 
Activity 2-unit 2-update 2024. English translation
Activity 2-unit 2-update 2024. English translationActivity 2-unit 2-update 2024. English translation
Activity 2-unit 2-update 2024. English translation
 
4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx
 
Presentation Activity 2. Unit 3 transv.pptx
Presentation Activity 2. Unit 3 transv.pptxPresentation Activity 2. Unit 3 transv.pptx
Presentation Activity 2. Unit 3 transv.pptx
 
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptxLEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
 
Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4
 
Textual Evidence in Reading and Writing of SHS
Textual Evidence in Reading and Writing of SHSTextual Evidence in Reading and Writing of SHS
Textual Evidence in Reading and Writing of SHS
 
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
 
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
 
INCLUSIVE EDUCATION PRACTICES FOR TEACHERS AND TRAINERS.pptx
INCLUSIVE EDUCATION PRACTICES FOR TEACHERS AND TRAINERS.pptxINCLUSIVE EDUCATION PRACTICES FOR TEACHERS AND TRAINERS.pptx
INCLUSIVE EDUCATION PRACTICES FOR TEACHERS AND TRAINERS.pptx
 
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxMULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
 
Integumentary System SMP B. Pharm Sem I.ppt
Integumentary System SMP B. Pharm Sem I.pptIntegumentary System SMP B. Pharm Sem I.ppt
Integumentary System SMP B. Pharm Sem I.ppt
 
How to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPHow to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERP
 
ICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdfICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdf
 
The Contemporary World: The Globalization of World Politics
The Contemporary World: The Globalization of World PoliticsThe Contemporary World: The Globalization of World Politics
The Contemporary World: The Globalization of World Politics
 
Transaction Management in Database Management System
Transaction Management in Database Management SystemTransaction Management in Database Management System
Transaction Management in Database Management System
 
Active Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfActive Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdf
 
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfInclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
 
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSGRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
 

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