Stack is a very basic ADT (Abstract Data Type) that has many implementations in computer-related fields. The characteristics of a stack can be summarized as follows:
| Stack is a LIFO (Last-in First-out) process.
| | Because of that, Stack must have a pointer called Top of Stack (TOS) that always refers to the top of stack.
| | Stack then must have two mandatory methods. The first is called Push(). The second is called Pop(). The push method is needed to put a new item into the stack and the pop method is intended for removing an item from the top of stack.
| | Stack must also have a base. From which the stack grows up and shrinks down.
|
Pentium processors have a built-in stack. This stack is implemented as an array of consecutive memory cells. Because of array characteristic, the stack in Pentium has limited size.
| SS:[SP] always points to the top of stack.
| | SS:[0] is the end of the stack. At this point, the stack is full.
| | Initially, SS:[SP] is set to the particular address that depends upon the stack size. This address may be viewed as the base. At this point, the stack is empty. The size of the stack is defined by the programmer using directive .STACK.
.STACK 200h means the maximum size of the stack is fixed to 512 bytes.
| | Every time a new item is about to be put to the stack, the content of SP register is decremented first and then the new item is placed in that location. This mechanism ensures that SS:[SP] always points to the top of stack.
| | Every time the last item put in the stack is going to be removed, Pentium reads the item from the location referred by SS:[SP] and stores it in another location. After that, Pentium adjusts the TOS by incrementing SS:[SP].
| | However, Pentium can push/pop only either word (16-bit) or doubleword (32-bit) data.
| |
Pentium Stack Instructions and Directives
Pentium has two very basic but powerful stack instructions:
| PUSH
| | POP
|
Instruction | Operand | Note
| PUSH | r/m16 r/m32 imm Sreg | If (the size of operand is 32-bit) { SP=SP-4 ;SS:[SP]=operand (dword assignment)} If (the size of operand is 16-bit){ SP=SP-2 SS:[SP]=operand (word assignment)}
| POP | r/m16 r/m32 Sreg | POP CS is invalid If (the size of operand is 32-bit)N=4 If (the size of operand is 16-bit) N=2 Read N bytes from memory at location SS:SP and store at operand Operand=SS:[SP] SP= SP + N |
|
MASM provides us a special directive to initialize the size of the stack segment:
The directive .STACK 200h in the beginning of your source code tells the assembler to set information in the program that when the program runs it will initialize the content of SP to 512.
|
Stack Usage
The Stack and its instructions can be utilized for
| preserving temporarily data and registers.
| | supporting procedure and interrupt mechanism.
| | passing parameters into procedures.
| | allocating local variables.
|
Pentium Advanced Stack Instructions
Instruction | Operand | Note
| PUSHF | | It pushes the rightmost 16-bit EFLAG register onto the stack.
| POPF | | It pops up the 16-bit data from SS:[SP] and stores into the righmost 16-bit EFLAG register.
| PUSHFD | | It pushes the content of EFLAGS register onto the stack.
| POPFD | | It pops up the 32-bit data from SS;[SP] and stores into EFLAGS register.
| PUSHA | | It is equivalent to instructions: Temp=SP PUSH AX PUSH CX PUSH DX PUSH BX PUSH Temp PUSH BP PUSH SI PUSH DI
| POPA | | It is complementary for PUSHA instruction. It pops up DI first, then SI, BP, skip for SP, pops up BX, DX, CX, and finally AX.
| PUSHAD | | It is 32-bit version of PUSHA instruction. Temp=ESP PUSH EAX PUSH ECX PUSH EDX PUSH EBX PUSH Temp PUSH EBP PUSH ESI PUSH EDI
| POPAD | | It reverses a previous PUSHAD instruction, restoring the general registers to their values before the PUSHAD instruction was executed. POP EDI POP ESI POP EBP throwaway=POP() POP EBX POP EDX POP ECX POP EAX
|
|
The following examples illustrate some useful applications of the stack instructions.
Example 1: Assign DX:AX into EAX |
push DX
push AX
pop EAX
|
Example 2: Assign CS into DS |
push CS
pop DS
|
Example 3: Set CF=PF=1 |
pushf
pop AX
or AX, 5
push AX
popf
For 8086, the equivalent code is
LAHF
or AH, 5
SAHF
|
|