The Stack

Anyone who has studied Computer Science have heard about The Stack at least once, a data structure used as a fundamental block inside a process.

A Tasty Stack 😋

A stack allows just 2 operation one is to put(PUSH) pancake and other is to eat(POP) the top pancake. So with PUSH you add the data to the top of stack and with POP you remove the data from top of the stack, you can eat from the middle 😢.

X-Ray of a process

Any program in the state of execution can be is a process. When ever we execute a program it looks something like this on a 32 bit computer.

Lets go through each section one by one.

Text Segment: Also called as code segment contains instructions for the program to execute, basically compiled code. Any attempt to modify this section of memory will crash the program because it is marked as read-only.

Data Segment: This section contains information regarding all the variables declared or used in the program. All the variables which are assigned a value are stored inside the initialized data segment, and once which are just declared are stored inside uninitialized data segment aka .bss(Block Starting Symbol) which tell the Operating System that I have a variable which will require N number of bytes during the life cycle of the program.

Dynamic Memory: There are instances where a developer is not aware of how much memory will be required for the program during execution, the memory usage can increase/decrease throughout the execution. Its divided into 2 section Stack and Heap. Heap allows developers to allocate/reallocate/free the memory whenever required.

Now The Stack

We know that the stack supports two operations PUSH & POP which works in Last In First Out(LIFO) fashion that means the data which is inserted last is the first to be removed. There are 2 registers ESP(Stack Pointer) points to the top of stack and EBP(Stack Base Pointer) which points to the base of the stack.

So for starters lets see the PUSH & POP operations in action and how the stack related registers get updated. There are PUSH & POP operations on the top left, the table below that contains details about the registers being updated and finally on the right show the changes in the stack values as those operations are executed. Observe the reducing value of the addresses of the stack as it grows from higher memory address to lower memory address.

Wait I forgot the return address

Whenever a new function call is made the program stores next value of instruction pointer(EIP) on the stack, so once the execution of the function is completed the program knows where to continue the execution. This is called the Return address. Once the execution of the function is completed the return address is restored to the EIP with the help of RET instruction.

The processor does not keep track of the location of the return-instruction pointer. It is thus up to the programmer to ensure that stack pointer is pointing to the return-instruction pointer on the stack, prior to issuing a RET instruction. A common way to reset the stack pointer to the point to the return-instruction pointer is to move the contents of the EBP register into the ESP register. If the EBP register is loaded with the stack pointer immediately following a procedure call, it should point to the return instruction pointer on the stack.

Intel® 64 and IA-32 Architectures Software Developer’s Manual Volume 1 https://www.intel.com/content/www/us/en/developer/articles/technical/intel-sdm.html

Lets take the following program as example.

#include<stdio.h>
#include<string.h>
// gcc file_name.c -o stacker
int add_val(int a, int b){
	int c;
	c=a+b;
	return 0;
}
int main(int argc, char *argv[]){
	add_val(10,23);
        return 0;
}

The above program is pretty simple it calls a function named add_val passes two values add them and return’s zero like a good boy.

The GIF below will help us confirm that address of the next instruction was pushed on the stack after the call is made to the add_val function.

Hello Stack Frame

A stack frame is created whenever a function call is performed. Its used for storing the function parameters, local variables, return address and register values.

Lets take example of the add_val function from the previous GIF, observe the starting 2 instructions of the function are used for setting the up the stack frame for new function and at the same time its storing the values of previous functions frame.

0x0000118d:	push   ebp
0x0000118e:	mov    ebp,esp
0x00001190:	sub    esp,0x10
0x00001193:	mov    edx,DWORD PTR [ebp+0x8]
0x00001196:	mov    eax,DWORD PTR [ebp+0xc]
0x00001199:	add    eax,edx
0x0000119b:	mov    DWORD PTR [ebp-0x4],eax
0x0000119e:	mov    eax,0x0
0x000011a3:	leave  
0x000011a4:	ret    

The life time of stack frame is same as the function, once the execution of the current function is completed the old stack frame is restored. This is done using LEAVE instruction.

The LEAVE instruction, which does not have any operands, reverses the action of the previous ENTER instruction.The LEAVE instruction copies the contents of the EBP register into the ESP register to release all stack space allocated to the procedure. Then it restores the old value of the EBP register from the stack. This simultaneously restores the ESP register to its original value. A subsequent RET instruction then can remove any arguments and the return address pushed on the stack by the calling program for use by the procedure.

Intel® 64 and IA-32 Architectures Software Developer’s Manual Volume 1 https://www.intel.com/content/www/us/en/developer/articles/technical/intel-sdm.html

The above topics should have given you idea about What, Why and How for the stack.

1 thought on “The Stack”

Leave a Comment