<?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>standard output Archives - NeuralLantern.com</title>
	<atom:link href="https://www.NeuralLantern.com/tag/standard-output/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.NeuralLantern.com/tag/standard-output/</link>
	<description></description>
	<lastBuildDate>Mon, 22 Sep 2025 11:53:49 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>

<image>
	<url>https://www.NeuralLantern.com/wp-content/uploads/2025/04/cropped-2025-04-04-Lantern-03-32x32.png</url>
	<title>standard output Archives - NeuralLantern.com</title>
	<link>https://www.NeuralLantern.com/tag/standard-output/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<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>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>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>Thanks for watching!</p>



<p>Find us on other social media here:</p>



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



<p>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>Hey everybody.</p>



<p>In this video I&#8217;m going to talk to you a little bit about standard input, standard output,</p>



<p>and standard error.</p>



<p>Three pipes that every program has in Linux that allow it to just communicate in a very</p>



<p>standard way.</p>



<p>I&#8217;m going to show you some example code written in Yasm Assembly, but assembly knowledge is</p>



<p>not necessarily required for this video.</p>



<p>You can just sort of follow along.</p>



<p>Anyway, what is standard input, standard output, and standard error?</p>



<p>Well for starters, every program that gets launched, under the hood, the operating system</p>



<p>will typically attach three pipes to it.</p>



<p>Basically one for standard input, one for standard output, and one for standard error.</p>



<p>the program pretty easily. So let&#8217;s see. Oh, I know what to do. There&#8217;s an echo command, right?</p>



<p>So here&#8217;s the program called echo. When the program launches, it&#8217;s going to have three pipes</p>



<p>attached to it, just like I told you before. These pipes also behave as file descriptors,</p>



<p>as if you had opened a file, but their file descriptor numbers are always zero and one and two.</p>



<p>Zero for standard input, one for standard output, and two for standard error. So if I launch the</p>



<p>program it&#8217;s going to get those three pipes those three file descriptors and</p>



<p>right away the standard output is going to get attached to the program running</p>



<p>our terminal here well not necessarily the actual terminal but bash under the</p>



<p>hood which is the program that we&#8217;re using to interact with the operating</p>



<p>system so basically the program launches and based on this incoming argument that</p>



<p>I give it hello then it&#8217;s going to print the string hello right but what it&#8217;s</p>



<p>its standard output pipe its standard output file descriptor I can test this</p>



<p>for sure by just sort of doing the command again and then muting its</p>



<p>standard output pipe with this little shell trick I&#8217;ll do a arrow or a greater</p>



<p>than angle bracket and I&#8217;ll just send the first pipe I&#8217;ll do one I&#8217;ll send it</p>



<p>to dev null and on Linux dev null is just if you send anything there it just</p>



<p>it&#8217;s just totally ignored so if i send uh the pipe labeled number one to just nowhere then notice how</p>



<p>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>i just want you to see real fast before i write an example program in assembly what you would</p>



<p>probably end up doing in c plus plus so we usually do something like iostream in germane you know</p>



<p>We&#8217;ll say using std cout std c error std and l and so forth.</p>



<p>And then if you actually want to print a message to the user, we&#8217;ll say cout this message prints</p>



<p>to std out because that&#8217;s what cout does by default.</p>



<p>And then if we do c error, a lot of people haven&#8217;t actually seen this and I personally</p>



<p>always forget that it&#8217;s even there if you print to see error it prints a message to standard error</p>



<p>why would you want two different pipes that printed messages and one is called output and</p>



<p>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>have programs automate other programs for example if you have a program that is launching you know</p>



<p>a second program maybe the second program fails in some way or wants to complain to you in some way</p>



<p>or wants to complain to you in some way, it would usually, if it&#8217;s smart,</p>



<p>print the complaint to standard error.</p>



<p>It may not actually use C error because maybe it&#8217;s not a C++ program,</p>



<p>but it would print to pipe number two, file descriptor number two, standard error.</p>



<p>And then that way, the program that&#8217;s launching it can just sort of ignore messages</p>



<p>that happen on standard output because we can assume all of those messages</p>



<p>are just normal info messages that a human might want to see.</p>



<p>Or maybe you want to record to a log or something somewhere</p>



<p>log or something somewhere and just kind of save it away.</p>



<p>But the error messages are a little bit more important than the regular,</p>



<p>you know, normal messages.</p>



<p>So if you&#8217;re automating another program,</p>



<p>then your program probably wants to take standard output and either ignore it or</p>



<p>just kind of send it to a different file.</p>



<p>And then it wants to take standard error and pay more attention to it.</p>



<p>Write it to a log file, save it, maybe check that it&#8217;s actually blank.</p>



<p>And if it&#8217;s not empty, then maybe alarm bells go off.</p>



<p>Something went wrong.</p>



<p>We have to alert someone or, you know, make a database entry, send an email, whatever.</p>



<p>And yeah, just pipe one and pipe two is how you accomplish that.</p>



<p>So this is what&#8217;s happening in C++ when you do that sort of thing.</p>



<p>Okay, so now I would like to write a sample program that just kind of demonstrates this</p>



<p>real fast.</p>



<p>So please bear with me.</p>



<p>I&#8217;m going to go really fast for the assembly portion because this is not an assembly video.</p>



<p>to learn more about how to write assembly in the first place than see my other videos.</p>



<p>For now, I&#8217;m just going to create a little blank file here.</p>



<p>Let&#8217;s see where the heck is my window.</p>



<p>I&#8217;m on a multi-window system here.</p>



<p>Okay, so I&#8217;m just going to paste a little data section.</p>



<p>This is Yasm assembly.</p>



<p>A little data section.</p>



<p>I&#8217;m just going to fill it up with strings.</p>



<p>I&#8217;m going to make a string for the introduction.</p>



<p>Hello, the module has started.</p>



