Mastering System Services in x86-64 Assembly Programming

Mastering System Services in x86-64 Assembly Programming

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

Hey everybody in this video we’re going to talk a little bit about system services

in an x86-64 machine while programming in assembly

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

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

that in mind okay so for starters let’s see here’s a wonderful book i love pushing this book i did

not write it the person who wrote it is a genius it’s called x86 64 assembly language programming

with the boot to it’s a free and open source book so anybody can go get it find his website

and here’s the version i’m using and honestly a really old version of this book is still really

good he just keeps you know making little improvements but it was it was good even a

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

a little bit more, what is the system service? Suppose for the sake of argument that I’ve

already written an assembly program, a pure assembly program, not a hybrid module program

or anything. If you want to learn assembly, see my other videos, but for now I’m just going to

assume you kind of already know how, so I’m just going to open up a pre-made program here.

I’m going to do nano so I can edit the assembly file just so you can see what’s inside of here.

just so you can see what’s inside of here.

Okay, so I hope the red is 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 Genie.

Should have done that to begin with, okay.

Yeah, okay, I wrote it in Genie

and I guess I don’t have the tabs set up in Nano very well.

Okay, so here’s like my assembly program.

You know, I just have a little string here

string length and CRLF for like a line feed and some file descriptors and stuff

that I talk about in other videos here’s the entry point of the program just like

a little start entry point because it’s pure assembly and all I’m gonna do is

I’m gonna print a message right I’m just gonna print a little hello message and

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

if i run it right now you’ll just see what happens if i say let’s see clear and make run

then you can see there’s some stuff happening up here in the make file which i forgot to update

the title ignore that basically the real action is this line right here hello i’m printing using

a system service if you already know assembly then you have probably already seen this before

and it seems a little boring that’s good but i just want you to know that we’re using a system

system service to actually do the printing.

Kind of take it for granted here.

This little instruction called syscall.

What is a syscall?

Well, it’s just basically you set up some variables, some,

some codes, some data, whatever in, in some special registers.

One of the registers lets the system know what you want it to do.

One of the registers is like, or I guess all the following registers

if they’re needed, are just input to the system call that you’re trying to do.

do so in this case rax is letting the system know i want you to do this this code i want you to do

this certain thing i have a defined set up here called system right but if you look up in my

little defines area it’s just a number one so that means if i set the number one in rax i’m letting

the system know that i want it to write to some pipe to some file then you know depending on what

code you’re using rdi rsi rdx and other things may be needed or maybe not be needed at all

in this case when i’m writing somewhere it wants three arguments so i have to use rdi rsi and rdx

because those are the three arguments the first argument that it wants is where to print

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

pipes standard input standard output and standard error standard out is a pipe

number one let’s see where’s that yeah it’s just assigned a number one so

every program has its own number one pipe which is just standard output and I

guess I should say that it gets piped to file descriptor one it’s kind of like an

ambiguous term sort of but the system right call will write data to a file

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

write to this standard output for the one particular process that we’re inside of but

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

realize oh you know i i associate that file handle i gave you with this certain open file that you

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

knows where to write. So in this case,

it’s treating the standard output just like a file,

just a file with a special handle of just one.

I’ll talk about that more in another video.

Then the next two arguments it wants is just the,

a pointer to the first character of the message that you want to print.

And then just an integer representing the length of the message that you want

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

my hello symbol points to an array of bytes, just characters.

bytes just characters there is no null terminator on there it’s just bytes only

and so when I give it you know message hello by itself what I’m really giving

it is a pointer to that H letter just a pointer to the memory look you know I’m

pointing to the memory location of the very first character in that string and

then for the length I’m just using a special token little shortcut thing

where you just put a dollar sign minus and then the name of another string and

and then the name of another string,

and then the assembler will compute the length of that string.

So this right here is the same thing as me

just typing the exact length of the string.

I don’t even know what it is.

I don’t feel like counting it.

You can at home if you want.

One, two, three, four, I’m not gonna do it.

Anyway, so we basically just say,

you know, I would like to write somewhere.

I would like to write to the standard output file,

file handle, I would like to, you know, to that pipe.

I would like to write, you know, this string,

you know this string and then i would like to tell you that this is how long that string is

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

do all of the hard work for you it’ll go figure out how to actually write data to the file and

you know figure out how to do everything that it’s supposed to do and all you have to do is

a little system call here right after this you can see that i have a call crlf that’s just a function

call i talked about functions in other videos but basically you know i’m just calling a function here

you know i’m just calling a function here and it does the same thing it makes a system call but

instead of printing the hello string it’s printing my crlf string and if you just kind of look at what

my crlf is it’s just these two characters 13 and 10 so like you know slash r slash n for carriage

return line feed that’s why i call it crlf so all this program does just print a message and then a

new line and then at the very end it does another system call to properly exit the program if this

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

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

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

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

