{"id":2214,"date":"2016-12-03T00:01:59","date_gmt":"2016-12-03T08:01:59","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=2214"},"modified":"2016-12-10T07:58:08","modified_gmt":"2016-12-10T15:58:08","slug":"behold-the-stack-part-iii","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=2214","title":{"rendered":"Behold the Stack, Part III"},"content":{"rendered":"<p>Whether you simulate a stack in your C code or just ignore the concept altogether, your program constantly uses the processor&#8217;s stack. Specifically, if the code contains a function, it uses this stack not only to call and return from the function, but to pass and return arguments as well.<br \/>\n<!--more--><br \/>\nAll programming languages use the stack for function calls and returns. You can see this process in action when you use a debugger to run your code. The Code::Blocks debugger is excellent for this purpose.<\/p>\n<p>In Code::Blocks, create a new C language project. Ensure that a Debug version is created. Then write this sample code:<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nvoid f(void)\r\n{\r\n    puts(\"Function f()\");\r\n}\r\n\r\nint main()\r\n{\r\n    puts(\"Main function\");\r\n    f();\r\n\r\n    return(0);\r\n}<\/pre>\n<p>Build the code and fix any typoes, if necessary.<\/p>\n<p>To debut the code, click the cursor on Line 9. On the Debug toolbar, click the Run to Cursor button. Refer to Figure 1 for visual assistance.<\/p>\n<div id=\"attachment_2228\" style=\"width: 560px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-2228\" src=\"http:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2016\/12\/1203-figure1.png\" alt=\"Figure 1. Checking the esp (stack pointer) register in the Code::Blocks debugger.\" width=\"550\" height=\"505\" class=\"size-full wp-image-2228\" srcset=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2016\/12\/1203-figure1.png 550w, https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2016\/12\/1203-figure1-300x275.png 300w, https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2016\/12\/1203-figure1-327x300.png 327w\" sizes=\"auto, (max-width: 550px) 100vw, 550px\" \/><p id=\"caption-attachment-2228\" class=\"wp-caption-text\">Figure 1. Checking the <code>esp<\/code> (stack pointer) register in the Code::Blocks debugger.<\/p><\/div>\n<p>The program runs, but halts at the instruction at Line 9. At this point, you can open a register dump to see what&#8217;s going on with the processor: Click the Debugging Windows button (refer to Figure 1) and choose CPU Registers. The window appears as shown in the figure.<\/p>\n<p>Examine the <code>esp<\/code> register, the stack pointer, on your screen. In Figure 1, it shows address <code>0x60ff00<\/code>.<\/p>\n<p>Click the Step Into button until the yellow triangle is inside the <em>f()<\/em> function, as shown in Figure 2.<\/p>\n<div id=\"attachment_2229\" style=\"width: 560px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-2229\" src=\"http:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2016\/12\/1203-figure2.png\" alt=\"Figure 2. The effect of calling a C language function on the processor&#039;s esp stack pointer register.\" width=\"550\" height=\"488\" class=\"size-full wp-image-2229\" srcset=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2016\/12\/1203-figure2.png 550w, https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2016\/12\/1203-figure2-300x266.png 300w, https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2016\/12\/1203-figure2-338x300.png 338w\" sizes=\"auto, (max-width: 550px) 100vw, 550px\" \/><p id=\"caption-attachment-2229\" class=\"wp-caption-text\">Figure 2. The effect of calling a C language function on the processor&#8217;s <code>esp<\/code> stack pointer register.<\/p><\/div>\n<p>Observe the <code>esp<\/code> (stack pointer) register. Its value on my PC changes to <code>0x60fee0<\/code>. What&#8217;s happened is that the value of the <code>eip<\/code> (instruction pointer) register has been pushed to the stack. Other values may be pushed as well, but the effect is that the stack has changed.<\/p>\n<p>Click the Step Into button until the function call returns. At that point, you see the <code>esp<\/code> register restored to its original value.<\/p>\n<p>When you pass arguments to a function, the values are also pushed to the stack. To retrieve the values, internally the function pops them from the stack and then does whatever.<\/p>\n<p>When a value is returned from a function, it&#8217;s pushed to the stack as well, then recovered after the return. The overhead for the function call is handled by the compiler, so you don&#8217;t have to think about the specific stack operations in your C code. Yet, the stack is used internally to call and return from a function.<\/p>\n<p>Beyond the processor&#8217;s stack pointer register, you can simulate stack operations in any programming language. The simulated stack operates like the processor&#8217;s stack pointer, but it&#8217;s not the same thing as manipulating the stack pointer directly.<\/p>\n<p>In <a href=\"http:\/\/c-for-dummies.com\/blog\/?p=2243\">next week&#8217;s Lesson<\/a>, I introduce a program that simulates stack storage.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The stack pointer is manipulated when you call a function in C, as well as pass and return values. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=2214\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-2214","post","type-post","status-publish","format-standard","hentry","category-main"],"_links":{"self":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2214","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2214"}],"version-history":[{"count":5,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2214\/revisions"}],"predecessor-version":[{"id":2257,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2214\/revisions\/2257"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2214"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2214"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2214"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}