<p>A string announcing everything that we&#8217;re going to do.</p>



<p>We&#8217;re going to begin printing to standard output and standard error,</p>



<p>and then some messages that will go to each.</p>



<p>And then some messages that will go to each and then a standard input string, which we&#8217;ll</p>



<p>talk about after the standard output and standard error.</p>



<p>So just a bunch of strings.</p>



<p>I&#8217;m going to make a buffer size.</p>



<p>I&#8217;m going to make system call codes for reading, writing and exiting.</p>



<p>If you want to know more about system call codes, see my other videos and some file descriptors.</p>



<p>This is exactly what I just talked about.</p>



<p>The file descriptor for standard input is zero for standard output is one for standard</p>



<p>for standard error is two.</p>



<p>And then I&#8217;m going to do a system exit code of zero,</p>



<p>which actually doesn&#8217;t matter that much,</p>



<p>but I&#8217;m going to do it.</p>



<p>So now I&#8217;m going to make a, let&#8217;s see,</p>



<p>a global,</p>



<p>hmm, make a global.</p>



<p>Oh, did I already make the text section?</p>



<p>Here we go.</p>



<p>So there&#8217;s the text section.</p>



<p>So the data section is over now.</p>



<p>So data section is over now.</p>



<p>I&#8217;m going to make the tech section.</p>



<p>I&#8217;m going to make a global entry point called start.</p>



<p>Again, if you want to know more about assembly programming in general,</p>



<p>see my other videos.</p>



<p>But so this is the entry point of our program.</p>



<p>At the very end, we should probably just program that it exits with success</p>



<p>and test it out real fast.</p>



<p>So basically right now this program does nothing.</p>



<p>Let me just double check.</p>



<p>Clear and make run.</p>



<p>I have a make file under the hood, but this is not a make file video.</p>



<p>If you want to know more about make files, see my other videos.</p>



<p>know more about make files see my other videos but basically it just starts running and then</p>



<p>it just dies right away okay no problem let me actually can i i want to fix that make file</p>



<p>because it&#8217;s it&#8217;s labeled after like a different project that i was doing let&#8217;s do</p>



<p>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>going back to the editor.</p>



<p>We have a program that does nothing.</p>



<p>I&#8217;m going to copy paste in a couple of useful functions that I use elsewhere.</p>



<p>Again, this is not an assembly video.</p>



<p>So, so you can look at my code if you want to, if you just kind of want to know how this</p>



<p>works, but not the point of this video.</p>



<p>So I&#8217;m not going to spend too much time.</p>



<p>Maybe I&#8217;ll just briefly talk about what it does, but not the point of this video.</p>



<p>So the first thing I&#8217;m doing is I&#8217;m adding a function called print null terminated string</p>



<p>function called print null terminated string just you know it sweeps through a</p>



<p>string or actually it asks another function I made to find out how long the</p>



<p>string is and then it&#8217;ll print the string to some file handle that I gave</p>



<p>it like standard output or standard error and you know it calls on this other</p>



<p>function called string length which I just wrote in pure assembly for fun even</p>



<p>though I didn&#8217;t need to and it just kind of sweeps through the string until it</p>



<p>finds a zero and then it knows how long the string is and then CRLF just prints</p>



<p>is and then crlf just prints a carriage return new line uh carriage return new line just um</p>



<p>with the same idea with the print null terminated string function and you can see all my strings have</p>



<p>null terminators that&#8217;s the dot zero at the very end of it okay so we got those helper functions</p>



<p>uh the next thing that i need to add in here is</p>



<p>do an intro like my growling i used to have a professor who would go</p>



<p>and he would go oh god when something went wrong and i do that all the time even when things are</p>



<p>going right okay so i&#8217;m going to paste a little function here called intro and this is not going</p>



<p>to be anything important it&#8217;s just going to print a little hello message you know it just uses my</p>



<p>intro string and then just prints it with those other functions i made so that means now my my</p>



<p>I can call intro real fast and then it&#8217;ll return when it&#8217;s done.</p>



<p>If I just sort of run the program again, you can see after all the build messages,</p>



<p>it&#8217;s just printing that the module has started.</p>



<p>Okay, so nothing super important yet.</p>



<p>Now, what I&#8217;d like to do is capture some incoming arguments real quick because…</p>



<p>Let&#8217;s see standard input.</p>



<p>Well, that&#8217;s actually not going to work.</p>



<p>Let&#8217;s see what I want to do here.</p>



<p>No, I think that&#8217;s part of a different program.</p>



<p>OK, I&#8217;m going to call a demo.</p>



<p>I don&#8217;t think I need to add that one thing that I was just looking at.</p>



<p>So I&#8217;m going to call a function that&#8217;s going to be our main demo.</p>



<p>So it&#8217;s going to jump down somewhere.</p>



<p>I&#8217;ll just create the function down later.</p>



<p>So let&#8217;s see.</p>



<p>Where is it?</p>



<p>Standard input.</p>



<p>I&#8217;m going to start off by saying this is my standard input function which is this and</p>



<p>is this pop p and then pop r12 and then it returns if you don&#8217;t know what a prologue</p>



<p>and epilogue are or why I&#8217;m doing pushes and pops just see my other videos but basically</p>



<p>I&#8217;m responsible for preserving it.</p>



<p>And I&#8217;m going to use the base pointer to help me remember where the stack started.</p>



<p>I&#8217;m just going to choose to…</p>



<p>What did I do?</p>



<p>I started the wrong function.</p>



<p>I&#8217;m going to do that one later.</p>



<p>Okay, for now, let me go find where that other one is.</p>



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



<p>Should have double checked.</p>



<p>I&#8217;m going way too fast.</p>



<p>I&#8217;ll just copy paste this whole thing from my solution.</p>



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



<p>This video is a mess.</p>



