0% found this document useful (0 votes)
35 views

L6 8086 Stack Subroutine

The document provides an overview of stack memory and its role in the Intel 8086 microprocessor, emphasizing its importance in managing subroutines and function calls. It explains how stack operations like PUSH and POP work, including how data is stored and retrieved, and illustrates these concepts with examples of swapping registers and passing arguments to subroutines. Additionally, it discusses the stack pointer and how it tracks the current position in the stack memory.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views

L6 8086 Stack Subroutine

The document provides an overview of stack memory and its role in the Intel 8086 microprocessor, emphasizing its importance in managing subroutines and function calls. It explains how stack operations like PUSH and POP work, including how data is stored and retrieved, and illustrates these concepts with examples of swapping registers and passing arguments to subroutines. Additionally, it discusses the stack pointer and how it tracks the current position in the stack memory.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 59

Intel 8086

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

o 8086 push instruction always pushes a 16-bit value to the stack


Eg:
push ax ; push 16 bit register
push 16 ; Even if you write 16, 0016 is pushed
push [1000];Contents of address 1000 and 1001 (16
bit data) is pushed
pushf;Push 16-bit flag to stack

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

o Stack is indispensable in implementing sub-routines (functions)


o Have you wondered how a function returns once it finishes
execution?
o Eg:
main(){ foo(){
……… ………
……… ………
foo() ………
……… return
} }

14
Stack and Subroutines

o Assembly instruction used to call a subroutine is the call instruction


o Eg: call myFunction
o Here myFunction represents the starting address of the
subroutine
o So how program control goes to the subroutine is pretty straight
forward
o The starting address of the subroutine is loaded to the instruction
pointer (IP)

15
Stack and Subroutines

o Assembly instruction used to return from a subroutine is the RET


instruction
o A subroutine may have one or more RET instructions (similar to a
high-level language function having more than one return
statement)
o You should make sure in assembly the processor always
encounters an RET instruction
o But how the program flows comes back to the instruction after
CALL instruction since there are not return address specified with
the RET instruction?
16
o That is where stack comes into picture
Stack and Subroutines
main: add: 0
100: mov ax,10 200: add ax,bx 2
103: mov bx,20 202: ret 4
106: call add 6
109: mov cx,ax 8
A
C
XXXX E
CX 10
12
XXXX 100 Stack Memory 14
AX IP 16
18
XXXX 001C
1A
BX SP 17
XXXX 1C
Stack and Subroutines
main: add: 0
100: mov ax,10 200: add ax,bx 2
103: mov bx,20 202: ret 4
106: call add 6
109: mov cx,ax 8
A
C
XXXX E
CX 10
12
0010 100 Stack Memory 14
AX IP 16
18
XXXX 001C
1A
BX SP 18
XXXX 1C
Stack and Subroutines
main: add: 0
100: mov ax,10 200: add ax,bx 2
103: mov bx,20 202: ret 4
106: call add 6
109: mov cx,ax 8
A
C
XXXX E
CX 10
12
0010 103 Stack Memory 14
AX IP 16
18
0020 001C
1A
BX SP 19
XXXX 1C
Stack and Subroutines
main: add: 0
100: mov ax,10 200: add ax,bx 2
103: mov bx,20 202: ret 4
106: call add 6
109: mov cx,ax 8
A
C
XXXX E
CX 10
12
0010 106 Stack Memory 14
AX IP 16
18
0020 001C
1A
BX SP 20
XXXX 1C
Stack and Subroutines
main: add: 0
100: mov ax,10 200: add ax,bx 2
103: mov bx,20 202: ret 4
106: call add 6
109: mov cx,ax 8
A
C
XXXX E
Automatic push
CX 10
12
0010 106 Stack Memory 14
AX IP 16
18
0020 001C
1A
BX SP 21
XXXX 1C
Stack and Subroutines
main: add: 0
100: mov ax,10 200: add ax,bx 2
103: mov bx,20 202: ret 4
106: call add 6
109: mov cx,ax 8
A
C
XXXX E
Automatic push
CX 10
12
0010 106 Stack Memory 14
AX IP 16
18
0020 001A
0109 1A
BX SP 22
XXXX 1C
Stack and Subroutines
main: add: 0
100: mov ax,10 200: add ax,bx 2
103: mov bx,20 202: ret 4
106: call add 6
109: mov cx,ax 8
A
C
XXXX E
CX 10
12
0030 200 Stack Memory 14
AX IP 16
18
0020 001A
0109 1A
BX SP 23
XXXX 1C
Stack and Subroutines
main: add: 0
100: mov ax,10 200: add ax,bx 2
103: mov bx,20 202: ret 4
106: call add 6
109: mov cx,ax 8
A
C
XXXX E
CX 10
12
0030 202 Stack Memory 14
AX IP 16
18
0020 001A
0109 1A
BX SP 24
XXXX 1C
Stack and Subroutines
main: add: 0
100: mov ax,10 200: add ax,bx 2
103: mov bx,20 202: ret 4
106: call add 6
109: mov cx,ax 8
A
Automatic C
pop
XXXX E
CX 10
12
0030 202 Stack Memory 14
AX IP 16
18
0020 001A
0109 1A
BX SP 25
XXXX 1C
Stack and Subroutines
main: add: 0
100: mov ax,10 200: add ax,bx 2
103: mov bx,20 202: ret 4
106: call add 6
109: mov cx,ax 8
A
C
XXXX E
CX 10
12
0030 109 Stack Memory 14
AX IP 16
18
0020 001A
0109 1A
BX SP 26
XXXX 1C
Stack and Subroutines
main: add: 0
100: mov ax,10 200: add ax,bx 2
103: mov bx,20 202: ret 4
106: call add 6
109: mov cx,ax 8
A
C
0030 E
CX 10
12
0030 109 Stack Memory 14
AX IP 16
18
0020 001A
0109 1A
BX SP 27
XXXX 1C
Stack and subroutine arguments and return
value

o Stack can be used as a method to pass arguments and return


values(s) from a subroutine
o In 8086 this is slightly tricky
o Before calling the subroutine, you can push the arguments to the
stack
o Inside the subroutine, top of the stack is popped and backedup
(that is the return address)
o Then arguments are popped
o After processing return values(s) are pushed and then the return
address 28
Stack and subroutine arguments and return
value

o The following example uses a subroutine add to find the sum of 2


16-bit numbers
o Function arguments are passed through the stack and the return
value also comes through stack
o Main function passes 100H and 200H as the subroutine arguments

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

o It is possible that in subroutines you modify register values


o It is also possible that different people write different subroutines
for the same program
o How do you make sure that you are not inadvertently modifying
register contents which are used in other subroutines?
o To avoid this, a thumb rule is any register content you are
modifying within a subroutine should be restored before returning
from the subroutine
o In the following example, subroutine is modifying ax register, but is
restored before returning
43
Stack and subroutine arguments and return
value
add: 0
100: mov ax,10 200: push ax 2
103: mov bx,20 201: pushf 4
106: call add 202: add ax,bx 6
109: mov cx,[1000] 204: mov [1000],ax 8
208: popf A
209: pop ax C
XXXX 20a: ret E
CX 10
12
0010 100 Stack Memory 14
AX IP 16
18
XXXX 001C
1A
BX SP 44
XXXX 1C
Stack and subroutine arguments and return
value
add: 0
100: mov ax,10 200: push ax 2
103: mov bx,20 201: pushf 4
106: call add 202: add ax,bx 6
109: mov cx,[1000] 204: mov [1000],ax 8
208: popf A
209: pop ax C
XXXX 20a: ret E
CX 10
12
0010 103 Stack Memory 14
AX IP 16
18
0020 001C
1A
BX SP 45
XXXX 1C
Stack and subroutine arguments and return
value
add: 0
100: mov ax,10 200: push ax 2
103: mov bx,20 201: pushf 4
106: call add 202: add ax,bx 6
109: mov cx,[1000] 204: mov [1000],ax 8
208: popf A
209: pop ax C
XXXX 20a: ret E
CX 10
12
0010 106 Stack Memory 14
AX IP 16
18
0020 001A
0109 1A
BX SP 46
XXXX 1C
Stack and subroutine arguments and return
value
add: 0
100: mov ax,10 200: push ax 2
103: mov bx,20 201: pushf 4
106: call add 202: add ax,bx 6
109: mov cx,[1000] 204: mov [1000],ax 8
208: popf A
209: pop ax C
XXXX 20a: ret E
CX 10
12
0010 200 Stack Memory 14
AX IP 16
0010 18
0020 0018
0109 1A
BX SP 47
XXXX 1C
Stack and subroutine arguments and return
value
add: 0
100: mov ax,10 200: push ax 2
103: mov bx,20 201: pushf 4
106: call add 202: add ax,bx 6
109: mov cx,[1000] 204: mov [1000],ax 8
208: popf A
209: pop ax C
XXXX 20a: ret E
CX 10
12
0010 201 Stack Memory 14
AX IP FlagReg 16
0010 18
0020 0016
0109 1A
BX SP 48
XXXX 1C
Stack and subroutine arguments and return
value
add: 0
100: mov ax,10 200: push ax 2
103: mov bx,20 201: pushf 4
106: call add 202: add ax,bx 6
109: mov cx,[1000] 204: mov [1000],ax 8
208: popf A
209: pop ax C
XXXX 20a: ret E
CX 10
12
0010 201 Stack Memory 14
AX IP FlagReg 16
0010 18
0020 0016
0109 1A
BX SP 49
XXXX 1C
Stack and subroutine arguments and return
value
add: 0
100: mov ax,10 200: push ax 2
103: mov bx,20 201: pushf 4
106: call add 202: add ax,bx 6
109: mov cx,[1000] 204: mov [1000],ax 8
208: popf A
209: pop ax C
XXXX 20a: ret E
CX 10
12
0030 202 Stack Memory 14
AX IP FlagReg 16
0010 18
0020 0016
0109 1A
BX SP 50
XXXX 1C
Stack and subroutine arguments and return
value
add: 0
100: mov ax,10 200: push ax 2
103: mov bx,20 201: pushf 4
106: call add 202: add ax,bx 6
109: mov cx,[1000] 204: mov [1000],ax 8
208: popf A
209: pop ax C
XXXX 20a: ret E
CX 10
12
0030 204 Stack Memory 14
AX IP FlagReg 16
0010 18
0020 0016
0109 1A
BX SP 51
XXXX 1C
Stack and subroutine arguments and return
value
add: 0
100: mov ax,10 200: push ax 2
103: mov bx,20 201: pushf 4
106: call add 202: add ax,bx 6
109: mov cx,[1000] 204: mov [1000],ax 8
208: popf A
209: pop ax C
XXXX 20a: ret E
CX 10
12
0030 204 Stack Memory 14
AX IP FlagReg 16
0010 18
0020 0018
0109 1A
BX SP 52
XXXX 1C
Stack and subroutine arguments and return
value
add: 0
100: mov ax,10 200: push ax 2
103: mov bx,20 201: pushf 4
106: call add 202: add ax,bx 6
109: mov cx,[1000] 204: mov [1000],ax 8
208: popf A
209: pop ax C
XXXX 20a: ret E
CX 10
12
0010 204 Stack Memory 14
AX IP FlagReg 16
0010 18
0020 0018
0109 1A
BX SP 53
XXXX 1C
Stack and subroutine arguments and return
value
add: 0
100: mov ax,10 200: push ax 2
103: mov bx,20 201: pushf 4
106: call add 202: add ax,bx 6
109: mov cx,[1000] 204: mov [1000],ax 8
208: popf A
209: pop ax C
XXXX 20a: ret E
CX 10
12
0010 20a Stack Memory 14
AX IP FlagReg 16
0010 18
0020 0018
0109 1A
BX SP 54
XXXX 1C
Stack and subroutine arguments and return
value
add: 0
100: mov ax,10 200: push ax 2
103: mov bx,20 201: pushf 4
106: call add 202: add ax,bx 6
109: mov cx,[1000] 204: mov [1000],ax 8
208: popf A
209: pop ax C
XXXX 20a: ret E
CX 10
12
0010 109 Stack Memory 14
AX IP FlagReg 16
0010 18
0020 001C
0109 1A
BX SP 55
XXXX 1C
Stack and subroutine arguments and return
value
add: 0
100: mov ax,10 200: push ax 2
103: mov bx,20 201: pushf 4
106: call add 202: add ax,bx 6
109: mov cx,[1000] 204: mov [1000],ax 8
208: popf A
209: pop ax C
0030 20a: ret E
CX 10
12
0010 109 Stack Memory 14
AX IP FlagReg 16
0010 18
0020 001C
0109 1A
BX SP 56
XXXX 1C
Stack Overflow (in 8086)

o You can see stack is a dynamic memory, it grows and shrinks


o If you are not careful enough, it is possible that stack keeps
growing and encroaches portions of memory where you are stored
other data/code
o This could be catastrophic
o This scenario is especially possible when you have recursive
subroutines
o Each time the subroutine is called, the return address is pushed to
the stack and if termination condition takes too long, stack overflow
may happen
57
Stack Overflow (in 8086)

o To minimize the scenario, if you are using stack memory, it will be a


good idea to initialize it to towards the end of the segment
o Most 8086 systems initialize SP to 0xFFFE

58
Thank you
any questions

59

You might also like