<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>local variables stack Archives - NeuralLantern.com</title>
	<atom:link href="https://www.NeuralLantern.com/tag/local-variables-stack/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.NeuralLantern.com/tag/local-variables-stack/</link>
	<description></description>
	<lastBuildDate>Mon, 16 Mar 2026 06:39:34 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>

<image>
	<url>https://www.NeuralLantern.com/wp-content/uploads/2025/04/cropped-2025-04-04-Lantern-03-32x32.png</url>
	<title>local variables stack Archives - NeuralLantern.com</title>
	<link>https://www.NeuralLantern.com/tag/local-variables-stack/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>x86-64 Assembly: Local Variables, Stack Frames &#038; Alignment Explained</title>
		<link>https://www.NeuralLantern.com/x86-64-assembly-local-variables-stack-frames-alignment-explained/</link>
					<comments>https://www.NeuralLantern.com/x86-64-assembly-local-variables-stack-frames-alignment-explained/#respond</comments>
		
		<dc:creator><![CDATA[mike]]></dc:creator>
		<pubDate>Mon, 16 Mar 2026 06:39:32 +0000</pubDate>
				<category><![CDATA[Assembly Language]]></category>
		<category><![CDATA[Coding]]></category>
		<category><![CDATA[Computer Science]]></category>
		<category><![CDATA[Videos]]></category>
		<category><![CDATA[16 byte alignment]]></category>
		<category><![CDATA[assembly array on stack]]></category>
		<category><![CDATA[assembly language]]></category>
		<category><![CDATA[assembly stack allocation]]></category>
		<category><![CDATA[calling convention]]></category>
		<category><![CDATA[computer architecture]]></category>
		<category><![CDATA[local variables stack]]></category>
		<category><![CDATA[low level programming]]></category>
		<category><![CDATA[NASM tutorial]]></category>
		<category><![CDATA[printf crash assembly]]></category>
		<category><![CDATA[rbp rbp stack]]></category>
		<category><![CDATA[reverse engineering]]></category>
		<category><![CDATA[stack alignment]]></category>
		<category><![CDATA[stack frame assembly]]></category>
		<category><![CDATA[sub rsp assembly]]></category>
		<category><![CDATA[systems programming]]></category>
		<category><![CDATA[x64 assembly]]></category>
		<category><![CDATA[x86 assembly tutorial]]></category>
		<category><![CDATA[x86-64 assembly]]></category>
		<category><![CDATA[Yasm tutorial]]></category>
		<guid isPermaLink="false">https://www.NeuralLantern.com/?p=317</guid>

					<description><![CDATA[<p>Learn how local variables are allocated on the stack in x86-64 assembly using YASM. See exactly how to reserve space, address variables with offsets, understand downward stack growth, and fix mysterious printf segfaults caused by 16-byte stack misalignment. Includes two alignment solutions and a complete working example with a local integer array.</p>
<p>The post <a href="https://www.NeuralLantern.com/x86-64-assembly-local-variables-stack-frames-alignment-explained/">x86-64 Assembly: Local Variables, Stack Frames &amp; Alignment Explained</a> appeared first on <a href="https://www.NeuralLantern.com">NeuralLantern.com</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<figure class="wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio"><div class="wp-block-embed__wrapper">
<iframe title="x86-64 Assembly: Local Variables, Stack Frames &amp; Alignment Explained" width="1380" height="776" src="https://www.youtube.com/embed/woCCcOlopNo?feature=oembed" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen></iframe>
</div></figure>



<p>Quick but deep look at local variables on the stack in x86-64 assembly (YASM). We cover allocation with sub rsp, accessing via offsets, why the stack must be 16-byte aligned when calling libc functions like printf, and two practical ways to fix alignment crashes. Includes live segfault debugging and a full working example with a local array.</p>



<p>Great for anyone studying systems programming, computer architecture, or just trying to figure out why their assembly program randomly crashes on a library call.</p>



<p>Like + subscribe if you want more clear, practical assembly tutorials!</p>



<p>00:00 Introduction to Local Variables on the Stack<br>00:28 C++ Example of Function and Local Variables<br>01:06 Incoming Arguments in RDI vs Stack Variables<br>03:24 Pointers as Local Variables on Stack<br>04:34 Why the Stack – Recursion and Multiple Calls<br>05:18 Visualizing Multiple Stack Frames<br>09:24 How Function Returns Adjust RSP<br>10:35 Stack Grows Downward in Memory<br>11:33 Program Setup – Hybrid C++/Assembly<br>12:46 Assembly Module Overview<br>14:00 Function Prologue – Register Push &amp; Alignment<br>15:30 Allocating Stack Space for Local Array<br>17:45 Initializing Array in Loop<br>20:10 Printing Loop with printf<br>49:48 First Run – Segfault Observed<br>51:00 16-Byte Stack Alignment Requirement<br>51:55 Fix 1 – Extra Push/Pop in Prologue/Epilogue<br>53:15 Fix 2 – Push/Pop Around Each printf Call<br>55:04 Testing Different Array Sizes<br>56:52 Debugging Alignment Behavior<br>58:54 Summary – Creating Any Local Data on Stack<br>59:59 Closing Remarks &amp; Subscribe Call</p>



<p>=-=-=-=-=-=-=-=-=</p>



<p>Thanks for watching!</p>



<p>Find us on other social media here:</p>



<ul class="wp-block-list">
<li>https://www.NeuralLantern.com/social</li>



<li>Twitter / X: https://x.com/NeuralLantern</li>



<li>Rumble: https://rumble.com/c/c-3696939</li>



<li>BitChute: https://www.bitchute.com/channel/pg1Pvv5dN4Gt</li>



<li>Daily Motion: https://www.dailymotion.com/neurallantern</li>



<li>Minds: https://www.minds.com/neurallantern/</li>



<li>Odysee: https://odysee.com/@NeuralLantern:5</li>
</ul>



<p>Please show your support!</p>



<ul class="wp-block-list">
<li>Buy me a coffee: https://ko-fi.com/neurallantern</li>



<li>Subscribe + Sharing on Social Media</li>



<li>Leave a comment or suggestion</li>



<li>Subscribe to the Blog: https://www.NeuralLantern.com</li>



<li>Watch the main &#8220;pinned&#8221; video of this channel for offers and extras</li>
</ul>



<p>Hi there! Today I&#8217;d like to talk to you about local variables on the stack</p>



<p>in an x8664 assembly program written in YASM.</p>



<p>If you don&#8217;t understand assembly or local variables or other things like that you might</p>



<p>want to see my other videos but I&#8217;m just going to give you a quick example. So what am I talking</p>



<p>about with local variables on the stack? For starters forget what you&#8217;re seeing here for a</p>



<p>blank code page and pretend that we&#8217;re coding in c plus plus just for a moment this is an assembly</p>



<p>video but pretend this is c plus plus so suppose you have c plus plus you have a main function here</p>



<p>and at the end of it you know this is your entry point for your program we return zero and maybe</p>



<p>above it there&#8217;s a function called f i&#8217;m putting it above because i don&#8217;t want to use prototypes</p>



<p>in this video but you should probably use prototypes um maybe main calls on f and so then</p>



<p>what happens? I don&#8217;t know. Maybe we have some arguments. We&#8217;ll call this integer argument A</p>



<p>that comes in. If you&#8217;ve watched my other videos, hopefully you have by now,</p>



<p>you&#8217;ll know that A comes into the function as RDI because that&#8217;s the first integer argument</p>



<p>register. But then when we start creating local variables, we&#8217;ll say integer B equals, let&#8217;s say,</p>



<p>a five and then integer C is equal to an eight for whatever reason. Maybe there will be an array.</p>



<p>So I&#8217;ll call this a int array, I guess.</p>



<p>We&#8217;ll say that we have 100 integers in our array.</p>



<p>We could also have a pointer, int pointer p,</p>



<p>and then allocate it to some kind of new memory just to prove a point.</p>



<p>And then, you know, later in your function,</p>



<p>you&#8217;re probably going to want to do something with your data.</p>



<p>So I don&#8217;t know, maybe a, how about like b is plus equal to a,</p>



<p>of C++ and then oh C++ C is plus equal to B for some reason I&#8217;m just making random nonsense up</p>



<p>honestly I&#8217;m just showing you that we use our variables and then maybe you want to build an</p>



<p>array so we just declared the array up here on line six but maybe you want to actually do something</p>



<p>with it you want to fill it with data maybe so I&#8217;ll do size type I is zero keep going until I</p>



<p>is less than as long as I is less than 100 I plus plus and now I&#8217;m going through every single index</p>



<p>array maybe I&#8217;ll say the array at index i is equal to c and then we&#8217;ll just say c is plus equal to b</p>



<p>and then we&#8217;ll do b plus plus just to have something in there okay so let me explain the</p>



<p>parts of your program real fast if you haven&#8217;t watched my other videos please do because this</p>



<p>should help a little bit anyway so a is an incoming argument like I said before that&#8217;s</p>



<p>usually coming to you in the RDI register because it&#8217;s the first integer argument so we don&#8217;t need</p>



<p>to worry too much about that we know that a is actually just a register in your CPU and b that&#8217;s</p>



<p>created on the stack and c is created on the stack also and the array is created on the stack by the</p>



<p>way I could name the array anything if I wanted to like just v I&#8217;m using the name array because</p>



<p>Notice also that we have a pointer here. I just wanted to make a point that the pointers we make in our function are considered local variables.</p>



<p>And that means they do sit on the stack because what I&#8217;m trying to say is that local variables that are not arguments, they sit on the stack.</p>



<p>But the memory we allocated and then gave to the pointer, that&#8217;s sitting in the heap somewhere.</p>



<p>So when you use the new operator or the malloc operator, or you&#8217;re just like making dynamic memory,</p>



<p>that dynamic memory sits in the heap, but the pointer itself, since we just declared it here,</p>



<p>that sits on the stack. That means later on when your function ends, the p pointer itself will get</p>



<p>cleaned up automatically and itself won&#8217;t be leaked memory. But if you forget to clean up your memory</p>



<p>here, like I did, you&#8217;ll have a memory leak. But I&#8217;m going to talk about memory leaks probably in</p>



<p>some other videos somewhere else at another time. Anyway, so then we have our local variables here.</p>



<p>remember B and C and array are all on the stack and well we just have our</p>



<p>for loop here where we just kind of start modifying data so why would we use</p>



<p>the stack let me just do a quick tutorial this is not a stack video I&#8217;m</p>



<p>gonna make a stack video in the future but I&#8217;ll just do like a quick little</p>



<p>rundown of what is the stack and why is it really good for function calls</p>



<p>consider that sometimes F might want to call itself maybe it&#8217;s a recursive</p>



<p>be like a long chain of function calls like f calls g and g calls h and h calls you know whatever</p>



<p>and it just goes on and on and on as long as it&#8217;s not infinite recursion like all the functions are</p>



<p>calling themselves in one circle that never ends you should be allowed to do that we should be</p>



<p>allowed to have a function f that sometimes calls itself or other functions or maybe it&#8217;s called</p>



<p>multiple times within our giant call stack our call graph and this will work because these</p>



<p>you know a b and c and array are sitting on the stack and the stack allows you to have sort of</p>



<p>different instances of variables so for example and this is just a quick thing i&#8217;m not this is</p>



<p>not supposed to be a full stack tutorial here but um you know if we have like the function f</p>



<p>and let&#8217;s say we call it at some point maybe uh maybe main calls f uh and then let&#8217;s let&#8217;s pretend</p>



<p>f and then f calls h and then maybe h calls f and um did i write an h there no i think that&#8217;s</p>



<p>supposed to be a parenthesis i&#8217;m sorry i have bad penmanship oh god it&#8217;s even worse hang on let me</p>



<p>let me do another h here i&#8217;ll just do it i&#8217;ll just do a g about that f g and then uh</p>



<p>F again. Let&#8217;s just pretend that we can have a call graph where F is sometimes called by something</p>



<p>and sometimes called something else or maybe sometimes F is called by a different thing. So</p>



<p>like I guess this was supposed to be the G function. So maybe that&#8217;s why down here was the H.</p>



<p>Should I delete this video? I don&#8217;t know. You know what? I&#8217;m going to start. I&#8217;m going to delete this</p>



<p>and then G calls H and then H calls F and then F calls I I guess and then</p>



<p>eventually these functions start returning right oops how come my greens</p>



<p>not working green there we go too late now so we can have like a really</p>



<p>complicated call graph and F might appear in there multiple times and the</p>



<p>Each call to F along the call graph should have its own copy, unique copy of local variables.</p>



<p>So the B for this first F call, let me just do an arrow here, will have a B.</p>



<p>And you can imagine it as B subscript 1.</p>



<p>And down here when F executes, you can imagine that it&#8217;s got its own copy of B.</p>



<p>So we can imagine this as B subscript 2.</p>



<p>So these are two totally different Bs.</p>



<p>If you tried to use global variables for this, it would be really, really, really hard to get the code to work.</p>



<p>get the code to work and it would be really, really hard to debug.</p>



<p>So that&#8217;s kind of why we have local variables.</p>



<p>The stack allows us to do this.</p>



<p>So what is the stack itself?</p>



<p>Again, this is not a full tutorial on the stack,</p>



<p>but I just want you to kind of see what&#8217;s happening.</p>



<p>Imagine a data structure that kind of grows upward.</p>



<p>I&#8217;ll say that here&#8217;s the floor at the bottom.</p>



<p>And when you put an item onto the stack,</p>



<p>the items kind of stack on top of each other.</p>



<p>So imagine that this is the call to F.</p>



<p>is the call to f and embedded within this little stack frame area we&#8217;ll call it a stack frame but</p>



<p>actually the stack continues to grow as you create and destroy local variables within a function call</p>



<p>we&#8217;ll just imagine that there&#8217;s i don&#8217;t know a mini stack sitting inside of the stack that</p>



<p>contains all the local variables i&#8217;ll just put i&#8217;m not going to put a because a is an argument so</p>



<p>it&#8217;s sitting in a register but we could do dude i got to learn how to how to draw with this tablet</p>



<p>We got to do B and C and the array.</p>



<p>I&#8217;ll put AR for array.</p>



<p>Sitting in, you know, their own little spots on the stack within the major portion of the stack that is designated for that first F call.</p>



<p>And then, you know, maybe F calls G and some other stuff happens.</p>



<p>I&#8217;ll just pretend that a bunch of other stuff happened.</p>



<p>And then eventually F is called again.</p>



<p>But there&#8217;s a different, this is a different instance of the call to F.</p>



<p>it&#8217;s got its own little area that is separate from the the previous call to f and again we&#8217;ll</p>



<p>also have variables that we can create locally that are supposed to be separate from the original</p>



<p>variables oh god that&#8217;s awful and ugly i need to maybe decrease the size of my eraser or something</p>



<p>but imagine this is uh you know these are two separate copies so that&#8217;s like what i said before</p>



<p>when we have like a b1 and a b2 basically they&#8217;re not going to be called b1 and b2 but they&#8217;re just</p>



<p>They&#8217;re not going to be called B1 and B2, but they&#8217;re just two separate instances.</p>



<p>And I just want you to know.</p>



<p>So then when we start returning from functions, like when this F eventually returns, and by</p>



<p>the way, I know that what I&#8217;m drawing on the right doesn&#8217;t match the code because this</p>



<p>is not a function that calls itself, but just suppose that your code is a lot more complicated</p>



<p>than what I drew up.</p>



<p>When it eventually returns, all that happens is those items on the stack just sort of get</p>



<p>not necessarily deallocated, but ignored.</p>



<p>We&#8217;ll just say that they&#8217;re gone.</p>



<p>They&#8217;re still sitting as junk data in system RAM somewhere.</p>



<p>And in assembly talk, we know that we have a stack pointer called RSP.</p>



<p>We have a register called RSP that just sort of points to the location in the</p>



<p>stack that is considered the top of the stack, like the most recent piece of data</p>



<p>that we have available.</p>



<p>So all the other data is actually still kind of above, but we&#8217;re not pointing to it</p>



<p>anymore, so we consider that it doesn&#8217;t exist.</p>



<p>So then when G eventually returns, you know, we just change the stack pointer,</p>



<p>Rsp, to point to that other piece of data.</p>



<p>The G data frame and the other F data frame are still sitting above somewhere in memory,</p>



<p>but we just ignore them, right?</p>



<p>So that&#8217;s how the stack works.</p>



<p>And that&#8217;s how we have local function call copies of all of our local variables.</p>



<p>Something to note.</p>



<p>Something to note, this is not a stack video, but you know, just something to note that</p>



<p>even though I draw the stack visually as growing vertically up, when you actually manipulate</p>



<p>the stack in assembly or just like in any language, the stack grows downward in terms</p>



<p>of memory locations.</p>



<p>So you can imagine, I&#8217;m trying so hard not to make this like a huge stack video.</p>



<p>Imagine this is a memory location 80, we&#8217;ll say.</p>



<p>You would think that memory location 81 would be the next item of the stack, or I guess</p>



<p>the stack or i guess if you if you&#8217;re considering the fact that the items on the stack are quad words</p>



<p>we would say it goes up to 88 but that&#8217;s not true it goes down to 72 so the memory location goes down</p>



<p>even though we imagine the stack growing uh upward vertically just so you know that&#8217;s the kind of</p>



<p>thing we&#8217;re going to do so what i&#8217;m going to do is just show you an assembly program where we can</p>



<p>create local variables and i&#8217;m just going to show you how to create an array because this array is</p>



<p>just like a bunch of integers and you can imagine it would be really easy to create only one integer</p>



<p>by just imagining an example where the array is a size of one so keep that in mind and i&#8217;m not going</p>



<p>to show you malloc or anything like that we&#8217;re just going to look at the local variables okay</p>



<p>so for starters i have a make file here that&#8217;s just going to compile a hybrid program if you don&#8217;t</p>



<p>know make files or you don&#8217;t know hybrid programs that&#8217;s okay just see my other videos i&#8217;ve explained</p>



<p>The first source code file here is just driver.cpp.</p>



<p>Again, this is a hybrid program,</p>



<p>so I&#8217;m going to mix my C++ modules with my assembly modules,</p>



<p>which is pretty cool.</p>



<p>The whole point of the driver is just to contain the entry point, you know, main.</p>



<p>And I&#8217;m just going to print a hello message.</p>



<p>And then I&#8217;m going to call the real function that I&#8217;m interested in,</p>



<p>which I&#8217;ve named local underscore varrs.</p>



<p>And that&#8217;s going to be all the assembly stuff that we talked about.</p>



<p>block so that C++ can call an assembly module that&#8217;s explained in other videos. And then</p>



<p>here&#8217;s the real heart of what we got to do. Let&#8217;s write up an assembly module</p>



<p>that can do local variables. Okay. So again, if you don&#8217;t know assembly, that&#8217;s okay,</p>



<p>but you need to watch my other videos before you can understand this one. So I&#8217;m going to just</p>



<p>copy paste some starter code here. This is Yasm assembly in x86-64. So I&#8217;ve got a data section up</p>



<p>So I&#8217;ve got a data section up top and I&#8217;m just going to define some messages.</p>



<p>So, you know, I&#8217;ve got like an intro message that just says, hello, I&#8217;m so and so.</p>



<p>And that&#8217;s not my name, but I like those kinds of names.</p>



<p>And then over here, I&#8217;m going to do some printf formatted strings.</p>



<p>That&#8217;s why I&#8217;m using a hybrid program for this video.</p>



<p>I don&#8217;t want to import my own personal library.</p>



<p>I want you to be able to do this at home with just the GCC libraries.</p>



<p>link a hybrid library you know linking against gcc instead of linking against ld again if you don&#8217;t</p>



<p>know that stuff check my other videos then we&#8217;re allowed to call c functions in this case we&#8217;re</p>



<p>going to call printf and we&#8217;re just going to give it the string percent lu meaning i would like you</p>



<p>to print just you know an unsigned long integer so i&#8217;m going to give it a value at some point on the</p>



<p>stack representing a local variable and then i want it to print as just like a long like a string</p>



<p>along like a string that a human can read then after that this is the carriage return line feed</p>



<p>the crlf printf won&#8217;t flush its output unless that is sitting at the very end of the string so</p>



<p>i&#8217;m just going to use printf to also print my new lines and then i&#8217;m going to null terminate the</p>



<p>string so that printf doesn&#8217;t freak out and try to print a bunch of stuff after the crlf and uh</p>



<p>oh i this was from another video let me get rid of that we don&#8217;t really need crlf in this video</p>



<p>CRLF in this video because we&#8217;re just putting it directly inside of the printf string we&#8217;re not</p>



<p>making our own function to do that so then I&#8217;m going to make some defines I&#8217;m going to define</p>



<p>that we&#8217;re going to have 50 integers so I&#8217;m calling this define 50 I&#8217;m calling it num integers</p>



<p>and I&#8217;m saying that it has a value of 50 so I want to make an array that has 50 integers I don&#8217;t</p>



<p>know maybe if you want to imagine 100 you know like the example that I just showed I&#8217;m going to</p>



<p>going to define what is the integer size so i&#8217;m going to use quad words which are 64 bit integers</p>



<p>so i&#8217;m just going to say that there are eight bytes per integer that will help me multiply later</p>



<p>and then i&#8217;m going to decide to fill up the array on the stack with just some numbers just to prove</p>



<p>that i can just to prove that we can like uh you know manipulate and and fetch the values</p>



<p>of all this stuff going on in the stack and i&#8217;m going to say that the starting value is seven so</p>



<p>with this we should expect to see like an array of numbers that starts with seven and it just</p>



<p>kind of increases somehow then i&#8217;m going to do some system call codes we talked about that in</p>



<p>a different video and then some file descriptors i don&#8217;t think we actually need anything but standard</p>



<p>output but i put it in there anyway then the next thing we&#8217;re going to add is the text section</p>



<p>let me just do copy paste on my solution here so here&#8217;s the text section section text</p>



<p>section text in Yasm and I&#8217;m going to let my module know that I want to be able to call printf</p>



<p>which is a function sitting in the GCC libraries when I link against a GCC I have the ability to</p>



<p>do that that way I don&#8217;t have to come up with like a complicated printing method or use one</p>



<p>of my own shared libraries or something so we can just ask printf to do everything so now here&#8217;s the</p>



<p>entry point for the module it&#8217;s just a function called local VAERS I mark it as global so it&#8217;s</p>



<p>it&#8217;s accessible to outside modules ie or eg driver dot cpp and then so here&#8217;s the label saying the</p>



<p>function starts and here&#8217;s the return statement saying that we&#8217;re done with the function i&#8217;m not</p>



<p>going to manipulate any registers inside of the function so i don&#8217;t really need to do any push pop</p>



<p>to preserve them first thing i&#8217;m going to do is call a welcome let me comment this part out by</p>



<p>the way i&#8217;m going to call a welcome a function and the whole job of the welcome function is just to</p>



<p>is just to, you know, print a welcome message to the user.</p>



<p>So nothing that I haven&#8217;t talked about before in other videos.</p>



<p>So it&#8217;s just we&#8217;re using a system call to print a string.</p>



<p>Okay, so with that in mind, let me open this up here and see if this is going to work.</p>



<p>I just want to basically print the welcome message at this point.</p>



<p>Clear and make run.</p>



<p>And again, if you don&#8217;t know make files or anything like that, see my other videos.</p>



<p>So this is the driver, I think, that prints.</p>



<p>think that Prince maybe I should change the driver&#8217;s message to make it more</p>



<p>clear hello about this is the driver and my name is whatever I&#8217;m gonna do it again</p>



<p>and now it says hello this is the driver okay so that&#8217;s the CPP module and then</p>



<p>here is the assembly module and then finally the driver gets control back and</p>



<p>then the program exits so nothing really happened so now let&#8217;s upgrade the</p>



<p>So now let&#8217;s upgrade the assembly module a little bit.</p>



<p>Next thing I want to add is the actual demo function, which is going to be absolutely huge.</p>



<p>So first I&#8217;m going to start off with, how about just the signature here?</p>



<p>Let&#8217;s go right after the welcome module.</p>



<p>And I&#8217;m just going to copy paste the signature, put a return at the end of it.</p>



<p>So we&#8217;ll consider this a function that can be called.</p>



<p>instruction on line 47 of the entry point and then now we have a demo</p>



<p>function that is being called but it does nothing for starters you know I in</p>



<p>my comments I like to put the signature of the function and I like to remind</p>



<p>myself of how I&#8217;m using my registers hopefully I&#8217;m not using the same</p>



<p>register in two different ways but you know sometimes it happens if it does and</p>



<p>I&#8217;m able to break my function up into multiple parts I&#8217;ll probably do it</p>



<p>you know with modular thinking modular programming but in this case I&#8217;m just</p>



<p>using these registers it&#8217;s fine to use them they are all designated per the ABI</p>



<p>as callee saved so that means the callee which is the demo function is</p>



<p>responsible for preserving them so if you don&#8217;t remember that or if you don&#8217;t</p>



<p>know about that see my other videos this is not an ABI video so I&#8217;m just going to</p>



<p>push all of them to make sure that they&#8217;re preserved I&#8217;m gonna call this</p>



<p>call this the prolog then at the end of my function i&#8217;m going to call this the epilog where</p>



<p>i just restore them in reverse order because that&#8217;s the way the stack returns data to you</p>



<p>the stack returns data to you in reverse order so i have to pop in the reverse register order to</p>



<p>un-reverse the reversal if that makes sense pop r13 pop r12 okay so i think 15 14 13 okay</p>



<p>so i got that next thing we&#8217;ll do is um let&#8217;s remember where the stack pointer started because</p>



<p>started because we have our register here that we&#8217;re going to mess with let me just type rsp</p>



<p>real fast so this is the stack pointer register rsp this helps all programs know where where</p>



<p>they&#8217;re looking at in the stack all of your functions have to be really really careful about</p>



<p>messing with the stack pointer if you do it wrong you will crash the entire program because not only</p>



<p>will your local function not really know where its local variables end and begin it probably also</p>



<p>It&#8217;s return address when you try to return from the function because that is also sitting on the stack</p>



<p>And even if you were lucky enough to be able to jump back correctly to whoever called you</p>



<p>If you messed up the stack pointer then you&#8217;ve also messed it up for any caller of you and any of their callers</p>



<p>So the whole program is ruined. So we&#8217;ll start off by trying to remember where the stack pointer was</p>



<p>We&#8217;ll move</p>



<p>The stack pointer into the base pointer</p>



<p>didn&#8217;t do here that I that I want to do we should since we&#8217;re messing with the</p>



<p>base pointer and other programs sorry other functions or modules might also</p>



<p>rely on the base pointer and it&#8217;s considered callie saved we probably also</p>



<p>want to preserve that too so I&#8217;m going to do push RBP to basically say I would</p>



<p>like to restore I would like to preserve the base pointer so I don&#8217;t mess it up</p>



<p>for my callers so that means I have to restore it with the pop so RBP the base</p>



<p>The base pointer isn&#8217;t necessarily a pointer to the stack, but it&#8217;s often used as kind of like a bookmark.</p>



<p>So we have RBP at the front and the back there.</p>



<p>Let&#8217;s see.</p>



<p>Next thing I want to add is, so now that we&#8217;ve restored it, we&#8217;re allowed to just overwrite it because, you know, we&#8217;re kind of like keeping its value at the top.</p>



<p>Then we&#8217;re restoring its value at the bottom.</p>



<p>And that means we can actually mess it up in the middle if we want to, and it&#8217;ll be fine.</p>



<p>let me show you real fast what happens what happens is nothing the program is</p>



<p>still okay because we we restored it so now we&#8217;re using the base pointer to</p>



<p>remember where the stack was now we&#8217;ve got to sort of calculate how much room</p>



<p>we want to make on the stack let me show you what I&#8217;m talking about here</p>



<p>remember all of our local variables are going to be on the stack and before we</p>



<p>drew this thing where it was like well we&#8217;ve got like a stack sitting here and</p>



<p>let&#8217;s just pretend that there&#8217;s some kind of data sitting on the stack data</p>



<p>data data right if the stack pointer dude green green there we go oh I erased my</p>



<p>green RSP if the stack pointer is currently pointing to this frame then</p>



<p>in order for us to make room on the stack to hold our array well if the</p>



<p>whole array is going to be sitting on the stack that just basically means</p>



<p>have five integers suppose five suppose that we want to make five integers on the stack that just</p>



<p>means we need to do five extra slots let me draw it in red here well let&#8217;s see can we get a green</p>



<p>no how about a yellow my green is just having a hard time we can do it in red even though this is</p>



<p>not a bad thing to do so i&#8217;ll just draw like five extra frames on top of on top of the stack here</p>



<p>imagine these are all 64-bit integers and so they take eight bytes on the stack even though in our</p>



<p>previous example we were using just regular ints which are 32 we&#8217;ll just say we&#8217;re going to make</p>



<p>five 64-bit integers because that&#8217;s easier so they&#8217;re quad words so every frame is actually</p>



<p>eight bytes and not just four bytes and it&#8217;s definitely not one byte so we make five of those</p>



<p>how do we make five slots it&#8217;s pretty easy we we literally just move the stack pointer let&#8217;s see</p>



<p>we just move the stack pointer to or the you know the rsp register to just say let&#8217;s let&#8217;s point you</p>



<p>know further out and how do we get that number we&#8217;re just going to multiply the size of one</p>



<p>integer so you know the size of one integer here we know it&#8217;s going to be eight bytes we just</p>



<p>multiply that by the number of integers that we want you know that&#8217;s going to be 40 so we&#8217;ll just</p>



<p>increase uh sorry decrease the stack pointers value by 40 because remember again when the</p>



<p>vertically, it&#8217;s actually growing downward in memory. So we&#8217;re going to decrease by 40 there,</p>



<p>at least in this drawing example. And that gives us a bunch of junk data, you know, because there</p>



<p>is always going to be some kind of a value sitting at every memory location in your computer.</p>



<p>It&#8217;s impossible that there is literally nothing at some memory location,</p>



<p>unless you&#8217;re trying to go beyond your RAM stick. But then the system will still acknowledge that,</p>



<p>you know, you&#8217;ve done that and it won&#8217;t just give you back nothing if you try to read</p>



<p>nothing if you try to read.</p>



<p>So there&#8217;s going to be junk data sitting on there and then we&#8217;ll loop</p>



<p>through all those slots on the stack and we&#8217;ll just modify the data one by one</p>



<p>so that we can control what it is instead of just printing whatever junk data we end</p>



<p>up with.</p>



<p>So really, we&#8217;re just moving the stack pointer, just making room and then just</p>



<p>remembering where our array is.</p>



<p>We could put, you know, another frame on top if we wanted to make just like one</p>



<p>integer as a local variable.</p>



<p>You just got to remember where it is.</p>



<p>You know, what is it?</p>



<p>What is its offset?</p>



<p>is it what is its offset okay so i&#8217;m going to erase this because we&#8217;re going to do a lot more than</p>



<p>than five uh integers on the stack but i just want you to understand what we&#8217;re doing before we do it</p>



<p>okay so the next thing i&#8217;m going to grab is a move instruction and i&#8217;m going to put it right here so</p>



<p>to move the stack pointer rsp so the first thing i&#8217;m going to do is i&#8217;m going to use a temporary</p>



<p>register we don&#8217;t need to preserve this in the push and pop because it&#8217;s marked as temp so we&#8217;re</p>



<p>not responsible for preserving it and so i&#8217;m going to say r10 is just going to be the number of</p>



<p>integers that we want to create if you recall at the top of our program here num integers is just</p>



<p>50 okay so then the next thing that i&#8217;m going to grab is well i&#8217;m not going to grab it i&#8217;m going</p>



<p>it I&#8217;m going to straight multiply by integer size so again if you look at integer size that&#8217;s going</p>



<p>to be eight because we&#8217;re using quad words for our integers so we&#8217;re really just going to take</p>



<p>50 times 8 whatever number that is is that 400 tell me in the comments if that&#8217;s a right or wrong</p>



<p>and so uh you may or may not know if you don&#8217;t see more videos see more textbooks you may or may not</p>



<p>instruction just multiplies two numbers.</p>



<p>If you use the three operand format,</p>



<p>then the last two operands get multiplied</p>



<p>and the results stored in the first operand.</p>



<p>But if we use the two operand format like I&#8217;ve done here,</p>



<p>then both of those operands get multiplied</p>



<p>and then the result gets stored in the first operand.</p>



<p>So basically at this point,</p>



<p>R10 should hold the number of memory locations</p>



<p>that we should move the stack pointer</p>



<p>in order to make room for all those integers</p>



<p>like I showed you a second ago.</p>



<p>showed you a second ago. So then we&#8217;re going to move the stack pointer.</p>



<p>And maybe I&#8217;ll leave a little comment here. Remember, the stack grows downward</p>



<p>in memory. And so all I&#8217;m doing is subtracting the stack pointer. Remember the stack pointer</p>



<p>register, it just holds a number, which is a memory location. So if you subtract some numbers</p>



<p>from it, you&#8217;re really having it go downward in memory. And that&#8217;s what we want to do</p>



<p>memory and that&#8217;s what we want to do to you know grow the stack for a local variable so I&#8217;m going</p>



<p>to say well I should also say that we&#8217;re using the two operand version so just like I&#8217;m all if we</p>



<p>had the three operand version then the last two operands would have one subtracted from the other</p>



<p>and the result will be stored in the first one but since I&#8217;m using the two operand version</p>



<p>basically it&#8217;s taking RSP minus R10 and then storing it in RSP so this this instruction just</p>



<p>this instruction just says let&#8217;s move the stack pointer downward in memory</p>



<p>enough times that we have room for all of our integers okay no problem next thing that we&#8217;re</p>



<p>going to do is um we&#8217;re going to move r12 we&#8217;re going to move into r12 the current value of rsp</p>



<p>word in memory right less okay so the first integer you know it&#8217;s up to you</p>



<p>how you style this because once we do the subtraction then RSP is actually</p>



<p>going to be pointing towards an integer you could consider that to be the first</p>



<p>integer or the last integer because all we have is an array of integers so you</p>



<p>you know rsp wherever it&#8217;s sitting when we&#8217;re finished we could say that&#8217;s pointing to the</p>



<p>first integer or we could say it&#8217;s pointing to the last integer but um if we decide to say that it is</p>



<p>pointing to the let&#8217;s see yeah if we&#8217;re deciding that it points to the first integer let me just do</p>



<p>a little comment here first integer it just makes it a little bit easier for me to write our loop</p>



<p>You could start by pointing to the one that RSP is pointing to,</p>



<p>or you could start by pointing to the other one that was like the first one that you added onto the stack.</p>



<p>You could call either one of those the first integer, as long as you remember where you started.</p>



<p>So you can increase or decrease the memory location to get to the next integer.</p>



<p>So I&#8217;m just going to do it in this style.</p>



<p>But keep in mind, as long as the only thing that you modify and read is within that range, it&#8217;s okay.</p>



<p>So let&#8217;s remember where is RSP.</p>



<p>So that&#8217;s like the top of the stack.</p>



<p>We&#8217;re going to say R12 holds the stack pointer so that we can use R12 as sort of a running</p>



<p>pointer.</p>



<p>I think that&#8217;s the way I&#8217;m going to use it.</p>



<p>Let me just double check my solution.</p>



<p>Yeah.</p>



<p>Okay.</p>



<p>a calculation where we remember where the last integer is so we know where the first integer</p>



<p>we&#8217;re just going to call this the first integer i&#8217;ll put it in quotes just to remind you that</p>



<p>this is just the style i happen to be using so here we&#8217;re going to say that the first integer</p>



<p>is wherever rsp is pointing and then in r13 we&#8217;re going to remember where the other side of the</p>



<p>array is in this case we&#8217;re calling it the last integer and pretty much it&#8217;s just r12 which is</p>



<p>And then we add to it a memory location.</p>



<p>So remember we said before that the stack grows downward in memory.</p>



<p>So if we consider the top of the stack to be the first integer,</p>



<p>then that means previous items, maybe I should draw this,</p>



<p>are going to be increased memory locations.</p>



<p>So that&#8217;s kind of like the backwards of what you imagine a stack is doing,</p>



<p>but it&#8217;s kind of the way that I like it sometimes.</p>



<p>So imagine just in a very simple stack,</p>



<p>let&#8217;s pretend that the stack has one byte values,</p>



<p>one byte of values, which it doesn&#8217;t, but let&#8217;s pretend if you, if you do it,</p>



<p>I got to learn how to draw. I can&#8217;t get that five right. Okay. I&#8217;m defeated. So let&#8217;s pretend</p>



<p>that that address on the thing sitting at the bottom of the stack is five. So then that means</p>



<p>the next address would be four, right? But if we decided, let&#8217;s say that the, well, let&#8217;s do like</p>



<p>like a few more just to make it a little bit more interesting.</p>



<p>Let&#8217;s do a total of five.</p>



<p>So we&#8217;ll say four, three, two, one.</p>



<p>Maybe I should just add some numbers in front of those values.</p>



<p>So it doesn&#8217;t feel like we&#8217;re hitting zero.</p>



<p>But so just pretend we have a memory location of 15.</p>



<p>That&#8217;s not going to be the case in real life.</p>



<p>Pretend that we have one byte integers on the stack, which is not going to be the case.</p>



<p>We&#8217;re going to have quad words.</p>



<p>But you can see the memory locations go downward, right?</p>



<p>to the stack pointer is pointing to the topmost location we have to remember that anything up here</p>



<p>might exist in system ram but it&#8217;s not considered valid data because we didn&#8217;t</p>



<p>you know make it part of the stack by by growing the stack pointer so that means uh these</p>



<p>these items well maybe i&#8217;ll do a check instead of an x because x looks bad i&#8217;ll do a check</p>



<p>check and a check and a check and a check these items are okay to use so if I have</p>



<p>RSP or in this case we just remembered where RSP was by storing r12 if we have</p>



<p>that memory location 11 and we want to get an additional integer like somewhere</p>



<p>else like the next integer well that would be you know this one down here we</p>



<p>wouldn&#8217;t go in the other direction we wouldn&#8217;t go you know up we would just go</p>



<p>know up we would just go down in the stack but down visually is actually growing upwards in memory</p>



<p>because remember when we grow upwards visually we&#8217;re growing downward in memory so and you can</p>



<p>see here too if we&#8217;re if we&#8217;re increasing 11 to 12 that means we&#8217;re adding memory locations to get</p>



<p>to the next uh integer that we have in the stack so that&#8217;s why here my green is just like frustrating</p>



<p>fix this yeah so that&#8217;s why here we&#8217;re adding a little formula instead of subtracting because</p>



<p>the rsp started there we&#8217;re saying that the top of the stack is the first integer</p>



<p>just so we can add in a more convenient nice way so what are we adding to it we&#8217;re just adding to it</p>



<p>the number of integers minus one and then so that&#8217;s that&#8217;s the number of slots that</p>



<p>size. So if you imagine, you know, if we had 10 integers, then, you know, 10 minus one is nine</p>



<p>slots. So if you imagine that zero is the first integer, let&#8217;s say the memory location again,</p>



<p>then that means one, two, three, four, five, six, seven, eight, nine, 10 minus one is nine. So if we</p>



<p>added the number of integers minus one from the start, which we&#8217;ll consider zero here, that means</p>



<p>directly so that&#8217;s just like a little math because sometimes when you when you</p>



<p>think of adding two numbers together or taking the difference or including the</p>



<p>first number or not including the first number it&#8217;s a little confusing right so</p>



<p>keep in mind for this particular calculation we are adding to it the</p>



<p>number of integers minus one so that will be sitting on the last integer</p>



<p>rather than going past it so start with the first integers memory location and</p>



<p>the number of integers minus one or size minus one times the integer size,</p>



<p>because remember, every integer is going to be eight bytes.</p>



<p>And that will give us the memory location of the last integer in R13.</p>



<p>And then I&#8217;ve said this in other videos,</p>



<p>but basically you&#8217;re only allowed to make these sorts of calculations in Yasm</p>



<p>when you have the calculation inside of brackets,</p>



<p>but brackets will automatically dereference the value on the inside.</p>



<p>It&#8217;ll consider it as a pointer that needs to be dereferenced,</p>



<p>but we don&#8217;t want to dereference anything.</p>



<p>anything we don&#8217;t want to go to a memory location and take a value the memory location is the value</p>



<p>so these dereferencing brackets which are required for the formula are kind of bad so that&#8217;s why we</p>



<p>use the lea instruction instead of the move instruction if i put move there it would</p>



<p>definitely dereference the memory location and give me a value in r13 so r13 wouldn&#8217;t actually</p>



<p>be a pointer it would just be the value of the junk data of the last integer keep that in mind</p>



<p>keep that in mind okay so uh where&#8217;s RSP so we got that so now let&#8217;s do a loop</p>



<p>I&#8217;m gonna start off with a label called demo loop init so for me personally when</p>



<p>I&#8217;m looping you know I like to make my labels inside of my loops start with a</p>



<p>prefix that matches the function so it&#8217;s like demo and then everything else is</p>



<p>gonna be demo underscore something so uh init loop init I&#8217;m gonna make a loop</p>



<p>to make a loop where i initialize the values of the array so i&#8217;m calling this loop the init loop</p>



<p>and then the last part is just like this is the initialization part of the loop this is where we</p>



<p>sort of like set up the initial values to loop so we have r12 and 13 that point to the first</p>



<p>and last integers now we&#8217;re going to set up r14 and 15 where r14 is the running pointer i think</p>



<p>before i might have accidentally said that r13 is the running pointer it just points to the last</p>



<p>it just points to the last integer but if you look back up at my comments r14 is the running pointer</p>



<p>to the current integer what is a running pointer it&#8217;s just a pointer that runs it&#8217;s just a pointer</p>



<p>that just keeps increasing so we can look at different data values so i&#8217;m going to start it</p>



<p>by looking at the first integer so now r14 is pointing to the first integer and then r15 is</p>



<p>going to be the value that i want to put into that position in the array so like the first integer</p>



<p>integer I want to put some kind of a starting value into it you can put the</p>



<p>number zero or whatever you want I just wanted to have a start value so that it</p>



<p>sort of looks more like I&#8217;m putting data and less like I have a loop counter so</p>



<p>remember the integer start value up here is just seven so I&#8217;m just going to start</p>



<p>at the number seven and now I&#8217;m done initializing my loop then the next thing</p>



<p>I&#8217;m going to add is the top of my loop so you can imagine this as the top of a</p>



<p>top of a while loop where you start comparing some sort of an expression maybe i&#8217;ll say uh</p>



<p>you know expr to say that we&#8217;re comparing some sort of an expression</p>



<p>and if that expression evaluates to true the loop continues if it evaluates to false</p>



<p>then the loop does not continue let&#8217;s see so i&#8217;m going to compare r14 with r13 inside of the</p>



<p>and 13 remember R14 is the running pointer and R13 is the last integer so basically I&#8217;m trying to</p>



<p>figure out am I looking at or like let&#8217;s compare the running pointer with the pointer of the last</p>



<p>integer then I&#8217;m going to say if the running pointer has a greater memory location than than</p>



<p>the last integer that means I&#8217;ve gone beyond the last integer and again the way I arranged the first</p>



<p>last integers just makes it easier for me to think of them as having increasing</p>



<p>memory locations so I&#8217;m going to jump if the running pointer has already</p>



<p>surpassed the last integer by saying let&#8217;s jump if it&#8217;s greater than so you</p>



<p>can imagine maybe in the expression here I should probably say while not</p>



<p>r14 is greater than r13 not a great expression but it&#8217;ll do so that&#8217;s what</p>



<p>up there at the top let me just put that into my notes too so that my notes match</p>



<p>the video okay so we are comparing and then we&#8217;re jumping to the end of the</p>



<p>loop if we end up you know going beyond the last integer so that labels not</p>



<p>created yet I&#8217;ll create that in a moment but pretty much that&#8217;s a label that&#8217;s</p>



<p>just going to be below the loop just to say like we&#8217;re finished with the loop</p>



<p>the loop body I like to put comments here to help myself remember oh this is</p>



<p>the part of the while loop that I&#8217;m currently inside of just makes things a</p>



<p>little bit easier to understand you know you put a block comment up top of every</p>



<p>label or every you know chunk of instructions just to let you know the</p>



<p>general idea and then you know sometimes you put comments also on the right side</p>



<p>to help you remember what each instruction is actually doing so then</p>



<p>what am I going to do here remember R14 is the running pointer if I D ref R14</p>



<p>ref r14 that means i want to move an actual value into that memory location rather than changing the</p>



<p>memory location that r14 points to so r15 is going to be the uh the integer that we want to</p>



<p>write into the array so all i&#8217;m doing here is i&#8217;m saying let&#8217;s take that value seven which is what</p>



<p>it starts as and just move it in to you know the ram stick at that memory location so i&#8217;m setting</p>



<p>seven right now then I&#8217;m going to increase our 15 so that means every time</p>



<p>we iterate the loop we should see that the value increases so the first integer</p>



<p>should be seven the second one should be eight next one should be nine and so</p>



<p>forth so just a simple loop where I&#8217;m just writing data into my array so now</p>



<p>that we&#8217;ve ended the loop body let&#8217;s write the bottom of the loop which is</p>



<p>just going to increase the running pointer and jump back up to the top and</p>



<p>And this is not necessarily the only style for translating while loops.</p>



<p>I&#8217;m just doing it.</p>



<p>And, you know, I&#8217;m going to make another video in the future where we talk about, you know, for loops and while loops and all that stuff.</p>



<p>But this video is just really about local variables on the stack.</p>



<p>So I&#8217;m not going to go over all the different ways you can do it.</p>



<p>Anyway, so R14 is the running pointer.</p>



<p>So I&#8217;m going to just make the running pointer jump to the next integer.</p>



<p>And we can do that by increasing its memory location by the size of one integer.</p>



<p>Again, this is another benefit of the first and last pointers that I chose at the beginning.</p>



<p>I can just increase to go to the next integer.</p>



<p>So we&#8217;re going to increase by 8 bytes to just go to the next integer.</p>



<p>If you increase by 1 byte, you&#8217;ll probably have a huge corrupted mess because you&#8217;re</p>



<p>messing with 8 byte integers but you&#8217;re only increasing by 1 byte.</p>



<p>And then after we increase, we&#8217;re just going to jump to the top of the loop.</p>



<p>So notice how I have a jump statement here.</p>



<p>It&#8217;s going to go just to loop top.</p>



<p>loop top. So now this part is here is basically just going to execute over and over and over again</p>



<p>until we finally scan through all of the integers in our array. So that&#8217;s the bottom of the loop.</p>



<p>And then I&#8217;ll make the label for the loop being done. It&#8217;s not really going to do anything</p>



<p>except just be done. And do you, you know, I don&#8217;t know, depending on your style, maybe you can let</p>



<p>drop through if the loop&#8217;s done rather than always jumping to the top but i&#8217;m just going to say when</p>



<p>we&#8217;re done we jump to the loop done label and therefore there&#8217;s no more looping of that</p>



<p>initialization loop okay so we got that done let me just run the program real fast to make sure</p>



<p>that i haven&#8217;t screwed it up we actually should not see anything right now oh what did i do</p>



<p>must have done something naughty maybe if i finish</p>



<p>uh this program then everything will be okay oh my gosh what did i even do</p>



<p>well i&#8217;ve got a working solution in the other window so hopefully when i paste all the extra</p>



<p>steps everything will be fine you never know subtract the stack pointer oh did i forget to</p>



<p>oh did i forget to restore something at the very end print body move the base pointer into the</p>



<p>RSP stack pointer oh yeah okay that&#8217;s definitely what you got that&#8217;s why I</p>



<p>crashed okay so um remember I said you got to be very careful about the stack</p>



<p>this is a great lesson so I did preserve the base pointer but I didn&#8217;t actually</p>



<p>preserve the stack pointer notice how right here I subtracted from the stack</p>



<p>pointer but I did not restore the stack pointer anywhere so that means I</p>



<p>corrupted the stack for anyone that called me and also for my return address</p>



<p>address. So I&#8217;m kind of trying to copy paste my instructions from top to bottom, but I think I&#8217;m</p>



<p>just going to copy paste something else to make sure that we can actually run this. So I&#8217;m going</p>



<p>to copy paste into the epilog a restoration of the stack pointer. And that&#8217;s why we saved the</p>



<p>stack pointer in the base pointer, just to remember where it was when we originally started our</p>



<p>function. So now on line 128, it should be restored and the program should work. Let me just double</p>



<p>yeah it worked okay nothing happened that we can see but it did write values</p>



<p>into the array now let&#8217;s do another loop where we just print the array so let&#8217;s</p>



<p>see loop in it done looping it bottom and looping it done okay so now there&#8217;s</p>



<p>like gonna be another loop here we&#8217;re gonna call this the print loop and so</p>



<p>the print loop is gonna be kind of the same pattern we&#8217;re just gonna loop</p>



<p>going to loop through all the integers in the array but instead of modifying them we&#8217;re just</p>



<p>going to print them so now you know the first thing we&#8217;ll do is we&#8217;ll set r12 we&#8217;ll store that</p>



<p>inside of r14 in order to start the loop at the first integer what was r14 again that was the</p>



<p>running pointer remember let&#8217;s see where is that yeah it was the running pointer so now we&#8217;re</p>



<p>resetting the r14 running pointer to the very beginning of the array and we know where the</p>



<p>thing we&#8217;ll do is we will set up the top of the loop and the body so I&#8217;m just</p>



<p>going to copy paste again this stuff right here</p>



<p>right there okay so the top of the loop we&#8217;re asking ourselves you know we&#8217;re</p>



<p>gonna compare I&#8217;m not gonna put all the extra while stuff that I put in the</p>



<p>previous loop because hopefully by now you understand loops a little bit better</p>



<p>and if we&#8217;re beyond the last integer because the running point of r14 is beyond the memory</p>



<p>location of r13 that means we&#8217;re totally done so we should jump if it&#8217;s greater than</p>



<p>now i feel bad let&#8217;s uh let&#8217;s put a comment in here on the top let&#8217;s go uh</p>



<p>basically that you know if uh keep going as long as r14 is not greater than r13 so if it is</p>



<p>in the body and in the body all i&#8217;m going to do is use r14 the running pointer to print uh you know</p>



<p>whatever value is sitting in that particular integer so how do we do that i&#8217;m just going to</p>



<p>use the printf statement or sorry the printf function which is provided by the c libraries</p>



<p>that&#8217;s why we&#8217;re doing you know a modular or a hybrid program with multiple modules and c linking</p>



<p>very quickly there is a function called printf which I can call it it takes multiple arguments</p>



<p>but the first two arguments that I can give it are the string that represents the formatting</p>



<p>that I want to print like I could do like a regular string message I could do tokens</p>



<p>to format some data inside of them and then the second argument is going to be the piece of data</p>



<p>that I actually want to use let me see if I can just type that up for you real fast</p>



<p>So, you know, the printf instruction, or sorry, this is not an instruction.</p>



<p>This is a function in C.</p>



<p>We would typically, you know, give it some kind of string.</p>



<p>The string should be null terminated, and it should have a new line at the very end of it</p>



<p>to make sure that printf actually flushes.</p>



<p>It won&#8217;t flush if you don&#8217;t have a new line, so the program will look really weird.</p>



<p>But I guess it&#8217;s more performant if you have a way to delay the flushing,</p>



<p>flushing and you know that you can flush it later at the very end but for now i&#8217;m just going to</p>



<p>flush every time and then every argument after that is some sort of you know data that we can</p>



<p>print so imagine we have a long and we&#8217;ll call it a and we&#8217;ll say that it has like some giant value</p>



<p>so that means we would give that long as the next argument the rsi argument and then for the string</p>



<p>what I&#8217;m using right here %lu so you can imagine instead of this string it is this</p>



<p>string right here whoops too many too many quotes it&#8217;s just this string right</p>



<p>here and then instead of a 10 13 that&#8217;s the same thing as just doing an or if</p>



<p>you want to be you know more of a windows windows person slash r slash n</p>



<p>it&#8217;s all good and the zero is not needed because the string if you put a string</p>



<p>literal it&#8217;s automatically going to be null terminated which means there&#8217;s just</p>



<p>null terminated which means there&#8217;s just a zero at the end of the string in memory so this is</p>



<p>basically what I&#8217;m doing I&#8217;m making a an integer in the case of the assembly program it&#8217;s going to</p>



<p>grab an integer from that position in the array that we&#8217;re looking at and it&#8217;s going to give it</p>



<p>as an argument and then the first argument is going to say let&#8217;s just print this as a unsigned</p>



<p>long so that&#8217;s why I have that string here let me search for it and go down a little bit again so</p>



<p>I&#8217;m saying first argument is this the format that I want to be printed second argument is the actual</p>



<p>is the actual value and then I&#8217;m gonna make a call to printf why do I have this</p>



<p>weird push and pop pair sitting around printf so this is not a video about</p>



<p>stack alignment in GCC but basically the GCC libraries expect that your stack is</p>



<p>aligned to I think 16 bytes but since we use 8 byte integers every single time we</p>



<p>address to the stack which is eight bytes and then every time we do one single push or pop</p>



<p>we&#8217;re modifying the alignment of the stack by eight bytes so if you think about it when we&#8217;re</p>



<p>programming in assembly for the most part the stack is going in and out of alignment because</p>



<p>every time we modify it by eight bytes it it might line up with a 16 byte alignment or it might not</p>



<p>it&#8217;s just kind of like oscillating right so when I first wrote this solution I wasn&#8217;t doing the</p>



<p>what happened oh actually maybe i guess i don&#8217;t need to jump to the top right now i don&#8217;t need</p>



<p>to finish the loop let&#8217;s see if this prints just one number well let me let me let me see if this</p>



<p>prints one number certainly and if it&#8217;s an okay assembly program just to print one number is it</p>



<p>going to work okay we got to do the done symbol okay so i&#8217;ll show you in a minute why we need</p>



<p>that for stack alignment but i guess i&#8217;ll just finish the loop so demo print loop done so we</p>



<p>just did print loop top and that means we need the bottom and the done so i&#8217;m just going to copy paste</p>



<p>into program here i&#8217;ll just say nada because we&#8217;re not really doing anything and then at the bottom</p>



<p>you know that&#8217;s the epilogue that&#8217;s separate from the the other label so basically now let me finish</p>



<p>know we already know the loops but I&#8217;ll just I&#8217;ll just say it at the bottom of</p>



<p>the loop we do the same thing that we did with the initialization loop we just</p>



<p>increase the running pointer you know we move it along to the next integer and</p>



<p>then we jump to the top of the loop that&#8217;s it and then the done label we</p>



<p>don&#8217;t really do anything we&#8217;re just letting execution drop through down to</p>



<p>that point so that the loop doesn&#8217;t continue so now we should be able to run</p>



<p>the program don&#8217;t get excited oh actually you know what get excited I</p>



<p>was gonna say don&#8217;t get excited because it was gonna totally work now but now</p>



<p>because it was going to totally work now,</p>



<p>but now I think we can just say that it&#8217;s going to crash.</p>



<p>So if I run it, notice how there&#8217;s a segpult.</p>



<p>So the GCC libraries, many functions expect your stack</p>



<p>to be aligned to 16 bytes.</p>



<p>So if you see mysterious crashes</p>



<p>and you are absolutely sure that you&#8217;re not ruining</p>



<p>the stack pointer or ruining something else,</p>



<p>you&#8217;re doing everything correctly,</p>



<p>but the program still crashes, it might be stack alignment.</p>



<p>So one way to get around stack alignment</p>



<p>is just to move the stack pointer.</p>



<p>move the stack pointer like at the top here we could have said oh we&#8217;ve got one two three four</p>



<p>five we&#8217;ve got five pushes and then here we&#8217;re moving the stack by I don&#8217;t know how many other</p>



<p>addresses the stack might be out of alignment somehow so we could add an extra push up here</p>



<p>and then add a corresponding pop down at the bottom like we could easily do this let me just</p>



<p>show you real fast we could push r15 twice for no reason I acknowledge and then at the bottom</p>



<p>and then at the bottom we pop R15 twice,</p>



<p>that would change the alignment because that&#8217;s one more 8 byte push.</p>



<p>But in my case, and actually that would be a little bit smarter</p>



<p>because if you have our loop where it&#8217;s constantly calling on printf,</p>



<p>this is a lot of hits to memory, right?</p>



<p>This is like 100 hits to memory because every single time we do a push-pop pair</p>



<p>around a call to printf, we&#8217;re like touching memory.</p>



<p>Whereas if I did it at the beginning and the end,</p>



<p>Maybe I should just do it this way.</p>



<p>I want to do it both ways so you understand, but it&#8217;s more efficient, I think, if we do</p>



<p>it this way.</p>



<p>Anyway, so we&#8217;ll do pop twice at the bottom and then push twice at the top.</p>



<p>And so then we don&#8217;t really need to surround it with a push-pop pair.</p>



<p>I think I haven&#8217;t tested this.</p>



<p>We&#8217;ll hope now that the stack is in alignment at all times in our functions so that it doesn&#8217;t</p>



<p>crash.</p>



<p>Yeah, so now see how the program works.</p>



<p>So I&#8217;m going to do it the other way now, which is the less efficient way, because here</p>



<p>now which is the less efficient way because here we just have one extra push</p>



<p>pop pair but if we do it the other way it&#8217;ll still work but we&#8217;ll be hitting</p>



<p>memory much more often so I&#8217;m just gonna do it this way just to show you you can</p>



<p>surround any call because sometimes in your programs you might have the stack</p>



<p>like you know modified throughout the function many different times so it</p>



<p>wouldn&#8217;t make too much sense for you to add an extra push pop pair in the prologue</p>



<p>and epilogue because that might not solve it for all of your calls to all of</p>



<p>to all of your seed library functions.</p>



<p>So in that case,</p>



<p>where you can&#8217;t really predict the stack well enough,</p>



<p>you can just surround your call with a push-pop pair.</p>



<p>It hits memory more, but it&#8217;ll work.</p>



<p>So this is basically, you can imagine,</p>



<p>by the time we get to line 139,</p>



<p>the stack is out of alignment.</p>



<p>So I just do a push that puts it into alignment.</p>



<p>And then after the call comes back,</p>



<p>I just pop it so it&#8217;s back out of alignment again,</p>



<p>but I don&#8217;t have extra erroneous data sitting on the stack.</p>



<p>Because if I only had push and not pop,</p>



<p>then it&#8217;s going to push it more and more out of alignment.</p>



<p>It&#8217;s going to push it in and out and in and out,</p>



<p>but it&#8217;s going to add a bunch of junk data to the stack</p>



<p>that I&#8217;ll never recover from.</p>



<p>Or actually, I guess I will recover at the very end</p>



<p>when I restore the stack pointer, but it&#8217;s pointless.</p>



<p>It&#8217;s going to consume too much memory.</p>



<p>What if I was writing like a billion</p>



<p>or like a million items on the stack, right?</p>



<p>a billion iterations of the loop would probably be a bad idea to start adding onto the stack</p>



<p>we&#8217;ll probably end up stack overflowing probably with far less than a billion items so anyway i&#8217;m</p>



<p>going to surround the call with the push pop pair and then at the bottom we just do the same thing</p>



<p>you know increase the integer and then go to the top and so now we you know you already can see it</p>



<p>it starts off printing well it starts off initializing the array but then the thing that we</p>



<p>can see starts off printing the first integer and then the next iteration of the loop prints the</p>



<p>second integer and the third and the fourth and so forth until we get all the way down just to</p>



<p>prove to you maybe we can increase it by two instead of one each time I&#8217;ll just modify that</p>



<p>Oh no, that&#8217;s the print loop. Let&#8217;s do the init loop.</p>



<p>Init loop bottom.</p>



<p>Oh, right there, when we&#8217;re in the body.</p>



<p>So I&#8217;m just going to increase R15 twice.</p>



<p>Just to show you, you know, we can kind of control what&#8217;s going inside of the local array.</p>



<p>So see how it goes from 7 to 9 to 11 instead of 7, 8, 9.</p>



<p>So I&#8217;m going to take that out and then just show you that we can control</p>



<p>how many integers we have in our local array with just the number of integers.</p>



<p>just the number of integers so I&#8217;m going to change the 50 to a 5 and run it again and you can see</p>



<p>what the dang did I do number of integers is 5 and then it was 50 did I put the stack out of</p>



<p>alignment no I I have a push put pair there number 50 is not hard-coded anywhere anywhere</p>



<p>there. Oh, I have a bug that I can debug. That&#8217;s nice. I guess the bug debugging is not for this</p>



<p>video. I wonder if some other value would work for that. There&#8217;s a number of things that could</p>



<p>be the problem. It could be like stack alignment somehow. It could also be, let&#8217;s see, am I making</p>



<p>any calls here inside of my function demo? I&#8217;m saying sub and LEA and then start and then jump</p>



<p>and start and then jump and increase and jump and then RDI.</p>



<p>Not really doing anything else.</p>



<p>So I don&#8217;t think it&#8217;s stack alignment.</p>



<p>I must have miscalculated somewhere somehow for changing that.</p>



<p>All right.</p>



<p>Well, if I figured it out, then I will release another video.</p>



<p>But basically, this is the idea.</p>



<p>100 works, 50 works.</p>



<p>What about 99?</p>



<p>Would that work?</p>



<p>Save faults on 99.</p>



<p>faults on 99 and then 98. 98 works so like every two seems to work but that is</p>



<p>two quad words or 16 oh right so like a hundred integers if we assume that</p>



<p>that&#8217;s in alignment then 99 would be eight bytes less that we&#8217;re moving the</p>



<p>98, notice how it is okay.</p>



<p>And then 97, it&#8217;s going to segfault again.</p>



<p>Watch.</p>



<p>This is a great stack alignment video, I guess.</p>



<p>96, it won&#8217;t segfault.</p>



<p>So it&#8217;s going in and out of alignment.</p>



<p>I think I figured it out.</p>



<p>So if we use the number 100, it&#8217;s in alignment.</p>



<p>Sorry, it&#8217;s out of alignment.</p>



<p>If we use the number 100, the program works because the number 100 throws it out of alignment.</p>



<p>number 100 throws it out of alignment but then we have this push pop pair here around the call to</p>



<p>printf which puts it back in alignment so if I change the number of integers I&#8217;m actually</p>



<p>changing the number of memory locations that I modify the stack pointer so I have to do it by</p>



<p>twos if I wanted to do 99 here then that means the stack is in alignment by the time I call</p>



<p>by the time I&#8217;m getting ready to call printf which means the push pop pair around it throws</p>



<p>push pop pair around it throws the stack out of alignment so just watch here I&#8217;ll</p>



<p>prove it okay so it&#8217;s sake false I comment that out now it won&#8217;t throw it</p>



<p>out of alignment okay should have known that before I recorded the video it&#8217;s</p>



<p>fun to guess sometimes though you know I get a little nervous oh no my program</p>



<p>broke on camera can I debug it live well I guess I can but kind of slowly</p>



<p>anyway so we I think we&#8217;ve gone over every single part that I wanted to show you</p>



<p>we know how to create a local array on the stack and and therefore you also know how to create like</p>



<p>any other data type on the stack if you want you want to create a long a 64-bit integer</p>



<p>just move it by eight bytes instead of moving it by eight times however many integers we were doing</p>



<p>in this video you want to I don&#8217;t know put a character on the stack you can do that if you</p>



<p>Just, you know, move it by one memory location instead of eight memory locations.</p>



<p>So, you know, one byte instead of eight bytes.</p>



<p>You want to store a short, you know, a two byte integer?</p>



<p>Well, just move it by two bytes instead of eight bytes, right?</p>



<p>So you can do this as many times as you want.</p>



<p>You want to have several local variables?</p>



<p>Just move it one time for every local variable.</p>



<p>Same thing for accessing.</p>



<p>You just have to remember where everything is.</p>



<p>offset of the of the first variable local variable and then the offset of the second</p>



<p>local variable you can store those in globals or store those in registers if you can you just</p>



<p>got to remember somehow where everything starts but it&#8217;s all sitting on the stack if it&#8217;s a local</p>



<p>variable okay I guess that&#8217;s everything that I really have to say I hope you enjoyed this video</p>



<p>I hope you learned a little bit of stuff and had a little bit of fun I&#8217;ll see you in the next video</p>



<p>have a good one</p>



<p>you</p>



<p>Hey everybody, thanks for watching this video again from the bottom of my heart. I really appreciate it</p>



<p>I do hope you did learn something and have some fun</p>



<p>If you could do me a please a small little favor</p>



<p>Could you please subscribe and follow this channel or these videos or whatever it is?</p>



<p>You do on the current social media website that you&#8217;re looking at right now</p>



<p>It would really mean the world to me and it&#8217;ll help make more videos and grow this community</p>



<p>more videos, longer videos, better videos, or just I&#8217;ll be able to keep making videos in general.</p>



<p>So please do me a kindness and subscribe. You know, sometimes I&#8217;m sleeping in the middle of</p>



<p>the night and I just wake up because I know somebody subscribed or followed. It just wakes</p>



<p>me up and I get filled with joy. That&#8217;s exactly what happens every single time. So you could do</p>



<p>it as a nice favor to me or you could troll me if you want to just wake me up in the middle of</p>



<p>the night, just subscribe and then I&#8217;ll just wake up. I promise that&#8217;s what will happen.</p>



<p>Also, if you look at the middle of the screen right now, you should see a QR code which you can scan in order to go to the website</p>



<p>which I think is also named somewhere at the bottom of this video and</p>



<p>It&#8217;ll take you to my main website where you can just kind of like see all the videos</p>



<p>I published and the services and tutorials and things that I offer and all that good stuff and</p>



<p>If you have a suggestion for</p>



<p>Please leave a comment or if you just want to say hey, what&#8217;s up? What&#8217;s going on?</p>



<p>You know just send me a comment whatever I also wake up for those in the middle of the night</p>



<p>I get I wake up in a cold sweat. I&#8217;m like</p>



<p>It would really it really mean the world to me. I would really appreciate it. So again, thank you so much for watching this video and</p>



<p>Enjoy the cool music as I fade into the darkness, which is coming for us all</p>



<p>Thank you.</p>
<p>The post <a href="https://www.NeuralLantern.com/x86-64-assembly-local-variables-stack-frames-alignment-explained/">x86-64 Assembly: Local Variables, Stack Frames &amp; Alignment Explained</a> appeared first on <a href="https://www.NeuralLantern.com">NeuralLantern.com</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.NeuralLantern.com/x86-64-assembly-local-variables-stack-frames-alignment-explained/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>The Call Stack Explained: Call Frames, Return Addresses, Local Variables</title>
		<link>https://www.NeuralLantern.com/the-call-stack-explained-call-frames-return-addresses-local-variables/</link>
					<comments>https://www.NeuralLantern.com/the-call-stack-explained-call-frames-return-addresses-local-variables/#respond</comments>
		
		<dc:creator><![CDATA[mike]]></dc:creator>
		<pubDate>Sat, 07 Mar 2026 22:50:18 +0000</pubDate>
				<category><![CDATA[Assembly Language]]></category>
		<category><![CDATA[Coding]]></category>
		<category><![CDATA[Computer Science]]></category>
		<category><![CDATA[Videos]]></category>
		<category><![CDATA[assembly language stack]]></category>
		<category><![CDATA[C++ stack]]></category>
		<category><![CDATA[call frames]]></category>
		<category><![CDATA[call stack]]></category>
		<category><![CDATA[call stack explained]]></category>
		<category><![CDATA[call stack tutorial]]></category>
		<category><![CDATA[computer science fundamentals]]></category>
		<category><![CDATA[function call stack]]></category>
		<category><![CDATA[heap vs stack]]></category>
		<category><![CDATA[how function calls work]]></category>
		<category><![CDATA[local variables stack]]></category>
		<category><![CDATA[memory management]]></category>
		<category><![CDATA[process stack]]></category>
		<category><![CDATA[programming under the hood]]></category>
		<category><![CDATA[recursion stack overflow]]></category>
		<category><![CDATA[return address]]></category>
		<category><![CDATA[stack frames]]></category>
		<category><![CDATA[stack memory]]></category>
		<category><![CDATA[x86-64 call stack]]></category>
		<guid isPermaLink="false">https://www.NeuralLantern.com/?p=314</guid>

					<description><![CDATA[<p>The call stack is a special stack data structure that your computer uses to manage function calls. Every time a function is called, a call frame is pushed onto the stack containing the return address, function arguments (beyond what fits in registers), preserved registers, and space for local variables. When the function returns, its call frame is popped and execution jumps back to the return address - this is how your program knows exactly where to continue after a function finishes.</p>
<p>The post <a href="https://www.NeuralLantern.com/the-call-stack-explained-call-frames-return-addresses-local-variables/">The Call Stack Explained: Call Frames, Return Addresses, Local Variables</a> appeared first on <a href="https://www.NeuralLantern.com">NeuralLantern.com</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<figure class="wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio"><div class="wp-block-embed__wrapper">
<iframe title="The Call Stack Explained: Call Frames, Return Addresses, Local Variables" width="1380" height="776" src="https://www.youtube.com/embed/kT_s5XADSgs?feature=oembed" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen></iframe>
</div></figure>



<p>Ever wonder how your program remembers where to return after calling a function? That&#8217;s the call stack&#8217;s job.</p>



<p>In this clear, whiteboard-style explanation we cover:</p>



<ul class="wp-block-list">
<li>What the call stack actually is</li>



<li>How call frames are built and what they contain</li>



<li>Return addresses, function arguments, local variables</li>



<li>Why recursion works (until you overflow the stack)</li>



<li>Stack vs heap memory differences</li>



<li>Why too much recursion = crash</li>
</ul>



<p>Great for beginners to intermediate programmers who want to understand what&#8217;s happening under the hood. References Ed Jorgensen&#8217;s excellent (and free) x86-64 assembly book.</p>



<p>00:00 Introduction to the Call Stack<br>00:16 Prerequisites &#8211; Understanding Basic Stacks<br>00:41 Simple Example Program with Function Calls<br>01:24 Function Call Chain and Recursion Example<br>02:20 Visualizing the Call Sequence<br>03:06 Local Variables and Return Values<br>03:55 Quick Recap &#8211; How Generic Stacks Work<br>04:34 Stacks Can Hold Any Data Type<br>05:36 Introducing the Call Frame Concept<br>06:22 What Belongs Inside a Call Frame<br>07:00 Return Address Explained<br>07:44 How the CALL Instruction Works<br>08:32 Function Arguments on the Stack<br>09:57 Extra Arguments Beyond Registers<br>10:19 Preserving Registers (Callee-Saved)<br>11:12 Local Variables on the Stack<br>11:40 Multiple Call Frames Example<br>12:54 Tracing Function Calls Step by Step<br>13:21 Starting with Main&#8217;s Call Frame<br>15:00 Pushing Frames for Nested Calls<br>16:30 Returning &#8211; Popping Frames<br>18:10 Why the Stack is Perfect for Returns<br>19:45 Recursion and Multiple Instances<br>21:30 Stack Grows Downward in Memory<br>23:10 Stack vs Heap Memory Comparison<br>25:12 Local Variables vs Dynamically Allocated Memory<br>26:16 Pointers on Stack Pointing to Heap<br>27:32 Automatic Cleanup of Stack Variables<br>28:00 Memory Leaks with Heap Allocations<br>28:56 Recommended Reading &#8211; Ed Jorgensen Book<br>29:56 Call Frame Layout in x86-64 Assembly<br>31:16 Process Memory Layout Overview<br>32:22 Stack Grows Down, Heap Grows Up<br>33:52 Stack Overflow from Deep Recursion<br>35:09 Summary &#8211; Why the Call Stack Matters<br>35:28 Closing Remarks and Call to Subscribe</p>



<p>=-=-=-=-=-=-=-=-=</p>



<p>Thanks for watching!</p>



<p>Find us on other social media here:</p>



<ul class="wp-block-list">
<li>https://www.NeuralLantern.com/social</li>



<li>Twitter / X: https://x.com/NeuralLantern</li>



<li>Rumble: https://rumble.com/c/c-3696939</li>



<li>BitChute: https://www.bitchute.com/channel/pg1Pvv5dN4Gt</li>



<li>Daily Motion: https://www.dailymotion.com/neurallantern</li>



<li>Minds: https://www.minds.com/neurallantern/</li>



<li>Odysee: https://odysee.com/@NeuralLantern:5</li>
</ul>



<p>Please show your support!</p>



<ul class="wp-block-list">
<li>Buy me a coffee: https://ko-fi.com/neurallantern</li>



<li>Subscribe + Sharing on Social Media</li>



<li>Leave a comment or suggestion</li>



<li>Subscribe to the Blog: https://www.NeuralLantern.com</li>



<li>Watch the main &#8220;pinned&#8221; video of this channel for offers and extras</li>
</ul>



<p>Hello there. In this video we&#8217;re going to talk about the call stack. What is the call stack?</p>



<p>It&#8217;s an important data structure inside of your computer that helps your computer call</p>



<p>functions inside of code. It&#8217;s pretty cool.</p>



<p>So first off, before you watch this video, you should probably already understand</p>



<p>how a stack works, what a stack is in terms of its interface and how to manipulate it</p>



<p>See my previous video if you don&#8217;t understand how to use stacks yet. In this video, I&#8217;m just going to talk about the call stack, which is just a special use of the generic stack abstract data type data structure.</p>



<p>Okay, so let&#8217;s see. First off, let&#8217;s suppose for the sake of argument that I have, you know, a function called main. This is not a C++ video or a coding video, but the call stack is related to coding.</p>



<p>mean when I write down some code. This is just a simple C++ program. You can imagine that</p>



<p>up above main, actually I don&#8217;t want to use prototypes, but I&#8217;m going to put the other</p>



<p>functions below. Please forgive me. You can imagine that main calls on the function f,</p>



<p>so I&#8217;m just going to do a function called f here. And so execution, when it gets to that part of</p>



<p>the program on line three, execution is going to jump down to line nine to just execute the f</p>



<p>And then maybe F calls G and then G it calls H and maybe H sometimes calls itself.</p>



<p>Maybe we&#8217;ll say that it&#8217;s a recursive function.</p>



<p>We&#8217;ll say if, you know, something happens, then we will return a call or we&#8217;ll just call H.</p>



<p>So it calls itself sometimes.</p>



<p>Otherwise, we will have it to call on the I function.</p>



<p>then you know execution just kind of returns to the caller which would be G in</p>



<p>this case and then we have the I function that just you know does whatever</p>



<p>right so the code here doesn&#8217;t really matter what I&#8217;m really trying to show</p>



<p>you is that one function calls another calls another and we can sort of allow</p>



<p>this to happen under the hood with a call stack so I&#8217;m just gonna draw this</p>



<p>and G calls on H and H we&#8217;ll say that we&#8217;ll say for the sake of argument one time H calls on H</p>



<p>just so it&#8217;s easier to draw later and then H eventually calls I for some other reason</p>



<p>maybe the deeper H calls I and then eventually this starts to return right so if you&#8217;re a</p>



<p>programmer you kind of take this situation for for granted you think like well I just kind of</p>



<p>call a function and then and then when the function&#8217;s done I just return to the place</p>



<p>everything is fine. If we had, let&#8217;s say, local variables in here, they would be preserved. Let&#8217;s</p>



<p>say, I don&#8217;t know, maybe h is like a number or something like that. Or how about let&#8217;s do g as</p>



<p>a number. We&#8217;ll say g returns some kind of a number. And then maybe in the f function, when</p>



<p>we call g, we&#8217;ll just print whatever g returned. So g calls h, and then we&#8217;ll just say like return</p>



<p>h and then we&#8217;ll just say like return whatever just trying to show you that we might have</p>



<p>a local variable so that means um in the g function maybe you&#8217;ll have a integer a is equal</p>



<p>to five and integer b is equal to six i&#8217;m just making up stuff here and then when we return</p>



<p>our data we might say you know return a plus b for whatever reason this is a nonsense program again</p>



<p>don&#8217;t take it too seriously but i&#8217;m just trying to show you that we have functions that can call</p>



<p>we have functions that can call each other sometimes call themselves and maybe they have</p>



<p>local variables on top of that so if you understand what is a stack then you understand that a stack</p>



<p>can hold many different types of data just as a quick recap here um you know a regular</p>



<p>generic data structure stack or an abstract data type stack we could say that the stack hold held</p>



<p>integers so if i wanted to put like an eight on there and then put like a five on there it would</p>



<p>it would just kind of stack up as we&#8217;re adding data.</p>



<p>And then when eventually we decided to remove data from the stack,</p>



<p>we&#8217;d pop it by just removing the thing from the top.</p>



<p>But because stacks are supposed to be abstract data types that hold any type of data,</p>



<p>we&#8217;re not limited to putting integers inside of the stack.</p>



<p>So we could invent a totally new data structure</p>



<p>and put instances of that data structure inside of the stack.</p>



<p>For example, if you had a custom class that you wrote yourself</p>



<p>and you just called it my class,</p>



<p>my class let&#8217;s see uh let me do class my class and i&#8217;ll just put braces here again this is not</p>



<p>a c++ video but you know you can imagine that you sort of made a blueprint for a new type of object</p>



<p>and you called it my class and then later on you&#8217;re like let&#8217;s make instances of my class</p>



<p>let&#8217;s make an instance called mc1 and mc2 and mc3 and so forth right so in the stack you could just</p>



<p>instances of my class i&#8217;m just going to put mc1 here if we created mc1 and then pushed it onto</p>



<p>the stack and then we can have mc2 and created an instance of that and just kind of pushed it</p>



<p>on the stack so we can do whatever we want we can put any type of data on the stack m</p>



<p>and this is not exactly the process stack so far i&#8217;m just telling you what a stack</p>



<p>in general can do so now instead of imagining that we have a custom class called my class</p>



<p>made a new class called call frame I&#8217;ll say we have like an instance let&#8217;s say</p>



<p>like a blueprint for a class we&#8217;ll call it a call frame I&#8217;m a penmanship it&#8217;s</p>



<p>because this thing this pen and I push it down and it always like keeps drawing</p>



<p>after I start lifting it up I don&#8217;t know what I&#8217;m doing wrong I&#8217;ve been trying</p>



<p>this for like 50 videos and it&#8217;s always bad and wrong so imagine you have a</p>



<p>class called call frame and inside of it you can just put some sort of data in</p>



<p>some sort of data inside the call frame.</p>



<p>You can imagine defining what an object might look like and then creating instances and</p>



<p>filling it up with data and then every instance just like we did with my class we could just</p>



<p>put that onto a stack right?</p>



<p>So now let&#8217;s try to understand what might be inside a data structure called a call frame</p>



<p>because when we have a stack under the hood in our machine that holds instances of call</p>



<p>frames then we actually have a process stack or a call stack which allows us to implement</p>



<p>which allows us to implement these function calls.</p>



<p>So let&#8217;s see, the first thing that we might imagine,</p>



<p>I&#8217;m gonna maybe just do like a little box here,</p>



<p>just to let you know that we&#8217;re deciding</p>



<p>what will go inside of a call frame.</p>



<p>So this is one call frame.</p>



<p>So you can imagine that the F function here,</p>



<p>in order for the F function to exist,</p>



<p>the particular call to exist,</p>



<p>what you know what it has what it is to be alive you know it needs its return</p>



<p>address I&#8217;m gonna put RA here for its return address what is a return address</p>



<p>well you know when we were inside of the main function let me draw the execution</p>



<p>path real fast when we were inside of the main function then execution was</p>



<p>going from top to bottom when we hit this F function call execution actually</p>



<p>jumped to the first line of the F function under the hood in assembly</p>



<p>there&#8217;s a jump instruction that allows you to just kind of jump to a totally</p>



<p>just kind of jump to a totally different memory location and start executing</p>



<p>instructions over there somewhere. In assembly also we have a call</p>



<p>instruction which means I would like to jump to another location but also I want</p>



<p>to remember where I just came from so that it&#8217;s easy for me to return later.</p>



<p>Okay so we&#8217;ll call that the return address. The return address in this case</p>



<p>would be let&#8217;s just call it line 4 meaning when function f returns then the</p>



<p>is this return zero in well I mean this is C++ we don&#8217;t it&#8217;s not really instructions it&#8217;s more</p>



<p>like statements but you can just imagine that F gets called and when it returns then the next</p>



<p>statement is return zero right so if we say that the memory location of wherever line four starts</p>



<p>is the return address of F then you can imagine that in order for F&#8217;s you know call instance to</p>



<p>exist it has to remember its return address so therefore we would put the return address inside</p>



<p>call frame. So the return address goes in there and function arguments sometimes go in there. If</p>



<p>you know assembly, and that&#8217;s kind of what we&#8217;re talking about right now in this video, then</p>



<p>you&#8217;ll know that function arguments, at least the integer arguments usually come in certain</p>



<p>registers like RDI and RSI, or if you&#8217;re, whoops, or if you&#8217;re passing float arguments,</p>



<p>and XMM1 and so forth right so you just kind of like load up registers with</p>



<p>arguments but if you have more arguments than you have registers you know</p>



<p>there&#8217;s only so many registers you can actually use to pass arguments into a</p>



<p>function that you&#8217;re calling so what if you run out of float registers you run</p>



<p>out of the general purpose registers but you want to have more arguments than</p>



<p>that for example if you look at the ABI then the integer arguments there&#8217;s only</p>



<p>integer arguments there&#8217;s only six registers that we can use to pass integer or pointer arguments</p>



<p>so if you wanted to have seven arguments or more then you know you should be able to do that but</p>



<p>there has to be a different way to accomplish that so the seventh and beyond arguments will</p>



<p>actually uh show up in the call frame oh I guess ra should probably be blue because the the call</p>



<p>frame I&#8217;m just you know making that green I&#8217;ll say return address so the seventh and uh the</p>



<p>the seventh and beyond arguments args we&#8217;ll just say so the call frame contains the return address</p>



<p>and any argument that&#8217;s that&#8217;s like the seventh argument and beyond and if you don&#8217;t have seven</p>



<p>arguments then there&#8217;s not going to be that&#8217;s not going to be in the in the call frame it&#8217;ll just be</p>



<p>blank at that point sometimes when people are messing around with the stack pointer they will</p>



<p>Basically, anytime you push a register in order to preserve it,</p>



<p>then it will end up on the call frame, at least at the assembly level.</p>



<p>So suppose you&#8217;re going to use the base pointer register.</p>



<p>You probably want to preserve the base pointer before you start using it.</p>



<p>So we would push it onto the stack and it would become part of the call frame.</p>



<p>Any registers that are designated as callee saved</p>



<p>would also be pushed into the call frame like R12.</p>



<p>Oh my God.</p>



<p>R13.</p>



<p>my penmanship there&#8217;s like it&#8217;s the ones the ones are always l&#8217;s okay and so forth right so</p>



<p>anything that&#8217;s designated as a callee saved register that you intend to use would end up</p>



<p>getting pushed onto the stack at the assembly level and thus it would end up in the call frame</p>



<p>but you know if you aren&#8217;t going to use any callee preserved registers then you don&#8217;t need to</p>



<p>preserve them and therefore they would not show up in the call frame and let&#8217;s see and local</p>



<p>local variables in assembly, you just basically end up pushing them to the stack.</p>



<p>So I&#8217;m going to put local variables here.</p>



<p>And in a second, I&#8217;ll show you how that kind of works in higher level languages like on</p>



<p>C++.</p>



<p>That&#8217;s why I wrote this down right here, line 12.</p>



<p>We&#8217;re making local variables.</p>



<p>And let&#8217;s see what else.</p>



<p>I should emphasize that local variables are very different from heap variables.</p>



<p>some point let&#8217;s say that when when g was called let&#8217;s say execution came in here</p>



<p>into main and then execution jumped into the f function so it jumped in there and then f wanted</p>



<p>to call on g in order to get some information right so we have a call frame just for g and we</p>



<p>have a call frame just for f and we have a call frame just for main and so we have like three</p>



<p>items on our stack at least just to get to that point of the program and every call frame you</p>



<p>you know, basically has this sort of information inside of it.</p>



<p>The return address, so the function knows where to jump back to</p>



<p>when it&#8217;s time to return from the function.</p>



<p>Seventh and beyond arguments, if any exist.</p>



<p>The base pointer, if you happen to modify that at the assembly level.</p>



<p>And then same thing for Kali saved registers.</p>



<p>And then local variables, which you could probably imagine more clearly.</p>



<p>So now I&#8217;m just going to erase this call frame thing here.</p>



<p>Oh God, this eraser needs to be improved.</p>



<p>god this eraser needs to be improved oh my gosh maybe i should make that bigger i need like a</p>



<p>little shortcut to make it bigger for a second because i don&#8217;t really want to draw that thing</p>



<p>on top again that&#8217;s just annoying i don&#8217;t want to do it i won&#8217;t do it i won&#8217;t do it okay so uh</p>



<p>imagine now that we are trying to sort of trace what&#8217;s happening in our uh call graph you know</p>



<p>in our in our program while we&#8217;re calling functions so first imagine we have an empty</p>



<p>have an empty stack and again if you don&#8217;t understand stacks you better go see my other video</p>



<p>imagine we have an empty stack the stack is not ever really going to be empty when you launch</p>



<p>your program it&#8217;s going to be uh there&#8217;s going to be stuff on it already but let&#8217;s just pretend that</p>



<p>we haven&#8217;t put anything on the stack at this point so there would be a you know a call frame just for</p>



<p>the main function i&#8217;m just going to write main here to say that this is the call frame for main</p>



<p>so it contains all the information that main needs and um if you have a main function in the first</p>



<p>first place then you&#8217;re probably linking against the GCC libraries which means</p>



<p>a main definitely has to return somewhere inside of the GCC library so</p>



<p>this kind of makes sense for our program so it&#8217;s a call frame for main</p>



<p>and then when main calls on F then another call frame gets created for F</p>



<p>and then when F calls on G another call frame gets created for G I don&#8217;t know if</p>



<p>I need to put these parentheses but I guess I&#8217;ve already started to do that</p>



<p>h and then h sometimes calls itself so I&#8217;m gonna put another call frame for h</p>



<p>up here oh my god h&#8217;s look like just crazy okay that&#8217;s been uh every time I</p>



<p>lift up the pen h calls itself sometimes we&#8217;ll just say that it calls itself</p>



<p>once in this case and then h calls i and then that&#8217;s let&#8217;s just say that&#8217;s the</p>



<p>end of our little call graph when i is finished doing whatever it&#8217;s gonna do</p>



<p>to do then it&#8217;ll just return to the caller right so notice how every single time we made a function</p>



<p>call we added a call frame for that particular instance of a call and we called it a call frame</p>



<p>something else that i should point out is that i have been describing to you a call frame as if it&#8217;s</p>



<p>a a data structure with sort of like a fixed amount of data inside of it and you would sort of</p>



<p>blob that whole block of data on at the same time that&#8217;s not really how it works in assembly in</p>



<p>you kind of just push, you know, one tiny piece of data at a time.</p>



<p>But in the abstract, you imagine that there&#8217;s a call frame.</p>



<p>Just keep that in mind.</p>



<p>Anyway, so in the F function, you know, we&#8217;ve been making these calls F and then G and then H and then H and then I.</p>



<p>Eventually, when we got to the, let&#8217;s say, what did I do wrong?</p>



<p>Oh, I did something wrong.</p>



<p>I put the local variables in the wrong function.</p>



<p>Hang on a second.</p>



<p>I want them to be, I mean, it would be okay to have them inside of the G function, but I really want them.</p>



<p>but I really want them, I really want them to be inside the H function for a certain reason.</p>



<p>So I&#8217;m just going to cut and paste here.</p>



<p>And I&#8217;m going to do like local variables inside of the H function and not in the G function.</p>



<p>And then G should maybe return an integer result.</p>



<p>And then this return statement shouldn&#8217;t even be in G.</p>



<p>It&#8217;s just going to be inside of H.</p>



<p>the a and the b variables at some point before returning them modify a and b somehow maybe I can</p>



<p>even put that comment inside of both of the blocks of the if so like so maybe if something happens</p>



<p>we&#8217;ll modify them in one way and if it doesn&#8217;t happen we&#8217;ll modify them in some other way</p>



<p>and then at the very end we return them just to kind of feel like we have more of an excuse</p>



<p>to have return variables in the first place and then when h returns something now we have an excuse to kind of print out whatever h is</p>



<p>doing okay and the reason I moved all that inside of h and not in g is because I just want you to know that</p>



<p>There are two instances of the h, you know call instance on the call stack which means the local variables</p>



<p>They&#8217;re not linked. They&#8217;re two distinct copies of each local variable inside of the stack think of it this way</p>



<p>When we call our h the first time,</p>



<p>we have an integer a and an integer b.</p>



<p>You could imagine that there&#8217;s an integer a</p>



<p>and an integer b sitting inside of the call frame somewhere,</p>



<p>you know, being pushed onto the call stack.</p>



<p>And, but the other called h also has a variable a</p>



<p>and a variable b,</p>



<p>but because those are local variables,</p>



<p>changing one doesn&#8217;t actually modify the other copy</p>



<p>that is in the other call frame.</p>



<p>So you could imagine this as A sub one and B sub one,</p>



<p>and then A sub two and B sub two.</p>



<p>This is kind of important later on when you realize</p>



<p>that your operating system is capable of launching multiple threads for your program</p>



<p>and local variables won&#8217;t potentially become race conditions,</p>



<p>which is just like for a totally different video.</p>



<p>But for now, just to understand these are totally separate instances.</p>



<p>If I change A sub two, it&#8217;s not changing A sub one and vice versa.</p>



<p>And same thing for B.</p>



<p>vice versa and same thing for B so anyway we we make all these calls and</p>



<p>somewhere along the way you know we&#8217;re creating a bunch of data and a bunch of</p>



<p>call frames eventually when we are done with the I function when I wants to</p>



<p>return here we don&#8217;t have an explicit return statement but if you if you know</p>



<p>C++ or higher level languages if there&#8217;s no return statement in most of</p>



<p>the language in most of these languages for a void function it just returns when</p>



<p>when it reaches the end of the function we don&#8217;t really need to say return because it doesn&#8217;t</p>



<p>return anything it just ends so when i is ready to return what happens is we look inside of its</p>



<p>call frame and we look for the return address which is inside of its call frame the return</p>



<p>address specifies exactly where we&#8217;re supposed to return for example maybe we&#8217;re supposed to return</p>



<p>right here maybe we&#8217;re supposed to return right here maybe to make things a little bit more</p>



<p>so maybe if we called h then we increased a and if not then we increased b something like that</p>



<p>so that means the the call frame for the first h function let&#8217;s suppose let&#8217;s see if it called</p>



<p>if it called itself then that means this block right here is the first part of the if block so</p>



<p>the return address for h return address would actually be pointing to</p>



<p>would actually be pointing to the next line right so when h called on h</p>



<p>it would look at line 21 or whatever the address was for that instruction</p>



<p>to begin at you know increasing a and it would just say that&#8217;s the return address so that</p>



<p>whenever h returns from itself it ends up executing a plus plus and then the same thing for i so</p>



<p>i has a return address so when we&#8217;re inside of the i function it&#8217;s got its own call frame and it</p>



<p>knows where it&#8217;s supposed to return which in this case would be that instruction right there or that</p>



<p>statement right there so whatever is the first instruction that implements that higher level</p>



<p>language statement that&#8217;s going to be the return address inside the the i call frame so we look</p>



<p>inside of the call frame for the return address we know where to return it&#8217;s going to return to h at</p>



<p>at this point in time.</p>



<p>And once we&#8217;re done with that,</p>



<p>then we just deallocate the call frame.</p>



<p>In assembly language,</p>



<p>all we&#8217;re really doing is moving the stack pointer</p>



<p>and just sort of ignoring the call frame</p>



<p>from that point forward and we&#8217;ll call it junk data.</p>



<p>So the data is kind of actually still there,</p>



<p>but we&#8217;re just not using it.</p>



<p>And then later, if we wanna start adding more stuff</p>



<p>on top of the stack,</p>



<p>then it will just override that junk data.</p>



<p>But for now, just imagine that it&#8217;s gone.</p>



<p>Anyway, so then we returned from I back to H</p>



<p>from I back to H and so that&#8217;s line 26 right here imagine we perform this</p>



<p>instruction the B++ and then execution falls through down to the very end the</p>



<p>return statement so then same thing happens this version or I guess this</p>



<p>version of H had its own no hang on a second we returned from I yeah yeah this</p>



<p>to h so that means execution will will basically jump from here to right here so it kind of jumps</p>



<p>you know back to that next line like we talked about before when it returns based off the return</p>



<p>address then again you can imagine that that call frame is deallocated or just ignored as junk data</p>



<p>and the top of the stack is kind of like moving and then when that call on h uh finishes so you</p>



<p>executed and then execution falls through down to the return statement then again h has its own</p>



<p>return address in its in its call frame or that instance of h has its own call frame then it knows</p>



<p>where to return and this time it knows to return to whoever called h in the very first place so</p>



<p>it&#8217;s going to be um i guess it&#8217;s going to be like when it returns it&#8217;s going to go back to g</p>



<p>h on g but I guess you could imagine under the hood you know instead of like</p>



<p>another statement you can imagine more stuff is happening right like when we</p>



<p>called h and then we returned then we had an expression that evaluated to</p>



<p>something because h returned to value and then we would send that to the stream</p>



<p>operator so in assembly there are many instructions happening after the call to</p>



<p>h but you could just imagine at this point you know it&#8217;s just gonna finish up</p>



<p>that that existing statement and then move down to finish and return so then</p>



<p>so then again you know we we just returned from h to g so we de-allocated that call frame the top</p>



<p>of the stack is now somewhere around there in that call frame when g finishes and it returns</p>



<p>same thing it&#8217;s got a return address so it knows where to return and then top of the stack kind of</p>



<p>goes down there and g knows to jump to whoever called it because of the return address so it&#8217;s</p>



<p>going to be at this point in time again there&#8217;s more statements that happen after the return</p>



<p>see out in a stream operator uh you know but uh you know basically when f is done then again it&#8217;s</p>



<p>going to use the return address and know where to jump back to which is just going to be this next</p>



<p>line right here so when f was called originally it like ended up calling h and then h uh you know</p>



<p>called itself and h called itself and then it called i and then we returned from i and then we</p>



<p>returned from h and returned from h twice and then eventually we returned to g and then we returned</p>



<p>and all of its calls were finished then execution returned just a line for and</p>



<p>that&#8217;s that&#8217;s basically saying like oh what happens when F is done let&#8217;s look</p>



<p>at its return address hop that off the call stack and then the top of the stack</p>



<p>is basically just sitting inside of the main function so yeah this is how we</p>



<p>implement function calls in the machine you know there&#8217;s there&#8217;s really no so if</p>



<p>you know, from a certain point of view, there&#8217;s no such thing as functions inside the machine.</p>



<p>In assembly language, we certainly see that we have a label and then we have like a return</p>



<p>instruction and we have a call instruction, but you know, there&#8217;s no like function. It&#8217;s all just</p>



<p>ones and zeros all over the place, right? So the idea of labels and functions and things,</p>



<p>that&#8217;s kind of an abstraction just for the human being with feelings to make it a little bit easier</p>



<p>stack we have a stack and we stack things on top of the stack and the things that we stack are</p>



<p>called call frames call frames are kind of flexible because sometimes there&#8217;s more data</p>



<p>sometimes there&#8217;s less data depending on what the function itself is doing but it allows us to have</p>



<p>a function call itself many times it allows us to have many different instances of function calls</p>



<p>exist at the same time even if we&#8217;re only looking at the very top of the call stack in terms of</p>



<p>conflict with each other.</p>



<p>And then, you know, like we have a trail of breadcrumbs when we start returning and</p>



<p>returning and returning, the stack makes it really, really easy.</p>



<p>And so, you know, in the abstract, we can basically say that there are functions in</p>



<p>our machines because this implementation helps make it happen.</p>



<p>One last thing that I wanted to say before I go here is just keep in mind that these</p>



<p>local variables, they&#8217;re on the stack, but dynamically allocated memory is not on the</p>



<p>stack.</p>



<p>stack. Let me just show you what I&#8217;m talking about right here. You probably understand the</p>



<p>heap already at this point. If you&#8217;re watching this video, then you know a little bit about</p>



<p>dynamic memory allocation. So I&#8217;m just going to do, let&#8217;s say we have like an F function void F.</p>



<p>And inside of the F function, we have let&#8217;s say a integer pointer P, and we will allocate P to a new</p>



<p>So just so you know, the variables A and B and also P, they&#8217;re considered local variables</p>



<p>because I just declared them.</p>



<p>I declared a pointer on line four.</p>



<p>I declared two integers on line seven.</p>



<p>Maybe I should put those like at the top, I guess.</p>



<p>Okay, so lines four and five, they declare local variables.</p>



<p>The pointer is a special type of variable, right?</p>



<p>That just points to a memory location.</p>



<p>So the thing that it points to might not be a local variable, but the pointer itself is</p>



<p>a local variable.</p>



<p>The pointer itself is a local variable.</p>



<p>So these three variables on lines four and five, A and B and P, those are on the stack.</p>



<p>Any local variable gets put on the stack.</p>



<p>And then when you allocate a new integer with dynamic memory, maybe you could imagine if</p>



<p>you&#8217;re a C programmer, this would be a call to malloc rather than new.</p>



<p>Then what&#8217;s happening under the hood is that the system goes and tries to find some free</p>



<p>memory in a different area of your process called the heap, not the stack.</p>



<p>heap not the stack it tries to find some free memory in the heap it once it finds some then it</p>



<p>designates that as you know being in use and then it returns to you the memory location of that new</p>



<p>allocation so in this case we&#8217;re allocating a new integer so that&#8217;s going to be 32 bits of data or</p>



<p>four bytes and uh so it just you know it just finds four bytes somewhere in the heap and it finds the</p>



<p>memory location of the very first byte and it returns that to the caller and so p its value is</p>



<p>is now going to be the memory location of the new integer that you allocated.</p>



<p>But P itself is on the stack.</p>



<p>So P is kind of weird, right?</p>



<p>Like P exists on the stack, but it points to something in the heap.</p>



<p>Keep in mind the difference.</p>



<p>And then if we just let this function go to the very end</p>



<p>without cleaning up that memory, we would have a memory leak.</p>



<p>I should probably just do like a dedicated video on pointers</p>



<p>for people who are interested in pointers in higher level languages later.</p>



<p>But keep in mind that when the function ends</p>



<p>and you pop the call frame off the call stack,</p>



<p>then that automatically cleans up all the local variables.</p>



<p>So A and B and P itself will be deallocated</p>



<p>and not become memory leaks.</p>



<p>However, the thing that P pointed to,</p>



<p>that will be a memory leak.</p>



<p>I guess that&#8217;s a topic that&#8217;s too advanced</p>



<p>for this stack video,</p>



<p>but I just want you to know the difference</p>



<p>between something that exists on the stack</p>



<p>and something that exists on the heap.</p>



<p>Okay, so then I think the last thing</p>



<p>that I need to show you here</p>



<p>here is uh let me uh shoot let&#8217;s see shambler okay so here&#8217;s uh uh my favorite book i love to</p>



<p>i love to pump up this book it&#8217;s awesome the author is like a brilliant man very kind man</p>



<p>he offers this book for free it&#8217;s got a copy left license you can literally just download it for</p>



<p>and it&#8217;s legal because this is an open book basically it&#8217;s called x86 64 assembly language</p>



<p>programming with ubuntu by the brilliant dr ed jorgensen phd or professor ed jorgensen</p>



<p>this is the latest version that i&#8217;m working with right now i think it&#8217;s the latest version i don&#8217;t</p>



<p>know he updates this thing all the time it&#8217;s always getting better but um if we look at this</p>



<p>book uh you can get a copy of it on your own or you can just watch this video i&#8217;m going to go to</p>



<p>12.8.3 so 12 section 12 12.8 12.8.3 called the call frame and here you can kind of see a</p>



<p>description of some of the stuff that I talked about in terms of what sort of data you would</p>



<p>expect to see in the call frame but if you just kind of look down a little bit you can see that</p>



<p>we are sort of pushing some preserved registers</p>



<p>sort of like save the stack pointer as the RBP register.</p>



<p>But before we do that, we wanna push the RBP register.</p>



<p>And then notice out here,</p>



<p>it shows that there&#8217;s a return address</p>



<p>and the RIP register.</p>



<p>That&#8217;s actually the instruction pointer register.</p>



<p>The stack pointer is RSP.</p>



<p>And then other arguments that go into it.</p>



<p>So just keep in mind that we have a little layout here</p>



<p>a little layout here and then somewhere else there was something I wanted to show you here</p>



<p>no no no no no no no no no no no no no no no no no no okay I guess I&#8217;m gonna</p>



<p>oh I guess I was just supposed to show you the call frame anyway okay so I guess that&#8217;s pretty</p>



<p>much it I just wanted to show you some extra stuff just so you understand this is the basic</p>



<p>Oh, okay, I found it.</p>



<p>I spent a second scrolling through the book to find the other diagram that I really wanted</p>



<p>to show you.</p>



<p>So same book, X-8664 Assembly with Ubuntu by Ed Jorgensen, Professor Ed Jorgensen, PhD.</p>



<p>And if you look at section 9.3, there&#8217;s another picture here that&#8217;s kind of useful.</p>



<p>And it basically shows, you know, the general idea of how information is laid out inside</p>



<p>information is laid out inside of your process so when you launch a process in</p>



<p>order to execute your program then it&#8217;s kind of laid out like this there&#8217;s like</p>



<p>a little reserved area and then there is the text section for assembly you know</p>



<p>that&#8217;s where you have all your instructions and then there&#8217;s the data</p>



<p>section and then there&#8217;s uninitialized data which is the BSS section if we&#8217;re</p>



<p>talking about Yasm assembly and then notice how in this big blob of an area</p>



<p>and the stack kind of sharing an area inside of available memory it&#8217;s important to understand that</p>



<p>the stack when you actually add things onto the process stack like call like call frame data</p>



<p>you&#8217;re actually decreasing memory location so the stack grows downward in memory so I think I&#8217;m going</p>



<p>to put this in another video but basically right now I just want to show you if we have memory</p>



<p>location let me let&#8217;s pretend that we have like a stack here and there&#8217;s like a couple of frames</p>



<p>there&#8217;s like a couple of frames you know sitting on top of it suppose you had I don&#8217;t know memory</p>



<p>location OX you know 99 or something suppose for the sake of argument we&#8217;re only pushing one byte</p>



<p>at a time so it&#8217;s easier for me to write then that means you know the next thing that shows up</p>



<p>you know a little higher on the stack you would intuitively think that the memory location is</p>



<p>I guess I increased that 9 to a but it actually goes down in memory so I mean</p>



<p>look at this diagram right here the stack goes down in memory so every time</p>



<p>you push to the stack you&#8217;re pushing to a lower and lower and lower memory</p>



<p>location so right here I&#8217;m just gonna write down OX 9 8 and it just keeps</p>



<p>going down and down and down let&#8217;s see 1 2 3 4 5 right and the heap is is</p>



<p>upward in memory so the heap when you call new allocations like if you say new or malloc or you</p>



<p>know some kind of dynamic memory then its memory locations grow up and the stack grows down and if</p>



<p>they ever actually meet then you&#8217;ve ran out of memory just keep that in mind but um you know</p>



<p>in modern systems the heap can kind of grow almost endlessly i&#8217;m sure i&#8217;m sure you&#8217;ve all</p>



<p>been running programs in the past and noticed that the program was consuming gigabytes of data right</p>



<p>gigabytes of data right but it&#8217;s not like you had gigabytes of data allocated to that process</p>



<p>when you first opened it up like if you have a browser with like 100 million tabs open</p>



<p>i know some of you people do that then when you first open your browser it doesn&#8217;t really use</p>



<p>that much memory but then as you start opening more tabs maybe some pages are doing some weird</p>



<p>stuff and you get memory leaks then the heap starts to grow and grow and grow and then you&#8217;ll</p>



<p>notice the process takes way more than did in the beginning so the heap can kind of grow in an</p>



<p>of course that is limited to your physical system ram and eventually in the far off future will be</p>



<p>limited by whatever 64 bits can get us i don&#8217;t think we&#8217;re ever going to reach that in my lifetime</p>



<p>but you know i&#8217;ve been wrong many times before however the stack it grows downward in memory</p>



<p>and it&#8217;s kind of limited you can actually overflow your stack pretty easily so when the stack grows</p>



<p>downward too much then you just you have a stack overflow and the program crashes but when the heap</p>



<p>But when the heap grows, then it can just be resized again and again and again.</p>



<p>Or that little block can kind of expand again and again and again.</p>



<p>If you don&#8217;t believe me, try writing a program with a recursive loop that just goes on forever.</p>



<p>So imagine, actually, I don&#8217;t know, don&#8217;t do this on your boss&#8217;s computer because maybe</p>



<p>it&#8217;ll crash things.</p>



<p>Do it on your own personal computer.</p>



<p>like a function f and uh you know you&#8217;ll probably want to call f inside of main or something so i&#8217;ll</p>



<p>just say like int main and we&#8217;ll just call f right away and then return zero when you&#8217;re inside of f</p>



<p>you just call f right so this is like a horrible program if you ran this very quickly you&#8217;d crash</p>



<p>the program because every single time you call f you&#8217;re adding another call frame on top of the</p>



<p>stack your call stack is going to just be filled with f call frames and then you know it runs out</p>



<p>you know, it runs out of available memory locations to use, and then you&#8217;re just done.</p>



<p>It just crashes the whole program.</p>



<p>So let&#8217;s see.</p>



<p>I think hopefully now you feel like you&#8217;re an expert on the process stack and call frames.</p>



<p>Thank you for watching this video.</p>



<p>And I hope you learned a little bit and had some fun.</p>



<p>I guess I&#8217;ll see you in the next video.</p>



<p>hey everybody thanks for watching this video again from the bottom of my heart I really</p>



<p>appreciate it I do hope you did learn something and have some fun if you could do me a please a</p>



<p>small little favor could you please subscribe and follow this channel or these videos or whatever it</p>



<p>is you do on the current social media website that you&#8217;re looking at right now it would really mean</p>



<p>the world to me and it&#8217;ll help make more videos and grow this community so we&#8217;ll be able to do</p>



<p>better videos or just I&#8217;ll be able to keep making videos in general so please</p>



<p>do do me a kindness and and subscribe you know sometimes I&#8217;m sleeping in the</p>



<p>middle of the night and I just wake up because I know somebody subscribed or</p>



<p>followed it just wakes me up and I get filled with joy that&#8217;s exactly what</p>



<p>happens every single time so you could do it as a nice favor to me or you could</p>



<p>you control me if you want to just wake me up in the middle of the night just</p>



<p>subscribe and then I&#8217;ll just wake up I promise that&#8217;s what will happen also if</p>



<p>if you look at the middle of the screen right now, you should see a QR code, which you can scan</p>



<p>in order to go to the website, which I think is also named somewhere at the bottom of this video.</p>



<p>And it&#8217;ll take you to my main website where you can just kind of like see</p>



<p>all the videos I published and the services and tutorials and things that I offer and all that</p>



<p>good stuff. And if you have a suggestion for clarifications or errata or just future videos</p>



<p>Or if you just want to say, hey, what&#8217;s up? What&#8217;s going on? You know, just send me a comment, whatever.</p>



<p>I also wake up for those in the middle of the night. I get, I wake up in a cold sweat and I&#8217;m like,</p>



<p>it would really, it really mean the world to me. I would really appreciate it. So again,</p>



<p>thank you so much for watching this video and enjoy the cool music as, as I fade into the</p>



<p>darkness, which is coming for us all.</p>



<p>Thank you.</p>
<p>The post <a href="https://www.NeuralLantern.com/the-call-stack-explained-call-frames-return-addresses-local-variables/">The Call Stack Explained: Call Frames, Return Addresses, Local Variables</a> appeared first on <a href="https://www.NeuralLantern.com">NeuralLantern.com</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.NeuralLantern.com/the-call-stack-explained-call-frames-return-addresses-local-variables/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