<p>Honestly, it&#8217;s a mess, but that&#8217;s okay.</p>



<p>I&#8217;m going to try my best to explain it.</p>



<p>So I have this function standard output standard error and I&#8217;m calling it here.</p>



<p>So it should jump right down there as soon as the program starts.</p>



<p>The standard input one is not getting called yet, but we&#8217;ll call it at the end of this</p>



<p>video.</p>



<p>print null terminated string function to print a message to standard output.</p>



<p>Remember standard output was just you know pipe one. Standard error is going to</p>



<p>be pipe two. So if I go back here and I say I want to print this matches to that</p>



<p>pipe then it&#8217;ll go to standard output just like the echo program was doing</p>



<p>when we looked at it a moment ago. And then next we&#8217;re going to print to</p>



<p>Oh, that was just an announcement that we&#8217;re going to begin.</p>



<p>So this is the real message that will say this is definitely going to standard output.</p>



<p>And then this other message is going to go to standard error, which is when we when we</p>



<p>looked at C error or pipe number two a moment ago.</p>



<p>So basically, I&#8217;m just printing an announcement and then two important messages.</p>



<p>One is going to pipe one and one is going to pipe two for standard output and standard</p>



<p>error.</p>



<p>OK, like really not very complicated at all.</p>



<p>Right.</p>



<p>So if I run this program now, hopefully I didn&#8217;t crash it by copy pasting out of order here.</p>



<p>Yeah, there we go.</p>



<p>Clear and make run.</p>



<p>So notice how it says that the messages are going to be printed.</p>



<p>And then notice that both of my messages actually show up on the terminal.</p>



<p>The one that says this message will print to standard output, number one, pipe one.</p>



<p>This message will print to standard error, pipe two.</p>



<p>They both get printed to the terminal because by default, the terminal will just take both of those pipes.</p>



<p>both of those pipes it&#8217;ll take one and two standard output and error but I can</p>



<p>use those little redirection tricks to mute one or more of those pipes so I&#8217;m</p>



<p>gonna say pipe one which is standard output I&#8217;m just gonna redirect that to</p>



<p>dev null to say I don&#8217;t want to see standard output if I run it again</p>



<p>notice how the only thing you can see on the string or on the screen is standard</p>



<p>error pipe to file descriptor 2 on the other hand if I redirect file descriptor</p>



<p>descriptor to pipe two to nothing then I should see all the normal messages and not see the error</p>



<p>message notice right here it&#8217;s it prints all the regular you know make file stuff and then the</p>



<p>welcome message and then here&#8217;s our message that says this is going to be printed to standard output</p>



<p>so notice how the error message is missing so cool if I mute both of those I can do that I can</p>



<p>then nothing gets printed at all because that&#8217;s all we really have is standard output and standard</p>



<p>error all right not too bad right that&#8217;s the basic idea between redirecting and things you</p>



<p>could also do things like this if I okay this is not part of my plan but suppose I wanted to take</p>



<p>any error message and redirect it to instead of dev null I wanted to redirect it to some kind of</p>



<p>standard error.txt.</p>



<p>Notice how nothing seems to have shown up on the screen, but if I list the directory,</p>



<p>now there&#8217;s a text file called standard error that contains the error message.</p>



<p>So you can redirect pipes, you can reattach them if you&#8217;re like an elite hacker.</p>



<p>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>I&#8217;m going to remove that.</p>



<p>Now let&#8217;s look at standard input.</p>



<p>How do you send input to the program?</p>



<p>Remember when programs call other programs, they can send each other standard input too.</p>



<p>It&#8217;s just that when I launch something on the terminal, I&#8217;m just, you know, I&#8217;m the</p>



<p>human and so whatever I type or, you know, what command I type is going to go into standard</p>



<p>input if I do it the right way.</p>



<p>So let&#8217;s go back to the program here.</p>



<p>Okay, so let&#8217;s finish up standard input.</p>



<p>And let&#8217;s see the pre-made code that I had here.</p>



<p>I had the prolog and the epilog, right?</p>



<p>Move the base pointer</p>



<p>RBP and R12. Oh, I got to put the</p>



<p>Base pointer back into the stack pointer. Let&#8217;s see move</p>



<p>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>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>Thought hey wouldn&#8217;t it be fun instead of having a global</p>



<p>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>a local array in C++. You&#8217;re making a buffer on the stack. So this is not a stack pointer video.</p>



<p>So long story short, I&#8217;m just going to keep track of where the stack pointer started with this base</p>



<p>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>from the stack pointer. And what is the buffer size? I&#8217;ve chosen to do a buffer size of about</p>



<p>So that means the user or me, I could send about eight kilobytes, maybe eight kilobytes</p>



<p>or less into standard input and be sure that it&#8217;s all actually going to be received by</p>



<p>the program.</p>



<p>And then I&#8217;m just going to use R12 to remember where the start of the buffer is, which is</p>



<p>just wherever the stack pointer started.</p>



<p>Because if we subtract and then the data that increases is actually increasing in memory,</p>



<p>but making room on the stack decreased, it&#8217;s just the way it works.</p>



<p>There&#8217;s going to be another stack video out there.</p>



<p>stack video out there anyway for now I just create a buffer on the stack and</p>



<p>then I&#8217;m gonna print a little hello message so I&#8217;m gonna print this so just</p>



<p>my message saying hey we&#8217;re about to do something from standard input and then</p>



<p>I&#8217;m gonna send that to a standard output so the message is gonna go to standard</p>



<p>output but I&#8217;m announcing that we&#8217;re about to read from standard input</p>



<p>assuming I actually cleaned up correctly let me double check this this should</p>



<p>It should just simply say that it&#8217;s going to without crashing.</p>



<p>Okay, looks good.</p>



<p>So then now I&#8217;m ready to actually read from standard input.</p>



<p>We&#8217;re going to use a system call from this.</p>



