<?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>system calls Archives - NeuralLantern.com</title>
	<atom:link href="https://www.NeuralLantern.com/tag/system-calls/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.NeuralLantern.com/tag/system-calls/</link>
	<description></description>
	<lastBuildDate>Sun, 05 Oct 2025 11:22:41 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=7.0.1</generator>

<image>
	<url>https://www.NeuralLantern.com/wp-content/uploads/2025/04/cropped-2025-04-04-Lantern-03-32x32.png</url>
	<title>system calls Archives - NeuralLantern.com</title>
	<link>https://www.NeuralLantern.com/tag/system-calls/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>Master File Copying with System Calls in x86-64 YASM Assembly</title>
		<link>https://www.NeuralLantern.com/master-file-copying-with-system-calls-in-x86-64-yasm-assembly/</link>
					<comments>https://www.NeuralLantern.com/master-file-copying-with-system-calls-in-x86-64-yasm-assembly/#respond</comments>
		
		<dc:creator><![CDATA[mike]]></dc:creator>
		<pubDate>Sun, 05 Oct 2025 11:22:39 +0000</pubDate>
				<category><![CDATA[Assembly Language]]></category>
		<category><![CDATA[Coding]]></category>
		<category><![CDATA[Computer Science]]></category>
		<category><![CDATA[Videos]]></category>
		<category><![CDATA[assembly language]]></category>
		<category><![CDATA[assembly programming]]></category>
		<category><![CDATA[coding]]></category>
		<category><![CDATA[file copying]]></category>
		<category><![CDATA[file handling]]></category>
		<category><![CDATA[file I/O]]></category>
		<category><![CDATA[file permissions]]></category>
		<category><![CDATA[Linux system calls]]></category>
		<category><![CDATA[low-level programming]]></category>
		<category><![CDATA[MD5 checksum]]></category>
		<category><![CDATA[programming tutorial]]></category>
		<category><![CDATA[software development]]></category>
		<category><![CDATA[system calls]]></category>
		<category><![CDATA[x86-64 assembly]]></category>
		<category><![CDATA[Yasm assembly]]></category>
		<guid isPermaLink="false">https://www.NeuralLantern.com/?p=237</guid>

					<description><![CDATA[<p>Learn file copying in x86-64 YASM assembly with system calls. This tutorial covers opening files, using looping buffers, and verifying with MD5 checksums.</p>
<p>The post <a href="https://www.NeuralLantern.com/master-file-copying-with-system-calls-in-x86-64-yasm-assembly/">Master File Copying with System Calls in x86-64 YASM Assembly</a> appeared first on <a href="https://www.NeuralLantern.com">NeuralLantern.com</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<figure class="wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio"><div class="wp-block-embed__wrapper">
<iframe title="Master File Copying with System Calls in x86-64 YASM Assembly" width="1380" height="776" src="https://www.youtube.com/embed/hvq4VGqA6Ks?feature=oembed" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen></iframe>
</div></figure>



<p class="wp-block-paragraph">Join me as I break down a simple yet powerful x86-64 YASM assembly program to copy files using system calls! Learn how to open input files, create output files, and use a looping buffer for efficient data transfer. I’ll demo the code, explain file handles, permissions, and error handling, and even verify the copy with MD5 checksums. Perfect for intermediate assembly programmers or anyone curious about low-level file operations. Check out my other videos for more assembly tips, and don’t forget to subscribe!</p>



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



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



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



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



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



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



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



<li>Subscribing to our Blog</li>



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



<p class="wp-block-paragraph">Hey everybody in this video i&#8217;m going to show and explain a simple program that copies a file</p>



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



<p class="wp-block-paragraph">Nice, okay, so if I run it you can see that the the make file system again</p>



<p class="wp-block-paragraph">file system again this is not a make file video so I&#8217;m not going to show you</p>



<p class="wp-block-paragraph">my make file see my other videos if you want to learn more about make files but</p>



<p class="wp-block-paragraph">you can see that the assembly program says that it started and then it just</p>



<p class="wp-block-paragraph">exited then the make file continues to run these extra commands that I have</p>



<p class="wp-block-paragraph">set up so this is not part of the assembly program this is just taking a</p>



<p class="wp-block-paragraph">checksum an MD5 checksum of the input file and then an MD5 checksum of the</p>



<p class="wp-block-paragraph">what empty five checksums are i&#8217;ll probably release a video sometime in the future if you&#8217;re</p>



<p class="wp-block-paragraph">interested on all my platforms just talking about why we why you would use a checksum but for now</p>



<p class="wp-block-paragraph">just imagine this is a fingerprint so if the fingerprints don&#8217;t match that means the files</p>



<p class="wp-block-paragraph">don&#8217;t match right now the output file doesn&#8217;t even exist so it just is an error and that&#8217;s why the</p>



<p class="wp-block-paragraph">make file thinks there&#8217;s an error because the output file didn&#8217;t exist so that&#8217;s the basics</p>



<p class="wp-block-paragraph">of kind of getting started with this you know bare bones program now let&#8217;s start looking at running</p>



<p class="wp-block-paragraph">program. Now let&#8217;s start looking at running the file tests. So file tests, we got to make a new</p>



<p class="wp-block-paragraph">method. I&#8217;m going to stick it down here. Let&#8217;s see. I got a solution up so that I don&#8217;t have to</p>



<p class="wp-block-paragraph">spend too much time typing. I&#8217;m going to try my best to balance between copy pasting and just</p>



<p class="wp-block-paragraph">typing very quickly. But let&#8217;s see, where&#8217;s the file test? So we got this. I&#8217;m going to put the</p>



<p class="wp-block-paragraph">I&#8217;m feeling pretty lazy.</p>



<p class="wp-block-paragraph">I&#8217;m just going to copy paste the whole thing.</p>



<p class="wp-block-paragraph">Copy paste the whole thing.</p>



<p class="wp-block-paragraph">So now this method right here is going to get called the file tests.</p>



<p class="wp-block-paragraph">I keep saying method because I teach C++ a lot too.</p>



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



<p class="wp-block-paragraph">If we go down further, this function is called file tests.</p>



<p class="wp-block-paragraph">Here&#8217;s the signature.</p>



<p class="wp-block-paragraph">It doesn&#8217;t take any arguments.</p>



<p class="wp-block-paragraph">It just kind of does stuff.</p>



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



<p class="wp-block-paragraph">And here are the registers that I&#8217;m going to use.</p>



<p class="wp-block-paragraph">the input file handle, the output file handle, and then the count of bytes read from the input</p>



<p class="wp-block-paragraph">file at any given time inside of our looping buffer reader section. So the first thing I&#8217;ve</p>



<p class="wp-block-paragraph">got to do is I&#8217;ve got to open a file to read. The second thing I&#8217;ve got to do is create a file to</p>



<p class="wp-block-paragraph">write. And then I&#8217;m going to copy the input file to the output file. And then I&#8217;m going to print</p>



<p class="wp-block-paragraph">a message saying, hey, everything was successful. And then I&#8217;m going to close both files. Notice how</p>



<p class="wp-block-paragraph">anything i&#8217;m just sort of calling other functions do the work for me again if you don&#8217;t know the</p>



<p class="wp-block-paragraph">prologue and epilogue stuff or calling functions and returning you should see my other videos</p>



<p class="wp-block-paragraph">but i just like to use helper functions assembly is like so unwieldy right it&#8217;s it just gets out</p>



<p class="wp-block-paragraph">of control so quick and so confusing so fast so anytime you can just you know take a chunk out</p>



<p class="wp-block-paragraph">of your assembly code and move it somewhere to another module or to another function</p>



<p class="wp-block-paragraph">going to make your life a lot easier and make debugging a lot easier people who try to write</p>



<p class="wp-block-paragraph">the entire portion of their program in just one gigantic function those are the people who usually</p>



<p class="wp-block-paragraph">end up spending 10 times longer debugging for no reason at all so i believe in the power of modular</p>



<p class="wp-block-paragraph">thinking so anyway what are we going to do inside of the open file read function it&#8217;s not too bad</p>



<p class="wp-block-paragraph">to be honest let&#8217;s see if i can find it real quick open file read i&#8217;m going to copy paste it because</p>



<p class="wp-block-paragraph">Again, I&#8217;m pretty lazy.</p>



<p class="wp-block-paragraph">I&#8217;ll just explain what it is though.</p>



<p class="wp-block-paragraph">So down here we need a function called open file read.</p>



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



<p class="wp-block-paragraph">And you can see the signature that I&#8217;ve chosen for this is, you know, I like to</p>



<p class="wp-block-paragraph">write all my functions in C++, uh, kind of lingo or prototypes so that I can have a</p>



<p class="wp-block-paragraph">better understanding of what the assembly is actually supposed to do.</p>



<p class="wp-block-paragraph">So you can see the function called open file read.</p>



<p class="wp-block-paragraph">I want it to take two arguments.</p>



<p class="wp-block-paragraph">two arguments the first argument should be a pointer to a string that represents the file</p>



<p class="wp-block-paragraph">name that i want to open and it should be null terminated again if you look back at my uh at</p>



<p class="wp-block-paragraph">my strings up here they all have little zeros at the end so they are all null terminated anyway uh</p>



<p class="wp-block-paragraph">so that&#8217;s the first argument that&#8217;s going to show up as rdi in assembly and then the second argument</p>



<p class="wp-block-paragraph">is the flags for opening the file so i think probably that was redundant if i if i name this</p>



<p class="wp-block-paragraph">that was redundant if i if i name this open file read then i think it should be obvious that the</p>



<p class="wp-block-paragraph">flags are just going to be the read flags only so i probably didn&#8217;t even need to provide this</p>



<p class="wp-block-paragraph">that was bad design on my part you could write a better one on your own where it&#8217;s just one argument</p>



<p class="wp-block-paragraph">and if the name is read then just use the read flags but if the maybe if you want to leave the</p>



<p class="wp-block-paragraph">flags in there you could just say open file only it&#8217;s up to you anyway so it attempts to open a</p>



<p class="wp-block-paragraph">successful it&#8217;ll return the file handle that&#8217;s the long return type right here</p>



<p class="wp-block-paragraph">long file handle if it fails then it will just basically complain and exit</p>



<p class="wp-block-paragraph">the program you probably want a more sophisticated way of handling errors in</p>



<p class="wp-block-paragraph">your program I just decided to complain and exit the entire program because this</p>



<p class="wp-block-paragraph">is you know not supposed to be super complicated I just want to show you how</p>



<p class="wp-block-paragraph">to copy files and then for me I like to leave comments that just sort of explain</p>



<p class="wp-block-paragraph">And once my functions get so complicated that I actually run out of registers,</p>



<p class="wp-block-paragraph">then that tells me I probably need to just make another function,</p>



<p class="wp-block-paragraph">you know, split the work up in some way.</p>



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



<p class="wp-block-paragraph">I only end up reusing the same registers for multiple purposes occasionally.</p>



<p class="wp-block-paragraph">I&#8217;m not a hardcore assembly programmer.</p>



<p class="wp-block-paragraph">I&#8217;m just, you know, I&#8217;m like medium.</p>



<p class="wp-block-paragraph">So I&#8217;m going to use R12 and 13 and 14.</p>



<p class="wp-block-paragraph">That&#8217;s why we have the prolog pushing all those registers to preserve them</p>



<p class="wp-block-paragraph">pushing all those registers to preserve them because they&#8217;re callee saved and then the epilogue</p>



<p class="wp-block-paragraph">epilogue that just kind of pops them i&#8217;ve got a label here for the function remember a function</p>



<p class="wp-block-paragraph">is just a label and then a return statement as long as you obey and respect the abi like preserving</p>



<p class="wp-block-paragraph">certain registers then you should be okay first thing i&#8217;m going to do is grab the incoming</p>



<p class="wp-block-paragraph">arguments so i&#8217;m going to grab the file name which is a pointer which means it&#8217;s just a 64-bit</p>



<p class="wp-block-paragraph">integer i can stick that into a register and then the flags same thing so i&#8217;m going to grab</p>



<p class="wp-block-paragraph">rdi and rsi the incoming arguments i&#8217;m going to stick them into r12 and r13 so the character</p>



<p class="wp-block-paragraph">pointer in the flags and then i&#8217;m going to attempt to open the file with a system call so</p>



<p class="wp-block-paragraph">system call is right there line 168 if you look up the table for system calls in my favorite book</p>



<p class="wp-block-paragraph">that i usually recommend or any table that knows the system call codes for x86-64 assembly</p>



<p class="wp-block-paragraph">call code to open a file is some number i&#8217;ve assigned that to sys open if we look up here</p>



<p class="wp-block-paragraph">open is code 2 so that&#8217;s why i you know i don&#8217;t want to remember the numbers it&#8217;s bad to hard</p>



<p class="wp-block-paragraph">code number so i just i just put it as a define and then the first argument that it wants in rdi</p>



<p class="wp-block-paragraph">is the name of the file so i just gave it r12 i guess i probably could have just used rdi</p>



<p class="wp-block-paragraph">directly in the system call but that tends to make me nervous reusing the argument registers</p>



<p class="wp-block-paragraph">registers, I like to have them somewhere where they&#8217;re not going to be destroyed.</p>



<p class="wp-block-paragraph">If I like, let&#8217;s say I accidentally added some code here on line 163.</p>



<p class="wp-block-paragraph">If I wanted to reuse RDI and I accidentally added some code there,</p>



<p class="wp-block-paragraph">then RDI would have been destroyed.</p>



<p class="wp-block-paragraph">Would have cost me a bunch of time debugging my program.</p>



<p class="wp-block-paragraph">Although I admit it&#8217;s not super efficient to do it that way.</p>



<p class="wp-block-paragraph">Then we&#8217;re going to do the file status</p>



<p class="wp-block-paragraph">flags as the second argument, and then we just do a system call right away.</p>



<p class="wp-block-paragraph">register gives you the result of your system call.</p>



<p class="wp-block-paragraph">Notice how we use RAX to send in the call code.</p>



<p class="wp-block-paragraph">And then the system call sends us back its return result also in the RAX</p>



<p class="wp-block-paragraph">register. So I&#8217;m just going to save that right away to R14.</p>



<p class="wp-block-paragraph">You can see up here, it&#8217;s just the file handle.</p>



<p class="wp-block-paragraph">So we can assume maybe at this point that we have a file handle sitting in R14.</p>



<p class="wp-block-paragraph">You know, what is a file handle when you ask the system to open a file for you?</p>



<p class="wp-block-paragraph">The operating system under the hood is just going to do a bunch of stuff to</p>



<p class="wp-block-paragraph">bunch of stuff to actually open a file it&#8217;s going to go it&#8217;s going to take the string</p>



<p class="wp-block-paragraph">that you sent it and it&#8217;s going to parse it and figure out you know how do i how do i interpret</p>



<p class="wp-block-paragraph">where that actually is on disk i&#8217;m going to look at like the file system in the past that you</p>



<p class="wp-block-paragraph">provided i&#8217;m going to look at the mount points and i&#8217;m going to figure out like where exactly on disk</p>



<p class="wp-block-paragraph">does that file start and then the operating system stores that the operating system stores the file</p>



<p class="wp-block-paragraph">name it stores where you&#8217;re looking at the file currently stores a bunch of stuff that you don&#8217;t</p>



<p class="wp-block-paragraph">a bunch of stuff that you don&#8217;t want to have to remember you know it creates data structures under</p>



<p class="wp-block-paragraph">the hood and all it&#8217;ll give you back in return is a file handle for simplicity because then later</p>



<p class="wp-block-paragraph">you can use that file handle to just sort of say I would like to write some bytes to a file or I</p>



<p class="wp-block-paragraph">would like to read some bytes from an open file here&#8217;s the handle you gave me previously and then</p>



<p class="wp-block-paragraph">it&#8217;ll just work assuming you have a valid handle so the handle is kind of the most important part</p>



<p class="wp-block-paragraph">failed because it&#8217;s a mistake i&#8217;ve said this in other videos it&#8217;s a mistake that new programmers</p>



<p class="wp-block-paragraph">make or lazy programmers make uh let&#8217;s suppose for the sake of argument uh uh file open sys call</p>



<p class="wp-block-paragraph">pretend we&#8217;re in c plus plus and there is some sort of an api function that we can call either</p>



<p class="wp-block-paragraph">directly to the system or some person&#8217;s library and it&#8217;ll open a file for us so maybe like a</p>



<p class="wp-block-paragraph">just say we call this right so i&#8217;m going to do like a little comment if inside of your program you just</p>



<p class="wp-block-paragraph">call it with the you know some some path and then you assume that what you have is a valid you know</p>



<p class="wp-block-paragraph">handle if you assume that what you got back is a valid handle or maybe this is not a function</p>



<p class="wp-block-paragraph">just assume that the call succeeded, your program is probably going to have errors when you least</p>



<p class="wp-block-paragraph">expect it. And it&#8217;s not going to look good. Especially if you release a function like this</p>



<p class="wp-block-paragraph">to the public, or if you have like a professor who&#8217;s like grading your code and they are testing</p>



<p class="wp-block-paragraph">to see if you&#8217;re checking for return codes and stuff like that. That&#8217;s not a smart idea, right?</p>



<p class="wp-block-paragraph">Like you shouldn&#8217;t, should not, shouldn&#8217;t proceed as if everything went according to plan. What,</p>



<p class="wp-block-paragraph">just proceeding as if everything went according to plan.</p>



<p class="wp-block-paragraph">You want to use an if statement, right?</p>



<p class="wp-block-paragraph">You want to say if the handle has some value that seems to be valid,</p>



<p class="wp-block-paragraph">like for example, more than zero in the case of opening a file,</p>



<p class="wp-block-paragraph">I would say probably more than two because all of our programs</p>



<p class="wp-block-paragraph">always have automatically assigned file handles of zero and one and two</p>



<p class="wp-block-paragraph">to represent standard input, standard output, and standard error.</p>



<p class="wp-block-paragraph">But I think usually people just say like,</p>



<p class="wp-block-paragraph">if it&#8217;s greater than zero, then it&#8217;s fine.</p>



<p class="wp-block-paragraph">zero then it&#8217;s fine uh you know for me i might put greater than two but more than zero is fine</p>



<p class="wp-block-paragraph">the point is check to make sure that it actually succeeded</p>



<p class="wp-block-paragraph">do i have spell check on this thing oh god you&#8217;re all going to see my true spelling let me see if i</p>



<p class="wp-block-paragraph">can get it on real fast plug-ins what how come this oh okay now it&#8217;s highlighted</p>



<p class="wp-block-paragraph">If we&#8217;ve succeeded based on some kind of a comparison of the return result,</p>



<p class="wp-block-paragraph">then we&#8217;ll proceed in one way.</p>



<p class="wp-block-paragraph">Otherwise we&#8217;ll respond to the error by, you know, doing something else.</p>



<p class="wp-block-paragraph">Somehow, like writing a log file, sending an email, complaining to the user,</p>



<p class="wp-block-paragraph">doing any number of things where you can actually respond to the error.</p>



<p class="wp-block-paragraph">Maybe you probably want to change your execution path.</p>



<p class="wp-block-paragraph">Like if the file successfully opened, then go ahead and start writing to it</p>



<p class="wp-block-paragraph">or reading from it or whatever.</p>



<p class="wp-block-paragraph">But if the file did not successfully open,</p>



<p class="wp-block-paragraph">want to do something else in the program and not just start trying to read from the file so anyway</p>



<p class="wp-block-paragraph">super super good idea and so that&#8217;s why i&#8217;m going to implement that inside of assembly 2. so instead</p>



<p class="wp-block-paragraph">of just immediately using the file handle i&#8217;m going to check it i&#8217;m going to say let&#8217;s compare</p>



<p class="wp-block-paragraph">it to the number zero if we succeeded then let&#8217;s go to another label called uh read success i</p>



<p class="wp-block-paragraph">personally when i&#8217;m doing branching logic i like to say open file read is the name of the function</p>



<p class="wp-block-paragraph">read is the name of the function and I&#8217;ll just I&#8217;ll just uh uh append some kind of a suffix</p>



<p class="wp-block-paragraph">to the original function name that way it&#8217;s it&#8217;s easier to avoid collisions when you have lots of</p>



<p class="wp-block-paragraph">functions and lots of labels and things like that so I&#8217;m basically saying if we succeeded I&#8217;m going</p>



<p class="wp-block-paragraph">to jump to this label which is down here and so uh this video also is not about branching logic</p>



<p class="wp-block-paragraph">and and how to implement those instructions you can probably infer it by looking at my code but</p>



<p class="wp-block-paragraph">it by looking at my code but you know see another video in the future for that topic anyway if we</p>



<p class="wp-block-paragraph">succeeded in opening the file handle then we&#8217;re just going to say oh we were successful and then</p>



<p class="wp-block-paragraph">we&#8217;re going to uh um let&#8217;s see we&#8217;re going to print the name of the file that we just successfully</p>



<p class="wp-block-paragraph">opened and then we&#8217;re going to send the file handle into rax so that this function has a return</p>



<p class="wp-block-paragraph">So when you open a file to read successfully, the caller will receive the file handle.</p>



<p class="wp-block-paragraph">You might be wondering yourself, wait a minute, wait a minute, wait a minute.</p>



<p class="wp-block-paragraph">It just gave us the file handle in RAX.</p>



<p class="wp-block-paragraph">Remember, we got to respect the ABI.</p>



<p class="wp-block-paragraph">Anytime we jump anywhere or call another function or call another syscall,</p>



<p class="wp-block-paragraph">which a lot of these things do,</p>



<p class="wp-block-paragraph">we will probably lose the value of registers that are not callee preserved.</p>



<p class="wp-block-paragraph">And that&#8217;s definitely RAX.</p>



<p class="wp-block-paragraph">I mean, just doing any system call like this print null terminated string function does.</p>



<p class="wp-block-paragraph">print null terminated string function does that&#8217;s going to destroy rex so that&#8217;s why i saved it away</p>



<p class="wp-block-paragraph">first in r14 then at the very end of the function right before i return i&#8217;m just going to grab r14</p>



<p class="wp-block-paragraph">and send it into rex again respect the abi do not return data as a return value in r14 or any other</p>



<p class="wp-block-paragraph">register you have to use the designated registers and in this case an integer that you&#8217;re returning</p>



<p class="wp-block-paragraph">x anyway so that&#8217;s the gist of that let me go back up for a second uh so notice how</p>



<p class="wp-block-paragraph">we&#8217;re we&#8217;re sort of comparing oh gosh i just i just reconfigured my annotator and i bought a</p>



<p class="wp-block-paragraph">new drawpad i wonder if this is going to work ah it works there&#8217;s a bunch of stuff i added too</p>



<p class="wp-block-paragraph">so notice how uh we try to open the file and then we sort of compare the file handle to see if we</p>



<p class="wp-block-paragraph">off to the success area down here oh my green&#8217;s not working oh it&#8217;s tricky it&#8217;s tricky i gotta</p>



<p class="wp-block-paragraph">hit it in a certain way there we go if it succeeds we go down here to the success label so we</p>



<p class="wp-block-paragraph">basically at that point um totally ignore all of the fail code right we&#8217;re just like branching</p>



<p class="wp-block-paragraph">around on the other hand if it fails then execution falls through because it&#8217;s not going to jump</p>



<p class="wp-block-paragraph">if uh if if r14 is not greater than or equal to zero so if we fail maybe i should put this in a</p>



<p class="wp-block-paragraph">red if we fail then it&#8217;s going to fall through to the next label and then the next instructions</p>



<p class="wp-block-paragraph">some people like to say let&#8217;s uh let&#8217;s jump to the success label and then if not let&#8217;s jump to</p>



<p class="wp-block-paragraph">the fail label that can buy you a little bit more uh i don&#8217;t know jump length if you have a giant</p>



<p class="wp-block-paragraph">program but in this case i&#8217;m just going to let it fall through it saves us one jump instruction</p>



<p class="wp-block-paragraph">and so if it fails then it&#8217;s going to say first off let&#8217;s print a message that we failed</p>



<p class="wp-block-paragraph">to standard error if you want a refresher on standard input standard output and standard error</p>



<p class="wp-block-paragraph">see my other videos and then it&#8217;s going to print the name of the file that failed so that&#8217;s this</p>



<p class="wp-block-paragraph">part right here it&#8217;s going to say hey we we failed to print or sorry we failed to open this file name</p>



<p class="wp-block-paragraph">for reading and it&#8217;s going to print a new line there with the crlf thing</p>



<p class="wp-block-paragraph">and then it&#8217;s actually going to exit the program at that point it&#8217;s going to say all right we</p>



<p class="wp-block-paragraph">failed and so the whole program just just quits and that&#8217;s my die function that i showed you</p>



<p class="wp-block-paragraph">earlier it&#8217;s just going to call the system call code for quitting and it&#8217;s going to give a return</p>



<p class="wp-block-paragraph">code of one to indicate to anyone automating our program including new make that well our program</p>



<p class="wp-block-paragraph">failed at least it failed okay so we got all that and then we got the success and so now you kind</p>



<p class="wp-block-paragraph">of know the idea behind opening a file let&#8217;s do that real fast just for fun so file tests</p>



<p class="wp-block-paragraph">um oh i know what to do i&#8217;ll just comment out these uh instructions just so that we&#8217;re only</p>



<p class="wp-block-paragraph">doing the file open yeah that&#8217;s pretty good and then we won&#8217;t close anything just yet</p>



<p class="wp-block-paragraph">so now let me run this in the terminal real fast make run it says it successfully opened</p>



<p class="wp-block-paragraph">it says it successfully opened the file input.txt</p>



<p class="wp-block-paragraph">and then the program exited.</p>



<p class="wp-block-paragraph">No problem, that error code one,</p>



<p class="wp-block-paragraph">that&#8217;s because the output file doesn&#8217;t actually exist.</p>



<p class="wp-block-paragraph">So don&#8217;t worry about that.</p>



<p class="wp-block-paragraph">And let&#8217;s change the name of the input file</p>



<p class="wp-block-paragraph">just to show you that it can fail and we can detect it.</p>



<p class="wp-block-paragraph">I&#8217;m gonna put a two there</p>



<p class="wp-block-paragraph">so that the program will try to open an input file</p>



<p class="wp-block-paragraph">that doesn&#8217;t actually exist.</p>



<p class="wp-block-paragraph">And then I&#8217;ll run it again.</p>



<p class="wp-block-paragraph">Notice how it says fail to open file input2.txt.</p>



<p class="wp-block-paragraph">And then it says terminating program after failure to open file.</p>



<p class="wp-block-paragraph">And then this time, notice how the make file never got far enough</p>



<p class="wp-block-paragraph">so that it tried to print the MD5 sums of the input and output files.</p>



<p class="wp-block-paragraph">The assembly program just failed.</p>



<p class="wp-block-paragraph">And so GNU make said, I&#8217;m not going to proceed any further.</p>



<p class="wp-block-paragraph">Kind of useful when your program gives good exit codes, right?</p>



<p class="wp-block-paragraph">Because then other programs know when to stop</p>



<p class="wp-block-paragraph">or maybe what to do depending on what&#8217;s happening.</p>



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



<p class="wp-block-paragraph">down here and maybe the next thing I want to add is closing the files so we&#8217;ve got a function to</p>



<p class="wp-block-paragraph">open a file for reading and then we have a function down here that is not implemented yet</p>



<p class="wp-block-paragraph">for closing the files so I&#8217;m going to do let&#8217;s see what is r12 r12 is the input file so I&#8217;m</p>



<p class="wp-block-paragraph">going to close r12 so basically this function it&#8217;s going to it&#8217;s going to take one argument</p>



<p class="wp-block-paragraph">handle. So that&#8217;s why I&#8217;m giving the file handle of the input file as the first argument. And then</p>



<p class="wp-block-paragraph">I&#8217;m just going to call it. So let&#8217;s copy paste close file underneath this. So I&#8217;m going to do</p>



<p class="wp-block-paragraph">like some more space. And let me let me go get this from my solution close file.</p>



<p class="wp-block-paragraph">And it&#8217;s going to be a pretty simple one. Really nothing much to it. I&#8217;m just going to write my</p>



<p class="wp-block-paragraph">C++ prototype saying, well, it takes in one argument and it&#8217;s a handle. It doesn&#8217;t return</p>



<p class="wp-block-paragraph">argument and it&#8217;s a handle it doesn&#8217;t return anything it attempts to close a</p>



<p class="wp-block-paragraph">file and I&#8217;m just gonna use R12 to hold the incoming argument your programs at</p>



<p class="wp-block-paragraph">home should probably be a little bit better than mine you should check to see</p>



<p class="wp-block-paragraph">if the file successfully closed or not and respond in some kind of a way but</p>



<p class="wp-block-paragraph">for me I&#8217;m just saying I don&#8217;t really care I already showed you that we can</p>



<p class="wp-block-paragraph">check for a return value so I&#8217;m allowed to be lazy now and just sort of try to</p>



<p class="wp-block-paragraph">close it and then just assume it all went according to plan so grabbing the</p>



<p class="wp-block-paragraph">So grabbing the incoming arguments here.</p>



<p class="wp-block-paragraph">That&#8217;s why I&#8217;m using R12.</p>



<p class="wp-block-paragraph">That&#8217;s why I&#8217;m doing a push-pop pair.</p>



<p class="wp-block-paragraph">And then the system call code to close is pretty easy.</p>



<p class="wp-block-paragraph">You just say, here&#8217;s the code to close.</p>



<p class="wp-block-paragraph">Stick that in RAX.</p>



<p class="wp-block-paragraph">Let me go up real fast.</p>



<p class="wp-block-paragraph">Notice how sysclose is the call code number three</p>



<p class="wp-block-paragraph">on line 43 there.</p>



<p class="wp-block-paragraph">So I send it the call code three to say, let&#8217;s close a file.</p>



<p class="wp-block-paragraph">It only wants one argument, which is just,</p>



<p class="wp-block-paragraph">what is the file handle that you want me to close?</p>



<p class="wp-block-paragraph">what is the file handle that you want me to close remember before the operating system created a</p>



<p class="wp-block-paragraph">bunch of stuff under the hood and gave you a file handle you can then use the file handle</p>



<p class="wp-block-paragraph">to close a file read a file write to the file whatever to the file so it&#8217;s pretty easy once</p>



<p class="wp-block-paragraph">i&#8217;ve set up the incoming arguments to the system then i actually use the system call instruction</p>



<p class="wp-block-paragraph">syscall and then i can assume it&#8217;s probably closed at that point then a return statement at the end</p>



<p class="wp-block-paragraph">like any additional thing happening.</p>



<p class="wp-block-paragraph">It should just be, it said it successfully opened the file</p>



<p class="wp-block-paragraph">and then it just exited.</p>



<p class="wp-block-paragraph">Okay, now we&#8217;re ready to add a little bit more.</p>



<p class="wp-block-paragraph">Let&#8217;s create a file to write.</p>



<p class="wp-block-paragraph">So this is going to be the same thing basically</p>



<p class="wp-block-paragraph">as opening a file to read,</p>



<p class="wp-block-paragraph">except it&#8217;s going to be a different call code</p>



<p class="wp-block-paragraph">and we&#8217;ll give it initial file permissions</p>



<p class="wp-block-paragraph">rather than a read mode flag.</p>



<p class="wp-block-paragraph">But then we&#8217;ll just get a file handle in return.</p>



<p class="wp-block-paragraph">get a file handle in return.</p>



<p class="wp-block-paragraph">And I&#8217;ve also stuck this into another function, of course.</p>



<p class="wp-block-paragraph">So the file name to write line 109.</p>



<p class="wp-block-paragraph">If you just scroll up real fast here, or if I scroll up real fast,</p>



<p class="wp-block-paragraph">it&#8217;s a line 27 here, just output.txt.</p>



<p class="wp-block-paragraph">And then the file permissions that we want to use.</p>



<p class="wp-block-paragraph">I&#8217;m going to scroll up here.</p>



<p class="wp-block-paragraph">That&#8217;s the second argument to the system call code.</p>



<p class="wp-block-paragraph">That&#8217;s just the stuff that I talked about a little while ago where it&#8217;s like,</p>



<p class="wp-block-paragraph">we want the user to be able to read and write to the file.</p>



<p class="wp-block-paragraph">to be able to read and write to the file we want people in the same group to be able to read only</p>



<p class="wp-block-paragraph">and we want everyone else to not be able to do anything basic linux permissions not a big deal</p>



<p class="wp-block-paragraph">at the end you should probably maybe note that uh you know this q i think i might have said this</p>



<p class="wp-block-paragraph">before this q just means quad word and these zeros are always going to be the same so really it&#8217;s just</p>



<p class="wp-block-paragraph">like three numbers representing file permissions i&#8217;ll go over that in more detail in some other</p>



<p class="wp-block-paragraph">So for now, we know that we&#8217;re going to open a file name to we&#8217;re going to open a file</p>



<p class="wp-block-paragraph">for writing doesn&#8217;t have to exist yet.</p>



<p class="wp-block-paragraph">We&#8217;ll give it some default permissions.</p>



<p class="wp-block-paragraph">And those are the incoming arguments to the function that we&#8217;re going to call now called</p>



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



<p class="wp-block-paragraph">When that file when that function comes back, assuming it didn&#8217;t decide to exit the program</p>



<p class="wp-block-paragraph">because we failed to open the file, we will receive the file handle in our ex per usual.</p>



<p class="wp-block-paragraph">And so I&#8217;m just going to stash that away real fast into our 13.</p>



<p class="wp-block-paragraph">Remember R12 has the handle to the input file.</p>



<p class="wp-block-paragraph">R13 has the handle to the output file.</p>



<p class="wp-block-paragraph">And then, you know, for me personally,</p>



<p class="wp-block-paragraph">I put that in comments to help myself remember</p>



<p class="wp-block-paragraph">what I&#8217;m even doing,</p>



<p class="wp-block-paragraph">because things can get confusing really, really fast.</p>



<p class="wp-block-paragraph">And while we&#8217;re at it,</p>



<p class="wp-block-paragraph">before we even write the create file function,</p>



<p class="wp-block-paragraph">I might as well just uncomment these things at the bottom,</p>



<p class="wp-block-paragraph">just to say, let&#8217;s close both files properly.</p>



<p class="wp-block-paragraph">You always want to do that.</p>



<p class="wp-block-paragraph">let&#8217;s copy paste the create file function let&#8217;s see I didn&#8217;t do that already right yeah okay</p>



<p class="wp-block-paragraph">so let&#8217;s do that maybe like right here I&#8217;m going to go grab it from my solution real fast create</p>



<p class="wp-block-paragraph">file okay about as complicated as opening a file just because I put in some some checking logic to</p>



<p class="wp-block-paragraph">see if it successfully opened the file which is a good idea so here&#8217;s my function create file</p>



<p class="wp-block-paragraph">arguments. Oh shoot, file creation hand. Let&#8217;s see, am I using R13? Yeah, oh, I mislabeled that.</p>



<p class="wp-block-paragraph">Instead of saying flags, it should be permissions. So let me just do perms here. Long perms,</p>



<p class="wp-block-paragraph">file creation perms, perm. I wish I could get a perm, if you know what I mean. Anyway, so,</p>



<p class="wp-block-paragraph">you know, we just have like the file name that comes in and the permissions that come in,</p>



<p class="wp-block-paragraph">And it&#8217;s going to return a handle just like we did with the read file for opening function.</p>



<p class="wp-block-paragraph">But this time we&#8217;re going to do something slightly different.</p>



<p class="wp-block-paragraph">So the system call is going to be the code for creating a file.</p>



<p class="wp-block-paragraph">SYS create, which is not real unless you define it.</p>



<p class="wp-block-paragraph">So if we just look up, I define that as.</p>



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



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



<p class="wp-block-paragraph">So like right here, code 85 for system create.</p>



<p class="wp-block-paragraph">Again, not a smart idea to hard code numbers.</p>



<p class="wp-block-paragraph">Defines are way better.</p>



<p class="wp-block-paragraph">numbers defines are way better. So then the first argument that it wants is the name of the file.</p>



<p class="wp-block-paragraph">That&#8217;s the incoming argument that I took into R12. It has to be a null terminated string. It&#8217;s going</p>



<p class="wp-block-paragraph">to be output.txt. Then the second argument is the file&#8217;s permissions. So that&#8217;s R13 that I took here</p>



<p class="wp-block-paragraph">from the second argument, long perms. Once I&#8217;ve set up those things, I can do the system call</p>



<p class="wp-block-paragraph">right away. The system, again, will try to open the file. It&#8217;ll try to create the file,</p>



<p class="wp-block-paragraph">set up some data structures under the hood. If it succeeds, it&#8217;ll give me a valid file handle</p>



<p class="wp-block-paragraph">in RAX, the return register. If not, then things have failed and I need to respond to that. So</p>



<p class="wp-block-paragraph">I&#8217;m just going to stash the file handle in R14 right away. That&#8217;s why I&#8217;m also preserving R14</p>



<p class="wp-block-paragraph">in the push pop pair that I have, R14 up at the top and the bottom.</p>



<p class="wp-block-paragraph">So now we got to check whether or not we successfully created the file. Again,</p>



<p class="wp-block-paragraph">logic is just opening up off a reading I&#8217;m going to compare our 14 to zero if</p>



<p class="wp-block-paragraph">it&#8217;s greater than zero I&#8217;m going to assume it&#8217;s a valid file handle so jump</p>



<p class="wp-block-paragraph">if it&#8217;s greater than or equal to to that label create file success I&#8217;m still</p>



<p class="wp-block-paragraph">using my appending can naming convention so the name of the function is create</p>



<p class="wp-block-paragraph">file so that means the success area is create file and then append underscore</p>



<p class="wp-block-paragraph">success and so then I&#8217;m just gonna you know print a cute message saying hey we</p>



<p class="wp-block-paragraph">successfully created the file. Yay.</p>



<p class="wp-block-paragraph">Same thing that we looked at before basically.</p>



<p class="wp-block-paragraph">And then I&#8217;m going to return the file handle in RAX.</p>



<p class="wp-block-paragraph">If we fail, same thing that we did before when we were opening a read file,</p>



<p class="wp-block-paragraph">I&#8217;m just going to complain basically to the user and then call on my little</p>



<p class="wp-block-paragraph">die function to properly exit the assembly program with a</p>



<p class="wp-block-paragraph">exit code of one, you know,</p>



<p class="wp-block-paragraph">and I could enhance that to exit with different codes. Like maybe exit code.</p>



<p class="wp-block-paragraph">Maybe exit code one means the read file didn&#8217;t work and exit code two means the</p>



<p class="wp-block-paragraph">write file didn&#8217;t work and exit code three means we failed for some reason</p>



<p class="wp-block-paragraph">while we were copying the data, you know, whatever you want.</p>



<p class="wp-block-paragraph">I&#8217;m keeping it kind of simple.</p>



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



<p class="wp-block-paragraph">Again, this is not different than reading just, you know, the call codes pretty</p>



<p class="wp-block-paragraph">much and, you know, the arguments, but the idea is the same.</p>



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



<p class="wp-block-paragraph">I think we actually already did that.</p>



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



<p class="wp-block-paragraph">to create the file and the only thing we need to add after that is the copying portion and the</p>



<p class="wp-block-paragraph">successful message so let&#8217;s see if this works do clear and make run so you can see that the program</p>



<p class="wp-block-paragraph">starts running here and then it says okay the module started and then it says we successfully</p>



<p class="wp-block-paragraph">opened the read file and then we successfully created the output file notice how the make file</p>



<p class="wp-block-paragraph">doesn&#8217;t fail when it tries to call the md5 checksum of the output file because now it actually exists</p>



<p class="wp-block-paragraph">because now it actually exists if we list the program uh list the directory here you&#8217;ll see</p>



<p class="wp-block-paragraph">that the out file has actually been created it just has a length of zero because there&#8217;s nothing</p>



<p class="wp-block-paragraph">inside of it notice also that the permissions match what we intended the initial user can read</p>



<p class="wp-block-paragraph">and write or the owning user can read and write the group can only read and everybody else can do</p>



<p class="wp-block-paragraph">nothing to heck with you we could change that real fast just to show you i can get this r to</p>



<p class="wp-block-paragraph">show you i can get this r to turn into an rw just by modifying permissions up here</p>



<p class="wp-block-paragraph">you know giving permissions to a group uh just is a nice way of allowing multiple users</p>



<p class="wp-block-paragraph">to have a shared file location you know add them all to the same group and then set that group</p>



<p class="wp-block-paragraph">onto the file and then set the group permissions to allow people to do whatever you want them to</p>



<p class="wp-block-paragraph">do read or read and write or whatever so let&#8217;s see where the perms i&#8217;m going to change this</p>



<p class="wp-block-paragraph">So now the group people should be able to read and write.</p>



<p class="wp-block-paragraph">I can&#8217;t remember if this will work because the file already exists.</p>



<p class="wp-block-paragraph">Let me give it a try.</p>



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



<p class="wp-block-paragraph">I ran it one more time and it looks like it did not create the file because it already</p>



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



<p class="wp-block-paragraph">So let me just remove output.txt.</p>



<p class="wp-block-paragraph">There should be another system call code you could use to just check to see if a file</p>



<p class="wp-block-paragraph">exists or if you wanted to be kind of hacky, you could just try to open the file and see</p>



<p class="wp-block-paragraph">open the file and see if it succeeded and then close it right away.</p>



<p class="wp-block-paragraph">I wonder if there is a call code for just exists only.</p>



<p class="wp-block-paragraph">I don&#8217;t remember off the top of my head.</p>



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



<p class="wp-block-paragraph">And then we should see now.</p>



<p class="wp-block-paragraph">Yeah, now that the file didn&#8217;t exist and was newly created.</p>



<p class="wp-block-paragraph">Now those new permissions that we added are reflected.</p>



<p class="wp-block-paragraph">So the group, anybody who&#8217;s on the group can read and write.</p>



<p class="wp-block-paragraph">And obviously the group is just the same as my new user by default.</p>



<p class="wp-block-paragraph">But again, you could be more complicated than that.</p>



<p class="wp-block-paragraph">you were running a multi-user system</p>



<p class="wp-block-paragraph">and you wanted to share folders or whatever.</p>



<p class="wp-block-paragraph">Okay, so I&#8217;m gonna remove output real fast</p>



<p class="wp-block-paragraph">and I&#8217;m just gonna revert the permissions to 640</p>



<p class="wp-block-paragraph">so that the group really can&#8217;t do very much</p>



<p class="wp-block-paragraph">except just read it.</p>



<p class="wp-block-paragraph">And then if I, oh shoot.</p>



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



<p class="wp-block-paragraph">So now you can see it reverted.</p>



<p class="wp-block-paragraph">Okay, so we got the out file.</p>



<p class="wp-block-paragraph">I&#8217;m starting to get lost here.</p>



<p class="wp-block-paragraph">I&#8217;m starting to get lost here. What am I doing? I&#8217;m supposed to copy the data, I think. So file</p>



<p class="wp-block-paragraph">tests and we are copying the file. I think I just uncommented that. Really? Oh, no, no, no. I&#8217;m</p>



<p class="wp-block-paragraph">looking at my solution repo. Okay. That&#8217;s what I&#8217;m doing wrong. So we&#8217;ll look into your repo or your</p>



<p class="wp-block-paragraph">the copy file I was like what how did I how did that program run if I already uncommented that</p>



<p class="wp-block-paragraph">and we&#8217;ll we&#8217;ll uncomment this message for now no let&#8217;s leave it until we actually finish everything</p>



<p class="wp-block-paragraph">so now we got to do the copy file function so where&#8217;s that okay I&#8217;m going to copy paste this</p>



<p class="wp-block-paragraph">whole thing you know what this would have been like a five hour video if I actually had to type</p>



<p class="wp-block-paragraph">this by hand I can&#8217;t even remember how long it took me to write this program I think it was like</p>



<p class="wp-block-paragraph">off the top of my head. So this would be a nightmare to type, I think, on video, even if I</p>



<p class="wp-block-paragraph">kind of already know how to do it now. So let&#8217;s see, string length, print an alternated string.</p>



<p class="wp-block-paragraph">This is close file. This is create file. Okay, so I&#8217;m just going to do copy.</p>



<p class="wp-block-paragraph">File is going to be right before close file and right after create file. Okay, so copy file.</p>



<p class="wp-block-paragraph">Notice the signature that I&#8217;ve chosen for this one. It doesn&#8217;t return anything. So</p>



<p class="wp-block-paragraph">So that&#8217;s like not great design.</p>



<p class="wp-block-paragraph">You know, I need to check to see if the copying operations inside of my loop actually succeeded</p>



<p class="wp-block-paragraph">and maybe return something to indicate success or failure or at least exit the program.</p>



<p class="wp-block-paragraph">But I&#8217;m not doing that right now.</p>



<p class="wp-block-paragraph">I&#8217;m just keeping it a little bit more simple.</p>



<p class="wp-block-paragraph">It takes two arguments.</p>



<p class="wp-block-paragraph">The first thing is the input handle and the second thing is the output handle.</p>



<p class="wp-block-paragraph">Conveniently, we have both of those now at this point.</p>



<p class="wp-block-paragraph">And then here&#8217;s my register usage.</p>



<p class="wp-block-paragraph">through r15 to just sort of grab the incoming arguments here for r12 and 13 and then</p>



<p class="wp-block-paragraph">r14 grabs the beginning of the temporary buffer which i&#8217;m going to make on the stack because i</p>



<p class="wp-block-paragraph">think i&#8217;m so cool instead of making it as a global in the bss section and then uh</p>



<p class="wp-block-paragraph">r15 is going to hold the result from the copy operation or i guess the the write operation so</p>



<p class="wp-block-paragraph">Sorry, the read operation only.</p>



<p class="wp-block-paragraph">Oh, I think I&#8217;m only checking the read bytes instead of the read and the write bytes.</p>



<p class="wp-block-paragraph">I&#8217;ll talk about that in a second.</p>



<p class="wp-block-paragraph">So I could upgrade my program a little bit more if I wanted to.</p>



<p class="wp-block-paragraph">But basically R15 is going to be my temporary variable that looks at the return value to</p>



<p class="wp-block-paragraph">say, hey, did we do we read anything?</p>



<p class="wp-block-paragraph">Like how much did we read?</p>



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



<p class="wp-block-paragraph">This is not a video about making local variables.</p>



<p class="wp-block-paragraph">So just trust me on this.</p>



<p class="wp-block-paragraph">so that I can use it to save the location of the stack pointer.</p>



<p class="wp-block-paragraph">Then I&#8217;m going to make a copy buffer on the stack</p>



<p class="wp-block-paragraph">just by subtracting the stack pointer</p>



<p class="wp-block-paragraph">because the stack grows downward in memory location numbers.</p>



<p class="wp-block-paragraph">And then the base pointer is going to help me remember</p>



<p class="wp-block-paragraph">where the stack was when I started.</p>



<p class="wp-block-paragraph">I&#8217;m just going to say that that&#8217;s going to be the first byte in my buffer.</p>



<p class="wp-block-paragraph">And I&#8217;m going to move that into R14.</p>



<p class="wp-block-paragraph">And let&#8217;s see, what did I do?</p>



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



<p class="wp-block-paragraph">Think about from the stack.</p>



<p class="wp-block-paragraph">Oh, did I write like a good program or a bad program?</p>



<p class="wp-block-paragraph">I feel like I should have actually saved the base pointer and not the stack pointer there.</p>



<p class="wp-block-paragraph">Let me see if this runs.</p>



<p class="wp-block-paragraph">If it does run, I probably have a naughty program that might self-corrupt sometimes.</p>



<p class="wp-block-paragraph">And it might be a good idea to move the base pointer as the first.</p>



<p class="wp-block-paragraph">Because this is not a stack video, but basically the stack grows downward.</p>



<p class="wp-block-paragraph">that means you&#8217;re sort of like extending its reach you&#8217;re allocating like a free space</p>



<p class="wp-block-paragraph">if i take where the stack</p>



<p class="wp-block-paragraph">nope nope nope i got it right i&#8217;m sorry if i take where the stack ends up after i extend it then i</p>



<p class="wp-block-paragraph">have a lower address right because it grows downward to memory if i then say that the new</p>



<p class="wp-block-paragraph">tip where it grew not where it grew from but where it grew to if i say that&#8217;s the first bite in a</p>



<p class="wp-block-paragraph">in a buffer then when you&#8217;re actually using the buffer you increase memory locations as you&#8217;re</p>



<p class="wp-block-paragraph">filling up a buffer so then that&#8217;s going to grow back towards where the stack started so it should</p>



<p class="wp-block-paragraph">be fine i think if i if i did go ahead and reverse this if i used rbp instead of rsp there then i</p>



<p class="wp-block-paragraph">think i would be going in the wrong direction and just corrupting memory so just so you know</p>



<p class="wp-block-paragraph">this is not a stack video but just so you know so here&#8217;s a little label again i&#8217;m just kind of</p>



<p class="wp-block-paragraph">iteration label and at the bottom I&#8217;m just kind of jumping back up to that label so this is a loop</p>



<p class="wp-block-paragraph">you can imagine this is a while loop maybe I should draw that for fun I gotta find more excuses</p>



<p class="wp-block-paragraph">to use my drophead so this is kind of like a while loop I&#8217;ll say while true maybe and here&#8217;s like the</p>



<p class="wp-block-paragraph">body and then here&#8217;s like maybe the end of the body and so this jump statement just kind of goes back</p>



<p class="wp-block-paragraph">this thing&#8217;s going to frustrate me so the first thing that happens is we read a portion</p>



<p class="wp-block-paragraph">of the input file into the buffer so what are we doing we&#8217;re just using another system call</p>



<p class="wp-block-paragraph">we&#8217;re saying let&#8217;s use the call code for read I can&#8217;t remember what that was it might have been</p>



<p class="wp-block-paragraph">uh whoops oh I cleared the dang drawing oh hang on I have undo I think wait I have that I&#8217;m gonna</p>



<p class="wp-block-paragraph">Okay, I&#8217;m not going to mess with it because this will actually terminate my whole program.</p>



<p class="wp-block-paragraph">One of these keys, I forgot which one, will just kill the whole annotator.</p>



<p class="wp-block-paragraph">So if we go up to SYS read in the defines, you can see that it is just call code zero.</p>



<p class="wp-block-paragraph">So that means I&#8217;m telling the system I would like to read something.</p>



<p class="wp-block-paragraph">First argument it wants is a file descriptor for the input file.</p>



<p class="wp-block-paragraph">In a different video, I showed you how to use exactly this sort of thing to read standard</p>



<p class="wp-block-paragraph">of thing to read standard input from the user or from another program that launched your program</p>



<p class="wp-block-paragraph">but in this case r12 it&#8217;s not going to be the file handle zero for standard input it&#8217;s going</p>



<p class="wp-block-paragraph">to be the actual file handle of the file that you&#8217;re trying to read from whatever that may have</p>



<p class="wp-block-paragraph">been whatever the os gave you and then here here&#8217;s the address of where to store our characters and</p>



<p class="wp-block-paragraph">if you look up again we decided to remember where the first byte in our buffer on the stack was or</p>



<p class="wp-block-paragraph">like a local array like oh shoot let&#8217;s do it again let&#8217;s do more drawings you have like a function</p>



<p class="wp-block-paragraph">right in c and then here it&#8217;s like we declared a local variable maybe not int maybe a character</p>



<p class="wp-block-paragraph">array we&#8217;ll call it a or how about b for buffer and then we just gave it you know let&#8217;s say eight</p>



<p class="wp-block-paragraph">bytes or something for the buffer i think i still have it set to two bytes obviously you want to use</p>



<p class="wp-block-paragraph">more bytes for efficiency but just to prove that the loop actually works i&#8217;m keeping it small like</p>



<p class="wp-block-paragraph">keeping it small like I said before whoa what&#8217;s all that you see that it&#8217;s like smearing okay</p>



<p class="wp-block-paragraph">that&#8217;s not good so uh anyway we remembered where the uh buffer starts and it&#8217;s going to be r14</p>



<p class="wp-block-paragraph">and then the copy buffer length is being you know used here uh and I think I have that set up to uh</p>



<p class="wp-block-paragraph">from the input file and we want to read into our temporary buffer and we want to read at most</p>



<p class="wp-block-paragraph">this many characters and then we say system call and it does all the work for us to read that many</p>



<p class="wp-block-paragraph">characters and then we want to remember how many bytes were actually read because that could be</p>



<p class="wp-block-paragraph">different from the number of bytes we requested maybe we&#8217;re at the end of the file maybe the</p>



<p class="wp-block-paragraph">system is having like some kind of a buffer issue or something so we just want to remember how many</p>



<p class="wp-block-paragraph">this little token up there to remind myself the temporary bytes read should</p>



<p class="wp-block-paragraph">go there. Notice also that when we created the stack,</p>



<p class="wp-block-paragraph">this is not a stack video again,</p>



<p class="wp-block-paragraph">but like I made the stack buffer equal to the length of the buffer that I</p>



<p class="wp-block-paragraph">actually wanted to use. So I use the same symbol,</p>



<p class="wp-block-paragraph">copy buffer length and copy buffer length.</p>



<p class="wp-block-paragraph">So then it&#8217;s going to try to figure out, okay,</p>



<p class="wp-block-paragraph">how many bytes did we actually read? Let me,</p>



<p class="wp-block-paragraph">let me do that while loop thing again. Cause I&#8217;m, I&#8217;m feeling it.</p>



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



<p class="wp-block-paragraph">feeling it did i really have to write the word true especially my bad penmanship so we&#8217;re doing</p>



<p class="wp-block-paragraph">while true and then i&#8217;ll just say like read you know do some kind of a call to read maybe we&#8217;ll</p>



<p class="wp-block-paragraph">say n equals read like a long you know the the r15 uh register is like the number of bytes that we</p>



<p class="wp-block-paragraph">actually read and then we do a comparison here so we&#8217;re saying you know if you know n is um</p>



<p class="wp-block-paragraph">if it&#8217;s equal to zero then we&#8217;re going to jump to the position in the while loop where we&#8217;re</p>



<p class="wp-block-paragraph">um done which is actually past the while loop if you scroll down you&#8217;ll kind of see it so i&#8217;m just</p>



<p class="wp-block-paragraph">going to say break so basically if we read zero bytes then we&#8217;re done reading bytes we&#8217;re just</p>



<p class="wp-block-paragraph">finished so we just break the while loop and that&#8217;s what&#8217;s going to happen here when we say</p>



<p class="wp-block-paragraph">the end of the body and you&#8217;ll see that in a second so then otherwise if we&#8217;re not done that</p>



<p class="wp-block-paragraph">means execution is not going to jump it&#8217;s going to just fall through to the next statement here</p>



<p class="wp-block-paragraph">and that&#8217;s going to be another system call code to write to the output file and it&#8217;s going to be</p>



<p class="wp-block-paragraph">very similar we just load it up with the system call code for write let me just double check</p>



<p class="wp-block-paragraph">is code one and then we&#8217;re going to give it the target for output which is going to be</p>



<p class="wp-block-paragraph">a file handle so you know r13 r13 right here that&#8217;s a file handle that&#8217;s where we want to</p>



<p class="wp-block-paragraph">write and then the next argument that it wants is the buffer and that&#8217;s going to be r14 which</p>



<p class="wp-block-paragraph">is where we just what&#8217;s going on green grain there we go i&#8217;m having issues that&#8217;s uh the</p>



<p class="wp-block-paragraph">that&#8217;s uh the buffer that we just read into right so if you look here r14 green oh my gosh okay so</p>



<p class="wp-block-paragraph">green we uh read from uh the buffer pointed to by r14 and then we uh we read into the buffer</p>



<p class="wp-block-paragraph">pointed to by r14 and then we are using that same buffer pointed to by r14 in order to grab</p>



<p class="wp-block-paragraph">R15 says how many files or sorry, how many bytes?</p>



<p class="wp-block-paragraph">Green, I&#8217;m having such problems.</p>



<p class="wp-block-paragraph">R15 says how many bytes do you want to write to the file?</p>



<p class="wp-block-paragraph">That should be our return value from before, right?</p>



<p class="wp-block-paragraph">Because we want to read a certain number of bytes from the file</p>



<p class="wp-block-paragraph">and then write exactly that many bytes.</p>



<p class="wp-block-paragraph">If we did more, we would be writing some junk data probably.</p>



<p class="wp-block-paragraph">And if we wrote less, then we would be missing data in the output file</p>



<p class="wp-block-paragraph">that was originally in the input file.</p>



<p class="wp-block-paragraph">So you want to do it exactly.</p>



<p class="wp-block-paragraph">in the input file so you want to do it exactly and of course i&#8217;m just doing a system call here</p>



<p class="wp-block-paragraph">but like i said before your program should probably be a little bit smarter and check</p>



<p class="wp-block-paragraph">rax after you write to the file just to make sure that you know how many bytes were actually written</p>



<p class="wp-block-paragraph">like what if you what if you read a hundred bytes but then you wrote 90 bytes only right that would</p>



<p class="wp-block-paragraph">be like a bad situation so you&#8217;d want to do some branching logic there so that if you read 100 and</p>



<p class="wp-block-paragraph">100 and you wrote 90 you probably want to backtrack the position of the file by like 10.</p>



<p class="wp-block-paragraph">You know just to make sure that you can actually get all of the bytes into the output file</p>



<p class="wp-block-paragraph">and seeking backwards 10 that&#8217;s just another system call. It&#8217;s not in this program but it&#8217;s</p>



<p class="wp-block-paragraph">not too bad. You just make another system call give it the right call code tell it how far back</p>



<p class="wp-block-paragraph">you want to go no problem. So then when by the time we make it here we&#8217;re going to jump back</p>



<p class="wp-block-paragraph">We&#8217;re going to jump back up to the iterate label.</p>



<p class="wp-block-paragraph">Let&#8217;s see, where&#8217;s that?</p>



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



<p class="wp-block-paragraph">We&#8217;re going to jump back up to the iterate label.</p>



<p class="wp-block-paragraph">So basically this is how the while loop continues looping.</p>



<p class="wp-block-paragraph">So let me clear that real fast.</p>



<p class="wp-block-paragraph">Eventually when we&#8217;re out of data to read, you know, that&#8217;s because if we try to read</p>



<p class="wp-block-paragraph">and we end up with zero bytes, that means we&#8217;re done.</p>



<p class="wp-block-paragraph">We&#8217;ll break the while loop.</p>



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



<p class="wp-block-paragraph">Notice how it says copy file done.</p>



<p class="wp-block-paragraph">What I was talking about before, just sort of jumping past the end of the body.</p>



<p class="wp-block-paragraph">so if we are done we jump to copy file done and all it does is just restore a</p>



<p class="wp-block-paragraph">bunch of registers for us and then return to the caller so copy file we have that</p>



<p class="wp-block-paragraph">handled now and if I go back up here file tests I guess I can I feel bad about</p>



<p class="wp-block-paragraph">this I can uncomment the success message but we probably should have done more</p>



<p class="wp-block-paragraph">checking on the reads and the rights just to make sure that we actually wrote</p>



<p class="wp-block-paragraph">to make sure that we actually wrote the correct amount of characters and everything succeeded</p>



<p class="wp-block-paragraph">every time and if not we exit the program and only if everything went well then we print a</p>



<p class="wp-block-paragraph">successful message at this point in this program the way it&#8217;s written it could totally fail and</p>



<p class="wp-block-paragraph">it&#8217;ll still say that it was successful so just keep that in mind that&#8217;s bad bad for your users</p>



<p class="wp-block-paragraph">okay i think we have everything that we need now i can probably just run the program and</p>



<p class="wp-block-paragraph">run. Notice now, oh shoot, I didn&#8217;t even show this to you before. Let me just emphasize this</p>



<p class="wp-block-paragraph">one more time real fast. So I&#8217;m going to comment out the part where we actually copy the file</p>



<p class="wp-block-paragraph">and I&#8217;m going to remove the output file. Then I&#8217;m going to run the whole program again. Notice how</p>



<p class="wp-block-paragraph">the two MD5 sums are different. So remember before I said that MD5 sums, they&#8217;re basically</p>



<p class="wp-block-paragraph">fingerprints. They&#8217;re not actually considered secure in the modern era. I just use them because</p>



<p class="wp-block-paragraph">and fun if you are interested in security you probably want to use a</p>



<p class="wp-block-paragraph">modern hashing algorithm so don&#8217;t use md5 but I am but don&#8217;t but I am and it&#8217;s</p>



<p class="wp-block-paragraph">basically saying the fingerprint here of input is different than the fingerprint</p>



<p class="wp-block-paragraph">here of the output quickly indicating to me that the files are different so if I</p>



<p class="wp-block-paragraph">list the contents of the directory obviously that&#8217;s true because the output</p>



<p class="wp-block-paragraph">file is empty still but if I uncomment this part here where I&#8217;m actually</p>



<p class="wp-block-paragraph">here where I&#8217;m actually copying the file then I should see that the fingerprints</p>



<p class="wp-block-paragraph">match and then if I look at what&#8217;s inside of the output file it should match</p>



<p class="wp-block-paragraph">what&#8217;s inside of the input file really fast let me open up a terminal and I&#8217;ll</p>



<p class="wp-block-paragraph">just cat the input file so this is all I added you know why hello there add some</p>



<p class="wp-block-paragraph">stuff this is definitely more than two characters so we can be sure that our</p>



<p class="wp-block-paragraph">buffer loop is actually working and so I&#8217;m gonna do clear and make run and now</p>



<p class="wp-block-paragraph">And now the output file, notice how it has a matching file size.</p>



<p class="wp-block-paragraph">It&#8217;s got 38 bytes in there.</p>



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



<p class="wp-block-paragraph">And then the thumbprints or, you know, the signatures, whatever you want to call it,</p>



<p class="wp-block-paragraph">the hashes, they match exactly, indicating that we probably have two identical files.</p>



<p class="wp-block-paragraph">Even with MD5, even though it&#8217;s old and not considered secure,</p>



<p class="wp-block-paragraph">the chances of two files kind of having random differences,</p>



<p class="wp-block-paragraph">not hacked differences, but just like random differences,</p>



<p class="wp-block-paragraph">and having the same fingerprint is like astronomically almost impossible so if i do</p>



<p class="wp-block-paragraph">cat input oops input.txt that&#8217;s what&#8217;s in there like we showed before and if i cat the output</p>



<p class="wp-block-paragraph">now it is uh the same thing why hello there added some stuff for both i could make this as big as i</p>



<p class="wp-block-paragraph">wanted to just for fun maybe let&#8217;s do uh let&#8217;s do a nano on the input file</p>



<p class="wp-block-paragraph">I&#8217;ll just add like I don&#8217;t even know what I&#8217;m doing I&#8217;m just going to type a bunch of stuff</p>



<p class="wp-block-paragraph">oh wait what is this remember that thing that people got taught a long time ago it was like</p>



<p class="wp-block-paragraph">the quick brown fox jumped over the lazy dog and this was supposed to be all the letters of the</p>



<p class="wp-block-paragraph">alphabet there was an there&#8217;s another one that I just heard about and I don&#8217;t remember exactly</p>



<p class="wp-block-paragraph">look it up on the internet it&#8217;s it&#8217;s pretty cool i think i need to memorize this and stop using the</p>



<p class="wp-block-paragraph">lazy dog it was um something like those sphinx of quartz hear my vow or something like that</p>



<p class="wp-block-paragraph">you know what i&#8217;m gonna look it up for you right now i don&#8217;t want to do the wrong thing</p>



<p class="wp-block-paragraph">it&#8217;s really cool uh i&#8217;ll type brown fox and then uh</p>



<p class="wp-block-paragraph">Hear my vow.</p>



<p class="wp-block-paragraph">Oh, Sphinx of black quartz.</p>



<p class="wp-block-paragraph">Judge my vow.</p>



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



<p class="wp-block-paragraph">Okay, so I&#8217;m going to go back to my little VM here.</p>



<p class="wp-block-paragraph">Sphinx of black quartz.</p>



<p class="wp-block-paragraph">Judge my…</p>



<p class="wp-block-paragraph">I didn&#8217;t even write vow correctly.</p>



<p class="wp-block-paragraph">Judge my vow.</p>



<p class="wp-block-paragraph">And I think that has all the letters in the alphabet too.</p>



<p class="wp-block-paragraph">probably less spaces. I wonder if that&#8217;s, I mean, that&#8217;s what the internet says. If this is true,</p>



<p class="wp-block-paragraph">and it has all the letters of the alphabet, that&#8217;s going to be awesome. I&#8217;m going to memorize that</p>



<p class="wp-block-paragraph">for sure. Sphinx of black quartz, judge my vow. Anyway, so I&#8217;m just kind of adding stuff into</p>



<p class="wp-block-paragraph">this file. And if I run, let me save that here. I&#8217;ll do a clear. And then I&#8217;ll do, um,</p>



<p class="wp-block-paragraph">I&#8217;ll cat the input file here, and then cat the output file. So you can see they&#8217;re different.</p>



<p class="wp-block-paragraph">again let me remove the output file just in case I can&#8217;t remember if I&#8217;m supposed</p>



<p class="wp-block-paragraph">to remove it manually or if I put that into the program we&#8217;ll just try it like</p>



<p class="wp-block-paragraph">this so now they match and then if I cat again the output file notice how it&#8217;s a</p>



<p class="wp-block-paragraph">perfect copy of the input file nice so I think that&#8217;s pretty much everything</p>



<p class="wp-block-paragraph">that I wanted to show you maybe um well maybe we can use a more efficient buffer</p>



<p class="wp-block-paragraph">we can use a more efficient buffer now that it&#8217;s done the copy buffer we could change this to like</p>



<p class="wp-block-paragraph">eight kilobytes or something we should end up with the same result let me run this just as is</p>



<p class="wp-block-paragraph">and see if it ends up being the same thing without erasing the file first yeah it looks good let me</p>



<p class="wp-block-paragraph">remove the output file and then run it one more time so make run and then uh cat the output file</p>



<p class="wp-block-paragraph">yeah okay so it still works but um you know whereas before we were just using a two byte buffer</p>



<p class="wp-block-paragraph">two byte buffer there&#8217;s like very little chance except maybe at the end of the file that we would</p>



<p class="wp-block-paragraph">request more data than the file had but using the return value of the read operation always told us</p>



<p class="wp-block-paragraph">exactly how much was read by the operating system on the other hand if we have a giant buffer we</p>



<p class="wp-block-paragraph">could request way more bytes than the file could ever have because that file is way less than eight</p>



<p class="wp-block-paragraph">kilobytes so again we still want to look at the return value to make sure we know how many bytes</p>



<p class="wp-block-paragraph">how many bytes should be sent into the right file.</p>



<p class="wp-block-paragraph">So I guess that&#8217;s everything that I wanted to tell you</p>



<p class="wp-block-paragraph">about reading and writing files using system calls.</p>



<p class="wp-block-paragraph">I hope you&#8217;ve learned a lot of stuff</p>



<p class="wp-block-paragraph">and you enjoyed this video and had a little bit of fun.</p>



<p class="wp-block-paragraph">Thank you so much for watching.</p>



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



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



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



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



<p class="wp-block-paragraph">Thanks for watching this video again</p>



<p class="wp-block-paragraph">from the bottom of my heart.</p>



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



<p class="wp-block-paragraph">I do hope you did learn something</p>



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



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



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



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



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



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



<p class="wp-block-paragraph">and subscribe. You know sometimes I&#8217;m sleeping in the middle of the night and</p>



<p class="wp-block-paragraph">I just wake up because I know somebody subscribed or followed. It just wakes me</p>



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



<p class="wp-block-paragraph">So you could do it as a nice favor to me or you could you control me if you want</p>



<p class="wp-block-paragraph">to just wake me up in the middle of the night just subscribe and then I&#8217;ll just</p>



<p class="wp-block-paragraph">wake up. I promise that&#8217;s what will happen. Also if you look at the middle of</p>



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



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



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



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



<p class="wp-block-paragraph">things that I offer and all that good stuff and uh if you have a suggestion for uh uh clarifications</p>



<p class="wp-block-paragraph">or errata or just future videos that you want to see please leave a comment or if you just want to</p>



<p class="wp-block-paragraph">say hey what&#8217;s up what&#8217;s going on you know just send me a comment whatever I also wake up for</p>



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



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



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



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



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



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



<p class="wp-block-paragraph">Thank you.</p>
<p>The post <a href="https://www.NeuralLantern.com/master-file-copying-with-system-calls-in-x86-64-yasm-assembly/">Master File Copying with System Calls in x86-64 YASM Assembly</a> appeared first on <a href="https://www.NeuralLantern.com">NeuralLantern.com</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.NeuralLantern.com/master-file-copying-with-system-calls-in-x86-64-yasm-assembly/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>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 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>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 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>
	</channel>
</rss>
