Welcome to this exciting tutorial on x86 64 assembly programming.
In this lecture we will walk you through the process of creating a classic Hello World application using
Nasm Assembly and GCC on a Linux system and assembly language might seem intimidating, but don't worry,
we will break it down step by step, making it easy to understand.
And before we dive into the code, let's talk about the tools we will be using.
Nasm Short for Netwide Assembler is a powerful assembler for the x86 architecture and it allows us to
write low level code using human readable mnemonics which will be translated into machine code.
On the other hand, GCC and compiler collection will help us link our assembly code into an executable
program.
In this case, we will use the CSM, which is an IDE for assembler, but you can also use the regular
text editor for this project.
And here we will open this project here, right click on it, or actually we can also create a new file
with this.
And see here we are on the assembly directory, which we will create our project in this directory here.
So we will use the.
Make file.
Oops.
Sorry.
Clear Pouch.
Um, here.
And after that, we will create our.
And as you can see here, we will.
We created our hello ASM here.
Now we will go to open project and click on Hello ASM.
And as you can see here, our file is empty for now.
But we will fill this and write our codes in this file right now.
So firstly, we will need the section for data here.
Data section here.
Data.
And after that we will.
Hello.
And I will explain all of these codes.
Now, after writing this code, Hello world.
And we will also add zero, which you will learn why we added right at this zero right here.
This is a null terminated terminated string.
And after that we will also create a text section.
So section text.
And here we will global.
Part.
And we will now write our start here.
And here we will write the add our commands.
Hello, world.
To start out.
Which this is in the file descriptor one.
The Scripture.
And after that we will move.
Use the tab.
MOV rax one.
Okay to use spaces between this comma and your.
Here because it makes it easier to read.
And before comment, you can add two tabs.
Call.
I'll number.
Number for Seasprite.
And or the I also one.
And this is for the file descriptor.
One, which is STD out.
Here and we will also remove the error.
You.
Hello.
And here, as you can see here, we created our hello data in the data section so we can use this variable.
And we will point the we will pointer to the string.
To the string to.
Right.
And after that.
Move are the.
Richard Burton here.
This is the verdict, this 13 here, because this is the length of the string.
Now we will add the length.
Let's count this again.
One, two, three, four, five, six, seven, eight, nine, ten.
11, 12, 13.
And here, if you want to count the string, you need to start from one and not zero like in an array.
Right?
You will need to start counting from one.
So the first alphabet is one count as one, and after that we will go to.
Uh, Siskel, We will invoke the system.
Call here.
Invoke the system call.
And after that, we will need to exit the program.
So exit the program.
So move rax 60.
This is for the.
This is for the Siskel number forces exit.
I'll number four's exit and X or D, A or D.
So this is for the returning code zero return code zero.
And lastly, we will also create the syscall.
This skull, which is this for?
Again, invoke the system call.
And here.
That's it.
Now, let's explain this code.
And.
This code starts by defining two sections data and text.
Uh, so this data section, this section is used to declare the data segments of the program.
Here we define a variable named hello and assign the string Hello world to it.
So the DB directive is used to define data bytes and we terminate the string with a null character to
embark.
This is the end of the string.
And we also have the text section here.
This section contains the actual code of the program.
We mark the entry point of our program using the Global Start Declaration, and the Start label serves
as the entry point of our application.
And in this start here, as you can see here.
Um, we in the this this is the text section.
The start we proceed to write the code to print hello world to the screen and exit to exit from the
program here.
And the.
So here we will see.
Here we will we added some comments here.
But now let's start with our first line in the start in the stack section.
So this line loads the syscall number for this, right?
This is the system.
System call to overwrite data into the Rax register.
So this number one represents the size right here.
Right.
So move the i comma one so we load the file descriptor for standard output STD out into the d register.
So file descriptor one corresponds to stdout which is the screen and MOV rsi.
Hello.
The RSI register is loaded with the memory address of the hello variable which points to our hello world
string.
Right.
And after that we have mov rdx 13.
So we select the RDX register to the length of the string which is the 13 characters, including this
new Terminator and the syscall.
This instruction invokes the system call with the values loaded into the registers, effectively printing
hello world to the string and the next we add code to exit the program gracefully.
So here we have mov rax 60.
As I explained this, this line loads the syscall number for this exit.
This is the system call to terminate the program into rax register and we have xor r the i r d i this.
We set the d register to zero, indicating the return code of zero in this case success for the program.
And lastly, we have the syscall.
The fiscal interaction invokes the system exit system call with a return code.
So this concise hello world program in x86 64 assembly and the code uses Linux system calls to print
the strings and exit the program.
And now let's proceed with the detailed explanation of the code and the process of assembling and executing
it using Nasm.
And.
So here now, we will need to compile this code here.
And in order to do that, we will open our terminal again.
Let's clear the terminal.
So we are in the disassembly directory where our our Hello dot ASM file resides.
So we will pat the Hello dot ASM.
As you can see here, this is our assembler program.
Now we will compile it.
So we will open the terminal and navigate to the directory.
As you can see here, we are already navigated.
Then you will use this.
Nasm f elf 64 elf 64.
Hello dot ASM.
Output file here.
Hello, All here.
And as you can see here, we have this.
Hello, newly created Hello all file here.
And after that we will also create the GCC to linking.
We will use the GCC for linking purposes.
So no start files and all here.
Hello.
Hello, Dot all.
And as you can see, we got some warning, but, uh, the program will work here.
This is for the.
Mining purposes.
And as you can see, we have created our hello here.
Now we will execute this.
And as you can see here, our hello world is Hello World Assembly program that we've wrote in this lecture
is works.
Here.
And with this method here we compile this directly assembles hello dot code using nasm and then links
it into an executable using GCC without using the makefile.
And if this still produces any errors, please ensure that you are using the system with nasm and GCC
properly installed.
And I'm waiting you in next lecture.