<p>If you want more information on system calls, see my other videos.</p>



<p>But basically you just go to the system call codes table,</p>



<p>look up appendix area of whatever textbook you&#8217;re using.</p>



<p>And you say, what&#8217;s the code to read?</p>



<p>I want the system to read.</p>



<p>what you know i want the system to read so i define that as system read and if you look up</p>



<p>here in my program it&#8217;s just call code zero so sys call code zero is to read from somewhere</p>



<p>and then uh where do i want to read from that&#8217;s the second argument for the standard</p>



<p>or for the system call i want to read from standard input that&#8217;s going to be the number zero</p>



<p>if you recall and then here is a pointer to the first character of wherever i want it to</p>



<p>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>read somewhere so that I can save it and access it. So R12 is basically going to be a bunch of</p>



<p>free bytes that I created on the stack. So I like, you know, I&#8217;m, I like made the stack bigger,</p>



<p>but it&#8217;s mostly empty or with junk data. And R12 is just pointing to the first byte that I have</p>



<p>reserved. So, you know, you could imagine maybe you make a global variable in the BSS section up</p>



<p>up at the top, you know, like an eight kilobyte array of bytes, and then just sort of point</p>



<p>it to that. But in my case, I&#8217;m just using the stack because I think it&#8217;s cooler. So</p>



<p>I tell it, Hey, that&#8217;s the first bite on the stack that you want that I want you to start</p>



<p>reading from. And then I give it the buffer size, which we just looked at. And then I</p>



<p>do a system call and then the system now should do all the hard work for me. It&#8217;ll look at</p>



<p>the pipe for standard input. It&#8217;ll grab all of it. And then it will only grab, you know,</p>



<p>it&#8217;ll stop if the input ends before that and then when I&#8217;m done I should be able</p>



<p>to actually read from the buffer which is sitting at R12 and print it to the</p>



<p>screen so the next thing I&#8217;m gonna copy paste in there is I&#8217;m gonna print what</p>



<p>I just read from standard input so this is another call to my function my little</p>



<p>doggy wants to go pee even though he&#8217;s completely lying he just went pee so at</p>



<p>wants to go outside and lift his leg and do nothing and then come back in and demand a treat.</p>



<p>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>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>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>take that R12 buffer that I just wrote to and I&#8217;m going to print that directly to standard output</p>



<p>output and this is not really the important part of the video.</p>



<p>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>function.</p>



<p>And then maybe I&#8217;ll say goodbye.</p>



<p>I think I just added a string for that.</p>



<p>So maybe I&#8217;ll say goodbye.</p>



<p>I&#8217;ll say goodbye before we end the function and then I&#8217;ll actually return.</p>



<p>So let me go here and run the program.</p>



<p>Let&#8217;s see, begin.</p>



<p>printed because we didn&#8217;t send anything and we also didn&#8217;t call that let me call</p>



<p>standard input call stdin and so now it&#8217;s actually gonna print basically</p>



<p>nothing oh I didn&#8217;t send it anything so it&#8217;s just kind of hanging you probably</p>



<p>want to check this sort of situation but that&#8217;s that&#8217;s not the point of this</p>



<p>video let me show you how to pipe some some stuff into standard input real fast</p>



<p>hello and then just put a little or bar in the shell this will take the output</p>



<p>of echo and instead of printing it to the terminal it&#8217;ll attach its standard</p>



<p>output into the standard input of make run pretty cool shell trick so now we</p>



<p>should see the word hello the word hello printed and that&#8217;s what it does we have</p>



<p>detected the following stdin and it says like here&#8217;s begin that&#8217;s just a string</p>



<p>that I had that I print every time and then here&#8217;s the string that it actually</p>



<p>And then it ends.</p>



<p>Pretty nice.</p>



<p>I think maybe we could probably do the empty thing if I just started typing here.</p>



<p>And then maybe I hit control D to detach the buffer.</p>



<p>And yeah, that&#8217;s what it does.</p>



<p>It sort of prints here and then it ends there.</p>



<p>Didn&#8217;t do a new line.</p>



<p>I think it probably doubled it for some reason because I…</p>



<p>Let&#8217;s see.</p>



<p>I didn&#8217;t plan for this.</p>



<p>Let me see.</p>



<p>plan for this let me see it printed oh because I did I did not null terminate the string so</p>



<p>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>one two three four five then I&#8217;m going to just disconnect the standard input buffer because the</p>



<p>printing function needs a null terminator to know when to stop there&#8217;s not going to be a null</p>



<p>terminator because I just typed and disconnected the uh the input so it&#8217;s actually going to just</p>



<p>until it randomly finds a zero and then it&#8217;ll stop so if I disconnect it it</p>



<p>that explanation was bunk let me try that one more time I&#8217;m gonna go a bunch</p>



<p>of H&#8217;s I guess there is a null terminator I disconnect it no no see how</p>



<p>yeah these little symbols there so it does kind of print until it finds a</p>



<p>null terminator but I guess I was confused because it seemed like it</p>



<p>like it printed it twice what&#8217;s actually happening is when i type then it&#8217;s getting sent to the</p>



<p>terminal which and that&#8217;s not part of the program that we just wrote then when i disconnect standard</p>



<p>input then the program finally prints everything that i actually typed until it hits a null</p>



<p>terminator in this case it looks like it seems to have worked but uh if i just do like a couple</p>



<p>sevens and then disconnect probably have to do this a bunch of times before we&#8217;ll see those weird</p>



<p>What did I do? Hit enter. That seems to have worked. My dog is stressing me out</p>



<p>but I love him. Alright anyway just trust me on this sometimes it&#8217;s hitting the</p>



<p>null terminator in time sometimes it&#8217;s not. But I hope by now you understand the</p>



<p>difference between standard input standard output and standard error. Thank</p>



