{"id":2210,"date":"2016-11-26T00:01:27","date_gmt":"2016-11-26T08:01:27","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=2210"},"modified":"2016-12-03T08:12:41","modified_gmt":"2016-12-03T16:12:41","slug":"behold-the-stack-part-ii","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=2210","title":{"rendered":"Behold the Stack, Part II"},"content":{"rendered":"<p>At the lowest programming level, you encounter machine code. This is the language of the processor itself. Machine code directs the processor to do things, such as read from or write to memory, store data, perform math, and other tasks. And if you dare to program a processor directly, you&#8217;ll encounter something called the <em>stack pointer register<\/em>.<br \/>\n<!--more--><br \/>\nIn Figure 1, you see the Code::Blocks debugger showing the processor&#8217;s register dump. The Registers are storage areas in the processor; the values stored in each register are shown in the Hex and Integer columns.<\/p>\n<div id=\"attachment_2212\" style=\"width: 344px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-2212\" src=\"http:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2016\/11\/1126-figure1.png\" alt=\"Figure 1. The processor&#039;s register dump.\" width=\"334\" height=\"367\" class=\"size-full wp-image-2212\" srcset=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2016\/11\/1126-figure1.png 334w, https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2016\/11\/1126-figure1-273x300.png 273w\" sizes=\"auto, (max-width: 334px) 100vw, 334px\" \/><p id=\"caption-attachment-2212\" class=\"wp-caption-text\">Figure 1. The processor&#8217;s register dump.<\/p><\/div>\n<p>Most of the registers store data or reference locations in memory. All-in-all, I find processor programming fascinating, but it&#8217;s all terribly trivial these days.<\/p>\n<p>Of note for this Lesson, register <code>esp<\/code> is the stack pointer register (The <code>e<\/code> means that it&#8217;s 32-bits wide.) The register holds the memory location of the top of the stack.<\/p>\n<p>Register <code>ebp<\/code> is the base pointer register. It references the bottom or start of the stack.<\/p>\n<p>As the processor churns through machine code, the various registers are manipulated. The stack pointer is also manipulated, but only under certain conditions.<\/p>\n<p>For example, a <em>push<\/em> instruction sets a value onto the stack, either directly or the value stored in a register. The value is copied to the stack pointer&#8217;s memory location, then the stack pointer is incremented. Figure 2 illustrates this process.<\/p>\n<div id=\"attachment_2231\" style=\"width: 510px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-2231\" src=\"http:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2016\/11\/1126-figure2_stack.png\" alt=\"Figure 2. The direct value 0x002A is pushed onto the stack.\" width=\"500\" height=\"175\" class=\"size-full wp-image-2231\" srcset=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2016\/11\/1126-figure2_stack.png 500w, https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2016\/11\/1126-figure2_stack-300x105.png 300w\" sizes=\"auto, (max-width: 500px) 100vw, 500px\" \/><p id=\"caption-attachment-2231\" class=\"wp-caption-text\">Figure 2. The direct value 0x002A is pushed onto the stack.<\/p><\/div>\n<p>A <em>pop<\/em> instruction removes a value from the stack. The value stored at the stack pointer&#8217;s location is transferred into a named register (typically <code>eax<\/code>, known as the <em>accumulator<\/em>) and the stack pointer is decremented. Figure 3 illustrates this concept.<\/p>\n<div id=\"attachment_2221\" style=\"width: 510px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-2221\" src=\"http:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2016\/11\/1126-figure3_stack-1.png\" alt=\"Figure 3. The value 0x002A is popped from the stack and stored in register eax.\" width=\"500\" height=\"176\" class=\"size-full wp-image-2221\" srcset=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2016\/11\/1126-figure3_stack-1.png 500w, https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2016\/11\/1126-figure3_stack-1-300x106.png 300w\" sizes=\"auto, (max-width: 500px) 100vw, 500px\" \/><p id=\"caption-attachment-2221\" class=\"wp-caption-text\">Figure 3. The value 0x002A is popped from the stack and stored in register eax.<\/p><\/div>\n<p>Stack storage is used in several ways. To directly manipulate the stack, you can use the Assembly language <em>push<\/em> and <em>pop<\/em> instructions. In the following chunk of Assembly code, the contents of register <code>eax<\/code> and swapped with register <code>ebx<\/code>:<\/p>\n<pre><code>    push eax        <span class=\"comments\">;save register value on the stack<\/span>\r\n    push ebx\r\n    pop eax         <span class=\"comments\">;put ebx into eax<\/span>\r\n    pop ebx         <span class=\"comments\">;put eax into ebx<\/span><\/code><\/pre>\n<p>This operation succeeds because the last item sent (pushed) to the stack is the first item popped out &mdash; that&#8217;s LIFO or last-in, first-out. The result is that the register&#8217;s values are exchanged. And this operation is necessary because Assembly language lacks an assignment operator. (And even if the processor&#8217;s instruction set features a <em>swap<\/em> or similar instruction, using <em>push-pop<\/em> works faster.)<\/p>\n<p>The other way the stack is used is for Assembly <em>call<\/em> and <em>ret<\/em> (return) instructions, which has direct application in C and other programming languages. I&#8217;ll cover more on this topic in <a href=\"http:\/\/c-for-dummies.com\/blog\/?p=2214\">next week&#8217;s Lesson<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The processor&#8217;s stack is the paradigm used for all simulated programming stacks. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=2210\">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-2210","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\/2210","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=2210"}],"version-history":[{"count":8,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2210\/revisions"}],"predecessor-version":[{"id":2246,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2210\/revisions\/2246"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2210"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2210"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2210"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}