{"id":41,"date":"2024-04-15T23:59:28","date_gmt":"2024-04-15T18:29:28","guid":{"rendered":"https:\/\/crazynoob.net\/?p=41"},"modified":"2024-04-16T00:15:49","modified_gmt":"2024-04-15T18:45:49","slug":"the-stack","status":"publish","type":"post","link":"https:\/\/crazynoob.net\/index.php\/2024\/04\/15\/the-stack\/","title":{"rendered":"The Stack"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Anyone who has studied Computer Science have heard about <em>The Stack<\/em> at least once, a data structure used as a fundamental block inside a process.<\/p>\n\n\n\n<div class=\"wp-block-cover\"><span aria-hidden=\"true\" class=\"wp-block-cover__background has-background-dim\"><\/span><img loading=\"lazy\" decoding=\"async\" width=\"444\" height=\"333\" class=\"wp-block-cover__image-background wp-image-44\" alt=\"\" src=\"https:\/\/crazynoob.net\/wp-content\/uploads\/2024\/02\/stack1.gif\" data-object-fit=\"cover\"\/><div class=\"wp-block-cover__inner-container is-layout-flow wp-block-cover-is-layout-flow\">\n<p class=\"has-text-align-center has-large-font-size wp-block-paragraph\">A Tasty Stack \ud83d\ude0b<\/p>\n<\/div><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">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 \ud83d\ude22.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"X-Ray_of_a_process\"><\/span><em>X-Ray of a process<\/em><span class=\"ez-toc-section-end\"><\/span><\/h2><div id=\"ez-toc-container\" class=\"ez-toc-v2_0_91 counter-hierarchy ez-toc-counter ez-toc-grey ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/crazynoob.net\/index.php\/2024\/04\/15\/the-stack\/#X-Ray_of_a_process\" >X-Ray of a process<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/crazynoob.net\/index.php\/2024\/04\/15\/the-stack\/#Now_The_Stack\" >Now The Stack<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/crazynoob.net\/index.php\/2024\/04\/15\/the-stack\/#Wait_I_forgot_the_return_address\" >Wait I forgot the return address<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/crazynoob.net\/index.php\/2024\/04\/15\/the-stack\/#Hello_Stack_Frame\" >Hello Stack Frame<\/a><\/li><\/ul><\/nav><\/div>\n\n\n\n<div class=\"gb-container gb-container-ece7053e\">\n\n<p class=\"wp-block-paragraph\">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.<\/p>\n\n<\/div>\n\n\n<figure class=\"wp-block-image size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"660\" height=\"646\" src=\"https:\/\/crazynoob.net\/wp-content\/uploads\/2024\/02\/Memory-of-program.drawio6.svg\" alt=\"\" class=\"wp-image-67\" style=\"width:902px;height:auto\"\/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Lets go through each section one by one. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Text Segment:<\/strong> 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.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Data Segment:<\/strong> 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 <strong><em>initialized data segment<\/em><\/strong>, and once which are <strong><em>just declared<\/em><\/strong> are stored inside <strong><em>uninitialized data segment<\/em><\/strong> aka <strong>.bss<\/strong>(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.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Dynamic Memory:<\/strong> 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 <strong><em>Stack <\/em><\/strong>and <strong><em>Heap<\/em><\/strong>. Heap allows developers to allocate\/reallocate\/free the memory whenever required.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Now_The_Stack\"><\/span><em>Now The Stack<\/em><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">We know that the stack supports two operations PUSH &amp; 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.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">So for starters lets see the PUSH &amp; POP operations in action and how the stack related registers get updated. There are  PUSH &amp; 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.<\/p>\n\n\n\n<figure class=\"wp-block-gallery has-nested-images columns-default is-cropped wp-block-gallery-1 is-layout-flex wp-block-gallery-is-layout-flex\">\n<figure class=\"wp-block-image size-full is-style-default\"><img loading=\"lazy\" decoding=\"async\" width=\"1280\" height=\"775\" data-id=\"84\" src=\"https:\/\/crazynoob.net\/wp-content\/uploads\/2024\/02\/stackfun.gif\" alt=\"\" class=\"wp-image-84\"\/><\/figure>\n<\/figure>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Wait_I_forgot_the_return_address\"><\/span><em>Wait I forgot the return address<\/em><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">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 <strong>Return address<\/strong>. Once the execution of the function is completed the return address is restored to the EIP with the help of <strong>RET <\/strong>instruction.<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p class=\"wp-block-paragraph\">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.<\/p>\n<cite>Intel\u00ae 64 and IA-32 Architectures Software Developer\u2019s Manual Volume 1 https:\/\/www.intel.com\/content\/www\/us\/en\/developer\/articles\/technical\/intel-sdm.html <\/cite><\/blockquote>\n\n\n\n<p class=\"wp-block-paragraph\">Lets take the following program as example.<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#2e3440ff\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"#include&lt;stdio.h&gt;\n#include&lt;string.h&gt;\n\/\/ gcc file_name.c -o stacker\nint add_val(int a, int b){\n\tint c;\n\tc=a+b;\n\treturn 0;\n}\nint main(int argc, char *argv[]){\n\tadd_val(10,23);\n        return 0;\n}\" style=\"color:#d8dee9ff;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki nord\" style=\"background-color: #2e3440ff\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #5E81AC; font-weight: bold\">#<\/span><span style=\"color: #81A1C1\">include<\/span><span style=\"color: #ECEFF4\">&lt;<\/span><span style=\"color: #8FBCBB\">stdio.h<\/span><span style=\"color: #ECEFF4\">&gt;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #5E81AC; font-weight: bold\">#<\/span><span style=\"color: #81A1C1\">include<\/span><span style=\"color: #ECEFF4\">&lt;<\/span><span style=\"color: #8FBCBB\">string.h<\/span><span style=\"color: #ECEFF4\">&gt;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #616E88\">\/\/ gcc file_name.c -o stacker<\/span><\/span>\n<span class=\"line\"><span style=\"color: #81A1C1\">int<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #88C0D0\">add_val<\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #81A1C1\">int<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #D8DEE9\">a<\/span><span style=\"color: #ECEFF4\">,<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #81A1C1\">int<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #D8DEE9\">b<\/span><span style=\"color: #ECEFF4\">){<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">\t<\/span><span style=\"color: #81A1C1\">int<\/span><span style=\"color: #D8DEE9FF\"> c<\/span><span style=\"color: #81A1C1\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">\tc<\/span><span style=\"color: #81A1C1\">=<\/span><span style=\"color: #D8DEE9FF\">a<\/span><span style=\"color: #81A1C1\">+<\/span><span style=\"color: #D8DEE9FF\">b<\/span><span style=\"color: #81A1C1\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">\t<\/span><span style=\"color: #81A1C1\">return<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #B48EAD\">0<\/span><span style=\"color: #81A1C1\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ECEFF4\">}<\/span><\/span>\n<span class=\"line\"><span style=\"color: #81A1C1\">int<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #88C0D0\">main<\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #81A1C1\">int<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #D8DEE9\">argc<\/span><span style=\"color: #ECEFF4\">,<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #81A1C1\">char<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #81A1C1\">*<\/span><span style=\"color: #D8DEE9\">argv<\/span><span style=\"color: #81A1C1\">[]<\/span><span style=\"color: #ECEFF4\">){<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">\t<\/span><span style=\"color: #88C0D0\">add_val<\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #B48EAD\">10<\/span><span style=\"color: #ECEFF4\">,<\/span><span style=\"color: #B48EAD\">23<\/span><span style=\"color: #ECEFF4\">)<\/span><span style=\"color: #81A1C1\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">        <\/span><span style=\"color: #81A1C1\">return<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #B48EAD\">0<\/span><span style=\"color: #81A1C1\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ECEFF4\">}<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">The above program is pretty simple it calls a function named <strong><em>add_val <\/em><\/strong>passes two values add them and return&#8217;s zero like a good boy. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">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 <strong>add_val<\/strong> function.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1920\" height=\"1080\" src=\"https:\/\/crazynoob.net\/wp-content\/uploads\/2024\/03\/return1-1.gif\" alt=\"\" class=\"wp-image-113\"\/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Hello_Stack_Frame\"><\/span><em>Hello Stack Frame<\/em><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">A stack frame is created whenever a function call is performed. Its used for storing the function parameters, local variables, <em>return address<\/em> and register values. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Lets take example of the <strong><em>add_val<\/em><\/strong> 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. <\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#2e3440ff\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"0x0000118d:\tpush   ebp\n0x0000118e:\tmov    ebp,esp\n0x00001190:\tsub    esp,0x10\n0x00001193:\tmov    edx,DWORD PTR [ebp+0x8]\n0x00001196:\tmov    eax,DWORD PTR [ebp+0xc]\n0x00001199:\tadd    eax,edx\n0x0000119b:\tmov    DWORD PTR [ebp-0x4],eax\n0x0000119e:\tmov    eax,0x0\n0x000011a3:\tleave  \n0x000011a4:\tret    \" style=\"color:#d8dee9ff;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki nord\" style=\"background-color: #2e3440ff\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #B48EAD\">0x0000118d<\/span><span style=\"color: #D8DEE9FF\">:\t<\/span><span style=\"color: #81A1C1\">push<\/span><span style=\"color: #D8DEE9FF\">   <\/span><span style=\"color: #81A1C1\">ebp<\/span><\/span>\n<span class=\"line\"><span style=\"color: #B48EAD\">0x0000118e<\/span><span style=\"color: #D8DEE9FF\">:\t<\/span><span style=\"color: #81A1C1\">mov<\/span><span style=\"color: #D8DEE9FF\">    <\/span><span style=\"color: #81A1C1\">ebp<\/span><span style=\"color: #D8DEE9FF\">,<\/span><span style=\"color: #81A1C1\">esp<\/span><\/span>\n<span class=\"line\"><span style=\"color: #B48EAD\">0x00001190<\/span><span style=\"color: #D8DEE9FF\">:\t<\/span><span style=\"color: #81A1C1\">sub<\/span><span style=\"color: #D8DEE9FF\">    <\/span><span style=\"color: #81A1C1\">esp<\/span><span style=\"color: #D8DEE9FF\">,<\/span><span style=\"color: #B48EAD\">0x10<\/span><\/span>\n<span class=\"line\"><span style=\"color: #B48EAD\">0x00001193<\/span><span style=\"color: #D8DEE9FF\">:\t<\/span><span style=\"color: #81A1C1\">mov<\/span><span style=\"color: #D8DEE9FF\">    <\/span><span style=\"color: #81A1C1\">edx<\/span><span style=\"color: #D8DEE9FF\">,<\/span><span style=\"color: #81A1C1\">DWORD<\/span><span style=\"color: #D8DEE9FF\"> PTR [<\/span><span style=\"color: #81A1C1\">ebp<\/span><span style=\"color: #D8DEE9FF\">+<\/span><span style=\"color: #B48EAD\">0x8<\/span><span style=\"color: #D8DEE9FF\">]<\/span><\/span>\n<span class=\"line\"><span style=\"color: #B48EAD\">0x00001196<\/span><span style=\"color: #D8DEE9FF\">:\t<\/span><span style=\"color: #81A1C1\">mov<\/span><span style=\"color: #D8DEE9FF\">    <\/span><span style=\"color: #81A1C1\">eax<\/span><span style=\"color: #D8DEE9FF\">,<\/span><span style=\"color: #81A1C1\">DWORD<\/span><span style=\"color: #D8DEE9FF\"> PTR [<\/span><span style=\"color: #81A1C1\">ebp<\/span><span style=\"color: #D8DEE9FF\">+<\/span><span style=\"color: #B48EAD\">0xc<\/span><span style=\"color: #D8DEE9FF\">]<\/span><\/span>\n<span class=\"line\"><span style=\"color: #B48EAD\">0x00001199<\/span><span style=\"color: #D8DEE9FF\">:\t<\/span><span style=\"color: #81A1C1\">add<\/span><span style=\"color: #D8DEE9FF\">    <\/span><span style=\"color: #81A1C1\">eax<\/span><span style=\"color: #D8DEE9FF\">,<\/span><span style=\"color: #81A1C1\">edx<\/span><\/span>\n<span class=\"line\"><span style=\"color: #B48EAD\">0x0000119b<\/span><span style=\"color: #D8DEE9FF\">:\t<\/span><span style=\"color: #81A1C1\">mov<\/span><span style=\"color: #D8DEE9FF\">    <\/span><span style=\"color: #81A1C1\">DWORD<\/span><span style=\"color: #D8DEE9FF\"> PTR [<\/span><span style=\"color: #81A1C1\">ebp<\/span><span style=\"color: #D8DEE9FF\">-<\/span><span style=\"color: #B48EAD\">0x4<\/span><span style=\"color: #D8DEE9FF\">],<\/span><span style=\"color: #81A1C1\">eax<\/span><\/span>\n<span class=\"line\"><span style=\"color: #B48EAD\">0x0000119e<\/span><span style=\"color: #D8DEE9FF\">:\t<\/span><span style=\"color: #81A1C1\">mov<\/span><span style=\"color: #D8DEE9FF\">    <\/span><span style=\"color: #81A1C1\">eax<\/span><span style=\"color: #D8DEE9FF\">,<\/span><span style=\"color: #B48EAD\">0x0<\/span><\/span>\n<span class=\"line\"><span style=\"color: #B48EAD\">0x000011a3<\/span><span style=\"color: #D8DEE9FF\">:\t<\/span><span style=\"color: #81A1C1\">leave<\/span><span style=\"color: #D8DEE9FF\">  <\/span><\/span>\n<span class=\"line\"><span style=\"color: #B48EAD\">0x000011a4<\/span><span style=\"color: #D8DEE9FF\">:\t<\/span><span style=\"color: #81A1C1\">ret<\/span><span style=\"color: #D8DEE9FF\">    <\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">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 <strong>LEAVE <\/strong>instruction.<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-style-plain is-layout-flow wp-block-quote-is-layout-flow\">\n<p class=\"wp-block-paragraph\">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.<\/p>\n<cite>Intel\u00ae 64 and IA-32 Architectures Software Developer\u2019s Manual Volume 1 https:\/\/www.intel.com\/content\/www\/us\/en\/developer\/articles\/technical\/intel-sdm.html <\/cite><\/blockquote>\n\n\n\n<p class=\"wp-block-paragraph\">The above topics should have given you idea about What, Why and How for the stack.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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 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 &#8230; <a title=\"The Stack\" class=\"read-more\" href=\"https:\/\/crazynoob.net\/index.php\/2024\/04\/15\/the-stack\/\" aria-label=\"Read more about The Stack\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":152,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[1],"tags":[],"class_list":["post-41","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-blog"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v28.9 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>The Stack - Crazynoob<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/crazynoob.net\/index.php\/2024\/04\/15\/the-stack\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"The Stack - Crazynoob\" \/>\n<meta property=\"og:description\" content=\"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 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 ... Read more\" \/>\n<meta property=\"og:url\" content=\"https:\/\/crazynoob.net\/index.php\/2024\/04\/15\/the-stack\/\" \/>\n<meta property=\"og:site_name\" content=\"Crazynoob\" \/>\n<meta property=\"article:published_time\" content=\"2024-04-15T18:29:28+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-04-15T18:45:49+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/crazynoob.net\/wp-content\/uploads\/2024\/04\/Quotefancy-18771-3840x2160-1.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"3840\" \/>\n\t<meta property=\"og:image:height\" content=\"2160\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"noob\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"noob\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"5 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/crazynoob.net\\\/index.php\\\/2024\\\/04\\\/15\\\/the-stack\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/crazynoob.net\\\/index.php\\\/2024\\\/04\\\/15\\\/the-stack\\\/\"},\"author\":{\"name\":\"noob\",\"@id\":\"https:\\\/\\\/crazynoob.net\\\/#\\\/schema\\\/person\\\/c44aab508373661773c0f05e51bbdc26\"},\"headline\":\"The Stack\",\"datePublished\":\"2024-04-15T18:29:28+00:00\",\"dateModified\":\"2024-04-15T18:45:49+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/crazynoob.net\\\/index.php\\\/2024\\\/04\\\/15\\\/the-stack\\\/\"},\"wordCount\":895,\"commentCount\":1,\"publisher\":{\"@id\":\"https:\\\/\\\/crazynoob.net\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/crazynoob.net\\\/index.php\\\/2024\\\/04\\\/15\\\/the-stack\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/crazynoob.net\\\/wp-content\\\/uploads\\\/2024\\\/04\\\/Quotefancy-18771-3840x2160-1.jpg\",\"articleSection\":[\"Blog\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/crazynoob.net\\\/index.php\\\/2024\\\/04\\\/15\\\/the-stack\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/crazynoob.net\\\/index.php\\\/2024\\\/04\\\/15\\\/the-stack\\\/\",\"url\":\"https:\\\/\\\/crazynoob.net\\\/index.php\\\/2024\\\/04\\\/15\\\/the-stack\\\/\",\"name\":\"The Stack - Crazynoob\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/crazynoob.net\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/crazynoob.net\\\/index.php\\\/2024\\\/04\\\/15\\\/the-stack\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/crazynoob.net\\\/index.php\\\/2024\\\/04\\\/15\\\/the-stack\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/crazynoob.net\\\/wp-content\\\/uploads\\\/2024\\\/04\\\/Quotefancy-18771-3840x2160-1.jpg\",\"datePublished\":\"2024-04-15T18:29:28+00:00\",\"dateModified\":\"2024-04-15T18:45:49+00:00\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/crazynoob.net\\\/index.php\\\/2024\\\/04\\\/15\\\/the-stack\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/crazynoob.net\\\/index.php\\\/2024\\\/04\\\/15\\\/the-stack\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/crazynoob.net\\\/index.php\\\/2024\\\/04\\\/15\\\/the-stack\\\/#primaryimage\",\"url\":\"https:\\\/\\\/crazynoob.net\\\/wp-content\\\/uploads\\\/2024\\\/04\\\/Quotefancy-18771-3840x2160-1.jpg\",\"contentUrl\":\"https:\\\/\\\/crazynoob.net\\\/wp-content\\\/uploads\\\/2024\\\/04\\\/Quotefancy-18771-3840x2160-1.jpg\",\"width\":3840,\"height\":2160},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/crazynoob.net\\\/index.php\\\/2024\\\/04\\\/15\\\/the-stack\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/crazynoob.net\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"The Stack\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/crazynoob.net\\\/#website\",\"url\":\"https:\\\/\\\/crazynoob.net\\\/\",\"name\":\"Crazynoob\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\\\/\\\/crazynoob.net\\\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/crazynoob.net\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/crazynoob.net\\\/#organization\",\"name\":\"Crazynoob\",\"url\":\"https:\\\/\\\/crazynoob.net\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/crazynoob.net\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/crazynoob.net\\\/wp-content\\\/uploads\\\/2024\\\/02\\\/cropped-Crazynoob.png\",\"contentUrl\":\"https:\\\/\\\/crazynoob.net\\\/wp-content\\\/uploads\\\/2024\\\/02\\\/cropped-Crazynoob.png\",\"width\":761,\"height\":752,\"caption\":\"Crazynoob\"},\"image\":{\"@id\":\"https:\\\/\\\/crazynoob.net\\\/#\\\/schema\\\/logo\\\/image\\\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/crazynoob.net\\\/#\\\/schema\\\/person\\\/c44aab508373661773c0f05e51bbdc26\",\"name\":\"noob\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/crazynoob.net\\\/wp-content\\\/litespeed\\\/avatar\\\/8b04e4228776f34753e6e5ba1874b999.jpg?ver=1795477321\",\"url\":\"https:\\\/\\\/crazynoob.net\\\/wp-content\\\/litespeed\\\/avatar\\\/8b04e4228776f34753e6e5ba1874b999.jpg?ver=1795477321\",\"contentUrl\":\"https:\\\/\\\/crazynoob.net\\\/wp-content\\\/litespeed\\\/avatar\\\/8b04e4228776f34753e6e5ba1874b999.jpg?ver=1795477321\",\"caption\":\"noob\"},\"sameAs\":[\"https:\\\/\\\/crazynoob.net\"],\"url\":\"https:\\\/\\\/crazynoob.net\\\/index.php\\\/author\\\/noob\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"The Stack - Crazynoob","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/crazynoob.net\/index.php\/2024\/04\/15\/the-stack\/","og_locale":"en_US","og_type":"article","og_title":"The Stack - Crazynoob","og_description":"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 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 ... Read more","og_url":"https:\/\/crazynoob.net\/index.php\/2024\/04\/15\/the-stack\/","og_site_name":"Crazynoob","article_published_time":"2024-04-15T18:29:28+00:00","article_modified_time":"2024-04-15T18:45:49+00:00","og_image":[{"width":3840,"height":2160,"url":"https:\/\/crazynoob.net\/wp-content\/uploads\/2024\/04\/Quotefancy-18771-3840x2160-1.jpg","type":"image\/jpeg"}],"author":"noob","twitter_card":"summary_large_image","twitter_misc":{"Written by":"noob","Est. reading time":"5 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/crazynoob.net\/index.php\/2024\/04\/15\/the-stack\/#article","isPartOf":{"@id":"https:\/\/crazynoob.net\/index.php\/2024\/04\/15\/the-stack\/"},"author":{"name":"noob","@id":"https:\/\/crazynoob.net\/#\/schema\/person\/c44aab508373661773c0f05e51bbdc26"},"headline":"The Stack","datePublished":"2024-04-15T18:29:28+00:00","dateModified":"2024-04-15T18:45:49+00:00","mainEntityOfPage":{"@id":"https:\/\/crazynoob.net\/index.php\/2024\/04\/15\/the-stack\/"},"wordCount":895,"commentCount":1,"publisher":{"@id":"https:\/\/crazynoob.net\/#organization"},"image":{"@id":"https:\/\/crazynoob.net\/index.php\/2024\/04\/15\/the-stack\/#primaryimage"},"thumbnailUrl":"https:\/\/crazynoob.net\/wp-content\/uploads\/2024\/04\/Quotefancy-18771-3840x2160-1.jpg","articleSection":["Blog"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/crazynoob.net\/index.php\/2024\/04\/15\/the-stack\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/crazynoob.net\/index.php\/2024\/04\/15\/the-stack\/","url":"https:\/\/crazynoob.net\/index.php\/2024\/04\/15\/the-stack\/","name":"The Stack - Crazynoob","isPartOf":{"@id":"https:\/\/crazynoob.net\/#website"},"primaryImageOfPage":{"@id":"https:\/\/crazynoob.net\/index.php\/2024\/04\/15\/the-stack\/#primaryimage"},"image":{"@id":"https:\/\/crazynoob.net\/index.php\/2024\/04\/15\/the-stack\/#primaryimage"},"thumbnailUrl":"https:\/\/crazynoob.net\/wp-content\/uploads\/2024\/04\/Quotefancy-18771-3840x2160-1.jpg","datePublished":"2024-04-15T18:29:28+00:00","dateModified":"2024-04-15T18:45:49+00:00","breadcrumb":{"@id":"https:\/\/crazynoob.net\/index.php\/2024\/04\/15\/the-stack\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/crazynoob.net\/index.php\/2024\/04\/15\/the-stack\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/crazynoob.net\/index.php\/2024\/04\/15\/the-stack\/#primaryimage","url":"https:\/\/crazynoob.net\/wp-content\/uploads\/2024\/04\/Quotefancy-18771-3840x2160-1.jpg","contentUrl":"https:\/\/crazynoob.net\/wp-content\/uploads\/2024\/04\/Quotefancy-18771-3840x2160-1.jpg","width":3840,"height":2160},{"@type":"BreadcrumbList","@id":"https:\/\/crazynoob.net\/index.php\/2024\/04\/15\/the-stack\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/crazynoob.net\/"},{"@type":"ListItem","position":2,"name":"The Stack"}]},{"@type":"WebSite","@id":"https:\/\/crazynoob.net\/#website","url":"https:\/\/crazynoob.net\/","name":"Crazynoob","description":"","publisher":{"@id":"https:\/\/crazynoob.net\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/crazynoob.net\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/crazynoob.net\/#organization","name":"Crazynoob","url":"https:\/\/crazynoob.net\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/crazynoob.net\/#\/schema\/logo\/image\/","url":"https:\/\/crazynoob.net\/wp-content\/uploads\/2024\/02\/cropped-Crazynoob.png","contentUrl":"https:\/\/crazynoob.net\/wp-content\/uploads\/2024\/02\/cropped-Crazynoob.png","width":761,"height":752,"caption":"Crazynoob"},"image":{"@id":"https:\/\/crazynoob.net\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/crazynoob.net\/#\/schema\/person\/c44aab508373661773c0f05e51bbdc26","name":"noob","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/crazynoob.net\/wp-content\/litespeed\/avatar\/8b04e4228776f34753e6e5ba1874b999.jpg?ver=1795477321","url":"https:\/\/crazynoob.net\/wp-content\/litespeed\/avatar\/8b04e4228776f34753e6e5ba1874b999.jpg?ver=1795477321","contentUrl":"https:\/\/crazynoob.net\/wp-content\/litespeed\/avatar\/8b04e4228776f34753e6e5ba1874b999.jpg?ver=1795477321","caption":"noob"},"sameAs":["https:\/\/crazynoob.net"],"url":"https:\/\/crazynoob.net\/index.php\/author\/noob\/"}]}},"_links":{"self":[{"href":"https:\/\/crazynoob.net\/index.php\/wp-json\/wp\/v2\/posts\/41","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/crazynoob.net\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/crazynoob.net\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/crazynoob.net\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/crazynoob.net\/index.php\/wp-json\/wp\/v2\/comments?post=41"}],"version-history":[{"count":34,"href":"https:\/\/crazynoob.net\/index.php\/wp-json\/wp\/v2\/posts\/41\/revisions"}],"predecessor-version":[{"id":151,"href":"https:\/\/crazynoob.net\/index.php\/wp-json\/wp\/v2\/posts\/41\/revisions\/151"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/crazynoob.net\/index.php\/wp-json\/wp\/v2\/media\/152"}],"wp:attachment":[{"href":"https:\/\/crazynoob.net\/index.php\/wp-json\/wp\/v2\/media?parent=41"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/crazynoob.net\/index.php\/wp-json\/wp\/v2\/categories?post=41"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/crazynoob.net\/index.php\/wp-json\/wp\/v2\/tags?post=41"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}