<?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>NeuralLantern.com</title>
	<atom:link href="https://www.NeuralLantern.com/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.NeuralLantern.com/</link>
	<description></description>
	<lastBuildDate>Sat, 02 May 2026 23:55:00 +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>NeuralLantern.com</title>
	<link>https://www.NeuralLantern.com/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>How to Search in a Binary Search Tree (BST) &#8211; Step by Step Explanation with Examples</title>
		<link>https://www.NeuralLantern.com/how-to-search-in-a-binary-search-tree-bst-step-by-step-explanation-with-examples/</link>
					<comments>https://www.NeuralLantern.com/how-to-search-in-a-binary-search-tree-bst-step-by-step-explanation-with-examples/#respond</comments>
		
		<dc:creator><![CDATA[mike]]></dc:creator>
		<pubDate>Sat, 02 May 2026 23:54:59 +0000</pubDate>
				<category><![CDATA[Binary Search Trees]]></category>
		<category><![CDATA[Computer Science]]></category>
		<category><![CDATA[Data Structures]]></category>
		<category><![CDATA[Videos]]></category>
		<category><![CDATA[algorithms]]></category>
		<category><![CDATA[binary search tree]]></category>
		<category><![CDATA[binary search tree tutorial]]></category>
		<category><![CDATA[binary tree search]]></category>
		<category><![CDATA[BST explained]]></category>
		<category><![CDATA[BST search]]></category>
		<category><![CDATA[computer science]]></category>
		<category><![CDATA[data structures]]></category>
		<category><![CDATA[data structures and algorithms]]></category>
		<category><![CDATA[log n time]]></category>
		<category><![CDATA[programming tutorial]]></category>
		<category><![CDATA[search in BST]]></category>
		<category><![CDATA[tree traversal]]></category>
		<guid isPermaLink="false">https://www.NeuralLantern.com/?p=336</guid>

					<description><![CDATA[<p>Learn how to search in a Binary Search Tree. We demonstrate searching for existing and non-existing values, explain why BST search is O(log n) on average, and show how poor data ordering can turn your tree into a slow linear structure similar to a linked list. Includes discussion of tree height and balanced vs unbalanced BSTs.</p>
<p>The post <a href="https://www.NeuralLantern.com/how-to-search-in-a-binary-search-tree-bst-step-by-step-explanation-with-examples/">How to Search in a Binary Search Tree (BST) &#8211; Step by Step Explanation with Examples</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="How to Search in a Binary Search Tree (BST) - Step by Step Explanation with Examples" width="1380" height="776" src="https://www.youtube.com/embed/FCz9IbLywI4?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>In this video we walk through how to perform searches in a Binary Search Tree. Starting with a perfectly balanced BST, we cover the search process step by step, including finding existing nodes like 73 and determining that numbers like 44 do not exist in the tree.</p>



<p>We discuss time complexity for searches in BSTs &#8211; O(log n) on average for balanced trees and O(n) in the worst case when the tree becomes skewed like a linked list due to sorted or bad data. Learn why each comparison eliminates half the remaining data and how height affects performance.</p>



<p>Perfect for students learning data structures and algorithms. If you&#8217;re studying BSTs, this clear explanation will help you understand searching, insertion paths, and why self-balancing trees matter.</p>



<p>Thanks for watching!</p>



<p>00:00 Introduction to BST Search<br>00:31 BST Not Self-Balancing<br>00:40 Average Case Log Time<br>00:53 Linear Time in Worst Case<br>01:13 How BST Search Works<br>01:18 Search Path Example<br>02:08 Searching for Existing Node<br>02:35 Searching for Non-Existent Node<br>03:19 Tree Size and Height<br>03:30 Time Complexity O(h)<br>05:40 Bad Data Example<br>08:23 Skewed Tree Like Linked List<br>09:53 Linear Time in Worst Case<br>11:58 Why Log Time<br>12:36 Halving the Search Space<br>14:14 O(log n) Summary<br>14:28 Conclusion</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>hey there let&#8217;s perform searches in a binary search tree</p>



<p>okay if you watch my previous videos you know how to define a binary search tree by now</p>



<p>you know some terminology you know how to construct a tree and so now let&#8217;s just take</p>



<p>a tree that&#8217;s already been constructed and let&#8217;s search through it first thing i want to say is</p>



<p>first thing I want to say is this is a binary search tree this is not a self</p>



<p>balancing tree so in the future this tree could get lopsided and messed up</p>



<p>depending on what kind of data we add we can always expect that binary search</p>



<p>trees have a search time or an insert time or removal time of log log base 2</p>



<p>of n on average assuming that the data is random or in the worst possible case</p>



<p>scenario linear time so these could get slower right now you can see this tree</p>



<p>Right now you can see this tree is perfectly balanced.</p>



<p>So if we say that our tree is currently perfectly balanced and it&#8217;s always going to be perfectly balanced,</p>



<p>which is a fantasy, but let&#8217;s just say that it is,</p>



<p>then we could just say it&#8217;s a log tree.</p>



<p>It&#8217;s going to take log time to search through.</p>



<p>So how do we perform searches?</p>



<p>Again, if you saw my previous videos, you should know that it&#8217;s really easy to find out where a node belongs.</p>



<p>Like for example, if I was going to add the number 14,</p>



<p>I would first look at the root node and I&#8217;d say, okay, the root node is occupied.</p>



<p>and I&#8217;d say, okay, the root node is occupied.</p>



<p>And so where would 14 belong?</p>



<p>Well, it would belong on the left of 37</p>



<p>because it&#8217;s less than 37.</p>



<p>It would belong on the left of 16</p>



<p>because it&#8217;s less than 16.</p>



<p>It would belong on the right of 9</p>



<p>because it&#8217;s greater than 9.</p>



<p>It would belong on the right of 11</p>



<p>because it&#8217;s greater than 11.</p>



<p>And so the 14 node that we just wanted to add,</p>



<p>is that what I just said, 14?</p>



<p>Would end up going there.</p>



<p>So when you&#8217;re searching through a binary search tree,</p>



<p>you really just have to go find</p>



<p>have to go find where the node would belong as if you&#8217;re performing insert or you could think of it</p>



<p>the other way around like insert is really thinking of a search first but basically we&#8217;re just going</p>



<p>to start by saying you know pick a number and we&#8217;ll see if it exists so uh let&#8217;s pick the number</p>



<p>67 or actually let&#8217;s pick the number 73 just to see if it exists in the tree so 73 i&#8217;m going to</p>



<p>do like you know seven three with like a question mark we check the root node 73 belongs on the</p>



<p>side we look at the 59 it belongs on the right side and we found our 73 so 73 exists in the tree</p>



<p>and it only took us three examinations only three examinations let&#8217;s look for a number that doesn&#8217;t</p>



<p>actually exist in the tree so maybe oh my thing is freezing what&#8217;s going on okay so maybe uh let&#8217;s</p>



<p>The 37 here, 44 belongs on the right of 37.</p>



<p>We look at the 59, it belongs on the left.</p>



<p>We look at the 48, it belongs on the left.</p>



<p>We look at the 43, it belongs on the right.</p>



<p>The 44 would have been there, but it&#8217;s not because that right child pointer off the 43</p>



<p>is null.</p>



<p>So we only had to really examine one, two, three, four nodes in order to determine that</p>



<p>our number 44 didn&#8217;t exist in the tree.</p>



<p>Notice how we&#8217;re not doing a lot of examinations compared to the total size of the tree.</p>



<p>compared to the total size of the tree.</p>



<p>So let me just write this up here.</p>



<p>How many nodes do we have?</p>



<p>1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15.</p>



<p>We got 15 nodes up here.</p>



<p>Someone say N is equal to 15.</p>



<p>And what&#8217;s the height of the tree?</p>



<p>Remember, the height of the tree is the number of nodes you must touch</p>



<p>as you make your way down towards the deepest node.</p>



<p>Or if you&#8217;ve already found the deepest node,</p>



<p>you take its depth and you just add 1, assuming that depths start at 0.</p>



<p>start at zero. So I&#8217;ll just say that the depth of the root node is zero and the 59 and the 16 are</p>



<p>one. And then that third row is two. And that last row is three. And then I just add one to the</p>



<p>deepest node that I can find, which is just going to have a depth of three. All of these on the</p>



<p>bottom row have a depth of three. So the height of this tree is actually four, because we would</p>



<p>so like 37 59 48 43 that&#8217;s four touches um or the or the depth of the deepest node plus one okay um</p>



<p>all trees all binary search trees whether they&#8217;re actually avl trees or regular binary search trees</p>



<p>or whether they&#8217;re balanced or imbalanced they&#8217;re always going to have a time complexity to search</p>



<p>through of o of h meaning in the worst case scenario you&#8217;ll have to touch h number of nodes</p>



<p>order to find a node by search query or to add a new node or even to delete a new node.</p>



<p>So how does that compare to this tree in reality?</p>



<p>Let me just open up a little calculator real fast.</p>



<p>I just want to show you.</p>



<p>So if we do log base two, because we have a binary search tree, not a trinary search</p>



<p>nodes 15 it tells us that it should take us no more than four examinations to find the node that</p>



<p>we&#8217;re looking for if the tree was perfectly balanced and if you if you if you notice that&#8217;s</p>



<p>exactly what the height is telling us here if the tree was more imbalanced the height would get</p>



<p>really bad compared to the number of nodes and it would no longer be considered a log tree it would</p>



<p>just be you know somewhere in between linear time and log time um let&#8217;s see what else can i show you</p>



<p>let&#8217;s do well we did that computation let&#8217;s do a search through the tree that</p>



<p>through a tree that&#8217;s actually really really bad let&#8217;s pretend that we have a</p>



<p>bunch of nodes here and you know I&#8217;ve got an old story that I love to tell</p>



<p>about this when like when my grandma was alive she was kind of bitter towards the</p>



<p>end you know I loved her but she used to call the cops on her neighbors for</p>



<p>gossip and at some point in time she got into a feud with her neighbor who was</p>



<p>with her neighbor who was also a bitter old lady.</p>



<p>You know, towards the end, it kind of happens sometimes.</p>



<p>It sucks, but it happened.</p>



<p>And I guess the neighbor didn&#8217;t like my grandma&#8217;s tree</p>



<p>that she had in her backyard.</p>



<p>So they fought over it, they argued over it,</p>



<p>and then one day my grandma said</p>



<p>she was looking out the backyard window</p>



<p>to yell at ducks who were jumping into her pool</p>



<p>because she just hated when the ducks went into her pool.</p>



<p>She was waiting for them to show up.</p>



<p>She wasn&#8217;t even looking at ducks.</p>



<p>even looking at ducks she was just waiting and she saw the other little old lady reach her arm over</p>



<p>the fence and spray my grandma&#8217;s tree with some sort of poison then my grandma&#8217;s tree died and uh</p>



<p>i guess you know my grandma never never got over it i always thought it was like a funny story it</p>



<p>was a little sad but um she poisoned my grandma&#8217;s tree so we can kind of do the same thing with the</p>



<p>binary search tree we can give bad data let me show you what happens here if i use this data set</p>



<p>search tree. So we&#8217;re going to add this node right here. It&#8217;s going to be the 14.</p>



<p>And then when we add the 19 node, I&#8217;m just going to duplicate it. The 19 is supposed to be on the</p>



<p>right side of 14 because it&#8217;s greater than 14. So I&#8217;m just going to like, you know, draw a 19 right</p>



<p>here and a little connecting line. Okay. Then when we add the 24, I&#8217;m going to duplicate this node</p>



<p>the 24 go belongs on the right side of the 14 and also belongs on the right side of the 19</p>



<p>because it&#8217;s greater than 19 so again we have a node that shows up on the right side let me</p>



<p>remember to actually fix the 19 so it looks like 24. then when we add the uh 29 it&#8217;s gonna end up</p>



<p>being the same thing right the 29 is gonna end up showing on the right the right the right it&#8217;s</p>



<p>If we have bad data, we can end up with a tree that kind of looks like a straight line.</p>



<p>And if you are watching this video from the future,</p>



<p>after I&#8217;ve posted all of my data structures videos that I have,</p>



<p>you might notice that this looks like another data structure.</p>



<p>If you&#8217;re watching this right away, I haven&#8217;t posted those videos just yet.</p>



<p>But just tilt your head to the right a little bit</p>



<p>and tell me if that looks like something that you recognize.</p>



<p>It looks like a linked list.</p>



<p>looks like a linked list just like a straight line or even i guess you could kind of say maybe an array</p>



<p>um just like a straight line of data there&#8217;s no uh you know there&#8217;s no splitting of the data</p>



<p>so we have the 29 and then the 35 and then uh maybe i&#8217;ll just do one more node only and i&#8217;ll</p>



<p>get rid of that 54 because that&#8217;s too many nodes so what happens is if you have you know bad data</p>



<p>tree doesn&#8217;t actually search or add insert or remove or anything like that in log time anymore</p>



<p>instead it operates in linear time just like a linked list would meaning we would have to scan</p>



<p>through every single node in the entire tree to be sure that some value did not exist well</p>



<p>not exactly but in the worst case scenario we&#8217;d have to scan through the whole entire tree</p>



<p>for example let&#8217;s just look at the the number of nodes real fast we&#8217;ll say the</p>



<p>number of nodes here is one two three four oh I put two 24s in there oh dearie</p>



<p>let me get that says 14 19 24 29 and then I guess I got to make that a 35 oh</p>



<p>okay and then there&#8217;s a 49 I guess I did add every single node okay so we added</p>



<p>so there&#8217;s like seven uh items in the tree so i&#8217;m going to say n is equal to seven the height also</p>



<p>is seven though so the height is seven notice how if we say that it&#8217;s always true that a binary</p>



<p>search tree has a time complexity in the worst case of o of h meaning you&#8217;d have to go down</p>



<p>in the worst case the number of levels that is equal to the whole entire height of the entire</p>



<p>tree that&#8217;s also the number of nodes in the tree which means uh if i wanted to add let&#8217;s say the</p>



<p>I don&#8217;t know, if you wanted to add the number 60,</p>



<p>60 would end up going down here.</p>



<p>And in order to find where 60 would go,</p>



<p>we&#8217;d have to look at the 14, that&#8217;s 1, 19, 24.</p>



<p>We&#8217;d have to look at all these nodes.</p>



<p>That is seven nodes.</p>



<p>We&#8217;d have to examine seven nodes</p>



<p>just to figure out where the 60 would go.</p>



<p>Or if we were searching for 60,</p>



<p>then we&#8217;d have to go down that far</p>



<p>to find out that 60 didn&#8217;t exist.</p>



<p>So this is nowhere near a log tree.</p>



<p>This is a linear tree, a linear time tree.</p>



<p>year time tree and just to drive the point further if we did log base two of the number of nodes in</p>



<p>this case seven notice how it&#8217;s telling us that we should if it was a log tree if it was perfectly</p>



<p>balanced or a self-balancing tree we should have to touch no more than three nodes in order to find</p>



<p>what we&#8217;re looking for to search through the tree or to insert a new node or even to delete a node</p>



<p>but that&#8217;s not the case here because the tree is really really slow compared to a perfectly balanced</p>



<p>slow compared to a perfectly balanced tree.</p>



<p>So we call this a linear tree.</p>



<p>It sucks.</p>



<p>You want to try to avoid that.</p>



<p>You don&#8217;t want your data to be poisoned.</p>



<p>On average, your trees won&#8217;t really look like this if you just have totally</p>



<p>random data, but if you think there&#8217;s a chance that your data might be skewed</p>



<p>in some way, it&#8217;s probably a good idea to upgrade to a self-balancing tree,</p>



<p>which we&#8217;re going to talk about later.</p>



<p>Okay.</p>



<p>What else can I tell you real fast?</p>



<p>So we talked about searching.</p>



<p>why are these trees considered log time? I mean, they&#8217;re really, really fast, but why?</p>



<p>Let&#8217;s just duplicate this slide here and I&#8217;ll move it down one level. I just want to kind of</p>



<p>show you why this ends up being a log time tree. Okay. So why, why is this a log time tree?</p>



<p>Suppose for the sake of argument, we&#8217;re searching for the number 52. So if we&#8217;re searching for the</p>



<p>node the root node and decide where would the 52 belong let me actually write that down so i don&#8217;t</p>



<p>forget 52 question mark where would the 52 belong it would belong on the right side of the 37 because</p>



<p>it&#8217;s greater than 37 right which means it&#8217;s actually impossible that the 52 would be in any</p>



<p>of these other nodes on the left side there&#8217;s no need to check them we&#8217;re not going to scan the</p>



<p>whole tree we&#8217;re just going to go left or right and ignore the whole other side of the tree</p>



<p>we just did is with one examination we eliminated half of the remaining data set or i guess of the</p>



<p>initial data set if we&#8217;re looking at the root node so then the next thing that we do is we just kind</p>



<p>of say okay it belongs on the right side so we look at the 59 node and we do the same thing we</p>



<p>say where would 52 be would it be on the left side or the right side of the 59 node it would be on</p>



<p>the left side which means we&#8217;re going to eliminate from consideration the entire right subtree of the</p>



<p>So again, we&#8217;re eliminating half of the remaining data set.</p>



<p>Then we kind of go down here and we look at the 48.</p>



<p>We do the same thing.</p>



<p>52 belongs on the right side.</p>



<p>So we eliminate half of the remaining data set, you know, and then eventually we do find</p>



<p>the 52 and then that&#8217;s it.</p>



<p>So if we&#8217;re eliminating half of the data set with every single, just one examination at</p>



<p>a time, that&#8217;s why it ends up being log base two.</p>



<p>getting twice as close to your destination at every step you took if you&#8217;re like in a car like</p>



<p>running maybe you&#8217;re on the moon or something then it would be a log time you know uh progress</p>



<p>towards your goal or eliminating half of the data set at every single step sometimes people see</p>



<p>for loops on exams and stuff when we talk about this um like for i equals zero uh i is less than</p>



<p>it would be like i times equals two.</p>



<p>So i&#8217;s jumping twice as far each time.</p>



<p>That&#8217;s the sort of thing that makes it log.</p>



<p>So I&#8217;m just going to write down log here real fast</p>



<p>to drive the point home a little bit more.</p>



<p>O of log base two of n.</p>



<p>Yeah, so I guess that&#8217;s all I wanted to tell you in this video.</p>



<p>Hopefully you feel like an expert in searching now.</p>



<p>If you want to see more searching videos,</p>



<p>leave a comment or something like that.</p>



<p>leave a comment or something like that. But yeah, thank you for watching. And I hope you</p>



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



<p>I&#8217;m going to go find a blueberry muffin.</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,</p>



<p>subscribe and follow this channel or these videos or whatever it is you do on the current social</p>



<p>media website that you&#8217;re looking at right now it would really mean the world to me and it&#8217;ll</p>



<p>help make more videos and grow this community so we&#8217;ll be able to do more videos longer videos</p>



<p>better videos or just i&#8217;ll be able to keep making videos in general so please do do me a kindness</p>



<p>and uh and subscribe you know sometimes i&#8217;m sleeping in the middle of the night and i just</p>



<p>It just wakes me up and I get filled with joy. That&#8217;s exactly what happens every single time</p>



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



<p>And I 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</p>



<p>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 it&#8217;ll take you to my main website where you can just kind of like</p>



<p>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>Clarifications or errata or just future videos that you want to see please leave a comment or if you just want to say hey</p>



<p>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. I&#8217;m like this</p>



<p>It would really it really mean the world to me. I would really appreciate it. So</p>



<p>So again, thank you so much for watching this video and 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/how-to-search-in-a-binary-search-tree-bst-step-by-step-explanation-with-examples/">How to Search in a Binary Search Tree (BST) &#8211; Step by Step Explanation with Examples</a> appeared first on <a href="https://www.NeuralLantern.com">NeuralLantern.com</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.NeuralLantern.com/how-to-search-in-a-binary-search-tree-bst-step-by-step-explanation-with-examples/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Binary Search Tree Insertion Tutorial: Building a BST from Scratch Step by Step</title>
		<link>https://www.NeuralLantern.com/binary-search-tree-insertion-tutorial-building-a-bst-from-scratch-step-by-step/</link>
					<comments>https://www.NeuralLantern.com/binary-search-tree-insertion-tutorial-building-a-bst-from-scratch-step-by-step/#respond</comments>
		
		<dc:creator><![CDATA[mike]]></dc:creator>
		<pubDate>Mon, 27 Apr 2026 06:42:54 +0000</pubDate>
				<category><![CDATA[Binary Search Trees]]></category>
		<category><![CDATA[Computer Science]]></category>
		<category><![CDATA[Data Structures]]></category>
		<category><![CDATA[Videos]]></category>
		<category><![CDATA[algorithms]]></category>
		<category><![CDATA[binary search tree]]></category>
		<category><![CDATA[binary search tree tutorial]]></category>
		<category><![CDATA[binary tree]]></category>
		<category><![CDATA[BST insertion]]></category>
		<category><![CDATA[BST insertion step by step]]></category>
		<category><![CDATA[build BST from scratch]]></category>
		<category><![CDATA[coding interview prep]]></category>
		<category><![CDATA[computer science]]></category>
		<category><![CDATA[data structures]]></category>
		<category><![CDATA[inorder traversal]]></category>
		<category><![CDATA[self balancing trees]]></category>
		<category><![CDATA[time complexity BST]]></category>
		<category><![CDATA[tree height]]></category>
		<guid isPermaLink="false">https://www.NeuralLantern.com/?p=333</guid>

					<description><![CDATA[<p>Learn how to build a binary search tree (BST) by inserting numbers one by one. Follow the insertion process for the sequence 88, 21, 3, 6, 72, 34, 11, 1, 90, 65, 55, 17, 23, 9. See left/right decisions, pointer updates, inorder verification for sorted order, tree height calculation (6), and discussion of O(h) time complexity versus log n for balanced trees. Clear explanations without skipping steps.</p>
<p>The post <a href="https://www.NeuralLantern.com/binary-search-tree-insertion-tutorial-building-a-bst-from-scratch-step-by-step/">Binary Search Tree Insertion Tutorial: Building a BST from Scratch Step by Step</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="Binary Search Tree Insertion Tutorial: Building a BST from Scratch Step by Step" width="1380" height="776" src="https://www.youtube.com/embed/-vtcFOEkrEk?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>In this hands-on tutorial, we build a binary search tree (BST) from scratch by inserting numbers one by one: 88, 21, 3, 6, 72, 34, 11, 1, 90, 65, 55, 17, 23, and 9. Watch as each node finds its correct position following BST rules, with clear explanations of left and right child decisions, parent pointers, and why the tree ends up looking a bit wonky but is still valid.</p>



<p>We cover the full insertion process, verify the tree is a proper BST by checking inorder traversal (sorted order), confirm it&#8217;s a connected acyclic rooted binary tree, and discuss tree height, time complexity (O(h) where h=6 for this tree), and why self-balancing trees matter for better performance.</p>



<p>Great practice for computer science students, programmers learning data structures, or anyone prepping for coding interviews. No fancy animations &#8211; just real step-by-step drawing and reasoning.</p>



<p>If you want more BST practice videos or specific insertion sequences, drop a comment below!</p>



<p>00:00 Introduction to BST Practice<br>00:35 Insertion Sequence Overview<br>00:59 Rules for Building BST<br>01:30 Insert 88 as Root<br>02:20 Continue building the tree from input data<br>13:04 Finish building the tree<br>14:56 Clean Up Diagram<br>15:24 Verify Inorder Traversal<br>16:48 Confirm Tree Properties<br>19:23 Tree Height and Time Complexity<br>22:01 Search Example for 55<br>23:09 Log Time vs Actual Height<br>24:21 Conclusion and Future Topics</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! Let&#8217;s practice constructing a binary search tree from scratch.</p>



<p>In my previous video, we actually did this already, and in other previous videos we defined</p>



<p>what is a binary search tree and some terminology and stuff like that, but I thought it would</p>



<p>be nice to have two practice videos up. I&#8217;m willing to put more up if people want to make</p>



<p>comments about more trees that they want to see inserted. For now, I&#8217;m just going to say</p>



<p>we will add the following sequence of numbers. So this is a little diagram from the last time we</p>



<p>did this. Let me just, oh, I&#8217;m supposed to make that full screen so it can be bigger. Whoops.</p>



<p>Let me go ahead and add a new slide here. And then I&#8217;m going to do,</p>



<p>Okay, so we&#8217;re going to try to add the following sequence of numbers.</p>



<p>Like I said in my other video, don&#8217;t try to make the tree prettier.</p>



<p>Don&#8217;t get mad if a number looks like it&#8217;s too far to the left or something.</p>



<p>Just follow the normal rules of letting a node drop wherever it is supposed to drop</p>



<p>according to the rules of building a binary search tree.</p>



<p>So the first thing that I&#8217;m going to do is maybe draw a little arrow here.</p>



<p>I&#8217;m going to do like a pink arrow to indicate, you know,</p>



<p>You know, well, maybe I&#8217;ll do like a red one to indicate which number we&#8217;re actually trying to add at the moment.</p>



<p>So it&#8217;s going to be the 88 first.</p>



<p>Let&#8217;s see, we&#8217;ll put that right there.</p>



<p>And then now let&#8217;s say that the 88 is going to drop into our tree somewhere.</p>



<p>First question we have to ask ourselves is do we have a root node?</p>



<p>We don&#8217;t have a root node, which means the number we&#8217;re trying to add should be the root node.</p>



<p>And that&#8217;s it.</p>



<p>So I&#8217;m going to draw like a little circle here, a little node.</p>



<p>and I&#8217;m going to stick the number 88 inside of it we&#8217;re using integer t types</p>



<p>if you recall the last videos these nodes are supposed to be able to hold</p>



<p>almost any data type that you want they don&#8217;t have to be numbers but I&#8217;m just</p>



<p>going to use numbers for the sake of this example because it&#8217;s easier so I&#8217;m</p>



<p>going to say this is our root node it&#8217;s at the very top of the tree and we&#8217;re</p>



<p>done adding 88 and that&#8217;s it next step we&#8217;re going to add the 21 so I&#8217;m just</p>



<p>forward a little bit here and the 21 same question do we have a root node we currently do so that</p>



<p>means we can&#8217;t actually put the 21 there so we have to make a decision does the 21 belong on the</p>



<p>left or the right of the 88 the 21 is less than 88 which means it belongs on the left side so i&#8217;m</p>



<p>just going to duplicate this and try to make a pretty diagram by sort of splitting the difference</p>



<p>in terms of the empty space on the left between the edge of the screen and the existing root node</p>



<p>and make sure that it goes down, you know, a full rank, you know, at least the full height of a node,</p>



<p>maybe a little bit more, but you want to keep all same depth nodes on the same Y coordinate.</p>



<p>It&#8217;s just easier to debug your diagrams that way.</p>



<p>This one&#8217;s actually probably going to get messy. I might have to readjust this a few times.</p>



<p>I think I recalled these were, these might&#8217;ve been bad numbers. I can&#8217;t remember.</p>



<p>Okay. So the 21 is going to be the left child of the 88 because it belongs on the left side.</p>



<p>So I&#8217;m just going to do my little connecting line here.</p>



<p>connecting line here now 21 has a parent of 88 and 88 has a left child of 21 and we&#8217;re done adding</p>



<p>the 21 so i&#8217;m going to move the arrow to the three we&#8217;re now trying to add the three node</p>



<p>again we look at the root node we say okay that&#8217;s occupied so we can&#8217;t use that does three belong on</p>



<p>the left or the right of the 88 well it&#8217;s less than 88 so it belongs on the left the left child</p>



<p>again, does the three belong on the left or the right of the 21? It belongs on the left</p>



<p>because it&#8217;s less than 21 and the left child of 21 is currently doesn&#8217;t exist. It&#8217;s null.</p>



<p>So that means the three is going to be the new left child of the 21 node. Maybe just put it</p>



<p>like right there, put the three there. And then I&#8217;m going to do the connecting line in your code.</p>



<p>You can imagine that the left child pointer of the 21 node has now been set. So it&#8217;s pointing</p>



<p>been set so it&#8217;s pointing to the brand new node we just made for the three and then inside of the</p>



<p>three node its parent pointer is pointing to the 21 node okay so we&#8217;re done adding the three we</p>



<p>move on to the six same thing that we did before we look at the root node uh the six belongs on</p>



<p>the left so i&#8217;m gonna say go to the left the 21 node is it&#8217;s there it&#8217;s occupied so we have to</p>



<p>left of 21 because it&#8217;s less than 21 we go down to the left then we look at the three that&#8217;s also</p>



<p>occupied does the six belong on the left or right of three it belongs on the right side of three</p>



<p>because it&#8217;s greater than three so that means the sixth node is going to be the right child</p>



<p>of three again try to split the difference these diagrams get ugly fast i really</p>



<p>i don&#8217;t i don&#8217;t want to rearrange the topology but i&#8217;m probably gonna have to</p>



<p>I think I&#8217;m like crashing out here.</p>



<p>Oh, no.</p>



<p>Okay.</p>



<p>Nope.</p>



<p>Okay.</p>



<p>Let me try one more time.</p>



<p>These lines are killing me.</p>



<p>Okay.</p>



<p>I&#8217;m just going to update the three to a six and leave a little gap on that line.</p>



<p>It&#8217;s fine.</p>



<p>Now we&#8217;re ready to try to add the 72 node.</p>



<p>We have a little relief here because this is not like a smaller and smaller number that we&#8217;re dealing with.</p>



<p>So, well, we just look at the root node.</p>



<p>72 belongs on the left side so I&#8217;m going to hop to the left here we then look at</p>



<p>the 21 72 belongs on the right side because it&#8217;s greater than 21 so that</p>



<p>means 72 is going to be the right child of 21 so I&#8217;m just going to select this</p>



<p>make a duplicate try to keep the three and the six at the same you know y</p>



<p>coordinate because they&#8217;re on the same descendancy rank they&#8217;re at the same</p>



<p>you know generation whatever you want to call it so I&#8217;m going to put the 72 on</p>



<p>And then I&#8217;m going to do our connecting line, indicating that both nodes now have pointers that refer to each other.</p>



<p>Okay, we&#8217;re done adding the 72.</p>



<p>I&#8217;m going to move this to the right a little bit.</p>



<p>And then, oops, get rid of that little dot.</p>



<p>Now we&#8217;re ready to add the 34.</p>



<p>So the 34, we look at the 88 node occupied.</p>



<p>We go left.</p>



<p>We look at the 21 node.</p>



<p>34 belongs on the right side of 21.</p>



<p>of 21 so then we look at the 72 34 belongs on the left side of 72 so that means 34 is going to be</p>



<p>the left child of 72 so I&#8217;m going to select one of these nodes and duplicate it and say that</p>



<p>you have a left child of 72 now and it&#8217;s going to be the 34 node do my pointer connection real fast</p>



<p>I haven&#8217;t been duplicating the pages. Okay. So, uh, I guess that&#8217;s just for me.</p>



<p>We&#8217;ll move on to adding the 11 node.</p>



<p>So we add the 11 node by looking at the root node first.</p>



<p>11 belongs on the left side. We then look at the 21,</p>



<p>11 belongs on the left of 21. Go to the left here. We look at the three,</p>



<p>11 belongs on the right of the three. We go to the right,</p>



<p>11 belongs on the right of the six node.</p>



<p>This is definitely a gross graph at this point.</p>



<p>at this point. So I&#8217;m going to duplicate this node here and try to split the difference between</p>



<p>the six and the 21 in terms of like the free space. So I&#8217;m just going to drag this down a</p>



<p>little bit there and say that 11 goes as the new right child of the six node. So</p>



<p>just do that. This is a gross diagram. I might want to redraw this later.</p>



<p>Now we&#8217;re ready to try to add the one node.</p>



<p>the one again we look at the root node of the tree and it&#8217;s occupied so we can&#8217;t put it there</p>



<p>we go to the left we go to the left again and the one belongs on the left of the three nodes so we</p>



<p>have a little bit more room there so that&#8217;s good i&#8217;m gonna copy this node and make it the new left</p>



<p>child of the three i&#8217;m gonna like say that our one is right there and then i&#8217;m gonna do the connecting line</p>



<p>okay all right we&#8217;re done adding the one now I&#8217;m gonna add the 90 node oh this is</p>



<p>gonna be nice and easy because the 90 definitely belongs on the right side of</p>



<p>the 80 node whoops what happened okay sorry for making that noise I&#8217;m gonna</p>



<p>duplicate this node here and make it the right child of the 88 node so let&#8217;s see</p>



<p>kind of even there. So we&#8217;re going to put 90 right there. And then I&#8217;m going to do a connecting line</p>



<p>in blue. Now we&#8217;re done adding the 90.</p>



<p>So then we&#8217;re going to add the 65. 65 belongs on the left of the 88 node.</p>



<p>So we&#8217;re kind of in left territory now. It&#8217;s kind of sucky again.</p>



<p>And bounce down to the left. 66 belongs on the right side of the 21 node. So we look at the 72.</p>



<p>look at the 72 it belongs on the left of the 72 we go there it belongs on the right of the 34 so we</p>



<p>go right here as the new right child and the diagram kind of sucks at this point it&#8217;s a little</p>



<p>cramped but i&#8217;m going to try my best to split the difference between the 34 and the 32 visually to</p>



<p>make a nice easy to debug graph so maybe like somewhere around there put 66 then i&#8217;m going to</p>



<p>So the 66 is now the right child of 34.</p>



<p>Okay, next number that we&#8217;re going to add is my computer&#8217;s crashing.</p>



<p>Hello?</p>



<p>What happened?</p>



<p>There we go.</p>



<p>Okay.</p>



<p>So we&#8217;re ready to add the 55.</p>



<p>Oh, God.</p>



<p>Okay, let me see what&#8217;s going to happen here.</p>



<p>This is awful.</p>



<p>is awful i usually say you have to space these out enough so that none of these overlap but</p>



<p>they&#8217;re about to overlap okay we&#8217;ll do go to the left because 55 is less than 88 go to the right</p>



<p>because 55 is greater than 21 go to the left because it&#8217;s less than 72 go to the right because</p>



<p>it&#8217;s greater than 34 then go to the left again because it&#8217;s less than 66 i hate this i hate this</p>



<p>between 34 and 66, just to try to make it easier to debug.</p>



<p>This is almost, I&#8217;m almost stretched</p>



<p>to my mental limit right now.</p>



<p>So I&#8217;m gonna make the left child pointer here</p>



<p>and the new node we just added was 55.</p>



<p>Whoops, yeah, 55.</p>



<p>Okay, my God.</p>



<p>Let me just duplicate this one more time</p>



<p>and get rid of those pink arrows.</p>



<p>arrows let&#8217;s add the 17 next 17 right here um so once again we look at the root node it&#8217;s occupied</p>



<p>go to the left because it&#8217;s less than we look at the 21 we go left because it&#8217;s less than 21</p>



<p>we go to the right because it&#8217;s greater than three we look at the six we go to the right</p>



<p>because it&#8217;s greater than six we go to the right because it&#8217;s greater than 11.</p>



<p>So a super cramped diagram.</p>



<p>I&#8217;m in hell.</p>



<p>I&#8217;m just going to move this slightly to the right because this is just awful.</p>



<p>And I&#8217;m going to try to make sure again that same depth nodes are at the same Y coordinate.</p>



<p>Let me fix that number real fast before I forget.</p>



<p>And put it as a 17.</p>



<p>And then do my connecting line.</p>



<p>Like that.</p>



<p>Okay.</p>



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



<p>The, uh, whoops, the 23, where is this going to go?</p>



<p>Is it 23?</p>



<p>Um, Hmm.</p>



<p>Hmm.</p>



<p>Oh, well, it&#8217;s going to be like left child of the 34 probably.</p>



<p>Okay.</p>



<p>Let me just, uh, look at the root node.</p>



<p>Okay.</p>



<p>23 belongs on the left of that.</p>



<p>Look at the 21, 23 belongs on the right of that.</p>



<p>Cause it&#8217;s greater than 21.</p>



<p>Look at the 72.</p>



<p>It belongs on the left.</p>



<p>it belongs on the left look at the 34 belongs on the left and then that&#8217;s going to be the new left</p>



<p>child okay so I&#8217;m going to duplicate one of these nodes and it&#8217;s going to be the new left child of</p>



<p>the 34 node that is awful but I guess we barely have enough room let me change that to a 23 and</p>



<p>then I&#8217;ll add my connecting line okay and now we&#8217;re ready to add the final value in our tree</p>



<p>to be? It&#8217;s going to be like left child of 11 maybe. So we look at the root node. Whoops,</p>



<p>forgot to move that arrow. We look at the root node. We can&#8217;t put it there. Nine belongs on the</p>



<p>left side of the root. So we go like that. Nine belongs on the left side of the 21. Oh, did I just</p>



<p>guess something wrong? No, no, no. I think I&#8217;m okay. Go to the left side of that. We look at</p>



<p>right side of three we look at the six nine belongs on the right side of the six we look at</p>



<p>the eleven nine belongs on the left side of eleven because it&#8217;s less than eleven so I&#8217;m just going to</p>



<p>duplicate this and it&#8217;s going to end up being the left side whoops nope that&#8217;s wrong hang on a second</p>



<p>let me oh come on man let me get this down it&#8217;s going to be the left child of eleven not the left</p>



<p>there and the diagram is a little messed up maybe I should try to rearrange this in a second</p>



<p>it the topology is correct but it&#8217;s harder to debug because you can see that one of the right</p>



<p>descendants of the six node is actually physically on its left side and that just makes it extra hard</p>



<p>to debug but for now we&#8217;ll say that we&#8217;re done adding stuff into the tree so maybe I can kind</p>



<p>I&#8217;m not going to change the actual topology.</p>



<p>I&#8217;m just going to like, you know, redraw this in a slightly nicer way.</p>



<p>So I can probably take this whole subtree and move it over to the left a little bit to make it a little cleaner.</p>



<p>Which will give me some room to move this subtree a little bit over to the right.</p>



<p>Am I still wearing my microphone? Yeah.</p>



<p>And then it&#8217;s a little bit cleaner.</p>



<p>Then I just have to redraw this line.</p>



<p>Whoops.</p>



<p>oops yeah just have to redraw this line and now i think we&#8217;re following the rules that i talked</p>



<p>about before where a good diagram has all the left descendants physically on the left side of</p>



<p>a given node so let&#8217;s see the six has everything on the right and everything i think we&#8217;re okay</p>



<p>i&#8217;m going to duplicate this real fast and now let&#8217;s verify that our tree is actually correct</p>



<p>So I said this in the last video when we built a tree, but you basically want to take like the input data set here and sort it.</p>



<p>And whatever sorted list you end up with, that&#8217;s what you should see when you scan your eyes from left to right.</p>



<p>Again, this is another good argument for drawing the diagram in a really good way.</p>



<p>All left descendants are physically on the left. All right descendants are physically on the right.</p>



<p>So another way that I could do this, besides I don&#8217;t really want to sort this list right now,</p>



<p>Just scan from left to right and make sure that I see increasing numbers</p>



<p>Or if you&#8217;ve decided to make a tree that supports duplicates, make sure that you see non-decreasing numbers</p>



<p>But I&#8217;m looking for an ascending list</p>



<p>Okay, so I&#8217;m gonna just like do little hashes here</p>



<p>One is the lowest number. That&#8217;s good. Then if I go to the right just a little bit</p>



<p>I see a three go to the right a little bit. I see a six that&#8217;s still increasing then I see a nine and then I see an 11</p>



<p>still been increasing go to the right a little bit more it&#8217;s a 21 go to the right a little bit more</p>



<p>it&#8217;s a 23 then it&#8217;s a 34 then it&#8217;s a 55 then it&#8217;s a 66 then it&#8217;s a 72 so far everything&#8217;s been</p>



<p>increasing and then from 72 to 88 and from 88 to 90. so the ordering of the numbers are correct</p>



<p>let me just make sure i actually have a binary search tree by the definition of a bst first it</p>



<p>crazy lines going off in some direction or like different symbols this is a</p>



<p>connected graph meaning I could probably find well I definitely could find a path</p>



<p>from any node to any other node if I just you know trace the edges so can the</p>



<p>9 find its way to the 90 yeah if we just kind of like go you know up this way and</p>



<p>follow the edges up to the 88 and then back down to the 90 so every single node</p>



<p>could do that it could find every single other node on the graph which means this</p>



<p>every single other node on the graph which means this is a connected graph so that&#8217;s good the next</p>



<p>thing is now that we have a connected graph do we have a tree so check for cycles uh can any start</p>



<p>node find its way back to itself in a path that does not repeat edges i don&#8217;t see that anywhere</p>



<p>in the tree so i think we have a valid tree let me get rid of that real fast so for example uh</p>



<p>Could we find a path that goes away from the 9 node and then comes back without repeating any edges?</p>



<p>I cannot because as soon as we go out to the 11,</p>



<p>later on we would have to use that same edge to come back down to the 9 and that would be repeating an edge.</p>



<p>So we just kind of check that for every single node.</p>



<p>We can&#8217;t find a cycle anywhere in this entire graph.</p>



<p>So this is an acyclic graph.</p>



<p>Okay, that means it&#8217;s a tree.</p>



<p>Is this a rooted tree?</p>



<p>ancestor to all other nodes and it&#8217;s that 88 node that&#8217;s the root node of the</p>



<p>entire tree every single node in the tree can say that 88 is its greatest</p>



<p>ancestor there&#8217;s no you know same leveled nodes at the very very very top okay so</p>



<p>this is a rooted tree now is it a binary tree yeah we look at every single node</p>



<p>and we can see that none of the nodes have more than two children they&#8217;ve all</p>



<p>this is a binary tree. And then when we, you know, we verified the numbers a second ago,</p>



<p>that means this is a binary search tree. So it satisfies all the properties of a binary search</p>



<p>tree. It doesn&#8217;t look very good. It&#8217;s kind of wonky, but it is valid. Remember this,</p>



<p>you know, a binary search tree just by itself is not a self-balancing tree,</p>



<p>which means you, you, you need to like not inject your human feelings into the tree while you&#8217;re</p>



<p>building it and start rearranging it to make it prettier. It wouldn&#8217;t be a valid binary search</p>



<p>if you tried to add special rules when you were creating it.</p>



<p>I don&#8217;t know.</p>



<p>If you imagine that maybe you, the human,</p>



<p>were changing the input data,</p>



<p>then I guess that&#8217;s valid.</p>



<p>But don&#8217;t imagine that the tree</p>



<p>is changing the data as you add it.</p>



<p>Anyway, so how fast is this tree?</p>



<p>Remember we said in a previous video</p>



<p>that a perfect binary search tree,</p>



<p>meaning perfectly balanced on every single node,</p>



<p>would be a log time tree.</p>



<p>It would be really, really fast to add and search</p>



<p>to add and search and even remove but this tree is definitely lopsided so this</p>



<p>is not a log tree the time complexity of a binary search tree actually is not</p>



<p>really log because the tree could just grow in any direction so in the worst</p>



<p>case scenario if you add the worst possible data you&#8217;d end up with a linear</p>



<p>time tree self balancing trees will be log time trees because they always</p>



<p>rearrange themselves to make sure that they&#8217;re balanced to a certain extent but</p>



<p>But, you know, as a binary search tree scales, it could just get slower and slower and slower.</p>



<p>So we wouldn&#8217;t be able to say that BSTs, binary search trees in general are log time.</p>



<p>We could pull up a calculator though and we could say, well, you know, what&#8217;s the height of this particular tree?</p>



<p>Let me just look at the depths real fast here.</p>



<p>What&#8217;s the height of this tree?</p>



<p>So the root node has a depth of zero and then one and then two and then three and then four.</p>



<p>drawing your tree in a nice pretty way where all the ranks are the same physically.</p>



<p>We just take the deepest node and we add one to it and we say that&#8217;s the height.</p>



<p>So the height of this tree is six.</p>



<p>Another way of looking at it is if we wanted to touch one of the deepest nodes,</p>



<p>how many nodes would we have to touch as we travel down through the tree?</p>



<p>So how many nodes would we have to touch to find, you know, the nine or the 17 or the 55?</p>



<p>one node the 88 two and let&#8217;s go to the left three four five six we&#8217;d have to touch six nodes</p>



<p>to get to the deepest node so that&#8217;s why we&#8217;re saying that&#8217;s another way that we&#8217;re justifying</p>



<p>that we say the height is six so the height of this tree is six which means in the worst case</p>



<p>scenario if we needed to search this tree again we&#8217;ll talk about actually searching in another</p>



<p>video but for now i can just promise you that if we were going to search this tree we could do it</p>



<p>going to search this tree we could do it in at most six uh examinations so even though we have</p>



<p>a lot of nodes here how many nodes do we actually have let me just double check this we have n</p>



<p>is equal to one two three four five six seven eight nine ten eleven twelve thirteen fourteen</p>



<p>we have fourteen nodes but at most we&#8217;d have to examine six nodes because this is a binary search</p>



<p>tree so it&#8217;s a lot faster than scanning the whole tree in linear time but it could be a lot faster</p>



<p>It could be a lot faster if the tree was better balanced.</p>



<p>That&#8217;s a topic for another day.</p>



<p>So for example, if we wanted to search for the number 55, let&#8217;s say we were just going</p>



<p>to search for 55, you know, we&#8217;d look at the 88.</p>



<p>Basically searching is pretty much the same as adding a node.</p>



<p>You just kind of go find where the node would belong.</p>



<p>And then if it&#8217;s there, then you found it.</p>



<p>And if not, then it&#8217;s not in there.</p>



<p>55 belongs on the left of 88, belongs on the right of 21, it belongs on the left of 72.</p>



<p>belongs on the left of 72, belongs on the right of 34, belongs on the left of 66, and there we</p>



<p>found it. Notice how we only did one, two, three, four, five, six examinations. So I&#8217;m going to talk</p>



<p>about big O and time complexities in another video, but long story short, the time complexity</p>



<p>of this particular tree, or actually any binary search tree, is always going to be O of H, no</p>



<p>imbalanced or perfectly balanced O of H so in the worst case scenario since H is 6 we&#8217;re going to have to touch H or 6 nodes</p>



<p>To search or add something or whatever if the tree was perfect. It would look more like log time. Let&#8217;s pull up that calculator real fast</p>



<p>Where the heck is my calculator? I think I like got sidetracked</p>



<p>so if I pull up a calculator here</p>



<p>and I typed in</p>



<p>log base 2 it&#8217;s base 2 because we only allow two children per node at most log</p>



<p>base 2 of the number of nodes in the tree so log base 2 of 14 if we get the</p>



<p>answer to that it says 3.8 which means the calculator is telling us that in a</p>



<p>log time tree we should not have to examine more than four nodes in order to</p>



<p>search the tree or add a brand new node but this is not a log tree because it is</p>



<p>totally imbalanced it&#8217;s kind of wonky it&#8217;s not it&#8217;s not perfect so it&#8217;s telling us in a perfect</p>



<p>tree we&#8217;d have to touch four nodes only but we clearly know we might have to touch six nodes in</p>



<p>the worst case so that i&#8217;m just trying to bring to your attention that the more imbalanced the tree is</p>



<p>the further its uh performance drifts away from log time which is a great argument</p>



<p>I think this is pretty much everything I wanted to talk about today.</p>



<p>I&#8217;m going to take a break and like, you know, eat some cookies and stuff.</p>



<p>But in a future video, we&#8217;re going to talk about inserting.</p>



<p>No, we already just did that literally right now.</p>



<p>We&#8217;re going to talk about searching through the tree.</p>



<p>We&#8217;re going to talk about removing nodes from the tree.</p>



<p>We&#8217;re going to talk about linear trees.</p>



<p>We&#8217;re going to talk about all the time complexities.</p>



<p>And then eventually we&#8217;ll move on to self-balancing trees.</p>



<p>on to self-balancing trees. So I hope you had fun watching this video. I hope you learned a little</p>



<p>bit of stuff and I&#8217;ll see you at a later date in time. I&#8217;m outie. Patowdy. Note to self, edit out</p>



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



<p>hope you did learn something and have some fun 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 you do on the</p>



<p>current social media website that you&#8217;re looking at right now it would really mean the world to me</p>



<p>and it&#8217;ll help make more videos and grow this community so we&#8217;ll be able to do more videos</p>



<p>longer videos better videos or just i&#8217;ll be able to keep making videos in general so please</p>



<p>and subscribe. You know, sometimes I&#8217;m sleeping in the middle of the night and I just wake up</p>



<p>because I know somebody subscribed or followed. It just wakes me up and I get filled with joy.</p>



<p>That&#8217;s exactly what happens every single time. So you could do it as a nice favor to me or you</p>



<p>could, you could troll me if you want to just wake me up in the middle of the night, just subscribe</p>



<p>and then I&#8217;ll, I&#8217;ll just wake up. I promise that&#8217;s what will happen. Also, uh, if you look at the</p>



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



<p>we&#8217;re at the bottom of this video and it&#8217;ll take you to my main website where you can just kind of</p>



<p>like see all the videos i published and the services and tutorials and things that i offer</p>



<p>and all that good stuff and uh if you have a suggestion for uh uh clarifications or errata</p>



<p>or just future videos that you want to see please leave a comment or if you just want to say hey</p>



<p>what&#8217;s up what&#8217;s going on you know just send me a comment whatever i also wake up for those in the</p>



<p>wake up in a cold sweat and i&#8217;m like it would really it really mean the world to me i would</p>



<p>really appreciate it so again thank you so much for watching this video and um enjoy the cool music</p>



<p>as 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/binary-search-tree-insertion-tutorial-building-a-bst-from-scratch-step-by-step/">Binary Search Tree Insertion Tutorial: Building a BST from Scratch Step by Step</a> appeared first on <a href="https://www.NeuralLantern.com">NeuralLantern.com</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.NeuralLantern.com/binary-search-tree-insertion-tutorial-building-a-bst-from-scratch-step-by-step/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Building a Binary Search Tree &#8211; Insertion Tutorial</title>
		<link>https://www.NeuralLantern.com/building-a-binary-search-tree-insertion-tutorial/</link>
					<comments>https://www.NeuralLantern.com/building-a-binary-search-tree-insertion-tutorial/#respond</comments>
		
		<dc:creator><![CDATA[mike]]></dc:creator>
		<pubDate>Sun, 12 Apr 2026 05:12:30 +0000</pubDate>
				<category><![CDATA[Binary Search Trees]]></category>
		<category><![CDATA[Computer Science]]></category>
		<category><![CDATA[Data Structures]]></category>
		<category><![CDATA[Videos]]></category>
		<category><![CDATA[algorithms]]></category>
		<category><![CDATA[binary search tree]]></category>
		<category><![CDATA[binary search tree example]]></category>
		<category><![CDATA[binary search tree tutorial]]></category>
		<category><![CDATA[binary tree insertion]]></category>
		<category><![CDATA[BST depth]]></category>
		<category><![CDATA[BST from scratch]]></category>
		<category><![CDATA[BST height]]></category>
		<category><![CDATA[BST insertion]]></category>
		<category><![CDATA[build binary search tree]]></category>
		<category><![CDATA[computer science tutorial]]></category>
		<category><![CDATA[data structures]]></category>
		<category><![CDATA[data structures and algorithms]]></category>
		<category><![CDATA[in order traversal]]></category>
		<category><![CDATA[insert into binary search tree]]></category>
		<guid isPermaLink="false">https://www.NeuralLantern.com/?p=330</guid>

					<description><![CDATA[<p>In this tutorial we build a binary search tree together by inserting the numbers 73, 98, 12, 45, 67, 10, 43, 11, 99, and 32 one by one. Starting from an empty tree, each number is placed according to binary search tree rules: smaller values go left, larger values go right. We let the nodes fall where they belong without any rearranging, resulting in a realistic (and slightly lopsided) BST. The video explains tree depth, height, and confirms the final structure maintains all BST properties with a properly sorted in-order traversal.</p>
<p>The post <a href="https://www.NeuralLantern.com/building-a-binary-search-tree-insertion-tutorial/">Building a Binary Search Tree &#8211; Insertion Tutorial</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="Building a Binary Search Tree - Insertion Tutorial" width="1380" height="776" src="https://www.youtube.com/embed/iuuUaa4CxoU?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>In this hands-on video we build a binary search tree by inserting numbers one by one: 73, 98, 12, 45, 67, 10, 43, 11, 99, and 32. Watch exactly how each node finds its place following BST rules with no rearranging allowed.</p>



<p>We discuss why the tree becomes lopsided, calculate its height, and verify the in-order traversal is sorted. Great practice for anyone learning binary search trees.</p>



<p>Next videos will cover searching and deleting nodes.</p>



<p>If you&#8217;re studying data structures, build along with me!</p>



<p>00:00 Introduction to Building a Binary Search Tree<br>00:14 Plan for Inserting Numbers<br>00:30 First Node Becomes Root<br>02:26 Begin inserting from data set<br>12:29 Final insertion from data set<br>13:26 Final Tree Overview<br>13:29 Tree is Lopsided<br>14:00 Drawing Depths and Levels<br>14:50 Calculating Tree Height<br>15:08 Verifying Sorted Order<br>16:00 Confirming BST Properties<br>16:48 Time Complexity and Height<br>18:59 No Self-Balancing<br>19:07 End of Video and Thanks</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>hey there let&#8217;s build a binary search tree together if you&#8217;ve seen my previous videos</p>



<p>you understand now how to define a binary search tree meaning what are the rules</p>



<p>that you have to like adhere to if you want your thing to be a binary search tree</p>



<p>also terminology we&#8217;re in previous videos so in this video we&#8217;re just going to take a bunch of</p>



<p>random numbers and we&#8217;re just going to start dropping them into our tree and see how the</p>



<p>tree builds itself out we have to build our tree in a specific way if it&#8217;s going to be valid we&#8217;re</p>



<p>uh valid we&#8217;re not going to rearrange our tree and try to make it super pretty this is going to be</p>



<p>like a real binary search tree it might not be pretty but it&#8217;ll be real let&#8217;s see if i can find</p>



<p>those numbers that i was going to set up okay so for this particular video i think i&#8217;m going to do</p>



<p>two videos i want to build a binary search tree using the following numbers they&#8217;re not special</p>



<p>not not perfect but what I mean to say when I say inserting these numbers is one by one we&#8217;ll look</p>



<p>at each number and we&#8217;ll drop it in the tree and we&#8217;ll see where it falls according to binary</p>



<p>search tree rules of insertion then we&#8217;ll just go to the next number and then the next and the next</p>



<p>when we&#8217;re done we should have a tree and if you follow along at home and you use the right rules</p>



<p>to build your tree then if we use the same sequence of numbers to build the tree we should</p>



<p>they won&#8217;t suddenly be different they&#8217;re deterministic okay so the first thing that</p>



<p>we should try to understand is uh when we want to insert a node the 73 node right here the first</p>



<p>thing we should ask ourselves is do we actually have a root node for our binary search tree yet</p>



<p>right now the answer is no this is just a blank tree there&#8217;s nothing in it which means that first</p>



<p>number is going to be the root node you might be tempted to say no that&#8217;s a sucky root node let&#8217;s</p>



<p>do something else well i don&#8217;t know i mean you just have to let the numbers fall where they&#8217;re</p>



<p>going to fall just because a number seems to suck as a root node doesn&#8217;t mean it&#8217;s not going to be</p>



<p>the root node if it&#8217;s the first thing you added it&#8217;s going to be the root node don&#8217;t try to change</p>



<p>it or you&#8217;re going to mess you&#8217;re going to end up with topology that doesn&#8217;t match whatever system</p>



<p>or person is trying to grade your work or evaluate your work so we have no root node that means the</p>



<p>node of the entire tree we&#8217;re now done with 73 i&#8217;m going to duplicate this slide real fast</p>



<p>and we&#8217;ll say now it&#8217;s time to try to add the 98 you know what i can i think i can do better than</p>



<p>that arrow i can make a nice cute arrow with this program i&#8217;m going to do this can i like yeah</p>



<p>hang on one more one more try nice okay so now let&#8217;s add the 98 node</p>



<p>the first thing we have to ask is do we have a root node uh yes we can&#8217;t actually put the 98 there</p>



<p>we now have to make a decision uh if you recall in my previous videos the lesser values go on the</p>



<p>left and the greater values go on the right uh in in these trees which we&#8217;re not going to support</p>



<p>duplicate values with if you wanted to support duplicate values you could do something like</p>



<p>less than or equal to would be on the left and greater than on the right there&#8217;s a bunch of</p>



<p>other ways you can do it but i&#8217;m just going to say no duplicate values in our trees so we look at the</p>



<p>So we look at the 73 node and we ask, you know, is that where our 98 is going to go?</p>



<p>It can&#8217;t go there because 73 exists.</p>



<p>Now we have to ask the question, does 98 belong on the left side or the right side of the 73?</p>



<p>Well, because 98 is greater than 73, it belongs on the right side, which means I&#8217;m going to duplicate this node over here.</p>



<p>And I&#8217;m going to try to draw this well, which means basically splitting the difference in terms of the empty space.</p>



<p>empty space and trying to keep the same rank as we go down one level of</p>



<p>descendancy or like one generation down at a time.</p>



<p>So the next time I go down a level,</p>



<p>let&#8217;s say I do a left child in the future,</p>



<p>it should be at the same level as the new node I just added.</p>



<p>So I did a duplicate and I forgot to change the number.</p>



<p>And then I&#8217;m going to do a line to connect the 73 and the 98.</p>



<p>98 so now the 73 has a right child which is 98 the 98 has a parent which is 73 and we&#8217;re done</p>



<p>adding the 98 we have to just move on to the next number okay so i&#8217;m going to move on to the 12</p>



<p>again we look at the root node it&#8217;s occupied we cannot put the 12 right there on the 73 we have</p>



<p>to make a decision do we go to the left or the right of the 73 12 is less than 73 so it belongs</p>



<p>is to jump to the left side. Notice how the left side there&#8217;s no child on the left that means that&#8217;s</p>



<p>where the new node is going to go so I&#8217;m just going to like select this duplicate it real fast</p>



<p>maybe stick it on the same vertical coordinate and then also try to split the difference on</p>



<p>the screen just to make it look pretty. I&#8217;m going to update that 98 so that it is now the 12.</p>



<p>child 12 and then we&#8217;re done adding the 12. Okay, no problem so far. We then are ready to try to</p>



<p>add the 45. Maybe I should squish this a little bit. Nope, that looks gross. No, it&#8217;s okay.</p>



<p>We&#8217;ll do that. We&#8217;re going to add the 45. Again, we look at the root node. It already exists,</p>



<p>so we can&#8217;t put the 45 there. Instead, we will just kind of decide does the 45 belong on the</p>



<p>Okay, well, belongs on the left because it&#8217;s less than 73.</p>



<p>So that means I&#8217;m now going to make a little decision.</p>



<p>I&#8217;m going to jump down to the left.</p>



<p>If you&#8217;re in some code, you&#8217;re following the left child pointer down.</p>



<p>But notice how the left child pointer or the left child, it already exists.</p>



<p>So we can&#8217;t actually put 45 right there.</p>



<p>We have to make another decision.</p>



<p>Does 45 belong on the left or the right of the 12?</p>



<p>Well, it&#8217;s greater than 12.</p>



<p>So it belongs on the right side of 12.</p>



<p>So I&#8217;m going to highlight this and duplicate it.</p>



<p>and duplicate it. I guess I&#8217;ll just do it that way. Then I&#8217;m going to update that number</p>



<p>with 45 and then I&#8217;m going to connect the 12 and the 45 with the line. So now 45 is the right child</p>



<p>of 12 and 12 is the parent of 45. It&#8217;s at this point that a lot of people start to get frustrated</p>



<p>with me and they say, well, how come, how come the 12 is like all the way to the left and the 45 is</p>



<p>45 is like didn&#8217;t become like you know a level higher because it kind of seems like it&#8217;s closer</p>



<p>to 73 shouldn&#8217;t the 12 be hanging off the left child of the 45 no no no no don&#8217;t rearrange the</p>



<p>nodes as you&#8217;re building the tree just let the numbers let the nodes fall wherever they&#8217;re</p>



<p>supposed to go another tip is if you&#8217;re building a tree that has other data types as as the t types</p>



<p>inside of the nodes as the template types inside of the nodes for example right now I&#8217;m just using</p>



<p>type is int, at least in my mind, if you wanted to put dinosaur objects in the tree, all you&#8217;d</p>



<p>have to do is make sure that your code for your dinosaur class for your dinosaur object</p>



<p>had its inequality operators implemented, you know, less than, greater than, and then the tree</p>



<p>would still know where to put all the objects. So you could make any kind of custom object you want</p>



<p>and they would all fall in their correct places in the tree as long as the operators actually</p>



<p>just as a side note. Okay, so now we&#8217;re ready to add the next item. So I&#8217;m going to erase this</p>



<p>stuff here, and I&#8217;m going to move the arrow from the 45 to the 67. So now we&#8217;re going to try to</p>



<p>add the 67. We look at the root node, it&#8217;s occupied. We should go to the left because 67</p>



<p>is less than 73. We look at the 12, that&#8217;s occupied. So we go left or right. We should go</p>



<p>to the right because 67 is greater than 12. So we look at the 45. Again, it&#8217;s occupied. There&#8217;s a</p>



<p>a node there so we have to make another decision do we go to the left or to the right well 67 is</p>



<p>greater than 45 so it goes on the right side so i&#8217;m going to duplicate this off to the right side</p>



<p>and down a rank and we&#8217;ll call that the right child of 45. let me update the number here</p>



<p>to 67 and i&#8217;ll connect that as the right child okay</p>



<p>Any questions? I&#8217;m just kidding. That was rude. Just know that my heart is with you if you&#8217;re</p>



<p>asking questions. Send me a comment in the comments. So now I&#8217;m going to move over to the 10 here.</p>



<p>Again, we look at the 67. The 10 belongs on the left. So we kind of go to the left.</p>



<p>We look at the 12. The 10 belongs on the left of the 12 because it&#8217;s less than 12.</p>



<p>available slot that&#8217;s an available child pointer so I&#8217;m going to say the 10 is going to be the</p>



<p>left child of the 12 node maybe I&#8217;ll stick it all the way over there let&#8217;s see is there anything</p>



<p>less than 10 in there nope okay so we&#8217;ll be good we have enough space on the screen</p>



<p>so change that to a 10 do the connecting line so the 10 is now the left child</p>



<p>whoops the left child of the 12 node and now we&#8217;re ready to add the next node</p>



<p>node. Okay. So now the 43 is the next one we want to add. One more time, we look at the 73 node,</p>



<p>the root node is occupied. So we have to go to the left because 43 is less. So oops, that&#8217;s a line.</p>



<p>So now we look at the 12 node is 43 greater than or less than the 12. It&#8217;s greater than so that</p>



<p>means we go down to the right. But the 45 is there. So we have to make another decision. 43 is less</p>



<p>43 is less than 45 so we go to the left that means this 43 is going to be the left child of 45.</p>



<p>And again don&#8217;t try to make the tree pretty or better just let the nodes fall wherever they&#8217;re</p>



<p>supposed to fall according to the rules of less than or greater than. So now we got the uh whoops</p>



<p>I forgot to change that to a 43. The 43 right there and we are now ready to add another node</p>



<p>So I&#8217;m going to like erase this stuff here and I want to move the arrow to the 11.</p>



<p>Now once again, we&#8217;re looking for the root node.</p>



<p>It&#8217;s already occupied.</p>



<p>We say that 11 is supposed to go on the left because it&#8217;s less than.</p>



<p>We look at the 12.</p>



<p>It&#8217;s occupied.</p>



<p>So we have to go to the left because 11 is less than 12.</p>



<p>We go to the 10 and that&#8217;s also occupied.</p>



<p>So we have to make another decision.</p>



<p>Is 11 greater than or less than 10?</p>



<p>It&#8217;s greater than.</p>



<p>why the 11 goes on the right side of the 10. So I&#8217;m going to duplicate this over here. Notice how</p>



<p>I&#8217;m trying to keep same ranked children or same depth children physically at the same level. It&#8217;s</p>



<p>a lot easier to debug your diagrams if you do things this way. Notice also, as I mentioned in</p>



<p>a previous video, that every left descendant of a node should be physically on the left side in my</p>



<p>diagram and every right descendant should be physically on the right side. That just makes</p>



<p>right side that just makes the diagram way easier to debug what do I mean by this look at 73&#8217;s left</p>



<p>descendants its left subtree they&#8217;re all physically on the left look at the 12 node all of its right</p>



<p>descendants or its right subtree they&#8217;re all physically on the right so you if you follow</p>



<p>that rule it&#8217;s just way easier to debug I think I forgot to update that number from 43 to 11 so</p>



<p>I&#8217;m going to do that now and we are ready to add the next number the 99 so I&#8217;m going to do this</p>



<p>So I&#8217;m going to do this. We&#8217;re looking at 99.</p>



<p>So first we start off by looking at the root node.</p>



<p>It&#8217;s 73, occupied. 99 belongs on the right side.</p>



<p>So now we bounce to the right side and we look at the 98.</p>



<p>Whoops, whoops, whoops. We look at the 98.</p>



<p>That&#8217;s occupied, so we make another decision.</p>



<p>The 99 belongs on the right side of 98. That slot is available.</p>



<p>So now we can say that the 99 node is supposed to be</p>



<p>a right child of 99.</p>



<p>Sorry, sorry.</p>



<p>The 99 is the right child of the 98 node.</p>



<p>I&#8217;m getting a little confused.</p>



<p>Okay, so I&#8217;m gonna do a little connecting line there.</p>



<p>And we&#8217;re ready to add the final value into our tree.</p>



<p>So I&#8217;m gonna do, let&#8217;s see.</p>



<p>Oh, oh, I accidentally erased the arrow.</p>



<p>Oh, there it is.</p>



<p>We can add the 32 next.</p>



<p>add the 32 next and if we look at the 73 it&#8217;s occupied 32 belongs on the left so then we just</p>



<p>kind of like follow down to the left here we look at the 12 32 belongs on the right of 12 so we</p>



<p>follow down to the right 32 belongs on the left of 45 so we follow down to the left 32 belongs on</p>



<p>the left of 43 and so we will make 32 the left child of the 43 node so I&#8217;m going to like select</p>



<p>this real fast we go down a full rank and I&#8217;m going to try to split the difference between 12</p>



<p>And I&#8217;m going to try to split the difference between 12 and 43 here just to make the diagram easier to debug.</p>



<p>Okay, so then I&#8217;m going to do…</p>



<p>It&#8217;s looking a little gross, but at least we&#8217;re finished.</p>



<p>I have to update that 11 to be the 32.</p>



<p>And now we&#8217;re done building our tree.</p>



<p>Okay, so some things to point out real fast.</p>



<p>Notice how the tree is lopsided.</p>



<p>to look at various parts of the tree you might be frustrated why didn&#8217;t we put the 98 over there</p>



<p>why didn&#8217;t we put the right just it&#8217;s fine as long as we follow the rules of building a binary search</p>



<p>tree then this is the valid tree that we would have built of course later on you&#8217;ll learn techniques</p>



<p>to make the tree a little bit better looking a little bit faster but right now we&#8217;re just building</p>



<p>a tree with no optimizations no self balancing no anything like that this is our binary search tree</p>



<p>tree if we were to take all of the uh you know all the numbers that we added and sort them</p>



<p>then that should be the numbers we see from left to right this is another good argument for why we</p>



<p>should draw the diagram in this way again like uh same depth nodes let me just draw the the depths</p>



<p>real fast just to kind of illustrate the point a little bit same depth nodes should be on the same</p>



<p>y coordinate or the same uh horizontal plane notice how 73 is the root node and then 12 with 98 have</p>



<p>with 98 have a depth of 1 and then 10 45 and 99 have a depth of 2 and then 11 43 and 67 have a</p>



<p>depth of 3 and then 32 has a depth of 4 so the deepest node we can find has a depth of 4 that&#8217;s</p>



<p>the 32 node which means the height of the tree um height is 4 sorry 5 the height is always the</p>



<p>deepest nodes depth plus 1 or the number of nodes you would have to touch as you found your way</p>



<p>you found your way down to the deepest node for example if we wanted to get to that 32 we would</p>



<p>touch one two three four five nodes so the height of this tree is five</p>



<p>okay um let&#8217;s debug the order of our numbers because this is kind of easy to get wrong</p>



<p>so from left to right we should see a sorted list so let me just uh do little markings here</p>



<p>to indicate that i&#8217;m seeing increasing or at least non-decreasing numbers</p>



<p>If our tree supported duplicates, then it would be non-decreasing.</p>



<p>But since we don&#8217;t allow duplicates, it should be increasing or an ascending list.</p>



<p>So I&#8217;m going to do the 10.</p>



<p>And as we look towards the right, we should see increasing numbers.</p>



<p>So we got the 10 there.</p>



<p>And then we got the 11 there.</p>



<p>And then we got the 12 there.</p>



<p>32, 43, 45, 67.</p>



<p>So far, so good.</p>



<p>It&#8217;s all increasing.</p>



<p>73 increases, 98 increases, and 99 increases.</p>



<p>and 99 increases. So from left to right, it was basically a sorted list, which is what we want.</p>



<p>So we&#8217;ve created a valid ordering for the tree. Now maybe just think about all the rules of a</p>



<p>binary search tree to make sure we did that right too. So is this a graph? Yeah. Is it a connected</p>



<p>graph? Yeah. Is it a tree? You know, no cycles in the graph? Yeah. Is it a rooted tree? Yeah. The 73</p>



<p>root node is the common ancestor of all other nodes. We have one node that&#8217;s common to the</p>



<p>in terms of ancestry um does any node have more than two children no none of the nodes have more</p>



<p>than two children um they&#8217;ve all got zero or one or two children and then the numbers are in order</p>



<p>and therefore this is a valid binary search tree so in other videos i&#8217;m going to talk about the</p>



<p>time complexity and and like you know how fast it is to do certain operations or how imbalanced a</p>



<p>just so you know this is not a perfect binary search tree you can tell because it&#8217;s a little</p>



<p>lopsided um we calculated the height as five and let&#8217;s see where the heck is that calculator i</p>



<p>thought i had a calculator on here oh no oh there it is if we calculated the height as five</p>



<p>you could probably imagine and we&#8217;ll talk about searching in a future video like how do you search</p>



<p>in a way that makes the tree super super fast well we certainly built the tree in a way that</p>



<p>makes it super super fast we just have to get to searching later but if we</p>



<p>search properly through a properly built binary search tree then at most it</p>



<p>should only take us height number of examinations to find our way to any</p>



<p>piece of data or to determine that a piece of data doesn&#8217;t exist for example</p>



<p>I could say that the time complexity is O and we&#8217;ll do this in other videos of</p>



<p>of h no matter what the tree looks like no matter how lopsided it is and what is h here five that</p>



<p>just basically is saying in the worst case scenario i would have to touch five nodes to reach the very</p>



<p>bottom of the tree and determine that a value didn&#8217;t exist or you know find a value at the very</p>



<p>bottom of the tree so the worst case scenario touch five nodes so it&#8217;s o of h if we if we plug</p>



<p>log base 2 so if we did log base 2 of n where n is the number of nodes uh oh how many nodes do we</p>



<p>have shoot one two three four five six seven eight nine ten i think one two three four five six seven</p>



<p>eight nine ten yeah log base two of ten notice how the number here says well it should take like</p>



<p>somewhat close to three uh examinations at most four examinations but we just said it&#8217;s going to</p>



<p>of a divergence between the real speed of the tree.</p>



<p>It might take up to five examinations and log base two,</p>



<p>which would be a perfect binary search tree.</p>



<p>So the more lopsided a tree gets, the slower it actually becomes.</p>



<p>And we&#8217;ll deal with that in future videos.</p>



<p>We&#8217;ll learn how to like detect that and balance that and so forth.</p>



<p>But so we&#8217;ve built a binary search tree.</p>



<p>It&#8217;s definitely valid if not, you know, like super, super fast.</p>



<p>And I think that&#8217;s the end of this video.</p>



<p>Thank you so much for watching.</p>



<p>I hope you learned a little bit of stuff and had a little bit of fun.</p>



<p>In the next video, I think I&#8217;m just going to build another tree for you so we can get</p>



<p>a second practice in.</p>



<p>Okay, future videos also are going to include searching and deleting nodes and modifying</p>



<p>existing nodes and just a whole bunch of other stuff.</p>



<p>Then eventually we&#8217;ll do self-balancing trees.</p>



<p>I&#8217;ll see you later.</p>



<p>Hey everybody.</p>



<p>Hey everybody!</p>



<p>Thanks for watching this video again from the bottom of my heart.</p>



<p>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, could you please subscribe and follow</p>



<p>this channel or these videos or whatever it is you do on the current social media website</p>



<p>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>So we&#8217;ll be able to do more videos, longer videos, better videos, or just I&#8217;ll be able</p>



<p>I&#8217;ll be able to keep making videos in general.</p>



<p>So please do me a kindness and subscribe.</p>



<p>You know, sometimes I&#8217;m sleeping in the middle of the night</p>



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



<p>It just wakes me up and I get filled with joy.</p>



<p>That&#8217;s exactly what happens every single time.</p>



<p>So you could do it as a nice favor to me</p>



<p>or you could troll me if you want to just wake me up in the middle of the night.</p>



<p>Just subscribe and then I&#8217;ll just wake up.</p>



<p>I promise that&#8217;s what will happen.</p>



<p>Also, if you look at the middle of the screen right now,</p>



<p>right now you should see a QR code which you can scan in order to go to the website which I think</p>



<p>is also named somewhere at the bottom of this video and it&#8217;ll take you to my main website where</p>



<p>you can just kind of like see all the videos I published and the services and tutorials and</p>



<p>things that I offer and all that good stuff and if you have a suggestion for clarifications or</p>



<p>errata or just future videos that you want to see please leave a comment or if you just want to say</p>



<p>what&#8217;s going on you know just send me a comment whatever i also wake up for those in the middle</p>



<p>of the night i get i wake up in a cold sweat and i&#8217;m like it would really it really mean the world</p>



<p>to me i would really appreciate it so again thank you so much for watching this video and um enjoy</p>



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



<p>Thank you.</p>



<p>hey there let&#8217;s talk about uh</p>



<p>hello there hello there let&#8217;s build a binary search tree by just inserting a bunch of numbers</p>



<p>inserting a bunch of numbers and will follow the rules</p>
<p>The post <a href="https://www.NeuralLantern.com/building-a-binary-search-tree-insertion-tutorial/">Building a Binary Search Tree &#8211; Insertion Tutorial</a> appeared first on <a href="https://www.NeuralLantern.com">NeuralLantern.com</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.NeuralLantern.com/building-a-binary-search-tree-insertion-tutorial/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>BST Terminology: Root, Leaves, Subtrees, Depth, Height, Ancestors &#038; More</title>
		<link>https://www.NeuralLantern.com/bst-terminology-root-leaves-subtrees-depth-height-ancestors-more/</link>
					<comments>https://www.NeuralLantern.com/bst-terminology-root-leaves-subtrees-depth-height-ancestors-more/#respond</comments>
		
		<dc:creator><![CDATA[mike]]></dc:creator>
		<pubDate>Mon, 06 Apr 2026 09:25:41 +0000</pubDate>
				<category><![CDATA[Binary Search Trees]]></category>
		<category><![CDATA[Computer Science]]></category>
		<category><![CDATA[Data Structures]]></category>
		<category><![CDATA[Videos]]></category>
		<category><![CDATA[binary search tree]]></category>
		<category><![CDATA[binary search tree basics]]></category>
		<category><![CDATA[binary search tree explained]]></category>
		<category><![CDATA[binary tree terminology]]></category>
		<category><![CDATA[BST ancestors]]></category>
		<category><![CDATA[BST descendants]]></category>
		<category><![CDATA[bst for beginners]]></category>
		<category><![CDATA[BST internal nodes]]></category>
		<category><![CDATA[BST leaf nodes]]></category>
		<category><![CDATA[BST root node]]></category>
		<category><![CDATA[BST subtrees]]></category>
		<category><![CDATA[BST terminology]]></category>
		<category><![CDATA[coding interview prep]]></category>
		<category><![CDATA[computer science trees]]></category>
		<category><![CDATA[data structures tutorial]]></category>
		<category><![CDATA[left subtree]]></category>
		<category><![CDATA[right subtree]]></category>
		<category><![CDATA[tree depth]]></category>
		<category><![CDATA[tree height]]></category>
		<guid isPermaLink="false">https://www.NeuralLantern.com/?p=327</guid>

					<description><![CDATA[<p>This video explains essential binary search tree terminology including root node, internal nodes, external nodes (leaves), left and right subtrees, depth of a node, height of the tree and subtrees, ancestors, descendants, siblings, parent and child relationships using a clear example tree.</p>
<p>The post <a href="https://www.NeuralLantern.com/bst-terminology-root-leaves-subtrees-depth-height-ancestors-more/">BST Terminology: Root, Leaves, Subtrees, Depth, Height, Ancestors &amp; More</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 loading="lazy" title="BST Terminology: Root, Leaves, Subtrees, Depth, Height, Ancestors &amp; More" width="1380" height="776" src="https://www.youtube.com/embed/4iuhXHw26ec?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 thorough run-through of binary search tree terminology: root, leaves, internal nodes, subtrees, depth, height, ancestors, descendants, siblings, left/right child &#8211; everything clearly labeled on a working example.</p>



<p>Great for beginners, interview prep, or reviewing foundational BST concepts before coding insert/search/delete.</p>



<p>00:00 Introduction to BST Terminology<br>00:28 Root Node<br>01:10 Ancestors and Descendants<br>01:58 Children, Grandchildren, and Siblings<br>04:07 Internal Nodes vs External Nodes (Leaves)<br>05:34 Understanding Subtrees<br>06:09 Left Subtree and Right Subtree Examples<br>08:34 Depth of a Node<br>11:02 Height of the Tree<br>12:48 Height of Subtrees<br>17:32 Node Structure and Pointers Overview<br>18:12 Closing Remarks and Call to Action</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! Let&#8217;s talk about binary search tree terminology. If you saw my</p>



<p>previous video we talked about how to define a binary search tree meaning a</p>



<p>whole bunch of rules so that if the thing you&#8217;re looking at follows all the</p>



<p>rules then you know you&#8217;re actually looking at a binary search tree if not</p>



<p>then not. So see my previous video if you want to know for sure whether you&#8217;re</p>



<p>looking at a binary search tree. For now we&#8217;re just going to talk about some</p>



<p>Okay, so the first thing we should probably obviously talk about is the root node over here.</p>



<p>So I mean, well, in my previous video, we talked about nodes, right?</p>



<p>So this is kind of a graph with a whole bunch of rules on top of it.</p>



<p>That means we have nodes and edges.</p>



<p>So if you look at the very top node here, sometimes also referred to as a vertex,</p>



<p>I think in binary search tree terminology, we usually say nodes.</p>



<p>We usually say nodes. I can&#8217;t actually remember. But so look at the 42 there</p>



<p>That node is the root node of all other nodes. It&#8217;s the highest common ancestor in the entire tree. So this is the root node</p>



<p>First bit of terminology. Also, I tried to sneak past you ancestor</p>



<p>So these trees are supposed to be written in a way that kind of looks like they have a family hierarchy with one parent per</p>



<p>like not two children per parent, but just like one parent and then either zero or one or two</p>



<p>children. So we&#8217;ll say that ancestors are higher on the tree. So that means 42 is actually an</p>



<p>ancestor of 33. And it&#8217;s also an ancestor of 12. It&#8217;s also an ancestor of 19. Just anything that&#8217;s</p>



<p>higher is an ancestor of anything that&#8217;s lower. We could also say that 33 is an ancestor of 39</p>



<p>and 19 and so forth. You can probably also imagine that we have children and grandchildren. Yeah,</p>



<p>we do go that far in binary search trees. So the 42 node, it has two children. It has a left child</p>



<p>and a right child. The 33 is the left child. I&#8217;ll put LC for left child. And the 67 is its right</p>



<p>child. I&#8217;ll put an RC there. The 67 in turn has two children. The 56 has no children of its own.</p>



<p>the 33 has two children the 12 only has one child it was you know playing it safe i guess</p>



<p>you never know if these children are going to come out and just like run amok</p>



<p>and and engage in constant shenanigans so the 12 has a right child but no left child that&#8217;s okay</p>



<p>um in terms of going higher on the tree anything that is higher is an ancestor sorry i should have</p>



<p>said lower anything that&#8217;s higher is an ancestor anything that&#8217;s lower is a descendant so if we&#8217;re</p>



<p>If we&#8217;re looking at the 33 node, the 33 is a descendant of 42 because it&#8217;s the left child of 42.</p>



<p>It&#8217;s also an ancestor of anything that comes below it.</p>



<p>So it&#8217;s an ancestor of 12 and 39 and 19, right?</p>



<p>So if we&#8217;re looking at 33, we&#8217;ve got a left child over here and we&#8217;ve got a right child over here.</p>



<p>And then we have a grandchild, which is the 19 node.</p>



<p>We don&#8217;t really have left and right grandchildren.</p>



<p>You could say a grandchild in the left subtree or the right subtree.</p>



<p>subtree talk about sub trees in a second the 33 has a parent node which is just the 42 node</p>



<p>which is also the root node of course it&#8217;s got a sibling the 67 is the sibling you can tell</p>



<p>something&#8217;s a sibling because it&#8217;s got the same parent as you it&#8217;s the people that you&#8217;re usually</p>



<p>fighting with right anyway so if we&#8217;re looking at any node in particular it might have a whole bunch</p>



<p>of ancestors above the tree it might have a whole bunch of descendants below the tree</p>



<p>It has siblings or it usually has zero or one sibling because in a binary search tree,</p>



<p>we can only have up to two children per node.</p>



<p>It&#8217;s got sometimes, you know, grandparents and great grandparents and children and great</p>



<p>grandchildren.</p>



<p>So just think about the hierarchy like a family tree would have.</p>



<p>Okay, moving on to some more terminology.</p>



<p>Next thing is we have internal nodes and also external nodes.</p>



<p>So what do I mean by internal? Internal means a node has more than zero children. It has one or two children.</p>



<p>So I&#8217;m going to put internal on the 33 because the 33 node has children.</p>



<p>The 12 also has children, so it&#8217;s internal. The 39 does not have children, so it&#8217;s not internal.</p>



<p>67 has children, so it&#8217;s internal. And the root node, 42, also has children, so it&#8217;s considered internal.</p>



<p>That 42 has a lot of different names.</p>



<p>It&#8217;s the root node, it&#8217;s the greatest common ancestor,</p>



<p>it&#8217;s an internal node and so forth.</p>



<p>Notice how the other nodes that I have not highlighted,</p>



<p>they have zero children.</p>



<p>So when a node has zero children,</p>



<p>it&#8217;s known as an external node.</p>



<p>It&#8217;s also known as a leaf</p>



<p>because we&#8217;re talking about trees</p>



<p>and I guess it&#8217;s kind of like a nice synonym.</p>



<p>So the 19, the node with no children of its own is a leaf.</p>



<p>So is the 39.</p>



<p>So is the 56.</p>



<p>So is the 76.</p>



<p>I just want to point out also, if you were with me on my last video,</p>



<p>then the numbers need to be ordered from left to right.</p>



<p>But don&#8217;t worry, we&#8217;re going to do another video where we build a complete tree from scratch.</p>



<p>There&#8217;s some more terminology we should talk about.</p>



<p>So I&#8217;m going to get rid of all these externals and internals real fast.</p>



<p>Or the labels.</p>



<p>We should talk about the left subtree versus the right subtree.</p>



<p>left subtree versus the right subtree i mean what is a subtree anyway the subtree is basically</p>



<p>a subtree is basically just pick any node you want in the entire tree let&#8217;s pick</p>



<p>the 76 and then we&#8217;ll just pretend that it&#8217;s the root node of a separate tree starting with 76 so</p>



<p>if there was anything below it then all those nodes would be included so this 76 right here it</p>



<p>really has nothing underneath it it&#8217;s a leaf which means well it can be the root node of its own</p>



<p>the subtree is just going to be a tree of one node.</p>



<p>So kind of boring, right?</p>



<p>You&#8217;re boring.</p>



<p>If instead we decided to look at the 33,</p>



<p>which is a little bit more interesting,</p>



<p>and we called the 33 the root node of its own subtree,</p>



<p>then really what we&#8217;re saying is all these nodes here</p>



<p>are included in that subtree.</p>



<p>So if I told you, give me the subtree starting with node 33,</p>



<p>then you would say, oh, it&#8217;s 33, 12, 39, 19.</p>



<p>descendants of the subtree root node that we picked out. So subtree just means, you know,</p>



<p>like a little fragment or a portion of the original tree. You could also say that the</p>



<p>entire tree is a subtree of itself. If you chose the subtree root to be the real root node, I mean,</p>



<p>that&#8217;s not super useful, but you can do it. Anyway, so if we decide to say that the 67 is the root of</p>



<p>with 67 and below in terms of descendancy is going to be considered part of the subtree.</p>



<p>I&#8217;ve highlighted the left subtree and the right subtree of the root node because that&#8217;s usually</p>



<p>what we say. We&#8217;ll say this is the left subtree over here and then over here we&#8217;re going to say</p>



<p>this is the right subtree. Meaning if you look at any node at all, if it has a left child,</p>



<p>then that left child is the root node of the left subtree of the node in question. Same thing for</p>



<p>same thing for the right so if I say all right let me duplicate this real fast</p>



<p>let me get rid of actually this real fast too if I say okay give me the left subtree of the 33 node</p>



<p>well then you would know to include the 12 and the 19 because the left subtree of the 30 node</p>



<p>has to the 33 node has to start with the left child of the 33 node which would be the 12 and</p>



<p>And we&#8217;ll just say, okay, the 12 is now the root node of its own subtree.</p>



<p>And then anything that goes below it in descendancy is going to be considered part of that subtree.</p>



<p>So that highlighted subtree is the left subtree of the 33 node.</p>



<p>The 39 is the right subtree of the 33 node.</p>



<p>I think I just did the wrong color.</p>



<p>Let me do that in gray.</p>



<p>Right, so we can do left subtree and right subtree for any node in the entire tree.</p>



<p>if a node has no children, then there are no subtrees, but we can still look and check.</p>



<p>And if there are children, then we&#8217;ve got subtrees or left and right subtrees.</p>



<p>Okay, so now that we&#8217;re done talking about subtrees real fast, let&#8217;s talk about the depth of a node.</p>



<p>So for me, I like to say that the depth of the root node is zero.</p>



<p>And so I&#8217;ll just, I guess we could start off by putting a zero on the 42 indicating it has zero depth.</p>



<p>It has zero depth.</p>



<p>Imagine maybe it&#8217;s a buoy in the water and it&#8217;s just like sitting, floating like directly</p>



<p>on the water.</p>



<p>So it has no depth.</p>



<p>It&#8217;s just like kind of on the surface.</p>



<p>But if you draw your binary search trees in this nice pretty way where every single time</p>



<p>you go down a generation from parent to child, from parent to child, you maintain, I guess,</p>



<p>like the same Y coordinate for same leveled nodes, then it&#8217;s really easy to calculate</p>



<p>the depth of every single node.</p>



<p>of every single note let me show you what show you what i mean real fast you saw my video uh</p>



<p>previously then you already know this but the 42 it&#8217;s got two children so if i go down to get one</p>



<p>of its children i&#8217;m going down to the 33 and then i&#8217;m going down to the 67 right since those two</p>



<p>children are on the same i guess level as if we were looking at a family tree they should be</p>



<p>physically on the same level they should be on the same y coordinate or the same horizontal plane</p>



<p>plane if we go down one more level which means any child of 33 or any child of 67 then those all</p>



<p>should be lined up also so notice how these are all lined up on the same y coordinate then if we</p>



<p>go down another level then this 19 here is just kind of by itself because the tree is not very big</p>



<p>so if we draw the tree like this which is a really smart way because uh it&#8217;s easier to debug</p>



<p>whether it&#8217;s a valid binary search tree and all sorts of other things,</p>



<p>then we can easily write down the depth kind of on the side of the graph.</p>



<p>We can say, all right, here&#8217;s depth zero, and here&#8217;s depth one,</p>



<p>and here&#8217;s depth two, and here&#8217;s depth three.</p>



<p>Just every time you go down one level, you just increase the depth.</p>



<p>And now you know the depths of all the nodes in the entire tree pretty quickly.</p>



<p>The 19, I&#8217;m just going to maybe do this in red.</p>



<p>the 56 and the 76 have a depth of 2, the 33 has a depth of 1, and the 42 has a depth of 0.</p>



<p>So all these trees, sorry, all these nodes in the tree have their own depth,</p>



<p>which are very easy to calculate if you draw the tree well. The next thing after depth is the height</p>



<p>of the tree. So what is the height of the tree? Well, that&#8217;s basically the depth of the deepest</p>



<p>to the very deepest node what is the minimum number of nodes that you must touch when you</p>



<p>start at the root node and then find your way to the deepest possible node in the entire tree so</p>



<p>um if you if you notice the 19 node is definitely the deepest node in the entire tree</p>



<p>it&#8217;s got a depth of three which means the height of the entire tree is four heights</p>



<p>equals four and maybe i&#8217;ll change that to like just black or something okay</p>



<p>So, let&#8217;s do it the other way real fast.</p>



<p>If we&#8217;re kind of just walking down the tree, let&#8217;s start at the 42 and then we go down</p>



<p>to the 33, we&#8217;ve touched two nodes so far.</p>



<p>We go down to the 12, we&#8217;ve touched three nodes.</p>



<p>We go down to the 19, we&#8217;ve touched four nodes.</p>



<p>So the height of the tree is four or the number of nodes that you need to touch as you make</p>



<p>your way down towards the deepest node or just a shortcut is the deepest nodes depth</p>



<p>plus one.</p>



<p>And that&#8217;s the height of the tree.</p>



<p>you can also have a height of a left subtree and a height of a right subtree so let me just</p>



<p>what&#8217;s going on here i think my thing is crashing hello oh i was definitely crashing i think my cpu</p>



<p>is burning right now all right i&#8217;m going to be complaining about my new cpu for a long time</p>



<p>i sprung only a few bucks for the best cpu that this motherboard could hold but it&#8217;s an old</p>



<p>something percent um i&#8217;m eventually going to have to like build a brand new computer</p>



<p>anyway so uh suppose we&#8217;re looking at the 67 node and the question is you know what is the height</p>



<p>of the left subtree of the 67 node versus the right subtree of the 67 node well if you recall</p>



<p>the left subtree is just all the nodes that are included uh beginning with the root node of the</p>



<p>subtree of the 67 node well that&#8217;s just the 56 node totally by itself what&#8217;s the right subtree</p>



<p>of the 67 it&#8217;s just that 76 node all by itself what&#8217;s the depth of 56 and 76 they&#8217;re both depth</p>



<p>zero if we&#8217;re talking about relative depths per their subtrees that means what is the height of</p>



<p>the left subtree and the right subtree they&#8217;re just one because the depth is zero the maximum</p>



<p>depth is zero and if we want to get to the deepest node in one of those subtrees we&#8217;re just</p>



<p>end up touching the one node that&#8217;s in the in the subtree at all so that means uh</p>



<p>uh i don&#8217;t want to write down uh left subtree height right now so left subtree height of 67</p>



<p>is one right subtree height of 67 is also one so we can do this with any node we want you know what</p>



<p>is the uh what is the situation with the 33 node let&#8217;s do uh the 33 node yeah uh it has a left</p>



<p>It has a left subtree height of 2 and you can tell because, well, the left subtree starts with the left child and the left child is going to be the root node of its own subtree.</p>



<p>Notice how the maximum depth we can find here is 1, right?</p>



<p>Like if we start at depth 0 for the 12, considering like it&#8217;s a relative depth, that means we take the deepest node, which is the 19 node, which has a depth of 1.</p>



<p>We add 1 to that.</p>



<p>So that means the height of that subtree is 2.</p>



<p>if you wanted to find the deepest node in that whole subtree, how many nodes would you have to</p>



<p>touch to get there? We&#8217;d have to touch the 12 and then touch the 19. We touch two nodes, so the height</p>



<p>of that subtree is two. Maybe I&#8217;ll just put H equals two here. So now for the right subtree of</p>



<p>the 33 node, it&#8217;s kind of easier. We just basically only have one node to really look at.</p>



<p>that means that 39 has a depth of 0 and the right subtree has a height of 1.</p>



<p>Whoops, 8 equals 1.</p>



<p>I didn&#8217;t put two equal signs. I like to do two.</p>



<p>I like to do the comparison operator.</p>



<p>We could also do the same thing with the 42 node, right?</p>



<p>We can say, let&#8217;s get rid of all this stuff real fast.</p>



<p>We can do the 42.</p>



<p>Its left subtree starts with that 33 node.</p>



<p>33 node so I&#8217;m just going to highlight that real fast the 42 nodes left subtree has a height of</p>



<p>one two three and you can tell because the 33 has a depth of zero a relative depth of zero and</p>



<p>the 12 and the 39 have one and the 19 has two so the deepest node has a depth of two</p>



<p>so that means the height of that subtree is is three so I&#8217;m just going to like do this real fast</p>



<p>and then the right subtree of the 42 node, the root node of the entire tree, is going to be this.</p>



<p>So the root node has a depth of zero, depth of zero, and then these other leaves over here have</p>



<p>depth of ones, which means the height of this subtree is going to be two, or the deepest node</p>



<p>plus one, or the number of nodes you need to touch to find the deepest node. And 56 and 76,</p>



<p>those are both equally the deepest node in those trees okay so we talked about a bunch of</p>



<p>terminology here let me just double check my notes in case I forgot to to tell you anything</p>



<p>I think I&#8217;m all right well maybe okay maybe I should real fast just briefly mention that these</p>



<p>nodes I mean this is not really part of the video exactly but let&#8217;s let me just mention that these</p>



<p>whoops that&#8217;s dumb let me do a blue circle they would have you know there&#8217;s</p>



<p>like some sort of an object you would call it a node and then they would have</p>



<p>pointers they would have each of these nodes would have a pointer to its</p>



<p>parents and it would have a left child pointer that goes down into the left and</p>



<p>a right child pointer that goes down to the right and also a little slot in that</p>



<p>object for the data so I&#8217;m just going to put t type data and C++ I usually say</p>



<p>I usually say that the templated data type for a node or data structure is just the T type.</p>



<p>That just means you could put anything you want.</p>



<p>You could have your nodes hold integers, letters, strings, custom objects, whatever you want to do.</p>



<p>And we&#8217;ll talk about this more in future videos.</p>



<p>But long story short, I want you to just imagine that every node actually has three pointers inside of it pointing to something else, to other nodes,</p>



<p>graph because in a graph you could have like a whole bunch of different connections and that&#8217;s</p>



<p>usually managed by the actual graph object itself so anyway we&#8217;re done with terminology i hope you</p>



<p>learned a little bit of stuff and had a little bit of fun thanks for watching this video</p>



<p>tell your friends eat a donut and a chocolate and then be really happy and stuff okay i gotta go</p>



<p>Hey everybody!</p>



<p>Thanks for watching this video again from the bottom of my heart.</p>



<p>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</p>



<p>or whatever it is you do on the current social media website</p>



<p>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</p>



<p>and grow this community.</p>



<p>So we&#8217;ll be able to do more videos, longer videos, better videos,</p>



<p>or just I&#8217;ll be able to keep making videos in general so please do do me a</p>



<p>kindness and 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</p>



<p>just wakes me up and I get filled with joy that&#8217;s exactly what happens every</p>



<p>single time so you could do it as a nice favor to me or you could you control me</p>



<p>if you want to just wake me up in the middle of the night just subscribe and</p>



<p>then I&#8217;ll just wake up I promise that&#8217;s what will happen also if you look at the</p>



<p>at the middle of the screen right now you should see a qr code which you can scan in order to go</p>



<p>to the website which i think is also named somewhere at the bottom of this video and it&#8217;ll</p>



<p>take you to my main website where you can just kind of like see all the videos i published and</p>



<p>the services and tutorials and things that i offer and all that good stuff and uh</p>



<p>if you have a suggestion for uh clarifications or errata or just future videos that you want to see</p>



<p>say hey what&#8217;s up what&#8217;s going on you know just send me a comment whatever i also wake up for</p>



<p>those in the middle of the night i get i wake up in a cold sweat and i&#8217;m like it would really it</p>



<p>really mean the world to me i would really appreciate it so again thank you so much for</p>



<p>watching this video and um enjoy the cool music as as i fade into the darkness which is coming for us</p>



<p>Thank you.</p>
<p>The post <a href="https://www.NeuralLantern.com/bst-terminology-root-leaves-subtrees-depth-height-ancestors-more/">BST Terminology: Root, Leaves, Subtrees, Depth, Height, Ancestors &amp; More</a> appeared first on <a href="https://www.NeuralLantern.com">NeuralLantern.com</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.NeuralLantern.com/bst-terminology-root-leaves-subtrees-depth-height-ancestors-more/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Binary Search Tree Definition Explained Step by Step</title>
		<link>https://www.NeuralLantern.com/binary-search-tree-definition-explained-step-by-step/</link>
					<comments>https://www.NeuralLantern.com/binary-search-tree-definition-explained-step-by-step/#respond</comments>
		
		<dc:creator><![CDATA[mike]]></dc:creator>
		<pubDate>Tue, 31 Mar 2026 03:10:16 +0000</pubDate>
				<category><![CDATA[Binary Search Trees]]></category>
		<category><![CDATA[Computer Science]]></category>
		<category><![CDATA[Data Structures]]></category>
		<category><![CDATA[Videos]]></category>
		<category><![CDATA[acyclic graph]]></category>
		<category><![CDATA[algorithms]]></category>
		<category><![CDATA[binary search tree]]></category>
		<category><![CDATA[binary search tree definition]]></category>
		<category><![CDATA[binary search tree explained]]></category>
		<category><![CDATA[binary tree vs binary search tree]]></category>
		<category><![CDATA[bst]]></category>
		<category><![CDATA[bst for beginners]]></category>
		<category><![CDATA[bst ordering property]]></category>
		<category><![CDATA[bst rules]]></category>
		<category><![CDATA[computer science]]></category>
		<category><![CDATA[connected graph]]></category>
		<category><![CDATA[data structures]]></category>
		<category><![CDATA[left subtree right subtree]]></category>
		<category><![CDATA[rooted binary tree]]></category>
		<category><![CDATA[tree data structure]]></category>
		<category><![CDATA[what is a binary search tree]]></category>
		<guid isPermaLink="false">https://www.NeuralLantern.com/?p=324</guid>

					<description><![CDATA[<p>A Binary Search Tree (BST) is a rooted binary tree where for every node, all values in its left subtree are less than the node's value, and all values in its right subtree are greater than the node's value. No duplicates in this simple version.</p>
<p>The post <a href="https://www.NeuralLantern.com/binary-search-tree-definition-explained-step-by-step/">Binary Search Tree Definition Explained Step by Step</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 loading="lazy" title="Binary Search Tree Definition Explained Step by Step" width="1380" height="776" src="https://www.youtube.com/embed/H1BU1UkFiDo?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>Clear step-by-step explanation of what defines a Binary Search Tree (BST). We build the definition rule-by-rule starting from graphs all the way to the BST ordering property (all left descendants, node, all right descendants). Great first video before learning insert, delete, search, and Big-O.</p>



<p>What is a Binary Search Tree? 00:00<br>Intro to Graphs and Nodes 00:42<br>Connected Graph Requirement 03:16<br>Acyclic Graph &#8211; Removing Cycles 04:54<br>Turning Graph into Tree 07:05<br>Establishing a Root 07:09<br>Adding Hierarchy and Levels 09:05<br>Single Common Ancestor 11:18<br>Binary Tree Definition 17:30<br>BST Ordering Property 17:39<br>Left Subtree Less Than Node 18:29<br>Fixing Invalid BST Example 20:26<br>Valid BST Final Check 21:24<br>In-Order Ascending Order 24:11<br>Video Summary and Next Steps 25:02<br>Thanks and Call to Action 25:30</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>hey there let&#8217;s talk about binary search trees specifically in this video I want to help you</p>



<p>define and identify what is a binary search tree all the rules that go into determining whether</p>



<p>or not something is a binary search tree so you can you can tell that you&#8217;re looking at one or</p>



<p>whether you&#8217;re not looking at one we&#8217;ll talk about some terminology but keep in mind I&#8217;m</p>



<p>going to save searching and sorting and like you know deleting and adding items and a whole bunch</p>



<p>extra stuff, especially the hard stuff for videos that&#8217;ll come right after this one.</p>



<p>For now, we&#8217;re just going to talk about what is a binary search tree.</p>



<p>So you can see on the screen right here, I hope that we have a binary search tree.</p>



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



<p>Can you see that?</p>



<p>Yeah, I think you probably can.</p>



<p>Yeah, I think you can.</p>



<p>This is not drawn quite as well as I would like to draw ours.</p>



<p>I&#8217;ll tell you how to do that in a little while.</p>



<p>to do that in a little while but when you draw a tree in a really really nice pretty aligned way</p>



<p>it&#8217;s way easier to debug it&#8217;s way easier to tell that you&#8217;re looking at something that is sorted</p>



<p>and so forth anyway okay so first let&#8217;s start off with a graph so this is not a video about graphs</p>



<p>uh i guess i can probably expect that you already kind of know what a graph is at this point in time</p>



<p>and edges. So what do I mean by nodes? Well just imagine there&#8217;s like a little circle here</p>



<p>that contains some sort of data. If you know data structures already then</p>



<p>there would be a t-type sitting inside the node. We&#8217;ll just call the t-type an integer</p>



<p>and so for our nodes, for our trees, we&#8217;re just going to say that they hold integers even though</p>



<p>you know technically speaking in a binary search tree or a graph you could have all sorts of</p>



<p>Okay. So I&#8217;m just going to, you know, grab this and sort of like duplicate it and just duplicate it again.</p>



<p>I&#8217;m just going to make a bunch of duplicates. Something is definitely wonky about my setup right now.</p>



<p>I think I just upgraded the camera and the CPU is maxed out. I just upgraded the CPU on this computer too.</p>



<p>Oh well. Okay. So we aren&#8217;t going to support duplicate values in our binary search tree,</p>



<p>I&#8217;m going to say it doesn&#8217;t really matter for the graph. Later on, we&#8217;ll start making sure there&#8217;s</p>



<p>no duplicate data. For now, I think I can probably do that somewhat quickly. Let&#8217;s see. So a graph,</p>



<p>you know, for the for our purposes right now, a graph is just basically a collection of nodes and</p>



<p>edges. It could be empty. It could be just nodes. It couldn&#8217;t just be edges, it would have an edge</p>



<p>always has to have a node or two connected to it. So we just have like a bunch of nodes inside of</p>



<p>data values this is a valid graph but I&#8217;m going to draw some edges randomly here so we&#8217;re going</p>



<p>to do this it doesn&#8217;t really matter I&#8217;m just randomly drawing edges okay actually I should</p>



<p>probably do maybe like one that&#8217;s kind of separated okay so this is a graph this is not a binary search</p>



<p>tree so let&#8217;s add a bunch of rules let me just pull up my rules real fast here okay so what is</p>



<p>First we&#8217;ll start off with the graph like we have here and then we&#8217;ll say that the graph must be connected.</p>



<p>What is a connected graph? A connected graph is basically a graph where every single node can find every single other node</p>



<p>or find a path to every single other node only following along edges and only by respecting the direction of edges.</p>



<p>You can see this particular graph is undirected meaning the edges don&#8217;t really have a direction to them.</p>



<p>So that means we could travel along in any direction from node to node.</p>



<p>So just as a real quick, you know, I guess like tutorial, I&#8217;m going to say is 8 connected to every single other node?</p>



<p>Well, it can find a path to 12 and it can find a path to 33 and it can find a path to 76 and 45.</p>



<p>But it cannot find a path to 99, which means this is not a connected graph.</p>



<p>So that&#8217;s the first rule that we have to implement here.</p>



<p>We&#8217;re going to say this graph needs to be upgraded or modified so that it will be a connected graph.</p>



<p>start randomly adding edges until this is a connected graph I&#8217;m gonna do maybe</p>



<p>like an edge from 99 to 12 there and then now I think this is a connected</p>



<p>graph but let&#8217;s double check real fast so connected graph you know what I&#8217;m</p>



<p>gonna put graph connectedness in another video to keep this one short for now</p>



<p>check out my other videos if you would like to see whether or not a graph is</p>



<p>but long story short as I just said if every single node can find a path to</p>



<p>every single other node without you know skipping where there is no edge then</p>



<p>it&#8217;s connected okay so that&#8217;s the first thing the next thing that we need let me</p>



<p>just put the words connected up here we need a connected graph the next thing</p>



<p>that we need is we need an acyclic graph which basically means a graph with no</p>



<p>A cycle is, can you find a path in this graph that starts at one node and ends at the same node without repeating any edges?</p>



<p>So for example, if I was asking, you know, is 76 involved in any cycles?</p>



<p>Well, we could go from 76 to 33 and then 12.</p>



<p>And, you know, we could kind of take 8, 45, 12 again.</p>



<p>But by the time we turn around and try to get back to the 33, we&#8217;ve already crossed this edge twice.</p>



<p>so that means the 76 is not involved in any cycles because we cannot find our way back to 76</p>



<p>without repeating an edge so 76 is okay i&#8217;m just going to maybe put like a little check box here</p>



<p>and uh you can also tell that the 33 is not involved in any cycles because we go down to</p>



<p>the 76 and then come back up to 33 we already repeated an edge so that&#8217;s not valid same thing</p>



<p>again. Same thing for the 34, same thing for the 99. But if you look at the 12 here, we could</p>



<p>actually find a cycle. We could go from the 12 to the 8, and then from the 8 to the 45, and from the</p>



<p>45 to the 12. Now we&#8217;re back where we started. We&#8217;re at the 12 node again, and we did not repeat</p>



<p>any edges. So this is not a cyclic. This is a cyclic graph. It&#8217;s got a cycle. So that&#8217;s the</p>



<p>we want to make sure that our graph has no cycles. So, uh, the next rule I&#8217;m just going to say is,</p>



<p>is going to be satisfied by me removing some edges at random. So I don&#8217;t know, let&#8217;s get rid of, uh,</p>



<p>the eight to the 45 connection. See if that works. Um, let&#8217;s see. Well, wait a minute. What did I</p>



<p>just do? Something happened here. I just accidentally, oh, I erased too many edges</p>



<p>from the last slide. Okay. Let me add that one back in 33 to 76. That was supposed to be there.</p>



<p>33 to 76 that was supposed to be there.</p>



<p>So now we have an acyclic graph</p>



<p>and we can consider this graph a tree.</p>



<p>The next thing we need to do is add a hierarchy to this tree.</p>



<p>So this is gonna make more sense in a little bit</p>



<p>but for now I&#8217;m gonna say,</p>



<p>we&#8217;ll imagine a family tree kind of,</p>



<p>it&#8217;s not really exactly a family tree</p>



<p>but you know imagine there are parents</p>



<p>and children and grandchildren</p>



<p>and we&#8217;ll just try to like,</p>



<p>we&#8217;ll try to rank children according to</p>



<p>according to you know when they were born or or how many parents or children</p>



<p>they have you know their descendancy their lineage whatever you want to call</p>



<p>it so I think I&#8217;m just gonna maybe move the eight up a little bit so that it&#8217;s</p>



<p>on the same rank with the 12 and the 33 so I&#8217;m just gonna move these up a little</p>



<p>bit and then redo their connecting lines so I&#8217;m gonna do this and that and then</p>



<p>and then we&#8217;ll say that the 76 is still a child of the 33.</p>



<p>So I&#8217;ll just kind of move it over here.</p>



<p>Or sorry, we never said it was a child.</p>



<p>Now we&#8217;re saying it&#8217;s a child</p>



<p>because when you go from top to bottom,</p>



<p>imagine like a family tree,</p>



<p>we&#8217;ll say we have parents and children.</p>



<p>The children or the descendants are lower.</p>



<p>The ancestors or the parents or the grandparents are higher.</p>



<p>Maybe I want to put this 45 over on the left</p>



<p>so that it&#8217;s a child of the 8 node.</p>



<p>And maybe I&#8217;ll say that the 99 node</p>



<p>the 99 node is a child of the 12 node. Okay, so eventually this is going to look not exactly like</p>



<p>a family tree because usually in the family tree we have two parents that go to one or more children.</p>



<p>In this type of tree, in eventually our binary tree, we&#8217;re going to have one parent that has,</p>



<p>you know, children just by itself, like asexual reproduction. We&#8217;re pretending to be bacteria</p>



<p>What was that comedy skit a long time ago, Tiny Elvis?</p>



<p>And he was like, hey, see that protozoa over there?</p>



<p>The thing is huge.</p>



<p>Anyone?</p>



<p>No?</p>



<p>Okay.</p>



<p>So the next rule we need is we must have a rooted tree.</p>



<p>What is a rooted tree?</p>



<p>Actually, let me clean up the lineage real fast, the heritage.</p>



<p>I want to make nodes that have the same level with respect to their ancestors</p>



<p>respect to their ancestors to have the same physical y coordinate the same</p>



<p>physical level so notice how the 99 I just moved it down a little bit because</p>



<p>I wanted it to look like it&#8217;s the same you know number of generations lower</p>



<p>than the top row so like with aid it had a child 45 so that&#8217;s one level down 33</p>



<p>had a child that&#8217;s one level down that&#8217;s a 76 and the 12 also one level down</p>



<p>would have been 99 but the 99 was kind of too physically high this will help</p>



<p>will help you debug your binary trees in the future anyway so i&#8217;m just going to clean this up right now</p>



<p>and uh now that we have you know parent child relationships we need to look at the most common</p>



<p>ancestor in the whole entire tree or if there is one so notice how uh i guess i&#8217;m just kind of using</p>



<p>the word ancestor without explaining it so your ancestors are the people that you&#8217;re disappointing</p>



<p>or whatever it is, ancestors came before us, right?</p>



<p>Like my parents are my ancestors.</p>



<p>My grandparents are also my ancestors.</p>



<p>My great-grandparents are also my ancestors.</p>



<p>Going down in the family tree are your descendants.</p>



<p>You know, my children are my descendants.</p>



<p>My grandchildren are my descendants.</p>



<p>My great-grandchildren are my descendants and so forth.</p>



<p>So if you look at the number 34 here,</p>



<p>what is the greatest ancestor that it has?</p>



<p>Well, if you just go up and up and up</p>



<p>until you can&#8217;t go up anymore,</p>



<p>up until you can&#8217;t go up anymore it&#8217;s the 12th so that means the greatest ancestor of at least in</p>



<p>this tree of the 34 is the 12th the 45 the highest you can go is the 8 notice how 8 and 12 and 33 are</p>



<p>siblings there&#8217;s not really like a parent-child relationship they&#8217;re connected sideways we&#8217;ll</p>



<p>eventually get rid of the sideways connections for our binary search trees but for now we have</p>



<p>some siblings and so we actually have three different greatest ancestors in this tree we&#8217;ve</p>



<p>tree we&#8217;ve got 8 and 12 and 33 and that&#8217;s no good so the next rule that we need is we need</p>



<p>one common ancestor for every single other node in the entire tree so I&#8217;m going to duplicate this</p>



<p>slide right here and I&#8217;m going to say we need to clean this up so we got to choose 8 or 12 or 33</p>



<p>I don&#8217;t know I&#8217;m just going to maybe say that 8 and it&#8217;s 45 are children of the 12 so I&#8217;m just</p>



<p>line here arbitrarily so i&#8217;m gonna go like that notice how i&#8217;m trying to keep the 8 the 99 and</p>



<p>the 76 on the same y coordinate the same level now we have two uh you know greatest ancestors</p>



<p>we need to have only one so i think i&#8217;m probably going to say that the 33 and the 76 are going to</p>



<p>become children of the 12 node just for simplicity so i&#8217;m going to put this over here i&#8217;m going to</p>



<p>i&#8217;m gonna like move this over here this is a gross tree but we&#8217;ll fix it soon we&#8217;ll do that all right</p>



<p>okay okay</p>



<p>okay and then i have to get rid of that little sibling line and then i have to add another line</p>



<p>to say that 33 is a child of 12. okay so now we have a rooted tree what we had before was an</p>



<p>unrooted tree or just like not a rooted tree because there was no root there was no greatest</p>



<p>greatest common ancestor to the entire rest of the tree. But now we can say that 12 is the root</p>



<p>of the tree because it&#8217;s the ancestor for all other nodes. I guess even if 12 was by itself,</p>



<p>we could call it a root of tree. It just wouldn&#8217;t be a very good example. So I&#8217;m going to actually</p>



<p>get rid of the word acyclic here since we already handled that. And I&#8217;m going to do,</p>



<p>I&#8217;m going to say that 34&#8217;s greatest ancestor is 12, 99&#8217;s greatest ancestor is 12, 33&#8217;s greatest</p>



<p>all the nodes in the tree their greatest ancestor is 12 and it&#8217;s common to all of them so now we</p>



<p>have a rooted tree maybe I should type in my doomed I really hope the screen I really hope</p>



<p>the screen cast doesn&#8217;t freeze it probably will upgraded the CPU it&#8217;s 33 faster than it was before</p>



<p>board so i am still using an old cpu even though it&#8217;s new to me dang it i&#8217;m gonna have to spend</p>



<p>like 500 okay uh so the next rule is so we have a rooted tree and now let&#8217;s rearrange everything</p>



<p>hierarchically which we already kind of did so uh there&#8217;s not really much to do here except</p>



<p>we did hierarchical organization</p>



<p>we probably just need to move on to our next rule which is to say that</p>



<p>if our rooted tree is to become a binary tree</p>



<p>then each node in the entire tree</p>



<p>cannot have more than two children</p>



<p>every single node in the entire tree</p>



<p>can have zero or one or two children</p>



<p>if there are three or more anywhere in the entire tree</p>



<p>then it&#8217;s not a binary tree</p>



<p>so okay that means you know the 45 is okay</p>



<p>because it has no children</p>



<p>because it has no children. All the rows, all the leaves at the bottom are okay. All these nodes</p>



<p>over here are okay because they just have one child. But the 12 node, the root of the tree,</p>



<p>it&#8217;s got three children and that violates the rule. So now we need to rearrange this.</p>



<p>So I think I&#8217;m going to just move these numbers down here maybe. Whoops. Maybe move these nodes</p>



<p>descendants of the 33 node. So I&#8217;m going to do this.</p>



<p>Okay. So cool. Now we have a binary tree and now we need to rearrange the drawing a little bit.</p>



<p>This is not part of the definition, but this will make it easier to debug your trees. So what I want</p>



<p>to do is make it so that every left descendant of a node appears physically on that node&#8217;s left</p>



<p>every right descendant of a node should appear on its right side</p>



<p>or like further to the right on the x-coordinate</p>



<p>so sort of similar to you know like the y-coordinate ranking</p>



<p>but notice how the 12 its left child is 8 and its right child is 33</p>



<p>so that means all the nodes here on the left side you know that come underneath the 8</p>



<p>are left descendants or you could say that they are in the left subtree of the 12.</p>



<p>So, they&#8217;re okay because 8 is to the left of 12 and 45 is to the left of 12.</p>



<p>But notice how 33 is a right child of 12 and it&#8217;s part of the right subtree.</p>



<p>It&#8217;s part of the right descendancy, but it&#8217;s physically on the left of 12.</p>



<p>That&#8217;s going to be really bad because it&#8217;ll be harder to debug our trees later.</p>



<p>So, I&#8217;m just going to slide it over without rearranging the tree.</p>



<p>And I usually try to split the difference here.</p>



<p>and then their parent is kind of like physically in the middle that will also help you debug in</p>



<p>the future so i&#8217;m going to do this wrong let me get the blue i&#8217;m going to do this make sure that</p>



<p>my ranking is still okay not the best most neatest tree and so now i&#8217;m going to look at the eight</p>



<p>it&#8217;s left subtree or it&#8217;s left uh descendancy uh all of those nodes which is just the 45 are</p>



<p>it&#8217;s left child 76 is the only thing that&#8217;s on the left side in the left subtree so that&#8217;s all</p>



<p>right probably the 33 I think it&#8217;s still a little lopsided I probably could have you know dragged</p>



<p>it a little bit more to the right but I&#8217;m going to leave it so the 33 and the 76 are okay and then</p>



<p>the 99 is a right descendant or it&#8217;s part of the right subtree of the 33 so it should be on the</p>



<p>right side it is that&#8217;s good the 34 should be on the right side of the 33 because it&#8217;s it&#8217;s a it&#8217;s</p>



<p>It&#8217;s part of the right subtree of 33.</p>



<p>It&#8217;s part of the right descendancy.</p>



<p>And it should also be on the left side of the 99</p>



<p>because it&#8217;s part of the left subtree of the 99.</p>



<p>So, so far, this is okay.</p>



<p>This is not the best drawing.</p>



<p>So now we have a binary tree</p>



<p>and we have kind of drawn it in a slightly good way.</p>



<p>Not super great.</p>



<p>The last rule that we&#8217;re going to add here is,</p>



<p>whoops, what happened there?</p>



<p>I&#8217;m supposed to do that, yeah.</p>



<p>The last rule we&#8217;re going to add is</p>



<p>we must have ordering to our nodes, to the numbers in our nodes. So what do I mean by ordering? Well,</p>



<p>the whole reason that we want a binary search tree, at least usually, is because it&#8217;s really,</p>



<p>really fast to search through. This is going to be a tree that we can search through in log time,</p>



<p>which we&#8217;ll talk more about in another video. We&#8217;ll do time complexities and searching and stuff. But</p>



<p>in order for this to be a super fast tree to search through, we need to be able to</p>



<p>very quickly make a decision. Do we go down and to the left? Do we go down and to the right?</p>



<p>left do we go down into the right and every decision we make should help us eliminate half</p>



<p>of the remaining data set half of the tree at a time every time we go down a level and that&#8217;s only</p>



<p>possible if the nodes themselves are ordered so what i&#8217;m going to do is say every node that&#8217;s on</p>



<p>the left side or let&#8217;s say every descendant that&#8217;s on the left side of a node should be of a lesser</p>



<p>value than that particular node every node that is on the right side of a node meaning it&#8217;s a right</p>



<p>descendant of that node or part of the right subtree of the node should be greater than that</p>



<p>node. Please note that this would not allow us to support duplicate values in our tree.</p>



<p>You could use something like less than or equal to versus greater than if you wanted to support</p>



<p>duplicate values in your tree. You could also change the T type, you know, the template type</p>



<p>to a node from the data you want it to store to a list. So every single node has a list inside of it</p>



<p>and the items in the list are just, you know, the regular values that you wanted. There&#8217;s a whole</p>



<p>values that you wanted there&#8217;s a whole bunch of different ways you could actually do that</p>



<p>but for now we&#8217;re going to see our trees don&#8217;t support duplicate values</p>



<p>so okay we don&#8217;t support duplicate values but we need you know from left to right</p>



<p>the order needs to look sane and this is kind of why we&#8217;re drawing the tree in this special way</p>



<p>we&#8217;re trying to make our tree really easy to debug visually so check this out the way we&#8217;ve</p>



<p>drawn this every single node has its own x coordinate basically notice how the 12 there&#8217;s</p>



<p>nothing underneath it at least not directly underneath it the 33 the 34 all the nodes you</p>



<p>know you can draw a straight line down from the node and it shouldn&#8217;t cross over with any other</p>



<p>nodes or edges so that means now that we&#8217;ve drawn it this way and we&#8217;ve made sure that all the left</p>



<p>descendants are on the left and all the right descendants are on the right we could actually</p>



<p>just scan our eyes from left to right and be sure that we are seeing an increasing order or</p>



<p>Since duplicates aren&#8217;t allowed in this tree, we&#8217;ll say an increasing order or an ascending order.</p>



<p>So if we start at 45 and then we go to the right, we should see a higher number.</p>



<p>So if we look at 8, 8 is a lower number.</p>



<p>So this violates the ordering rule.</p>



<p>This is not a valid binary search tree.</p>



<p>It&#8217;s only a valid binary tree.</p>



<p>So sad.</p>



<p>Okay, so I could, you know, swap the 45 and the 8 or I could just increase the value of the 8 itself.</p>



<p>of the 8 itself, I think it&#8217;ll be faster for me to just increase the 8 value. So I&#8217;m going to make</p>



<p>it 52 now. So we&#8217;ve got 45, 52. Then we go to the right a little bit more. It should also increase.</p>



<p>So 52 should increase. I&#8217;m just going to randomly increase it to, you know, 59, let&#8217;s say. Then as</p>



<p>we go to the right, we see another number here. It is 76. That already is increased from 59.</p>



<p>So we don&#8217;t need, we don&#8217;t need to alter that number. Then we go to the right one more time</p>



<p>that because it&#8217;s lower than 76 so I&#8217;m going to put 87 then when we go to the right one more time</p>



<p>that&#8217;s the 34 down here that&#8217;s definitely a decrease so we have to increase it now so I&#8217;m</p>



<p>going to say 99 how about if I just do 92 yeah so now we have a 92 down here and then if we go</p>



<p>to the right a little bit more there&#8217;s the 99 and now we actually have a binary search tree let&#8217;s</p>



<p>just double check all the rules first off is this a graph yeah there&#8217;s nodes and edges there&#8217;s nothing</p>



<p>There&#8217;s nodes and edges.</p>



<p>There&#8217;s nothing weird happening.</p>



<p>There&#8217;s not like a edge floating in the middle of nowhere.</p>



<p>Is this a connected graph?</p>



<p>Yeah, it is.</p>



<p>Because every single node, if you followed, you know, the edges, you know, could find</p>



<p>every single other node.</p>



<p>Like the 45, it could find, you know, the 92 node if it just followed all these edges.</p>



<p>Notice again that these edges have no direction.</p>



<p>There&#8217;s no arrows on them.</p>



<p>So we could go up or down in any direction we want.</p>



<p>So that means the 76 can find everything else.</p>



<p>else and so this is a connected graph for sure okay now that we have a</p>



<p>connected graph is this an acyclic graph I don&#8217;t know about you but I can&#8217;t find</p>



<p>any cycles in the graph right like the 92 it goes out but it doesn&#8217;t come back in</p>



<p>same thing for the 76 if we went from the 76 up to the 87 and then took a</p>



<p>right turn we wouldn&#8217;t be able to come back towards it without repeating that</p>



<p>same edge so this is definitely an acyclic graph all of the nodes follow</p>



<p>they&#8217;re not involved in cycles is this a rooted tree yeah there&#8217;s only one common ancestor to the</p>



<p>entire tree it&#8217;s the 59 it&#8217;s the highest node in the entire tree and it&#8217;s common to all other of</p>



<p>all of its descendants um so let&#8217;s see i&#8217;m going to duplicate this real fast the next thing is</p>



<p>we arranged it hierarchically and we added the terminology of like i think i kind of just like</p>



<p>tried to sneak this one past you but we added the terminology of a left child and a right child</p>



<p>and a right child.</p>



<p>Right? We also added the terminology of a right subtree and a left subtree.</p>



<p>Just to clarify what I mean, what do I mean by a right subtree and a left subtree.</p>



<p>So imagine we&#8217;re considering the 59.</p>



<p>If we consider the 59, then the 52 is the root node of the left subtree of the 59 node.</p>



<p>If we consider the 59, then the 87 is the root node of the right subtree of the 59 node.</p>



<p>so the right child is basically the root of the subtree uh in in you know the right child is the</p>



<p>root of a subtree of the right subtree uh i think i&#8217;m getting muddled my words here okay</p>



<p>anyway so we have a little bit of terminology uh the 59 is the root node that&#8217;s like the</p>



<p>the only node in the entire tree without uh an ancestor or a parent so okay uh then we have a</p>



<p>Note, and the entire tree doesn&#8217;t have more than two children.</p>



<p>Notice how the 59 has two children.</p>



<p>That&#8217;s fine.</p>



<p>If it was three, that&#8217;s way too much.</p>



<p>52 has one.</p>



<p>87 has one.</p>



<p>No, 87 has two children.</p>



<p>99 has one child.</p>



<p>92 has zero children.</p>



<p>So, okay, we&#8217;re not violating that rule.</p>



<p>So, we definitely have a binary tree.</p>



<p>And then we, you know, kind of read from left to right.</p>



<p>Let me write down the numbers this time for fun.</p>



<p>physically without kind of like following edges or anything we just scan</p>



<p>our eyes from left to right we can see that we have an ascending list if we</p>



<p>supported duplicates it would be okay to just see a non decreasing list but we</p>



<p>see an increasing list or an ascended list ascending list okay whoops this is</p>



<p>left to right so this is actually a binary search tree and um i don&#8217;t know about you but i think i</p>



<p>need to like you know get a snickers or something right now because uh this video has been going on</p>



<p>for longer than i wanted it to go on so we&#8217;ve defined a binary search tree in future videos</p>



<p>we&#8217;ll talk about how to add nodes into a binary search tree how to build the tree what&#8217;s the time</p>



<p>complexity of the tree in various operations deleting nodes from a tree terminology like</p>



<p>terminology like what are these nodes called things like that so anyway thank you so much</p>



<p>for for watching i hope you learned a little bit of stuff and had a little bit of fun</p>



<p>i&#8217;ll see you at a later date in time</p>



<p>just kidding</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 uh if you could do me a please</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 you do on the current social media?</p>



<p>It&#8217;s a 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>So we&#8217;ll be able to do more videos longer videos better videos or just i&#8217;ll be able to keep making videos in general</p>



<p>So please do do me a kindness and uh and subscribe</p>



<p>I&#8217;m sleeping in the middle of the night and I just wake up because I know somebody subscribed or followed.</p>



<p>It just wakes me up and I get filled with joy.</p>



<p>That&#8217;s exactly what happens every single time.</p>



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



<p>Just subscribe and then I&#8217;ll just wake up.</p>



<p>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.</p>



<p>and 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</p>



<p>stuff.</p>



<p>And if you have a suggestion for clarifications or errata or just future videos that you want</p>



<p>to see, please leave a comment.</p>



<p>Or if you just want to say, Hey, what&#8217;s up?</p>



<p>What&#8217;s going on?</p>



<p>You know, just send me a comment, whatever.</p>



<p>I also wake up for those in the middle of the night.</p>



<p>I get, I wake up in a cold sweat and I&#8217;m like, it would really, it would really, it</p>



<p>it would really mean the world to me.</p>



<p>I would really appreciate it.</p>



<p>So again, thank you so much for watching this video</p>



<p>and enjoy the cool music</p>



<p>as I fade into the darkness,</p>



<p>which is coming for us all.</p>



<p>Thank you.</p>
<p>The post <a href="https://www.NeuralLantern.com/binary-search-tree-definition-explained-step-by-step/">Binary Search Tree Definition Explained Step by Step</a> appeared first on <a href="https://www.NeuralLantern.com">NeuralLantern.com</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.NeuralLantern.com/binary-search-tree-definition-explained-step-by-step/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>YASM x86-64 Assembly Functions Tutorial &#8211; Integers, Floats, Pointers, ABI, C++ Interoperability</title>
		<link>https://www.NeuralLantern.com/yasm-x86-64-assembly-functions-tutorial-integers-floats-pointers-abi-c-interoperability/</link>
					<comments>https://www.NeuralLantern.com/yasm-x86-64-assembly-functions-tutorial-integers-floats-pointers-abi-c-interoperability/#respond</comments>
		
		<dc:creator><![CDATA[mike]]></dc:creator>
		<pubDate>Sun, 22 Mar 2026 01:28:30 +0000</pubDate>
				<category><![CDATA[Assembly Language]]></category>
		<category><![CDATA[Coding]]></category>
		<category><![CDATA[Computer Science]]></category>
		<category><![CDATA[Videos]]></category>
		<category><![CDATA[abi x86-64]]></category>
		<category><![CDATA[amd64]]></category>
		<category><![CDATA[assembly language]]></category>
		<category><![CDATA[C++ assembly interop]]></category>
		<category><![CDATA[call ret assembly]]></category>
		<category><![CDATA[extern C]]></category>
		<category><![CDATA[floating point assembly]]></category>
		<category><![CDATA[hybrid C++ assembly]]></category>
		<category><![CDATA[linux assembly]]></category>
		<category><![CDATA[passing arguments assembly]]></category>
		<category><![CDATA[rax return]]></category>
		<category><![CDATA[system v abi]]></category>
		<category><![CDATA[ubuntu assembly]]></category>
		<category><![CDATA[writing functions assembly]]></category>
		<category><![CDATA[x64 assembly]]></category>
		<category><![CDATA[x86-64]]></category>
		<category><![CDATA[xmm registers]]></category>
		<category><![CDATA[xmm0 return]]></category>
		<category><![CDATA[yasm]]></category>
		<category><![CDATA[Yasm tutorial]]></category>
		<guid isPermaLink="false">https://www.NeuralLantern.com/?p=321</guid>

					<description><![CDATA[<p>Learn how to write callable functions in YASM x86-64 assembly on Ubuntu. Covers passing integers, pointers, and floating-point values, using the correct registers (RDI, RSI, XMM0, etc.), returning values in RAX/XMM0, extern \"C\" for C++ interop, and building a hybrid C++/Assembly program.</p>
<p>The post <a href="https://www.NeuralLantern.com/yasm-x86-64-assembly-functions-tutorial-integers-floats-pointers-abi-c-interoperability/">YASM x86-64 Assembly Functions Tutorial &#8211; Integers, Floats, Pointers, ABI, C++ Interoperability</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 loading="lazy" title="YASM x86-64 Assembly Functions Tutorial - Integers, Floats, Pointers, ABI, C++ Interoperability" width="1380" height="776" src="https://www.youtube.com/embed/fXf3Gpf-fyM?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 complete guide to writing proper functions in YASM x86-64 assembly on Linux. See how to pass integers, pointers &amp; floats, return values, follow the ABI, disable C++ name mangling with extern &#8220;C&#8221;, and call back and forth between C++ and assembly in a real working example.</p>



<p>Great for people moving from NASM/GAS or trying to mix assembly with higher-level code.</p>



<p>00:00 Introduction and Video Overview<br>00:28 What Are Functions in Programming<br>00:56 Why Functions Are Harder in Assembly<br>01:24 Topics Covered in This Video<br>01:59 About the Makefile and Prerequisites<br>02:28 Hybrid C++ and Assembly Program Plan<br>03:01 Using extern &#8220;C&#8221; to Disable Name Mangling<br>04:03 Main Driver Function in C++<br>05:00 Adding a Callable C++ Function for Assembly<br>05:42 Explaining extern &#8220;C&#8221; Placement<br>06:16 Assembly File Skeleton and Data Section<br>06:41 Creating Null-Terminated Strings<br>07:49 Section .text and External Symbols<br>08:52 Declaring my_cpp_function as extern<br>09:20 Defining my_assembly_function<br>09:40 Labels vs Real Functions<br>10:31 The call Instruction and Return Address<br>11:16 Why Jumping Instead of Calling Crashes<br>11:47 Global Directive for Exporting Functions<br>12:32 Basic Function Structure<br>13:20 Implementing my_assembly_function Prologue<br>14:50 Receiving Arguments in ABI Registers<br>16:30 Printing Received Integer Arguments<br>18:10 Handling Pointer Arguments (C Strings)<br>19:40 Passing Floating-Point Arguments in XMM<br>21:15 Printing Floats from Assembly<br>23:00 Calling Back to C++ Function<br>25:40 Preparing Arguments for my_cpp_function<br>27:20 Loading XMM0 and XMM1 for Floats<br>29:10 Making the Call to C++ Function<br>30:50 Receiving Double Return Value in XMM0<br>32:30 Saving Returned Float to Memory<br>34:10 Printing the Returned Value<br>36:00 Final Messages and Program Flow<br>38:20 Fixing String Pointer Crash Issue<br>40:00 Correcting Argument Loading<br>42:10 Passing String Owned by Assembly<br>44:00 Observing Successful Output<br>45:47 Saving and Restoring XMM0 Safely<br>47:14 Printing Final Returned Float<br>48:32 Importance of Following the ABI<br>50:29 Summary of Covered Topics<br>51:03 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.</p>



<p>In this video I&#8217;m going to teach you how to write functions in YASM x86-64 assembly inside</p>



<p>of Ubuntu, although probably any YASM assembler will be fine.</p>



<p>I&#8217;ve covered these topics before in other videos, but I thought it would be nice to</p>



<p>put them all here in one single video to make it a little bit easier to understand.</p>



<p>Okay, so first off, what the heck am I talking about?</p>



<p>Well, you know, when you have a program, let&#8217;s say we have a higher level language program</p>



<p>and we have like, you know, void or actually probably not void main, let&#8217;s say int main</p>



<p>and we&#8217;ll forget about the arguments and inside of main, it just sort of calls f and then</p>



<p>we have an f function over here and it, you know, does stuff, right?</p>



<p>So that&#8217;s the basic idea of having a function that you can call and calling it.</p>



<p>We sort of take the whole process for granted in higher level languages, but in assembly,</p>



<p>in assembly we have to do a lot more from scratch so I&#8217;m going to show you about how to pass</p>



<p>arguments how to pass integer arguments pointer arguments floating point arguments return types</p>



<p>you know double like floating return types integer return types pointer return types</p>



<p>how to use special registers to actually pass the arguments into the functions how to write a</p>



<p>so that&#8217;s basically what we&#8217;re going to do before before i get any further here i just want to point</p>



<p>out that um this video is not about the basics of assembly nor is it about the basics of um</p>



<p>make files or some of the other related technologies that i&#8217;m going to show</p>



<p>i&#8217;ve covered everything in this video in a previous video um so if you if you find yourself</p>



<p>getting lost and you don&#8217;t understand what i&#8217;m saying or what i&#8217;m doing you probably want to</p>



<p>look at my previous videos because this is sort of a summary video to kind of help you</p>



<p>of help you help you really lock down on the concepts of writing functions okay so for starters</p>



<p>this right here that you&#8217;re looking at is a make file this is not a make file video if you want to</p>



<p>learn how to make make files or why they&#8217;re awesome see my other videos but i&#8217;m just going to assume</p>



<p>at this point you&#8217;re either willing to research my other video or you already know or you don&#8217;t care</p>



<p>how to make a make file so you know we&#8217;re just going to skip this i&#8217;m going to close it</p>



<p>function or sorry a driver module and what I&#8217;m going to do is I&#8217;m going to write this program</p>



<p>that I&#8217;m showing you now as a as a hybrid program a program with modules of different languages so</p>



<p>this module right here is going to be c++ and then we&#8217;re going to have an assembly module and</p>



<p>then they&#8217;re going to be able to call each other so the first thing that I&#8217;m going to do is I&#8217;m</p>



<p>going to name some functions that are going to be inside of my assembly module and also name some</p>



<p>module and also name some functions that are going to well name a function that&#8217;s</p>



<p>going to be just inside of the C++ module but it should be callable by the</p>



<p>assembly module so for that we use a little block called extern C and long</p>



<p>story short that disables name mangling which C++ does in order to provide</p>



<p>overloading functionality so we&#8217;re basically going to disable overload</p>



<p>functionality in order to have simple function names that way assembly can</p>



<p>and also when we attempt to call a function inside of assembly we&#8217;ll just call it by its</p>



<p>simple name rather than try to mangle the name based on the the arguments and return type and</p>



<p>such so that that&#8217;s something we have to do for compatibility at least for this video and I&#8217;m just</p>



<p>going to move on so basically you just take you just take the keyword extern and you put a c in</p>



<p>a quote and then you make a block right a little braces scope and then you just list prototypes of</p>



<p>types of either the functions inside of the current module which you would like to demangle</p>



<p>or other modules functions that you would like to be able to call in a demangled or</p>



<p>I guess a non-mangled way.</p>



<p>Mangled meaning name mangling.</p>



<p>Okay, so then I&#8217;m going to copy paste the main function that I&#8217;m going to do.</p>



<p>So this is a hybrid program, which means you know there are several different modules of</p>



<p>different languages.</p>



<p>driver which just launches the program and it contains like the entry point</p>



<p>like see how there&#8217;s like the the main entry point which if you know how to do</p>



<p>other languages this is usually where you start in C++ or like the GCC</p>



<p>libraries so this is the entry point for our whole program and then once we&#8217;re</p>



<p>inside of here we&#8217;re just kind of print out a message to the user we&#8217;re gonna</p>



<p>make a C string and then we&#8217;re gonna call on the assembly function we&#8217;re</p>



<p>gonna give it you know some numbers some integers and some floats and we&#8217;re</p>



<p>gonna give it our C string and then when we&#8217;re inside of the assembly</p>



<p>and then when we&#8217;re inside of the assembly function we&#8217;re just going to print those things to prove</p>



<p>that we know how to call and we know how to receive data from c or c plus plus into assembly</p>



<p>and then we&#8217;re just going to print you know goodbye basically okay so uh i&#8217;m going to make</p>



<p>another uh let&#8217;s see another function in the driver so that assembly has something to call</p>



<p>because i don&#8217;t just want to be able to call assembly from c i also want to be able to call</p>



<p>put in something that we can call on from assembly here.</p>



<p>I&#8217;m going to call it my CPP function.</p>



<p>And it&#8217;s just going to have like some arguments,</p>



<p>just some nonsense arguments.</p>



<p>And all it&#8217;s going to do is just print out that it&#8217;s entering,</p>



<p>print out the arguments and then just sort of say goodbye.</p>



<p>And then just return a value to the caller.</p>



<p>We&#8217;re going to return a floating point value to the call.</p>



<p>And we&#8217;re going to return a floating point value to the caller.</p>



<p>floating point value to the color okay so notice how at the top here i have uh in the extern c my</p>



<p>cpp function it&#8217;s the same thing that we see at the top is now at the bottom and that just means</p>



<p>uh this normally would be compiled and linked with name mangling and so because it&#8217;s in the</p>



<p>extern c block there&#8217;s no name mangling and the reason that works with an extern c block is</p>



<p>because there&#8217;s no name mangling in c that&#8217;s sort of like a c plus plus thing with overloads and like</p>



<p>whatever okay so we have like the basics for our driver it&#8217;s a simple module the hard work is going</p>



<p>to be inside of assembly so uh maybe before we write assembly oh you know what let&#8217;s write the</p>



<p>skeleton of the assembly and then i&#8217;ll start explaining uh what it means to call back and</p>



<p>forth and to make functions and things like that okay so for starters i&#8217;m just going to make a data</p>



<p>section with a bunch of stuff inside of it and i&#8217;ll explain it just very briefly again this video is</p>



<p>basics of assembly in Yasm. If you want to learn the basics, go see my other videos.</p>



<p>But for now, I&#8217;ll just assume that you understand why we&#8217;re making variables in the data section</p>



<p>and kind of how to do it. So I&#8217;m just, you can see here, I&#8217;m making a bunch of null terminated</p>



<p>strings. They&#8217;re null because they have a zero at the end. And I&#8217;m just saying, well, here&#8217;s a</p>



<p>message when we begin the program. Here&#8217;s a message to announce we&#8217;re about to print some integers,</p>



<p>to print a C string, to print some floats. Here&#8217;s another message for this, another message for</p>



<p>messages right then a crlf that&#8217;s just you know taking the cursor down to the</p>



<p>next line and then back to the very beginning so it&#8217;s like a basically a new</p>



<p>line and then I&#8217;m gonna have some floating point numbers and I&#8217;m gonna</p>



<p>have an a and a B number and then I&#8217;m gonna have a B and a D that I&#8217;ll send</p>



<p>somewhere else and so I&#8217;m gonna populate them with numbers from the start the B</p>



<p>and the D that just are not being sent they start at zero because we&#8217;re gonna</p>



<p>we&#8217;re going to receive those numbers from C and then we&#8217;re going to get a return value as a</p>



<p>floating point from the C function that we call so we&#8217;re just going to store it there so I&#8217;m just</p>



<p>like making a bunch of room to store some from some variables and then here we&#8217;re just going</p>



<p>to do system right and we&#8217;re going to send to standard output that&#8217;s covered in other videos</p>



<p>but basically we&#8217;re just going to be printing stuff okay so section text now this is where</p>



<p>actually starts. Well, sort of. This is where the program contains instructions to execute.</p>



<p>So section text. And the first thing I&#8217;m going to do is I&#8217;m going to name some external symbols</p>



<p>that I have for myself just to help me print integers and floats. So, you know, if you&#8217;ve</p>



<p>seen my other videos, you know that you can do a wide variety of things to sort of print characters</p>



<p>and numbers if you were clever. And you can also call on C library functions like printf.</p>



<p>functions like printf if you really wanted to print floating point numbers</p>



<p>and signed integers and stuff so you don&#8217;t have access to this library so</p>



<p>don&#8217;t worry about putting this part in your code if you&#8217;re working at this from</p>



<p>home unless you&#8217;re someone that has taken a class that I&#8217;ve taught somewhere</p>



<p>and for now just just assume that this just lets me print very easily no</p>



<p>problem you don&#8217;t really need that if you&#8217;re just kind of experimenting with</p>



<p>function this is the name of the function that we made in the driver my cpp function notice how it&#8217;s</p>



<p>over here my cpp function we&#8217;re just letting our assembly module know that there&#8217;s a function in</p>



<p>another module that we should be able to call on even though all these modules are going to be</p>



<p>linked into the same program we still have to do this just to let the assembly module know</p>



<p>okay so now we need to make a function called my assembly function if you look at the top right here</p>



<p>We had this other D name mingled prototype, my assembly function.</p>



<p>So we have to actually write that somewhere.</p>



<p>It&#8217;s not in the driver.</p>



<p>Let&#8217;s write it inside of assembly.</p>



<p>So my assembly function is going to start right here.</p>



<p>Boom, right there.</p>



<p>So this is when we kind of start on the basics of writing a function.</p>



<p>So for starters, hopefully you understand labels.</p>



<p>If you don&#8217;t understand labels by now, you might want to see my other videos.</p>



<p>just some sort of an alphanumeric symbol that you just kind of write as sort of</p>



<p>sort of like a variable name.</p>



<p>You can&#8217;t use every symbol available.</p>



<p>I would just start with using letters and numbers in the underscore.</p>



<p>Of course, you can always experiment if you want to see what&#8217;s allowed and what&#8217;s</p>



<p>not just type something and see if it compiles.</p>



<p>So the label starts with just, you know, this name and then a colon, right?</p>



<p>So that&#8217;s a label.</p>



<p>You can jump into labels.</p>



<p>Like if we were somewhere else in the program right now, we could like, if we</p>



<p>we could like if we wanted to do an infinite loop we could say jump and then the name of the label</p>



<p>and what would happen is execution would go back up to this label and then it would jump again</p>



<p>it would go back up it would be an infinite loop but we wouldn&#8217;t technically be calling a function</p>



<p>we&#8217;re just sort of jumping so labels are sort of the first step we have to do a little bit more to</p>



<p>actually make a function so we have an assembly function which right now is just a label and in</p>



<p>side we have to mark it as global so remember when we marked my CPP function</p>



<p>as extern that means we can call a function that lives somewhere else now</p>



<p>we&#8217;re marking this function as global which means it lives here but we want</p>



<p>other modules to be able to call on our function or our label okay so how do we</p>



<p>actually make a function let&#8217;s see the first thing that you should do is try to</p>



<p>understand that if that all functions return even if they don&#8217;t return any any</p>



<p>Under the hood, what this is actually going to do is crash the program if you just jumped</p>



<p>into this label and didn&#8217;t call it like a function, because the return instruction will</p>



<p>look onto the call stack and try to find a return address for wherever this function</p>



<p>was called from and then go jump, do a jump instruction to return there.</p>



<p>And so if you just jumped into this label and then there&#8217;s a return, it&#8217;s going to pop</p>



<p>something up the stack that&#8217;s not actually a return address and probably crash the program.</p>



<p>So that means now that we have a return statement, we definitely have to call this like a function.</p>



<p>And I guess that&#8217;s pretty easy because, well, if we were inside of assembly and we wanted to call another function,</p>



<p>let me just show you real fast.</p>



<p>I&#8217;ll say some other function.</p>



<p>If we were inside of this label, for some reason, we could call the above function by just saying call.</p>



<p>And then the label.</p>



<p>So now it&#8217;s being treated like a function.</p>



<p>it&#8217;s being treated like a function.</p>



<p>What the call instruction really does is it just looks at the address of the next instruction</p>



<p>that would have been executed.</p>



<p>So, you know, whatever, let&#8217;s say we have like a nope on the next line, whatever the</p>



<p>memory location or the relative memory location is of that nope instruction, it&#8217;s going to</p>



<p>push that onto the call stack.</p>



<p>And then it&#8217;s going to just do a jump instruction to this label.</p>



<p>So then later when we hit this return statement, then it&#8217;s going to look onto the call stack</p>



<p>and it&#8217;s going to find that return address, which is going to correspond with the nope</p>



<p>instruction and the return instruction is going to do a jump back to that address.</p>



<p>So basically you have to use the call instruction to get there and then use a return instruction</p>



<p>to return from there.</p>



<p>And then now we can actually call it a function.</p>



<p>But then there&#8217;s other things that functions have to do to behave themselves without crashing</p>



<p>the program.</p>



<p>So for example, notice up here that I have copy pasted the let&#8217;s see call on assembly.</p>



<p>Oh, I think I forgot to update that.</p>



<p>I forgot to update that. Let&#8217;s see. Call on this. Yeah. It&#8217;s supposed to be called my assembly</p>



<p>function now. Hold on. Let me update my, uh, my solution. Whoops. Hold on. Call on.</p>



<p>There we go. My assembly. Okay. So I&#8217;ve got my solution up above on another monitor.</p>



<p>So I have the prototype of the function here. Uh, it&#8217;s just going to return nothing. And it&#8217;s</p>



<p>going to like take in a long and a double and a long and a double and a character pointer.</p>



<p>And then my intention here is to save these arguments with some registers.</p>



<p>So notice how I&#8217;m saying we&#8217;re going to use R12, 13, and 14 to save the arguments A, C, and E.</p>



<p>And we&#8217;ll talk about why we&#8217;re not saving the other ones with those registers in a second.</p>



<p>But basically we&#8217;re going to use those registers.</p>



<p>So the thing about the registers is we have to do something called respecting the ABI.</p>



<p>Respect the ABI.</p>



<p>The ABI stands for the Application Binary Interface.</p>



<p>interface and it&#8217;s just sort of like a standard that governs all of the things that you can do</p>



<p>and you&#8217;re supposed to do when you&#8217;re working with x86 64 assembly so the abi is pretty cool</p>



<p>hang on a second the abi is pretty cool because it actually standardizes things</p>



<p>for instance if we didn&#8217;t have the abi and we didn&#8217;t respect it then we couldn&#8217;t actually call</p>



<p>plus or vice versa because c plus plus the higher level languages they&#8217;re going to use the abi so if</p>



<p>you try to do things your own way then all you&#8217;re going to end up doing is wasting time and energy</p>



<p>coming up with two different ways of doing things when you could have just done it the abi way</p>



<p>right so and one of those and one of those ways your way wouldn&#8217;t even work uh for cross you know</p>



<p>module calling it would only work internally to your own program so we&#8217;re going to respect the abi</p>



<p>does the abi say about these registers and again i&#8217;ve talked about this extensively in other videos</p>



<p>but let me pull up a fun book that i love to uh to talk about uh so this book right here i did not</p>



<p>write it uh the book here is written by a really wonderful uh professor dr ed jorgensen phd and um</p>



<p>this book is totally free it&#8217;s not i&#8217;m not selling you anything uh this is literally you can just go</p>



<p>to this this professor&#8217;s website and download his book for free and he&#8217;s already given me permission</p>



<p>and he&#8217;s already given me permission to just tell everybody to just share it with everybody if you</p>



<p>if you look in the license area it&#8217;s a copyleft license so it&#8217;s sort of like the spirit of open</p>



<p>source just sort of like sharing knowledge and stuff so it&#8217;s awesome so I suggest everybody you</p>



<p>know follow the link that I hopefully put in the video and and grab a copy of this book but</p>



<p>basically what I&#8217;m going to do is I&#8217;m going to go to a special area and I&#8217;m going to search for</p>



<p>here and so that would be I guess section 12.8.2 and it&#8217;s called register usage subsection 12.8.2</p>



<p>notice how it lists all of the registers and kind of how they are typically used</p>



<p>this falls under the scope of the abi so this is not all the abi is but this is one of the</p>



<p>things you&#8217;re supposed to do when you&#8217;re respecting the abi is you&#8217;re supposed to sort of like</p>



<p>are supposed to be used. Notice R12, which is one of the registers that we&#8217;re going to use,</p>



<p>is designated as callee saved. So that means whoever is being called has to preserve that</p>



<p>register if they intend to mess it up. So for example, if I just had this function here,</p>



<p>move some value into, you know, R12, I have now basically broken the program. If any, you know,</p>



<p>C function or other library that you didn&#8217;t write, just any anywhere else calls on this function,</p>



<p>anywhere else calls on this function then their version of r12 is going to get destroyed by what</p>



<p>i just did and so i&#8217;m not respecting the abi when this function returns to the caller they&#8217;re going</p>



<p>to expect that their original data was intact and if it&#8217;s not program is not going to work it&#8217;s going</p>



<p>to screw up so respecting the ai means you have to preserve any registered mark as callee saved or</p>



<p>that&#8217;s one of the things that it means so notice how r12 through r15 are callee saved so we have</p>



<p>the abi now and sort of preserve 12 13 and 14. we can do that pretty easily with some push and pop</p>



<p>statements so i&#8217;m going to do push r12 push r13 push r14 and now what happens is those values</p>



<p>are actually on the stack now and i can retrieve them later at the end of the function even if i</p>



<p>destroy them while i&#8217;m inside of the function i&#8217;ll just restore them right before i return so</p>



<p>because it happens before the function does anything.</p>



<p>And then we have to make sure that we pop those values</p>



<p>because you have to be careful with the stack.</p>



<p>If you just start pushing values</p>



<p>and you don&#8217;t restore the stack to its original state</p>



<p>by the time you return,</p>



<p>you&#8217;ve basically broken the program.</p>



<p>It&#8217;s either going to crash right away</p>



<p>or whoever called it is not going to function correctly anymore.</p>



<p>So let&#8217;s do some pops.</p>



<p>We&#8217;re going to do three pops</p>



<p>and we&#8217;ll call this the epilogue,</p>



<p>which means uh you know something we do right before we exit the function and keep in mind</p>



<p>that the order of the pops should be reverse of the order of the pushes notice how we&#8217;re popping</p>



<p>in reverse order from what i did before so we&#8217;re going to pop uh 14 13 and 12 where as we pushed</p>



<p>12 13 and 14 before if you pop in the wrong order like if you try to do it in in the same order as</p>



<p>the pushes then uh you&#8217;re still going to end up destroying data for the caller because you&#8217;re</p>



<p>because you&#8217;re going to be restoring data to the wrong registers.</p>



<p>So just keep that in mind.</p>



<p>OK, so now the ABI is being respected.</p>



<p>Let me see, by the way, do we have enough to actually even run the driver right now?</p>



<p>We&#8217;re just calling it and then it didn&#8217;t really do anything.</p>



<p>Yeah, I think we could probably this might compile.</p>



<p>OK, let me let me just check this out.</p>



<p>I want to say clear and make run.</p>



<p>Yeah, it compiles.</p>



<p>OK, so the driver printed its hello message.</p>



<p>again it&#8217;s just sort of saying like hello from the driver and then when the driver comes</p>



<p>back it says the driver regained control and nothing really happened because well we didn&#8217;t</p>



<p>do anything in our assembly function yet but at least we&#8217;re calling multiple modules and again</p>



<p>if you want to know how to do hybrid programs and linking and compiling and all that stuff</p>



<p>see my other videos for now I&#8217;m just going to move on so the next thing we have to do is we have to</p>



<p>have to try to understand like how are we going to receive these arguments so this is one of the</p>



<p>other you know building blocks to making functions notice how the assembly function i&#8217;m sending in an</p>



<p>integer and then a float and then another integer and then another floats i&#8217;m kind of mixing the</p>



<p>arguments then at the very end i&#8217;m sending in a pointer if i look back up at the prototype here</p>



<p>which matches what i&#8217;ve what how i&#8217;ve used it it&#8217;s a long a double a long a double and a character</p>



<p>longs and doubles, they actually, they&#8217;re called mixed arguments and they don&#8217;t actually count</p>



<p>against each other when you&#8217;re looking at the order of the registers to stuff them into.</p>



<p>So for example, let me show you here. We have, what did I just do? I clicked on the wrong</p>



<p>computer. Okay. I&#8217;m on the wrong computer. Okay. Let me close this real fast. So</p>



<p>When you think about registers for incoming arguments, you basically start to think about this.</p>



<p>RDI and RSI, those registers represent the first and the second integer arguments.</p>



<p>If we look back to the book real fast, we can see in that same section, RDI is the first argument,</p>



<p>and then RSI is the second argument, and then RDX is the third, and then the fourth.</p>



<p>We can do up to six arguments with R9, and then after that, we have to start pushing arguments to the stack.</p>



<p>stack i&#8217;m not going to go that far in this video i&#8217;ve actually done that already in a previous video</p>



<p>but basically we for for now we can just use six registers to push arguments but if you think</p>



<p>about it these registers these are not floating point registers these are general purpose registers</p>



<p>they&#8217;re meant for integers and pointers the reason they&#8217;re used for in it for pointers also is because</p>



<p>a pointer is just an integer a pointer is just a 64-bit integer unsigned which represents a memory</p>



<p>can use these registers only for integers and pointers but we can&#8217;t use them for floats so that</p>



<p>means the first integer argument is going to be RDI and the second integer argument is going to be</p>



<p>RSI but if there was a float argument in between then RSI would still be the second integer argument</p>



<p>the floats don&#8217;t count against the integers and vice versa so for example if we&#8217;re talking about</p>



<p>float registers you know the first one available is XMM0 and then we have XMM1 and then we have</p>



<p>we have XMM1 and we have XMM2 and it goes all the way up to I think XMM15 so we have 16 floating</p>



<p>point registers. So we could pass in 16 floating point arguments just using these registers if we</p>



<p>wanted to and then if we want to do even more than that we probably have to get you know funky with</p>



<p>the stack or something or maybe hopefully you just have an array somewhere and you&#8217;re just going to</p>



<p>pass in a pointer. But the way you have to think of these arguments is that even though they might</p>



<p>they might be mixed in the prototype of the function that you&#8217;re calling from a higher level</p>



<p>language you shouldn&#8217;t think of them as being mixed when you&#8217;re actually loading up registers so</p>



<p>again if we just kind of like go back here to this prototype</p>



<p>notice how that a variable we&#8217;re going to say that the a is rdi because a is along it&#8217;s an integer</p>



<p>but then right after that there&#8217;s a double b we would not skip rsi or assign b to rsi</p>



<p>b would just be the first float argument and then when we go back to long c long c is actually the</p>



<p>second integer argument because that double doesn&#8217;t count against the integer so it&#8217;s going to be c</p>



<p>is going to be rsi and then uh for double d uh same thing uh we&#8217;re not going to skip xmm1 just</p>



<p>because there was a an integer we&#8217;re going to go straight to saying that xmm1 is the second float</p>



<p>so it&#8217;s the d then for the last argument that we have i&#8217;m just going to erase this stuff down here</p>



<p>it was going to be e and remember pointers are integers they&#8217;re just unsigned uh 64-bit integers</p>



<p>we have to go on to the next one which i think i recall is like rdx let me just double check</p>



<p>i don&#8217;t want to say this wrong rdx yeah the third argument so i&#8217;m going to do rdx and now</p>



<p>that we&#8217;ve kind of like mapped this out we we know now what what registers we should be looking for</p>



<p>we should be looking for when the function comes in in order to receive</p>



<p>our data we should look at those registers for those variables another</p>



<p>thing to keep in mind by the way is that usually when we return something in</p>



<p>assembly we will move a value into RAX right like some value if you&#8217;ve been</p>



<p>following my assembly videos so far but that only counts if you want to return</p>



<p>an integer or a pointer if you instead wanted to return a floating point number</p>



<p>use XMM zero and I&#8217;ll just you&#8217;re not allowed to you&#8217;re not allowed to hard code a floating</p>



<p>point number in assembly like this or at least in the ASM so we&#8217;ll just pretend that there&#8217;s</p>



<p>like a float somewhere and I&#8217;ll just load it from memory and I&#8217;ll say like the float</p>



<p>something like that so notice how we&#8217;re using a different instruction we&#8217;re not using the regular</p>



<p>move instruction that works with integers we&#8217;re instead using the floating point version we&#8217;re</p>



<p>saying let&#8217;s move a single piece of data and let&#8217;s move a double precision floating point number</p>



<p>xmm zero and then we&#8217;ll just grab from memory whatever whatever that variable has we&#8217;ll just</p>



<p>take that floating point number and stick it into xmm zero so uh if you want to return an integer</p>



<p>or float you use rax if you want to re sorry if you want to return an integer or a pointer</p>



<p>you use rax if you want to return a float you use xmm zero you shouldn&#8217;t do that at the same time</p>



<p>if you have like two assembly functions calling each other you might be tempted to do that and i</p>



<p>you know very standard and it wouldn&#8217;t work very well with other people&#8217;s code or library code or</p>



<p>higher level language code so only one or the other and it just has to match your prototype</p>



<p>so notice how here inside of my cpp function notice how it&#8217;s going to return a double</p>



<p>right so when the assembly module is done calling on this function it should expect xmm0 to be</p>



<p>loaded up with that double precision loading point number okay so that&#8217;s the basic idea</p>



<p>Okay, so that&#8217;s the basic idea.</p>



<p>So now let&#8217;s maybe let me pin this.</p>



<p>Let&#8217;s kind of fill this out a little bit more.</p>



<p>So the first thing that we should do</p>



<p>is we should save our integer or pointer arguments.</p>



<p>So I&#8217;m gonna leave the respect ABI thing there.</p>



<p>And notice how I&#8217;m just looking at RDI and RSI and RDX.</p>



<p>And well, I guess, you know,</p>



<p>we have it in a comment up here, A, C and E,</p>



<p>but I&#8217;ll just make another comment here.</p>



<p>here I&#8217;ll say like a and a or sorry a C and E and maybe we&#8217;ll specify the data types for fun</p>



<p>it&#8217;s going to be long a and then long C and then the character pointer C even if this was a float</p>



<p>pointer it would still be an integer because all pointers are integers no matter what they&#8217;re</p>



<p>pointing to so just keep that in mind anyway so we&#8217;re going to save r12 r13 and 14 with the</p>



<p>14 with the incoming arguments and the reason we want to save those right away is because rdi rsi</p>



<p>and rdx those are not callee saved which means the moment we call in another function they&#8217;ll</p>



<p>possibly be destroyed because we don&#8217;t really know what&#8217;s going on in other functions that we might</p>



<p>call so it&#8217;s a good idea to just kind of save right away either to a global or the stack or</p>



<p>in this case just registers being faster okay so we have that now we have to save our float arguments</p>



<p>b and d were xmm0 and xmm1 so I&#8217;m just going to save both of those</p>



<p>and if you&#8217;re wondering what this float b and float d are that&#8217;s just</p>



<p>you know up here I just have a global variable so I can save them to memory</p>



<p>easily and not worry about the stack this is not a stack video so much</p>



<p>but yeah okay so I&#8217;m just saving all of the incoming arguments that&#8217;s all I&#8217;ve</p>



<p>done so far and let&#8217;s see it should probably still</p>



<p>Let me see if this works.</p>



<p>I&#8217;m going to go make run.</p>



<p>Okay.</p>



<p>So nothing happens, but it at least worked.</p>



<p>So I&#8217;m going to same as other windows for that so that we don&#8217;t have to look at it anymore.</p>



<p>And now that we&#8217;re done saving our float arguments, let&#8217;s, let&#8217;s print a welcome message.</p>



<p>So I&#8217;m going to do, you know, welcome.</p>



<p>And I&#8217;m going to use a special function that I&#8217;ve made in previous videos called print</p>



<p>null terminated string, which means I should probably copy paste that into this program</p>



<p>now.</p>



<p>What is print null terminated string?</p>



<p>It&#8217;s just another convenience function that I wrote.</p>



<p>I&#8217;m not going to explain it too much because it&#8217;s in other videos and</p>



<p>I&#8217;m already here trying to explain functions to you in general.</p>



<p>So long story short, it takes in a C string and a file handle, you know, to like where</p>



<p>you want to write, like if you want to write to a file or you want to write to standard</p>



<p>output or standard error.</p>



<p>And it just takes those arguments and then it sort of says, all right, how long is the</p>



<p>string?</p>



<p>And it uses another function called string length to figure out how long the string is.</p>



<p>how long the string is and then it just uses a system call to actually print the</p>



<p>string and again system calls are covered in other videos this one looks</p>



<p>really convoluted because it&#8217;s like you know customized for this function but</p>



<p>just trust me on this this prints a string next function I got a paste in</p>



<p>real fast again explained in other videos is the string length function so</p>



<p>all this does is it just takes a pointer to a string and it sort of scans the</p>



<p>string is and as soon as it sees a zero like a null terminator then it just says all right that&#8217;s</p>



<p>that&#8217;s the end of the string and it&#8217;ll just return the length to the caller so that&#8217;s all you need to</p>



<p>know about this covered in other videos then i&#8217;m going to make a convenience function here called</p>



<p>crlf and all that&#8217;s going to do is just print the new line that we talked about earlier so just</p>



<p>just a bunch of convenience functions on top of the real part of the program so now that we have</p>



<p>the convenience functions in there we should be able to see the welcome message let me just double</p>



<p>yeah now inside assembly entry point oh what did I do wrong now inside I keep forgetting to change</p>



<p>the strings on this now inside where&#8217;s that now inside my assembly function okay let me change</p>



<p>that in my solution to you know I&#8217;m just kind of like writing these things and I&#8217;m having fun and</p>



<p>I keep changing my mind about what they should be named and then I I get some inconsistencies okay</p>



<p>all right so then uh we printed the welcome message and now let&#8217;s print the integer arguments</p>



<p>but first let&#8217;s print a little introduction uh to the integers let&#8217;s just say hey we&#8217;re about</p>



<p>to print the integer so that&#8217;s just this other string a message saying we&#8217;re about to print the</p>



<p>integers nothing really that complicated so far it says now we&#8217;re printing all the integer arguments</p>



<p>so now we can actually print the integer arguments so we have two integer arguments</p>



<p>if you recall let&#8217;s see yeah we had like a and c those were integers I&#8217;m not going to talk about</p>



<p>e right now because that was a pointer but you know right now we&#8217;re just saying a and c so that</p>



<p>was r12 and r13 so I&#8217;m just going to paste some code here to actually print those and</p>



<p>talk about this library in other videos but basically I&#8217;m moving r12 which is the first</p>



<p>integer argument that functions typically receive and once that&#8217;s loaded up i&#8217;m just going to call</p>



<p>on my special function to just print the integer and again you could use printf from the c libraries</p>



<p>if you actually wanted to print it and not just experiment and stuff like that so i&#8217;m going to use</p>



<p>r12 and r13 and i&#8217;m just going to print both of those integer arguments and then after each one</p>



<p>is printed notice i&#8217;m calling the crlf function which is just the convenience function of just</p>



<p>which is just the convenience function of just like doing a new line.</p>



<p>So now we should see two numbers. Yeah.</p>



<p>Now printing all integer arguments,</p>



<p>we&#8217;ve got an 88 there and then that other giant number there.</p>



<p>Let&#8217;s just double check that that&#8217;s actually what we&#8217;re supposed to be seeing.</p>



<p>So I&#8217;m going to do this and I&#8217;m going to say,</p>



<p>the driver called the assembly module with these numbers.</p>



<p>It gave it an 88 and then for the next integer,</p>



<p>it gave it the 287 giant number. So great.</p>



<p>so great we&#8217;re printing the integers now let&#8217;s print the floats so we should see</p>



<p>like a 99 point something in a 32 point something next okay so let&#8217;s continue</p>



<p>with printing oh sorry actually let&#8217;s print the the C string because that&#8217;s a</p>



<p>pointer that&#8217;s still more closely related than the floats so the first</p>



<p>thing I&#8217;m going to do is I&#8217;m going to</p>



<p>print the received c string how about like announce that we will print the c string because</p>



<p>that&#8217;s what we&#8217;re doing right here let me change my solution to match and then we&#8217;ll uh we&#8217;ll</p>



<p>actually print the received c string next so same stuff as before first we call print null terminated</p>



<p>string to print out a little welcome message or just like an intro message like we are going to</p>



<p>print the c string and then we&#8217;ll use that print function again but we&#8217;ll give it the c string so</p>



<p>it the c string so it just prints the whole c string out and this should prove to you that we</p>



<p>are indeed receiving a pointer to some data owned by the c plus plus module so if we run this real</p>



<p>fast it should just tell us two more things it&#8217;ll give us the announcement now printing the received</p>



<p>c string and then on that same line it says hello this is the c string owned by main and if we just</p>



<p>that&#8217;s exactly what string is inside of that variable so hello this is a c string owned by</p>



<p>main and we gave it to the function by just kind of passing it in and we know that character arrays</p>



<p>are basically character pointers or any array is just a pointer to the first item in the array so</p>



<p>my c string is really a pointer to that h character so if we pass that in then a pointer</p>



<p>based print function should be able to work and that&#8217;s what happened okay so we&#8217;ve done that</p>



<p>So we&#8217;ve done that.</p>



<p>And then the next thing we should do is let&#8217;s print the floats.</p>



<p>So first let&#8217;s announce that we&#8217;re going to print the floats.</p>



<p>Same thing we&#8217;d before we&#8217;re just printing like an announcement message.</p>



<p>If we run the program again, it&#8217;s just like now printing the floats, but it doesn&#8217;t actually</p>



<p>do anything.</p>



<p>So then the next step is let&#8217;s, let&#8217;s grab the first float into XMM zero.</p>



<p>And then let&#8217;s call a function to print it.</p>



<p>so right here we have like that 99 number that we expected from before by</p>



<p>the way so why am I doing it this way why am I not just keeping XMM zero</p>



<p>because you remember before we had XMM zero had that had the float that we</p>



<p>received and then we&#8217;re using it again down here but remember XMM zero and all</p>



<p>the other float registers they&#8217;re not designated as callee saved which means</p>



<p>the moment we call any other function we should expect that that data has been</p>



<p>so I can&#8217;t actually count on XMM 0 surviving just this little simple function instead I have to save</p>



<p>it somewhere to the stack to memory you know whatever so I&#8217;m just that&#8217;s why I put that into</p>



<p>a global variable so it&#8217;s sitting in float underscore B right now and then we saved it at</p>



<p>the beginning to float underscore B and if you just kind of look up to the data area well it was</p>



<p>just float underscore B was just a little quad word you know eight bytes of memory that can hold</p>



<p>hold our float. So we have like float allocations for B and D, the first and second float arguments.</p>



<p>So we&#8217;re saving it there. And then we&#8217;re recalling it here. And remember, the first function argument</p>



<p>is going to be XMM0, regardless of where that data originally came from. So if we look at the next</p>



<p>one here, if we kind of like, let&#8217;s see, copy paste this, and we want to grab like the D float,</p>



<p>still going to load it into XMM zero because right now it&#8217;s not about what we originally</p>



<p>received as an argument.</p>



<p>It&#8217;s what this function expects as an argument.</p>



<p>This function only takes one argument.</p>



<p>It just wants a float so that it can print it and that&#8217;s it.</p>



<p>So both times we&#8217;re going to load it up into XMM zero and then we&#8217;re going to print a new</p>



<p>line.</p>



<p>Okay.</p>



<p>So let me just run the program one more time and we should now see we&#8217;ve got two floats</p>



<p>and they should match what the driver tried to send in.</p>



<p>Right?</p>



<p>99, that&#8217;s the first one.</p>



<p>And then 32 point something, that&#8217;s the second one.</p>



<p>So cool, we have received integers and pointers and floats.</p>



<p>We&#8217;ve recalled them and then we&#8217;ve printed them.</p>



<p>Pretty slick, what do you think?</p>



<p>Anyway, so we&#8217;ve done that.</p>



<p>And the next thing that we should probably do is…</p>



<p>Well, at this point, we just maybe have to mess with return types.</p>



<p>Even though I&#8217;ve told you about it, we&#8217;ll just mess with it a little bit.</p>



<p>mess with it a little bit. But let&#8217;s call the C++ module. So what I&#8217;d like to do first is just sort</p>



<p>of announce that we&#8217;re going to call on the C++ module. Again, typical design pattern, let&#8217;s just</p>



<p>print a message saying what we&#8217;re about to do. And if we run this now, it&#8217;s going to say assembly</p>



<p>module will now call on the C++ module. So nothing really too complicated. So now we&#8217;re going to call</p>



<p>on that function. Let me let me paste the name of it here. Actually, I&#8217;m going to put this one right</p>



<p>I want to put this one right here.</p>



<p>It&#8217;s the my CPP function function.</p>



<p>So if we look back at the driver</p>



<p>and look at the signature for my CPP function,</p>



<p>whoops, it has this signature.</p>



<p>And if you wanted to look up higher,</p>



<p>you totally can just look into the name mangling section.</p>



<p>Whoops, the my CPP function, it returns a double.</p>



<p>It takes in a long, a double, a long, a double,</p>



<p>and a character pointer.</p>



<p>Basically the same thing as the other one,</p>



<p>as the other one except it returns a double so what&#8217;s going to happen is when it takes all of</p>



<p>these in it&#8217;s just going to print all of them and then i&#8217;m just going to have it return just kind of</p>



<p>like some random double that i decided to type because this is not an arithmetic video okay so</p>



<p>cpp function and then um now how do we interpret that so if we uh let&#8217;s see let me maybe just for</p>



<p>help this usually helps me when i&#8217;m trying to do this i&#8217;m going to take the prototype and just sort</p>



<p>and just sort of like paste it right where I&#8217;m about to call the function.</p>



<p>So I&#8217;m going to do this just to remind myself of what I&#8217;m actually calling.</p>



<p>Let me add that to my solution, by the way.</p>



<p>Okay.</p>



<p>So we&#8217;re going to call myCPP function,</p>



<p>which means it&#8217;s expecting some registers to be loaded up with arguments.</p>



<p>If we don&#8217;t actually load up anything right now,</p>



<p>it&#8217;ll probably do some sort of nonsense.</p>



<p>Let&#8217;s actually see what happens right now.</p>



<p>If we don&#8217;t load up the appropriate registers,</p>



<p>then C++ will still look at those registers expecting to see valid data.</p>



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



<p>I don&#8217;t know if it&#8217;s going to be good or bad.</p>



<p>This would probably be something called undefined behavior,</p>



<p>meaning you did something wrong,</p>



<p>and probably sometimes your program will work,</p>



<p>and sometimes it won&#8217;t.</p>



<p>Sometimes you won&#8217;t understand what&#8217;s going on.</p>



<p>So I&#8217;m going to do this right now,</p>



<p>and it&#8217;s saying, oh, it&#8217;s segfaulted.</p>



<p>Okay.</p>



<p>Why did it segfault?</p>



<p>all right well I guess maybe because I did something naughty I don&#8217;t know</p>



<p>if this say faults by the end we&#8217;re going to be in trouble I&#8217;ll have to debug on camera</p>



<p>so uh we&#8217;re going to enter into my cpp function and then it says we got a variable a</p>



<p>which was a long we didn&#8217;t give it a one a one was just probably sitting in there</p>



<p>before we even called that function so like one is definitely not it and then b was like some</p>



<p>floating point number and then C was just like another seemingly random value</p>



<p>that kind of looks a little bit more like a pointer I&#8217;m not really sure it&#8217;s</p>



<p>probably not but it&#8217;s just some junk data coming from somewhere and then D</p>



<p>which I think D was supposed to be a character pointer it says not a number</p>



<p>so we just got like a a really bad value for D let me upgrade this real fast</p>



<p>because maybe we should be printing what printing it as a memory location so E is</p>



<p>So E is, let me bring this down real fast.</p>



<p>So E is supposed to be a character pointer.</p>



<p>Let me, instead of printing the C string as just like itself, let&#8217;s first print the memory location.</p>



<p>And I&#8217;ll just say memory.</p>



<p>And then we&#8217;ll do a static cast.</p>



<p>Oh, I wonder if this will actually change anything.</p>



<p>I wonder.</p>



<p>Let&#8217;s static cast both of them just to see what happens.</p>



<p>print e and uh c string here and then it&#8217;s going to be um</p>



<p>hmm no i don&#8217;t think that&#8217;s actually going to change anything</p>



<p>because if i cast it as a point as a character that&#8217;s definitely wrong</p>



<p>and if i don&#8217;t cast it then it&#8217;s going to show up as its original data type how do i get the memory</p>



<p>location hmm oh i know what to do i can static cast it maybe as a as an unsigned long okay</p>



<p>and unsigned long. Okay, so a character pointer, we&#8217;ll leave that for the C string. Unsigned long,</p>



<p>long, just in case. And I think I should see the memory location first and then the actual C</p>



<p>string later. If not, then whatever. Let&#8217;s try one more time. Oh dear. Invalid static cast from</p>



<p>a character pointer to a type long, long unsigned integer. What have I done? How about unsigned long?</p>



<p>long see if that works character pointer long int i guess i forgot how to cast pointers to longs</p>



<p>i&#8217;ll look that up and post another video in the future but i guess for now we&#8217;ll just</p>



<p>we&#8217;ll deal with this humiliation and i&#8217;ll just print e by itself</p>



<p>all right so i&#8217;m just going to run it one more time it should say fault again</p>



<p>is wrong let&#8217;s uh let&#8217;s just fix the arguments so before we make that call we should load up some</p>



<p>stuff right so i&#8217;m going to do maybe like a semicolon comment here and then i&#8217;m going to do</p>



<p>what else do i have i&#8217;ve got three move instructions so the first argument is going to get</p>



<p>77118 the second argument is going to get 1111 and the third argument looks like it&#8217;s going to</p>



<p>get a pointer that i&#8217;ve defined inside of the assembly module so if you look back up at the</p>



<p>look back up at the top let&#8217;s see message string inside asm message string inside asm so basically</p>



<p>i&#8217;m going to be sending a character pointer to this t right here which is just this string says</p>



<p>this string is owned by the assembly module and it&#8217;s a null terminated string which uh c definitely</p>



<p>needs in order to print correctly if we took that zero off we&#8217;d probably get a bunch of junk data</p>



<p>we might crash the program i don&#8217;t know so anyway basically we&#8217;re just going to be saying you know</p>



<p>we&#8217;re just going to be saying, you know, here you go. Here&#8217;s a pointer. Where the heck am I?</p>



<p>Okay. Here&#8217;s a pointer to a C string. And so we&#8217;re giving it one, two, three arguments. We&#8217;re giving</p>



<p>it the A and the C and the E. Now we just have to load it up with the other two floats. So</p>



<p>I&#8217;m going to do this real fast. Remember the first float argument is going to be XMM zero.</p>



<p>And the second one is going to be XMM one. And you can see that send B corresponds to the first</p>



<p>corresponds to the first float argument.</p>



<p>That&#8217;s why it&#8217;s XMM zero.</p>



<p>And then the float send D corresponds</p>



<p>to the second argument.</p>



<p>That&#8217;s why it&#8217;s XMM one.</p>



<p>And that&#8217;s the D right here.</p>



<p>And there&#8217;s no other floats.</p>



<p>And if we just kind of look back up real fast,</p>



<p>the float send B and D,</p>



<p>I just defined those arbitrarily</p>



<p>to just these two random numbers.</p>



<p>So we should see like a two one nine</p>



<p>and then a nine nine eight eight seven seven six</p>



<p>sort of, you know, weird number.</p>



<p>number then when we&#8217;re done loading all those things you know all of these registers up now</p>



<p>the function should be able to receive something i bet you the reason it crashes because we</p>



<p>we had like a bad address for the the string previously because we didn&#8217;t load it</p>



<p>so let me go up here and now it doesn&#8217;t crash so if you just kind of like look we have hard-coded a</p>



<p>and b sorry not not a and b a and also c so that&#8217;s the seven seven one and then the bunch of ones</p>



<p>of ones and then the string which was rdx which was the e so this string is owned by the assembly</p>



<p>module nice we&#8217;re now able to print a string that&#8217;s owned by assembly in another module in</p>



<p>the c plus plus module and then xmm0 and xmm1 those are the two floats so if we look back up</p>



<p>again that&#8217;s uh we should expect to see these two numbers right here whoops um bloat send b and d</p>



<p>and then D. So the printing kind of turned it into scientific notation. That&#8217;s okay. You know,</p>



<p>there&#8217;s other stuff you can do in C++ to just not print in scientific notation, but for now,</p>



<p>I don&#8217;t really care. It&#8217;s basically the same value. So it&#8217;s fine. I just want to show you</p>



<p>how to transport data. This is not a C++ video. Anyway, if we go back down to the end of that,</p>



<p>so we called the function, but then this function, if you notice, it returns a double,</p>



<p>we should expect to see something loaded into the XMM zero register.</p>



<p>So I&#8217;m going to move a single piece of data and it&#8217;s going to be a double precision floating</p>



<p>point number.</p>



<p>And I&#8217;m just going to store it into my float got return value global variable,</p>



<p>which I defined up above.</p>



<p>So let me scroll up real fast.</p>



<p>So where is that?</p>



<p>Where is that?</p>



<p>Float got return value.</p>



<p>I initialized it with a zero just because we&#8217;re going to receive something in there.</p>



<p>I could have used the BSS section if I wanted to, probably would have been a little bit</p>



<p>bit smarter and more performant but i don&#8217;t know i personally don&#8217;t like using the bss section in</p>



<p>yasm unless uh i want to allocate an array if i&#8217;m just doing one variable here and there then i&#8217;m</p>



<p>just going to put it in the globals or or the data section so float got return value i&#8217;m saving it in</p>



<p>there and then uh i want to grab it from xmm zero so that&#8217;s where it comes from when uh my cpp</p>



<p>return the double to us inside of XMM0.</p>



<p>So I&#8217;m just moving that into my data section into the global variable.</p>



<p>And then now that we have finally returned from the C++ module,</p>



<p>I&#8217;m just going to announce that we have indeed returned from the C++ module.</p>



<p>Let&#8217;s get that far real fast.</p>



<p>So this string is owned and then my CPP function exiting.</p>



<p>exiting and then now it says the assembly module received this return value from the</p>



<p>C++ module and then it says the driver has regained control because we were supposed</p>



<p>to print the float and do a line feed after that and we didn&#8217;t so basically assembly just</p>



<p>printed this string that was the last part we just added and then it returned right away</p>



<p>to the driver so let&#8217;s finish this up so next thing we&#8217;re going to do is we&#8217;re going to</p>



<p>move the float value back into XMM0.</p>



<p>Remember, anytime you call a function, all your float registers could be destroyed.</p>



<p>So it makes sense that we saved XMM0 and then we loaded it only several instructions later</p>



<p>because you don&#8217;t know what&#8217;s happening under the hood inside of print null terminated string</p>



<p>or any of the system calls that it uses.</p>



<p>Your XMM0 is likely destroyed, but you don&#8217;t know for sure, right?</p>



<p>If you count on it not being destroyed, you might be introducing undefined behavior to your program.</p>



<p>So that&#8217;s not good.</p>



<p>Anyway, so we&#8217;re just loading back up from that global into XMM zero.</p>



<p>And then we&#8217;re calling that function again to just sort of print the float.</p>



<p>And it wants the float as argument zero.</p>



<p>So we&#8217;re just sticking into XMM zero.</p>



<p>And then we&#8217;re printing a new line.</p>



<p>So then when we&#8217;re done here, run it.</p>



<p>and now it says the assembly module received this value from the C++ module and notice how it&#8217;s that</p>



<p>special value that I hard-coded 112222 whatever so that&#8217;s this value right here</p>



<p>and again if we had this cpp function returning along then all we would have to do is look at</p>



<p>the RAX register and if we had let&#8217;s let&#8217;s say for some reason that this assembly function we</p>



<p>function we wanted to get something from it we could say you know double temp</p>



<p>equals that then all we&#8217;d have to do is make sure that we returned sorry we</p>



<p>loaded XMM zero with our return value in the assembly module because XMM zero is</p>



<p>the return register for floats or if we wanted to do like a long then just load</p>



<p>RAX with something at the end of my assembly function and that&#8217;s how C would</p>



<p>C and C++ they&#8217;re following the ABI so if you personally don&#8217;t follow the ABI</p>



<p>you&#8217;re just going to end up memorizing two different ways of doing things and</p>



<p>your way is not going to be compatible with all the other modules written in</p>



<p>higher level languages so it&#8217;s kind of a waste of time and your modules won&#8217;t be</p>



<p>compatible with other people&#8217;s libraries and functions it&#8217;s just a huge waste of</p>



<p>time so just follow the ABI in the first place I&#8217;ve literally known people who</p>



<p>said you know what I don&#8217;t want to learn the ABI that&#8217;s dumb then as they wrote</p>



<p>they wrote a bunch of assembly they just started thinking like oh this is too confusing because i</p>



<p>keep forgetting what registers i was going to pass back and forth between my functions</p>



<p>and like a month goes by i come back to an old program i can&#8217;t remember i have to like look</p>



<p>through all my code again so then they started accidentally inventing their own little scheme</p>



<p>like oh i know what to do i&#8217;ll put the first argument in this register and i&#8217;ll put the</p>



<p>second argument in this all they were doing is just reinventing the abi from scratch and then</p>



<p>got to that point and then later wasting more time when they realized they just needed to learn the</p>



<p>ABI and forget about their way so don&#8217;t let that happen to you and you should probably just try to</p>



<p>do it the right way the first time right um a lot of things in coding and computer science in general</p>



<p>are just time savers that feel like wastes of time at first okay what else we have anything</p>



<p>that I wanted to show you that was actually I think the entirety of that program let&#8217;s run it</p>



<p>while I check to see if there&#8217;s any other stuff that I was supposed to show you.</p>



<p>So let&#8217;s do this.</p>



<p>Everything seems to be working.</p>



<p>Let&#8217;s see, I wanted to tell you about labels, return, call,</p>



<p>the stack, the return address, prologue and epilog,</p>



<p>pushing and popping, respecting the ABI.</p>



<p>I wanted to show you that textbook function arguments, mixed arguments,</p>



<p>int or pointer return types and float return types.</p>



<p>I guess I got through everything without forgetting something huge, which, hey,</p>



<p>that&#8217;s probably a first not that i recall at the moment but it probably is</p>



<p>anyway so uh i guess that&#8217;s it i hope you feel like function experts now in yasm x86-64 assembly</p>



<p>also known as a amd64 assembly in ubuntu okay thank you so much for watching this video i</p>



<p>hope you learned a little bit and had a little bit of fun 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,</p>



<p>a small little favor, could you please subscribe and follow this channel or these videos or whatever</p>



<p>it is 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>videos, better videos, or just I&#8217;ll be able to keep making videos in general. So please do me a</p>



<p>kindness and subscribe. You know, sometimes I&#8217;m sleeping in the middle of the night and I just</p>



<p>wake up because I know somebody subscribed or followed. It just wakes me up and I get filled</p>



<p>with joy. That&#8217;s exactly what happens every single time. So you could do it as a nice favor to me or</p>



<p>you could troll me if you want to just wake me up in the middle of the night, just subscribe</p>



<p>and then I&#8217;ll just wake up. I promise that&#8217;s what will happen. Also, if you look at the middle of</p>



<p>if you look at the middle of the screen right now you should see a QR code which</p>



<p>you can scan in order to go to the website which I think is also named</p>



<p>somewhere at the bottom of this video and it&#8217;ll take you to my main website</p>



<p>where you can just kind of like see all the videos I published and the services</p>



<p>and tutorials and things that I offer and all that good stuff and if you have</p>



<p>a suggestion for clarifications or errata or just future videos that you</p>



<p>comment or if you just want to say hey what&#8217;s up what&#8217;s going on you know just send me a comment</p>



<p>whatever 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</p>



<p>like it would really it really mean the world to me i would really appreciate it so again thank you</p>



<p>so much for watching this video and um enjoy the cool music as as i fade into the darkness</p>



<p>which is coming for us all</p>



<p>Thank you.</p>
<p>The post <a href="https://www.NeuralLantern.com/yasm-x86-64-assembly-functions-tutorial-integers-floats-pointers-abi-c-interoperability/">YASM x86-64 Assembly Functions Tutorial &#8211; Integers, Floats, Pointers, ABI, C++ Interoperability</a> appeared first on <a href="https://www.NeuralLantern.com">NeuralLantern.com</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.NeuralLantern.com/yasm-x86-64-assembly-functions-tutorial-integers-floats-pointers-abi-c-interoperability/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<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 loading="lazy" 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 loading="lazy" 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>
		<item>
		<title>Mixed Function Arguments in x86-64 Assembly &#8211; Integers &#038; Floats Explained</title>
		<link>https://www.NeuralLantern.com/mixed-function-arguments-in-x86-64-assembly-integers-floats-explained/</link>
					<comments>https://www.NeuralLantern.com/mixed-function-arguments-in-x86-64-assembly-integers-floats-explained/#respond</comments>
		
		<dc:creator><![CDATA[mike]]></dc:creator>
		<pubDate>Sun, 01 Mar 2026 02:14:39 +0000</pubDate>
				<category><![CDATA[Assembly Language]]></category>
		<category><![CDATA[Coding]]></category>
		<category><![CDATA[Computer Science]]></category>
		<category><![CDATA[Videos]]></category>
		<category><![CDATA[abi calling convention]]></category>
		<category><![CDATA[assembly language]]></category>
		<category><![CDATA[Ed Jorgensen]]></category>
		<category><![CDATA[floating point registers]]></category>
		<category><![CDATA[function calling convention]]></category>
		<category><![CDATA[integer float arguments]]></category>
		<category><![CDATA[linux assembly]]></category>
		<category><![CDATA[low level programming]]></category>
		<category><![CDATA[mixed arguments]]></category>
		<category><![CDATA[nasm]]></category>
		<category><![CDATA[rax return]]></category>
		<category><![CDATA[rdi rsi xmm0]]></category>
		<category><![CDATA[system v abi]]></category>
		<category><![CDATA[ubuntu assembly]]></category>
		<category><![CDATA[x86 assembly]]></category>
		<category><![CDATA[x86-64 assembly]]></category>
		<category><![CDATA[xmm registers]]></category>
		<category><![CDATA[xmm0 return]]></category>
		<category><![CDATA[yasm]]></category>
		<guid isPermaLink="false">https://www.NeuralLantern.com/?p=310</guid>

					<description><![CDATA[<p>In the System V x86-64 ABI, integer and floating-point arguments use separate register assignment sequences. Integer arguments go into rdi, rsi, rdx, rcx, r8, r9 (then stack), while floating-point arguments use xmm0 through xmm7 independently. This means the first integer you see — even if it appears after several doubles — always goes into rdi, and the second float always goes into xmm1 regardless of how many integers came before it.</p>
<p>The post <a href="https://www.NeuralLantern.com/mixed-function-arguments-in-x86-64-assembly-integers-floats-explained/">Mixed Function Arguments in x86-64 Assembly &#8211; Integers &amp; Floats 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 loading="lazy" title="Mixed Function Arguments in x86-64 Assembly - Integers &amp; Floats Explained" width="1380" height="776" src="https://www.youtube.com/embed/ZAY3srLRx8c?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 guide to passing mixed int + float arguments in x86-64 assembly (System V ABI). See why rdi can still be the first integer after several doubles, why xmm registers count separately, and how return values switch between rax and xmm0. Perfect for YASM/NASM programmers interfacing with C/C++.</p>



<p>Intro to Mixed Arguments 00:00<br>Simple Integer-Only Functions 00:46<br>Adding Arguments and Return Values 01:01<br>Integer Arguments in RDI and RSI 01:30<br>Pointers Treated as Integers 02:06<br>Introducing Floating-Point Returns 02:31<br>Returning Double in XMM0 03:06<br>First Float Argument in XMM0 03:36<br>Float Registers Count Separately 04:03<br>Integer Register Order Explained 04:16<br>Separate Counting for Integers and Floats 05:21<br>RSI as First Integer After Float 05:38<br>Reference to Ed Jorgensen&#8217;s Book 07:00<br>Callee-Saved Registers Overview 07:56<br>Complex Mixed Argument Examples 09:48<br>Inserting Integer Among Floats 10:57<br>Skipping Float Registers on Integer 11:18<br>Calling C from Assembly Notes 11:56<br>Name Mangling Reminder 12:21<br>Closing Remarks and Thanks 12:30<br>Call to Subscribe and Support 13:04<br>Website and QR Code Mention 13:38<br>Final Thanks and Outro Music 14:19</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>Hey everybody! In this video I&#8217;m going to talk to you a little bit about using functions with</p>



<p>mixed arguments in a YASM x86-64 assembly language program. Although this</p>



<p>video will probably still be useful to you if you&#8217;re using NASM or MASM because the</p>



<p>mixed arguments and their order and how they work is governed by the ABI which is a system</p>



<p>so what the heck am i talking about okay so the first thing that i want to show you is uh</p>



<p>imagine we have a function here let&#8217;s say void f this function doesn&#8217;t take any arguments and it</p>



<p>doesn&#8217;t return anything congratulations you&#8217;re done that was the easiest thing that you&#8217;ve ever</p>



<p>done in your entire life but as soon as you start adding arguments to it you have to uh</p>



<p>start understanding how to uh how to call registers and stuff so imagine we have like a</p>



<p>a long a and a long b so we give it two arguments and maybe we want it to return a long</p>



<p>this is kind of like level one of writing functions in assembly right we just have</p>



<p>a bunch of integers and we realize okay um this long the return value is going to come to the</p>



<p>Maybe I&#8217;ll do a comment up here, we&#8217;ll do rax, we&#8217;ll say the long is actually coming</p>



<p>back to the user in rax and the long is coming into the function with rdi, the long a and</p>



<p>the long b is coming into the function with rsi.</p>



<p>So if you understand this then you can write a function in assembly where basically you</p>



<p>two incoming integer arguments and then you return to the caller a long value using rax</p>



<p>keep in mind also that if this second argument was a pointer long pointer then it would be the</p>



<p>same thing rsi because pointers are integers they&#8217;re 64-bit unsigned integers even if the</p>



<p>pointer was not to a long even if the pointer was to a double or a character or whatever it would</p>



<p>pointers are integers. It starts getting a little bit more complicated when you want to mix between</p>



<p>floats and numeric arguments. So what I&#8217;ve been saying so far, it&#8217;s all just integers.</p>



<p>Even if we&#8217;re mixing pointers with regular longs, it&#8217;s just, you know, they&#8217;re all longs basically,</p>



<p>right? So, but as soon as we start introducing floats, then things get a little bit more</p>



<p>complicated. So for starters, what if we did a function that was, that returned a double?</p>



<p>that returned a double. So let&#8217;s say we have a function G and it returns a double. Maybe it</p>



<p>still takes in a long and a double pointer. If it returns a double, let&#8217;s see, is this still</p>



<p>lining up? I don&#8217;t think it&#8217;s lining up anymore. There we go. If it&#8217;s returning a double, then</p>



<p>that means we have to return to the caller with the XMM0 register. So XMM0 is always designated as</p>



<p>the well the first float argument but also the float return value so keep in</p>



<p>mind if I&#8217;m returning a double to the caller I&#8217;m not going to use our a X at</p>



<p>all I&#8217;m just going to use XMM zero load it up with the return value and then</p>



<p>return to the caller and that&#8217;s it if I wanted to do another function let&#8217;s say</p>



<p>hmm how about H and then we&#8217;ll say I don&#8217;t know maybe the first argument is a</p>



<p>argument is a float we call it a double a that means rdi is not going to be the argument that</p>



<p>or the register that we look at to see our incoming double it has to be a float register</p>



<p>xmm0 is the float register that we look at for the first argument the float registers are really</p>



<p>easy you just kind of go in order like xmm0 is the first argument xmm1 is the next argument xmm2</p>



<p>the general purpose uh integer registers they&#8217;re a little more complicated you have to remember</p>



<p>their their labels uh you know like the letters have an ordering to it uh suppose for the sake</p>



<p>of argument that we&#8217;re going to return a double from this also uh in that case let&#8217;s see did i</p>



<p>just mess the formatting up again i think that&#8217;s okay um again we&#8217;re going to return xmm0</p>



<p>notice how we&#8217;re returning xmm0 oh that&#8217;s why i spaced it forward notice how we are</p>



<p>forward notice how we are returning xmm0 and we&#8217;re also taking xmm0 these float registers tend to be</p>



<p>reused all over the place none of them are designated as callee saved within the abi</p>



<p>so you always have to stash their values somewhere if you&#8217;re ever going to make a function call or</p>



<p>anything or a system call the other thing to keep in mind is notice how rsi i still have rsi there</p>



<p>for the second incoming argument you would imagine that rsi is supposed to be the second argument</p>



<p>supposed to be the second argument even if it uh you know comes after a double but that&#8217;s not</p>



<p>actually true because the the order i guess like the the ordering of the uh of the registers they</p>



<p>only count against their own class like the float registers the float arguments they only count</p>



<p>against the position of the float and the uh of the float registers and and and the general purpose</p>



<p>they only count against themselves.</p>



<p>What I&#8217;m trying to say is that RSI is the second integer argument,</p>



<p>but if you look at the signature, we only have one integer argument, right?</p>



<p>So RSI is not the second integer argument.</p>



<p>It&#8217;s actually the first integer argument.</p>



<p>So I&#8217;m going to put an RDI there.</p>



<p>You probably can infer that the second float argument should be XMM1 at this point, I hope.</p>



<p>and that&#8217;s true if we just say that there&#8217;s going to be another double</p>



<p>that this function takes then it&#8217;s going to be xmm1</p>



<p>not xmm2 because if you&#8217;re just counting the arguments</p>



<p>and you&#8217;re sort of grouping the integers with the floats together</p>



<p>you might think well first argument is xmm0 second argument would have been xmm1</p>



<p>third argument would have been xmm2 therefore if I see</p>



<p>a float coming into the third argument position it&#8217;ll be xmm2</p>



<p>No, the integers don&#8217;t count against the floats and the floats don&#8217;t count against the integers.</p>



<p>That&#8217;s why RDI is the first argument for integers, even though it&#8217;s in the second position,</p>



<p>it&#8217;s still the first integer that we see.</p>



<p>And XMM1 is the second float argument that we see.</p>



<p>That&#8217;s why it&#8217;s XMM1 instead of XMM2, because we&#8217;re looking at it in the third position.</p>



<p>So you&#8217;re not really looking at, whoops, you&#8217;re not looking at overall position.</p>



<p>at position only counting that type of register let me pull open my favorite book to get a little</p>



<p>bit more clarity on this um this book is written by a wonderful professor dr ed jorgensen phd</p>



<p>this book i did not write it ed jorgensen wrote this book it&#8217;s called x86 64 assembly language</p>



<p>programming with ubuntu this book can turn you into an expert with yasm assembly so uh</p>



<p>I recommend everybody grab a copy. It&#8217;s also free and it&#8217;s got a copy left license.</p>



<p>You can see like the license here. But anyway, I wanted to open up this book real fast</p>



<p>just to show you the registers. Okay. I&#8217;m going to search for Kali saved so I don&#8217;t have to fumble</p>



<p>around the book too long. Kali saved and that brings us to section 12.8.2 register usage.</p>



<p>let&#8217;s see callee saved you&#8217;ll see rbx is callee saved rbp is callee saved probably shouldn&#8217;t</p>



<p>mess with that one too much r12 13 14 15 all callee saved there&#8217;s a couple temporary registers</p>



<p>but if you look carefully rdi is designated as the first argument but it should say really</p>



<p>the first integer argument so rdi is the first integer argument that we see that&#8217;s why</p>



<p>argument that we see that&#8217;s why in all of these examples the first integer argument that we</p>



<p>actually see is rdi so like for function f long a that was the first integer argument</p>



<p>and then in the function g it&#8217;s also the first integer argument and then in function h rdi is</p>



<p>the double pointer b because that is the first integer argument that we actually see</p>



<p>so keep that in mind you would just repeat that pattern you know the second integer argument you</p>



<p>second integer argument you see that&#8217;s RSI. That&#8217;s why RSI is the pointer here because</p>



<p>pointers are integers and the pointer here and we don&#8217;t have anything else but if I guess if we</p>



<p>added another you know let&#8217;s say a long D then it would be RSI right there. Hopefully that makes</p>



<p>sense and you can you can carry this logic forward for the first argument the second argument the</p>



<p>and then R8 for the fifth argument R9 for the sixth argument and then you can</p>



<p>look for further arguments on the stack if you need more than that but basically</p>



<p>that&#8217;s how you count them up and then the float registers are just a lot</p>



<p>easier they always start at zero and they go all the way up to 15 and so I</p>



<p>don&#8217;t know maybe I should just copy paste well maybe I&#8217;ll start it from</p>



<p>scratch we&#8217;ll say dub no let&#8217;s let&#8217;s mix it up a little bit more we&#8217;ll say long</p>



<p>And the long is going to be returned with the RAX.</p>



<p>And I guess I have to space this forward a little bit.</p>



<p>And then if we have a bunch of floats, say double A.</p>



<p>Can I maybe just copy paste this a little bit so I don&#8217;t have to do so much typing?</p>



<p>Okay, so we&#8217;ll do double A, double B, double C, double D, double E.</p>



<p>and then this is going to be xmm zero why did i put the extra comment there i don&#8217;t know</p>



<p>and then we&#8217;re going to have xmm one and then we&#8217;re going to have xmm two xmm three and so forth</p>



<p>and just to make sure you&#8217;re paying attention ask yourself this real fast if i were to insert</p>



<p>z what register would we use for that would it be the one two three four five the fifth argument</p>



<p>would it be uh would it be r8 or would it be something else hopefully you understand now</p>



<p>let&#8217;s say we do a character pointer that it would be rdi because that&#8217;s the first integer argument</p>



<p>that we actually see all right and then you know to return we would return in rax and then the</p>



<p>return in RAX and then the floats just keep counting notice how it skipped from XMM2 or it</p>



<p>went from XMM2 to XMM3 even though we crossed over an integer argument in between because the</p>



<p>float arguments don&#8217;t count against the integers and the ints don&#8217;t count against the floats</p>



<p>let&#8217;s see I think</p>



<p>well if you want you can look at section 18.2 which just sort of talks about the floating</p>



<p>which just sort of talks about the floating point registers we have 0 through 15 but I don&#8217;t think</p>



<p>we really need to do that here I think we pretty much have everything we need to know to to call</p>



<p>on functions with mixed arguments and to have a function within assembly that supports mixed</p>



<p>arguments so again if your C or C++ modules are expecting some kind of signature like this now you</p>



<p>know how to calculate what registers they&#8217;re actually going to populate by the time execution</p>



<p>execution jumps into your assembly function.</p>



<p>And if you want to call a C or a C++ function from assembly,</p>



<p>now you know which registers to populate so that the the other function that</p>



<p>you&#8217;re calling can receive the right data from you.</p>



<p>Don&#8217;t forget about name mangling, which I talked about in another video.</p>



<p>Okay, I think that&#8217;s all I really have to say.</p>



<p>This video was pretty short.</p>



<p>Thank you so much for watching.</p>



<p>I hope you learned a little bit of stuff and had a little bit of fun.</p>



<p>I&#8217;ll see you in the next video.</p>



<p>make more videos and grow this community so we&#8217;ll be able to do more videos longer videos better</p>



<p>videos or just i&#8217;ll be able to keep making videos in general so please do do me a kindness and uh</p>



<p>and subscribe you know sometimes i&#8217;m sleeping in the middle of the night and i just wake up because</p>



<p>i know somebody subscribed or followed it just wakes me up and i get filled with joy that&#8217;s</p>



<p>exactly what happens every single time so you could do it as a nice favor to me or you could</p>



<p>you could troll me if you want to just wake me up in the middle of the night just subscribe</p>



<p>and then I&#8217;ll just wake up. I promise that&#8217;s what will happen. Also, if you look at the middle of</p>



<p>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 it&#8217;ll take you to my</p>



<p>main website where you can just kind of like see all the videos I published and the services and</p>



<p>tutorials and things that I offer and all that good stuff. And if you have a suggestion for</p>



<p>clarifications or errata or just future videos that you want to see please leave a comment or</p>



<p>if you just want to say hey what&#8217;s up what&#8217;s going on you know just send me a comment whatever i also</p>



<p>wake up for those in the middle of the night i get i wake up in a cold sweat and i&#8217;m like it would</p>



<p>really it really mean the world to me i would really appreciate it so again thank you so much</p>



<p>for watching this video and um enjoy the cool music as as i fade into the darkness which is</p>



<p>for us all.</p>



<p>Thank you.</p>
<p>The post <a href="https://www.NeuralLantern.com/mixed-function-arguments-in-x86-64-assembly-integers-floats-explained/">Mixed Function Arguments in x86-64 Assembly &#8211; Integers &amp; Floats Explained</a> appeared first on <a href="https://www.NeuralLantern.com">NeuralLantern.com</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.NeuralLantern.com/mixed-function-arguments-in-x86-64-assembly-integers-floats-explained/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>x86-64 Assembly Pointers &#038; Dereferencing Explained &#8211; Hybrid C++/YASM Example</title>
		<link>https://www.NeuralLantern.com/x86-64-assembly-pointers-dereferencing-explained-hybrid-c-yasm-example/</link>
					<comments>https://www.NeuralLantern.com/x86-64-assembly-pointers-dereferencing-explained-hybrid-c-yasm-example/#respond</comments>
		
		<dc:creator><![CDATA[mike]]></dc:creator>
		<pubDate>Sun, 22 Feb 2026 03:21:32 +0000</pubDate>
				<category><![CDATA[Assembly Language]]></category>
		<category><![CDATA[Coding]]></category>
		<category><![CDATA[Computer Science]]></category>
		<category><![CDATA[Videos]]></category>
		<category><![CDATA[assembly C++ interop]]></category>
		<category><![CDATA[assembly data section]]></category>
		<category><![CDATA[assembly memory addresses]]></category>
		<category><![CDATA[assembly pointers]]></category>
		<category><![CDATA[assembly tutorial]]></category>
		<category><![CDATA[dereferencing assembly]]></category>
		<category><![CDATA[extern C assembly]]></category>
		<category><![CDATA[hybrid C++ assembly]]></category>
		<category><![CDATA[low level programming]]></category>
		<category><![CDATA[passing pointers assembly]]></category>
		<category><![CDATA[stack alignment assembly]]></category>
		<category><![CDATA[systems programming]]></category>
		<category><![CDATA[x86 assembly tutorial]]></category>
		<category><![CDATA[x86-64 assembly]]></category>
		<category><![CDATA[x86-64 calling convention]]></category>
		<category><![CDATA[x86-64 pointers]]></category>
		<category><![CDATA[Yasm assembly]]></category>
		<category><![CDATA[Yasm tutorial]]></category>
		<guid isPermaLink="false">https://www.NeuralLantern.com/?p=305</guid>

					<description><![CDATA[<p>This video explains pointers and dereferencing in x86-64 YASM assembly and demonstrates passing pointers between assembly and C++ in a hybrid program. We show how to read a C string from assembly, modify a long via pointer dereference so the change is visible in C++, and send assembly-owned data (string, long, double) back to C++ using pointers.</p>
<p>The post <a href="https://www.NeuralLantern.com/x86-64-assembly-pointers-dereferencing-explained-hybrid-c-yasm-example/">x86-64 Assembly Pointers &amp; Dereferencing Explained &#8211; Hybrid C++/YASM Example</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 loading="lazy" title="x86-64 Assembly Pointers &amp; Dereferencing Explained - Hybrid C++/YASM Example" width="1380" height="776" src="https://www.youtube.com/embed/jzj0iqC-XJI?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>Learn pointers &amp; dereferencing in x86-64 YASM assembly and how to pass them correctly between assembly and C++ in a hybrid program. We build a small working example that sends strings, longs, and doubles both directions using pointers, modifies values across module boundaries, and explains why pointer-to-double still uses general-purpose registers. Includes a quick demo of stack misalignment crash + fix.</p>



<p>Great for assembly beginners moving to real programs, systems programming students, or anyone curious how low-level code talks to C/C++.</p>



<p>00:00 Introduction to Pointers and Dereferencing in x86-64 Assembly<br>00:28 Pointers explained in C++<br>01:02 Changing values via pointers in C++<br>01:43 Pointers in assembly basics<br>02:09 Defining variables and pointers in YASM data section<br>03:23 Pointers are always integers even to doubles<br>04:20 Function arguments are pointers treated as 64-bit integers<br>05:00 Driver C++ code overview<br>05:58 Marking extern &#8220;C&#8221; functions<br>06:40 Local stack variables and passing pointers<br>07:51 Stack lifetime warning<br>08:34 Assembly data section strings and numbers<br>09:39 Print null-terminated string helper functions<br>10:38 External symbols and hey_driver_print_this<br>11:29 Point function prologue and stack alignment<br>13:04 Extra push for 16-byte alignment<br>14:20 Printing welcome message from assembly<br>16:00 Driver sees initial long value<br>16:58 Printing received string from C++<br>18:20 Using received char pointer without dereference<br>20:21 Modifying incoming long via dereference<br>21:46 Driver sees modified long value 101<br>22:43 Calling back to C++ to print assembly-owned data<br>23:48 Passing pointers to assembly string long and double<br>25:08 Driver prints assembly-owned values and addresses<br>26:14 Summary of pointer passing between modules<br>26:36 Stack alignment crash demonstration<br>27:39 Adding extra push/pop fixes segfault<br>28:00 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 Blog: https://www.NeuralLantern.com</li>



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



<p>Hey there! In this video we&#8217;re going to talk about pointers and dereferencing in a YASM x8664</p>



<p>assembly program, also as a hybrid program so that assembly and C++ can talk to each other</p>



<p>and send each other pointers and send each other data and things like that.</p>



<p>for what pointers are.</p>



<p>I&#8217;m going to write in C++ for a second.</p>



<p>Suppose you have a pointer for an integer.</p>



<p>We&#8217;ll call it P.</p>



<p>Suppose you have an integer by itself.</p>



<p>We&#8217;ll call it A.</p>



<p>Let&#8217;s say that the value of A is 5.</p>



<p>And if you wanted to say that P points to A,</p>



<p>you could say P equals the address of A.</p>



<p>I&#8217;ll put C++ at the top here.</p>



<p>And so now if I set A to 6</p>



<p>then I print P a dereference of P this is not like a full pointers tutorial</p>



<p>but basically by changing a I&#8217;m changing what P thinks it sees as a value</p>



<p>assuming ID reference it I could also let me do a print 6 here I could also</p>



<p>just change the value through P I could say dereference P and I could say equals</p>



<p>would actually print a seven right so you know you can have regular variables global variables</p>



<p>whatever kind of you know memory stuff on the stack and to get a pointer to it you really just</p>



<p>need to get its memory location in c++ it&#8217;s kind of easy syntactically you can see what&#8217;s happening</p>



<p>in assembly you really just need the memory location stored somewhere you could store that</p>



<p>variable that just simply stored the memory location of some other variable.</p>



<p>You could have a 64-bit register store the value of a variable.</p>



<p>Let&#8217;s say we have like a, I don&#8217;t know, my whatever, my number let&#8217;s say inside of assembly.</p>



<p>I&#8217;ll do ASM here and we say it&#8217;s a quad word and it starts off as this number or whatever.</p>



<p>So if you haven&#8217;t seen my previous videos, go see them for the basics of assembly and</p>



<p>of assembly and linking and make files and all that stuff but you know if you</p>



<p>have an assembly program and you have a data section and you define a global</p>



<p>variable like this what you&#8217;re basically saying is I want to take this giant</p>



<p>number and I want to write it into eight bytes that&#8217;s the DQ it says data quad</p>



<p>word I want to write that giant number across eight bytes and then I want to</p>



<p>get a pointer to it stored in the my number symbol so my number is not</p>



<p>actually the value it&#8217;s a pointer to the value so you know later if you want to</p>



<p>you know later if you want to move you know something into a register if you did this</p>



<p>that would move the pointer into rax but if you did this</p>



<p>with deref symbols after it or around it then you would move</p>



<p>maybe i&#8217;ll put that into rex you&#8217;d move that actual number that we specified into rex</p>



<p>into Rx. It&#8217;s important to understand also that pointers are integers even when we&#8217;re pointing to</p>



<p>doubles. So for example sometimes people make this mistake they&#8217;ll say you know my double</p>



<p>and they&#8217;ll say it&#8217;s a quad word meaning this is going to be a 64-bit double precision floating</p>



<p>point number and they&#8217;ll do like 44.55 or whatever. So that is a double and it is in memory</p>



<p>you know what is the symbol of my double remember it&#8217;s supposed to be just a</p>



<p>pointer right it can&#8217;t be an actual double because a memory location is not</p>



<p>a double a memory location is an integer so that means if you wanted to move a</p>



<p>pointer into a register you would only be able to move the pointer into a</p>



<p>regular general purpose register not a floating point register and you should</p>



<p>use the regular movement instructions for just regular general purpose</p>



<p>So keep that in mind if you see a signature like this like let&#8217;s say function F and we have</p>



<p>You know, let&#8217;s say long a and long B and actually let&#8217;s do pointers</p>



<p>Let&#8217;s say long pointer a and long pointer</p>



<p>B and double pointer C all three of those arguments are actually 64 bit integers</p>



<p>Because they&#8217;re all pointers even if one of the pointers points to adult a double</p>



<p>double why did I say dull pointers aren&#8217;t dull they&#8217;re exciting okay so I&#8217;m gonna open up some</p>



<p>code here real fast so usually I don&#8217;t explain my uh my driver I&#8217;m gonna explain it to you this time</p>



<p>because it&#8217;s kind of doing a little bit more than my other videos um again if you don&#8217;t have uh the</p>



<p>knowledge of how to make a make file see my other videos because that&#8217;s explained there for now I&#8217;m</p>



<p>what we really need to do is write a driver and an assembly module for a</p>



<p>hybrid program again hybrid programs covered in other videos so the driver is</p>



<p>pretty easy I&#8217;m just going to copy paste it honestly here and then just kind of</p>



<p>explain it to you the driver is pretty easy we&#8217;re going to do I O stream so we</p>



<p>can print stuff we&#8217;re going to mark an external function called point as extern</p>



<p>C so that just disables name mangling which means the C++ module will be able</p>



<p>will be able to call on this function called point and it won&#8217;t expect that</p>



<p>the point function has its name mangled like C++ does the reason being is that</p>



<p>point is actually going to be in a side it&#8217;s going to be inside assembly where</p>



<p>its name will not be mangled this disables the ability to overload but</p>



<p>that&#8217;s okay we don&#8217;t care it&#8217;s going to take two pointers a pointer to a character</p>



<p>and a pointer to a long since both of those are pointers they&#8217;re both</p>



<p>64-bit integers even the character pointer and then we have a function that is internal to this</p>



<p>module called hey driver print this remember we&#8217;re inside of the driver program right now</p>



<p>so if you look at the bottom it&#8217;s just a function that takes in some pointers</p>



<p>and then prints some stuff so it&#8217;s going to print like it&#8217;s going to print what the string is</p>



<p>it&#8217;s going to print what the long is my dog&#8217;s growling at me i&#8217;m going to ignore him because</p>



<p>i literally just let him pee and poop at this point now he&#8217;s harassing me for treats</p>



<p>now he&#8217;s harassing me for treats he always does this okay so uh the string the long the double</p>



<p>this function expects to receive three pointers to different data types it&#8217;s just going to print</p>



<p>all of them and the point get it the point of this function is we&#8217;re going to go inside of</p>



<p>the assembly module and then have the assembly module call on this function so that we can we</p>



<p>can prove that we can have stuff sent from assembly to c plus plus or c using pointers</p>



<p>using pointers we can have data sent over so anyway that&#8217;s why both of these</p>



<p>are in here the point needs to be marked as no name mangling because point is</p>



<p>inside of assembly which will not name mangle and then hey driver print this</p>



<p>that needs to have name mangling disabled also so that the assembly</p>



<p>module can call on this other than that we&#8217;re just basically inside of a main</p>



<p>saying hey this is the c string we&#8217;re making a c string inside of the main function notice how</p>



<p>this is a local variable so that c string is going to show up on the stack it&#8217;s going to show up in</p>



<p>the area that is owned by main for main stack area same thing for my long that&#8217;s a local variable on</p>



<p>the stack um and but then we can actually send pointers to those pieces of data to another</p>



<p>function in another module you don&#8217;t have to only transport globals or stuff on the heap</p>



<p>or stuff on the heap, you can transport pointers to local variables. Just make sure that by the</p>



<p>time this function finishes, then nowhere else is actually using that data because,</p>



<p>well, being on the stack, once main function or once any function finishes, then its portion of</p>



<p>the stack will be cleaned up and removed and it&#8217;ll be junk data. You&#8217;ll probably get a seg fault.</p>



<p>But for now, we&#8217;re not going to use anything on the stack. We&#8217;re not going to use these local</p>



<p>just going to use them quickly on this call to point and then we&#8217;re going to return to the</p>



<p>operating system and finish the program. So that&#8217;s the driver. Now the hard part. Let&#8217;s do this in</p>



<p>assembly. So for starters, I&#8217;m going to make a data section and just explain it to you very,</p>



<p>very quickly. Again, if you don&#8217;t understand the basics of YASM x86-64 assembly, did I mention</p>



<p>that that&#8217;s what this language is at the beginning of the video? I guess I should put that in the</p>



<p>put that in the description or record an announcement that I can tack on at the beginning</p>



<p>or something. Anyway, so if you don&#8217;t understand how to do this, see my other videos, but basically</p>



<p>we&#8217;re going to make a data section. We&#8217;re going to define some strings. Here&#8217;s like an announcement.</p>



<p>Oh, we&#8217;re inside of, you know, the module now, the assembly module. And now we&#8217;re going to print</p>



<p>the received string. And then we&#8217;re going to make a string that is owned by assembly, which we can</p>



<p>into C++ when we call the function inside of the driver.</p>



<p>So this string is owned by the assembly module.</p>



<p>Notice how these are null terminated strings.</p>



<p>I just have like a comma zero there,</p>



<p>which means I have some extra functions</p>



<p>I&#8217;m gonna paste in that we&#8217;re not really gonna talk about</p>



<p>because they&#8217;ve been discussed in other videos</p>



<p>just so that we can print null terminated strings.</p>



<p>Then I&#8217;ve got a new line here,</p>



<p>you know, carriage return line feed.</p>



<p>And then I&#8217;ve just got some numbers</p>



<p>that are owned by the assembly module.</p>



<p>Then I&#8217;ve got a system write call,</p>



<p>call code one for the system call writes and file descriptor standard output so I</p>



<p>can print just to the terminal again if you don&#8217;t understand this see my other</p>



<p>videos so now let&#8217;s start the actual text section so this is where our</p>



<p>instructions start so we got the text section here and we&#8217;re going to use some</p>



<p>external symbols don&#8217;t worry about these I&#8217;m just using my own little library to</p>



<p>and input integers if you have access to this library use it if you don&#8217;t if you&#8217;re watching</p>



<p>at home and you don&#8217;t have this library then that&#8217;s fine you can use you know printf or</p>



<p>scanf or something like that to get and print floats from and to the user</p>



<p>but yeah I&#8217;m just using that and then I&#8217;m marking an external function here called hey driver print</p>



<p>this if you recall the driver module has a function called hey driver print this so</p>



<p>just allows my assembly code to call on that external function. Okay now next</p>



<p>piece of code. This is going to be… actually I&#8217;m going to paste the print</p>



<p>null terminated string function and related code because it&#8217;s just like a</p>



<p>big giant mess and we&#8217;re mostly going to ignore it. So just to show you what I&#8217;m</p>



<p>doing here I have a function called print null terminated string so that I</p>



<p>can print these strings up here and then I have it rely on a function called</p>



<p>string length that I have implemented up here and all it does is just</p>



<p>implemented up here and all it does just calculates the length of the string and</p>



<p>then a crlf function so I can just call that so that&#8217;s all explained in other</p>



<p>videos don&#8217;t worry about it for now we&#8217;re going to start the actual entry</p>



<p>point remember the driver was just gonna call point right so now we just have to</p>



<p>implement point in the assembly module so that&#8217;s gonna be like down here our</p>



<p>our entry point so the signature for this function is going to be character</p>



<p>pointer and then a long pointer and it doesn&#8217;t return anything and remember</p>



<p>that if we look back at the driver that should match the signature right it&#8217;s a</p>



<p>character pointer and a long pointer and of course this is just a comment that</p>



<p>reminds me of what to do in assembly you don&#8217;t really have a signature you just</p>



<p>sort of use registers but I&#8217;m reminding myself that RDI is going to be a</p>



<p>character pointer and RSI is going to be a long pointer.</p>



<p>Here&#8217;s a note to myself that I&#8217;m going to use R12 and R13, which means</p>



<p>the first thing that I should do, well actually before I even do that, I should</p>



<p>return from this function because it is a function. I marked it as global</p>



<p>so that the other module could call it, the driver module could call it. Again,</p>



<p>see my other videos for hybrid programs.</p>



<p>But so now the, you know, if the driver calls this function, then now we&#8217;re inside of</p>



<p>and there&#8217;s a return statement so it&#8217;s a valid function I should preserve the</p>



<p>registers that I&#8217;m going to use that are marked as Kali saved for the ABI so I&#8217;m</p>



<p>going to go prologue and then an epilogue and I&#8217;m going to say push r12 and push</p>



<p>r13 and then I&#8217;m going to pop r13 pop r12 they should be in reverse order if</p>



<p>you&#8217;ve seen my other videos you&#8217;ll know this and the the thing about this</p>



<p>the thing about this particular program is we&#8217;re going to run into stack alignment issues</p>



<p>so uh if you don&#8217;t know about stack alignment and how it can crash your program without you</p>



<p>realizing what&#8217;s wrong see my other videos but for now we&#8217;ll assume you know that and uh i i</p>



<p>already know from running this program in advance that it&#8217;s going to be out of alignment by eight</p>



<p>bytes so i&#8217;m just going to push an extra register onto the stack and that&#8217;s going to put it back</p>



<p>I know it looks weird, but this is going to work.</p>



<p>Let me get rid of this here.</p>



<p>Okay, so.</p>



<p>And then maybe if I can remember at the end of the video,</p>



<p>I can just remove that extra push-pop pair,</p>



<p>and you&#8217;ll see the program starts crashing.</p>



<p>But at home, you can do it just to double check.</p>



<p>So the first thing I really want to do is,</p>



<p>after I push and pop,</p>



<p>is save our incoming arguments.</p>



<p>Remember, the first integer argument</p>



<p>and the second integer argument,</p>



<p>argument they come in as RDI and RSI in assembly per the ABI if both of these</p>



<p>things are pointers it doesn&#8217;t matter what the data type is it could be</p>



<p>pointing to anything including a double and these would still be considered</p>



<p>integer arguments because well RDI and RSI are just going to be loaded up with</p>



<p>memory locations which which are integers so I&#8217;m going to save our</p>



<p>arguments to R12 and R13 now justifying our push and pop pair then I&#8217;m going to</p>



<p>little welcome message so print a little welcome message again you don&#8217;t need to know about this</p>



<p>function but it&#8217;s explained in other videos that I&#8217;ve already published we&#8217;re going to print our</p>



<p>hello beginning message I&#8217;m getting nervous he needs to take a second poop sometimes it&#8217;s poopoo</p>



<p>number two time for him and he&#8217;s not really just lying about a treat but he did go pee and poop</p>



<p>But he did go pee and poop already.</p>



<p>Okay, he just left and walked away.</p>



<p>Okay, if he comes back, I&#8217;m letting him out this time.</p>



<p>I&#8217;ll pause the video if he does it again.</p>



<p>Okay, I&#8217;m pausing the video.</p>



<p>No pee lied.</p>



<p>He went outside, lifted up his little leg, and a couple of drops of pee came out.</p>



<p>Now he&#8217;s staring at me like he deserves a treat.</p>



<p>Sorry, buddy.</p>



<p>I wish I could eat constantly all day long, too.</p>



<p>But life isn&#8217;t always fair.</p>



<p>isn&#8217;t always fair anyway let&#8217;s see I might even lined up on the camera</p>



<p>anymore I don&#8217;t even know so we&#8217;re looking at this code here is going to</p>



<p>print a welcome message let&#8217;s see if that actually works so I&#8217;m gonna do make</p>



<p>run again make files are whoops what did I do loader dot asm what did I do what</p>



<p>did I do I somehow copy pasted the wrong make file</p>



<p>What&#8217;s the name of my source code file?</p>



<p>It&#8217;s point.</p>



<p>I guess I&#8217;ll just change it, and then it&#8217;ll probably work.</p>



<p>It&#8217;s still in assembly module.</p>



<p>Hopefully that didn&#8217;t mess it up too bad by copy-pasting the wrong source code.</p>



<p>Okay.</p>



<p>What is going on here?</p>



<p>Floater.</p>



<p>Oh, I need to change that.</p>



<p>Hang on.</p>



<p>Let me fix this.</p>



<p>I don&#8217;t know if I&#8217;m going to edit this out.</p>



<p>out. It&#8217;s fun to watch me struggle sometimes. There we go.</p>



<p>Point.</p>



<p>Alright, let&#8217;s give it another try.</p>



<p>Oh no, star dot so no such file a directory. Dang it.</p>



<p>Okay, now this seems to work. I may or may not have edited</p>



<p>that out. I copy pasted the wrong source code into my make</p>



<p>file. So I had to manually adjust it. Then I forgot to</p>



<p>copy paste my library file into the build directory. So I had</p>



<p>The driver sees my long as whatever.</p>



<p>What&#8217;s going on?</p>



<p>Print an alternate string begin.</p>



<p>Oh, the driver is printing a bunch of stuff.</p>



<p>Okay.</p>



<p>I started to think, why does it look like the program has a lot of stuff going on?</p>



<p>Oh, that&#8217;s the driver.</p>



<p>Okay.</p>



<p>So the driver says it sees its long as 100.</p>



<p>And then now we&#8217;re inside of the point module.</p>



<p>So that&#8217;s the only thing we&#8217;ve done in assembly so far.</p>



<p>so far then the driver has regained control maybe I should add a couple of</p>



<p>new lines in there so I don&#8217;t get confused again we will do a C out and L</p>



<p>and we&#8217;ll do two of those run the program again and then I won&#8217;t get</p>



<p>confused about the messages okay so now we&#8217;re inside of the point module and</p>



<p>nothing is happening so points let me get rid of the make file here and</p>



<p>and we&#8217;re just printing a welcome message nothing else so now let&#8217;s print</p>



<p>the received string so what am I talking about so we&#8217;re gonna print a prefix</p>



<p>basically saying hey we received the following string right so if you look at</p>



<p>the symbol message received string it&#8217;s just gonna say we&#8217;re now printing the</p>



<p>received string and then it&#8217;ll print it so what are we actually printing we&#8217;re</p>



<p>What is R12? R12 is a character pointer to the print me string. And so basically this</p>



<p>function print null terminated string, it takes a character pointer. So we&#8217;re giving it a character</p>



<p>pointer that we received. When point was called by the driver, notice how it gave a pointer to</p>



<p>the C string. You know, all arrays are basically pointers. They&#8217;re just different syntactically</p>



<p>just different syntactically sometimes so if i declare an array of some length and i give the</p>



<p>symbol somewhere that symbol is really a character pointer so um by calling point with my c string</p>



<p>i&#8217;m calling point inside of the assembly module with this character pointer so that means even</p>



<p>though this c string is owned by the driver by the c plus plus module the assembly module has access</p>



<p>So that means we should be able to print it right now already.</p>



<p>So just the rest of it is just like giving a pointer.</p>



<p>And notice how I&#8217;m not dereferencing R12.</p>



<p>If I did dereferencing around R12, then we would be looking to that address and seeing what&#8217;s there,</p>



<p>which wouldn&#8217;t work for printing a null terminated string.</p>



<p>So let&#8217;s just run it again.</p>



<p>I don&#8217;t know if you can hear him.</p>



<p>This dude is growling at me still because he wants another treat.</p>



<p>He just got denied.</p>



<p>He&#8217;s trying to do it again.</p>



<p>do it again. I let him outside people. He&#8217;s been outside like three times already and he just went</p>



<p>out like two minutes ago. Okay. I love him so much. It hurts my heart and he knows eventually he&#8217;s</p>



<p>going to break me because it hurts my heart or I&#8217;m like too distracted. It&#8217;s like, you know,</p>



<p>pulling the crank on a slot machine in Vegas. You know, eventually something comes out.</p>



<p>That&#8217;s what he does to me. I&#8217;ve accidentally trained him. So now printing the received</p>



<p>Now printing the received string and notice how it prints.</p>



<p>Hello, this is a C string owned by me.</p>



<p>So our assembly module is able to print a C string that was created locally by a C++ module.</p>



<p>So we&#8217;re handing around pointers.</p>



<p>Nice.</p>



<p>Can you hear me?</p>



<p>He&#8217;s getting louder.</p>



<p>So now let&#8217;s modify the incoming long.</p>



<p>Can you shush your freaking pants, please?</p>



<p>Shush your pants.</p>



<p>shush your pants you know the sad thing also is he&#8217;s so old that he&#8217;s deaf now</p>



<p>so he used to know what shush your pants meant it meant I&#8217;m not listening to you</p>



<p>and you might as well stop because I&#8217;m not gonna do anything based on your</p>



<p>harassment but now he can&#8217;t hear me say shush your pants so he just harasses me</p>



<p>all day and all night okay um so I&#8217;m gonna copy paste a little bit more code</p>



<p>Modify the incoming long.</p>



<p>So remember again that the point function, it received a pointer to a long.</p>



<p>We&#8217;re calling the long change me on the inside of this, but it&#8217;s coming in as R13.</p>



<p>And if you notice what I&#8217;m doing here is I&#8217;m just saying let&#8217;s increase the long.</p>



<p>So I&#8217;m going to dereference R13 because R13 is a pointer.</p>



<p>So I&#8217;m saying let&#8217;s go to the memory and change the long that is inside of memory.</p>



<p>And we have to specify that it is a keyword.</p>



<p>it as a keyword so that we you know we don&#8217;t confuse the system the system might think are</p>



<p>you modifying a keyword or like a double word or like a word like how big is your data all we know</p>



<p>is it&#8217;s an integer because it&#8217;s the increase instruction so I&#8217;m saying we got a keyword you</p>



<p>know a 64-bit integer sitting at that memory location I want you to dereference it and increase</p>



<p>it and going back to the driver we&#8217;re providing a pointer to our long so the long starts off is 100</p>



<p>and we&#8217;re just giving a pointer to it the next thing that we can do is we can</p>



<p>ask the driver to print our own stuff actually you know what let&#8217;s run the program right now</p>



<p>just to show that the driver can see the change in the long so i&#8217;m going to run it again notice how</p>



<p>first when the driver says hello it sees its own long as 100 then we&#8217;re inside the assembly module</p>



<p>long and then we return to the caller which is the driver notice how at the</p>



<p>very end of the program the driver sees its long as being 101 so we were able to</p>



<p>modify data that was owned by a different module just by passing pointers</p>



<p>and de-referencing them okay cool so now the next thing that we should do is let&#8217;s</p>



<p>ask the driver to print our own stuff that we own because remember if you go</p>



<p>to the very top you know we own some stuff we own some we own a long we own</p>



<p>float, right? So we want to be able to do something with that. So I&#8217;m going to copy paste this,</p>



<p>ask the driver to print our own stuff. So I&#8217;m going to move three items inside of arguments</p>



<p>for a function call. And then I&#8217;m going to make a function call calling the function,</p>



<p>Hey driver, print this again, Hey driver, print this is actually owned by the C++ module.</p>



<p>a pointer to a long and a pointer to a double remember even pointers to doubles are actually</p>



<p>integers so they use the general purpose register so that&#8217;s the three arguments right there rdi rsi</p>



<p>and rdx m and then we&#8217;re giving the first pointer is going to be the c string so message string</p>



<p>inside asm so you can see that&#8217;s this right here and then the next pointer is the long</p>



<p>inside ASM and the third is the float where did I just go I&#8217;m getting confused my dog is harassing</p>



<p>me right now so bad notice how I&#8217;m not dereferencing so like if when we were increasing the incoming</p>



<p>long before R13 was a pointer so we dereferenced while we increased so that we would increase the</p>



<p>actual value and not the pointer and not the pointer&#8217;s memory location but here we&#8217;re not</p>



<p>C++ module the actual pointers to our data. We don&#8217;t want to give it the data itself. We want</p>



<p>to give pointers to the data so we&#8217;re not derefing with the brackets. So then we call it and when we</p>



<p>get back in here it should just be able to print everything. So I&#8217;m going to run it one more time.</p>



<p>We&#8217;re going to make it and run it and so now let&#8217;s see. So here we&#8217;re inside of our assembly module</p>



<p>And then here the assembly module has just called on hey driver print this.</p>



<p>Remember the C++ module doesn&#8217;t actually call this function.</p>



<p>The assembly module calls it.</p>



<p>So we&#8217;re like going back and forth.</p>



<p>We&#8217;re kind of crisscrossing.</p>



<p>So now the drivers print this function says we got the following string.</p>



<p>Notice how that&#8217;s the string that is owned by assembly.</p>



<p>So we define that inside of our data section in the assembly module.</p>



<p>And then it prints the long.</p>



<p>It prints it as hex.</p>



<p>And it just sort of prints the value.</p>



<p>it just sort of prints the value then it prints it as hex again and then prints at the value</p>



<p>i think actually not hex i think this prints the memory location let&#8217;s double check real fast</p>



<p>yeah so remember um in c plus plus i know this is not like a c plus plus video but um</p>



<p>if the long is a pointer then if we just print it without dereferencing it we should see a memory</p>



<p>location so it&#8217;s telling us uh that the long&#8217;s memory location is this and the doubles memory</p>



<p>location is that and if you stare at those two numbers long enough and you understand hex which</p>



<p>And do you understand hex, which you can see my other videos for?</p>



<p>You&#8217;ll see that those memory locations are right next to each other because that&#8217;s the way we define them inside of assembly.</p>



<p>So we now have the ability to have data that is owned by assembly and give it to C++ or C using pointers.</p>



<p>No problem at all.</p>



<p>And then the printing driver thing exits and then the actual driver regains control.</p>



<p>And it just says that it sees it&#8217;s long as 101.</p>



<p>it sees it&#8217;s long as 101 so uh yeah that&#8217;s that&#8217;s pretty much all i wanted to show you for this</p>



<p>now you hopefully are an expert at passing data back and forth between various modules using</p>



<p>pointers we&#8217;re not using references because references are like a little bit a little bit</p>



<p>less compatible pointers are just really easy they totally work in assembly no problem</p>



<p>one more thing i just wanted to show you real fast before we go even though there&#8217;s another</p>



<p>video you should check out for stack alignment I just want you to see what</p>



<p>happens if I remove this extra push-pop pair so now my stack is about eight</p>



<p>bytes off of its previous alignment because you know we&#8217;re not pushing an</p>



<p>extra eight byte value and somewhere inside of the let&#8217;s see print null</p>



<p>terminated string and then the hey driver print this oh and then we go into</p>



<p>like a bunch of C stuff the program should probably crash because anytime</p>



<p>you use a GCC function or a GCC library or something like that the stack has to</p>



<p>be aligned to 16 bytes so if it&#8217;s off by 8 then it&#8217;ll crash and how did I know</p>



<p>that I needed this well I just ran it first and it crashed and then I added</p>



<p>the extra push pop pair and it didn&#8217;t crash and I realized it was definitely</p>



<p>one more time we should get a seg fault yeah we get a seg fault stack alignment oh no with no</p>



<p>description of what&#8217;s going on if you were in gcc you could i mean sorry if you were in gdb you</p>



<p>could probably figure that out eventually but why not just give it a try add another push pop pair</p>



<p>run the program again with no other modifications now it totally works</p>



<p>okay well uh i think that&#8217;s uh that&#8217;s all i have for this video thank you so much for watching i</p>



<p>I hope you learned a little bit of stuff and you had a little bit of fun.</p>



<p>I will see you in the next video.</p>



<p>Hey everybody.</p>



<p>Thanks for watching this video again from the bottom of my heart.</p>



<p>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</p>



<p>or whatever it is you do on the current social media website</p>



<p>that you&#8217;re looking at right now.</p>



<p>It would really mean the world to me</p>



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



<p>this community so we&#8217;ll be able to do more videos longer videos better videos</p>



<p>or just I&#8217;ll be able to keep making videos in general so please do do me a</p>



<p>kindness and 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</p>



<p>just wakes me up and I get filled with joy that&#8217;s exactly what happens every</p>



<p>single time so you could do it as a nice favor to me or you could you control me</p>



<p>up in the middle of the night just subscribe and then I&#8217;ll just wake up I promise that&#8217;s what will</p>



<p>happen also if you look at the middle of the screen right now you should see a QR code which</p>



<p>you can scan in order to go to the website which I think is also named somewhere at the bottom of</p>



<p>this video and 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>for</p>



<p>Clarifications or errata or just future videos that you want to see please leave a comment or if you just want to say hey</p>



<p>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. I&#8217;m like this</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>darkness, which is coming for us all.</p>



<p>Thank you.</p>
<p>The post <a href="https://www.NeuralLantern.com/x86-64-assembly-pointers-dereferencing-explained-hybrid-c-yasm-example/">x86-64 Assembly Pointers &amp; Dereferencing Explained &#8211; Hybrid C++/YASM Example</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-pointers-dereferencing-explained-hybrid-c-yasm-example/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
