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

8.EEE 342 08 Stack Programming

The document discusses stack and stack programming in assembly language, including how a stack works as a LIFO data structure using push and pop operations, how the stack pointer register is used, and examples of using push and pop instructions to implement stacks in assembly language programs. It also provides examples of stack programming code and diagrams illustrating how data is stored in the stack memory.

Uploaded by

Rehan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
54 views

8.EEE 342 08 Stack Programming

The document discusses stack and stack programming in assembly language, including how a stack works as a LIFO data structure using push and pop operations, how the stack pointer register is used, and examples of using push and pop instructions to implement stacks in assembly language programs. It also provides examples of stack programming code and diagrams illustrating how data is stored in the stack memory.

Uploaded by

Rehan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

Microprocessor Systems and

Interfacing
EEE 342
Stack and Stack Programming

◼ Outline
❑ Stack
❑ Stack Programming in assembly language

Saturday, April 27, 2019 2


Stack
◼ Stack
❑ Access is allowed only at one point of the structure,

normally termed the top of the stack


❑ access to the most recently added item only

◼ Operations are limited:


❑ push (add item to stack)
❑ pop (remove top item from stack)
◼ Described as a "Last In First Out"
(LIFO) data structure

Saturday, April 27, 2019 3


Stack

Saturday, April 27, 2019 4


Stack

Saturday, April 27, 2019 5


Stack

Saturday, April 27, 2019 6


Stack Pointer
◼ It is a 16-bit register, contains the address of the data
item currently on top of the stack.

◼ Stack operation includes pushing (providing) data on to


the stack and popping (taking)data from the stack.

◼ Pushing operation decrements stack pointer and


Popping operation increments stack pointer. i.e. there is
a last in first out (LIFO) operation.

Saturday, April 27, 2019 7


Push
◼ Source can be register, segment register or memory.
◼ This instruction pushes the contents of specified source
on to the stack.
◼ In this stack pointer is decremented by 2.
◼ The higher byte data is pushed first (SP-1).
◼ Then lower byte data is pushed (SP-2).
❑ E.g.:
❑ (1). PUSH AX;
❑ (2). PUSH DS;
❑ (3). PUSH [5000H];

Saturday, April 27, 2019 8


Push INITIAL POSITION

(1) STACK
POINTER
DECREMENTS SP & STORES HIGHER
BYTE

(2) STACK HIGHER BYTE


POINTER
DECREMENTS SP & STORES LOWER
BYTE
(3) STACK
LOWER BYTE
POINTER
HIGHER BYTE

Saturday, April 27, 2019 9


Push BEFORE EXECUTION
SP 2002H
2000H
BH BL
2001H
CH 10 CL 50
DH DL
2002H
PUSH CX
AFTER EXECUTION
SP 2000H 50
2000H
BH BL
2001H 10
CH 10 CL 50

DH DL 2002H

Saturday, April 27, 2019 10


Saturday, April 27, 2019 11
PUSH Instruction
Example
To
address
12FFF
Register Array
PUSH BX 03800
AX
6A 037FF
BX 6AB3 6AB3
B3 037FE
CX
DX

SP 0800

SS 0300 03000
STACK segment
EENG4005
Pop
◼ Destination can be register, segment register or memory.
◼ This instruction pops (takes) the contents of specified
destination.
◼ In this stack pointer is incremented by 2.
◼ The lower byte data is popped first (SP+1).
◼ Then higher byte data is popped (SP+2).
❑ E.g.
❑ (1). POP AX;
❑ (2). POP DS;
❑ (3). POP [5000H];

Saturday, April 27, 2019 13


Pop INITIAL POSITION AND READS LOWER
BYTE
(1) STACK LOWER BYTE
POINTER HIGHER BYTE

INCREMENTS SP & READS HIGHER BYTE


LOWER BYTE
(2) STACK HIGHER BYTE
POINTER

INCREMENTS SP

LOWER BYTE
HIGHER BYTE
(3) STACK
POINTER
Saturday, April 27, 2019 14
Pop
BEFORE EXECUTION
2000H 30
SP 2000H
2001H 50
BH BL
2002H

POP BX
AFTER EXECUTION
2000H 30
SP 2002H
2001H 50
BH 50 BL 30
2002H

Saturday, April 27, 2019 15


POP Instruction
Example
To
address
0FFFF
Register Array
01008
AX POP BX
39 01007
BX 392F 392F
2F 01006
CX
DX

SP 1006

SS 0000 00000
STACK segment
EENG4005
Stack Programming

◼ MOV DX, 02004H


◼ MOV SP, DX
◼ MOV DX, 0A000H
◼ MOV SS, DX
◼ MOV AX, 0ACADH
◼ MOV BX, 1000H
◼ PUSH AX
◼ PUSH BX
◼ ADD AX, BX
◼ PUSH AX
◼ POP CX
◼ POP DX

Saturday, April 27, 2019 17


STACK Program
◼ MOV DX, 02004H
◼ MOV SP, DX
◼ MOV DX, 0A000H
◼ MOV SS, DX
◼ MOV AX, 0ACADH
◼ MOV BX, 1000H
◼ PUSH AX
◼ PUSH BX
◼ ADD AX, BX
◼ PUSH AX
◼ POP CX
◼ POP DX
Example
◼ Find the contents of AX after execution of this code

◼ Mov DX,4000H
◼ Mov SS,Dx
◼ Mov SP,0200H
◼ MOV BX, 4510H
◼ PUSH BX
◼ Mov BX, 0ABH,
◼ Mov AX, SS:[1FFH]

◼ AX = 0045H
Example
◼ Suppose SS = 4000H, SP = 0200H. For the following given Code

MOV BX, 4510H


PUSH BX
ADD BX, 20
PUSH BX
POP CX
ADD BX, CX
PUSH BX
◼ Determine the values of ‘BX’ and ‘SP’ Registers after the execution
of the above code.
◼ Sketch the stack block diagram illustrating the values stored in the
stack with real memory addresses (make sure to show where BL
and BH are stored).
Example
◼ Write a program to store 8 words of data 0011H, 0301H, 045H,
0447H, 0A45H, 05685H,5826H to a stack with SS = A000H, SP =
0500H. Also sketch the stack block diagram illustrating the values
stored in the stack with real memory addresses.

MOV AX, A000H,


MOV SS, AX
MOV DX, 0500H
MOV SP, DX
MOV AX, 011H
PUSH AX
MOV AX, 0301H
PUSH AX
MOV AX, 045H
PUSH AX
……..

You might also like