<p>you so much for watching this video that I&#8217;m recording and I hope you learned a</p>



<p>little bit of stuff and had a little bit of fun. I&#8217;m gonna let my dog lie to me</p>



<p>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>Hey everybody, thanks for watching this video again from the bottom of my heart.</p>



<p>I really appreciate it.</p>



<p>I do hope you did learn something and have some fun.</p>



<p>If you could do me a please, a small little favor, could you please subscribe and follow</p>



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



<p>that you&#8217;re looking at right now.</p>



<p>It would really mean the world to me and it&#8217;ll help make more videos and grow this community.</p>



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



<p>or just I&#8217;ll be able to keep making videos in general. So please do me a kindness and subscribe.</p>



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



<p>somebody subscribed or followed. It just wakes me up and I get filled with joy. That&#8217;s exactly what</p>



<p>happens every single time. So you could do it as a nice favor to me or you could troll me if you</p>



<p>want to just wake me up in the middle of the night, just subscribe and then I&#8217;ll just wake up.</p>



<p>wake up I promise that&#8217;s what will happen also if you look at the middle of</p>



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



<p>to the website which I think is also named somewhere at the bottom of this</p>



<p>video and it&#8217;ll take you to my main website where you can just kind of like</p>



<p>see all the videos I published and the services and tutorials and things that I</p>



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



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



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



<p>those in the middle of the night i get i wake up in a cold sweat and i&#8217;m like it would really it</p>



<p>really mean the world to me i would really appreciate it so again thank you so much for</p>



<p>watching this video and um enjoy the cool music as as i fade into the darkness which is coming for us</p>



<p>Thank you.</p>
<p>The post <a href="https://www.NeuralLantern.com/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>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 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>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>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>Thanks for watching!</p>



<p>Find us on other social media here:</p>



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



<p>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>Hey everybody in this video we&#8217;re going to talk a little bit about system services</p>



<p>in an x86-64 machine while programming in assembly</p>



<p>this video is going to focus on yasm assembly but this should also work for any other assembly</p>



<p>language that you program as long as you have the right kind of cpu x86-64 is also known as</p>



<p>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>not write it the person who wrote it is a genius it&#8217;s called x86 64 assembly language programming</p>



<p>with the boot to it&#8217;s a free and open source book so anybody can go get it find his website</p>



<p>and here&#8217;s the version i&#8217;m using and honestly a really old version of this book is still really</p>



<p>good he just keeps you know making little improvements but it was it was good even a</p>



<p>even a long time ago. Anyway, so what is a system service? For starters, before I go into this book</p>



<p>a little bit more, what is the system service? Suppose for the sake of argument that I&#8217;ve</p>



<p>already written an assembly program, a pure assembly program, not a hybrid module program</p>



<p>or anything. If you want to learn assembly, see my other videos, but for now I&#8217;m just going to</p>



<p>assume you kind of already know how, so I&#8217;m just going to open up a pre-made program here.</p>



<p>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>just so you can see what&#8217;s inside of here.</p>



<p>Okay, so I hope the red is not too hard to see.</p>



<p>Oh, the tabs got ruined on Nano.</p>



<p>Hang on, let me see if I can just open this up in Genie.</p>



<p>Should have done that to begin with, okay.</p>



<p>Yeah, okay, I wrote it in Genie</p>



<p>and I guess I don&#8217;t have the tabs set up in Nano very well.</p>



<p>Okay, so here&#8217;s like my assembly program.</p>



<p>You know, I just have a little string here</p>



<p>string length and CRLF for like a line feed and some file descriptors and stuff</p>



<p>that I talk about in other videos here&#8217;s the entry point of the program just like</p>



<p>a little start entry point because it&#8217;s pure assembly and all I&#8217;m gonna do is</p>



<p>I&#8217;m gonna print a message right I&#8217;m just gonna print a little hello message and</p>



<p>then call on CRLF all CRLF does is just it just prints a new line so the program</p>



<p>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>then you can see there&#8217;s some stuff happening up here in the make file which i forgot to update</p>



<p>the title ignore that basically the real action is this line right here hello i&#8217;m printing using</p>



<p>a system service if you already know assembly then you have probably already seen this before</p>



<p>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>system service to actually do the printing.</p>



<p>Kind of take it for granted here.</p>



<p>This little instruction called syscall.</p>



<p>What is a syscall?</p>



<p>Well, it&#8217;s just basically you set up some variables, some,</p>



<p>some codes, some data, whatever in, in some special registers.</p>



<p>One of the registers lets the system know what you want it to do.</p>



<p>One of the registers is like, or I guess all the following registers</p>



<p>if they&#8217;re needed, are just input to the system call that you&#8217;re trying to do.</p>



<p>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>this certain thing i have a defined set up here called system right but if you look up in my</p>



<p>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>the system know that i want it to write to some pipe to some file then you know depending on what</p>



<p>code you&#8217;re using rdi rsi rdx and other things may be needed or maybe not be needed at all</p>



<p>in this case when i&#8217;m writing somewhere it wants three arguments so i have to use rdi rsi and rdx</p>



<p>because those are the three arguments the first argument that it wants is where to print</p>



<p>see my other videos for a more in-depth explanation of file descriptors and pipes but just long story</p>



<p>pipes standard input standard output and standard error standard out is a pipe</p>



<p>number one let&#8217;s see where&#8217;s that yeah it&#8217;s just assigned a number one so</p>



<p>every program has its own number one pipe which is just standard output and I</p>



<p>guess I should say that it gets piped to file descriptor one it&#8217;s kind of like an</p>



<p>ambiguous term sort of but the system right call will write data to a file</p>



<p>file and if i give it a file handle of just one that lets the system know oh actually i want to</p>



<p>write to this standard output for the one particular process that we&#8217;re inside of but</p>



<p>you could open any file that you want and get like a file handle to it and the operating system will</p>



