Introduction to 8086 Assembly Language Programming( alp5) 15
push ax
will cause the value of ax to be stored on the stack. In this case the
number 123 is stored on the stack and sp points to the location on the
stack where 123 is stored.
The 8086 pop instruction is used to retrieve a value previously placed on
the stack. The stack pointer is left pointing at the next element on the
stack. Thus pop conceptually removes the value from the stack. Having
stored a value on the stack as above, we can retrieve it by:
pop ax
which transfers the data from the top of the stack to ax, (or any register)
in this case the number 123 is transferred. Information is stored on the
stack starting from high memory locations. As we place data on the
stack, the stack pointer points to successively lower memory locations.
We say that the stack grows downwards. If we assume that the top of the
stack is location 1000 (sp contains 1000) then the operation of push
ax is as follows.
Firstly, sp is decremented by the size of the element (2 bytes for the
8086) to be pushed on the stack. Then the value of ax is copied to the
location pointed to by sp, i.e. 998 in this case. If we then assign bx the
value 212 and carry out a push bx operation, sp is again
decremented by two, giving it the value 996 and 212 is stored at this
location on the stack. We now have two values on the stack.
As mentioned earlier, if we now retrieve these values, we encounter the
fundamental feature of any stack mechanism. Values are retrieved in
reverse order. This means that the last item placed on the stack, is the
first item to be retrieved. We call such a process a Last-In-First-Out
process or a LIFO process.