Stack POP Operation through 8051 Microcontroller

Basic architecture of a stack:-
A typical stack, storing local data and call information for nested procedure calls (not necessarily nested procedures!). This stack grows downward from its origin. The stack pointer points to the current topmost datum on the stack. A push operation decrements the pointer and copies the data to the stack; a pop operation copies data from the stack and then increments the pointer. Each procedure called in the program stores procedure return information (in yellow) and local data (in other colors) by pushing them onto the stack. This type of stack implementation is extremely common, but it is vulnerable to buffer overflow attacks (see the text).
A typical stack is an area of computer memory with a fixed origin and a variable size. Initially the size of the stack is zero. A stack pointer, usually in the form of a hardware register, points to the most recently referenced location on the stack; when the stack has a size of zero, the stack pointer points to the origin of the stack.
The two operations applicable to all stacks are:
a push operation, in which a data item is placed at the location pointed to by the stack pointer, and the address in the stack pointer is adjusted by the size of the data item;
a pop or pull operation: a data item at the current location pointed to by the stack pointer is removed, and the stack pointer is adjusted by the size of the data item.

In computer science, a stack is a last in, first out (LIFO) abstract data type and linear data structure. A stack can have any abstract data type as an element, but is characterized by only three fundamental operations: push, pop and stack top. The push operation adds a new item to the top of the stack, or initializes the stack if it is empty. If the stack is full and does not contain enough space to accept the given item, the stack is then considered to be in an overflow state. The pop operation removes an item from the top of the stack. A pop either reveals previously concealed items, or results in an empty stack, but if the stack is empty then it goes into underflow state (It means no items are present in stack to be removed). The stack top operation gets the data from the top-most position and returns it to the user without deleting it. The same underflow state can also occur in stack top operation if stack is empty.
A stack is a restricted data structure, because only a small number of operations are performed on it. The nature of the pop and push operations also means that stack elements have a natural order. Elements are removed from the stack in the reverse order to the order of their addition: therefore, the lower elements are those that have been on the stack the longest
Microcontroller have a hardware call stack, which is used to save return addresses. The hardware stack is not software accessible on earlier devices, but this changed with the 18 series devices

In modern computer languages, the stack is usually implemented with more operations than just “push”,”pop” and “Stack Top”. Some implementations have a function which returns the current number of items on the stack. Alternatively, some implementations have a function that just returns if the stack is empty. Another typical helper operation stack top[4] (also known as peek) can return the current top element of the stack without removing it.
In most high level languages, a stack can be easily implemented either through an array or a linked list. What identifies the data structure as a stack in either case is not the implementation but the interface: the user is only allowed to pop or push items onto the array or linked list, with few other helper operations. The following will demonstrate both implementations, using C.

The array implementation aims to create an array where the first element (usually at the zero-offset) is the bottom. That is, array[0] is the first element pushed onto the stack and the last element popped off. The program must keep track of the size, or the length of the stack. The stack itself can therefore be effectively implemented as a two-element structure in C:


/**********************STARTING OF PROGRAM FUNCTION********************************************/

//_________________________DELAY SUBROUTINE____________________________________________/

void delay(int a)
int i,j;
for(j=0;j<1275;j++);                                         //CREATING DELAY BY INSTRUCTION

/*************************** MAIN FUNCTION ***********************************************************/

void main()
char d=0x00,b,c;
int n=7,x;
P1=0x00;                                                                         //SET P1 PORT AS OUTPUT PORT
delay(50);                                                                       //CALL DELAY SUBROUTINE
b<<=n;                                                                                //SHIFTING N-TIMES OF DATA ‘b’ IN “LEFT” SIDE
for(x=n;x<7;x++)                                                       //LOOP FOR INCREASING X FROM N TO LESS THAN 7
d=c^b;                                                                                  //X-OR OPERATION B/W DATA ‘C & b’ and STORE RESULT IN DATA ‘d’
P1=d;                                                                                     //SET P1 PORT CORRESPONDING TO DATA ‘d’
delay(50);                                                                          //CALL DELAY SUBROUTINE
b<<=1;                                                                           //SHIFTING ONE-TIME OF DATA ‘b’ IN LEFT SIDE

c=d;                                                                              //COPY OF DATA ‘d’ INTO DATA ‘c’
c=d^b;                                                                         //X-OR OPERATION B/W DATA ‘b & c’ and STORE RESULT IN DATA ‘c’
P1=c;                                                                               //SET P1 PORT CORRESPONDING TO DATA ‘c’
n–;                                                                                    //DECREMENT IN THE VALUE OF N




Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s