<p>realize oh you know i i associate that file handle i gave you with this certain open file that you</p>



<p>created or opened for reading or whatever um and so when you give the handle back it knows where</p>



<p>knows where to write. So in this case,</p>



<p>it&#8217;s treating the standard output just like a file,</p>



<p>just a file with a special handle of just one.</p>



<p>I&#8217;ll talk about that more in another video.</p>



<p>Then the next two arguments it wants is just the,</p>



<p>a pointer to the first character of the message that you want to print.</p>



<p>And then just an integer representing the length of the message that you want</p>



<p>to print. And if we just look up here again at my little hello,</p>



<p>my hello symbol points to an array of bytes, just characters.</p>



<p>bytes just characters there is no null terminator on there it&#8217;s just bytes only</p>



<p>and so when I give it you know message hello by itself what I&#8217;m really giving</p>



<p>it is a pointer to that H letter just a pointer to the memory look you know I&#8217;m</p>



<p>pointing to the memory location of the very first character in that string and</p>



<p>then for the length I&#8217;m just using a special token little shortcut thing</p>



<p>where you just put a dollar sign minus and then the name of another string and</p>



<p>and then the name of another string,</p>



<p>and then the assembler will compute the length of that string.</p>



<p>So this right here is the same thing as me</p>



<p>just typing the exact length of the string.</p>



<p>I don&#8217;t even know what it is.</p>



<p>I don&#8217;t feel like counting it.</p>



<p>You can at home if you want.</p>



<p>One, two, three, four, I&#8217;m not gonna do it.</p>



<p>Anyway, so we basically just say,</p>



<p>you know, I would like to write somewhere.</p>



<p>I would like to write to the standard output file,</p>



<p>file handle, I would like to, you know, to that pipe.</p>



<p>I would like to write, you know, this string,</p>



<p>you know this string and then i would like to tell you that this is how long that string is</p>



<p>and then after you set all that stuff up you just say system call and the system will go ahead and</p>



<p>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>you know figure out how to do everything that it&#8217;s supposed to do and all you have to do is</p>



<p>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>call i talked about functions in other videos but basically you know i&#8217;m just calling a function here</p>



<p>you know i&#8217;m just calling a function here and it does the same thing it makes a system call but</p>



<p>instead of printing the hello string it&#8217;s printing my crlf string and if you just kind of look at what</p>



<p>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>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>new line and then at the very end it does another system call to properly exit the program if this</p>



<p>hybrid program and I had like the entry point of main, you know, that the GCC libraries give you,</p>



<p>then I would probably just want to return at the end of this function. But since this is pure</p>



<p>assembly, I can actually just exit the program and be fine. So exit with success is a different</p>



<p>system call code. Notice how I have the symbol system exit loaded into RAX. And if you look up</p>



<p>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>a code of 60 somewhere that means i want to exit the program the only argument that takes is rdi</p>



<p>which is just what is the exit code that you want to return to the operating system so um</p>



<p>if you recall from some of my other videos let&#8217;s see if we do echo hello echo always succeeds at</p>



<p>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>the program echo launches and it will succeed by just printing the word hello like see how it just</p>



<p>like see how it just does that so that means after that command i could echo the special</p>



<p>variable dollar sign question mark just to see what the echo command exited with right it should</p>



<p>be a zero because it succeeded so you can see now a zero under the hello on the other hand</p>



<p>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>So it&#8217;s a zero at the very end.</p>



<p>But if I try to concatenate a file that doesn&#8217;t actually exist,</p>



<p>I&#8217;ll put a OS release two because that doesn&#8217;t exist.</p>



<p>Notice how the cat command fails because it couldn&#8217;t find the file and the exit</p>



<p>code is a one.</p>



<p>So all you&#8217;re doing when setting RDI here is just controlling what exit code you</p>



<p>want your program to exit with.</p>



<p>This isn&#8217;t very convenient for a human running a program.</p>



<p>Maybe if you want to look at the exit code for some reason, but this is really,</p>



<p>really convenient for programs that want to automate other programs.</p>



<p>programs just keep in mind if you have a program that is executing another</p>



<p>program and it wants to see if that program succeeded the exit code is one</p>



<p>of the easiest ways to find out if the program succeeded so this is the whole</p>



<p>idea of this entire program we&#8217;re just using two system calls to print</p>



<p>something and then to exit the program hopefully this makes sense and now I</p>



<p>want to go show you this wonderful wonderful book if I didn&#8217;t already say</p>



<p>If I didn&#8217;t already say this before, sorry if I already did.</p>



<p>This book is written by a genius.</p>



<p>It&#8217;s called this.</p>



<p>Here&#8217;s the guy who wrote the book.</p>



<p>And here&#8217;s the version that I&#8217;m using, but old versions are good.</p>



<p>You can get this book for free on his website if you go find it.</p>



<p>So I&#8217;m just going to go to this assembly book, which deals with a lot of Yasm assembly tips</p>



<p>and tricks and tutorials and explanations and things.</p>



<p>And I&#8217;m going to go to the section labeled after system services.</p>



<p>So for me, this is a chapter 13 system services, just to give you a little explanation of what</p>



<p>this is.</p>



<p>You know, this is basically what I said.</p>



<p>It gives you, you know, a chance for your application to ask the operating system to</p>



<p>do something for you that you don&#8217;t want to program from scratch in assembly or that you</p>



<p>can&#8217;t program.</p>



<p>But here&#8217;s the real juicy details here.</p>



<p>If we go to, yeah, yeah.</p>



<p>Okay.</p>



<p>C, which is section 23.0 system services.</p>



<p>There&#8217;s a whole bunch of system services in here that you can, uh, that you can read about.</p>



<p>So for starters, if we go down a little bit, notice how it says basic system services.</p>



<p>If we go down a little bit, you can see right here, the system write service that we&#8217;ve</p>



