Lecture 15 8086 Stack
Lecture 15 8086 Stack
Lecture 15
1
Stack Characteristics
• Used to store temporary data during program execution
• One point of access - the top of the stack
• A stack is always operated as Last-In-First-Out (LIFO) storage,
i.e., data are retrieved in the reverse order to which they were
stored
• Instructions that directly manipulate the stack
PUSH - place element on top of stack
POP - remove element from top of stack
2
The Stack Use
• To store
registers
return address information while procedures are
executing
local variables that procedures may require
• To pass parameters to procedures
3
Stack Implementation in Memory
4
• SS - Stack Segment
• SP (stack pointer) always points to the top of the stack
SP initially points to top of the stack (high memory address).
SP decreases as data is PUSHed
PUSH AX ==> SUB SP, 2 ; MOV [SS:SP], AX
SP increases as data is POPed
POP AX ==> MOV AX, [SS:SP] ; ADD SP, 2
5
PUSH example
6
POP example
7
PUSH and OP
• PUSH and POP always store or retrieve words of data
(never bytes) in the 8086-80286 microprocessor
• The 80386/80486 allow words or double words to be
transferred to and from the stack
• The source of data for PUSH
any internal 16-bit/32-bit register, immediate data, any segment
register, or any two bytes of memory data
8
PUSH &POP ctd
• The 80286 and later microprocessors there is also
PUSHA and POPA to store and retrieve,
respectively the contents of internal register set
(AX, CX, DX, BX, SP, BP, SI, and DI)
9
Exercise
1. Write a code fragment that initialises the 8086 stack to SS:SP=
1000:0000
10
REMEMBER
11
PUSH Examples
12
PUSH AX
PUSH AX execution results in the following:
• SP SP - 1 ;SP is decremented
• SS:SP <= AH ;AH is PUSHed on the Stack
• SP SP - 1 ;SP is decremented
• SS:SP <= AL ;AL is PUSHed on the Stack
13
Syntax for PUSH instruction
PUSH REG
PUSH SREG
PUSH memory
PUSH immediate
14
POP AX
POP AX execution results in the following:
• AL SS:SP ;AL is POPped from the Stack
• SP SP + 1 ;SP is incremented
• AH <= SS:SP ;AH is POPped from the Stack
• SP SP + 1 ;SP is incremented
15
Syntax for POP instruction
POP REG
POP SREG
POP memory
16
A program to show PUSH and POP
operations
org 100h
mov ax, 1234h
push ax
pop ax
ret
17
Stack Frame
• Information related to each function CALL is stored in the
stack called ‘stack frame,’ which has
Return address,
Local variables and,
Arguments for calling functions.
18
Subroutine
• A subroutine is a set of code that can be
branched to and returned from such a way that
the code is as it were inserted at the point
• The branch to a subroutine is referred to as
CALL and the corresponding branch back is
known as RETURN.
• The return is always made to the instruction
immediately following the call
19
Subroutine ctd
• Requirements of subroutine:
A procedure CALL must save the addresses of
the next instruction, so that the return will be
able to branch back to the proper pace in the
calling program.
The registers used by the procedure need to be
stored before their contents are changed and
then restored just before the procedure is
exited.
20
The CALL and RET Instructions
21
Related Instructions
• Flags:
PUSHF (push flags) instruction copies the contents of the flag
register (Flags) to the stack
POPF instruction retrieves and loads the (FLAGS) from the stack
PUSHFD and POPFD push and pop 32-bit registers
• General-purpose registers
PUSHA instruction copies contents of the internal register set, except
the segment registers, to the stack.
PUSHA (push all) instruction copies the registers to the stack in the
following order: AX, CX, DX, BX, SP, BP, SI, and DI
POPA pops the same registers off the stack in reverse order
PUSHAD pushes the 32-bit general-purpose registers on the stack
order: EAX, ECX, EDX, EBX, ESP, EBP, ESI, EDI
POPAD pops the same registers (32-bit general-purpose registers) off
the stack in reverse order
22
Programming for
Stack
23
Program Structure
TITLE……..
.MODEL
.STACK
.DATA
…………..
……………
.CODE
Main PROC
……………..
……………..
Main ENDP
END Main
24
Description
• TITLE: identifies the program listing title. Any text typed to the right
side of the directive is printed at the top of each page in the listing file
• MODEL: selects a standard memory model for the programs
• STACK sets the size of the program stack which may be any size up
to 64kb
• DATA all variables pertaining to the program are defined in the area
following this directive called data segment
• CODE identifies the part of the program that contains instructions.
• PROC creates a name and address for the beginning of a procedure
• ENDP indicates the end of procedure
• END terminates assembly of the program. Any lines of text placed
after this directive is ignored.
25
Example of subroutine
26
Passing Parameters on the Stack
• Stack can be used to pass parameter(s) to a procedure
• The caller pushes the procedure parameter onto the stack and the
callee finds the parameter there
• When the procedure completes its task, the parameter should be
popped from the stack
27
Procedures at a glance
• Group of instructions that usually perform one task
• Reusable section of the software that is stored in memory once, but use as often
as necessary
• The stack stores the return address whenever a procedure is called during the
execution of the program
CALL pushes the address of the instruction following it on the stack
RET removes an address from the stack so the program returns to the instruction
following the call
PROC NEAR My_Subroutine PROC FAR My_Subroutine RET
EENG4005
• JUMP Segment My_Subroutine:Offset My_Subroutine
Near calls and returns transfer
Far calls and returns pass control between
control between procedures in the
different segments
same code segment
Procedures ctd
• Parameters to a procedure can be passed in
on the stack
global memory locations
registers
in the code stream
in a parameter block reference by a pointer
29
Macros
• A macro inserts a block of statements at various points in a
program during assembly
EENG4005
Macros (cont.)
• General Format
MACRO_NAME MACRO Param1,Param2,...,ParamN
LOCAL MyLabel
Your Code ...
... Param1 ...
...Param2 ...
Your Code ...
JMP MyLabel
Your Code ...
MyLabel:
... ParamN ...
EENG4005
Your Code ...
ENDM
Local Variable(s) in a Macro
• A local variable is one that appears in the macro, but is not available outside the macro
• We use the LOCAL directive for defining a local variable
If the label MyLabel in the previous example is not defined as local, the assembler will flag it
with errors on the second and subsequent attempts to use the macro
• The LOCAL directive must always immediately follow the MACRO directive without
any intervening comments or spaces
• Macros can be placed in a separate file
use INCLUDE directive to include the file with external macro definitions into a program
no EXTERN statement is needed to access the macro statements that have been included
EENG4005
Problems
33
Problem 1: Write an ALP that uses the stack to modify contents
of a register
Problem 2: write an ALP where two registers use the stack for
exchanging the values:
34
Problems ctd
Problem 3: Write an ALP to evaluate an expression
(A+B)*(C+D), where A,B,C and D are the hexadecimal bytes.
STORE INTERIM RESULTS ON STACK
35
Problems ctd
Problem 5: An ALP to compare two 16 bit numbers stored in the
AX and BX registers. If both are equal they increment SI
register.
36
Problems ctd
Problem 8: Write an ALP to find the sum of series of data. The
length of the array is stored in a location whose 16-bit offset
address is 8100h. the series begins from the location, whose
offset 16-bit address is 8102h. Store the result in location whose
16-bit offset is 8150h
37