you can become a GIGACHAD assembly programmer in 10 minutes (try it RIGHT NOW)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
Assembly Language is the language that computers speak it's not C it's not python it's not JavaScript and before you say well actually Triple L computers speak machine code we all know that assembly is the human readable version of machine code and while assembly may seem like an alien language it's actually really easy to learn once you know how to do it you can show off to your friends on how big your brain is and how cool your programming knowledge is today we'll be writing baby's first 64-bit assembly program it'll only take us 10 minutes and please follow along because you can do it too now before we actually get started coding in assembly we have to talk about a few things that are important to know about the CPU so if this little square here is your CPU it will say CPU inside of the CPU are a bunch of variables that are called registers they're just super fast memory that your computer uses to do operations with the registers are the width of the instruction set so today we're doing a 64-bit assembly so every register is 64 bits wide they have different names like RDI and RSI and maybe R8 for example all of these are 64-bit registers and this are here I think of it meaning really big meaning that it's a 64-bit register and also before you ask yes I did lose my voice this weekend and yes I did shave my beard off this weekend it's been a weekend full of events I think you for asking so with these registers we can do a series of operations on them one of them being move for example so we'll say move RDI the number eight that means move the number eight the constant value into RDI we can also move registers between each other so we can say move into RDI the value in RSI so these are both movements in and out of registers those are pretty simple and then one of the more important ones we can do are called memory operations we can move into RDI the value at we call it the quad word pointer of RSI for example what that means is it says treat the value in RSI as a pointer and then remove the quad word length that's 8 bytes or 64 bits remove eight bytes from memory and put it into RDI and then we can do the opposite we can do the same thing in Reverse we can say move to the quad word pointer in RSI the value in RDI so the first one was a load we took values from memory and put them into registers now we can take values that are in registers and put them into memory so this is a store operation so with the basics out of the way and again if you're a little lost it's okay we're going to write some code now to kind of clear it all up let's get into our virtual machine and write some code okay so to get started we have to install the assembler and the Linker that will allow us to compile our assembly code into machine code that runs on the computer so do that we have to run a sudo app install GCC by running this we'll actually get the assembler as and the loader LD I already have it so nothing happened here but just to test you have it if you type as assembler that should hang meaning it's looking for an input of the file to assemble so we're good there and also LD it should say no input files if you have both of those that run you should be able to follow along as well so now let's actually write some code that we're going to assemble in our program so in my folder assembly tutorial what we're going to do is we're going to open up our file we're going to say Vim assem.s so you can name this file whatever you want and here we are we're in a file that we're going to actually write our machine code now before we write our machine code we have to start the file off with some boilerplate code that tells the assembler what to do so the first one is global start that exposes a symbol called start to the Linker so it knows where our code starts and also we're going to say Intel syntax no prefix that makes our code a little easier to read and write for an average human being so then we can say underscore start and then a colon and that is where our code is going to start so if we write this and we get out of here we're now going to try to assemble our code using the assemblers we'll say as on our code.s we're going to say assem.o and there we go so now we have a file called a sem.o now we need to invoke the Linker to actually convert that into an executable elf because right now this is an intermediate object we can't actually run this we have to use the Linker to make it a full Elf that runs we can use the Linker itself or we can use GCC we're going to use GCC and say GCC Tac o assem do that on a sem.o we're also going to say no standard lib and tax static that makes it so that the binary doesn't get any additional stuff in there from lib C and makes it easier to run so if we run this file we are going to get a crash and there's a reason for that we haven't told the binary how to properly exit so we can go back into our file here and we can do some operations that we talked about in our previous example when I was talking about the registers right we can say things like move RDI the number eight and we can say move into RSI the value of RDI so at the end of this code both RDI and RSI the registers will have the value 8 in them so that's pretty cool let's go back and rerun our Command so again we use as to assemble it we use GCC to compile it and then we can use that and run the programs and you're going to see we get a crash again so those operations actually did happen under the hood when it got to the end of our code it didn't know how to exit our program you may be asking you know we have these operations to move values in and out of registers in and out of memory but how do we make the computer do something how do we make it print data or properly exit our process or do something that matters to us as human beings to do that we have to invoke what is called a syscall and the actual instruction in assembly to do that is called a syscall now it's a little more complicated than just typing the word syscall we have to set up the registers in a particular way that the kernel beneath us understands what to do what is the question that we're asking the kernel to do for us so what I'm looking at here is what is called a syscall table this is a list of all the operations the kernel is able to do for us when we invoke a syscall and as you can see here we have things like sys read sys right we have Assist exit in here somewhere let's see if we can find it I'm pretty sure it's like syscall 62 or something 16 there we go so you'll see that we have all these things we can ask the kernel to do for us the way we ask the kernel to do them is before we invoke the syscall we have to set up the registers in a way that the kernel recognizes our Command so here we have register Rax that means for example to do the sysread operation we put the zero into the Rax register we also put the file description number we want to read the buffer we want to read into Etc so let's test this out with the exit syscall again it's going to be just called number 60 here so we put into Rax the number 60 and then into RDI we put the error code we want our process to return with so let's let's try this out so we said we wanted to put the number 60 into Rax and then into RDI we wanted the error code and I'll show you how to check that at the end of this run we're gonna try the number 69 real quick so let's do that and we're going to re-assemble our program we're going to run GCC on it and then if we run our code our program didn't crash and the reason for that is we told it how to exit we gave it the instruction from the kernel to exit and now we're going to check the exit code to see if it's actually doing what we asked it to do or something else so we'll do Echo oh dollar sign question mark and boom the return code is 69 which means that this is the code that we ran so let's level this up let's add another layer to this let's make the program print the string hello world the way we do the Hello World operation is we're going to invoke a syscall called sys right we're going to put 1 into RX to do that we're going to write out to the standard out file descriptor which is file descriptor number one in out an error zero one two and we're going to write out a buffer that contains our hello world string and also we're going to be put into RDX the number of bytes to write so let's add that into our program real quick so we're going to leave this in here because this is actually just us exiting so we'll add a little comment here we'll say this is the sys exit call and then we're going to do the sys right call to do the sysrite syscall we have to do a few things again first we have to tell Rax what is this call we're doing so we move 1 into Rax we need to put the file descriptor into rdis remember standard out is file descript number one so we'll also put one here and we're going to move into our SI the address of the buffer that contains our string so this is actually going to be a new operation called load effective address we're going to load the effective address into RSI of our hello world buffer and we're going to Define that here in a second and then finally we move into our DX the length of our buffer which is just going to be 13. we can hardly code that into our code right there and then we'll invoke the syscall and then we'll go from there now we can do it on here is we can create a new symbol called hello world with a colon we're going to say it's an ASCII string zero delimited that'll be hello world exclamation point new line and then a zero will come at the end of that like C is supposed to have so we can do that we can save it we can quit and now we'll do the same thing as before we can type as to assemble our program oh it didn't like my comments hold on I think it's a double backslash here yeah let's try that it's either double backslash or a pound sign okay double backslash is good GCC and then we're gonna run our program hello world uh we missed a new line so it's actually gonna be 14 characters let's fix that real quick instead of 13 it's 14. we'll do as we'll do GCC and we'll run it again hello world all done at assembly assembly isn't that hard I hope you learned something today if you did do me a favor hit that like button hit subscribe and we'll see you in the next low level tutorial talk to you then
Info
Channel: Low Level Learning
Views: 565,931
Rating: undefined out of 5
Keywords: assembly programming tutorial, 64-bit hello world, x64 hello world, x86_64 hello world, x86 hello world, call stack, c programming, c pointers, c pointers tutorial, pointers tutorial, programming for beginners, programming language, programming in c, programming hero, programming fundamentals, programming memes
Id: 6S5KRJv-7RU
Channel Id: undefined
Length: 9min 48sec (588 seconds)
Published: Mon May 08 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.