Stack in assembly

Theory

To demonstrate, let’s begin with a stack containing one value.
Figure 1

PUSH Operation

Figure 2

Figure 3

POP Operation

Figure 4

Stack Applications

There are several important uses of runtime stacks in programs:

PUSH and POP Instructions

PUSH Instructions

The PUSH instruction first decrements ESP and then copies a source operand into the stack.
A 16-bit operand causes ESP to be decremented by 2. A 32-bit operand causes ESP to be decremented by 4.

There are three instruction formats:
PUSH reg/mem16
PUSH reg/mem32
PUSH imm32

POP Instructions

The POP instruction first copies the contents of the stack element pointed to by ESP into a 16- or 32-bit destination operand and then increments ESP. If the operand is 16 bits, ESP is incremented by 2; if the operand is 32 bits, ESP is incremented by 4:
POP reg/mem16
POP reg/mem32

PUSHFD and POPFD Instructions

pushfd
popfd
pushfd ; save the flags
;
; any sequence of  statements here...
;
popfd ; restore the flags

The need for precise documentation is critical!
A less error-prone way to save and restore the flags is to push them on the stack and immediately pop them into a variable:

.data
saveFlags DW 0
.code
pushfd ; push flags on  stack
pop saveFlags ; copy into  a variable
push saveFlags ; push  saved flag values
popfd ; copy into the  flags

PUSHAD, PUSHA, POPAD, and POPA

If you write a procedure that modifies a number of 32-bit registers, use PUSHAD at the beginning of the procedure and POPAD at the end to save and restore the registers. The following code fragment is an example:

pushad ; save  general-purpose registers
.
.
mov eax,...
mov edx,...
mov ecx,...
.
.
popad ; restore  general-purpose registers
ReadValue PROC
pushad ; save  general-purpose registers
.
.
mov eax,return_value
.
.
popad ; overwrites EAX!
ret
ReadValue ENDP

Examples

AddTwo:

push ebp; saving the caller’s  stackframe base for further being able to restore it
mov ebp,esp ; initialising the base of the new stack frame for the currently executing
; procedure AddTwo  (see the picture below which illustrates exactly
; this described situation)

mov eax,[ebp + 12] ; transferring into EAX the value of the second parameter passed
; on to the stack by the caller BEFORE the new stackframe takes
; the run time control
add eax,[ebp + 8] ; adding to EAX the first parameter
pop ebp ; restoring the caller stackframe as being the new currently executing one
ret ; going back immediately to the point of call for continuing the execution
;  of the program

 

Stack Frame after Pushing EBP and ESP value was copied to EBP:

After the next two instructions (mov and add) execute, the following figure shows the contents of the stack frame: a function call such as AddTwo(5, 6) would cause the second parameter to be pushed on the stack, followed by the first parameter:

AddTwo could push additional registers on the stack without altering the offsets of the stack parameters from EBP. ESP would change value, but EBP would not.