here, system exit is just the code 60. So if I send a code of one into RAX, that means I want to write

a code of 60 somewhere that means i want to exit the program the only argument that takes is rdi

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

if you recall from some of my other videos let’s see if we do echo hello echo always succeeds at

least as far as i know there’s like it’s like really hard to make it fail if i echo hello then

the program echo launches and it will succeed by just printing the word hello like see how it just

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

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

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

if i concatenate um let’s say a file that exists i should also get you know an error code that is a

So it’s a zero at the very end.

But if I try to concatenate a file that doesn’t actually exist,

I’ll put a OS release two because that doesn’t exist.

Notice how the cat command fails because it couldn’t find the file and the exit

code is a one.

So all you’re doing when setting RDI here is just controlling what exit code you

want your program to exit with.

This isn’t very convenient for a human running a program.

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

really convenient for programs that want to automate other programs.

programs just keep in mind if you have a program that is executing another

program and it wants to see if that program succeeded the exit code is one

of the easiest ways to find out if the program succeeded so this is the whole

idea of this entire program we’re just using two system calls to print

something and then to exit the program hopefully this makes sense and now I

want to go show you this wonderful wonderful book if I didn’t already say

If I didn’t already say this before, sorry if I already did.

This book is written by a genius.

It’s called this.

Here’s the guy who wrote the book.

And here’s the version that I’m using, but old versions are good.

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

So I’m just going to go to this assembly book, which deals with a lot of Yasm assembly tips

and tricks and tutorials and explanations and things.

And I’m going to go to the section labeled after system services.

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

this is.

You know, this is basically what I said.

It gives you, you know, a chance for your application to ask the operating system to

do something for you that you don’t want to program from scratch in assembly or that you

can’t program.

But here’s the real juicy details here.

If we go to, yeah, yeah.

Okay.

C, which is section 23.0 system services.

There’s a whole bunch of system services in here that you can, uh, that you can read about.

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

If we go down a little bit, you can see right here, the system write service that we’ve

been using in the sample assembly program.

You know, when we’re just printing, we say code one is to write something.

And so this is what the table says.

you place inside of RAX before syscall.

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

So like RDI is the file descriptor where do you want to write to.

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

opening a file or creating a file.

Or you could give it 0 or 1 or 2 if you want to try to write to one of the pipes although

actually file descriptor 0 is standard input I don’t think that would actually work.

zero to reading if you wanted to read the user’s input or or you know any input that that program

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

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

more arguments and that’s why i only gave it three arguments besides rex notice how it says here if

unsuccessful it returns a negative value if successful it returns the count of characters

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

um a lot of new programmers they don’t really look at return codes when they call system services

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

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

and maybe some of the characters did write but the system decided to only write i don’t know half for

reason maybe it ran out of buffer maybe you know it interrupted you know your right or something

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

zero indicating that some characters were written but it’ll tell you exactly how many characters

were written so you’ll know that you got to keep calling the sys call until all the characters were

successfully written why would you do this i don’t know maybe you have a gigantic string like maybe

like a several gigabyte file and you wanted to copy it to another file so you’ll be calling

system write over and over again and system write it’s only going to write so many bytes at the same

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

or you know whatever so that you can well write the entire complete file or the entire complete

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

And then of course, if it returns a negative value, then it totally failed.

Like you tried to write to a bad file handle, like standard input or a file handle that

was closed or a file handle that was open for reading only, something like that.

And then you can use some if else, you know, branching logic.

I mean, not if else in assembly, it’s just going to be comparison and conditional jumping

or branching.

But you can have more control over your program, right?

Because you want your program to be able to respond to errors.

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

and let you know that in another video I’m gonna do a full tutorial for how to

copy one file to another by opening one file for reading and then like using

system calls to read from it and then open another file for writing and then

you know use system calls to write to it and I’m gonna do that looping buffer

stuff that I talked about before anyway so but for now I’m just gonna go through

the rest of these system call codes let’s see so first off we got open well

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

address here, you know, RDI, you say, here’s the address of a null terminated file name.

So that means somewhere in memory, you have to have the file name that you want to open

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

relative path to wherever the program is currently running.

And then RSI is going to be file status flags.

And what does that mean?

And what does that mean? We can actually just search for that.

I’m going to say control C to copy,

and then I’m going to do control F to search for that.

And let’s see what page of mine right now, 340.

If I go down to the search results for that,

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

that means read only you’re opening the file and read only mode value one.

That means a write only a value to allows reading and writing to the file.

So if you don’t know what that means, well, there it was.

What was I just on before?

40?

Oh, gosh.

Completely lost my…

Oh, yeah, 40.

Okay, so opening pretty easy, right?

Like if you didn’t know this before, you didn’t necessarily read me.

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

I want to open.

So here’s the code.

Here’s the address that it needs.

When you’re done opening a file, when you’re done working with a file,

you want to close that file unless the whole program is going to terminate.

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

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

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

don’t want to have a file handle just open forever that’s a waste of memory and it might introduce

bad behavior to your program if you just have a bunch of file handles floating around that you

forgot about so it’s proper to close a file when you’re finished with it that’s just going to be

and it only wants one argument.

It just wants the file descriptor of the file that you wanted to close.

So if you open a file for a reading or to create it,

you get a file descriptor back.

You should probably hang on to it somewhere on the stack

or like in a global variable or like, you know, in a register or whatever.

Then when you’re done doing something to that file

and you’re sure that it’s time to close it,

just give that handle right back when you call code three to close it.

Then you can sort of like seek, you know,

forward and backwards to the file if you want to.

You can fork the current process.

You can fork the current process that’s kind of advanced for this video.

You can exit.

Remember before we exited from that program with code 60.

So that’s it. Call code 60.

If you didn’t watch this video, you could have gone through this table and just said,

Oh, you know, I want to terminate the executing process.

I do a system call with code 60 and I’ll give the exit status to RDI,

which is typically zero for success.

Bunch of other stuffs create, get the time of day,

and then the file modes that we talked about before.

we talked about before and then file permissions. Let’s see, where’s the permission. I think it’s

when you want to create a file. Yeah, right here. If we wanted to create a file, that’s code 85.

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

And then you’re thinking like, what are the file mode flags? Just go down here and then here you

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

I guess I can just copy paste this number right here and it’ll end up being, you know,

whatever is described on the left and the right.

Like the group has read, write and execute permissions and so forth.

I mean, really, if you look at this long enough, you’ll kind of realize that this is a quad

word because it has a Q on the end of it.

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

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

to the user and then the next one applies to the group the next one applies to others who are not

part of the user or group and this video is not about file permissions I’ll probably make one

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

you want to and just copy paste that into a define somewhere let’s see what else I just want to show

you a little code snip just to illustrate a little bit more what I was talking about before

for. People usually make this mistake, they’ll do see this is what people will usually do like new

programmers. Suppose this is a function pretend we’re in C or C++. For the time being, you call

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

works, you just call it and then your program continues underneath, right? It’s a bad idea,

because maybe that failed, maybe there’s some action you need to take. So instead, it’s a really

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

documentation usually these types of you know c system calls or function calls or api calls or

whatever they’ll return something indicating success or failure sometimes the documentation

will tell you oh you’ve got to put like a pointer to some other data structure in the arguments

and then the result comes there of course just read the documentation but usually it’s just

So you want to grab the result and then check the result.

In this case I’m assuming this is sort of a standard function that will return zero

or greater on success like the system write call or the system read call and return less

than zero if it fails.

So here I’m just responding, you know, if it’s like if the result was greater than zero

then it’s like, yay we can proceed.

Maybe I’ll continue with my program in some way and if it’s not then I’ll print a complaint

then I’ll print a complaint to the user and then I’ll take some sort of an action

like I’ll write to a log file, send an email somewhere, you know,

do something to try and recover from the error and alert admins to the error.

And in this case, I’m just throwing an exception.

This video is not about exceptions, but so I just want you to know,

this is a good design pattern to not just discard the result of a call to

something. You should check the result and see what you’re supposed to do.

Let’s see, what else did I want to tip?

system services, file handles.

Yeah, okay, I think that’s everything that I wanted to tell you.

In future videos, I’m going to talk about, you know, some of this stuff much more in-depth,

like with my file.io video that I’m going to publish pretty soon, which is, you know,

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

Okay, thank you so much for watching this video.

I hope you learned a little bit of stuff and had a little bit of fun.

I’m outie.

Or something.

Goodbye.

you’re looking at right now it would really mean the world to me and it’ll

help make more videos and grow this community so we’ll be able to do more

videos longer videos better videos or just I’ll be able to keep making videos

in general so please do do me a kindness and and subscribe you know sometimes

I’m sleeping in the middle of the night and I just wake up because I know

somebody subscribed or followed it just wakes me up and I get filled with joy

that’s exactly what happens every single time so you could do it as a nice favor

could you control me if you want to just wake me up in the middle of the night

just subscribe and then I’ll just wake up I promise that’s what will happen

also if you look at the middle of the screen right now you should see a QR

code which you can scan in order to go to the website which I think is also

named somewhere at the bottom of this video and it’ll take you to my main

website where you can just kind of like see all the videos I published and the

services and tutorials and things that I offer and all that good stuff and if

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

see please leave a comment or if you just want to say hey what’s up what’s going on you know just

send me a comment 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 like it would really it really mean the world to me i would really

appreciate it so again thank you so much for watching this video and um enjoy the cool music

as I fade into the darkness which is coming for us all.

Thank you.

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply