WEBVTT

00:00.290 --> 00:04.910
Welcome to this exciting tutorial on x86 64 assembly programming.

00:04.940 --> 00:11.690
In this lecture we will walk you through the process of creating a classic Hello World application using

00:11.720 --> 00:20.630
Nasm Assembly and GCC on a Linux system and assembly language might seem intimidating, but don't worry,

00:20.630 --> 00:25.360
we will break it down step by step, making it easy to understand.

00:25.370 --> 00:30.120
And before we dive into the code, let's talk about the tools we will be using.

00:30.140 --> 00:38.930
Nasm Short for Netwide Assembler is a powerful assembler for the x86 architecture and it allows us to

00:38.930 --> 00:45.740
write low level code using human readable mnemonics which will be translated into machine code.

00:45.770 --> 00:53.870
On the other hand, GCC and compiler collection will help us link our assembly code into an executable

00:53.870 --> 00:54.590
program.

00:54.590 --> 01:03.000
In this case, we will use the CSM, which is an IDE for assembler, but you can also use the regular

01:03.000 --> 01:04.920
text editor for this project.

01:04.920 --> 01:10.710
And here we will open this project here, right click on it, or actually we can also create a new file

01:10.710 --> 01:11.610
with this.

01:11.850 --> 01:17.790
And see here we are on the assembly directory, which we will create our project in this directory here.

01:17.790 --> 01:19.500
So we will use the.

01:20.660 --> 01:22.010
Make file.

01:22.580 --> 01:22.940
Oops.

01:23.060 --> 01:23.470
Sorry.

01:23.480 --> 01:25.220
Clear Pouch.

01:26.690 --> 01:27.620
Um, here.

01:27.740 --> 01:29.750
And after that, we will create our.

01:31.340 --> 01:33.160
And as you can see here, we will.

01:33.170 --> 01:35.480
We created our hello ASM here.

01:35.510 --> 01:39.370
Now we will go to open project and click on Hello ASM.

01:39.380 --> 01:42.800
And as you can see here, our file is empty for now.

01:42.800 --> 01:47.510
But we will fill this and write our codes in this file right now.

01:47.510 --> 01:51.680
So firstly, we will need the section for data here.

01:52.870 --> 01:54.100
Data section here.

01:54.910 --> 01:55.570
Data.

01:55.570 --> 01:57.000
And after that we will.

01:57.010 --> 01:57.550
Hello.

01:57.550 --> 01:59.560
And I will explain all of these codes.

01:59.590 --> 02:04.360
Now, after writing this code, Hello world.

02:06.810 --> 02:13.440
And we will also add zero, which you will learn why we added right at this zero right here.

02:13.590 --> 02:18.510
This is a null terminated terminated string.

02:18.750 --> 02:23.010
And after that we will also create a text section.

02:23.440 --> 02:26.580
So section text.

02:27.090 --> 02:29.730
And here we will global.

02:30.650 --> 02:31.150
Part.

02:31.760 --> 02:35.270
And we will now write our start here.

02:38.800 --> 02:43.900
And here we will write the add our commands.

02:48.050 --> 02:49.090
Hello, world.

02:49.520 --> 02:52.580
To start out.

02:53.190 --> 02:58.710
Which this is in the file descriptor one.

02:59.670 --> 03:00.450
The Scripture.

03:00.480 --> 03:01.380
The Scripture.

03:01.500 --> 03:04.830
And after that we will move.

03:05.760 --> 03:06.910
Use the tab.

03:06.930 --> 03:09.090
MOV rax one.

03:10.580 --> 03:13.970
Okay to use spaces between this comma and your.

03:15.310 --> 03:18.730
Here because it makes it easier to read.

03:18.730 --> 03:22.930
And before comment, you can add two tabs.

03:24.520 --> 03:25.030
Call.

03:26.050 --> 03:27.660
I'll number.

03:28.880 --> 03:30.950
Number for Seasprite.

03:31.710 --> 03:35.250
And or the I also one.

03:37.950 --> 03:41.760
And this is for the file descriptor.

03:45.990 --> 03:48.570
One, which is STD out.

03:51.900 --> 03:54.810
Here and we will also remove the error.