<p>been using in the sample assembly program.</p>



<p>You know, when we&#8217;re just printing, we say code one is to write something.</p>



<p>And so this is what the table says.</p>



<p>you place inside of RAX before syscall.</p>



<p>Call code 1 is going to be to write characters and then it tells you what arguments it needs.</p>



<p>So like RDI is the file descriptor where do you want to write to.</p>



<p>Just like I said before you could give it a file handle that you already received from</p>



<p>opening a file or creating a file.</p>



<p>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>actually file descriptor 0 is standard input I don&#8217;t think that would actually work.</p>



<p>zero to reading if you wanted to read the user&#8217;s input or or you know any input that that program</p>



<p>was given through its standard input rsi is just the address of the characters to write like we</p>



<p>talked about before and then rdx is the count of the characters to write notice how there are no</p>



<p>more arguments and that&#8217;s why i only gave it three arguments besides rex notice how it says here if</p>



<p>unsuccessful it returns a negative value if successful it returns the count of characters</p>



<p>count of characters actually written this is a really really good idea if you think about it</p>



<p>um a lot of new programmers they don&#8217;t really look at return codes when they call system services</p>



<p>or built-in c functions or built-in c++ functions they just kind of call it and hope for the best</p>



<p>but imagine if you uh if you try to write a huge long string in uh using system write call code one</p>



<p>and maybe some of the characters did write but the system decided to only write i don&#8217;t know half for</p>



<p>reason maybe it ran out of buffer maybe you know it interrupted you know your right or something</p>



<p>like that so it could happen what that would mean is that the return value it would be greater than</p>



<p>zero indicating that some characters were written but it&#8217;ll tell you exactly how many characters</p>



<p>were written so you&#8217;ll know that you got to keep calling the sys call until all the characters were</p>



<p>successfully written why would you do this i don&#8217;t know maybe you have a gigantic string like maybe</p>



<p>like a several gigabyte file and you wanted to copy it to another file so you&#8217;ll be calling</p>



<p>system write over and over again and system write it&#8217;s only going to write so many bytes at the same</p>



<p>time so you use the return value to figure out how far forward in the read buffer you need to advance</p>



<p>or you know whatever so that you can well write the entire complete file or the entire complete</p>



<p>string or whatever it is without any gaps without it being truncated and so forth and then of course</p>



<p>And then of course, if it returns a negative value, then it totally failed.</p>



<p>Like you tried to write to a bad file handle, like standard input or a file handle that</p>



<p>was closed or a file handle that was open for reading only, something like that.</p>



<p>And then you can use some if else, you know, branching logic.</p>



<p>I mean, not if else in assembly, it&#8217;s just going to be comparison and conditional jumping</p>



<p>or branching.</p>



<p>But you can have more control over your program, right?</p>



<p>Because you want your program to be able to respond to errors.</p>



<p>Let me just quickly cruise through all of the other options and let you know that</p>



<p>and let you know that in another video I&#8217;m gonna do a full tutorial for how to</p>



<p>copy one file to another by opening one file for reading and then like using</p>



<p>system calls to read from it and then open another file for writing and then</p>



<p>you know use system calls to write to it and I&#8217;m gonna do that looping buffer</p>



<p>stuff that I talked about before anyway so but for now I&#8217;m just gonna go through</p>



<p>the rest of these system call codes let&#8217;s see so first off we got open well</p>



<p>Well, if you want to open a file, then you just give it call code 2 and you pass in the</p>



<p>address here, you know, RDI, you say, here&#8217;s the address of a null terminated file name.</p>



<p>So that means somewhere in memory, you have to have the file name that you want to open</p>



<p>with a null terminator, like a zero at the end of the string, either the full path or</p>



<p>relative path to wherever the program is currently running.</p>



<p>And then RSI is going to be file status flags.</p>



<p>And what does that mean?</p>



<p>And what does that mean? We can actually just search for that.</p>



<p>I&#8217;m going to say control C to copy,</p>



<p>and then I&#8217;m going to do control F to search for that.</p>



<p>And let&#8217;s see what page of mine right now, 340.</p>



<p>If I go down to the search results for that,</p>



<p>it just explains what the file modes are. So if you put value zero,</p>



<p>that means read only you&#8217;re opening the file and read only mode value one.</p>



<p>That means a write only a value to allows reading and writing to the file.</p>



<p>So if you don&#8217;t know what that means, well, there it was.</p>



<p>What was I just on before?</p>



<p>40?</p>



<p>Oh, gosh.</p>



<p>Completely lost my…</p>



<p>Oh, yeah, 40.</p>



<p>Okay, so opening pretty easy, right?</p>



<p>Like if you didn&#8217;t know this before, you didn&#8217;t necessarily read me.</p>



<p>You could just look at this table and go, what do I want to do?</p>



<p>I want to open.</p>



<p>So here&#8217;s the code.</p>



<p>Here&#8217;s the address that it needs.</p>



<p>When you&#8217;re done opening a file, when you&#8217;re done working with a file,</p>



<p>you want to close that file unless the whole program is going to terminate.</p>



<p>unless the whole program is going to terminate at that point you i mean maybe you have a function</p>



<p>that gets called somewhat often and you kind of always have to open a file and sort of look at</p>



<p>something or write something and maybe you want to close it when the function is over right you</p>



<p>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>bad behavior to your program if you just have a bunch of file handles floating around that you</p>



<p>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>and it only wants one argument.</p>



<p>It just wants the file descriptor of the file that you wanted to close.</p>



<p>So if you open a file for a reading or to create it,</p>



<p>you get a file descriptor back.</p>



<p>You should probably hang on to it somewhere on the stack</p>



<p>or like in a global variable or like, you know, in a register or whatever.</p>



<p>Then when you&#8217;re done doing something to that file</p>



<p>and you&#8217;re sure that it&#8217;s time to close it,</p>



