Skip to playerSkip to main content
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

Introduction to System Services 00:00:00
System Services in x86-64 Assembly 00:00:04
Recommended Book on Assembly 00:00:35
What is a System Service? 00:01:01
Example Assembly Program 00:01:21
Syscall Instruction Explanation 00:02:52
Standard Output and File Descriptors 00:04:01
Printing a String with Syscall 00:05:16
Exiting a Program with Syscall 00:07:21
Exit Codes and Program Success 00:08:02
Book Reference for System Services 00:09:51
Detailed System Write Service 00:11:01
Checking System Call Return Values 00:12:08
File Operations with System Calls 00:14:35
Closing Files and Best Practices 00:15:50
Other System Call Codes 00:16:43
File Permissions and Modes 00:17:20
Handling System Call Results 00:19:03
Future Video Plans and Wrap-Up 00:20:40
Call to Subscribe and Outro 00:21:28

Thanks for watching!

Find us on other social media here:
- https://www.NeuralLantern.com/social

Please help support us!

- Subscribing + Sharing on Social Media
- Leaving a comment or suggestion
- Subscribing to our Blog
- Watching the main "pinned" video of this channel for offers and extras
Transcript
00:00hey everybody in this video we're going to talk a little bit about system services
00:04in an x86-64 machine while programming in assembly
00:15this video is going to focus on yasm assembly but this should also work for any other assembly
00:19language that you program as long as you have the right kind of cpu x86-64 is also known as
00:25amd 64 architecture so uh bear that in mind okay so for starters let's see here's a wonderful book
00:33i love pushing this book i did not write it the person who wrote it is a genius it's called x86-64
00:39assembly language programming with ubuntu it's a free and open source book so anybody can go get it
00:44find his website and um here's the version i'm using and uh honestly a really old version of
00:50this book is still really good he just keeps you know making little improvements uh but it was it
00:55was good even a long time ago anyway so what is the system service for starters before i go into this
01:01book a little bit more uh what is the system service suppose for the sake of argument that i'm i've
01:06already written an assembly program a pure assembly program not a hybrid module program or anything
01:12if you want to learn assembly see my other videos but for now i'm just going to assume you kind of
01:17already know how so i'm just going to open up a pre-made program here i'm going to do nano so i
01:22can edit the assembly file just so you can see what's inside of here okay so uh i hope the red
01:30is not too hard to see oh the tabs got ruined on nano hang on let me see if i can just open this up in
01:35genie should have done that to begin with okay yeah okay i wrote it in genie and i guess i don't have
01:41the tabs set up in nano very well okay so you know here's like my assembly program you know i just have
01:49a little string here and um a string length and crlf for like a line feed and some file descriptors and
01:57stuff that i talk about in other videos here's the entry point of the program uh just like a little
02:03start entry point because it's pure assembly and uh all i'm going to do is i'm going to print a message
02:08right i'm just going to print a little hello message and then call on crlf all crlf does is
02:14just it just prints a new line so the program is really really simple um if i run it right now you'll
02:21just see what happens if i say let's see clear and make run then you can see there's some stuff happening
02:29up here in the make file which i forgot to update the title ignore that uh basically the real action
02:36is this line right here hello i'm printing using a system service if you already know assembly then
02:40you have probably already seen this before and it seems a little boring that's good but i just want
02:45you to know that we're using a system service to actually do the printing kind of take it for granted
02:52here this little instruction called syscall what is a syscall well it's just basically you set up some
02:58variables some some codes some data whatever in in some special registers one of the registers lets the
03:06system know what you want it to do one of the registers is like or i guess all the following
03:10registers if they're needed are just input to the system call that you're trying to do so in this case
03:17rax is letting the system know i want you to do this this code i want you to do this certain thing
03:24i have a defined set up here called system right but if you look up in my little defines area
03:30it's just a number one so that means if i've set the number one in rax i'm letting the system know
03:37that i want it to write to some pipe to some file uh then you know depending on what code you're using
03:45rdi rsi rdx and other things may be needed or may not be needed at all in this case when i'm writing
03:51somewhere it wants three arguments so i have to use rdi rsi and rdx because those are the three arguments
03:58the first argument that it wants is where to print see my other videos for a more in-depth explanation
04:05of uh file descriptors and pipes but just long story short every program that gets created ends
04:11up with three pipes uh standard input standard output and standard error standard out is a pipe
04:18number one let's see where's that yeah it's just assigned a number one so every program has its own
04:24number one pipe which is just standard output and um uh i guess i should say that it gets piped to
04:32file descriptor one it's kind of like an ambiguous term sort of but uh the the system write call will
04:38write uh data to a file and if i give it a file handle of just one that lets the system know oh actually
04:45i want to write to this standard output for the one particular process that we're inside of
04:51but you could open any file that you want and get like a file handle to it and the operating system
04:56will realize oh you know i i associate that file handle i gave you with this certain open file
05:02that you created or opened for reading or whatever um and so when you give the handle back it knows
05:08where to write so in this case it's treating the standard output just like a file just a file with a
05:13special handle of just one i'll talk about that more in another video then the next two arguments
05:19it wants is just the uh the a pointer to the first character of the message that you want to print and
05:25then just an integer representing the length of the message that you want to print and if we just look
05:30up here again at my little hello my hello symbol points to an array of bytes just characters there is
05:38no null terminator on there it's just bytes only and so when i give it you know message hello by
05:45itself what i'm really giving it is a pointer to that h letter just a pointer to the memory look you
05:51know i'm pointing to the memory location of the very first character in that string and then for the
05:56length i'm just using a special uh token little shortcut thing where you just put a dollar sign minus
06:03and then the name of another string and then the assembler will compute the length of that string
06:09so this right here is the same thing as me just typing the exact length of the string i don't even
06:14know what it is i don't feel like counting it you can at home if you want one two three four i'm not
06:18going to do it anyway so uh we basically just say you know i would like to write somewhere i would like
06:26to write to the standard output file file handle i would like to you know to that pipe i would like to
06:31write you know this string and then i would like to tell you that this is how long that string is
06:37and then after you set all that stuff up you just say system call and the system will go ahead and do
06:41all of the hard work for you it'll go figure out how to actually write data to the file and you know
06:48figure out how to do everything that it's supposed to do and all you have to do is a little system call
06:52here right after this you can see that i have a call crlf that's just a function call i talked about
06:58functions in other videos but basically you know i'm just calling a function here and it does the
07:02same thing it makes a system call but instead of printing the hello string it's printing my crlf
07:07string and if you just kind of look at what my crlf is it's just these two characters 13 and 10 so
07:12like you know slash r slash n for carriage return line feed that's why i call it crlf
07:19so all this program does is just print a message and then a new line and then at the very end it does
07:24another system call to properly exit the program if this was a hybrid program and i had like
07:29the entry point of main you know that the gcc libraries give you then i would probably just
07:34want to return at the end of this function but since this is pure assembly i can actually just
07:38exit the program and be fine so exit with success is a different system call code notice how i have
07:44the symbol system exit loaded into rax and if you look up here system exit is just the code 60.
07:51so if i send a code of one into rax that means i want to write some data somewhere if i send a code
07:56of 60 somewhere that means i want to exit the program the only argument that takes is rdi which
08:02is just what is the exit code that you want to return to the operating system so um if you recall
08:10from some of my other videos let's see if we do echo hello echo always succeeds at least as far as i know
08:15there's like it's like really hard to make it fail if i echo hello then the program echo
08:21launches and it will succeed by just printing the word hello like see how it just does that
08:26so that means after that command i could echo the special variable dollar sign question mark just to
08:32see what the echo command exited with right it should be a zero because it succeeded so you can
08:38see now a zero under the hello on the other hand if i concatenate um let's say a file that exists
08:47i should also get you know an error code that is a zero see how or an exit code so it's a zero at the
08:52very end but if i try to concatenate a file that doesn't actually exist i'll put uh os release 2
08:59because that doesn't exist notice how the cat command fails because it couldn't find the file
09:04and the exit code is a one so all you're doing when setting rdi here is just controlling what exit
09:09code you want your program to exit with this isn't very convenient for for a human running a program maybe if
09:15you want to look at the exit code for some reason but this is really really convenient for programs
09:19that want to automate other programs just keep in mind if you have a program that is executing another
09:24program and it wants to see if that program succeeded the exit code is one of the easiest ways to find out
09:30if the program succeeded so this is the whole idea of this entire program we're just using two system
09:34calls to print something and then to exit the program hopefully this makes sense and now i want to
09:41go show you this wonderful wonderful book if i didn't already say this before sorry if i already
09:51did this book is written by a genius it's called this here's the guy who wrote the book and here's
09:58the version that i'm using but old versions are good you can get this book for free on his website if
10:03you go find it so i'm just going to go to this assembly book which deals with a lot of yasm assembly tips
10:08and tricks and tutorials and explanations and things and i'm going to go to the section uh labeled after
10:15system services so for me this is a chapter 13 system services just to give you a little explanation
10:21of what this is uh you know this is basically what i said it gives you you know a chance for your
10:27application to ask the operating system to do something for you that you don't want to program
10:30from scratch in assembly or that you can't program but here's the real juicy details here if we go to
10:36uh yeah yeah okay okay if we go to uh the appendix which is it appendix c which is section 23.0 system
10:48services there's a whole bunch of system services in here that you can uh that you can read about so
10:53for starters if we go down a little bit notice how it says basic system services if we go down a little
10:58bit you can see right here the system right service that we've been using in the sample assembly program
11:04you know when we're just printing we say code one is to write something and so this is what the table
11:10says it says here's the call code where you which you place inside of rex before syscall
11:15call code one is going to be to write characters and then it tells you what arguments it needs so like
11:20rdi is the file descriptor where do you want to write to just like i said before you could give
11:25it a file handle that you already received from opening a file or creating a file
11:28or you could give it a zero or one or two if you want to try to write to one of the pipes although
11:34actually file descriptor zero is standard input i don't think that would actually work you could
11:39give a file descriptor zero to reading if you wanted to read the user's input or or you know any input
11:45that that program was given through its standard input rsi is just the address of the characters to
11:50write like we talked about before and then rdx is the count of the characters to write notice how there
11:55are no more arguments and that's why i only gave it three arguments besides rex notice how
12:02it says here if unsuccessful it returns a negative value if successful it returns the count of
12:08characters actually written this is a really really good idea if you think about it a lot of new
12:14programmers they don't really look at return codes when they call system services or built-in c functions
12:19or built-in c++ functions they just kind of call it and hope for the best but imagine if you uh if you
12:24try to write a huge long string in uh using system write call code one and maybe some of the characters
12:32did write but the system decided to only write i don't know half for whatever reason maybe it ran
12:37out of buffer maybe you know it interrupted you know you're right or something like that so it could
12:42happen what that would mean is that the return value it would be greater than zero indicating that some
12:47characters were written but it'll tell you exactly how many characters were written so you'll know
12:53that you got to keep calling the sys call until all the characters were successfully written
12:58why would you do this i don't know maybe you have a gigantic string like maybe a full huge file like a
13:04like a several gigabyte file and you wanted to copy it to another file so you'll be calling system right
13:11over and over again and system right it's only going to write so many bytes at the same time
13:16so you use the return value to figure out how far forward in the read buffer you need to advance
13:21or you know whatever so that you can uh well write the entire complete file or the entire complete
13:28string or whatever it is without any gaps without it being truncated and so forth and then of course
13:32if it returns a negative value then it totally failed like you tried to write to a bad file handle
13:36like standard input or a file handle that was closed or a file handle that was open for reading
13:41only something like that and then you can use some if else you know branching logic
13:45i mean not if else in assembly it's just going to be comparison and conditional jumping or branching
13:52and but you can have more control over your program right because you want your program to be able to
13:56respond to errors let me just quickly cruise through all of the other options and let you know that
14:01in another video i'm going to do a full tutorial for how to copy one file to another by opening one file
14:07for reading and then like using system calls to read from it and then open another file for writing
14:13and then you know use system calls to write to it and i'm going to do that looping buffer stuff that
14:17i talked about before anyway so but for now i'm just going to go through the rest of these system
14:21call codes let's see so first off we got uh open um well if you want to open a file then you just uh
14:32you know give it call code 2 and uh you pass in the address here you know rdi you say here's the
14:37address of a null terminated file name so that means somewhere in memory you have to have the file name
14:43that you want to open uh with with a null terminator like a zero at the end of the string
14:48either the full path or relative path to wherever the program is currently running
14:52and then rsi is going to be file status flags and what does that mean we can actually just search
14:58for that i'm going to say ctrl c to copy and then i'm going to do ctrl f to search
15:03for that and uh let's see what page am i on right now 340. if i go down to the search results for that
15:09it just explains what the file modes are so if you put value zero that means read only you're opening
15:16the file in read only mode a value one that means uh write only a value two allows reading and writing
15:23to the file so if you don't know what that means well there it was what was i just on before 40 oh
15:30gosh completely lost my oh yeah 40. okay so opening pretty easy right like if you didn't know this
15:37before you didn't necessarily read me you could just look at this table and go what do i want to do i
15:40want to open so here's the code here's the address that it needs when you're done opening a file when
15:48you're done working with a file you want to close that file unless the whole program is going to
15:53terminate at that point you i mean maybe you have a function that gets called somewhat often and you
15:59kind of always have to open a file and sort of look at something or write something and maybe you want
16:03to close it when the function is over right you don't want to have a file handle just open forever
16:08that's a waste of memory and it might introduce bad behavior to your program if you just have a
16:12bunch of file handles floating around that you forgot about so it's proper to close a file when
16:17you're finished with it that's just going to be code three and it only wants one argument it just wants
16:22the file descriptor of the file that you wanted to close so if you open a file for a reading or to
16:27create it you get a file descriptor back you should probably hang on to it somewhere on the stack or like
16:32in a global variable or like you know in a register or whatever then when you're done doing something to
16:36that file and you're sure that it's time to close it just give that handle right back when you call
16:42code three to close it then you can sort of like seek you know forward and backwards to the file if
16:46you want to you can fork the current process that's kind of advanced for this video you can exit remember
16:52before we exited from that program with code 60 so that's it call code 60 if you didn't watch this
16:59video you could have gone through this table and just said oh you know if i want to terminate the
17:02executing process i do a system call with code 60 and i'll give the exit status to rdi which is
17:08typically zero for success a bunch of other stuff's create uh get the time of day and then the file
17:15modes that we talked about before and then file permissions let's see where's the permission i think
17:20it's when you want to create a file yeah right here if we wanted to create a file that's code 85 rdi is
17:26the name of the file you know like a pointer to the string rsi is the file mode flags and then you're
17:32thinking like what are the file mode flags just go down here and then here you go uh all the file
17:39modes that you could ever want you just look at it and go well i guess i can just copy paste this number
17:46right here and it'll end up being you know whatever is described on the left and the right like uh the
17:51group has read write and execute permissions and so forth i mean really if you look at this long
17:57enough you'll kind of realize that this is a quad word because it has a q on the end of it and the
18:02rest is just an octal file permissions notation where this first number here notice how the first two
18:08are zeros but this first actual number here is something that applies to the user and then the
18:13next one applies to the group the next one applies to others who are not part of the user or group
18:17um and this video is not about file permissions i'll probably make one later if i think uh people
18:24will actually watch it but um yeah you can just kind of like look it up here if you want to and just
18:29copy paste that into a define somewhere let's see what else um i just want to show you a little code
18:36snip just to illustrate a little bit more what i was talking about before um people usually make this
18:43mistake they'll do let's see this is what people will usually do like new programmers suppose this
18:50is a function pretend we're in c or c plus plus for the time being you call some sort of a system
18:55function or an api of some other library and you just you just hope that it works you just call it
19:00and then your program continues underneath right it's a bad idea because maybe that failed maybe there's
19:05some action you need to take so instead it's a really good idea to check the return result
19:10or i should say an even better idea is check the documentation usually these types of you know
19:17c system calls or function calls or api calls or whatever they'll return something indicating success
19:23or failure sometimes the documentation will tell you oh you've got to put like a pointer to some
19:27other data structure in the arguments and then the result comes there of course just read the
19:32documentation but usually it's just it returns some sort of a result so you want to grab the results
19:39and then check the result in this case i'm assuming this is sort of a standard function that will
19:45return zero or greater on success like the system write call or the system read call and return less
19:52than zero if it fails so here i'm just responding you know if it's like if the result was greater than
19:58or zero then it's like yay we can proceed maybe i'll continue with my program in some way and if it's not
20:03then i'll print a complaint to the user and then i'll take some sort of an action like i'll write
20:08to a log file send an email somewhere you know do something to try and recover from the error and
20:14alert admins to the error and in this case i'm just throwing an exception this video is not about
20:19exceptions but so i just want you to know this is a good design pattern to not just discard the result
20:25of a call to something you should check the result and see what you're supposed to do
20:28um let's see what else did i want to tip system services file handles um yeah okay i think that's
20:37everything that i wanted to tell you in future videos i'm going to talk about uh you know some
20:44of this stuff much more in depth uh like with my file io video that i'm going to publish pretty soon
20:50which is uh you know it lets you read and write uh to files and it uses a loop to read a little bit
20:56at a time okay thank you so much for watching this video i hope you learned a little bit of stuff and
21:01had a little bit of fun i'm outie or something goodbye hey everybody thanks for watching this video
21:12again from the bottom of my heart i really appreciate it i do hope you did learn something and have some
21:16fun uh if you could do me a please a small little favor could you please subscribe and follow this
21:23channel or these videos or whatever it is you do on the current social media website that you're looking
21:28at right now it would really mean the world to me and it'll help make more videos and grow this
21:33community so we'll be able to do more videos longer videos better videos or just i'll be able to keep
21:38making videos in general so please do do me a kindness and uh and subscribe you know sometimes
21:45i'm sleeping in the middle of the night and i just wake up because i know somebody subscribed or
21:49followed it just wakes me up and i get filled with joy that's exactly what happens every single time
21:54so you could do it as a nice favor to me or you could you control me if you want to just wake me up
21:57in the middle of the night just subscribe and then i'll i'll just wake up i promise that's what will
22:02happen also uh if you look at the middle of the screen right now you should see a qr code which you can scan
22:08in order to go to the website which i think is also named somewhere at the bottom of this video
22:13and it'll take you to my main website where you can just kind of like see all the videos i published
22:17and the services and tutorials and things that i offer and all that good stuff and uh
22:24if you have a suggestion for uh uh clarifications or errata or just future videos that you want to see
22:31please leave a comment or if you just want to say hey what's up what's going on you know just send me a
22:35comment whatever i also wake up for those in the middle of the night i get i wake up in a cold sweat and i'm
22:40like it would really it really mean the world to me i would really appreciate it so again thank you
22:46so much for watching this video and um enjoy the cool music as as i fade into the darkness
22:54which is coming for us all
23:01so
23:03so
23:05so
23:07so
23:09so
23:11so
23:25so
23:27so
23:42so
23:44so
23:48so
24:04so
24:06so
24:08so
24:29so
24:31so
24:52so
24:54so
25:16so
25:18and
25:41so
25:43so
25:51you
26:05so
Be the first to comment
Add your comment

Recommended