03:56.850 --> 03:57.090
You.

03:57.210 --> 03:57.720
Hello.

03:57.720 --> 04:03.930
And here, as you can see here, we created our hello data in the data section so we can use this variable.

04:05.140 --> 04:08.980
And we will point the we will pointer to the string.

04:09.010 --> 04:11.110
To the string to.

04:11.120 --> 04:12.040
Right.

04:12.220 --> 04:13.930
And after that.

04:15.070 --> 04:16.840
Move are the.

04:18.350 --> 04:20.660
Richard Burton here.

04:20.750 --> 04:25.730
This is the verdict, this 13 here, because this is the length of the string.

04:26.000 --> 04:28.640
Now we will add the length.

04:34.120 --> 04:35.710
Let's count this again.

04:35.800 --> 04:40.720
One, two, three, four, five, six, seven, eight, nine, ten.

04:41.710 --> 04:43.920
11, 12, 13.

04:43.930 --> 04:52.360
And here, if you want to count the string, you need to start from one and not zero like in an array.

04:52.360 --> 04:52.790
Right?

04:52.810 --> 04:56.560
You will need to start counting from one.

04:56.560 --> 05:03.790
So the first alphabet is one count as one, and after that we will go to.

05:05.730 --> 05:10.020
Uh, Siskel, We will invoke the system.

05:10.020 --> 05:11.040
Call here.

05:11.580 --> 05:14.790
Invoke the system call.

05:14.790 --> 05:17.880
And after that, we will need to exit the program.

05:18.000 --> 05:21.840
So exit the program.

05:21.900 --> 05:25.540
So move rax 60.

05:25.830 --> 05:27.180
This is for the.

05:28.240 --> 05:31.600
This is for the Siskel number forces exit.

05:33.270 --> 05:41.260
I'll number four's exit and X or D, A or D.

05:41.880 --> 05:48.510
So this is for the returning code zero return code zero.

05:48.510 --> 05:51.450
And lastly, we will also create the syscall.

05:52.450 --> 05:54.700
This skull, which is this for?

05:54.700 --> 05:58.480
Again, invoke the system call.

06:00.010 --> 06:00.790
And here.

06:00.790 --> 06:01.510
That's it.

06:01.540 --> 06:03.820
Now, let's explain this code.

06:04.180 --> 06:04.960
And.

06:06.070 --> 06:12.610
This code starts by defining two sections data and text.

06:13.420 --> 06:19.960
Uh, so this data section, this section is used to declare the data segments of the program.

06:20.080 --> 06:28.320
Here we define a variable named hello and assign the string Hello world to it.

06:28.330 --> 06:36.400
So the DB directive is used to define data bytes and we terminate the string with a null character to

06:36.400 --> 06:37.010
embark.

06:37.030 --> 06:38.650
This is the end of the string.

06:38.650 --> 06:41.290
And we also have the text section here.

06:41.290 --> 06:44.390
This section contains the actual code of the program.

06:44.410 --> 06:51.430
We mark the entry point of our program using the Global Start Declaration, and the Start label serves

06:51.430 --> 06:54.520
as the entry point of our application.

06:54.520 --> 06:57.940
And in this start here, as you can see here.

06:59.100 --> 07:02.640
Um, we in the this this is the text section.

07:03.090 --> 07:12.660
The start we proceed to write the code to print hello world to the screen and exit to exit from the

07:12.660 --> 07:13.770
program here.

07:14.890 --> 07:16.160
And the.

07:16.360 --> 07:19.180
So here we will see.

07:19.180 --> 07:21.850
Here we will we added some comments here.

07:21.850 --> 07:28.930
But now let's start with our first line in the start in the stack section.

07:29.730 --> 07:34.740
So this line loads the syscall number for this, right?

07:34.770 --> 07:35.670
This is the system.

07:36.560 --> 07:40.550
System call to overwrite data into the Rax register.

07:40.550 --> 07:45.950
So this number one represents the size right here.

07:45.980 --> 07:46.610
Right.

07:46.610 --> 07:59.750
So move the i comma one so we load the file descriptor for standard output STD out into the d register.