<p>just give that handle right back when you call code three to close it.</p>



<p>Then you can sort of like seek, you know,</p>



<p>forward and backwards to the file if you want to.</p>



<p>You can fork the current process.</p>



<p>You can fork the current process that&#8217;s kind of advanced for this video.</p>



<p>You can exit.</p>



<p>Remember before we exited from that program with code 60.</p>



<p>So that&#8217;s it. Call code 60.</p>



<p>If you didn&#8217;t watch this video, you could have gone through this table and just said,</p>



<p>Oh, you know, I want to terminate the executing process.</p>



<p>I do a system call with code 60 and I&#8217;ll give the exit status to RDI,</p>



<p>which is typically zero for success.</p>



<p>Bunch of other stuffs create, get the time of day,</p>



<p>and then the file modes that we talked about before.</p>



<p>we talked about before and then file permissions. Let&#8217;s see, where&#8217;s the permission. I think it&#8217;s</p>



<p>when you want to create a file. Yeah, right here. If we wanted to create a file, that&#8217;s code 85.</p>



<p>RDI is the name of the file, you know, like a pointer to the string. RSI is the file mode flags.</p>



<p>And then you&#8217;re thinking like, what are the file mode flags? Just go down here and then here you</p>



<p>go. All the file modes that you could ever want. You just look at it and go, well, I guess I can</p>



<p>I guess I can just copy paste this number right here and it&#8217;ll end up being, you know,</p>



<p>whatever is described on the left and the right.</p>



<p>Like the group has read, write and execute permissions and so forth.</p>



<p>I mean, really, if you look at this long enough, you&#8217;ll kind of realize that this is a quad</p>



<p>word because it has a Q on the end of it.</p>



<p>And the rest is just an octal file permissions notation where this first number here, notice</p>



<p>how the first two are zeros, but this first actual number here is something that applies</p>



<p>to the user and then the next one applies to the group the next one applies to others who are not</p>



<p>part of the user or group and this video is not about file permissions I&#8217;ll probably make one</p>



<p>later if I think people will actually watch it but yeah you can just kind of like look it up here if</p>



<p>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>you a little code snip just to illustrate a little bit more what I was talking about before</p>



<p>for. People usually make this mistake, they&#8217;ll do see this is what people will usually do like new</p>



<p>programmers. Suppose this is a function pretend we&#8217;re in C or C++. For the time being, you call</p>



<p>some sort of a system function or an API of some other library. And you just you just hope that it</p>



<p>works, you just call it and then your program continues underneath, right? It&#8217;s a bad idea,</p>



<p>because maybe that failed, maybe there&#8217;s some action you need to take. So instead, it&#8217;s a really</p>



<p>really good idea to check the return result or i should say an even better idea is check the</p>



<p>documentation usually these types of you know c system calls or function calls or api calls or</p>



<p>whatever they&#8217;ll return something indicating success or failure sometimes the documentation</p>



<p>will tell you oh you&#8217;ve got to put like a pointer to some other data structure in the arguments</p>



<p>and then the result comes there of course just read the documentation but usually it&#8217;s just</p>



<p>So you want to grab the result and then check the result.</p>



<p>In this case I&#8217;m assuming this is sort of a standard function that will return zero</p>



<p>or greater on success like the system write call or the system read call and return less</p>



<p>than zero if it fails.</p>



<p>So here I&#8217;m just responding, you know, if it&#8217;s like if the result was greater than zero</p>



<p>then it&#8217;s like, yay we can proceed.</p>



<p>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>then I&#8217;ll print a complaint to the user and then I&#8217;ll take some sort of an action</p>



<p>like I&#8217;ll write to a log file, send an email somewhere, you know,</p>



<p>do something to try and recover from the error and alert admins to the error.</p>



<p>And in this case, I&#8217;m just throwing an exception.</p>



<p>This video is not about exceptions, but so I just want you to know,</p>



<p>this is a good design pattern to not just discard the result of a call to</p>



<p>something. You should check the result and see what you&#8217;re supposed to do.</p>



<p>Let&#8217;s see, what else did I want to tip?</p>



<p>system services, file handles.</p>



<p>Yeah, okay, I think that&#8217;s everything that I wanted to tell you.</p>



<p>In future videos, I&#8217;m going to talk about, you know, some of this stuff much more in-depth,</p>



<p>like with my file.io video that I&#8217;m going to publish pretty soon, which is, you know,</p>



<p>it lets you read and write to files, and it uses a loop to read a little bit at a time.</p>



<p>Okay, thank you so much for watching this video.</p>



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



<p>I&#8217;m outie.</p>



<p>Or something.</p>



<p>Goodbye.</p>



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



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



<p>videos longer videos better videos or just I&#8217;ll be able to keep making videos</p>



<p>in general so please do do me a kindness and and subscribe you know sometimes</p>



<p>I&#8217;m sleeping in the middle of the night and I just wake up because I know</p>



<p>somebody subscribed or followed it just wakes me up and I get filled with joy</p>



<p>that&#8217;s exactly what happens every single time so you could do it as a nice favor</p>



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



<p>just subscribe and then I&#8217;ll just wake up I promise that&#8217;s what will happen</p>



<p>also if you look at the middle of the screen right now you should see a QR</p>



<p>code which you can scan in order to go to the website which I think is also</p>



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



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



<p>services and tutorials and things that I offer and all that good stuff and if</p>



<p>if you have a suggestion for uh uh clarifications or errata or just future videos that you want to</p>



<p>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>send me a comment whatever i also wake up for those in the middle of the night i get i wake</p>



<p>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>appreciate it so again thank you so much for watching this video and um enjoy the cool music</p>



<p>as I fade into the darkness which is coming for us all.</p>



<p>Thank you.</p>
<p>The post <a href="https://www.NeuralLantern.com/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>
