<?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>low-level programming Archives - NeuralLantern.com</title>
	<atom:link href="https://www.NeuralLantern.com/tag/low-level-programming/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.NeuralLantern.com/tag/low-level-programming/</link>
	<description></description>
	<lastBuildDate>Mon, 29 Dec 2025 08:55:42 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=7.1.2</generator>

<image>
	<url>https://www.NeuralLantern.com/wp-content/uploads/2025/04/cropped-2025-04-04-Lantern-03-32x32.png</url>
	<title>low-level programming Archives - NeuralLantern.com</title>
	<link>https://www.NeuralLantern.com/tag/low-level-programming/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>x86-64 Assembly Bit Manipulation: AND, OR, XOR, NOT, Shifts &#038; Rotates Tutorial</title>
		<link>https://www.NeuralLantern.com/x86-64-assembly-bit-manipulation-and-or-xor-not-shifts-rotates-tutorial/</link>
					<comments>https://www.NeuralLantern.com/x86-64-assembly-bit-manipulation-and-or-xor-not-shifts-rotates-tutorial/#respond</comments>
		
		<dc:creator><![CDATA[mike]]></dc:creator>
		<pubDate>Mon, 29 Dec 2025 08:55:41 +0000</pubDate>
				<category><![CDATA[Assembly Language]]></category>
		<category><![CDATA[Coding]]></category>
		<category><![CDATA[Computer Science]]></category>
		<category><![CDATA[Videos]]></category>
		<category><![CDATA[AND OR XOR]]></category>
		<category><![CDATA[arithmetic shift]]></category>
		<category><![CDATA[assembly language tutorial]]></category>
		<category><![CDATA[assembly programming]]></category>
		<category><![CDATA[bit manipulation]]></category>
		<category><![CDATA[bit rotation]]></category>
		<category><![CDATA[bit shifting]]></category>
		<category><![CDATA[bitwise operations]]></category>
		<category><![CDATA[computer architecture]]></category>
		<category><![CDATA[gdb debugging]]></category>
		<category><![CDATA[logical shift]]></category>
		<category><![CDATA[low-level programming]]></category>
		<category><![CDATA[two's complement]]></category>
		<category><![CDATA[x86-64 assembly]]></category>
		<category><![CDATA[Yasm assembly]]></category>
		<guid isPermaLink="false">https://www.NeuralLantern.com/?p=272</guid>

					<description><![CDATA[<p>Learn bitwise operations in x86-64 assembly language including AND, OR, XOR, NOT, logical and arithmetic shifts, and bit rotations with practical examples and GDB debugging.</p>
<p>The post <a href="https://www.NeuralLantern.com/x86-64-assembly-bit-manipulation-and-or-xor-not-shifts-rotates-tutorial/">x86-64 Assembly Bit Manipulation: AND, OR, XOR, NOT, Shifts &amp; Rotates Tutorial</a> appeared first on <a href="https://www.NeuralLantern.com">NeuralLantern.com</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<figure class="wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio"><div class="wp-block-embed__wrapper">
<iframe title="x86-64 Assembly Bit Manipulation: AND, OR, XOR, NOT, Shifts &amp; Rotates Tutorial" width="1380" height="776" src="https://www.youtube.com/embed/N8Nciq6l5IU?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 hands-on tutorial, we dive into bit manipulation in x86-64 assembly using Yasm on Ubuntu. We cover the basics of logical operations like AND, OR, XOR, and NOT, then move into shifting (logical vs arithmetic), rotating bits, and why signed vs unsigned matters for shifts.</p>



<p class="wp-block-paragraph">I explain each concept on paper first, then show real code examples running in GDB to prove how the bits change. We look at patterns in registers, see what happens with negative numbers in two&#8217;s complement, and even touch on how shifts can multiply or divide by powers of 2.</p>



<p class="wp-block-paragraph">Perfect if you&#8217;re learning low-level programming, reverse engineering, or just curious about how bits work under the hood. No prior assembly knowledge needed, but it&#8217;ll help if you&#8217;ve seen my other videos.</p>



<p class="wp-block-paragraph">Like and subscribe for more assembly tutorials!</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.</p>



<p class="wp-block-paragraph">In this video, we&#8217;re going to talk all about manipulating bits in x86-64 Yasm assembly within</p>



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



<p class="wp-block-paragraph">But if you&#8217;re not using Yasm or you&#8217;re not on Ubuntu, this video is probably going to</p>



<p class="wp-block-paragraph">be pretty useful to you anyway, because I&#8217;m going to talk about the basic concept of manipulating</p>



<p class="wp-block-paragraph">bits in various ways.</p>



<p class="wp-block-paragraph">We&#8217;re going to talk about some basic logical operators like ANDing, ORing, XORing, and</p>



<p class="wp-block-paragraph">XORing, and then we&#8217;re going to talk about shifting the bits to the left and the right,</p>



<p class="wp-block-paragraph">and then we&#8217;re going to talk about the difference between shifting without any respect for whether</p>



<p class="wp-block-paragraph">the number inside of the bits is an actual integer, or whether we just consider them</p>



<p class="wp-block-paragraph">to just be bits by themselves, or whether we&#8217;re actually respecting the bits as numbers,</p>



<p class="wp-block-paragraph">and also we&#8217;re going to look at the rotation instruction.</p>



<p class="wp-block-paragraph">So first I&#8217;m going to explain this, I&#8217;m going to write it down in a pad, and then I&#8217;m going</p>



<p class="wp-block-paragraph">show you a program that actually does it just so we can prove that it all works.</p>



<p class="wp-block-paragraph">Okay, so for starters, let me open up a little notepad here.</p>



<p class="wp-block-paragraph">What the heck am I even talking about? Let me write this down because I forgot this on my</p>



<p class="wp-block-paragraph">first take I spent like five minutes, blew right past the explanation of and and or and xor because</p>



<p class="wp-block-paragraph">I forgot that that was supposed to be part of this video. We&#8217;re going to look at and or and xor</p>



<p class="wp-block-paragraph">at least for now and then we&#8217;re going to look at shifting and then we&#8217;re going to look at rotating.</p>



<p class="wp-block-paragraph">at rotating. Okay so first off what is anding? I&#8217;ve explained that in another video but long</p>



<p class="wp-block-paragraph">story short and just requires that two bits that you&#8217;re comparing against each other are both a one</p>



<p class="wp-block-paragraph">in order to receive a one and so any other pair like a zero versus a one or one versus a zero or</p>



<p class="wp-block-paragraph">two zeros are just going to be a zero. So and means both bits that you&#8217;re comparing have to be a one</p>



<p class="wp-block-paragraph">The OR is pretty easy.</p>



<p class="wp-block-paragraph">If either of the bits is a 1, then the result is going to be 1.</p>



<p class="wp-block-paragraph">So if I say 1 versus 1, the result is going to be 1.</p>



<p class="wp-block-paragraph">If I say 0 versus 1, the result is going to be a 1.</p>



<p class="wp-block-paragraph">Or if I say 1 versus 0, the result is going to be a 1.</p>



<p class="wp-block-paragraph">In the only case where you end up with a 0, it&#8217;s just going to be 0 versus 0 results in a 0.</p>



<p class="wp-block-paragraph">To upgrade to exclusive OR, basically exactly 1 bit needs to be a 1.</p>



<p class="wp-block-paragraph">bit needs to be a one and if two or zero are a one then the result is a zero which means here when</p>



<p class="wp-block-paragraph">we have the two ones at the top the answer is going to be a zero these are the two stay the</p>



<p class="wp-block-paragraph">same and this one stays the same so that&#8217;s xor again i have another video where i explain this</p>



<p class="wp-block-paragraph">a little bit better but i&#8217;m just doing like a quick overview shifting is pretty interesting</p>



<p class="wp-block-paragraph">because let me do a pattern let&#8217;s say zero one one zero suppose we just have four bits</p>



<p class="wp-block-paragraph">to the left or to the right. So imagine left, if I shift them to the left, then what I&#8217;m really</p>



<p class="wp-block-paragraph">doing is I&#8217;m just taking off that zero on the end and padding on the right with a zero. And then if</p>



<p class="wp-block-paragraph">I&#8217;m shifting to the right, then well, it&#8217;s to the right. I&#8217;m just going to, you know, take off,</p>



<p class="wp-block-paragraph">let&#8217;s see the zero on the right side and pad with the zero on the left. Be careful though, because</p>



<p class="wp-block-paragraph">or logical shifting. I haven&#8217;t actually shifted with respect to the original number. Think</p>



<p class="wp-block-paragraph">about this. That number right there on line three, if we just consider that an unsigned</p>



<p class="wp-block-paragraph">integer or just bits, then what I just did with the shifting is fine. But what if that</p>



<p class="wp-block-paragraph">was a two&#8217;s complement number that can represent a signed integer? If it&#8217;s two&#8217;s complement</p>



<p class="wp-block-paragraph">or if it&#8217;s just like a signed integer, then the leftmost bit being a zero tells me that</p>



<p class="wp-block-paragraph">result of my left shift, there&#8217;s a one now in that first position, which tells me that&#8217;s</p>



<p class="wp-block-paragraph">probably a negative number.</p>



<p class="wp-block-paragraph">We could face a similar problem with shifting to the right.</p>



<p class="wp-block-paragraph">I mean, we didn&#8217;t this time.</p>



<p class="wp-block-paragraph">It just looks like it actually shifted and the answer is going to end up being right.</p>



<p class="wp-block-paragraph">But what if we shifted something else right?</p>



<p class="wp-block-paragraph">Let&#8217;s see, what am I going to do here?</p>



<p class="wp-block-paragraph">One, one, zero, zero.</p>



<p class="wp-block-paragraph">What if I shifted that number one to the right?</p>



<p class="wp-block-paragraph">The answer would be zero, you know, one, one, zero, right?</p>



<p class="wp-block-paragraph">you know 110 right but then that&#8217;s wrong because the original number was negative because there&#8217;s</p>



<p class="wp-block-paragraph">a one in the first position but the result is a zero so if i&#8217;m not careful about what instructions</p>



<p class="wp-block-paragraph">i&#8217;m using and how to respect whether the number i&#8217;m shifting is actually considered an integer</p>



<p class="wp-block-paragraph">assigned integer or if it&#8217;s just bits if i don&#8217;t respect that then i could end up ruining the</p>



<p class="wp-block-paragraph">number so keep that in mind we&#8217;re going to do several examples of that i&#8217;m not going to show</p>



<p class="wp-block-paragraph">shift by hand I&#8217;m just going to show you what the instructions in the assembler</p>



<p class="wp-block-paragraph">will do so then the other thing that we should talk about is rotating so imagine</p>



<p class="wp-block-paragraph">that I rotate this number one to the right well nothing really happens</p>



<p class="wp-block-paragraph">visually but what you&#8217;re supposed to do if we rotate to the right you take the</p>



<p class="wp-block-paragraph">number on the very right side and you just pop it onto the front so it sort of</p>



<p class="wp-block-paragraph">it doesn&#8217;t fall off and get replaced by a zero or get replaced by a one depending</p>



<p class="wp-block-paragraph">moves to the other side so if I shift this again maybe let&#8217;s shift it one more</p>



<p class="wp-block-paragraph">time to the right I&#8217;m gonna take that one and pop it off the right side and</p>



<p class="wp-block-paragraph">put it on the very front so see do you see how there it&#8217;s kind of rotating I do</p>



<p class="wp-block-paragraph">this one more time actually let me copy paste this I&#8217;ll take that number on the</p>



<p class="wp-block-paragraph">right side pop it off put it back on the front then if I do it again take that</p>



<p class="wp-block-paragraph">zero pop it off put it back on the front notice how it eventually gets back to</p>



<p class="wp-block-paragraph">the original pattern and I would have to rotate you know exactly four times to</p>



<p class="wp-block-paragraph">to get that to happen same same thing applies for rotating to the left i&#8217;m not going to show you</p>



<p class="wp-block-paragraph">because uh it&#8217;s it&#8217;s the same thing as rotating to the right just in a different direction</p>



<p class="wp-block-paragraph">one thing that i forgot to say a moment ago when i was talking about shifting is uh think about this</p>



<p class="wp-block-paragraph">if we shift to the right pretend that uh we don&#8217;t really care that much let&#8217;s let me put some extra</p>



<p class="wp-block-paragraph">zeros here so that we don&#8217;t have to carry too much we don&#8217;t have to care too much about what&#8217;s</p>



<p class="wp-block-paragraph">happening with positive or negative. If you know how to convert to binary, which I&#8217;ve explained in</p>



<p class="wp-block-paragraph">other videos, then you&#8217;ll know this number is, let&#8217;s see, there&#8217;s a one and a two, and then these</p>



<p class="wp-block-paragraph">positions are four and eight. So this should be a 12, right? If I shift this to the right,</p>



<p class="wp-block-paragraph">I should be dividing this by two because the base of binary is two. So every time you kind of like</p>



<p class="wp-block-paragraph">move to the left in terms of what digit you&#8217;re looking at, you&#8217;re increasing the power of that</p>



<p class="wp-block-paragraph">every time you move to the right you&#8217;re decreasing the power of the digit by two</p>



<p class="wp-block-paragraph">so that means if I shift the entire thing to the right I should be dividing the whole thing by two</p>



<p class="wp-block-paragraph">right not just one bit so if I just take a zero off of there and put it on there I&#8217;ve now shifted</p>



<p class="wp-block-paragraph">all the bits shifted all the bits one to the right I should end up with the six let&#8217;s just</p>



<p class="wp-block-paragraph">compute what it is there&#8217;s a one there and there&#8217;s a two there and there&#8217;s a four there what&#8217;s four</p>



<p class="wp-block-paragraph">plus two that&#8217;s six so this is actually six in binary or the original number divided by two</p>



<p class="wp-block-paragraph">because we shifted to the right on the other hand if I take this number one more time and</p>



<p class="wp-block-paragraph">just kind of shift it to the left I should multiply it by two so that means I should</p>



<p class="wp-block-paragraph">expect to see a 24 let me just remove one of those zeros and put it on the right side there</p>



<p class="wp-block-paragraph">this is not a rotation I&#8217;m just kind of like filling in zeros I&#8217;m keeping it simple with</p>



<p class="wp-block-paragraph">the shifted number this is a 1, a 2, and a 4 again if you don&#8217;t know how to convert</p>



<p class="wp-block-paragraph">from binary to decimal or back see my other videos so then we basically have</p>



<p class="wp-block-paragraph">an 8 here and a 16 what&#8217;s 8 plus 16 that&#8217;s 24 24 is twice of 12 so we have</p>



<p class="wp-block-paragraph">multiplied by 2 so you could multiply by 4 by shifting twice multiply by 8 by</p>



<p class="wp-block-paragraph">shifting 3 times you know whatever you want to do but shifting is is like a</p>



<p class="wp-block-paragraph">is like a shortcut besides just to move the bits it&#8217;s also a shortcut to multiply and divide by two</p>



<p class="wp-block-paragraph">okay so that&#8217;s the basic idea let me start working on a little program here for you before I continue</p>



<p class="wp-block-paragraph">I just want you to know that my program it&#8217;s got stuff in it already we&#8217;ve got a make file here</p>



<p class="wp-block-paragraph">if I uh I edit the make file I just have a make file set up to build the program so again this</p>



<p class="wp-block-paragraph">is specific to just you know bits we&#8217;re not going to talk about make files see my other videos if</p>



<p class="wp-block-paragraph">you want to know how to make your own make file same thing for this gdb.txt file I&#8217;ve just put</p>



<p class="wp-block-paragraph">commands that I&#8217;m going to send to gdb my debugger of choice this is not a gdb video I haven&#8217;t</p>



<p class="wp-block-paragraph">actually made my gdb video yet so look out for that in this in the somewhat near future</p>



<p class="wp-block-paragraph">so we&#8217;re just going to focus on the actual program for now anyway so I&#8217;ve got a solution</p>



<p class="wp-block-paragraph">Anyway, so I&#8217;ve got a solution already set up here.</p>



<p class="wp-block-paragraph">Let me start copy pasting some stuff in there.</p>



<p class="wp-block-paragraph">Going to copy paste my data section.</p>



<p class="wp-block-paragraph">So first off,</p>



<p class="wp-block-paragraph">my sample program is just gonna have codes for writing,</p>



<p class="wp-block-paragraph">you know, with the system call</p>



<p class="wp-block-paragraph">and exiting with the system call.</p>



<p class="wp-block-paragraph">And then I&#8217;m gonna output to standard output</p>



<p class="wp-block-paragraph">and I&#8217;m gonna do a zero return code.</p>



<p class="wp-block-paragraph">If you don&#8217;t know that,</p>



<p class="wp-block-paragraph">see my other videos that I&#8217;ve already put out.</p>



<p class="wp-block-paragraph">I&#8217;m gonna make a sample string where I just say,</p>



<p class="wp-block-paragraph">I just say hello my name is Gibson Montgomery Gibson that&#8217;s not my name but I love those names</p>



<p class="wp-block-paragraph">I&#8217;m just going to say hello basically when the program starts then I have some bit strings here</p>



<p class="wp-block-paragraph">that will help me with the you know help me demonstrate some of the bitwise operations</p>



<p class="wp-block-paragraph">the ending and the oring and the shifting and whatever and then I&#8217;m going to put a negative</p>



<p class="wp-block-paragraph">number here as a variable just to prove to you that sometimes the bitwise or logical shifting</p>



<p class="wp-block-paragraph">break a number if the number is supposed to actually be assigned integer and and use that</p>



<p class="wp-block-paragraph">as evidence for why you should use arithmetic shifting so now I&#8217;m going to start my program</p>



<p class="wp-block-paragraph">here let&#8217;s see my entry point should start at line 30 I got to be super careful about this</p>



<p class="wp-block-paragraph">because if I don&#8217;t know 26 if I don&#8217;t then all of my debug statements are going to have to be</p>



<p class="wp-block-paragraph">it lined up correctly so i&#8217;m basically just going to print the hello message and then maybe you know</p>



<p class="wp-block-paragraph">what at the very end i&#8217;m going to exit from the system with a system call so basically if we run</p>



<p class="wp-block-paragraph">this program again system calls and everything like that are not part of this video see my other</p>



<p class="wp-block-paragraph">videos but uh if this works we should see just like a simple message okay so that&#8217;s it my name</p>



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



<p class="wp-block-paragraph">So then the next thing that I&#8217;m going to do is I&#8217;m going to show you a bitwise</p>



<p class="wp-block-paragraph">and operation. So I&#8217;m going to copy paste that should start on line 42.</p>



<p class="wp-block-paragraph">And if I get this wrong, the debugging is going to be awful.</p>



<p class="wp-block-paragraph">I was trying to make this a pure assembly program without a bunch of extra work.</p>



<p class="wp-block-paragraph">I guess I could have linked GCC so I could just print results to the terminal,</p>



<p class="wp-block-paragraph">but whatever too late now. So it starts on 42.</p>



<p class="wp-block-paragraph">and ends on 51 okay so I think that&#8217;s good so the instruction for ending doing</p>



<p class="wp-block-paragraph">a logical and between two registers which are full of bits and then what</p>



<p class="wp-block-paragraph">will happen is every bit gets anded against its own course or I guess the</p>



<p class="wp-block-paragraph">corresponding bit in the other other register is just and so what I&#8217;m doing</p>



<p class="wp-block-paragraph">first is I&#8217;m gonna set up the registers so I&#8217;m gonna move the a sequence which</p>



<p class="wp-block-paragraph">sequence, which if you look up is just, you know, I put a bunch of bits. So like in hex,</p>



<p class="wp-block-paragraph">I like to type hex more than binary when I&#8217;m just kind of putting stuff in there.</p>



<p class="wp-block-paragraph">So, you know, each F is basically four ones. So it&#8217;s going to be like, I guess, 16 ones,</p>



<p class="wp-block-paragraph">and then a bunch of zeros, and then, you know, a few ones, and then some zeros and ones.</p>



<p class="wp-block-paragraph">So I&#8217;m just making a little pattern for bits A. Don&#8217;t think that they mean anything right now.</p>



<p class="wp-block-paragraph">And I&#8217;m going to make a different pattern with bits B. And when we look into the debugger,</p>



<p class="wp-block-paragraph">And when we look into the debugger, we should see the before and after patterns so we can kind of see what the AND instruction did.</p>



<p class="wp-block-paragraph">So I&#8217;m going to move bits A and bits B into R10 and R11.</p>



<p class="wp-block-paragraph">Then I&#8217;m going to move, so these are going to be like the original bits that we can inspect later.</p>



<p class="wp-block-paragraph">this AND instruction against R11 which is the B pattern. So I&#8217;m basically taking the A pattern</p>



<p class="wp-block-paragraph">and I&#8217;m ANDing it against the B pattern but I&#8217;m also loading up the registers so we can see</p>



<p class="wp-block-paragraph">the A pattern then the B pattern and then the result of ANDing them together.</p>



<p class="wp-block-paragraph">Finally on line 50 here I&#8217;m using a NOPE which is just a non-operation so that I can easily</p>



<p class="wp-block-paragraph">break the debugger so we can sort of inspect the program and see what&#8217;s going on inside of there.</p>



<p class="wp-block-paragraph">program and see what&#8217;s going on inside of there. You know what, let me, uh, let me open this up</p>



<p class="wp-block-paragraph">again here so that I can control where I&#8217;m breaking. So 48 is where the nope is. That&#8217;s</p>



<p class="wp-block-paragraph">totally wrong. I&#8217;ve already ruined my program somehow. It&#8217;s going to be 50, I guess. All right,</p>



<ol start="50" class="wp-block-list">
<li>And what I&#8217;m doing inside of this, and again, this is not a GDB video. I&#8217;m just saying,</li>
</ol>



<p class="wp-block-paragraph">let&#8217;s break at line 50 so I can kind of poke around. And I&#8217;m going to comment all these</p>



<p class="wp-block-paragraph">And I&#8217;m going to comment all these things out for now.</p>



<p class="wp-block-paragraph">That&#8217;s going to happen at the very end.</p>



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



<p class="wp-block-paragraph">How many different things we can look at.</p>



<p class="wp-block-paragraph">So I&#8217;m telling GDB not to execute any of these instructions, except for the, uh,</p>



<p class="wp-block-paragraph">the break point instructions.</p>



<p class="wp-block-paragraph">So we can just break on line 50.</p>



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



<p class="wp-block-paragraph">Hopefully this works.</p>



<p class="wp-block-paragraph">I&#8217;m going to now use make debug, which under the hood is just going to be compiling and</p>



<p class="wp-block-paragraph">then telling GDB to launch with the name of my program inside of it.</p>



<p class="wp-block-paragraph">And then to execute those things.</p>



<p class="wp-block-paragraph">and then to execute those things.</p>



<p class="wp-block-paragraph">So it hits breakpoint number 50.</p>



<p class="wp-block-paragraph">There&#8217;s a bunch of crap that gets printed.</p>



<p class="wp-block-paragraph">And I can now inspect my registers.</p>



<p class="wp-block-paragraph">I&#8217;m going to go info R for just info registers.</p>



<p class="wp-block-paragraph">And let&#8217;s see.</p>



<p class="wp-block-paragraph">So we had R10, 11, and 12.</p>



<p class="wp-block-paragraph">So if we look at the terminal, you can see that R10, the hex version,</p>



<p class="wp-block-paragraph">has the original pattern of the A sequence,</p>



<p class="wp-block-paragraph">and then R11 has the original pattern of the B sequence.</p>



<p class="wp-block-paragraph">original pattern of the b sequence if we do a logical and and put the result into r12 then</p>



<p class="wp-block-paragraph">you should see let me just double check that that&#8217;s definitely r12 yeah then you should see</p>



<p class="wp-block-paragraph">the result of ending all of the f&#8217;s together remember that an f is just four ones so i&#8217;m kind</p>



<p class="wp-block-paragraph">of simplifying it a little bit you could make yours more complicated with lots of randomness</p>



<p class="wp-block-paragraph">in your ones and zeros just to prove it to yourself but notice how r12 only has an f</p>



<p class="wp-block-paragraph">So like FF means F, F0 means 0, F0 means 0, you know, and so forth.</p>



<p class="wp-block-paragraph">0F means 0.</p>



<p class="wp-block-paragraph">So only where you see two Fs will you see an F in the final target pattern.</p>



<p class="wp-block-paragraph">So that&#8217;s a logical AND, and that&#8217;s how you do it in Yasm Assembly.</p>



<p class="wp-block-paragraph">You just load up a register with some bits, R12.</p>



<p class="wp-block-paragraph">We loaded them up with the first pattern.</p>



<p class="wp-block-paragraph">And then we take the second pattern and say that&#8217;s what I want to AND it against.</p>



<p class="wp-block-paragraph">that&#8217;s what I want to end it against. And you can have that in a register or I think an immediate</p>



<p class="wp-block-paragraph">or a memory location. So we&#8217;re just ending these registers together. And you know that they&#8217;re</p>



<p class="wp-block-paragraph">loaded up with patterns A and B. So nothing too hard at this point. Let&#8217;s do a, let&#8217;s look at</p>



<p class="wp-block-paragraph">bitwise or this should start on line 52 or I&#8217;m screwing myself. 52. Okay. 52. So same thing,</p>



<p class="wp-block-paragraph">up R10 and R11 with the bit patterns and then we&#8217;re going to move the first bit pattern</p>



<p class="wp-block-paragraph">into R12 so that we can or it against the second pattern and so basically R10 and 11</p>



<p class="wp-block-paragraph">will have the original bit patterns R12 will have the result of oring them.</p>



<p class="wp-block-paragraph">Then we&#8217;ll do a nope on line 60 so I want to break here on line 60.</p>



<p class="wp-block-paragraph">Yep I ruined all my line numbers. What did I do wrong? I think I just have it offset by 2 now</p>



<p class="wp-block-paragraph">two now for some reason so if we just kind of run the program now in the debugger again i&#8217;m going to</p>



<p class="wp-block-paragraph">quit and then yes if we clear and make debug probably clear was unnecessary we&#8217;re broken at</p>



<p class="wp-block-paragraph">line 60 if i say info registers the registers in question are oh shoot um 10 11 and 12 right so</p>



<p class="wp-block-paragraph">we look at 10 and 11 they have the same pattern as before and 12 has a lot more f&#8217;s because if</p>



<p class="wp-block-paragraph">has a lot more F&#8217;s because if either of the patterns had an F,</p>



<p class="wp-block-paragraph">then the resulting pattern would have an F.</p>



<p class="wp-block-paragraph">So this is a logical OR.</p>



<p class="wp-block-paragraph">Same idea as AND in terms of the way you format the instruction and the OPS,</p>



<p class="wp-block-paragraph">but it ORs instead of ANDs.</p>



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



<p class="wp-block-paragraph">Let&#8217;s move on.</p>



<p class="wp-block-paragraph">Next thing I want to show you is the XOR, which is pretty cool.</p>



<p class="wp-block-paragraph">By the way, so far we&#8217;re just operating on bits.</p>



<p class="wp-block-paragraph">We don&#8217;t consider these bits to actually be numbers.</p>



<p class="wp-block-paragraph">maybe they are but we should probably be careful about that anyway so I&#8217;m going to say let&#8217;s check</p>



<p class="wp-block-paragraph">out our bitwise xor which is exclusive or which means exactly one bit in any pair needs to be a</p>



<p class="wp-block-paragraph">one for the resulting bit to be a one and of course per usual all bits will be checked against each</p>



<p class="wp-block-paragraph">other in the pair of of bit patterns it&#8217;ll be one bit at a time from each pattern that gets checked</p>



<p class="wp-block-paragraph">Okay, so it starts on 62 and then we got a nope there.</p>



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



<p class="wp-block-paragraph">So same thing that we did before.</p>



<p class="wp-block-paragraph">We&#8217;re just loading up two registers with the bit patterns and then R12 is going to be the</p>



<p class="wp-block-paragraph">result of exclusive oring or X oring the two bit patterns.</p>



<p class="wp-block-paragraph">So that means we&#8217;re going to break on line 70.</p>



<p class="wp-block-paragraph">Now, let me just get rid of my other break point, change that 68 to a 70.</p>



<p class="wp-block-paragraph">And now I should quit.</p>



<p class="wp-block-paragraph">bug we&#8217;re at 70 good info registers so I think we said it was 10 and 11 had the original bit patterns</p>



<p class="wp-block-paragraph">and notice the resulting bit pattern it only has an f in a position where there was exactly</p>



<p class="wp-block-paragraph">one f between the two input patterns so</p>



<p class="wp-block-paragraph">wait did I just do something wrong here what did I just do wrong</p>



<p class="wp-block-paragraph">hang on a second I&#8217;m seeing something bad x or r12 am I looking at r12</p>



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



<p class="wp-block-paragraph">R10 and 11.</p>



<p class="wp-block-paragraph">Did I just do something wrong?</p>



<p class="wp-block-paragraph">10 and 11 and then we load 10 up into 12 and then we XOR it by 11 and then it stores it</p>



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



<p class="wp-block-paragraph">Something&#8217;s a little fishy here.</p>



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



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



<p class="wp-block-paragraph">I don&#8217;t like that.</p>



<p class="wp-block-paragraph">Oh, I understand now.</p>



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



<p class="wp-block-paragraph">So that was a little scary.</p>



<p class="wp-block-paragraph">This has shifted because the printout is not great.</p>



<p class="wp-block-paragraph">Because the printout is not great. See how there&#8217;s like a blank space here?</p>



<p class="wp-block-paragraph">That threw me off. So you have to imagine that all of the bit patterns are kind of shifted.</p>



<p class="wp-block-paragraph">Maybe I should just copy paste this.</p>



<p class="wp-block-paragraph">I was worried that I suddenly didn&#8217;t know how to do an XOR in assembly.</p>



<p class="wp-block-paragraph">So let me get rid of these integers here.</p>



<p class="wp-block-paragraph">Notice how there&#8217;s just a blank space that shifted everything and threw it off in terms of the display.</p>



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



<p class="wp-block-paragraph">And what should really happen now is that there should be a zero here.</p>



<p class="wp-block-paragraph">A zero at the front.</p>



<p class="wp-block-paragraph">a zero at the front now everything would make more sense sometimes the display likes to just</p>



<p class="wp-block-paragraph">kind of truncate things and just assume that everything will be read correctly but it wasn&#8217;t</p>



<p class="wp-block-paragraph">so notice uh in the first position both of the uh both the input patterns are are an f so the</p>



<p class="wp-block-paragraph">output pattern will be a zero you can imagine under the hood there was a bunch of ones and</p>



<p class="wp-block-paragraph">then a bunch of ones and that resulted in a bunch of zeros uh here we&#8217;ve got like an f and a zero so</p>



<p class="wp-block-paragraph">the result is going to be an f and then f and a zero and after zero zero and an f is an f</p>



<p class="wp-block-paragraph">f um and then where you see zeros that means uh there was not exactly one uh one in each bit</p>



<p class="wp-block-paragraph">position so for the let&#8217;s say the first one that&#8217;s a bunch of zeros for both input patterns so that&#8217;s</p>



<p class="wp-block-paragraph">why there&#8217;s a zero there then for the next two each of them had all ones or just the two f&#8217;s</p>



<p class="wp-block-paragraph">uh so that&#8217;s why x or says it&#8217;s a zero and so forth right so like at the end two f&#8217;s making</p>



<p class="wp-block-paragraph">Okay, that was a little scary.</p>



<p class="wp-block-paragraph">I&#8217;m glad I figured that out quickly instead of taking a million years.</p>



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



<p class="wp-block-paragraph">So now let&#8217;s do another example.</p>



<p class="wp-block-paragraph">We&#8217;re done with XOR.</p>



<p class="wp-block-paragraph">Now let&#8217;s look at bitwise knot.</p>



<p class="wp-block-paragraph">I forgot to explain knot.</p>



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



<p class="wp-block-paragraph">But let me just say that a knot is just inverting the bit.</p>



<p class="wp-block-paragraph">So if we have a zero, it becomes a one.</p>



<p class="wp-block-paragraph">And if we have a one, it becomes a zero.</p>



<p class="wp-block-paragraph">If you knot against a register that has lots of bits or a memory location that has lots</p>



<p class="wp-block-paragraph">of bits, you can do that and just every single bit in the sequence will just be flipped from</p>



<p class="wp-block-paragraph">a zero to a one or vice versa.</p>



<p class="wp-block-paragraph">So let&#8217;s see, we just did bitwise XOR.</p>



<p class="wp-block-paragraph">Now we&#8217;re going to do a knot starting on line 72, I guess.</p>



<p class="wp-block-paragraph">And then we&#8217;re going to do another nope there.</p>



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



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



<p class="wp-block-paragraph">So now let&#8217;s get rid of the break point at the line 70 and put it at line 78 instead.</p>



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



<p class="wp-block-paragraph">Oh man, this video is going to take forever.</p>



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



<p class="wp-block-paragraph">So now let&#8217;s check out bitwise knot.</p>



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



<p class="wp-block-paragraph">Oh, I guess I should improve this because what I&#8217;m doing is I&#8217;m moving everything into</p>



<p class="wp-block-paragraph">everything into R10 and then I&#8217;m just nodding R10 so we can&#8217;t really compare R10 against itself.</p>



<p class="wp-block-paragraph">That&#8217;s not fun. Let me instead move R10 into R11 so that I can nod R11 so that we can</p>



<p class="wp-block-paragraph">see two different registers at least. Okay, so let me just update my solution real fast.</p>



<p class="wp-block-paragraph">move r11 r10 okay so now it stops at 75 and that one stops at 75 okay cool</p>



<p class="wp-block-paragraph">now we got our breakpoint should be at line 79 not 78 now we&#8217;re ready to run it and see what the</p>



<p class="wp-block-paragraph">does clear and make debug info registers so we should have r10 r11 again the formatting is a</p>



<p class="wp-block-paragraph">so I&#8217;m just going to copy paste it up here. I&#8217;m going to do this, get rid of the numbers,</p>



<p class="wp-block-paragraph">pad on the left for the missing zeros, and then now you can see clearly that all of the</p>



<p class="wp-block-paragraph">F&#8217;s were flipped to zero. Again, that means, you know, each F had four ones inside of it,</p>



<p class="wp-block-paragraph">so all those ones were flipped to a zero, so that&#8217;s why it&#8217;s a zero. Okay, so nodding, pretty easy,</p>



<p class="wp-block-paragraph">this and show the next example which was gonna be let&#8217;s see seven let&#8217;s check out bitwise rotation</p>



<p class="wp-block-paragraph">i&#8217;m going to copy paste all the way up to that nope there should start on line 81</p>



<p class="wp-block-paragraph">i paused before i took a drink of water hopefully i reminded myself later to edit the drink of water</p>



<p class="wp-block-paragraph">out i don&#8217;t know maybe you&#8217;ll be wondering why my mustache is wet right now if i did not edit it out</p>



<p class="wp-block-paragraph">I did not edit it out.</p>



<p class="wp-block-paragraph">Okay, so let&#8217;s check out bitwise rotation.</p>



<p class="wp-block-paragraph">So I&#8217;m gonna set up R10 and 11 and 12 and 13 and 14,</p>



<p class="wp-block-paragraph">all with the A pattern.</p>



<p class="wp-block-paragraph">And then I&#8217;m gonna start rotating things.</p>



<p class="wp-block-paragraph">So first I&#8217;m gonna rotate to the left R11 and then R12.</p>



<p class="wp-block-paragraph">I&#8217;m gonna rotate by one position on R11</p>



<p class="wp-block-paragraph">and then two positions on R12.</p>



<p class="wp-block-paragraph">And then I&#8217;m going to rotate</p>



<p class="wp-block-paragraph">rotate by one position for R13 and then two positions for R14 so we should see</p>



<p class="wp-block-paragraph">some interesting patterns. The NOPE is now on line 94 so I&#8217;m gonna break there</p>



<p class="wp-block-paragraph">instead whoops so I&#8217;m gonna change that and I&#8217;m now off by three it&#8217;s getting</p>



<p class="wp-block-paragraph">precarious and then I&#8217;m gonna uncomment all of my little print statements to</p>



<p class="wp-block-paragraph">show you the value of the registers after I rotate it so we should see like</p>



<p class="wp-block-paragraph">printed in the terminal just to prove to you what&#8217;s going to happen okay so man I got to</p>



<p class="wp-block-paragraph">remember what&#8217;s happening now first thing that I did was I printed r10 to show you one type of</p>



<p class="wp-block-paragraph">rotation and then I printed r10 again to show you another type of rotation so we should see the</p>



<p class="wp-block-paragraph">original and then the two rotated values and then the original again and then the other two rotated</p>



<p class="wp-block-paragraph">values so here&#8217;s the original we&#8217;re printing it in binary with the special print command in gdb</p>



<p class="wp-block-paragraph">in GDB, which is just like P slash T and then the register. So this is the original pattern</p>



<p class="wp-block-paragraph">right here. And if you just kind of squint at it for a while, you can realize that the dollar</p>



<p class="wp-block-paragraph">sign two pattern has been, uh, shifted once to the left. Actually, let me just make sure that</p>



<p class="wp-block-paragraph">I&#8217;m doing a shift and not a rotate. I&#8217;m accidentally saying the word, uh, rotate, I think. And, uh,</p>



<p class="wp-block-paragraph">that&#8217;s, oh no, no, it is rotate. Yeah. We&#8217;re going to shift later, I guess.</p>



<p class="wp-block-paragraph">I guess. Okay, so we are definitely rotating. So we should see some wrap around. I guess I used</p>



<p class="wp-block-paragraph">bad bit patterns for the wrap around. But just imagine that, you know, in this pattern right here,</p>



<p class="wp-block-paragraph">this one, if we&#8217;re rotating to the left, this one fell off and it got placed on the right side of</p>



<p class="wp-block-paragraph">of the next pattern it&#8217;s going to be the same thing basically so but this is rotation so we</p>



<p class="wp-block-paragraph">wouldn&#8217;t be reusing the bits that fell off but anyway you can see that if I rotated to the left</p>



<p class="wp-block-paragraph">by one position that whole thing is is kind of shifted to the left I shouldn&#8217;t use the word shift</p>



<p class="wp-block-paragraph">and then for the second pattern we did a rotation by two positions so all the bits are just kind of</p>



<p class="wp-block-paragraph">moved to the left compared to the very first pattern okay so no problem then the next thing</p>



<p class="wp-block-paragraph">that we&#8217;re looking at is just you know rotating to the right and you can tell that everything is</p>



<p class="wp-block-paragraph">offset by one here and it&#8217;s offset by two here so rotating to the left by one position by two</p>



<p class="wp-block-paragraph">positions rotating to the right by one position or by two positions that&#8217;s rotation in a nutshell</p>



<p class="wp-block-paragraph">okay so then the next thing that we&#8217;re going to do we&#8217;re on 94 now</p>



<p class="wp-block-paragraph">um let&#8217;s see logical shifting nope nope nope I&#8217;m gonna grab some code that will shift both</p>



<p class="wp-block-paragraph">logically and arithmetically so we can kind of see the difference</p>



<p class="wp-block-paragraph">okay so show maybe I should change this to show some some shifting</p>



<p class="wp-block-paragraph">because uh if I just say it&#8217;s logical then yeah sure uh the shift function uh the shift</p>



<p class="wp-block-paragraph">those are logical shifts but then down here when it&#8217;s a sal and sar those are arithmetic shifts</p>



<p class="wp-block-paragraph">I don&#8217;t know maybe maybe I can do that maybe I can just</p>



<p class="wp-block-paragraph">duplicate the comment maybe to make it more more intuitive so show arithmetic shifts</p>



<p class="wp-block-paragraph">Okay, I think that looks pretty good. Let me paste that into 105 on my solution so I don&#8217;t get out of sync here.</p>



<p class="wp-block-paragraph">So what&#8217;s going to happen here? We&#8217;re basically going to load up R10 with a negative number.</p>



<p class="wp-block-paragraph">I&#8217;ve chosen the number negative 12 because I kind of feel like it at least shows you a little bit for dividing and stuff like that.</p>



<p class="wp-block-paragraph">So we&#8217;re loading up R10 with a negative number, and then R11 is going to get R10, but then we&#8217;re going to shift it left by two positions.</p>



<p class="wp-block-paragraph">positions so that should be multiplying it by four but this is well a bitwise</p>



<p class="wp-block-paragraph">shifting so it it&#8217;s not going to necessarily respect the fact that the</p>



<p class="wp-block-paragraph">number is in twos complement so this is not a good idea if you consider r tend</p>



<p class="wp-block-paragraph">to be a number because sometimes the result might be right and sometimes the</p>



<p class="wp-block-paragraph">result might be wrong here it says math won&#8217;t work in my comment but sometimes</p>



<p class="wp-block-paragraph">it will accidentally work but you still want to consider it to be non-working</p>



<p class="wp-block-paragraph">It&#8217;s only good for bit shifting.</p>



<p class="wp-block-paragraph">Then we&#8217;re going to put R12, we&#8217;re going to give it the same value as R10 and then we&#8217;re</p>



<p class="wp-block-paragraph">just going to shift it to the right by two positions.</p>



<p class="wp-block-paragraph">This will definitely break because when you shift to the right, the sine bit is going</p>



<p class="wp-block-paragraph">to get padded with a zero rather than what the actual sine bit was originally.</p>



<p class="wp-block-paragraph">So that will almost certainly break it almost every time.</p>



<p class="wp-block-paragraph">Then we&#8217;re going to use arithmetic shifts which just respect the fact that the original</p>



<p class="wp-block-paragraph">number is two&#8217;s complement.</p>



<p class="wp-block-paragraph">two&#8217;s complement so you know like we have this negative 12 number it&#8217;s going to end up being</p>



<p class="wp-block-paragraph">bits but the uh the shl and the shr instructions are not going to respect the fact that it&#8217;s</p>



<p class="wp-block-paragraph">negative 12 they&#8217;re just going to consider them to be bits but the sal shifting to the left</p>



<p class="wp-block-paragraph">arithmetically and the sar shifting to the right arithmetically are going to respect that respect</p>



<p class="wp-block-paragraph">the fact that it&#8217;s two&#8217;s complement so let&#8217;s see we&#8217;ve got all this stuff and then we&#8217;ve got a</p>



<p class="wp-block-paragraph">We&#8217;ve got all this stuff and then we&#8217;ve got a non-operation here.</p>



<p class="wp-block-paragraph">You know, again, like per usual, when we&#8217;re shifting, when we&#8217;re rotating, when we&#8217;re</p>



<p class="wp-block-paragraph">oaring, anding, whatever, it&#8217;s just the instruction name followed by both the source and the</p>



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



<p class="wp-block-paragraph">And then the second operand is only going to be source.</p>



<p class="wp-block-paragraph">So if we have a shifting left R11, then the result is going to be stored in R11, but it&#8217;s</p>



<p class="wp-block-paragraph">going to take as the source R11 and shift it to the left by two positions.</p>



<p class="wp-block-paragraph">to the left by two positions and so forth.</p>



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



<p class="wp-block-paragraph">So we have all of that.</p>



<p class="wp-block-paragraph">We&#8217;re going to nope on line one of what is it?</p>



<p class="wp-block-paragraph">One, one 14.</p>



<p class="wp-block-paragraph">So that means I&#8217;m going to stop breaking on 94 and I&#8217;m going to break on one 14.</p>



<p class="wp-block-paragraph">I thought it was four lines off and now I&#8217;m only two lines off.</p>



<p class="wp-block-paragraph">I think I just did something else wrong here between my source.</p>



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



<p class="wp-block-paragraph">Is the nope on one 14 up there?</p>



<p class="wp-block-paragraph">Yeah, it is.</p>



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



<p class="wp-block-paragraph">Guess I didn&#8217;t do that much wrong.</p>



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



<p class="wp-block-paragraph">Okay, so let&#8217;s run this, but this code up here was just going to print bit patterns</p>



<p class="wp-block-paragraph">related to our rotations.</p>



<p class="wp-block-paragraph">I don&#8217;t really want that anymore.</p>



<p class="wp-block-paragraph">So I&#8217;m going to uncomment all of these other prints that relate to our shifts.</p>



<p class="wp-block-paragraph">And this is going to be a nightmare to read and interpret, but I&#8217;m going to try.</p>



<p class="wp-block-paragraph">I really should have written a more complicated program that can just print them in the terminal</p>



<p class="wp-block-paragraph">so I don&#8217;t have to like read the debugger.</p>



<p class="wp-block-paragraph">Whatever, it&#8217;s brain exercise.</p>



<p class="wp-block-paragraph">size. And I think this actually is the end of the program. 119 is the last line. Yeah. Okay.</p>



<p class="wp-block-paragraph">So we just need to run this and see what happens. So really we&#8217;re interested in these, uh, you know,</p>



<p class="wp-block-paragraph">shifting to the left twice, shifting to the right twice bit wise, and then shifting to the left</p>



<p class="wp-block-paragraph">twice, arithmetically shifting to the right twice, arithmetically. Okay. So we&#8217;ll go back to the</p>



<p class="wp-block-paragraph">terminal here and quit. And then I&#8217;m going to do make debug. And now there&#8217;s a bunch of junk.</p>



<p class="wp-block-paragraph">here&#8217;s the original number and then we&#8217;re gonna bitwise shift left binary</p>



<p class="wp-block-paragraph">notice how it just sort of you know it shifted everything to the left and you</p>



<p class="wp-block-paragraph">know we&#8217;re not using we&#8217;re not printing hex anymore because I want you to see</p>



<p class="wp-block-paragraph">the binary and then you know like the patterns are just shifted to to the left</p>



<p class="wp-block-paragraph">like see here&#8217;s the first one if we&#8217;re looking at the right side and then here</p>



<p class="wp-block-paragraph">is the first one if we&#8217;re looking at the right side for the shifted version</p>



<p class="wp-block-paragraph">A similar deal for shift right except we lost a couple of numbers there because of the display</p>



<p class="wp-block-paragraph">so I&#8217;m going to copy paste both of these real fast.</p>



<p class="wp-block-paragraph">Per usual we lost numbers due to the display so I&#8217;m going to say notice how we have two</p>



<p class="wp-block-paragraph">numbers missing here if I added zeros on the right side that&#8217;s going to change the actual</p>



<p class="wp-block-paragraph">value so I&#8217;m going to have to add them to the left and now we can clearly see that the</p>



<p class="wp-block-paragraph">shifted four positions I think to the left wait did I do that where did I where did I even just</p>



<p class="wp-block-paragraph">copy paste from original shift left and shift right okay so shift right is what it&#8217;s supposed</p>



<p class="wp-block-paragraph">to be doing let me just double check here shift right is printing r12 and what does r12 have it</p>



<p class="wp-block-paragraph">shifting right by two. Okay.</p>



<p class="wp-block-paragraph">Oh, I&#8217;m supposed to compare it to the very first one.</p>



<p class="wp-block-paragraph">Let me just double check that I have this correct now.</p>



<p class="wp-block-paragraph">Why is it</p>



<p class="wp-block-paragraph">shifting to the right? Oh gosh, I think I just lost track of myself. Let me just,</p>



<p class="wp-block-paragraph">give me a second here. Or 14.</p>



<p class="wp-block-paragraph">I&#8217;m forgetting where I copy and paste. Okay. Bitwise shift right is</p>



<p class="wp-block-paragraph">bitwise shift right is R12, not R14. Oh gosh. Did I do it wrong? Were you cringing at home?</p>



<p class="wp-block-paragraph">Maybe this will work. And the two, that&#8217;s kind of the same thing that I did before. No, no,</p>



<p class="wp-block-paragraph">it&#8217;s different. Okay. So I had it wrong before. Now I finally have it right. So you can see that</p>



<p class="wp-block-paragraph">So you can see that the bit pattern is shifted twice to the right and then on the left two zeros show up which would have destroyed a number.</p>



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



<p class="wp-block-paragraph">So we shift it to the right and now we&#8217;re ready to look at arithmetic shift to the left and right.</p>



<p class="wp-block-paragraph">And let&#8217;s see do we have enough bits to actually print everything?</p>



<p class="wp-block-paragraph">This one should look like it&#8217;s shifted to the right just because of the way it prints.</p>



<p class="wp-block-paragraph">So that means this one is totally lined up.</p>



<p class="wp-block-paragraph">Okay so let me grab the original pattern here.</p>



<p class="wp-block-paragraph">grab the original pattern here so this is the original pattern and then if I go shift left</p>



<p class="wp-block-paragraph">in binary I&#8217;ll just put an L here and then I&#8217;ll put shift right in binary</p>



<p class="wp-block-paragraph">there we have enough bits I think in both cases so we don&#8217;t have to pad</p>



<p class="wp-block-paragraph">so now if we look at the original pattern and then we shift left arithmetically twice</p>



<p class="wp-block-paragraph">twice notice how it does kind of look like it survived right like this one moved over to the</p>



<p class="wp-block-paragraph">left twice because the sine bits are just kind of falling off but there&#8217;s enough of them that it&#8217;s</p>



<p class="wp-block-paragraph">not going to change the sine bit thank you two&#8217;s complement but when we shift to the right</p>



<p class="wp-block-paragraph">arithmetically normally we would have padded on the left with zeros remember we had zeros there</p>



<p class="wp-block-paragraph">on the left before but now that we&#8217;re respecting the arithmetic uh the the padding is correct now</p>



<p class="wp-block-paragraph">is correct now for two&#8217;s complement so that&#8217;s why there are ones there instead of zeros</p>



<p class="wp-block-paragraph">and then if you kind of look at it it&#8217;s like well we shifted twice to the right let me get</p>



<p class="wp-block-paragraph">rid of the left because we already understand that now it seems to be that the pattern is uh</p>



<p class="wp-block-paragraph">every bit is shifted over to the right the correct number of times the zeros dropped off the side</p>



<p class="wp-block-paragraph">and um the sine bits were padded on the left so everything seems to be correct and then just to</p>



<p class="wp-block-paragraph">to kind of prove to you that what we&#8217;re seeing is real, I just have the same things printed</p>



<p class="wp-block-paragraph">all over again from scratch in decimal. So originally we had our R10 as negative 12.</p>



<p class="wp-block-paragraph">And then if we do a bitwise shift to the left, it multiplies it by four. And so we&#8217;re accidentally</p>



<p class="wp-block-paragraph">correct here. Remember, this is a bitwise shift, so it doesn&#8217;t respect two&#8217;s complement. So just,</p>



<p class="wp-block-paragraph">we happen to have a starting number that would end up looking correct after we shifted it to the left.</p>



<p class="wp-block-paragraph">And then when we do a bitwise shift to the right, again, this could be disaster because we&#8217;re not</p>



<p class="wp-block-paragraph">respecting two&#8217;s complement or the sine bit. And that&#8217;s exactly what happened. You know, definitely</p>



<p class="wp-block-paragraph">if we&#8217;re shifting to the right, that should be 12 divided by four equals three or negative three.</p>



<p class="wp-block-paragraph">But instead we get this gigantic huge number because the sine bit got killed with the zeros</p>



<p class="wp-block-paragraph">killed with the zeros that were padded on the left. And then we finally try to shift left</p>



<p class="wp-block-paragraph">arithmetically. We end up still with negative 48, which is the correct number. If we&#8217;re going to</p>



<p class="wp-block-paragraph">shift left twice, that&#8217;s multiplying 12 by 4 and that&#8217;s 48 or negative 48. And then when we shift</p>



<p class="wp-block-paragraph">to the right, that should divide it by 2 twice because we&#8217;re shifting to the right by two</p>



<p class="wp-block-paragraph">positions. So that should be 12 divided by 4, which is negative 3 or sorry, negative 12 divided</p>



<p class="wp-block-paragraph">which is negative three so everything seems to be working except for a couple of moments of</p>



<p class="wp-block-paragraph">stumbling on my part which i hope you laughed at um and i think this is the end of the program so</p>



<p class="wp-block-paragraph">now hopefully you&#8217;re an expert in uh ending oring x oring nodding shifting left and right uh bitwise</p>



<p class="wp-block-paragraph">rotating. We&#8217;ve done it all in this one video. How long did this video take? 40 minutes?</p>



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



<p class="wp-block-paragraph">and you 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. If you could do me a please,</p>



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



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



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



<p class="wp-block-paragraph">better videos, or just I&#8217;ll be able to keep making videos in general. So please do me a kindness</p>



<p class="wp-block-paragraph">and subscribe. 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 filled with joy.</p>



<p class="wp-block-paragraph">just wakes me up and I get filled with joy. That&#8217;s exactly what happens every</p>



<p class="wp-block-paragraph">single time. So you could do it as a nice favor to me or you could you could</p>



<p class="wp-block-paragraph">troll me if you want to just wake me up in the middle of the night just subscribe</p>



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



<p class="wp-block-paragraph">at the middle of the screen right now you should see a QR code which you can</p>



<p class="wp-block-paragraph">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</p>



<p class="wp-block-paragraph">kind of like see all the videos I published and the services and tutorials</p>



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



<p class="wp-block-paragraph">If you have a suggestion for</p>



<p class="wp-block-paragraph">Clarifications or errata or just future videos that you want to see</p>



<p class="wp-block-paragraph">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</p>



<p class="wp-block-paragraph">I get I wake up in a cold sweat. 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 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/x86-64-assembly-bit-manipulation-and-or-xor-not-shifts-rotates-tutorial/">x86-64 Assembly Bit Manipulation: AND, OR, XOR, NOT, Shifts &amp; Rotates Tutorial</a> appeared first on <a href="https://www.NeuralLantern.com">NeuralLantern.com</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.NeuralLantern.com/x86-64-assembly-bit-manipulation-and-or-xor-not-shifts-rotates-tutorial/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Why C Strings End with \0 &#8211; Null Terminated Strings Explained Simply</title>
		<link>https://www.NeuralLantern.com/why-c-strings-end-with-0-null-terminated-strings-explained-simply/</link>
					<comments>https://www.NeuralLantern.com/why-c-strings-end-with-0-null-terminated-strings-explained-simply/#respond</comments>
		
		<dc:creator><![CDATA[mike]]></dc:creator>
		<pubDate>Sun, 23 Nov 2025 23:41:20 +0000</pubDate>
				<category><![CDATA[Assembly Language]]></category>
		<category><![CDATA[Coding]]></category>
		<category><![CDATA[Computer Science]]></category>
		<category><![CDATA[Videos]]></category>
		<category><![CDATA[\0 in c]]></category>
		<category><![CDATA[ascii strings]]></category>
		<category><![CDATA[c char array]]></category>
		<category><![CDATA[c language basics]]></category>
		<category><![CDATA[c programming for beginners]]></category>
		<category><![CDATA[c programming strings]]></category>
		<category><![CDATA[c programming tutorial]]></category>
		<category><![CDATA[c string functions]]></category>
		<category><![CDATA[c string termination]]></category>
		<category><![CDATA[c strings]]></category>
		<category><![CDATA[low-level programming]]></category>
		<category><![CDATA[null terminated strings]]></category>
		<category><![CDATA[null terminator]]></category>
		<category><![CDATA[string handling in c]]></category>
		<category><![CDATA[strlen implementation]]></category>
		<guid isPermaLink="false">https://www.NeuralLantern.com/?p=256</guid>

					<description><![CDATA[<p>Null-terminated strings in C use a zero byte (\0) to mark the end instead of storing length. Learn exactly how C strings work under the hood, see the ASCII values for "Hello", and understand why the null terminator exists.</p>
<p>The post <a href="https://www.NeuralLantern.com/why-c-strings-end-with-0-null-terminated-strings-explained-simply/">Why C Strings End with \0 &#8211; Null Terminated Strings Explained Simply</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="Why C Strings End with   - Null Terminated Strings Explained Simply" width="1380" height="776" src="https://www.youtube.com/embed/IPWsPMH12YE?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">Ever wondered why C strings are just character arrays that magically know where they end? It&#8217;s all because of the null terminator &#8211; that sneaky \0 at the end.</p>



<p class="wp-block-paragraph">In this video we break it down super simply:</p>



<ul class="wp-block-list">
<li>What a C string actually is (spoiler: just bytes)</li>



<li>Why old-school assembly needed separate length labels</li>



<li>How adding a single zero byte fixed everything</li>



<li>Quick look at ASCII codes for &#8220;Hello&#8221;</li>



<li>Why \0 is the perfect terminator (and how to write it in code)</li>



<li>How loops know when to stop reading the string</li>



<li>Real example in assembly with and without null termination</li>
</ul>



<p class="wp-block-paragraph">Perfect if you&#8217;re learning C, diving into low-level programming, or just curious about what&#8217;s really happening when you write &#8220;hello&#8221; in C.</p>



<p class="wp-block-paragraph">Introduction to Null Terminators 00:00:00<br>What is a C String? 00:00:11<br>Problems with Explicit Length in Assembly 00:01:06<br>Idea of a Special Terminator Symbol 00:01:24<br>Introduction to ASCII 00:02:20<br>Why ASCII is Outdated &#8211; Move to UTF 00:03:59<br>Example &#8211; &#8220;Hello&#8221; as ASCII Bytes 00:06:32<br>Mapping &#8220;Hello&#8221; to ASCII Codes 00:07:00<br>How Strings are Stored in Memory 00:08:46<br>The Null Terminator (\0) Explained 00:09:39<br>Null is ASCII Code 0 (NUL) 00:10:02<br>Escape Sequence \0 in Code 00:10:49<br>How Loops Use Null Terminator to Stop 00:11:55<br>Null-Terminated Strings in Assembly 00:12:41<br>Summary of C Strings 00:13:23<br>Outro and Call to Action 00:13: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">hey there let&#8217;s quickly talk about null terminators in c strings</p>



<p class="wp-block-paragraph">so first off what&#8217;s a c string it&#8217;s basically just a character array it&#8217;s just an array of numbers</p>



<p class="wp-block-paragraph">and the numbers will be interpreted as characters and what&#8217;s a null terminator it&#8217;s pretty easy it&#8217;s</p>



<p class="wp-block-paragraph">just a a way to terminate the string so that a program can automatically determine where the</p>



<p class="wp-block-paragraph">string ends in assembly a lot of times we&#8217;ll have you know a string like we&#8217;ll</p>



<p class="wp-block-paragraph">say oh gosh I don&#8217;t want to write this out right now we&#8217;ll say you know my</p>



<p class="wp-block-paragraph">string and we&#8217;ll define it as an array of bytes and then we&#8217;ll say you know</p>



<p class="wp-block-paragraph">hello or something right and then later in order to tell the assembly program</p>



<p class="wp-block-paragraph">how long the string is we&#8217;ll use an extra symbol this is not really an</p>



<p class="wp-block-paragraph">My string length is going to be equal to, this is the ASM assembly by the way in case</p>



<p class="wp-block-paragraph">you&#8217;re wondering, some kind of special symbol, my string length.</p>



<p class="wp-block-paragraph">And then now we have defined the actual string and then the length of the string.</p>



<p class="wp-block-paragraph">But this is kind of annoying, right?</p>



<p class="wp-block-paragraph">We have to define an extra thing to say how long the string is.</p>



<p class="wp-block-paragraph">And then we have to pass two different things that are kind of the same name later on when</p>



<p class="wp-block-paragraph">we want to print a string.</p>



<p class="wp-block-paragraph">But that&#8217;s not really the best way to do it.</p>



<p class="wp-block-paragraph">that&#8217;s not really the best way to do it what if instead we just put a special symbol I don&#8217;t know</p>



<p class="wp-block-paragraph">what should I put right now put a special zero I&#8217;ll just put like a question mark and we&#8217;ll say</p>



<p class="wp-block-paragraph">that the question mark is a special symbol and then when we want to have part of our program</p>



<p class="wp-block-paragraph">print the string we can just only give it the string and it can scan the string and as soon</p>



<p class="wp-block-paragraph">as it sees that special symbol let&#8217;s say the question mark for now it&#8217;ll know when to stop</p>



<p class="wp-block-paragraph">processing it or whatever we could allow the program to scan in advance to compute the the</p>



<p class="wp-block-paragraph">length and maybe store it somewhere so we can keep using it we can allow the program to just</p>



<p class="wp-block-paragraph">continuously print out characters maybe they&#8217;re buffered or something or processing characters</p>



<p class="wp-block-paragraph">and then just stop when we get to the question mark but the point is it&#8217;s a little bit less work</p>



<p class="wp-block-paragraph">for us to have a special symbol at the very end okay so a spoiler the symbol is going to be a zero</p>



<p class="wp-block-paragraph">Now let&#8217;s look at a special table that is called ASCII.</p>



<p class="wp-block-paragraph">Well, it&#8217;s not, the table&#8217;s not called ASCII,</p>



<p class="wp-block-paragraph">but this is sort of like a scheme that was invented a long time ago.</p>



<p class="wp-block-paragraph">I forget what this stands for.</p>



<p class="wp-block-paragraph">It&#8217;s like American standard, maybe character something, something.</p>



<p class="wp-block-paragraph">It&#8217;s basically for American purposes.</p>



<p class="wp-block-paragraph">A long time ago, we decided, you know what?</p>



<p class="wp-block-paragraph">I&#8217;d like to represent characters using one byte per character.</p>



<p class="wp-block-paragraph">bits or you know a value that can go between 0 and 255 and we&#8217;ll just start assigning these numbers</p>



<p class="wp-block-paragraph">to different characters so you can see here on this ascii table it&#8217;s kind of like an old thing</p>



<p class="wp-block-paragraph">let me turn on my little annotator you can see on this table well the letter h is mapped</p>



<p class="wp-block-paragraph">to the number 72. so this is this was arbitrary at the time now it&#8217;s a standard because everybody</p>



<p class="wp-block-paragraph">accepts uh you know ascii uh in certain parts of your programs it&#8217;s kind of a little out of date</p>



<p class="wp-block-paragraph">of a little out of date now because you know in the modern era it&#8217;s a lot smarter for your programs</p>



<p class="wp-block-paragraph">to support a world of different characters you know for all different languages than only to</p>



<p class="wp-block-paragraph">support characters that are common in english and even here there&#8217;s probably some stuff missing that</p>



<p class="wp-block-paragraph">english users would probably want to add like certain accents and symbols and things like that</p>



<p class="wp-block-paragraph">so you know the the letter o lowercase o it&#8217;s 111 uh the letter o uppercase is 79 so we&#8217;ve got you</p>



<p class="wp-block-paragraph">so we&#8217;ve got you know a basic idea of mapping so for the purposes of this example i&#8217;m just</p>



<p class="wp-block-paragraph">gonna assume we&#8217;re using ascii to make things easier okay so that&#8217;s an ascii table um</p>



<p class="wp-block-paragraph">oh i guess i should say that the modern scheme that we usually use is called utf let me just uh</p>



<p class="wp-block-paragraph">write that down on a notepad right now so we&#8217;ve got utf right now it&#8217;s just a different standard</p>



<p class="wp-block-paragraph">but the same idea you know with ASCII we only had one byte with UTF we have a wide variety of</p>



<p class="wp-block-paragraph">choices we can do UTF-8 which basically also means one byte it closely map maps to ASCII I</p>



<p class="wp-block-paragraph">think all or almost all the characters will map to ASCII and then we have UTF-16 which means two</p>



<p class="wp-block-paragraph">bytes or 16 bits and then we have UTF-32 which is basically four bytes or 32 bits as far as I</p>



<p class="wp-block-paragraph">while. UTF-32 should be able to handle all the characters that all the languages of the world</p>



<p class="wp-block-paragraph">commonly use. Just to double check, if you take two to the something power where the power, let&#8217;s</p>



<p class="wp-block-paragraph">say y, two to the y, where y is the number of bits in question, then you can get an idea for the</p>



<p class="wp-block-paragraph">number of combinations that you can represent. So two to the 32nd power, that&#8217;s about four billion.</p>



<p class="wp-block-paragraph">I should have known that off the top of my head. That&#8217;s about four billion possible combinations.</p>



<p class="wp-block-paragraph">So I don&#8217;t know.</p>



<p class="wp-block-paragraph">I think in my opinion, probably 32 is enough for all symbols in the world, for all languages</p>



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



<p class="wp-block-paragraph">I&#8217;m not sure.</p>



<p class="wp-block-paragraph">If not, then we would probably want to use UTF-64 if that exists, which definitely would</p>



<p class="wp-block-paragraph">cover all symbols in the world.</p>



<p class="wp-block-paragraph">But that&#8217;s really not the point of this video.</p>



<p class="wp-block-paragraph">I just wanted to mention it because if you&#8217;re writing an application on your own, you want</p>



<p class="wp-block-paragraph">to start your own business or you&#8217;re, you know, you&#8217;re working for someone, you probably</p>



<p class="wp-block-paragraph">almost certainly do not want to use ASCII.</p>



<p class="wp-block-paragraph">do not want to use ascii you probably want your database and your program to be able to use utf</p>



<p class="wp-block-paragraph">16 or 32 because you want to be able to support international users and that&#8217;s not going to work</p>



<p class="wp-block-paragraph">i had a website a long time ago that i wrote the database was in uh it was like latin 8 or</p>



<p class="wp-block-paragraph">some scheme in the database where it was basically ascii just you know eight bits per character and i</p>



<p class="wp-block-paragraph">could type characters in their native language and they were kind of disappointed and i lost a</p>



<p class="wp-block-paragraph">lot of business until i realized oh i should upgrade my database and my program to support</p>



<p class="wp-block-paragraph">utf but anyway the point of this video is not international character support the point of this</p>



<p class="wp-block-paragraph">video is what is a null terminator and how do you use it in a c string so i&#8217;m going to get back on</p>



<p class="wp-block-paragraph">Hello. Okay, so we got hello. This would basically be an array of characters or an array of in ASCII</p>



<p class="wp-block-paragraph">one byte characters. So I&#8217;ll put like a number here and a number here and there&#8217;ll be five numbers.</p>



<p class="wp-block-paragraph">What numbers are they? Well, if we&#8217;re using ASCII, then we should map these</p>



<p class="wp-block-paragraph">using the ASCII table. So again, in the ASCII table here, what is capital H?</p>



<p class="wp-block-paragraph">the number 72 and then the lowercase e that&#8217;s going to be the number 101 so i&#8217;ll just maybe do</p>



<p class="wp-block-paragraph">like an arrow here the number 101 and then the capital h is going to be the number 72 and then</p>



<p class="wp-block-paragraph">we got two l&#8217;s lowercase so that&#8217;s going to be this right here and that&#8217;s going to be the number</p>



<p class="wp-block-paragraph">108 and then lowercase o that&#8217;s going to be the number 111 so basically you know you just go</p>



<p class="wp-block-paragraph">through the characters you want to represent and map them using the ascii table or hopefully</p>



<p class="wp-block-paragraph">table or hopefully an actual lookup table inside your program and then you can do the same thing</p>



<p class="wp-block-paragraph">backwards you know what is what does 72 mean it means h so i&#8217;m going to erase this for now</p>



<p class="wp-block-paragraph">and i&#8217;m going to go back to my little notepad here and i&#8217;m just going to fill out the numbers</p>



<p class="wp-block-paragraph">so the first is going to be 72 oh actually before i fill these out hang on let me just</p>



<p class="wp-block-paragraph">show you that this is basically what we&#8217;re trying to translate here so we got like an uppercase h</p>



<p class="wp-block-paragraph">In many programming languages, definitely C++, which is the one I&#8217;m thinking of, we would use double quotes for an actual long string, like a proper string, a literal string, or like an STD string or something.</p>



<p class="wp-block-paragraph">And we would use an array of single quoted characters to say this is one character that maps to one or more bytes, in this case, one byte.</p>



<p class="wp-block-paragraph">So I&#8217;m going to say H-E-L-L-O and put them all in single quotes.</p>



<p class="wp-block-paragraph">And then we&#8217;ll just sort of like translate the numbers here.</p>



<p class="wp-block-paragraph">translate the numbers here. So we said the H was 72 because it was capital. It was like a lower</p>



<p class="wp-block-paragraph">number. And then the E is going to be 101. And then the L is 108. We&#8217;ll do two of those 108.</p>



<p class="wp-block-paragraph">And then the O was a 111. So you can imagine somewhere in an array of data in the computer,</p>



<p class="wp-block-paragraph">there&#8217;s the number 72, 101, 108, you know, whatever. And even further thinking about it,</p>



<p class="wp-block-paragraph">about it a little more deeply the number 72 is not actually in the machine it&#8217;s really a sequence of</p>



<p class="wp-block-paragraph">ones and zeros right so just imagine that inside the machine there&#8217;s just a ton of ones and zeros</p>



<p class="wp-block-paragraph">for this particular sequence you know five times eight which would be the number of uh you know</p>



<p class="wp-block-paragraph">eight is like the number of bits per byte since we&#8217;re doing one byte characters um what is that</p>



<p class="wp-block-paragraph">40 let me just double check here i want to make sure i&#8217;m saying things that are somewhat correct</p>



<p class="wp-block-paragraph">that are somewhat correct we&#8217;ll do eight times five whoops i think i screwed something up</p>



<p class="wp-block-paragraph">eight times five 40 yeah okay so you can imagine 40 ones and zeros just inside the machine</p>



<p class="wp-block-paragraph">anyway so we talked about before not wanting to specify the length of a string so we&#8217;ll just</p>



<p class="wp-block-paragraph">place a special marker instead of a question mark we&#8217;ll use the number zero so i&#8217;m going to put a</p>



<p class="wp-block-paragraph">I forgot if I actually pointed this out or not. I&#8217;ll just do this to be sure.</p>



<p class="wp-block-paragraph">Somewhere on this, you can see that the number zero in decimal, or zero in hexa, or zero in octal,</p>



<p class="wp-block-paragraph">you&#8217;re welcome, you know how to do those other systems, is just considered null. So it&#8217;s sort</p>



<p class="wp-block-paragraph">of a control code, or it&#8217;s not necessarily a symbol, it&#8217;s more of a control code. We&#8217;ll say</p>



<p class="wp-block-paragraph">the number 72 that means h capital h but if we see the number zero that means null and in the</p>



<p class="wp-block-paragraph">scheme that we&#8217;re going to use we will say that null terminates the string that&#8217;s why we call it</p>



<p class="wp-block-paragraph">a null terminator so null terminators are basically just zeros that terminate the string</p>



<p class="wp-block-paragraph">i could put null here inside of the the character representation just to kind of show you</p>



<p class="wp-block-paragraph">other languages and we want to do single quoted characters we can usually use an escape sequence</p>



<p class="wp-block-paragraph">which is just basically slash zero this is not a video about escape sequences but imagine that</p>



<p class="wp-block-paragraph">every time there is a slash a forward slash and then you know one character after that then the</p>



<p class="wp-block-paragraph">character will be interpreted zero for example if we just put zero by itself then that actually would</p>



<p class="wp-block-paragraph">be the ascii code for zero or just the character code for zero in this particular case zero is uh</p>



<p class="wp-block-paragraph">where the heck is the zero you can&#8217;t even find it oh there it is right there that&#8217;s like so the</p>



<p class="wp-block-paragraph">the character zero is represented by the number 48 but that&#8217;s not what we actually want we want</p>



<p class="wp-block-paragraph">the actual number zero because we&#8217;re trying to get a null terminator so anyway we couldn&#8217;t just put</p>



<p class="wp-block-paragraph">a regular zero in there because it would be interpreted as 48 instead we put a slash which</p>



<p class="wp-block-paragraph">tells the computer that the thing that follows is a control character so that will be interpreted as</p>



<p class="wp-block-paragraph">interpreted as actually a zero or null terminator.</p>



<p class="wp-block-paragraph">So now that we know how to put a null terminator in an array of characters and we know what</p>



<p class="wp-block-paragraph">it&#8217;s going to look like if we&#8217;re just talking about numbers, we now know that if you have</p>



<p class="wp-block-paragraph">a program that has let&#8217;s say a for loop and it&#8217;s kind of like going through, you know,</p>



<p class="wp-block-paragraph">do all the characters, if it&#8217;s going through every single character one by one and doing</p>



<p class="wp-block-paragraph">something to them, printing them, modifying, adding them to an algorithm, whatever.</p>



<p class="wp-block-paragraph">this for loop or actually this should probably be a while loop since for loops</p>



<p class="wp-block-paragraph">are kind of limited and the while loop will know when to break because as soon as it sees a</p>



<p class="wp-block-paragraph">character that is actually just the number zero it&#8217;ll say oh i found the null terminator i will</p>



<p class="wp-block-paragraph">now ignore this and break the loop i won&#8217;t do anything with this character because the null</p>



<p class="wp-block-paragraph">is a terminator it&#8217;s not part of the string um what else you know actually i think that&#8217;s pretty</p>



<p class="wp-block-paragraph">much it if you&#8217;re in assembly uh i&#8217;m just going to go back to yasm assembly for a second because i</p>



<p class="wp-block-paragraph">like i like to talk about that lately and imagine we have like a string called my string and we say</p>



<p class="wp-block-paragraph">it&#8217;s a sequence of bytes and i say hello you know this is how you would do this in x84 60 x 84 64</p>



<p class="wp-block-paragraph">yasm assembly on ubuntu or just linux then uh you know this string right here is just a string with</p>



<p class="wp-block-paragraph">null terminate with no null terminator you want to add a null terminator to the end of it so that</p>



<p class="wp-block-paragraph">null terminator to the end of it so that you can write a better function that would scan your string</p>



<p class="wp-block-paragraph">and print it without knowing in advance how long it was then you just do a comma zero at the end</p>



<p class="wp-block-paragraph">and now you can easily write a function to scan or read or process your strings because this is</p>



<p class="wp-block-paragraph">really what&#8217;s what&#8217;s happening under the hood it&#8217;s just like a sequence of characters and then it</p>



<p class="wp-block-paragraph">ends with the zero and there you go that&#8217;s the basics of c strings with null terminators</p>



<p class="wp-block-paragraph">this video i hope you had fun and learned a little bit of stuff 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 if you could do me a please</p>



<p class="wp-block-paragraph">a small little favor could you please subscribe and follow this channel or these videos or whatever</p>



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



<p class="wp-block-paragraph">looking at right now, 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. 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 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</p>



<p class="wp-block-paragraph">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 troll me if you</p>



<p class="wp-block-paragraph">You could troll me if you want to just wake me up in the middle of the night, just subscribe.</p>



<p class="wp-block-paragraph">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</p>



<p class="wp-block-paragraph">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.</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</p>



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



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



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



<p class="wp-block-paragraph">please leave a comment.</p>



<p class="wp-block-paragraph">Or if you just want to say, hey, what&#8217;s up?</p>



<p class="wp-block-paragraph">What&#8217;s going on?</p>



<p class="wp-block-paragraph">You know, just send me a comment.</p>



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



<p class="wp-block-paragraph">I also wake up for those in the middle of the night.</p>



<p class="wp-block-paragraph">I wake up in a cold sweat and I&#8217;m like,</p>



<p class="wp-block-paragraph">It would really mean the world to me.</p>



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



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



<p class="wp-block-paragraph">And enjoy the cool music as I fade into the darkness.</p>



<p class="wp-block-paragraph">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/why-c-strings-end-with-0-null-terminated-strings-explained-simply/">Why C Strings End with \0 &#8211; Null Terminated Strings Explained Simply</a> appeared first on <a href="https://www.NeuralLantern.com">NeuralLantern.com</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.NeuralLantern.com/why-c-strings-end-with-0-null-terminated-strings-explained-simply/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Master File Copying with System Calls in x86-64 YASM Assembly</title>
		<link>https://www.NeuralLantern.com/master-file-copying-with-system-calls-in-x86-64-yasm-assembly/</link>
					<comments>https://www.NeuralLantern.com/master-file-copying-with-system-calls-in-x86-64-yasm-assembly/#respond</comments>
		
		<dc:creator><![CDATA[mike]]></dc:creator>
		<pubDate>Sun, 05 Oct 2025 11:22:39 +0000</pubDate>
				<category><![CDATA[Assembly Language]]></category>
		<category><![CDATA[Coding]]></category>
		<category><![CDATA[Computer Science]]></category>
		<category><![CDATA[Videos]]></category>
		<category><![CDATA[assembly language]]></category>
		<category><![CDATA[assembly programming]]></category>
		<category><![CDATA[coding]]></category>
		<category><![CDATA[file copying]]></category>
		<category><![CDATA[file handling]]></category>
		<category><![CDATA[file I/O]]></category>
		<category><![CDATA[file permissions]]></category>
		<category><![CDATA[Linux system calls]]></category>
		<category><![CDATA[low-level programming]]></category>
		<category><![CDATA[MD5 checksum]]></category>
		<category><![CDATA[programming tutorial]]></category>
		<category><![CDATA[software development]]></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=237</guid>

					<description><![CDATA[<p>Learn file copying in x86-64 YASM assembly with system calls. This tutorial covers opening files, using looping buffers, and verifying with MD5 checksums.</p>
<p>The post <a href="https://www.NeuralLantern.com/master-file-copying-with-system-calls-in-x86-64-yasm-assembly/">Master File Copying with System Calls in x86-64 YASM 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 title="Master File Copying with System Calls in x86-64 YASM Assembly" width="1380" height="776" src="https://www.youtube.com/embed/hvq4VGqA6Ks?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 me as I break down a simple yet powerful x86-64 YASM assembly program to copy files using system calls! Learn how to open input files, create output files, and use a looping buffer for efficient data transfer. I’ll demo the code, explain file handles, permissions, and error handling, and even verify the copy with MD5 checksums. Perfect for intermediate assembly programmers or anyone curious about low-level file operations. Check out my other videos for more assembly tips, and don’t forget to subscribe!</p>



<p class="wp-block-paragraph">Introduction to File Copy Program 00:00:00<br>System Calls in YASM Assembly 00:00:06<br>Opening Input File 00:00:17<br>Creating Output File 00:00:22<br>Using Looping Buffer 00:00:28<br>Assembly Program Prerequisites 00:00:37<br>Overview of Source File 00:00:53<br>Data Section and Strings 00:01:06<br>Copy Buffer Length 00:01:21<br>File Permissions Explanation 00:02:05<br>System Call Codes 00:02:57<br>File Descriptors and Exit Codes 00:03:24<br>Text Section and Entry Point 00:03:45<br>Welcome Message Function 00:04:01<br>Print Null Terminated String 00:04:16<br>Running Initial Program 00:06:26<br>MD5 Checksum Explanation 00:07:00<br>File Tests Function Introduction 00:07:28<br>Open File Read Function 00:09:52<br>Checking File Handle 00:14:00<br>File Handle Concept 00:14:25<br>Error Handling Importance 00:15:24<br>Testing File Open 00:23:00<br>Create File Function 00:31:16<br>Testing File Creation 00:32:36<br>Copy File Function 00:36:14<br>Stack Buffer Creation 00:38:16<br>While Loop for Copying 00:40:14<br>Read System Call 00:40:47<br>Write System Call 00:45:03<br>Checking Read/Write Operations 00:46:51<br>Final Program Run 00:49:00<br>Verifying File Copy with MD5 00:49:22<br>Testing with Larger Input 00:51:20<br>Optimizing Buffer Size 00:53:41<br>Conclusion and Call to Subscribe 00:54:41</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 and explain a simple program that copies a file</p>



<p class="wp-block-paragraph">using system calls in an x8664 yasm assembly program</p>



<p class="wp-block-paragraph">we&#8217;re going to use the system calls to open an input file and read characters from it we&#8217;re</p>



<p class="wp-block-paragraph">going to use another system call to create a destination file and write characters to it</p>



<p class="wp-block-paragraph">we&#8217;re going to use a looping buffer,</p>



<p class="wp-block-paragraph">which should be kind of fun in the middle.</p>



<p class="wp-block-paragraph">And I&#8217;m just going to do my best to explain as much as I can.</p>



<p class="wp-block-paragraph">I should say though that before you can watch this video,</p>



<p class="wp-block-paragraph">you probably already need to know how to program an assembly,</p>



<p class="wp-block-paragraph">at least in a basic level, Yasm assembly.</p>



<p class="wp-block-paragraph">And so if you don&#8217;t know how to do that yet,</p>



<p class="wp-block-paragraph">this video is probably going to be confusing for you.</p>



<p class="wp-block-paragraph">You probably need to look at my other videos dealing with Yasm assembly and</p>



<p class="wp-block-paragraph">system calls and so forth.</p>



<p class="wp-block-paragraph">So I&#8217;m just going to show you real fast what&#8217;s actually going on in this source</p>



<p class="wp-block-paragraph">actually going on in this source file so far.</p>



<p class="wp-block-paragraph">So this is my assembly program.</p>



<p class="wp-block-paragraph">It&#8217;s not finished.</p>



<p class="wp-block-paragraph">I&#8217;m going to write it on screen for you.</p>



<p class="wp-block-paragraph">For the most part, you can see I&#8217;ve got a data section here and then I just have a</p>



<p class="wp-block-paragraph">bunch of null terminated strings.</p>



<p class="wp-block-paragraph">So I&#8217;ve got a string saying, hey, the module started.</p>



<p class="wp-block-paragraph">We&#8217;re about to open the file.</p>



<p class="wp-block-paragraph">We&#8217;re about to create the file.</p>



<p class="wp-block-paragraph">We failed to do something.</p>



<p class="wp-block-paragraph">We&#8217;re done copying.</p>



<p class="wp-block-paragraph">You know, we terminated the program, you know, whatever.</p>



<p class="wp-block-paragraph">Right. So I just have some strings.</p>



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



<p class="wp-block-paragraph">Down here, I have something called the copy buffer length, which is which is just</p>



<p class="wp-block-paragraph">length with just which is just the size of the buffer that I&#8217;m going to use</p>



<p class="wp-block-paragraph">between the input file and the output file so a two byte buffer is really</p>



<p class="wp-block-paragraph">inefficient it&#8217;s too small but I made it two bytes just to show to you that are</p>



<p class="wp-block-paragraph">looping you know a read area is going to actually work because if I make a buffer</p>



<p class="wp-block-paragraph">that&#8217;s too big if it&#8217;s bigger than the file then we won&#8217;t actually know if the</p>



<p class="wp-block-paragraph">if the buffer loop works or not so I&#8217;m just gonna put two here could change</p>



<p class="wp-block-paragraph">that later if we wanted to we&#8217;re gonna be reading from a file called input and</p>



<p class="wp-block-paragraph">called input and writing to a file called output so that&#8217;s no big deal in order to open a file with</p>



<p class="wp-block-paragraph">a system call for read mode we&#8217;re just going to use a zero as the flags that just means read mode</p>



<p class="wp-block-paragraph">and then for the creation of the file we&#8217;re going to use some standard permissions this is not a</p>



<p class="wp-block-paragraph">permissions video I&#8217;m going to probably release a permissions video at some later point in time</p>



<p class="wp-block-paragraph">not too important right now but basically this 640 is the heart of what permissions we&#8217;re actually</p>



<p class="wp-block-paragraph">what permissions we&#8217;re actually looking at.</p>



<p class="wp-block-paragraph">Q is quad word.</p>



<p class="wp-block-paragraph">And six just means that the owner can read</p>



<p class="wp-block-paragraph">and write to the file.</p>



<p class="wp-block-paragraph">Four means that anyone who is in the same group</p>



<p class="wp-block-paragraph">as the file has been assigned to,</p>



<p class="wp-block-paragraph">which is usually the owner,</p>



<p class="wp-block-paragraph">can just read it but not write to it.</p>



<p class="wp-block-paragraph">And then everybody else,</p>



<p class="wp-block-paragraph">people who are not the owner</p>



<p class="wp-block-paragraph">and who are not in the right group,</p>



<p class="wp-block-paragraph">they have no access to it.</p>



<p class="wp-block-paragraph">Just a simple security feature of Linux</p>



<p class="wp-block-paragraph">for file permissions.</p>



<p class="wp-block-paragraph">You can go a lot further than this in the terminal,</p>



<p class="wp-block-paragraph">in the terminal, but for this assembly program,</p>



<p class="wp-block-paragraph">we&#8217;re just gonna use basics.</p>



<p class="wp-block-paragraph">We&#8217;re not gonna use ACLs.</p>



<p class="wp-block-paragraph">I don&#8217;t even know how to do that in assembly.</p>



<p class="wp-block-paragraph">System call codes.</p>



<p class="wp-block-paragraph">So again, if you don&#8217;t understand system calls,</p>



<p class="wp-block-paragraph">you gotta watch my other videos first,</p>



<p class="wp-block-paragraph">but we have a code of zero to read from a file,</p>



<p class="wp-block-paragraph">one to write to a file, two to open a file,</p>



<p class="wp-block-paragraph">three to close and open file.</p>



<p class="wp-block-paragraph">Probably should have put that one before create,</p>



<p class="wp-block-paragraph">but I don&#8217;t really care.</p>



<p class="wp-block-paragraph">85 is the code to create a file for writing,</p>



<p class="wp-block-paragraph">to create a file for writing and then 60 is the code to exit from the program.</p>



<p class="wp-block-paragraph">So then we have file descriptors.</p>



<p class="wp-block-paragraph">Descriptor number one is a standard output.</p>



<p class="wp-block-paragraph">Descriptor number two is standard error.</p>



<p class="wp-block-paragraph">And it&#8217;s always one and two, no matter what program you&#8217;re dealing with.</p>



<p class="wp-block-paragraph">Unless you have some kind of really crazy non-standard thing going on.</p>



<p class="wp-block-paragraph">And then for the exit codes, we&#8217;re just going to say exit zero for success and exit one</p>



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



<p class="wp-block-paragraph">Now to the actual tech section where our instructions are in the assembly program.</p>



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



<p class="wp-block-paragraph">So you can see section text here.</p>



<p class="wp-block-paragraph">We&#8217;ve got a global entry point called underscore start.</p>



<p class="wp-block-paragraph">So this is not a GCC program that requires a main entry point.</p>



<p class="wp-block-paragraph">This is a pure assembly program that requires</p>



<p class="wp-block-paragraph">an underscore start entry point.</p>



<p class="wp-block-paragraph">Again, see my other videos for more assembly explanation.</p>



<p class="wp-block-paragraph">I&#8217;m gonna call on a method,</p>



<p class="wp-block-paragraph">well, not a method of regular function called welcome.</p>



<p class="wp-block-paragraph">Just to print a little welcome message,</p>



<p class="wp-block-paragraph">you can see right here, that&#8217;s all that&#8217;s doing.</p>



<p class="wp-block-paragraph">It&#8217;s just loading, you know, a string,</p>



<p class="wp-block-paragraph">sending it to standard output, and calling on a helper function that I made called print null</p>



<p class="wp-block-paragraph">terminated string. When it&#8217;s done with that, it just uses the call code to exit the program.</p>



<p class="wp-block-paragraph">No big deal. Nothing too advanced so far. I have the tests function commented out because I want</p>



<p class="wp-block-paragraph">to write that in front of you. And if you look at what I have inside of print null terminated string,</p>



<p class="wp-block-paragraph">this is not the point of the video, so I&#8217;m just going to skim it. It&#8217;s not super important.</p>



<p class="wp-block-paragraph">called print null terminated string that takes in a pointer to a character array</p>



<p class="wp-block-paragraph">and a file handle for where you want to print it and what it&#8217;ll do is it&#8217;ll come</p>



<p class="wp-block-paragraph">in grab the incoming arguments it&#8217;s only got two arguments so it uses r12 and r13</p>



<p class="wp-block-paragraph">to store those arguments and then r14 is the result of calling another helper</p>



<p class="wp-block-paragraph">called string length you can imagine my string length function just kind of</p>



<p class="wp-block-paragraph">scans the string until it finds a null terminator aka the number zero not the</p>



<p class="wp-block-paragraph">zero but actually a zero in order to figure out how long the string is and then it can use a</p>



<p class="wp-block-paragraph">regular system call to print the string to the right file descriptor so we&#8217;re going to be doing</p>



<p class="wp-block-paragraph">most mostly this kind of this exact same thing when we copy the file so I&#8217;m just definitely not</p>



<p class="wp-block-paragraph">going to explain it if you don&#8217;t know pushes and pops and epilogues and prologues see my other</p>



<p class="wp-block-paragraph">videos crlf is just going to print a carriage return new line so it or sorry carriage return</p>



<p class="wp-block-paragraph">sorry, carriage return line feed.</p>



<p class="wp-block-paragraph">So that&#8217;s just like making the cursor go to the next line.</p>



<p class="wp-block-paragraph">Not a big deal.</p>



<p class="wp-block-paragraph">We just print a string basically.</p>



<p class="wp-block-paragraph">And then I have a custom die function</p>



<p class="wp-block-paragraph">that allows us to die with a failure code of one.</p>



<p class="wp-block-paragraph">Oh, I forgot to mark that.</p>



<p class="wp-block-paragraph">Let&#8217;s see, 206 exit fail.</p>



<p class="wp-block-paragraph">I&#8217;m just gonna put that down here.</p>



<p class="wp-block-paragraph">Yeah, instead of hard coding values,</p>



<p class="wp-block-paragraph">it&#8217;s a lot better to use variables if you can or defines.</p>



<p class="wp-block-paragraph">So all that&#8217;s gonna do is just kind of print</p>



<p class="wp-block-paragraph">an error message and then exit</p>



<p class="wp-block-paragraph">then exit with the you know the appropriate</p>



<p class="wp-block-paragraph">Exit code which is just going to be one because this is a simple program</p>



<p class="wp-block-paragraph">So CRLF print an alternated string dying string length a welcome message all this fun pretty stuff</p>



<p class="wp-block-paragraph">That doesn&#8217;t really do anything except make the program more fun to look at so I&#8217;m going to run it here and make sure that actually works</p>



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



<p class="wp-block-paragraph">Nice, okay, so if I run it you can see that the the make file system again</p>



<p class="wp-block-paragraph">file system again this is not a make file video so I&#8217;m not going to show you</p>



<p class="wp-block-paragraph">my make file see my other videos if you want to learn more about make files but</p>



<p class="wp-block-paragraph">you can see that the assembly program says that it started and then it just</p>



<p class="wp-block-paragraph">exited then the make file continues to run these extra commands that I have</p>



<p class="wp-block-paragraph">set up so this is not part of the assembly program this is just taking a</p>



<p class="wp-block-paragraph">checksum an MD5 checksum of the input file and then an MD5 checksum of the</p>



<p class="wp-block-paragraph">what empty five checksums are i&#8217;ll probably release a video sometime in the future if you&#8217;re</p>



<p class="wp-block-paragraph">interested on all my platforms just talking about why we why you would use a checksum but for now</p>



<p class="wp-block-paragraph">just imagine this is a fingerprint so if the fingerprints don&#8217;t match that means the files</p>



<p class="wp-block-paragraph">don&#8217;t match right now the output file doesn&#8217;t even exist so it just is an error and that&#8217;s why the</p>



<p class="wp-block-paragraph">make file thinks there&#8217;s an error because the output file didn&#8217;t exist so that&#8217;s the basics</p>



<p class="wp-block-paragraph">of kind of getting started with this you know bare bones program now let&#8217;s start looking at running</p>



<p class="wp-block-paragraph">program. Now let&#8217;s start looking at running the file tests. So file tests, we got to make a new</p>



<p class="wp-block-paragraph">method. I&#8217;m going to stick it down here. Let&#8217;s see. I got a solution up so that I don&#8217;t have to</p>



<p class="wp-block-paragraph">spend too much time typing. I&#8217;m going to try my best to balance between copy pasting and just</p>



<p class="wp-block-paragraph">typing very quickly. But let&#8217;s see, where&#8217;s the file test? So we got this. I&#8217;m going to put the</p>



<p class="wp-block-paragraph">I&#8217;m feeling pretty lazy.</p>



<p class="wp-block-paragraph">I&#8217;m just going to copy paste the whole thing.</p>



<p class="wp-block-paragraph">Copy paste the whole thing.</p>



<p class="wp-block-paragraph">So now this method right here is going to get called the file tests.</p>



<p class="wp-block-paragraph">I keep saying method because I teach C++ a lot too.</p>



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



<p class="wp-block-paragraph">If we go down further, this function is called file tests.</p>



<p class="wp-block-paragraph">Here&#8217;s the signature.</p>



<p class="wp-block-paragraph">It doesn&#8217;t take any arguments.</p>



<p class="wp-block-paragraph">It just kind of does stuff.</p>



<p class="wp-block-paragraph">It doesn&#8217;t return anything.</p>



<p class="wp-block-paragraph">And here are the registers that I&#8217;m going to use.</p>



<p class="wp-block-paragraph">the input file handle, the output file handle, and then the count of bytes read from the input</p>



<p class="wp-block-paragraph">file at any given time inside of our looping buffer reader section. So the first thing I&#8217;ve</p>



<p class="wp-block-paragraph">got to do is I&#8217;ve got to open a file to read. The second thing I&#8217;ve got to do is create a file to</p>



<p class="wp-block-paragraph">write. And then I&#8217;m going to copy the input file to the output file. And then I&#8217;m going to print</p>



<p class="wp-block-paragraph">a message saying, hey, everything was successful. And then I&#8217;m going to close both files. Notice how</p>



<p class="wp-block-paragraph">anything i&#8217;m just sort of calling other functions do the work for me again if you don&#8217;t know the</p>



<p class="wp-block-paragraph">prologue and epilogue stuff or calling functions and returning you should see my other videos</p>



<p class="wp-block-paragraph">but i just like to use helper functions assembly is like so unwieldy right it&#8217;s it just gets out</p>



<p class="wp-block-paragraph">of control so quick and so confusing so fast so anytime you can just you know take a chunk out</p>



<p class="wp-block-paragraph">of your assembly code and move it somewhere to another module or to another function</p>



<p class="wp-block-paragraph">going to make your life a lot easier and make debugging a lot easier people who try to write</p>



<p class="wp-block-paragraph">the entire portion of their program in just one gigantic function those are the people who usually</p>



<p class="wp-block-paragraph">end up spending 10 times longer debugging for no reason at all so i believe in the power of modular</p>



<p class="wp-block-paragraph">thinking so anyway what are we going to do inside of the open file read function it&#8217;s not too bad</p>



<p class="wp-block-paragraph">to be honest let&#8217;s see if i can find it real quick open file read i&#8217;m going to copy paste it because</p>



<p class="wp-block-paragraph">Again, I&#8217;m pretty lazy.</p>



<p class="wp-block-paragraph">I&#8217;ll just explain what it is though.</p>



<p class="wp-block-paragraph">So down here we need a function called open file read.</p>



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



<p class="wp-block-paragraph">And you can see the signature that I&#8217;ve chosen for this is, you know, I like to</p>



<p class="wp-block-paragraph">write all my functions in C++, uh, kind of lingo or prototypes so that I can have a</p>



<p class="wp-block-paragraph">better understanding of what the assembly is actually supposed to do.</p>



<p class="wp-block-paragraph">So you can see the function called open file read.</p>



<p class="wp-block-paragraph">I want it to take two arguments.</p>



<p class="wp-block-paragraph">two arguments the first argument should be a pointer to a string that represents the file</p>



<p class="wp-block-paragraph">name that i want to open and it should be null terminated again if you look back at my uh at</p>



<p class="wp-block-paragraph">my strings up here they all have little zeros at the end so they are all null terminated anyway uh</p>



<p class="wp-block-paragraph">so that&#8217;s the first argument that&#8217;s going to show up as rdi in assembly and then the second argument</p>



<p class="wp-block-paragraph">is the flags for opening the file so i think probably that was redundant if i if i name this</p>



<p class="wp-block-paragraph">that was redundant if i if i name this open file read then i think it should be obvious that the</p>



<p class="wp-block-paragraph">flags are just going to be the read flags only so i probably didn&#8217;t even need to provide this</p>



<p class="wp-block-paragraph">that was bad design on my part you could write a better one on your own where it&#8217;s just one argument</p>



<p class="wp-block-paragraph">and if the name is read then just use the read flags but if the maybe if you want to leave the</p>



<p class="wp-block-paragraph">flags in there you could just say open file only it&#8217;s up to you anyway so it attempts to open a</p>



<p class="wp-block-paragraph">successful it&#8217;ll return the file handle that&#8217;s the long return type right here</p>



<p class="wp-block-paragraph">long file handle if it fails then it will just basically complain and exit</p>



<p class="wp-block-paragraph">the program you probably want a more sophisticated way of handling errors in</p>



<p class="wp-block-paragraph">your program I just decided to complain and exit the entire program because this</p>



<p class="wp-block-paragraph">is you know not supposed to be super complicated I just want to show you how</p>



<p class="wp-block-paragraph">to copy files and then for me I like to leave comments that just sort of explain</p>



<p class="wp-block-paragraph">And once my functions get so complicated that I actually run out of registers,</p>



<p class="wp-block-paragraph">then that tells me I probably need to just make another function,</p>



<p class="wp-block-paragraph">you know, split the work up in some way.</p>



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



<p class="wp-block-paragraph">I only end up reusing the same registers for multiple purposes occasionally.</p>



<p class="wp-block-paragraph">I&#8217;m not a hardcore assembly programmer.</p>



<p class="wp-block-paragraph">I&#8217;m just, you know, I&#8217;m like medium.</p>



<p class="wp-block-paragraph">So I&#8217;m going to use R12 and 13 and 14.</p>



<p class="wp-block-paragraph">That&#8217;s why we have the prolog pushing all those registers to preserve them</p>



<p class="wp-block-paragraph">pushing all those registers to preserve them because they&#8217;re callee saved and then the epilogue</p>



<p class="wp-block-paragraph">epilogue that just kind of pops them i&#8217;ve got a label here for the function remember a function</p>



<p class="wp-block-paragraph">is just a label and then a return statement as long as you obey and respect the abi like preserving</p>



<p class="wp-block-paragraph">certain registers then you should be okay first thing i&#8217;m going to do is grab the incoming</p>



<p class="wp-block-paragraph">arguments so i&#8217;m going to grab the file name which is a pointer which means it&#8217;s just a 64-bit</p>



<p class="wp-block-paragraph">integer i can stick that into a register and then the flags same thing so i&#8217;m going to grab</p>



<p class="wp-block-paragraph">rdi and rsi the incoming arguments i&#8217;m going to stick them into r12 and r13 so the character</p>



<p class="wp-block-paragraph">pointer in the flags and then i&#8217;m going to attempt to open the file with a system call so</p>



<p class="wp-block-paragraph">system call is right there line 168 if you look up the table for system calls in my favorite book</p>



<p class="wp-block-paragraph">that i usually recommend or any table that knows the system call codes for x86-64 assembly</p>



<p class="wp-block-paragraph">call code to open a file is some number i&#8217;ve assigned that to sys open if we look up here</p>



<p class="wp-block-paragraph">open is code 2 so that&#8217;s why i you know i don&#8217;t want to remember the numbers it&#8217;s bad to hard</p>



<p class="wp-block-paragraph">code number so i just i just put it as a define and then the first argument that it wants in rdi</p>



<p class="wp-block-paragraph">is the name of the file so i just gave it r12 i guess i probably could have just used rdi</p>



<p class="wp-block-paragraph">directly in the system call but that tends to make me nervous reusing the argument registers</p>



<p class="wp-block-paragraph">registers, I like to have them somewhere where they&#8217;re not going to be destroyed.</p>



<p class="wp-block-paragraph">If I like, let&#8217;s say I accidentally added some code here on line 163.</p>



<p class="wp-block-paragraph">If I wanted to reuse RDI and I accidentally added some code there,</p>



<p class="wp-block-paragraph">then RDI would have been destroyed.</p>



<p class="wp-block-paragraph">Would have cost me a bunch of time debugging my program.</p>



<p class="wp-block-paragraph">Although I admit it&#8217;s not super efficient to do it that way.</p>



<p class="wp-block-paragraph">Then we&#8217;re going to do the file status</p>



<p class="wp-block-paragraph">flags as the second argument, and then we just do a system call right away.</p>



<p class="wp-block-paragraph">register gives you the result of your system call.</p>



<p class="wp-block-paragraph">Notice how we use RAX to send in the call code.</p>



<p class="wp-block-paragraph">And then the system call sends us back its return result also in the RAX</p>



<p class="wp-block-paragraph">register. So I&#8217;m just going to save that right away to R14.</p>



<p class="wp-block-paragraph">You can see up here, it&#8217;s just the file handle.</p>



<p class="wp-block-paragraph">So we can assume maybe at this point that we have a file handle sitting in R14.</p>



<p class="wp-block-paragraph">You know, what is a file handle when you ask the system to open a file for you?</p>



<p class="wp-block-paragraph">The operating system under the hood is just going to do a bunch of stuff to</p>



<p class="wp-block-paragraph">bunch of stuff to actually open a file it&#8217;s going to go it&#8217;s going to take the string</p>



<p class="wp-block-paragraph">that you sent it and it&#8217;s going to parse it and figure out you know how do i how do i interpret</p>



<p class="wp-block-paragraph">where that actually is on disk i&#8217;m going to look at like the file system in the past that you</p>



<p class="wp-block-paragraph">provided i&#8217;m going to look at the mount points and i&#8217;m going to figure out like where exactly on disk</p>



<p class="wp-block-paragraph">does that file start and then the operating system stores that the operating system stores the file</p>



<p class="wp-block-paragraph">name it stores where you&#8217;re looking at the file currently stores a bunch of stuff that you don&#8217;t</p>



<p class="wp-block-paragraph">a bunch of stuff that you don&#8217;t want to have to remember you know it creates data structures under</p>



<p class="wp-block-paragraph">the hood and all it&#8217;ll give you back in return is a file handle for simplicity because then later</p>



<p class="wp-block-paragraph">you can use that file handle to just sort of say I would like to write some bytes to a file or I</p>



<p class="wp-block-paragraph">would like to read some bytes from an open file here&#8217;s the handle you gave me previously and then</p>



<p class="wp-block-paragraph">it&#8217;ll just work assuming you have a valid handle so the handle is kind of the most important part</p>



<p class="wp-block-paragraph">failed because it&#8217;s a mistake i&#8217;ve said this in other videos it&#8217;s a mistake that new programmers</p>



<p class="wp-block-paragraph">make or lazy programmers make uh let&#8217;s suppose for the sake of argument uh uh file open sys call</p>



<p class="wp-block-paragraph">pretend we&#8217;re in c plus plus and there is some sort of an api function that we can call either</p>



<p class="wp-block-paragraph">directly to the system or some person&#8217;s library and it&#8217;ll open a file for us so maybe like a</p>



<p class="wp-block-paragraph">just say we call this right so i&#8217;m going to do like a little comment if inside of your program you just</p>



<p class="wp-block-paragraph">call it with the you know some some path and then you assume that what you have is a valid you know</p>



<p class="wp-block-paragraph">handle if you assume that what you got back is a valid handle or maybe this is not a function</p>



<p class="wp-block-paragraph">just assume that the call succeeded, your program is probably going to have errors when you least</p>



<p class="wp-block-paragraph">expect it. And it&#8217;s not going to look good. Especially if you release a function like this</p>



<p class="wp-block-paragraph">to the public, or if you have like a professor who&#8217;s like grading your code and they are testing</p>



<p class="wp-block-paragraph">to see if you&#8217;re checking for return codes and stuff like that. That&#8217;s not a smart idea, right?</p>



<p class="wp-block-paragraph">Like you shouldn&#8217;t, should not, shouldn&#8217;t proceed as if everything went according to plan. What,</p>



<p class="wp-block-paragraph">just proceeding as if everything went according to plan.</p>



<p class="wp-block-paragraph">You want to use an if statement, right?</p>



<p class="wp-block-paragraph">You want to say if the handle has some value that seems to be valid,</p>



<p class="wp-block-paragraph">like for example, more than zero in the case of opening a file,</p>



<p class="wp-block-paragraph">I would say probably more than two because all of our programs</p>



<p class="wp-block-paragraph">always have automatically assigned file handles of zero and one and two</p>



<p class="wp-block-paragraph">to represent standard input, standard output, and standard error.</p>



<p class="wp-block-paragraph">But I think usually people just say like,</p>



<p class="wp-block-paragraph">if it&#8217;s greater than zero, then it&#8217;s fine.</p>



<p class="wp-block-paragraph">zero then it&#8217;s fine uh you know for me i might put greater than two but more than zero is fine</p>



<p class="wp-block-paragraph">the point is check to make sure that it actually succeeded</p>



<p class="wp-block-paragraph">do i have spell check on this thing oh god you&#8217;re all going to see my true spelling let me see if i</p>



<p class="wp-block-paragraph">can get it on real fast plug-ins what how come this oh okay now it&#8217;s highlighted</p>



<p class="wp-block-paragraph">If we&#8217;ve succeeded based on some kind of a comparison of the return result,</p>



<p class="wp-block-paragraph">then we&#8217;ll proceed in one way.</p>



<p class="wp-block-paragraph">Otherwise we&#8217;ll respond to the error by, you know, doing something else.</p>



<p class="wp-block-paragraph">Somehow, like writing a log file, sending an email, complaining to the user,</p>



<p class="wp-block-paragraph">doing any number of things where you can actually respond to the error.</p>



<p class="wp-block-paragraph">Maybe you probably want to change your execution path.</p>



<p class="wp-block-paragraph">Like if the file successfully opened, then go ahead and start writing to it</p>



<p class="wp-block-paragraph">or reading from it or whatever.</p>



<p class="wp-block-paragraph">But if the file did not successfully open,</p>



<p class="wp-block-paragraph">want to do something else in the program and not just start trying to read from the file so anyway</p>



<p class="wp-block-paragraph">super super good idea and so that&#8217;s why i&#8217;m going to implement that inside of assembly 2. so instead</p>



<p class="wp-block-paragraph">of just immediately using the file handle i&#8217;m going to check it i&#8217;m going to say let&#8217;s compare</p>



<p class="wp-block-paragraph">it to the number zero if we succeeded then let&#8217;s go to another label called uh read success i</p>



<p class="wp-block-paragraph">personally when i&#8217;m doing branching logic i like to say open file read is the name of the function</p>



<p class="wp-block-paragraph">read is the name of the function and I&#8217;ll just I&#8217;ll just uh uh append some kind of a suffix</p>



<p class="wp-block-paragraph">to the original function name that way it&#8217;s it&#8217;s easier to avoid collisions when you have lots of</p>



<p class="wp-block-paragraph">functions and lots of labels and things like that so I&#8217;m basically saying if we succeeded I&#8217;m going</p>



<p class="wp-block-paragraph">to jump to this label which is down here and so uh this video also is not about branching logic</p>



<p class="wp-block-paragraph">and and how to implement those instructions you can probably infer it by looking at my code but</p>



<p class="wp-block-paragraph">it by looking at my code but you know see another video in the future for that topic anyway if we</p>



<p class="wp-block-paragraph">succeeded in opening the file handle then we&#8217;re just going to say oh we were successful and then</p>



<p class="wp-block-paragraph">we&#8217;re going to uh um let&#8217;s see we&#8217;re going to print the name of the file that we just successfully</p>



<p class="wp-block-paragraph">opened and then we&#8217;re going to send the file handle into rax so that this function has a return</p>



<p class="wp-block-paragraph">So when you open a file to read successfully, the caller will receive the file handle.</p>



<p class="wp-block-paragraph">You might be wondering yourself, wait a minute, wait a minute, wait a minute.</p>



<p class="wp-block-paragraph">It just gave us the file handle in RAX.</p>



<p class="wp-block-paragraph">Remember, we got to respect the ABI.</p>



<p class="wp-block-paragraph">Anytime we jump anywhere or call another function or call another syscall,</p>



<p class="wp-block-paragraph">which a lot of these things do,</p>



<p class="wp-block-paragraph">we will probably lose the value of registers that are not callee preserved.</p>



<p class="wp-block-paragraph">And that&#8217;s definitely RAX.</p>



<p class="wp-block-paragraph">I mean, just doing any system call like this print null terminated string function does.</p>



<p class="wp-block-paragraph">print null terminated string function does that&#8217;s going to destroy rex so that&#8217;s why i saved it away</p>



<p class="wp-block-paragraph">first in r14 then at the very end of the function right before i return i&#8217;m just going to grab r14</p>



<p class="wp-block-paragraph">and send it into rex again respect the abi do not return data as a return value in r14 or any other</p>



<p class="wp-block-paragraph">register you have to use the designated registers and in this case an integer that you&#8217;re returning</p>



<p class="wp-block-paragraph">x anyway so that&#8217;s the gist of that let me go back up for a second uh so notice how</p>



<p class="wp-block-paragraph">we&#8217;re we&#8217;re sort of comparing oh gosh i just i just reconfigured my annotator and i bought a</p>



<p class="wp-block-paragraph">new drawpad i wonder if this is going to work ah it works there&#8217;s a bunch of stuff i added too</p>



<p class="wp-block-paragraph">so notice how uh we try to open the file and then we sort of compare the file handle to see if we</p>



<p class="wp-block-paragraph">off to the success area down here oh my green&#8217;s not working oh it&#8217;s tricky it&#8217;s tricky i gotta</p>



<p class="wp-block-paragraph">hit it in a certain way there we go if it succeeds we go down here to the success label so we</p>



<p class="wp-block-paragraph">basically at that point um totally ignore all of the fail code right we&#8217;re just like branching</p>



<p class="wp-block-paragraph">around on the other hand if it fails then execution falls through because it&#8217;s not going to jump</p>



<p class="wp-block-paragraph">if uh if if r14 is not greater than or equal to zero so if we fail maybe i should put this in a</p>



<p class="wp-block-paragraph">red if we fail then it&#8217;s going to fall through to the next label and then the next instructions</p>



<p class="wp-block-paragraph">some people like to say let&#8217;s uh let&#8217;s jump to the success label and then if not let&#8217;s jump to</p>



<p class="wp-block-paragraph">the fail label that can buy you a little bit more uh i don&#8217;t know jump length if you have a giant</p>



<p class="wp-block-paragraph">program but in this case i&#8217;m just going to let it fall through it saves us one jump instruction</p>



<p class="wp-block-paragraph">and so if it fails then it&#8217;s going to say first off let&#8217;s print a message that we failed</p>



<p class="wp-block-paragraph">to standard error if you want a refresher on standard input standard output and standard error</p>



<p class="wp-block-paragraph">see my other videos and then it&#8217;s going to print the name of the file that failed so that&#8217;s this</p>



<p class="wp-block-paragraph">part right here it&#8217;s going to say hey we we failed to print or sorry we failed to open this file name</p>



<p class="wp-block-paragraph">for reading and it&#8217;s going to print a new line there with the crlf thing</p>



<p class="wp-block-paragraph">and then it&#8217;s actually going to exit the program at that point it&#8217;s going to say all right we</p>



<p class="wp-block-paragraph">failed and so the whole program just just quits and that&#8217;s my die function that i showed you</p>



<p class="wp-block-paragraph">earlier it&#8217;s just going to call the system call code for quitting and it&#8217;s going to give a return</p>



<p class="wp-block-paragraph">code of one to indicate to anyone automating our program including new make that well our program</p>



<p class="wp-block-paragraph">failed at least it failed okay so we got all that and then we got the success and so now you kind</p>



<p class="wp-block-paragraph">of know the idea behind opening a file let&#8217;s do that real fast just for fun so file tests</p>



<p class="wp-block-paragraph">um oh i know what to do i&#8217;ll just comment out these uh instructions just so that we&#8217;re only</p>



<p class="wp-block-paragraph">doing the file open yeah that&#8217;s pretty good and then we won&#8217;t close anything just yet</p>



<p class="wp-block-paragraph">so now let me run this in the terminal real fast make run it says it successfully opened</p>



<p class="wp-block-paragraph">it says it successfully opened the file input.txt</p>



<p class="wp-block-paragraph">and then the program exited.</p>



<p class="wp-block-paragraph">No problem, that error code one,</p>



<p class="wp-block-paragraph">that&#8217;s because the output file doesn&#8217;t actually exist.</p>



<p class="wp-block-paragraph">So don&#8217;t worry about that.</p>



<p class="wp-block-paragraph">And let&#8217;s change the name of the input file</p>



<p class="wp-block-paragraph">just to show you that it can fail and we can detect it.</p>



<p class="wp-block-paragraph">I&#8217;m gonna put a two there</p>



<p class="wp-block-paragraph">so that the program will try to open an input file</p>



<p class="wp-block-paragraph">that doesn&#8217;t actually exist.</p>



<p class="wp-block-paragraph">And then I&#8217;ll run it again.</p>



<p class="wp-block-paragraph">Notice how it says fail to open file input2.txt.</p>



<p class="wp-block-paragraph">And then it says terminating program after failure to open file.</p>



<p class="wp-block-paragraph">And then this time, notice how the make file never got far enough</p>



<p class="wp-block-paragraph">so that it tried to print the MD5 sums of the input and output files.</p>



<p class="wp-block-paragraph">The assembly program just failed.</p>



<p class="wp-block-paragraph">And so GNU make said, I&#8217;m not going to proceed any further.</p>



<p class="wp-block-paragraph">Kind of useful when your program gives good exit codes, right?</p>



<p class="wp-block-paragraph">Because then other programs know when to stop</p>



<p class="wp-block-paragraph">or maybe what to do depending on what&#8217;s happening.</p>



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



<p class="wp-block-paragraph">down here and maybe the next thing I want to add is closing the files so we&#8217;ve got a function to</p>



<p class="wp-block-paragraph">open a file for reading and then we have a function down here that is not implemented yet</p>



<p class="wp-block-paragraph">for closing the files so I&#8217;m going to do let&#8217;s see what is r12 r12 is the input file so I&#8217;m</p>



<p class="wp-block-paragraph">going to close r12 so basically this function it&#8217;s going to it&#8217;s going to take one argument</p>



<p class="wp-block-paragraph">handle. So that&#8217;s why I&#8217;m giving the file handle of the input file as the first argument. And then</p>



<p class="wp-block-paragraph">I&#8217;m just going to call it. So let&#8217;s copy paste close file underneath this. So I&#8217;m going to do</p>



<p class="wp-block-paragraph">like some more space. And let me let me go get this from my solution close file.</p>



<p class="wp-block-paragraph">And it&#8217;s going to be a pretty simple one. Really nothing much to it. I&#8217;m just going to write my</p>



<p class="wp-block-paragraph">C++ prototype saying, well, it takes in one argument and it&#8217;s a handle. It doesn&#8217;t return</p>



<p class="wp-block-paragraph">argument and it&#8217;s a handle it doesn&#8217;t return anything it attempts to close a</p>



<p class="wp-block-paragraph">file and I&#8217;m just gonna use R12 to hold the incoming argument your programs at</p>



<p class="wp-block-paragraph">home should probably be a little bit better than mine you should check to see</p>



<p class="wp-block-paragraph">if the file successfully closed or not and respond in some kind of a way but</p>



<p class="wp-block-paragraph">for me I&#8217;m just saying I don&#8217;t really care I already showed you that we can</p>



<p class="wp-block-paragraph">check for a return value so I&#8217;m allowed to be lazy now and just sort of try to</p>



<p class="wp-block-paragraph">close it and then just assume it all went according to plan so grabbing the</p>



<p class="wp-block-paragraph">So grabbing the incoming arguments here.</p>



<p class="wp-block-paragraph">That&#8217;s why I&#8217;m using R12.</p>



<p class="wp-block-paragraph">That&#8217;s why I&#8217;m doing a push-pop pair.</p>



<p class="wp-block-paragraph">And then the system call code to close is pretty easy.</p>



<p class="wp-block-paragraph">You just say, here&#8217;s the code to close.</p>



<p class="wp-block-paragraph">Stick that in RAX.</p>



<p class="wp-block-paragraph">Let me go up real fast.</p>



<p class="wp-block-paragraph">Notice how sysclose is the call code number three</p>



<p class="wp-block-paragraph">on line 43 there.</p>



<p class="wp-block-paragraph">So I send it the call code three to say, let&#8217;s close a file.</p>



<p class="wp-block-paragraph">It only wants one argument, which is just,</p>



<p class="wp-block-paragraph">what is the file handle that you want me to close?</p>



<p class="wp-block-paragraph">what is the file handle that you want me to close remember before the operating system created a</p>



<p class="wp-block-paragraph">bunch of stuff under the hood and gave you a file handle you can then use the file handle</p>



<p class="wp-block-paragraph">to close a file read a file write to the file whatever to the file so it&#8217;s pretty easy once</p>



<p class="wp-block-paragraph">i&#8217;ve set up the incoming arguments to the system then i actually use the system call instruction</p>



<p class="wp-block-paragraph">syscall and then i can assume it&#8217;s probably closed at that point then a return statement at the end</p>



<p class="wp-block-paragraph">like any additional thing happening.</p>



<p class="wp-block-paragraph">It should just be, it said it successfully opened the file</p>



<p class="wp-block-paragraph">and then it just exited.</p>



<p class="wp-block-paragraph">Okay, now we&#8217;re ready to add a little bit more.</p>



<p class="wp-block-paragraph">Let&#8217;s create a file to write.</p>



<p class="wp-block-paragraph">So this is going to be the same thing basically</p>



<p class="wp-block-paragraph">as opening a file to read,</p>



<p class="wp-block-paragraph">except it&#8217;s going to be a different call code</p>



<p class="wp-block-paragraph">and we&#8217;ll give it initial file permissions</p>



<p class="wp-block-paragraph">rather than a read mode flag.</p>



<p class="wp-block-paragraph">But then we&#8217;ll just get a file handle in return.</p>



<p class="wp-block-paragraph">get a file handle in return.</p>



<p class="wp-block-paragraph">And I&#8217;ve also stuck this into another function, of course.</p>



<p class="wp-block-paragraph">So the file name to write line 109.</p>



<p class="wp-block-paragraph">If you just scroll up real fast here, or if I scroll up real fast,</p>



<p class="wp-block-paragraph">it&#8217;s a line 27 here, just output.txt.</p>



<p class="wp-block-paragraph">And then the file permissions that we want to use.</p>



<p class="wp-block-paragraph">I&#8217;m going to scroll up here.</p>



<p class="wp-block-paragraph">That&#8217;s the second argument to the system call code.</p>



<p class="wp-block-paragraph">That&#8217;s just the stuff that I talked about a little while ago where it&#8217;s like,</p>



<p class="wp-block-paragraph">we want the user to be able to read and write to the file.</p>



<p class="wp-block-paragraph">to be able to read and write to the file we want people in the same group to be able to read only</p>



<p class="wp-block-paragraph">and we want everyone else to not be able to do anything basic linux permissions not a big deal</p>



<p class="wp-block-paragraph">at the end you should probably maybe note that uh you know this q i think i might have said this</p>



<p class="wp-block-paragraph">before this q just means quad word and these zeros are always going to be the same so really it&#8217;s just</p>



<p class="wp-block-paragraph">like three numbers representing file permissions i&#8217;ll go over that in more detail in some other</p>



<p class="wp-block-paragraph">So for now, we know that we&#8217;re going to open a file name to we&#8217;re going to open a file</p>



<p class="wp-block-paragraph">for writing doesn&#8217;t have to exist yet.</p>



<p class="wp-block-paragraph">We&#8217;ll give it some default permissions.</p>



<p class="wp-block-paragraph">And those are the incoming arguments to the function that we&#8217;re going to call now called</p>



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



<p class="wp-block-paragraph">When that file when that function comes back, assuming it didn&#8217;t decide to exit the program</p>



<p class="wp-block-paragraph">because we failed to open the file, we will receive the file handle in our ex per usual.</p>



<p class="wp-block-paragraph">And so I&#8217;m just going to stash that away real fast into our 13.</p>



<p class="wp-block-paragraph">Remember R12 has the handle to the input file.</p>



<p class="wp-block-paragraph">R13 has the handle to the output file.</p>



<p class="wp-block-paragraph">And then, you know, for me personally,</p>



<p class="wp-block-paragraph">I put that in comments to help myself remember</p>



<p class="wp-block-paragraph">what I&#8217;m even doing,</p>



<p class="wp-block-paragraph">because things can get confusing really, really fast.</p>



<p class="wp-block-paragraph">And while we&#8217;re at it,</p>



<p class="wp-block-paragraph">before we even write the create file function,</p>



<p class="wp-block-paragraph">I might as well just uncomment these things at the bottom,</p>



<p class="wp-block-paragraph">just to say, let&#8217;s close both files properly.</p>



<p class="wp-block-paragraph">You always want to do that.</p>



<p class="wp-block-paragraph">let&#8217;s copy paste the create file function let&#8217;s see I didn&#8217;t do that already right yeah okay</p>



<p class="wp-block-paragraph">so let&#8217;s do that maybe like right here I&#8217;m going to go grab it from my solution real fast create</p>



<p class="wp-block-paragraph">file okay about as complicated as opening a file just because I put in some some checking logic to</p>



<p class="wp-block-paragraph">see if it successfully opened the file which is a good idea so here&#8217;s my function create file</p>



<p class="wp-block-paragraph">arguments. Oh shoot, file creation hand. Let&#8217;s see, am I using R13? Yeah, oh, I mislabeled that.</p>



<p class="wp-block-paragraph">Instead of saying flags, it should be permissions. So let me just do perms here. Long perms,</p>



<p class="wp-block-paragraph">file creation perms, perm. I wish I could get a perm, if you know what I mean. Anyway, so,</p>



<p class="wp-block-paragraph">you know, we just have like the file name that comes in and the permissions that come in,</p>



<p class="wp-block-paragraph">And it&#8217;s going to return a handle just like we did with the read file for opening function.</p>



<p class="wp-block-paragraph">But this time we&#8217;re going to do something slightly different.</p>



<p class="wp-block-paragraph">So the system call is going to be the code for creating a file.</p>



<p class="wp-block-paragraph">SYS create, which is not real unless you define it.</p>



<p class="wp-block-paragraph">So if we just look up, I define that as.</p>



<p class="wp-block-paragraph">Where is it?</p>



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



<p class="wp-block-paragraph">So like right here, code 85 for system create.</p>



<p class="wp-block-paragraph">Again, not a smart idea to hard code numbers.</p>



<p class="wp-block-paragraph">Defines are way better.</p>



<p class="wp-block-paragraph">numbers defines are way better. So then the first argument that it wants is the name of the file.</p>



<p class="wp-block-paragraph">That&#8217;s the incoming argument that I took into R12. It has to be a null terminated string. It&#8217;s going</p>



<p class="wp-block-paragraph">to be output.txt. Then the second argument is the file&#8217;s permissions. So that&#8217;s R13 that I took here</p>



<p class="wp-block-paragraph">from the second argument, long perms. Once I&#8217;ve set up those things, I can do the system call</p>



<p class="wp-block-paragraph">right away. The system, again, will try to open the file. It&#8217;ll try to create the file,</p>



<p class="wp-block-paragraph">set up some data structures under the hood. If it succeeds, it&#8217;ll give me a valid file handle</p>



<p class="wp-block-paragraph">in RAX, the return register. If not, then things have failed and I need to respond to that. So</p>



<p class="wp-block-paragraph">I&#8217;m just going to stash the file handle in R14 right away. That&#8217;s why I&#8217;m also preserving R14</p>



<p class="wp-block-paragraph">in the push pop pair that I have, R14 up at the top and the bottom.</p>



<p class="wp-block-paragraph">So now we got to check whether or not we successfully created the file. Again,</p>



<p class="wp-block-paragraph">logic is just opening up off a reading I&#8217;m going to compare our 14 to zero if</p>



<p class="wp-block-paragraph">it&#8217;s greater than zero I&#8217;m going to assume it&#8217;s a valid file handle so jump</p>



<p class="wp-block-paragraph">if it&#8217;s greater than or equal to to that label create file success I&#8217;m still</p>



<p class="wp-block-paragraph">using my appending can naming convention so the name of the function is create</p>



<p class="wp-block-paragraph">file so that means the success area is create file and then append underscore</p>



<p class="wp-block-paragraph">success and so then I&#8217;m just gonna you know print a cute message saying hey we</p>



<p class="wp-block-paragraph">successfully created the file. Yay.</p>



<p class="wp-block-paragraph">Same thing that we looked at before basically.</p>



<p class="wp-block-paragraph">And then I&#8217;m going to return the file handle in RAX.</p>



<p class="wp-block-paragraph">If we fail, same thing that we did before when we were opening a read file,</p>



<p class="wp-block-paragraph">I&#8217;m just going to complain basically to the user and then call on my little</p>



<p class="wp-block-paragraph">die function to properly exit the assembly program with a</p>



<p class="wp-block-paragraph">exit code of one, you know,</p>



<p class="wp-block-paragraph">and I could enhance that to exit with different codes. Like maybe exit code.</p>



<p class="wp-block-paragraph">Maybe exit code one means the read file didn&#8217;t work and exit code two means the</p>



<p class="wp-block-paragraph">write file didn&#8217;t work and exit code three means we failed for some reason</p>



<p class="wp-block-paragraph">while we were copying the data, you know, whatever you want.</p>



<p class="wp-block-paragraph">I&#8217;m keeping it kind of simple.</p>



<p class="wp-block-paragraph">So we got all that.</p>



<p class="wp-block-paragraph">Again, this is not different than reading just, you know, the call codes pretty</p>



<p class="wp-block-paragraph">much and, you know, the arguments, but the idea is the same.</p>



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



<p class="wp-block-paragraph">I think we actually already did that.</p>



<p class="wp-block-paragraph">We&#8217;re ready to let the program try to create the file.</p>



<p class="wp-block-paragraph">to create the file and the only thing we need to add after that is the copying portion and the</p>



<p class="wp-block-paragraph">successful message so let&#8217;s see if this works do clear and make run so you can see that the program</p>



<p class="wp-block-paragraph">starts running here and then it says okay the module started and then it says we successfully</p>



<p class="wp-block-paragraph">opened the read file and then we successfully created the output file notice how the make file</p>



<p class="wp-block-paragraph">doesn&#8217;t fail when it tries to call the md5 checksum of the output file because now it actually exists</p>



<p class="wp-block-paragraph">because now it actually exists if we list the program uh list the directory here you&#8217;ll see</p>



<p class="wp-block-paragraph">that the out file has actually been created it just has a length of zero because there&#8217;s nothing</p>



<p class="wp-block-paragraph">inside of it notice also that the permissions match what we intended the initial user can read</p>



<p class="wp-block-paragraph">and write or the owning user can read and write the group can only read and everybody else can do</p>



<p class="wp-block-paragraph">nothing to heck with you we could change that real fast just to show you i can get this r to</p>



<p class="wp-block-paragraph">show you i can get this r to turn into an rw just by modifying permissions up here</p>



<p class="wp-block-paragraph">you know giving permissions to a group uh just is a nice way of allowing multiple users</p>



<p class="wp-block-paragraph">to have a shared file location you know add them all to the same group and then set that group</p>



<p class="wp-block-paragraph">onto the file and then set the group permissions to allow people to do whatever you want them to</p>



<p class="wp-block-paragraph">do read or read and write or whatever so let&#8217;s see where the perms i&#8217;m going to change this</p>



<p class="wp-block-paragraph">So now the group people should be able to read and write.</p>



<p class="wp-block-paragraph">I can&#8217;t remember if this will work because the file already exists.</p>



<p class="wp-block-paragraph">Let me give it a try.</p>



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



<p class="wp-block-paragraph">I ran it one more time and it looks like it did not create the file because it already</p>



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



<p class="wp-block-paragraph">So let me just remove output.txt.</p>



<p class="wp-block-paragraph">There should be another system call code you could use to just check to see if a file</p>



<p class="wp-block-paragraph">exists or if you wanted to be kind of hacky, you could just try to open the file and see</p>



<p class="wp-block-paragraph">open the file and see if it succeeded and then close it right away.</p>



<p class="wp-block-paragraph">I wonder if there is a call code for just exists only.</p>



<p class="wp-block-paragraph">I don&#8217;t remember off the top of my head.</p>



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



<p class="wp-block-paragraph">And then we should see now.</p>



<p class="wp-block-paragraph">Yeah, now that the file didn&#8217;t exist and was newly created.</p>



<p class="wp-block-paragraph">Now those new permissions that we added are reflected.</p>



<p class="wp-block-paragraph">So the group, anybody who&#8217;s on the group can read and write.</p>



<p class="wp-block-paragraph">And obviously the group is just the same as my new user by default.</p>



<p class="wp-block-paragraph">But again, you could be more complicated than that.</p>



<p class="wp-block-paragraph">you were running a multi-user system</p>



<p class="wp-block-paragraph">and you wanted to share folders or whatever.</p>



<p class="wp-block-paragraph">Okay, so I&#8217;m gonna remove output real fast</p>



<p class="wp-block-paragraph">and I&#8217;m just gonna revert the permissions to 640</p>



<p class="wp-block-paragraph">so that the group really can&#8217;t do very much</p>



<p class="wp-block-paragraph">except just read it.</p>



<p class="wp-block-paragraph">And then if I, oh shoot.</p>



<p class="wp-block-paragraph">LS, okay, yeah.</p>



<p class="wp-block-paragraph">So now you can see it reverted.</p>



<p class="wp-block-paragraph">Okay, so we got the out file.</p>



<p class="wp-block-paragraph">I&#8217;m starting to get lost here.</p>



<p class="wp-block-paragraph">I&#8217;m starting to get lost here. What am I doing? I&#8217;m supposed to copy the data, I think. So file</p>



<p class="wp-block-paragraph">tests and we are copying the file. I think I just uncommented that. Really? Oh, no, no, no. I&#8217;m</p>



<p class="wp-block-paragraph">looking at my solution repo. Okay. That&#8217;s what I&#8217;m doing wrong. So we&#8217;ll look into your repo or your</p>



<p class="wp-block-paragraph">the copy file I was like what how did I how did that program run if I already uncommented that</p>



<p class="wp-block-paragraph">and we&#8217;ll we&#8217;ll uncomment this message for now no let&#8217;s leave it until we actually finish everything</p>



<p class="wp-block-paragraph">so now we got to do the copy file function so where&#8217;s that okay I&#8217;m going to copy paste this</p>



<p class="wp-block-paragraph">whole thing you know what this would have been like a five hour video if I actually had to type</p>



<p class="wp-block-paragraph">this by hand I can&#8217;t even remember how long it took me to write this program I think it was like</p>



<p class="wp-block-paragraph">off the top of my head. So this would be a nightmare to type, I think, on video, even if I</p>



<p class="wp-block-paragraph">kind of already know how to do it now. So let&#8217;s see, string length, print an alternated string.</p>



<p class="wp-block-paragraph">This is close file. This is create file. Okay, so I&#8217;m just going to do copy.</p>



<p class="wp-block-paragraph">File is going to be right before close file and right after create file. Okay, so copy file.</p>



<p class="wp-block-paragraph">Notice the signature that I&#8217;ve chosen for this one. It doesn&#8217;t return anything. So</p>



<p class="wp-block-paragraph">So that&#8217;s like not great design.</p>



<p class="wp-block-paragraph">You know, I need to check to see if the copying operations inside of my loop actually succeeded</p>



<p class="wp-block-paragraph">and maybe return something to indicate success or failure or at least exit the program.</p>



<p class="wp-block-paragraph">But I&#8217;m not doing that right now.</p>



<p class="wp-block-paragraph">I&#8217;m just keeping it a little bit more simple.</p>



<p class="wp-block-paragraph">It takes two arguments.</p>



<p class="wp-block-paragraph">The first thing is the input handle and the second thing is the output handle.</p>



<p class="wp-block-paragraph">Conveniently, we have both of those now at this point.</p>



<p class="wp-block-paragraph">And then here&#8217;s my register usage.</p>



<p class="wp-block-paragraph">through r15 to just sort of grab the incoming arguments here for r12 and 13 and then</p>



<p class="wp-block-paragraph">r14 grabs the beginning of the temporary buffer which i&#8217;m going to make on the stack because i</p>



<p class="wp-block-paragraph">think i&#8217;m so cool instead of making it as a global in the bss section and then uh</p>



<p class="wp-block-paragraph">r15 is going to hold the result from the copy operation or i guess the the write operation so</p>



<p class="wp-block-paragraph">Sorry, the read operation only.</p>



<p class="wp-block-paragraph">Oh, I think I&#8217;m only checking the read bytes instead of the read and the write bytes.</p>



<p class="wp-block-paragraph">I&#8217;ll talk about that in a second.</p>



<p class="wp-block-paragraph">So I could upgrade my program a little bit more if I wanted to.</p>



<p class="wp-block-paragraph">But basically R15 is going to be my temporary variable that looks at the return value to</p>



<p class="wp-block-paragraph">say, hey, did we do we read anything?</p>



<p class="wp-block-paragraph">Like how much did we read?</p>



<p class="wp-block-paragraph">So copy file.</p>



<p class="wp-block-paragraph">This is not a video about making local variables.</p>



<p class="wp-block-paragraph">So just trust me on this.</p>



<p class="wp-block-paragraph">so that I can use it to save the location of the stack pointer.</p>



<p class="wp-block-paragraph">Then I&#8217;m going to make a copy buffer on the stack</p>



<p class="wp-block-paragraph">just by subtracting the stack pointer</p>



<p class="wp-block-paragraph">because the stack grows downward in memory location numbers.</p>



<p class="wp-block-paragraph">And then the base pointer is going to help me remember</p>



<p class="wp-block-paragraph">where the stack was when I started.</p>



<p class="wp-block-paragraph">I&#8217;m just going to say that that&#8217;s going to be the first byte in my buffer.</p>



<p class="wp-block-paragraph">And I&#8217;m going to move that into R14.</p>



<p class="wp-block-paragraph">And let&#8217;s see, what did I do?</p>



<p class="wp-block-paragraph">Let&#8217;s see, what did I do?</p>



<p class="wp-block-paragraph">Think about from the stack.</p>



<p class="wp-block-paragraph">Oh, did I write like a good program or a bad program?</p>



<p class="wp-block-paragraph">I feel like I should have actually saved the base pointer and not the stack pointer there.</p>



<p class="wp-block-paragraph">Let me see if this runs.</p>



<p class="wp-block-paragraph">If it does run, I probably have a naughty program that might self-corrupt sometimes.</p>



<p class="wp-block-paragraph">And it might be a good idea to move the base pointer as the first.</p>



<p class="wp-block-paragraph">Because this is not a stack video, but basically the stack grows downward.</p>



<p class="wp-block-paragraph">that means you&#8217;re sort of like extending its reach you&#8217;re allocating like a free space</p>



<p class="wp-block-paragraph">if i take where the stack</p>



<p class="wp-block-paragraph">nope nope nope i got it right i&#8217;m sorry if i take where the stack ends up after i extend it then i</p>



<p class="wp-block-paragraph">have a lower address right because it grows downward to memory if i then say that the new</p>



<p class="wp-block-paragraph">tip where it grew not where it grew from but where it grew to if i say that&#8217;s the first bite in a</p>



<p class="wp-block-paragraph">in a buffer then when you&#8217;re actually using the buffer you increase memory locations as you&#8217;re</p>



<p class="wp-block-paragraph">filling up a buffer so then that&#8217;s going to grow back towards where the stack started so it should</p>



<p class="wp-block-paragraph">be fine i think if i if i did go ahead and reverse this if i used rbp instead of rsp there then i</p>



<p class="wp-block-paragraph">think i would be going in the wrong direction and just corrupting memory so just so you know</p>



<p class="wp-block-paragraph">this is not a stack video but just so you know so here&#8217;s a little label again i&#8217;m just kind of</p>



<p class="wp-block-paragraph">iteration label and at the bottom I&#8217;m just kind of jumping back up to that label so this is a loop</p>



<p class="wp-block-paragraph">you can imagine this is a while loop maybe I should draw that for fun I gotta find more excuses</p>



<p class="wp-block-paragraph">to use my drophead so this is kind of like a while loop I&#8217;ll say while true maybe and here&#8217;s like the</p>



<p class="wp-block-paragraph">body and then here&#8217;s like maybe the end of the body and so this jump statement just kind of goes back</p>



<p class="wp-block-paragraph">this thing&#8217;s going to frustrate me so the first thing that happens is we read a portion</p>



<p class="wp-block-paragraph">of the input file into the buffer so what are we doing we&#8217;re just using another system call</p>



<p class="wp-block-paragraph">we&#8217;re saying let&#8217;s use the call code for read I can&#8217;t remember what that was it might have been</p>



<p class="wp-block-paragraph">uh whoops oh I cleared the dang drawing oh hang on I have undo I think wait I have that I&#8217;m gonna</p>



<p class="wp-block-paragraph">Okay, I&#8217;m not going to mess with it because this will actually terminate my whole program.</p>



<p class="wp-block-paragraph">One of these keys, I forgot which one, will just kill the whole annotator.</p>



<p class="wp-block-paragraph">So if we go up to SYS read in the defines, you can see that it is just call code zero.</p>



<p class="wp-block-paragraph">So that means I&#8217;m telling the system I would like to read something.</p>



<p class="wp-block-paragraph">First argument it wants is a file descriptor for the input file.</p>



<p class="wp-block-paragraph">In a different video, I showed you how to use exactly this sort of thing to read standard</p>



<p class="wp-block-paragraph">of thing to read standard input from the user or from another program that launched your program</p>



<p class="wp-block-paragraph">but in this case r12 it&#8217;s not going to be the file handle zero for standard input it&#8217;s going</p>



<p class="wp-block-paragraph">to be the actual file handle of the file that you&#8217;re trying to read from whatever that may have</p>



<p class="wp-block-paragraph">been whatever the os gave you and then here here&#8217;s the address of where to store our characters and</p>



<p class="wp-block-paragraph">if you look up again we decided to remember where the first byte in our buffer on the stack was or</p>



<p class="wp-block-paragraph">like a local array like oh shoot let&#8217;s do it again let&#8217;s do more drawings you have like a function</p>



<p class="wp-block-paragraph">right in c and then here it&#8217;s like we declared a local variable maybe not int maybe a character</p>



<p class="wp-block-paragraph">array we&#8217;ll call it a or how about b for buffer and then we just gave it you know let&#8217;s say eight</p>



<p class="wp-block-paragraph">bytes or something for the buffer i think i still have it set to two bytes obviously you want to use</p>



<p class="wp-block-paragraph">more bytes for efficiency but just to prove that the loop actually works i&#8217;m keeping it small like</p>



<p class="wp-block-paragraph">keeping it small like I said before whoa what&#8217;s all that you see that it&#8217;s like smearing okay</p>



<p class="wp-block-paragraph">that&#8217;s not good so uh anyway we remembered where the uh buffer starts and it&#8217;s going to be r14</p>



<p class="wp-block-paragraph">and then the copy buffer length is being you know used here uh and I think I have that set up to uh</p>



<p class="wp-block-paragraph">from the input file and we want to read into our temporary buffer and we want to read at most</p>



<p class="wp-block-paragraph">this many characters and then we say system call and it does all the work for us to read that many</p>



<p class="wp-block-paragraph">characters and then we want to remember how many bytes were actually read because that could be</p>



<p class="wp-block-paragraph">different from the number of bytes we requested maybe we&#8217;re at the end of the file maybe the</p>



<p class="wp-block-paragraph">system is having like some kind of a buffer issue or something so we just want to remember how many</p>



<p class="wp-block-paragraph">this little token up there to remind myself the temporary bytes read should</p>



<p class="wp-block-paragraph">go there. Notice also that when we created the stack,</p>



<p class="wp-block-paragraph">this is not a stack video again,</p>



<p class="wp-block-paragraph">but like I made the stack buffer equal to the length of the buffer that I</p>



<p class="wp-block-paragraph">actually wanted to use. So I use the same symbol,</p>



<p class="wp-block-paragraph">copy buffer length and copy buffer length.</p>



<p class="wp-block-paragraph">So then it&#8217;s going to try to figure out, okay,</p>



<p class="wp-block-paragraph">how many bytes did we actually read? Let me,</p>



<p class="wp-block-paragraph">let me do that while loop thing again. Cause I&#8217;m, I&#8217;m feeling it.</p>



<p class="wp-block-paragraph">I&#8217;m feeling it.</p>



<p class="wp-block-paragraph">feeling it did i really have to write the word true especially my bad penmanship so we&#8217;re doing</p>



<p class="wp-block-paragraph">while true and then i&#8217;ll just say like read you know do some kind of a call to read maybe we&#8217;ll</p>



<p class="wp-block-paragraph">say n equals read like a long you know the the r15 uh register is like the number of bytes that we</p>



<p class="wp-block-paragraph">actually read and then we do a comparison here so we&#8217;re saying you know if you know n is um</p>



<p class="wp-block-paragraph">if it&#8217;s equal to zero then we&#8217;re going to jump to the position in the while loop where we&#8217;re</p>



<p class="wp-block-paragraph">um done which is actually past the while loop if you scroll down you&#8217;ll kind of see it so i&#8217;m just</p>



<p class="wp-block-paragraph">going to say break so basically if we read zero bytes then we&#8217;re done reading bytes we&#8217;re just</p>



<p class="wp-block-paragraph">finished so we just break the while loop and that&#8217;s what&#8217;s going to happen here when we say</p>



<p class="wp-block-paragraph">the end of the body and you&#8217;ll see that in a second so then otherwise if we&#8217;re not done that</p>



<p class="wp-block-paragraph">means execution is not going to jump it&#8217;s going to just fall through to the next statement here</p>



<p class="wp-block-paragraph">and that&#8217;s going to be another system call code to write to the output file and it&#8217;s going to be</p>



<p class="wp-block-paragraph">very similar we just load it up with the system call code for write let me just double check</p>



<p class="wp-block-paragraph">is code one and then we&#8217;re going to give it the target for output which is going to be</p>



<p class="wp-block-paragraph">a file handle so you know r13 r13 right here that&#8217;s a file handle that&#8217;s where we want to</p>



<p class="wp-block-paragraph">write and then the next argument that it wants is the buffer and that&#8217;s going to be r14 which</p>



<p class="wp-block-paragraph">is where we just what&#8217;s going on green grain there we go i&#8217;m having issues that&#8217;s uh the</p>



<p class="wp-block-paragraph">that&#8217;s uh the buffer that we just read into right so if you look here r14 green oh my gosh okay so</p>



<p class="wp-block-paragraph">green we uh read from uh the buffer pointed to by r14 and then we uh we read into the buffer</p>



<p class="wp-block-paragraph">pointed to by r14 and then we are using that same buffer pointed to by r14 in order to grab</p>



<p class="wp-block-paragraph">R15 says how many files or sorry, how many bytes?</p>



<p class="wp-block-paragraph">Green, I&#8217;m having such problems.</p>



<p class="wp-block-paragraph">R15 says how many bytes do you want to write to the file?</p>



<p class="wp-block-paragraph">That should be our return value from before, right?</p>



<p class="wp-block-paragraph">Because we want to read a certain number of bytes from the file</p>



<p class="wp-block-paragraph">and then write exactly that many bytes.</p>



<p class="wp-block-paragraph">If we did more, we would be writing some junk data probably.</p>



<p class="wp-block-paragraph">And if we wrote less, then we would be missing data in the output file</p>



<p class="wp-block-paragraph">that was originally in the input file.</p>



<p class="wp-block-paragraph">So you want to do it exactly.</p>



<p class="wp-block-paragraph">in the input file so you want to do it exactly and of course i&#8217;m just doing a system call here</p>



<p class="wp-block-paragraph">but like i said before your program should probably be a little bit smarter and check</p>



<p class="wp-block-paragraph">rax after you write to the file just to make sure that you know how many bytes were actually written</p>



<p class="wp-block-paragraph">like what if you what if you read a hundred bytes but then you wrote 90 bytes only right that would</p>



<p class="wp-block-paragraph">be like a bad situation so you&#8217;d want to do some branching logic there so that if you read 100 and</p>



<p class="wp-block-paragraph">100 and you wrote 90 you probably want to backtrack the position of the file by like 10.</p>



<p class="wp-block-paragraph">You know just to make sure that you can actually get all of the bytes into the output file</p>



<p class="wp-block-paragraph">and seeking backwards 10 that&#8217;s just another system call. It&#8217;s not in this program but it&#8217;s</p>



<p class="wp-block-paragraph">not too bad. You just make another system call give it the right call code tell it how far back</p>



<p class="wp-block-paragraph">you want to go no problem. So then when by the time we make it here we&#8217;re going to jump back</p>



<p class="wp-block-paragraph">We&#8217;re going to jump back up to the iterate label.</p>



<p class="wp-block-paragraph">Let&#8217;s see, where&#8217;s that?</p>



<p class="wp-block-paragraph">Yeah, right there.</p>



<p class="wp-block-paragraph">We&#8217;re going to jump back up to the iterate label.</p>



<p class="wp-block-paragraph">So basically this is how the while loop continues looping.</p>



<p class="wp-block-paragraph">So let me clear that real fast.</p>



<p class="wp-block-paragraph">Eventually when we&#8217;re out of data to read, you know, that&#8217;s because if we try to read</p>



<p class="wp-block-paragraph">and we end up with zero bytes, that means we&#8217;re done.</p>



<p class="wp-block-paragraph">We&#8217;ll break the while loop.</p>



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



<p class="wp-block-paragraph">Notice how it says copy file done.</p>



<p class="wp-block-paragraph">What I was talking about before, just sort of jumping past the end of the body.</p>



<p class="wp-block-paragraph">so if we are done we jump to copy file done and all it does is just restore a</p>



<p class="wp-block-paragraph">bunch of registers for us and then return to the caller so copy file we have that</p>



<p class="wp-block-paragraph">handled now and if I go back up here file tests I guess I can I feel bad about</p>



<p class="wp-block-paragraph">this I can uncomment the success message but we probably should have done more</p>



<p class="wp-block-paragraph">checking on the reads and the rights just to make sure that we actually wrote</p>



<p class="wp-block-paragraph">to make sure that we actually wrote the correct amount of characters and everything succeeded</p>



<p class="wp-block-paragraph">every time and if not we exit the program and only if everything went well then we print a</p>



<p class="wp-block-paragraph">successful message at this point in this program the way it&#8217;s written it could totally fail and</p>



<p class="wp-block-paragraph">it&#8217;ll still say that it was successful so just keep that in mind that&#8217;s bad bad for your users</p>



<p class="wp-block-paragraph">okay i think we have everything that we need now i can probably just run the program and</p>



<p class="wp-block-paragraph">run. Notice now, oh shoot, I didn&#8217;t even show this to you before. Let me just emphasize this</p>



<p class="wp-block-paragraph">one more time real fast. So I&#8217;m going to comment out the part where we actually copy the file</p>



<p class="wp-block-paragraph">and I&#8217;m going to remove the output file. Then I&#8217;m going to run the whole program again. Notice how</p>



<p class="wp-block-paragraph">the two MD5 sums are different. So remember before I said that MD5 sums, they&#8217;re basically</p>



<p class="wp-block-paragraph">fingerprints. They&#8217;re not actually considered secure in the modern era. I just use them because</p>



<p class="wp-block-paragraph">and fun if you are interested in security you probably want to use a</p>



<p class="wp-block-paragraph">modern hashing algorithm so don&#8217;t use md5 but I am but don&#8217;t but I am and it&#8217;s</p>



<p class="wp-block-paragraph">basically saying the fingerprint here of input is different than the fingerprint</p>



<p class="wp-block-paragraph">here of the output quickly indicating to me that the files are different so if I</p>



<p class="wp-block-paragraph">list the contents of the directory obviously that&#8217;s true because the output</p>



<p class="wp-block-paragraph">file is empty still but if I uncomment this part here where I&#8217;m actually</p>



<p class="wp-block-paragraph">here where I&#8217;m actually copying the file then I should see that the fingerprints</p>



<p class="wp-block-paragraph">match and then if I look at what&#8217;s inside of the output file it should match</p>



<p class="wp-block-paragraph">what&#8217;s inside of the input file really fast let me open up a terminal and I&#8217;ll</p>



<p class="wp-block-paragraph">just cat the input file so this is all I added you know why hello there add some</p>



<p class="wp-block-paragraph">stuff this is definitely more than two characters so we can be sure that our</p>



<p class="wp-block-paragraph">buffer loop is actually working and so I&#8217;m gonna do clear and make run and now</p>



<p class="wp-block-paragraph">And now the output file, notice how it has a matching file size.</p>



<p class="wp-block-paragraph">It&#8217;s got 38 bytes in there.</p>



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



<p class="wp-block-paragraph">And then the thumbprints or, you know, the signatures, whatever you want to call it,</p>



<p class="wp-block-paragraph">the hashes, they match exactly, indicating that we probably have two identical files.</p>



<p class="wp-block-paragraph">Even with MD5, even though it&#8217;s old and not considered secure,</p>



<p class="wp-block-paragraph">the chances of two files kind of having random differences,</p>



<p class="wp-block-paragraph">not hacked differences, but just like random differences,</p>



<p class="wp-block-paragraph">and having the same fingerprint is like astronomically almost impossible so if i do</p>



<p class="wp-block-paragraph">cat input oops input.txt that&#8217;s what&#8217;s in there like we showed before and if i cat the output</p>



<p class="wp-block-paragraph">now it is uh the same thing why hello there added some stuff for both i could make this as big as i</p>



<p class="wp-block-paragraph">wanted to just for fun maybe let&#8217;s do uh let&#8217;s do a nano on the input file</p>



<p class="wp-block-paragraph">I&#8217;ll just add like I don&#8217;t even know what I&#8217;m doing I&#8217;m just going to type a bunch of stuff</p>



<p class="wp-block-paragraph">oh wait what is this remember that thing that people got taught a long time ago it was like</p>



<p class="wp-block-paragraph">the quick brown fox jumped over the lazy dog and this was supposed to be all the letters of the</p>



<p class="wp-block-paragraph">alphabet there was an there&#8217;s another one that I just heard about and I don&#8217;t remember exactly</p>



<p class="wp-block-paragraph">look it up on the internet it&#8217;s it&#8217;s pretty cool i think i need to memorize this and stop using the</p>



<p class="wp-block-paragraph">lazy dog it was um something like those sphinx of quartz hear my vow or something like that</p>



<p class="wp-block-paragraph">you know what i&#8217;m gonna look it up for you right now i don&#8217;t want to do the wrong thing</p>



<p class="wp-block-paragraph">it&#8217;s really cool uh i&#8217;ll type brown fox and then uh</p>



<p class="wp-block-paragraph">Hear my vow.</p>



<p class="wp-block-paragraph">Oh, Sphinx of black quartz.</p>



<p class="wp-block-paragraph">Judge my vow.</p>



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



<p class="wp-block-paragraph">Okay, so I&#8217;m going to go back to my little VM here.</p>



<p class="wp-block-paragraph">Sphinx of black quartz.</p>



<p class="wp-block-paragraph">Judge my…</p>



<p class="wp-block-paragraph">I didn&#8217;t even write vow correctly.</p>



<p class="wp-block-paragraph">Judge my vow.</p>



<p class="wp-block-paragraph">And I think that has all the letters in the alphabet too.</p>



<p class="wp-block-paragraph">probably less spaces. I wonder if that&#8217;s, I mean, that&#8217;s what the internet says. If this is true,</p>



<p class="wp-block-paragraph">and it has all the letters of the alphabet, that&#8217;s going to be awesome. I&#8217;m going to memorize that</p>



<p class="wp-block-paragraph">for sure. Sphinx of black quartz, judge my vow. Anyway, so I&#8217;m just kind of adding stuff into</p>



<p class="wp-block-paragraph">this file. And if I run, let me save that here. I&#8217;ll do a clear. And then I&#8217;ll do, um,</p>



<p class="wp-block-paragraph">I&#8217;ll cat the input file here, and then cat the output file. So you can see they&#8217;re different.</p>



<p class="wp-block-paragraph">again let me remove the output file just in case I can&#8217;t remember if I&#8217;m supposed</p>



<p class="wp-block-paragraph">to remove it manually or if I put that into the program we&#8217;ll just try it like</p>



<p class="wp-block-paragraph">this so now they match and then if I cat again the output file notice how it&#8217;s a</p>



<p class="wp-block-paragraph">perfect copy of the input file nice so I think that&#8217;s pretty much everything</p>



<p class="wp-block-paragraph">that I wanted to show you maybe um well maybe we can use a more efficient buffer</p>



<p class="wp-block-paragraph">we can use a more efficient buffer now that it&#8217;s done the copy buffer we could change this to like</p>



<p class="wp-block-paragraph">eight kilobytes or something we should end up with the same result let me run this just as is</p>



<p class="wp-block-paragraph">and see if it ends up being the same thing without erasing the file first yeah it looks good let me</p>



<p class="wp-block-paragraph">remove the output file and then run it one more time so make run and then uh cat the output file</p>



<p class="wp-block-paragraph">yeah okay so it still works but um you know whereas before we were just using a two byte buffer</p>



<p class="wp-block-paragraph">two byte buffer there&#8217;s like very little chance except maybe at the end of the file that we would</p>



<p class="wp-block-paragraph">request more data than the file had but using the return value of the read operation always told us</p>



<p class="wp-block-paragraph">exactly how much was read by the operating system on the other hand if we have a giant buffer we</p>



<p class="wp-block-paragraph">could request way more bytes than the file could ever have because that file is way less than eight</p>



<p class="wp-block-paragraph">kilobytes so again we still want to look at the return value to make sure we know how many bytes</p>



<p class="wp-block-paragraph">how many bytes should be sent into the right file.</p>



<p class="wp-block-paragraph">So I guess that&#8217;s everything that I wanted to tell you</p>



<p class="wp-block-paragraph">about reading and writing files using system calls.</p>



<p class="wp-block-paragraph">I hope you&#8217;ve learned a lot of stuff</p>



<p class="wp-block-paragraph">and you enjoyed this video and had a little bit of fun.</p>



<p class="wp-block-paragraph">Thank you so much for watching.</p>



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



<p class="wp-block-paragraph">I&#8217;ll see you, whoops.</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">Thanks for watching this video again</p>



<p class="wp-block-paragraph">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</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 this channel or these videos or</p>



<p class="wp-block-paragraph">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">and subscribe. You know sometimes I&#8217;m sleeping in the middle of the night and</p>



<p class="wp-block-paragraph">I just wake up because I know somebody subscribed or followed. It just wakes me</p>



<p class="wp-block-paragraph">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 control me if you want</p>



<p class="wp-block-paragraph">to just wake me up in the middle of the night just subscribe and then I&#8217;ll just</p>



<p class="wp-block-paragraph">wake up. I promise that&#8217;s what will happen. Also if you look at the middle of</p>



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



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



<p class="wp-block-paragraph">also named somewhere at the bottom of this video and it&#8217;ll take you to my main website where you</p>



<p class="wp-block-paragraph">can just kind of like see all the videos I published and the services and tutorials and</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 clarifications</p>



<p class="wp-block-paragraph">or errata or just future videos that you want to see please leave a comment or if you just want to</p>



<p class="wp-block-paragraph">say hey what&#8217;s up what&#8217;s going on you know just send me a comment whatever I also wake up for</p>



<p class="wp-block-paragraph">I wake up in a cold sweat and I&#8217;m like,</p>



<p class="wp-block-paragraph">it would really mean the world to me.</p>



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



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



<p class="wp-block-paragraph">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/master-file-copying-with-system-calls-in-x86-64-yasm-assembly/">Master File Copying with System Calls in x86-64 YASM Assembly</a> appeared first on <a href="https://www.NeuralLantern.com">NeuralLantern.com</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.NeuralLantern.com/master-file-copying-with-system-calls-in-x86-64-yasm-assembly/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Command Line Arguments in Yasm Assembly with GCC: A Practical Guide</title>
		<link>https://www.NeuralLantern.com/command-line-arguments-in-yasm-assembly-with-gcc-a-practical-guide/</link>
					<comments>https://www.NeuralLantern.com/command-line-arguments-in-yasm-assembly-with-gcc-a-practical-guide/#respond</comments>
		
		<dc:creator><![CDATA[mike]]></dc:creator>
		<pubDate>Sun, 14 Sep 2025 23:46:11 +0000</pubDate>
				<category><![CDATA[Assembly Language]]></category>
		<category><![CDATA[Coding]]></category>
		<category><![CDATA[Computer Science]]></category>
		<category><![CDATA[Videos]]></category>
		<category><![CDATA[argv argc]]></category>
		<category><![CDATA[assembly programming]]></category>
		<category><![CDATA[coding tutorials]]></category>
		<category><![CDATA[command line args]]></category>
		<category><![CDATA[command line arguments]]></category>
		<category><![CDATA[GCC assembly]]></category>
		<category><![CDATA[GCC linking]]></category>
		<category><![CDATA[hybrid programming]]></category>
		<category><![CDATA[low-level programming]]></category>
		<category><![CDATA[programming guide]]></category>
		<category><![CDATA[software development]]></category>
		<category><![CDATA[x86 assembly]]></category>
		<category><![CDATA[x86-64 programming]]></category>
		<category><![CDATA[Yasm assembly]]></category>
		<category><![CDATA[Yasm tutorial]]></category>
		<guid isPermaLink="false">https://www.NeuralLantern.com/?p=227</guid>

					<description><![CDATA[<p>Learn to process command line arguments in Yasm assembly with GCC. Practical x86-64 guide for hybrid programs with clear examples. #Assembly #Programming</p>
<p>The post <a href="https://www.NeuralLantern.com/command-line-arguments-in-yasm-assembly-with-gcc-a-practical-guide/">Command Line Arguments in Yasm Assembly with GCC: A Practical Guide</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="Command Line Arguments in Yasm Assembly with GCC: A Practical Guide" width="1380" height="776" src="https://www.youtube.com/embed/r5K10mL2OPE?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 master command line arguments in Yasm assembly? This video breaks down how to access and process args in x86-64 hybrid programs linked with GCC. From understanding argv and argc to looping through arguments, we cover it all with practical examples. Perfect for programmers diving into assembly or looking to level up their low-level coding skills. Check out my other videos for more on Yasm and pure assembly! Subscribe for more coding tutorials. #AssemblyProgramming #YasmAssembly #GCC #x86_64</p>



<p class="wp-block-paragraph">Introduction 00:00:00<br>Command Line Arguments Overview 00:00:03<br>Yasm Assembly and GCC Linking 00:00:07<br>Hybrid Program Explanation 00:01:27<br>Makefile Overview 00:02:16<br>Assembly Program Structure 00:03:16<br>Main Entry Point and Registers 00:03:51<br>Accessing Command Line Arguments 00:04:26<br>Argument Count and Pointers 00:05:03<br>Loop Initialization for Arguments 00:08:57<br>Loop Logic and Dereferencing 00:10:52<br>Printing Arguments 00:13:02<br>Incrementing Pointers in Loop 00:14:08<br>Running the Program 00:15:35<br>Practical Application of Arguments 00:17:24<br>Conclusion and Next Steps 00:17:51<br>Call to Subscribe and Outro 00:18: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">Hello there. In this video,</p>



<p class="wp-block-paragraph">I&#8217;m going to talk to you about how to accept incoming command line arguments to</p>



<p class="wp-block-paragraph">an x86-64 Yasm assembly program that is probably linking to</p>



<p class="wp-block-paragraph">GCC and is thus probably a hybrid program under the hood.</p>



<p class="wp-block-paragraph">If you don&#8217;t know how to program in Yasm assembly yet,</p>



<p class="wp-block-paragraph">check out my other videos. If you don&#8217;t understand command line arguments yet,</p>



<p class="wp-block-paragraph">yet check my other videos I&#8217;m going to assume a lot of knowledge here I&#8217;m</p>



<p class="wp-block-paragraph">really just going to show you how to pull the arguments inside of assembly</p>



<p class="wp-block-paragraph">using using the GCC setup that they&#8217;ve given you so just a quick just a very</p>



<p class="wp-block-paragraph">quick recap this is not a command line arguments video if we have the program</p>



<p class="wp-block-paragraph">echo and we give it like one argument of just the word hello then it&#8217;s it&#8217;s</p>



<p class="wp-block-paragraph">going to receive one command line argument in addition to its program</p>



<p class="wp-block-paragraph">echo as argument zero and hello as argument index one.</p>



<p class="wp-block-paragraph">And it&#8217;s just going to be able to look at them and say,</p>



<p class="wp-block-paragraph">oh, the user wants me to print this hello string.</p>



<p class="wp-block-paragraph">So it just prints it.</p>



<p class="wp-block-paragraph">And so that&#8217;s kind of how you can tell a program what to do,</p>



<p class="wp-block-paragraph">either when you&#8217;re running them directly</p>



<p class="wp-block-paragraph">or having one program automate another program.</p>



<p class="wp-block-paragraph">Anyway, so in our Yasm assembly programs,</p>



<p class="wp-block-paragraph">how do we actually get that out?</p>



<p class="wp-block-paragraph">Well, for starters, we are dynamically linking.</p>



<p class="wp-block-paragraph">Sorry, not dynamically linking.</p>



<p class="wp-block-paragraph">We are linking a hybrid program.</p>



<p class="wp-block-paragraph">So I just want to make sure that you understand this is the right video for you.</p>



<p class="wp-block-paragraph">If you&#8217;re trying to do this in pure assembly, or if you need to know that for some reason,</p>



<p class="wp-block-paragraph">then you&#8217;ll probably want to watch the next video that I publish.</p>



<p class="wp-block-paragraph">But for now, this is a hybrid program.</p>



<p class="wp-block-paragraph">Hybrid programs, for those of you who don&#8217;t know, it just means you have modules that</p>



<p class="wp-block-paragraph">are written in different source code and they&#8217;re compiled differently, different source code</p>



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



<p class="wp-block-paragraph">So you might have a source code file that&#8217;s written in C++ another one that&#8217;s written in C another one that&#8217;s written in assembly</p>



<p class="wp-block-paragraph">And you compile them all down to their own object files and then you link them together into an executable</p>



<p class="wp-block-paragraph">If GCC is part of the linking process</p>



<p class="wp-block-paragraph">Then it&#8217;s going to end up giving you a main function as your entry point and it&#8217;s going to make things a little bit easier</p>



<p class="wp-block-paragraph">Here&#8217;s my make file. I&#8217;m just going to skim through it real fast. This is not a make file video</p>



<p class="wp-block-paragraph">So if you don&#8217;t understand it go look at my other videos</p>



<p class="wp-block-paragraph">But basically I&#8217;m just going to assemble with Yasm and I&#8217;m going to use G++ as the linker</p>



<p class="wp-block-paragraph">and I&#8217;m going to gobble up all of the object files. Here&#8217;s like a little menu.</p>



<p class="wp-block-paragraph">I have a target for running and building only. And here&#8217;s the main thing that I&#8217;m doing. This</p>



<p class="wp-block-paragraph">is not really a hybrid program. This is just kind of a pure assembly program that is linked with GCC,</p>



<p class="wp-block-paragraph">but you can imagine you could add other source files in C and C++ and still totally get away</p>



<p class="wp-block-paragraph">GCC. But anyway, I&#8217;m just compiling one source code of assembly and,</p>



<p class="wp-block-paragraph">and compiling it down to an object file right here.</p>



<p class="wp-block-paragraph">And then when it comes time to linking, uh, you can kind of tell here, uh,</p>



<p class="wp-block-paragraph">especially if you&#8217;re familiar with make files that I&#8217;m just gobbling up all the</p>



<p class="wp-block-paragraph">object files and linking them together into the executable.</p>



<p class="wp-block-paragraph">So that&#8217;s as far as I&#8217;ll go there again,</p>



<p class="wp-block-paragraph">I&#8217;ve got another video that totally explains a lot more in my assembly program.</p>



<p class="wp-block-paragraph">video just for pure assembly so if you don&#8217;t understand assembly see my other videos but for</p>



<p class="wp-block-paragraph">now we&#8217;re just going to say i&#8217;ve got a data section it&#8217;s got a couple of null terminated strings we&#8217;ve</p>



<p class="wp-block-paragraph">got a hello message a begin message a goodbye message carriage return line feed string i&#8217;ve</p>



<p class="wp-block-paragraph">got the system call codes for you know printing to standard output and to exit the program and then</p>



<p class="wp-block-paragraph">i&#8217;ve got the standard output file handle and then my exit code zero for success again this is all</p>



<p class="wp-block-paragraph">other videos. So because I&#8217;m linking with GCC, my text section here, the actual place where the</p>



<p class="wp-block-paragraph">instructions are, not the data, is going to have a main entry point. So of course, if you had a</p>



<p class="wp-block-paragraph">hybrid program, you could have main somewhere else in a different module and then just call on a</p>



<p class="wp-block-paragraph">function that was inside of assembly. You could totally do that. But for now, I&#8217;m just going to</p>



<p class="wp-block-paragraph">say this is the only source code file. So I mark main as global and I put main right here. And then</p>



<p class="wp-block-paragraph">I preserve all of the Kali saved registers that I&#8217;m supposed to by pushing them at the beginning and popping them in reverse order at the end.</p>



<p class="wp-block-paragraph">And then I return a return value.</p>



<p class="wp-block-paragraph">Standard assembly stuff covered in other videos.</p>



<p class="wp-block-paragraph">But here&#8217;s the key for getting command line arguments in assembly.</p>



<p class="wp-block-paragraph">If you recall, the ABI specifies that in x86-64 programs, the first argument is always supposed to come into a function as RDI in the RDI register.</p>



<p class="wp-block-paragraph">register or rather I should say the first integer argument I haven&#8217;t talked</p>



<p class="wp-block-paragraph">about to float arguments in any of my videos yet in the future you should</p>



<p class="wp-block-paragraph">probably search for other videos that that explain how to do float arguments</p>



<p class="wp-block-paragraph">but for now all of the arguments I&#8217;m talking about in this video are just</p>



<p class="wp-block-paragraph">integer arguments and pointer arguments which is kind of the same thing a</p>



<p class="wp-block-paragraph">pointer is an integer it&#8217;s a 64-bit integer but we&#8217;ll just use it as a</p>



<p class="wp-block-paragraph">using it as an integer so anyway first argument comes in on RDI second argument</p>



<p class="wp-block-paragraph">comes in on RSI and you can imagine that basically this is the thing you&#8217;re</p>



<p class="wp-block-paragraph">probably used to seeing in your C++ programs if we do this int main and then</p>



<p class="wp-block-paragraph">integer arg C and then a character pointer arg V and then an array those are</p>



<p class="wp-block-paragraph">those two registers right there I probably want to change this into long</p>



<p class="wp-block-paragraph">because you know rdi is in 64-bit form but an integer is an unsigned 32-bit int.</p>



<p class="wp-block-paragraph">I don&#8217;t like that so I&#8217;m just going to put long. I probably should put unsigned but I don&#8217;t really</p>



<p class="wp-block-paragraph">care. Anyway so we&#8217;re just going to grab those two things so obviously rdi is probably pretty</p>



<p class="wp-block-paragraph">easy for you to guess how to use it&#8217;s just the number of arguments. In this program we&#8217;re going</p>



<p class="wp-block-paragraph">to use it to control a loop counter that&#8217;s going to loop through all incoming arguments.</p>



<p class="wp-block-paragraph">which is going to be the name of the program in this case it&#8217;ll be I think</p>



<p class="wp-block-paragraph">just main is what I called it which is confusing against the entry point I</p>



<p class="wp-block-paragraph">admit it but imagine that my main program that sits in the file system is</p>



<p class="wp-block-paragraph">actually called program or hello or whatever so that&#8217;s the number of</p>



<p class="wp-block-paragraph">arguments in RDI and then RSI notice how it&#8217;s a character pointer to an array of</p>



<p class="wp-block-paragraph">characters that means it&#8217;s actually a pointer to a pointer and the reason for</p>



<p class="wp-block-paragraph">every argument on the command line like for instance if i went back here and i said echo</p>



<p class="wp-block-paragraph">hello let&#8217;s just do without quotes hello you i&#8217;ve given it two arguments and so when the program</p>



<p class="wp-block-paragraph">launches it&#8217;s going to receive three total it&#8217;s going to receive its own program name at index zero</p>



<p class="wp-block-paragraph">echo it&#8217;s going to receive at index one the first argument hello and it&#8217;s going to receive at index</p>



<p class="wp-block-paragraph">two the second argument as you it prints them both so how does echo know how many arguments i have</p>



<p class="wp-block-paragraph">strings. Remember argc is the number of arguments so that&#8217;s kind of easy but how does it dereference</p>



<p class="wp-block-paragraph">all of the strings if there could be any number of strings? Well that&#8217;s this right here.</p>



<p class="wp-block-paragraph">This whole symbol argv is a pointer and it points to an array of pointers, of character</p>



<p class="wp-block-paragraph">pointers. That&#8217;s why it&#8217;s written like this like an array. So it&#8217;s a pointer that points</p>



<p class="wp-block-paragraph">points to an array.</p>



<p class="wp-block-paragraph">So like if you go to that location in memory,</p>



<p class="wp-block-paragraph">then what you will see there for the first eight bytes</p>



<p class="wp-block-paragraph">is just the address of some other memory location where a string has been stored.</p>



<p class="wp-block-paragraph">Then if you advance that pointer another eight bytes for it,</p>



<p class="wp-block-paragraph">you know, in memory, because all pointers are eight bytes or 64 bits,</p>



<p class="wp-block-paragraph">then again, you&#8217;ll see that that value of that next eight bytes</p>



<p class="wp-block-paragraph">is actually a memory location that points to another string somewhere else.</p>



<p class="wp-block-paragraph">So these strings could be all over the place,</p>



<p class="wp-block-paragraph">But the pointers to those strings are contiguous in memory starting with what you were given in RSI.</p>



<p class="wp-block-paragraph">So it&#8217;s a character pointer pointer or an array of character pointers, however you want to imagine that.</p>



<p class="wp-block-paragraph">Anyway, so we&#8217;ll grab those and then I&#8217;ll just show you how to use them down further in the code.</p>



<p class="wp-block-paragraph">For starters, I&#8217;m calling on an intro function which just basically prints some stuff.</p>



<p class="wp-block-paragraph">I have a custom function that I wrote called print null terminated string.</p>



<p class="wp-block-paragraph">That&#8217;s not the point of this video.</p>



<p class="wp-block-paragraph">I have that explained in other videos a little bit.</p>



<p class="wp-block-paragraph">So it&#8217;s up to you if you want to even care.</p>



<p class="wp-block-paragraph">Right now I just want to be able to print something.</p>



<p class="wp-block-paragraph">And then let&#8217;s see.</p>



<p class="wp-block-paragraph">So after we print our welcome message in the intro, then we print another message basically</p>



<p class="wp-block-paragraph">saying okay now we&#8217;re about to start printing all the arguments.</p>



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



<p class="wp-block-paragraph">Nothing complicated at this point.</p>



<p class="wp-block-paragraph">Then I have another label here with my preferred style of adding a suffix after the function</p>



<p class="wp-block-paragraph">after the function that I&#8217;m currently inside of.</p>



<p class="wp-block-paragraph">And I&#8217;m going to say, all right, this is where I initialize my loop.</p>



<p class="wp-block-paragraph">This is not a looping video, so I&#8217;m going to skim through it kind of.</p>



<p class="wp-block-paragraph">But I&#8217;m basically going to say, let&#8217;s start the loop as basically saying</p>



<p class="wp-block-paragraph">the index of the argument that we&#8217;re currently looking at is zero</p>



<p class="wp-block-paragraph">because we&#8217;ll start with zero.</p>



<p class="wp-block-paragraph">So that means we&#8217;ll print like if we were going to, if our program was named Echo,</p>



<p class="wp-block-paragraph">we would also print Echo itself and not just all the incoming arguments</p>



<p class="wp-block-paragraph">that the user might have typed.</p>



<p class="wp-block-paragraph">index zero and then we&#8217;re going to use r15 as the current character pointer so r15 its current</p>



<p class="wp-block-paragraph">character pointer is going to be coming from r13 which was the argv argument so that means</p>



<p class="wp-block-paragraph">r15 is now going to hold a pointer uh let&#8217;s see it&#8217;s going to hold</p>



<p class="wp-block-paragraph">when you have an array and the array is contiguous in memory then the pointer itself that points to</p>



<p class="wp-block-paragraph">the array is also a pointer to the first item that&#8217;s just kind of the way it works like if we</p>



<p class="wp-block-paragraph">have 10 items and it&#8217;s an array then your pointer to that array is also a pointer to the first item</p>



<p class="wp-block-paragraph">in the array if that makes sense so if r13 was a pointer to an array it&#8217;s also a pointer to the</p>



<p class="wp-block-paragraph">go off to a null terminated string i know that sounds weird you got to get used to double dereferencing</p>



<p class="wp-block-paragraph">here so all we really need to do to access uh the first pointer to the actual string is take the</p>



<p class="wp-block-paragraph">array pointer that we have and dereference it one time so remember we&#8217;re receiving an argv</p>



<p class="wp-block-paragraph">a pointer to a pointer or like a you know a double pointer if we dereference it once then we should</p>



<p class="wp-block-paragraph">terminated string. Anyway, so that&#8217;s the initialization part. I&#8217;m going to start looking</p>



<p class="wp-block-paragraph">at the first string here and I&#8217;m going to say we&#8217;re at index zero. And then here&#8217;s the top of</p>



<p class="wp-block-paragraph">my loop. You can imagine this is a while loop. This is not a while video, but you know, there it is.</p>



<p class="wp-block-paragraph">And we&#8217;re just going to ask first, are we actually done? Do we need to break the loop? I like to do</p>



<p class="wp-block-paragraph">that at the top of my while loop. And how we&#8217;ll do that is we&#8217;ll say, all right, R14 is the index</p>



<p class="wp-block-paragraph">the number of arguments so notice how like right here we grabbed RDI right</p>



<p class="wp-block-paragraph">into R12 right away so basically I&#8217;m saying if the index we&#8217;re currently</p>



<p class="wp-block-paragraph">looking at is greater than or equal to the number of arguments then we&#8217;re</p>



<p class="wp-block-paragraph">actually already done remember if we&#8217;re using zero based indexing and you</p>



<p class="wp-block-paragraph">suppose that you have three items in your array their indexes would be zero</p>



<p class="wp-block-paragraph">So that means if the size of your array is three, like the count, like the RFC, that</p>



<p class="wp-block-paragraph">means the last valid index is two.</p>



<p class="wp-block-paragraph">It&#8217;s one less than the size.</p>



<p class="wp-block-paragraph">You know, it&#8217;s one less than the count.</p>



<p class="wp-block-paragraph">So I&#8217;m saying at this point that if we ever reach an index number that is equal to the</p>



<p class="wp-block-paragraph">size, then we&#8217;ve already finished.</p>



<p class="wp-block-paragraph">We do not need to look at the current item.</p>



<p class="wp-block-paragraph">So I&#8217;m comparing those two registers.</p>



<p class="wp-block-paragraph">You know, where are we looking at versus what is the count?</p>



<p class="wp-block-paragraph">You know, what is the index we&#8217;re looking at versus the count?</p>



<p class="wp-block-paragraph">If the index we&#8217;re looking at is greater than or equal to the count, then that means we&#8217;re done.</p>



<p class="wp-block-paragraph">I&#8217;m going to jump out of the loop to a label called main loop done.</p>



<p class="wp-block-paragraph">So then, you know, that&#8217;s down here.</p>



<p class="wp-block-paragraph">Basically, we just say goodbye and then we jump to our exit function.</p>



<p class="wp-block-paragraph">And all that does is it just returns from main with some kind of a success code.</p>



<p class="wp-block-paragraph">You know, so just you can imagine in C++, it&#8217;s just return zero.</p>



<p class="wp-block-paragraph">So, you know, breaking the loop doesn&#8217;t really do anything except end the program and say goodbye.</p>



<p class="wp-block-paragraph">However, if we did not jump, then the, let&#8217;s see if we did not jump, let&#8217;s, let&#8217;s see if</p>



<p class="wp-block-paragraph">we did jump, we compared it, we realized that we&#8217;re done.</p>



<p class="wp-block-paragraph">Then it&#8217;s going to jump down to the, to the done.</p>



<p class="wp-block-paragraph">But if we&#8217;re not done, execution is going to fall through to the next statement.</p>



<p class="wp-block-paragraph">So that means we&#8217;re going to end up doing something with the current item inside of</p>



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



<p class="wp-block-paragraph">So what are we going to do?</p>



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



<p class="wp-block-paragraph">was your pointer to a pointer it was your double pointer there if we dereference it once then</p>



<p class="wp-block-paragraph">instead of being a pointer to an array of pointers it&#8217;s going to be a pointer to one string one one</p>



<p class="wp-block-paragraph">character that starts one string so dereferencing it once means you know i put brackets around the</p>



<p class="wp-block-paragraph">double pointer it&#8217;s now dereferenced once that means rdi is going to receive a pointer to one</p>



<p class="wp-block-paragraph">into my function my function takes two arguments it wants a pointer to a string and it wants</p>



<p class="wp-block-paragraph">the file descriptor to print to again other videos explain you know printing to standard</p>



<p class="wp-block-paragraph">output but i&#8217;m just going to print the string so this part right here should actually print</p>



<p class="wp-block-paragraph">the argument and then after we&#8217;re done with that uh well maybe i should change this</p>



<p class="wp-block-paragraph">so that it&#8217;s a little bit more clear i&#8217;m going to maybe push this down and say</p>



<p class="wp-block-paragraph">these lines up here actually do something they do the printing by</p>



<p class="wp-block-paragraph">dereferencing and then here we just sort of increment along the array of pointers</p>



<p class="wp-block-paragraph">and jump back to the top of the loop so what are we doing here with r14</p>



<p class="wp-block-paragraph">remember r14 was the index that we&#8217;re currently looking at so we we start</p>



<p class="wp-block-paragraph">looking at index 0 line 83 is going to say let&#8217;s next look at index 1 and then</p>



<p class="wp-block-paragraph">this part right here add 8 to r15 that just basically means remember r15 was</p>



<p class="wp-block-paragraph">remember r15 was the double pointer the double pointer like i said before it&#8217;s looking at an</p>



<p class="wp-block-paragraph">array of pointers so it&#8217;s actually a pointer to a pointer if i increase the memory location that</p>



<p class="wp-block-paragraph">r15 holds then it&#8217;s now looking at the next pointer it&#8217;s moving through the array r15 at</p>



<p class="wp-block-paragraph">that point would no longer be a valid pointer to the original array it&#8217;s sort of like a running</p>



<p class="wp-block-paragraph">pointer it&#8217;s kind of like scanning all of the pointers but a pointer is eight bytes so if we&#8217;re</p>



<p class="wp-block-paragraph">a pointer is eight bytes so if we&#8217;re just looking at the first pointer and we increase the memory</p>



<p class="wp-block-paragraph">location that we&#8217;re looking at by eight bytes now we&#8217;re looking at the second pointer so then on the</p>



<p class="wp-block-paragraph">next iteration of the loop if we dereference then we&#8217;re going to end up dereferencing to the second</p>



<p class="wp-block-paragraph">string then i just print a little new line here honestly probably should have put that up top</p>



<p class="wp-block-paragraph">sloppy code i was doing this quickly and then we jump to the top of the loop so then we just</p>



<p class="wp-block-paragraph">going up to the top of the loop and printing arguments and advancing to the</p>



<p class="wp-block-paragraph">next pointer. And we just keep going at it.</p>



<p class="wp-block-paragraph">Let me see if this works. Hopefully I didn&#8217;t mess this up while I was screwing</p>



<p class="wp-block-paragraph">around. All right. So I&#8217;m going to do this make run. Okay.</p>



<p class="wp-block-paragraph">So under the hood inside of my make file,</p>



<p class="wp-block-paragraph">maybe let&#8217;s open the make file real fast just so you can see.</p>



<p class="wp-block-paragraph">I&#8217;m just going to nano it real fast here under the hood.</p>



<p class="wp-block-paragraph">So when I call the program to run, notice how I&#8217;m giving it arguments.</p>



<p class="wp-block-paragraph">You can see right here on this line, or actually let me get the line numbers up.</p>



<p class="wp-block-paragraph">You can see on line 48, I am calling my executable, which is named main.</p>



<p class="wp-block-paragraph">In the make file, I&#8217;m using a variable, so don&#8217;t worry about that.</p>



<p class="wp-block-paragraph">But I&#8217;m giving it four arguments.</p>



<p class="wp-block-paragraph">And I&#8217;m just saying first arg, second arg, third arg, fourth arg.</p>



<p class="wp-block-paragraph">So that&#8217;s why you see that printed in the previous screen.</p>



<p class="wp-block-paragraph">screen it&#8217;s just going through all the arguments that I gave it it&#8217;s saying</p>



<p class="wp-block-paragraph">first arg second arg third arg fourth arg notice how it knows when to stop</p>



<p class="wp-block-paragraph">because of the argc that came in on RDI now that the program is built you know</p>



<p class="wp-block-paragraph">I&#8217;ve got my main executable which I named main which I said before was kind</p>



<p class="wp-block-paragraph">of confusing now that I&#8217;ve got it I can just execute it again and give it like</p>



<p class="wp-block-paragraph">one argument of like hello notice how it prints hello on a line by itself you do</p>



<p class="wp-block-paragraph">do it again dudes and notice how every argument I give it it just prints it no</p>



<p class="wp-block-paragraph">matter how many I do I can go a b c d e f g I could probably do this so many</p>



<p class="wp-block-paragraph">times that I exhaust you know a long integer which is would be a horrible</p>



<p class="wp-block-paragraph">endeavor but yeah everything that I put on there it just loops through it and</p>



<p class="wp-block-paragraph">prints it so when you are linking with GCC because you&#8217;re probably using a</p>



<p class="wp-block-paragraph">if you&#8217;re just linking with GCC.</p>



<p class="wp-block-paragraph">GCC makes it really, really easy</p>



<p class="wp-block-paragraph">to access the command line arguments.</p>



<p class="wp-block-paragraph">Imagine now instead of just printing these things,</p>



<p class="wp-block-paragraph">you used them to somehow decide</p>



<p class="wp-block-paragraph">what your program was gonna do.</p>



<p class="wp-block-paragraph">Maybe the user will give you a sub command.</p>



<p class="wp-block-paragraph">Like if you&#8217;re a Git user, we say Git status, right?</p>



<p class="wp-block-paragraph">So the Git says like, oh, you Git launches</p>



<p class="wp-block-paragraph">and it goes, you want the status of something?</p>



<p class="wp-block-paragraph">Yeah, sure.</p>



<p class="wp-block-paragraph">So in your program now,</p>



<p class="wp-block-paragraph">you could read what the user typed,</p>



<p class="wp-block-paragraph">figure out a way to parse it and interpret it</p>



<p class="wp-block-paragraph">have your program&#8217;s behavior adjust to whatever the user typed in okay that&#8217;s it for this video</p>



<p class="wp-block-paragraph">the next video I&#8217;m going to post is how to do basically exactly the same thing but using pure</p>



<p class="wp-block-paragraph">assembly without linking against GCC so like LD is going to be the linker just like pure assembly</p>



<p class="wp-block-paragraph">no extra libraries okay thank you so much for watching this video I hope you learned a little</p>



<p class="wp-block-paragraph">tell your friends 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 i really appreciate it</p>



<p class="wp-block-paragraph">i do hope you did learn something and have some fun uh if you could do me a please a small little</p>



<p class="wp-block-paragraph">favor could you please 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 really mean the</p>



<p class="wp-block-paragraph">and grow this community 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 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</p>



<p class="wp-block-paragraph">somebody subscribed or followed. It just wakes me up and I get filled with joy. That&#8217;s exactly</p>



<p class="wp-block-paragraph">what happens every single time. So you could do it as a nice favor to me or you could troll 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 just wake up.</p>



<p class="wp-block-paragraph">just wake up I promise that&#8217;s what will happen also if you look at the middle of</p>



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



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



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



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



<p class="wp-block-paragraph">offer and all that good stuff and if you have a suggestion for clarifications or</p>



<p class="wp-block-paragraph">or errata or just future videos that you want to see please leave a comment or if you just want to</p>



<p class="wp-block-paragraph">say hey what&#8217;s up what&#8217;s going on you know just send me a comment whatever i also wake up for</p>



<p class="wp-block-paragraph">those in the middle of the night i get i wake up in a cold sweat and i&#8217;m like it would really it</p>



<p class="wp-block-paragraph">really mean the world to me i would really appreciate it so again thank you so much for</p>



<p class="wp-block-paragraph">watching this video and um enjoy the cool music as as i fade into the darkness which is coming for us</p>



<p class="wp-block-paragraph">Thank you.</p>
<p>The post <a href="https://www.NeuralLantern.com/command-line-arguments-in-yasm-assembly-with-gcc-a-practical-guide/">Command Line Arguments in Yasm Assembly with GCC: A Practical Guide</a> appeared first on <a href="https://www.NeuralLantern.com">NeuralLantern.com</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.NeuralLantern.com/command-line-arguments-in-yasm-assembly-with-gcc-a-practical-guide/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Master Command Line Arguments in x86-64 Assembly with YASM</title>
		<link>https://www.NeuralLantern.com/master-command-line-arguments-in-x86-64-assembly-with-yasm/</link>
					<comments>https://www.NeuralLantern.com/master-command-line-arguments-in-x86-64-assembly-with-yasm/#respond</comments>
		
		<dc:creator><![CDATA[mike]]></dc:creator>
		<pubDate>Mon, 08 Sep 2025 07:44:41 +0000</pubDate>
				<category><![CDATA[Assembly Language]]></category>
		<category><![CDATA[Coding]]></category>
		<category><![CDATA[Computer Science]]></category>
		<category><![CDATA[Videos]]></category>
		<category><![CDATA[argc argv]]></category>
		<category><![CDATA[assembly programming]]></category>
		<category><![CDATA[assembly tutorial]]></category>
		<category><![CDATA[command line arguments]]></category>
		<category><![CDATA[Linux programming]]></category>
		<category><![CDATA[low-level programming]]></category>
		<category><![CDATA[stack pointer]]></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=221</guid>

					<description><![CDATA[<p>Learn to access command line arguments in x86-64 YASM assembly on Linux. Master stack handling and argv looping in this clear, practical tutorial!</p>
<p>The post <a href="https://www.NeuralLantern.com/master-command-line-arguments-in-x86-64-assembly-with-yasm/">Master Command Line Arguments in x86-64 Assembly with YASM</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 Command Line Arguments in x86-64 Assembly with YASM" width="1380" height="776" src="https://www.youtube.com/embed/hRl3hKvHiyM?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 master command line arguments in pure x86-64 YASM assembly on Linux? This video breaks down how to access argc and argv directly from the stack, without GCC libraries. Follow along as we write a program to loop through and print user-provided arguments, complete with a visual stack explanation. Whether you&#8217;re new to assembly or leveling up, this tutorial offers clear, practical insights for coding low-level programs. Check out my other videos for more assembly tips, and subscribe for more coding goodness!</p>



<p class="wp-block-paragraph">Introduction to Command Line Arguments 00:00:00<br>Target Architecture and Assumptions 00:00:04<br>Recap of Command Line Arguments 00:00:56<br>Accessing Arguments in Pure Assembly 00:01:47<br>Program Structure and Data Section 00:02:19<br>Stack Pointer and Argument Count 00:04:16<br>Accessing Argument Vector (argv) 00:07:14<br>Looping Through Arguments 00:12:16<br>Incrementing to Next Argument 00:16:45<br>Visualizing the Stack 00:19:24<br>Running the Program with Arguments 00:21:56<br>Conclusion and Call to Subscribe 00:23:48</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 accepting command line arguments in a pure assembly program</p>



<p class="wp-block-paragraph">written for x86-64 aka amd yasm assembly</p>



<p class="wp-block-paragraph">on linux anyway so uh there are going to be a lot of topics that i that i sort of mention in this</p>



<p class="wp-block-paragraph">video that are not actually covered in this video so if you feel yourself getting lost like for</p>



<p class="wp-block-paragraph">program in assembly in the first place.</p>



<p class="wp-block-paragraph">If you don&#8217;t know what command line arguments are,</p>



<p class="wp-block-paragraph">if you don&#8217;t know the terminal, things like that,</p>



<p class="wp-block-paragraph">you probably want to check out my previous videos</p>



<p class="wp-block-paragraph">because they will be explained in detail.</p>



<p class="wp-block-paragraph">For now, I&#8217;m just going to assume you know how to do all this basic stuff.</p>



<p class="wp-block-paragraph">And the only thing that I need to show you</p>



<p class="wp-block-paragraph">is how to write a program in Yasm assembly that can</p>



<p class="wp-block-paragraph">grab your command line arguments.</p>



<p class="wp-block-paragraph">So, you know, just to do a quick recap of what I&#8217;m even talking about.</p>



<p class="wp-block-paragraph">echo we can launch it with no arguments it doesn&#8217;t do anything but we can give</p>



<p class="wp-block-paragraph">it some arguments we can say hello and then we can say goodbye and that&#8217;s two</p>



<p class="wp-block-paragraph">arguments that I&#8217;m giving to echo echo will grab on to the arguments it was</p>



<p class="wp-block-paragraph">given it&#8217;s actually going to receive three arguments the index zero argument</p>



<p class="wp-block-paragraph">being the name of itself its own program and then the next two arguments being</p>



<p class="wp-block-paragraph">the stuff that I typed after the program but you can see that it somehow figured</p>



<p class="wp-block-paragraph">two arguments as strings and print them right so imagine you have an assembly</p>



<p class="wp-block-paragraph">program and you want the user to be able to launch your program and add</p>



<p class="wp-block-paragraph">arguments at the end of the command line and have your program behave in some</p>



<p class="wp-block-paragraph">certain way according to what the user wants the question then becomes how do</p>



<p class="wp-block-paragraph">you access those arguments it&#8217;s a little bit easier in GCC look at my other video</p>



<p class="wp-block-paragraph">that I posted about the topic for GCC linked assembly programs it&#8217;s a little</p>



<p class="wp-block-paragraph">You pretty much just look at RDI and RSI to get your argc and your character pointer array.</p>



<p class="wp-block-paragraph">When you&#8217;re dealing with pure assembly though, the GCC libraries don&#8217;t bundle up the command</p>



<p class="wp-block-paragraph">line arguments in a really convenient way for you.</p>



<p class="wp-block-paragraph">You&#8217;ve got to look for them elsewhere.</p>



<p class="wp-block-paragraph">It&#8217;s honestly not that much harder.</p>



<p class="wp-block-paragraph">It&#8217;s just a little, feels a little weird.</p>



<p class="wp-block-paragraph">I think at least for me, I was hoping to see the arguments in a register and I did not.</p>



<p class="wp-block-paragraph">But anyway, so here&#8217;s my program.</p>



<p class="wp-block-paragraph">Again, this is not an assembly programming video.</p>



<p class="wp-block-paragraph">an assembly programming video if you need to know how to program assembly see my other videos for</p>



<p class="wp-block-paragraph">now i&#8217;m just going to quickly go over it i&#8217;ve got my data section in yasm i am printing out a couple</p>



<p class="wp-block-paragraph">of strings or i i have a couple of strings defined i&#8217;m saying like the module has started i&#8217;m going</p>



<p class="wp-block-paragraph">to begin printing arguments i have some system call codes so that i can write just to standard</p>



<p class="wp-block-paragraph">output and so that i can exit the program this is pure assembly so we don&#8217;t actually return from</p>



<p class="wp-block-paragraph">we just get jumped into and then we call exit normally and then here&#8217;s the standard output pipe</p>



<p class="wp-block-paragraph">again another video covers that and then i&#8217;ve got the exit success code again another video covers</p>



<p class="wp-block-paragraph">that but basically you know exiting zero is usually what you do for success then i&#8217;ve got the</p>



<p class="wp-block-paragraph">text section which holds all my assembly instructions and this is my entry point we don&#8217;t</p>



<p class="wp-block-paragraph">really need to uh push and pop these registers because we&#8217;re just going to exit the program when</p>



<p class="wp-block-paragraph">it&#8217;s totally fine we would need to preserve Kali saved registers if we were</p>



<p class="wp-block-paragraph">jumping into a main function or if this was a different function that was being</p>



<p class="wp-block-paragraph">jumped into but we&#8217;re not so we don&#8217;t have to anyway here&#8217;s my entry point the</p>



<p class="wp-block-paragraph">underscore start for a pure assembly program and then I&#8217;m going to grab the</p>



<p class="wp-block-paragraph">incoming arguments when you&#8217;re linking with GCC which we&#8217;re not doing here in</p>



<p class="wp-block-paragraph">this video then the arguments come in very easily they just come in argc</p>



<p class="wp-block-paragraph">up in RDI and arg sorry argc comes into RDI and the character pointer array comes in as RSI so you</p>



<p class="wp-block-paragraph">can imagine in GCC this would be integer main and then integer argc character pointer array argv</p>



<p class="wp-block-paragraph">right hopefully you&#8217;ve done this in a higher level language so you have a better idea of what I&#8217;m</p>



<p class="wp-block-paragraph">talking about if not I guess that&#8217;s okay it will still allow you to grab even if you don&#8217;t</p>



<p class="wp-block-paragraph">it will still allow you to grab,</p>



<p class="wp-block-paragraph">even if you don&#8217;t understand this in C++.</p>



<p class="wp-block-paragraph">So argc is actually the stack pointer.</p>



<p class="wp-block-paragraph">The stack pointer register, RSP,</p>



<p class="wp-block-paragraph">always tells you where the top of the stack is,</p>



<p class="wp-block-paragraph">like what&#8217;s the last piece of data</p>



<p class="wp-block-paragraph">that we actually have sitting on the stack.</p>



<p class="wp-block-paragraph">So that&#8217;s gonna be the count of arguments.</p>



<p class="wp-block-paragraph">argc is always gonna be the stack pointer.</p>



<p class="wp-block-paragraph">So if you dereference the stack pointer register,</p>



<p class="wp-block-paragraph">pointer register then what you&#8217;re doing is you know the stack pointer register</p>



<p class="wp-block-paragraph">actually points to memory locations within the stack so if the memory</p>



<p class="wp-block-paragraph">location that it&#8217;s looking at is where argc is stored then you dereference it</p>



<p class="wp-block-paragraph">with the brackets and you&#8217;ll actually get argc so right away I&#8217;m just gonna</p>



<p class="wp-block-paragraph">steal argc off the top of the stack and this is a good idea to do right away</p>



<p class="wp-block-paragraph">because if you start doing other stuff you might end up modifying the stack</p>



<p class="wp-block-paragraph">especially if you start calling functions and things like that and and</p>



<p class="wp-block-paragraph">and and these these pieces of information will be lost or they&#8217;ll be a lot harder to find so right</p>



<p class="wp-block-paragraph">away i&#8217;m just going to say argc goes directly into r12 and i have a little comment reminder</p>



<p class="wp-block-paragraph">for myself at the top saying r12 is now argc same thing for uh r13 i&#8217;m going to say that&#8217;s a pointer</p>



<p class="wp-block-paragraph">to my character pointer array i talked about this in depth on my other video where we talked about</p>



<p class="wp-block-paragraph">an assembly program but basically if you look at uh if you look at argv right here it&#8217;s not just a</p>



<p class="wp-block-paragraph">pointer and it&#8217;s not just an array it&#8217;s a pointer to an array and if you recall in higher level</p>



<p class="wp-block-paragraph">languages or just i guess anything an array itself is a pointer because argv the symbol let&#8217;s say we</p>



<p class="wp-block-paragraph">were in a higher level language argv can only point to one thing so it&#8217;s going to point to the very</p>



<p class="wp-block-paragraph">but the beginning of your array is not going to be one string like one argument that the user gave</p>



<p class="wp-block-paragraph">you because the user could give you many arguments instead it&#8217;s an array of pointers</p>



<p class="wp-block-paragraph">so it&#8217;s a character pointer array meaning argv is a pointer to a pointer</p>



<p class="wp-block-paragraph">the first item in any array is what is pointed to by the symbol so like imagine if we had just</p>



<p class="wp-block-paragraph">had just for the sake of argument imagine if we had an integer array let&#8217;s say we had like 500</p>



<p class="wp-block-paragraph">integers in like a c++ program so the symbol a is what i&#8217;m trying to say the symbol a</p>



<p class="wp-block-paragraph">is really a pointer that points to the first integer in that array and then later of course</p>



<p class="wp-block-paragraph">you can dereference that pointer with some sort of an index to get the index 5 integer or the</p>



<p class="wp-block-paragraph">the array and a pointer to the first item.</p>



<p class="wp-block-paragraph">Or I guess you could say it&#8217;s both a pointer to the array</p>



<p class="wp-block-paragraph">and a pointer to the first item,</p>



<p class="wp-block-paragraph">because it&#8217;s kind of the same thing.</p>



<p class="wp-block-paragraph">So that means if we grab argv,</p>



<p class="wp-block-paragraph">then we&#8217;ll be grabbing a pointer.</p>



<p class="wp-block-paragraph">If we then dereference that pointer,</p>



<p class="wp-block-paragraph">it would tell us where another pointer is.</p>



<p class="wp-block-paragraph">And then that other pointer would point to a string</p>



<p class="wp-block-paragraph">representing the first argument.</p>



<p class="wp-block-paragraph">first argument so we&#8217;ll explain that more as i do this loop i&#8217;m going to i&#8217;m going to show you</p>



<p class="wp-block-paragraph">some code that actually loops through all the arguments anyway the thing about where to find</p>



<p class="wp-block-paragraph">that argv is it&#8217;s just the next pointer or i guess it&#8217;s the it&#8217;s the next item that the stack holds</p>



<p class="wp-block-paragraph">so remember i told you that rsp when this when this function comes in rsp is a pointer to wherever</p>



<p class="wp-block-paragraph">we&#8217;re we&#8217;re looking what memory location the top of the stack is so if we dereference that we get</p>



<p class="wp-block-paragraph">So if we dereference that, we get argc.</p>



<p class="wp-block-paragraph">Well, we just need to go find the next previous item in the stack</p>



<p class="wp-block-paragraph">and then dereference that so that we can get the first pointer or argv,</p>



<p class="wp-block-paragraph">you know, the first pointer to a pointer,</p>



<p class="wp-block-paragraph">or the first double pointer in your array of pointer to pointers.</p>



<p class="wp-block-paragraph">Why am I doing plus eight?</p>



<p class="wp-block-paragraph">This might actually make sense as is,</p>



<p class="wp-block-paragraph">sense as is but it&#8217;s important to understand that when you add stuff to the stack you&#8217;re actually</p>



<p class="wp-block-paragraph">decreasing the memory locations uh that you are pointing to so like if i if i increase a stack</p>



<p class="wp-block-paragraph">this is not a stack video there&#8217;s other videos that i have for that um if we add something to</p>



<p class="wp-block-paragraph">the top of a stack you imagine the stack growing visually in a in a vertical direction like it&#8217;s</p>



<p class="wp-block-paragraph">growing up right but inside of the computer the memory locations are actually going down</p>



<p class="wp-block-paragraph">If we&#8217;re looking at the stack pointer and we&#8217;re adding eight, what it&#8217;s saying is that</p>



<p class="wp-block-paragraph">we&#8217;re looking back into memory that was already added for the stack.</p>



<p class="wp-block-paragraph">So that&#8217;s like if we grew the stack, that would be subtracting from the memory location.</p>



<p class="wp-block-paragraph">So like looking further up in the stack would be subtracting from the memory location because</p>



<p class="wp-block-paragraph">the stack grows upward in the abstract, but downward in memory locations.</p>



<p class="wp-block-paragraph">So that means if we add eight, that means we&#8217;re going in the other direction.</p>



<p class="wp-block-paragraph">that means we&#8217;re going in the other direction.</p>



<p class="wp-block-paragraph">We&#8217;re looking downward into the stack.</p>



<p class="wp-block-paragraph">So what that means is that the top two items on the stack</p>



<p class="wp-block-paragraph">are first argc and then second, that first pointer,</p>



<p class="wp-block-paragraph">you know, the argv argument.</p>



<p class="wp-block-paragraph">And that&#8217;s how we access it with plus eight.</p>



<p class="wp-block-paragraph">Why is it a plus eight instead of plus something else?</p>



<p class="wp-block-paragraph">Because all pointers on 64-bit systems are 64-bit integers</p>



<p class="wp-block-paragraph">and 64-bit integers are eight bytes.</p>



<p class="wp-block-paragraph">If we&#8217;re talking about eight bytes, sorry,</p>



<p class="wp-block-paragraph">if we&#8217;re talking about eight bits per byte.</p>



<p class="wp-block-paragraph">So this is just how you access it.</p>



<p class="wp-block-paragraph">What about this other thing going on here?</p>



<p class="wp-block-paragraph">What&#8217;s this LEA instruction?</p>



<p class="wp-block-paragraph">Usually you see a move instruction, right?</p>



<p class="wp-block-paragraph">So we basically want to look at the stack pointer,</p>



<p class="wp-block-paragraph">but look one level lower.</p>



<p class="wp-block-paragraph">And each item on the stack is also going to be eight bytes.</p>



<p class="wp-block-paragraph">We want to look one item lower on the stack,</p>



<p class="wp-block-paragraph">and then we want to dereference it.</p>



<p class="wp-block-paragraph">but we don&#8217;t want to dereference and store the dereferenced value we still want to grab the</p>



<p class="wp-block-paragraph">actual pointer the thing is I shouldn&#8217;t have said dereference in the first place these are dereferencing</p>



<p class="wp-block-paragraph">brackets right so like up here on line 46 when you put brackets around rsp it dereferences whatever</p>



<p class="wp-block-paragraph">value rsp holds so therefore when you put brackets around this you kind of expect that you&#8217;re going</p>



<p class="wp-block-paragraph">to be dereferencing right but we don&#8217;t want to dereference we just want the original address that</p>



<p class="wp-block-paragraph">showing us. So the problem with this is that when we say RSP plus eight,</p>



<p class="wp-block-paragraph">we can&#8217;t actually do a mathematical formula unless we&#8217;re inside of brackets, which means we</p>



<p class="wp-block-paragraph">can&#8217;t do them in a move instruction without accidentally dereferencing and losing a pointer</p>



<p class="wp-block-paragraph">to the actual array of pointers. If we dereferenced like this, we would just end up with,</p>



<p class="wp-block-paragraph">you know, a pointer to one string. So the way around this is instead of using the move instruction,</p>



<p class="wp-block-paragraph">instead of using the move instruction we use the lea instruction the lea instruction allows us to</p>



<p class="wp-block-paragraph">put a formula inside of brackets so that the assembler won&#8217;t get confused but then it won&#8217;t</p>



<p class="wp-block-paragraph">put the dereferenced value into r13 it&#8217;ll just put the actual value of whatever we see</p>



<p class="wp-block-paragraph">with that formula meaning it&#8217;ll give us the memory location of the item sitting one under the top of</p>



<p class="wp-block-paragraph">this at this point r13 is now the memory location of the of the item sitting one underneath the top</p>



<p class="wp-block-paragraph">of the stack and then we can de-reference that later in order to look at all of our arguments</p>



<p class="wp-block-paragraph">so the next part of our code is just going to print an intro message so there&#8217;s like this intro</p>



<p class="wp-block-paragraph">string up here hello and um i&#8217;m basically going to call a custom function that i made to just sort of</p>



<p class="wp-block-paragraph">Printing. Don&#8217;t worry about this code right here.</p>



<p class="wp-block-paragraph">This is not the point of the video.</p>



<p class="wp-block-paragraph">I just made a custom function that just kind of helped me print.</p>



<p class="wp-block-paragraph">Don&#8217;t worry about that.</p>



<p class="wp-block-paragraph">The real meat of this video is that we&#8217;re going to loop through all arguments</p>



<p class="wp-block-paragraph">and just print every single argument that the user provided.</p>



<p class="wp-block-paragraph">So you can imagine this is maybe the top of a while loop here.</p>



<p class="wp-block-paragraph">Notice how, oh, I forgot to replace main with start with start.</p>



<p class="wp-block-paragraph">I had another version of this program that used main.</p>



<p class="wp-block-paragraph">pretend we have this is called the main loop instead of the the main functions</p>



<p class="wp-block-paragraph">loop anyway so main loop initialize first thing I&#8217;m going to do is I&#8217;m going</p>



<p class="wp-block-paragraph">to initialize the loop by saying we&#8217;re looking at index 0 okay no problem so</p>



<p class="wp-block-paragraph">we&#8217;re looking at index 0 we want to do that we want to look at all the</p>



<p class="wp-block-paragraph">arguments you know we want to look at index 0 and index 1 and index 2 and we</p>



<p class="wp-block-paragraph">just want to keep going until we&#8217;re out of arguments remember that we also have</p>



<p class="wp-block-paragraph">the number of arguments coming from argc now sitting in r12. So that means if we have a</p>



<p class="wp-block-paragraph">counter that starts at zero with r14 and then we know how many arguments there are in r12,</p>



<p class="wp-block-paragraph">we should be able to know when the loop stops. So just a reminder that, you know, the loops here,</p>



<p class="wp-block-paragraph">the arrays here are zero-based indexed or zero-index based, which means, for example,</p>



<p class="wp-block-paragraph">that&#8217;s three arguments but the array of arguments is going to have indexes zero and one and two</p>



<p class="wp-block-paragraph">which means the last valid index is going to be two or the size minus one think about that if you</p>



<p class="wp-block-paragraph">have five arguments the last index that is valid is going to be four size minus one because it&#8217;s</p>



<p class="wp-block-paragraph">zero based so we can use that logic to figure out if we&#8217;re done so we start off with an index of zero</p>



<p class="wp-block-paragraph">here and then at the top of the loop i&#8217;m just going to quickly ask are we done how do we know</p>



<p class="wp-block-paragraph">are we done how do we know if we&#8217;re done we compare the current index we&#8217;re</p>



<p class="wp-block-paragraph">looking at with the count and we say if the current index we&#8217;re looking at is</p>



<p class="wp-block-paragraph">greater than or equal to the count then we know we&#8217;re already done we don&#8217;t need</p>



<p class="wp-block-paragraph">to look at any more indexes why am I saying is is the index greater than or</p>



<p class="wp-block-paragraph">equal to the count because remember if we have five arguments then the last</p>



<p class="wp-block-paragraph">valid index is four therefore if we find ourselves looking at index five we know</p>



<p class="wp-block-paragraph">already done five is equal to or greater than five, but four is not equal to or greater</p>



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



<p class="wp-block-paragraph">So that&#8217;s why I&#8217;m using this logic here.</p>



<p class="wp-block-paragraph">I&#8217;m saying if we&#8217;re done, then jump to the done label.</p>



<p class="wp-block-paragraph">Basically what&#8217;s the done label?</p>



<p class="wp-block-paragraph">It&#8217;s main loop done.</p>



<p class="wp-block-paragraph">All it really does is it says goodbye and then it just exits the program with a system</p>



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



<p class="wp-block-paragraph">Again, I have other videos that explain system calls and all that stuff.</p>



<p class="wp-block-paragraph">So if we&#8217;re done, we, uh, we jumped down to the done area, but if not, then execute</p>



<p class="wp-block-paragraph">to the done area but if not then execution will fall through to this next loop or sorry this next</p>



<p class="wp-block-paragraph">label which is not necessarily we don&#8217;t really need a label there but i i like to put i like to</p>



<p class="wp-block-paragraph">put it there just to help myself remember this is what it looks like in terms of a while loop you</p>



<p class="wp-block-paragraph">know i have like here&#8217;s the top of the while loop here&#8217;s the little comparison part of the while loop</p>



<p class="wp-block-paragraph">here&#8217;s the body like the opening braces of the while loop and then here&#8217;s sort of like the</p>



<p class="wp-block-paragraph">closing braces i just like to do that but anyway after we decided that we are not done and we drop</p>



<p class="wp-block-paragraph">done and we drop through to the next actual instruction we&#8217;re just going to</p>



<p class="wp-block-paragraph">print the next argument so what&#8217;s the next argument we will dereference r13</p>



<p class="wp-block-paragraph">remember up here we took the second item sitting like you know one under the top</p>



<p class="wp-block-paragraph">of the stack and we just stuck it into r13 so this is r13 is now the address of</p>



<p class="wp-block-paragraph">the second item on the stack if we de-reference it then that&#8217;s going to give</p>



<p class="wp-block-paragraph">then that&#8217;s going to give us a pointer to our um sorry it&#8217;s going to give us a pointer to the first</p>



<p class="wp-block-paragraph">item to the first string i should say to the first string uh the f this the string of the first</p>



<p class="wp-block-paragraph">argument so if we dereference you know let me say this one more time just to make sure that i&#8217;m</p>



<p class="wp-block-paragraph">item, you know, the item right under the top of the stack, that&#8217;s going to be a pointer</p>



<p class="wp-block-paragraph">to the first argument string.</p>



<p class="wp-block-paragraph">So if we just simply dereference R13, then we&#8217;re basically telling RDI, here&#8217;s a pointer</p>



<p class="wp-block-paragraph">to the string that we want to print.</p>



<p class="wp-block-paragraph">Okay, so then I just call my helper function to print that string to standard output, no</p>



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



<p class="wp-block-paragraph">And then we have to figure out like, how do we increment to go to the next string?</p>



<p class="wp-block-paragraph">might have been unclear earlier because gcc does it in a different way but on the stack</p>



<p class="wp-block-paragraph">every item underneath the the top of the stack is a is another pointer it&#8217;s part of the original argv</p>



<p class="wp-block-paragraph">array it&#8217;s another pointer to a different string so basically if i increase r14 here then i&#8217;m</p>



<p class="wp-block-paragraph">increasing the index counter that&#8217;ll help us eventually terminate but notice how here i&#8217;m</p>



<p class="wp-block-paragraph">Remember R13 is the second item, you know, the item right under the top of the stack.</p>



<p class="wp-block-paragraph">And if I dereference that, then I now have a pointer to the first argument string.</p>



<p class="wp-block-paragraph">If I want to go one lower into the stack, then I just add eight to that register&#8217;s value.</p>



<p class="wp-block-paragraph">Because again, remember, the stack grows downward in memory.</p>



<p class="wp-block-paragraph">So if I increase the value, then I&#8217;m sort of like going through previous items that were put into the stack.</p>



<p class="wp-block-paragraph">So imagine the stack here, it&#8217;s got like,</p>



<p class="wp-block-paragraph">you know, argc sitting on top</p>



<p class="wp-block-paragraph">and then underneath argc,</p>



<p class="wp-block-paragraph">it&#8217;s got a pointer to the first argument.</p>



<p class="wp-block-paragraph">And then under that in the stack,</p>



<p class="wp-block-paragraph">it&#8217;s got a pointer to the second argument.</p>



<p class="wp-block-paragraph">And then under that in the stack,</p>



<p class="wp-block-paragraph">it&#8217;s got a pointer to the third argument.</p>



<p class="wp-block-paragraph">Then under that, it&#8217;s got a pointer</p>



<p class="wp-block-paragraph">to the next argument and so forth.</p>



<p class="wp-block-paragraph">So every time we wanna go to the next arguments pointer,</p>



<p class="wp-block-paragraph">the next arguments string pointer,</p>



<p class="wp-block-paragraph">we just add eight to that R13 register,</p>



<p class="wp-block-paragraph">which was originally just pointing at the first string.</p>



<p class="wp-block-paragraph">so again why eight because pointers are 64-bit integers therefore they are eight bytes so I&#8217;m</p>



<p class="wp-block-paragraph">just literally increasing the index by one and then I&#8217;m moving that register r13 to point to</p>



<p class="wp-block-paragraph">the next pointer that way next time I dereference it up here on line 70 I&#8217;ll be dereferencing the</p>



<p class="wp-block-paragraph">next string and these strings don&#8217;t need to be contiguous they could be located anywhere it&#8217;s</p>



<p class="wp-block-paragraph">that is contiguous but it&#8217;s just full of pointers to other strings or sorry it&#8217;s full of if we de</p>



<p class="wp-block-paragraph">reference the stack pointer at any point then we will get the address of a string so then we will</p>



<p class="wp-block-paragraph">get a pointer to a string this double pointer stuff sometimes i get tongue tongue tired okay so uh</p>



<p class="wp-block-paragraph">when we&#8217;re done printing and incrementing then we just jump to the top of the loop</p>



<p class="wp-block-paragraph">And then finally, when we&#8217;re done, well, we&#8217;re done.</p>



<p class="wp-block-paragraph">Let me draw this out for you, because I think the way I&#8217;m explaining it might be</p>



<p class="wp-block-paragraph">a little bit unclear, so I just want to make sure that I&#8217;m being totally clear on</p>



<p class="wp-block-paragraph">this, okay, so I&#8217;ve got my little annotator here and you can imagine here&#8217;s a stack</p>



<p class="wp-block-paragraph">and it visually grows up.</p>



<p class="wp-block-paragraph">We can imagine that every time we add an item to the stack, it grows up.</p>



<p class="wp-block-paragraph">Like if you wanted to take a five, stick it on top of the stack.</p>



<p class="wp-block-paragraph">Well, then it would end up on the top of the stack, right?</p>



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



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



<p class="wp-block-paragraph">Oh, maybe this needs to be bigger because of my pen size.</p>



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



<p class="wp-block-paragraph">What the heck happened?</p>



<p class="wp-block-paragraph">I lost the whole desktop.</p>



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



<p class="wp-block-paragraph">So we have this and this and this and this.</p>



<p class="wp-block-paragraph">And so there&#8217;s probably some kind of a value sitting on top of the stack.</p>



<p class="wp-block-paragraph">When we started the program,</p>



<p class="wp-block-paragraph">RSP is a register that just has the memory location of the top of the stack,</p>



<p class="wp-block-paragraph">you know, the most recently added item.</p>



<p class="wp-block-paragraph">you know the most recently added item we know that this was actually arg c so if we dereferenced rsp</p>



<p class="wp-block-paragraph">that&#8217;ll go to wherever rx is and get it so we can basically say that rx is sitting on the top of</p>



<p class="wp-block-paragraph">the stack so you know we dereference our rsp which holds a memory location to this place in memory</p>



<p class="wp-block-paragraph">we just had an integer stored an eight byte integer okay cool so then the next lowest i guess</p>



<p class="wp-block-paragraph">the next highest item or the next lower item is arg v at index zero which is the same thing as</p>



<p class="wp-block-paragraph">just saying arg v remember a pointer to the array is really a pointer to the first item</p>



<p class="wp-block-paragraph">the next lower item is arg v at index one and so forth so we can just keep doing this we can keep</p>



<p class="wp-block-paragraph">keep doing this we can keep going lower and lower and lower on the stack by adding eight to r13</p>



<p class="wp-block-paragraph">because remember r13 originally pointed uh you know to this item right here i don&#8217;t know i&#8217;ll</p>



<p class="wp-block-paragraph">say like r13 maybe starts off pointing uh to the first argument because that&#8217;s the way we have it</p>



<p class="wp-block-paragraph">set up so every time the loop iterates if we dereference r13 then we&#8217;re getting a pointer to</p>



<p class="wp-block-paragraph">when we add eight bytes to r13 we&#8217;re really just moving it down to the next pointer that we can</p>



<p class="wp-block-paragraph">dereference hopefully that was a little bit more clear than what i said before or maybe you&#8217;re just</p>



<p class="wp-block-paragraph">a visual learner um you know it&#8217;s a good idea to try to explain things in many different ways</p>



<p class="wp-block-paragraph">but long story short we&#8217;re going down down down further in the stack to get more arguments and</p>



<p class="wp-block-paragraph">we&#8217;re just printing them and so now that we&#8217;ve explained it all we should be able to just run</p>



<p class="wp-block-paragraph">clear and make run and under the hood let me just show you real fast what I&#8217;ve got inside of my make</p>



<p class="wp-block-paragraph">file under the hood when we do make run you don&#8217;t need to know about make files I have a video that</p>



<p class="wp-block-paragraph">explains all of this don&#8217;t worry but well I mean you don&#8217;t need to know it to understand this video</p>



<p class="wp-block-paragraph">but you probably do need to know it notice how when I call the executable which in this case is</p>



<p class="wp-block-paragraph">I give it some arguments.</p>



<p class="wp-block-paragraph">I give it just some strings.</p>



<p class="wp-block-paragraph">First arg, second arg, third arg, fourth arg.</p>



<p class="wp-block-paragraph">So that&#8217;s the same thing as if I typed echo first arg,</p>



<p class="wp-block-paragraph">second arg, third arg, fourth arg, right?</p>



<p class="wp-block-paragraph">That&#8217;s what echo is doing.</p>



<p class="wp-block-paragraph">So in the make file, I&#8217;m just giving it those arguments.</p>



<p class="wp-block-paragraph">And then our program now is looping</p>



<p class="wp-block-paragraph">through all of the arguments.</p>



<p class="wp-block-paragraph">It knows when to stop because we grabbed argc</p>



<p class="wp-block-paragraph">and it knows where those strings are located</p>



<p class="wp-block-paragraph">are located because the pointers to those strings are just sitting on the stack so if we deref twice</p>



<p class="wp-block-paragraph">we&#8217;re derefing we&#8217;re doing a double dereference a pointer to a pointer</p>



<p class="wp-block-paragraph">at least we could say r13 is a pointer to a pointer but the actual values sitting inside</p>



<p class="wp-block-paragraph">the stack are just pointers to strings um so let&#8217;s just do this in a slightly different way</p>



<p class="wp-block-paragraph">clear and then I&#8217;ll say main because that&#8217;s the name of the program that I</p>



<p class="wp-block-paragraph">compiled you can imagine this is could be named something else and I&#8217;ll just put</p>



<p class="wp-block-paragraph">hello you are super cool now so I&#8217;m giving it one two three four five</p>



<p class="wp-block-paragraph">arguments in addition to main so it should print six things main hello you</p>



<p class="wp-block-paragraph">are super cool now so notice how it prints main hello you are super cool now</p>



<p class="wp-block-paragraph">Alright, that&#8217;s pretty much everything that I wanted to show you for the pure assembly</p>



<p class="wp-block-paragraph">version of Grappin Command Line Arguments.</p>



<p class="wp-block-paragraph">Thank you so much for listening.</p>



<p class="wp-block-paragraph">It&#8217;s a little late for me.</p>



<p class="wp-block-paragraph">The sun&#8217;s starting to come up.</p>



<p class="wp-block-paragraph">I&#8217;ve got to go and eat a bunch of cookies.</p>



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



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



<p class="wp-block-paragraph">I will see you in the next one.</p>



<p class="wp-block-paragraph">Happy studying and coding and all that stuff.</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-command-line-arguments-in-x86-64-assembly-with-yasm/">Master Command Line Arguments in x86-64 Assembly with YASM</a> appeared first on <a href="https://www.NeuralLantern.com">NeuralLantern.com</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.NeuralLantern.com/master-command-line-arguments-in-x86-64-assembly-with-yasm/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 loading="lazy" 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>Mastering System Services in x86-64 Assembly Programming</title>
		<link>https://www.NeuralLantern.com/mastering-system-services-in-x86-64-assembly-programming/</link>
					<comments>https://www.NeuralLantern.com/mastering-system-services-in-x86-64-assembly-programming/#respond</comments>
		
		<dc:creator><![CDATA[mike]]></dc:creator>
		<pubDate>Mon, 18 Aug 2025 14:42:23 +0000</pubDate>
				<category><![CDATA[Assembly Language]]></category>
		<category><![CDATA[Coding]]></category>
		<category><![CDATA[Computer Science]]></category>
		<category><![CDATA[Videos]]></category>
		<category><![CDATA[assembly language]]></category>
		<category><![CDATA[assembly programming]]></category>
		<category><![CDATA[coding tutorials]]></category>
		<category><![CDATA[computer science]]></category>
		<category><![CDATA[exit codes]]></category>
		<category><![CDATA[file descriptors]]></category>
		<category><![CDATA[file operations]]></category>
		<category><![CDATA[low-level programming]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[standard output]]></category>
		<category><![CDATA[syscalls]]></category>
		<category><![CDATA[system calls]]></category>
		<category><![CDATA[system services]]></category>
		<category><![CDATA[x86-64 assembly]]></category>
		<category><![CDATA[Yasm assembly]]></category>
		<guid isPermaLink="false">https://www.NeuralLantern.com/?p=205</guid>

					<description><![CDATA[<p>Learn x86-64 assembly system services! Master syscalls, file descriptors, and program exits with clear examples in this coding tutorial. #AssemblyProgramming</p>
<p>The post <a href="https://www.NeuralLantern.com/mastering-system-services-in-x86-64-assembly-programming/">Mastering System Services in x86-64 Assembly Programming</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="Mastering System Services in x86-64 Assembly Programming" width="1380" height="776" src="https://www.youtube.com/embed/-ukGDzRNTxU?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">Ready to master x86-64 assembly? This video breaks down system services, showing you how to use syscalls to print messages, handle file descriptors, and exit programs cleanly. We’ll walk through a real assembly program, explain key concepts like standard output and file handles, and share tips from a top book on the subject. Whether you’re new to assembly or sharpening your skills, this tutorial is packed with clear examples and practical advice. Subscribe for more coding deep dives, and check out our upcoming file I/O video! #AssemblyProgramming #SystemCalls #x86_64 #CodingTutorials</p>



<p class="wp-block-paragraph">Introduction to System Services 00:00:00<br>System Services in x86-64 Assembly 00:00:04<br>Recommended Book on Assembly 00:00:35<br>What is a System Service? 00:01:01<br>Example Assembly Program 00:01:21<br>Syscall Instruction Explanation 00:02:52<br>Standard Output and File Descriptors 00:04:01<br>Printing a String with Syscall 00:05:16<br>Exiting a Program with Syscall 00:07:21<br>Exit Codes and Program Success 00:08:02<br>Book Reference for System Services 00:09:51<br>Detailed System Write Service 00:11:01<br>Checking System Call Return Values 00:12:08<br>File Operations with System Calls 00:14:35<br>Closing Files and Best Practices 00:15:50<br>Other System Call Codes 00:16:43<br>File Permissions and Modes 00:17:20<br>Handling System Call Results 00:19:03<br>Future Video Plans and Wrap-Up 00:20:40<br>Call to Subscribe and Outro 00:21:28</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 we&#8217;re going to talk a little bit about system services</p>



<p class="wp-block-paragraph">in an x86-64 machine while programming in assembly</p>



<p class="wp-block-paragraph">this video is going to focus on yasm assembly but this should also work for any other assembly</p>



<p class="wp-block-paragraph">language that you program as long as you have the right kind of cpu x86-64 is also known as</p>



<p class="wp-block-paragraph">that in mind okay so for starters let&#8217;s see here&#8217;s a wonderful book i love pushing this book i did</p>



<p class="wp-block-paragraph">not write it the person who wrote it is a genius it&#8217;s called x86 64 assembly language programming</p>



<p class="wp-block-paragraph">with the boot to it&#8217;s a free and open source book so anybody can go get it find his website</p>



<p class="wp-block-paragraph">and here&#8217;s the version i&#8217;m using and honestly a really old version of this book is still really</p>



<p class="wp-block-paragraph">good he just keeps you know making little improvements but it was it was good even a</p>



<p class="wp-block-paragraph">even a long time ago. Anyway, so what is a system service? For starters, before I go into this book</p>



<p class="wp-block-paragraph">a little bit more, what is the system service? Suppose for the sake of argument that I&#8217;ve</p>



<p class="wp-block-paragraph">already written an assembly program, a pure assembly program, not a hybrid module program</p>



<p class="wp-block-paragraph">or anything. If you want to learn assembly, see my other videos, but for now I&#8217;m just going to</p>



<p class="wp-block-paragraph">assume you kind of already know how, so I&#8217;m just going to open up a pre-made program here.</p>



<p class="wp-block-paragraph">I&#8217;m going to do nano so I can edit the assembly file just so you can see what&#8217;s inside of here.</p>



<p class="wp-block-paragraph">just so you can see what&#8217;s inside of here.</p>



<p class="wp-block-paragraph">Okay, so I hope the red is not too hard to see.</p>



<p class="wp-block-paragraph">Oh, the tabs got ruined on Nano.</p>



<p class="wp-block-paragraph">Hang on, let me see if I can just open this up in Genie.</p>



<p class="wp-block-paragraph">Should have done that to begin with, okay.</p>



<p class="wp-block-paragraph">Yeah, okay, I wrote it in Genie</p>



<p class="wp-block-paragraph">and I guess I don&#8217;t have the tabs set up in Nano very well.</p>



<p class="wp-block-paragraph">Okay, so here&#8217;s like my assembly program.</p>



<p class="wp-block-paragraph">You know, I just have a little string here</p>



<p class="wp-block-paragraph">string length and CRLF for like a line feed and some file descriptors and stuff</p>



<p class="wp-block-paragraph">that I talk about in other videos here&#8217;s the entry point of the program just like</p>



<p class="wp-block-paragraph">a little start entry point because it&#8217;s pure assembly and all I&#8217;m gonna do is</p>



<p class="wp-block-paragraph">I&#8217;m gonna print a message right I&#8217;m just gonna print a little hello message and</p>



<p class="wp-block-paragraph">then call on CRLF all CRLF does is just it just prints a new line so the program</p>



<p class="wp-block-paragraph">if i run it right now you&#8217;ll just see what happens if i say let&#8217;s see clear and make run</p>



<p class="wp-block-paragraph">then you can see there&#8217;s some stuff happening up here in the make file which i forgot to update</p>



<p class="wp-block-paragraph">the title ignore that basically the real action is this line right here hello i&#8217;m printing using</p>



<p class="wp-block-paragraph">a system service if you already know assembly then you have probably already seen this before</p>



<p class="wp-block-paragraph">and it seems a little boring that&#8217;s good but i just want you to know that we&#8217;re using a system</p>



<p class="wp-block-paragraph">system service to actually do the printing.</p>



<p class="wp-block-paragraph">Kind of take it for granted here.</p>



<p class="wp-block-paragraph">This little instruction called syscall.</p>



<p class="wp-block-paragraph">What is a syscall?</p>



<p class="wp-block-paragraph">Well, it&#8217;s just basically you set up some variables, some,</p>



<p class="wp-block-paragraph">some codes, some data, whatever in, in some special registers.</p>



<p class="wp-block-paragraph">One of the registers lets the system know what you want it to do.</p>



<p class="wp-block-paragraph">One of the registers is like, or I guess all the following registers</p>



<p class="wp-block-paragraph">if they&#8217;re needed, are just input to the system call that you&#8217;re trying to do.</p>



<p class="wp-block-paragraph">do so in this case rax is letting the system know i want you to do this this code i want you to do</p>



<p class="wp-block-paragraph">this certain thing i have a defined set up here called system right but if you look up in my</p>



<p class="wp-block-paragraph">little defines area it&#8217;s just a number one so that means if i set the number one in rax i&#8217;m letting</p>



<p class="wp-block-paragraph">the system know that i want it to write to some pipe to some file then you know depending on what</p>



<p class="wp-block-paragraph">code you&#8217;re using rdi rsi rdx and other things may be needed or maybe not be needed at all</p>



<p class="wp-block-paragraph">in this case when i&#8217;m writing somewhere it wants three arguments so i have to use rdi rsi and rdx</p>



<p class="wp-block-paragraph">because those are the three arguments the first argument that it wants is where to print</p>



<p class="wp-block-paragraph">see my other videos for a more in-depth explanation of file descriptors and pipes but just long story</p>



<p class="wp-block-paragraph">pipes standard input standard output and standard error standard out is a pipe</p>



<p class="wp-block-paragraph">number one let&#8217;s see where&#8217;s that yeah it&#8217;s just assigned a number one so</p>



<p class="wp-block-paragraph">every program has its own number one pipe which is just standard output and I</p>



<p class="wp-block-paragraph">guess I should say that it gets piped to file descriptor one it&#8217;s kind of like an</p>



<p class="wp-block-paragraph">ambiguous term sort of but the system right call will write data to a file</p>



<p class="wp-block-paragraph">file and if i give it a file handle of just one that lets the system know oh actually i want to</p>



<p class="wp-block-paragraph">write to this standard output for the one particular process that we&#8217;re inside of but</p>



<p class="wp-block-paragraph">you could open any file that you want and get like a file handle to it and the operating system will</p>



<p class="wp-block-paragraph">realize oh you know i i associate that file handle i gave you with this certain open file that you</p>



<p class="wp-block-paragraph">created or opened for reading or whatever um and so when you give the handle back it knows where</p>



<p class="wp-block-paragraph">knows where to write. So in this case,</p>



<p class="wp-block-paragraph">it&#8217;s treating the standard output just like a file,</p>



<p class="wp-block-paragraph">just a file with a special handle of just one.</p>



<p class="wp-block-paragraph">I&#8217;ll talk about that more in another video.</p>



<p class="wp-block-paragraph">Then the next two arguments it wants is just the,</p>



<p class="wp-block-paragraph">a pointer to the first character of the message that you want to print.</p>



<p class="wp-block-paragraph">And then just an integer representing the length of the message that you want</p>



<p class="wp-block-paragraph">to print. And if we just look up here again at my little hello,</p>



<p class="wp-block-paragraph">my hello symbol points to an array of bytes, just characters.</p>



<p class="wp-block-paragraph">bytes just characters there is no null terminator on there it&#8217;s just bytes only</p>



<p class="wp-block-paragraph">and so when I give it you know message hello by itself what I&#8217;m really giving</p>



<p class="wp-block-paragraph">it is a pointer to that H letter just a pointer to the memory look you know I&#8217;m</p>



<p class="wp-block-paragraph">pointing to the memory location of the very first character in that string and</p>



<p class="wp-block-paragraph">then for the length I&#8217;m just using a special token little shortcut thing</p>



<p class="wp-block-paragraph">where you just put a dollar sign minus and then the name of another string and</p>



<p class="wp-block-paragraph">and then the name of another string,</p>



<p class="wp-block-paragraph">and then the assembler will compute the length of that string.</p>



<p class="wp-block-paragraph">So this right here is the same thing as me</p>



<p class="wp-block-paragraph">just typing the exact length of the string.</p>



<p class="wp-block-paragraph">I don&#8217;t even know what it is.</p>



<p class="wp-block-paragraph">I don&#8217;t feel like counting it.</p>



<p class="wp-block-paragraph">You can at home if you want.</p>



<p class="wp-block-paragraph">One, two, three, four, I&#8217;m not gonna do it.</p>



<p class="wp-block-paragraph">Anyway, so we basically just say,</p>



<p class="wp-block-paragraph">you know, I would like to write somewhere.</p>



<p class="wp-block-paragraph">I would like to write to the standard output file,</p>



<p class="wp-block-paragraph">file handle, I would like to, you know, to that pipe.</p>



<p class="wp-block-paragraph">I would like to write, you know, this string,</p>



<p class="wp-block-paragraph">you know this string and then i would like to tell you that this is how long that string is</p>



<p class="wp-block-paragraph">and then after you set all that stuff up you just say system call and the system will go ahead and</p>



<p class="wp-block-paragraph">do all of the hard work for you it&#8217;ll go figure out how to actually write data to the file and</p>



<p class="wp-block-paragraph">you know figure out how to do everything that it&#8217;s supposed to do and all you have to do is</p>



<p class="wp-block-paragraph">a little system call here right after this you can see that i have a call crlf that&#8217;s just a function</p>



<p class="wp-block-paragraph">call i talked about functions in other videos but basically you know i&#8217;m just calling a function here</p>



<p class="wp-block-paragraph">you know i&#8217;m just calling a function here and it does the same thing it makes a system call but</p>



<p class="wp-block-paragraph">instead of printing the hello string it&#8217;s printing my crlf string and if you just kind of look at what</p>



<p class="wp-block-paragraph">my crlf is it&#8217;s just these two characters 13 and 10 so like you know slash r slash n for carriage</p>



<p class="wp-block-paragraph">return line feed that&#8217;s why i call it crlf so all this program does just print a message and then a</p>



<p class="wp-block-paragraph">new line and then at the very end it does another system call to properly exit the program if this</p>



<p class="wp-block-paragraph">hybrid program and I had like the entry point of main, you know, that the GCC libraries give you,</p>



<p class="wp-block-paragraph">then I would probably just want to return at the end of this function. But since this is pure</p>



<p class="wp-block-paragraph">assembly, I can actually just exit the program and be fine. So exit with success is a different</p>



<p class="wp-block-paragraph">system call code. Notice how I have the symbol system exit loaded into RAX. And if you look up</p>



<p class="wp-block-paragraph">here, system exit is just the code 60. So if I send a code of one into RAX, that means I want to write</p>



<p class="wp-block-paragraph">a code of 60 somewhere that means i want to exit the program the only argument that takes is rdi</p>



<p class="wp-block-paragraph">which is just what is the exit code that you want to return to the operating system so um</p>



<p class="wp-block-paragraph">if you recall from some of my other videos let&#8217;s see if we do echo hello echo always succeeds at</p>



<p class="wp-block-paragraph">least as far as i know there&#8217;s like it&#8217;s like really hard to make it fail if i echo hello then</p>



<p class="wp-block-paragraph">the program echo launches and it will succeed by just printing the word hello like see how it just</p>



<p class="wp-block-paragraph">like see how it just does that so that means after that command i could echo the special</p>



<p class="wp-block-paragraph">variable dollar sign question mark just to see what the echo command exited with right it should</p>



<p class="wp-block-paragraph">be a zero because it succeeded so you can see now a zero under the hello on the other hand</p>



<p class="wp-block-paragraph">if i concatenate um let&#8217;s say a file that exists i should also get you know an error code that is a</p>



<p class="wp-block-paragraph">So it&#8217;s a zero at the very end.</p>



<p class="wp-block-paragraph">But if I try to concatenate a file that doesn&#8217;t actually exist,</p>



<p class="wp-block-paragraph">I&#8217;ll put a OS release two because that doesn&#8217;t exist.</p>



<p class="wp-block-paragraph">Notice how the cat command fails because it couldn&#8217;t find the file and the exit</p>



<p class="wp-block-paragraph">code is a one.</p>



<p class="wp-block-paragraph">So all you&#8217;re doing when setting RDI here is just controlling what exit code you</p>



<p class="wp-block-paragraph">want your program to exit with.</p>



<p class="wp-block-paragraph">This isn&#8217;t very convenient for a human running a program.</p>



<p class="wp-block-paragraph">Maybe if you want to look at the exit code for some reason, but this is really,</p>



<p class="wp-block-paragraph">really convenient for programs that want to automate other programs.</p>



<p class="wp-block-paragraph">programs just keep in mind if you have a program that is executing another</p>



<p class="wp-block-paragraph">program and it wants to see if that program succeeded the exit code is one</p>



<p class="wp-block-paragraph">of the easiest ways to find out if the program succeeded so this is the whole</p>



<p class="wp-block-paragraph">idea of this entire program we&#8217;re just using two system calls to print</p>



<p class="wp-block-paragraph">something and then to exit the program hopefully this makes sense and now I</p>



<p class="wp-block-paragraph">want to go show you this wonderful wonderful book if I didn&#8217;t already say</p>



<p class="wp-block-paragraph">If I didn&#8217;t already say this before, sorry if I already did.</p>



<p class="wp-block-paragraph">This book is written by a genius.</p>



<p class="wp-block-paragraph">It&#8217;s called this.</p>



<p class="wp-block-paragraph">Here&#8217;s the guy who wrote the book.</p>



<p class="wp-block-paragraph">And here&#8217;s the version that I&#8217;m using, but old versions are good.</p>



<p class="wp-block-paragraph">You can get this book for free on his website if you go find it.</p>



<p class="wp-block-paragraph">So I&#8217;m just going to go to this assembly book, which deals with a lot of Yasm assembly tips</p>



<p class="wp-block-paragraph">and tricks and tutorials and explanations and things.</p>



<p class="wp-block-paragraph">And I&#8217;m going to go to the section labeled after system services.</p>



<p class="wp-block-paragraph">So for me, this is a chapter 13 system services, just to give you a little explanation of what</p>



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



<p class="wp-block-paragraph">You know, this is basically what I said.</p>



<p class="wp-block-paragraph">It gives you, you know, a chance for your application to ask the operating system to</p>



<p class="wp-block-paragraph">do something for you that you don&#8217;t want to program from scratch in assembly or that you</p>



<p class="wp-block-paragraph">can&#8217;t program.</p>



<p class="wp-block-paragraph">But here&#8217;s the real juicy details here.</p>



<p class="wp-block-paragraph">If we go to, yeah, yeah.</p>



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



<p class="wp-block-paragraph">C, which is section 23.0 system services.</p>



<p class="wp-block-paragraph">There&#8217;s a whole bunch of system services in here that you can, uh, that you can read about.</p>



<p class="wp-block-paragraph">So for starters, if we go down a little bit, notice how it says basic system services.</p>



<p class="wp-block-paragraph">If we go down a little bit, you can see right here, the system write service that we&#8217;ve</p>



<p class="wp-block-paragraph">been using in the sample assembly program.</p>



<p class="wp-block-paragraph">You know, when we&#8217;re just printing, we say code one is to write something.</p>



<p class="wp-block-paragraph">And so this is what the table says.</p>



<p class="wp-block-paragraph">you place inside of RAX before syscall.</p>



<p class="wp-block-paragraph">Call code 1 is going to be to write characters and then it tells you what arguments it needs.</p>



<p class="wp-block-paragraph">So like RDI is the file descriptor where do you want to write to.</p>



<p class="wp-block-paragraph">Just like I said before you could give it a file handle that you already received from</p>



<p class="wp-block-paragraph">opening a file or creating a file.</p>



<p class="wp-block-paragraph">Or you could give it 0 or 1 or 2 if you want to try to write to one of the pipes although</p>



<p class="wp-block-paragraph">actually file descriptor 0 is standard input I don&#8217;t think that would actually work.</p>



<p class="wp-block-paragraph">zero to reading if you wanted to read the user&#8217;s input or or you know any input that that program</p>



<p class="wp-block-paragraph">was given through its standard input rsi is just the address of the characters to write like we</p>



<p class="wp-block-paragraph">talked about before and then rdx is the count of the characters to write notice how there are no</p>



<p class="wp-block-paragraph">more arguments and that&#8217;s why i only gave it three arguments besides rex notice how it says here if</p>



<p class="wp-block-paragraph">unsuccessful it returns a negative value if successful it returns the count of characters</p>



<p class="wp-block-paragraph">count of characters actually written this is a really really good idea if you think about it</p>



<p class="wp-block-paragraph">um a lot of new programmers they don&#8217;t really look at return codes when they call system services</p>



<p class="wp-block-paragraph">or built-in c functions or built-in c++ functions they just kind of call it and hope for the best</p>



<p class="wp-block-paragraph">but imagine if you uh if you try to write a huge long string in uh using system write call code one</p>



<p class="wp-block-paragraph">and maybe some of the characters did write but the system decided to only write i don&#8217;t know half for</p>



<p class="wp-block-paragraph">reason maybe it ran out of buffer maybe you know it interrupted you know your right or something</p>



<p class="wp-block-paragraph">like that so it could happen what that would mean is that the return value it would be greater than</p>



<p class="wp-block-paragraph">zero indicating that some characters were written but it&#8217;ll tell you exactly how many characters</p>



<p class="wp-block-paragraph">were written so you&#8217;ll know that you got to keep calling the sys call until all the characters were</p>



<p class="wp-block-paragraph">successfully written why would you do this i don&#8217;t know maybe you have a gigantic string like maybe</p>



<p class="wp-block-paragraph">like a several gigabyte file and you wanted to copy it to another file so you&#8217;ll be calling</p>



<p class="wp-block-paragraph">system write over and over again and system write it&#8217;s only going to write so many bytes at the same</p>



<p class="wp-block-paragraph">time so you use the return value to figure out how far forward in the read buffer you need to advance</p>



<p class="wp-block-paragraph">or you know whatever so that you can well write the entire complete file or the entire complete</p>



<p class="wp-block-paragraph">string or whatever it is without any gaps without it being truncated and so forth and then of course</p>



<p class="wp-block-paragraph">And then of course, if it returns a negative value, then it totally failed.</p>



<p class="wp-block-paragraph">Like you tried to write to a bad file handle, like standard input or a file handle that</p>



<p class="wp-block-paragraph">was closed or a file handle that was open for reading only, something like that.</p>



<p class="wp-block-paragraph">And then you can use some if else, you know, branching logic.</p>



<p class="wp-block-paragraph">I mean, not if else in assembly, it&#8217;s just going to be comparison and conditional jumping</p>



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



<p class="wp-block-paragraph">But you can have more control over your program, right?</p>



<p class="wp-block-paragraph">Because you want your program to be able to respond to errors.</p>



<p class="wp-block-paragraph">Let me just quickly cruise through all of the other options and let you know that</p>



<p class="wp-block-paragraph">and let you know that in another video I&#8217;m gonna do a full tutorial for how to</p>



<p class="wp-block-paragraph">copy one file to another by opening one file for reading and then like using</p>



<p class="wp-block-paragraph">system calls to read from it and then open another file for writing and then</p>



<p class="wp-block-paragraph">you know use system calls to write to it and I&#8217;m gonna do that looping buffer</p>



<p class="wp-block-paragraph">stuff that I talked about before anyway so but for now I&#8217;m just gonna go through</p>



<p class="wp-block-paragraph">the rest of these system call codes let&#8217;s see so first off we got open well</p>



<p class="wp-block-paragraph">Well, if you want to open a file, then you just give it call code 2 and you pass in the</p>



<p class="wp-block-paragraph">address here, you know, RDI, you say, here&#8217;s the address of a null terminated file name.</p>



<p class="wp-block-paragraph">So that means somewhere in memory, you have to have the file name that you want to open</p>



<p class="wp-block-paragraph">with a null terminator, like a zero at the end of the string, either the full path or</p>



<p class="wp-block-paragraph">relative path to wherever the program is currently running.</p>



<p class="wp-block-paragraph">And then RSI is going to be file status flags.</p>



<p class="wp-block-paragraph">And what does that mean?</p>



<p class="wp-block-paragraph">And what does that mean? We can actually just search for that.</p>



<p class="wp-block-paragraph">I&#8217;m going to say control C to copy,</p>



<p class="wp-block-paragraph">and then I&#8217;m going to do control F to search for that.</p>



<p class="wp-block-paragraph">And let&#8217;s see what page of mine right now, 340.</p>



<p class="wp-block-paragraph">If I go down to the search results for that,</p>



<p class="wp-block-paragraph">it just explains what the file modes are. So if you put value zero,</p>



<p class="wp-block-paragraph">that means read only you&#8217;re opening the file and read only mode value one.</p>



<p class="wp-block-paragraph">That means a write only a value to allows reading and writing to the file.</p>



<p class="wp-block-paragraph">So if you don&#8217;t know what that means, well, there it was.</p>



<p class="wp-block-paragraph">What was I just on before?</p>



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



<p class="wp-block-paragraph">Oh, gosh.</p>



<p class="wp-block-paragraph">Completely lost my…</p>



<p class="wp-block-paragraph">Oh, yeah, 40.</p>



<p class="wp-block-paragraph">Okay, so opening pretty easy, right?</p>



<p class="wp-block-paragraph">Like if you didn&#8217;t know this before, you didn&#8217;t necessarily read me.</p>



<p class="wp-block-paragraph">You could just look at this table and go, what do I want to do?</p>



<p class="wp-block-paragraph">I want to open.</p>



<p class="wp-block-paragraph">So here&#8217;s the code.</p>



<p class="wp-block-paragraph">Here&#8217;s the address that it needs.</p>



<p class="wp-block-paragraph">When you&#8217;re done opening a file, when you&#8217;re done working with a file,</p>



<p class="wp-block-paragraph">you want to close that file unless the whole program is going to terminate.</p>



<p class="wp-block-paragraph">unless the whole program is going to terminate at that point you i mean maybe you have a function</p>



<p class="wp-block-paragraph">that gets called somewhat often and you kind of always have to open a file and sort of look at</p>



<p class="wp-block-paragraph">something or write something and maybe you want to close it when the function is over right you</p>



<p class="wp-block-paragraph">don&#8217;t want to have a file handle just open forever that&#8217;s a waste of memory and it might introduce</p>



<p class="wp-block-paragraph">bad behavior to your program if you just have a bunch of file handles floating around that you</p>



<p class="wp-block-paragraph">forgot about so it&#8217;s proper to close a file when you&#8217;re finished with it that&#8217;s just going to be</p>



<p class="wp-block-paragraph">and it only wants one argument.</p>



<p class="wp-block-paragraph">It just wants the file descriptor of the file that you wanted to close.</p>



<p class="wp-block-paragraph">So if you open a file for a reading or to create it,</p>



<p class="wp-block-paragraph">you get a file descriptor back.</p>



<p class="wp-block-paragraph">You should probably hang on to it somewhere on the stack</p>



<p class="wp-block-paragraph">or like in a global variable or like, you know, in a register or whatever.</p>



<p class="wp-block-paragraph">Then when you&#8217;re done doing something to that file</p>



<p class="wp-block-paragraph">and you&#8217;re sure that it&#8217;s time to close it,</p>



<p class="wp-block-paragraph">just give that handle right back when you call code three to close it.</p>



<p class="wp-block-paragraph">Then you can sort of like seek, you know,</p>



<p class="wp-block-paragraph">forward and backwards to the file if you want to.</p>



<p class="wp-block-paragraph">You can fork the current process.</p>



<p class="wp-block-paragraph">You can fork the current process that&#8217;s kind of advanced for this video.</p>



<p class="wp-block-paragraph">You can exit.</p>



<p class="wp-block-paragraph">Remember before we exited from that program with code 60.</p>



<p class="wp-block-paragraph">So that&#8217;s it. Call code 60.</p>



<p class="wp-block-paragraph">If you didn&#8217;t watch this video, you could have gone through this table and just said,</p>



<p class="wp-block-paragraph">Oh, you know, I want to terminate the executing process.</p>



<p class="wp-block-paragraph">I do a system call with code 60 and I&#8217;ll give the exit status to RDI,</p>



<p class="wp-block-paragraph">which is typically zero for success.</p>



<p class="wp-block-paragraph">Bunch of other stuffs create, get the time of day,</p>



<p class="wp-block-paragraph">and then the file modes that we talked about before.</p>



<p class="wp-block-paragraph">we talked about before and then file permissions. Let&#8217;s see, where&#8217;s the permission. I think it&#8217;s</p>



<p class="wp-block-paragraph">when you want to create a file. Yeah, right here. If we wanted to create a file, that&#8217;s code 85.</p>



<p class="wp-block-paragraph">RDI is the name of the file, you know, like a pointer to the string. RSI is the file mode flags.</p>



<p class="wp-block-paragraph">And then you&#8217;re thinking like, what are the file mode flags? Just go down here and then here you</p>



<p class="wp-block-paragraph">go. All the file modes that you could ever want. You just look at it and go, well, I guess I can</p>



<p class="wp-block-paragraph">I guess I can just copy paste this number right here and it&#8217;ll end up being, you know,</p>



<p class="wp-block-paragraph">whatever is described on the left and the right.</p>



<p class="wp-block-paragraph">Like the group has read, write and execute permissions and so forth.</p>



<p class="wp-block-paragraph">I mean, really, if you look at this long enough, you&#8217;ll kind of realize that this is a quad</p>



<p class="wp-block-paragraph">word because it has a Q on the end of it.</p>



<p class="wp-block-paragraph">And the rest is just an octal file permissions notation where this first number here, notice</p>



<p class="wp-block-paragraph">how the first two are zeros, but this first actual number here is something that applies</p>



<p class="wp-block-paragraph">to the user and then the next one applies to the group the next one applies to others who are not</p>



<p class="wp-block-paragraph">part of the user or group and this video is not about file permissions I&#8217;ll probably make one</p>



<p class="wp-block-paragraph">later if I think people will actually watch it but yeah you can just kind of like look it up here if</p>



<p class="wp-block-paragraph">you want to and just copy paste that into a define somewhere let&#8217;s see what else I just want to show</p>



<p class="wp-block-paragraph">you a little code snip just to illustrate a little bit more what I was talking about before</p>



<p class="wp-block-paragraph">for. People usually make this mistake, they&#8217;ll do see this is what people will usually do like new</p>



<p class="wp-block-paragraph">programmers. Suppose this is a function pretend we&#8217;re in C or C++. For the time being, you call</p>



<p class="wp-block-paragraph">some sort of a system function or an API of some other library. And you just you just hope that it</p>



<p class="wp-block-paragraph">works, you just call it and then your program continues underneath, right? It&#8217;s a bad idea,</p>



<p class="wp-block-paragraph">because maybe that failed, maybe there&#8217;s some action you need to take. So instead, it&#8217;s a really</p>



<p class="wp-block-paragraph">really good idea to check the return result or i should say an even better idea is check the</p>



<p class="wp-block-paragraph">documentation usually these types of you know c system calls or function calls or api calls or</p>



<p class="wp-block-paragraph">whatever they&#8217;ll return something indicating success or failure sometimes the documentation</p>



<p class="wp-block-paragraph">will tell you oh you&#8217;ve got to put like a pointer to some other data structure in the arguments</p>



<p class="wp-block-paragraph">and then the result comes there of course just read the documentation but usually it&#8217;s just</p>



<p class="wp-block-paragraph">So you want to grab the result and then check the result.</p>



<p class="wp-block-paragraph">In this case I&#8217;m assuming this is sort of a standard function that will return zero</p>



<p class="wp-block-paragraph">or greater on success like the system write call or the system read call and return less</p>



<p class="wp-block-paragraph">than zero if it fails.</p>



<p class="wp-block-paragraph">So here I&#8217;m just responding, you know, if it&#8217;s like if the result was greater than zero</p>



<p class="wp-block-paragraph">then it&#8217;s like, yay we can proceed.</p>



<p class="wp-block-paragraph">Maybe I&#8217;ll continue with my program in some way and if it&#8217;s not then I&#8217;ll print a complaint</p>



<p class="wp-block-paragraph">then I&#8217;ll print a complaint to the user and then I&#8217;ll take some sort of an action</p>



<p class="wp-block-paragraph">like I&#8217;ll write to a log file, send an email somewhere, you know,</p>



<p class="wp-block-paragraph">do something to try and recover from the error and alert admins to the error.</p>



<p class="wp-block-paragraph">And in this case, I&#8217;m just throwing an exception.</p>



<p class="wp-block-paragraph">This video is not about exceptions, but so I just want you to know,</p>



<p class="wp-block-paragraph">this is a good design pattern to not just discard the result of a call to</p>



<p class="wp-block-paragraph">something. You should check the result and see what you&#8217;re supposed to do.</p>



<p class="wp-block-paragraph">Let&#8217;s see, what else did I want to tip?</p>



<p class="wp-block-paragraph">system services, file handles.</p>



<p class="wp-block-paragraph">Yeah, okay, I think that&#8217;s everything that I wanted to tell you.</p>



<p class="wp-block-paragraph">In future videos, I&#8217;m going to talk about, you know, some of this stuff much more in-depth,</p>



<p class="wp-block-paragraph">like with my file.io video that I&#8217;m going to publish pretty soon, which is, you know,</p>



<p class="wp-block-paragraph">it lets you read and write to files, and it uses a loop to read a little bit at a time.</p>



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



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



<p class="wp-block-paragraph">I&#8217;m outie.</p>



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



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



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



<p class="wp-block-paragraph">help make more videos and grow this community so we&#8217;ll be able to do more</p>



<p class="wp-block-paragraph">videos longer videos better videos or just I&#8217;ll be able to keep making videos</p>



<p class="wp-block-paragraph">in general so please do do me a kindness and and subscribe you know sometimes</p>



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



<p class="wp-block-paragraph">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 so you could do it as a nice favor</p>



<p class="wp-block-paragraph">could you control 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 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</p>



<p class="wp-block-paragraph">code which you can scan in order to go to the website which I think is also</p>



<p class="wp-block-paragraph">named somewhere at the bottom of this video and it&#8217;ll take you to my main</p>



<p class="wp-block-paragraph">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 if</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">send me a comment whatever i also wake up for those in the middle of the night i get i wake</p>



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



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



<p class="wp-block-paragraph">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/mastering-system-services-in-x86-64-assembly-programming/">Mastering System Services in x86-64 Assembly Programming</a> appeared first on <a href="https://www.NeuralLantern.com">NeuralLantern.com</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.NeuralLantern.com/mastering-system-services-in-x86-64-assembly-programming/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>Mastering x86-64 Assembly: A Beginner&#8217;s Guide to CPU Registers</title>
		<link>https://www.NeuralLantern.com/mastering-x86-64-assembly-a-beginners-guide-to-cpu-registers/</link>
					<comments>https://www.NeuralLantern.com/mastering-x86-64-assembly-a-beginners-guide-to-cpu-registers/#respond</comments>
		
		<dc:creator><![CDATA[mike]]></dc:creator>
		<pubDate>Sun, 27 Jul 2025 00:12:09 +0000</pubDate>
				<category><![CDATA[Assembly Language]]></category>
		<category><![CDATA[Coding]]></category>
		<category><![CDATA[Computer Science]]></category>
		<category><![CDATA[Videos]]></category>
		<category><![CDATA[ABI]]></category>
		<category><![CDATA[assembly language]]></category>
		<category><![CDATA[assembly programming]]></category>
		<category><![CDATA[callee saved registers]]></category>
		<category><![CDATA[coding optimization]]></category>
		<category><![CDATA[computer architecture]]></category>
		<category><![CDATA[CPU registers]]></category>
		<category><![CDATA[general purpose registers]]></category>
		<category><![CDATA[low-level programming]]></category>
		<category><![CDATA[programming tutorial]]></category>
		<category><![CDATA[RAX register]]></category>
		<category><![CDATA[RBX register]]></category>
		<category><![CDATA[stack pointer]]></category>
		<category><![CDATA[x86-64 assembly]]></category>
		<category><![CDATA[Yasm assembly]]></category>
		<guid isPermaLink="false">https://www.NeuralLantern.com/?p=190</guid>

					<description><![CDATA[<p>Learn x86-64 assembly with this beginner’s guide to CPU registers. Master RAX, RBX, ABI rules, and optimize code. Perfect for low-level programming enthusiasts.</p>
<p>The post <a href="https://www.NeuralLantern.com/mastering-x86-64-assembly-a-beginners-guide-to-cpu-registers/">Mastering x86-64 Assembly: A Beginner&#8217;s Guide to CPU Registers</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="Mastering x86-64 Assembly: A Beginner&#039;s Guide to CPU Registers" width="1380" height="776" src="https://www.youtube.com/embed/jPysL_y4rRA?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">Ready to dive into x86-64 assembly? This beginner-friendly video breaks down CPU registers like RAX, RBX, and more in Yasm assembly. Learn how to use general-purpose registers, respect the ABI to avoid debugging nightmares, and optimize code by minimizing RAM hits. We’ll cover callee-saved registers, function arguments, and why you shouldn’t mess with the stack pointer. Perfect for coders looking to master low-level programming. Grab the free book mentioned and subscribe for more assembly tips! #AssemblyProgramming #x86</p>



<p class="wp-block-paragraph">Introduction to Registers 00:00:00<br>x86-64 Yasm Assembly 00:00:04<br>Recommended Book 00:00:09<br>Move Instruction Example 00:01:04<br>64-bit Registers Overview 00:01:52<br>General Purpose Registers 00:02:24<br>Two&#8217;s Complement for Integers 00:03:37<br>Floating Point Registers Introduction 00:03:54<br>ABI Importance 00:04:55<br>Function Return Values 00:08:01<br>C++ to Assembly Translation 00:08:43<br>Avoiding System RAM 00:11:45<br>Optimizing with Registers 00:13:04<br>Callee Saved Registers 00:14:16<br>Preserving Registers with Push/Pop 00:16:40<br>Stack and Local Variables 00:22:00<br>Function Arguments in Registers 00:25:12<br>Stack Pointer and Base Pointer 00:31:29<br>Instruction Pointer (RIP) 00:36:30<br>Temporary Registers (R10, R11) 00:36:49<br>Accessing 32-bit Register Portions 00:38:29<br>Floating Point Registers Details 00:42:53<br>Conclusion and Call to Subscribe 00:45: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">Hello there.</p>



<p class="wp-block-paragraph">Let&#8217;s talk about registers in assembly.</p>



<p class="wp-block-paragraph">More specifically, x8664 Yasm assembly.</p>



<p class="wp-block-paragraph">Okay, so first off, check out this book.</p>



<p class="wp-block-paragraph">It&#8217;s a wonderful book.</p>



<p class="wp-block-paragraph">The author is a genius.</p>



<p class="wp-block-paragraph">It&#8217;s called x8664 assembly language programming with Ubuntu.</p>



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



<p class="wp-block-paragraph">website and you know grab your copy it&#8217;s wonderful it&#8217;s released under a copy left license</p>



<p class="wp-block-paragraph">author&#8217;s a genius whenever i&#8217;m trying to remember things about cpu registers for assembly</p>



<p class="wp-block-paragraph">i always go to this section i say callie saved i search for callie saved i already had it there</p>



<p class="wp-block-paragraph">callie saved and it ends up being section 12.8.2 register usage so before we do that let me just</p>



<p class="wp-block-paragraph">that let me just give you a little quick example so you kind of know what I&#8217;m</p>



<p class="wp-block-paragraph">talking about here hopefully everybody&#8217;s seen this instruction by now this is a</p>



<p class="wp-block-paragraph">move instruction in Yasm x86 64 assembly it just moves data from one place to</p>



<p class="wp-block-paragraph">another it takes two operands so you know we have like operand one and</p>



<p class="wp-block-paragraph">operand two basically operand one is the destination operand so we could put</p>



<p class="wp-block-paragraph">destination there if we wanted to operand two is the source we could put</p>



<p class="wp-block-paragraph">that actually won&#8217;t, you know, compile or assemble,</p>



<p class="wp-block-paragraph">but I&#8217;m just saying that&#8217;s what those positions are for.</p>



<p class="wp-block-paragraph">So here&#8217;s an example of a register receiving the value 10.</p>



<p class="wp-block-paragraph">We use the move instruction and we say,</p>



<p class="wp-block-paragraph">we would like to move something into the REX register.</p>



<p class="wp-block-paragraph">We would like to move the value 10 into the REX register.</p>



<p class="wp-block-paragraph">And there you have it. There&#8217;s a move instruction. Okay.</p>



<p class="wp-block-paragraph">So REX is a register.</p>



<p class="wp-block-paragraph">that it is a 64-bit register in our 64-bit CPUs every single register or</p>



<p class="wp-block-paragraph">sorry every single general purpose register and the plot registers they</p>



<p class="wp-block-paragraph">have 64 bits available for us to use 64 bits like you know ones and zeros it&#8217;s</p>



<p class="wp-block-paragraph">like a very very very big number a long time ago we had 32 bit registers and so</p>



<p class="wp-block-paragraph">these are like you know twice as mathematically robust or whatever you</p>



<p class="wp-block-paragraph">allows the registers to address, you know, the 64 bit memory addressing space,</p>



<p class="wp-block-paragraph">which helps us go beyond four gigabytes of RAM in our modern computers,</p>



<p class="wp-block-paragraph">helps us go far beyond what we can even achieve right now.</p>



<p class="wp-block-paragraph">So we have a register called RAX and there&#8217;s a list of other registers on this</p>



<p class="wp-block-paragraph">page here. So basically, you know,</p>



<p class="wp-block-paragraph">we have the RAX register and the RBX register and the RCX register and so</p>



<p class="wp-block-paragraph">forth. I have a hard time remembering all these names,</p>



<p class="wp-block-paragraph">remembering all these names so I go to this book when I forget but otherwise</p>



<p class="wp-block-paragraph">you know you can kind of think of like ABCD and then just surround them with</p>



<p class="wp-block-paragraph">RNX you know our kind of for register and X for extra big maybe so we&#8217;ve got</p>



<p class="wp-block-paragraph">the RAX RBX RCX and RDX registers and then we have a bunch of other ones</p>



<p class="wp-block-paragraph">through R15 but first before we do that let me just emphasize that these are</p>



<p class="wp-block-paragraph">general purpose registers to be used for integers or data you can put</p>



<p class="wp-block-paragraph">characters in there for strings you can put just like you know whatever data you</p>



<p class="wp-block-paragraph">want inside the machine we use twos complement to represent integers so it&#8217;s</p>



<p class="wp-block-paragraph">it&#8217;s fine the instructions that you will do for addition and other mathematical</p>



<p class="wp-block-paragraph">but don&#8217;t put floating point numbers in these registers because the computer the</p>



<p class="wp-block-paragraph">system the machine won&#8217;t know how to operate on that data the thing is</p>



<p class="wp-block-paragraph">integers are stored as twos complement I&#8217;m going to talk about that more in</p>



<p class="wp-block-paragraph">another video and floats are stored in something called I triple E seven five</p>



<p class="wp-block-paragraph">four standard which is just like a crazy different way of storing floating point</p>



<p class="wp-block-paragraph">numbers that makes more sense for floating point numbers but because the</p>



<p class="wp-block-paragraph">differently in the machine you can&#8217;t really use normal instructions like you</p>



<p class="wp-block-paragraph">can&#8217;t add two floats together the same with the same instruction that you would</p>



<p class="wp-block-paragraph">add two integers together you would have to use special floating point registers</p>



<p class="wp-block-paragraph">and special floating point instructions I&#8217;ll talk about floats at the end of</p>



<p class="wp-block-paragraph">this video I hope but basically just keep in mind these are general purpose</p>



<p class="wp-block-paragraph">registers to be used for integers in data only okay so what am I talking</p>



<p class="wp-block-paragraph">Okay, so what am I talking about?</p>



<p class="wp-block-paragraph">What are we seeing here on the side here?</p>



<p class="wp-block-paragraph">Why does it say return value?</p>



<p class="wp-block-paragraph">What is going on with Kali saved and fourth argument and third argument and so forth?</p>



<p class="wp-block-paragraph">So there&#8217;s a standard that you&#8217;re supposed to use when you program.</p>



<p class="wp-block-paragraph">You can get yourself into a lot of hot water or you can dramatically increase your debugging</p>



<p class="wp-block-paragraph">time if you don&#8217;t do this.</p>



<p class="wp-block-paragraph">standard called the ABI which I think is short for application binary interface but basically I just</p>



<p class="wp-block-paragraph">like to say the ABI respect the ABI there&#8217;s a plan that people came up with that says this is how you</p>



<p class="wp-block-paragraph">should use these registers don&#8217;t use them in a different way and that way if everybody is writing</p>



<p class="wp-block-paragraph">you know modules and they&#8217;re all going to interact together like I write a module you write a module</p>



<p class="wp-block-paragraph">the abi then it&#8217;s it&#8217;s pretty much guaranteed that we can we can expect certain things like</p>



<p class="wp-block-paragraph">our registers won&#8217;t become corrupted after a function call or that we&#8217;re not going to corrupt</p>



<p class="wp-block-paragraph">someone else&#8217;s uh function or you know whatever right so you can also use this to benefit yourself</p>



<p class="wp-block-paragraph">even if you&#8217;re the only person who is writing your code and you&#8217;re not calling on anyone else&#8217;s</p>



<p class="wp-block-paragraph">function um then you know like how are you going to remember like what was i using this register</p>



<p class="wp-block-paragraph">Was I supposed to save that one?</p>



<p class="wp-block-paragraph">Was I supposed to preserve it?</p>



<p class="wp-block-paragraph">How was I going to return data from that function?</p>



<p class="wp-block-paragraph">Was I going to use this register or the other register?</p>



<p class="wp-block-paragraph">It&#8217;s hard to remember what you,</p>



<p class="wp-block-paragraph">what you yourself even did like a month ago.</p>



<p class="wp-block-paragraph">So if you respect this plan,</p>



<p class="wp-block-paragraph">then your functions will work not only</p>



<p class="wp-block-paragraph">with other people&#8217;s functions,</p>



<p class="wp-block-paragraph">but with your own functions from a month ago.</p>



<p class="wp-block-paragraph">The other thing too, is we have system calls, right?</p>



<p class="wp-block-paragraph">So you have like a system call.</p>



<p class="wp-block-paragraph">Let&#8217;s say we want to move something into REX.</p>



<p class="wp-block-paragraph">something into REX, maybe I want to exit the system or something.</p>



<p class="wp-block-paragraph">Just forget about this.</p>



<p class="wp-block-paragraph">This is not a system call video.</p>



<p class="wp-block-paragraph">Suppose I want to do a system call, right?</p>



<p class="wp-block-paragraph">The system call itself is also going to respect the ABI.</p>



<p class="wp-block-paragraph">So if you start calling other</p>



<p class="wp-block-paragraph">services in the system call instruction, like if you want to open a file,</p>



<p class="wp-block-paragraph">close a file, read a file, write a file, whatever.</p>



<p class="wp-block-paragraph">If you don&#8217;t respect the ABI and the system call is respecting the ABI,</p>



<p class="wp-block-paragraph">might have some of your data corrupted so it&#8217;s a really really good idea to respect the abi</p>



<p class="wp-block-paragraph">in fact think about it this way what if uh what if you wrote a bunch of functions for a long long</p>



<p class="wp-block-paragraph">time and then like a year later you couldn&#8217;t you couldn&#8217;t remember what you were doing you know</p>



<p class="wp-block-paragraph">last year oh what was the return register uh and it costs you a bunch of extra time debugging</p>



<p class="wp-block-paragraph">eventually you&#8217;ll probably decide to write your write your own standard okay from now on i&#8217;m always</p>



<p class="wp-block-paragraph">going to use this register for return values and i&#8217;m going to use this register for the first</p>



<p class="wp-block-paragraph">and i&#8217;m going to use this register for the first argument and i&#8217;m going to write you probably</p>



<p class="wp-block-paragraph">come up with a plan right that&#8217;s basically the abi the abi covers more than just how to use</p>



<p class="wp-block-paragraph">registers but um register usage is covered in the abi so why would you put yourself through</p>



<p class="wp-block-paragraph">a bunch of heartache and then eventually come up with your own plan when you could have just</p>



<p class="wp-block-paragraph">been following the correct plan in the first place right so everybody should respect the abi</p>



<p class="wp-block-paragraph">I wanted to talk next about the return value.</p>



<p class="wp-block-paragraph">Okay, so let me write up a little function here.</p>



<p class="wp-block-paragraph">Let me just say that we have a C++ function.</p>



<p class="wp-block-paragraph">And we&#8217;ll just say it&#8217;s like void f.</p>



<p class="wp-block-paragraph">And we&#8217;ll say it calls on g.</p>



<p class="wp-block-paragraph">And then we&#8217;ll have another function called g.</p>



<p class="wp-block-paragraph">And actually, maybe it&#8217;s not void.</p>



<p class="wp-block-paragraph">Maybe it actually does return a long.</p>



<p class="wp-block-paragraph">returns the number five or something. Okay.</p>



<p class="wp-block-paragraph">Hopefully you&#8217;ve seen some kind of a higher level language before like C++</p>



<p class="wp-block-paragraph">so that you can understand what&#8217;s going on here.</p>



<p class="wp-block-paragraph">We&#8217;re just making two functions and one is calling the other.</p>



<p class="wp-block-paragraph">And the second one is just returning a value to the first one.</p>



<p class="wp-block-paragraph">So in assembly, the equivalent of this would be,</p>



<p class="wp-block-paragraph">let&#8217;s make a label with the name of the function and then a little colon,</p>



<p class="wp-block-paragraph">and then just put a return instruction at the end of it. Wham,</p>



<p class="wp-block-paragraph">it, wham, you&#8217;ve got a function. It&#8217;s not going to work very well, but you do have a function at</p>



<p class="wp-block-paragraph">this point. So F, uh, I&#8217;m going to try to copy the C++, uh, function. So it&#8217;s a really good idea.</p>



<p class="wp-block-paragraph">Whenever you&#8217;re writing a function in assembly, try to imagine what the prototype would be for</p>



<p class="wp-block-paragraph">C++ and just put it as a comment at the top. So I know how the F function is going to behave in</p>



<p class="wp-block-paragraph">my assembly module because I put a comment up there just kind of reminding me what the prototype</p>



<p class="wp-block-paragraph">for C++. It doesn&#8217;t really do anything. It just sort of runs. So that&#8217;s okay. And then I&#8217;ll make</p>



<p class="wp-block-paragraph">another function down here. I&#8217;ll do the long G again in a comment, and then I&#8217;ll do the actual</p>



<p class="wp-block-paragraph">assembly version. I&#8217;ll say G colon, and then I&#8217;ll return. So just by putting that comment up,</p>



<p class="wp-block-paragraph">I kind of can remind myself now that the G function is supposed to return some kind of a value.</p>



<p class="wp-block-paragraph">up here, you know, do how about let&#8217;s say, I don&#8217;t want to start adding a bunch of local</p>



<p class="wp-block-paragraph">variables right now.</p>



<p class="wp-block-paragraph">Pretend that we&#8217;re going to print the return value of the call to G somehow we&#8217;re going</p>



<p class="wp-block-paragraph">to use C out or whatever.</p>



<p class="wp-block-paragraph">We&#8217;re going to send it to a variable, whatever.</p>



<p class="wp-block-paragraph">I&#8217;m just going to try to keep the assembly as simple as possible.</p>



<p class="wp-block-paragraph">But anyway, the point is G returns something.</p>



<p class="wp-block-paragraph">you know, a value in your assembly function.</p>



<p class="wp-block-paragraph">Well, remember that higher level languages,</p>



<p class="wp-block-paragraph">part of why they&#8217;re so awesome</p>



<p class="wp-block-paragraph">is they do a lot of extra work for us under the hood</p>



<p class="wp-block-paragraph">and they actually provide illusions to us</p>



<p class="wp-block-paragraph">that make programming easier.</p>



<p class="wp-block-paragraph">For example, there are no functions</p>



<p class="wp-block-paragraph">going on inside the actual machine.</p>



<p class="wp-block-paragraph">I mean, I guess from a certain point of view there are,</p>



<p class="wp-block-paragraph">but basically the machine is just sort of like moving data</p>



<p class="wp-block-paragraph">and jumping to instructions</p>



<p class="wp-block-paragraph">and jumping back from somewhere.</p>



<p class="wp-block-paragraph">And, you know, it&#8217;s just sort of like jumping around</p>



<p class="wp-block-paragraph">and executing instructions.</p>



<p class="wp-block-paragraph">and executing instructions there&#8217;s no actual function it&#8217;s so it&#8217;s like if you want to pretend</p>



<p class="wp-block-paragraph">that you have a function in assembly well there is a return instruction that will help you jump</p>



<p class="wp-block-paragraph">back to wherever you came from most recently but we have to we have to implement more is what i&#8217;m</p>



<p class="wp-block-paragraph">saying so how do we return a value in an assembly function we just have to load up the return value</p>



<p class="wp-block-paragraph">it as return value that just means we have to load rax up with something so for example we wanted to</p>



<p class="wp-block-paragraph">return the number five so i&#8217;m just going to load it up with the number five and then return</p>



<p class="wp-block-paragraph">now we&#8217;ve pretty much translated a c plus plus function into uh an assembly function i mean</p>



<p class="wp-block-paragraph">that&#8217;s really what&#8217;s happening under the hood when you compile your c plus plus program</p>



<p class="wp-block-paragraph">it&#8217;s um it&#8217;s just you know translating all the c plus plus into assembly</p>



<p class="wp-block-paragraph">Another thing that I should probably point out is that you should use registers as often as possible</p>



<p class="wp-block-paragraph">and you should try your best not to touch memory.</p>



<p class="wp-block-paragraph">Of course at some point you have to touch memory when you want to save your final result or send</p>



<p class="wp-block-paragraph">it off somewhere or whatever but you know imagine that you have an assembly function and you&#8217;re</p>



<p class="wp-block-paragraph">doing lots and lots of calculations you&#8217;re performing an algorithm or something your</p>



<p class="wp-block-paragraph">program will be so much more efficient by like a factor of a hundred or probably more if you don&#8217;t</p>



<p class="wp-block-paragraph">if you don&#8217;t hit system ram because every time you hit system ram like a global variable or the</p>



<p class="wp-block-paragraph">stack or something then um your cpu you know typically in the uncashed uh scenario your cpu</p>



<p class="wp-block-paragraph">has to go talk to the system bus on your motherboard and then you know send a message</p>



<p class="wp-block-paragraph">to system ram and then wait for the system ram to figure out what it&#8217;s doing and then get a response</p>



<p class="wp-block-paragraph">back so when your program is executing on the cpu you can encounter a stall which is like your</p>



<p class="wp-block-paragraph">like a hundred clock cycles or more like it&#8217;s a long time at least in the most basic case</p>



<p class="wp-block-paragraph">and how do you avoid that just use registers only when you&#8217;re doing lots of calculations the</p>



<p class="wp-block-paragraph">registers are built into the cpu they&#8217;re part of the cpu&#8217;s hardware so they are lightning fast</p>



<p class="wp-block-paragraph">compared to system ram or your disk or whatever</p>



<p class="wp-block-paragraph">i probably say this a lot but you know part of the reason you would even want to code an assembly</p>



<p class="wp-block-paragraph">Maybe you have a big program that does a lot of stuff and eventually you profile the program</p>



<p class="wp-block-paragraph">and you realize, Hey, this one part of the program is really slow because it gets called</p>



<p class="wp-block-paragraph">constantly and it&#8217;s not super efficient.</p>



<p class="wp-block-paragraph">That might be a good use case for writing a hybrid program where you have multiple modules.</p>



<p class="wp-block-paragraph">Some of your modules are in a higher level language and some of your modules are in assembly.</p>



<p class="wp-block-paragraph">So you take your most important function that slows down your program that gets called all</p>



<p class="wp-block-paragraph">like a big loop or something and you rewrite it to assembly so that you can</p>



<p class="wp-block-paragraph">have more control over how often you touch system RAM to try to make the</p>



<p class="wp-block-paragraph">whole thing more efficient and you know reduce the number of instructions and</p>



<p class="wp-block-paragraph">just like whatever and you can improve the product of the efficiency of your</p>



<p class="wp-block-paragraph">program so anyway you want to try to avoid hitting system RAM your registers</p>



<p class="wp-block-paragraph">are built in the CPU there&#8217;s 64 bits that gives us 64 bits of address space</p>



<p class="wp-block-paragraph">can reach you know back in the day we had 32 bit systems that means you could only have a memory</p>



<p class="wp-block-paragraph">stick that was about four gigabytes so now we can go much further so um yeah that&#8217;s what&#8217;s going on</p>



<p class="wp-block-paragraph">with our registers on the cpu back to the return value okay so we have return value here goes into</p>



<p class="wp-block-paragraph">the rax register and then the next thing here is it&#8217;s saying that the rbx register is something</p>



<p class="wp-block-paragraph">remember we said that uh we have to respect the abi right like you have to respect this convention</p>



<p class="wp-block-paragraph">uh because if you don&#8217;t you&#8217;re going to cost yourself debug time and your</p>



<p class="wp-block-paragraph">functions probably won&#8217;t be interoperable interoperable with other people&#8217;s functions</p>



<p class="wp-block-paragraph">or the system call uh instruction well so one of the things about the abi is some of the registers</p>



<p class="wp-block-paragraph">are designated as the function that is being called has to be the thing that saves the register&#8217;s</p>



<p class="wp-block-paragraph">registers value so that it doesn&#8217;t corrupt for the caller. Imagine this if I had a function</p>



<p class="wp-block-paragraph">called f I&#8217;m going to do like f and g here suppose the function f is going to move something into</p>



<p class="wp-block-paragraph">let&#8217;s say r12 or actually let&#8217;s do rbx move the value 10 into rbx and then it&#8217;ll call g for some</p>



<p class="wp-block-paragraph">reason maybe g doesn&#8217;t take any arguments maybe g is just kind of doing stuff let&#8217;s just say that</p>



<p class="wp-block-paragraph">g just moves a different value into rbx so the thing is when the call comes back like after we</p>



<p class="wp-block-paragraph">get to this next line let&#8217;s say we&#8217;re going to do something with do something do something with</p>



<p class="wp-block-paragraph">rbx by the time we get to this line dude can i get the line numbers on this oh yeah i forgot okay</p>



<p class="wp-block-paragraph">so by the time we get to line seven uh the register rbx is ruined these registers are not</p>



<p class="wp-block-paragraph">local variables they&#8217;re not like uh you know tied to any scope or function call these registers are</p>



<p class="wp-block-paragraph">just basically global variables that are sitting on the cpu for lightning fast performance but that</p>



<p class="wp-block-paragraph">means the rbx i use inside of f is the same rbx i use inside of g so that means if g messes up the</p>



<p class="wp-block-paragraph">value of rbx and then if f calls g then f now has a bad value for rbx this is a broken program</p>



<p class="wp-block-paragraph">broken program because we did not respect the ABI.</p>



<p class="wp-block-paragraph">The ABI says that RBX is callee saved,</p>



<p class="wp-block-paragraph">which means if I use RBX in the G,</p>



<p class="wp-block-paragraph">then I have to preserve the value so that by the time I return,</p>



<p class="wp-block-paragraph">the value is the same as it was when the call first came in.</p>



<p class="wp-block-paragraph">So how do we do that?</p>



<p class="wp-block-paragraph">We&#8217;ll do that with a push pop pair.</p>



<p class="wp-block-paragraph">I&#8217;ll probably make more videos in the future about pushing and popping,</p>



<p class="wp-block-paragraph">but I&#8217;m going to try to keep it simple for now.</p>



<p class="wp-block-paragraph">We just say, let&#8217;s push RBX onto the stack.</p>



<p class="wp-block-paragraph">at the beginning and then we&#8217;ll pop it off the stack at the very end.</p>



<p class="wp-block-paragraph">This basically means we&#8217;re going to take the value of RBX.</p>



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



<p class="wp-block-paragraph">So, so yeah, we are hitting system RAM.</p>



<p class="wp-block-paragraph">It&#8217;s a little slower at that point.</p>



<p class="wp-block-paragraph">The ABI will save us a little time for the other registers.</p>



<p class="wp-block-paragraph">I&#8217;ll try to explain that in a second, but basically we&#8217;re preserving the value here</p>



<p class="wp-block-paragraph">with push and then we&#8217;re popping it back off.</p>



<p class="wp-block-paragraph">So we&#8217;re restoring it.</p>



<p class="wp-block-paragraph">So that means even though we ruined the value at line 14, when F does its line</p>



<p class="wp-block-paragraph">When F does its line 7, it&#8217;s going to have the correct value of RBX.</p>



<p class="wp-block-paragraph">It&#8217;s going to have the value 10.</p>



<p class="wp-block-paragraph">The book and I like to call this the prologue, just meaning this is a little section of code</p>



<p class="wp-block-paragraph">where we&#8217;re going to set things up.</p>



<p class="wp-block-paragraph">We&#8217;re going to start getting ready to do more instructions.</p>



<p class="wp-block-paragraph">And then this at the bottom is going to be called the epilogue.</p>



<p class="wp-block-paragraph">This is just like the finale.</p>



<p class="wp-block-paragraph">We&#8217;re like, we&#8217;re kind of cleaning up.</p>



<p class="wp-block-paragraph">We&#8217;re finishing up right before we return.</p>



<p class="wp-block-paragraph">If you wanted to return a value from G, then you could put, you know, the move inside of</p>



<p class="wp-block-paragraph">RAX somewhere else.</p>



<p class="wp-block-paragraph">Like you could put it like below the epilogue or just above the epilogue.</p>



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



<p class="wp-block-paragraph">As long as you&#8217;re sure that RAX is not getting trampled upon.</p>



<p class="wp-block-paragraph">So it&#8217;s important to note also that if I do a, let&#8217;s say a system call right after that,</p>



<p class="wp-block-paragraph">let&#8217;s say I call G and then I do a system call with, you know, like, you know, some</p>



<p class="wp-block-paragraph">you know some other number as the call code i want to open a file i want to close a file</p>



<p class="wp-block-paragraph">i want to read or write a file whatever i want to ask the system to do something for me the</p>



<p class="wp-block-paragraph">system is also going to respect the abi which means i&#8217;m guaranteed that rbx is not modified</p>



<p class="wp-block-paragraph">when the system call comes back if if the system call you know if the syscall instruction didn&#8217;t</p>



<p class="wp-block-paragraph">respect the abi then my rbx could be ruined i&#8217;d have to do a bunch of stuff to preserve it so this</p>



<p class="wp-block-paragraph">some of these registers let me go down a little bit here like this R10 the</p>



<p class="wp-block-paragraph">temporary register and also the RDI if you decided to use that in the body of</p>



<p class="wp-block-paragraph">your program which you&#8217;re allowed to these are not designated as callee saved</p>



<p class="wp-block-paragraph">which means system call could actually ruin the value of those registers so</p>



<p class="wp-block-paragraph">suppose for the sake of argument I really decided that I needed to use I</p>



<p class="wp-block-paragraph">10 because that&#8217;s marked as a temporary I&#8217;ll put a value in our 10 and then I&#8217;ll say you know</p>



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



<p class="wp-block-paragraph">With our 10 I&#8217;m sorry our 10</p>



<p class="wp-block-paragraph">Then that means by the time I&#8217;m done with my call to G</p>



<p class="wp-block-paragraph">I should assume that our 10 is ruined because even though you&#8217;re you can see right here that that G doesn&#8217;t actually do anything</p>



<p class="wp-block-paragraph">To our 10 we should assume that we have to respect the ABI which means the other function could have ruined it</p>



<p class="wp-block-paragraph">Ruined it. This is especially true if you call someone else&#8217;s module or system call or whatever same thing for the system call</p>



<p class="wp-block-paragraph">so if I call system call</p>



<p class="wp-block-paragraph">and I use</p>



<p class="wp-block-paragraph">R10 at some point before and then after then I have to assume R10 is destroyed by the time I get back in system call</p>



<p class="wp-block-paragraph">So that&#8217;s no good</p>



<p class="wp-block-paragraph">The cure to that when we&#8217;re talking about a temporary or something that is not designated as callee saved is that the caller has to save</p>



<p class="wp-block-paragraph">hit a system RAM just to make a call.</p>



<p class="wp-block-paragraph">Anything that we think we need when we&#8217;re finished.</p>



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



<p class="wp-block-paragraph">This is not the only way to do it, but I&#8217;m going to do a push R10 here.</p>



<p class="wp-block-paragraph">And then afterwards, I&#8217;m going to do pop R10.</p>



<p class="wp-block-paragraph">So that sucks.</p>



<p class="wp-block-paragraph">And then I have to do the same thing for the system call.</p>



<p class="wp-block-paragraph">I can go push R10 and then pop R10.</p>



<p class="wp-block-paragraph">And of course, if you were clever, you probably would, you know, put the call to G inside</p>



<p class="wp-block-paragraph">of that system call push pop pair.</p>



<p class="wp-block-paragraph">So you could have one less push pop pair.</p>



<p class="wp-block-paragraph">push pop pair but I&#8217;m just saying there&#8217;s no guarantee r10 is going to survive so you always</p>



<p class="wp-block-paragraph">have to preserve it if you ever want to use it again so this is like another way that the abi</p>



<p class="wp-block-paragraph">kind of can help you save time notice how here in the uh in the g function which I probably should</p>



<p class="wp-block-paragraph">have prototyped let me just say it looks like to me it&#8217;s a void and it doesn&#8217;t take any arguments</p>



<p class="wp-block-paragraph">to prototype your functions in comments.</p>



<p class="wp-block-paragraph">Maybe let me do the same thing to F up here.</p>



<p class="wp-block-paragraph">So F looks like it&#8217;s not returning anything and it&#8217;s not taking anything.</p>



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



<p class="wp-block-paragraph">Those are C++ comments that would not compile.</p>



<p class="wp-block-paragraph">All right. So notice how I&#8217;m using RBX.</p>



<p class="wp-block-paragraph">So I decided to preserve RBX, but there&#8217;s also other registers that are marked as</p>



<p class="wp-block-paragraph">marked as Kali saved like R12 through 15 and RBP and whatever,</p>



<p class="wp-block-paragraph">but I&#8217;m not preserving those.</p>



<p class="wp-block-paragraph">The reason I don&#8217;t have to preserve those is because I&#8217;m not using them.</p>



<p class="wp-block-paragraph">So the, uh, the, uh, the ABI can save you a little time here.</p>



<p class="wp-block-paragraph">Like for example, I&#8217;m preserving R10 because I&#8217;m supposed to assume that I,</p>



<p class="wp-block-paragraph">that I, that they could be destroyed by the time the function comes back.</p>



<p class="wp-block-paragraph">What if instead of using R10, I used R12.</p>



<p class="wp-block-paragraph">have to surround any calls with a push pop pair because I can trust that whoever is going to</p>



<p class="wp-block-paragraph">modify R12 will preserve it for me. But then notice down here G is not actually using R12.</p>



<p class="wp-block-paragraph">So G doesn&#8217;t even do push pop on R12. So we save hits to memory. We don&#8217;t even have to touch memory</p>



<p class="wp-block-paragraph">to preserve R12 because the ABI is helping us sort of stay a little bit more efficient.</p>



<p class="wp-block-paragraph">So this is great, right? F is a broken function at this point though, because I&#8217;m using</p>



<p class="wp-block-paragraph">at this point though because I&#8217;m using two registers that are callee saved I</p>



<p class="wp-block-paragraph">can&#8217;t assume that whoever called F is gonna know that I&#8217;m modifying RBX and</p>



<p class="wp-block-paragraph">R12 so I should do a prolog and an epilog up here I&#8217;m gonna say push RBX</p>



<p class="wp-block-paragraph">and then I&#8217;m gonna push R12 and then down here at the bottom I&#8217;m gonna do an</p>



<p class="wp-block-paragraph">epilog epilog and I&#8217;m gonna pop RBX and I&#8217;m gonna pop R12</p>



<p class="wp-block-paragraph">but I&#8217;ve done something wrong.</p>



<p class="wp-block-paragraph">The thing is, I&#8217;m not going to talk about the stack too much in this video,</p>



<p class="wp-block-paragraph">but the stack is a particular type of data structure</p>



<p class="wp-block-paragraph">that will return to you data in a reverse order than you sent into it.</p>



<p class="wp-block-paragraph">This is great for helping us keep track of function calls and return addresses,</p>



<p class="wp-block-paragraph">as I&#8217;ll probably try to explain in this video.</p>



<p class="wp-block-paragraph">But basically, the data comes out backwards.</p>



<p class="wp-block-paragraph">The data comes out backwards. So if I do it like this</p>



<p class="wp-block-paragraph">Then actually by the time the return statement instruction gets executed</p>



<p class="wp-block-paragraph">RBX will have the value that was intended for R12 and R12 will have the value that was intended for RBX</p>



<p class="wp-block-paragraph">It&#8217;ll be backwards. So you have to do your push pops in the reverse order to notice how</p>



<p class="wp-block-paragraph">It&#8217;s kind of like a shell that goes outwards. It&#8217;s like R12 is first on the inside and then RBX is next on the outside</p>



<p class="wp-block-paragraph">That&#8217;s just what you have to do to preserve everything.</p>



<p class="wp-block-paragraph">So the F function works now.</p>



<p class="wp-block-paragraph">The G function, I think it works now.</p>



<p class="wp-block-paragraph">We have a prologue and epilogue.</p>



<p class="wp-block-paragraph">Okay, so we talked about the ABI.</p>



<p class="wp-block-paragraph">We talked about calls.</p>



<p class="wp-block-paragraph">We talked about the registers.</p>



<p class="wp-block-paragraph">Talked about callee saved.</p>



<p class="wp-block-paragraph">We talked about the fact that we don&#8217;t have to save the other ones.</p>



<p class="wp-block-paragraph">I think I can talk about…</p>



<p class="wp-block-paragraph">um so uh one thing that i should make sure to mention is that these are general purpose registers</p>



<p class="wp-block-paragraph">they&#8217;re to be used for integers integer data or like characters or just like regular data</p>



<p class="wp-block-paragraph">they use certain instructions that are not to be used with floating point data so you actually</p>



<p class="wp-block-paragraph">should not store floating point numbers inside of your general purpose registers you should only</p>



<p class="wp-block-paragraph">store floating points in special registers which i&#8217;ll talk about at the end of this video</p>



<p class="wp-block-paragraph">So keep that in mind.</p>



<p class="wp-block-paragraph">Floating point numbers are stored differently in the system.</p>



<p class="wp-block-paragraph">They&#8217;re stored with a scheme called IEEE 754 floating point.</p>



<p class="wp-block-paragraph">So like the numbers wouldn&#8217;t make sense.</p>



<p class="wp-block-paragraph">Integers and floats are stored differently.</p>



<p class="wp-block-paragraph">So the hardware is wired differently to operate on two different types of data.</p>



<p class="wp-block-paragraph">So anyway, I just want to say that this is only for integers and general purpose data.</p>



<p class="wp-block-paragraph">So now let&#8217;s look at the other types of registers.</p>



<p class="wp-block-paragraph">rc x and rd x and rsi and rdi are designated as arguments first second third and fourth</p>



<p class="wp-block-paragraph">okay so let me uh let me write like a quick c plus function here and this is going to be uh let&#8217;s</p>



<p class="wp-block-paragraph">say we have like a function that returns some value we&#8217;ll call it uh we&#8217;ll call it f and we&#8217;ll</p>



<p class="wp-block-paragraph">say that f takes in some arguments let&#8217;s say that it takes in three arguments um again uh</p>



<p class="wp-block-paragraph">integers right now because we&#8217;re only using integer registers.</p>



<p class="wp-block-paragraph">We can&#8217;t use floating point arguments or return values.</p>



<p class="wp-block-paragraph">Just keep that in mind.</p>



<p class="wp-block-paragraph">So I&#8217;m just going to do like three arguments long A and B and C.</p>



<p class="wp-block-paragraph">OK, so we can take in three arguments and maybe we&#8217;re just going to return,</p>



<p class="wp-block-paragraph">you know, a plus B plus C.</p>



<p class="wp-block-paragraph">OK, simple C++ function.</p>



<p class="wp-block-paragraph">Let&#8217;s do this in assembly.</p>



<p class="wp-block-paragraph">We&#8217;ll make a label and we&#8217;ll do a little comment that just reminds us.</p>



<p class="wp-block-paragraph">of sorry of the</p>



<p class="wp-block-paragraph">Function that we&#8217;re trying to implement we stick a little return instruction at the end of it so that</p>



<p class="wp-block-paragraph">it will jump back to the caller and</p>



<p class="wp-block-paragraph">then we&#8217;ll say</p>



<p class="wp-block-paragraph">Well when the caller of this function</p>



<p class="wp-block-paragraph">Called us and gave us a and B and C. How do we get a and B and C?</p>



<p class="wp-block-paragraph">Well a is the first integer argument, so it&#8217;s just going to be the RDI register</p>



<p class="wp-block-paragraph">So we&#8217;ll literally just use a move instruction</p>



<p class="wp-block-paragraph">just use a move instruction.</p>



<p class="wp-block-paragraph">Let&#8217;s say we want to, for whatever reason, use the R12 register</p>



<p class="wp-block-paragraph">and we&#8217;ll do something with it later.</p>



<p class="wp-block-paragraph">We&#8217;ll print it, we&#8217;ll save it, we&#8217;ll do whatever.</p>



<p class="wp-block-paragraph">Let&#8217;s just grab the incoming argument.</p>



<p class="wp-block-paragraph">So we&#8217;ll just grab RDI.</p>



<p class="wp-block-paragraph">So I&#8217;m going to put a little comment up above here, you know, grab</p>



<p class="wp-block-paragraph">a and do something</p>



<p class="wp-block-paragraph">with it and I&#8217;ll just write down here more instructions just to just to denote</p>



<p class="wp-block-paragraph">Just to denote that we&#8217;re doing something with the a value, but I&#8217;m not going to write it down here because I don&#8217;t want this to get huge.</p>



<p class="wp-block-paragraph">So we&#8217;ll do the same thing with the b.</p>



<p class="wp-block-paragraph">Maybe I should remember that programming is case sensitive.</p>



<p class="wp-block-paragraph">We&#8217;ll do something with the b.</p>



<p class="wp-block-paragraph">It&#8217;s not RDI.</p>



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



<p class="wp-block-paragraph">So that&#8217;s actually RSI.</p>



<p class="wp-block-paragraph">And then we&#8217;ll do something else.</p>



<p class="wp-block-paragraph">We could have used R13 or other registers if we wanted to.</p>



<p class="wp-block-paragraph">I&#8217;m just showing you how to grab incoming arguments.</p>



<p class="wp-block-paragraph">So then we&#8217;ll grab the c and do something with it also.</p>



<p class="wp-block-paragraph">C and do something with it also it&#8217;s not going to be RDI it&#8217;s going to be RDX and you can do this</p>



<p class="wp-block-paragraph">up to like I think six arguments let me just double check here yeah so like R9 ends up being</p>



<p class="wp-block-paragraph">the sixth argument and if you want more than that then the caller will have to put stuff on the stack</p>



<p class="wp-block-paragraph">and in fact if you if you kind of like understand what&#8217;s happening here let me just do like a main</p>



<p class="wp-block-paragraph">Let&#8217;s say we do cout whatever f returns.</p>



<p class="wp-block-paragraph">You know, this is like a typical function call, right?</p>



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



<p class="wp-block-paragraph">f will jump in there.</p>



<p class="wp-block-paragraph">It&#8217;ll jump into its body.</p>



<p class="wp-block-paragraph">It&#8217;ll do some sort of a manipulation with the incoming arguments.</p>



<p class="wp-block-paragraph">It&#8217;ll return a final value.</p>



<p class="wp-block-paragraph">Then that final value gets printed out.</p>



<p class="wp-block-paragraph">So what&#8217;s 5 plus 6 plus 7?</p>



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



<p class="wp-block-paragraph">What is that, like 18?</p>



<p class="wp-block-paragraph">I&#8217;m bad at math.</p>



<p class="wp-block-paragraph">So let&#8217;s just say that&#8217;s 18 gets printed.</p>



<p class="wp-block-paragraph">So what happens is, um, we return, Oh, I forgot to return.</p>



<p class="wp-block-paragraph">I&#8217;m going to say compute the, uh,</p>



<p class="wp-block-paragraph">compute the sum of the things.</p>



<p class="wp-block-paragraph">Uh, and I&#8217;ll just say like block cause I don&#8217;t want to put a bunch of addition</p>



<p class="wp-block-paragraph">instructions here. Uh, and we&#8217;ll just like store in, uh,</p>



<p class="wp-block-paragraph">in R13, let&#8217;s say. So then when we have finally computed our result, I&#8217;ll just move R13 into the</p>



<p class="wp-block-paragraph">return value. And what I&#8217;m really trying to say here is if you imagine a function that behaves</p>



<p class="wp-block-paragraph">this way in C++, then what I&#8217;ve written down below is really what&#8217;s actually happening in</p>



<p class="wp-block-paragraph">assembly. Your compiler compiles the code down to assembly language first, and then it assembles it</p>



<p class="wp-block-paragraph">down to machine code. So really this is what&#8217;s happening under the hood. It&#8217;s not like a special</p>



<p class="wp-block-paragraph">under the hood it&#8217;s not like a special trick when you call a function in C++</p>



<p class="wp-block-paragraph">literally the A B and C have been loaded up just before the the jump</p>



<p class="wp-block-paragraph">instruction to go to that you know line 9 they&#8217;ve been loaded up with the</p>



<p class="wp-block-paragraph">appropriate values so that&#8217;s part of like you know the magic of what higher</p>



<p class="wp-block-paragraph">level languages give us it&#8217;s just makes things a little bit easier okay so I&#8217;ve</p>



<p class="wp-block-paragraph">If I had only used RDI and RSI and RDX, those are not designated as callee saved.</p>



<p class="wp-block-paragraph">So I wouldn&#8217;t have had to preserve those, but because I chose to use R12 and R13, I</p>



<p class="wp-block-paragraph">have to preserve those for the caller, even if the caller is not going to use them.</p>



<p class="wp-block-paragraph">So I&#8217;m going to do, I&#8217;m going to do push R12 and I&#8217;m going to do push R13.</p>



<p class="wp-block-paragraph">And then at the bottom, let me do like a prologue here.</p>



<p class="wp-block-paragraph">is kind of helpful because it kind of helps you remember like, oh, did I forget the prologue?</p>



<p class="wp-block-paragraph">Did I forget the epilogue? They got a match. And then I guess the fact that you can see the words</p>



<p class="wp-block-paragraph">prologue and epilogue also helps remind you that everything is supposed to be in reverse order</p>



<p class="wp-block-paragraph">in the epilogue. So again, notice how R13 is on the inside and R12 is on the outside</p>



<p class="wp-block-paragraph">of the push pop pair. So now my program is good. And we&#8217;ve, you know, successfully translated</p>



<p class="wp-block-paragraph">We&#8217;ve, you know, successfully translated a nice function call there.</p>



<p class="wp-block-paragraph">And we&#8217;ve done a bunch of nonsense in the middle that I&#8217;m not writing down at this point.</p>



<p class="wp-block-paragraph">We are respecting the ABI.</p>



<p class="wp-block-paragraph">So if somebody else uses our function in their module, then they can be pretty confident</p>



<p class="wp-block-paragraph">that we&#8217;re not going to ruin their registers and so forth.</p>



<p class="wp-block-paragraph">We talked about the return value.</p>



<p class="wp-block-paragraph">We talked about that these general purpose registers are not for floats.</p>



<p class="wp-block-paragraph">We talked about pushes and pops.</p>



<p class="wp-block-paragraph">and pops. We&#8217;ve talked about function arguments,</p>



<p class="wp-block-paragraph">which right now we can only do integer arguments and return values.</p>



<p class="wp-block-paragraph">We&#8217;ve talked about, uh, well, he talked about the stack a little bit.</p>



<p class="wp-block-paragraph">So let me, let me just point out that, um,</p>



<p class="wp-block-paragraph">suppose you have a function. I just want to mention this briefly.</p>



<p class="wp-block-paragraph">Suppose you have a function F let&#8217;s say it&#8217;s, it&#8217;s void.</p>



<p class="wp-block-paragraph">So it doesn&#8217;t return anything and it has an integer a equals five.</p>



<p class="wp-block-paragraph">I think the reason that I wanted to do this is to show you how dangerous it is to mess</p>



<p class="wp-block-paragraph">with the wrong register like the stack register.</p>



<p class="wp-block-paragraph">So suppose we have two variables and then we&#8217;ll do something with a and b.</p>



<p class="wp-block-paragraph">So now suppose we have two threads.</p>



<p class="wp-block-paragraph">We have an execution thread one and an execution thread two.</p>



<p class="wp-block-paragraph">If you&#8217;re not familiar with threads, that just basically means your computer is literally</p>



<p class="wp-block-paragraph">executing your function or sorry, it&#8217;s executing twice at the exact same time.</p>



<p class="wp-block-paragraph">Maybe not the exact same time if you only have a single core on your CPU, but you could</p>



<p class="wp-block-paragraph">imagine that that&#8217;s possible, especially if you have many cores, that&#8217;s an operating systems</p>



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



<p class="wp-block-paragraph">But basically pretend that we have an execution thread.</p>



<p class="wp-block-paragraph">So it&#8217;s like going through your program, executing one instruction at a time.</p>



<p class="wp-block-paragraph">Then you launch another execution thread that also executes your program one instruction</p>



<p class="wp-block-paragraph">at a time.</p>



<p class="wp-block-paragraph">at a time so at some point it&#8217;s possible that um thread one and thread two could be executing f</p>



<p class="wp-block-paragraph">at the exact same time right so i&#8217;m not going to talk too much about this because this is not an</p>



<p class="wp-block-paragraph">os video but basically this could create a race condition where thread one and thread two kind</p>



<p class="wp-block-paragraph">of step on each other&#8217;s toes and they both try to modify the value of a uh at the wrong time and</p>



<p class="wp-block-paragraph">because the other thread modified it.</p>



<p class="wp-block-paragraph">So this is why local variables in a function actually live on the stack.</p>



<p class="wp-block-paragraph">A and B are not global variables.</p>



<p class="wp-block-paragraph">They&#8217;re actually temporary local variables that are sitting on the stack.</p>



<p class="wp-block-paragraph">And the way the stack works, I&#8217;m not going to talk about it too much in this video,</p>



<p class="wp-block-paragraph">is that, well, every function kind of has its own area of the stack.</p>



<p class="wp-block-paragraph">So when the function comes in, on the stack sits the return address</p>



<p class="wp-block-paragraph">return address so that the function knows where to jump back to when it returns and any local</p>



<p class="wp-block-paragraph">variable that the function makes. So if you think about it, when thread one calls F, we actually</p>



<p class="wp-block-paragraph">end up with a different version of A and a different version of B. We could imagine them</p>



<p class="wp-block-paragraph">as being, you know, A sub one and B sub one. But when thread two comes in and, you know,</p>



<p class="wp-block-paragraph">creates those local variables, we can imagine that we get two different versions of A and B also.</p>



<p class="wp-block-paragraph">of a and b also so this is how the computer prevents multiple threads from stepping on each</p>



<p class="wp-block-paragraph">other&#8217;s toes if you&#8217;re using local variables local variables are just local to the function</p>



<p class="wp-block-paragraph">this also helps if a function calls itself a bunch of times maybe one call or another might want to</p>



<p class="wp-block-paragraph">see a different version of a or b and it would be just incredibly difficult to keep track of that</p>



<p class="wp-block-paragraph">if you were using globals but when you use locals well they&#8217;re just basically different variables</p>



<p class="wp-block-paragraph">trying to lead up to is the stack is pretty important if you corrupt the</p>



<p class="wp-block-paragraph">stack then you probably are going to crash the entire program you might make</p>



<p class="wp-block-paragraph">it so that a function doesn&#8217;t know where to return from like when you do the</p>



<p class="wp-block-paragraph">return statement it might jump to some other part of the code that doesn&#8217;t even</p>



<p class="wp-block-paragraph">make sense and then everything crashes or you might mess up some data in a local</p>



<p class="wp-block-paragraph">variable like if you mess up if you mess up the stack you might be messing up the</p>



<p class="wp-block-paragraph">that the caller is depending on and so then the caller continues to execute and</p>



<p class="wp-block-paragraph">it sees the wrong value and it just crashes or you might even mess up your</p>



<p class="wp-block-paragraph">own local variables so don&#8217;t miss with the stack the reason I&#8217;m saying that now</p>



<p class="wp-block-paragraph">is because this very this register right here the RBP sorry that&#8217;s not it the</p>



<p class="wp-block-paragraph">RSP register that&#8217;s the stack pointer eventually you can learn how to</p>



<p class="wp-block-paragraph">manipulate the stack pointer in order to create your own local variables that&#8217;s</p>



<p class="wp-block-paragraph">variables that&#8217;s okay once you know what you&#8217;re doing but unless you know exactly what you&#8217;re</p>



<p class="wp-block-paragraph">doing you probably shouldn&#8217;t touch the stack pointer especially you shouldn&#8217;t mess with it</p>



<p class="wp-block-paragraph">right before you call a function or right before you return from your own function be very careful</p>



<p class="wp-block-paragraph">with that the rbp uh register is similarly dangerous uh it won&#8217;t like automatically destroy</p>



<p class="wp-block-paragraph">your program but it is uh it&#8217;s usually i mean it&#8217;s quite often used as sort of like a bookmark for</p>



<p class="wp-block-paragraph">mark for where the stack pointer was pointing in other modules and other</p>



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



<p class="wp-block-paragraph">So if you fail to preserve the base pointer,</p>



<p class="wp-block-paragraph">the RBP and then you returned from your function,</p>



<p class="wp-block-paragraph">then you might have actually messed up the stack pointer for the caller or some</p>



<p class="wp-block-paragraph">other caller somewhere in like the ancestry of your call graph.</p>



<p class="wp-block-paragraph">So be very careful. I mean,</p>



<p class="wp-block-paragraph">I guess all callee preserved variables or registers you should be very careful,</p>



<p class="wp-block-paragraph">careful about, but these are like kind of the two worst ones to forget about.</p>



<p class="wp-block-paragraph">first ones to forget about.</p>



<p class="wp-block-paragraph">There&#8217;s also another register that is not listed on this page called the</p>



<p class="wp-block-paragraph">instruction pointer. It&#8217;s RIP.</p>



<p class="wp-block-paragraph">It basically is a register that holds</p>



<p class="wp-block-paragraph">the memory location of the next instruction to be executed.</p>



<p class="wp-block-paragraph">So if you modify that, then you just told your program to go execute</p>



<p class="wp-block-paragraph">in some random crazy place and probably everything is going to crash.</p>



<p class="wp-block-paragraph">R10 and R11.</p>



<p class="wp-block-paragraph">I can&#8217;t remember already if I explain this, but I&#8217;ll just say it again.</p>



<p class="wp-block-paragraph">But I&#8217;ll just say it again, R10 and R11 are just temporary registers.</p>



<p class="wp-block-paragraph">You don&#8217;t have to preserve them.</p>



<p class="wp-block-paragraph">But the callee, if you call another function, they don&#8217;t have to preserve the registers either,</p>



<p class="wp-block-paragraph">which means it can be really fast to use R10 and R12 if you&#8217;re kind of like already running out of registers.</p>



<p class="wp-block-paragraph">If you don&#8217;t have to make a function call anywhere, so you know no one is going to ruin your R10 and R11,</p>



<p class="wp-block-paragraph">then you can also use R10 and R11 to sort of like, you know,</p>



<p class="wp-block-paragraph">reduce your hits to system RAM to speed up your program.</p>



<p class="wp-block-paragraph">So R10 and R11, they should probably be used last.</p>



<p class="wp-block-paragraph">R12, 13, 14, 15, I usually use those first.</p>



<p class="wp-block-paragraph">Once I start running out of those, Kali saved.</p>



<p class="wp-block-paragraph">Then I&#8217;ll start eating into the arguments.</p>



<p class="wp-block-paragraph">I&#8217;ll say, you know, I&#8217;m going to start using RBX</p>



<p class="wp-block-paragraph">and then I&#8217;m going to use RDI and RSI and whatever.</p>



<p class="wp-block-paragraph">It&#8217;s okay if you use, you know, the argument registers.</p>



<p class="wp-block-paragraph">as long as you&#8217;ve already done something with your incoming arguments or you don&#8217;t have any.</p>



<p class="wp-block-paragraph">So just keep that in mind.</p>



<p class="wp-block-paragraph">You&#8217;re allowed to use them.</p>



<p class="wp-block-paragraph">I mean, this is all just a standard.</p>



<p class="wp-block-paragraph">As long as you obey what the rule is, then you&#8217;ll be all right.</p>



<p class="wp-block-paragraph">The rule is you got to callee save those.</p>



<p class="wp-block-paragraph">The rule is that one&#8217;s just temporary.</p>



<p class="wp-block-paragraph">The rule is this one&#8217;s an argument and so forth.</p>



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



<p class="wp-block-paragraph">locals and how we use these registers. Let me try to just show you one more</p>



<p class="wp-block-paragraph">thing real fast. So I said before that the size of the REX register is 64 bits</p>



<p class="wp-block-paragraph">because it takes up the whole available register bits in the CPU but there are</p>



<p class="wp-block-paragraph">other versions of this register that we could use. What if you only wanted to use</p>



<p class="wp-block-paragraph">32 bits in your register? What if you wanted to pack like two different 32</p>



<p class="wp-block-paragraph">32-bit numbers in the same 64-bit register.</p>



<p class="wp-block-paragraph">You could do that.</p>



<p class="wp-block-paragraph">There are also older modules and instructions</p>



<p class="wp-block-paragraph">that will operate only on 32-bits.</p>



<p class="wp-block-paragraph">So how do you reference only 32-bits of a register?</p>



<p class="wp-block-paragraph">Well, if I search for, oh shoot, what was it?</p>



<p class="wp-block-paragraph">Is it EDI?</p>



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



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



<p class="wp-block-paragraph">It&#8217;s in here somewhere.</p>



<p class="wp-block-paragraph">This is like a good lesson in actually preparing before you record a video.</p>



<p class="wp-block-paragraph">So I&#8217;m going to do architecture overview, CPU registers.</p>



<p class="wp-block-paragraph">Oh, it was at the beginning of this section.</p>



<p class="wp-block-paragraph">I always skip down to 2.4, 12 point something, and I should have gone to 2.3.1.1.</p>



<p class="wp-block-paragraph">So obviously I&#8217;m not going to remember all of this, but basically notice at the top,</p>



<p class="wp-block-paragraph">we have the REX register, all 64 bits of it.</p>



<p class="wp-block-paragraph">But if you wanted to access the lowest six, sorry, the lowest 32 bits, then in your code,</p>



<p class="wp-block-paragraph">instead of putting REX, you put EAX.</p>



<p class="wp-block-paragraph">So for example, you know, move REX, whoops, REX, let&#8217;s say the number five, but over</p>



<p class="wp-block-paragraph">here we&#8217;re going to move EAX, the number five.</p>



<p class="wp-block-paragraph">five uh here it&#8217;s going to set the whole 64 bits to just like a bunch of zeros and then there&#8217;s</p>



<p class="wp-block-paragraph">going to be a few bits that help represent five the lowest bits so it&#8217;s going to erase like the</p>



<p class="wp-block-paragraph">whole thing all 64 bits but if you use the eax instruction then only the lowest 32 bits will</p>



<p class="wp-block-paragraph">actually be modified which means whatever data you had in the higher 60 the higher 32 bits of the</p>



<p class="wp-block-paragraph">of the register itself they&#8217;re just going to stay there it&#8217;s going to be considered</p>



<p class="wp-block-paragraph">point so sometimes if you&#8217;re going to work with something that&#8217;s going to be using only 32 bits</p>



<p class="wp-block-paragraph">of your register you better make sure that the other bits are uh are cleared out if you intend</p>



<p class="wp-block-paragraph">to use rax right after that and and vice versa so keep in mind that we have other forms of the same</p>



<p class="wp-block-paragraph">register a designation but just to let the cpu know that we only want to touch 32 bits worth we</p>



<p class="wp-block-paragraph">And this, this kind of table covers all of the registers,</p>



<p class="wp-block-paragraph">REX, RBX, RCX, and so forth.</p>



<p class="wp-block-paragraph">I think it doesn&#8217;t cover the instruction pointer</p>



<p class="wp-block-paragraph">and something else in there, but you know, most of them.</p>



<p class="wp-block-paragraph">However, you know, for my purposes,</p>



<p class="wp-block-paragraph">I usually just use 64 bits</p>



<p class="wp-block-paragraph">because I&#8217;m not like super advanced</p>



<p class="wp-block-paragraph">unless I have to divide or do something.</p>



<p class="wp-block-paragraph">Okay, so that&#8217;s the last thing I wanted to mention</p>



<p class="wp-block-paragraph">about the general purpose registers.</p>



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



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



<p class="wp-block-paragraph">So the not sure if I said this before already, but basically the.</p>



<p class="wp-block-paragraph">So just to emphasize the, uh, the RBP register is the base pointer register.</p>



<p class="wp-block-paragraph">You&#8217;re probably going to mess up, uh, the caller because the caller often uses RBP as</p>



<p class="wp-block-paragraph">a bookmark for the stack pointer.</p>



<p class="wp-block-paragraph">mark for the stack pointer you definitely don&#8217;t want to mess with a stack pointer unless you</p>



<p class="wp-block-paragraph">actually know what you&#8217;re doing in order to create local variables um there&#8217;s another variable</p>



<p class="wp-block-paragraph">another register in here called rip which is the instruction pointer register so if you</p>



<p class="wp-block-paragraph">if you mess that one up then the program is going to start executing in some crazy place that doesn&#8217;t</p>



<p class="wp-block-paragraph">even make sense okay so now that we&#8217;ve talked about general purpose registers by the way i</p>



<p class="wp-block-paragraph">called special purpose registers, not general purpose.</p>



<p class="wp-block-paragraph">General purpose is like RAX, RBX, all these ones that you can just randomly use as long</p>



<p class="wp-block-paragraph">as you respect the ABI.</p>



<p class="wp-block-paragraph">But the base pointer and the stack pointer, those are kind of special.</p>



<p class="wp-block-paragraph">And the RIP, the instruction pointer, that&#8217;s a special register.</p>



<p class="wp-block-paragraph">You shouldn&#8217;t mess with those unless you really know what you&#8217;re doing or you&#8217;ll get yourself</p>



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



<p class="wp-block-paragraph">But now that we&#8217;ve talked about those, let&#8217;s talk about the floating point registers just</p>



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



<p class="wp-block-paragraph">about using floats until a lot later but basically</p>



<p class="wp-block-paragraph">i at least want to mention that we do have registers that will work with floating point</p>



<p class="wp-block-paragraph">operations let me see there we go okay so it&#8217;s 18.2 floating point registers um essentially</p>



<p class="wp-block-paragraph">we use xmm0 through xmm13 for floating point numbers there&#8217;s not going to be any</p>



<p class="wp-block-paragraph">not going to be any crazy letters like RDI, RSI that you have to memorize. You can just use zero</p>



<p class="wp-block-paragraph">through 15 and it&#8217;s totally fine. When you want to return a floating point number from a function,</p>



<p class="wp-block-paragraph">you will load up XMM0 as the return value. You won&#8217;t even touch RAX. The arguments are the same.</p>



<p class="wp-block-paragraph">They&#8217;re just kind of ordered. It&#8217;s like the first argument is going to be XMM0. The second argument</p>



<p class="wp-block-paragraph">is going to be XMM1 and so forth all the way up to XMM15. If you need more than 15 arguments,</p>



<p class="wp-block-paragraph">some sorcery with the stack or with some kind of system like the global variable or something</p>



<p class="wp-block-paragraph">and also for floating point registers we use different instructions so what I really want</p>



<p class="wp-block-paragraph">you to know because this is mostly about the regular registers is that if you have floating</p>



<p class="wp-block-paragraph">point numbers and you want to multiply them or do some other operation on them the normal</p>



<p class="wp-block-paragraph">instructions for regular registers won&#8217;t actually work you&#8217;ll have to look up a different set of</p>



<p class="wp-block-paragraph">and i&#8217;m going to do this in another video some other time uh if you want to move uh data with</p>



<p class="wp-block-paragraph">a floating point register you can&#8217;t just use the regular move instruction you got to use either</p>



<p class="wp-block-paragraph">move ss or move sd um and and both operands have to be floats or actually i think the one on the</p>



<p class="wp-block-paragraph">right can be memory but um move ss basically means a single piece of data single precision</p>



<p class="wp-block-paragraph">So you can move like multiple pieces of data at a time.</p>



<p class="wp-block-paragraph">I&#8217;m not going to really cover that.</p>



<p class="wp-block-paragraph">Um, but you can move single precision floating points, which means there are 32 bit floating</p>



<p class="wp-block-paragraph">points or you can move double precision floating points, which means they&#8217;re a full 64 bit</p>



<p class="wp-block-paragraph">floating point number.</p>



<p class="wp-block-paragraph">There&#8217;s also bigger registers, but I&#8217;m not going to talk about that.</p>



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



<p class="wp-block-paragraph">Well, yeah.</p>



<p class="wp-block-paragraph">Later processors 256.</p>



<p class="wp-block-paragraph">I think mine has that, but I&#8217;m not going to talk about that.</p>



<p class="wp-block-paragraph">I think mine are like YMM or something.</p>



<p class="wp-block-paragraph">So yeah, with that, those are the basics of registers and some stuff to keep in mind.</p>



<p class="wp-block-paragraph">In future videos, I&#8217;m going to talk more in depth about making functions and calling functions</p>



<p class="wp-block-paragraph">and all that stuff, respecting the ABI.</p>



<p class="wp-block-paragraph">But for now, here is your primer.</p>



<p class="wp-block-paragraph">I hope it was useful on CPU registers.</p>



<p class="wp-block-paragraph">Thank you for watching this video.</p>



<p class="wp-block-paragraph">I&#8217;ll see you in the future.</p>



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



<p class="wp-block-paragraph">small little favor could you please subscribe and follow this channel or these videos or whatever</p>



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



<p class="wp-block-paragraph">mean the world to me and it&#8217;ll help make more videos and grow this community so we&#8217;ll be able</p>



<p class="wp-block-paragraph">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. You know sometimes I&#8217;m sleeping in the</p>



<p class="wp-block-paragraph">middle of the night and I just wake up because I know somebody subscribed or</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 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. 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">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 and</p>



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



<p class="wp-block-paragraph">If you have a suggestion for</p>



<p class="wp-block-paragraph">Clarifications or errata or just future videos that you want to see</p>



<p class="wp-block-paragraph">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">just send me a comment, whatever. I also wake up for those in the middle of the night. I get,</p>



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



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



<p class="wp-block-paragraph">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/mastering-x86-64-assembly-a-beginners-guide-to-cpu-registers/">Mastering x86-64 Assembly: A Beginner&#8217;s Guide to CPU Registers</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-assembly-a-beginners-guide-to-cpu-registers/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>
	</channel>
</rss>