07:59.750 --> 08:08.390
So file descriptor one corresponds to stdout which is the screen and MOV rsi.

08:08.420 --> 08:08.960
Hello.

08:08.990 --> 08:18.050
The RSI register is loaded with the memory address of the hello variable which points to our hello world

08:18.050 --> 08:18.860
string.

08:18.860 --> 08:19.550
Right.

08:19.550 --> 08:23.870
And after that we have mov rdx 13.

08:23.870 --> 08:30.680
So we select the RDX register to the length of the string which is the 13 characters, including this

08:30.680 --> 08:33.380
new Terminator and the syscall.

08:33.380 --> 08:41.070
This instruction invokes the system call with the values loaded into the registers, effectively printing

08:41.100 --> 08:48.180
hello world to the string and the next we add code to exit the program gracefully.

08:48.180 --> 08:50.850
So here we have mov rax 60.

08:51.030 --> 08:56.490
As I explained this, this line loads the syscall number for this exit.

08:56.580 --> 09:05.790
This is the system call to terminate the program into rax register and we have xor r the i r d i this.

09:05.820 --> 09:14.250
We set the d register to zero, indicating the return code of zero in this case success for the program.

09:14.250 --> 09:16.410
And lastly, we have the syscall.

09:16.440 --> 09:22.650
The fiscal interaction invokes the system exit system call with a return code.

09:22.680 --> 09:30.720
So this concise hello world program in x86 64 assembly and the code uses Linux system calls to print

09:30.720 --> 09:33.060
the strings and exit the program.

09:33.060 --> 09:39.780
And now let's proceed with the detailed explanation of the code and the process of assembling and executing

09:39.780 --> 09:41.430
it using Nasm.

09:42.200 --> 09:42.890
And.

09:44.330 --> 09:49.250
So here now, we will need to compile this code here.

09:50.800 --> 09:53.980
And in order to do that, we will open our terminal again.

09:53.980 --> 09:55.630
Let's clear the terminal.

09:55.630 --> 10:03.610
So we are in the disassembly directory where our our Hello dot ASM file resides.

10:03.610 --> 10:07.690
So we will pat the Hello dot ASM.

10:07.840 --> 10:10.180
As you can see here, this is our assembler program.

10:10.360 --> 10:12.430
Now we will compile it.

10:12.430 --> 10:15.520
So we will open the terminal and navigate to the directory.

10:15.520 --> 10:18.310
As you can see here, we are already navigated.

10:18.340 --> 10:20.500
Then you will use this.

10:20.530 --> 10:26.230
Nasm f elf 64 elf 64.

10:26.230 --> 10:27.630
Hello dot ASM.

10:28.880 --> 10:30.110
Output file here.

10:30.110 --> 10:31.250
Hello, All here.

10:31.250 --> 10:33.530
And as you can see here, we have this.

10:33.530 --> 10:36.230
Hello, newly created Hello all file here.

10:36.230 --> 10:40.100
And after that we will also create the GCC to linking.

10:40.130 --> 10:44.480
We will use the GCC for linking purposes.

10:44.630 --> 10:48.850
So no start files and all here.

10:48.860 --> 10:49.640
Hello.

10:50.430 --> 10:51.600
Hello, Dot all.

10:51.900 --> 10:56.970
And as you can see, we got some warning, but, uh, the program will work here.

10:57.150 --> 10:58.110
This is for the.

11:00.210 --> 11:01.080
Mining purposes.

11:01.080 --> 11:03.980
And as you can see, we have created our hello here.

11:03.990 --> 11:05.880
Now we will execute this.

11:06.030 --> 11:14.910
And as you can see here, our hello world is Hello World Assembly program that we've wrote in this lecture

11:14.910 --> 11:16.050
is works.

11:17.180 --> 11:17.630
Here.

11:17.630 --> 11:26.990
And with this method here we compile this directly assembles hello dot code using nasm and then links

11:26.990 --> 11:32.000
it into an executable using GCC without using the makefile.

11:32.000 --> 11:39.110
And if this still produces any errors, please ensure that you are using the system with nasm and GCC

11:39.860 --> 11:41.240
properly installed.

11:41.240 --> 11:43.670
And I'm waiting you in next lecture.
