Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 37

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

• BP (base pointer) can point to any element on the stack

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

• The POP places data into


 internal register, segment register (except CS), or a memory
location

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

2. Calculate the 20 bit ending address of the stack segment given


that it is 64kb.

10
REMEMBER

• We must make sure to create a Stack, and make it large enough


for any program that has a CALL or any program that will use
the PUSH and POP instructions.
•  PUSH all registers that contain data or addresses that we need
after the RETurn
• When we RETurn we must POP the registers in the reverse order
of the PUSH
• We must POP each register that we PUSH
• If we PUSH before the CALL, we must POP after the RETurn
(in the calling module or subroutine)
• If we PUSH after the CALL, we must POP before the RETurn
(inside the subroutine)

11
PUSH Examples

• PUSH AX – the contents of a 16-bit register


• PUSH EBX - the contents of a 32-bit register
• PUSHA (286 and higher) – Preserves all usable registers of
80286
• PUSHAD (386 and higher) – Preserves all usable registers of
80386
• There are corresponding POP instructions for each of the above
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

REG: AX, BX, CX, DX, DI, SI, BP, SP.

SREG: DS, ES, SS, CS.

memory: [BX], [BX+SI+7], 16 bit variable, etc...

immediate: 5, -24, 3Fh, 10001101b, etc...

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

REG: AX, BX, CX, DX, DI, SI, BP, SP.

SREG: DS, ES, SS, (except CS).

memory: [BX], [BX+SI+7], 16 bit variable, etc...

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.

• Each stack frame is appended to the previous frame and the


register BP (base pointer) is used to specify the boundary of
current frame.
• BP – Base Pointer – contains an assumed offset from the SS
register. Often used by a subroutine to locate variables that
were passed on the stack by a calling program.

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

• CALL branches to the indicated address and also pushes the


return address on the stack.
• CALL ProcName
• The CALL instruction makes an unconditional jump to a
subroutine and pushes the address of the next instruction on the
stack.
• RET instruction gets (POPS) the return address from the stack
and jumps to it (offset). Return from procedure to calling
program

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

• At execution time when the


processor encounters the
CALL instruction
 it pushes the return address
(instruction pointer of the
next instruction after the
CALL) onto the stack
 Then jumps to Addnum and
executes the code there

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

• PUSH IP • PUSH CS POP (CS:) IP


• JUMP Offset My_Subroutine • PUSH IP

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

• Text substitutions made at compile time


 NOT a procedure -- Code is literally dumped into program
 Parameter names are substituted
 Useful for tedious programming tasks
 Instantiated within code segment.

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

Problem 4: Write an ALP to perform simple unsigned


multiplication. The two 16 bit numbers are 1121h and 1301h.
Store the product in the location whose offset address is 8100h.

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.

Problem 6: An ALP to add two 16bit numbers stored in the AX


and BX registers. If no carry exists after addition increment SI
register.

Problem 7: Write an ALP to find the greatest number in a given


series of 8-bit numbers. The length of the series is stored in a
location whose 16 bit offset address is 8100h. the series begins
from the location whose offset address is 8102. Store the result
in the location whose 16-bit offset address is 8150h

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

You might also like