0% found this document useful (0 votes)
27 views17 pages

7 - Stack Management

The document discusses stack management and procedures in Intel 8086 processors. It describes stacks as last-in, first-out data structures used for temporary storage. The 8086 uses a runtime stack managed by the stack pointer register and stack segment register. It grows downward in memory. Instructions like PUSH and POP modify the stack pointer and are used to transfer data on and off the stack. The stack can store registers, parameters, local variables and is used for procedure calls and nested loops.

Uploaded by

hafsatamer03
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)
27 views17 pages

7 - Stack Management

The document discusses stack management and procedures in Intel 8086 processors. It describes stacks as last-in, first-out data structures used for temporary storage. The 8086 uses a runtime stack managed by the stack pointer register and stack segment register. It grows downward in memory. Instructions like PUSH and POP modify the stack pointer and are used to transfer data on and off the stack. The stack can store registers, parameters, local variables and is used for procedure calls and nested loops.

Uploaded by

hafsatamer03
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/ 17

Chapter 3 : Stack Management &

Procedures The Intel 8086


Outline

 Runtime Stack and Stack Operations


 Defining and Using Procedures

Chapter 3: Stack Management & Procedures CCSE YANBU 2


What is a Stack?
 Stacks provide a new way to access data in main memory
 They are used to store temporary values​​
 A stack consists of
 Memory area and a
 Pointer which keeps the address of the top of stack
 Stack is a Last-In-First-Out (LIFO) data structure
 Analogous to a stack of plates in a cafeteria
 Plate on Top of Stack is directly accessible
 Two basic stack operations
 Push: Inserts a new element on top of the stack
 Pop: deletes top element from the stack
 Stack can be viewed as a linear array of elements
 Insertion and deletion is restricted to one end of the array
 Stack must have a maximum capacity, so
 When stack is full, no element should be pushed
 When stack is empty, no element should be popped
Chapter 3: Stack Management & Procedures CCSE YANBU 3
8086 Runtime Stack
 Runtime stack:
Array of successive memory locations managed by the processor
using two registers
1. Stack Segment register SS
2. Stack Pointer register SP
 SP register points to the top of stack
 Always points to last data item placed on the stack

 Only words can be pushed or popped

Chapter 3: Stack Management & Procedures CCSE YANBU 4


Runtime
TheStack
Stack Allocation
is built like that
because instructions and
 Operating system allocates memory area for the stack
data of a program are usually
 Runtime stack is initially empty
at the bottom of the memory
 SP is initialized by the operating system and automatically modified by the
and the Stack in the top of
processor with PUSH, POP, RET and CALL instructions
the memory
 The current address pointed by SS:SP is the top of the Stack
 The stack grows downwards toward lower memory addresses
 SP is decremented to allocate stack memory
16 BITS
¿ High Address
Full Space
POP SP
PUSH SP-2
¿ Free Space

Low Address
Chapter 3: Stack Management & Procedures CCSE YANBU 5
Stack Instructions
 Two basic stack instructions:
 PUSH source
 POP destination
 Source can be
 General-purpose register
 Segment register: CS, DS, SS, ES
 Memory operand (memory-to-stack transfer is allowed)
 Destination can be
 General-purpose register
 Segment register, except that “POP CS” is NOT allowed
 Memory operand (stack-to-memory transfer is allowed)

Chapter 3: Stack Management & Procedures CCSE YANBU 6


Push Instruction
 PUSH source
1. SP is first decremented by 2
SP = SP – 2 (stack grows by 2 bytes)
2. 16-bit source is then copied onto the stack at the new SP :
stack  source ([SP] = source)
 Operating system puts a limit on the stack capacity
 Push can cause a Stack Overflow (stack cannot grow)

MOV AX,86
PUSH AX
MOV AX,70 SP 45 300
PUSH AX SP 86 298
SP 70 296
SP 298
300
296
Chapter 3: Stack Management & Procedures CCSE YANBU 7
Examples on the Push Instruction
• Suppose we execute: The stack grows
– PUSH AX ; AX = 80FFh downwards
– PUSH BX ; BX = 2Eh The area below SP is
free
– PUSH CX ; CX = B61Dh
BEFORE AFTER

2FFB4 SP 2FFB4

2FFB2 2FFB2 80FF

2FFB0 2FFB0 002E

2FFAE 2FFAE B61D SP

2FFAD 2FFAD

Chapter 3: Stack Management & Procedures CCSE YANBU 8


Pop Instruction
 POP destination
1. 16-bit word at SP is first copied into destination
destination  Stack (destination = [SP])
2. SP is then incremented by 2
SP = SP + 2 (stack shrinks by 2 bytes)
 Popping from an empty stack causes a stack underflow

POP AX
POP BX
SP 45 300
AX 70
SP 86 298
BX 86 SP 70 296

SP 298
300
296
Chapter 3: Stack Management & Procedures CCSE YANBU 9
Examples on the Pop Instruction

 Suppose we execute:  The stack shrinks upwards


– POP SI ; SI = B61Dh  The area at & above SP is
allocated
– POP DI ; DI = 002Eh

BEFORE AFTER

2FFB4 2FFB4

2FFB2 80FF 2FFB2 80FF SP

2FFB0 002E 2FFB0 002E


002E

2FFAE B61D SP 2FFAE B61D

2FFAD 2FFAD

Chapter 3: Stack Management & Procedures CCSE YANBU 10


Stack & code Location
 The stack and the program grow in the opposite direction to
decrease the risk of collision between the code and the Stack in the
case where they are placed in the same segment (SS = CS)
Separate Single
Segments Segments
FFFF
FFFF SP
IP

Stack growth
Code growth SP
CS IP
0000
0100
SP IP
Stack growth Code growth

SS SP ES SS DS CS IP
0000 0000
Chapter 3: Stack Management & Procedures CCSE YANBU 11
Uses of the Runtime Stack
 Runtime Stack can be utilized for
 Temporary storage of data and registers
 Transfer of program control in procedures and interrupts
 Parameter passing during a procedure call
 Allocating local variables used inside procedures

 Stack can be used as temporary storage of data


Example: exchanging two variables in a data segment

push var1 ; var1 is pushed


push var2 ; var2 is pushed
pop var1 ; var1 = var2 on stack
pop var2 ; var2 = var1 on stack

The value popped by POP is the last value stacked;


that is why we are talking about LIFO

Chapter 3: Stack Management & Procedures CCSE YANBU 12


Example: Temporary Storage of Registers
 Example on moving BX to AX

PUSH BX
POP AX

 This example is equivalent to MOV AX,BX, is not very useful, it is better to


use MOV AX,BX.
 The Stack is often used to temporarily save the contents of registers
 Push and Pop are especially useful because we don’t have too much
registers to operate with, so
 Stack is often used to free a set of registers

PUSH BX ; save BX
PUSH CX ; save CX
. . .
MOV BX,adr ; BX and CX can now be modified
ADD CX,[Bx]
. . .
POP CX ; restore CX first, then
POP BX ; restore BX
Chapter 3: Stack Management & Procedures CCSE YANBU 13
Example: Nested Loop
 When writing a nested loop, push the outer loop counter CX before
entering the inner loop, and restore CX after exiting the inner loop
and before repeating the outer loop

MOV CX,100 ; set outer loop count


L1: . . . ; begin the outer loop
PUSH CX ; save outer loop count

MOV CX,20 ; set inner loop count


L2: . . . ; begin the inner loop
. . . ; inner loop
LOOP L2 ; repeat the inner loop

. . . ; outer loop
POP CX ; restore outer loop count
LOOP L1 ; repeat the outer loop

Chapter 3: Stack Management & Procedures CCSE YANBU 14


Push/Pop All Registers & Flags
 PUSHA
 Pushes all the general-purpose registers
 AX, CX, DX, BX, SP, BP, SI, and DI in this order
 Initial SP value is pushed
 SP = SP – 16
 POPA
 Pops into registers DI through AX in reverse order of PUSHA
 SP is not read from stack. It is computed as: SP = SP + 16

 PUSHF & POPF


 Special Stack instructions for pushing and popping flags
 Useful for saving and restoring the flags
Chapter 3: Stack Management & Procedures CCSE YANBU 15
Declaring a Stack
 To use a Stack in assembler, you must declare a stack segment,
and reserve sufficient space.
 It is necessary to initialize the SS and SP registers to point the top
of the stack.
 Exemple Stack declaration of 200 bytes

Stack SEGMENT stack ; Stack beginning


DW 100 dup (?) ; space reservation
base_Stack EQU this word
ENDS
To initialize SP, assembler locates the address of the bottom of the stack
 Older assemblers need initialization of SS & SP
 SS can be initialized like DS
 SP needs the address of the bottom of the stack to be defined
Chapter 3: Stack Management & Procedures CCSE YANBU 16
Next

 Runtime Stack and Stack Operations


 Defining and Using Procedures

Chapter 3: Stack Management & Procedures CCSE YANBU 17

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy