1 00:00:00,290 --> 00:00:04,910 Welcome to this exciting tutorial on x86 64 assembly programming. 2 00:00:04,940 --> 00:00:11,690 In this lecture we will walk you through the process of creating a classic Hello World application using 3 00:00:11,720 --> 00:00:20,630 Nasm Assembly and GCC on a Linux system and assembly language might seem intimidating, but don't worry, 4 00:00:20,630 --> 00:00:25,360 we will break it down step by step, making it easy to understand. 5 00:00:25,370 --> 00:00:30,120 And before we dive into the code, let's talk about the tools we will be using. 6 00:00:30,140 --> 00:00:38,930 Nasm Short for Netwide Assembler is a powerful assembler for the x86 architecture and it allows us to 7 00:00:38,930 --> 00:00:45,740 write low level code using human readable mnemonics which will be translated into machine code. 8 00:00:45,770 --> 00:00:53,870 On the other hand, GCC and compiler collection will help us link our assembly code into an executable 9 00:00:53,870 --> 00:00:54,590 program. 10 00:00:54,590 --> 00:01:03,000 In this case, we will use the CSM, which is an IDE for assembler, but you can also use the regular 11 00:01:03,000 --> 00:01:04,920 text editor for this project. 12 00:01:04,920 --> 00:01:10,710 And here we will open this project here, right click on it, or actually we can also create a new file 13 00:01:10,710 --> 00:01:11,610 with this. 14 00:01:11,850 --> 00:01:17,790 And see here we are on the assembly directory, which we will create our project in this directory here. 15 00:01:17,790 --> 00:01:19,500 So we will use the. 16 00:01:20,660 --> 00:01:22,010 Make file. 17 00:01:22,580 --> 00:01:22,940 Oops. 18 00:01:23,060 --> 00:01:23,470 Sorry. 19 00:01:23,480 --> 00:01:25,220 Clear Pouch. 20 00:01:26,690 --> 00:01:27,620 Um, here. 21 00:01:27,740 --> 00:01:29,750 And after that, we will create our. 22 00:01:29,760 --> 00:01:30,170 Hello. 23 00:01:31,340 --> 00:01:33,160 And as you can see here, we will. 24 00:01:33,170 --> 00:01:35,480 We created our hello ASM here. 25 00:01:35,510 --> 00:01:39,370 Now we will go to open project and click on Hello ASM. 26 00:01:39,380 --> 00:01:42,800 And as you can see here, our file is empty for now. 27 00:01:42,800 --> 00:01:47,510 But we will fill this and write our codes in this file right now. 28 00:01:47,510 --> 00:01:51,680 So firstly, we will need the section for data here. 29 00:01:52,870 --> 00:01:54,100 Data section here. 30 00:01:54,910 --> 00:01:55,570 Data. 31 00:01:55,570 --> 00:01:57,000 And after that we will. 32 00:01:57,010 --> 00:01:57,550 Hello. 33 00:01:57,550 --> 00:01:59,560 And I will explain all of these codes. 34 00:01:59,590 --> 00:02:04,360 Now, after writing this code, Hello world. 35 00:02:06,810 --> 00:02:13,440 And we will also add zero, which you will learn why we added right at this zero right here. 36 00:02:13,590 --> 00:02:18,510 This is a null terminated terminated string. 37 00:02:18,750 --> 00:02:23,010 And after that we will also create a text section. 38 00:02:23,440 --> 00:02:26,580 So section text. 39 00:02:27,090 --> 00:02:29,730 And here we will global. 40 00:02:30,650 --> 00:02:31,150 Part. 41 00:02:31,760 --> 00:02:35,270 And we will now write our start here. 42 00:02:38,800 --> 00:02:43,900 And here we will write the Add commands. 43 00:02:48,050 --> 00:02:49,090 Hello, world. 44 00:02:49,520 --> 00:02:52,580 To start out. 45 00:02:53,190 --> 00:02:58,710 Which this is in the file descriptor one. 46 00:02:59,670 --> 00:03:00,450 The Scripture. 47 00:03:00,480 --> 00:03:01,380 The Scripture. 48 00:03:01,500 --> 00:03:04,830 And after that we will move. 49 00:03:05,760 --> 00:03:06,910 Use the tab. 50 00:03:06,930 --> 00:03:09,090 MOV rax one. 51 00:03:10,580 --> 00:03:13,970 Okay to use spaces between this comma and your. 52 00:03:15,310 --> 00:03:18,730 Here because it makes it easier to read. 53 00:03:18,730 --> 00:03:22,930 And before comment, you can add two tabs. 54 00:03:24,520 --> 00:03:25,030 Call. 55 00:03:26,050 --> 00:03:27,660 I'll number. 56 00:03:28,880 --> 00:03:30,950 Number for Seasprite. 57 00:03:31,710 --> 00:03:35,250 And or the I also one. 58 00:03:37,950 --> 00:03:41,760 And this is for the file descriptor. 59 00:03:45,990 --> 00:03:48,570 One, which is STD out. 60 00:03:51,900 --> 00:03:54,810 Here and we will also remove the error. 61 00:03:56,850 --> 00:03:57,090 You. 62 00:03:57,210 --> 00:03:57,720 Hello. 63 00:03:57,720 --> 00: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. 64 00:04:05,140 --> 00:04:08,980 And we will point the we will pointer to the string. 65 00:04:09,010 --> 00:04:11,110 To the string to. 66 00:04:11,120 --> 00:04:12,040 Right. 67 00:04:12,220 --> 00:04:13,930 And after that. 68 00:04:15,070 --> 00:04:16,840 Move are the. 69 00:04:18,350 --> 00:04:20,660 Richard Burton here. 70 00:04:20,750 --> 00:04:25,730 This is the verdict, this 13 here, because this is the length of the string. 71 00:04:26,000 --> 00:04:28,640 Now we will add the length. 72 00:04:34,120 --> 00:04:35,710 Let's count this again. 73 00:04:35,800 --> 00:04:40,720 One, two, three, four, five, six, seven, eight, nine, ten. 74 00:04:41,710 --> 00:04:43,920 11, 12, 13. 75 00:04:43,930 --> 00: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. 76 00:04:52,360 --> 00:04:52,790 Right? 77 00:04:52,810 --> 00:04:56,560 You will need to start counting from one. 78 00:04:56,560 --> 00:05:03,790 So the first alphabet is one count as one, and after that we will go to. 79 00:05:05,730 --> 00:05:10,020 Uh, Siskel, We will invoke the system. 80 00:05:10,020 --> 00:05:11,040 Call here. 81 00:05:11,580 --> 00:05:14,790 Invoke the system call. 82 00:05:14,790 --> 00:05:17,880 And after that, we will need to exit the program. 83 00:05:18,000 --> 00:05:21,840 So exit the program. 84 00:05:21,900 --> 00:05:25,540 So move rax 60. 85 00:05:25,830 --> 00:05:27,180 This is for the. 86 00:05:28,240 --> 00:05:31,600 This is for the Siskel number forces exit. 87 00:05:33,270 --> 00:05:41,260 I'll number four's exit and X or D, A or D. 88 00:05:41,880 --> 00:05:48,510 So this is for the returning code zero return code zero. 89 00:05:48,510 --> 00:05:51,450 And lastly, we will also create the syscall. 90 00:05:52,450 --> 00:05:54,700 This skull, which is this for? 91 00:05:54,700 --> 00:05:58,480 Again, invoke the system call. 92 00:06:00,010 --> 00:06:00,790 And here. 93 00:06:00,790 --> 00:06:01,510 That's it. 94 00:06:01,540 --> 00:06:03,820 Now, let's explain this code. 95 00:06:04,180 --> 00:06:04,960 And. 96 00:06:06,070 --> 00:06:12,610 This code starts by defining two sections data and text. 97 00:06:13,420 --> 00:06:19,960 Uh, so this data section, this section is used to declare the data segments of the program. 98 00:06:20,080 --> 00:06:28,320 Here we define a variable named hello and assign the string Hello world to it. 99 00:06:28,330 --> 00:06:36,400 So the DB directive is used to define data bytes and we terminate the string with a null character to 100 00:06:36,400 --> 00:06:37,010 embark. 101 00:06:37,030 --> 00:06:38,650 This is the end of the string. 102 00:06:38,650 --> 00:06:41,290 And we also have the text section here. 103 00:06:41,290 --> 00:06:44,390 This section contains the actual code of the program. 104 00:06:44,410 --> 00:06:51,430 We mark the entry point of our program using the Global Start Declaration, and the Start label serves 105 00:06:51,430 --> 00:06:54,520 as the entry point of our application. 106 00:06:54,520 --> 00:06:57,940 And in this start here, as you can see here. 107 00:06:59,100 --> 00:07:02,640 Um, we in the this this is the text section. 108 00:07:03,090 --> 00:07:12,660 The start we proceed to write the code to print hello world to the screen and exit to exit from the 109 00:07:12,660 --> 00:07:13,770 program here. 110 00:07:14,890 --> 00:07:16,160 And the. 111 00:07:16,360 --> 00:07:19,180 So here we will see. 112 00:07:19,180 --> 00:07:21,850 Here we will we added some comments here. 113 00:07:21,850 --> 00:07:28,930 But now let's start with our first line in the start in this tech section. 114 00:07:29,730 --> 00:07:34,740 So this line loads the syscall number for this, right? 115 00:07:34,770 --> 00:07:35,670 This is the system. 116 00:07:36,560 --> 00:07:40,550 System call to overwrite data into the Rax register. 117 00:07:40,550 --> 00:07:45,950 So this number one represents the size right here. 118 00:07:45,980 --> 00:07:46,610 Right. 119 00:07:46,610 --> 00:07:59,750 So move the i comma one so we load the file descriptor for standard output STD out into the d register. 120 00:07:59,750 --> 00:08:08,390 So file descriptor one corresponds to stdout which is the screen and MOV rsi. 121 00:08:08,420 --> 00:08:08,960 Hello. 122 00:08:08,990 --> 00:08:18,050 The RSI register is loaded with the memory address of the hello variable which points to our hello world 123 00:08:18,050 --> 00:08:18,860 string. 124 00:08:18,860 --> 00:08:19,550 Right. 125 00:08:19,550 --> 00:08:23,870 And after that we have mov rdx 13. 126 00:08:23,870 --> 00:08:30,680 So we select the RDX register to the length of the string which is the 13 characters, including this 127 00:08:30,680 --> 00:08:33,380 new Terminator and the syscall. 128 00:08:33,380 --> 00:08:41,070 This instruction invokes the system call with the values loaded into the registers, effectively printing 129 00:08:41,100 --> 00:08:48,180 hello world to the string and the next we add code to exit the program gracefully. 130 00:08:48,180 --> 00:08:50,850 So here we have mov rax 60. 131 00:08:51,030 --> 00:08:56,490 As I explained this, this line loads the syscall number for this exit. 132 00:08:56,580 --> 00: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. 133 00:09:05,820 --> 00:09:14,250 We set the d register to zero, indicating the return code of zero in this case success for the program. 134 00:09:14,250 --> 00:09:16,410 And lastly, we have the syscall. 135 00:09:16,440 --> 00:09:22,650 The fiscal interaction invokes the system exit system call with a return code. 136 00:09:22,680 --> 00:09:30,720 So this concise hello world program in x86 64 assembly and the code uses Linux system calls to print 137 00:09:30,720 --> 00:09:33,060 the strings and exit the program. 138 00:09:33,060 --> 00:09:39,780 And now let's proceed with the detailed explanation of the code and the process of assembling and executing 139 00:09:39,780 --> 00:09:41,430 it using Nasm. 140 00:09:42,200 --> 00:09:42,890 And. 141 00:09:44,330 --> 00:09:49,250 So here now, we will need to compile this code here. 142 00:09:50,800 --> 00:09:53,980 And in order to do that, we will open our terminal again. 143 00:09:53,980 --> 00:09:55,630 Let's clear the terminal. 144 00:09:55,630 --> 00:10:03,610 So we are in the disassembly directory where our our Hello dot ASM file resides. 145 00:10:03,610 --> 00:10:07,690 So we will pat the Hello dot ASM. 146 00:10:07,840 --> 00:10:10,180 As you can see here, this is our assembler program. 147 00:10:10,360 --> 00:10:12,430 Now we will compile it. 148 00:10:12,430 --> 00:10:15,520 So we will open the terminal and navigate to the directory. 149 00:10:15,520 --> 00:10:18,310 As you can see here, we are already navigated. 150 00:10:18,340 --> 00:10:20,500 Then you will use this. 151 00:10:20,530 --> 00:10:26,230 Nasm f elf 64 elf 64. 152 00:10:26,230 --> 00:10:27,640 Hello dot ASM. 153 00:10:28,880 --> 00:10:30,110 Output file here. 154 00:10:30,110 --> 00:10:31,250 Hello, All here. 155 00:10:31,250 --> 00:10:33,530 And as you can see here, we have this. 156 00:10:33,530 --> 00:10:36,230 Hello, newly created Hello all file here. 157 00:10:36,230 --> 00:10:40,100 And after that we will also create the GCC to linking. 158 00:10:40,130 --> 00:10:44,480 We will use the GCC for linking purposes. 159 00:10:44,630 --> 00:10:48,850 So no start files and all here. 160 00:10:48,860 --> 00:10:49,640 Hello. 161 00:10:50,430 --> 00:10:51,600 Hello, Dot all. 162 00:10:51,900 --> 00:10:56,970 And as you can see, we got some warning, but, uh, the program will work here. 163 00:10:57,150 --> 00:10:58,110 This is for the. 164 00:11:00,210 --> 00:11:01,080 Mining purposes. 165 00:11:01,080 --> 00:11:03,980 And as you can see, we have created our hello here. 166 00:11:03,990 --> 00:11:05,880 Now we will execute this. 167 00:11:06,030 --> 00:11:14,910 And as you can see here, our hello world is Hello World Assembly program that we've wrote in this lecture 168 00:11:14,910 --> 00:11:16,050 is works. 169 00:11:17,180 --> 00:11:17,630 Here. 170 00:11:17,630 --> 00:11:26,990 And with this method here we compile this directly assembles hello dot code using nasm and then links 171 00:11:26,990 --> 00:11:32,000 it into an executable using GCC without using the makefile. 172 00:11:32,000 --> 00:11:39,110 And if this still produces any errors, please ensure that you are using the system with nasm and GCC 173 00:11:39,860 --> 00:11:41,240 properly installed. 174 00:11:41,240 --> 00:11:43,670 And I'm waiting you in next lecture.