<?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>assembly programming Archives - NeuralLantern.com</title>
	<atom:link href="https://www.NeuralLantern.com/tag/assembly-programming/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.NeuralLantern.com/tag/assembly-programming/</link>
	<description></description>
	<lastBuildDate>Sun, 18 Jan 2026 00:49:40 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=7.0</generator>

<image>
	<url>https://www.NeuralLantern.com/wp-content/uploads/2025/04/cropped-2025-04-04-Lantern-03-32x32.png</url>
	<title>assembly programming Archives - NeuralLantern.com</title>
	<link>https://www.NeuralLantern.com/tag/assembly-programming/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>Learn Conditional Branching in x86-64 Assembly (YASM/NASM) &#8211; CMP, JE, JNE, JL, JG &#038; More</title>
		<link>https://www.NeuralLantern.com/learn-conditional-branching-in-x86-64-assembly-yasm-nasm-cmp-je-jne-jl-jg-more/</link>
					<comments>https://www.NeuralLantern.com/learn-conditional-branching-in-x86-64-assembly-yasm-nasm-cmp-je-jne-jl-jg-more/#respond</comments>
		
		<dc:creator><![CDATA[mike]]></dc:creator>
		<pubDate>Sun, 18 Jan 2026 00:49:38 +0000</pubDate>
				<category><![CDATA[Assembly Language]]></category>
		<category><![CDATA[Coding]]></category>
		<category><![CDATA[Computer Science]]></category>
		<category><![CDATA[Videos]]></category>
		<category><![CDATA[assembly language tutorial]]></category>
		<category><![CDATA[assembly programming]]></category>
		<category><![CDATA[cmp instruction]]></category>
		<category><![CDATA[computer architecture]]></category>
		<category><![CDATA[conditional branching]]></category>
		<category><![CDATA[conditional jumps]]></category>
		<category><![CDATA[Ed Jorgensen]]></category>
		<category><![CDATA[JE JNE]]></category>
		<category><![CDATA[JL JG]]></category>
		<category><![CDATA[jump out of range]]></category>
		<category><![CDATA[low level programming]]></category>
		<category><![CDATA[NASM tutorial]]></category>
		<category><![CDATA[operating systems programming]]></category>
		<category><![CDATA[reverse engineering]]></category>
		<category><![CDATA[signed unsigned jumps]]></category>
		<category><![CDATA[x86 assembly]]></category>
		<category><![CDATA[x86-64 assembly]]></category>
		<category><![CDATA[Yasm tutorial]]></category>
		<guid isPermaLink="false">https://www.NeuralLantern.com/?p=287</guid>

					<description><![CDATA[<p>Learn how conditional branching really works in x86-64 assembly using CMP and conditional jumps like JE, JNE, JL, JG. See signed vs unsigned comparison instructions and real code examples that show how if-statements are implemented at the machine level.</p>
<p>The post <a href="https://www.NeuralLantern.com/learn-conditional-branching-in-x86-64-assembly-yasm-nasm-cmp-je-jne-jl-jg-more/">Learn Conditional Branching in x86-64 Assembly (YASM/NASM) &#8211; CMP, JE, JNE, JL, JG &amp; More</a> appeared first on <a href="https://www.NeuralLantern.com">NeuralLantern.com</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<figure class="wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio"><div class="wp-block-embed__wrapper">
<iframe title="Learn Conditional Branching in x86-64 Assembly (YASM/NASM) - CMP, JE, JNE, JL, JG &amp; More" width="1380" height="776" src="https://www.youtube.com/embed/aB_BOp0KCh8?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 practical x86-64 assembly language tutorial we dive deep into conditional branching using YASM (also works with NASM) on Ubuntu/Linux.</p>



<p class="wp-block-paragraph">We cover:</p>



<ul class="wp-block-list">
<li>The CMP instruction and how it sets the RFLAGS register</li>



<li>All major conditional jump instructions (JE, JNE, JL, JLE, JG, JGE, JB, JBE…)</li>



<li>Important difference between signed (JL/JG) vs unsigned (JB/JA) jumps</li>



<li>Real code examples showing how to implement if-style decisions</li>



<li>How to handle the &#8220;jump out of range&#8221; assembler error</li>



<li>Visual flow diagrams explaining control flow</li>
</ul>



<p class="wp-block-paragraph">Perfect for students learning low-level programming, reverse engineering, operating systems, or anyone who wants to understand how if-statements, comparisons, and decisions are actually implemented at the machine level.</p>



<p class="wp-block-paragraph">Highly recommended companion resource: Professor Ed Jorgensen&#8217;s free x86-64 Assembly Language Programming book (linked in comments).</p>



<p class="wp-block-paragraph">Enjoy learning assembly!</p>



<p class="wp-block-paragraph">Intro to Conditional Branching in Assembly 00:00:00<br>Recommended Book: x86-64 Guide by Ed Jorgensen 00:00:23<br>Unconditional vs Conditional Jumps 00:01:39<br>How Conditional Branching Works 00:02:06<br>The CMP Instruction Explained 00:03:21<br>Overview of Conditional Jump Instructions 00:04:50<br>Signed vs Unsigned Jump Instructions 00:06:10<br>Jump Equal &amp; Jump Not Equal (JE/JNE) 00:07:00<br>Main Conditional Jumps Summary 00:08:14<br>Starting the Code Example 00:09:09<br>Program Structure &amp; Printing Setup 00:10:35<br>Function Prologue &amp; Getting User Input 00:13:21<br>First Branch: Is Number Greater Than 0? 00:15:17<br>Live Demo: Testing Greater Than Zero 00:21:53<br>Second Branch: Is Number Less Than 10? 00:22:52<br>Live Demo: Testing Range 0-10 00:26:06<br>Third Branch: Is Number == 5? 00:26:52<br>Live Demo: Testing Equal to 5 00:28:00<br>Jump Out of Range Error &amp; Workaround 00:29:00<br>Final Summary &amp; Closing 00:32:17<br>Thanks &amp; Call to Subscribe 00:32:57</p>



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



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



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



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



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



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



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



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



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



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



<p class="wp-block-paragraph">Please show your support!</p>



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



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



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



<li>Subscribe to Blog: https://www.NeuralLantern.com</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. In this video,</p>



<p class="wp-block-paragraph">we&#8217;re going to talk about conditional branching in an assembly program.</p>



<p class="wp-block-paragraph">We&#8217;re going to be looking at YASM x86-64 assembly or AMD 64 assembly</p>



<p class="wp-block-paragraph">within Ubuntu, but this will probably be a useful video.</p>



<p class="wp-block-paragraph">If you&#8217;re just thinking about how to branch in assembly in general.</p>



<p class="wp-block-paragraph">So for starters, I&#8217;m going to promote one of my favorite books,</p>



<p class="wp-block-paragraph">as I often do. This is a book that is free and open source. You can get a copy yourself. I did</p>



<p class="wp-block-paragraph">not write this. The author of this book is a genius. His name is Professor Ed Jorgensen, PhD.</p>



<p class="wp-block-paragraph">He made this whole book and released it under an open source license. So you can literally just go</p>



<p class="wp-block-paragraph">to his website and get a copy of this for free. You can use this to make yourself into an assembly</p>



<p class="wp-block-paragraph">expert. So I highly recommend this to everybody. Anyway, so I&#8217;m going to open up this book. I&#8217;m</p>



<p class="wp-block-paragraph">up this book i&#8217;m going to assume that maybe you&#8217;ll get a copy or you&#8217;re just going to follow along</p>



<p class="wp-block-paragraph">with what i&#8217;m doing on the screen but i&#8217;m going to open this book and i&#8217;m going to go to section 7</p>



<p class="wp-block-paragraph">which is entitled instruction set overview i&#8217;m going to double click it so open out open the</p>



<p class="wp-block-paragraph">subsections and then i&#8217;m going to click on 7.7 control control instructions and then under that</p>



<p class="wp-block-paragraph">i&#8217;m going to click on 7.7.3 conditional control instructions so uh that&#8217;s a lot of subsections</p>



<p class="wp-block-paragraph">So that&#8217;s a lot of subsections, isn&#8217;t it? I love a book with a ton of subsections. It makes things so much easier to find</p>



<p class="wp-block-paragraph">If you or someone you know is going to write a book, especially if it&#8217;s technical, please use lots and lots of subsections</p>



<p class="wp-block-paragraph">Anyway, so conditional control instructions in my last video</p>



<p class="wp-block-paragraph">We talked about the jump instruction where we can just be sitting somewhere in our program and then just jump somewhere else</p>



<p class="wp-block-paragraph">Anywhere we want based on the label so conditional branching or conditional control instructions</p>



<p class="wp-block-paragraph">conditional control instructions are kind of the same thing except they will</p>



<p class="wp-block-paragraph">decide whether or not to actually jump they might fall through to the next</p>



<p class="wp-block-paragraph">instruction or they might jump away to somewhere else so conditional branching</p>



<p class="wp-block-paragraph">or conditional jumping it basically kind of goes like this step one you compare</p>



<p class="wp-block-paragraph">two values to each other they will probably be sitting in registers and then</p>



<p class="wp-block-paragraph">conditional branching instruction or a conditional jump instruction there&#8217;s</p>



<p class="wp-block-paragraph">lots of synonyms for these but basically in the regular jump instruction that I</p>



<p class="wp-block-paragraph">just talked about in the last video you just jumped and it was only one thing</p>



<p class="wp-block-paragraph">that you had to do but in this case we first have to compare two items and then</p>



<p class="wp-block-paragraph">we&#8217;ll issue the jumping instruction because the jumping instruction will</p>



<p class="wp-block-paragraph">have to decide whether to jump or not based on the result of your comparison</p>



<p class="wp-block-paragraph">which makes sense right like if you&#8217;re going to implement something that&#8217;s like</p>



<p class="wp-block-paragraph">if you know I&#8217;ll put like a expr for expression then well the first thing that we have to decide</p>



<p class="wp-block-paragraph">before we jump either into the block of the if or if we jump into the block of the else is whether</p>



<p class="wp-block-paragraph">or not that expression is true right so that&#8217;s kind of what the compare instruction is doing</p>



<p class="wp-block-paragraph">sort of in higher level languages like c++ it can get a lot more complicated than that but you know</p>



<p class="wp-block-paragraph">Okay, so we&#8217;re going to do conditional branching instructions.</p>



<p class="wp-block-paragraph">Let me show you a few of those.</p>



<p class="wp-block-paragraph">Okay, so for starters, here&#8217;s the compare instruction right here on page 126.</p>



<p class="wp-block-paragraph">I think it&#8217;s actually 127 in the PDF, but basically, you know, 126.</p>



<p class="wp-block-paragraph">We do CMP followed by two operands.</p>



<p class="wp-block-paragraph">So if we want to compare, I don&#8217;t know, let&#8217;s say R12 with R13, that&#8217;s how you would do it.</p>



<p class="wp-block-paragraph">just a two operand instruction there&#8217;s no results that are going to get stored in one of these</p>



<p class="wp-block-paragraph">operands both of these operands are just for input the result actually gets stored in a special</p>



<p class="wp-block-paragraph">register called r flags which we hardly ever use or look at in in the most common assembly</p>



<p class="wp-block-paragraph">instructions and design patterns we just kind of like let the r flags do whatever it&#8217;s going to do</p>



<p class="wp-block-paragraph">and the compare instruction will fill that up so imagine that you know depending on whether r13 is</p>



<p class="wp-block-paragraph">is like greater than r12 or less than or they&#8217;re equal or something else is going on between the</p>



<p class="wp-block-paragraph">two of them the compare instruction just fills up all sorts of flags defining the relationship</p>



<p class="wp-block-paragraph">between those two registers or those two operands r12 and r13 and then after the r flags register</p>



<p class="wp-block-paragraph">is all set up then the conditional branching instruction will sort of rely on r flags in</p>



<p class="wp-block-paragraph">order to decide whether or not it&#8217;s going to jump or not okay so let&#8217;s see there are tons</p>



<p class="wp-block-paragraph">there are tons of conditional well not tons i guess like half a page worth but there are lots</p>



<p class="wp-block-paragraph">of conditional branching instructions we can use the first two i want to show you and after this</p>



<p class="wp-block-paragraph">by the way i&#8217;m going to write a sample program just to show you how it is in practice but so the</p>



<p class="wp-block-paragraph">first one is je which means let&#8217;s jump if the two operands were equal so again you know we compare</p>



<p class="wp-block-paragraph">here we&#8217;ll say sets up r flags and then we&#8217;ll decide like what we&#8217;re going to do are we going</p>



<p class="wp-block-paragraph">to jump if they&#8217;re equal to some label i&#8217;ll write some label here so that means uh the je instruction</p>



<p class="wp-block-paragraph">will look at the r flags register and decide did compare think that r12 and r13 were equal if it</p>



<p class="wp-block-paragraph">did then we&#8217;ll jump if not execution will fall down to the next line so i&#8217;ll put a nope instruction</p>



<p class="wp-block-paragraph">So if they were equal, then we&#8217;ll end up jumping to some label.</p>



<p class="wp-block-paragraph">And if they were not equal, we&#8217;ll end up letting execution fall through to the nope on line 10.</p>



<p class="wp-block-paragraph">And same thing for jump not equal and all these other ones.</p>



<p class="wp-block-paragraph">But there are actually two different versions of some of these instructions that kind of seem the same.</p>



<p class="wp-block-paragraph">Like for instance, this one right here, JL, that means jump if the first operand was less than the second operand.</p>



<p class="wp-block-paragraph">is a jump below that jumps also if the first operand was less than the second operand so</p>



<p class="wp-block-paragraph">what&#8217;s the difference between these two let me get my pen I love excuses to do my little pen now</p>



<p class="wp-block-paragraph">well I&#8217;ve got my pen it&#8217;s working oh I erased it okay okay it&#8217;s working anyway so notice how jump</p>



<p class="wp-block-paragraph">less than and jump below are basically doing the same thing but there&#8217;s one difference one of them</p>



<p class="wp-block-paragraph">One of them works on signed operands and one of them works on unsigned operands.</p>



<p class="wp-block-paragraph">So keep this in mind.</p>



<p class="wp-block-paragraph">If you&#8217;re comparing two signed integers, you want to use this group up here of instructions.</p>



<p class="wp-block-paragraph">But if you&#8217;re using unsigned integers or just bits, then you want to use this group of instructions down here.</p>



<p class="wp-block-paragraph">And you definitely want to know what kind of data you have that you&#8217;re comparing.</p>



<p class="wp-block-paragraph">Notice though that jump equal and jump not equal, they don&#8217;t really care.</p>



<p class="wp-block-paragraph">not equal they don&#8217;t really care if the operands are signed or unsigned that&#8217;s because if you want</p>



<p class="wp-block-paragraph">to check to see if two things are equal you just look at all of their bits and if all of their</p>



<p class="wp-block-paragraph">bits are the same then you just say they&#8217;re equal you don&#8217;t even care actually if it&#8217;s a signed or</p>



<p class="wp-block-paragraph">unsigned integer if it&#8217;s a float if it&#8217;s a whatever you just you just go are all the bits equal or are</p>



<p class="wp-block-paragraph">at least one of the bits unequal then we&#8217;ll say it&#8217;s equal or not equal that&#8217;s it so keep that in</p>



<p class="wp-block-paragraph">mind for all of your jump instructions where you&#8217;re trying to see if things are equal or not</p>



<p class="wp-block-paragraph">or not equal you&#8217;re going to definitely be using these whether you&#8217;re using signed or unsigned</p>



<p class="wp-block-paragraph">integers or floats or anything else like that but when it comes to actually you know integers that</p>



<p class="wp-block-paragraph">are signed or unsigned these are the instructions you use for signed and these are the instructions</p>



<p class="wp-block-paragraph">whoops that you use for what have i done that you&#8217;ll use for unsigned i keep confusing the on</p>



<p class="wp-block-paragraph">off button for the red button is what i&#8217;m doing oh okay whatever i guess we&#8217;re done anyway</p>



<p class="wp-block-paragraph">I guess we&#8217;re done anyway.</p>



<p class="wp-block-paragraph">So keep that in mind.</p>



<p class="wp-block-paragraph">In this video, we&#8217;re just going to be using the signed integer instructions</p>



<p class="wp-block-paragraph">because that&#8217;s the example that I have set up.</p>



<p class="wp-block-paragraph">But just imagine you could very easily translate this to unsigned integers or floats.</p>



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



<p class="wp-block-paragraph">We&#8217;ve got jump less than, we&#8217;ve got jump less than or equal to,</p>



<p class="wp-block-paragraph">then we&#8217;ve got jump greater than, and then we&#8217;ve got jump greater than or equal to.</p>



<p class="wp-block-paragraph">And when you combine those with jump equal to or jump not equal to,</p>



<p class="wp-block-paragraph">jump not equal to that&#8217;s pretty much all the comparison that you need and if you want to do</p>



<p class="wp-block-paragraph">something much much more complex well then you can just you know start stacking logic on top of</p>



<p class="wp-block-paragraph">logic and and branching on top of branching to do something that&#8217;s like this is what i&#8217;m talking</p>



<p class="wp-block-paragraph">like if you want to do this expression and expression or expression and then maybe like a</p>



<p class="wp-block-paragraph">expression, right? If you want to do something super complicated, like we can do in a higher</p>



<p class="wp-block-paragraph">level language, you&#8217;re just going to have to make a bunch of mini jumps or find another</p>



<p class="wp-block-paragraph">way to reduce your logic. But in this video, we&#8217;re just going to be taking a very simple</p>



<p class="wp-block-paragraph">comparison. All right. So I think that&#8217;s everything that I can show you inside of this book. Let&#8217;s</p>



<p class="wp-block-paragraph">go to the code now. So here&#8217;s a little empty code, source code, a page. I&#8217;m going to stick</p>



<p class="wp-block-paragraph">all my code inside of it. Another reminder that if you don&#8217;t know how to write assembly</p>



<p class="wp-block-paragraph">you don&#8217;t know how to write assembly at all you should see my other videos because i&#8217;m not going</p>



<p class="wp-block-paragraph">to cover the basics of assembly in this video if you want to learn how to make a make file or compile</p>



<p class="wp-block-paragraph">or assemble or like do all the basics that i&#8217;m not talking about in this video i&#8217;ve covered them all</p>



<p class="wp-block-paragraph">in my other videos up to this point so for now we&#8217;re just going to focus only on conditional</p>



<p class="wp-block-paragraph">branching conditional jumping inside of an assembly module so i&#8217;m going to start off by setting up my</p>



<p class="wp-block-paragraph">and i&#8217;m gonna say paste and again you know this is covered in other videos but long story short</p>



<p class="wp-block-paragraph">i&#8217;m just going to be printing out a bunch of strings while this program executes so</p>



<p class="wp-block-paragraph">i&#8217;m going to announce that the branching test is going to begin and then i&#8217;m going to ask the user</p>



<p class="wp-block-paragraph">for some input and then i&#8217;m going to based on whatever the user inputs i&#8217;m going to just print</p>



<p class="wp-block-paragraph">out a message that sort of you know understands what the user inputted so if the user inputted</p>



<p class="wp-block-paragraph">something greater than zero it&#8217;s going to output that if they entered something that was less than</p>



<p class="wp-block-paragraph">10 it&#8217;s going to print that if they entered something that was equal to five it&#8217;s going to</p>



<p class="wp-block-paragraph">print that and then it&#8217;s going to say we&#8217;ve ended the branching tests then i have like a crlf again</p>



<p class="wp-block-paragraph">all this stuff is explained in other videos but we&#8217;re just basically printing so i&#8217;m going to</p>



<p class="wp-block-paragraph">start the text section of my program which is where all the instructions are and i&#8217;m going to</p>



<p class="wp-block-paragraph">a function that I can call on. Again, this video does not cover a bunch of other extra stuff. I&#8217;m</p>



<p class="wp-block-paragraph">just going to show you how to branch. Just trust me when I say that this helps me easily print</p>



<p class="wp-block-paragraph">numbers and things, or I guess in this particular case, it&#8217;ll help me input a number from the user.</p>



<p class="wp-block-paragraph">But for you, if you&#8217;re trying to run this at home, you can just start hard coding numbers just to</p>



<p class="wp-block-paragraph">make sure that you can branch correctly and then eventually work your way up to a library that can</p>



<p class="wp-block-paragraph">videos in the distant future. I don&#8217;t know years from now. I don&#8217;t know.</p>



<p class="wp-block-paragraph">I might release the library or some other stuff to the casual viewer.</p>



<p class="wp-block-paragraph">So now here&#8217;s our entry point. Again,</p>



<p class="wp-block-paragraph">I mentioned that there&#8217;s going to be other parts of this program that I&#8217;m not</p>



<p class="wp-block-paragraph">showing. So this is a hybrid program where a C++ driver actually will call on</p>



<p class="wp-block-paragraph">my cool function. So just imagine from somewhere else in the program,</p>



<p class="wp-block-paragraph">you&#8217;re calling on the cool function. How do we know it&#8217;s a function?</p>



<p class="wp-block-paragraph">a label called cool and then it has a return statement at the very end.</p>



<p class="wp-block-paragraph">That&#8217;s pretty much all we need to know right now.</p>



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



<p class="wp-block-paragraph">I don&#8217;t know why I always have the word load in all of my things.</p>



<p class="wp-block-paragraph">I think I put it in there on an early example</p>



<p class="wp-block-paragraph">and then I just kept copy pasting to my heart&#8217;s content and never stopped.</p>



<p class="wp-block-paragraph">We mark the function cool as global so that other modules can call on it</p>



<p class="wp-block-paragraph">like the C++ module that I&#8217;m not showing you.</p>



<p class="wp-block-paragraph">But if you&#8217;re writing a pure assembly module,</p>



<p class="wp-block-paragraph">you don&#8217;t really need to mark your functions as global.</p>



<p class="wp-block-paragraph">if everything&#8217;s within the same piece of source code it&#8217;s totally fine so then I&#8217;m going to make</p>



<p class="wp-block-paragraph">a function call to a function called branch test so very quickly before I actually add branch test</p>



<p class="wp-block-paragraph">I&#8217;m just going to add another function that we&#8217;re not covering in this video called crlf and long</p>



<p class="wp-block-paragraph">story short all it does is just it prints a new line in the terminal and that&#8217;s it like it&#8217;s just</p>



<p class="wp-block-paragraph">it&#8217;s a convenience function I like to be able to call crlf and have a new line okay so let&#8217;s start</p>



<p class="wp-block-paragraph">our branch test function so here&#8217;s the signature it is just branch test with a</p>



<p class="wp-block-paragraph">void in front of it I&#8217;m gonna put a ret to say that this is a function that we</p>



<p class="wp-block-paragraph">will return from and I left myself a little note here saying that I&#8217;m gonna</p>



<p class="wp-block-paragraph">use register r12 as the number that the user inputs so that means I&#8217;m gonna have</p>



<p class="wp-block-paragraph">to preserve that because you have to respect the ABI and the ABI says that</p>



<p class="wp-block-paragraph">The ABI says that R12 is a Kali saved register.</p>



<p class="wp-block-paragraph">Again, see my other videos if you don&#8217;t know what I&#8217;m talking about.</p>



<p class="wp-block-paragraph">But I&#8217;m just going to preserve it in a section that I call the prologue, or actually the</p>



<p class="wp-block-paragraph">book calls the prologue.</p>



<p class="wp-block-paragraph">I do that now though too.</p>



<p class="wp-block-paragraph">And, you know, restore it in a section called the epilogue.</p>



<p class="wp-block-paragraph">So we have prologue and epilogue.</p>



<p class="wp-block-paragraph">We can have a push-pop pair.</p>



<p class="wp-block-paragraph">And then in between, we can have, you know, the meat of our function.</p>



<p class="wp-block-paragraph">Okay, so for the introduction, I&#8217;m just going to write a few instructions that just print</p>



<p class="wp-block-paragraph">out a message.</p>



<p class="wp-block-paragraph">print out a message. So we&#8217;re going to print out that intro message and then call</p>



<p class="wp-block-paragraph">CRLF to print a new line and then return. So maybe actually at this point we could</p>



<p class="wp-block-paragraph">probably check to see if this program works by trying to run it.</p>



<p class="wp-block-paragraph">So let me say clear and make run.</p>



<p class="wp-block-paragraph">Okay. So now the driver just prints hello. The driver is</p>



<p class="wp-block-paragraph">elsewhere and we get that welcome message that says begin</p>



<p class="wp-block-paragraph">branching test and then the driver retains control after that. So</p>



<p class="wp-block-paragraph">let&#8217;s do let&#8217;s do a goodbye message real fast so we&#8217;ll do our goodbye message</p>



<p class="wp-block-paragraph">it&#8217;s just another string that I&#8217;m printing it&#8217;s no big deal right now but</p>



<p class="wp-block-paragraph">if I run the program again you can see that begin branching test and end</p>



<p class="wp-block-paragraph">branching test okay so far we&#8217;re not really doing branching yet the next thing</p>



<p class="wp-block-paragraph">we need to do is ask for a number so this is how I&#8217;m going to do it in my</p>



<p class="wp-block-paragraph">this is how I&#8217;m going to do it in my program. In your program it might be different or you might</p>



<p class="wp-block-paragraph">have a hard-coded number or something like that. So for now just trust me when I say that I&#8217;m going</p>



<p class="wp-block-paragraph">to print a prompt basically saying could you please enter a number and then the next two lines</p>



<p class="wp-block-paragraph">are going to be just calling on one of my custom functions that will input a number from the user</p>



<p class="wp-block-paragraph">so the user can type at the terminal and we will receive their number through REX which is the</p>



<p class="wp-block-paragraph">return value register for integer return values and we&#8217;re going to store it in R12. So that&#8217;s why</p>



<p class="wp-block-paragraph">And we&#8217;re going to store it in R12.</p>



<p class="wp-block-paragraph">So that&#8217;s why I had to preserve R12,</p>



<p class="wp-block-paragraph">because I&#8217;m actually going to start,</p>



<p class="wp-block-paragraph">you know, messing with the value of R12 now.</p>



<p class="wp-block-paragraph">Okay, so we ask for a number,</p>



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



<p class="wp-block-paragraph">If we run the program again,</p>



<p class="wp-block-paragraph">it&#8217;s just asking for a number,</p>



<p class="wp-block-paragraph">and I can just like type some stuff and hit enter,</p>



<p class="wp-block-paragraph">and then the program ends, nothing actually happens.</p>



<p class="wp-block-paragraph">We&#8217;re building, we&#8217;re building.</p>



<p class="wp-block-paragraph">So now let&#8217;s ask ourselves,</p>



<p class="wp-block-paragraph">is the number greater than zero?</p>



<p class="wp-block-paragraph">So let me copy paste some code here.</p>



<p class="wp-block-paragraph">some code here so right now we&#8217;ve just finished inputting a number from the user and we&#8217;ll ask</p>



<p class="wp-block-paragraph">you know is that number that they inputted greater than zero so again the first thing we do is we use</p>



<p class="wp-block-paragraph">a compare instruction cmp we give it two operands the order doesn&#8217;t necessarily matter um for</p>



<p class="wp-block-paragraph">instance uh right now i&#8217;m asking is r12 greater than zero so i can do a jump less than or equal</p>



<p class="wp-block-paragraph">to like a different branch or i can jump greater than equal to if i switch the operands or if i</p>



<p class="wp-block-paragraph">in a different order, but I&#8217;m just choosing to say, let&#8217;s compare the two and then let&#8217;s jump</p>



<p class="wp-block-paragraph">if R12 is less than or equal to, which basically means not greater than, this label right here,</p>



<p class="wp-block-paragraph">which is just branch test after greater test. So I&#8217;m going to do a print statement right after that</p>



<p class="wp-block-paragraph">and then I&#8217;m going to draw out what the code is kind of doing. So let me make a label here so</p>



<p class="wp-block-paragraph">actually will compile. Whoops. I&#8217;m in the wrong window. Okay. There we go. So what am I saying</p>



<p class="wp-block-paragraph">here? Okay. Do the comparison. And then if the condition seems to be satisfied, then we&#8217;ll jump</p>



<p class="wp-block-paragraph">to this, which means we&#8217;ll just say goodbye and then exit the program because we&#8217;ll return to the</p>



<p class="wp-block-paragraph">caller. But if that condition was not satisfied, then we&#8217;re going to end up executing this stuff</p>



<p class="wp-block-paragraph">in the middle. So think about this. This is kind of the idea. Let&#8217;s see if I can remember how to</p>



<p class="wp-block-paragraph">idea let&#8217;s see if I can remember how to draw this right now off the top of my head suppose we have</p>



<p class="wp-block-paragraph">like some sort of an input I&#8217;ll call this in put and then maybe the input is going to be you know</p>



<p class="wp-block-paragraph">whether r12 and r0 are equal or greater than or whatever so I&#8217;m just gonna say r12 and 0</p>



<p class="wp-block-paragraph">god that&#8217;s awful I really need to practice this draw pad I spend like a whole week practicing</p>



<p class="wp-block-paragraph">whole week practicing with a different draw pad and then when I come back to</p>



<p class="wp-block-paragraph">this one I&#8217;m even worse than when I started so we we come in and we kind of</p>



<p class="wp-block-paragraph">look at our 12 versus zero and then we will jump in one direction if that&#8217;s not</p>



<p class="wp-block-paragraph">you know greater than or equal to or less than or equal to and we&#8217;ll jump in</p>



<p class="wp-block-paragraph">another direction otherwise so basically here we&#8217;ll say jump less than equal to</p>



<p class="wp-block-paragraph">um less than or equal to whoops maybe i&#8217;ll do r12 uh i need to somehow adjust the pressure on this</p>



<p class="wp-block-paragraph">thing r12 less than or equal to and so this is sort of uh has to do with you know this branching</p>



<p class="wp-block-paragraph">path that we take like if r12 was less than or equal to zero then we&#8217;ll take the right branching</p>



<p class="wp-block-paragraph">to after, I&#8217;ll just put the word after,</p>



<p class="wp-block-paragraph">we&#8217;ll jump to the after greater test sub label.</p>



<p class="wp-block-paragraph">And otherwise, if R12 is, sorry,</p>



<p class="wp-block-paragraph">I said less than or equal to, yeah,</p>



<p class="wp-block-paragraph">if R12 is greater than zero,</p>



<p class="wp-block-paragraph">then we jump to this other thing,</p>



<p class="wp-block-paragraph">which is gonna be the fall through.</p>



<p class="wp-block-paragraph">So it&#8217;s gonna be line 96, but I&#8217;ll just say,</p>



<p class="wp-block-paragraph">I&#8217;ll say fall for fall through.</p>



<p class="wp-block-paragraph">basically the jump won&#8217;t happen if it&#8217;s greater than or equal to so you could imagine if you</p>



<p class="wp-block-paragraph">wanted to reverse the logic so you can use a jump greater than instruction you could but I&#8217;m choosing</p>



<p class="wp-block-paragraph">to do it this way and then both of those will eventually reach let&#8217;s see will eventually</p>



<p class="wp-block-paragraph">actually yeah the fall through instructions those will eventually reach the after instruction I&#8217;ll</p>



<p class="wp-block-paragraph">here and then the after instruction will just kind of like finish the program</p>



<p class="wp-block-paragraph">and exit and all that stuff. So if you think about the control path here,</p>



<p class="wp-block-paragraph">we have an input we&#8217;re looking at R12 with the zero with the compare instruction</p>



<p class="wp-block-paragraph">and then we use a conditional branching instruction. So maybe I should write,</p>



<p class="wp-block-paragraph">what do you want to write here? Maybe I&#8217;ll just put JLE up here because usually the compare</p>



<p class="wp-block-paragraph">usually the compare instruction and the conditional jump or branching instructions</p>



<p class="wp-block-paragraph">kind of you know come as a pair so both of these combined mean let&#8217;s look at r12 versus zero and</p>



<p class="wp-block-paragraph">if r12 is less than or equal to zero meaning if it will jump if we&#8217;re less than or equal to the</p>



<p class="wp-block-paragraph">the right operand r12 less than equal to the right operand then we go down this path on the right</p>



<p class="wp-block-paragraph">to the after label if that is false meaning r12 was greater than zero then we&#8217;ll fall through</p>



<p class="wp-block-paragraph">this stuff right here and then whether or not we actually fall through will always end up at the</p>



<p class="wp-block-paragraph">at the after area because we&#8217;re either going to jump directly to the after area or we&#8217;re going</p>



<p class="wp-block-paragraph">to fall through and then the fall through falls through to the after area anyway as well let me</p>



<p class="wp-block-paragraph">just show you that real fast oh man i&#8217;m having a hard time with this okay so let&#8217;s pretend that r12</p>



<p class="wp-block-paragraph">less than or equal to. So that means instructions are coming, you know,</p>



<p class="wp-block-paragraph">instructions are getting executed. We&#8217;re going down and down and down and down and down.</p>



<p class="wp-block-paragraph">Once we see jump less than or equal to, if that&#8217;s true, then we jump to the after label,</p>



<p class="wp-block-paragraph">which means we just kind of jump around this message. And so we&#8217;re not going to exit,</p>



<p class="wp-block-paragraph">sorry, we&#8217;re not going to execute that code. So in the case where that is true,</p>



<p class="wp-block-paragraph">where it is less than or equal to, we&#8217;re not going to execute those instructions.</p>



<p class="wp-block-paragraph">So we&#8217;re not going to say their number was greater than zero. We&#8217;re just going to simply</p>



<p class="wp-block-paragraph">We&#8217;re just going to simply say goodbye.</p>



<p class="wp-block-paragraph">However, on the other hand, if that is not true, meaning if R12 was indeed greater than</p>



<p class="wp-block-paragraph">zero, then we&#8217;re going to fall through because this jump less than or equal to instruction,</p>



<p class="wp-block-paragraph">it will only jump if the R flags, if the comparison instruction thought that R12 was less than</p>



<p class="wp-block-paragraph">or equal to zero.</p>



<p class="wp-block-paragraph">If that&#8217;s not true, if it&#8217;s greater than, then instructions, the control path is just</p>



<p class="wp-block-paragraph">the control path is just going to fall through.</p>



<p class="wp-block-paragraph">So that means we will actually execute these.</p>



<p class="wp-block-paragraph">And then when they&#8217;re done,</p>



<p class="wp-block-paragraph">execution will continue to fall through to the rest.</p>



<p class="wp-block-paragraph">So that means we&#8217;re either going to see a message saying</p>



<p class="wp-block-paragraph">their number was greater than zero or not,</p>



<p class="wp-block-paragraph">based on whether their number actually was greater than zero.</p>



<p class="wp-block-paragraph">Okay, hopefully I explained that somewhat clearly.</p>



<p class="wp-block-paragraph">Now let&#8217;s run the code and see if it works.</p>



<p class="wp-block-paragraph">Hopefully I didn&#8217;t forget anything.</p>



<p class="wp-block-paragraph">This is kind of a partial program at this point.</p>



<p class="wp-block-paragraph">program at this point I&#8217;m gonna add a bunch more stuff to it so hopefully</p>



<p class="wp-block-paragraph">this actually compiles enter an integer five notice how it says your number was</p>



<p class="wp-block-paragraph">greater than zero and if I run it again and I just type zero it does not say</p>



<p class="wp-block-paragraph">that it was greater than zero if I run it again I type like a negative five it</p>



<p class="wp-block-paragraph">also does not say that it was greater than zero you know a huge giant number</p>



<p class="wp-block-paragraph">here it&#8217;ll say your number was greater than zero you could probably type a</p>



<p class="wp-block-paragraph">number that was just absolutely huge and have it overflow I actually don&#8217;t know</p>



<p class="wp-block-paragraph">nines I need to type and I don&#8217;t even know what the behavior is going to be.</p>



<p class="wp-block-paragraph">I don&#8217;t know if it&#8217;s just going to crash the program because this also relies on my ability</p>



<p class="wp-block-paragraph">to input a number which is happening in a different library.</p>



<p class="wp-block-paragraph">Let&#8217;s see if that even works greater than zero.</p>



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



<p class="wp-block-paragraph">It probably overflowed and went to a very, very low negative number.</p>



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



<p class="wp-block-paragraph">I guess at least I know my library will probably work sort of.</p>



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



<p class="wp-block-paragraph">So let&#8217;s go back to the code here and let&#8217;s upgrade it.</p>



<p class="wp-block-paragraph">how to to check for jump less than or equal to um we&#8217;re going to say goodbye right there so now the</p>



<p class="wp-block-paragraph">next thing we should do is check to see if the number was less than 10. so after the greater test</p>



<p class="wp-block-paragraph">it&#8217;s going to be here so again the same thing like we can just reuse r12 because we&#8217;re not really</p>



<p class="wp-block-paragraph">modifying it and we&#8217;re not calling uh well i guess even if we were calling a function r12 was supposed</p>



<p class="wp-block-paragraph">supposed to be Kali preserved so we&#8217;re just comparing it and then maybe</p>



<p class="wp-block-paragraph">printing a message if we want to and regardless we&#8217;re going to end up at 103</p>



<p class="wp-block-paragraph">this after greater test label so then we&#8217;ll compare again r12 with the number</p>



<p class="wp-block-paragraph">10 and we&#8217;ll say jump if it&#8217;s greater than or equal to branch test after less</p>



<p class="wp-block-paragraph">so now at this point we&#8217;re saying compare r12 with 10 if it&#8217;s greater than or equal to 10 then</p>



<p class="wp-block-paragraph">just skip over the message so if it&#8217;s greater than or equal to 10 then wait a minute wait wait</p>



<p class="wp-block-paragraph">yeah yeah if it&#8217;s greater than or equal to 10 then skip the message otherwise the message</p>



<p class="wp-block-paragraph">that is printed is going to say that their number is less than 10. so again you could imagine a</p>



<p class="wp-block-paragraph">want me to write it out for you I don&#8217;t know I think once is probably enough but let me just do</p>



<p class="wp-block-paragraph">it again we&#8217;ll do input is coming in and it&#8217;s just going to be you know comparing and then</p>



<p class="wp-block-paragraph">jumping greater than or equal to the input is going to be where we&#8217;re just looking to see if</p>



<p class="wp-block-paragraph">R12 how it compares to zero god the number one is awful my entire penmanship is awful okay one</p>



<p class="wp-block-paragraph">we&#8217;ll do r12 and 10 and so that&#8217;s just sort of what we&#8217;re comparing with the compare instruction</p>



<p class="wp-block-paragraph">and then the jump instruction and so if we go on the left i&#8217;m going to say that&#8217;s the fall through</p>



<p class="wp-block-paragraph">path again and if we go to the right it&#8217;s going to be um r12 greater than or equal to 10 is going</p>



<p class="wp-block-paragraph">and then here this is going to be after less I&#8217;ll say the after label the after less label</p>



<p class="wp-block-paragraph">so if we fall through it&#8217;s just going to print a message maybe I should do another little bubble</p>



<p class="wp-block-paragraph">here that&#8217;s just like print p for how about p for print p for print it&#8217;s going to fall through and</p>



<p class="wp-block-paragraph">then it&#8217;s going to print and then it&#8217;s going to go to the after label so you can see that</p>



<p class="wp-block-paragraph">the print instructions are only reached if r12 is not greater than equal to 10 which is the same</p>



<p class="wp-block-paragraph">to 10 which is the same thing as saying it&#8217;s going to fall through if r12 is less than</p>



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



<p class="wp-block-paragraph">And then after we finally get to the after label, then we&#8217;re just going to do, you know,</p>



<p class="wp-block-paragraph">the rest of the instructions.</p>



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



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



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



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



<p class="wp-block-paragraph">So say there never was less than 10.</p>



<p class="wp-block-paragraph">And then regardless, here&#8217;s like the ending label that we were conditionally jumping to.</p>



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



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



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



<p class="wp-block-paragraph">we&#8217;ll enter five notice how it says your number was greater than zero and your</p>



<p class="wp-block-paragraph">number was less than ten let&#8217;s do an 11 I guess it says your number was greater</p>



<p class="wp-block-paragraph">than zero but it does not say that our number was less than ten because 11 is</p>



<p class="wp-block-paragraph">not less than ten if I do a negative three here it says that our number was</p>



<p class="wp-block-paragraph">less than ten but it doesn&#8217;t say that it&#8217;s greater than zero so we have like a</p>



<p class="wp-block-paragraph">lot of decisions that we could make right like imagine if instead of just</p>



<p class="wp-block-paragraph">based on something happening, you could call a full function or skip a bunch of extra code,</p>



<p class="wp-block-paragraph">you know, whatever you want it to do.</p>



<p class="wp-block-paragraph">So I&#8217;m going to go to the next condition where we&#8217;re going to compare R12 to the number five.</p>



<p class="wp-block-paragraph">And we&#8217;re going to ask, you know, is R12 equal to five?</p>



<p class="wp-block-paragraph">So this is going to be the jump not equal branch.</p>



<p class="wp-block-paragraph">And again, you know, if you wanted to say jump, if it&#8217;s equal to the place where we</p>



<p class="wp-block-paragraph">and then maybe like right after that fall through and just do an unconditional jump you can</p>



<p class="wp-block-paragraph">I just think there are less jumps if I do it this way so uh here we&#8217;re just going to print you know</p>



<p class="wp-block-paragraph">your number was equal to five and so basically if it was not equal we should have another label here</p>



<p class="wp-block-paragraph">that allows us to skip that so all right so same thing I&#8217;m not going to draw the diagram this time</p>



<p class="wp-block-paragraph">R12 with 5 and if it&#8217;s not equal to 5 then jump to this label down here which just means</p>



<p class="wp-block-paragraph">let&#8217;s say goodbye and not actually say that their number was equal to 5.</p>



<p class="wp-block-paragraph">But if it was equal to 5 then this JNE conditional branch won&#8217;t actually jump anywhere.</p>



<p class="wp-block-paragraph">Execution will fall through to the next statements which is just printing the message that their</p>



<p class="wp-block-paragraph">number is equal to 5 and then when that&#8217;s done it&#8217;ll fall through to this other label</p>



<p class="wp-block-paragraph">and eventually fall through to the return statement.</p>



<p class="wp-block-paragraph">the whole thing already nice that was easy um let&#8217;s do another run and um let&#8217;s enter the</p>



<p class="wp-block-paragraph">integer one it says one is uh greater than zero and one is less than 10</p>



<p class="wp-block-paragraph">greater than zero and less than 10 is there any way i can get nothing to print out</p>



<p class="wp-block-paragraph">greater than zero and less than 10</p>



<p class="wp-block-paragraph">you don&#8217;t think i can do it let me try it nine no</p>



<p class="wp-block-paragraph">why did I even put 9 okay yeah it&#8217;s always gonna say 0 or 10 okay so let&#8217;s</p>



<p class="wp-block-paragraph">do a 4 we&#8217;ll get both of those greater than 0 and also less than 10 let&#8217;s do a</p>



<p class="wp-block-paragraph">5 now we should get all those messages your number was greater than 0 your</p>



<p class="wp-block-paragraph">number was less than 10 your number was equal to 5 and if I just do a 6 in there</p>



<p class="wp-block-paragraph">again it doesn&#8217;t print your number was equal to 5 and we have to enter an</p>



<p class="wp-block-paragraph">actual 5 for that so that&#8217;s the that&#8217;s the basics of</p>



<p class="wp-block-paragraph">conditional branching oh there&#8217;s one other thing that I wanted to make sure</p>



<p class="wp-block-paragraph">in my previous video where we only talked about jumps bear in mind that</p>



<p class="wp-block-paragraph">these conditional branching statements or instructions you know je j any all</p>



<p class="wp-block-paragraph">these things they have a limited range of jumps of where they can jump to so if</p>



<p class="wp-block-paragraph">you have a gigantic program and you&#8217;re trying to jump very very very far away</p>



<p class="wp-block-paragraph">based on a condition you might have an assembler error where your program won&#8217;t</p>



<p class="wp-block-paragraph">even compile won&#8217;t even assemble telling you i think the message is i wrote it</p>



<p class="wp-block-paragraph">I wrote it down somewhere jump out of range coming from the assembler that basically means that these</p>



<p class="wp-block-paragraph">instructions can only jump about 128 bytes away from whatever instruction they are at or whatever</p>



<p class="wp-block-paragraph">memory location they are at so you know after your assembler assembles your program and after</p>



<p class="wp-block-paragraph">your linker links your program and you just have like a binary you know if you were to inspect</p>



<p class="wp-block-paragraph">all the instructions inside of your executable your binary you would see that they have offsets</p>



<p class="wp-block-paragraph">offsets, right? You know, like one instruction is basically eight bytes away from the one that</p>



<p class="wp-block-paragraph">comes right after it. But some instructions are a lot further away. So if you&#8217;re trying to jump</p>



<p class="wp-block-paragraph">to an instruction that is 100 is more than 128 bytes away, it won&#8217;t work, you&#8217;ll get that error.</p>



<p class="wp-block-paragraph">So how do you solve that problem? Well, maybe I&#8217;ll just write this down real fast. I&#8217;m not going to</p>



<p class="wp-block-paragraph">make this part of the code example. But just imagine we have, you know, jump equal to some</p>



<p class="wp-block-paragraph">and maybe I&#8217;ll do a comment very very far away and here&#8217;s the label some label right</p>



<p class="wp-block-paragraph">so if your assembler says hey you can&#8217;t jump to some label because it&#8217;s too far away it&#8217;s a</p>



<p class="wp-block-paragraph">jump out of range assembler error then basically you can just make some other labels here you can</p>



<p class="wp-block-paragraph">say uh you can say short jump true and short jump false you know just make up some sort of a label</p>



<p class="wp-block-paragraph">label and basically say if it is um well false yeah okay i guess this is the way i&#8217;ll do it this</p>



<p class="wp-block-paragraph">is not super efficient but instead of jumping to the very far away label in the case of true you</p>



<p class="wp-block-paragraph">can just jump to the true label so i&#8217;m going to say jump uh to the short jump true label otherwise</p>



<p class="wp-block-paragraph">To the false label. So now you&#8217;re definitely jumping in either case and both of these labels are close by</p>



<p class="wp-block-paragraph">So then I can pretty much just have the close by true label do an unconditional</p>



<p class="wp-block-paragraph">Jump to some label and that overcomes the limitation of branching instructions</p>



<p class="wp-block-paragraph">So, you know JMP the regular jump instruction</p>



<p class="wp-block-paragraph">It has an unlimited jump that it can do it can go just to like very very very far away instructions</p>



<p class="wp-block-paragraph">And then for the false, you know, I don&#8217;t know, do whatever you want and then jump wherever you want.</p>



<p class="wp-block-paragraph">It doesn&#8217;t really matter.</p>



<p class="wp-block-paragraph">I&#8217;m just trying to show you that if you make a short jump to a label that will only have an unconditional jump,</p>



<p class="wp-block-paragraph">so it&#8217;s sort of like two jumps, you know, like a short jump and then a long jump,</p>



<p class="wp-block-paragraph">then you can overcome that limitation.</p>



<p class="wp-block-paragraph">Again, bearing in mind that some label is supposed to be very, very far away and sort of unreachable.</p>



<p class="wp-block-paragraph">Okay, so let me just double check that I talked about everything that I wanted to.</p>



<p class="wp-block-paragraph">We looked at the branching instructions, the sign in this, I drew a diagram for you.</p>



<p class="wp-block-paragraph">We solved the jump out of range error.</p>



<p class="wp-block-paragraph">Okay, so that&#8217;s, I think that&#8217;s basically it.</p>



<p class="wp-block-paragraph">Hopefully you feel like an expert now at conditional branching and how to make decisions.</p>



<p class="wp-block-paragraph">In future videos, I&#8217;ll talk about how to implement an if else block and how to implement loops</p>



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



<p class="wp-block-paragraph">decisions in yasm x86-64 assembly and conceptually you can apply this to other assembly languages too</p>



<p class="wp-block-paragraph">so thanks for watching my video i hope you learned a little bit of stuff and had a little bit of fun</p>



<p class="wp-block-paragraph">i&#8217;ll see you in the next video</p>



<p class="wp-block-paragraph">hey everybody 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">Could you please subscribe and follow this channel or these videos or whatever it is you do on the current social media website that you&#8217;re looking at right now?</p>



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



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



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



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



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



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



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



<p class="wp-block-paragraph">you can scan in order to go to the website which I think is also named</p>



<p class="wp-block-paragraph">somewhere at the bottom of this video and it&#8217;ll take you to my main website</p>



<p class="wp-block-paragraph">where you can just kind of like see all the videos I published and the</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">good stuff 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">appreciate it so again thank you so much for watching this video and enjoy the cool music</p>



<p class="wp-block-paragraph">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/learn-conditional-branching-in-x86-64-assembly-yasm-nasm-cmp-je-jne-jl-jg-more/">Learn Conditional Branching in x86-64 Assembly (YASM/NASM) &#8211; CMP, JE, JNE, JL, JG &amp; More</a> appeared first on <a href="https://www.NeuralLantern.com">NeuralLantern.com</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.NeuralLantern.com/learn-conditional-branching-in-x86-64-assembly-yasm-nasm-cmp-je-jne-jl-jg-more/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Complex If-ElseIf-Else in x86-64 YASM Assembly &#8211; Full Guide with Code Examples</title>
		<link>https://www.NeuralLantern.com/complex-if-elseif-else-in-x86-64-yasm-assembly-full-guide-with-code-examples/</link>
					<comments>https://www.NeuralLantern.com/complex-if-elseif-else-in-x86-64-yasm-assembly-full-guide-with-code-examples/#respond</comments>
		
		<dc:creator><![CDATA[mike]]></dc:creator>
		<pubDate>Sun, 04 Jan 2026 18:55: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 if statement]]></category>
		<category><![CDATA[assembly language tutorial]]></category>
		<category><![CDATA[assembly programming]]></category>
		<category><![CDATA[chained if else assembly]]></category>
		<category><![CDATA[cmp instruction]]></category>
		<category><![CDATA[conditional branching assembly]]></category>
		<category><![CDATA[control flow assembly]]></category>
		<category><![CDATA[if else assembly]]></category>
		<category><![CDATA[intel assembly]]></category>
		<category><![CDATA[je jmp assembly]]></category>
		<category><![CDATA[low level programming]]></category>
		<category><![CDATA[nasm assembly]]></category>
		<category><![CDATA[x86 assembly tutorial]]></category>
		<category><![CDATA[x86-64 assembly]]></category>
		<category><![CDATA[Yasm assembly]]></category>
		<guid isPermaLink="false">https://www.NeuralLantern.com/?p=275</guid>

					<description><![CDATA[<p>Step-by-step x86-64 YASM assembly tutorial showing how to implement complex if-else and if-elseif-else control flow structures with multiple chained conditions, proper labels, conditional jumps, and fall-through logic. Includes complete working code and live execution.</p>
<p>The post <a href="https://www.NeuralLantern.com/complex-if-elseif-else-in-x86-64-yasm-assembly-full-guide-with-code-examples/">Complex If-ElseIf-Else in x86-64 YASM Assembly &#8211; Full Guide with Code Examples</a> appeared first on <a href="https://www.NeuralLantern.com">NeuralLantern.com</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<figure class="wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio"><div class="wp-block-embed__wrapper">
<iframe title="Complex If-ElseIf-Else in x86-64 YASM Assembly - Full Guide with Code Examples" width="1380" height="776" src="https://www.youtube.com/embed/G7am7avKi9w?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 in-depth x86-64 assembly tutorial using YASM, we dive deep into implementing complex if-else and if-elseif-else control structures from scratch. Starting with the fundamentals of conditional branching, we build up to full chained if-elseif-else blocks with multiple conditions &#8211; exactly how high-level languages handle them under the hood.</p>



<p class="wp-block-paragraph">You&#8217;ll see real working code that:</p>



<ul class="wp-block-list">
<li>Takes user integer input</li>



<li>Tests against specific values (5, 6, etc.)</li>



<li>Handles greater-than/less-than comparisons</li>



<li>Properly branches so only one block executes</li>



<li>Uses labels, cmp, conditional jumps (je, jl), and unconditional jumps (jmp) correctly</li>
</ul>



<p class="wp-block-paragraph">We cover the classic pattern: compare to conditional jump to true block to execute true code to jmp to end to false block falls through or jumps in. Everything is shown step-by-step with live compilation and runtime demos.</p>



<p class="wp-block-paragraph">Perfect for anyone learning low-level programming, reverse engineering, or wanting to understand how compilers translate if-else chains into machine code. Prerequisites: basic conditional jumps (see my earlier videos).</p>



<p class="wp-block-paragraph">Code shown works on Linux x86-64 with YASM/NASM syntax. Grab the concepts and apply them anywhere.</p>



<p class="wp-block-paragraph">Introduction to If-Else in Assembly 00:00:00<br>Explaining the If-Else Design Pattern 00:00:56<br>Drawing the Basic If-Else Flow 00:01:01<br>Comparison and Conditional Jumps 00:02:30<br>Labels for True and False Blocks 00:03:07<br>Unconditional Jump to End 00:04:50<br>Diagram of Execution Flow 00:05:51<br>Alternative Pattern with Inverted Jump 00:07:00<br>Recapping the If-Else Pattern 00:08:45<br>Starting the Code Example 00:09:16<br>Setting Up Input and Strings 00:09:40<br>Calling External Functions 00:10:57<br>Entry Point and Prologue 00:11:40<br>Asking User for Integer Input 00:13:09<br>Creating the if_test Function 00:14:56<br>Preserving Callee-Saved Registers 00:15:51<br>Printing Begin Message 00:17:03<br>Implementing Simple If Block 00:18:29<br>Comparison and je Jump 00:19:18<br>True Block: Equality Message 00:21:08<br>Testing Simple If Examples 00:23:48<br>Transition to If-Else Blocks 00:24:21<br>Creating if_else_test Function 00:24:47<br>Setting Up Complex If-Else 00:26:25<br>First If: Equal to 5 00:27:22<br>True Block for Equal 5 00:28:33<br>Else If: Equal to 6 00:30:29<br>Else If: Less Than 10 00:34:17<br>Final Else Block 00:37:33<br>Done Label and Goodbye 00:38:23<br>Recap of Full Flow 00:39:06<br>Live Demo of All Branches 00:40:54<br>Signed vs Unsigned Jumps Note 00:43:38<br>Recommended Assembly Book 00:44:12<br>Conditional Jump Families 00:45:05<br>Closing and Practice Advice 00:46: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">Hello there.</p>



<p class="wp-block-paragraph">In this video we&#8217;re going to talk about implementing simple if-else blocks in YASM x86-64 assembly.</p>



<p class="wp-block-paragraph">Although if you&#8217;re writing in a different assembly language, this video will probably</p>



<p class="wp-block-paragraph">still be useful to you because I&#8217;m going to explain the design pattern or how we can achieve</p>



<p class="wp-block-paragraph">that at the assembly level.</p>



<p class="wp-block-paragraph">So x86-64 YASM assembly, also known as AMD64 YASM assembly.</p>



<p class="wp-block-paragraph">So if you have not seen my other videos about how to do conditional branching in the first</p>



<p class="wp-block-paragraph">place, you probably want to go check that first because that knowledge is required for</p>



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



<p class="wp-block-paragraph">There&#8217;s also a lot of other stuff that I&#8217;m just not going to explain in this video, such</p>



<p class="wp-block-paragraph">as creating a make file, you know, compiling and linking your executable and so forth.</p>



<p class="wp-block-paragraph">So see my other videos where all those concepts are explained already.</p>



<p class="wp-block-paragraph">how to implement a simple if-else block.</p>



<p class="wp-block-paragraph">So for starters, maybe let me draw a little bit.</p>



<p class="wp-block-paragraph">Suppose we had, let me get rid of this thing</p>



<p class="wp-block-paragraph">and then I&#8217;ll just do a regular notepad.</p>



<p class="wp-block-paragraph">Suppose we had a higher level language,</p>



<p class="wp-block-paragraph">just so you know what I&#8217;m talking about.</p>



<p class="wp-block-paragraph">Suppose we had a higher level language</p>



<p class="wp-block-paragraph">and we wanted to say if, you know, some expression is true.</p>



<p class="wp-block-paragraph">We wanted to say if expression is true,</p>



<p class="wp-block-paragraph">then print, you know, it was true.</p>



<p class="wp-block-paragraph">Anybody remember that old movie, Little Nicky?</p>



<p class="wp-block-paragraph">Somebody got exploded and then the guy next to him goes it&#8217;s not true</p>



<p class="wp-block-paragraph">Okay, it was not true</p>



<p class="wp-block-paragraph">So this is the basic idea of what we&#8217;re going to implement in assembly</p>



<p class="wp-block-paragraph">I&#8217;m going to write a full program for assembly to show you this but um</p>



<p class="wp-block-paragraph">You know you&#8217;re in C++ you&#8217;re in C you&#8217;re in I don&#8217;t know whatever language you&#8217;re in and</p>



<p class="wp-block-paragraph">You obviously know how to use if-else blocks at this point hopefully</p>



<p class="wp-block-paragraph">And now we&#8217;re going to just try to figure out how to implement them in assembly</p>



<p class="wp-block-paragraph">So it&#8217;s important to understand that really under the hood</p>



<p class="wp-block-paragraph">really under the hood there&#8217;s a bunch of stuff happening surprise right okay so</p>



<p class="wp-block-paragraph">first off we look at this expression which could be I don&#8217;t know let&#8217;s say</p>



<p class="wp-block-paragraph">five is greater than ten or a is equal to B or whatever it is that you put in</p>



<p class="wp-block-paragraph">there you could make a very complicated expression we&#8217;re going to use simple</p>



<p class="wp-block-paragraph">expressions for this video so if we&#8217;re comparing a to B we&#8217;ll end up using the</p>



<p class="wp-block-paragraph">compare instruction remember there are two steps to conditional branching and</p>



<p class="wp-block-paragraph">in YASM we first use the compare instruction against two operands and then that will end up</p>



<p class="wp-block-paragraph">filling up the rflags register so that we can later conditionally jump based on the results</p>



<p class="wp-block-paragraph">of the comparison. So we do a comparison and then you can imagine that the beginning of this</p>



<p class="wp-block-paragraph">you know the the true body notice how it has a scope I&#8217;m going to just put this brace on another</p>



<p class="wp-block-paragraph">line here to indicate that there is a scope from line four to six indicating all this code in here</p>



<p class="wp-block-paragraph">in here is executed only if the if statement was true and then all of this other stuff is executed</p>



<p class="wp-block-paragraph">only if the original comparison was false right so you can imagine now label is something like</p>



<p class="wp-block-paragraph">my if was true so we can make a label for where that body starts and another label for where the</p>



<p class="wp-block-paragraph">label is something like if was false and then what will happen is we can use a</p>



<p class="wp-block-paragraph">conditional jump instruction after we do our comparison maybe I should put to</p>



<p class="wp-block-paragraph">compare right here oh I&#8217;m using I&#8217;m using assembly style comments I should</p>



<p class="wp-block-paragraph">be using a C style comments if I&#8217;m actually writing C++ here let me just do</p>



<p class="wp-block-paragraph">that okay so this is the comparison instruction here and then here&#8217;s the</p>



<p class="wp-block-paragraph">here and then here&#8217;s the beginning of the body of stuff to execute if it was true you can imagine</p>



<p class="wp-block-paragraph">that there could be many statements here um if it was true or if it was false</p>



<p class="wp-block-paragraph">just to prove to you that we can execute like a full body of stuff so we have a label that</p>



<p class="wp-block-paragraph">designates when that body starts and then we have another label designating when the else body</p>



<p class="wp-block-paragraph">starts and then we should have a label that uh that designates when the whole thing is over</p>



<p class="wp-block-paragraph">label is something like if and or if is done or something like that so basically what we want to</p>



<p class="wp-block-paragraph">do is to implement an if else a simple if else in assembly we&#8217;re going to say let&#8217;s do a comparison</p>



<p class="wp-block-paragraph">and then we&#8217;ll do a conditional jump where are we going to conditionally jump well if the comparison</p>



<p class="wp-block-paragraph">was true label. And then what will happen is execution will fall through.</p>



<p class="wp-block-paragraph">When it reaches the end, we want to have another jump statement that unconditionally jumps</p>



<p class="wp-block-paragraph">to the end of the if statement. If we didn&#8217;t, then whenever the expression was true,</p>



<p class="wp-block-paragraph">we would end up executing all of the true statements and then it would fall through</p>



<p class="wp-block-paragraph">to all of the false statements or the not true statements. So we have to have many jumps in here</p>



<p class="wp-block-paragraph">let&#8217;s get into the body and then let&#8217;s finish and jump out of the body on the other hand if the</p>



<p class="wp-block-paragraph">expression was false then our jump instruction is going to jump you know over the true body so</p>



<p class="wp-block-paragraph">it&#8217;s not even going to do that at all it&#8217;s going to execute all of the else stuff and then it&#8217;s</p>



<p class="wp-block-paragraph">you know sometimes a good idea if you have another jump here that just jumps to the end but you can</p>



<p class="wp-block-paragraph">kind of see by the way i&#8217;ve written this out that there&#8217;s not going to be anything between the end</p>



<p class="wp-block-paragraph">you know place where we&#8217;re finished with everything which means we don&#8217;t really</p>



<p class="wp-block-paragraph">need an unconditional jump at the end of the else body we can just let the</p>



<p class="wp-block-paragraph">execution fall through so maybe if I can draw a little diagram here I&#8217;ll say I</p>



<p class="wp-block-paragraph">don&#8217;t know I&#8217;ll do like if put it in a little bubble and we&#8217;ll say that if it</p>



<p class="wp-block-paragraph">was false we jump to one place and if it was true we jump to another place</p>



<p class="wp-block-paragraph">we jump to another place.</p>



<p class="wp-block-paragraph">I hope you&#8217;ve already started to understand this by now.</p>



<p class="wp-block-paragraph">So we&#8217;ll say if the expression is true, we jump here.</p>



<p class="wp-block-paragraph">If the expression is false, we jump over here.</p>



<p class="wp-block-paragraph">And so true would be saying, let&#8217;s jump to the if was true.</p>



<p class="wp-block-paragraph">So we&#8217;re going to jump to if was,</p>



<p class="wp-block-paragraph">I&#8217;ll put a T there because I&#8217;m running out of space.</p>



<p class="wp-block-paragraph">If was true.</p>



<p class="wp-block-paragraph">was I&#8217;ll put an F false and then at the end of the true we jump to the done</p>



<p class="wp-block-paragraph">label so I&#8217;m just gonna put done maybe down here</p>



<p class="wp-block-paragraph">so at the end of the true we just unconditionally jump to the done area</p>



<p class="wp-block-paragraph">and then at the end of the false we jump unconditionally to the done area as well</p>



<p class="wp-block-paragraph">here&#8217;s something interesting though when we have a comparison instruction and</p>



<p class="wp-block-paragraph">conditional branch instruction let&#8217;s say uh let&#8217;s say a equals b we did that comparison and then we</p>



<p class="wp-block-paragraph">wanted to jump into the true area if a equals b was true so that means we&#8217;ll say jump</p>



<p class="wp-block-paragraph">equal so if a is equal to b after we compare well let&#8217;s say a comma b we&#8217;re going to use registers</p>



<p class="wp-block-paragraph">when we come to the code we&#8217;ll say compare a and b and then jump if they&#8217;re equal to some label</p>



<p class="wp-block-paragraph">but the conditional branching instructions they only jump to one</p>



<p class="wp-block-paragraph">potential place or fall through so if the comparison was false meaning if those</p>



<p class="wp-block-paragraph">two things were not equal then we&#8217;re not going to actually be able to jump to a</p>



<p class="wp-block-paragraph">different label we&#8217;re going to simply fall through to the next instruction so</p>



<p class="wp-block-paragraph">in that case the very next instruction would get executed let&#8217;s just put a jump</p>



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



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



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



<p class="wp-block-paragraph">we compare A and B</p>



<p class="wp-block-paragraph">and if the two things are equal</p>



<p class="wp-block-paragraph">we&#8217;ll jump into the true block.</p>



<p class="wp-block-paragraph">Otherwise we fall through to the next instruction</p>



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



<p class="wp-block-paragraph">that&#8217;s a very poorly written J</p>



<p class="wp-block-paragraph">but we&#8217;re going to unconditionally jump to false.</p>



<p class="wp-block-paragraph">So if we did not</p>



<p class="wp-block-paragraph">jump to the true area we fall through to the next</p>



<p class="wp-block-paragraph">instruction where we will always</p>



<p class="wp-block-paragraph">jump to false and that implements the diagram</p>



<p class="wp-block-paragraph">that implements the diagram that you see up above.</p>



<p class="wp-block-paragraph">I mean, you know, if we have if statement, we compare,</p>



<p class="wp-block-paragraph">maybe I&#8217;ll do, you know, A equals B.</p>



<p class="wp-block-paragraph">We jumped to true, if was true.</p>



<p class="wp-block-paragraph">And otherwise we end up falling through</p>



<p class="wp-block-paragraph">and then jumping to if was false.</p>



<p class="wp-block-paragraph">And then at the end of both of those,</p>



<p class="wp-block-paragraph">we have an unconditional jump instruction</p>



<p class="wp-block-paragraph">that jumps to the done.</p>



<p class="wp-block-paragraph">So I&#8217;ll put JMP down here just to let you know,</p>



<p class="wp-block-paragraph">you know, at the end of each of these blocks,</p>



<p class="wp-block-paragraph">that they are jumping out of themselves at the very end to the done area maybe</p>



<p class="wp-block-paragraph">I&#8217;ll put an arrow here so that we know both of these jumps end up jumping to</p>



<p class="wp-block-paragraph">the done area that&#8217;s the basic idea for how to implement an if-else block a very</p>



<p class="wp-block-paragraph">basic one we&#8217;re gonna do more complicated ones later but for now we</p>



<p class="wp-block-paragraph">kind of have the idea down I think let&#8217;s look at some code dang I blew 10 minutes</p>



<p class="wp-block-paragraph">already on that okay so I&#8217;m gonna copy paste some code from my solution here</p>



<p class="wp-block-paragraph">code for my solution here this again this is not like a beginner&#8217;s assembly</p>



<p class="wp-block-paragraph">video if you need to learn how to write assembly in the first place how to</p>



<p class="wp-block-paragraph">compile you know link create a make file and so forth you need to see my other</p>



<p class="wp-block-paragraph">videos first but for now we&#8217;re just going to assume that you know how to</p>



<p class="wp-block-paragraph">make a data section in Yasm and we&#8217;re gonna say let&#8217;s make a bunch of strings</p>



<p class="wp-block-paragraph">so first I&#8217;m gonna ask the user for an integer and then I&#8217;m gonna make a bunch</p>



<p class="wp-block-paragraph">of decisions like I&#8217;m gonna do you know an if-else block to test what kind of</p>



<p class="wp-block-paragraph">what kind of number they put you know did they make a number that uh</p>



<p class="wp-block-paragraph">equal one if it was equal to something oh did I ask twice I can&#8217;t remember what I&#8217;m doing</p>



<p class="wp-block-paragraph">but basically we&#8217;re going to print something if their number was equal to something else</p>



<p class="wp-block-paragraph">we&#8217;re going to print uh something if their number that they inputted was equal to five we&#8217;re going</p>



<p class="wp-block-paragraph">to print something else if their number was equal to six we&#8217;re going to print again something else</p>



<p class="wp-block-paragraph">a bunch of stuff and then wait isn&#8217;t this the complicated example oh no i think i am using code</p>



<p class="wp-block-paragraph">for my more complicated example else less than 10 i don&#8217;t know maybe this is the simple one</p>



<p class="wp-block-paragraph">let&#8217;s double check i guess if i put more complicated code in here you&#8217;ll probably be happy</p>



<p class="wp-block-paragraph">but whatever i thought this was going to be a simple example so i&#8217;m just defining strings at</p>



<p class="wp-block-paragraph">codes stuff that is covered in other videos now i&#8217;m going to start my text section so my text</p>



<p class="wp-block-paragraph">section begins with a declaration that i&#8217;m going to use two external functions so this video is</p>



<p class="wp-block-paragraph">not about this library that takes input and sends output i have other videos for that but basically</p>



<p class="wp-block-paragraph">i&#8217;m just using a library that lets you type an integer into the terminal and then it will print</p>



<p class="wp-block-paragraph">a different number to the terminal for you so you can imagine if you wanted to follow along</p>



<p class="wp-block-paragraph">imagine if you wanted to follow along with this code at home and you don&#8217;t</p>



<p class="wp-block-paragraph">have this library you can just hard code your numbers just to prove to yourself</p>



<p class="wp-block-paragraph">that you can get it to work or you can use a different library if you have a</p>



<p class="wp-block-paragraph">hybrid program and you&#8217;re linking against GCC you can just use scanf and</p>



<p class="wp-block-paragraph">printf pretty much those take a little bit more work to do but you can do it</p>



<p class="wp-block-paragraph">anyway so our entry point is going to be called if tester it&#8217;s a function called</p>



<p class="wp-block-paragraph">Again, this is a hybrid program and hybrid programs are not covered in this video, but you can imagine that there is a C++ module elsewhere in my source that is just going to call on a function called if tester.</p>



<p class="wp-block-paragraph">And so that&#8217;s why I&#8217;m marking this as global so that other modules in my program can call on if tester.</p>



<p class="wp-block-paragraph">So it&#8217;s just a little label that we can jump into or in this case call into.</p>



<p class="wp-block-paragraph">Let me go down to the bottom of that.</p>



<p class="wp-block-paragraph">We&#8217;ll put a return statement at the very end.</p>



<p class="wp-block-paragraph">very end so now this is officially a function and notice how i made a note here that says r12 is</p>



<p class="wp-block-paragraph">the user&#8217;s inputted integer so that means i&#8217;m going to be using r12 and since r12 is designated</p>



<p class="wp-block-paragraph">in the abi the application binary interface as callee saved or callee preserved that means i</p>



<p class="wp-block-paragraph">have to do a push pop pair to preserve it or i&#8217;ll get in lots and lots of trouble my program will</p>



<p class="wp-block-paragraph">prologue which my favorite book also calls the prologue and then I&#8217;m going to</p>



<p class="wp-block-paragraph">say epilogue and so now we have a function that basically doesn&#8217;t do</p>



<p class="wp-block-paragraph">anything but we can at least jump into it let&#8217;s see if this compiles as is I</p>



<p class="wp-block-paragraph">think it probably will yeah so the driver just says hello that&#8217;s the C++</p>



<p class="wp-block-paragraph">program with the actual main function and it calls my if tester function but</p>



<p class="wp-block-paragraph">nothing happens the if tester function returns control to the driver and then</p>



<p class="wp-block-paragraph">the driver just says okay i got control back so nothing really happened so now let&#8217;s ask the user</p>



<p class="wp-block-paragraph">for some input so again this is not a video about this library or how to print with this with system</p>



<p class="wp-block-paragraph">calls see my other videos if you need help on that but basically we&#8217;re going to print a message to</p>



<p class="wp-block-paragraph">the user hey please input an integer and then we&#8217;re going to call on a library function that</p>



<p class="wp-block-paragraph">lets them type in a number and then returns it to us in rax we&#8217;re then going to store it</p>



<p class="wp-block-paragraph">well just basic stuff right so if I run this again it&#8217;s going to ask for an</p>



<p class="wp-block-paragraph">integer please enter an integer if I can type that and then nothing else happens</p>



<p class="wp-block-paragraph">okay now we can kind of start making more decisions so I&#8217;m going to let&#8217;s see</p>



<p class="wp-block-paragraph">if test if test if test okay so what I need to do now is run another function</p>



<p class="wp-block-paragraph">called if tests several times and I&#8217;m going to compare the number that the</p>



<p class="wp-block-paragraph">times and I&#8217;m going to compare the number that the user inputted so remember r12 is the user&#8217;s</p>



<p class="wp-block-paragraph">input so I&#8217;m going to load that as the first argument of a function call and then I&#8217;m going</p>



<p class="wp-block-paragraph">to load the number five as the second argument of a function call probably not a great idea to hard</p>



<p class="wp-block-paragraph">code numbers in your assembly you should probably define them up in the globals or the the data</p>



<p class="wp-block-paragraph">section at least as just regular defines and not numbers in memory but I&#8217;m going to just keep these</p>



<p class="wp-block-paragraph">So three times we&#8217;re gonna call if test and then also another function called CRLF. So first I&#8217;m gonna paste in</p>



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



<p class="wp-block-paragraph">Where the heck is that? Oh, dude?</p>



<p class="wp-block-paragraph">Again, this is not a video about the basics</p>



<p class="wp-block-paragraph">So you&#8217;re just gonna have to trust me or go watch my other videos if you don&#8217;t understand what I&#8217;m doing here</p>



<p class="wp-block-paragraph">But I have a function called</p>



<p class="wp-block-paragraph">CRLF and its whole job in life is just to print a new line for me just because I like to do it that way</p>



<p class="wp-block-paragraph">handles the CRLF call. Now let&#8217;s make another function called if test.</p>



<p class="wp-block-paragraph">So I&#8217;m going to start that by designating its label right after this block of</p>



<p class="wp-block-paragraph">code here, maybe before CRLF.</p>



<p class="wp-block-paragraph">So we have like a basic if test function and here&#8217;s my prototype just to remind</p>



<p class="wp-block-paragraph">myself what I&#8217;m going to be doing.</p>



<p class="wp-block-paragraph">It&#8217;s going to take an input and it&#8217;s going to take another input for a test</p>



<p class="wp-block-paragraph">against me. So the first one is like the user&#8217;s input.</p>



<p class="wp-block-paragraph">The second one is the number that I want to test it against.</p>



<p class="wp-block-paragraph">number that I want to test it against they&#8217;re both longs which means they are both integers</p>



<p class="wp-block-paragraph">which means the incoming arguments are going to be rdi and rsi if you&#8217;re respecting the abi</p>



<p class="wp-block-paragraph">and then some notes to remind myself I&#8217;m going to be using r12 and r13 inside of this function so</p>



<p class="wp-block-paragraph">I&#8217;m going to start by putting a return statement there since that is what it takes to make a label</p>



<p class="wp-block-paragraph">into a return into into a function then I&#8217;m going to preserve the callee saved registers again if</p>



<p class="wp-block-paragraph">don&#8217;t know what i&#8217;m talking about see my other videos we&#8217;re going to push r12 and push r13 so</p>



<p class="wp-block-paragraph">that they are not ruined for the caller we call this the prolog then at the very bottom of the</p>



<p class="wp-block-paragraph">function we have the epilog which just restores uh the registers in reverse order you&#8217;ve got to</p>



<p class="wp-block-paragraph">do it in reverse order see my other videos if you don&#8217;t understand why okay so that&#8217;s basically a</p>



<p class="wp-block-paragraph">function that can get called it doesn&#8217;t do anything let me double check that the program still actually</p>



<p class="wp-block-paragraph">works. 66 and nothing happens. We just printed CRLF a bunch of times. Okay, so now we&#8217;re ready</p>



<p class="wp-block-paragraph">to continue. So let&#8217;s grab the function arguments. Remember we were going to use R12 and R13 for the</p>



<p class="wp-block-paragraph">user&#8217;s input and the number we will test against. Those came into our function with RDI and RSI.</p>



<p class="wp-block-paragraph">So I&#8217;m just going to copy those two incoming arguments into R12 and R13. And you&#8217;re supposed</p>



<p class="wp-block-paragraph">keep the user&#8217;s input in RDI then the moment I call any other function or system call I&#8217;m just</p>



<p class="wp-block-paragraph">going to lose that data so I&#8217;m going to keep it inside of R12 and R13 so I grab the function</p>



<p class="wp-block-paragraph">arguments and then I print a begin message just to let the user know that we&#8217;re going to start</p>



<p class="wp-block-paragraph">you know making tests against our number so this is just basically a message saying hey begin the</p>



<p class="wp-block-paragraph">if test and then print what we&#8217;re going to check against so the next thing is</p>



<p class="wp-block-paragraph">I&#8217;m going to let the user know what the second incoming argument was.</p>



<p class="wp-block-paragraph">If you look at R13 here, that was RSI, which was the second argument.</p>



<p class="wp-block-paragraph">So the test against me number.</p>



<p class="wp-block-paragraph">So we&#8217;re going to check the user&#8217;s input against whatever we called for the second argument.</p>



<p class="wp-block-paragraph">So I just wanted to print it out.</p>



<p class="wp-block-paragraph">You know, like we&#8217;re testing your number against whatever.</p>



<p class="wp-block-paragraph">So that means I need to make another call to my little printing library here.</p>



<p class="wp-block-paragraph">To RDI which is the first argument notice how we already we already destroyed RDI. That&#8217;s why I&#8217;m keeping the input in our 12 and our 13</p>



<p class="wp-block-paragraph">And then we&#8217;re gonna make that call and we can just assume everything will go according to plan at that point and then</p>



<p class="wp-block-paragraph">Then we&#8217;re gonna print a special message only if something actually happens</p>



<p class="wp-block-paragraph">So we&#8217;re gonna implement the if else block in a moment. Let me just run this real fast</p>



<p class="wp-block-paragraph">and then it says we&#8217;re calling the function three times and we&#8217;re saying the basic test has begun</p>



<p class="wp-block-paragraph">we&#8217;ll test against this number five so we&#8217;re testing your input against five and then six and</p>



<p class="wp-block-paragraph">then seven and then we didn&#8217;t actually do anything we&#8217;re about to okay so then the next thing is we&#8217;re</p>



<p class="wp-block-paragraph">going to print a special message only if the user entered the right number so first off remember we</p>



<p class="wp-block-paragraph">see if else block and converting it into assembly so i&#8217;m sort of placing that in comments for you</p>



<p class="wp-block-paragraph">so the comparison instruction i i didn&#8217;t want to put the r12 equals r13 inside of the same comment</p>



<p class="wp-block-paragraph">that lines up with the the compare instruction because the compare instruction as i&#8217;ve said in</p>



<p class="wp-block-paragraph">other videos already it doesn&#8217;t actually check to see if something&#8217;s equal it just makes a bunch of</p>



<p class="wp-block-paragraph">called r flags with information that we can later use to decide if the two things were equal or not</p>



<p class="wp-block-paragraph">equal or greater than or less than or whatever so that&#8217;s why i chose to put that on on the next line</p>



<p class="wp-block-paragraph">so at this point we&#8217;re saying if those two things were equal and that&#8217;s how i implement the expression</p>



<p class="wp-block-paragraph">in the middle then let&#8217;s jump to a special label called if test if was equal so you can come up</p>



<p class="wp-block-paragraph">with any scheme you want for your labels but for me when i have when i have sub labels inside of a</p>



<p class="wp-block-paragraph">when i have sub labels inside of a function i like to just suffix the function&#8217;s name with an</p>



<p class="wp-block-paragraph">underscore and then start thinking of sub labels after that so everything&#8217;s like kind of clean</p>



<p class="wp-block-paragraph">there&#8217;s less chance of overlap in labels if you have like a giant module with tons of functions</p>



<p class="wp-block-paragraph">so i&#8217;m going to say this is like my main if that i&#8217;m checking and uh i&#8217;m going to jump to a label</p>



<p class="wp-block-paragraph">called was equal meaning you know this evaluated to true that means that should be the true part</p>



<p class="wp-block-paragraph">block let&#8217;s see do i still have that code here yeah right so here i&#8217;m going to jump to</p>



<p class="wp-block-paragraph">the true block so you know if was true in the first example that we talked about so i&#8217;m going</p>



<p class="wp-block-paragraph">to say if it&#8217;s if those things were equal jump to the code for the true block okay that means i</p>



<p class="wp-block-paragraph">actually need the true block uh but i guess we&#8217;re going to set that up in a second otherwise if that</p>



<p class="wp-block-paragraph">did not jump away it means that those two things are not equal so i said i should jump to the</p>



<p class="wp-block-paragraph">I should jump to the else block or the false block.</p>



<p class="wp-block-paragraph">Wait, do I have else in this example?</p>



<p class="wp-block-paragraph">Oh, okay, okay.</p>



<p class="wp-block-paragraph">This first thing that we&#8217;re looking at is only if.</p>



<p class="wp-block-paragraph">So we don&#8217;t even have an else block yet.</p>



<p class="wp-block-paragraph">We&#8217;re going to do that as the second example.</p>



<p class="wp-block-paragraph">So we&#8217;re basically going to jump to the done area</p>



<p class="wp-block-paragraph">if we didn&#8217;t jump into the true area.</p>



<p class="wp-block-paragraph">And then at the end of the true area,</p>



<p class="wp-block-paragraph">we can either jump to the done area</p>



<p class="wp-block-paragraph">or we can let the execution fall through.</p>



<p class="wp-block-paragraph">fall through okay so now that means i need if was equal and if i just copy paste a big giant</p>



<p class="wp-block-paragraph">block of code and try to explain it to you real fast let&#8217;s do this</p>



<p class="wp-block-paragraph">okay so we were going to jump to if was equal if r12 was equal to r13 and then um</p>



<p class="wp-block-paragraph">we uh have this you know label here and notice how i&#8217;ve kind of like put a brace here indicating</p>



<p class="wp-block-paragraph">like put a brace here indicating hey this is the beginning of the true block body you should</p>



<p class="wp-block-paragraph">consider doing this too when you&#8217;re first learning and even after you&#8217;ve learned because let&#8217;s face</p>



<p class="wp-block-paragraph">it assembly is tough and so in the true area i&#8217;m just going to print the equality message i&#8217;m going</p>



<p class="wp-block-paragraph">to say hey your number was equal to you know whatever and then uh i&#8217;m going to actually print</p>



<p class="wp-block-paragraph">the let&#8217;s see r13 number so that was uh i think the number to compare against the test against</p>



<p class="wp-block-paragraph">against me number so that means here in this message we&#8217;re going to say hey your number was</p>



<p class="wp-block-paragraph">equal to the number that we tested against so then otherwise let&#8217;s see or sorry after that</p>



<p class="wp-block-paragraph">we&#8217;ll print the suffix of the message um and so you know i just i just like to make pretty</p>



<p class="wp-block-paragraph">pretty uh printed messages so let&#8217;s see where&#8217;s the suffix here i can&#8217;t even find it um how about</p>



<p class="wp-block-paragraph">oh i should have put suffix instead of the number two that would have been better so what i wanted</p>



<p class="wp-block-paragraph">to do is say your input was equal to and then print the number and then after that print an</p>



<p class="wp-block-paragraph">exclamation just to prove to you how easy it is to to make a pretty message that&#8217;s formatted nicely</p>



<p class="wp-block-paragraph">for the user um or your professor or whoever so uh you know you basically just print a number and</p>



<p class="wp-block-paragraph">then you or sorry you print the prefix and then you print the number and then you print the suffix</p>



<p class="wp-block-paragraph">suffix and then that&#8217;s the end of the true body and then since we&#8217;re done with the true body we</p>



<p class="wp-block-paragraph">can basically just say all right now we&#8217;re done with the if so the next instruction that follows</p>



<p class="wp-block-paragraph">is the done area again looking back at the example here that would be sort of like here after the</p>



<p class="wp-block-paragraph">whole entire block was finished we&#8217;re ignoring the else in this code but you can imagine we&#8217;ll do</p>



<p class="wp-block-paragraph">that soon so if the user&#8217;s number matched something we execute a true body if not we jump to the done</p>



<p class="wp-block-paragraph">to the done area and if the user&#8217;s uh number did not match then we just immediately jump to the</p>



<p class="wp-block-paragraph">done area so that we don&#8217;t do the true area and then we write comments to ourselves to help us</p>



<p class="wp-block-paragraph">remember oh look here&#8217;s like the comparison and then here&#8217;s the body of true and then uh the done</p>



<p class="wp-block-paragraph">area is like we could put another comment if we want we could say like this is done but i kind of</p>



<p class="wp-block-paragraph">think like i kind of think the label is self-explanatory so let&#8217;s see if this worked it&#8217;s</p>



<p class="wp-block-paragraph">was talking too fast we&#8217;ll run it and we&#8217;ll say 44. okay so nothing matched any of the numbers so</p>



<p class="wp-block-paragraph">let&#8217;s type the number six so that we get a little message on the second one so i&#8217;m going to type the</p>



<p class="wp-block-paragraph">number six and you can see it did not match the number five and it did match the number six so</p>



<p class="wp-block-paragraph">we got that true block executing when we called on that function where the number to compare to</p>



<p class="wp-block-paragraph">nothing there. So now we know simple if blocks.</p>



<p class="wp-block-paragraph">The next thing we&#8217;re going to do is if else blocks.</p>



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



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



<p class="wp-block-paragraph">Not sure if I&#8217;m going to cut the video and split this up into multiple parts.</p>



<p class="wp-block-paragraph">Probably would have been a smart idea.</p>



<p class="wp-block-paragraph">Let me know if I ended up doing it.</p>



<p class="wp-block-paragraph">We&#8217;re at about 25 minutes now.</p>



<p class="wp-block-paragraph">But anyway,</p>



<p class="wp-block-paragraph">so now we&#8217;re going to look at if else blocks.</p>



<p class="wp-block-paragraph">if else blocks.</p>



<p class="wp-block-paragraph">So I&#8217;m going to start off with another function.</p>



<p class="wp-block-paragraph">I&#8217;m going to call it if else test.</p>



<p class="wp-block-paragraph">So here&#8217;s the tester.</p>



<p class="wp-block-paragraph">It called on if test.</p>



<p class="wp-block-paragraph">And then I&#8217;m going to, you know, I had the if test function that I did previously.</p>



<p class="wp-block-paragraph">And so now we&#8217;re just going to make an if else test function.</p>



<p class="wp-block-paragraph">The if else test, it just has one input, one argument for input.</p>



<p class="wp-block-paragraph">And we&#8217;re going to just sort of compare it against different values.</p>



<p class="wp-block-paragraph">of compare it against different values we&#8217;re not going to call this multiple times with different</p>



<p class="wp-block-paragraph">values to compare against so that means let&#8217;s see we only need to use one register so</p>



<p class="wp-block-paragraph">i&#8217;ve designated r12 as the user&#8217;s input so that&#8217;s where we&#8217;re going to store it which means we</p>



<p class="wp-block-paragraph">should preserve it in the prologue so i&#8217;m going to push r12 and then again if you don&#8217;t understand</p>



<p class="wp-block-paragraph">some of the basic stuff that i&#8217;m that i&#8217;m skipping over see my other videos where i explained</p>



<p class="wp-block-paragraph">to know to actually have this kind of a program. So I&#8217;m going to have a function that enters and</p>



<p class="wp-block-paragraph">it returns at the end. It uses R12 so we will preserve it with a push pop pair.</p>



<p class="wp-block-paragraph">And then the first thing that I should do is grab the user&#8217;s input from the first argument.</p>



<p class="wp-block-paragraph">And if you&#8217;re respecting the ABI that means it should come from RDI. So I&#8217;m going to move RDI</p>



<p class="wp-block-paragraph">somebody remind me that the first part of the program has to actually call this function or</p>



<p class="wp-block-paragraph">nothing will happen. So we&#8217;re going to say hello. That&#8217;s just a simple message that we talked about</p>



<p class="wp-block-paragraph">before. And then now we&#8217;re going to actually implement the if else block. So it&#8217;s like a</p>



<p class="wp-block-paragraph">little bit more complicated than just the simple if block. It&#8217;s going to be this whole example that</p>



<p class="wp-block-paragraph">we talked about before. Let me see. Maybe I should add the calls to this block real fast. So we have</p>



<p class="wp-block-paragraph">to this block real fast so we have the if tester and then run the complex if else tests</p>



<p class="wp-block-paragraph">right before r12 okay let me just double check that i&#8217;m making a call at the right spot here</p>



<p class="wp-block-paragraph">so we have r12 and 7 and then epilog okay so now finally in our program we&#8217;re going to have a call</p>



<p class="wp-block-paragraph">to um the if else test function that we&#8217;re just making right now</p>



<p class="wp-block-paragraph">Okay, so we have that.</p>



<p class="wp-block-paragraph">And let me find that source again real fast.</p>



<p class="wp-block-paragraph">If else test, we got the prologue.</p>



<p class="wp-block-paragraph">We take their input and then we say hello.</p>



<p class="wp-block-paragraph">Okay, so now we need another label that begins the if block.</p>



<p class="wp-block-paragraph">So what we&#8217;re doing is we&#8217;re checking to see if the user&#8217;s input was equal to five.</p>



<p class="wp-block-paragraph">And then we&#8217;re going to say something if it was.</p>



<p class="wp-block-paragraph">And otherwise, we&#8217;re going to do an else block on that.</p>



<p class="wp-block-paragraph">block on that so again i like to write my comparison instructions with a blank expression</p>



<p class="wp-block-paragraph">in terms of the c equivalent comment so notice how the we&#8217;re checking to see if r12 is equal to</p>



<p class="wp-block-paragraph">five i put that on the next block because we&#8217;re going to jump to the to the true part of the if</p>



<p class="wp-block-paragraph">block if it equals five so that&#8217;s why i put that there the comparison instruction pretty much just</p>



<p class="wp-block-paragraph">compares r12 with five sets a bunch of values into the r flags registers so that we can later</p>



<p class="wp-block-paragraph">so that we can later conditionally jump if we want to.</p>



<p class="wp-block-paragraph">So basically we&#8217;ll jump to the true place if that was true.</p>



<p class="wp-block-paragraph">And then if not, execution falls through to the next statement,</p>



<p class="wp-block-paragraph">which will just jump to the else place.</p>



<p class="wp-block-paragraph">So we need more labels is what I&#8217;m saying.</p>



<p class="wp-block-paragraph">So now we need a body for if the statement was true</p>



<p class="wp-block-paragraph">or the expression was true,</p>



<p class="wp-block-paragraph">we want to be able to execute the true portion of the if block.</p>



<p class="wp-block-paragraph">the if block so that&#8217;s this right here so we&#8217;re going to jump to if it did equal to five you</p>



<p class="wp-block-paragraph">could imagine you know making a better label instead of equal five you could say first if</p>



<p class="wp-block-paragraph">first else if complicated block true scope or true block or something but i just put equal to five</p>



<p class="wp-block-paragraph">basically saying we&#8217;ll execute this code if it was equal to five so again we&#8217;re just kind of like</p>



<p class="wp-block-paragraph">into this label equal five if r12 was indeed equal to five so that means if it was we execute</p>



<p class="wp-block-paragraph">all this code right here we know we&#8217;re finished when we have the very last line saying let&#8217;s jump</p>



<p class="wp-block-paragraph">to finish the the if else block again just just to clarify</p>



<p class="wp-block-paragraph">we do the comparison first and if the comparison was true then we&#8217;ll jump into the true area so</p>



<p class="wp-block-paragraph">you know we&#8217;ll jump into the true area and then all of these uh instructions get executed</p>



<p class="wp-block-paragraph">instructions get executed but if we don&#8217;t have a way to jump out of that block then whoops</p>



<p class="wp-block-paragraph">all of the else statements are going to get executed too right so we don&#8217;t want that</p>



<p class="wp-block-paragraph">we don&#8217;t want to execute both the true and the false statements we want to have a jump instruction</p>



<p class="wp-block-paragraph">at the very end i&#8217;ll put jmp just so that we jump to the to the end of the if else block</p>



<p class="wp-block-paragraph">you know jump to the place where it&#8217;s just all over and finished</p>



<p class="wp-block-paragraph">Okay, so now I&#8217;m going to look at, whoops, turn that off.</p>



<p class="wp-block-paragraph">So we have equal five.</p>



<p class="wp-block-paragraph">So we&#8217;re going to jump to if else test if done.</p>



<p class="wp-block-paragraph">Let me just double check to make sure I&#8217;m not forgetting anything.</p>



<p class="wp-block-paragraph">Oh, we need to jump here.</p>



<p class="wp-block-paragraph">Basically meaning we&#8217;re going to jump into the else portion.</p>



<p class="wp-block-paragraph">So here we covered jumping into the true portion, you know, the regular top block.</p>



<p class="wp-block-paragraph">Now here we&#8217;re going to jump into the else portion.</p>



<p class="wp-block-paragraph">We need a label and some code for that.</p>



<p class="wp-block-paragraph">code for that so this is the else six begin i copy that oh man it&#8217;s starting to get kind of</p>



<p class="wp-block-paragraph">hectic in my brain here the copy pasting is worse than actually writing the program</p>



<p class="wp-block-paragraph">so uh if the comparison was not equal like if r12 was not equal then execution falls through</p>



<p class="wp-block-paragraph">to line 216 and then we unconditionally jump to the l6 begin area which is like down here</p>



<p class="wp-block-paragraph">like down here and then we&#8217;ll print something else.</p>



<p class="wp-block-paragraph">We&#8217;ll, oh, we&#8217;ll, we&#8217;ll check again.</p>



<p class="wp-block-paragraph">So we&#8217;re doing like, um, if else, if else.</p>



<p class="wp-block-paragraph">Okay. So originally when I was talking about the, uh,</p>



<p class="wp-block-paragraph">if else block, I didn&#8217;t do a,</p>



<p class="wp-block-paragraph">I didn&#8217;t do like a very complex if else statement.</p>



<p class="wp-block-paragraph">We&#8217;ll say a is greater than B something like that.</p>



<p class="wp-block-paragraph">So I&#8217;ll just put a more code here just so you know that we can do if else,</p>



<p class="wp-block-paragraph">if else blocks but just you know again keep in mind that every scope here that you&#8217;re going to</p>



<p class="wp-block-paragraph">try to run based on some condition you just make it its own label and make sure that at the very</p>



<p class="wp-block-paragraph">end of the scope you jump away so that you reach the very end of all this stuff because just as a</p>



<p class="wp-block-paragraph">quick review if we are just writing in c or c plus plus only one of these blocks is going to execute</p>



<p class="wp-block-paragraph">right like if a is equal to b then only that first block will execute the second and third blocks</p>



<p class="wp-block-paragraph">will not execute it&#8217;ll jump after that all the way down to line 21 only if a does not equal b</p>



<p class="wp-block-paragraph">do we even have the chance of checking to see if a is more than b if it&#8217;s false then we&#8217;ll have the</p>



<p class="wp-block-paragraph">chance to check the else and if it&#8217;s true we will only execute the code in the line 12 block you</p>



<p class="wp-block-paragraph">know imagine there are more statements there and when that block is done then we will jump to line</p>



<p class="wp-block-paragraph">to b and a is not greater than b so just a quick uh you know c plus plus uh you know design pattern</p>



<p class="wp-block-paragraph">review if else block review so we have like an else if here we&#8217;re going to say check if the input</p>



<p class="wp-block-paragraph">was equal to six so we just do the same thing that we did before we compare r12 the user&#8217;s input with</p>



<p class="wp-block-paragraph">six we jump if they were equal to the else if equals true block and if not execution falls</p>



<p class="wp-block-paragraph">Less than 10 begin block. So this is going to be like really complicated</p>



<p class="wp-block-paragraph">How many lines did I actually I think I got excited when I wrote this</p>



<p class="wp-block-paragraph">Okay, else if equal six true</p>



<p class="wp-block-paragraph">My challenge to you is to come up with labels that are like way easier than the labels that I came up with</p>



<p class="wp-block-paragraph">All right, so we&#8217;re gonna do this I</p>



<p class="wp-block-paragraph">Can almost guarantee that when I&#8217;m done copy pasting everything something is not going to compile because I forgot a label somewhere</p>



<p class="wp-block-paragraph">Else if equals six true, okay</p>



<p class="wp-block-paragraph">equals six true okay so what&#8217;s happening here again um uh so at this point you know the first</p>



<p class="wp-block-paragraph">if uh expression was not true r12 was definitely not equal to five so we jumped down to else if</p>



<p class="wp-block-paragraph">equal six begin which was here and so then we just make another comparison to see well okay it was</p>



<p class="wp-block-paragraph">those two things weren&#8217;t equal it wasn&#8217;t equal to five so let&#8217;s check to see if it was equal to six</p>



<p class="wp-block-paragraph">to yet another scope.</p>



<p class="wp-block-paragraph">If it was false, we go down to line 237</p>



<p class="wp-block-paragraph">and jump to yet another scope.</p>



<p class="wp-block-paragraph">So here is what will get executed</p>



<p class="wp-block-paragraph">if R12 was indeed equal to six,</p>



<p class="wp-block-paragraph">and then we&#8217;re just basically gonna say it to the user</p>



<p class="wp-block-paragraph">and then jump to the done label,</p>



<p class="wp-block-paragraph">meaning like we&#8217;re totally done with our if else block.</p>



<p class="wp-block-paragraph">Notice how the first if here,</p>



<p class="wp-block-paragraph">when it was totally finished, it jumped to the done area.</p>



<p class="wp-block-paragraph">this else if block is also jumping down to the if done area.</p>



<p class="wp-block-paragraph">So eventually we&#8217;re going to need that label.</p>



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



<p class="wp-block-paragraph">So then next we&#8217;re going to check to see if the user&#8217;s input was equal to a 10.</p>



<p class="wp-block-paragraph">So like kind of the same thing here.</p>



<p class="wp-block-paragraph">We&#8217;re going to do another copy paste and we&#8217;re going to say, all right.</p>



<p class="wp-block-paragraph">So if the user&#8217;s input was not equal to, let&#8217;s see,</p>



<p class="wp-block-paragraph">if it was not equal to five, then we jump down here for our next comparison.</p>



<p class="wp-block-paragraph">comparison we check to see if it&#8217;s equal to six if it was not equal to six then we jump down to</p>



<p class="wp-block-paragraph">the less 10 begin line which is like all the way down here we do another comparison to see all right</p>



<p class="wp-block-paragraph">well is it less than 10 you know if it was not equal to five or not equal to six then we check</p>



<p class="wp-block-paragraph">is it less than 10 if it uh if it is then we uh jump to the else if equal less 10 true block which</p>



<p class="wp-block-paragraph">I made this way too complicated. I realize that now, but it&#8217;s too late. I&#8217;m going for it, man.</p>



<p class="wp-block-paragraph">Anyway, so if that statement is true, if R12 was indeed less than 10, then we jump to this block</p>



<p class="wp-block-paragraph">and we basically just say that to the user and then we jump to the done. So finally, notice how</p>



<p class="wp-block-paragraph">this part right here on line 259, it&#8217;s basically saying if R12 was not less than 10, then we&#8217;ll</p>



<p class="wp-block-paragraph">then we&#8217;ll jump somewhere else notice how it&#8217;s just else right so this is like the very the very</p>



<p class="wp-block-paragraph">bottom so i think the way that i wrote this uh code is uh we have two else ifs right we have like</p>



<p class="wp-block-paragraph">an if five else if six else if less than 10 so maybe i could do something like this um if r12</p>



<p class="wp-block-paragraph">r12 I think I said six just now right hopefully I actually did say six</p>



<p class="wp-block-paragraph">otherwise if r12 is less than 10 then do some stuff otherwise if nothing else</p>



<p class="wp-block-paragraph">matched then we&#8217;ll execute you know this block right here so again remember</p>



<p class="wp-block-paragraph">every single scope has to have its own label so that you know where to jump and</p>



<p class="wp-block-paragraph">it&#8217;s also a really really smart idea for every single scope to have you know</p>



<p class="wp-block-paragraph">to have a little jump instruction that jumps past all of the if else if else if else stuff</p>



<p class="wp-block-paragraph">so we&#8217;ll say like you know label you know done so that we can make sure that only one of these</p>



<p class="wp-block-paragraph">blocks actually executes which is how you&#8217;re supposed to imagine c and c plus plus work</p>



<p class="wp-block-paragraph">um and so it&#8217;s just complicated because there&#8217;s a lot of stuff to copy paste but you can just</p>



<p class="wp-block-paragraph">still see you know only one of these blocks ever is supposed to execute so we give the first one</p>



<p class="wp-block-paragraph">we jump into it if it&#8217;s true if it&#8217;s not true then we give the second one a chance</p>



<p class="wp-block-paragraph">we jump into it if it&#8217;s true if not we jump to compare the third one if it&#8217;s true then we jump</p>



<p class="wp-block-paragraph">to its scope if not we jump to the else and the else always executes if nothing else above</p>



<p class="wp-block-paragraph">was true so that&#8217;s the basic idea here oh dear i&#8217;ve probably lost my place</p>



<p class="wp-block-paragraph">looks like i just copy pasted less 10 true which was this right here so</p>



<p class="wp-block-paragraph">so um less than true or less than 10 true okay so we told the user your stuff is less than 10</p>



<p class="wp-block-paragraph">and then we jumped to the done area so that means we are probably working on the else area okay let</p>



<p class="wp-block-paragraph">me grab that so now finally we&#8217;re going to have the else area which is going to you know finish</p>



<p class="wp-block-paragraph">this all up that&#8217;s going to be after the if done so if nothing else matched then we will end up</p>



<p class="wp-block-paragraph">nothing else matched then we will end up jumping to the else area and then we&#8217;ll basically</p>



<p class="wp-block-paragraph">just tell the user none of the conditions seemed to have applied and then even at the</p>



<p class="wp-block-paragraph">end of the else block even though you could probably get away with just letting execution</p>



<p class="wp-block-paragraph">fall through just to save yourself one instruction you know you could comment that out assuming</p>



<p class="wp-block-paragraph">you were sure that the very next instruction was the beginning of the done area but otherwise</p>



<p class="wp-block-paragraph">I&#8217;m just going to play it safe and jump directly there then we have to make the actual done</p>



<p class="wp-block-paragraph">So, just so you know, a label doesn&#8217;t have to have any instructions.</p>



<p class="wp-block-paragraph">We could have something like this, if else test say goodbye.</p>



<p class="wp-block-paragraph">We could have two labels right next to each other and one doesn&#8217;t actually have instructions.</p>



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



<p class="wp-block-paragraph">If you jumped to the done area, then execution would just fall through to the next valid</p>



<p class="wp-block-paragraph">instruction which could go through another label.</p>



<p class="wp-block-paragraph">So you know, we could have a say goodbye label and an if done label.</p>



<p class="wp-block-paragraph">if done label, I&#8217;m just going to have the goodbye stuff happening inside of the done.</p>



<p class="wp-block-paragraph">But for clarity&#8217;s sake, you might want to keep that label in there that I just deleted.</p>



<p class="wp-block-paragraph">And all that we need to do at the very end when we&#8217;re done with everything is just say goodbye.</p>



<p class="wp-block-paragraph">So I&#8217;m just printing a message.</p>



<p class="wp-block-paragraph">So just to do a quick recap here, let&#8217;s see.</p>



<p class="wp-block-paragraph">We come in with the user&#8217;s input as R12.</p>



<p class="wp-block-paragraph">We ask, does R12 equal five?</p>



<p class="wp-block-paragraph">If that&#8217;s true, we&#8217;ll say that it was equal to 5, and then we&#8217;ll go to the done area,</p>



<p class="wp-block-paragraph">meaning we&#8217;ll skip past all the other blocks for the if-else block, or all the other scopes.</p>



<p class="wp-block-paragraph">But if that did not equal 5, then we&#8217;ll fall through to that jump instruction,</p>



<p class="wp-block-paragraph">which takes us to the next test, which is going to be our else-if equals 6.</p>



<p class="wp-block-paragraph">So we are checking now to see if R12 is equal to 6.</p>



<p class="wp-block-paragraph">If that&#8217;s true, we jump to this next block.</p>



<p class="wp-block-paragraph">Why do we need to jump to a block that&#8217;s so close?</p>



<p class="wp-block-paragraph">Well, if we don&#8217;t, then we&#8217;re going to end up definitely jumping to the next comparison</p>



<p class="wp-block-paragraph">and you know, that wouldn&#8217;t work.</p>



<p class="wp-block-paragraph">So we&#8217;re just kind of jumping over this unconditional jump statement and we&#8217;re saying, all right,</p>



<p class="wp-block-paragraph">your input was definitely equal to six.</p>



<p class="wp-block-paragraph">And then we&#8217;re jumping to the done area just past everything.</p>



<p class="wp-block-paragraph">But if that wasn&#8217;t true, then we hit this unconditional jump that takes us to the next</p>



<p class="wp-block-paragraph">comparison to see if the user&#8217;s input was less than 10.</p>



<p class="wp-block-paragraph">You know, if that&#8217;s true, then we go to the true area to print another message and then</p>



<p class="wp-block-paragraph">go to the done area.</p>



<p class="wp-block-paragraph">If it was false, we fall through to the else jump, which will take us to here.</p>



<p class="wp-block-paragraph">And notice how the else scope doesn&#8217;t actually make any comparisons because, you know, when</p>



<p class="wp-block-paragraph">you have like an if else, sorry, when you have an if else if else if else if, you know,</p>



<p class="wp-block-paragraph">any number of else if blocks, the else will always be executed if nothing above it actually</p>



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



<p class="wp-block-paragraph">execute so if you have an else block that means something will execute so we&#8217;re not doing any</p>



<p class="wp-block-paragraph">comparisons we&#8217;re just saying you know when that scope is done we&#8217;re just going to jump to the done</p>



<p class="wp-block-paragraph">area so all of these different scopes they&#8217;re jumping to the done area when they finish and</p>



<p class="wp-block-paragraph">the done area is just this label right here where we say goodbye and that&#8217;s it let&#8217;s see did i copy</p>



<p class="wp-block-paragraph">paste everything that i was supposed to i think so probably so let&#8217;s run the program and see if it</p>



<p class="wp-block-paragraph">All right, it at least compiled.</p>



<p class="wp-block-paragraph">So the basic if test, that was the first part of either this video or the previous video,</p>



<p class="wp-block-paragraph">depending on whether I chose to split this up.</p>



<p class="wp-block-paragraph">So let me comment out those calls real fast.</p>



<p class="wp-block-paragraph">So I&#8217;m going to just comment out these calls real quick so we can only deal with the complicated</p>



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



<p class="wp-block-paragraph">So I&#8217;m going to enter like a three.</p>



<p class="wp-block-paragraph">Whoops, let me do it again.</p>



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



<p class="wp-block-paragraph">And so begin the if else test.</p>



<p class="wp-block-paragraph">test your input was definitely less than 10 and then we end the if-else test so notice how only</p>



<p class="wp-block-paragraph">one scope executed three was definitely less than 10 what other numbers did we have let me write</p>



<p class="wp-block-paragraph">them down somewhere so I don&#8217;t forget oh they&#8217;re written down right here so we entered a three</p>



<p class="wp-block-paragraph">which was definitely not a five and not a six so that&#8217;s why the less than 10 block executed maybe</p>



<p class="wp-block-paragraph">only the first scope should execute so it says your input was equal to five notice how it didn&#8217;t</p>



<p class="wp-block-paragraph">mention that it was a six or less than ten and then if we do a six it should just tell us that</p>



<p class="wp-block-paragraph">we have a six it will not mention the five it will not mention the ten so we did we did five</p>



<p class="wp-block-paragraph">already and then we did six and we did like a two which was less than ten we could also do a one</p>



<p class="wp-block-paragraph">which was less than ten we can do anything that was less than ten and then the else would be if</p>



<p class="wp-block-paragraph">And then the else would be if our input is probably like 11 or greater,</p>



<p class="wp-block-paragraph">meaning it&#8217;s not less than 10 and it&#8217;s also not five or six.</p>



<p class="wp-block-paragraph">Just we can see the else block.</p>



<p class="wp-block-paragraph">So I&#8217;m going to do 11, which is the first number that should trigger the else block.</p>



<p class="wp-block-paragraph">And it says no conditions were satisfied,</p>



<p class="wp-block-paragraph">which was the message inside of our else block.</p>



<p class="wp-block-paragraph">Let me just show you that again real fast.</p>



<p class="wp-block-paragraph">This was the else block.</p>



<p class="wp-block-paragraph">So message if else else or labeling on my port, I realize that.</p>



<p class="wp-block-paragraph">But it basically says no conditions were satisfied.</p>



<p class="wp-block-paragraph">The if else else.</p>



<p class="wp-block-paragraph">I think that&#8217;s basically everything that I wanted to show you.</p>



<p class="wp-block-paragraph">At this point, you should feel like you&#8217;re starting to become an expert at complicated</p>



<p class="wp-block-paragraph">if else if else blocks, converting those from a higher level language into assembly.</p>



<p class="wp-block-paragraph">assembly and I honestly recommend that you practice this like crazy while you&#8217;re trying</p>



<p class="wp-block-paragraph">to get more used to it but you know hopefully you have everything you need at this point</p>



<p class="wp-block-paragraph">keep in mind I&#8217;ve said this at the beginning I&#8217;ve actually I think I said this in a different video</p>



<p class="wp-block-paragraph">when we use these branching instructions notice how I have jump equal to where&#8217;s that less than</p>



<p class="wp-block-paragraph">yeah notice how I have a instruction jump less than the the family of conditional branching</p>



<p class="wp-block-paragraph">conditional branching instructions that compares less than greater than less than or equal to</p>



<p class="wp-block-paragraph">greater than or equal to the ones that I&#8217;m using here apply to signed integers and they won&#8217;t</p>



<p class="wp-block-paragraph">necessarily work with unsigned integers and they won&#8217;t work with floats if you compare floats later</p>



<p class="wp-block-paragraph">so just keep that in mind let me pull up my favorite book again real fast</p>



<p class="wp-block-paragraph">where what the heck is that book oh dude where&#8217;s my document viewer document viewer can I get</p>



<p class="wp-block-paragraph">document viewer can I get there there we go so I guess I didn&#8217;t introduce this</p>



<p class="wp-block-paragraph">book at the beginning of this video but I probably should have this is my</p>



<p class="wp-block-paragraph">favorite assembly book you can turn yourself into an expert with this book</p>



<p class="wp-block-paragraph">by just on its own it&#8217;s open source and free the author gives this away he&#8217;s a</p>



<p class="wp-block-paragraph">genius the person who wrote this book is a dr. Ed Jorgensen he&#8217;s a professor he</p>



<p class="wp-block-paragraph">wrote this for his own classes and again it&#8217;s like a free open source book you</p>



<p class="wp-block-paragraph">download a copy from his website if you look this up and and convert yourself</p>



<p class="wp-block-paragraph">into an expert so let me collapse everything here and then I&#8217;ll go to</p>



<p class="wp-block-paragraph">instruction set overview control instructions conditional control</p>



<p class="wp-block-paragraph">instructions and I just want you to see real fast just as a recap I talked about</p>



<p class="wp-block-paragraph">this in a previous video but when it comes to comparing things and checking</p>



<p class="wp-block-paragraph">operands because if you want to see if two operands are equal you just check to see if</p>



<p class="wp-block-paragraph">all of their bits are equal you don&#8217;t even really care whether they&#8217;re both integers or not</p>



<p class="wp-block-paragraph">on the other hand notice how there is like a family here jump less than jump less than equal</p>



<p class="wp-block-paragraph">to jump greater than jump greater than equal to that those instructions apply only if your</p>



<p class="wp-block-paragraph">if they are unsigned or if you&#8217;re using floats for your comparisons you have to use this other family</p>



<p class="wp-block-paragraph">of jump conditional branching instructions called jump below jump below equal jump above jump above</p>



<p class="wp-block-paragraph">equal and i&#8217;m sure you can infer that jump below is the same thing as jump less than right jump</p>



<p class="wp-block-paragraph">less than or equal to is the same thing as jump below or equal to it&#8217;s just that you need to use</p>



<p class="wp-block-paragraph">a different instruction uh if you&#8217;re using signed integers or floats versus sorry sorry</p>



<p class="wp-block-paragraph">a versus sorry sorry you need to use different instructions if you&#8217;re using signed integers</p>



<p class="wp-block-paragraph">versus unsigned integers or floats so keep that in mind the code demo that i just showed you it&#8217;s</p>



<p class="wp-block-paragraph">for signed integers if you were going to use unsigned integers or floats you would this</p>



<p class="wp-block-paragraph">program wouldn&#8217;t work for you you&#8217;d need to replace my my branching instructions with the</p>



<p class="wp-block-paragraph">the unsigned versions which i mean they work the same thing you&#8217;re still going to do a comparison</p>



<p class="wp-block-paragraph">it still works. Anyway, so after having shown you that and making sure that everything actually</p>



<p class="wp-block-paragraph">did work, I think we&#8217;ve talked about everything that we need to for this video. So yeah, I hope</p>



<p class="wp-block-paragraph">you feel like an expert practice this on your own, you know, write your own programs just to make</p>



<p class="wp-block-paragraph">sure you know how to convert a higher level language block into an assembly block. And you</p>



<p class="wp-block-paragraph">should be on your way. Thank you for watching this video. I hope you learned a little bit of</p>



<p class="wp-block-paragraph">bit of stuff and had a little bit of fun 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</p>



<p class="wp-block-paragraph">appreciate it i do hope you did learn something and have some fun uh if you could do me a please</p>



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



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



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



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



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



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



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



<p class="wp-block-paragraph">just wake me up in the middle of the night just subscribe and then I&#8217;ll just wake up I promise</p>



<p class="wp-block-paragraph">that&#8217;s what will happen also uh if you look at the middle of the screen right now you should see a</p>



<p class="wp-block-paragraph">QR code which you can scan in order to go to the website which I think is also named somewhere at</p>



<p class="wp-block-paragraph">the 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">all the videos I published and the services and tutorials and things that I offer and all that</p>



<p class="wp-block-paragraph">for clarifications or errata or just future videos that you want to see please leave a comment or if</p>



<p class="wp-block-paragraph">you just want to say hey what&#8217;s up what&#8217;s going on you know just send me a comment whatever I</p>



<p class="wp-block-paragraph">also wake up for those in the middle of the night I get I wake up in a cold sweat and I&#8217;m like</p>



<p class="wp-block-paragraph">it would really it really mean the world to me I would really appreciate it so again thank you so</p>



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



<p class="wp-block-paragraph">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/complex-if-elseif-else-in-x86-64-yasm-assembly-full-guide-with-code-examples/">Complex If-ElseIf-Else in x86-64 YASM Assembly &#8211; Full Guide with Code Examples</a> appeared first on <a href="https://www.NeuralLantern.com">NeuralLantern.com</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.NeuralLantern.com/complex-if-elseif-else-in-x86-64-yasm-assembly-full-guide-with-code-examples/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<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>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 loading="lazy" 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>Mastering Standard Input, Output, and Error (STDIN, STDOUT, STDERR) in Linux with Yasm Assembly Examples</title>
		<link>https://www.NeuralLantern.com/mastering-standard-input-output-and-error-stdin-stdout-stderr-in-linux-with-yasm-assembly-examples/</link>
					<comments>https://www.NeuralLantern.com/mastering-standard-input-output-and-error-stdin-stdout-stderr-in-linux-with-yasm-assembly-examples/#respond</comments>
		
		<dc:creator><![CDATA[mike]]></dc:creator>
		<pubDate>Mon, 22 Sep 2025 11:53:48 +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[coding]]></category>
		<category><![CDATA[file descriptors]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[Linux pipes]]></category>
		<category><![CDATA[Linux tutorial]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[standard error]]></category>
		<category><![CDATA[standard input]]></category>
		<category><![CDATA[standard output]]></category>
		<category><![CDATA[STDERR]]></category>
		<category><![CDATA[STDIN]]></category>
		<category><![CDATA[STDOUT]]></category>
		<category><![CDATA[system calls]]></category>
		<category><![CDATA[Yasm assembly]]></category>
		<guid isPermaLink="false">https://www.NeuralLantern.com/?p=230</guid>

					<description><![CDATA[<p>Explore Linux's STDIN, STDOUT, and STDERR pipes with a fun Yasm Assembly demo. Learn how programs communicate, redirect outputs, and handle inputs in this beginner-friendly guide.</p>
<p>The post <a href="https://www.NeuralLantern.com/mastering-standard-input-output-and-error-stdin-stdout-stderr-in-linux-with-yasm-assembly-examples/">Mastering Standard Input, Output, and Error (STDIN, STDOUT, STDERR) in Linux with Yasm Assembly Examples</a> appeared first on <a href="https://www.NeuralLantern.com">NeuralLantern.com</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<figure class="wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio"><div class="wp-block-embed__wrapper">
<iframe loading="lazy" title="Mastering Standard Input, Output, and Error (STDIN, STDOUT, STDERR) in Linux with Yasm Assembly" width="1380" height="776" src="https://www.youtube.com/embed/8Jv1_q0cyaw?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, hands-on look at Linux&#8217;s standard input (STDIN), output (STDOUT), and error (STDERR) pipes! In this video, we explore how programs communicate using these essential streams, with practical examples in Yasm Assembly (no assembly knowledge needed!). Learn to redirect outputs, handle errors, and read inputs like a pro. Whether you&#8217;re a beginner or seasoned coder, this fun demo breaks down the basics of Linux pipes. Subscribe for more coding insights and check out the full tutorial on our site!</p>



<p class="wp-block-paragraph">Introduction to STDIN, STDOUT, STDERR 00:00:00<br>Overview of Pipes in Linux 00:00:07<br>Explanation of File Descriptors 00:01:12<br>Demonstrating Echo Command 00:01:24<br>Redirecting Standard Output 00:02:02<br>C++ Example with cout and cerr 00:02:48<br>Purpose of Separate Error Pipe 00:03:28<br>Assembly Program Introduction 00:04:55<br>Creating Data Section in Yasm 00:05:22<br>Writing Helper Functions 00:07:37<br>Printing to Standard Output and Error 00:11:29<br>Testing Output Redirection 00:12:43<br>Standard Input Explanation 00:14:54<br>Creating Stack Buffer for Input 00:16:03<br>Reading from Standard Input 00:17:28<br>Piping Input to Program 00:20:54<br>Handling Null Terminators 00:22:02<br>Conclusion and Call to Subscribe 00:23:53</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 I&#8217;m going to talk to you a little bit about standard input, standard output,</p>



<p class="wp-block-paragraph">and standard error.</p>



<p class="wp-block-paragraph">Three pipes that every program has in Linux that allow it to just communicate in a very</p>



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



<p class="wp-block-paragraph">I&#8217;m going to show you some example code written in Yasm Assembly, but assembly knowledge is</p>



<p class="wp-block-paragraph">not necessarily required for this video.</p>



<p class="wp-block-paragraph">You can just sort of follow along.</p>



<p class="wp-block-paragraph">Anyway, what is standard input, standard output, and standard error?</p>



<p class="wp-block-paragraph">Well for starters, every program that gets launched, under the hood, the operating system</p>



<p class="wp-block-paragraph">will typically attach three pipes to it.</p>



<p class="wp-block-paragraph">Basically one for standard input, one for standard output, and one for standard error.</p>



<p class="wp-block-paragraph">the program pretty easily. So let&#8217;s see. Oh, I know what to do. There&#8217;s an echo command, right?</p>



<p class="wp-block-paragraph">So here&#8217;s the program called echo. When the program launches, it&#8217;s going to have three pipes</p>



<p class="wp-block-paragraph">attached to it, just like I told you before. These pipes also behave as file descriptors,</p>



<p class="wp-block-paragraph">as if you had opened a file, but their file descriptor numbers are always zero and one and two.</p>



<p class="wp-block-paragraph">Zero for standard input, one for standard output, and two for standard error. So if I launch the</p>



<p class="wp-block-paragraph">program it&#8217;s going to get those three pipes those three file descriptors and</p>



<p class="wp-block-paragraph">right away the standard output is going to get attached to the program running</p>



<p class="wp-block-paragraph">our terminal here well not necessarily the actual terminal but bash under the</p>



<p class="wp-block-paragraph">hood which is the program that we&#8217;re using to interact with the operating</p>



<p class="wp-block-paragraph">system so basically the program launches and based on this incoming argument that</p>



<p class="wp-block-paragraph">I give it hello then it&#8217;s going to print the string hello right but what it&#8217;s</p>



<p class="wp-block-paragraph">its standard output pipe its standard output file descriptor I can test this</p>



<p class="wp-block-paragraph">for sure by just sort of doing the command again and then muting its</p>



<p class="wp-block-paragraph">standard output pipe with this little shell trick I&#8217;ll do a arrow or a greater</p>



<p class="wp-block-paragraph">than angle bracket and I&#8217;ll just send the first pipe I&#8217;ll do one I&#8217;ll send it</p>



<p class="wp-block-paragraph">to dev null and on Linux dev null is just if you send anything there it just</p>



<p class="wp-block-paragraph">it&#8217;s just totally ignored so if i send uh the pipe labeled number one to just nowhere then notice how</p>



<p class="wp-block-paragraph">it doesn&#8217;t show on the terminal for me right it&#8217;s pretty cool let&#8217;s go a little bit deeper into this</p>



<p class="wp-block-paragraph">i just want you to see real fast before i write an example program in assembly what you would</p>



<p class="wp-block-paragraph">probably end up doing in c plus plus so we usually do something like iostream in germane you know</p>



<p class="wp-block-paragraph">We&#8217;ll say using std cout std c error std and l and so forth.</p>



<p class="wp-block-paragraph">And then if you actually want to print a message to the user, we&#8217;ll say cout this message prints</p>



<p class="wp-block-paragraph">to std out because that&#8217;s what cout does by default.</p>



<p class="wp-block-paragraph">And then if we do c error, a lot of people haven&#8217;t actually seen this and I personally</p>



<p class="wp-block-paragraph">always forget that it&#8217;s even there if you print to see error it prints a message to standard error</p>



<p class="wp-block-paragraph">why would you want two different pipes that printed messages and one is called output and</p>



<p class="wp-block-paragraph">one is called error well like i&#8217;ve said before in other videos it&#8217;s really convenient to be able to</p>



<p class="wp-block-paragraph">have programs automate other programs for example if you have a program that is launching you know</p>



<p class="wp-block-paragraph">a second program maybe the second program fails in some way or wants to complain to you in some way</p>



<p class="wp-block-paragraph">or wants to complain to you in some way, it would usually, if it&#8217;s smart,</p>



<p class="wp-block-paragraph">print the complaint to standard error.</p>



<p class="wp-block-paragraph">It may not actually use C error because maybe it&#8217;s not a C++ program,</p>



<p class="wp-block-paragraph">but it would print to pipe number two, file descriptor number two, standard error.</p>



<p class="wp-block-paragraph">And then that way, the program that&#8217;s launching it can just sort of ignore messages</p>



<p class="wp-block-paragraph">that happen on standard output because we can assume all of those messages</p>



<p class="wp-block-paragraph">are just normal info messages that a human might want to see.</p>



<p class="wp-block-paragraph">Or maybe you want to record to a log or something somewhere</p>



<p class="wp-block-paragraph">log or something somewhere and just kind of save it away.</p>



<p class="wp-block-paragraph">But the error messages are a little bit more important than the regular,</p>



<p class="wp-block-paragraph">you know, normal messages.</p>



<p class="wp-block-paragraph">So if you&#8217;re automating another program,</p>



<p class="wp-block-paragraph">then your program probably wants to take standard output and either ignore it or</p>



<p class="wp-block-paragraph">just kind of send it to a different file.</p>



<p class="wp-block-paragraph">And then it wants to take standard error and pay more attention to it.</p>



<p class="wp-block-paragraph">Write it to a log file, save it, maybe check that it&#8217;s actually blank.</p>



<p class="wp-block-paragraph">And if it&#8217;s not empty, then maybe alarm bells go off.</p>



<p class="wp-block-paragraph">Something went wrong.</p>



<p class="wp-block-paragraph">We have to alert someone or, you know, make a database entry, send an email, whatever.</p>



<p class="wp-block-paragraph">And yeah, just pipe one and pipe two is how you accomplish that.</p>



<p class="wp-block-paragraph">So this is what&#8217;s happening in C++ when you do that sort of thing.</p>



<p class="wp-block-paragraph">Okay, so now I would like to write a sample program that just kind of demonstrates this</p>



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



<p class="wp-block-paragraph">So please bear with me.</p>



<p class="wp-block-paragraph">I&#8217;m going to go really fast for the assembly portion because this is not an assembly video.</p>



<p class="wp-block-paragraph">to learn more about how to write assembly in the first place than see my other videos.</p>



<p class="wp-block-paragraph">For now, I&#8217;m just going to create a little blank file here.</p>



<p class="wp-block-paragraph">Let&#8217;s see where the heck is my window.</p>



<p class="wp-block-paragraph">I&#8217;m on a multi-window system here.</p>



<p class="wp-block-paragraph">Okay, so I&#8217;m just going to paste a little data section.</p>



<p class="wp-block-paragraph">This is Yasm assembly.</p>



<p class="wp-block-paragraph">A little data section.</p>



<p class="wp-block-paragraph">I&#8217;m just going to fill it up with strings.</p>



<p class="wp-block-paragraph">I&#8217;m going to make a string for the introduction.</p>



<p class="wp-block-paragraph">Hello, the module has started.</p>



<p class="wp-block-paragraph">A string announcing everything that we&#8217;re going to do.</p>



<p class="wp-block-paragraph">We&#8217;re going to begin printing to standard output and standard error,</p>



<p class="wp-block-paragraph">and then some messages that will go to each.</p>



<p class="wp-block-paragraph">And then some messages that will go to each and then a standard input string, which we&#8217;ll</p>



<p class="wp-block-paragraph">talk about after the standard output and standard error.</p>



<p class="wp-block-paragraph">So just a bunch of strings.</p>



<p class="wp-block-paragraph">I&#8217;m going to make a buffer size.</p>



<p class="wp-block-paragraph">I&#8217;m going to make system call codes for reading, writing and exiting.</p>



<p class="wp-block-paragraph">If you want to know more about system call codes, see my other videos and some file descriptors.</p>



<p class="wp-block-paragraph">This is exactly what I just talked about.</p>



<p class="wp-block-paragraph">The file descriptor for standard input is zero for standard output is one for standard</p>



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



<p class="wp-block-paragraph">And then I&#8217;m going to do a system exit code of zero,</p>



<p class="wp-block-paragraph">which actually doesn&#8217;t matter that much,</p>



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



<p class="wp-block-paragraph">So now I&#8217;m going to make a, let&#8217;s see,</p>



<p class="wp-block-paragraph">a global,</p>



<p class="wp-block-paragraph">hmm, make a global.</p>



<p class="wp-block-paragraph">Oh, did I already make the text section?</p>



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



<p class="wp-block-paragraph">So there&#8217;s the text section.</p>



<p class="wp-block-paragraph">So the data section is over now.</p>



<p class="wp-block-paragraph">So data section is over now.</p>



<p class="wp-block-paragraph">I&#8217;m going to make the tech section.</p>



<p class="wp-block-paragraph">I&#8217;m going to make a global entry point called start.</p>



<p class="wp-block-paragraph">Again, if you want to know more about assembly programming in general,</p>



<p class="wp-block-paragraph">see my other videos.</p>



<p class="wp-block-paragraph">But so this is the entry point of our program.</p>



<p class="wp-block-paragraph">At the very end, we should probably just program that it exits with success</p>



<p class="wp-block-paragraph">and test it out real fast.</p>



<p class="wp-block-paragraph">So basically right now this program does nothing.</p>



<p class="wp-block-paragraph">Let me just double check.</p>



<p class="wp-block-paragraph">Clear and make run.</p>



<p class="wp-block-paragraph">I have a make file under the hood, but this is not a make file video.</p>



<p class="wp-block-paragraph">If you want to know more about make files, see my other videos.</p>



<p class="wp-block-paragraph">know more about make files see my other videos but basically it just starts running and then</p>



<p class="wp-block-paragraph">it just dies right away okay no problem let me actually can i i want to fix that make file</p>



<p class="wp-block-paragraph">because it&#8217;s it&#8217;s labeled after like a different project that i was doing let&#8217;s do</p>



<p class="wp-block-paragraph">there&#8217;s no need for me to even do this i&#8217;m just uh i just can&#8217;t help myself okay so there we go</p>



<p class="wp-block-paragraph">going back to the editor.</p>



<p class="wp-block-paragraph">We have a program that does nothing.</p>



<p class="wp-block-paragraph">I&#8217;m going to copy paste in a couple of useful functions that I use elsewhere.</p>



<p class="wp-block-paragraph">Again, this is not an assembly video.</p>



<p class="wp-block-paragraph">So, so you can look at my code if you want to, if you just kind of want to know how this</p>



<p class="wp-block-paragraph">works, but not the point of this video.</p>



<p class="wp-block-paragraph">So I&#8217;m not going to spend too much time.</p>



<p class="wp-block-paragraph">Maybe I&#8217;ll just briefly talk about what it does, but not the point of this video.</p>



<p class="wp-block-paragraph">So the first thing I&#8217;m doing is I&#8217;m adding a function called print null terminated string</p>



<p class="wp-block-paragraph">function called print null terminated string just you know it sweeps through a</p>



<p class="wp-block-paragraph">string or actually it asks another function I made to find out how long the</p>



<p class="wp-block-paragraph">string is and then it&#8217;ll print the string to some file handle that I gave</p>



<p class="wp-block-paragraph">it like standard output or standard error and you know it calls on this other</p>



<p class="wp-block-paragraph">function called string length which I just wrote in pure assembly for fun even</p>



<p class="wp-block-paragraph">though I didn&#8217;t need to and it just kind of sweeps through the string until it</p>



<p class="wp-block-paragraph">finds a zero and then it knows how long the string is and then CRLF just prints</p>



<p class="wp-block-paragraph">is and then crlf just prints a carriage return new line uh carriage return new line just um</p>



<p class="wp-block-paragraph">with the same idea with the print null terminated string function and you can see all my strings have</p>



<p class="wp-block-paragraph">null terminators that&#8217;s the dot zero at the very end of it okay so we got those helper functions</p>



<p class="wp-block-paragraph">uh the next thing that i need to add in here is</p>



<p class="wp-block-paragraph">do an intro like my growling i used to have a professor who would go</p>



<p class="wp-block-paragraph">and he would go oh god when something went wrong and i do that all the time even when things are</p>



<p class="wp-block-paragraph">going right okay so i&#8217;m going to paste a little function here called intro and this is not going</p>



<p class="wp-block-paragraph">to be anything important it&#8217;s just going to print a little hello message you know it just uses my</p>



<p class="wp-block-paragraph">intro string and then just prints it with those other functions i made so that means now my my</p>



<p class="wp-block-paragraph">I can call intro real fast and then it&#8217;ll return when it&#8217;s done.</p>



<p class="wp-block-paragraph">If I just sort of run the program again, you can see after all the build messages,</p>



<p class="wp-block-paragraph">it&#8217;s just printing that the module has started.</p>



<p class="wp-block-paragraph">Okay, so nothing super important yet.</p>



<p class="wp-block-paragraph">Now, what I&#8217;d like to do is capture some incoming arguments real quick because…</p>



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



<p class="wp-block-paragraph">Well, that&#8217;s actually not going to work.</p>



<p class="wp-block-paragraph">Let&#8217;s see what I want to do here.</p>



<p class="wp-block-paragraph">No, I think that&#8217;s part of a different program.</p>



<p class="wp-block-paragraph">OK, I&#8217;m going to call a demo.</p>



<p class="wp-block-paragraph">I don&#8217;t think I need to add that one thing that I was just looking at.</p>



<p class="wp-block-paragraph">So I&#8217;m going to call a function that&#8217;s going to be our main demo.</p>



<p class="wp-block-paragraph">So it&#8217;s going to jump down somewhere.</p>



<p class="wp-block-paragraph">I&#8217;ll just create the function down later.</p>



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



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



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



<p class="wp-block-paragraph">I&#8217;m going to start off by saying this is my standard input function which is this and</p>



<p class="wp-block-paragraph">is this pop p and then pop r12 and then it returns if you don&#8217;t know what a prologue</p>



<p class="wp-block-paragraph">and epilogue are or why I&#8217;m doing pushes and pops just see my other videos but basically</p>



<p class="wp-block-paragraph">I&#8217;m responsible for preserving it.</p>



<p class="wp-block-paragraph">And I&#8217;m going to use the base pointer to help me remember where the stack started.</p>



<p class="wp-block-paragraph">I&#8217;m just going to choose to…</p>



<p class="wp-block-paragraph">What did I do?</p>



<p class="wp-block-paragraph">I started the wrong function.</p>



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



<p class="wp-block-paragraph">Okay, for now, let me go find where that other one is.</p>



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



<p class="wp-block-paragraph">Should have double checked.</p>



<p class="wp-block-paragraph">I&#8217;m going way too fast.</p>



<p class="wp-block-paragraph">I&#8217;ll just copy paste this whole thing from my solution.</p>



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



<p class="wp-block-paragraph">This video is a mess.</p>



<p class="wp-block-paragraph">Honestly, it&#8217;s a mess, but that&#8217;s okay.</p>



<p class="wp-block-paragraph">I&#8217;m going to try my best to explain it.</p>



<p class="wp-block-paragraph">So I have this function standard output standard error and I&#8217;m calling it here.</p>



<p class="wp-block-paragraph">So it should jump right down there as soon as the program starts.</p>



<p class="wp-block-paragraph">The standard input one is not getting called yet, but we&#8217;ll call it at the end of this</p>



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



<p class="wp-block-paragraph">print null terminated string function to print a message to standard output.</p>



<p class="wp-block-paragraph">Remember standard output was just you know pipe one. Standard error is going to</p>



<p class="wp-block-paragraph">be pipe two. So if I go back here and I say I want to print this matches to that</p>



<p class="wp-block-paragraph">pipe then it&#8217;ll go to standard output just like the echo program was doing</p>



<p class="wp-block-paragraph">when we looked at it a moment ago. And then next we&#8217;re going to print to</p>



<p class="wp-block-paragraph">Oh, that was just an announcement that we&#8217;re going to begin.</p>



<p class="wp-block-paragraph">So this is the real message that will say this is definitely going to standard output.</p>



<p class="wp-block-paragraph">And then this other message is going to go to standard error, which is when we when we</p>



<p class="wp-block-paragraph">looked at C error or pipe number two a moment ago.</p>



<p class="wp-block-paragraph">So basically, I&#8217;m just printing an announcement and then two important messages.</p>



<p class="wp-block-paragraph">One is going to pipe one and one is going to pipe two for standard output and standard</p>



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



<p class="wp-block-paragraph">OK, like really not very complicated at all.</p>



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



<p class="wp-block-paragraph">So if I run this program now, hopefully I didn&#8217;t crash it by copy pasting out of order here.</p>



<p class="wp-block-paragraph">Yeah, there we go.</p>



<p class="wp-block-paragraph">Clear and make run.</p>



<p class="wp-block-paragraph">So notice how it says that the messages are going to be printed.</p>



<p class="wp-block-paragraph">And then notice that both of my messages actually show up on the terminal.</p>



<p class="wp-block-paragraph">The one that says this message will print to standard output, number one, pipe one.</p>



<p class="wp-block-paragraph">This message will print to standard error, pipe two.</p>



<p class="wp-block-paragraph">They both get printed to the terminal because by default, the terminal will just take both of those pipes.</p>



<p class="wp-block-paragraph">both of those pipes it&#8217;ll take one and two standard output and error but I can</p>



<p class="wp-block-paragraph">use those little redirection tricks to mute one or more of those pipes so I&#8217;m</p>



<p class="wp-block-paragraph">gonna say pipe one which is standard output I&#8217;m just gonna redirect that to</p>



<p class="wp-block-paragraph">dev null to say I don&#8217;t want to see standard output if I run it again</p>



<p class="wp-block-paragraph">notice how the only thing you can see on the string or on the screen is standard</p>



<p class="wp-block-paragraph">error pipe to file descriptor 2 on the other hand if I redirect file descriptor</p>



<p class="wp-block-paragraph">descriptor to pipe two to nothing then I should see all the normal messages and not see the error</p>



<p class="wp-block-paragraph">message notice right here it&#8217;s it prints all the regular you know make file stuff and then the</p>



<p class="wp-block-paragraph">welcome message and then here&#8217;s our message that says this is going to be printed to standard output</p>



<p class="wp-block-paragraph">so notice how the error message is missing so cool if I mute both of those I can do that I can</p>



<p class="wp-block-paragraph">then nothing gets printed at all because that&#8217;s all we really have is standard output and standard</p>



<p class="wp-block-paragraph">error all right not too bad right that&#8217;s the basic idea between redirecting and things you</p>



<p class="wp-block-paragraph">could also do things like this if I okay this is not part of my plan but suppose I wanted to take</p>



<p class="wp-block-paragraph">any error message and redirect it to instead of dev null I wanted to redirect it to some kind of</p>



<p class="wp-block-paragraph">standard error.txt.</p>



<p class="wp-block-paragraph">Notice how nothing seems to have shown up on the screen, but if I list the directory,</p>



<p class="wp-block-paragraph">now there&#8217;s a text file called standard error that contains the error message.</p>



<p class="wp-block-paragraph">So you can redirect pipes, you can reattach them if you&#8217;re like an elite hacker.</p>



<p class="wp-block-paragraph">You can do lots of things with them, but that&#8217;s as far as I&#8217;m going to take it right now.</p>



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



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



<p class="wp-block-paragraph">How do you send input to the program?</p>



<p class="wp-block-paragraph">Remember when programs call other programs, they can send each other standard input too.</p>



<p class="wp-block-paragraph">It&#8217;s just that when I launch something on the terminal, I&#8217;m just, you know, I&#8217;m the</p>



<p class="wp-block-paragraph">human and so whatever I type or, you know, what command I type is going to go into standard</p>



<p class="wp-block-paragraph">input if I do it the right way.</p>



<p class="wp-block-paragraph">So let&#8217;s go back to the program here.</p>



<p class="wp-block-paragraph">Okay, so let&#8217;s finish up standard input.</p>



<p class="wp-block-paragraph">And let&#8217;s see the pre-made code that I had here.</p>



<p class="wp-block-paragraph">I had the prolog and the epilog, right?</p>



<p class="wp-block-paragraph">Move the base pointer</p>



<p class="wp-block-paragraph">RBP and R12. Oh, I got to put the</p>



<p class="wp-block-paragraph">Base pointer back into the stack pointer. Let&#8217;s see move</p>



<p class="wp-block-paragraph">Again, this is not an assembly video. So just try to hang on here. So what I&#8217;m gonna do in this video</p>



<p class="wp-block-paragraph">I thought I was cute when I was making this plan. I know I&#8217;m not like that clever, but um, I</p>



<p class="wp-block-paragraph">Thought hey wouldn&#8217;t it be fun instead of having a global</p>



<p class="wp-block-paragraph">or the BSS section. What if I just made the stack a buffer? This is kind of what you do when you make</p>



<p class="wp-block-paragraph">a local array in C++. You&#8217;re making a buffer on the stack. So this is not a stack pointer video.</p>



<p class="wp-block-paragraph">So long story short, I&#8217;m just going to keep track of where the stack pointer started with this base</p>



<p class="wp-block-paragraph">pointer. That&#8217;s why I have to preserve it there. And then I&#8217;m just going to subtract my buffer size</p>



<p class="wp-block-paragraph">from the stack pointer. And what is the buffer size? I&#8217;ve chosen to do a buffer size of about</p>



<p class="wp-block-paragraph">So that means the user or me, I could send about eight kilobytes, maybe eight kilobytes</p>



<p class="wp-block-paragraph">or less into standard input and be sure that it&#8217;s all actually going to be received by</p>



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



<p class="wp-block-paragraph">And then I&#8217;m just going to use R12 to remember where the start of the buffer is, which is</p>



<p class="wp-block-paragraph">just wherever the stack pointer started.</p>



<p class="wp-block-paragraph">Because if we subtract and then the data that increases is actually increasing in memory,</p>



<p class="wp-block-paragraph">but making room on the stack decreased, it&#8217;s just the way it works.</p>



<p class="wp-block-paragraph">There&#8217;s going to be another stack video out there.</p>



<p class="wp-block-paragraph">stack video out there anyway for now I just create a buffer on the stack and</p>



<p class="wp-block-paragraph">then I&#8217;m gonna print a little hello message so I&#8217;m gonna print this so just</p>



<p class="wp-block-paragraph">my message saying hey we&#8217;re about to do something from standard input and then</p>



<p class="wp-block-paragraph">I&#8217;m gonna send that to a standard output so the message is gonna go to standard</p>



<p class="wp-block-paragraph">output but I&#8217;m announcing that we&#8217;re about to read from standard input</p>



<p class="wp-block-paragraph">assuming I actually cleaned up correctly let me double check this this should</p>



<p class="wp-block-paragraph">It should just simply say that it&#8217;s going to without crashing.</p>



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



<p class="wp-block-paragraph">So then now I&#8217;m ready to actually read from standard input.</p>



<p class="wp-block-paragraph">We&#8217;re going to use a system call from this.</p>



<p class="wp-block-paragraph">If you want more information on system calls, see my other videos.</p>



<p class="wp-block-paragraph">But basically you just go to the system call codes table,</p>



<p class="wp-block-paragraph">look up appendix area of whatever textbook you&#8217;re using.</p>



<p class="wp-block-paragraph">And you say, what&#8217;s the code to read?</p>



<p class="wp-block-paragraph">I want the system to read.</p>



<p class="wp-block-paragraph">what you know i want the system to read so i define that as system read and if you look up</p>



<p class="wp-block-paragraph">here in my program it&#8217;s just call code zero so sys call code zero is to read from somewhere</p>



<p class="wp-block-paragraph">and then uh where do i want to read from that&#8217;s the second argument for the standard</p>



<p class="wp-block-paragraph">or for the system call i want to read from standard input that&#8217;s going to be the number zero</p>



<p class="wp-block-paragraph">if you recall and then here is a pointer to the first character of wherever i want it to</p>



<p class="wp-block-paragraph">read into. So it&#8217;s going to read from that pipe, but it has to, it has to write the data that it</p>



<p class="wp-block-paragraph">read somewhere so that I can save it and access it. So R12 is basically going to be a bunch of</p>



<p class="wp-block-paragraph">free bytes that I created on the stack. So I like, you know, I&#8217;m, I like made the stack bigger,</p>



<p class="wp-block-paragraph">but it&#8217;s mostly empty or with junk data. And R12 is just pointing to the first byte that I have</p>



<p class="wp-block-paragraph">reserved. So, you know, you could imagine maybe you make a global variable in the BSS section up</p>



<p class="wp-block-paragraph">up at the top, you know, like an eight kilobyte array of bytes, and then just sort of point</p>



<p class="wp-block-paragraph">it to that. But in my case, I&#8217;m just using the stack because I think it&#8217;s cooler. So</p>



<p class="wp-block-paragraph">I tell it, Hey, that&#8217;s the first bite on the stack that you want that I want you to start</p>



<p class="wp-block-paragraph">reading from. And then I give it the buffer size, which we just looked at. And then I</p>



<p class="wp-block-paragraph">do a system call and then the system now should do all the hard work for me. It&#8217;ll look at</p>



<p class="wp-block-paragraph">the pipe for standard input. It&#8217;ll grab all of it. And then it will only grab, you know,</p>



<p class="wp-block-paragraph">it&#8217;ll stop if the input ends before that and then when I&#8217;m done I should be able</p>



<p class="wp-block-paragraph">to actually read from the buffer which is sitting at R12 and print it to the</p>



<p class="wp-block-paragraph">screen so the next thing I&#8217;m gonna copy paste in there is I&#8217;m gonna print what</p>



<p class="wp-block-paragraph">I just read from standard input so this is another call to my function my little</p>



<p class="wp-block-paragraph">doggy wants to go pee even though he&#8217;s completely lying he just went pee so at</p>



<p class="wp-block-paragraph">wants to go outside and lift his leg and do nothing and then come back in and demand a treat.</p>



<p class="wp-block-paragraph">I&#8217;ll let him do it in a few minutes just in case but trust me he&#8217;s lying he&#8217;s laid like five times</p>



<p class="wp-block-paragraph">today. Anyway I sometimes I regret teaching him to growl at me. It&#8217;s so cute but like it&#8217;s not</p>



<p class="wp-block-paragraph">cute right now. I don&#8217;t know maybe it is. Let me know in the comments. So basically I&#8217;m going to</p>



<p class="wp-block-paragraph">take that R12 buffer that I just wrote to and I&#8217;m going to print that directly to standard output</p>



<p class="wp-block-paragraph">output and this is not really the important part of the video.</p>



<p class="wp-block-paragraph">So I&#8217;m going to do that and then I&#8217;m going to kind of clean up here at the end of the</p>



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



<p class="wp-block-paragraph">And then maybe I&#8217;ll say goodbye.</p>



<p class="wp-block-paragraph">I think I just added a string for that.</p>



<p class="wp-block-paragraph">So maybe I&#8217;ll say goodbye.</p>



<p class="wp-block-paragraph">I&#8217;ll say goodbye before we end the function and then I&#8217;ll actually return.</p>



<p class="wp-block-paragraph">So let me go here and run the program.</p>



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



<p class="wp-block-paragraph">printed because we didn&#8217;t send anything and we also didn&#8217;t call that let me call</p>



<p class="wp-block-paragraph">standard input call stdin and so now it&#8217;s actually gonna print basically</p>



<p class="wp-block-paragraph">nothing oh I didn&#8217;t send it anything so it&#8217;s just kind of hanging you probably</p>



<p class="wp-block-paragraph">want to check this sort of situation but that&#8217;s that&#8217;s not the point of this</p>



<p class="wp-block-paragraph">video let me show you how to pipe some some stuff into standard input real fast</p>



<p class="wp-block-paragraph">hello and then just put a little or bar in the shell this will take the output</p>



<p class="wp-block-paragraph">of echo and instead of printing it to the terminal it&#8217;ll attach its standard</p>



<p class="wp-block-paragraph">output into the standard input of make run pretty cool shell trick so now we</p>



<p class="wp-block-paragraph">should see the word hello the word hello printed and that&#8217;s what it does we have</p>



<p class="wp-block-paragraph">detected the following stdin and it says like here&#8217;s begin that&#8217;s just a string</p>



<p class="wp-block-paragraph">that I had that I print every time and then here&#8217;s the string that it actually</p>



<p class="wp-block-paragraph">And then it ends.</p>



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



<p class="wp-block-paragraph">I think maybe we could probably do the empty thing if I just started typing here.</p>



<p class="wp-block-paragraph">And then maybe I hit control D to detach the buffer.</p>



<p class="wp-block-paragraph">And yeah, that&#8217;s what it does.</p>



<p class="wp-block-paragraph">It sort of prints here and then it ends there.</p>



<p class="wp-block-paragraph">Didn&#8217;t do a new line.</p>



<p class="wp-block-paragraph">I think it probably doubled it for some reason because I…</p>



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



<p class="wp-block-paragraph">I didn&#8217;t plan for this.</p>



<p class="wp-block-paragraph">Let me see.</p>



<p class="wp-block-paragraph">plan for this let me see it printed oh because I did I did not null terminate the string so</p>



<p class="wp-block-paragraph">actually what it did is it uh let&#8217;s see if we can do that real fast one more time I&#8217;m going to go</p>



<p class="wp-block-paragraph">one two three four five then I&#8217;m going to just disconnect the standard input buffer because the</p>



<p class="wp-block-paragraph">printing function needs a null terminator to know when to stop there&#8217;s not going to be a null</p>



<p class="wp-block-paragraph">terminator because I just typed and disconnected the uh the input so it&#8217;s actually going to just</p>



<p class="wp-block-paragraph">until it randomly finds a zero and then it&#8217;ll stop so if I disconnect it it</p>



<p class="wp-block-paragraph">that explanation was bunk let me try that one more time I&#8217;m gonna go a bunch</p>



<p class="wp-block-paragraph">of H&#8217;s I guess there is a null terminator I disconnect it no no see how</p>



<p class="wp-block-paragraph">yeah these little symbols there so it does kind of print until it finds a</p>



<p class="wp-block-paragraph">null terminator but I guess I was confused because it seemed like it</p>



<p class="wp-block-paragraph">like it printed it twice what&#8217;s actually happening is when i type then it&#8217;s getting sent to the</p>



<p class="wp-block-paragraph">terminal which and that&#8217;s not part of the program that we just wrote then when i disconnect standard</p>



<p class="wp-block-paragraph">input then the program finally prints everything that i actually typed until it hits a null</p>



<p class="wp-block-paragraph">terminator in this case it looks like it seems to have worked but uh if i just do like a couple</p>



<p class="wp-block-paragraph">sevens and then disconnect probably have to do this a bunch of times before we&#8217;ll see those weird</p>



<p class="wp-block-paragraph">What did I do? Hit enter. That seems to have worked. My dog is stressing me out</p>



<p class="wp-block-paragraph">but I love him. Alright anyway just trust me on this sometimes it&#8217;s hitting the</p>



<p class="wp-block-paragraph">null terminator in time sometimes it&#8217;s not. But I hope by now you understand the</p>



<p class="wp-block-paragraph">difference between standard input standard output and standard error. Thank</p>



<p class="wp-block-paragraph">you so much for watching this video that I&#8217;m recording and I hope you learned a</p>



<p class="wp-block-paragraph">little bit of stuff and had a little bit of fun. I&#8217;m gonna let my dog lie to me</p>



<p class="wp-block-paragraph">I&#8217;m gonna let my dog lie to me for a second 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">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">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">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">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/mastering-standard-input-output-and-error-stdin-stdout-stderr-in-linux-with-yasm-assembly-examples/">Mastering Standard Input, Output, and Error (STDIN, STDOUT, STDERR) in Linux with Yasm Assembly Examples</a> appeared first on <a href="https://www.NeuralLantern.com">NeuralLantern.com</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.NeuralLantern.com/mastering-standard-input-output-and-error-stdin-stdout-stderr-in-linux-with-yasm-assembly-examples/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>
	</channel>
</rss>
