<?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>coding tutorial Archives - NeuralLantern.com</title>
	<atom:link href="https://www.NeuralLantern.com/tag/coding-tutorial/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.NeuralLantern.com/tag/coding-tutorial/</link>
	<description></description>
	<lastBuildDate>Mon, 08 Jun 2026 03:26:18 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=7.0</generator>

<image>
	<url>https://www.NeuralLantern.com/wp-content/uploads/2025/04/cropped-2025-04-04-Lantern-03-32x32.png</url>
	<title>coding tutorial Archives - NeuralLantern.com</title>
	<link>https://www.NeuralLantern.com/tag/coding-tutorial/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>How to Perform AVL Tree Rotations &#8211; Step By Step Example</title>
		<link>https://www.NeuralLantern.com/how-to-perform-avl-tree-rotations-step-by-step-example/</link>
					<comments>https://www.NeuralLantern.com/how-to-perform-avl-tree-rotations-step-by-step-example/#respond</comments>
		
		<dc:creator><![CDATA[mike]]></dc:creator>
		<pubDate>Mon, 08 Jun 2026 03:26:17 +0000</pubDate>
				<category><![CDATA[AVL Trees]]></category>
		<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[AVL tree]]></category>
		<category><![CDATA[avl tree example]]></category>
		<category><![CDATA[AVL tree rotation]]></category>
		<category><![CDATA[balance factor]]></category>
		<category><![CDATA[binary search tree]]></category>
		<category><![CDATA[bst]]></category>
		<category><![CDATA[coding tutorial]]></category>
		<category><![CDATA[computer science]]></category>
		<category><![CDATA[data structures]]></category>
		<category><![CDATA[double rotation]]></category>
		<category><![CDATA[self balancing tree]]></category>
		<category><![CDATA[tree rotation]]></category>
		<guid isPermaLink="false">https://www.NeuralLantern.com/?p=387</guid>

					<description><![CDATA[<p>Learn how to rotate an AVL tree in this detailed example. We identify imbalance using balance factors, locate X Y Z nodes, and apply a double right rotation to restore the self-balancing property of the binary search tree.</p>
<p>The post <a href="https://www.NeuralLantern.com/how-to-perform-avl-tree-rotations-step-by-step-example/">How to Perform AVL Tree Rotations &#8211; Step By Step 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 title="How to Perform AVL Tree Rotations - Step By Step Example" width="1380" height="776" src="https://www.youtube.com/embed/StHS7CQa6BQ?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 class="wp-block-paragraph">In this video we walk through a real example of maintaining an AVL tree by performing a rotation. We start with an unbalanced tree, compute balance factors, identify the Z Y and X nodes, and apply a double right rotation to restore balance. Perfect follow-up if you&#8217;ve seen the basics of binary search trees and AVL trees.</p>



<p class="wp-block-paragraph">Watch as we turn an invalid AVL tree into a perfectly balanced one with clear step-by-step instructions.</p>



<p class="wp-block-paragraph">If you&#8217;re learning data structures and algorithms, this practical example will help you understand when and how to rotate.</p>



<p class="wp-block-paragraph">00:00 Introduction to AVL Tree Rotation<br>00:14 Prerequisites and Previous Videos<br>00:36 AVL Trees Overview<br>00:40 Types of Rotations and Balance Factors<br>01:01 Examining the Example Tree<br>01:24 Confirming Binary Search Tree Properties<br>01:27 Computing Balance Factors<br>01:39 Identifying Imbalance at Node 65<br>02:01 Locating the Z Node<br>02:54 Finding Y and X Nodes<br>04:08 Assigning X Y Z Values<br>04:33 Creating ABC In-Order Representation<br>04:58 Drawing the Target Rotation Pattern<br>05:34 Updating Node Values in Pattern<br>06:04 Checking Unaccounted Children<br>07:00 Reattaching Nodes and Performing Rotation<br>07:56 Recomputing Balance Factors<br>08:24 Updating Root Balance Factor<br>08:41 Resulting Perfectly Balanced Tree<br>08:48 Identifying Double Right Rotation<br>09:06 Explaining the Rotation Process<br>10:16 Conclusion and Next Video Teaser<br>11:56 Channel Promotion and Outro<br>15:48 Final Hello and Recap</p>



<p class="wp-block-paragraph">=-=-=-=-=-=-=-=-=</p>



<p class="wp-block-paragraph">Thanks for watching!</p>



<p class="wp-block-paragraph">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 class="wp-block-paragraph">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 class="wp-block-paragraph">Hey there! Let&#8217;s maintain and rotate an AVL tree which is a self-balancing binary search tree.</p>



<p class="wp-block-paragraph">Okay. Alright, so hopefully by now you&#8217;ve seen my other videos. If you haven&#8217;t,</p>



<p class="wp-block-paragraph">you probably won&#8217;t understand this unless you&#8217;ve seen other videos elsewhere.</p>



<p class="wp-block-paragraph">But in my previous videos we talked about what is a binary search tree, how to make one, how to</p>



<p class="wp-block-paragraph">binary search tree, how to make one, how to define one, how to know you&#8217;re looking at one, how to build one, search,</p>



<p class="wp-block-paragraph">insert, remove all the operations and time complexities.</p>



<p class="wp-block-paragraph">And then we talked about AVL trees, which are a self-balancing binary search tree.</p>



<p class="wp-block-paragraph">And we talked about what are the different types of rotations you can do? Why would you rotate?</p>



<p class="wp-block-paragraph">How do you compute balance factors? How do you know when it&#8217;s time to rotate?</p>



<p class="wp-block-paragraph">How do you do the rotation? Stuff like that. So this is just going to be one</p>



<p class="wp-block-paragraph">example tree, and we&#8217;re just going to try to burn through it. If you want to learn more about</p>



<p class="wp-block-paragraph">see my previous videos. Okay, so supposing now that you have a tree that looks like this, let&#8217;s</p>



<p class="wp-block-paragraph">confirm that this is a valid AVL tree. This seems to be a tree. It&#8217;s a connected graph. It&#8217;s got no</p>



<p class="wp-block-paragraph">cycles. It&#8217;s a rooted tree. It&#8217;s a binary tree. There are no more than two children per every</p>



<p class="wp-block-paragraph">single node. And the data is in order. So 14, 22, 36, 41, 48, 55, 65. So this is a valid binary search</p>



<p class="wp-block-paragraph">Now to determine if it&#8217;s a valid AVL tree, we just have to compute balance factors.</p>



<p class="wp-block-paragraph">So in my previous videos, we talked about doing this.</p>



<p class="wp-block-paragraph">So I&#8217;m just going to try to burn through this quickly.</p>



<p class="wp-block-paragraph">I want to put zeros on all the leaves because that&#8217;s pretty easy.</p>



<p class="wp-block-paragraph">This 48 gets a 1.</p>



<p class="wp-block-paragraph">The 65 actually gets a 2.</p>



<p class="wp-block-paragraph">So at this point already, we know there&#8217;s something wrong with our tree.</p>



<p class="wp-block-paragraph">We need to rotate it.</p>



<p class="wp-block-paragraph">This is not a valid AVL tree because we see a balance factor that is worse than, you know,</p>



<p class="wp-block-paragraph">plus or minus 1.</p>



<p class="wp-block-paragraph">61 is going to have a balance factor of, let&#8217;s see, just a 1.</p>



<p class="wp-block-paragraph">And so really the only thing that we need to rotate is going to be this 65 node.</p>



<p class="wp-block-paragraph">This is the node that&#8217;s out of whack.</p>



<p class="wp-block-paragraph">Sometimes you see, you know, the bad balance factor in other areas of the tree,</p>



<p class="wp-block-paragraph">maybe like a little bit higher and also a little bit lower.</p>



<p class="wp-block-paragraph">You want to rotate as low as possible first because sometimes when you rotate low,</p>



<p class="wp-block-paragraph">it fixes stuff that&#8217;s higher.</p>



<p class="wp-block-paragraph">This is actually only true if the thing that you&#8217;re rotating is a direct descendant,</p>



<p class="wp-block-paragraph">Not necessarily a direct child, but just a descendant of the thing that you see that is higher if you have two different nodes that are</p>



<p class="wp-block-paragraph">unrelated in terms of</p>



<p class="wp-block-paragraph">You know, they&#8217;re not direct descendants or ancestors of one another. They&#8217;re sort of like siblings or cousins</p>



<p class="wp-block-paragraph">Then it doesn&#8217;t really matter so much which one you rotate first because they&#8217;re not really going to affect each other</p>



<p class="wp-block-paragraph">but</p>



<p class="wp-block-paragraph">In this case if we rotate, you know starting with the 65 there&#8217;s a chance that the 41 will get fixed up a little bit</p>



<p class="wp-block-paragraph">I haven&#8217;t tried this yet, so I don&#8217;t know for sure, but I have a feeling it might.</p>



<p class="wp-block-paragraph">Let&#8217;s see what happens.</p>



<p class="wp-block-paragraph">So we know that the first node that is out of balance we&#8217;ll call the Z node.</p>



<p class="wp-block-paragraph">So I&#8217;m just going to put a Z here.</p>



<p class="wp-block-paragraph">Let me just duplicate this and we&#8217;ll say Z.</p>



<p class="wp-block-paragraph">Hang on a second.</p>



<p class="wp-block-paragraph">I got some dashes in my pen.</p>



<p class="wp-block-paragraph">There we go.</p>



<p class="wp-block-paragraph">We&#8217;ll make this the Z node.</p>



<p class="wp-block-paragraph">And then we have to find X and Y.</p>



<p class="wp-block-paragraph">We&#8217;ll do that backwards.</p>



<p class="wp-block-paragraph">We&#8217;ll find the Y first and then the X.</p>



<p class="wp-block-paragraph">them by going down one level at a time so the Y node will be a child of Z and</p>



<p class="wp-block-paragraph">we&#8217;ll have to find the child that has the taller subtree in this case there&#8217;s</p>



<p class="wp-block-paragraph">no choice because there&#8217;s no subtree on the right side of 65 so we can only go</p>



<p class="wp-block-paragraph">left but just keep in mind if there was a subtree let&#8217;s say like that then we</p>



<p class="wp-block-paragraph">would still go to the left and select that 48 as the Y node let me just write</p>



<p class="wp-block-paragraph">down Y node here because it would have a taller subtree notice the left subtree</p>



<p class="wp-block-paragraph">in this little example has a height of 2 and the right subtree has a height of 1</p>



<p class="wp-block-paragraph">That subtree has a height of one.</p>



<p class="wp-block-paragraph">So let&#8217;s just get rid of that little extra thing.</p>



<p class="wp-block-paragraph">Same thing for the X.</p>



<p class="wp-block-paragraph">We want to find a child of Y and we&#8217;ll always take the taller subtree.</p>



<p class="wp-block-paragraph">So you know, if there was a subtree over here and maybe like another like node down there,</p>



<p class="wp-block-paragraph">then we would still go to the right to get the 55 because it would have the taller subtree.</p>



<p class="wp-block-paragraph">In this case, there&#8217;s only one choice.</p>



<p class="wp-block-paragraph">So the X node is just going to be this 55 and that&#8217;s it.</p>



<p class="wp-block-paragraph">So let me just put an X right here.</p>



<p class="wp-block-paragraph">are x and y and z i&#8217;m just going to write that down real fast x equals 55 i&#8217;m going to probably</p>



<p class="wp-block-paragraph">change the color here y is equal to 48 uh z is equal to 65 um and then we want to make an in</p>



<p class="wp-block-paragraph">order representation of xyz and just call it abc so i&#8217;m going to say a is equal to something</p>



<p class="wp-block-paragraph">and so is c so a is going to be the least value the value that belongs on the left because again</p>



<p class="wp-block-paragraph">these are supposed to have ordered data like binary search trees so i&#8217;m going to put 48 here</p>



<p class="wp-block-paragraph">and 55 there and then 65 here now abc is the in order representation of xyz now that we&#8217;ve done</p>



<p class="wp-block-paragraph">that i&#8217;m going to draw our target pattern here so let&#8217;s see i&#8217;m going to do i&#8217;m going to do a</p>



<p class="wp-block-paragraph">to connect with some blue lines because that&#8217;s just like such a cool connection.</p>



<p class="wp-block-paragraph">Hang on a second.</p>



<p class="wp-block-paragraph">I put that a little bit too far down.</p>



<p class="wp-block-paragraph">Okay.</p>



<p class="wp-block-paragraph">So we&#8217;ll do that.</p>



<p class="wp-block-paragraph">And then I&#8217;m going to do another connected in line.</p>



<p class="wp-block-paragraph">Okay.</p>



<p class="wp-block-paragraph">I had to make a little edit there.</p>



<p class="wp-block-paragraph">So I&#8217;m going to just continue making this blue line.</p>



<p class="wp-block-paragraph">So I&#8217;m going to do this.</p>



<p class="wp-block-paragraph">And I have to update the values now.</p>



<p class="wp-block-paragraph">the values now. So this 14 on the left, that should be the A node. So we&#8217;re going to see 48,</p>



<p class="wp-block-paragraph">and then the new parent is going to be 55, and then the right child is going to be 65.</p>



<p class="wp-block-paragraph">Always after you fill in these values, ask yourself, have I actually drawn a valid binary</p>



<p class="wp-block-paragraph">search tree? If you haven&#8217;t, meaning if the data is out of order in some way, then you haven&#8217;t</p>



<p class="wp-block-paragraph">drawn a valid binary search tree, and you probably need to try again. So we&#8217;ve got 48 and 55 and 60.</p>



<p class="wp-block-paragraph">is are there any children that are unaccounted for in terms of the input nodes to the pattern</p>



<p class="wp-block-paragraph">that we&#8217;ve just drawn down here, the output pattern.</p>



<p class="wp-block-paragraph">So let me just kind of remind you that there could have been, you know, potentially some</p>



<p class="wp-block-paragraph">nodes, right?</p>



<p class="wp-block-paragraph">Like we could have had a right child of 65, we could have had a left child of 48, and</p>



<p class="wp-block-paragraph">we could have had two children of 55.</p>



<p class="wp-block-paragraph">So there&#8217;s always a potential for four nodes that are unaccounted for.</p>



<p class="wp-block-paragraph">We&#8217;ll just check them one by one real fast here.</p>



<p class="wp-block-paragraph">Look at the 48 first. The 48 had a right child of 55, but that&#8217;s already taken care of in the output pattern, so we don&#8217;t have to worry about that.</p>



<p class="wp-block-paragraph">Now we look at the 55. We could go XYZ if we wanted to, but I like looking at the output pattern.</p>



<p class="wp-block-paragraph">55 had no children, so that&#8217;s fine. And then we look at the 65. The 65 had a left child of 48, but that&#8217;s already in the output pattern.</p>



<p class="wp-block-paragraph">So at this point, all children are accounted for. We don&#8217;t really need to do that much.</p>



<p class="wp-block-paragraph">that were unaccounted for they would need to be attached under the 48 and 65 somewhere and each</p>



<p class="wp-block-paragraph">child would only go in one place without an there&#8217;s only one place that the child could go</p>



<p class="wp-block-paragraph">without invalidating the binary search tree but that&#8217;ll be i&#8217;m going to make a harder example in</p>



<p class="wp-block-paragraph">the next video so now that we&#8217;ve done this we&#8217;re ready to reattach all the nodes are accounted for</p>



<p class="wp-block-paragraph">that we&#8217;re about to remove from the diagram we&#8217;re not actually going to be deleting nodes or creating</p>



<p class="wp-block-paragraph">creating nodes or anything like that in the code we&#8217;re really just disconnecting</p>



<p class="wp-block-paragraph">pointers and reattaching them so I&#8217;m gonna just erase this from the diagram</p>



<p class="wp-block-paragraph">but keeping in mind that the nodes are not really being recreated and I&#8217;ll just</p>



<p class="wp-block-paragraph">put the 55 over here where the old trinode subtree was and then I&#8217;m gonna</p>



<p class="wp-block-paragraph">recompute the balance factors for all of the nodes involved so you know I just</p>



<p class="wp-block-paragraph">the output pattern someone put a 0 48 also 0 because their leaves the 55 it&#8217;s a 0 it&#8217;s perfectly</p>



<p class="wp-block-paragraph">balanced that&#8217;s pretty sweet and now that we&#8217;re done with the nodes that we touched we have to</p>



<p class="wp-block-paragraph">work our way up to the root node we don&#8217;t have to worry about anything over here on the left</p>



<p class="wp-block-paragraph">because those nodes couldn&#8217;t possibly be affected by uh by the rotation we just did because only</p>



<p class="wp-block-paragraph">nodes that are above the rotation will be affected so uh the last one we you know looked at was this</p>



<p class="wp-block-paragraph">basically going to say all right 55 let&#8217;s look up one more parent it&#8217;s going to be the root node</p>



<p class="wp-block-paragraph">the 41 so what&#8217;s the new balance factor of the 41 node it&#8217;s actually going to be better it&#8217;s going</p>



<p class="wp-block-paragraph">to be a zero so with that in mind we&#8217;ve actually created a perfectly balanced binary search tree</p>



<p class="wp-block-paragraph">this is definitely a log time tree super super fast and super super cool and what type of rotation</p>



<p class="wp-block-paragraph">So we had XYZ. If you don&#8217;t have the rotations memorized, keep in mind my previous videos explain all of that with some fun ways to help you remember, hopefully. Maybe some unfun ways.</p>



<p class="wp-block-paragraph">So I&#8217;ll just kind of like reiterate real fast here. There are two nodes that need to be moved into position. Notice how 55 is supposed to be the new parent node, which means the 65 needs to be rotated under the 55, and so does the 48.</p>



<p class="wp-block-paragraph">And so does the 48. So the 48 and the 65 both are going to be rotated into position.</p>



<p class="wp-block-paragraph">That means two nodes are rotated, which means it&#8217;s a double rotation, not a single rotation.</p>



<p class="wp-block-paragraph">Which node was rotated first? I always rotate the Z node first, you know, the node on the very top.</p>



<p class="wp-block-paragraph">And so I&#8217;ll just use the rotation direction of that Z node.</p>



<p class="wp-block-paragraph">So that&#8217;s a regular clockwise rotation.</p>



<p class="wp-block-paragraph">And that&#8217;s a right rotation, so this is a double right rotation.</p>



<p class="wp-block-paragraph">you can do to remember is if you go from y to z you&#8217;re going up into the right so that&#8217;s a right</p>



<p class="wp-block-paragraph">rotation you could also imagine that there is some negative space kind of sitting here on the right</p>



<p class="wp-block-paragraph">side a little right pocket so that&#8217;s a right rotation and i&#8217;m sure there were some other ways</p>



<p class="wp-block-paragraph">that i added in the other video but i can no longer remember them so i&#8217;ll just move on we&#8217;ll</p>



<p class="wp-block-paragraph">say we did a double right rotation in order to get this uh this perfectly balanced binary search tree</p>



<p class="wp-block-paragraph">research tree so I&#8217;m gonna cut the video here in the next video we&#8217;re going to I</p>



<p class="wp-block-paragraph">think probably rotate a linear tree you can kind of see I&#8217;ve already started to</p>



<p class="wp-block-paragraph">draw one here in the next video we&#8217;re gonna look at this like giant gnarly</p>



<p class="wp-block-paragraph">linear tree and we&#8217;re gonna pretend that we have the ABL rotating disabled for a</p>



<p class="wp-block-paragraph">while while we built this tree and then all at once we&#8217;re gonna turn it on and</p>



<p class="wp-block-paragraph">do like a whole bunch of rotations and see what the tree is gonna end up</p>



<p class="wp-block-paragraph">looking like it&#8217;s gonna be pretty ugly and fun anyway thank you so much for</p>



<p class="wp-block-paragraph">Anyway, thank you so much for watching this video.</p>



<p class="wp-block-paragraph">I hope you learned a little bit of stuff and you had a little bit of fun.</p>



<p class="wp-block-paragraph">I&#8217;ll see you in a moment, hopefully.</p>



<p class="wp-block-paragraph">Hey everybody, thanks for watching this video again from the bottom of my heart.</p>



<p class="wp-block-paragraph">I really appreciate it.</p>



<p class="wp-block-paragraph">I do hope you did learn something and have some fun.</p>



<p class="wp-block-paragraph">If you could do me a please, a small little favor, could you please subscribe and follow</p>



<p class="wp-block-paragraph">as you do on the current social media website that you&#8217;re looking at right now.</p>



<p class="wp-block-paragraph">It would really mean the world to me and it&#8217;ll help make more videos and grow this community.</p>



<p class="wp-block-paragraph">So we&#8217;ll be able to do more videos, longer videos, better videos, or just I&#8217;ll be able</p>



<p class="wp-block-paragraph">to keep making videos in general.</p>



<p class="wp-block-paragraph">So please do me a kindness and subscribe.</p>



<p class="wp-block-paragraph">You know, sometimes I&#8217;m sleeping in the middle of the night and I just wake up because I</p>



<p class="wp-block-paragraph">know somebody subscribed or followed.</p>



<p class="wp-block-paragraph">It just wakes me up and I get filled with joy.</p>



<p class="wp-block-paragraph">That&#8217;s exactly what happens every single time.</p>



<p class="wp-block-paragraph">So you could do it as a nice favor to me or you could you could troll me if you want to just wake me up in the middle</p>



<p class="wp-block-paragraph">And I just subscribe and then I&#8217;ll just wake up. I promise that&#8217;s what will happen</p>



<p class="wp-block-paragraph">Also, uh, if you look at the middle of the screen right now</p>



<p class="wp-block-paragraph">You should see a QR code which you can scan in order to go to the website</p>



<p class="wp-block-paragraph">Which I think is also named somewhere at the bottom of this video</p>



<p class="wp-block-paragraph">And it&#8217;ll take you to my main website where you can just kind of like see</p>



<p class="wp-block-paragraph">All the videos I published and the services and tutorials and things that I offer and all that good stuff and</p>



<p class="wp-block-paragraph">and uh if you have a suggestion for uh uh clarifications or errata or just future videos</p>



<p class="wp-block-paragraph">that you want to see please leave a comment or if you just want to say hey what&#8217;s up what&#8217;s going on</p>



<p class="wp-block-paragraph">you know just send me a comment whatever i also wake up for those in the middle of the night i</p>



<p class="wp-block-paragraph">get i wake up in a cold sweat and i&#8217;m like it would really it really mean the world to me i</p>



<p class="wp-block-paragraph">would really appreciate it so again thank you so much for watching this video and um enjoy the cool</p>



<p class="wp-block-paragraph">Enjoy the cool music as I fade into the darkness which is coming for us all.</p>



<p class="wp-block-paragraph">Thank you.</p>



<p class="wp-block-paragraph">Hello there.</p>



<p class="wp-block-paragraph">Hi there.</p>



<p class="wp-block-paragraph">Hi there. Let&#8217;s maintain a self-balancing AVL tree,</p>



<p class="wp-block-paragraph">which is recursive. Hello there. Let&#8217;s,</p>
<p>The post <a href="https://www.NeuralLantern.com/how-to-perform-avl-tree-rotations-step-by-step-example/">How to Perform AVL Tree Rotations &#8211; Step By Step Example</a> appeared first on <a href="https://www.NeuralLantern.com">NeuralLantern.com</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.NeuralLantern.com/how-to-perform-avl-tree-rotations-step-by-step-example/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>AVL Tree Tutorial: Balance Factors and Why They Fix Slow BSTs</title>
		<link>https://www.NeuralLantern.com/avl-tree-tutorial-balance-factors-and-why-they-fix-slow-bsts/</link>
					<comments>https://www.NeuralLantern.com/avl-tree-tutorial-balance-factors-and-why-they-fix-slow-bsts/#respond</comments>
		
		<dc:creator><![CDATA[mike]]></dc:creator>
		<pubDate>Mon, 18 May 2026 06:09:32 +0000</pubDate>
				<category><![CDATA[AVL Trees]]></category>
		<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[AVL tree]]></category>
		<category><![CDATA[AVL tree rotation]]></category>
		<category><![CDATA[AVL trees]]></category>
		<category><![CDATA[balance factor]]></category>
		<category><![CDATA[balanced binary tree]]></category>
		<category><![CDATA[binary search tree]]></category>
		<category><![CDATA[bst]]></category>
		<category><![CDATA[coding tutorial]]></category>
		<category><![CDATA[computer science]]></category>
		<category><![CDATA[data structures]]></category>
		<category><![CDATA[programming tutorial]]></category>
		<category><![CDATA[self balancing binary search tree]]></category>
		<category><![CDATA[tree rotation]]></category>
		<guid isPermaLink="false">https://www.NeuralLantern.com/?p=371</guid>

					<description><![CDATA[<p>AVL trees are self-balancing binary search trees that prevent the tree from becoming unbalanced. We compute balance factors as the absolute value of left subtree height minus right subtree height. If any node has a balance factor of 2 or worse, we rebalance using rotations on trinode subtrees. This keeps search, insert, and other operations efficient at logarithmic time.</p>
<p>The post <a href="https://www.NeuralLantern.com/avl-tree-tutorial-balance-factors-and-why-they-fix-slow-bsts/">AVL Tree Tutorial: Balance Factors and Why They Fix Slow BSTs</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="AVL Tree Tutorial: Balance Factors and Why They Fix Slow BSTs" width="1380" height="776" src="https://www.youtube.com/embed/xfMFNMpGVz0?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 class="wp-block-paragraph">Learn AVL trees in this beginner-friendly introduction. We cover balance factors, why regular BSTs get slow, and how AVL trees stay balanced with rotations. Great for coding interviews and data structure understanding.</p>



<p class="wp-block-paragraph">00:00 AVL Trees Introduction<br>00:00:28 Problems with Regular BSTs<br>00:00:56 AVL Tree Balance Rule<br>00:02:10 Balance Factor Explained<br>00:02:48 Computing Balance Factors<br>00:03:11 Example Tree Analysis<br>00:04:40 Imbalance at 65 Node<br>00:05:08 Invalid AVL Tree<br>00:06:18 Linear Tree Problem<br>00:06:50 Trinode Subtree<br>00:07:40 Selecting Z Y X Nodes<br>00:09:20 Rotation Overview<br>00:10:03 Next Videos Preview<br>00:11:09 Thank You and Subscribe</p>



<p class="wp-block-paragraph">=-=-=-=-=-=-=-=-=</p>



<p class="wp-block-paragraph">Thanks for watching!</p>



<p class="wp-block-paragraph">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 class="wp-block-paragraph">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 class="wp-block-paragraph">Hello there. Let&#8217;s talk about a type of self-balancing binary search tree called an AVL tree.</p>



<p class="wp-block-paragraph">Okay, so by now I hope you&#8217;ve seen my other videos that I&#8217;ve posted</p>



<p class="wp-block-paragraph">previously where we talked about binary search trees, how to define them,</p>



<p class="wp-block-paragraph">terminology, how to know you&#8217;re actually looking at one, how to build them, insert,</p>



<p class="wp-block-paragraph">If you finish those videos already with me, then you know that binary search trees can actually get slow sometimes because if we have bad data, regular binary search trees don&#8217;t really know how to rearrange themselves.</p>



<p class="wp-block-paragraph">They just might become slow depending on how good or bad the data is.</p>



<p class="wp-block-paragraph">The data is totally random.</p>



<p class="wp-block-paragraph">Then on average, it&#8217;ll, you know, the trees will be log time, but that&#8217;s not always the case.</p>



<p class="wp-block-paragraph">Maybe sometimes you have bad data, sorted data, poison data, whatever kind of data.</p>



<p class="wp-block-paragraph">The secret to AVL trees is we&#8217;re going to start with the binary search tree.</p>



<p class="wp-block-paragraph">And so first it has to satisfy all the rules of the binary search tree.</p>



<p class="wp-block-paragraph">And then we add on another rule.</p>



<p class="wp-block-paragraph">The rule is going to be that we don&#8217;t have a valid AVL tree.</p>



<p class="wp-block-paragraph">If the balance factor for any of the nodes, we&#8217;ll talk about balance factors in a second.</p>



<p class="wp-block-paragraph">If the balance factor for any node is two or worse.</p>



<p class="wp-block-paragraph">Meaning if we think the tree is too imbalanced at any node, it&#8217;s not a valid AVL tree.</p>



<p class="wp-block-paragraph">and therefore we must rebalance the tree with something called a rotation.</p>



<p class="wp-block-paragraph">After we do enough rotations and we see that the tree is balanced again, then it&#8217;ll be valid.</p>



<p class="wp-block-paragraph">And so AVL trees sometimes are invalid in some intermediate state.</p>



<p class="wp-block-paragraph">Like if you have an AVL tree and you add some data into it,</p>



<p class="wp-block-paragraph">for a moment it might be an invalid AVL tree and then internally it&#8217;s sort of just like rotating itself and rebalancing itself.</p>



<p class="wp-block-paragraph">Then when the tree is done, you&#8217;ll have a valid AVL tree again, if that makes sense.</p>



<p class="wp-block-paragraph">AVL tree again if that makes sense. So suppose this tree here we can tell this is a valid binary</p>



<p class="wp-block-paragraph">search tree because it follows all the rules that we talked about before. So let&#8217;s start implementing</p>



<p class="wp-block-paragraph">the first rule of an AVL tree which is let&#8217;s compute the balance factors for every single node.</p>



<p class="wp-block-paragraph">How do we compute balance factors? The basic idea is I want to say BF for balance factor</p>



<p class="wp-block-paragraph">the left subtree height minus the right subtree height.</p>



<p class="wp-block-paragraph">If you don&#8217;t know what a subtree or height is,</p>



<p class="wp-block-paragraph">you should probably check out my other videos right now.</p>



<p class="wp-block-paragraph">But basically, we&#8217;re just going to take the absolute value</p>



<p class="wp-block-paragraph">just to get the difference in the height of the left</p>



<p class="wp-block-paragraph">and the height of the right.</p>



<p class="wp-block-paragraph">So if you know binary search trees already,</p>



<p class="wp-block-paragraph">then you know that the leaves, they don&#8217;t have any subtrees.</p>



<p class="wp-block-paragraph">So their balance factor is going to be pretty easy to compute.</p>



<p class="wp-block-paragraph">It&#8217;s just going to be a zero.</p>



<p class="wp-block-paragraph">I should also point out that some other tutorials out there use positive and negative numbers</p>



<p class="wp-block-paragraph">for the balance factor.</p>



<p class="wp-block-paragraph">That&#8217;s okay.</p>



<p class="wp-block-paragraph">I&#8217;m just going to use absolute values here because it&#8217;s a little bit more simple.</p>



<p class="wp-block-paragraph">So all the leaves get a balance factor of zero, which is fine.</p>



<p class="wp-block-paragraph">We&#8217;re looking to see if we can find a balance factor of two or worse to indicate to us that</p>



<p class="wp-block-paragraph">we have an invalid AVL tree.</p>



<p class="wp-block-paragraph">So far, so good.</p>



<p class="wp-block-paragraph">Let&#8217;s look at the 22 node.</p>



<p class="wp-block-paragraph">we&#8217;ll compute the height of the left subtree versus the height of the right subtree.</p>



<p class="wp-block-paragraph">So, you know, I&#8217;m going to highlight its left subtree.</p>



<p class="wp-block-paragraph">That&#8217;s just the 14 node that has a height of 1.</p>



<p class="wp-block-paragraph">And then its right subtree is just the 36 node that also has a height of 1,</p>



<p class="wp-block-paragraph">which means the balance factor for the 22 node is the absolute value of 1 minus 1 is 0.</p>



<p class="wp-block-paragraph">So actually the 22 node is perfectly balanced.</p>



<p class="wp-block-paragraph">If you think about it, that makes sense.</p>



<p class="wp-block-paragraph">Left and right subtrees have the same height.</p>



<p class="wp-block-paragraph">No problem.</p>



<p class="wp-block-paragraph">have the same height no problem so I&#8217;m going to move on let&#8217;s compute the balance factor for the</p>



<p class="wp-block-paragraph">48 node this is a little trickier notice how the right subtree has a height of one and there is no</p>



<p class="wp-block-paragraph">left subtree which means its height is zero so for the 48 node it&#8217;s actually going to be</p>



<p class="wp-block-paragraph">the absolute value of zero minus one or just one so we find our first node that&#8217;s a little bit out</p>



<p class="wp-block-paragraph">imbalance here but avl trees tolerate imbalances of one they don&#8217;t really care we sort of try to</p>



<p class="wp-block-paragraph">make a trade-off between constantly always rotating every single time anything happens</p>



<p class="wp-block-paragraph">which would perhaps burn a little too much cpu versus letting the tree just be imbalanced to a</p>



<p class="wp-block-paragraph">reasonable amount so we could still call this a log tree in the end so we consider this to be okay</p>



<p class="wp-block-paragraph">65 node and compute the balance factor so 65 node has no right subtree so the height is zero there</p>



<p class="wp-block-paragraph">and its left subtree is those two nodes on the left so that has a height of two which means the</p>



<p class="wp-block-paragraph">balance factor for the 65 node is going to be the absolute value of two minus zero is two at this</p>



<p class="wp-block-paragraph">point we are already certain that this is not a valid avl tree and it would need to be rebalanced</p>



<p class="wp-block-paragraph">you know, consider it a valid AVL tree in the future.</p>



<p class="wp-block-paragraph">So invalid AVL tree, still a valid binary search tree.</p>



<p class="wp-block-paragraph">Something has to be done.</p>



<p class="wp-block-paragraph">Normally what I would say is if you find some nodes</p>



<p class="wp-block-paragraph">way down lower in the tree that are imbalanced,</p>



<p class="wp-block-paragraph">then just go ahead and perform a rebalancing</p>



<p class="wp-block-paragraph">or a rotation immediately,</p>



<p class="wp-block-paragraph">because sometimes when you rotate lower in the tree,</p>



<p class="wp-block-paragraph">you&#8217;ll end up fixing nodes that are a little higher.</p>



<p class="wp-block-paragraph">But since we started with this tree</p>



<p class="wp-block-paragraph">and we&#8217;re not kind of, you know,</p>



<p class="wp-block-paragraph">building a tree step-by-step,</p>



<p class="wp-block-paragraph">I just want to compute the balance factor for all of the nodes at the same time.</p>



<p class="wp-block-paragraph">So we&#8217;ll do the same thing for the 41 node, the root node.</p>



<p class="wp-block-paragraph">Its left subtree has a height of two and its right subtree has a height of three.</p>



<p class="wp-block-paragraph">So its balance factor is the absolute value of two minus three is one.</p>



<p class="wp-block-paragraph">So actually the root node is okay.</p>



<p class="wp-block-paragraph">If it was only up to the root node, we would say this is a valid AVL tree</p>



<p class="wp-block-paragraph">and we don&#8217;t really need to do anything.</p>



<p class="wp-block-paragraph">need to do anything however we&#8217;ve already seen that the 65 node is invalid so again uh we we</p>



<p class="wp-block-paragraph">need to do something here&#8217;s another quick example before we move on to actually identifying which</p>



<p class="wp-block-paragraph">nodes to to modify and rotate just real fast i want to show you you know that a binary search</p>



<p class="wp-block-paragraph">tree could actually end up being a linear tree if you had really really really bad data the binary</p>



<p class="wp-block-paragraph">This is slow because this is a linear tree.</p>



<p class="wp-block-paragraph">The time complexity of searching through this tree is linear time.</p>



<p class="wp-block-paragraph">It scales linearly with the number of nodes in your data set.</p>



<p class="wp-block-paragraph">That&#8217;s really far away from log time, which is supposed to be lightning fast.</p>



<p class="wp-block-paragraph">So an AVL tree will fix this kind of bad data.</p>



<p class="wp-block-paragraph">Let&#8217;s take a step back here and focus your attention for a second on these three nodes.</p>



<p class="wp-block-paragraph">We noticed that there is one node that&#8217;s actually out of whack.</p>



<p class="wp-block-paragraph">It&#8217;s the 65 node, right?</p>



<p class="wp-block-paragraph">What we&#8217;re going to do is we&#8217;re going to find a trinode subtree that starts with the first node or the lowest node we can find that&#8217;s out of whack.</p>



<p class="wp-block-paragraph">It&#8217;s the 65 node.</p>



<p class="wp-block-paragraph">And you can tell that if we just kind of go down a level and down another level to select the other two nodes,</p>



<p class="wp-block-paragraph">it&#8217;s just going to be this little, you know, subtree of three nodes or otherwise known as a trinode subtree.</p>



<p class="wp-block-paragraph">So here&#8217;s kind of a diagram for that.</p>



<p class="wp-block-paragraph">Whoops, forgot to delete that earlier.</p>



<p class="wp-block-paragraph">Let me get rid of that.</p>



<p class="wp-block-paragraph">So this is the trinode subtree that we selected, right?</p>



<p class="wp-block-paragraph">So we&#8217;ve got the 65 and the 48 and the 55.</p>



<p class="wp-block-paragraph">How did we actually select this?</p>



<p class="wp-block-paragraph">So what you kind of want to do is the first node that is out of whack,</p>



<p class="wp-block-paragraph">you want to call that Z.</p>



<p class="wp-block-paragraph">So I&#8217;m going to write a Z here.</p>



<p class="wp-block-paragraph">What we&#8217;re looking for is a trinode subtree,</p>



<p class="wp-block-paragraph">which we&#8217;ll first call X and Y and Z.</p>



<p class="wp-block-paragraph">And then we&#8217;ll end up reordering the nodes</p>



<p class="wp-block-paragraph">and then rearranging all the pointers to the nodes</p>



<p class="wp-block-paragraph">so that the trinode subtree is a little bit more in balance.</p>



<p class="wp-block-paragraph">more in balance. So we see the Z node. We have to find the Y node next. To find the Y node,</p>



<p class="wp-block-paragraph">what&#8217;s going on with my computer? Oh, there we go. We have the Z node. To find the Y node,</p>



<p class="wp-block-paragraph">really, we&#8217;re just going to take the child of the 65 node that has the taller subtree. So</p>



<p class="wp-block-paragraph">there&#8217;s only one subtree, you know, on the left, there&#8217;s no subtree on the right of the 65 node.</p>



<p class="wp-block-paragraph">So the choice is obvious. But just so you know, if there was, you know, another, you know, child</p>



<p class="wp-block-paragraph">another you know child hanging off of the right for some reason we would still choose the number</p>



<p class="wp-block-paragraph">48 node as the y node because it has the taller subtree notice how the subtree on the left is two</p>



<p class="wp-block-paragraph">and the subtree on the right has a height of one so always take the taller subtree when you&#8217;re</p>



<p class="wp-block-paragraph">looking for uh y and then x so then we do the same thing we go down to the grandchild of the z node</p>



<p class="wp-block-paragraph">and if we had a choice to go left or right again we would always choose the taller subtree in this</p>



<p class="wp-block-paragraph">taller subtree in this case there&#8217;s only one choice so that x node is going to be the 55</p>



<p class="wp-block-paragraph">now in this next slide i&#8217;ve kind of redrawn this in a little bit different of a way</p>



<p class="wp-block-paragraph">notice the trinode subtree that we just selected here the 65 48 55 subtree it has a height of three</p>



<p class="wp-block-paragraph">i mean that should be obvious right it&#8217;s just got a height of three</p>



<p class="wp-block-paragraph">or height. Notice on the left, these are the same three nodes. Exactly. 48, 55, and 65. That&#8217;s the</p>



<p class="wp-block-paragraph">same three nodes that we originally had. But notice how the height is too. So the basic idea for ABL</p>



<p class="wp-block-paragraph">trees is, you know, compute the balance factors. And as soon as you identify a node that&#8217;s out of</p>



<p class="wp-block-paragraph">whack, you grab a tri-node sub-tree starting at the node that&#8217;s out of whack, the Z node.</p>



<p class="wp-block-paragraph">And then we&#8217;ll perform, quote unquote, a rotation where we just kind of smoosh it to be a little</p>



<p class="wp-block-paragraph">be a little bit more balanced and a little bit more a little bit shorter so the height goes from</p>



<p class="wp-block-paragraph">three to two once we do that the rest of the tree it&#8217;s going to get a little bit better you&#8217;ll notice</p>



<p class="wp-block-paragraph">that this will end up sometimes completely but sometimes partially fixing bad balance factors</p>



<p class="wp-block-paragraph">in the tree so um this is the end of the intro i only wanted to spend a little time just kind of</p>



<p class="wp-block-paragraph">like warming you up to the idea in the next videos uh we&#8217;re going to actually look at performing</p>



<p class="wp-block-paragraph">identifying the four different types of rotations that you could see and like how to do them.</p>



<p class="wp-block-paragraph">We&#8217;ll talk more about the idea that we&#8217;re really just rearranging pointers here.</p>



<p class="wp-block-paragraph">If you&#8217;re thinking about code, you&#8217;re thinking about, oh, I&#8217;ve got some pointers between these</p>



<p class="wp-block-paragraph">nodes. We&#8217;re not going to delete any nodes or add any nodes. We&#8217;re just going to like</p>



<p class="wp-block-paragraph">rearrange who is a parent of who, who is a child of who. And then eventually we&#8217;ll deal with some</p>



<p class="wp-block-paragraph">really bad trees and we&#8217;ll become experts at just like maintaining ABL trees.</p>



<p class="wp-block-paragraph">talk about ways to visualize the rotations, which makes it a little bit easier to remember these</p>



<p class="wp-block-paragraph">patterns and stuff. Okay, so thank you for watching this video. I hope you learned a little bit of</p>



<p class="wp-block-paragraph">stuff and had a little bit of fun. In the next one, we&#8217;re going to get started. We&#8217;re going to</p>



<p class="wp-block-paragraph">go deep. I&#8217;m going to get my gear. Sorry.</p>



<p class="wp-block-paragraph">Hey, everybody. Thanks for watching this video again from the bottom of my heart. I really</p>



<p class="wp-block-paragraph">from the bottom of my heart. I really appreciate it. I do hope you did learn something and have</p>



<p class="wp-block-paragraph">some fun. If you could do me a please, a small little favor, could you please subscribe and</p>



<p class="wp-block-paragraph">follow this channel or these videos or whatever it is you do on the current social media website</p>



<p class="wp-block-paragraph">that you&#8217;re looking at right now? It would really mean the world to me and it&#8217;ll help make more</p>



<p class="wp-block-paragraph">videos and grow this community. So we&#8217;ll be able to do more videos, longer videos, better videos,</p>



<p class="wp-block-paragraph">Do me a kindness and subscribe.</p>



<p class="wp-block-paragraph">You know, sometimes I&#8217;m sleeping in the middle of the night and I just wake up because I know somebody subscribed or followed.</p>



<p class="wp-block-paragraph">It just wakes me up and I get filled with joy.</p>



<p class="wp-block-paragraph">That&#8217;s exactly what happens every single time.</p>



<p class="wp-block-paragraph">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 class="wp-block-paragraph">Just subscribe and then I&#8217;ll just wake up.</p>



<p class="wp-block-paragraph">I promise that&#8217;s what will happen.</p>



<p class="wp-block-paragraph">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 class="wp-block-paragraph">in order to go to the website which i think is also named somewhere at the bottom of this video</p>



<p class="wp-block-paragraph">and it&#8217;ll take you to my main website where you can just kind of like see all the videos i published</p>



<p class="wp-block-paragraph">and the services and tutorials and things that i offer and all that good stuff and uh</p>



<p class="wp-block-paragraph">if you have a suggestion for uh uh clarifications or errata or just future videos that you want to</p>



<p class="wp-block-paragraph">see please leave a comment or if you just want to say hey what&#8217;s up what&#8217;s going on you know</p>



<p class="wp-block-paragraph">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 class="wp-block-paragraph">it would really it really mean the world to me. I would really appreciate it. So again thank you</p>



<p class="wp-block-paragraph">so much for watching this video and enjoy the cool music as I fade into the darkness</p>



<p class="wp-block-paragraph">which is coming for us all.</p>



<p class="wp-block-paragraph">Thank you.</p>
<p>The post <a href="https://www.NeuralLantern.com/avl-tree-tutorial-balance-factors-and-why-they-fix-slow-bsts/">AVL Tree Tutorial: Balance Factors and Why They Fix Slow BSTs</a> appeared first on <a href="https://www.NeuralLantern.com">NeuralLantern.com</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.NeuralLantern.com/avl-tree-tutorial-balance-factors-and-why-they-fix-slow-bsts/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Mastering x86-64 YASM Assembly: Functions Made Simple</title>
		<link>https://www.NeuralLantern.com/mastering-x86-64-yasm-assembly-functions-made-simple/</link>
					<comments>https://www.NeuralLantern.com/mastering-x86-64-yasm-assembly-functions-made-simple/#respond</comments>
		
		<dc:creator><![CDATA[mike]]></dc:creator>
		<pubDate>Sun, 24 Aug 2025 05:33:36 +0000</pubDate>
				<category><![CDATA[Assembly Language]]></category>
		<category><![CDATA[Coding]]></category>
		<category><![CDATA[Computer Science]]></category>
		<category><![CDATA[Videos]]></category>
		<category><![CDATA[ABI compliance]]></category>
		<category><![CDATA[assembly programming]]></category>
		<category><![CDATA[assembly tutorial]]></category>
		<category><![CDATA[coding tutorial]]></category>
		<category><![CDATA[function calls]]></category>
		<category><![CDATA[low-level programming]]></category>
		<category><![CDATA[programming basics]]></category>
		<category><![CDATA[system calls]]></category>
		<category><![CDATA[x86-64 assembly]]></category>
		<category><![CDATA[Yasm assembly]]></category>
		<guid isPermaLink="false">https://www.NeuralLantern.com/?p=214</guid>

					<description><![CDATA[<p>Master x86-64 YASM assembly functions! Learn to write, call, and manage arguments &#038; returns in this beginner-friendly tutorial. Avoid crashes &#038; respect the ABI.</p>
<p>The post <a href="https://www.NeuralLantern.com/mastering-x86-64-yasm-assembly-functions-made-simple/">Mastering x86-64 YASM Assembly: Functions Made Simple</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="Mastering x86-64 YASM Assembly: Functions Made Simple" width="1380" height="776" src="https://www.youtube.com/embed/KUshn1VczCA?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 class="wp-block-paragraph">Get hands-on with x86-64 YASM assembly in this in-depth tutorial on writing functions! Perfect for beginners and seasoned coders, we cover the basics of function creation, calling conventions, argument handling, and return values. Learn to avoid crashes, respect the ABI, and use prologue/epilogue for clean code. From printing messages to returning integers, see practical examples and tips to simplify your assembly programs. Subscribe for more low-level programming tutorials and take your skills to the next level!</p>



<p class="wp-block-paragraph">Introduction to Functions 00:00:00<br>Makefile Overview 00:00:19<br>Assembly Program Setup 00:02:01<br>Data Section Definitions 00:02:08<br>Text Section and Entry Point 00:03:02<br>Basic Assembly Program Demo 00:03:38<br>Return Codes Explained 00:04:30<br>Function Concepts Introduced 00:06:10<br>Creating a Simple Function 00:07:04<br>Function Call vs Jump 00:08:00<br>Adding Return Statement 00:09:06<br>Moving Print Logic to Function 00:09:48<br>Benefits of Functions 00:10:01<br>Calling Function Multiple Times 00:11:58<br>Creating Print Function with Arguments 00:14:00<br>Handling Function Arguments 00:15:45<br>Respecting the ABI 00:17:16<br>Prologue and Epilogue 00:23:15<br>Stack Operations 00:24:34<br>Calling Print Function 00:25:25<br>Avoiding Recursive Loop 00:34:17<br>Modifying Print Function to Print Line 00:35:21<br>Adding CRLF Function 00:30:13<br>Printing Multiple Messages 00:37:00<br>Returning Integer Values 00:37:48<br>Preserving Registers in Entry Point 00:41:50<br>Final Program Demo 00:43:24<br>Conclusion and Call to Subscribe 00:45:16</p>



<p class="wp-block-paragraph">Thanks for watching!</p>



<p class="wp-block-paragraph">Find us on other social media here:</p>



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



<p class="wp-block-paragraph">Please help support us!</p>



<ul class="wp-block-list">
<li>Subscribing + Sharing on Social Media</li>



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



<li>Subscribing to our Blog</li>



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



<p class="wp-block-paragraph">Hey there! Let&#8217;s talk about functions in x8664 Yasm Assembly.</p>



<p class="wp-block-paragraph">I&#8217;m just going to give you the basics of how to write a function.</p>



<p class="wp-block-paragraph">So let&#8217;s get started here. First thing I want to show you is just a makefile.</p>



<p class="wp-block-paragraph">This is not a makefile video, so you don&#8217;t have to worry about it too much.</p>



<p class="wp-block-paragraph">I just have it up because I need it for this demo.</p>



<p class="wp-block-paragraph">If you&#8217;re interested in makefiles, that would be a really, really good thing.</p>



<p class="wp-block-paragraph">thing i&#8217;ll probably make videos in the future about them but for now we&#8217;ll just say you hopefully have</p>



<p class="wp-block-paragraph">a script or method already set up that allows you to compile or assemble assembly programs</p>



<p class="wp-block-paragraph">so i&#8217;m just going to skim through it real fast if you&#8217;re interested you can kind of study it</p>



<p class="wp-block-paragraph">but basically i have like a little define here that allows me to print out a message that looks</p>



<p class="wp-block-paragraph">nice and then i define the repo path as the current path and i take the absolute path of that</p>



<p class="wp-block-paragraph">This is where the real work happens.</p>



<p class="wp-block-paragraph">I have Yasm flags because I&#8217;m using the Yasm assembler.</p>



<p class="wp-block-paragraph">I&#8217;m going to be assembling to x86 64.</p>



<p class="wp-block-paragraph">I like to out.</p>



<p class="wp-block-paragraph">I like to convert all of my warnings to errors to make sure that I don&#8217;t write sloppy code</p>



<p class="wp-block-paragraph">export debug symbols and export debug symbols for the linking stage to the name of my executable</p>



<p class="wp-block-paragraph">is going to be called a main.</p>



<p class="wp-block-paragraph">have set up and well I can just do make run or make debug as a little shortcut</p>



<p class="wp-block-paragraph">to run it in GDB if you don&#8217;t know GDB don&#8217;t worry that&#8217;s going to happen in a</p>



<p class="wp-block-paragraph">future video and make clean which just cleans the build area I like a clean</p>



<p class="wp-block-paragraph">build area and then this is the main command here we&#8217;re going to be using LD</p>



<p class="wp-block-paragraph">to do our linking because this is going to be a pure assembly program instead of</p>



<p class="wp-block-paragraph">a hybrid program which would require GCC or something else and then here&#8217;s my</p>



<p class="wp-block-paragraph">And then here&#8217;s my program main ASM is going to get compiled down to main.o and</p>



<p class="wp-block-paragraph">That&#8217;s it for the make pile</p>



<p class="wp-block-paragraph">Okay, so for the main assembly program I have some stuff written up already here, but I think I&#8217;m going to modify it for this video</p>



<p class="wp-block-paragraph">For starters, we have our data section which hopefully you understand already</p>



<p class="wp-block-paragraph">I&#8217;m just going to define a bunch of stuff in the data section like I&#8217;m going to define</p>



<p class="wp-block-paragraph">The system call code to write a string so I can print a message pretty easily</p>



<p class="wp-block-paragraph">so I can print a message pretty easily the system call code to actually exit the program</p>



<p class="wp-block-paragraph">remember when you&#8217;re writing a pure assembly program that has start as an entry point</p>



<p class="wp-block-paragraph">you need to be you know you&#8217;re responsible for exiting the program so the system call code is</p>



<p class="wp-block-paragraph">60 there and then I&#8217;m going to say exiting for success is a zero that&#8217;s kind of a standard thing</p>



<p class="wp-block-paragraph">and then I&#8217;m going to make two file descriptors for the standard input it&#8217;s going to be zero and</p>



<p class="wp-block-paragraph">the standard output it&#8217;s going to be one actually I don&#8217;t think we need that for this video we just</p>



<p class="wp-block-paragraph">We just need standard output, but not standard input.</p>



<p class="wp-block-paragraph">And then I&#8217;m just going to make two strings.</p>



<p class="wp-block-paragraph">That&#8217;s not my name, but I like those names.</p>



<p class="wp-block-paragraph">And then the BSS section.</p>



<p class="wp-block-paragraph">Actually, I&#8217;m going to get rid of that</p>



<p class="wp-block-paragraph">because we don&#8217;t really need that for this video.</p>



<p class="wp-block-paragraph">And then I have a text section, which is where all the code is.</p>



<p class="wp-block-paragraph">We&#8217;re going to begin our entry point.</p>



<p class="wp-block-paragraph">If you are writing a pure assembly program</p>



<p class="wp-block-paragraph">and this is your entry point module,</p>



<p class="wp-block-paragraph">make a function called underscore start.</p>



<p class="wp-block-paragraph">It&#8217;s not actually technically going to be a function because it won&#8217;t return anywhere.</p>



<p class="wp-block-paragraph">It&#8217;ll actually just call the system exit service.</p>



<p class="wp-block-paragraph">And then I mark start as global so that the operating system can go into it,</p>



<p class="wp-block-paragraph">can call it from the outside.</p>



<p class="wp-block-paragraph">And then all I&#8217;m doing right now is I&#8217;m printing a hello.</p>



<p class="wp-block-paragraph">So far, this is not really a demo of functions.</p>



<p class="wp-block-paragraph">This is just like a very basic pure assembly program.</p>



<p class="wp-block-paragraph">All it&#8217;s going to do is it&#8217;s going to enter.</p>



<p class="wp-block-paragraph">going to print the hello message and then down here it&#8217;s going to do a system call to exit the</p>



<p class="wp-block-paragraph">program with zero for success so let&#8217;s just see what that looks like real fast I&#8217;m going to do</p>



<p class="wp-block-paragraph">make run and then make run you can see the first line it just kind of prints and then the second</p>



<p class="wp-block-paragraph">line that&#8217;s make sort of echoing the command that I&#8217;m issuing it&#8217;s just going to assemble the main</p>



<p class="wp-block-paragraph">dot asm into a main dot o and then it&#8217;s letting me know that if there are any warnings it will</p>



<p class="wp-block-paragraph">It will refuse to compile.</p>



<p class="wp-block-paragraph">And then I use the LD linker to link the main executable.</p>



<p class="wp-block-paragraph">And then I actually run the executable.</p>



<p class="wp-block-paragraph">So only below this line that I have highlighted right now</p>



<p class="wp-block-paragraph">is gonna be the actual program.</p>



<p class="wp-block-paragraph">So you can see all it really did was just,</p>



<p class="wp-block-paragraph">you know, print the message and then exit.</p>



<p class="wp-block-paragraph">Just, you know, a little note here.</p>



<p class="wp-block-paragraph">I&#8217;ve probably said this in other videos,</p>



<p class="wp-block-paragraph">but the return code zero is kind of a standard</p>



<p class="wp-block-paragraph">to indicate success for programs,</p>



<p class="wp-block-paragraph">which is very useful if you want programs to automate other programs.</p>



<p class="wp-block-paragraph">If I change the system, let&#8217;s see what is it, exit success.</p>



<p class="wp-block-paragraph">If I just change that variable to a 3 so that I will exit with a return code of 3 instead of 0,</p>



<p class="wp-block-paragraph">then the operating system, or actually bash, since we&#8217;re in a terminal emulator,</p>



<p class="wp-block-paragraph">should consider the program to have exited in error.</p>



<p class="wp-block-paragraph">So you can see now it&#8217;s like, hey, hey, hey, the makefile system,</p>



<p class="wp-block-paragraph">oh not bash, the makefile system is like that program exited with code 3,</p>



<p class="wp-block-paragraph">three something went wrong and normally if you don&#8217;t add any extra stuff into your make file it&#8217;ll</p>



<p class="wp-block-paragraph">just refuse to continue at that point well so i&#8217;m just going to change it back to a zero and make</p>



<p class="wp-block-paragraph">sure that it still works and then we&#8217;ll get on to actually writing functions so i&#8217;m going to go clear</p>



<p class="wp-block-paragraph">and make run so it&#8217;s a little bit faster for me to run this repeatedly you can see actually the</p>



<p class="wp-block-paragraph">first time that i did that um the make file recompiled notice how it recompiled um and</p>



<p class="wp-block-paragraph">and relinked because I changed that source code.</p>



<p class="wp-block-paragraph">This is not a make file video, but I just want you to be aware.</p>



<p class="wp-block-paragraph">One of the great reasons to use make or a build system in general</p>



<p class="wp-block-paragraph">is because it makes it really easy to compile your code faster.</p>



<p class="wp-block-paragraph">Notice how if I didn&#8217;t actually change, let&#8217;s see.</p>



<p class="wp-block-paragraph">So I recompiled it because I just changed it again,</p>



<p class="wp-block-paragraph">but now I haven&#8217;t changed it since I&#8217;m going to recompile or rerun.</p>



<p class="wp-block-paragraph">Make just kind of like skips reassembling that file because it hasn&#8217;t changed.</p>



<p class="wp-block-paragraph">it can make your compilations and assemblies much, much faster.</p>



<p class="wp-block-paragraph">So anyway, functions. Functions in assembly are kind of simple and you&#8217;ll realize that</p>



<p class="wp-block-paragraph">some of the stuff that we take for granted in higher level languages is being done for us</p>



<p class="wp-block-paragraph">automatically by the compiler. But in assembly, we have labels. So for example, this start entry</p>



<p class="wp-block-paragraph">some valid symbols like characters underscore numbers I don&#8217;t think you can start a label with</p>



<p class="wp-block-paragraph">a number correct me if I&#8217;m wrong but I don&#8217;t think you can so we have like a label which is just like</p>



<p class="wp-block-paragraph">this collection of characters and then we have a colon after it and so you can jump into a label</p>



<p class="wp-block-paragraph">which I&#8217;ll talk about in a different video but you can jump into a label you can call a label as a</p>



<p class="wp-block-paragraph">function and so a label isn&#8217;t actually a function by itself you have to treat it in a certain way</p>



<p class="wp-block-paragraph">the abstract so notice how there&#8217;s no return statement at the bottom here let me write a</p>



<p class="wp-block-paragraph">function real fast and maybe i will have it so that i have a function that just says hello for</p>



<p class="wp-block-paragraph">me so i&#8217;ll make a label down here function that says hello and i&#8217;ll give it a label and i&#8217;ll say</p>



<p class="wp-block-paragraph">maybe like say hello and i&#8217;ll do a little label there first thing that you should do when you are</p>



<p class="wp-block-paragraph">trying to create a function is just put a return statement at the very end of it.</p>



<p class="wp-block-paragraph">That way it will return to the caller because if you don&#8217;t then it&#8217;s just going to continue</p>



<p class="wp-block-paragraph">to execute downward until it reaches no code and then the program will probably crash. In fact</p>



<p class="wp-block-paragraph">maybe I should do that. I&#8217;m going to do say hello as a label with no return so it&#8217;s not really a</p>



<p class="wp-block-paragraph">function now. It&#8217;s more of just like a label that you can jump to but there&#8217;s no code under it so</p>



<p class="wp-block-paragraph">I&#8217;m not even sure this will assemble, but it&#8217;ll definitely crash if we can call into this.</p>



<p class="wp-block-paragraph">So let me do, at the very start of our program, I&#8217;m going to type call say hello.</p>



<p class="wp-block-paragraph">Or actually, you know what, maybe first I&#8217;ll jump to hello.</p>



<p class="wp-block-paragraph">Jumping is going to be for a different video, but jumping just means go to that place.</p>



<p class="wp-block-paragraph">It&#8217;s a go-to statement, essentially.</p>



<p class="wp-block-paragraph">You&#8217;re not supposed to be able to return from a jump.</p>



<p class="wp-block-paragraph">but it doesn&#8217;t really follow the same design pattern logic as a function.</p>



<p class="wp-block-paragraph">So I&#8217;m going to immediately jump to the say hello logic and see what happens.</p>



<p class="wp-block-paragraph">I think that it&#8217;ll crash.</p>



<p class="wp-block-paragraph">Here we go.</p>



<p class="wp-block-paragraph">SegFault, core dumped, oh no.</p>



<p class="wp-block-paragraph">So, hey, it crashed.</p>



<p class="wp-block-paragraph">So instead of jumping there, let&#8217;s do a call.</p>



<p class="wp-block-paragraph">Now we&#8217;re treating it a little bit more like a function.</p>



<p class="wp-block-paragraph">We still don&#8217;t have the return statement down here.</p>



<p class="wp-block-paragraph">So it should still crash, I think.</p>



<p class="wp-block-paragraph">Let&#8217;s try that again.</p>



<p class="wp-block-paragraph">Yep, it crashed again.</p>



<p class="wp-block-paragraph">Because you&#8217;re supposed to exit from the operator, or you&#8217;re supposed to exit from the program</p>



<p class="wp-block-paragraph">properly to the operating system.</p>



<p class="wp-block-paragraph">So now I&#8217;m going to do a return statement here.</p>



<p class="wp-block-paragraph">There&#8217;s never an argument for the return statement.</p>



<p class="wp-block-paragraph">You just simply return to the caller.</p>



<p class="wp-block-paragraph">This uses the stack.</p>



<p class="wp-block-paragraph">If you don&#8217;t know what the stack is, I&#8217;ll probably make another video about that in</p>



<p class="wp-block-paragraph">the future.</p>



<p class="wp-block-paragraph">But the stack, you know, it helps your program understand where it just called to and how</p>



<p class="wp-block-paragraph">to get back from it.</p>



<p class="wp-block-paragraph">to get back from it it also stores local variables and things but so if i have a call it should jump</p>



<p class="wp-block-paragraph">down to the hello function and then return meaning it&#8217;ll just return to the caller immediately without</p>



<p class="wp-block-paragraph">actually doing anything but the program should be able to continue without crashing notice how</p>



<p class="wp-block-paragraph">there&#8217;s no crash here all it does is print out the welcome message and then no problem all right</p>



<p class="wp-block-paragraph">printing stuff and I&#8217;m just going to cut it and stick it inside of the say hello</p>



<p class="wp-block-paragraph">function that way when I call that function it should still print hello but</p>



<p class="wp-block-paragraph">now notice how we have less code to deal with in this primary function you know</p>



<p class="wp-block-paragraph">one of the first things that that programmers learn is that you know we&#8217;re</p>



<p class="wp-block-paragraph">human beings we&#8217;re not computers it&#8217;s really hard for us to write complex</p>



<p class="wp-block-paragraph">logic in a program without tools to help us and design patterns to help us to</p>



<p class="wp-block-paragraph">help us stay on track to help us you know make sure that we&#8217;re not going to</p>



<p class="wp-block-paragraph">we&#8217;re not going to be forgetting anything or screwing something up or you know to debug so</p>



<p class="wp-block-paragraph">you know one awesome design pattern is to write functions because you you you realize in your</p>



<p class="wp-block-paragraph">program you might be calling the same logic several times so you&#8217;re sort of like repeating a bunch of</p>



<p class="wp-block-paragraph">code that already is bad it&#8217;s really hard to debug repeating code and it&#8217;s really hard to make updates</p>



<p class="wp-block-paragraph">to it and it becomes unwieldy right so as soon as you realize you&#8217;re repeating the same logic</p>



<p class="wp-block-paragraph">places you should think about taking it and putting that logic into a function</p>



<p class="wp-block-paragraph">and then just simply calling that function many times it makes your coding</p>



<p class="wp-block-paragraph">a lot easier and then once you put all the work into getting a function to</p>



<p class="wp-block-paragraph">work you&#8217;re just you can just be done with it as soon as you&#8217;re sure that</p>



<p class="wp-block-paragraph">this function actually works you can just put it to the side and just forget</p>



<p class="wp-block-paragraph">about it you can stick it at the bottom of the source or just ignore it put it</p>



<p class="wp-block-paragraph">into a different module whatever you want to do and then the other parts of</p>



<p class="wp-block-paragraph">of the program that you&#8217;re still working on they become a lot more simple and so you know because</p>



<p class="wp-block-paragraph">we&#8217;re human beings we need all the advantages we can get to write powerful code so i am now just</p>



<p class="wp-block-paragraph">i&#8217;m not i don&#8217;t have to think anymore about all the system call things that&#8217;s happening</p>



<p class="wp-block-paragraph">to print the hello message all i have to do is think about calling the say hello function</p>



<p class="wp-block-paragraph">and of course this is simple but you can imagine using this for more complicated</p>



<p class="wp-block-paragraph">concepts in the future so let&#8217;s see if this still works if i didn&#8217;t screw this up</p>



<p class="wp-block-paragraph">It printed the same exact message.</p>



<p class="wp-block-paragraph">And if I keep running it, you know, the make file doesn&#8217;t compile as much, but it still</p>



<p class="wp-block-paragraph">runs the program.</p>



<p class="wp-block-paragraph">If you don&#8217;t believe me that this is inside of a function now, let&#8217;s actually just call</p>



<p class="wp-block-paragraph">that same function many times.</p>



<p class="wp-block-paragraph">We&#8217;ll call say hello.</p>



<p class="wp-block-paragraph">Let&#8217;s say we call it five times.</p>



<p class="wp-block-paragraph">This should work.</p>



<p class="wp-block-paragraph">I&#8217;m going to run the program again.</p>



<p class="wp-block-paragraph">Notice how it printed that message five times.</p>



<p class="wp-block-paragraph">Just to reemphasize my earlier point, wouldn&#8217;t it be a huge pain in the butt if you wrote</p>



<p class="wp-block-paragraph">all the code for the system call and the message printing five duplicate times?</p>



<p class="wp-block-paragraph">Wouldn&#8217;t that be hard to update? What if you had a hundred of those in there?</p>



<p class="wp-block-paragraph">What if you had something that was really complicated and you called on it like</p>



<p class="wp-block-paragraph">50 times,</p>



<p class="wp-block-paragraph">wouldn&#8217;t that be so much better than maintaining 50 different versions of the</p>



<p class="wp-block-paragraph">exact same idea of code and, and, you know,</p>



<p class="wp-block-paragraph">making sure that if you needed to upgrade it or change it in some way,</p>



<p class="wp-block-paragraph">you actually got it right for all 50 copies. So this is way better.</p>



<p class="wp-block-paragraph">All right. So that&#8217;s the basic idea for a function.</p>



<p class="wp-block-paragraph">for a function let&#8217;s see what else can i do um let&#8217;s let&#8217;s make a function that just prints</p>



<p class="wp-block-paragraph">anything because that would be kind of a good little practice in the uh in the arguments that</p>



<p class="wp-block-paragraph">we can use remember in c plus plus and other languages you have function signatures that</p>



<p class="wp-block-paragraph">you can use so for example say hello we know for sure that say hello is not actually doing anything</p>



<p class="wp-block-paragraph">it&#8217;s not receiving any arguments and it&#8217;s also not returning anything so if we know it&#8217;s not</p>



<p class="wp-block-paragraph">returning anything. So if we know it&#8217;s not returning anything, we can say that it&#8217;s a</p>



<p class="wp-block-paragraph">void function if we&#8217;re talking C++. We&#8217;ll do the name here. We&#8217;ll say say hello. And in the</p>



<p class="wp-block-paragraph">argument list, we know that it doesn&#8217;t take any arguments. So it&#8217;s just say hello with nothing,</p>



<p class="wp-block-paragraph">right? Okay. So let&#8217;s make another function that just prints something. And this function will</p>



<p class="wp-block-paragraph">kind of be a little redundant because if you just sort of look at the system call here,</p>



<p class="wp-block-paragraph">we have to load up the system call with the standard output and the system write code.</p>



<p class="wp-block-paragraph">So that&#8217;s going to be repeated.</p>



<p class="wp-block-paragraph">So we could move that into the print something function, but then, you know, giving an argument</p>



<p class="wp-block-paragraph">of like, here&#8217;s the string I want to print.</p>



<p class="wp-block-paragraph">And here&#8217;s the length of the string that I want to print that, that is something that</p>



<p class="wp-block-paragraph">we can pass as arguments every time.</p>



<p class="wp-block-paragraph">So we&#8217;ll be saving like a little bit of work, but it&#8217;s still, I think, I hope illustrates</p>



<p class="wp-block-paragraph">the idea.</p>



<p class="wp-block-paragraph">Maybe we will, oh, print something.</p>



<p class="wp-block-paragraph">Okay.</p>



<p class="wp-block-paragraph">So print something.</p>



<p class="wp-block-paragraph">Maybe we&#8217;ll do the signature here.</p>



<p class="wp-block-paragraph">Print something will not return any value.</p>



<p class="wp-block-paragraph">So we&#8217;ll just give it a void return type.</p>



<p class="wp-block-paragraph">And I&#8217;ll do a description up here.</p>



<p class="wp-block-paragraph">Print a message given by a character pointer.</p>



<p class="wp-block-paragraph">A pointer to a string.</p>



<p class="wp-block-paragraph">character is also a pointer to a C string, not a regular string class, but a C string.</p>



<p class="wp-block-paragraph">So I don&#8217;t know how to describe it. I think I&#8217;ll just say a pointer to a C string</p>



<p class="wp-block-paragraph">and length and integer length maybe. So that means the print something function should take</p>



<p class="wp-block-paragraph">two arguments. We want to be able to call that function and tell it, here&#8217;s a pointer to the</p>



<p class="wp-block-paragraph">string I want you to print. And here&#8217;s how long the string is. So still talking about C++, I&#8217;m</p>



<p class="wp-block-paragraph">character pointer p and maybe i&#8217;ll put like string just to remind myself when i look at this later</p>



<p class="wp-block-paragraph">it&#8217;s a pointer to the string or like p char for the pointer to like the first character in the</p>



<p class="wp-block-paragraph">string something like that and then length so the length is going to be a long because we&#8217;re using</p>



<p class="wp-block-paragraph">64-bit integers in this video long and i&#8217;ll just say maybe size or length or something so now we</p>



<p class="wp-block-paragraph">I want to use the same symbol that I&#8217;m describing in this little prototype here.</p>



<p class="wp-block-paragraph">So I&#8217;m just going to copy paste it and use a colon.</p>



<p class="wp-block-paragraph">And then because it&#8217;s a function, I immediately want to put a return statement at the very end.</p>



<p class="wp-block-paragraph">Okay.</p>



<p class="wp-block-paragraph">So then what I need to do is grab the incoming arguments.</p>



<p class="wp-block-paragraph">Okay.</p>



<p class="wp-block-paragraph">So, well, the first thing that every system call that prints a string should do,</p>



<p class="wp-block-paragraph">at least if it&#8217;s going to print it to standard output,</p>



<p class="wp-block-paragraph">to print it to standard output is it should start setting up the system call code registers or the</p>



<p class="wp-block-paragraph">system call registers to say all right the system call wants rax to describe what the system call</p>



<p class="wp-block-paragraph">is going to do we already have system write set up so that just means we&#8217;re telling syscall that</p>



<p class="wp-block-paragraph">we want to print something where do we want to print it we want to print to the standard output</p>



<p class="wp-block-paragraph">which if you look at earlier in the video that was just a file descriptor of one so that&#8217;s fine</p>



<p class="wp-block-paragraph">up the system call and then the next two things can be kind of variable uh you know we can we</p>



<p class="wp-block-paragraph">can grab that from incoming arguments so rsi wants a pointer to the string we&#8217;re not going to load</p>



<p class="wp-block-paragraph">hello string but we&#8217;re going to load whatever character pointer we were given in the incoming</p>



<p class="wp-block-paragraph">RDI is the first incoming integer argument.</p>



<p class="wp-block-paragraph">We&#8217;re not going to talk about mixed arguments with floats.</p>



<p class="wp-block-paragraph">If we just assume that we&#8217;re only going to use integer arguments or pointer arguments,</p>



<p class="wp-block-paragraph">then the first one is going to be RDI.</p>



<p class="wp-block-paragraph">So that means by the time print something gets called,</p>



<p class="wp-block-paragraph">by the time we come into this code right here,</p>



<p class="wp-block-paragraph">RDI should be loaded with the pointer to the character to the C string that we want to print.</p>



<p class="wp-block-paragraph">Again, this follows something called the ABI, which you should absolutely respect,</p>



<p class="wp-block-paragraph">which you should absolutely respect even if you&#8217;re the only one writing any of the code that you</p>



<p class="wp-block-paragraph">interact with your code will be bad and considered not good if you don&#8217;t respect the abi on this type</p>



<p class="wp-block-paragraph">of architecture so we definitely still need to load rsi with the string pointer but art whoops</p>



<p class="wp-block-paragraph">but rdi is what&#8217;s going to have the string pointer so we actually already just ruined rdi when we</p>



<p class="wp-block-paragraph">probably either load it up backwards or i think probably a better way to explain functions is</p>



<p class="wp-block-paragraph">let&#8217;s just use another register so i&#8217;m going to use another register to sort of store our incoming</p>



<p class="wp-block-paragraph">arguments i personally like to do this i admit that this will cost you cpu cycles to sort of</p>



<p class="wp-block-paragraph">like move things around in registers but it&#8217;s still way faster than sticking something into</p>



<p class="wp-block-paragraph">a global variable because then you&#8217;ll be hitting memory so first i&#8217;m going to say let&#8217;s move</p>



<p class="wp-block-paragraph">to r12 and I want to store the incoming argument so I&#8217;m just going to write here save the</p>



<p class="wp-block-paragraph">the p care you know c string character pointer or something like that so we&#8217;re going to save it</p>



<p class="wp-block-paragraph">and then the next thing we need to do is save the size because as soon as we load up rsi we&#8217;re also</p>



<p class="wp-block-paragraph">going to be destroying the second integer argument which should come to us in rsi</p>



<p class="wp-block-paragraph">Okay, so I&#8217;m going to save that too, I guess, with R13.</p>



<p class="wp-block-paragraph">So I&#8217;m going to move something into R13, RSI, and then save the size of the C string.</p>



<p class="wp-block-paragraph">Okay, so we saved both of those.</p>



<p class="wp-block-paragraph">Now, when we load RSI, we can just sort of say, RDI is saved.</p>



<p class="wp-block-paragraph">And then when we load RSI…</p>



<p class="wp-block-paragraph">Oh, that&#8217;ll be when we hit RDX.</p>



<p class="wp-block-paragraph">Oh, that&#8217;ll be when we hit RDX. Okay, I got confused. So we stored R12 was the character</p>



<p class="wp-block-paragraph">pointer. And then for the length of the string, we stored that as R13. Also for me personally,</p>



<p class="wp-block-paragraph">I feel like it&#8217;s a pretty good idea to kind of make a comment at the very top, because this is</p>



<p class="wp-block-paragraph">assembly. It&#8217;s really, really hard, right? Make a little comment at the top, just kind of reminding</p>



<p class="wp-block-paragraph">yourselves what you use all of the registers for. I know sometimes you&#8217;ll use the same register for</p>



<p class="wp-block-paragraph">sometimes you&#8217;ll use the same register for multiple purposes.</p>



<p class="wp-block-paragraph">You could either write that down</p>



<p class="wp-block-paragraph">or you could consider breaking up your function</p>



<p class="wp-block-paragraph">into multiple functions.</p>



<p class="wp-block-paragraph">That&#8217;s totally valid</p>



<p class="wp-block-paragraph">and it probably will make your life a lot easier.</p>



<p class="wp-block-paragraph">Anyway, so I&#8217;m gonna say register usage</p>



<p class="wp-block-paragraph">and then I&#8217;m gonna say R12 pointer to string, C string.</p>



<p class="wp-block-paragraph">And then I&#8217;m gonna say R13 is gonna be</p>



<p class="wp-block-paragraph">size of the string. And that seems simple but remember assembly especially when you&#8217;re new</p>



<p class="wp-block-paragraph">it&#8217;s really confusing and for me when I was first learning and even sometimes now I&#8217;ll be staring</p>



<p class="wp-block-paragraph">at a big blob of assembly code and I&#8217;ll just be like what register was I using you know or I&#8217;ll</p>



<p class="wp-block-paragraph">look at a register and I&#8217;ll be like what was that thing even for? So just make yourself a little</p>



<p class="wp-block-paragraph">comment up here to help you understand and then you&#8217;ll thank yourself later when you get a little</p>



<p class="wp-block-paragraph">a little bit too overloaded or maybe even if you come back to your code a month from</p>



<p class="wp-block-paragraph">now probably having totally forgotten what you even wrote then you&#8217;ll just have a nice</p>



<p class="wp-block-paragraph">little reminder here.</p>



<p class="wp-block-paragraph">It&#8217;s good to document and this is one of the ways that I recommend.</p>



<p class="wp-block-paragraph">Okay, so we&#8217;re saying that R12 and R13 are going to be used.</p>



<p class="wp-block-paragraph">We&#8217;re loading them up here from the incoming arguments.</p>



<p class="wp-block-paragraph">Now RDI and RSI are free to be destroyed if we want and of course we have to destroy them</p>



<p class="wp-block-paragraph">because RDI and RSI were incoming arguments when this function first came in but now that</p>



<p class="wp-block-paragraph">first came in but now that we&#8217;re going to be doing a system call we have to use</p>



<p class="wp-block-paragraph">them for incoming arguments to the sys call so they&#8217;re just they&#8217;re just meant</p>



<p class="wp-block-paragraph">to be constantly destroyed so we&#8217;re gonna do call code there and then the</p>



<p class="wp-block-paragraph">standard output is gonna be the first argument and then the string that we did</p>



<p class="wp-block-paragraph">which is an r12 is going to be the second argument and then rdx is going to</p>



<p class="wp-block-paragraph">be the next argument which is r13 which came into us through the second argument</p>



<p class="wp-block-paragraph">wait wait wait the string to write came into us through the first argument the</p>



<p class="wp-block-paragraph">came into us through the second argument but if you just kind of look at what we&#8217;re doing to the</p>



<p class="wp-block-paragraph">system call it&#8217;s actually the second and the third so I mean just forget about it okay so we set all</p>



<p class="wp-block-paragraph">that up let me make sure I&#8217;m not forgetting something by scrolling up real fast yeah so now</p>



<p class="wp-block-paragraph">we can do a system call one other thing that&#8217;s very very important about functions is you have</p>



<p class="wp-block-paragraph">to respect the ABI and that doesn&#8217;t just include using the right registers for incoming arguments</p>



<p class="wp-block-paragraph">out a call but you also have to keep in mind which registers are denoted as callee saved</p>



<p class="wp-block-paragraph">if something is callee saved then that means you know if i&#8217;m inside of print something</p>



<p class="wp-block-paragraph">i&#8217;m being called that means i&#8217;m the callee so that means if i&#8217;m going to use r12 and r13 which</p>



<p class="wp-block-paragraph">are designated as callee saved maybe i&#8217;ll pull up that my favorite assembly book after this</p>



<p class="wp-block-paragraph">but if i&#8217;m using registers which are designated as callee saved then i have to preserve those</p>



<p class="wp-block-paragraph">then I have to preserve those registers, meaning those registers,</p>



<p class="wp-block-paragraph">they should have their same values from when they first came in on the call</p>



<p class="wp-block-paragraph">when I exit the function. So when I exit the function, those registers,</p>



<p class="wp-block-paragraph">register values should appear to be untouched to the caller.</p>



<p class="wp-block-paragraph">Remember these registers are not local variables.</p>



<p class="wp-block-paragraph">They&#8217;re global to the entire system. There&#8217;s only one R12 on the whole CPU.</p>



<p class="wp-block-paragraph">So every program that runs every function that gets called,</p>



<p class="wp-block-paragraph">see the same exact R12 register. So if we&#8217;re not careful about preserving when we have to,</p>



<p class="wp-block-paragraph">we could end up crashing other programs or our own program. So anyway, let&#8217;s do something that</p>



<p class="wp-block-paragraph">I like to call the prologue and the epilogue. That&#8217;s another nod to my favorite assembly book,</p>



<p class="wp-block-paragraph">which I&#8217;ll probably show at the end of this video. So prologue is just, hey, let&#8217;s preserve</p>



<p class="wp-block-paragraph">the stuff that we need to preserve. We will preserve the R12 with a push and we will preserve</p>



<p class="wp-block-paragraph">and we will preserve the R13 with another push.</p>



<p class="wp-block-paragraph">We then have to restore them.</p>



<p class="wp-block-paragraph">So right now when we do this push,</p>



<p class="wp-block-paragraph">that just means that whatever value R12 and R13 had</p>



<p class="wp-block-paragraph">is going to go to the stack.</p>



<p class="wp-block-paragraph">So we&#8217;re going to hit memory.</p>



<p class="wp-block-paragraph">That&#8217;s not great, but like we have to in this case.</p>



<p class="wp-block-paragraph">We&#8217;re going to push it onto memory</p>



<p class="wp-block-paragraph">in order to preserve the value.</p>



<p class="wp-block-paragraph">And now we&#8217;re free to destroy the value if we want.</p>



<p class="wp-block-paragraph">And then right before we return,</p>



<p class="wp-block-paragraph">we&#8217;ll just pop those values.</p>



<p class="wp-block-paragraph">So that means we&#8217;re going to go to memory.</p>



<p class="wp-block-paragraph">We&#8217;re going to go to the stack.</p>



<p class="wp-block-paragraph">to go to the stack we&#8217;re going to grab the values that we just pushed and put them back onto the</p>



<p class="wp-block-paragraph">registers oops forgot to do 13 and 12 and I&#8217;ll call this the epilogue just meaning like we&#8217;re</p>



<p class="wp-block-paragraph">done we&#8217;re cleaning up and of course look very carefully at the fact that the push and the pop</p>



<p class="wp-block-paragraph">sequence are in reverse order notice how we push 12 first and we push our 13 second then at the end</p>



<p class="wp-block-paragraph">Then at the end, we pop R13 first, so it&#8217;s backwards.</p>



<p class="wp-block-paragraph">It&#8217;s like a little shell, you know, the R13s are on the inside</p>



<p class="wp-block-paragraph">and the R12s are on the outside.</p>



<p class="wp-block-paragraph">That logic would persist if we pushed,</p>



<p class="wp-block-paragraph">or if we had to preserve more Kali saved registers,</p>



<p class="wp-block-paragraph">if we were using more stuff.</p>



<p class="wp-block-paragraph">The reason we do that is because the stack as a data structure</p>



<p class="wp-block-paragraph">will return data to you backwards in the reverse order</p>



<p class="wp-block-paragraph">that you returned it or that you sent it into the stack.</p>



<p class="wp-block-paragraph">it into the stack so if i send a 12 and a 13 let&#8217;s just say the number 12 and the number 13</p>



<p class="wp-block-paragraph">into the stack then if i start popping stuff out of the stack it&#8217;s first going to give me the most</p>



<p class="wp-block-paragraph">recent item which is going to be the 13 so it&#8217;s going to give me backwards data and then it&#8217;ll</p>



<p class="wp-block-paragraph">give me the art the 12 seconds so um the type of data structure a stack is is first in last out</p>



<p class="wp-block-paragraph">last out no that&#8217;s a that&#8217;s a queue anyway so we&#8217;re preserving the registers we&#8217;re doing the</p>



<p class="wp-block-paragraph">system call we&#8217;re probably okay to call this function now so let me see if i can do this</p>



<p class="wp-block-paragraph">i&#8217;m going to comment out all of these things that the the say hello function does and i&#8217;m just going</p>



<p class="wp-block-paragraph">to call on print something in order to call that function but i have to load up my arguments i have</p>



<p class="wp-block-paragraph">pass arguments remember in the print something function it&#8217;s got a signature</p>



<p class="wp-block-paragraph">it wants a character pointer and a size we can&#8217;t just call it without giving it</p>



<p class="wp-block-paragraph">those things or it&#8217;s going to look at the registers anyway and just grab</p>



<p class="wp-block-paragraph">whatever junk data happened to be in there so that would be bad so first we</p>



<p class="wp-block-paragraph">have to load up the registers load up argument registers and call print</p>



<p class="wp-block-paragraph">load up the argument registers and call print something.</p>



<p class="wp-block-paragraph">So the first thing is going to be RDI, so we have to move something into RDI</p>



<p class="wp-block-paragraph">and then the second register for the second integer argument is going to be RSI.</p>



<p class="wp-block-paragraph">As far as I recall, if I&#8217;m wrong this is going to go horribly wrong.</p>



<p class="wp-block-paragraph">I&#8217;ll double check the book after this.</p>



<p class="wp-block-paragraph">And what do we want to do?</p>



<p class="wp-block-paragraph">We want to basically, inside of say hello,</p>



<p class="wp-block-paragraph">we could assume for now that say hello hardcodes the pointer and the length.</p>



<p class="wp-block-paragraph">So we&#8217;ll just say it&#8217;s going to grab the string from global and it&#8217;s going to grab the string</p>



<p class="wp-block-paragraph">length from the global variables.</p>



<p class="wp-block-paragraph">So it&#8217;s going to load up those two arguments and then call on print something.</p>



<p class="wp-block-paragraph">This still ends up being three lines.</p>



<p class="wp-block-paragraph">That&#8217;s why I was saying before, we&#8217;re not going to save too much, but we did save, you</p>



<p class="wp-block-paragraph">know, these two lines right here, you know, and the system call line.</p>



<p class="wp-block-paragraph">So it&#8217;s like slightly faster to do.</p>



<p class="wp-block-paragraph">You just load up two pointers and then you make a call.</p>



<p class="wp-block-paragraph">in this video so I can&#8217;t really make the function better but in the future when</p>



<p class="wp-block-paragraph">you learn how to loop and and learn about null terminated strings you could</p>



<p class="wp-block-paragraph">basically just pass a pointer to just the string only and then call on a</p>



<p class="wp-block-paragraph">function if the function was smart enough to scan through the string and</p>



<p class="wp-block-paragraph">figure out how long it was based on where the null terminator was but that&#8217;s</p>



<p class="wp-block-paragraph">gonna happen in another video anyway so if we call print something it should</p>



<p class="wp-block-paragraph">Notice also that we&#8217;re having a function call another function, which is pretty cool.</p>



<p class="wp-block-paragraph">So we have our entry point, it&#8217;s going to call on say hello, and then once we&#8217;re inside</p>



<p class="wp-block-paragraph">of say hello, we&#8217;re just going to load up some arguments and then call on the print</p>



<p class="wp-block-paragraph">something function.</p>



<p class="wp-block-paragraph">The print something function then does most of the work for the printing, and I think</p>



<p class="wp-block-paragraph">we&#8217;re ready to go.</p>



<p class="wp-block-paragraph">Let&#8217;s see if this works.</p>



<p class="wp-block-paragraph">All right, so we&#8217;re going to clear and then just run the program.</p>



<p class="wp-block-paragraph">Oh, that was so fast.</p>



<p class="wp-block-paragraph">That was so fast I don&#8217;t even feel like I&#8217;ve proved anything.</p>



<p class="wp-block-paragraph">Let&#8217;s comment out the system call so that all we really do is just load up some registers</p>



<p class="wp-block-paragraph">and then don&#8217;t do anything.</p>



<p class="wp-block-paragraph">And then you should see here that the print something function no longer actually does</p>



<p class="wp-block-paragraph">anything because it was too fast.</p>



<p class="wp-block-paragraph">Now if I uncomment the system call, you know that the print something function is actually</p>



<p class="wp-block-paragraph">doing something and it&#8217;s printing the message five times.</p>



<p class="wp-block-paragraph">We can also make some more messages if we wanted to,</p>



<p class="wp-block-paragraph">just as a way to make this video slightly more interesting.</p>



<p class="wp-block-paragraph">I&#8217;ll say msg1 is going to be a string of bytes,</p>



<p class="wp-block-paragraph">and I&#8217;m going to say this is message number one.</p>



<p class="wp-block-paragraph">Boring, sorry.</p>



<p class="wp-block-paragraph">And we&#8217;ll do a crlf there.</p>



<p class="wp-block-paragraph">Actually you know what, let&#8217;s make a function that does a crlf.</p>



<p class="wp-block-paragraph">Okay, let me just finish message one length,</p>



<p class="wp-block-paragraph">and we&#8217;ll do it equals that special string that you can do in yasm am i doing this right yeah okay</p>



<p class="wp-block-paragraph">so this is message number one so now we&#8217;ll call again with message we&#8217;ll call say hello just once</p>



<p class="wp-block-paragraph">and then we&#8217;ll call on print something loading it up first move something with rdi and then move</p>



<p class="wp-block-paragraph">message one is that what I had message underscore one and then RSI is going to</p>



<p class="wp-block-paragraph">get message underscore one length so basically we&#8217;re just you know print the</p>



<p class="wp-block-paragraph">first message it&#8217;s always a good idea to put comments on each line when you&#8217;re</p>



<p class="wp-block-paragraph">learning so that you don&#8217;t forget what&#8217;s happening in addition to like a comment</p>



<p class="wp-block-paragraph">for the whole block but I&#8217;m not going to do that here let&#8217;s just make sure that</p>



<p class="wp-block-paragraph">make sure that this actually works so it should print two different messages now</p>



<p class="wp-block-paragraph">one okay nice let&#8217;s enhance this a little bit more later i think i&#8217;m going to have us print like a i</p>



<p class="wp-block-paragraph">don&#8217;t know a character or maybe we&#8217;ll do a return value from something so we can just show you how</p>



<p class="wp-block-paragraph">to do the return values but i&#8217;m just going to enhance this a little bit more we&#8217;re going to say</p>



<p class="wp-block-paragraph">let&#8217;s make another function we&#8217;ll call it crlf the crlf function i don&#8217;t want it to do all the</p>



<p class="wp-block-paragraph">system call stuff because print something already does that for us i&#8217;m instead just going to make</p>



<p class="wp-block-paragraph">message so now that means i&#8217;m going to go crlf and that&#8217;s just going to be a 13 and a 10. remember</p>



<p class="wp-block-paragraph">you can define a c string with quotes but you can also separate quoted strings by commas and also</p>



<p class="wp-block-paragraph">integers just to sort of tack it on so you know this variable right here it&#8217;s going to be a</p>



<p class="wp-block-paragraph">sequence of characters representing the string that you see and then at the very end of the</p>



<p class="wp-block-paragraph">string it&#8217;s going to have a crlf carriage return line feed so that the cursor goes to the next line</p>



<p class="wp-block-paragraph">goes to the next line so I&#8217;m actually gonna take that off of message one</p>



<p class="wp-block-paragraph">because it&#8217;s gonna be here just on CRLF and then I&#8217;ll say CRLF length is gonna</p>



<p class="wp-block-paragraph">be two I could also do that special thing probably a little smarter CRLF</p>



<p class="wp-block-paragraph">yeah I mean it&#8217;s it&#8217;s better to avoid hard coding things if you can avoid</p>



<p class="wp-block-paragraph">hard coding the number two or any number just do it I guess I have to hard code</p>



<p class="wp-block-paragraph">So, so CRLF and then length.</p>



<p class="wp-block-paragraph">And so that means in my CRLF function, let&#8217;s see, let&#8217;s do CRLF, CRLF immediately return</p>



<p class="wp-block-paragraph">because it&#8217;s going to be a function.</p>



<p class="wp-block-paragraph">I&#8217;ll just say void CRLF with no arguments and prints a carriage return new line, also</p>



<p class="wp-block-paragraph">known as CRLF.</p>



<p class="wp-block-paragraph">and so that just kind of describes it and I don&#8217;t need to preserve RSI and RDI</p>



<p class="wp-block-paragraph">because those are not callie saved registers so respecting the ABI kind of</p>



<p class="wp-block-paragraph">helps you in this case I&#8217;m just gonna copy paste here and maybe I&#8217;ll copy</p>



<p class="wp-block-paragraph">paste that comment real fast too so I don&#8217;t have to like type that whole thing</p>



<p class="wp-block-paragraph">out and instead of doing hello string we&#8217;ll just say CRLF and then for here</p>



<p class="wp-block-paragraph">And now when I call CRLF, it should just make the cursor go down a little bit.</p>



<p class="wp-block-paragraph">So let&#8217;s test that out.</p>



<p class="wp-block-paragraph">I took the CRLF off of message one.</p>



<p class="wp-block-paragraph">So if we actually run this right now on modified, it should have the cursor.</p>



<p class="wp-block-paragraph">Now, what did I just do wrong?</p>



<p class="wp-block-paragraph">On number 28 and 29, instructions expected after label.</p>



<p class="wp-block-paragraph">I guess I forgot to complete something.</p>



<p class="wp-block-paragraph">Oh, I forgot to put, it&#8217;s a byte sequence right there.</p>



<p class="wp-block-paragraph">there and I forgot to put that this is basically a defined sequence you know</p>



<p class="wp-block-paragraph">that EQU okay my bad okay so now it prints notice how the message line it no</p>



<p class="wp-block-paragraph">longer does a new line and no longer jumps to the next line CRLF so that</p>



<p class="wp-block-paragraph">means when the program exits the terminal prompt is on the same line as</p>



<p class="wp-block-paragraph">the program just printed that&#8217;s ugly and awful so let&#8217;s call</p>



<p class="wp-block-paragraph">We could either say, let&#8217;s call CRLF after printing the first message,</p>



<p class="wp-block-paragraph">or we could just assume that print something is like print a line.</p>



<p class="wp-block-paragraph">I don&#8217;t know how you feel,</p>



<p class="wp-block-paragraph">but I think if we always called CRLF after every time we printed a message,</p>



<p class="wp-block-paragraph">I would probably be a little irritated.</p>



<p class="wp-block-paragraph">So maybe let&#8217;s change this to print line and then make it call CRLF.</p>



<p class="wp-block-paragraph">You can just keep adding this like 1013 at the end of every string, but I think I want</p>



<p class="wp-block-paragraph">to try to make this as compact as possible.</p>



<p class="wp-block-paragraph">So let&#8217;s add the CRLF for now and then we&#8217;ll change print something later.</p>



<p class="wp-block-paragraph">So we print the message.</p>



<p class="wp-block-paragraph">Now the prompt should be, you know, a line further down.</p>



<p class="wp-block-paragraph">So great works.</p>



<p class="wp-block-paragraph">I am now going to take the CRLF off and I&#8217;m going to put it inside of print something</p>



<p class="wp-block-paragraph">so that right after we do the system call to print the string in question, we&#8217;ll print</p>



<p class="wp-block-paragraph">we&#8217;ll print a new line this should now accomplish the same thing that we just saw</p>



<p class="wp-block-paragraph">oh no what did i do i think i did uh i think i have a infinite loop of recursive calls in there</p>



<p class="wp-block-paragraph">somewhere i had a stack overflow probably so let&#8217;s see what did i just do say hello is call</p>



<p class="wp-block-paragraph">print something and then crlf is call print something oh whoops print something calls crlf</p>



<p class="wp-block-paragraph">at the end so i should do one or the other and not have them call both so maybe just because crlf is</p>



<p class="wp-block-paragraph">special maybe i&#8217;ll just use a direct system call inside of crlf i thought i was so clever</p>



<p class="wp-block-paragraph">but i was wrong so we&#8217;re going to repeat a little code for the crlf and we&#8217;re going to load it up</p>



<p class="wp-block-paragraph">with um crlf dang how embarrassing i like to say this is the most public of my many humiliations</p>



<p class="wp-block-paragraph">of my many humiliations. But anyway, so now we don&#8217;t need to rely on print something. So this</p>



<p class="wp-block-paragraph">won&#8217;t be an infinite loop and it should work if print something calls on crlf now. Do it again.</p>



<p class="wp-block-paragraph">Okay, it works. So now let&#8217;s modify print something so that it is just print</p>



<p class="wp-block-paragraph">a line. Print a line. And then that, in my eyes, you don&#8217;t have to do it this way. It sort of</p>



<p class="wp-block-paragraph">to do it this way it sort of justifies printing a crlf at the end every single time so this is</p>



<p class="wp-block-paragraph">just my personal thinking and then maybe i&#8217;ll add a comment after prints a crlf</p>



<p class="wp-block-paragraph">so now when we call print line we no longer have to call crlf</p>



<p class="wp-block-paragraph">and then when we print that message should be pretty good oh we got to update that to print line</p>



<p class="wp-block-paragraph">Then for say hello, let&#8217;s just double check.</p>



<p class="wp-block-paragraph">We can get rid of this system call code here.</p>



<p class="wp-block-paragraph">Probably a good idea at this point.</p>



<p class="wp-block-paragraph">And then we just say print hello string.</p>



<p class="wp-block-paragraph">And then from the hello string, we can get rid of this CRLF at the end of it.</p>



<p class="wp-block-paragraph">So I&#8217;m just going to get rid of that.</p>



<p class="wp-block-paragraph">Actually, let&#8217;s do it first without getting rid of it.</p>



<p class="wp-block-paragraph">Just so you see, oh, what did I do wrong?</p>



<p class="wp-block-paragraph">Lion 61, forgot to update the symbol name.</p>



<p class="wp-block-paragraph">Oh, there we go.</p>



<p class="wp-block-paragraph">call print line because we don&#8217;t have print something anymore and try it again notice how</p>



<p class="wp-block-paragraph">the first message hello my name is it has two line feeds after it because it is calling print line</p>



<p class="wp-block-paragraph">which will do a crlf but then also in the string itself there&#8217;s a crlf so again the point is i&#8217;m</p>



<p class="wp-block-paragraph">to my advantage.</p>



<p class="wp-block-paragraph">Okay, so we got that working now.</p>



<p class="wp-block-paragraph">I&#8217;m just going to print like five different kinds of messages.</p>



<p class="wp-block-paragraph">How about three?</p>



<p class="wp-block-paragraph">So we don&#8217;t have to sit here and watch me typing for too long.</p>



<p class="wp-block-paragraph">So I&#8217;m going to say message two and then I&#8217;m going to do message three.</p>



<p class="wp-block-paragraph">Message three.</p>



<p class="wp-block-paragraph">This is message three unless I got that wrong.</p>



<p class="wp-block-paragraph">One, two, three, one, two, three, one, two, three.</p>



<p class="wp-block-paragraph">Now we can just call a print string on three different messages.</p>



<p class="wp-block-paragraph">And I&#8217;ll just change this to say, you know, print the second message, print the third</p>



<p class="wp-block-paragraph">message.</p>



<p class="wp-block-paragraph">And then I&#8217;m going to change that to message two and then message three.</p>



<p class="wp-block-paragraph">All right.</p>



<p class="wp-block-paragraph">So then I&#8217;m going to do that again.</p>



<p class="wp-block-paragraph">And then all three messages get printed.</p>



<p class="wp-block-paragraph">Nice.</p>



<p class="wp-block-paragraph">So now one more thing.</p>



<p class="wp-block-paragraph">I just want to show you how to return an integer to the caller in a basic YASM x86 assembly function.</p>



<p class="wp-block-paragraph">Again, this will not cover floats.</p>



<p class="wp-block-paragraph">This does not show you how to return floats.</p>



<p class="wp-block-paragraph">With floats, you have to use a different register called xmm0.</p>



<p class="wp-block-paragraph">That&#8217;s going to be a different video.</p>



<p class="wp-block-paragraph">But for now, I&#8217;m going to say all integer or pointer return values, like longs is going to be what we&#8217;re using,</p>



<p class="wp-block-paragraph">that is the register that is designated for return values.</p>



<p class="wp-block-paragraph">So what about this?</p>



<p class="wp-block-paragraph">Instead of just returning the exit success code</p>



<p class="wp-block-paragraph">that I&#8217;ve defined up above,</p>



<p class="wp-block-paragraph">we&#8217;ll return whatever some function call gives us.</p>



<p class="wp-block-paragraph">Maybe I&#8217;ll make another function down here</p>



<p class="wp-block-paragraph">and then I&#8217;ll say a function</p>



<p class="wp-block-paragraph">that just returns a simple long value.</p>



<p class="wp-block-paragraph">And we&#8217;ll call this function gimme long.</p>



<p class="wp-block-paragraph">We&#8217;ll call this function gimme long.</p>



<p class="wp-block-paragraph">It&#8217;ll take no arguments because we&#8217;re just keeping this simple for now.</p>



<p class="wp-block-paragraph">And instead of having a void return type, it&#8217;ll have a long return type because we expect</p>



<p class="wp-block-paragraph">RAX to be loaded up.</p>



<p class="wp-block-paragraph">So then I&#8217;m going to say gimme long and immediately do a return because it is a function.</p>



<p class="wp-block-paragraph">And then I&#8217;m just going to load up a value from somewhere.</p>



<p class="wp-block-paragraph">Obviously, it&#8217;s better if you&#8217;re doing something complicated to stick with registers as much</p>



<p class="wp-block-paragraph">or defines and hit memory only if you need to.</p>



<p class="wp-block-paragraph">And try not to hard code immediate into your code</p>



<p class="wp-block-paragraph">if you can possibly avoid it.</p>



<p class="wp-block-paragraph">It&#8217;s probably a lot better to define a value up at the top</p>



<p class="wp-block-paragraph">in the data area than to just hard code it here.</p>



<p class="wp-block-paragraph">But I&#8217;m gonna just hard code something here.</p>



<p class="wp-block-paragraph">No, I always second guess myself.</p>



<p class="wp-block-paragraph">I&#8217;m gonna say exit gimme.</p>



<p class="wp-block-paragraph">I&#8217;m gonna define something up here.</p>



<p class="wp-block-paragraph">So it&#8217;s easy to find what value I&#8217;m gonna return</p>



<p class="wp-block-paragraph">for the gimme.</p>



<p class="wp-block-paragraph">I&#8217;m just going to say 88, 89, 90, I don&#8217;t even know.</p>



<p class="wp-block-paragraph">About 33, I don&#8217;t even know.</p>



<p class="wp-block-paragraph">So we&#8217;re defining exitGimme as a symbol</p>



<p class="wp-block-paragraph">to just be a replacement of just the number 33.</p>



<p class="wp-block-paragraph">So down here, I&#8217;m going to say,</p>



<p class="wp-block-paragraph">let&#8217;s have gimme long simply return exitGimme into RAX.</p>



<p class="wp-block-paragraph">Nice, okay.</p>



<p class="wp-block-paragraph">And so that means it&#8217;s the official return value</p>



<p class="wp-block-paragraph">once you call gimme long.</p>



<p class="wp-block-paragraph">call gimme long so that means up here get our whoops get out it&#8217;s coming from inside the house</p>



<p class="wp-block-paragraph">get our return value from gimme long and again imagine gimme long might be a big complicated</p>



<p class="wp-block-paragraph">function that decides what a return value is going to be or does something so i&#8217;ll start with saying</p>



<p class="wp-block-paragraph">call gimme long because gimme long does not take any arguments i don&#8217;t have to load up any registers</p>



<p class="wp-block-paragraph">load up any registers nor do i have to worry about preserving them or setting them up or doing</p>



<p class="wp-block-paragraph">anything i just call but then let&#8217;s see inside of this function we&#8217;ll use rax in order to you know</p>



<p class="wp-block-paragraph">load up the system call to let it know what function or sorry what function we want from</p>



<p class="wp-block-paragraph">the system call so we got to give rax system exit so that means down here we can&#8217;t actually use rax</p>



<p class="wp-block-paragraph">those other registers again I&#8217;m going to use I&#8217;m going to use the r12 register</p>



<p class="wp-block-paragraph">I&#8217;m going to do like a little comment here that says register usage and I&#8217;ll say r12</p>



<p class="wp-block-paragraph">maybe I&#8217;ll do a tab r12 is going to be old return value</p>



<p class="wp-block-paragraph">or the syscall to exit you know whatever and that&#8217;s all we&#8217;re going to do so in the prologue</p>



<p class="wp-block-paragraph">I&#8217;m going to push R12.</p>



<p class="wp-block-paragraph">You should probably even preserve registers in the start function</p>



<p class="wp-block-paragraph">because something might, or in your entry point,</p>



<p class="wp-block-paragraph">because something that called you might be counting on those registers.</p>



<p class="wp-block-paragraph">This is going to be probably especially true if you&#8217;re using GCC</p>



<p class="wp-block-paragraph">to have a main entry point within a hybrid program.</p>



<p class="wp-block-paragraph">So I&#8217;m just going to go prolog, push R12,</p>



<p class="wp-block-paragraph">and then pop R12 at the very end.</p>



<p class="wp-block-paragraph">Pop R12.</p>



<p class="wp-block-paragraph">That&#8217;s not going to work because by the time we get there,</p>



<p class="wp-block-paragraph">because by the time we get there,</p>



<p class="wp-block-paragraph">it&#8217;s gonna exit the program.</p>



<p class="wp-block-paragraph">I&#8217;ll do this here.</p>



<p class="wp-block-paragraph">I&#8217;ll just try to be a good citizen.</p>



<p class="wp-block-paragraph">I&#8217;m not actually sure if we need this</p>



<p class="wp-block-paragraph">for a pure assembly function,</p>



<p class="wp-block-paragraph">but it&#8217;s just a really good habit to get into.</p>



<p class="wp-block-paragraph">And what is it gonna cost you?</p>



<p class="wp-block-paragraph">Just a touch memory,</p>



<p class="wp-block-paragraph">just one time right before the program exits.</p>



<p class="wp-block-paragraph">It&#8217;s not gonna be a giant for loop or anything.</p>



<p class="wp-block-paragraph">Okay, so epilog,</p>



<p class="wp-block-paragraph">probably I might be showing you bad practice</p>



<p class="wp-block-paragraph">by preserving things in the underscore start function.</p>



<p class="wp-block-paragraph">But again, you will definitely wanna do this</p>



<p class="wp-block-paragraph">Again, you will definitely want to do this for the main when you return from main.</p>



<p class="wp-block-paragraph">So we do prolog R12, epilog R12.</p>



<p class="wp-block-paragraph">I made a comment there.</p>



<p class="wp-block-paragraph">And then we&#8217;re going to store the return value in R12.</p>



<p class="wp-block-paragraph">So that means here we&#8217;re going to move something into R12.</p>



<p class="wp-block-paragraph">It&#8217;s getting precarious because we restored it right before the system call,</p>



<p class="wp-block-paragraph">but I think it&#8217;ll be okay.</p>



<p class="wp-block-paragraph">system call but i think it&#8217;ll be okay we&#8217;re going to grab rax because rax is supposed to be the</p>



<p class="wp-block-paragraph">return value register so when we called gimme long it loaded up rax with our return value</p>



<p class="wp-block-paragraph">we then send rax into r12 so we can use r12 uh even though our rax has been you know destroyed</p>



<p class="wp-block-paragraph">we&#8217;ll still have the return value that we wanted to use so i&#8217;m just going to load r12 up into the</p>



<p class="wp-block-paragraph">first argument of the sys exit call let&#8217;s see if i if i got this to work or if i totally ruin this</p>



<p class="wp-block-paragraph">work or if i totally ruin this okay all right so let&#8217;s see here what was</p>



<p class="wp-block-paragraph">why is it not erroring gimme 33 exit gimme should i did i load the wrong value exit gimme what is</p>



<p class="wp-block-paragraph">it doing exit gimme it&#8217;s moving rex into exit gimme so you call gimme long and then you move</p>



<p class="wp-block-paragraph">r a x into r 12 and r 12 i wonder if that destroyed it</p>



<p class="wp-block-paragraph">hmm let me get rid of this push pop pair because it&#8217;s supposed to be giving me</p>



<p class="wp-block-paragraph">an ever message that the function or that the program did not exit maybe this is proof that</p>



<p class="wp-block-paragraph">you don&#8217;t need to preserve inside of the underscore start function let&#8217;s see here</p>



<p class="wp-block-paragraph">yeah okay i guess that proves it hey don&#8217;t take my word for it i&#8217;m wrong all the time</p>



<p class="wp-block-paragraph">For yourself, be a scientist, be a computer scientist.</p>



<p class="wp-block-paragraph">Try it out.</p>



<p class="wp-block-paragraph">Try to do something different.</p>



<p class="wp-block-paragraph">Okay, so I guess this is proof we don&#8217;t really need a prolog and epilog in just a pure assembly,</p>



<p class="wp-block-paragraph">at least the entry point for start.</p>



<p class="wp-block-paragraph">But if you were calling any other functions, then they definitely need to preserve.</p>



<p class="wp-block-paragraph">And again, if you&#8217;re using GCC, so you can have a main for your entry point, then you</p>



<p class="wp-block-paragraph">still have to preserve.</p>



<p class="wp-block-paragraph">you know print the message and then print our other three messages and then the return code</p>



<p class="wp-block-paragraph">is going to be 33 that was the gimme code that we did so now we know how to return a value from a</p>



<p class="wp-block-paragraph">function and um let me set this back to just zero so the make system thinks that we succeeded</p>



<p class="wp-block-paragraph">run it again succeeded all right i guess this concludes the video i hope you had a little bit</p>



<p class="wp-block-paragraph">and I hope you learn a little bit of stuff and I&#8217;ll see you in the next video.</p>



<p class="wp-block-paragraph">Hey everybody, thanks for watching this video again from the bottom of my heart.</p>



<p class="wp-block-paragraph">I really appreciate it.</p>



<p class="wp-block-paragraph">I do hope you did learn something and have some fun.</p>



<p class="wp-block-paragraph">If you could do me a please, a small little favor, could you please subscribe and follow</p>



<p class="wp-block-paragraph">this channel or these videos or whatever it is you do on the current social media website</p>



<p class="wp-block-paragraph">that you&#8217;re looking at right now.</p>



<p class="wp-block-paragraph">world to me and it&#8217;ll help make more videos and grow this community so we&#8217;ll</p>



<p class="wp-block-paragraph">be able to do more videos longer videos better videos or just I&#8217;ll be able to</p>



<p class="wp-block-paragraph">keep making videos in general so please do do me a kindness and and subscribe</p>



<p class="wp-block-paragraph">you know sometimes I&#8217;m sleeping in the middle of the night and I just wake up</p>



<p class="wp-block-paragraph">because I know somebody subscribed or followed it just wakes me up and I get</p>



<p class="wp-block-paragraph">filled with joy that&#8217;s exactly what happens every single time so you could</p>



<p class="wp-block-paragraph">do it as a nice favor to me or you could you control me if you want to just wake</p>



<p class="wp-block-paragraph">just subscribe and then I&#8217;ll just wake up. I promise that&#8217;s what will happen.</p>



<p class="wp-block-paragraph">Also, if you look at the middle of the screen right now, you should see a QR code, which you</p>



<p class="wp-block-paragraph">can scan in order to go to the website, which I think is also named somewhere at the bottom of</p>



<p class="wp-block-paragraph">this video. And it&#8217;ll take you to my main website where you can just kind of like see</p>



<p class="wp-block-paragraph">all the videos I published and the services and tutorials and things that I offer and all that</p>



<p class="wp-block-paragraph">clarifications or errata or just future videos that you want to see please leave a comment or</p>



<p class="wp-block-paragraph">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 class="wp-block-paragraph">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 class="wp-block-paragraph">it would really it really mean the world to me i would really appreciate it so</p>



<p class="wp-block-paragraph">again thank you so much for watching this video and um enjoy the cool music as as i fade into the</p>



<p class="wp-block-paragraph">which is coming for us all.</p>



<p class="wp-block-paragraph">Thank you.</p>
<p>The post <a href="https://www.NeuralLantern.com/mastering-x86-64-yasm-assembly-functions-made-simple/">Mastering x86-64 YASM Assembly: Functions Made Simple</a> appeared first on <a href="https://www.NeuralLantern.com">NeuralLantern.com</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.NeuralLantern.com/mastering-x86-64-yasm-assembly-functions-made-simple/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Demystifying x86-64 Program Segments: Stack, Heap, and More</title>
		<link>https://www.NeuralLantern.com/demystifying-x86-64-program-segments-stack-heap-and-more/</link>
					<comments>https://www.NeuralLantern.com/demystifying-x86-64-program-segments-stack-heap-and-more/#respond</comments>
		
		<dc:creator><![CDATA[mike]]></dc:creator>
		<pubDate>Mon, 04 Aug 2025 01:16:47 +0000</pubDate>
				<category><![CDATA[Assembly Language]]></category>
		<category><![CDATA[Coding]]></category>
		<category><![CDATA[Computer Science]]></category>
		<category><![CDATA[Videos]]></category>
		<category><![CDATA[assembly programming]]></category>
		<category><![CDATA[BSS section]]></category>
		<category><![CDATA[C++ programming]]></category>
		<category><![CDATA[coding tutorial]]></category>
		<category><![CDATA[computer architecture]]></category>
		<category><![CDATA[data section]]></category>
		<category><![CDATA[heap memory]]></category>
		<category><![CDATA[low-level programming]]></category>
		<category><![CDATA[memory management]]></category>
		<category><![CDATA[program segments]]></category>
		<category><![CDATA[segmentation fault]]></category>
		<category><![CDATA[stack overflow]]></category>
		<category><![CDATA[virtual memory]]></category>
		<category><![CDATA[x86-64]]></category>
		<category><![CDATA[Yasm assembly]]></category>
		<guid isPermaLink="false">https://www.NeuralLantern.com/?p=195</guid>

					<description><![CDATA[<p>Understand x86-64 program segments like stack, heap, data, and BSS. Learn to avoid segmentation faults and stack overflows in this beginner-friendly coding tutorial.</p>
<p>The post <a href="https://www.NeuralLantern.com/demystifying-x86-64-program-segments-stack-heap-and-more/">Demystifying x86-64 Program Segments: Stack, Heap, and 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="Demystifying x86-64 Program Segments: Stack, Heap, and More" width="1380" height="776" src="https://www.youtube.com/embed/GUicAuRPgPA?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 class="wp-block-paragraph">Join us for a clear, engaging look at x86-64 program segments! We break down the text, data, and BSS sections, explore stack vs. heap memory, and show a real stack overflow in C++. Learn why segmentation faults happen, how virtual memory works, and tips to avoid crashes. Whether you&#8217;re new to assembly or leveling up your coding skills, this video is packed with insights to boost your understanding of low-level programming. Subscribe for more coding deep dives!</p>



<p class="wp-block-paragraph">Introduction to x86-64 program segments 00:00:01<br>Understanding segmentation faults 00:00:12<br>Importance of segments in assembly 00:00:32<br>Overview of assembly program structure 00:01:09<br>Defining segments in Yasm assembly 00:01:49<br>Data section: Initialized global variables 00:02:20<br>BSS section: Uninitialized variables 00:03:23<br>Text section: Code and functions 00:03:59<br>Global and extern function declarations 00:04:41<br>Stack: Local variables and return addresses 00:07:11<br>Stack overflow example in C++ 00:08:48<br>Heap: Dynamic memory allocation 00:11:31<br>Stack vs heap memory growth 00:11:56<br>Virtual memory and memory allocation 00:14:00<br>Demonstrating memory overflow with heap allocation 00:16:59<br>Summary of segments and their purposes 00:17:59<br>Closing remarks and call to subscribe 00:18:47</p>



<p class="wp-block-paragraph">Thanks for watching!</p>



<p class="wp-block-paragraph">Find us on other social media here:</p>



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



<p class="wp-block-paragraph">Please help support us!</p>



<ul class="wp-block-list">
<li>Subscribing + Sharing on Social Media</li>



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



<li>Subscribing to our Blog</li>



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



<p class="wp-block-paragraph">Hi there!</p>



<p class="wp-block-paragraph">Let&#8217;s talk about x86-64 programs and their segments and the purpose of the segments.</p>



<p class="wp-block-paragraph">Probably when you&#8217;ve been programming in the past you&#8217;ve seen segmentation fault errors.</p>



<p class="wp-block-paragraph">If you&#8217;ve accidentally crossed the bounds of some appropriate memory location, if you</p>



<p class="wp-block-paragraph">have an array and you try to index it way outside of bounds or something, you might</p>



<p class="wp-block-paragraph">have seen a segfault.</p>



<p class="wp-block-paragraph">SegFaults basically come from the idea that you have crossed a segment boundary.</p>



<p class="wp-block-paragraph">Segments used to be really, really important.</p>



<p class="wp-block-paragraph">They&#8217;re a little bit less important now that we have virtual memory.</p>



<p class="wp-block-paragraph">I&#8217;ll just talk a little bit about them.</p>



<p class="wp-block-paragraph">I&#8217;m going to be discussing this from the perspective of an assembly program,</p>



<p class="wp-block-paragraph">just because even in higher level languages,</p>



<p class="wp-block-paragraph">your languages will typically be compiled down to assembly</p>



<p class="wp-block-paragraph">first before they actually hit machine code and your actual executable binary.</p>



<p class="wp-block-paragraph">So if you&#8217;re not interested in Yasm Assembly,</p>



<p class="wp-block-paragraph">Yasm assembly I think this could still be worth it for you to watch because</p>



<p class="wp-block-paragraph">you&#8217;ll be able to see the different segments and just kind of get a better</p>



<p class="wp-block-paragraph">understanding of what they are okay so I&#8217;m gonna actually write a very very</p>



<p class="wp-block-paragraph">very quick program in assembly it&#8217;s not gonna really do anything I just want you</p>



<p class="wp-block-paragraph">to show I just want you to see the segments that are involved so for</p>



<p class="wp-block-paragraph">starters let&#8217;s just pretend that I&#8217;m writing a an assembly program here we&#8217;ll</p>



<p class="wp-block-paragraph">This is a comment just so you know what I&#8217;m about to type.</p>



<p class="wp-block-paragraph">Perhaps actually this is not the best editor.</p>



<p class="wp-block-paragraph">I don&#8217;t know why I pulled this up.</p>



<p class="wp-block-paragraph">Let me get Gene here.</p>



<p class="wp-block-paragraph">It&#8217;s a little bit better.</p>



<p class="wp-block-paragraph">Gene.</p>



<p class="wp-block-paragraph">There we go.</p>



<p class="wp-block-paragraph">Maybe I&#8217;ll save it somewhere.</p>



<p class="wp-block-paragraph">I&#8217;ll say segments assembly.</p>



<p class="wp-block-paragraph">And then now the comment gets highlighted.</p>



<p class="wp-block-paragraph">Okay.</p>



<p class="wp-block-paragraph">So in assembly in the ASM assembly,</p>



<p class="wp-block-paragraph">we denote various segments by using the section keywords.</p>



<p class="wp-block-paragraph">keywords so we&#8217;ll say section and then dot something indicating the type of segment that we&#8217;re</p>



<p class="wp-block-paragraph">defining so section text and then up here we&#8217;ll have another one section dot bss and you know if</p>



<p class="wp-block-paragraph">you already program assembly you know that somewhere in here is where the code goes right</p>



<p class="wp-block-paragraph">between these sections so just a quick recap of what the data section is it just is where you</p>



<p class="wp-block-paragraph">can name and initialize at the same time at the very top.</p>



<p class="wp-block-paragraph">Globals are not great, but sometimes you want to do that in assembly, depending.</p>



<p class="wp-block-paragraph">It&#8217;s better if you stick all your variables on the stack.</p>



<p class="wp-block-paragraph">Like if they&#8217;re a local variable, it&#8217;s a little bit better.</p>



<p class="wp-block-paragraph">Think of higher level languages.</p>



<p class="wp-block-paragraph">You typically want to avoid global variables if you can possibly avoid them and make</p>



<p class="wp-block-paragraph">everything else a local variable or a member variable.</p>



<p class="wp-block-paragraph">So that means somewhere else other than data, probably the stack, but or the heap.</p>



<p class="wp-block-paragraph">Anyway, you know, this is like the basic pattern.</p>



<p class="wp-block-paragraph">We&#8217;ll say, you know, my variable and then we&#8217;ll decide to say what data size it is.</p>



<p class="wp-block-paragraph">In this case, it&#8217;s DB for one byte of data.</p>



<p class="wp-block-paragraph">You could put a, I think DD in there or a DW for a word.</p>



<p class="wp-block-paragraph">Sorry, sorry, DW would be for a word.</p>



<p class="wp-block-paragraph">And then I think DD would be a double and then DQ would definitely be a quad word.</p>



<p class="wp-block-paragraph">So here quad word is eight bytes.</p>



<p class="wp-block-paragraph">And then I can sort of initialize it to some number.</p>



<p class="wp-block-paragraph">some number and now I have a global variable so not like a huge deal the BSS section is basically</p>



<p class="wp-block-paragraph">where you have uninitialized variables but there you can make huge amounts of variables you can</p>



<p class="wp-block-paragraph">make an array of variables so if I say my array something like that I could put resource queue</p>



<p class="wp-block-paragraph">to say I want the type of one item to be a quad word so you know eight bytes per item and then I</p>



<p class="wp-block-paragraph">could just say I want you know a hundred thousand items or something and I&#8217;ll</p>



<p class="wp-block-paragraph">double check that after I stop recording this video and correct it if I&#8217;m wrong</p>



<p class="wp-block-paragraph">it&#8217;s been a while since I actually typed BSS here and then in the text section</p>



<p class="wp-block-paragraph">this is where you put code and functions and things you typically want to have at</p>



<p class="wp-block-paragraph">least one entry point somewhere you know if you want to make like if you want to</p>



<p class="wp-block-paragraph">make this the entry point of your entire program you&#8217;ll probably do global main</p>



<p class="wp-block-paragraph">main and then this assumes that you use the GCC libraries and just sort of return from the label</p>



<p class="wp-block-paragraph">sort of makes it a function assuming you didn&#8217;t ruin any of the other data.</p>



<p class="wp-block-paragraph">So those are the three sections that we can start off looking at the data section,</p>



<p class="wp-block-paragraph">the BSS section, and the tech section. It&#8217;s important to note that besides marking functions</p>



<p class="wp-block-paragraph">as global so that they&#8217;re accessible to other segments or other modules, we also mark</p>



<p class="wp-block-paragraph">We also mark functions and labels that we would like to access that are in other modules.</p>



<p class="wp-block-paragraph">So if we&#8217;re using the GCC libraries, we could say something like global printf,</p>



<p class="wp-block-paragraph">and then right away we&#8217;re able to call the printf function.</p>



<p class="wp-block-paragraph">Or say if you have another function in another module,</p>



<p class="wp-block-paragraph">like a different source code file that&#8217;s compiled somewhere else,</p>



<p class="wp-block-paragraph">we could say, you know, my other function.</p>



<p class="wp-block-paragraph">Something like that.</p>



<p class="wp-block-paragraph">Just mark it as global.</p>



<p class="wp-block-paragraph">Or sorry, not global.</p>



<p class="wp-block-paragraph">Extern.</p>



<p class="wp-block-paragraph">Xtern. Global is when you have the function in the current module and you want it to be available to other modules.</p>



<p class="wp-block-paragraph">Xtern is when the function or label is in another module and it&#8217;s already been made available to you,</p>



<p class="wp-block-paragraph">but you want your current module to know that it can access it for the purposes of assembling the program.</p>



<p class="wp-block-paragraph">So that means inside of here we could do something like call.</p>



<p class="wp-block-paragraph">We could say, let&#8217;s call my other function.</p>



<p class="wp-block-paragraph">my other function that will work if the other function is actually a function</p>



<p class="wp-block-paragraph">with a return statement and it respects the ABI you know it pushes registers that</p>



<p class="wp-block-paragraph">it&#8217;s going to modify if they&#8217;re callie saved if it&#8217;s not another function maybe</p>



<p class="wp-block-paragraph">we&#8217;ll say extern my other jump point so like not a proper function but just</p>



<p class="wp-block-paragraph">something that you might want to jump to then instead of using call we would use</p>



<p class="wp-block-paragraph">jump, you know, and so forth. So those are the basic sections. Let&#8217;s see, for the BSS,</p>



<p class="wp-block-paragraph">I definitely talked about token size and count. And then there&#8217;s resource B, resource W, resource</p>



<p class="wp-block-paragraph">D, resource Q for the different sizes. So basically just take all the different sizes you could put</p>



<p class="wp-block-paragraph">in the regular data section and assume that that could be popped on to the end of RES inside of</p>



<p class="wp-block-paragraph">the BSS section. If you&#8217;re interested, this is a segments video, not an assembly video.</p>



<p class="wp-block-paragraph">assembly video um another segment uh another segment that we&#8217;re not showing here in this</p>



<p class="wp-block-paragraph">source code is the actual stack probably i guess the best way that i could show it is</p>



<p class="wp-block-paragraph">i don&#8217;t know by messing around with the stack pointer increase uh</p>



<p class="wp-block-paragraph">oh rsp why did i think it was the instruction pointer we could increase the stack pointer and</p>



<p class="wp-block-paragraph">stack pointer and then decrease the stack pointer.</p>



<p class="wp-block-paragraph">It doesn&#8217;t really help a whole lot.</p>



<p class="wp-block-paragraph">But in another video, I&#8217;m gonna talk more in depth</p>



<p class="wp-block-paragraph">about what the stack is and how to use it and such.</p>



<p class="wp-block-paragraph">But just keep in mind that the stack</p>



<p class="wp-block-paragraph">is where local variables are stored</p>



<p class="wp-block-paragraph">and you can use it in assembly and C++</p>



<p class="wp-block-paragraph">and other higher level languages.</p>



<p class="wp-block-paragraph">You can even make virtual stacks</p>



<p class="wp-block-paragraph">in any type of program almost</p>



<p class="wp-block-paragraph">to just sort of have a stack like structure.</p>



<p class="wp-block-paragraph">But in C++, if you make any local variable,</p>



<p class="wp-block-paragraph">any local variable, then it&#8217;s usually going on the stack.</p>



<p class="wp-block-paragraph">And the stack tends to have a fixed size.</p>



<p class="wp-block-paragraph">And so if you sort of put too much data on the stack,</p>



<p class="wp-block-paragraph">you risk a stack overflow, which will crash your program.</p>



<p class="wp-block-paragraph">Besides local variables, the stack will hold other information that the current scope needs,</p>



<p class="wp-block-paragraph">like its return address.</p>



<p class="wp-block-paragraph">So when we make a function and then let&#8217;s say we call another function inside of it,</p>



<p class="wp-block-paragraph">actually just up here even when we call another function on line 22 the stack will receive the</p>



<p class="wp-block-paragraph">return address of where we currently are so that later when we return from the other function you</p>



<p class="wp-block-paragraph">know the program knows where to jump back and forth it&#8217;s actually under the hood just jump</p>



<p class="wp-block-paragraph">instructions when you do call it&#8217;s it&#8217;s not it&#8217;s not like a special thing it&#8217;s just jumping to</p>



<p class="wp-block-paragraph">another location and then pushing the return address on the call stack so that&#8217;s other stuff</p>



<p class="wp-block-paragraph">other stuff that the stack holds, which means if you have like an endless or like a infinite</p>



<p class="wp-block-paragraph">recursion happening in your function where just a function calls itself over and over</p>



<p class="wp-block-paragraph">and over again, or you have some complicated call graph and you end up calling too many</p>



<p class="wp-block-paragraph">functions without ever returning, you could overflow the stack.</p>



<p class="wp-block-paragraph">So that&#8217;s not good.</p>



<p class="wp-block-paragraph">Let me show you a quick example that I&#8217;m going to make for you real fast.</p>



<p class="wp-block-paragraph">Oh shoot.</p>



<p class="wp-block-paragraph">Did I actually make this?</p>



<p class="wp-block-paragraph">Where is the, it&#8217;s on the desktop.</p>



<p class="wp-block-paragraph">Okay, I guess I&#8217;m going to make more on the desktop.</p>



<p class="wp-block-paragraph">Let&#8217;s do a stack overflow.</p>



<p class="wp-block-paragraph">Let me do desktop here.</p>



<p class="wp-block-paragraph">Let&#8217;s do stack overflow.</p>



<p class="wp-block-paragraph">That&#8217;s CBP.</p>



<p class="wp-block-paragraph">I&#8217;m just going to make a quick program.</p>



<p class="wp-block-paragraph">In C++, that doesn&#8217;t really do anything except overflow the stack.</p>



<p class="wp-block-paragraph">So I just want you to know the stack can overflow.</p>



<p class="wp-block-paragraph">I&#8217;ll make a function called F.</p>



<p class="wp-block-paragraph">And F just calls itself.</p>



<p class="wp-block-paragraph">calls itself. So that&#8217;s going to be an infinite recursion.</p>



<p class="wp-block-paragraph">And that means, you know, every single time F calls itself,</p>



<p class="wp-block-paragraph">the return address of like line five is going to get pushed onto the stack again.</p>



<p class="wp-block-paragraph">So it&#8217;s just going to get pushed and pushed and pushed and pushed.</p>



<p class="wp-block-paragraph">The stack will overflow faster if I had local variables because every scope that</p>



<p class="wp-block-paragraph">gets called, even if it&#8217;s the same function name, it&#8217;ll have its own allocation on</p>



<p class="wp-block-paragraph">the stack. So if I just sort of like call this from main and then I&#8217;ll just need</p>



<p class="wp-block-paragraph">and then I&#8217;ll just need to make a little uh a little make file that just kind of compiles</p>



<p class="wp-block-paragraph">I don&#8217;t really even need to make a make file I could do a build script but I&#8217;m going to do it</p>



<p class="wp-block-paragraph">anyway we&#8217;ll say g plus plus standard equals c plus plus 23 show all warnings show pedantic</p>



<p class="wp-block-paragraph">the input file will be stack overflow cpp and then the output file will be main</p>



<p class="wp-block-paragraph">main and then assuming that succeeds I&#8217;ll just run main so this is like not a make</p>



<p class="wp-block-paragraph">file video I have other videos for make files let&#8217;s see let me get to the</p>



<p class="wp-block-paragraph">desktop if I run the make file with just the word make you should see a stack</p>



<p class="wp-block-paragraph">overflow we should also see a warning about the fact that there&#8217;s infinite</p>



<p class="wp-block-paragraph">That&#8217;s funny, I must have done something wrong or different because on a different computer</p>



<p class="wp-block-paragraph">not too long ago I did the same thing and I saw a warning.</p>



<p class="wp-block-paragraph">Okay, well thanks for not warning me.</p>



<p class="wp-block-paragraph">But anyway, you can see that we have a segmentation fault here because the stack overflowed.</p>



<p class="wp-block-paragraph">We just made a stack that was too huge.</p>



<p class="wp-block-paragraph">This is a reminder that the stack is not necessarily as dynamic as the heap.</p>



<p class="wp-block-paragraph">You can definitely overflow it.</p>



<p class="wp-block-paragraph">It&#8217;s not really meant to store gigantic amounts of data.</p>



<p class="wp-block-paragraph">What am I talking about when I say the heap though?</p>



<p class="wp-block-paragraph">So just keep in mind that whenever you allocate dynamic data,</p>



<p class="wp-block-paragraph">well, maybe, well, what do you think?</p>



<p class="wp-block-paragraph">Maybe, maybe inside of, instead of calling F, let&#8217;s call G.</p>



<p class="wp-block-paragraph">The heap is where you allocate dynamic data.</p>



<p class="wp-block-paragraph">So anytime you use the malloc operator in C or the new allocator in C++,</p>



<p class="wp-block-paragraph">you&#8217;re creating like a dynamic allocation of data.</p>



<p class="wp-block-paragraph">I&#8217;ll do a new integer and then I&#8217;ll do like a thousand integers so this is</p>



<p class="wp-block-paragraph">basically going to allocate a thousand new integers in the heap which is a</p>



<p class="wp-block-paragraph">segment that is right up against the stack I should mention also that the</p>



<p class="wp-block-paragraph">stack it&#8217;s important the stack its memory locations go down as the stack</p>



<p class="wp-block-paragraph">grows keep that in mind because typically when you when you imagine a</p>



<p class="wp-block-paragraph">stack I&#8217;ll make more videos in the future so you can visualize the stack better but most of you who</p>



<p class="wp-block-paragraph">know what a stack is already you imagine that the stack grows upward visually right but in your</p>



<p class="wp-block-paragraph">computer the memory locations of the stack grow downward so if I add something to the stack then</p>



<p class="wp-block-paragraph">the new stack head pointer is actually going to be a lesser address and the reason for that is the</p>



<p class="wp-block-paragraph">stack and the heap they grow in the same direction towards the same unallocated space we&#8217;ll call that</p>



<p class="wp-block-paragraph">We&#8217;ll call that space the unallocated memory space or the unmapped memory space.</p>



<p class="wp-block-paragraph">And it&#8217;s just, you know, a segment stack in the heap.</p>



<p class="wp-block-paragraph">And then there&#8217;s the unallocated space that they grow towards each other.</p>



<p class="wp-block-paragraph">If they ever meet, then your program is out of memory and it&#8217;ll crash.</p>



<p class="wp-block-paragraph">But the heap actually can be dynamically allocated to store tons of RAM.</p>



<p class="wp-block-paragraph">I&#8217;m probably about to crash this computer.</p>



<p class="wp-block-paragraph">So maybe this video won&#8217;t finish recording.</p>



<p class="wp-block-paragraph">recording so maybe I&#8217;ll try to crash this at the very end I just wanted you to</p>



<p class="wp-block-paragraph">see let me let me do this I&#8217;ll say while true we&#8217;ll just allocate a new integer</p>



<p class="wp-block-paragraph">this is kind of bad form because when you allocate you know a new variable</p>



<p class="wp-block-paragraph">somewhere you want to have access to it after the allocation you would usually</p>



<p class="wp-block-paragraph">send that into a pointer you&#8217;d say like you know integer pointer P equals new</p>



<p class="wp-block-paragraph">in something like that right but in this case I don&#8217;t really care I&#8217;m not gonna</p>



<p class="wp-block-paragraph">that memory i just want you to see how fast the memory can grow when we did the stack overflow</p>



<p class="wp-block-paragraph">seemed like the program died pretty quickly but here we&#8217;re going to be able to see the memory</p>



<p class="wp-block-paragraph">boom pretty pretty big before the whole computer just starts to you know fall to its knees</p>



<p class="wp-block-paragraph">so i&#8217;m going to do a call to g here and then i won&#8217;t actually run this until</p>



<p class="wp-block-paragraph">i&#8217;m ready to crash this whole video let&#8217;s see what else can i say</p>



<p class="wp-block-paragraph">Oh, the reason that the heap can grow so much larger than the stack is our programs now in the modern era, they use virtual memory.</p>



<p class="wp-block-paragraph">So it&#8217;s not like when we allocate, let&#8217;s say we&#8217;re accessing a memory location via a pointer inside of our program.</p>



<p class="wp-block-paragraph">It&#8217;s not that the pointer has the absolute memory location through the whole system.</p>



<p class="wp-block-paragraph">Like it doesn&#8217;t, it&#8217;s not able to name locations outside of the current program.</p>



<p class="wp-block-paragraph">Every program sees its own start memory location as just zero.</p>



<p class="wp-block-paragraph">And then under the hood, there are some extra tricks so that when you try to access a memory location,</p>



<p class="wp-block-paragraph">let&#8217;s say memory location 100, because you&#8217;ve made, I don&#8217;t know, this many integers or whatever.</p>



<p class="wp-block-paragraph">Then under the hood, the computer will know what is the start offset of the program&#8217;s memory.</p>



<p class="wp-block-paragraph">So maybe the start offset is 1000.</p>



<p class="wp-block-paragraph">It&#8217;s like totally unrealistic, but just pretend.</p>



<p class="wp-block-paragraph">The start offset is 1000.</p>



<p class="wp-block-paragraph">1000 then that means if it&#8217;s trying to access a pointer with memory location 100 it just gets added to the offset the virtual offset</p>



<p class="wp-block-paragraph">So it&#8217;ll be a thousand and 100 is the real you know physical memory location or at least at that point</p>



<p class="wp-block-paragraph">It&#8217;ll probably go to the paging system, but that&#8217;s that&#8217;s another video</p>



<p class="wp-block-paragraph">So just know that it&#8217;s virtual which means the program thinks it has the full range of of the 64 bit memory address space</p>



<p class="wp-block-paragraph">Even if you don&#8217;t even have that much memory on your computer, which there&#8217;s no way there&#8217;s no way you have so much memory</p>



<p class="wp-block-paragraph">there&#8217;s no way you have so much memory that it&#8217;s going to overflow or even meet</p>



<p class="wp-block-paragraph">64 bits at all but the program thinks it can so it&#8217;s just going to grow and grow</p>



<p class="wp-block-paragraph">and grow until you actually run out of physical memory and then eventually</p>



<p class="wp-block-paragraph">probably your program is going to start going to the page file or to the swap</p>



<p class="wp-block-paragraph">file and then when you run out of that then your whole computer probably is</p>



<p class="wp-block-paragraph">going to start crashing to be honest it&#8217;ll probably start crashing as soon as</p>



<p class="wp-block-paragraph">it hits the swap file because at that point none of the other programs really</p>



<p class="wp-block-paragraph">None of the other programs really have enough leftover memory to to breathe to do extra</p>



<p class="wp-block-paragraph">allocations and things so everything&#8217;s going to start slowing down it&#8217;s going to be pretty bad</p>



<p class="wp-block-paragraph">and well i think that&#8217;s all i really need to say this is not supposed to be a really long video</p>



<p class="wp-block-paragraph">let me uh let me show you this crasher i&#8217;m going to say goodbye now thank you so much for watching</p>



<p class="wp-block-paragraph">the video i hope you learned a little bit and had some fun too and and that way i&#8217;ve already said</p>



<p class="wp-block-paragraph">if it actually crashes the computer. So I&#8217;m going to open up this and then I&#8217;m going to do maybe</p>



<p class="wp-block-paragraph">HTOP so that I can see memory. So if you kind of like look at HTOP real fast,</p>



<p class="wp-block-paragraph">this is just a Linux command line tool that you can install. It&#8217;s pretty cool.</p>



<p class="wp-block-paragraph">What the heck&#8217;s going on here? Oh, that&#8217;s my recorder. You can see here&#8217;s the memory allocation.</p>



<p class="wp-block-paragraph">So I&#8217;m using two gigs of eight gigs on this virtual machine and then I&#8217;m not using any swap</p>



<p class="wp-block-paragraph">space which means I have not exhausted all my memory and and I have not been forced to go to</p>



<p class="wp-block-paragraph">the disk for like additional memory but in a second we&#8217;ll see that will probably change</p>



<p class="wp-block-paragraph">so I&#8217;m going to do make just to run that it looks like it&#8217;s stuck but if you keep looking at the</p>



<p class="wp-block-paragraph">memory allocation oh it already hit eight gigs now it&#8217;s into the swap already probably</p>



<p class="wp-block-paragraph">The out of memory killer, the OOM, successfully killed the program.</p>



<p class="wp-block-paragraph">Thank goodness.</p>



<p class="wp-block-paragraph">Or the whole computer would have been down.</p>



<p class="wp-block-paragraph">I always forget that that&#8217;s going to happen lately.</p>



<p class="wp-block-paragraph">But you can see it spiked, right?</p>



<p class="wp-block-paragraph">It went right up to the full physical memory allocation of 7.75 gigs.</p>



<p class="wp-block-paragraph">And then the swap file went all the way up to 2 gigs.</p>



<p class="wp-block-paragraph">You can see that the swap file still has about 700 megs inside of it.</p>



<p class="wp-block-paragraph">That just means that there&#8217;s memory that isn&#8217;t really being accessed.</p>



<p class="wp-block-paragraph">the operating system doesn&#8217;t feel like it&#8217;s necessary to pull it back out of swap and put</p>



<p class="wp-block-paragraph">it into RAM but I wonder if the video skipped when I did that or stuttered or anything I&#8217;m</p>



<p class="wp-block-paragraph">interested to find out now okay so that&#8217;s the end of this video I hope you have a decent basic</p>



<p class="wp-block-paragraph">understanding of segments and then you know like the segments and their purpose you know the heap</p>



<p class="wp-block-paragraph">and the stack they grow together the heap can dynamically allocate to grow bigger notice how it</p>



<p class="wp-block-paragraph">Whereas the stack will crash a lot faster.</p>



<p class="wp-block-paragraph">If the stack and the heap meet, then you&#8217;ve ran out of memory.</p>



<p class="wp-block-paragraph">At that point, you know, you probably want to like resize the heap or the stack just crashes.</p>



<p class="wp-block-paragraph">And then we got the text section of the program and the data section and the BSS section of the program.</p>



<p class="wp-block-paragraph">And these sections are a little bit less relevant in the modern era because we have, you know, virtual memory and stuff.</p>



<p class="wp-block-paragraph">That definitely means you have tried to access outside of your segment and it&#8217;s a no go.</p>



<p class="wp-block-paragraph">Okay.</p>



<p class="wp-block-paragraph">Thank you for watching this video.</p>



<p class="wp-block-paragraph">I will see you at a later date and time.</p>



<p class="wp-block-paragraph">Hey everybody.</p>



<p class="wp-block-paragraph">Thanks for watching this video again from the bottom of my heart.</p>



<p class="wp-block-paragraph">I really appreciate it.</p>



<p class="wp-block-paragraph">I do hope you did learn something and have some fun.</p>



<p class="wp-block-paragraph">If you could do me a please, a small little favor, could you please subscribe and follow</p>



<p class="wp-block-paragraph">as you do on the current social media website</p>



<p class="wp-block-paragraph">that you&#8217;re looking at right now,</p>



<p class="wp-block-paragraph">it would really mean the world to me</p>



<p class="wp-block-paragraph">and it&#8217;ll help make more videos</p>



<p class="wp-block-paragraph">and grow this community.</p>



<p class="wp-block-paragraph">So we&#8217;ll be able to do more videos,</p>



<p class="wp-block-paragraph">longer videos, better videos,</p>



<p class="wp-block-paragraph">or just I&#8217;ll be able to keep making videos in general.</p>



<p class="wp-block-paragraph">So please do me a kindness and subscribe.</p>



<p class="wp-block-paragraph">You know, sometimes I&#8217;m sleeping</p>



<p class="wp-block-paragraph">in the middle of the night</p>



<p class="wp-block-paragraph">and I just wake up</p>



<p class="wp-block-paragraph">because I know somebody subscribed or followed.</p>



<p class="wp-block-paragraph">It just wakes me up and I get filled with joy.</p>



<p class="wp-block-paragraph">That&#8217;s exactly what happens every single time.</p>



<p class="wp-block-paragraph">So you could do it as a nice favor to me or you could you could troll me if you want to just wake me up in the middle</p>



<p class="wp-block-paragraph">And I just subscribe and then I&#8217;ll just wake up. I promise that&#8217;s what will happen</p>



<p class="wp-block-paragraph">Also, if you look at the middle of the screen right now, you should see a QR code which you can scan</p>



<p class="wp-block-paragraph">in order to go to the website which I think is also named somewhere at the bottom of this video</p>



<p class="wp-block-paragraph">and it&#8217;ll take you to my main website where you can just kind of like see</p>



<p class="wp-block-paragraph">all the videos I published and the services and tutorials and things that I offer</p>



<p class="wp-block-paragraph">and all that good stuff and</p>



<p class="wp-block-paragraph">and uh if you have a suggestion for uh uh clarifications or errata or just future videos</p>



<p class="wp-block-paragraph">that you want to see please leave a comment or if you just want to say hey what&#8217;s up what&#8217;s going on</p>



<p class="wp-block-paragraph">you know just send me a comment whatever i also wake up for those in the middle of the night i</p>



<p class="wp-block-paragraph">get i wake up in a cold sweat and i&#8217;m like it would really it really mean the world to me i</p>



<p class="wp-block-paragraph">would really appreciate it so again thank you so much for watching this video and um enjoy the cool</p>



<p class="wp-block-paragraph">Enjoy the cool music as I fade into the darkness which is coming for us all.</p>



<p class="wp-block-paragraph">Thank you.</p>
<p>The post <a href="https://www.NeuralLantern.com/demystifying-x86-64-program-segments-stack-heap-and-more/">Demystifying x86-64 Program Segments: Stack, Heap, and More</a> appeared first on <a href="https://www.NeuralLantern.com">NeuralLantern.com</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.NeuralLantern.com/demystifying-x86-64-program-segments-stack-heap-and-more/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>How to Input Double Precision Floating Point Numbers in C: Step-by-Step Guide with Error Handling</title>
		<link>https://www.NeuralLantern.com/how-to-input-double-precision-floating-point-numbers-in-c-step-by-step-guide-with-error-handling/</link>
					<comments>https://www.NeuralLantern.com/how-to-input-double-precision-floating-point-numbers-in-c-step-by-step-guide-with-error-handling/#respond</comments>
		
		<dc:creator><![CDATA[mike]]></dc:creator>
		<pubDate>Tue, 15 Jul 2025 02:58:41 +0000</pubDate>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[Computer Science]]></category>
		<category><![CDATA[C input validation]]></category>
		<category><![CDATA[C programming]]></category>
		<category><![CDATA[C tutorial]]></category>
		<category><![CDATA[coding tutorial]]></category>
		<category><![CDATA[double precision float]]></category>
		<category><![CDATA[error handling in C]]></category>
		<category><![CDATA[fgets in C]]></category>
		<category><![CDATA[input in C]]></category>
		<category><![CDATA[programming for beginners]]></category>
		<category><![CDATA[sscanf in C]]></category>
		<guid isPermaLink="false">https://www.NeuralLantern.com/?p=180</guid>

					<description><![CDATA[<p>Learn to input double precision floats in C with this easy guide! Covers fgets, sscanf, error handling, and code examples for beginners.</p>
<p>The post <a href="https://www.NeuralLantern.com/how-to-input-double-precision-floating-point-numbers-in-c-step-by-step-guide-with-error-handling/">How to Input Double Precision Floating Point Numbers in C: Step-by-Step Guide with Error Handling</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="How to Input Double Precision Floating Point Numbers in C: Step-by-Step Guide with Error Handling" width="1380" height="776" src="https://www.youtube.com/embed/-sBQdPUd-IM?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 class="wp-block-paragraph">Want to learn how to input double precision floating point numbers in C? This beginner-friendly tutorial walks you through the process step-by-step, showing how to use fgets and sscanf to get user input, validate it, and handle errors like a pro. With clear code examples and a simple program demo, you’ll see how to check if input succeeds or fails. Perfect for C programming newbies or anyone looking to sharpen their skills. Subscribe for more practical coding tips and tutorials!</p>



<p class="wp-block-paragraph">Introduction 00:00:00<br>Overview of Inputting Doubles 00:00:02<br>Program Setup and Includes 00:00:44<br>Main Function Explanation 00:01:13<br>Get Input Function Introduction 00:02:10<br>Defining Get Input Function 00:02:34<br>Character Buffer Creation 00:02:54<br>Using fgets for Input 00:03:49<br>Parsing Input with sscanf 00:06:33<br>Handling Success and Failure 00:08:07<br>Running the Program 00:09:05<br>Testing with Valid Input 00:09:20<br>Testing with Invalid Input 00:10:05<br>Conclusion and Call to Action 00:11:50</p>



<p class="wp-block-paragraph">Thanks for watching!</p>



<p class="wp-block-paragraph">Find us on other social media here:</p>



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



<p class="wp-block-paragraph">Please help support us!</p>



<ul class="wp-block-list">
<li>Subscribing + Sharing on Social Media</li>



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



<li>Subscribing to our Blog</li>



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



<p class="wp-block-paragraph">Hey there!</p>



<p class="wp-block-paragraph">In this video I&#8217;m going to show you how to input a double precision floating point number</p>



<p class="wp-block-paragraph">in C from the user into your program and decide whether or not it has succeeded or failed.</p>



<p class="wp-block-paragraph">Okay so for starters I just want you to know that this video is not about generating makefiles.</p>



<p class="wp-block-paragraph">own make files or how to compile link execute or even the basics of c see my other videos for now</p>



<p class="wp-block-paragraph">i&#8217;m just going to assume that you know how to make a make file here it is basically very quickly</p>



<p class="wp-block-paragraph">but i&#8217;m just i&#8217;m just compiling a simple program here&#8217;s my program here it&#8217;s called main.c it</p>



<p class="wp-block-paragraph">doesn&#8217;t really have anything else to it i&#8217;m going to start off by importing a couple includes or a</p>



<p class="wp-block-paragraph">few includes that&#8217;ll just help me get some stuff that i need so here&#8217;s an include for the standard</p>



<p class="wp-block-paragraph">input output here&#8217;s an include for the standard library here&#8217;s an include that</p>



<p class="wp-block-paragraph">helps with c string functions here&#8217;s an include that helps with limits not</p>



<p class="wp-block-paragraph">actually sure every single one of those includes is needed but we&#8217;ll see anyway</p>



<p class="wp-block-paragraph">here&#8217;s my main program or I guess my main function since this program only is</p>



<p class="wp-block-paragraph">just one module notice how we have a double called value here and then we</p>



<p class="wp-block-paragraph">make a call to a function called get input and we give a pointer to to the</p>



<p class="wp-block-paragraph">double as an argument to get input so basically get input should be able to just return to us</p>



<p class="wp-block-paragraph">the value by reference and then get input will be able to tell us whether or not it has succeeded</p>



<p class="wp-block-paragraph">as in has the user entered valid input by its return type or not not its return type but its</p>



<p class="wp-block-paragraph">return value so the result is going to be a long from get input and that&#8217;ll indicate</p>



<p class="wp-block-paragraph">runs we&#8217;re just going to print you know get input returned and then print the return value that it</p>



<p class="wp-block-paragraph">returned and then it&#8217;s going to print the double that it inputted from the user and then a new line</p>



<p class="wp-block-paragraph">so nothing uh nothing too big of a deal let me show you the main workhorse function that has</p>



<p class="wp-block-paragraph">all the logic you need to input from the user and check that the input was valid so this is called</p>



<p class="wp-block-paragraph">get input i&#8217;m sticking it on top of the main function because i don&#8217;t want to deal with</p>



<p class="wp-block-paragraph">because I don&#8217;t want to deal with prototypes right now.</p>



<p class="wp-block-paragraph">It&#8217;s a better idea to put prototypes up at the top of your source code file</p>



<p class="wp-block-paragraph">if your source code is even a little bit complicated.</p>



<p class="wp-block-paragraph">But in this case, it&#8217;s just one other function.</p>



<p class="wp-block-paragraph">So I will just put get input on top of main.</p>



<p class="wp-block-paragraph">That way, when the compiler is scanning the source code,</p>



<p class="wp-block-paragraph">it will have already seen that get input exists before main calls it,</p>



<p class="wp-block-paragraph">and it&#8217;ll compile.</p>



<p class="wp-block-paragraph">So first thing I&#8217;m going to do is say that get input returns along</p>



<p class="wp-block-paragraph">get input returns along like we just saw and it takes in a pointer to a double no references just</p>



<p class="wp-block-paragraph">a pointer then we have to create a character buffer and what that&#8217;s going to be for is basically</p>



<p class="wp-block-paragraph">you know the user is going to type some characters and i want that to go into the buffer so um</p>



<p class="wp-block-paragraph">a line max is the number of characters that the buffer can hold and that&#8217;s uh i think where we</p>



<p class="wp-block-paragraph">I think where we have limits, I think limits is providing that for us.</p>



<p class="wp-block-paragraph">But basically, you know, you could stick a number here if you wanted to like an eight</p>



<p class="wp-block-paragraph">character, eight kilobyte buffer, if you wanted like eight, one, nine, two, or just whatever.</p>



<p class="wp-block-paragraph">I&#8217;m just putting line max so that it sort of aligns with the system&#8217;s idea of what</p>



<p class="wp-block-paragraph">the maximum size of a line should be.</p>



<p class="wp-block-paragraph">Okay.</p>



<p class="wp-block-paragraph">So then I don&#8217;t know why I have it say grab side one.</p>



<p class="wp-block-paragraph">I&#8217;ll just say grab input here.</p>



<p class="wp-block-paragraph">Let me change my solution real fast.</p>



<p class="wp-block-paragraph">Grab input.</p>



<p class="wp-block-paragraph">That&#8217;s probably because that was part of a program.</p>



<p class="wp-block-paragraph">that&#8217;s probably because that was part of a program in the past so then the function that I&#8217;m going to</p>



<p class="wp-block-paragraph">actually use is called fget s which basically is saying let&#8217;s uh let&#8217;s input a string a c string</p>



<p class="wp-block-paragraph">from a file and we&#8217;re providing the target for the input so that&#8217;s going to be the character string</p>



<p class="wp-block-paragraph">right there and then we&#8217;re going to provide um the maximum number of characters that we&#8217;ll accept</p>



<p class="wp-block-paragraph">length of the buffer and then what file are we going to input from standard</p>



<p class="wp-block-paragraph">input if you don&#8217;t know standard input see my other videos but it&#8217;s basically a</p>



<p class="wp-block-paragraph">special file handle that means when the program first launches you know the</p>



<p class="wp-block-paragraph">operating system sort of gives it a special file handle for standard input</p>



<p class="wp-block-paragraph">standard output standard error and so this will be basically the user typing</p>



<p class="wp-block-paragraph">on the terminal so f get s will essentially take input from standard</p>



<p class="wp-block-paragraph">terminal until they you know hit like a new line or they disconnect the input</p>



<p class="wp-block-paragraph">buffer it will take up to this many characters line max which should match</p>



<p class="wp-block-paragraph">the buffer whatever value you put in the buffer and then it will write the</p>



<p class="wp-block-paragraph">characters into the buffer that&#8217;s why you have to match the length of the</p>



<p class="wp-block-paragraph">buffer so basically here this function call will return something if it returns</p>



<p class="wp-block-paragraph">a zero then that means we have failed so if it returns null then we&#8217;re going to return zero</p>



<p class="wp-block-paragraph">meaning fail so get input is kind of a boolean even though we don&#8217;t have booleans in c and it&#8217;s</p>



<p class="wp-block-paragraph">basically going to return a zero on a failure and a one on success so like you know zero is a falsy</p>



<p class="wp-block-paragraph">value and one is a truely value so we&#8217;re not going to use system style return codes where zero is</p>



<p class="wp-block-paragraph">codes where zero is success we&#8217;re going to use boolean style return codes where a zero is failure</p>



<p class="wp-block-paragraph">and one is uh success so you can see i&#8217;ve kind of chained two commands here i&#8217;ve decided to say all</p>



<p class="wp-block-paragraph">right um we&#8217;ll first grab some input into the buffer array with f get s if that is null then</p>



<p class="wp-block-paragraph">we immediately return zero because this this logical or operator says if one or the other thing</p>



<p class="wp-block-paragraph">is equal to a true then just go ahead and evaluate to true so here I&#8217;m asking like did the call</p>



<p class="wp-block-paragraph">equal null if the answer is yes then that part will evaluate to true and then it&#8217;ll basically</p>



<p class="wp-block-paragraph">short circuit the rest of that logical expression and just return zero right away and then if the</p>



<p class="wp-block-paragraph">previous command succeeds like the fget s succeeds then it will look at the buffer and try to scan it</p>



<p class="wp-block-paragraph">for valid input.</p>



<p class="wp-block-paragraph">So the left side is just taking raw input.</p>



<p class="wp-block-paragraph">The right side is parsing for certain data type.</p>



<p class="wp-block-paragraph">So you can see sscanf,</p>



<p class="wp-block-paragraph">we&#8217;re scanning a buffer that we put into the first argument.</p>



<p class="wp-block-paragraph">So that&#8217;s at that point, by the time sscanf gets called,</p>



<p class="wp-block-paragraph">the buffer should be filled</p>



<p class="wp-block-paragraph">with whatever input the user typed.</p>



<p class="wp-block-paragraph">So we&#8217;re scanning that</p>



<p class="wp-block-paragraph">and then here&#8217;s a little formatting argument,</p>



<p class="wp-block-paragraph">which is very similar to,</p>



<p class="wp-block-paragraph">use the same tokens basically as printf. So, you know, here we&#8217;re saying percent lf. So we&#8217;re</p>



<p class="wp-block-paragraph">looking for a long float, which means a double precision floating point number. And that</p>



<p class="wp-block-paragraph">percentage just means like, here&#8217;s a special token that comes after it to describe what sort of</p>



<p class="wp-block-paragraph">value we&#8217;re looking to scan for. So then if we&#8217;re successful, if it actually scans,</p>



<p class="wp-block-paragraph">the double. So fscanf expects a pointer to the variable that&#8217;s going to receive the value that</p>



<p class="wp-block-paragraph">has been scanned if it is scanned correctly. So notice how the double we don&#8217;t have an ampersand</p>



<p class="wp-block-paragraph">in front of it because it&#8217;s already a pointer because it came into us as a pointer in the first</p>



<p class="wp-block-paragraph">place. So we&#8217;re going to look at the buffer that the user just typed, the user just provided,</p>



<p class="wp-block-paragraph">and we&#8217;re going to try to scan for a double precision floating point number. If we&#8217;re</p>



<p class="wp-block-paragraph">then the system should write that double into the variable referred to by the</p>



<p class="wp-block-paragraph">pointer known as the double if all that succeeded then the value should be</p>



<p class="wp-block-paragraph">greater than or equal to zero if these if this command failed then the value</p>



<p class="wp-block-paragraph">should be less than one so that basically means if we have a value of</p>



<p class="wp-block-paragraph">less than one then we have failed and we&#8217;re going to return zero so now whether</p>



<p class="wp-block-paragraph">the initial scanning sorry whether the initial input fails or the parsing of</p>



<p class="wp-block-paragraph">the users input fails then we&#8217;re gonna end up returning zero for fail so then</p>



<p class="wp-block-paragraph">in main you can see all we&#8217;re doing is we&#8217;re just calling on input we&#8217;re giving</p>



<p class="wp-block-paragraph">it a pointer to the double that we want to populate and then we&#8217;re looking at</p>



<p class="wp-block-paragraph">the result so in your you know program logic you can do something like if the</p>



<p class="wp-block-paragraph">result is zero then assume we failed and if the result is one assume we succeeded</p>



<p class="wp-block-paragraph">And if the result is one, assume we succeeded and, you know, respond to the user or, you</p>



<p class="wp-block-paragraph">know, do something that&#8217;s appropriate for success or fail.</p>



<p class="wp-block-paragraph">I think that&#8217;s everything that I need to explain.</p>



<p class="wp-block-paragraph">Let&#8217;s try to run the program now.</p>



<p class="wp-block-paragraph">Open a terminal.</p>



<p class="wp-block-paragraph">I want to do make run and see if that works.</p>



<p class="wp-block-paragraph">Maybe I&#8217;ll do a clear here.</p>



<p class="wp-block-paragraph">Okay.</p>



<p class="wp-block-paragraph">So I didn&#8217;t print any, you know, prompting or anything special.</p>



<p class="wp-block-paragraph">so the cursor is just blinking at this point i can just enter 33.12 i guess whatever hit enter</p>



<p class="wp-block-paragraph">and the program now says get it get input returned one which means success so the value that i entered</p>



<p class="wp-block-paragraph">was a valid double and the value is now 33.12 so it successfully grabbed the float keep in mind</p>



<p class="wp-block-paragraph">these zeros or just precision related issues are related to the printing of the double not</p>



<p class="wp-block-paragraph">not necessarily related to what is inside of the double so I&#8217;m just saying</p>



<p class="wp-block-paragraph">you know print along float you can you can mess with print F a little bit more</p>



<p class="wp-block-paragraph">if you want to have a different value printed or more precision or less</p>



<p class="wp-block-paragraph">percent precision printed let&#8217;s try to do this with some invalid input I&#8217;m just</p>



<p class="wp-block-paragraph">going to do some letters and hit enter notice how the value didn&#8217;t actually</p>



<p class="wp-block-paragraph">change it&#8217;s it started or actually it did change it got set to a zero because</p>



<p class="wp-block-paragraph">initialized it as junk data so it got set to a zero but then more importantly get input returns</p>



<p class="wp-block-paragraph">zero as its return code so when you see zero that means the command failed so you should not consider</p>



<p class="wp-block-paragraph">the double as valid in any way you shouldn&#8217;t use it you should complain to the user or do whatever</p>



<p class="wp-block-paragraph">and that is how to get input let&#8217;s just try something for fun double value equals 3.333</p>



<p class="wp-block-paragraph">bad input i&#8217;m assuming it might but i haven&#8217;t done this in a while so we&#8217;ll see so bad input</p>



<p class="wp-block-paragraph">and looks like it did not change it so whatever junk data was already in value by the time you</p>



<p class="wp-block-paragraph">called on get input if get input failed then the value should probably be unchanged but again more</p>



<p class="wp-block-paragraph">importantly the return value is zero meaning fail because zero is a falsy value so i&#8217;m going to get</p>



<p class="wp-block-paragraph">so i&#8217;m going to get rid of that so i can go back to my solution there okay so now hopefully you are</p>



<p class="wp-block-paragraph">uh you&#8217;re on your way to start getting input in c at least in the in in terms of longs uh you can</p>



<p class="wp-block-paragraph">look up the formatting codes for print f if you would like to input different types of data like</p>



<p class="wp-block-paragraph">so this video is just for long floats doubles uh you can you can look up all other all sorts of</p>



<p class="wp-block-paragraph">five years I&#8217;ll make another video that has different types of tokens but I guess that&#8217;s</p>



<p class="wp-block-paragraph">it for this video. Thanks for watching I hope you learned a little bit and had a little bit of fun.</p>



<p class="wp-block-paragraph">See you in the next one.</p>



<p class="wp-block-paragraph">Hey everybody thanks for watching this video again from the bottom of my heart I really</p>



<p class="wp-block-paragraph">appreciate it. I do hope you did learn something and have some fun. If you could do me a please</p>



<p class="wp-block-paragraph">subscribe and follow this channel or these videos or whatever it is you do</p>



<p class="wp-block-paragraph">on the current social media website that you&#8217;re looking at right now it would</p>



<p class="wp-block-paragraph">really mean the world to me and it&#8217;ll help make more videos and grow this</p>



<p class="wp-block-paragraph">community so we&#8217;ll be able to do more videos longer videos better videos or</p>



<p class="wp-block-paragraph">just I&#8217;ll be able to keep making videos in general so please do do me a</p>



<p class="wp-block-paragraph">kindness and and subscribe you know sometimes I&#8217;m sleeping in the middle of</p>



<p class="wp-block-paragraph">the night and I just wake up because I know somebody subscribed or followed it</p>



<p class="wp-block-paragraph">followed it just wakes me up and I get filled with joy that&#8217;s exactly what</p>



<p class="wp-block-paragraph">happens every single time so you could do it as a nice favor to me or you could</p>



<p class="wp-block-paragraph">you control me if you want to just wake me up in the middle of the night just</p>



<p class="wp-block-paragraph">subscribe and then I&#8217;ll just wake up I promise that&#8217;s what will happen also if</p>



<p class="wp-block-paragraph">you look at the middle of the screen right now you should see a QR code which</p>



<p class="wp-block-paragraph">you can scan in order to go to the website which I think is also named</p>



<p class="wp-block-paragraph">somewhere at the bottom of this video and it&#8217;ll take you to my main website</p>



<p class="wp-block-paragraph">where you can just kind of like see all the videos I published and the services</p>



<p class="wp-block-paragraph">videos I published and the services and tutorials and things that I offer and all that good stuff</p>



<p class="wp-block-paragraph">and uh if you have a suggestion for uh uh clarifications or errata or just future videos</p>



<p class="wp-block-paragraph">that you want to see please leave a comment or if you just want to say hey what&#8217;s up what&#8217;s going on</p>



<p class="wp-block-paragraph">you know just send me a comment whatever I also wake up for those in the middle of the night I</p>



<p class="wp-block-paragraph">get I wake up in a cold sweat and I&#8217;m like it would really it really mean the world to me I</p>



<p class="wp-block-paragraph">it. So again, thank you so much for watching this video and enjoy the cool music as I fade</p>



<p class="wp-block-paragraph">into the darkness, which is coming for us all.</p>



<p class="wp-block-paragraph">Thank you.</p>
<p>The post <a href="https://www.NeuralLantern.com/how-to-input-double-precision-floating-point-numbers-in-c-step-by-step-guide-with-error-handling/">How to Input Double Precision Floating Point Numbers in C: Step-by-Step Guide with Error Handling</a> appeared first on <a href="https://www.NeuralLantern.com">NeuralLantern.com</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.NeuralLantern.com/how-to-input-double-precision-floating-point-numbers-in-c-step-by-step-guide-with-error-handling/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Master Debugging with xxd: Inspect Program Output at a Byte Level for Automation &#038; Clean Code!</title>
		<link>https://www.NeuralLantern.com/master-debugging-with-xxd-inspect-program-output-at-a-byte-level-for-automation-clean-code/</link>
					<comments>https://www.NeuralLantern.com/master-debugging-with-xxd-inspect-program-output-at-a-byte-level-for-automation-clean-code/#respond</comments>
		
		<dc:creator><![CDATA[mike]]></dc:creator>
		<pubDate>Mon, 23 Jun 2025 02:41:27 +0000</pubDate>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[Computer Science]]></category>
		<category><![CDATA[Debugging]]></category>
		<category><![CDATA[Videos]]></category>
		<category><![CDATA[assembly programming]]></category>
		<category><![CDATA[automation debugging]]></category>
		<category><![CDATA[binary file inspection]]></category>
		<category><![CDATA[byte level debugging]]></category>
		<category><![CDATA[clean code]]></category>
		<category><![CDATA[coding tips]]></category>
		<category><![CDATA[coding tutorial]]></category>
		<category><![CDATA[debugging]]></category>
		<category><![CDATA[linux tools]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[programming errors]]></category>
		<category><![CDATA[software development]]></category>
		<category><![CDATA[terminal commands]]></category>
		<category><![CDATA[text file debugging]]></category>
		<category><![CDATA[xxd]]></category>
		<guid isPermaLink="false">https://www.NeuralLantern.com/?p=165</guid>

					<description><![CDATA[<p>Debug program output with xxd! Learn to inspect text/binary files at byte level, fix hidden issues, and ensure clean code. Perfect for automation &#038; assignments.</p>
<p>The post <a href="https://www.NeuralLantern.com/master-debugging-with-xxd-inspect-program-output-at-a-byte-level-for-automation-clean-code/">Master Debugging with xxd: Inspect Program Output at a Byte Level for Automation &amp; Clean Code!</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="Master Debugging with xxd: Inspect Program Output at a Byte Level for Automation &amp; Clean Code!" width="1380" height="776" src="https://www.youtube.com/embed/UyE5t01Vnd0?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 class="wp-block-paragraph">Hey coders, ever wonder why your program’s acting funky? Join me to master debugging with xxd! This vid walks you through inspecting text or binary file output at the byte level, catching sneaky characters like null bytes that mess up automation or school assignments. From assembly to redirects, learn practical tips to ensure clean output. Perfect for beginners and pros alike! Hit subscribe for more coding tutorials, and check my site for extra goodies. Let’s debug smarter, not harder!</p>



<p class="wp-block-paragraph">Introduction to xxd 00:00:00<br>Purpose of debugging 00:00:12<br>Why debug output 00:00:23<br>Example assembly program 00:00:56<br>Running the program 00:01:26<br>Corrupting the message 00:01:45<br>Terminal vs automation 00:02:27<br>Installing xxd 00:03:16<br>Redirecting output 00:03:29<br>Using xxd to debug 00:04:33<br>Identifying newline issue 00:05:36<br>Finding zero in string 00:06:04<br>Debugging conclusion 00:06:32<br>Weird symbols warning 00:06:47<br>Debugging standard error 00:07:23<br>Closing remarks 00:07:40<br>Call to subscribe 00:07:56<br>QR code and website 00:08:45<br>Request for comments 00:09:09<br>Fade out 00:09:31</p>



<p class="wp-block-paragraph">Thanks for watching!</p>



<p class="wp-block-paragraph">Find us on other social media here:</p>



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



<p class="wp-block-paragraph">Please help support us!</p>



<ul class="wp-block-list">
<li>Subscribing + Sharing on Social Media</li>



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



<li>Subscribing to our Blog</li>



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



<p class="wp-block-paragraph">Hey there, I&#8217;d like to talk to you about debugging your program&#8217;s output with xxd.</p>



<p class="wp-block-paragraph">What am I talking about? Well, there&#8217;s a program called xxd that can sort of inspect</p>



<p class="wp-block-paragraph">at the byte level the output of some text file or binary file. It&#8217;s really useful for</p>



<p class="wp-block-paragraph">inspecting things. Why would you want to debug the output of your program? Well, I don&#8217;t know.</p>



<p class="wp-block-paragraph">print a message from your program the program might be printing some extra characters somewhere</p>



<p class="wp-block-paragraph">or some garbage and maybe maybe you don&#8217;t notice because you&#8217;re sort of looking at your program&#8217;s</p>



<p class="wp-block-paragraph">output in the terminal but then maybe if your output is being sent to another program</p>



<p class="wp-block-paragraph">for the purposes of automation or like you&#8217;re getting a grade from a class or something like</p>



<p class="wp-block-paragraph">that then um well it can it can sort of it can sort of get you without you realizing what&#8217;s wrong</p>



<p class="wp-block-paragraph">you an example real fast of what i&#8217;m talking about i have a simple assembly program here this is not</p>



<p class="wp-block-paragraph">a video about how to write assembly code so just assume that you know if you want to know assembly</p>



<p class="wp-block-paragraph">see my other videos but um i&#8217;m just going to print a simple message i&#8217;m just going to print</p>



<p class="wp-block-paragraph">hello i feel awesome and then the length of the message i&#8217;m specifying here you don&#8217;t know you</p>



<p class="wp-block-paragraph">don&#8217;t need to know too much about this for this particular video i&#8217;m just printing a string in</p>



<p class="wp-block-paragraph">assembly that&#8217;s all i&#8217;m doing and then here you can see i&#8217;m just kind of like printing it and then</p>



<p class="wp-block-paragraph">I&#8217;m just kind of like printing it and then I&#8217;m like exiting.</p>



<p class="wp-block-paragraph">So if we go back to the terminal here, I can type make run.</p>



<p class="wp-block-paragraph">This is not a make file video.</p>



<p class="wp-block-paragraph">See my other videos.</p>



<p class="wp-block-paragraph">I want to type make run just to compile and run the program.</p>



<p class="wp-block-paragraph">And then here&#8217;s the message that it prints, right?</p>



<p class="wp-block-paragraph">Hello, I feel awesome.</p>



<p class="wp-block-paragraph">It looks good in the terminal.</p>



<p class="wp-block-paragraph">I can&#8217;t be 100% sure though, because think about this.</p>



<p class="wp-block-paragraph">What if I corrupted the message on purpose?</p>



<p class="wp-block-paragraph">This can happen in assembly all the time.</p>



<p class="wp-block-paragraph">Wrong or if you add extra characters at the end of it or even if you have you know a corrupted register somewhere</p>



<p class="wp-block-paragraph">That either reads from the wrong spot</p>



<p class="wp-block-paragraph">To get a string or maybe it it writes over the string for some reason. There&#8217;s a lot of things that can go wrong</p>



<p class="wp-block-paragraph">So for now just suppose that I&#8217;ve corrupted my message somehow by I don&#8217;t know. Let&#8217;s put a zero</p>



<p class="wp-block-paragraph">In between the string so I&#8217;m gonna say is that it&#8217;s gonna be like H E L</p>



<p class="wp-block-paragraph">0 L O and what that should do is</p>



<p class="wp-block-paragraph">lo and what that should do uh well it should look like just the word hello but if i&#8217;m automating</p>



<p class="wp-block-paragraph">this program or sending it in to get a grade at a school somewhere then this should you know not</p>



<p class="wp-block-paragraph">match what is expected if i&#8217;m automating with like another system in some way but it should look okay</p>



<p class="wp-block-paragraph">in the terminal let&#8217;s see i&#8217;m going to do clear and make run notice how the message looks the same</p>



<p class="wp-block-paragraph">I feel awesome. So if I was automating, you know, if I was sending this string to another program</p>



<p class="wp-block-paragraph">for automation, I might be convinced that I&#8217;m not doing anything wrong. And I&#8217;ll, I&#8217;ll think it&#8217;s</p>



<p class="wp-block-paragraph">the other side that is wrong or, or our computers are just buggy or something like that. But actually</p>



<p class="wp-block-paragraph">I&#8217;ve outputted a corrupted message for some reason. So how can I really be sure that my</p>



<p class="wp-block-paragraph">message is what I think I&#8217;m seeing in the terminal? That&#8217;s the XXD program. Pretty easy.</p>



<p class="wp-block-paragraph">apt install xxd let me see if I have it on this system oh it&#8217;s already installed</p>



<p class="wp-block-paragraph">okay good so I&#8217;m gonna do clear and make run again so there it is I would like to</p>



<p class="wp-block-paragraph">just redirect the standard output pipe to a file with a special command in the</p>



<p class="wp-block-paragraph">shell let me just show you real fast I am definitely printing to std out the</p>



<p class="wp-block-paragraph">standard pipe which is just a number one file descriptor if you don&#8217;t understand</p>



<p class="wp-block-paragraph">If you don&#8217;t understand that, I have other videos, but I&#8217;m going to run the program again,</p>



<p class="wp-block-paragraph">but then I&#8217;m going to redirect the output to a file and I&#8217;m going to call it stdout.txt.</p>



<p class="wp-block-paragraph">It&#8217;s probably a good idea to eliminate spaces.</p>



<p class="wp-block-paragraph">I think I&#8217;ve made mistakes in other videos where I didn&#8217;t put a space before or after</p>



<p class="wp-block-paragraph">this little redirector symbol and I ended up creating a file just called the number</p>



<p class="wp-block-paragraph">two or the number one.</p>



<p class="wp-block-paragraph">But pipe number one is standard output.</p>



<p class="wp-block-paragraph">So I&#8217;m going to say anything that comes to pipe one, which is standard output, I&#8217;m just</p>



<p class="wp-block-paragraph">output I&#8217;m just going to redirect it to a text file. So when I hit enter notice</p>



<p class="wp-block-paragraph">how nothing prints to the screen but the standard output text file that I</p>



<p class="wp-block-paragraph">specified is now created. If I spill the contents of std out it still looks</p>



<p class="wp-block-paragraph">normal so don&#8217;t let that trick you but watch what happens if I use xxd.</p>



<p class="wp-block-paragraph">xxd std out basically xxd is going to read it as a binary file and tell me</p>



<p class="wp-block-paragraph">every single character in there while I oh I forgot to change the title of that</p>



<p class="wp-block-paragraph">that&#8217;s not this video but notice right here where it says H E L L O and then</p>



<p class="wp-block-paragraph">right before that there&#8217;s like a little dot if you look at the dot let&#8217;s see</p>



<p class="wp-block-paragraph">what is the dot it&#8217;s one two three four five six it&#8217;s the sixth thing one two</p>



<p class="wp-block-paragraph">one two three four five six seven eight nine ten twelve thirteen fourteen fifteen oh there&#8217;s</p>



<p class="wp-block-paragraph">fifteen characters but there&#8217;s not that many i think probably they&#8217;re packing them by two here</p>



<p class="wp-block-paragraph">let me see if that makes more sense now one two three four five six one two three four five six</p>



<p class="wp-block-paragraph">yeah that makes a lot more sense so the sixth uh byte not the sixth uh word uh is basically</p>



<p class="wp-block-paragraph">to this dot right here and if you recall oa is a decimal 10 which is uh the beginning of a new line</p>



<p class="wp-block-paragraph">you know like a carriage return line feed um 13 comma 10 so that&#8217;s what it is right there</p>



<p class="wp-block-paragraph">and then so now we know that there&#8217;s xxd is figuring out here or helping us understand</p>



<p class="wp-block-paragraph">that there&#8217;s a new line between slash main and then the message which if you recall that&#8217;s</p>



<p class="wp-block-paragraph">exactly what happened and then when we look into our hello string there&#8217;s a dot in the middle here</p>



<p class="wp-block-paragraph">string there&#8217;s a dot in the middle here what is that dot well we just have to figure out what</p>



<p class="wp-block-paragraph">number that corresponds to so the oa was that dot right there so we just got to go a couple more</p>



<p class="wp-block-paragraph">bytes forward one two three four bytes forward one two three four bytes forward it&#8217;s this zero right</p>



<p class="wp-block-paragraph">so xxd even though the terminal didn&#8217;t help us originally xxd has helped us realize that we&#8217;re</p>



<p class="wp-block-paragraph">that we&#8217;re actually printing a zero under the hood and we can&#8217;t see it in the terminal.</p>



<p class="wp-block-paragraph">So we have now debugged our output. You know, depending on where you print this,</p>



<p class="wp-block-paragraph">you might see nothing like in the terminal. It didn&#8217;t really show anything. But if you&#8217;re on a</p>



<p class="wp-block-paragraph">webpage somewhere or some other type of thing that&#8217;s going to print the data for you, you might</p>



<p class="wp-block-paragraph">see a weird symbol. I mean, it really depends. This is not something you&#8217;ll see everywhere, but</p>



<p class="wp-block-paragraph">you might see a weird symbol that kind of looks like a black diamond with a question mark inside</p>



<p class="wp-block-paragraph">mark inside of it or some other weird symbol.</p>



<p class="wp-block-paragraph">If you start seeing weird symbols all over the place or just in a place where you know</p>



<p class="wp-block-paragraph">there&#8217;s not supposed to be a weird symbol, that might be an indication to you that you</p>



<p class="wp-block-paragraph">have weird values sitting inside of your string.</p>



<p class="wp-block-paragraph">And then you should probably try to debug with XXD.</p>



<p class="wp-block-paragraph">So that&#8217;s it.</p>



<p class="wp-block-paragraph">That&#8217;s the basics for debugging with XXD.</p>



<p class="wp-block-paragraph">Just, you know, file output anyway, for your information.</p>



<p class="wp-block-paragraph">If you wanted to debug the standard error pipe, you could also do this.</p>



<p class="wp-block-paragraph">you know because the error pipe is just a two um and that&#8217;s basically it i&#8217;m not going to</p>



<p class="wp-block-paragraph">debug right now because that&#8217;s that&#8217;s too easy but i hope you enjoyed this video thank you for</p>



<p class="wp-block-paragraph">watching i hope you learned a little bit and had a little bit of fun i&#8217;ll see you in the next video</p>



<p class="wp-block-paragraph">hey everybody thanks for watching this video again from the bottom of my heart i really</p>



<p class="wp-block-paragraph">appreciate it i do hope you did learn something and have some fun uh if you could do me a please</p>



<p class="wp-block-paragraph">If you could do me a please, a small little favor, could you please subscribe and follow this channel or these videos or whatever it is you do on the current social media website that you&#8217;re looking at right now.</p>



<p class="wp-block-paragraph">It would really mean the world to me and it&#8217;ll help make more videos and grow this community.</p>



<p class="wp-block-paragraph">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 class="wp-block-paragraph">So please do me a kindness and subscribe.</p>



<p class="wp-block-paragraph">the middle of the night and i just wake up because i know somebody subscribed or followed</p>



<p class="wp-block-paragraph">it just wakes me up and i get filled with joy that&#8217;s exactly what happens every single time</p>



<p class="wp-block-paragraph">so you could do it as a nice favor to me or you could you could troll me if you want to just wake</p>



<p class="wp-block-paragraph">me up in the middle of the night just subscribe and then i&#8217;ll i&#8217;ll just wake up i promise that&#8217;s</p>



<p class="wp-block-paragraph">what will happen also uh if you look at the middle of the screen right now you should see a qr code</p>



<p class="wp-block-paragraph">which you can scan in order to go to the website which i think is also named somewhere at the</p>



<p class="wp-block-paragraph">you to my main website where you can just kind of like see all the videos I published and the</p>



<p class="wp-block-paragraph">services and tutorials and things that I offer and all that good stuff and uh if you have a</p>



<p class="wp-block-paragraph">suggestion for uh uh clarifications or errata or just future videos that you want to see please</p>



<p class="wp-block-paragraph">leave a comment or if you just want to say hey what&#8217;s up what&#8217;s going on you know just send me</p>



<p class="wp-block-paragraph">a comment whatever I also wake up for those in the middle of the night I get I wake up in a cold</p>



<p class="wp-block-paragraph">it would really mean the world to me. I would really appreciate it. So again,</p>



<p class="wp-block-paragraph">thank you so much for watching this video and enjoy the cool music as I fade</p>



<p class="wp-block-paragraph">into the darkness, which is coming for us all.</p>



<p class="wp-block-paragraph">Thank you.</p>
<p>The post <a href="https://www.NeuralLantern.com/master-debugging-with-xxd-inspect-program-output-at-a-byte-level-for-automation-clean-code/">Master Debugging with xxd: Inspect Program Output at a Byte Level for Automation &amp; Clean Code!</a> appeared first on <a href="https://www.NeuralLantern.com">NeuralLantern.com</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.NeuralLantern.com/master-debugging-with-xxd-inspect-program-output-at-a-byte-level-for-automation-clean-code/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Master CGDB: Boost Your Debugging with GDB’s Curses Interface in Terminal</title>
		<link>https://www.NeuralLantern.com/master-cgdb-boost-your-debugging-with-gdbs-curses-interface-in-terminal/</link>
					<comments>https://www.NeuralLantern.com/master-cgdb-boost-your-debugging-with-gdbs-curses-interface-in-terminal/#respond</comments>
		
		<dc:creator><![CDATA[mike]]></dc:creator>
		<pubDate>Sun, 15 Jun 2025 23:08:57 +0000</pubDate>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[Computer Science]]></category>
		<category><![CDATA[Debugging]]></category>
		<category><![CDATA[Videos]]></category>
		<category><![CDATA[assembly debugging]]></category>
		<category><![CDATA[C debugging]]></category>
		<category><![CDATA[c++ debugging]]></category>
		<category><![CDATA[CGDB]]></category>
		<category><![CDATA[coding tutorial]]></category>
		<category><![CDATA[curses interface]]></category>
		<category><![CDATA[debugging tools]]></category>
		<category><![CDATA[GDB]]></category>
		<category><![CDATA[software development]]></category>
		<category><![CDATA[terminal debugging]]></category>
		<guid isPermaLink="false">https://www.NeuralLantern.com/?p=158</guid>

					<description><![CDATA[<p>Learn CGDB, a GDB curses layer for terminal debugging. View source code &#038; breakpoints in one window. Ideal for C++, C, &#038; assembly!</p>
<p>The post <a href="https://www.NeuralLantern.com/master-cgdb-boost-your-debugging-with-gdbs-curses-interface-in-terminal/">Master CGDB: Boost Your Debugging with GDB’s Curses Interface in Terminal</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="Master CGDB: Boost Your Debugging with GDB&#039;s Curses Interface in Terminal" width="1380" height="776" src="https://www.youtube.com/embed/grU1mA3W8K4?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 class="wp-block-paragraph">Hey coders! In this video, I’m diving into CGDB, a powerful curses layer on top of GDB that makes terminal debugging a breeze. See your source code and breakpoints side-by-side in one clean interface. I’ll walk you through setup, breakpoints, and why CGDB rocks for C++, C, and assembly projects. Watch my GDB video first for the basics (link in comments)! If you dig this, subscribe and drop a comment for more coding tips. Let’s debug smarter! #CGDB #GDB #Debugging #Coding</p>



<p class="wp-block-paragraph">Introduction to CGDB 00:00:00<br>Recommendation to Watch GDB Video 00:00:05<br>Explanation of CGDB and Curses 00:00:23<br>Building and Running CGDB 00:01:03<br>CGDB Interface Overview 00:01:56<br>Setting Breakpoints in CGDB 00:02:18<br>Managing Breakpoints 00:02:53<br>Benefits of CGDB 00:03:47<br>Closing Remarks and Subscription Request 00:04:00<br>Call for Comments and Suggestions 00:05:30</p>



<p class="wp-block-paragraph">Thanks for watching!</p>



<p class="wp-block-paragraph">Find us on other social media here:</p>



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



<p class="wp-block-paragraph">Please help support us!</p>



<ul class="wp-block-list">
<li>Subscribing + Sharing on Social Media</li>



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



<li>Subscribing to our Blog</li>



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



<p class="wp-block-paragraph">Hey everybody! In this video I&#8217;m going to talk about CGDB which is just a debugging program.</p>



<p class="wp-block-paragraph">If you haven&#8217;t seen my other video on GDB please watch that first because</p>



<p class="wp-block-paragraph">I go over all the concepts of debugging an assembly program, a C++ program, a C program</p>



<p class="wp-block-paragraph">and it&#8217;ll be much more useful than this video.</p>



<p class="wp-block-paragraph">This video is just about CGDB which is really a curses layer on top of GDB.</p>



<p class="wp-block-paragraph">on top of GDB. Curses is a library that allows programmers to kind of make</p>



<p class="wp-block-paragraph">graphical interfaces in the terminal so the point of this video is just to show</p>



<p class="wp-block-paragraph">you a slightly different way to use GDB that&#8217;s why I&#8217;m saying go check out the</p>



<p class="wp-block-paragraph">previous GDB video first but you know I think you know sometimes people like CG</p>



<p class="wp-block-paragraph">to be so what am I talking about for starters pretend that I have a program</p>



<p class="wp-block-paragraph">I&#8217;ve got an assembly module a C++ module and a make file you don&#8217;t know</p>



<p class="wp-block-paragraph">or C++, see my other videos.</p>



<p class="wp-block-paragraph">This video is only about CGDB.</p>



<p class="wp-block-paragraph">But I can do, you know, make build,</p>



<p class="wp-block-paragraph">just to build my objects and my executable.</p>



<p class="wp-block-paragraph">And then normally, if you know GDB,</p>



<p class="wp-block-paragraph">you can go GDB main and just enter the GDB terminal, right?</p>



<p class="wp-block-paragraph">This is great, you can do a lot of commands from here,</p>



<p class="wp-block-paragraph">but it&#8217;s not very graphical.</p>



<p class="wp-block-paragraph">It does have a graphical mode, to be honest,</p>



<p class="wp-block-paragraph">but it seems to be kind of glitchy,</p>



<p class="wp-block-paragraph">so I&#8217;m not really gonna talk about it.</p>



<p class="wp-block-paragraph">going to talk about it anyway so instead of launching your program inside of gdb you can</p>



<p class="wp-block-paragraph">launch your program with c gdb c standing for curses i don&#8217;t think this is installed on the</p>



<p class="wp-block-paragraph">current system so i&#8217;ll probably have to install it right okay so you can go sudo apt install c gdb</p>



<p class="wp-block-paragraph">and then just enter your password</p>



<p class="wp-block-paragraph">Let me do clear and then we&#8217;ll do CGDB main.</p>



<p class="wp-block-paragraph">And I just want you to see that it&#8217;s got two windows.</p>



<p class="wp-block-paragraph">This is the whole point for me anyway.</p>



<p class="wp-block-paragraph">The bottom window is actually GDB.</p>



<p class="wp-block-paragraph">So anything you&#8217;ve learned there is going to apply here.</p>



<p class="wp-block-paragraph">But the difference is that there&#8217;s a top panel that shows your source code.</p>



<p class="wp-block-paragraph">So I don&#8217;t know.</p>



<p class="wp-block-paragraph">Let&#8217;s suppose for the sake of argument, I&#8217;m going to hit escape so I can go up to the top panel and kind of scroll my source code.</p>



<p class="wp-block-paragraph">Let&#8217;s suppose for the sake of argument, I want to break every time I hit line 36.</p>



<p class="wp-block-paragraph">time I hit line 36 okay I&#8217;m kind of scrolling I just hit escape to get into the top panel</p>



<p class="wp-block-paragraph">I&#8217;m gonna hit I for interactive mode which lets me go back down to the bottom panel</p>



<p class="wp-block-paragraph">and then I&#8217;ll just say B for breakpoints main.cpp line 36 and then just double check that I entered</p>



<p class="wp-block-paragraph">my breakpoints correctly and then I&#8217;ll run the program again if you don&#8217;t know GDB see my other</p>



<p class="wp-block-paragraph">video but notice how I started to run the program and then it immediately hit a breakpoint and then</p>



<p class="wp-block-paragraph">the top pointed to that line where it broke if you don&#8217;t believe me i&#8217;ll do another breakpoint</p>



<p class="wp-block-paragraph">at line let&#8217;s see main.cpp line maybe uh 32 and then another one at uh 39 well maybe i should</p>



<p class="wp-block-paragraph">have done 40. yeah i should have done 40 and then 33 so that means i probably want to uh</p>



<p class="wp-block-paragraph">delete that one at 32 so i&#8217;m gonna delete whichever one shows up at 32 that&#8217;s number two</p>



<p class="wp-block-paragraph">and then info breakpoints again okay so now if i continue the program also by the way notice how</p>



<p class="wp-block-paragraph">the top panel is showing red line numbers for my breakpoints if i hit c for continue</p>



<p class="wp-block-paragraph">and it hits the same breakpoint again if i hit enter to continue one more time it&#8217;ll do that</p>



<p class="wp-block-paragraph">eventually it hits a different breakpoint and notice how the source code</p>



<p class="wp-block-paragraph">moves down to the appropriate breakpoint so for me personally i kind of like to just regularly you</p>



<p class="wp-block-paragraph">and then go back and forth between that and my code window.</p>



<p class="wp-block-paragraph">But some people really love having both their code and GDB in the same terminal</p>



<p class="wp-block-paragraph">so they don&#8217;t have to go back and forth between windows.</p>



<p class="wp-block-paragraph">So if that&#8217;s you, then maybe CGDB is for you.</p>



<p class="wp-block-paragraph">My little doggy wants to go pee.</p>



<p class="wp-block-paragraph">Okay, that&#8217;s all I have for this video.</p>



<p class="wp-block-paragraph">Thank you so much for watching,</p>



<p class="wp-block-paragraph">and I hope you learned a little bit of stuff and had a little bit of fun.</p>



<p class="wp-block-paragraph">I&#8217;ll see you in the next video.</p>



<p class="wp-block-paragraph">Hey, everybody.</p>



<p class="wp-block-paragraph">Hey everybody!</p>



<p class="wp-block-paragraph">Thanks for watching this video again from the bottom of my heart.</p>



<p class="wp-block-paragraph">I really appreciate it.</p>



<p class="wp-block-paragraph">I do hope you did learn something and have some fun.</p>



<p class="wp-block-paragraph">If you could do me a please, a small little favor,</p>



<p class="wp-block-paragraph">could you please subscribe and follow this channel or these videos</p>



<p class="wp-block-paragraph">or whatever it is you do on the current social media website</p>



<p class="wp-block-paragraph">that you&#8217;re looking at right now.</p>



<p class="wp-block-paragraph">It would really mean the world to me and it&#8217;ll help make more videos</p>



<p class="wp-block-paragraph">and grow this community.</p>



<p class="wp-block-paragraph">So we&#8217;ll be able to do more videos, longer videos, better videos,</p>



<p class="wp-block-paragraph">or just i&#8217;ll be able to keep making videos in general so please do do me a kindness and uh and</p>



<p class="wp-block-paragraph">subscribe you know sometimes i&#8217;m sleeping in the middle of the night and i just wake up because i</p>



<p class="wp-block-paragraph">know somebody subscribed or followed it just wakes me up and i get filled with joy that&#8217;s exactly what</p>



<p class="wp-block-paragraph">happens every single time so you could do it as a nice favor to me or you could you control me if</p>



<p class="wp-block-paragraph">you want to just wake me up in the middle of the night just subscribe and then i&#8217;ll i&#8217;ll just wake</p>



<p class="wp-block-paragraph">up i promise that&#8217;s what will happen also uh if you look at the middle of the screen right now you</p>



<p class="wp-block-paragraph">of the screen right now you should see a qr code which you can scan in order to go to the website</p>



<p class="wp-block-paragraph">which i think is also named somewhere at the bottom of this video and it&#8217;ll take you to my</p>



<p class="wp-block-paragraph">main website where you can just kind of like see all the videos i published and the services and</p>



<p class="wp-block-paragraph">tutorials and things that i offer and all that good stuff and uh if you have a suggestion for uh</p>



<p class="wp-block-paragraph">clarifications or errata or just future videos that you want to see please leave a comment or</p>



<p class="wp-block-paragraph">up what&#8217;s going on you know just send me a comment whatever i also wake up for those in the middle of</p>



<p class="wp-block-paragraph">the night i get i wake up in a cold sweat and i&#8217;m like it would really it really mean the world to</p>



<p class="wp-block-paragraph">me i would really appreciate it so again thank you so much for watching this video and um enjoy the</p>



<p class="wp-block-paragraph">cool music as as i fade into the darkness which is coming for us all</p>



<p class="wp-block-paragraph">Thank you.</p>
<p>The post <a href="https://www.NeuralLantern.com/master-cgdb-boost-your-debugging-with-gdbs-curses-interface-in-terminal/">Master CGDB: Boost Your Debugging with GDB’s Curses Interface in Terminal</a> appeared first on <a href="https://www.NeuralLantern.com">NeuralLantern.com</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.NeuralLantern.com/master-cgdb-boost-your-debugging-with-gdbs-curses-interface-in-terminal/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Master GNU Debugger: Debug C++ &#038; Assembly Programs with GDB Like a Pro</title>
		<link>https://www.NeuralLantern.com/master-gnu-debugger-debug-c-assembly-programs-with-gdb-like-a-pro/</link>
					<comments>https://www.NeuralLantern.com/master-gnu-debugger-debug-c-assembly-programs-with-gdb-like-a-pro/#respond</comments>
		
		<dc:creator><![CDATA[mike]]></dc:creator>
		<pubDate>Sat, 14 Jun 2025 23:53:13 +0000</pubDate>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[Computer Science]]></category>
		<category><![CDATA[Debugging]]></category>
		<category><![CDATA[Videos]]></category>
		<category><![CDATA[assembly debugging]]></category>
		<category><![CDATA[assembly programming]]></category>
		<category><![CDATA[breakpoints]]></category>
		<category><![CDATA[c++ debugging]]></category>
		<category><![CDATA[C++ programming]]></category>
		<category><![CDATA[call stack]]></category>
		<category><![CDATA[coding for beginners]]></category>
		<category><![CDATA[coding tutorial]]></category>
		<category><![CDATA[debug symbols]]></category>
		<category><![CDATA[debugging tools]]></category>
		<category><![CDATA[GDB]]></category>
		<category><![CDATA[GNU Debugger]]></category>
		<category><![CDATA[null pointer]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[software development]]></category>
		<guid isPermaLink="false">https://www.NeuralLantern.com/?p=155</guid>

					<description><![CDATA[<p>Master GDB to debug C++ &#038; assembly! Learn breakpoints, call stacks, &#038; crash fixes in this fun guide for coders.</p>
<p>The post <a href="https://www.NeuralLantern.com/master-gnu-debugger-debug-c-assembly-programs-with-gdb-like-a-pro/">Master GNU Debugger: Debug C++ &amp; Assembly Programs with GDB Like a Pro</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="Master GNU Debugger: Debug C++ &amp; Assembly Programs with GDB Like a Pro" width="1380" height="776" src="https://www.youtube.com/embed/lPcwsvSWak0?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 class="wp-block-paragraph">Hey everybody! Ready to crush those pesky bugs in your C++ and assembly programs? In this video, we’re diving deep into the GNU Debugger (GDB) – the ultimate tool for tracking down crashes, inspecting variables, and mastering your code. From setting up debug symbols to navigating call stacks, setting breakpoints, and even debugging assembly registers, this fun and relatable guide has you covered. Whether you’re a beginner coder or a seasoned dev, you’ll learn practical tips to level up your debugging game. We’ll also tackle null pointers, create GDB scripts, and explore why your program might be misbehaving. Stick around for a few laughs and some serious skills! Don’t forget to subscribe, hit that like button, and check out my website for more coding tutorials. Let’s debug like pros together!</p>



<p class="wp-block-paragraph">Introduction to GNU Debugger 00:00:00<br>What is a Debugger 00:00:20<br>Debug Symbols Explanation 00:02:08<br>Compiling with Debug Symbols 00:03:13<br>File Size Comparison 00:05:30<br>Installing and Launching GDB 00:06:21<br>GDB Console Basics 00:06:55<br>Attaching GDB to Program 00:08:12<br>Running Program in GDB 00:09:09<br>Handling Program Crash 00:13:02<br>Understanding Call Stack 00:14:05<br>Navigating Call Stack 00:18:13<br>Inspecting Variables 00:19:08<br>Null Pointer Issues 00:21:25<br>Using GDB Script 00:25:10<br>Setting Breakpoints 00:27:04<br>Managing Breakpoints 00:28:37<br>Continuing Execution 00:30:41<br>Debugging Assembly 00:33:24<br>Global Variables in Assembly 00:35:43<br>Advanced GDB Commands 00:38:31<br>Conclusion and Call to Action 00:40:22</p>



<p class="wp-block-paragraph">Thanks for watching!</p>



<p class="wp-block-paragraph">Find us on other social media here:</p>



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



<p class="wp-block-paragraph">Please help support us!</p>



<ul class="wp-block-list">
<li>Subscribing + Sharing on Social Media</li>



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



<li>Subscribing to our Blog</li>



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



<p class="wp-block-paragraph">Hey everybody! In this video I&#8217;m going to show you how to use the GNU Debugger to</p>



<p class="wp-block-paragraph">debug your C++ and assembly programs.</p>



<p class="wp-block-paragraph">You can also use it for C and some other stuff but we&#8217;re just going to focus on</p>



<p class="wp-block-paragraph">C++ and assembly. What is the GNU Debugger and what is a debugger in the</p>



<p class="wp-block-paragraph">first place? So for starters the GNU Debugger it&#8217;s like it&#8217;s called GDB and</p>



<p class="wp-block-paragraph">the db you can imagine that stands for debugger something like that and the g stands for gnu i</p>



<p class="wp-block-paragraph">personally don&#8217;t know how to uh pronounce gnu so i just say gnu because it it feels more fun to do</p>



<p class="wp-block-paragraph">it that way it&#8217;s more interesting i think probably some people just say new but i don&#8217;t like that</p>



<p class="wp-block-paragraph">because the gdb debugger is really old and robust and powerful it&#8217;s not a new tool it&#8217;s it&#8217;s an old</p>



<p class="wp-block-paragraph">new debugger someone&#8217;s always going to ask what was the old debugger and then I say this is the</p>



<p class="wp-block-paragraph">old debugger the new debugger is the old debugger anyway so we&#8217;re going to use the GNU debugger</p>



<p class="wp-block-paragraph">if you haven&#8217;t yet watched my video on caveman debugging you probably want to watch that first</p>



<p class="wp-block-paragraph">just for like some basics of you know why you might want to look at information in your program</p>



<p class="wp-block-paragraph">but for now we&#8217;ll just assume that you already know what&#8217;s going on with just like why you would</p>



<p class="wp-block-paragraph">know how to make make files see my other videos but here i just have a basic make file all it&#8217;s</p>



<p class="wp-block-paragraph">really going to do is just sort of compile a very simple program and add debug symbols into it so</p>



<p class="wp-block-paragraph">let&#8217;s double check that i&#8217;m actually doing that here yeah there we go okay so the first thing</p>



<p class="wp-block-paragraph">that i should probably show you is uh we are compiling a c plus plus source module so i&#8217;ve</p>



<p class="wp-block-paragraph">actually using c but i i have that left over in there anyway and then we&#8217;re assembling something</p>



<p class="wp-block-paragraph">in yasm we&#8217;re linking with g plus plus because we want to have a hybrid program and we&#8217;ve got</p>



<p class="wp-block-paragraph">all these flags here those are just variables and so uh so now before i can debug with gdb</p>



<p class="wp-block-paragraph">i&#8217;ve got to talk about something called debug symbols what are debug symbols imagine just a</p>



<p class="wp-block-paragraph">bunch of extra information explaining all of the design and variables and everything inside of your</p>



<p class="wp-block-paragraph">about your code stuffed directly into the executable.</p>



<p class="wp-block-paragraph">So imagine, you know, when we compile a C++ source module</p>



<p class="wp-block-paragraph">into an object file, usually that&#8217;s just, you know,</p>



<p class="wp-block-paragraph">assembly at that point.</p>



<p class="wp-block-paragraph">But when we add debug symbols, we&#8217;ll have a bunch of information</p>



<p class="wp-block-paragraph">that can help the debugger figure out where we are in the program,</p>



<p class="wp-block-paragraph">what the variables look like, you know,</p>



<p class="wp-block-paragraph">a bunch of extra information to help the debugger pretty much.</p>



<p class="wp-block-paragraph">The assembler will put debug symbols into the object code</p>



<p class="wp-block-paragraph">symbols into the object code that you generate the compilers will do that the linker will do that</p>



<p class="wp-block-paragraph">and so it&#8217;s important to understand if you don&#8217;t generate debug symbols when you&#8217;re compiling and</p>



<p class="wp-block-paragraph">linking your code then your debugger won&#8217;t actually be able to do anything it won&#8217;t understand what&#8217;s</p>



<p class="wp-block-paragraph">going on you can debug it but like you&#8217;re not going to see anything that&#8217;s very uh that&#8217;s useful</p>



<p class="wp-block-paragraph">so let me show you real fast i&#8217;m going to do make clean and then i&#8217;m going to say make build</p>



<p class="wp-block-paragraph">that I do not have debug symbols.</p>



<p class="wp-block-paragraph">All right, okay, if you don&#8217;t know how to compile and link from the command line, see</p>



<p class="wp-block-paragraph">my other videos, but for now it looks like we&#8217;re not generating debug symbols.</p>



<p class="wp-block-paragraph">So if I list the contents of my directory here, just note the size is real fast.</p>



<p class="wp-block-paragraph">I have an assembly module here that I&#8217;m calling ASI.asm, ASI for assembly, and then we&#8217;re</p>



<p class="wp-block-paragraph">assembling it down to an object file.</p>



<p class="wp-block-paragraph">Notice how it&#8217;s just 656 bytes, very small.</p>



<p class="wp-block-paragraph">plus plus module that I&#8217;m creating here is compiling to an object file that is</p>



<p class="wp-block-paragraph">about four and a half kilobytes so you know not too big the final executable is</p>



<p class="wp-block-paragraph">called main and it&#8217;s about 17 kilobytes so this is without debug symbols when</p>



<p class="wp-block-paragraph">you&#8217;re compiling something on your own you need to add extra switches to enable</p>



<p class="wp-block-paragraph">debug symbols so you can see here in this make file that I&#8217;ve made and again if</p>



<p class="wp-block-paragraph">you don&#8217;t know how to make a make file or compile or link or anything see my</p>



<p class="wp-block-paragraph">But for now, I&#8217;m just going to assume you kind of know your way around to makefile,</p>



<p class="wp-block-paragraph">or at least you can kind of infer what&#8217;s going on.</p>



<p class="wp-block-paragraph">So I&#8217;ve commented out a different version of this variable for the flags of my C++ compiler.</p>



<p class="wp-block-paragraph">If I uncomment this and then comment out the other one,</p>



<p class="wp-block-paragraph">you&#8217;ll notice the only real difference is this part right here, dash G dwarf 2.</p>



<p class="wp-block-paragraph">Usually in C++ compilation, it&#8217;ll just be dash G to generate debug symbols,</p>



<p class="wp-block-paragraph">and then the symbol format will default to a format called dwarf2 so I don&#8217;t know for these</p>



<p class="wp-block-paragraph">tutorials I like to just specify the full format but you can just say dash g in your c++ compiler</p>



<p class="wp-block-paragraph">and linker we do the same thing for the linker down here so I&#8217;m going to uncomment this</p>



<p class="wp-block-paragraph">and comment that and the only difference is just g dwarf2 so I&#8217;m just telling my linker that I also</p>



<p class="wp-block-paragraph">want debug symbols in there too then we do the same thing for the assembler so I&#8217;m going to</p>



<p class="wp-block-paragraph">So I&#8217;m going to uncomment that and comment this other one.</p>



<p class="wp-block-paragraph">And the only difference is that in the assembler,</p>



<p class="wp-block-paragraph">we kind of have to specify it a little bit differently.</p>



<p class="wp-block-paragraph">Instead of G dwarf dash two,</p>



<p class="wp-block-paragraph">it&#8217;s just G dwarf two as just one string.</p>



<p class="wp-block-paragraph">And I think with Yasm, you have to specify the debug symbol format.</p>



<p class="wp-block-paragraph">So just keep that in mind.</p>



<p class="wp-block-paragraph">So now I&#8217;ve turned on all the debug symbols.</p>



<p class="wp-block-paragraph">I&#8217;m going to do real fast, make clean and make build one more time.</p>



<p class="wp-block-paragraph">And then if I list everything,</p>



<p class="wp-block-paragraph">list everything let me just double check that it&#8217;s building the symbols now right okay good</p>



<p class="wp-block-paragraph">notice that the files are bigger so this asio um file is now 1.8 kilobytes instead of you know</p>



<p class="wp-block-paragraph">half a kilobyte the main.o object file is now 36 kilobytes instead of only 4.5 and the executable</p>



<p class="wp-block-paragraph">main is 36 kilobytes instead of just 17. so you can feel it already right like there&#8217;s a bunch</p>



<p class="wp-block-paragraph">extra information sitting inside the object files and the binary which allows us to debug our program</p>



<p class="wp-block-paragraph">okay so now that we&#8217;ve generated debug symbols in our program we can actually think about starting</p>



<p class="wp-block-paragraph">to use gdb so gdb is a program just gdb by the command line if you don&#8217;t have it in your system</p>



<p class="wp-block-paragraph">already you can go sudo apt install gdb or depending on what system you&#8217;re on maybe a or</p>



<p class="wp-block-paragraph">or whatever your package management program is,</p>



<p class="wp-block-paragraph">but we just need to install GDB.</p>



<p class="wp-block-paragraph">On Ubuntu and Debian, I think,</p>



<p class="wp-block-paragraph">there is a meta package called build essential,</p>



<p class="wp-block-paragraph">which will give you a bunch of compilation tools</p>



<p class="wp-block-paragraph">and make file stuff and whatever.</p>



<p class="wp-block-paragraph">So I&#8217;m just going to assume at this point</p>



<p class="wp-block-paragraph">you already have GDB on your system.</p>



<p class="wp-block-paragraph">To launch GDB and go into its console,</p>



<p class="wp-block-paragraph">just type GDB by itself.</p>



<p class="wp-block-paragraph">So now we&#8217;re inside of the GDB console or terminal.</p>



<p class="wp-block-paragraph">console or terminal. We&#8217;re no longer inside of a bash terminal. We can&#8217;t type normal commands.</p>



<p class="wp-block-paragraph">Let me show you real fast. Here&#8217;s the first command you want to learn on GDB. It&#8217;s called</p>



<p class="wp-block-paragraph">quit. Never quit. Never surrender. So, you know, normally if you&#8217;re at the command line,</p>



<p class="wp-block-paragraph">you can do something like cat, etc. OS release just to see what operating system you&#8217;re using.</p>



<p class="wp-block-paragraph">So you can see for this virtual machine that I&#8217;m using right now, I&#8217;ve got like Ubuntu 22 on there.</p>



<p class="wp-block-paragraph">If we go inside of GDB, we can&#8217;t use regular bash commands anymore or regular operating</p>



<p class="wp-block-paragraph">system commands.</p>



<p class="wp-block-paragraph">We can&#8217;t say cat, et cetera, OS release because we can now only use GDB commands.</p>



<p class="wp-block-paragraph">We&#8217;re inside of GDB already.</p>



<p class="wp-block-paragraph">Notice how it says undefined catch command.</p>



<p class="wp-block-paragraph">I don&#8217;t know what you&#8217;re talking about, dude.</p>



<p class="wp-block-paragraph">Okay.</p>



<p class="wp-block-paragraph">So remember we do a Q or quit to get out.</p>



<p class="wp-block-paragraph">You can actually just type Q.</p>



<p class="wp-block-paragraph">A lot of commands that I&#8217;m going to be talking about.</p>



<p class="wp-block-paragraph">going to be talking about you can abbreviate them with one or two or three letters and it totally</p>



<p class="wp-block-paragraph">works just for speed so now that we know how to enter gdb and now that we also know that we have</p>



<p class="wp-block-paragraph">already compiled our program with debug symbols we can attach gdb to our program i mean what&#8217;s the</p>



<p class="wp-block-paragraph">whole point of a debugger we most of the time we will just have our debugger attached to our program</p>



<p class="wp-block-paragraph">and launch it or i guess launch it while it&#8217;s attached you can also attach to a running program</p>



<p class="wp-block-paragraph">but we&#8217;re going to launch a program and stay attached to it.</p>



<p class="wp-block-paragraph">And then we can sort of catch crashes and see what&#8217;s going on.</p>



<p class="wp-block-paragraph">Or we could also tell the debugger to stop the program at certain points in time,</p>



<p class="wp-block-paragraph">like when we hit a certain line number or when a variable changes or something like that.</p>



<p class="wp-block-paragraph">And then we can look around.</p>



<p class="wp-block-paragraph">We can look at all the variables, look at the state of the program,</p>



<p class="wp-block-paragraph">even change things if we wanted to.</p>



<p class="wp-block-paragraph">We can look at all the registers if you&#8217;re coding in assembly.</p>



<p class="wp-block-paragraph">So it&#8217;s pretty useful.</p>



<p class="wp-block-paragraph">Anyway, I&#8217;m going to go gdb and the next command we&#8217;re going to learn is file, which just tells</p>



<p class="wp-block-paragraph">gdb to attach to a program so that we can run it.</p>



<p class="wp-block-paragraph">So, you know, the name of our program here that we compiled is just called main.</p>



<p class="wp-block-paragraph">And so if I type file main, then gdb should load up that file and try to parse its debug</p>



<p class="wp-block-paragraph">symbols so it can begin debugging for you.</p>



<p class="wp-block-paragraph">Notice how it says reading symbols from main.</p>



<p class="wp-block-paragraph">you know if we did a typo or we had something with no debug symbols it will give us some sort</p>



<p class="wp-block-paragraph">of an error message so for now i&#8217;m just going to quit and then go back in just to make sure that</p>



<p class="wp-block-paragraph">it&#8217;s all clean and we&#8217;re going to do file main now it attaches once it&#8217;s attached that&#8217;s not i guess</p>



<p class="wp-block-paragraph">it&#8217;s not technically attached because the program&#8217;s not running but once it has loaded the debug</p>



<p class="wp-block-paragraph">symbols for that program we can run the program with the run command in gdb also kind of interesting</p>



<p class="wp-block-paragraph">If you have debug symbols that are generated separately from your executable,</p>



<p class="wp-block-paragraph">you could tell GDB to load the symbols file and then run your program.</p>



<p class="wp-block-paragraph">That happens a lot in Linux where people release the non-debug friendly version of a program</p>



<p class="wp-block-paragraph">and then they&#8217;ll release the debug symbols separately if you&#8217;re interested.</p>



<p class="wp-block-paragraph">Anyway, so I&#8217;m going to type run and it&#8217;ll run the program.</p>



<p class="wp-block-paragraph">Notice how GDB kind of tells us that it&#8217;s starting up now.</p>



<p class="wp-block-paragraph">here&#8217;s like a thread debugger that&#8217;s being enabled and it says we&#8217;re using this library to do it and</p>



<p class="wp-block-paragraph">then it says hello you know here&#8217;s the hello message so the program is actually running that&#8217;s</p>



<p class="wp-block-paragraph">not my name i wish it was and then it prints the final result and then gdb says your program exited</p>



<p class="wp-block-paragraph">normally on newer versions of gdb there&#8217;s a setting i think by default where it&#8217;ll ask you</p>



<p class="wp-block-paragraph">if you want to start a debug daemon a debug info daemon or something yes or no you can usually just</p>



<p class="wp-block-paragraph">without actually having to type n.</p>



<p class="wp-block-paragraph">It&#8217;s a little bit faster.</p>



<p class="wp-block-paragraph">So the whole program ran and finished.</p>



<p class="wp-block-paragraph">If I type run again, it does the same thing.</p>



<p class="wp-block-paragraph">In GDB, if you hit enter without typing anything,</p>



<p class="wp-block-paragraph">it ends up repeating the previous command.</p>



<p class="wp-block-paragraph">So I&#8217;m not going to type run.</p>



<p class="wp-block-paragraph">If I just hit enter, are you serious?</p>



<p class="wp-block-paragraph">GDB made me into a liar.</p>



<p class="wp-block-paragraph">Well, most of the commands will repeat if you hit enter.</p>



<p class="wp-block-paragraph">I guess not the run command.</p>



<p class="wp-block-paragraph">So I&#8217;ll type run again.</p>



<p class="wp-block-paragraph">And okay, I mean, the program is pretty good, right?</p>



<p class="wp-block-paragraph">So if you watched my…</p>



<p class="wp-block-paragraph">It&#8217;s not pretty good.</p>



<p class="wp-block-paragraph">watched my it&#8217;s not pretty good it&#8217;s a nonsense program I mean it it doesn&#8217;t crash is what I&#8217;m</p>



<p class="wp-block-paragraph">saying if you watched my previous video which you should on caveman debugging I had this uh you know</p>



<p class="wp-block-paragraph">nonsense portion of code that just sort of updated a variable and we pretended that we were confused</p>



<p class="wp-block-paragraph">and we didn&#8217;t understand what was happening and uh we used gdb to debug it so what I&#8217;m going to do</p>



<p class="wp-block-paragraph">either let&#8217;s say we&#8217;ll start off with throwing an exception and then we&#8217;ll try to use a no pointer</p>



<p class="wp-block-paragraph">and then we&#8217;ll see a crash and then after that I&#8217;m going to start using breakpoints where we can stop</p>



<p class="wp-block-paragraph">automatically inside of our program to print the state of the program print the variables</p>



<p class="wp-block-paragraph">and then I&#8217;m going to do that first in C++ and then hopefully I&#8217;ll remember to do that in</p>



<p class="wp-block-paragraph">assembly right after it&#8217;s basically the same thing only with assembly you don&#8217;t really have</p>



<p class="wp-block-paragraph">you know a bunch of variables everywhere you just kind of have registers and maybe some globals</p>



<p class="wp-block-paragraph">globals but it&#8217;s going to be the same deal okay so let&#8217;s pretend that uh we are gonna have a crash</p>



<p class="wp-block-paragraph">okay so this program just kind of runs later on we&#8217;ll figure out why the result is wrong we&#8217;ll</p>



<p class="wp-block-paragraph">just pretend that it&#8217;s wrong but for now we&#8217;ll just say how about we uncomment this line right</p>



<p class="wp-block-paragraph">here and what will happen is when we&#8217;re running the main loop at some point in the loop we get</p>



<p class="wp-block-paragraph">When F gets called, the input will be looked at here.</p>



<p class="wp-block-paragraph">And if the input is more than one,</p>



<p class="wp-block-paragraph">which is definitely gonna happen like right away,</p>



<p class="wp-block-paragraph">then we&#8217;ll call on the G function.</p>



<p class="wp-block-paragraph">Then the G function will just throw an exception.</p>



<p class="wp-block-paragraph">We&#8217;ll just pretend that something really bad happened.</p>



<p class="wp-block-paragraph">You crashed or you actually did throw</p>



<p class="wp-block-paragraph">and you didn&#8217;t catch your throw,</p>



<p class="wp-block-paragraph">or maybe the system threw something at you</p>



<p class="wp-block-paragraph">or another library threw something at you</p>



<p class="wp-block-paragraph">and you didn&#8217;t catch it.</p>



<p class="wp-block-paragraph">So just basically a program crash.</p>



<p class="wp-block-paragraph">Let me run this real fast.</p>



<p class="wp-block-paragraph">So make run just to show you that the program crashes.</p>



<p class="wp-block-paragraph">Make run at this point is the same thing as just kind of executing the program.</p>



<p class="wp-block-paragraph">But you can see that it says terminate called after throwing an instance of runtime error.</p>



<p class="wp-block-paragraph">And then there&#8217;s the message, oh my gosh, and then aborted core dumped.</p>



<p class="wp-block-paragraph">That&#8217;s not good.</p>



<p class="wp-block-paragraph">So now let&#8217;s try to understand why.</p>



<p class="wp-block-paragraph">Why did that crash?</p>



<p class="wp-block-paragraph">Pretend you don&#8217;t know that it happened in G. You know, you&#8217;re trying to investigate.</p>



<p class="wp-block-paragraph">So we could type GDB and then we could type a file to load the debug symbols, but it&#8217;s a little bit faster just to type GDB main and just sort of name the file that you want to load right away.</p>



<p class="wp-block-paragraph">So if I do that, notice how it automatically tries to load the debug symbols from main.</p>



<p class="wp-block-paragraph">Then I can just type run and then the program tries to run and notice how it actually catches the crash.</p>



<p class="wp-block-paragraph">If you look down here, terminate called after throwing an instance of runtime error.</p>



<p class="wp-block-paragraph">throwing an instance of runtime error so that&#8217;s what the program thinks is going on but then the</p>



<p class="wp-block-paragraph">program is trying to terminate but then down here gdb is like wait wait wait i just caught you know</p>



<p class="wp-block-paragraph">a termination uh getting invoked here and you can see this is sort of system code like we did not</p>



<p class="wp-block-paragraph">create this source file p thread kill we didn&#8217;t write that that&#8217;s the standard library and there&#8217;s</p>



<p class="wp-block-paragraph">a function here called p thread kill implementation with those you know fun c underscore prefixes</p>



<p class="wp-block-paragraph">prefixes everywhere and so this is like some sort of a c source file and we don&#8217;t have that file on</p>



<p class="wp-block-paragraph">the system so we can&#8217;t really debug that file lucky for you most of these standard libraries</p>



<p class="wp-block-paragraph">work all the time so you don&#8217;t really need to debug them you need to debug your code instead</p>



<p class="wp-block-paragraph">of the standard library code so the question really is how did i get here how did i get to</p>



<p class="wp-block-paragraph">this crash so the next command we&#8217;ll learn in gdb is just the where command the where command will</p>



<p class="wp-block-paragraph">I&#8217;m going to release a video in the future that kind of explains call stacks, but basically</p>



<p class="wp-block-paragraph">imagine a abstract data type, a data structure called a stack. And every time you want to add</p>



<p class="wp-block-paragraph">data to the stack, it just sort of stacks up on top of the last piece of data. So like if I add</p>



<p class="wp-block-paragraph">something, let&#8217;s say I want to add the number five, then I would just add the number five on top of</p>



<p class="wp-block-paragraph">the stack, right? If I wanted to add the number 11, then I would just add the number 11 onto the</p>



<p class="wp-block-paragraph">off of the stack and the stack is a really good data structure to uh sort of give you a trail of</p>



<p class="wp-block-paragraph">breadcrumbs to know where you got where did you come from so we call this the call stack because</p>



<p class="wp-block-paragraph">what&#8217;s happening is every single time you call a function then the new scope and then you know the</p>



<p class="wp-block-paragraph">new function that you&#8217;re invoking um it has something called a call frame full of information</p>



<p class="wp-block-paragraph">current address is and all that stuff.</p>



<p class="wp-block-paragraph">And we&#8217;ll just call that chunk of data that belongs to one instance of a function call.</p>



<p class="wp-block-paragraph">We&#8217;ll just call that a call frame.</p>



<p class="wp-block-paragraph">And so if you&#8217;re stacking call frames one on top of the other,</p>



<p class="wp-block-paragraph">then we call that whole entire thing the call stack.</p>



<p class="wp-block-paragraph">And this is what allows us to use recursion and all these complicated call graphs and everything.</p>



<p class="wp-block-paragraph">It&#8217;s really simple, but it&#8217;s really powerful.</p>



<p class="wp-block-paragraph">So you&#8217;re looking at the call stack.</p>



<p class="wp-block-paragraph">Each one of these lines is the call frame or is a call frame.</p>



<p class="wp-block-paragraph">And so for example, if you just look at number zero here, you&#8217;re looking at call frame zero,</p>



<p class="wp-block-paragraph">which is the closest frame to wherever the crash occurred, which is if you look at it,</p>



<p class="wp-block-paragraph">that&#8217;s exactly what we were looking at when we saw that original message, right? Call frame zero.</p>



<p class="wp-block-paragraph">So that&#8217;s not really useful. You want to go down to higher numbered call frames or I guess</p>



<p class="wp-block-paragraph">physically lowered number call frames and just kind of scan it until you find some code that</p>



<p class="wp-block-paragraph">raise no abort no none of this stuff is ours live standard c plus plus no the first frame that</p>



<p class="wp-block-paragraph">contains code that is ours is call frame number nine or i guess like nine away from our current</p>



<p class="wp-block-paragraph">position so i guess find the physically highest call frame that is your code or the lowest numbered</p>



<p class="wp-block-paragraph">call frame that is your code and it&#8217;s telling us right here all right we uh we actually seem to</p>



<p class="wp-block-paragraph">So if I go to 65, right there, it tells you exactly where the crash happened.</p>



<p class="wp-block-paragraph">If you&#8217;re still trying to figure out, well, how did I get to that crash?</p>



<p class="wp-block-paragraph">I don&#8217;t know how that actually happened.</p>



<p class="wp-block-paragraph">Just keep looking down further in the call frames.</p>



<p class="wp-block-paragraph">The next one, call frame 10, says, well, we were inside of function f with input equals</p>



<p class="wp-block-paragraph">three at main.cpb line 46.</p>



<p class="wp-block-paragraph">So if I just go to 46, I can get another clue.</p>



<p class="wp-block-paragraph">I can go, all right, so for some reason we called g.</p>



<p class="wp-block-paragraph">Imagine again, this program is much more complicated.</p>



<p class="wp-block-paragraph">And then if we&#8217;re still confused, we just look down at another one and it&#8217;s like, well, this all this whole mess started at CPP main dot CPP line 28.</p>



<p class="wp-block-paragraph">So if we look at line 28, it&#8217;s like, well, we were inside of this loop and we we called the F function and then the F function called the G function.</p>



<p class="wp-block-paragraph">And then G just kind of did a throw. So that gives us a lot of information.</p>



<p class="wp-block-paragraph">All right. Next thing we can do is we can sort of move up and down the call stack, because right now we&#8217;re sitting at call frame zero, which is just the system code.</p>



<p class="wp-block-paragraph">which is just the system code, but maybe we don&#8217;t understand why the throw actually happened at,</p>



<p class="wp-block-paragraph">let&#8217;s say, line 65, right? So we can use up to go up in the call stack to a different call frame.</p>



<p class="wp-block-paragraph">So if I hit up, notice how it mentions that we&#8217;re now in frame number one instead of zero.</p>



<p class="wp-block-paragraph">And if I do up again, we&#8217;re now in frame two, which is a little bit closer. We can use the</p>



<p class="wp-block-paragraph">trick finally where we hit the enter key instead of repeating the command. So I&#8217;m just going to</p>



<p class="wp-block-paragraph">and hit enter a couple times until we eventually get to our code,</p>



<p class="wp-block-paragraph">which I think was supposed to be at frame nine.</p>



<p class="wp-block-paragraph">So I&#8217;m just going to hit it a bunch of times until we get to frame nine.</p>



<p class="wp-block-paragraph">Now we&#8217;re looking sort of back in time at that point</p>



<p class="wp-block-paragraph">because all these other frames actually did get invoked,</p>



<p class="wp-block-paragraph">but we don&#8217;t really care about what was going on inside of them</p>



<p class="wp-block-paragraph">because they probably did their job correctly.</p>



<p class="wp-block-paragraph">We&#8217;re looking at our code, so we&#8217;re going back in time</p>



<p class="wp-block-paragraph">right when we were doing this throw.</p>



<p class="wp-block-paragraph">Oh no, we threw. Why did we throw?</p>



<p class="wp-block-paragraph">well there&#8217;s another command in gdb called info locals</p>



<p class="wp-block-paragraph">okay the info locals uh there&#8217;s there&#8217;s really not much to this maybe maybe let me do this again</p>



<p class="wp-block-paragraph">a equals five and uh then i&#8217;ll just say a plus plus just so that we have a local variable</p>



<p class="wp-block-paragraph">i forgot that gdb doesn&#8217;t consider the incoming arguments as locals so there was nothing there</p>



<p class="wp-block-paragraph">real fast uh gdp oh let&#8217;s do make build and then gdp main and then run and then we have a throw</p>



<p class="wp-block-paragraph">and then i say where and then i go up up up up to a frame you can also use the keyword down if you</p>



<p class="wp-block-paragraph">go too far if you want to go you know back down in the call stack but i&#8217;m just going to use up</p>



<p class="wp-block-paragraph">okay so now we have it here now finally i think i can do info locals just to see the local variables</p>



<p class="wp-block-paragraph">notice how the a variable is there now because I just added a local variable a a moment ago</p>



<p class="wp-block-paragraph">you can also print things directly you can say print the input incoming variable if you&#8217;re in</p>



<p class="wp-block-paragraph">assembly you can also print registers you can say info registers like that and you&#8217;ll get a nice</p>



<p class="wp-block-paragraph">print out of all the registers you could print individual registers you can say let&#8217;s print</p>



<p class="wp-block-paragraph">register r12 you can print register r12 as binary by saying I think it&#8217;s p slash t</p>



<p class="wp-block-paragraph">or print slash t yeah we can probably just do print slash t also</p>



<p class="wp-block-paragraph">t stands for two because they&#8217;re it&#8217;s a base two number system</p>



<p class="wp-block-paragraph">does that mean there&#8217;s a p slash h or a p slash f for 15 or 16 s for 16 i don&#8217;t know i haven&#8217;t</p>



<p class="wp-block-paragraph">tried that is it going to work hang on p slash s bar 12 it&#8217;s not going to work nope didn&#8217;t work</p>



<p class="wp-block-paragraph">i think it thinks it&#8217;s a character point or something but anyway so we can print a lot of</p>



<p class="wp-block-paragraph">So we can print a lot of stuff.</p>



<p class="wp-block-paragraph">And like I said before, we can go up and down the call stack.</p>



<p class="wp-block-paragraph">So I can go down a couple times and then go back up again.</p>



<p class="wp-block-paragraph">And now we can assume that we&#8217;ve kind of debugged, you know, where the source of the error was.</p>



<p class="wp-block-paragraph">So I&#8217;m going to comment that call out to G and run the program again one more time with a different error.</p>



<p class="wp-block-paragraph">So I&#8217;ve commented this code out from before, but just imagine now that we&#8217;re going to use some pointers.</p>



<p class="wp-block-paragraph">We&#8217;re going to start off with a pointer called P.</p>



<p class="wp-block-paragraph">a pointer called p we&#8217;re going to set it to null at the beginning and then we&#8217;re going to just</p>



<p class="wp-block-paragraph">kind of allocate it and then we&#8217;re going to set you know a value and then we&#8217;re going to dereference</p>



<p class="wp-block-paragraph">it and dereferencing a pointer just kind of sets the first value in an array you can do that and</p>



<p class="wp-block-paragraph">then we&#8217;re going to deallocate the pointer and then how about right after that we do something</p>



<p class="wp-block-paragraph">really really naughty we say p at some index is equal to a five this should be this should be a</p>



<p class="wp-block-paragraph">a program crash because we deallocated p already at that point we would be dealing with junk data</p>



<p class="wp-block-paragraph">if we tried to dereference p but to make it even more clear that it&#8217;s naughty we&#8217;ll set it to a no</p>



<p class="wp-block-paragraph">pointer and then we&#8217;ll try to actually use it so let me let me just restart the program real fast</p>



<p class="wp-block-paragraph">i&#8217;m gonna do quit yes and then we&#8217;ll do make a debug just so you know my make debug is just a</p>



<p class="wp-block-paragraph">Let&#8217;s see, where&#8217;s that? Where&#8217;s that? Where&#8217;s that? Do I not have a debug shortcut yet?</p>



<p class="wp-block-paragraph">Oh dear. I need to add that before this video is over. You know what? I&#8217;ll add it right now.</p>



<p class="wp-block-paragraph">We&#8217;ll do this run and we&#8217;ll say debug, make debug, and it needs to build first. And then I&#8217;ll say</p>



<p class="wp-block-paragraph">debugging the program. And then we will do the GDB binary and give it an argument of</p>



<p class="wp-block-paragraph">the executable and then later we can we can upgrade this to take a little script of commands</p>



<p class="wp-block-paragraph">because the commands are going to start getting out of control so now if i do make debug if i</p>



<p class="wp-block-paragraph">didn&#8217;t screw it up now it at least goes in there and i&#8217;ll do run and then we have a seg fault and</p>



<p class="wp-block-paragraph">notice how it tells us right away hey you have a seg fault right here you&#8217;re accessing invalid</p>



<p class="wp-block-paragraph">memory let&#8217;s do info locals just to see what&#8217;s up oh the p is actually a zero that&#8217;s a null pointer</p>



<p class="wp-block-paragraph">explicitly oh whoops you know oh what have I done but it&#8217;s a really good idea to set your</p>



<p class="wp-block-paragraph">pointers to null after you deallocate because if you don&#8217;t you might end up with undefined</p>



<p class="wp-block-paragraph">behavior where sometimes the program doesn&#8217;t crash while you&#8217;re overriding junk data or reading junk</p>



<p class="wp-block-paragraph">data but sometimes it does and you&#8217;re not really sure like why does it work sometimes and why does</p>



<p class="wp-block-paragraph">it not work sometimes so let me comment this out and see if it&#8217;ll crash uh let&#8217;s just let&#8217;s just</p>



<p class="wp-block-paragraph">make debug and then i&#8217;ll do run and then if we do info locals oh yeah okay so this is uh</p>



<p class="wp-block-paragraph">it did seg fault so that&#8217;s good uh if we were unlucky then the program wouldn&#8217;t have crashed</p>



<p class="wp-block-paragraph">it would have just started behaving strangely and um when we print out the value of p notice how it</p>



<p class="wp-block-paragraph">looks like a pointer still like if we were to debug the program and print the pointer value</p>



<p class="wp-block-paragraph">then it doesn&#8217;t really it looks the same it looks the same after we deallocated it right so that</p>



<p class="wp-block-paragraph">this is called a dangling pointer meaning you deallocated the pointer but you forgot to set</p>



<p class="wp-block-paragraph">it to null afterwards so then later when you&#8217;re trying to debug the program it&#8217;s a little confusing</p>



<p class="wp-block-paragraph">because you&#8217;re like that looks like a regular pointer what did i do wrong so um it&#8217;s really</p>



<p class="wp-block-paragraph">good practice to set your pointers to null right after you deallocate them that way when you&#8217;re</p>



<p class="wp-block-paragraph">debugging later because something crashed instead of seeing a memory location on p you&#8217;ll see</p>



<p class="wp-block-paragraph">On P, you&#8217;ll see something that looks definitely like a null pointer and print P.</p>



<p class="wp-block-paragraph">Then it&#8217;s way easier to realize, oh, whoops, I was trying to use a dead pointer, a pointer that I deallocated.</p>



<p class="wp-block-paragraph">Hopefully that makes sense so far.</p>



<p class="wp-block-paragraph">Next thing I want to do is a small upgrade.</p>



<p class="wp-block-paragraph">I can&#8217;t remember if it&#8217;s dash X or dash EX.</p>



<p class="wp-block-paragraph">I think it&#8217;s dash X.</p>



<p class="wp-block-paragraph">But you can write a little script.</p>



<p class="wp-block-paragraph">I have a little script here called Good Doggo.</p>



<p class="wp-block-paragraph">called good doggo you can just make any file name it anything you want i&#8217;m so i&#8217;m just naming it</p>



<p class="wp-block-paragraph">good doggo.txt because my dog&#8217;s taking a little nap behind me as usual um but you could name this</p>



<p class="wp-block-paragraph">gdb.txt or whatever it is you want so you can put inside of this script file any command that you</p>



<p class="wp-block-paragraph">want gdb to execute when it launches so let&#8217;s just comment this stuff out real fast and i&#8217;ll do</p>



<p class="wp-block-paragraph">I&#8217;ll just do run maybe for starters.</p>



<p class="wp-block-paragraph">Because whenever I&#8217;ve been launching for debug,</p>



<p class="wp-block-paragraph">I launch with GDB and then I type run manually, right?</p>



<p class="wp-block-paragraph">So I want to save a little time.</p>



<p class="wp-block-paragraph">So good doggo is the name of the script.</p>



<p class="wp-block-paragraph">So when I invoke GDB, I am going to,</p>



<p class="wp-block-paragraph">where is it right there?</p>



<p class="wp-block-paragraph">Okay, it&#8217;s GDB.</p>



<p class="wp-block-paragraph">And then I&#8217;m going to go dot, I think it&#8217;s X.</p>



<p class="wp-block-paragraph">And then I&#8217;m going to say good doggo.txt.</p>



<p class="wp-block-paragraph">If it&#8217;s not dash x, then it&#8217;s dash ex.</p>



<p class="wp-block-paragraph">One of those executes the command that comes after,</p>



<p class="wp-block-paragraph">and one of those executes the command script that comes after.</p>



<p class="wp-block-paragraph">Let me just double check that this actually is going to work.</p>



<p class="wp-block-paragraph">So make debug.</p>



<p class="wp-block-paragraph">All right, okay, so it was dash x.</p>



<p class="wp-block-paragraph">Dash x specifies a script that will be run.</p>



<p class="wp-block-paragraph">Dash ex just specifies a command that you can put right in there.</p>



<p class="wp-block-paragraph">Like I could have said dash ex run, and it would have ran right away.</p>



<p class="wp-block-paragraph">But the script is more convenient, right?</p>



<p class="wp-block-paragraph">Because we can save typing.</p>



<p class="wp-block-paragraph">So notice how it ran right away and then there&#8217;s a seg fault.</p>



<p class="wp-block-paragraph">Okay, so now let&#8217;s go a little bit deeper.</p>



<p class="wp-block-paragraph">Let&#8217;s change this to false again now that we understand like null pointers and all that</p>



<p class="wp-block-paragraph">stuff.</p>



<p class="wp-block-paragraph">So we can just basically deal a little bit more with only what the main loop is doing.</p>



<p class="wp-block-paragraph">Suppose we just still don&#8217;t really understand what the loop is doing, why our answer is</p>



<p class="wp-block-paragraph">you know, good or bad.</p>



<p class="wp-block-paragraph">So maybe I want to do a breakpoint at the top of that for loop.</p>



<p class="wp-block-paragraph">loop gets to the top of its iteration, I want the program to pause so I can look around.</p>



<p class="wp-block-paragraph">So I&#8217;m going to set up a breakpoint on line 22. And here&#8217;s how you set up breakpoints</p>



<p class="wp-block-paragraph">in GDB. I&#8217;m going to go ahead and maybe do the terminal for now. And then I&#8217;ll stick that into</p>



<p class="wp-block-paragraph">the script next. I will not remember that it&#8217;s line 22. That&#8217;s okay. I&#8217;m going to do quit.</p>



<p class="wp-block-paragraph">And I&#8217;m going to do make build. Then I&#8217;m going to go GDB main just to jump in there.</p>



<p class="wp-block-paragraph">and then i&#8217;m going to say break or just the letter b just to make a break point you type the name of</p>



<p class="wp-block-paragraph">the source code file that you want to break in and then a colon and then the line number where</p>



<p class="wp-block-paragraph">you want it to break so i think i think it actually was line 22 oh i can see it and then</p>



<p class="wp-block-paragraph">maybe i want to have it break at line i don&#8217;t know maybe right here line 25 right before it increases</p>



<p class="wp-block-paragraph">And then so I&#8217;m going to do an up arrow to just repeat the command so I can edit it real fast.</p>



<p class="wp-block-paragraph">And then I&#8217;ll break again at line 28 and then maybe 30 and then 32.</p>



<p class="wp-block-paragraph">So I&#8217;m going to go 28, 30 and 32.</p>



<p class="wp-block-paragraph">30 and I&#8217;m just making up break points.</p>



<p class="wp-block-paragraph">I just want to inspect the program and then maybe I&#8217;ll break at line 36 so I can see what the final answer is.</p>



<p class="wp-block-paragraph">So I just have a bunch of break points set up.</p>



<p class="wp-block-paragraph">Remember when the program is running now under GDB, every time it hits one of those lines,</p>



<p class="wp-block-paragraph">every time it hits one of those lines the program will pause and i&#8217;ll have a chance to look around</p>



<p class="wp-block-paragraph">at the variables and things so i&#8217;m not sure if i&#8217;ve set up the breakpoints correctly i can say</p>



<p class="wp-block-paragraph">info breakpoints and it&#8217;ll show me all of my breakpoints notice how you can enable them and</p>



<p class="wp-block-paragraph">disable them so let&#8217;s say for the sake of argument i want to disable the breakpoint at line 30 because</p>



<p class="wp-block-paragraph">maybe i don&#8217;t really need that right now but i kind of want to keep it in the system for later</p>



<p class="wp-block-paragraph">disable four because it&#8217;s number four.</p>



<p class="wp-block-paragraph">It&#8217;s breakpoint number four.</p>



<p class="wp-block-paragraph">So I can say disable four.</p>



<p class="wp-block-paragraph">Then if I do info breakpoints again,</p>



<p class="wp-block-paragraph">notice how there&#8217;s a little N on there,</p>



<p class="wp-block-paragraph">meaning breakpoint four is disabled.</p>



<p class="wp-block-paragraph">So I could re-enable it with a four</p>



<p class="wp-block-paragraph">and then go info breakpoints again.</p>



<p class="wp-block-paragraph">So now they&#8217;re all enabled.</p>



<p class="wp-block-paragraph">I am going to maybe copy this into the script file real fast</p>



<p class="wp-block-paragraph">so I don&#8217;t have to keep typing this over and over and over again.</p>



<p class="wp-block-paragraph">Imagine if every single time you changed your program and recompiled it you had to type all of the breakpoints from scratch</p>



<p class="wp-block-paragraph">That&#8217;s why I want you to use this little breakpoint script</p>



<p class="wp-block-paragraph">So we&#8217;re going to do B to break at main.cpp line 22 and 25 and</p>



<p class="wp-block-paragraph">28 and</p>



<p class="wp-block-paragraph">30 and 32. I should have copy pasted the three dang it 36</p>



<p class="wp-block-paragraph">And after we&#8217;re done setting up all the breakpoints</p>



<p class="wp-block-paragraph">show me the breakpoints just so I can have a visual reminder. Keep in mind if you change too</p>



<p class="wp-block-paragraph">much of your source code then you&#8217;re probably going to have to update these lines and that&#8217;s</p>



<p class="wp-block-paragraph">okay but it&#8217;s a good idea to just at least you know kind of look at them a little bit so that&#8217;s</p>



<p class="wp-block-paragraph">why I&#8217;m saying info breakpoints. And so then remember again the makefile all it&#8217;s doing when</p>



<p class="wp-block-paragraph">I type make debug is it&#8217;s just launching this command right here it&#8217;s just saying gdb with the</p>



<p class="wp-block-paragraph">with my gdb script so let me quit to get out of this and then i&#8217;m going to say clear and make</p>



<p class="wp-block-paragraph">debug and notice how it did all that stuff for me it added the breakpoints it showed me the break</p>



<p class="wp-block-paragraph">points and then it ran the program like i told it to and then it already broke on a breakpoint it</p>



<p class="wp-block-paragraph">already break it break breakened it&#8217;s broken i don&#8217;t know i don&#8217;t think it&#8217;s broken uh so we&#8217;re</p>



<p class="wp-block-paragraph">So we&#8217;re on breakpoint number one on line 22.</p>



<p class="wp-block-paragraph">And here&#8217;s the for loop.</p>



<p class="wp-block-paragraph">I can type all of my inspection stuff.</p>



<p class="wp-block-paragraph">I can say info locals.</p>



<p class="wp-block-paragraph">I can say print I, I can say print A, I can print whatever I want.</p>



<p class="wp-block-paragraph">And then when I&#8217;m done kind of looking around and inspecting things,</p>



<p class="wp-block-paragraph">I want to continue the program.</p>



<p class="wp-block-paragraph">I don&#8217;t want to just stop it here.</p>



<p class="wp-block-paragraph">So I&#8217;m just going to use the command continue.</p>



<p class="wp-block-paragraph">And it goes right to the next breakpoint.</p>



<p class="wp-block-paragraph">If I want to, you know, continue again, I can hit C.</p>



<p class="wp-block-paragraph">continue again i can hit c or i can just hit enter you know to just sort of keep continuing as i</p>



<p class="wp-block-paragraph">inspect and print things let&#8217;s see when can i get to break point number one is that what&#8217;s going on</p>



<p class="wp-block-paragraph">here hang on a second what is break point number one oh i know what to do info break points</p>



<p class="wp-block-paragraph">break point number one is that main.cpp line 22 is that actually going to get hit oh it&#8217;s</p>



<p class="wp-block-paragraph">having issues here with my GUI. Okay. Line 22. Okay. So it&#8217;s not really hitting that anymore.</p>



<p class="wp-block-paragraph">I guess it&#8217;s not going to think that it hits the top of the forelip. Okay. So I guess breakpoint</p>



<p class="wp-block-paragraph">two is where we&#8217;re always going to be hitting. So let&#8217;s just suppose that I&#8217;m continuing until</p>



<p class="wp-block-paragraph">I hit breakpoint number two. Suppose that the next 10 times I see breakpoint two, I don&#8217;t</p>



<p class="wp-block-paragraph">actually want to stop and break on it. I just want to skip the next 10 iterations for some reason.</p>



<p class="wp-block-paragraph">some reason you can say continue 10 and it&#8217;ll skip the next 10 times that it hits the number two</p>



<p class="wp-block-paragraph">notice how if i hit continue a bunch of times we&#8217;re not really seeing the break point number</p>



<p class="wp-block-paragraph">two anymore we&#8217;re seeing other break points and then eventually if i hit it enough times</p>



<p class="wp-block-paragraph">we&#8217;ll probably see two again oh my god oh my god are we seeing it yet no</p>



<p class="wp-block-paragraph">Okay, wait, are we seeing I&#8217;m freaking myself out.</p>



<p class="wp-block-paragraph">Okay, so let&#8217;s continue 10 times on breakpoint three, then we&#8217;ll only see</p>



<p class="wp-block-paragraph">breakpoint four, I guess.</p>



<p class="wp-block-paragraph">So we can do continue 10 times on breakpoint four.</p>



<p class="wp-block-paragraph">What is going on here?</p>



<p class="wp-block-paragraph">Did I write this program in a weird way?</p>



<p class="wp-block-paragraph">Hmm.</p>



<p class="wp-block-paragraph">Well, trust me on this.</p>



<p class="wp-block-paragraph">Let&#8217;s continue 100 on breakpoint three and then continue 100 on breakpoint four.</p>



<p class="wp-block-paragraph">breakpoint 4. Okay, so now we&#8217;re just done with the whole entire program. I guess there must have</p>



<p class="wp-block-paragraph">been a 2 there that I missed. So now we&#8217;re finally on breakpoint 6, which is when everything is</p>



<p class="wp-block-paragraph">finished. Let me show you real fast that we can add more breakpoints in assembly just to prove</p>



<p class="wp-block-paragraph">that we can debug assembly real fast. So if I want to debug assembly, I just have to type the name of</p>



<p class="wp-block-paragraph">that source code file. If it was nested in a folder, like if you had like a complicated hierarchy of</p>



<p class="wp-block-paragraph">of source code files you would just need to type the relative path but for now I&#8217;m just going to</p>



<p class="wp-block-paragraph">be able to type the name of the file so assy.asm and let&#8217;s suppose that I want to break at line</p>



<p class="wp-block-paragraph">16 just so I can see the registers that I set up so I&#8217;m going to do 16 and then I&#8217;m going to break</p>



<p class="wp-block-paragraph">at assy.asm line 16 and then I&#8217;m going to run and that actually never gets called does it no no it</p>



<p class="wp-block-paragraph">called does it no no it gets called I think I have a call up here somewhere</p>



<p class="wp-block-paragraph">nope I don&#8217;t have a call okay let&#8217;s make a call this is gonna throw off all of my</p>



<p class="wp-block-paragraph">C++ breakpoints so I&#8217;ll just comment them all out right now comment them all</p>



<p class="wp-block-paragraph">out so I don&#8217;t have to redo them and this is a hybrid program so I&#8217;m gonna go</p>



<p class="wp-block-paragraph">extern what is it extern C make a block where I name the function the reason we</p>



<p class="wp-block-paragraph">name mangling so the AC symbol is going to show up as just its regular function</p>



<p class="wp-block-paragraph">name rather than a bunch of extra stuff indicating the prototype so that we can</p>



<p class="wp-block-paragraph">do overloading so we&#8217;re just going to disable that and then at the very top of</p>



<p class="wp-block-paragraph">the program I&#8217;m just going to call it this should hopefully work let me just</p>



<p class="wp-block-paragraph">do a regular make run just to make sure the whole program compiles okay now</p>



<p class="wp-block-paragraph">debug script we&#8217;re gonna break on line 16 which is going to be letting us see</p>



<p class="wp-block-paragraph">the registers okay so I&#8217;m gonna go make debug notice how it hit the breakpoint</p>



<p class="wp-block-paragraph">in the assembly just the same as the C++ and we can print whatever we want now so</p>



<p class="wp-block-paragraph">I&#8217;m just gonna say info registers and if we look carefully we can kind of see</p>



<p class="wp-block-paragraph">that we modified racks REX is 15 so that&#8217;s expected RDI is 20 which is</p>



<p class="wp-block-paragraph">R12 should be a giant number. Where&#8217;s R12? Right there. So you can see the state of your program at any point in time just by breaking on it.</p>



<p class="wp-block-paragraph">I&#8217;m breaking on a certain line. So this is tremendously useful for debugging not just for higher level languages but also assembly.</p>



<p class="wp-block-paragraph">What else can I do? Oh let&#8217;s put a global in the data section. So I&#8217;m gonna do, I don&#8217;t know, my thing or something like that and we&#8217;ll call it a byte array and I&#8217;ll just go hello.</p>



<p class="wp-block-paragraph">I&#8217;ll say we have a thing one and a thing two.</p>



<p class="wp-block-paragraph">Something like that.</p>



<p class="wp-block-paragraph">And maybe thing two is a null terminated string,</p>



<p class="wp-block-paragraph">whereas thing one is a regular thing.</p>



<p class="wp-block-paragraph">We should be able to print those symbols.</p>



<p class="wp-block-paragraph">If that&#8217;s not true, I&#8217;ll come back at a later time</p>



<p class="wp-block-paragraph">and show you how to do it.</p>



<p class="wp-block-paragraph">Anyway, so we&#8217;ll do make debug</p>



<p class="wp-block-paragraph">to just kind of assemble everything again.</p>



<p class="wp-block-paragraph">And we know how to do info registers.</p>



<p class="wp-block-paragraph">We can print, you know, one register in particular,</p>



<p class="wp-block-paragraph">print R12 if we wanted to.</p>



<p class="wp-block-paragraph">History has not reached.</p>



<p class="wp-block-paragraph">History has not reached 12.</p>



<p class="wp-block-paragraph">Oh, I got to put R12.</p>



<p class="wp-block-paragraph">Yeah, then we can get the, wait a minute, wait a minute.</p>



<p class="wp-block-paragraph">Oh, I threw off the line numbers because I added the variables up here.</p>



<p class="wp-block-paragraph">So now we&#8217;re going to break at 21.</p>



<p class="wp-block-paragraph">Let me just fix that.</p>



<p class="wp-block-paragraph">21.</p>



<p class="wp-block-paragraph">We&#8217;ll do a quit.</p>



<p class="wp-block-paragraph">Make debug again.</p>



<p class="wp-block-paragraph">And now we&#8217;re at 21.</p>



<p class="wp-block-paragraph">If I say info registers, we can see R12 is that big value.</p>



<p class="wp-block-paragraph">you, but we can also print R12 directly. And we should be able to print my thing one, hopefully,</p>



<p class="wp-block-paragraph">has an unknown type, oh, we have to cast it. So we can do like basic casting in GDB. So I&#8217;m going</p>



<p class="wp-block-paragraph">to say this is a character pointer. Remember, all of the assembly symbols are basically pointers,</p>



<p class="wp-block-paragraph">unless it&#8217;s an EQU, EQU variable. In that case, it&#8217;s more of a define. So I&#8217;m going to print</p>



<p class="wp-block-paragraph">and then it says error cannot access the memory add address</p>



<p class="wp-block-paragraph">what the heck did i do wrong how about that cannot access that all right i&#8217;ll come back in</p>



<p class="wp-block-paragraph">another video because this wasn&#8217;t something that i prepared for just to print strings in globals</p>



<p class="wp-block-paragraph">i&#8217;ll come back at some later date in the future like five years from now and i&#8217;ll just show you</p>



<p class="wp-block-paragraph">how to print globals but i hope if you&#8217;re in assembly you probably don&#8217;t need to worry about</p>



<p class="wp-block-paragraph">You just have globals, but the real problem for you is going to be what&#8217;s inside of your registers and so forth.</p>



<p class="wp-block-paragraph">You can also deref, I think.</p>



<p class="wp-block-paragraph">Let me see.</p>



<p class="wp-block-paragraph">Okay, I&#8217;m going to move on from that.</p>



<p class="wp-block-paragraph">So anyway, we got a script and we got a lot of basic GDB commands.</p>



<p class="wp-block-paragraph">Is there anything else that I wanted to show you?</p>



<p class="wp-block-paragraph">Let me just look at my notes real fast here to make sure I&#8217;m not forgetting to say anything.</p>



<p class="wp-block-paragraph">make sure I&#8217;m not forgetting to say anything.</p>



<p class="wp-block-paragraph">So we&#8217;re going to do debug symbols, console, quit, attach to binary,</p>



<p class="wp-block-paragraph">launching the program, uncaught exception, breakpoints, info, delete,</p>



<p class="wp-block-paragraph">enable, disable, run, continue, end times, info registers, printing.</p>



<p class="wp-block-paragraph">Oh, we can print a register in binary.</p>



<p class="wp-block-paragraph">Forgot to show that.</p>



<p class="wp-block-paragraph">So we are at a breakpoint, I think.</p>



<p class="wp-block-paragraph">And if we printed R12 with the regular print, we can also do P slash T to just</p>



<p class="wp-block-paragraph">p slash t to just sort of print it in binary if you want to i think i might have said that already</p>



<p class="wp-block-paragraph">either that or i said that like a couple days ago to some and then we can do info locals and</p>



<p class="wp-block-paragraph">then print a variable and then we get the script file and then we can quit okay so i think this is</p>



<p class="wp-block-paragraph">pretty much everything that i wanted to show you you now have a basic idea of what we can do inside</p>



<p class="wp-block-paragraph">of gdb but keep in mind there is so much more you can do in gdb than i can fit in one video</p>



<p class="wp-block-paragraph">can type help and you can see a list of other major you know areas that gdb can handle um you</p>



<p class="wp-block-paragraph">can do uh let&#8217;s see i think it&#8217;s help and then like we&#8217;ve got information on aliases breakpoints</p>



<p class="wp-block-paragraph">data you know whatever we can do i think help breakpoints to see more information on breakpoint</p>



<p class="wp-block-paragraph">commands yeah so all of the things you can do with breakpoints is like all of these commands here</p>



<p class="wp-block-paragraph">one page we have to enter to see one more page or see to see all of the results without using</p>



<p class="wp-block-paragraph">paging anymore so i&#8217;m just hit c and so there&#8217;s like you know we can save trace points we can</p>



<p class="wp-block-paragraph">try to catch exceptions we can enable disable break points there&#8217;s like unwinders which i&#8217;ve</p>



<p class="wp-block-paragraph">never even used we can bookmark things we can you know start tracing memory we can rethrow</p>



<p class="wp-block-paragraph">variables so just keep in mind gdp is incredibly powerful this video barely scratches the surface</p>



<p class="wp-block-paragraph">but for a programmer who is new to debugging i think this will be very useful to you i hope it is</p>



<p class="wp-block-paragraph">anyway thanks for watching this video i hope you learned a little bit of stuff and had a</p>



<p class="wp-block-paragraph">little bit of fun um i will see you in the next video rest yourself and take a nap and have fun</p>



<p class="wp-block-paragraph">and hug your your loved ones okay I&#8217;m out see you later hey everybody thanks</p>



<p class="wp-block-paragraph">for watching this video again from the bottom of my heart I really appreciate</p>



<p class="wp-block-paragraph">it I do hope you did learn something and have some fun if you could do me a</p>



<p class="wp-block-paragraph">please a small little favor could you please subscribe and follow this channel</p>



<p class="wp-block-paragraph">channel or these videos or whatever it is you do on the current social media</p>



<p class="wp-block-paragraph">website that you&#8217;re looking at right now it would really mean the world to me</p>



<p class="wp-block-paragraph">and it&#8217;ll help make more videos and grow this community so we&#8217;ll be able to do</p>



<p class="wp-block-paragraph">more videos longer videos better videos or just I&#8217;ll be able to keep making</p>



<p class="wp-block-paragraph">videos in general so please do do me a kindness and and subscribe you know</p>



<p class="wp-block-paragraph">sometimes I&#8217;m sleeping in the middle of the night and I just wake up because I</p>



<p class="wp-block-paragraph">know somebody subscribed or followed it just wakes me up and I get filled with</p>



<p class="wp-block-paragraph">That&#8217;s exactly what happens every single time.</p>



<p class="wp-block-paragraph">So you could do it as a nice favor to me,</p>



<p class="wp-block-paragraph">or you could troll me if you want to just wake me up in the middle of the night.</p>



<p class="wp-block-paragraph">Just subscribe, and then I&#8217;ll just wake up.</p>



<p class="wp-block-paragraph">I promise that&#8217;s what will happen.</p>



<p class="wp-block-paragraph">Also, if you look at the middle of the screen right now,</p>



<p class="wp-block-paragraph">you should see a QR code, which you can scan in order to go to the website,</p>



<p class="wp-block-paragraph">which I think is also named somewhere at the bottom of this video.</p>



<p class="wp-block-paragraph">And it&#8217;ll take you to my main website,</p>



<p class="wp-block-paragraph">where you can just kind of like see all the videos I published,</p>



<p class="wp-block-paragraph">and the services and tutorials and things that I offer,</p>



<p class="wp-block-paragraph">and all that good stuff.</p>



<p class="wp-block-paragraph">things that I offer and all that good stuff. And, uh, if you have a suggestion for, uh, uh,</p>



<p class="wp-block-paragraph">clarifications or errata, or just future videos that you want to see, please leave a comment.</p>



<p class="wp-block-paragraph">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 class="wp-block-paragraph">whatever. I also wake up for those in the middle of the night. I get, I wake up in a cold sweat</p>



<p class="wp-block-paragraph">and I&#8217;m like, it would really, it really mean the world to me. I would really appreciate it. So</p>



<p class="wp-block-paragraph">watching this video and enjoy the cool music as I fade into the darkness which is coming for us all.</p>



<p class="wp-block-paragraph">Thank you.</p>
<p>The post <a href="https://www.NeuralLantern.com/master-gnu-debugger-debug-c-assembly-programs-with-gdb-like-a-pro/">Master GNU Debugger: Debug C++ &amp; Assembly Programs with GDB Like a Pro</a> appeared first on <a href="https://www.NeuralLantern.com">NeuralLantern.com</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.NeuralLantern.com/master-gnu-debugger-debug-c-assembly-programs-with-gdb-like-a-pro/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Master Data Sizes in x86-64 CPUs: Bytes, Words, D Words, and Quad Words Explained</title>
		<link>https://www.NeuralLantern.com/master-data-sizes-in-x86-64-cpus-bytes-words-d-words-and-quad-words-explained/</link>
					<comments>https://www.NeuralLantern.com/master-data-sizes-in-x86-64-cpus-bytes-words-d-words-and-quad-words-explained/#respond</comments>
		
		<dc:creator><![CDATA[mike]]></dc:creator>
		<pubDate>Mon, 09 Jun 2025 00:07:07 +0000</pubDate>
				<category><![CDATA[Computer Science]]></category>
		<category><![CDATA[Videos]]></category>
		<category><![CDATA[64-bit computing]]></category>
		<category><![CDATA[assembly language]]></category>
		<category><![CDATA[bytes]]></category>
		<category><![CDATA[coding tutorial]]></category>
		<category><![CDATA[computer architecture]]></category>
		<category><![CDATA[CPU basics]]></category>
		<category><![CDATA[CPU registers]]></category>
		<category><![CDATA[D word]]></category>
		<category><![CDATA[data sizes]]></category>
		<category><![CDATA[low-level programming]]></category>
		<category><![CDATA[programming for beginners]]></category>
		<category><![CDATA[quad word]]></category>
		<category><![CDATA[tech education]]></category>
		<category><![CDATA[words]]></category>
		<category><![CDATA[x86-64]]></category>
		<guid isPermaLink="false">https://www.NeuralLantern.com/?p=148</guid>

					<description><![CDATA[<p>Learn data sizes in x86-64 CPUs: bytes, words, D words, quad words. Clear guide for coders on assembly &#038; registers. Subscribe for more!</p>
<p>The post <a href="https://www.NeuralLantern.com/master-data-sizes-in-x86-64-cpus-bytes-words-d-words-and-quad-words-explained/">Master Data Sizes in x86-64 CPUs: Bytes, Words, D Words, and Quad Words 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="Master Data Sizes in x86-64 CPUs: Bytes, Words, D Words, and Quad Words Explained" width="1380" height="776" src="https://www.youtube.com/embed/v3fMANsXIwU?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 class="wp-block-paragraph">Get a clear, fun breakdown of data sizes in x86-64 CPUs! This video covers bytes (8 bits), words (16 bits), D words (32 bits), and quad words (64 bits), plus how they fit into assembly and CPU registers. Perfect for programmers, students, or anyone curious about low-level computing. I’ll walk you through the basics with relatable examples, no jargon overload. Subscribe for more coding &amp; tech videos, and scan the QR code to visit my site for extra tutorials! Leave a comment with your thoughts!</p>



<p class="wp-block-paragraph">Introduction 00:00:00<br>Data Sizes Overview 00:00:01<br>Byte Definition 00:00:44<br>Bits to Bytes Conversion 00:01:27<br>Word Size Explanation 00:02:20<br>Double Word (D Word) 00:04:03<br>Quad Word Definition 00:04:44<br>CPU Registers and Memory 00:05:21<br>Conclusion and Outro 00:06:07<br>Call to Action 00:06:32</p>



<p class="wp-block-paragraph">Thanks for watching!</p>



<p class="wp-block-paragraph">Find us on other social media here:</p>



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



<p class="wp-block-paragraph">Please help support us!</p>



<ul class="wp-block-list">
<li>Subscribing + Sharing on Social Media</li>



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



<li>Subscribing to our Blog</li>



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



<p class="wp-block-paragraph">Hello there.</p>



<p class="wp-block-paragraph">I&#8217;d like to talk to you about data sizes within the context of x86,</p>



<p class="wp-block-paragraph">64 CPUs,</p>



<p class="wp-block-paragraph">just like 64 bit CPUs for assembly and just whatever.</p>



<p class="wp-block-paragraph">So what&#8217;s going on inside of the machine and how do we describe the sizes of</p>



<p class="wp-block-paragraph">various clumps of data? So for starters, you know,</p>



<p class="wp-block-paragraph">of your computer, there&#8217;s just a bunch of ones and zeros represented in all the wires,</p>



<p class="wp-block-paragraph">in all the circuitry.</p>



<p class="wp-block-paragraph">The human beings with feelings decide, oh, you know, I&#8217;m going to take a group of this</p>



<p class="wp-block-paragraph">many bits, this many ones and zeros, and I&#8217;m going to say that it is something else.</p>



<p class="wp-block-paragraph">I&#8217;m going to call it an abstract unit.</p>



<p class="wp-block-paragraph">So the first thing that we do is we have something called a byte.</p>



<p class="wp-block-paragraph">That just means eight bits.</p>



<p class="wp-block-paragraph">So we can say one byte equals eight bits.</p>



<p class="wp-block-paragraph">equals eight bits. I don&#8217;t know why I hit the shift key there. That&#8217;s probably going to mess</p>



<p class="wp-block-paragraph">up my annotator. Nope. Okay. So what is eight bits? Just as a little reminder, you know, we&#8217;ll</p>



<p class="wp-block-paragraph">just say one, two, three, four, five, six, seven, eight, or it could be, you know, something more</p>



<p class="wp-block-paragraph">random looking one, two, three, four, five, six, seven, eight, you know, so like inside of the</p>



<p class="wp-block-paragraph">machine, we have eight ones and zeros for eight bits. And when we group them together, we&#8217;ll just</p>



<p class="wp-block-paragraph">Okay, no problem.</p>



<p class="wp-block-paragraph">So that means by the way, if somebody says,</p>



<p class="wp-block-paragraph">oh, my internet connection is this many megabits a second.</p>



<p class="wp-block-paragraph">If you wanna know how many bytes a second,</p>



<p class="wp-block-paragraph">try dividing by eight.</p>



<p class="wp-block-paragraph">Or if somebody says I have this many megabytes a second</p>



<p class="wp-block-paragraph">of transfer speed, try dividing that by eight.</p>



<p class="wp-block-paragraph">You know, so multiply or divide by eight</p>



<p class="wp-block-paragraph">to get the right number.</p>



<p class="wp-block-paragraph">Okay, so if we had two bytes together,</p>



<p class="wp-block-paragraph">that&#8217;s just gonna be 16 bits.</p>



<p class="wp-block-paragraph">No problem there.</p>



<p class="wp-block-paragraph">I&#8217;m just going to copy paste these bits and you don&#8217;t have to do this.</p>



<p class="wp-block-paragraph">But for me personally, I like to put a little space in between bite groupings just to help</p>



<p class="wp-block-paragraph">my human brain kind of see, oh, I&#8217;m looking at two bites right here when I&#8217;m referring</p>



<p class="wp-block-paragraph">to different data sizes.</p>



<p class="wp-block-paragraph">So then eventually we kind of need an idea of, you know, let&#8217;s come up with a different</p>



<p class="wp-block-paragraph">word that means more than one bite.</p>



<p class="wp-block-paragraph">ironically based on what I just said the the term we&#8217;re going to use is word a word</p>



<p class="wp-block-paragraph">the size of a word just kind of depends on the system that you&#8217;re on on my system</p>



<p class="wp-block-paragraph">the size of a word is two bytes so I&#8217;m going to say one word equals two bytes</p>



<p class="wp-block-paragraph">and you can imagine that&#8217;s 16 bits maybe I should start doing the double equal sign here just to be</p>



<p class="wp-block-paragraph">Okay, so equals two bytes.</p>



<p class="wp-block-paragraph">It also equals 16 bits.</p>



<p class="wp-block-paragraph">And you can imagine it&#8217;s basically the same thing</p>



<p class="wp-block-paragraph">that we just wrote up above.</p>



<p class="wp-block-paragraph">So I&#8217;m gonna erase this actually, 16.</p>



<p class="wp-block-paragraph">Oh, I put an E there, embarrassing.</p>



<p class="wp-block-paragraph">Okay, so one word is two bytes.</p>



<p class="wp-block-paragraph">Again, the size of a word depends on your system.</p>



<p class="wp-block-paragraph">If you&#8217;re not sure, you should probably look it up.</p>



<p class="wp-block-paragraph">What&#8217;s the word size on my system?</p>



<p class="wp-block-paragraph">But again, the typical system here in 2024</p>



<p class="wp-block-paragraph">6664 machines running Ubuntu, um, or just like Linux is going to be like, you know,</p>



<p class="wp-block-paragraph">two bytes. Okay. So now, uh, we have like words that we can do. So if I had, uh,</p>



<p class="wp-block-paragraph">let&#8217;s say I had two words, uh, two words is going to be double of what we just wrote. Right. So</p>



<p class="wp-block-paragraph">that means we&#8217;re going to have this many, whoops, I blew it. We&#8217;re going to have this many bits.</p>



<p class="wp-block-paragraph">So two words, two words is going to be equal to, um, 32 bits.</p>



<p class="wp-block-paragraph">And it&#8217;s also going to be equal to four bytes because, you know, we multiplied everything</p>



<p class="wp-block-paragraph">by two.</p>



<p class="wp-block-paragraph">So this is like four bytes.</p>



<p class="wp-block-paragraph">So now we need another term to represent this two words, which is four bytes, which</p>



<p class="wp-block-paragraph">is 32 bits.</p>



<p class="wp-block-paragraph">We can also call that a D word.</p>



<p class="wp-block-paragraph">Actually, I&#8217;ll say D word like that.</p>



<p class="wp-block-paragraph">And what does D word mean?</p>



<p class="wp-block-paragraph">is twice the size of the word length on your system.</p>



<p class="wp-block-paragraph">So like if we decided our word length is two bytes,</p>



<p class="wp-block-paragraph">then a D word is four bytes and that&#8217;s it.</p>



<p class="wp-block-paragraph">Four bytes is 32 bits.</p>



<p class="wp-block-paragraph">You might notice this is similar to like old school CPUs</p>



<p class="wp-block-paragraph">that ran in 32 bits and maxed out their RAM</p>



<p class="wp-block-paragraph">at about four gigabytes.</p>



<p class="wp-block-paragraph">So let&#8217;s come up with one more term here.</p>



<p class="wp-block-paragraph">Let&#8217;s come up with the term quad word.</p>



<p class="wp-block-paragraph">Actually, we&#8217;re not coming up with the term.</p>



<p class="wp-block-paragraph">We&#8217;re just using it.</p>



<p class="wp-block-paragraph">What is a quad word?</p>



<p class="wp-block-paragraph">A quad word is two D words.</p>



<p class="wp-block-paragraph">So we&#8217;re just kind of doubling it again.</p>



<p class="wp-block-paragraph">I forgot that extra equal sign there.</p>



<p class="wp-block-paragraph">Quad word is two D words, which means it&#8217;s also four words.</p>



<p class="wp-block-paragraph">And it also means it&#8217;s eight bytes.</p>



<p class="wp-block-paragraph">64 bits.</p>



<p class="wp-block-paragraph">And then I guess if I want to represent this to you,</p>



<p class="wp-block-paragraph">I can just kind of like copy paste that twice.</p>



<p class="wp-block-paragraph">And this is the size of the memory address space that we have on modern CPUs.</p>



<p class="wp-block-paragraph">This is also the size of your general purpose registers in modern 64-bit CPUs.</p>



<p class="wp-block-paragraph">So if somebody says, hey, I got a quad word.</p>



<p class="wp-block-paragraph">I want you to store it somewhere.</p>



<p class="wp-block-paragraph">You can instantly think to yourself, well, obviously I could put it in memory.</p>



<p class="wp-block-paragraph">I could put it in memory.</p>



<p class="wp-block-paragraph">You could also just put it directly into a CPU register.</p>



<p class="wp-block-paragraph">And that&#8217;s it.</p>



<p class="wp-block-paragraph">This also means that you could put two D words into one CPU register if you were very clever.</p>



<p class="wp-block-paragraph">You could also do the same thing.</p>



<p class="wp-block-paragraph">You could put like four words inside of one CPU register.</p>



<p class="wp-block-paragraph">Because the CPU registers, they&#8217;re always the same size no matter how you use them.</p>



<p class="wp-block-paragraph">They&#8217;re kind of hardwired.</p>



<p class="wp-block-paragraph">So I hope this has been, you know, kind of helpful.</p>



<p class="wp-block-paragraph">These are the basics of different data type sizes that we have in CPUs and</p>



<p class="wp-block-paragraph">assembly language. I&#8217;ll see you in the next video.</p>



<p class="wp-block-paragraph">Hey everybody. Thanks for watching this video again from the bottom of my heart.</p>



<p class="wp-block-paragraph">I really appreciate it. I do hope you did learn something and have some fun.</p>



<p class="wp-block-paragraph">If you could do me a please, a small little favor,</p>



<p class="wp-block-paragraph">Follow this channel or these videos or whatever it is you do on the current social media website that you&#8217;re looking at right now</p>



<p class="wp-block-paragraph">It would really mean the world to me and it&#8217;ll help make more videos and grow this community</p>



<p class="wp-block-paragraph">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 class="wp-block-paragraph">So, please do do me a kindness and and subscribe</p>



<p class="wp-block-paragraph">You know sometimes I&#8217;m sleeping in the middle of the night and I just wake up because I know somebody subscribed or followed</p>



<p class="wp-block-paragraph">and I get filled with joy. That&#8217;s exactly what happens every single time. So you could do it as</p>



<p class="wp-block-paragraph">a nice favor to me, or you could, you could troll me if you want to just wake me up in the middle</p>



<p class="wp-block-paragraph">of the night, just subscribe. And then I&#8217;ll, I&#8217;ll just wake up. I promise that&#8217;s what will happen.</p>



<p class="wp-block-paragraph">Also, uh, if you look at the middle of the screen right now, you should see a QR code,</p>



<p class="wp-block-paragraph">which you can scan in order to go to the website, which I think is also named somewhere at the</p>



<p class="wp-block-paragraph">bottom of this video. And it&#8217;ll take you to my main website where you can just kind of like see</p>



<p class="wp-block-paragraph">and tutorials and things that I offer and all that good stuff and if you have a suggestion for</p>



<p class="wp-block-paragraph">clarifications or errata or just future videos that you want to see please leave a comment or</p>



<p class="wp-block-paragraph">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 class="wp-block-paragraph">wake up for those in the middle of the night I get I wake up in a cold sweat and I&#8217;m like this</p>



<p class="wp-block-paragraph">it would really it really mean the world to me I would really appreciate it so again thank you so</p>



<p class="wp-block-paragraph">thank you so much for watching this video and enjoy the cool music as as i fade into the</p>



<p class="wp-block-paragraph">darkness which is coming for us all</p>



<p class="wp-block-paragraph">Thank you.</p>
<p>The post <a href="https://www.NeuralLantern.com/master-data-sizes-in-x86-64-cpus-bytes-words-d-words-and-quad-words-explained/">Master Data Sizes in x86-64 CPUs: Bytes, Words, D Words, and Quad Words Explained</a> appeared first on <a href="https://www.NeuralLantern.com">NeuralLantern.com</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.NeuralLantern.com/master-data-sizes-in-x86-64-cpus-bytes-words-d-words-and-quad-words-explained/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Compile, Link, Execute: A Beginner’s Guide to Linux Programming with C++, C, and Assembly</title>
		<link>https://www.NeuralLantern.com/compile-link-execute-a-beginners-guide-to-linux-programming-with-c-c-and-assembly/</link>
					<comments>https://www.NeuralLantern.com/compile-link-execute-a-beginners-guide-to-linux-programming-with-c-c-and-assembly/#respond</comments>
		
		<dc:creator><![CDATA[mike]]></dc:creator>
		<pubDate>Wed, 28 May 2025 03:37:23 +0000</pubDate>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[Computer Science]]></category>
		<category><![CDATA[Videos]]></category>
		<category><![CDATA[assembly programming]]></category>
		<category><![CDATA[bash scripting]]></category>
		<category><![CDATA[beginner coding]]></category>
		<category><![CDATA[C programming]]></category>
		<category><![CDATA[coding tutorial]]></category>
		<category><![CDATA[compile C++]]></category>
		<category><![CDATA[execute Linux]]></category>
		<category><![CDATA[G++ tutorial]]></category>
		<category><![CDATA[link object files]]></category>
		<category><![CDATA[Linux compile script]]></category>
		<category><![CDATA[Linux development]]></category>
		<category><![CDATA[Linux programming]]></category>
		<category><![CDATA[name mangling]]></category>
		<category><![CDATA[object files]]></category>
		<category><![CDATA[programming basics]]></category>
		<guid isPermaLink="false">https://www.NeuralLantern.com/?p=135</guid>

					<description><![CDATA[<p>Learn to compile, link &#038; execute C++, C, and assembly on Linux! Beginner-friendly guide with bash scripting tips. #Linux #Programming</p>
<p>The post <a href="https://www.NeuralLantern.com/compile-link-execute-a-beginners-guide-to-linux-programming-with-c-c-and-assembly/">Compile, Link, Execute: A Beginner’s Guide to Linux Programming with C++, C, and Assembly</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="Compile, Link, Execute: A Beginner&#039;s Guide to Linux Programming with C++, C, and Assembly" width="1380" height="776" src="https://www.youtube.com/embed/z9iuTrptPM0?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 class="wp-block-paragraph">Hey there, coding enthusiasts! Ready to master compiling, linking, and executing programs on Linux? In this fun and approachable video, we dive into the nuts and bolts of turning your code into runnable programs. We’ll create a simple C++ program, compile it with G++, and explore object files, linking, and execute permissions. Plus, we’ll mix in C and assembly code, tackle name mangling, and build a bash script to automate it all! Perfect for beginners or anyone curious about Linux programming, this guide is packed with practical tips and real-world examples. Don’t miss out—subscribe for more coding adventures, leave a comment with your thoughts, and let’s geek out together! Scan the QR code to visit my site for more tutorials. #LinuxProgramming #CodingForBeginners #CPlusPlus</p>



<p class="wp-block-paragraph">Introduction to Compiling, Linking, and Executing 00:00:00<br>Overview of the Process 00:00:05<br>Writing a Simple C++ Program 00:00:28<br>Compiling with G++ 00:02:01<br>Understanding Executable Files 00:03:04<br>Executing a Program 00:04:21<br>Handling Execute Permissions 00:05:13<br>Creating a Compile Script 00:07:00<br>Using Shebang for Scripts 00:08:43<br>Specifying Output Files 00:10:53<br>Breaking Down Compilation Steps 00:12:26<br>Compiling to Object Files 00:13:17<br>Linking Object Files 00:15:24<br>Handling Multiple Source Files 00:18:39<br>Mixing C and C++ with Name Mangling 00:22:52<br>Enhancing Script with Error Handling 00:24:29<br>Adding Assembly Source File 00:31:16<br>Using Bash Variables for Flags 00:41:53<br>Organizing Compiler Flags 00:44:41<br>Final Script Enhancements 00:46:36<br>Conclusion and Call to Action 00:47:35</p>



<p class="wp-block-paragraph">Thanks for watching!</p>



<p class="wp-block-paragraph">Find us on other social media here:</p>



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



<p class="wp-block-paragraph">Please help support us!</p>



<ul class="wp-block-list">
<li>Subscribing + Sharing on Social Media</li>



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



<li>Subscribing to our Blog</li>



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



<p class="wp-block-paragraph">Hey there! Let&#8217;s talk about compiling, linking and executing.</p>



<p class="wp-block-paragraph">This video is going to focus on Linux, but I think it&#8217;ll be useful for all other operating systems that you might be working with at home.</p>



<p class="wp-block-paragraph">I&#8217;m just going to basically explain like what does it mean to compile?</p>



<p class="wp-block-paragraph">What does it mean to link a program?</p>



<p class="wp-block-paragraph">What does it mean to execute a program?</p>



<p class="wp-block-paragraph">If you don&#8217;t know that, then I think this video might be for you.</p>



<p class="wp-block-paragraph">Okay, so for starters, imagine you have written a program.</p>



<p class="wp-block-paragraph">I&#8217;m going to write the program here real fast.</p>



<p class="wp-block-paragraph">I&#8217;m going to say, let&#8217;s do an empty file.</p>



<p class="wp-block-paragraph">We&#8217;ll call it, let&#8217;s see, I got my answer key up here.</p>



<p class="wp-block-paragraph">We&#8217;ll call it first.cpp.</p>



<p class="wp-block-paragraph">First.cpp.</p>



<p class="wp-block-paragraph">So I&#8217;m just like making a first piece of source code and I&#8217;m editing it in my little editor.</p>



<p class="wp-block-paragraph">Notice how I&#8217;m not calling this the main.cpp program because a lot of beginners, they always</p>



<p class="wp-block-paragraph">do main.</p>



<p class="wp-block-paragraph">I want you to know that you don&#8217;t actually need a source code file called main.cpp.</p>



<p class="wp-block-paragraph">Even if you&#8217;re writing a C++ program, all you really need is just one instance of the function main.</p>



<p class="wp-block-paragraph">Beyond that also, you only need an instance of main if you&#8217;re writing a hybrid program</p>



<p class="wp-block-paragraph">or some kind of program that is in pure C++ or that has the GCC libraries.</p>



<p class="wp-block-paragraph">GCC libraries call on the main function as your entry point.</p>



<p class="wp-block-paragraph">But if you&#8217;re writing pure assembly, the entry point is actually underscore start as a label.</p>



<p class="wp-block-paragraph">We&#8217;re not really going to do this is not an assembly tutorial.</p>



<p class="wp-block-paragraph">I just wanted you to know real fast.</p>



<p class="wp-block-paragraph">Okay.</p>



<p class="wp-block-paragraph">So I&#8217;m going to do like IO stream and then I&#8217;m going to say</p>



<p class="wp-block-paragraph">STDC out and I&#8217;m just going to print, you know, hello from first.</p>



<p class="wp-block-paragraph">Cpp.</p>



<p class="wp-block-paragraph">STD.</p>



<p class="wp-block-paragraph">And L.</p>



<p class="wp-block-paragraph">Okay.</p>



<p class="wp-block-paragraph">So we kind of have like a hello world program that&#8217;ll just</p>



<p class="wp-block-paragraph">that&#8217;ll just basically print a message and do nothing else how do we compile</p>



<p class="wp-block-paragraph">this program and execute it you know what does that actually even mean okay</p>



<p class="wp-block-paragraph">so for starters the first thing that you probably do when you&#8217;re compiling a</p>



<p class="wp-block-paragraph">program you&#8217;ve probably been told to do something like this let me get my little</p>



<p class="wp-block-paragraph">command line open here you&#8217;ve probably been told to do something like this</p>



<p class="wp-block-paragraph">let&#8217;s compile with GCC or G++ and we&#8217;ll say the standard for C++ that we&#8217;re</p>



<p class="wp-block-paragraph">There are a bunch of other standards you can use.</p>



<p class="wp-block-paragraph">We can turn on all warnings.</p>



<p class="wp-block-paragraph">We can have the compiler be pedantic in terms of the warnings that it gives us.</p>



<p class="wp-block-paragraph">You know, these two things at the end don&#8217;t really matter that much.</p>



<p class="wp-block-paragraph">It&#8217;s really all about G++ and then STD C++ 23 to get it compiled.</p>



<p class="wp-block-paragraph">And then we&#8217;ll give an input file.</p>



<p class="wp-block-paragraph">We&#8217;ll say burst.cpp.</p>



<p class="wp-block-paragraph">And then I think probably usually this is where most of you stop.</p>



<p class="wp-block-paragraph">you stop we just kind of like specify a couple flags specify the input file then</p>



<p class="wp-block-paragraph">hit enter and it compiles if we list the contents of the directory now this might</p>



<p class="wp-block-paragraph">be familiar you&#8217;ll probably see something called a.out a.out notice how</p>



<p class="wp-block-paragraph">the permissions look a little bit different for that for that file it&#8217;s</p>



<p class="wp-block-paragraph">kind of printed in green in Ubuntu and if you look at the left side right here</p>



<p class="wp-block-paragraph">you can see that it&#8217;s got a bunch of X&#8217;s in the permissions area that just means</p>



<p class="wp-block-paragraph">That just means it has execute permission.</p>



<p class="wp-block-paragraph">The compiler does that automatically for you,</p>



<p class="wp-block-paragraph">but essentially this is like just, you know,</p>



<p class="wp-block-paragraph">an executable binary file.</p>



<p class="wp-block-paragraph">It&#8217;s not a text file.</p>



<p class="wp-block-paragraph">It&#8217;s just something that the computer is meant to run.</p>



<p class="wp-block-paragraph">We can look inside of it real fast.</p>



<p class="wp-block-paragraph">There&#8217;s a program called XXD on Linux,</p>



<p class="wp-block-paragraph">and it&#8217;ll sort of spill the contents of that file.</p>



<p class="wp-block-paragraph">So if I say XXD and then I go a.out,</p>



<p class="wp-block-paragraph">it just spills like a whole lot of like binary nonsense,</p>



<p class="wp-block-paragraph">right?</p>



<p class="wp-block-paragraph">you can see at the very beginning elf that&#8217;s uh i think that&#8217;s executable linking format</p>



<p class="wp-block-paragraph">for linux and then there&#8217;s like a bunch of stuff and it just doesn&#8217;t make sense this is not text</p>



<p class="wp-block-paragraph">that you can read here&#8217;s like some sort of a shared argument that it&#8217;s using and then some</p>



<p class="wp-block-paragraph">register information i don&#8217;t even know what most of this is somewhere in here if you look</p>



<p class="wp-block-paragraph">carefully you&#8217;ll probably see that message where is that oh yeah hello from c first.cpp so it&#8217;s</p>



<p class="wp-block-paragraph">saying that the message we typed in the program is actually embedded in the executable.</p>



<p class="wp-block-paragraph">That makes sense.</p>



<p class="wp-block-paragraph">So hopefully I&#8217;ve proved to you that this is just an executable program.</p>



<p class="wp-block-paragraph">How do you execute the program?</p>



<p class="wp-block-paragraph">Well, usually you&#8217;ll have to type, you know, the full path to the program that you want to execute</p>



<p class="wp-block-paragraph">because your custom program is not in the system library paths.</p>



<p class="wp-block-paragraph">So your system&#8217;s not going to know where to find it automatically,</p>



<p class="wp-block-paragraph">even if it&#8217;s in the current folder.</p>



<p class="wp-block-paragraph">So you would usually do something like, you know, the full path to your executable,</p>



<p class="wp-block-paragraph">path to your executable like a.out. But there&#8217;s a shortcut we can use in Linux. The dot directory</p>



<p class="wp-block-paragraph">usually just means or it always means the current directory. So if we say dot and then a slash,</p>



<p class="wp-block-paragraph">that&#8217;s the same thing as typing out the full path to the current directory. Then it&#8217;s really easy to</p>



<p class="wp-block-paragraph">just type the name of the file that we compiled. Okay, so a.out. We executed it now. So that&#8217;s what</p>



<p class="wp-block-paragraph">it means to execute. We just compiled without a lot of options. We&#8217;re going to try to do that better</p>



<p class="wp-block-paragraph">We&#8217;re going to try to do that better in a second.</p>



<p class="wp-block-paragraph">And we know that you need execute permissions.</p>



<p class="wp-block-paragraph">Let me show you what happens if we take away execute permissions.</p>



<p class="wp-block-paragraph">I&#8217;m going to use a command called chamad, like change the mode.</p>



<p class="wp-block-paragraph">I&#8217;m going to say take away execute permissions from the a.out program.</p>



<p class="wp-block-paragraph">Then if I list the directory again, notice how there are a bunch of X&#8217;s missing now</p>



<p class="wp-block-paragraph">on the left side of that a.out permissions area.</p>



<p class="wp-block-paragraph">Notice how it&#8217;s not in green anymore.</p>



<p class="wp-block-paragraph">Depending on your system, it might not be green or gray or whatever.</p>



<p class="wp-block-paragraph">be green or gray or whatever but just look at the permissions so now if i do a dot out</p>



<p class="wp-block-paragraph">it&#8217;ll say permission denied maybe you&#8217;ve seen this before especially if you&#8217;re syncing with</p>



<p class="wp-block-paragraph">the cloud that doesn&#8217;t carry execute permissions from one machine to another which happens to me</p>



<p class="wp-block-paragraph">all the time we can reverse this with just the chamad you know plus x execute permissions</p>



<p class="wp-block-paragraph">and then we can just execute it again the funny thing about execute permissions is</p>



<p class="wp-block-paragraph">you can actually execute a bash program which is basically a text file without</p>



<p class="wp-block-paragraph">without needing to execute a binary program so this is not a bash video I&#8217;ll</p>



<p class="wp-block-paragraph">make other videos in the future on my channels for for bash but for now just</p>



<p class="wp-block-paragraph">know that something you compile is not the only type of thing that you can</p>



<p class="wp-block-paragraph">actually execute in fact in Linux under the hood a lot of system tools are just</p>



<p class="wp-block-paragraph">written in bash or Python or something else that looks like a human readable</p>



<p class="wp-block-paragraph">like a human readable language and they are not necessarily all compiled languages, you</p>



<p class="wp-block-paragraph">know, like C++.</p>



<p class="wp-block-paragraph">Okay, so now that we kind of know that we&#8217;re compiling a program, let&#8217;s be a little bit</p>



<p class="wp-block-paragraph">better about specifying the name of the output file.</p>



<p class="wp-block-paragraph">So I just want you to see one more flag here and while I&#8217;m doing that, I&#8217;m going to create</p>



<p class="wp-block-paragraph">a script that will compile our program for us.</p>



<p class="wp-block-paragraph">nano and I&#8217;ll just name a file I&#8217;ll name it compile and it&#8217;ll be our compiler program okay</p>



<p class="wp-block-paragraph">so what we did before is it was I think G++ and then we named the standard is C++ 23 and then we</p>



<p class="wp-block-paragraph">said wall wall just means give me all warnings which is good to help your you know to help you</p>



<p class="wp-block-paragraph">write better code same thing for pedantic just helps you write better code it gives you more</p>



<p class="wp-block-paragraph">but you know it&#8217;s pretty good. Okay so this is where we stopped last time so</p>



<p class="wp-block-paragraph">I&#8217;m going to say now let me remove a dot out now instead of typing the command by</p>



<p class="wp-block-paragraph">ourselves we can run the compile script. The language I&#8217;ve just written was was</p>



<p class="wp-block-paragraph">well I haven&#8217;t really written in any particular language but if we use a</p>



<p class="wp-block-paragraph">shell interpreter called bash which is actually what&#8217;s running in the terminal</p>



<p class="wp-block-paragraph">right now it&#8217;ll interpret any command line that we give it as just something</p>



<p class="wp-block-paragraph">line that we give it as just something that we want it to execute to the system. So if I type</p>



<p class="wp-block-paragraph">bash to run the bash interpreter and I just specify the name of the script, it should compile the</p>



<p class="wp-block-paragraph">program automatically by executing that one line that I put inside of it. See now I&#8217;ve got the a.out</p>



<p class="wp-block-paragraph">again. If you rewind the video you&#8217;ll see the a.out wasn&#8217;t there when I originally ran it.</p>



<p class="wp-block-paragraph">One more time just to prove it to you. bash compile. Now you can see that it&#8217;s back. Okay</p>



<p class="wp-block-paragraph">Okay, so let&#8217;s make this compile program a little bit better instead of typing bash every single time</p>



<p class="wp-block-paragraph">I think I just want to try to execute the compile program directly. I</p>



<p class="wp-block-paragraph">Can do that pretty easily. There&#8217;s something in</p>



<p class="wp-block-paragraph">In our script that we can put that will designate what interpreter we should use to run the program</p>



<p class="wp-block-paragraph">So right now it&#8217;s just like some text and it doesn&#8217;t really mean anything</p>



<p class="wp-block-paragraph">there&#8217;s something called the shebang which just means hash bang and</p>



<p class="wp-block-paragraph">just provide the path to some interpreter. I&#8217;m going to type shebang and then a path</p>



<p class="wp-block-paragraph">to the bash interpreter. If you don&#8217;t know where bash is on your system, you can go which bash.</p>



<p class="wp-block-paragraph">And I guess mine is a user bin bash. I think I can get away with bin bash. Let me see.</p>



<p class="wp-block-paragraph">Let me just type it out to make sure bin bash. Yeah. Okay. So if I type exit, I&#8217;m actually in</p>



<p class="wp-block-paragraph">another version of bash, which is inside of bash. If I do exit, I should just kind of stay where I</p>



<p class="wp-block-paragraph">Okay, so bin bash will work, user bin bash will also work.</p>



<p class="wp-block-paragraph">I&#8217;m going to go back to editing this compiler program.</p>



<p class="wp-block-paragraph">And now all I have to do, because the system is going to know what interpreter to use to execute my little script program here,</p>



<p class="wp-block-paragraph">all I have to do is add execute permissions on it, and it should work.</p>



<p class="wp-block-paragraph">So let me first remove a dot out.</p>



<p class="wp-block-paragraph">And then let me try first to execute compile.</p>



<p class="wp-block-paragraph">It should not work because I don&#8217;t have execute permissions.</p>



<p class="wp-block-paragraph">Remember we talked about that before.</p>



<p class="wp-block-paragraph">permissions. Remember we talked about that before? So if I do that it says</p>



<p class="wp-block-paragraph">permission denied. Oh no! So I&#8217;m going to give it permission to execute. I&#8217;m going</p>



<p class="wp-block-paragraph">to say change the mode plus X on compile. If I list the directory again notice how</p>



<p class="wp-block-paragraph">it&#8217;s green notice how there&#8217;s a bunch of X&#8217;s everywhere. So now I should be able</p>



<p class="wp-block-paragraph">to just say compile and then it seems to have worked and notice how it has an</p>



<p class="wp-block-paragraph">because it&#8217;s a pain in the butt to type out all the compile commands every single time,</p>



<p class="wp-block-paragraph">especially if you have a complicated compile command or many files that you want to compile</p>



<p class="wp-block-paragraph">in the same shot. So, you know, of course, in another video, I&#8217;m going to talk about make files,</p>



<p class="wp-block-paragraph">build systems, proper build systems are a lot better than just making a script. But for now,</p>



<p class="wp-block-paragraph">let&#8217;s just make a script because I just want you to understand, you know, compiling, linking,</p>



<p class="wp-block-paragraph">some of the surrounding ideas. Okay, so I&#8217;m going to, let&#8217;s see, I&#8217;m going to go nano compile.</p>



<p class="wp-block-paragraph">And the next upgrade that I want to make is I want to specify the output file because it&#8217;s</p>



<p class="wp-block-paragraph">kind of a bad idea to not specify the output file and just hope that the compiler gives you</p>



<p class="wp-block-paragraph">the file that you wanted. I don&#8217;t know. So I&#8217;m going to do dash O main, and that&#8217;s going to give</p>



<p class="wp-block-paragraph">So if I remove a.out just to keep my area clean, I can now do compile.</p>



<p class="wp-block-paragraph">And if I list again, notice how I&#8217;ve got main.</p>



<p class="wp-block-paragraph">So I can execute main and it&#8217;s the same program that we already had.</p>



<p class="wp-block-paragraph">In fact, maybe sometimes just as a shortcut since this is a script,</p>



<p class="wp-block-paragraph">what if I&#8217;m always hitting ls-la, I always want to list the folder after I compile.</p>



<p class="wp-block-paragraph">I could just put that into the script.</p>



<p class="wp-block-paragraph">The more commands you put into the script, the more convenient things can get, unless</p>



<p class="wp-block-paragraph">I guess it gets too messy.</p>



<p class="wp-block-paragraph">So I&#8217;m going to do, I&#8217;m going to list first, and I&#8217;m going to remove main, just to prove</p>



<p class="wp-block-paragraph">to you that we&#8217;re doing this from scratch, and then I&#8217;ll do compile.</p>



<p class="wp-block-paragraph">Notice how it compiles and then lists everything for me.</p>



<p class="wp-block-paragraph">Seems like it didn&#8217;t actually turn the compile green.</p>



<p class="wp-block-paragraph">That&#8217;s probably another flag that we have to stick in there.</p>



<p class="wp-block-paragraph">Let me see if I can do that real fast.</p>



<p class="wp-block-paragraph">If not, I won&#8217;t waste time.</p>



<p class="wp-block-paragraph">This is not an LS video.</p>



<p class="wp-block-paragraph">probably color or colors is what I got to do. Let me just do that one more time. Yeah, okay. So</p>



<p class="wp-block-paragraph">that&#8217;s just a flag on LS. That&#8217;s not what this video is about. So now we&#8217;re compiling directly</p>



<p class="wp-block-paragraph">to main, but here&#8217;s the problem though. We&#8217;re actually kind of sort of skipping over some</p>



<p class="wp-block-paragraph">steps that we could be aware of. When we compile our program, we&#8217;re actually taking our source code</p>



<p class="wp-block-paragraph">and we&#8217;re assembling it down to assembly code under the hood, and then we&#8217;re compiling it to</p>



<p class="wp-block-paragraph">And then we&#8217;re compiling it to an object file, which is sort of just machine code.</p>



<p class="wp-block-paragraph">It&#8217;s like a level lower than assembly.</p>



<p class="wp-block-paragraph">And then as a last step, it&#8217;s getting linked into an executable named main.</p>



<p class="wp-block-paragraph">So we&#8217;re skipping a ton of steps.</p>



<p class="wp-block-paragraph">Really what&#8217;s supposed to be happening is every single source file.</p>



<p class="wp-block-paragraph">Oh, whoops.</p>



<p class="wp-block-paragraph">I&#8217;m on the wrong machine right now.</p>



<p class="wp-block-paragraph">Okay.</p>



<p class="wp-block-paragraph">Where&#8217;s that?</p>



<p class="wp-block-paragraph">Oh, the machine that I&#8217;m recording on.</p>



<p class="wp-block-paragraph">There we go.</p>



<p class="wp-block-paragraph">CPP and we&#8217;re thinking that it just goes directly to main you know that works if you only have one source file or if you don&#8217;t want to have</p>



<p class="wp-block-paragraph">fine-tuned control over your compiling</p>



<p class="wp-block-paragraph">But I would like to do something a little bit more detailed. Let&#8217;s compile our</p>



<p class="wp-block-paragraph">CPP source file into an object file. I&#8217;ll name it first.o and then we&#8217;ll link that into main</p>



<p class="wp-block-paragraph">Why is that useful because later when we have multiple source files?</p>



<p class="wp-block-paragraph">We might we might want to compile them all to object files first and then link them all together in one step into main</p>



<p class="wp-block-paragraph">all together in one step into main this will be even more useful when you later realize that you</p>



<p class="wp-block-paragraph">can use a build system that does kind of the same thing to save compilation time i won&#8217;t talk too</p>



<p class="wp-block-paragraph">much about make files here but let me just tell you when you start using a build system</p>



<p class="wp-block-paragraph">your compile time can be drastically reduced uh story i used to have a program a long time ago</p>



<p class="wp-block-paragraph">that i wrote myself took like an hour to compile i would literally every little change i made i&#8217;d</p>



<p class="wp-block-paragraph">have to get up and leave and go make a sandwich or something and then come back by the time it</p>



<p class="wp-block-paragraph">back by the time it was finished compiling when i started using object files as intermediate steps</p>



<p class="wp-block-paragraph">and upgraded to a build system i was using gnu make then my compile time went down to like a</p>



<p class="wp-block-paragraph">minute each time so it was like so much more efficient anyway let&#8217;s do that let&#8217;s do that now</p>



<p class="wp-block-paragraph">let&#8217;s let&#8217;s uh let&#8217;s nano our compile script and you just need another flag in here for the gcc</p>



<p class="wp-block-paragraph">an object file and not actually produce a fully linked executable that you can execute that means</p>



<p class="wp-block-paragraph">i should probably rename main to something else because main is supposed to be my executable at</p>



<p class="wp-block-paragraph">least that&#8217;s what i&#8217;ve chosen i would like to have every object file be named similarly to the source</p>



<p class="wp-block-paragraph">file it belongs to so i&#8217;m going to type the output now is going to be first.o and then the c flag</p>



<p class="wp-block-paragraph">just says only compile don&#8217;t link this means of course that i&#8217;m not going to produce a program</p>



<p class="wp-block-paragraph">a program that I can run. Let&#8217;s see, I&#8217;m gonna end up accidentally deleting the</p>



<p class="wp-block-paragraph">compiler. Okay, but we can see what we&#8217;re doing. Okay, so compile and CPP for first.</p>



<p class="wp-block-paragraph">So I&#8217;m gonna compile it and now notice how we have an object file and no main</p>



<p class="wp-block-paragraph">and just the source file and its corresponding object file. So I can&#8217;t run</p>



<p class="wp-block-paragraph">this program. I have to do the additional linking step now. So I&#8217;m gonna do</p>



<p class="wp-block-paragraph">step now so I&#8217;m going to do compile and in my editor here let&#8217;s see where is my</p>



<p class="wp-block-paragraph">command that I wanted to do for linking okay so I&#8217;m going to link with the the</p>



<p class="wp-block-paragraph">GCC libraries I&#8217;m gonna use the G++ command I&#8217;m just gonna put a couple of</p>



<p class="wp-block-paragraph">flags I&#8217;ll explain them real fast I&#8217;m gonna say that I want a 64-bit</p>



<p class="wp-block-paragraph">executable which you probably actually don&#8217;t have to specify I&#8217;m going to</p>



<p class="wp-block-paragraph">anyway and then I&#8217;m gonna tell it again that I want it to link up against the</p>



<p class="wp-block-paragraph">23 libraries. This is important if multiple modules reference the C++ libraries. I&#8217;m not</p>



<p class="wp-block-paragraph">even sure. I think I need it in both the compiling command and the linking command. I&#8217;m going to</p>



<p class="wp-block-paragraph">leave it in both because it should work. Usually if you want to embed debugging symbols into your</p>



<p class="wp-block-paragraph">program, you want to put a dash G there, which is a great idea. But we&#8217;re not going to talk about</p>



<p class="wp-block-paragraph">debugging in this video. You can also specify the type of debugging symbols that you want.</p>



<p class="wp-block-paragraph">symbols that you want I&#8217;m just gonna put that in here right now G dwarf 2 and</p>



<p class="wp-block-paragraph">then there&#8217;s some new flags that you kind of need there&#8217;s one here called no</p>



<p class="wp-block-paragraph">PI which I think is just an Ubuntu thing your program probably won&#8217;t execute if</p>



<p class="wp-block-paragraph">you don&#8217;t do that just just put it in there and trust me there&#8217;s another one</p>



<p class="wp-block-paragraph">that we have to put in now called no exec stack which basically prevents your</p>



<p class="wp-block-paragraph">program from executing code sitting inside of the stack which is just sort</p>



<p class="wp-block-paragraph">not get hacked hopefully it&#8217;s not bulletproof but you know it might help</p>



<p class="wp-block-paragraph">and then i&#8217;m going to give it input files the input files for the linking</p>



<p class="wp-block-paragraph">stage maybe i should do a comment linking stage</p>



<p class="wp-block-paragraph">compile the c++ program or source or something</p>



<p class="wp-block-paragraph">for the linking stage uh you want to you want to provide all of the object files</p>



<p class="wp-block-paragraph">that you&#8217;re going to link together in this case we just have you know</p>



<p class="wp-block-paragraph">file named first and then we can specify the output file as main if we didn&#8217;t</p>



<p class="wp-block-paragraph">specify it would probably be a dot out like we saw before so if I got this</p>



<p class="wp-block-paragraph">right let&#8217;s see if I did now this should compile the C++ source code file into</p>



<p class="wp-block-paragraph">its corresponding object file and then scoop up the object file turn it into an</p>



<p class="wp-block-paragraph">executable okay so my clear and then list and then I&#8217;m gonna remove the object</p>



<p class="wp-block-paragraph">put star dot o which is a shell trick to basically say that that star dot o is</p>



<p class="wp-block-paragraph">going to expand to really mean every file that ends in dot o so you know in</p>



<p class="wp-block-paragraph">this case it&#8217;s actually just gonna end up being first dot o but it&#8217;s a trick you</p>



<p class="wp-block-paragraph">can use to grab multiple object files or files of a certain type all in one</p>



<p class="wp-block-paragraph">command so I&#8217;m gonna do that and then I&#8217;m gonna list again and then I&#8217;m gonna</p>



<p class="wp-block-paragraph">compile and you can see that the compile command it created first dot o the</p>



<p class="wp-block-paragraph">the object file, and it also created the executable main.</p>



<p class="wp-block-paragraph">So if I execute main, the same thing should happen.</p>



<p class="wp-block-paragraph">So great. We&#8217;re ready to move on to another step.</p>



<p class="wp-block-paragraph">Let me remove star dot O.</p>



<p class="wp-block-paragraph">I got to be so careful because I&#8217;m going to totally ruin this video by erasing the wrong thing.</p>



<p class="wp-block-paragraph">Okay. Because this is, I should be in a Git repository, but I&#8217;m trying to be fast here.</p>



<p class="wp-block-paragraph">Okay. So let&#8217;s nano compile.</p>



<p class="wp-block-paragraph">Let&#8217;s create a second source file actually.</p>



<p class="wp-block-paragraph">Okay. So let me get out of this.</p>



<p class="wp-block-paragraph">Let me get out of this.</p>



<p class="wp-block-paragraph">I want to create a second source file.</p>



<p class="wp-block-paragraph">I&#8217;m going to call it second.c.</p>



<p class="wp-block-paragraph">It&#8217;s going to be a pure C program.</p>



<p class="wp-block-paragraph">And because we don&#8217;t have C++ libraries in C source files,</p>



<p class="wp-block-paragraph">I&#8217;m just going to say, you know,</p>



<p class="wp-block-paragraph">standard input output dot O for the header.</p>



<p class="wp-block-paragraph">I&#8217;m going to make a function called void second probably.</p>



<p class="wp-block-paragraph">And it doesn&#8217;t do anything except just sort of print</p>



<p class="wp-block-paragraph">a little message.</p>



<p class="wp-block-paragraph">So I&#8217;m going to say hello from second.</p>



<p class="wp-block-paragraph">And then do a new line.</p>



<p class="wp-block-paragraph">because we don&#8217;t have endels in c all right i think that&#8217;s probably going to work so then</p>



<p class="wp-block-paragraph">let&#8217;s uh edit our compiling script and we&#8217;ll just add another line here to compile the c source</p>



<p class="wp-block-paragraph">okay so i&#8217;m going to go gcc because it&#8217;s not c plus plus anymore and um i&#8217;m going to do all</p>



<p class="wp-block-paragraph">warnings and pedantic again uh you don&#8217;t really need even need to worry about that and i&#8217;m going</p>



<p class="wp-block-paragraph">need even need to worry about that and i&#8217;m going to give it an input of second.c because that&#8217;s the</p>



<p class="wp-block-paragraph">file we just worked on and i&#8217;m going to tell it i only want you to compile with the dash c flag if i</p>



<p class="wp-block-paragraph">forgot that on this one then i&#8217;ll probably end up with an object file a main and then an a.out that&#8217;s</p>



<p class="wp-block-paragraph">only based on the c source code and that probably wouldn&#8217;t work because the second source code</p>



<p class="wp-block-paragraph">doesn&#8217;t have a main function inside of it so um let&#8217;s see we&#8217;ll do output and it&#8217;s going to be</p>



<p class="wp-block-paragraph">and then I think that should be okay you know something that I just realized I</p>



<p class="wp-block-paragraph">should mention too is when you&#8217;re making hybrid programs like this or or</p>



<p class="wp-block-paragraph">programs where there are just many many source files like I said before you</p>



<p class="wp-block-paragraph">don&#8217;t need to have a source code named main.cpp you can name all your source</p>



<p class="wp-block-paragraph">codes anything that you want the one thing that you really do need is a piece</p>



<p class="wp-block-paragraph">of source code oh I&#8217;m editing inside the terminal suddenly sorry I got</p>



<p class="wp-block-paragraph">Sorry, I got sidetracked.</p>



<p class="wp-block-paragraph">I wanted to edit all this stuff inside of Genie because it&#8217;s a little bit better.</p>



<p class="wp-block-paragraph">Maybe it doesn&#8217;t matter.</p>



<p class="wp-block-paragraph">So notice how we have the main function inside of our first.cpp program,</p>



<p class="wp-block-paragraph">and then there&#8217;s no main function inside of our other source codes.</p>



<p class="wp-block-paragraph">For your program that uses the GCC libraries, you need exactly one instance of main.</p>



<p class="wp-block-paragraph">so that means um you know you have to have at least one and if you have more than one it&#8217;s</p>



<p class="wp-block-paragraph">probably not going to link so if i put a main inside of here probably not going to work</p>



<p class="wp-block-paragraph">this also kind of means you can arrange your source code in a funny way you could</p>



<p class="wp-block-paragraph">have a bunch of different pieces of source code that you could consider maybe libraries maybe</p>



<p class="wp-block-paragraph">there are a bunch of classes or something that will help your program and you can release multiple</p>



<p class="wp-block-paragraph">different executable files by just having different source code files each with their own</p>



<p class="wp-block-paragraph">mains and linked separately against the object files that contain your library or classes or</p>



<p class="wp-block-paragraph">whatever that&#8217;s just a little side tip but um anyway let&#8217;s see if i actually did this right</p>



<p class="wp-block-paragraph">we&#8217;re linking this and i&#8217;m going to see if it works i get stuck inside of the terminal i start</p>



<p class="wp-block-paragraph">thinking i&#8217;m going to edit everything inside there okay so it compiled well let me let me</p>



<p class="wp-block-paragraph">remove the main and then just clear it again in the list okay so now I&#8217;m gonna</p>



<p class="wp-block-paragraph">run the compiler and after the compiler ran everything seemed to be okay it</p>



<p class="wp-block-paragraph">compiled first into its object file and it also compiled second into its object</p>



<p class="wp-block-paragraph">file if I run the main executable notice how it only prints hello from first dot</p>



<p class="wp-block-paragraph">CPP well what about the second one that we just added well what we need to do is</p>



<p class="wp-block-paragraph">improve our linking stage notice how in the linking stage right here we just</p>



<p class="wp-block-paragraph">stage right here we just only link first.o so we should also include the other object file that we</p>



<p class="wp-block-paragraph">want i&#8217;m going to glob that in a second but uh let me show you compile now if we run main</p>



<p class="wp-block-paragraph">what did i do wrong hold on let&#8217;s see here second.c oh i forgot to actually call the second</p>



<p class="wp-block-paragraph">function so that&#8217;s cool just uh wrote a bunch of stuff for no reason at all let me uh let me have</p>



<p class="wp-block-paragraph">have the main function call on the second function we&#8217;re going to encounter an issue here too the</p>



<p class="wp-block-paragraph">second function is written in c and the first function uh its its source code is in c plus plus</p>



<p class="wp-block-paragraph">and the thing is c plus plus uses something called name mangling i won&#8217;t go over it too much in this</p>



<p class="wp-block-paragraph">video but it uses name mangling so you can overload functions you can have functions with the same</p>



<p class="wp-block-paragraph">name but like different signatures right so it&#8217;ll go in there and it&#8217;ll mangle the name of a function</p>



<p class="wp-block-paragraph">the name of a function according to its overload prototype so that we can actually call overloaded</p>



<p class="wp-block-paragraph">functions but we don&#8217;t really have that in C so that means if we try to call a C function</p>



<p class="wp-block-paragraph">from C++ we&#8217;re probably going to end up calling the wrong name because there&#8217;s the expectation</p>



<p class="wp-block-paragraph">that the other module has name mangling enabled but it&#8217;s not because it&#8217;s C so I just have</p>



<p class="wp-block-paragraph">to do this little block extern C and just name all the stuff that is not going to be</p>



<p class="wp-block-paragraph">name mangled i&#8217;ll just provide the prototypes of all the functions that i want to be able to call</p>



<p class="wp-block-paragraph">so just void second if you look here again we just have void second and now when um when the c plus</p>



<p class="wp-block-paragraph">plus module calls on second it won&#8217;t expect name mangling so it should work so we&#8217;re calling it</p>



<p class="wp-block-paragraph">and then we linked it now everything i think should be okay i&#8217;m going to compile one more time</p>



<p class="wp-block-paragraph">and then i&#8217;m going to run the main program and now you see that we have both calls in there</p>



<p class="wp-block-paragraph">Awesome. Let&#8217;s give this script a little bit more output though. This is kind of</p>



<p class="wp-block-paragraph">precarious. What if something failed inside of there? Maybe you don&#8217;t want the script to continue</p>



<p class="wp-block-paragraph">if something fails and maybe you kind of want to know where the script is at while it&#8217;s compiling.</p>



<p class="wp-block-paragraph">So I&#8217;m going to upgrade it just a little bit. So compile the source. I&#8217;m just going to say echo</p>



<p class="wp-block-paragraph">compiling the, you know, compiling burst.cbp. And then here I&#8217;ll just do another echo and I&#8217;ll say</p>



<p class="wp-block-paragraph">and then here I&#8217;ll say echo linking the executable or the binary aka the binary</p>



<p class="wp-block-paragraph">I don&#8217;t even know if that&#8217;s the proper term I just like to call these things binaries because</p>



<p class="wp-block-paragraph">they&#8217;re not in text anyway so if we do another I&#8217;m going to do I&#8217;m going to do a compound</p>



<p class="wp-block-paragraph">command line here I&#8217;m going to go clear and compile so that I don&#8217;t have to continue to</p>



<p class="wp-block-paragraph">continue to clear every single time. Now notice how it prints what it&#8217;s doing. So if it kind of</p>



<p class="wp-block-paragraph">failed somewhere along the way, or if it took a long time, you&#8217;d kind of know what&#8217;s going on.</p>



<p class="wp-block-paragraph">Now let&#8217;s use a little bash trick, because remember this is a bash script. It&#8217;s not just</p>



<p class="wp-block-paragraph">going to only execute actual commands. I&#8217;m going to say or exit with the return code of the previous</p>



<p class="wp-block-paragraph">We&#8217;re going to try to execute G++.</p>



<p class="wp-block-paragraph">If G++ fails for some reason, then the whole program or the whole script is going to exit</p>



<p class="wp-block-paragraph">and it&#8217;s going to exit with the return code mentioned here.</p>



<p class="wp-block-paragraph">I think it&#8217;s not going to print it.</p>



<p class="wp-block-paragraph">I&#8217;ll show you how to print it in a second, but we can actually make a function called</p>



<p class="wp-block-paragraph">die up here in a second, which might be better.</p>



<p class="wp-block-paragraph">So I&#8217;m going to do the same thing over here.</p>



<p class="wp-block-paragraph">I&#8217;m going to say or exit with the return code of the GCC command and then here or exit with</p>



<p class="wp-block-paragraph">And then here or exit with the return code of the linking command.</p>



<p class="wp-block-paragraph">We&#8217;ll do it one more time here.</p>



<p class="wp-block-paragraph">Notice how it seems to work.</p>



<p class="wp-block-paragraph">So now let&#8217;s make the first C++ program not actually compile.</p>



<p class="wp-block-paragraph">I&#8217;ll put a bunch of Zs there.</p>



<p class="wp-block-paragraph">And so if we try to compile it again, it should just quit right after it tries to compile the C++ program.</p>



<p class="wp-block-paragraph">So that saves you some time and helps draw your attention to what went wrong.</p>



<p class="wp-block-paragraph">The C++ compiler is saying hey, there&#8217;s like a type there. It doesn&#8217;t show the exit code though. So let&#8217;s make a function in bash</p>



<p class="wp-block-paragraph">That will die and print an exit code. So I&#8217;m going to say do die or maybe just die by itself something like that</p>



<p class="wp-block-paragraph">Functions whoops. I&#8217;m in the wrong language</p>



<p class="wp-block-paragraph">Let me go back to the compiler</p>



<p class="wp-block-paragraph">make a function in bash</p>



<p class="wp-block-paragraph">Called die and it&#8217;ll take one argument and the argument will just be the exit code</p>



<p class="wp-block-paragraph">that we want it to alert us of.</p>



<p class="wp-block-paragraph">So I&#8217;m going to say echo,</p>



<p class="wp-block-paragraph">I&#8217;m going to say we are dying now.</p>



<p class="wp-block-paragraph">Previous exits code was,</p>



<p class="wp-block-paragraph">maybe I&#8217;ll do a little variable here,</p>



<p class="wp-block-paragraph">I&#8217;ll do a local variable.</p>



<p class="wp-block-paragraph">Exit code is going to be the first incoming argument.</p>



<p class="wp-block-paragraph">This is a bash program, not C++.</p>



<p class="wp-block-paragraph">And then here I can just say exit code.</p>



<p class="wp-block-paragraph">Okay.</p>



<p class="wp-block-paragraph">So then after writing all that,</p>



<p class="wp-block-paragraph">then after writing all that I can say or die and then just you know so I&#8217;m calling</p>



<p class="wp-block-paragraph">a function and the first argument I&#8217;m giving it is the actual exit code of the</p>



<p class="wp-block-paragraph">thing that died so we&#8217;re just kind of like using indirection to print a pretty</p>



<p class="wp-block-paragraph">pretty message to get a little bit more information so we&#8217;ll try that one more</p>



<p class="wp-block-paragraph">time notice how it says we are dying now previous exit code was one so that just</p>



<p class="wp-block-paragraph">that just tells us that gcc exited with the exit code of one not super useful but it&#8217;s nice to see</p>



<p class="wp-block-paragraph">could be something else entirely like i don&#8217;t know maybe uh maybe we misname the first source</p>



<p class="wp-block-paragraph">code file let&#8217;s see if the code changes i think it probably will nope maybe this exit code was one</p>



<p class="wp-block-paragraph">no such file or directory all right exit code am i doing something wrong i guess i&#8217;m all right</p>



<p class="wp-block-paragraph">Alright, okay, so let&#8217;s just move on.</p>



<p class="wp-block-paragraph">I wanted it to be fancier.</p>



<p class="wp-block-paragraph">We could also upgrade a little bit if we wanted to.</p>



<p class="wp-block-paragraph">Instead of echoing, I could make a function called log here,</p>



<p class="wp-block-paragraph">and I could just say that the log command just echoes,</p>



<p class="wp-block-paragraph">and maybe we&#8217;ll put a cute label, and we&#8217;ll say, you know,</p>



<p class="wp-block-paragraph">my super cool program compiler, or how about super cool compiler?</p>



<p class="wp-block-paragraph">And then just do the $1, because that&#8217;s the first incoming argument that you get.</p>



<p class="wp-block-paragraph">the first incoming argument that you get now I can type log instead of echo so</p>



<p class="wp-block-paragraph">that&#8217;s nice and why would you want to do this well now it&#8217;s gonna go inside of a</p>



<p class="wp-block-paragraph">function which means later on you can start adding extra commands like logger</p>



<p class="wp-block-paragraph">a log to the system daemon oh no so this would write to your system log in</p>



<p class="wp-block-paragraph">addition to printing something you could do anything you want you know it&#8217;s just</p>



<p class="wp-block-paragraph">like a regular program adding more power by sticking everything inside of</p>



<p class="wp-block-paragraph">functions I&#8217;m gonna put log here and let&#8217;s see if it works</p>



<p class="wp-block-paragraph">let&#8217;s see if it works pile okay nice notice how we have a little message with a little header which</p>



<p class="wp-block-paragraph">seems pretty cool maybe instead of echoing here let&#8217;s make a complaint function let&#8217;s do a</p>



<p class="wp-block-paragraph">complain and same thing as the echo function but maybe I think there&#8217;s a flag where you can tell</p>



<p class="wp-block-paragraph">echo to print to standard error instead of standard output but I&#8217;m just going to do it the easy way</p>



<p class="wp-block-paragraph">it the easy way I&#8217;m going to say one goes to two so that the the first pipe standard output is going</p>



<p class="wp-block-paragraph">to be redirected into the second pipe standard error so now I can use complain instead of um</p>



<p class="wp-block-paragraph">echo down here complain before I die oh I forgot to actually</p>



<p class="wp-block-paragraph">or die I forgot to actually exit so that means it was oh yeah it&#8217;s still compiling everything</p>



<p class="wp-block-paragraph">Let me fix that.</p>



<p class="wp-block-paragraph">So in the actual die function, hopefully you were at home cringing along with me.</p>



<p class="wp-block-paragraph">Let&#8217;s see, let&#8217;s exit with the actual exit code that was specified just to make things</p>



<p class="wp-block-paragraph">a little bit more automatable.</p>



<p class="wp-block-paragraph">Yeah, now it stops right away.</p>



<p class="wp-block-paragraph">And the complainer is like, you know, pretty cool.</p>



<p class="wp-block-paragraph">Let me fix the first source code real fast.</p>



<p class="wp-block-paragraph">And we&#8217;ll say that the second source code is broken.</p>



<p class="wp-block-paragraph">We&#8217;ll put a bunch of Z&#8217;s there.</p>



<p class="wp-block-paragraph">So now the first one compiles, the second one, it breaks.</p>



<p class="wp-block-paragraph">And we&#8217;re starting to, you know, get a hang for a compiler script that&#8217;s a lot more sophisticated</p>



<p class="wp-block-paragraph">than just typing things on the command line, right?</p>



<p class="wp-block-paragraph">You can take this as far as you want.</p>



<p class="wp-block-paragraph">The next step above this is using a proper build engine.</p>



<p class="wp-block-paragraph">So let&#8217;s see.</p>



<p class="wp-block-paragraph">Well, let&#8217;s do another source code.</p>



<p class="wp-block-paragraph">an assembly source code. So we&#8217;ll say third dot ASM and it&#8217;s just going to be Yasm. This is not</p>



<p class="wp-block-paragraph">a Yasm or assembly tutorial. I&#8217;m just showing you how to, you know, build a, you know, compile,</p>



<p class="wp-block-paragraph">link and execute. We&#8217;ll make a data section so I can just stick some defines in here real fast.</p>



<p class="wp-block-paragraph">I&#8217;ll say system write is going to be equal to a one and then the file descriptor for std out</p>



<p class="wp-block-paragraph">Do you ever feel like red jumps out at you on the screen?</p>



<p class="wp-block-paragraph">Does that mean I have eye problems?</p>



<p class="wp-block-paragraph">I am kind of old now.</p>



<p class="wp-block-paragraph">I don&#8217;t know.</p>



<p class="wp-block-paragraph">Hello from third dot ASM and I&#8217;ll just put a little new line there.</p>



<p class="wp-block-paragraph">So I don&#8217;t have to do it elsewhere.</p>



<p class="wp-block-paragraph">And then I&#8217;ll have to specify the magic message length because this is not a sophisticated program that can loop through the message looking for a null terminator.</p>



<p class="wp-block-paragraph">And then I&#8217;ll do a text section, which is where your code goes.</p>



<p class="wp-block-paragraph">which is where your code goes and you know in hybrid programs if I have this</p>



<p class="wp-block-paragraph">third function which is marked as a which is marked using a label and then</p>



<p class="wp-block-paragraph">in return statement I still have to mark a third as being accessible outside</p>



<p class="wp-block-paragraph">this module so I&#8217;m going to do the global statement and then I&#8217;m just going</p>



<p class="wp-block-paragraph">to do really quickly I&#8217;m going to move something into RAX it&#8217;s going to be</p>



<p class="wp-block-paragraph">system right so basically I would like to write something somewhere to some</p>



<p class="wp-block-paragraph">some file descriptor its first argument wants where I want to write it so I&#8217;m</p>



<p class="wp-block-paragraph">gonna write it to the standard output instead of an actual file and then I&#8217;m</p>



<p class="wp-block-paragraph">gonna move RSI the next argument is going to be the actual message or a</p>



<p class="wp-block-paragraph">pointer to the message that I defined above and you know check out my other</p>



<p class="wp-block-paragraph">videos if you want to learn assembly that&#8217;s this is not this is not an</p>



<p class="wp-block-paragraph">assembly learning video I&#8217;m just kind of doing it real fast in case you&#8217;re</p>



<p class="wp-block-paragraph">probably unless I ruined it somehow let&#8217;s start compiling or assembling the</p>



<p class="wp-block-paragraph">assembly program so just one more we&#8217;ll say assemble the assembly source or</p>



<p class="wp-block-paragraph">module or whatever you want to call it we&#8217;re going to use the ASM to do that</p>



<p class="wp-block-paragraph">G dwarf 2 is the debugging symbols not necessary in this video the format is I</p>



<p class="wp-block-paragraph">want a 64-bit executable that&#8217;s how you do it in Yasm and I&#8217;m going to give it</p>



<p class="wp-block-paragraph">and I&#8217;m going to give it an input of just the third assembly source file.</p>



<p class="wp-block-paragraph">And I want it to output the third object file.</p>



<p class="wp-block-paragraph">I got to remind myself to glob line 36 after this or die with the exit code.</p>



<p class="wp-block-paragraph">Okay. So now this might work.</p>



<p class="wp-block-paragraph">Oh, I got to put, I got to put third dot O in the linking stage.</p>



<p class="wp-block-paragraph">I&#8217;m going to do clear and I&#8217;m going to go here and compile.</p>



<p class="wp-block-paragraph">and compile.</p>



<p class="wp-block-paragraph">Unrecognized debug format, dward.</p>



<p class="wp-block-paragraph">Why didn&#8217;t you stop me?</p>



<p class="wp-block-paragraph">It&#8217;s g dwarf.</p>



<p class="wp-block-paragraph">Let&#8217;s see, what did I do wrong?</p>



<p class="wp-block-paragraph">g dwarf, oh, dwarf dash two.</p>



<p class="wp-block-paragraph">Oh, it&#8217;s not even g, it&#8217;s just dwarf two.</p>



<p class="wp-block-paragraph">Sorry.</p>



<p class="wp-block-paragraph">And I got to put a dash, I think, for EASM.</p>



<p class="wp-block-paragraph">Yeah, okay.</p>



<p class="wp-block-paragraph">Let&#8217;s see if that works.</p>



<p class="wp-block-paragraph">What did I do wrong?</p>



<p class="wp-block-paragraph">Oh no no, Yasm doesn&#8217;t need the dash, but GCC wants the dash.</p>



<p class="wp-block-paragraph">Okay, I had it right the first time.</p>



<p class="wp-block-paragraph">Doop doop doop, okay, so it probably linked.</p>



<p class="wp-block-paragraph">Let me run it and see what happened.</p>



<p class="wp-block-paragraph">Hello from the second, oh, we still have to actually call it from the C++ module.</p>



<p class="wp-block-paragraph">So now we&#8217;ll do the same thing.</p>



<p class="wp-block-paragraph">We&#8217;ll say that the assembly module, it&#8217;s not going to use name mangling.</p>



<p class="wp-block-paragraph">use name mangling there&#8217;s no overloads so we&#8217;re going to put it into the extern C</p>



<p class="wp-block-paragraph">block and we&#8217;ll just say you know void third how do we know that&#8217;s that it&#8217;s a</p>



<p class="wp-block-paragraph">void function with no arguments well you know that&#8217;s just a convenience that</p>



<p class="wp-block-paragraph">happens under the hood with C and C++ in assembly it&#8217;s just a label that you&#8217;re</p>



<p class="wp-block-paragraph">jumping to and returning from void means return type all that means is I&#8217;m</p>



<p class="wp-block-paragraph">trying my best not to make this a C++ or an assembly tutorial but all that</p>



<p class="wp-block-paragraph">All that means is there is no return value getting returned.</p>



<p class="wp-block-paragraph">That would be the RAX register if I wanted to move something in there.</p>



<p class="wp-block-paragraph">And then similarly, if I wanted to catch arguments, I&#8217;d be grabbing from other registers.</p>



<p class="wp-block-paragraph">So I&#8217;m not grabbing any register, any arguments from registers, and I&#8217;m not sending a value back with RAX or XMM0 or anything.</p>



<p class="wp-block-paragraph">So that just means it&#8217;s void and no arguments.</p>



<p class="wp-block-paragraph">So let&#8217;s do it again.</p>



<p class="wp-block-paragraph">Compile it and then run main.</p>



<p class="wp-block-paragraph">then run main hello from second oh i sure put in the name mingling thing but i forgot to call it</p>



<p class="wp-block-paragraph">let me do that third so now one more time compile and then run it</p>



<p class="wp-block-paragraph">what&#8217;s happening in here</p>



<p class="wp-block-paragraph">third i guess i should probably open this up now third asm so global third and then third oh</p>



<p class="wp-block-paragraph">important part of a system call you got to put the keyword or the instruction system call in there</p>



<p class="wp-block-paragraph">one more time i hope you&#8217;re laughing and cringing at home trying to make this more fun maybe i don&#8217;t</p>



<p class="wp-block-paragraph">know okay so now it&#8217;s all it&#8217;s all working notice how we have a bunch of different modules in</p>



<p class="wp-block-paragraph">different languages we&#8217;re compiling them all to object files first and we&#8217;re uh we&#8217;re linking</p>



<p class="wp-block-paragraph">them all into one executable and we&#8217;re doing this in a really really easy way by using a script</p>



<p class="wp-block-paragraph">script instead of typing all the commands out manually. Pretty convenient, right? Let me give</p>



<p class="wp-block-paragraph">you another upgrade real fast here. So this is kind of irritating. I have like all of these object</p>



<p class="wp-block-paragraph">files that I have to specify. What if I forget to specify some? Since this line 36 is just going to</p>



<p class="wp-block-paragraph">be something that goes directly, you know, to the shell as a command line. This is not like bash</p>



<p class="wp-block-paragraph">wildcards I can say star dot o meaning I&#8217;m going to give every object file to</p>



<p class="wp-block-paragraph">the linker as an input file and that should just work automatically keep in</p>



<p class="wp-block-paragraph">mind if you want to use an advanced strategy like I mentioned earlier where</p>



<p class="wp-block-paragraph">you have multiple different object files and they&#8217;ve each got their own main</p>



<p class="wp-block-paragraph">functions because they&#8217;re each going to be their own separate executables then</p>



<p class="wp-block-paragraph">globbing all object files probably not going to work you might want to organize</p>



<p class="wp-block-paragraph">files or stick the object files for executables in like their own folders or</p>



<p class="wp-block-paragraph">something like that we&#8217;re not going to go that far but I just want you to know</p>



<p class="wp-block-paragraph">so let&#8217;s do that again real fast here we&#8217;ll say clear and compile and I&#8217;ll</p>



<p class="wp-block-paragraph">just try to like run main assuming that the compiler works yes so it worked</p>



<p class="wp-block-paragraph">everything compiled by the way this and end that means only execute the next</p>



<p class="wp-block-paragraph">So if I, you know, clear is obviously going to succeed and then compile, it might succeed</p>



<p class="wp-block-paragraph">or not.</p>



<p class="wp-block-paragraph">Then I&#8217;m going to try to run main.</p>



<p class="wp-block-paragraph">Let me actually, let me actually do that real fast.</p>



<p class="wp-block-paragraph">Let me show you what I&#8217;m talking about.</p>



<p class="wp-block-paragraph">I&#8217;m going to remove main.</p>



<p class="wp-block-paragraph">You&#8217;re okay.</p>



<p class="wp-block-paragraph">So then I&#8217;m going to do clear and compile and main.</p>



<p class="wp-block-paragraph">So obviously if everything works, then everything works.</p>



<p class="wp-block-paragraph">But if I do that again, where something is not actually going to compile, like maybe</p>



<p class="wp-block-paragraph">the second the c source program is not going to compile by putting some z&#8217;s in there then the</p>



<p class="wp-block-paragraph">dot main or yeah that part shouldn&#8217;t get executed so it won&#8217;t try to execute our program</p>



<p class="wp-block-paragraph">notice how it just stops so that&#8217;s convenient if i do it again though with a different command</p>



<p class="wp-block-paragraph">if i chain instead of between the commands instead of putting and end to say one must execute before</p>



<p class="wp-block-paragraph">semicolon then they will all execute no matter whether whether they succeeded or not maybe in</p>



<p class="wp-block-paragraph">certain things you&#8217;re doing you actually want that behavior but in this case it&#8217;s going to be bad</p>



<p class="wp-block-paragraph">watch what happens it fails to compile and then at the end it also tries to run a program that</p>



<p class="wp-block-paragraph">doesn&#8217;t exist because it never got linked because the script stopped so i&#8217;m just letting you know</p>



<p class="wp-block-paragraph">you have lots of different options when you&#8217;re writing these scripts we do it again and then let</p>



<p class="wp-block-paragraph">And I think, well, maybe there&#8217;s one other thing I want you to know.</p>



<p class="wp-block-paragraph">Sometimes these lines get really, really long, right?</p>



<p class="wp-block-paragraph">And it&#8217;s messy and irritating.</p>



<p class="wp-block-paragraph">At least for me.</p>



<p class="wp-block-paragraph">Maybe if it goes off the side of the screen for you, it&#8217;s like, oh, that&#8217;s awful.</p>



<p class="wp-block-paragraph">You can actually just separate a command across multiple lines with a backslash.</p>



<p class="wp-block-paragraph">Or sorry, I think that is called a forward slash.</p>



<p class="wp-block-paragraph">It&#8217;s the drive slash.</p>



<p class="wp-block-paragraph">I don&#8217;t remember forward slash backslash.</p>



<p class="wp-block-paragraph">I just say web slash and drive slash.</p>



<p class="wp-block-paragraph">So I&#8217;m going to do G++ and do a little slash.</p>



<p class="wp-block-paragraph">Then I can put the whole rest of the thing on the next line.</p>



<p class="wp-block-paragraph">If I wanted to, I could start separating these out by category.</p>



<p class="wp-block-paragraph">So maybe I&#8217;m like, oh, I don&#8217;t know.</p>



<p class="wp-block-paragraph">Maybe M64 on its own line.</p>



<p class="wp-block-paragraph">And then I&#8217;ll keep these other two on their own line.</p>



<p class="wp-block-paragraph">And maybe these two on their own line.</p>



<p class="wp-block-paragraph">You know, it&#8217;s up to you how you organize this.</p>



<p class="wp-block-paragraph">And then I&#8217;ll say, give me all the input files on its own line.</p>



<p class="wp-block-paragraph">And then the output file on another line.</p>



<p class="wp-block-paragraph">on another line you could even put the or bars for the die part on its own line I</p>



<p class="wp-block-paragraph">guess I&#8217;ll just try that and now let&#8217;s just make sure that this still compiles</p>



<p class="wp-block-paragraph">let me remove star dot o and remove main it&#8217;s not there okay so clear and then</p>



<p class="wp-block-paragraph">list and then two showed up somewhere oh my mistake I probably should have caught</p>



<p class="wp-block-paragraph">some file got saved as a two I should have noticed okay so let&#8217;s do clear and</p>



<p class="wp-block-paragraph">compile and I just run main okay so now everything works notice how even though</p>



<p class="wp-block-paragraph">everything was on a different line it all just seemed to work cool what about</p>



<p class="wp-block-paragraph">something here on line 38 we&#8217;re using the C++23 standard but we&#8217;re also doing</p>



<p class="wp-block-paragraph">that up here and we&#8217;re also using the all warnings flag up here and down here</p>



<p class="wp-block-paragraph">and pedantic also and even the linker like what if I want to put all warnings</p>



<p class="wp-block-paragraph">into the linker we could make variables in bash and by the way this is totally</p>



<p class="wp-block-paragraph">starting to become a job for for mark for make files but for now we&#8217;re just</p>



<p class="wp-block-paragraph">stay in bash so I can make a variable in bash I can say maybe GCC flags name it something and I&#8217;ll</p>



<p class="wp-block-paragraph">just put equals whatever and I&#8217;ll just type the stuff that I know is going to appear in every</p>



<p class="wp-block-paragraph">single invocation of G++ or GCC so I&#8217;m going to go wall because I want to put that everywhere</p>



<p class="wp-block-paragraph">and I&#8217;m going to say C++23 because I&#8217;m going to put that everywhere and probably pedantic I&#8217;m</p>



<p class="wp-block-paragraph">I&#8217;m not going to put the dash C flag everywhere because notice how I&#8217;m only putting that on the compiler commands when I just want to make an object file.</p>



<p class="wp-block-paragraph">But when I&#8217;m linking, there&#8217;s no dash C because that means you got to actually link.</p>



<p class="wp-block-paragraph">So let&#8217;s see.</p>



<p class="wp-block-paragraph">We got G dwarf two, G dwarf two.</p>



<p class="wp-block-paragraph">Maybe I could put G dwarf two there and see if it still works.</p>



<p class="wp-block-paragraph">So I want debugging symbols and I want them to be in dwarf dash two format, which honestly is the default format.</p>



<p class="wp-block-paragraph">which honestly is the default format if you just put dash G.</p>



<p class="wp-block-paragraph">Let&#8217;s see if I can get away with that for now.</p>



<p class="wp-block-paragraph">So I&#8217;m going to go GCC flags.</p>



<p class="wp-block-paragraph">And here I can get rid of C++23 and wall and pedantic.</p>



<p class="wp-block-paragraph">And I&#8217;ll just insert instead GCC flags.</p>



<p class="wp-block-paragraph">And if you want to be extra careful,</p>



<p class="wp-block-paragraph">you can put these little braces around the variable name of that.</p>



<p class="wp-block-paragraph">Probably should be fine.</p>



<p class="wp-block-paragraph">But because I don&#8217;t want to type that again,</p>



<p class="wp-block-paragraph">I&#8217;m just going to copy paste it into the other command.</p>



<p class="wp-block-paragraph">So wall pedantic, just paste that.</p>



<p class="wp-block-paragraph">Just paste that and then I&#8217;m going to leave it alone for Yasm because Yasm is different than GCC.</p>



<p class="wp-block-paragraph">And then here I&#8217;m going to do another line where I just kind of put the flags.</p>



<p class="wp-block-paragraph">That means I can get rid of those two flags.</p>



<p class="wp-block-paragraph">And then wall and pedantic are going to show up.</p>



<p class="wp-block-paragraph">No pi is kind of a linker only thing.</p>



<p class="wp-block-paragraph">So is the no exec stack.</p>



<p class="wp-block-paragraph">I think probably that will be okay.</p>



<p class="wp-block-paragraph">Let&#8217;s see if I got that right or wrong.</p>



<p class="wp-block-paragraph">Oh no.</p>



<p class="wp-block-paragraph">Unrecognized command line option.</p>



<p class="wp-block-paragraph">Wall pedantic GCC.</p>



<p class="wp-block-paragraph">Oh, I guess because I quoted it.</p>



<p class="wp-block-paragraph">I shouldn&#8217;t have done that.</p>



<p class="wp-block-paragraph">Because I quoted it, the command line thought that that was all one argument.</p>



<p class="wp-block-paragraph">I should have remembered.</p>



<p class="wp-block-paragraph">So I&#8217;m going to unquote it right now.</p>



<p class="wp-block-paragraph">This should help the command line understand that all of the things inside of that variable,</p>



<p class="wp-block-paragraph">the GCC flags, are separate arguments instead of just one argument.</p>



<p class="wp-block-paragraph">Let me try one more time.</p>



<p class="wp-block-paragraph">Yay, it seems to work.</p>



<p class="wp-block-paragraph">Okay, we got a warning here.</p>



<p class="wp-block-paragraph">Command line option C++23 is valid for object,</p>



<p class="wp-block-paragraph">but not for C.</p>



<p class="wp-block-paragraph">Okay, so basically I made the mistake</p>



<p class="wp-block-paragraph">of trying to send a C++ object,</p>



<p class="wp-block-paragraph">which is this right here, or sorry,</p>



<p class="wp-block-paragraph">a C++ argument, which is this right here,</p>



<p class="wp-block-paragraph">into a C compiling command.</p>



<p class="wp-block-paragraph">Not great.</p>



<p class="wp-block-paragraph">So what if I did this?</p>



<p class="wp-block-paragraph">I&#8217;ll do C flags, and it will contain only flags for C,</p>



<p class="wp-block-paragraph">for C and then I&#8217;ll have another one for C++ flags</p>



<p class="wp-block-paragraph">which will only contain things that are for C++ compiling so the regular C compiling can take</p>



<p class="wp-block-paragraph">wall and pedantic okay and then it can take the G dwarf 2 okay the C++ flags it can take everything</p>



<p class="wp-block-paragraph">except for um sorry the C++ flags can actually take all of the C flags and then additionally the</p>



<p class="wp-block-paragraph">and then additionally the the c++ 23 flag so i could embed a variable inside of itself i could</p>



<p class="wp-block-paragraph">say c flags or sorry i could embed a variable inside of another variable so that i can just</p>



<p class="wp-block-paragraph">steal all of the flags from the c flags variable stick them also in the c++ flags variable and</p>



<p class="wp-block-paragraph">then just add one more at the end of that hopefully that works i think it probably will</p>



<p class="wp-block-paragraph">here and then use the C flags here just for compiling I could probably name the</p>



<p class="wp-block-paragraph">variable better put the word compile in there or something but I don&#8217;t really</p>



<p class="wp-block-paragraph">want to I think some people put CC to mean like C compiling but I don&#8217;t know I</p>



<p class="wp-block-paragraph">just want to do it this way so we have that and then we&#8217;re gonna do CPP flags</p>



<p class="wp-block-paragraph">like this we could also organize the variables differently so that the we</p>



<p class="wp-block-paragraph">We could have some linker flags, which basically would probably have all of this stuff minus dash C.</p>



<p class="wp-block-paragraph">And then we could have linking flags.</p>



<p class="wp-block-paragraph">No, we can have linking flags that has all that stuff minus the C.</p>



<p class="wp-block-paragraph">And then we&#8217;ll have like some compiler flags that has the linking flags plus the dash C.</p>



<p class="wp-block-paragraph">But I don&#8217;t really want to spend a bunch of time on this now.</p>



<p class="wp-block-paragraph">For now, we&#8217;ll just say we have C flags and CPP flags.</p>



<p class="wp-block-paragraph">And we&#8217;ll just manually add that dash C.</p>



<p class="wp-block-paragraph">But of course, keep in mind, you can organize these things any way you want.</p>



<p class="wp-block-paragraph">and organize these things any way you want.</p>



<p class="wp-block-paragraph">So I&#8217;m gonna do,</p>



<p class="wp-block-paragraph">well, this might actually work.</p>



<p class="wp-block-paragraph">I&#8217;m getting distracted.</p>



<p class="wp-block-paragraph">I think it works.</p>



<p class="wp-block-paragraph">Okay.</p>



<p class="wp-block-paragraph">Let me just double check by removing all the object files.</p>



<p class="wp-block-paragraph">There&#8217;s a two in there still.</p>



<p class="wp-block-paragraph">Let&#8217;s debug that if it happens again,</p>



<p class="wp-block-paragraph">then I&#8217;m gonna remove main.</p>



<p class="wp-block-paragraph">Okay, then I&#8217;m just gonna list everything real fast.</p>



<p class="wp-block-paragraph">Okay, let&#8217;s see.</p>



<p class="wp-block-paragraph">Is there gonna be a two in there now?</p>



<p class="wp-block-paragraph">Is there going to be a two in there now?</p>



<p class="wp-block-paragraph">No, is that only when it fails?</p>



<p class="wp-block-paragraph">Only when something fails to compile?</p>



<p class="wp-block-paragraph">I mean, it&#8217;s working, so that&#8217;s good news.</p>



<p class="wp-block-paragraph">You know what?</p>



<p class="wp-block-paragraph">Leave a comment somewhere letting me know</p>



<p class="wp-block-paragraph">where you think I went wrong with this two,</p>



<p class="wp-block-paragraph">and maybe I&#8217;ll release another video</p>



<p class="wp-block-paragraph">or something in the future,</p>



<p class="wp-block-paragraph">at some distant point in the future.</p>



<p class="wp-block-paragraph">But yeah, anyway, I hope you learned a lot of stuff</p>



<p class="wp-block-paragraph">in this video and you had a little bit of fun too.</p>



<p class="wp-block-paragraph">had a little bit of fun too. This has been, you know, a little intro for compiling, linking</p>



<p class="wp-block-paragraph">and executing in Linux, but also probably on a bunch of other systems if you have these</p>



<p class="wp-block-paragraph">same tools, and you&#8217;re working inside of bash. Thank you so much for watching this video.</p>



<p class="wp-block-paragraph">I&#8217;ll see you in the next one.</p>



<p class="wp-block-paragraph">of my heart i really appreciate it i do hope you did learn something and have some fun if you could</p>



<p class="wp-block-paragraph">do me a please a small little favor could you please subscribe and follow this channel or these</p>



<p class="wp-block-paragraph">videos or whatever it is you do on the current social media website that you&#8217;re looking at right</p>



<p class="wp-block-paragraph">now it would really mean the world to me and it&#8217;ll help make more videos and grow this community so</p>



<p class="wp-block-paragraph">we&#8217;ll be able to do more videos longer videos better videos or just i&#8217;ll be able to keep making</p>



<p class="wp-block-paragraph">Do me a kindness and and subscribe. You know sometimes I&#8217;m sleeping in the middle of the night</p>



<p class="wp-block-paragraph">And I just wake up because I know somebody subscribed or followed it just wakes me up and I get filled with joy</p>



<p class="wp-block-paragraph">That&#8217;s exactly what happens every single time</p>



<p class="wp-block-paragraph">So you could do it as a nice favor to me or you could you could troll me if you want to just wake me up in the middle</p>



<p class="wp-block-paragraph">And I just subscribe and then I&#8217;ll just wake up. I promise that&#8217;s what will happen</p>



<p class="wp-block-paragraph">Also, uh, if you look at the middle of the screen right now</p>



<p class="wp-block-paragraph">You should see a QR code which you can scan in order to go to the website</p>



<p class="wp-block-paragraph">go to the website which i think is also named somewhere at the bottom of this video and it&#8217;ll</p>



<p class="wp-block-paragraph">take you to my main website where you can just kind of like see all the videos i published and</p>



<p class="wp-block-paragraph">the services and tutorials and things that i offer and all that good stuff and uh</p>



<p class="wp-block-paragraph">if you have a suggestion for uh uh clarifications or errata or just future videos that you want to</p>



<p class="wp-block-paragraph">see please leave a comment or if you just want to say hey what&#8217;s up what&#8217;s going on you know just</p>



<p class="wp-block-paragraph">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 class="wp-block-paragraph">it would really it really mean the world to me i would really appreciate it so again thank you so</p>



<p class="wp-block-paragraph">much for watching this video and um enjoy the cool music as as i fade into the darkness which is</p>



<p class="wp-block-paragraph">coming for us all</p>



<p class="wp-block-paragraph">Thank you.</p>
<p>The post <a href="https://www.NeuralLantern.com/compile-link-execute-a-beginners-guide-to-linux-programming-with-c-c-and-assembly/">Compile, Link, Execute: A Beginner’s Guide to Linux Programming with C++, C, and Assembly</a> appeared first on <a href="https://www.NeuralLantern.com">NeuralLantern.com</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.NeuralLantern.com/compile-link-execute-a-beginners-guide-to-linux-programming-with-c-c-and-assembly/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
