Stack and Subroutine PDF
Stack and Subroutine PDF
Chapter 9
Stack and Subroutines
The Stack
Memory
The Stack
grows
backwards
into memory
Bottom
of the
Stack
The Stack
12
F3
FFFB
FFFC
FFFD
FFFE
FFFF
F3
12
SP
12
F3
FFFB
FFFC
FFFD
FFFE
FFFF
F3
12
SP
LIFO
Flag
12
80
FFFB
FFFC
FFFD
FFFE
FFFF
80
12
SP
10
11
Flag
12
80
FFFB
FFFC
FFFD
FFFE
FFFF
80
12
SP
LXI SP FFFF
PUSH PSW
POP H
MOV A L
ANI BFH (BFH= 1011 1111)
MOV L A
PUSH H
POP PSW
* Masking
12
13
Subroutines
14
Subroutines
15
CALL 4000
4 0 00
PC
[W] [Z]Register
2003
FFFB
FFFC
FFFD
FFFE
FFFF
03
20
SP
16
17
PC
4014
4015
...
RET
2003
FFFB
FFFC
FFFD
FFFE
FFFF
03
20
SP
18
19
20
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.
CSE 307- Microprocessor
21
22
23
A Proper Subroutine
24
Writing Subroutines
25
Program Transfer