L6 8086 Stack Subroutine
L6 8086 Stack Subroutine
Microprocessor
Stack Memory and Subroutines
Stack Memory
0
o Stack is a logical memory block allocated 2
4
in the main memory
6
o In modern computers, each process will 8
have its own stack memory A
C
o They are indispensable in the
E
implementation of subroutines (functions) 10
since function variables (local variables), 12
function parameters and return addressStack Memory 14
are stored in the stack 16
18
o Stack is implemented as a LIFO (Last in
1A
First Out) 2
1C
Stack Pointer
0
o There should be some way to track till 2
4
what point data is available in stack
6
o This is done with the help of stack 8
pointer (SP) A
C
o Stack pointer points to the top of
E
the stack stack pointer stores the 10
address till which data is filled 12
Stack Memory 14
16
18
001C
1A
SP 3
XXXX 1C
Stack PUSH
0
o We use push instruction to store new 2
data into stack 4
Eg: push AX 6
8
o This causes SP to decrement its A
content by 2 (thus points to next empty C
location in stack) E
10
o Store the data at the location
12
1234 Stack Memory 14
AX 16
001A
001C 18
A5A5
1234
XXXX 1A
BX SP 4
XXXX 1C
Stack PUSH
0
o Note than in the slides SP content is 2
directly used as the physical memory 4
address for simplicity 6
8
o Practically the physical address
A
pointed by stack will be C
ss<<4+SP E
10
12
Stack Memory 14
16
18
001A
1234 1A
SP 5
XXXX 1C
Push Instructions
6
Stack POP
0
o We use pop instruction to pull data
2
from stack 4
Eg: pop BX 6
8
o This causes data from top of stack A
(pointed by SP) to copy to the C
destination E
10
o SP increments by 2
12
1234 Stack Memory 14
AX 16
XXXX 001C
001A 18
1234
1234 1A
BX SP 7
XXXX 1C
Pop Instructions
o 8086 pop instruction always pops a 16-bit value from the top of
the stack
Eg:
pop ax;pops 16-bit data from stack memory and
stores in ax
pop [1000];pops 16-bit data from stack and stores
in address 1000 and 1001 (16-bit data)
popf;pops 16-bit data from stack and stores in
flag register
8
Swapping using Stack
0
o Swap AX and BX registers using Stack 2
4
6
8
A
C
E
10
12
1234 Stack Memory 14
AX 16
18
5678 001C
1A
BX SP 9
XXXX 1C
Swapping using Stack
0
push AX 2
4
6
8
A
C
E
10
12
1234 Stack Memory 14
AX 16
18
5678 001A
1234 1A
BX SP 10
XXXX 1C
Swapping using Stack
0
push AX 2
push BX 4
6
8
A
C
E
10
12
1234 Stack Memory 14
AX 16
5678 18
5678 0018
1234 1A
BX SP 11
XXXX 1C
Swapping using Stack
0
push AX 2
push BX 4
6
pop AX
8
A
C
E
10
12
5678 Stack Memory 14
AX 16
5678 18
5678 001A
1234 1A
BX SP 12
XXXX 1C
Swapping using Stack
0
push AX 2
push BX 4
6
pop AX
8
pop BX A
C
E
10
12
5678 Stack Memory 14
AX 16
5678 18
1234 001C
1234 1A
BX SP 13
XXXX 1C
Stack and Subroutines
14
Stack and Subroutines
15
Stack and Subroutines
29
Stack and subroutine arguments and return
value
main: add: 0
100: push 100 200: pop cx 2
103: push 200 203: pop ax 4
106: call add 206: pop bx 6
109: pop cx 209: add ax,bx 8
20b: push ax A
20e: push cx C
XXXX 211: ret E
CX 10
12
XXXX 100 Stack Memory 14
AX IP 16
18
XXXX 001C
1A
BX SP 30
XXXX 1C
Stack and subroutine arguments and return
value
main: add: 0
100: push 100 200: pop cx 2
103: push 200 203: pop ax 4
106: call add 206: pop bx 6
109: pop cx 209: add ax,bx 8
20b: push ax A
20e: push cx C
XXXX 211: ret E
CX 10
12
XXXX 100 Stack Memory 14
AX IP 16
18
XXXX 001A
0100 1A
BX SP 31
XXXX 1C
Stack and subroutine arguments and return
value
main: add: 0
100: push 100 200: pop cx 2
103: push 200 203: pop ax 4
106: call add 206: pop bx 6
109: pop cx 209: add ax,bx 8
20b: push ax A
20e: push cx C
XXXX 211: ret E
CX 10
12
XXXX 103 Stack Memory 14
AX IP 16
0200 18
XXXX 0018
0100 1A
BX SP 32
XXXX 1C
Stack and subroutine arguments and return
value
main: add: 0
100: push 100 200: pop cx 2
103: push 200 203: pop ax 4
106: call add 206: pop bx 6
109: pop cx 209: add ax,bx 8
20b: push ax A
20e: push cx C
XXXX 211: ret E
CX 10
12
XXXX 106 Stack Memory 14
AX IP 0109 16
0200 18
XXXX 0016
0100 1A
BX SP 33
XXXX 1C
Stack and subroutine arguments and return
value
main: add: 0
100: push 100 200: pop cx 2
103: push 200 203: pop ax 4
106: call add 206: pop bx 6
109: pop cx 209: add ax,bx 8
20b: push ax A
20e: push cx C
0109 211: ret E
CX 10
12
XXXX 200 Stack Memory 14
AX IP 0109 16
0200 18
XXXX 0018
0100 1A
BX SP 34
XXXX 1C
Stack and subroutine arguments and return
value
main: add: 0
100: push 100 200: pop cx 2
103: push 200 203: pop ax 4
106: call add 206: pop bx 6
109: pop cx 209: add ax,bx 8
20b: push ax A
20e: push cx C
0109 211: ret E
CX 10
12
0200 203 Stack Memory 14
AX IP 0109 16
0200 18
XXXX 001A
0100 1A
BX SP 35
XXXX 1C
Stack and subroutine arguments and return
value
main: add: 0
100: push 100 200: pop cx 2
103: push 200 203: pop ax 4
106: call add 206: pop bx 6
109: pop cx 209: add ax,bx 8
20b: push ax A
20e: push cx C
0109 211: ret E
CX 10
12
0200 206 Stack Memory 14
AX IP 0109 16
0200 18
0100 001C
0100 1A
BX SP 36
XXXX 1C
Stack and subroutine arguments and return
value
main: add: 0
100: push 100 200: pop cx 2
103: push 200 203: pop ax 4
106: call add 206: pop bx 6
109: pop cx 209: add ax,bx 8
20b: push ax A
20e: push cx C
0109 211: ret E
CX 10
12
0300 209 Stack Memory 14
AX IP 0109 16
0200 18
0100 001C
0100 1A
BX SP 37
XXXX 1C
Stack and subroutine arguments and return
value
main: add: 0
100: push 100 200: pop cx 2
103: push 200 203: pop ax 4
106: call add 206: pop bx 6
109: pop cx 209: add ax,bx 8
20b: push ax A
20e: push cx C
0109 211: ret E
CX 10
12
0300 20b Stack Memory 14
AX IP 0109 16
0200 18
0100 001A
0300 1A
BX SP 38
XXXX 1C
Stack and subroutine arguments and return
value
main: add: 0
100: push 100 200: pop cx 2
103: push 200 203: pop ax 4
106: call add 206: pop bx 6
109: pop cx 209: add ax,bx 8
20b: push ax A
20e: push cx C
0109 211: ret E
CX 10
12
0300 20e Stack Memory 14
AX IP 0109 16
0109 18
0100 0018
0300 1A
BX SP 39
XXXX 1C
Stack and subroutine arguments and return
value
main: add: 0
100: push 100 200: pop cx 2
103: push 200 203: pop ax 4
106: call add 206: pop bx 6
109: pop cx 209: add ax,bx 8
20b: push ax A
20e: push cx C
0109 211: ret E
CX 10
12
0300 211 Stack Memory 14
AX IP 0109 16
0109 18
0100 0018
0300 1A
BX SP 40
XXXX 1C
Stack and subroutine arguments and return
value
main: add: 0
100: push 100 200: pop cx 2
103: push 200 203: pop ax 4
106: call add 206: pop bx 6
109: pop cx 209: add ax,bx 8
20b: push ax A
20e: push cx C
0109 211: ret E
CX 10
12
0300 0109 Stack Memory 14
AX IP 0109 16
0109 18
0100 001A
0300 1A
BX SP 41
XXXX 1C
Stack and subroutine arguments and return
value
main: add: 0
100: push 100 200: pop cx 2
103: push 200 203: pop ax 4
106: call add 206: pop bx 6
109: pop cx 209: add ax,bx 8
20b: push ax A
20e: push cx C
0300 211: ret E
CX 10
12
0300 0109 Stack Memory 14
AX IP 0109 16
0109 18
0100 001C
0300 1A
BX SP 42
XXXX 1C
Stack to restore register values
58
Thank you
any questions
59