1 00:00:00,390 --> 00:00:01,470 Hello, my name is Stephan. 2 00:00:01,470 --> 00:00:05,310 And in this lecture we will create our Hello World program again. 3 00:00:05,310 --> 00:00:08,400 But in this time we will use the make file. 4 00:00:08,700 --> 00:00:15,750 In previous lecture we created our Hello World program in assembly language, but without the BS section 5 00:00:15,750 --> 00:00:17,860 and without the make file. 6 00:00:17,880 --> 00:00:24,320 But in this lecture we added this to knowledge into our brains and we will start our programming. 7 00:00:24,330 --> 00:00:30,150 So generations of programmers have started their programming careers by learning how to display Hello 8 00:00:30,150 --> 00:00:31,850 World on a computer screen. 9 00:00:31,860 --> 00:00:39,240 It is a tradition that was started in the 70s by brain koningen In the book he wrote with Dennis Ritchie 10 00:00:39,330 --> 00:00:45,270 the C programming language, Cunningham developed the C programming language at Bell Labs. 11 00:00:45,270 --> 00:00:53,130 Since then, the C language has changed a lot, but has remained the language that every self-respecting 12 00:00:53,130 --> 00:00:54,930 programmer should be familiar with. 13 00:00:54,960 --> 00:01:01,390 The majority of modern and fancy programming languages have their roots in C, and C is sometimes called 14 00:01:01,390 --> 00:01:06,760 a portable assembly language, and as an aspiring assembler programmer, you should get familiar with 15 00:01:06,790 --> 00:01:08,560 C to honor the tradition. 16 00:01:08,560 --> 00:01:13,570 We will start with an assembler program to put Hello World on your screen and we will also develop the 17 00:01:13,570 --> 00:01:14,290 Makefile. 18 00:01:14,290 --> 00:01:18,610 So let's get started with developing Hello dot CSM here. 19 00:01:18,610 --> 00:01:25,510 So instead of writing this again, we can also um, as you can see we have three Hello world here. 20 00:01:25,510 --> 00:01:35,350 So erm delete, everything starts with H here and here we will open the our fasm which is the IDE or 21 00:01:35,440 --> 00:01:38,770 CSM and here we will create a new project. 22 00:01:38,770 --> 00:01:45,430 And in this new project we will save this in our assembly here. 23 00:01:46,150 --> 00:01:46,720 That's it. 24 00:01:46,720 --> 00:01:48,970 And we will name it as. 25 00:01:54,470 --> 00:01:55,410 Not RSM. 26 00:01:56,130 --> 00:01:57,000 And that's it. 27 00:01:57,120 --> 00:02:05,220 And here we will delete the auto generate generated code by sacem and we will add new command and we 28 00:02:05,220 --> 00:02:13,230 will create our Hello, SSR, ESM, and we will add a section data section data. 29 00:02:13,380 --> 00:02:18,300 Again, Mxg DB Here again. 30 00:02:18,780 --> 00:02:19,440 Hello. 31 00:02:21,250 --> 00:02:24,100 World is okay, I think. 32 00:02:24,100 --> 00:02:32,440 And also we will add a null terminated string here and we will also add the section VSS and we will 33 00:02:32,440 --> 00:02:34,930 also create a section text. 34 00:02:34,930 --> 00:02:39,760 And in this lecture we will create again a global main. 35 00:02:39,760 --> 00:02:47,380 So if you remember, this code is different from the code we created in previous lecture and we will 36 00:02:47,380 --> 00:02:49,300 create our main function here. 37 00:02:49,450 --> 00:02:52,150 Main move here. 38 00:02:52,480 --> 00:02:53,350 Move. 39 00:02:55,210 --> 00:02:57,490 AX1. 40 00:02:57,910 --> 00:03:01,150 And also let's add the spaces between this. 41 00:03:03,050 --> 00:03:04,550 Our x one. 42 00:03:04,640 --> 00:03:09,110 This one corresponds for the right and move. 43 00:03:09,790 --> 00:03:10,510 Are. 44 00:03:14,450 --> 00:03:16,880 Move are the I. 45 00:03:18,230 --> 00:03:18,920 One. 46 00:03:18,920 --> 00:03:22,220 This is for the STD out. 47 00:03:24,350 --> 00:03:26,600 Out and we will also create the. 48 00:03:28,490 --> 00:03:28,820 Move. 49 00:03:30,830 --> 00:03:31,820 RSI. 50 00:03:32,940 --> 00:03:34,410 MCG here. 51 00:03:34,410 --> 00:03:39,510 As you can see here, we will get this variable name MCG. 52 00:03:39,750 --> 00:03:49,430 This is the string to display in RSA and we will also move here the RDX. 53 00:03:49,530 --> 00:03:54,150 This is the length of the string without the zero here. 54 00:03:54,390 --> 00:03:59,820 So this is the length of the string without zero. 55 00:03:59,820 --> 00:04:02,220 And we will call the fiscal. 56 00:04:02,370 --> 00:04:06,750 In this case, this fiscal will display the string on this here. 57 00:04:07,020 --> 00:04:08,340 Display the. 58 00:04:09,280 --> 00:04:11,260 Ring and move. 59 00:04:12,760 --> 00:04:14,130 Racks here. 60 00:04:14,140 --> 00:04:14,830 60. 61 00:04:14,860 --> 00:04:17,200 This is for exit code here. 62 00:04:17,560 --> 00:04:21,340 60 equals to exit and move. 63 00:04:21,610 --> 00:04:22,360 Ready? 64 00:04:22,960 --> 00:04:23,740 Zero. 65 00:04:23,740 --> 00:04:30,280 And this is this means the success exit code here that our program. 66 00:04:31,340 --> 00:04:32,870 With success here. 67 00:04:33,200 --> 00:04:35,630 It says exit code. 68 00:04:35,630 --> 00:04:38,000 And we will also add t-sql. 69 00:04:38,030 --> 00:04:40,040 This will quit the program. 70 00:04:43,160 --> 00:04:46,880 That here, this is the last line of our code. 71 00:04:47,000 --> 00:04:52,640 And so there are many good text editors on the market, both free and commercial. 72 00:04:52,640 --> 00:05:01,400 In this case, we will use CSM, which is a free and look one that supports syntax highlighting for 73 00:05:01,940 --> 00:05:02,630 64 bit. 74 00:05:02,630 --> 00:05:10,220 In this case, CSM is fully supports 64 bit and in most cases you will have to download some kind of 75 00:05:10,220 --> 00:05:11,420 plugin if you. 76 00:05:12,120 --> 00:05:16,290 Get it or uh, any other text editors to have syntax highlighting. 77 00:05:16,290 --> 00:05:23,010 But in, in s m the syntax highlighting come with pre-installed. 78 00:05:23,400 --> 00:05:28,860 And in this course we will write code for the netwide assembler here. 79 00:05:28,860 --> 00:05:32,310 Let's actually write this right now. 80 00:05:32,340 --> 00:05:37,710 Grommit np x This is for the writing things on the screen. 81 00:05:38,540 --> 00:05:46,040 So in this course we will write the code for Netwide assembler and. 82 00:05:46,640 --> 00:05:47,300 A. 83 00:05:48,110 --> 00:05:49,030 S. 84 00:05:49,250 --> 00:05:49,880 M. 85 00:05:50,780 --> 00:05:53,750 So there are other assemblers as well, such as. 86 00:05:53,900 --> 00:05:59,300 Y a s m f a s m gas or m a m from the Microsoft. 87 00:05:59,300 --> 00:06:05,030 And as with everything in the computer world, there are sometimes heavy discussions about which assembler 88 00:06:05,030 --> 00:06:14,150 is the best, and we will use an ASM in this course because it is available on Linux, Windows and Mac 89 00:06:14,150 --> 00:06:15,080 OS. 90 00:06:15,440 --> 00:06:19,970 This is available on Windows and Linux. 91 00:06:21,080 --> 00:06:24,670 And in Mac OS. 92 00:06:26,230 --> 00:06:27,940 Mac OS. 93 00:06:28,800 --> 00:06:31,140 And so this is one of the best. 94 00:06:32,960 --> 00:06:38,750 Assemblers here and RSM And because there are and also there is a. 95 00:06:38,770 --> 00:06:48,790 Large community using an RSM, you can find the the manual at the nasm.us and we can also use the jedit 96 00:06:48,790 --> 00:06:53,590 with an assembler language syntax file installed so you can find. 97 00:06:55,070 --> 00:06:57,980 The assembly language for the Jets as well. 98 00:06:57,980 --> 00:07:05,390 But in this case, as I said, M is a best idea for assembly and it's also free and open source. 99 00:07:05,390 --> 00:07:12,410 And here on our screen, the Hello dot Asem file is shown here. 100 00:07:12,410 --> 00:07:19,160 And as you can see here, we think that you will agree that syntax highlighting makes the assembler 101 00:07:19,160 --> 00:07:20,780 code a little bit easier to read. 102 00:07:20,780 --> 00:07:27,590 So when we're when we write assembly programs, we have two windows open on our screen, a windows with 103 00:07:27,590 --> 00:07:33,710 the IDE or text editor containing our assembler source code and the windows with a command prompt in 104 00:07:33,710 --> 00:07:38,450 the project directory so that we can easily switch between the editing and manipulating the project 105 00:07:38,450 --> 00:07:41,840 file and assembling a program, debugging and so on. 106 00:07:41,840 --> 00:07:45,770 And we agree that for more complex and larger project, this is not feasible. 107 00:07:45,770 --> 00:07:48,890 So you will need an integrated development environment. 108 00:07:48,920 --> 00:07:56,820 Ida But for now, working with a simple or text editor and the command line in other words, CLI will 109 00:07:56,820 --> 00:08:01,920 do our job and we will improve that in next lectures as well. 110 00:08:02,860 --> 00:08:07,090 And here we will also need the GCC for installing this. 111 00:08:07,720 --> 00:08:13,870 Installing GCC is one of the simplest things to do in some developing assembly languages. 112 00:08:13,870 --> 00:08:22,120 So GCC stands for new compiler collection and this is the standard compiler and linker tool Linux. 113 00:08:22,120 --> 00:08:27,070 So Genius stands for it's not Unix and it's a recursive acronym. 114 00:08:27,070 --> 00:08:33,430 Using the recursive acronyms for naming things is an insider programmer joke that started in the 70s 115 00:08:33,430 --> 00:08:37,030 by Lisp programmers and the GCC. 116 00:08:37,630 --> 00:08:41,980 So firstly, of course we need to install GCC here. 117 00:08:41,980 --> 00:08:43,390 Sudo apt. 118 00:08:44,510 --> 00:08:46,170 Install DCC. 119 00:08:46,250 --> 00:08:46,730 That's it. 120 00:08:46,730 --> 00:08:48,590 Enter your pseudo password and that's it. 121 00:08:48,590 --> 00:08:49,730 As you can see, I'm installed. 122 00:08:49,730 --> 00:08:54,260 But now let's actually remove the DCC again. 123 00:08:54,260 --> 00:08:58,130 So the APT remove DCC here. 124 00:08:58,160 --> 00:08:59,090 Yes. 125 00:08:59,090 --> 00:09:02,900 So this is a pretty easy to install here. 126 00:09:06,220 --> 00:09:10,150 And here we will also need to use bool essential. 127 00:09:10,540 --> 00:09:19,180 And here now we will install the GCC again actually clear the terminal and so sudo apt install GCC. 128 00:09:19,210 --> 00:09:19,930 That's it. 129 00:09:19,930 --> 00:09:22,750 And this is easy as that. 130 00:09:22,750 --> 00:09:25,300 And as you can see, GCC is already installed. 131 00:09:25,300 --> 00:09:29,920 So we will also need to install the nasm here. 132 00:09:29,920 --> 00:09:38,610 Sudo apt install, build, essential ascent tool and ASM. 133 00:09:38,680 --> 00:09:42,880 So actually let's actually first install it, remove our purge here. 134 00:09:42,880 --> 00:09:44,080 So we will remove it. 135 00:09:44,080 --> 00:09:49,450 As you can see, 556 disk space will be freed. 136 00:09:55,340 --> 00:09:56,650 And now pseudo. 137 00:09:59,920 --> 00:10:05,170 Sudo apt install build essential. 138 00:10:06,290 --> 00:10:07,760 And ask again. 139 00:10:07,760 --> 00:10:09,410 And as you can see here. 140 00:10:14,550 --> 00:10:15,120 Here. 141 00:10:16,260 --> 00:10:17,310 And that's it. 142 00:10:18,430 --> 00:10:22,780 So we have the essential and and also we have the GKE. 143 00:10:22,900 --> 00:10:29,170 So in order to check that, we will write GKE V here and as you can see here, using Boolean spec. 144 00:10:29,170 --> 00:10:37,330 So if you use if you are seeing this, this is a version Debian, then this means that GKE is installed 145 00:10:37,420 --> 00:10:40,360 and we will also check the V here. 146 00:10:40,360 --> 00:10:44,620 And as you can see in version 22.60.01. 147 00:10:44,620 --> 00:10:48,190 So NSM is also successfully installed. 148 00:10:48,550 --> 00:10:51,370 And now let's go back to our Hello World program. 149 00:10:51,370 --> 00:10:55,990 And this is the complete version of our code. 150 00:10:55,990 --> 00:11:03,850 And after that which we will create this Hello World program without the Makefile. 151 00:11:03,850 --> 00:11:08,590 But as a first lecture, let's create this with a. 152 00:11:09,850 --> 00:11:10,390 Make file. 153 00:11:10,390 --> 00:11:15,520 So if you click on this and as you can see, we have hello Dot Asem, if we open this with our text 154 00:11:15,520 --> 00:11:19,150 editor, you can see our code without syntax highlighting of course. 155 00:11:19,150 --> 00:11:22,780 And now we will go back to terminal here. 156 00:11:22,930 --> 00:11:28,030 Now we will what we will do is we will open the new file torch. 157 00:11:29,020 --> 00:11:31,180 Actually, let's use it with mouse pad. 158 00:11:31,570 --> 00:11:32,380 Mouse. 159 00:11:35,060 --> 00:11:45,860 Uh, so this is going to be Makefile and uh, yes, so here we will delete our old code. 160 00:11:45,860 --> 00:11:48,500 So this is a new file as you can see here. 161 00:11:48,890 --> 00:11:51,960 Clear LZ LA. 162 00:11:52,520 --> 00:11:55,850 Now we will open the make file here with mouse path. 163 00:11:56,990 --> 00:11:57,920 And that's it. 164 00:11:57,950 --> 00:12:00,950 Now we will write our make file here. 165 00:12:00,950 --> 00:12:02,810 So make file. 166 00:12:04,170 --> 00:12:05,340 Make file for. 167 00:12:05,340 --> 00:12:06,870 Hello, Esme. 168 00:12:07,140 --> 00:12:07,890 Hello. 169 00:12:08,400 --> 00:12:13,440 Here the hello all and JC all. 170 00:12:13,950 --> 00:12:14,820 Hello. 171 00:12:15,120 --> 00:12:15,510 Hello. 172 00:12:15,510 --> 00:12:16,230 That all? 173 00:12:16,230 --> 00:12:20,100 And we will also know pi argument. 174 00:12:20,100 --> 00:12:22,950 And after that we will use the Hello. 175 00:12:22,950 --> 00:12:24,390 That all here. 176 00:12:24,510 --> 00:12:24,870 Hello. 177 00:12:24,870 --> 00:12:31,620 That And after that we will with the tab here we will add an elf. 178 00:12:32,610 --> 00:12:36,690 For the F warfare. 179 00:12:38,070 --> 00:12:38,790 Hello. 180 00:12:39,790 --> 00:12:40,600 But Assam. 181 00:12:40,620 --> 00:12:44,400 And also we will create a yellow dot here. 182 00:12:44,580 --> 00:12:46,770 And that's it with our make file. 183 00:12:46,770 --> 00:12:49,940 Now we will go back and run. 184 00:12:50,100 --> 00:12:57,290 We will save the this file as a make file in the same directory as Hello Dot Assam. 185 00:12:57,570 --> 00:13:03,360 And also also after writing it, we can close the make file here. 186 00:13:03,360 --> 00:13:10,440 As you can see here, our make file is increased at make file and as it is our make file. 187 00:13:10,440 --> 00:13:11,970 And after that. 188 00:13:13,090 --> 00:13:13,900 Uh, we will. 189 00:13:14,430 --> 00:13:20,350 Um, actually, this, uh, firstly, uh, let's read the make file here, actually, and read it from 190 00:13:20,350 --> 00:13:20,800 here. 191 00:13:20,800 --> 00:13:26,310 So you read the make file from the bottom up to see what's what it is doing. 192 00:13:26,320 --> 00:13:28,900 So here's a simplified explanation here. 193 00:13:28,900 --> 00:13:33,640 So the make utility is open, the make file. 194 00:13:33,640 --> 00:13:35,440 So this make utility. 195 00:13:37,560 --> 00:13:39,200 Uh, works with a dependency tree. 196 00:13:39,210 --> 00:13:40,890 So it noticed that. 197 00:13:40,890 --> 00:13:41,510 Hello? 198 00:13:41,520 --> 00:13:44,190 Depends on Hello, Dot. 199 00:13:44,220 --> 00:13:45,710 All right. 200 00:13:45,720 --> 00:13:47,810 And it sees the Hello. 201 00:13:47,810 --> 00:13:51,310 All depends on Hello, Dot. 202 00:13:52,200 --> 00:13:52,830 And that. 203 00:13:52,860 --> 00:13:55,260 Hello, Asem depends on nothing else. 204 00:13:55,260 --> 00:14:01,310 So make compares the last modification date of Hello dot asem with the hello. 205 00:14:01,310 --> 00:14:02,250 Oh and. 206 00:14:02,250 --> 00:14:11,230 And if the date from hello Asem is more recent make executes the line after hello which is hello dot 207 00:14:11,280 --> 00:14:11,940 asem. 208 00:14:11,940 --> 00:14:20,130 So then make starts reading the make file and finds that the modification date of hello dot oh is more 209 00:14:20,130 --> 00:14:21,420 recent than the date from. 210 00:14:21,420 --> 00:14:21,750 Hello. 211 00:14:22,020 --> 00:14:22,790 Hello. 212 00:14:22,800 --> 00:14:25,950 So it executes the line after hello. 213 00:14:25,950 --> 00:14:27,340 Which is hello. 214 00:14:27,340 --> 00:14:33,720 Oh here and in the bottom line of our make file and is used as the assembler. 215 00:14:33,900 --> 00:14:41,620 So the f is followed by the output format in our case is elf64, which means executable linkable format 216 00:14:41,620 --> 00:14:42,820 for 64 bit. 217 00:14:42,820 --> 00:14:49,390 And the g means that we want to include the include the debug information in a debug format specified 218 00:14:49,390 --> 00:14:58,840 after the F option and we use the var debug format and the software geeks for this format seem to like 219 00:14:58,840 --> 00:15:02,470 The Hobbit and Lord of the Rings written by J.R.R. Tolkien. 220 00:15:02,470 --> 00:15:09,550 So maybe that's why they decided that the var will be a nice compliment to Elf, just in case you were 221 00:15:09,550 --> 00:15:10,090 wondering. 222 00:15:10,090 --> 00:15:13,900 So seriously, the var stands for the debug with arbitrary record format. 223 00:15:14,140 --> 00:15:17,080 The debug with arbitrary the record format. 224 00:15:17,710 --> 00:15:25,780 And stamps in another debug format here, which has nothing to do with all the stabbings in novels. 225 00:15:25,780 --> 00:15:29,460 So the name comes from symbol table strings we will use. 226 00:15:29,470 --> 00:15:33,790 We will not use the steps here so you won't get hurt here. 227 00:15:33,790 --> 00:15:40,000 And this l here tells NASA to generate the LST file. 228 00:15:40,150 --> 00:15:47,830 We will use the LST files to examine the results of the assembly, and NASA will create an object file 229 00:15:48,190 --> 00:15:50,140 with an dot or extension. 230 00:15:50,140 --> 00:15:51,880 And that object file will. 231 00:15:53,130 --> 00:15:56,310 What will next be used by our linker. 232 00:15:56,310 --> 00:16:03,780 And often it will happen that NSM complains a number of cryptic messages and refuses to give you an 233 00:16:03,780 --> 00:16:04,490 object file. 234 00:16:04,500 --> 00:16:10,980 So sometimes NSM will complain so often that it will drive you almost insane. 235 00:16:10,980 --> 00:16:14,480 In use cases, it is essential to keep calm. 236 00:16:14,490 --> 00:16:19,080 You have another coffee and review your code because you did something wrong. 237 00:16:19,110 --> 00:16:24,990 As you as you program more and more in assembly, you will catch mistakes faster. 238 00:16:24,990 --> 00:16:30,600 And when you finally convinced that NSM to give you an object file, this object file is linked with 239 00:16:30,600 --> 00:16:36,570 a linker and a linker takes your object code and searches the system for other files that are needed. 240 00:16:36,570 --> 00:16:40,980 So typically system services are or other object files as well. 241 00:16:40,980 --> 00:16:48,360 So these files are combined with your generated object code by the linker and then an executable file 242 00:16:48,360 --> 00:16:49,050 is produced. 243 00:16:49,080 --> 00:16:56,290 Of course, the linker will take every possible occasion to complain to you about missing things and 244 00:16:56,290 --> 00:16:57,010 so on. 245 00:16:57,010 --> 00:17:02,140 So if that is the case, have another coffee and check your code and make file as well. 246 00:17:02,140 --> 00:17:08,770 In our case, we use the linking functionality of GCC, as you can see here. 247 00:17:08,770 --> 00:17:09,040 Hello. 248 00:17:09,040 --> 00:17:09,310 Hello. 249 00:17:09,310 --> 00:17:13,060 All GCC or hello all know Pi. 250 00:17:13,090 --> 00:17:19,030 So there is an GCC linker and compiler generate position independent executable pis. 251 00:17:19,030 --> 00:17:25,840 So this is to prevent hackers from investigating how memory is used by a program and eventually interfering 252 00:17:25,840 --> 00:17:27,310 with a program execution. 253 00:17:27,310 --> 00:17:32,320 And at this point we will not build a position, independent executables, and it will really complicate 254 00:17:32,320 --> 00:17:36,430 the analysis of our program on purpose for security reasons. 255 00:17:36,520 --> 00:17:40,570 So we added the parameter no Pi in the make file. 256 00:17:40,570 --> 00:17:47,770 And finally, you can insert commands in your Makefile by preceding them with the this hashtag symbols 257 00:17:47,770 --> 00:17:54,970 as we did in first line here, make file for location and we use GCC because of the ease of accessing 258 00:17:54,990 --> 00:17:58,240 C standard library functions from within the assembler code. 259 00:17:58,240 --> 00:18:04,780 And to make life easy we will use C language functions from time to time to simplify the example assembly 260 00:18:04,780 --> 00:18:05,620 code as well. 261 00:18:05,620 --> 00:18:12,340 So just, you know, another popular linker on Linux is LD the linker. 262 00:18:13,290 --> 00:18:17,290 And if the previous paragraphs do not make sense to you, don't worry. 263 00:18:17,310 --> 00:18:18,360 Have a copy. 264 00:18:18,390 --> 00:18:20,140 Copy and carry on. 265 00:18:20,160 --> 00:18:21,180 It is just a background. 266 00:18:22,460 --> 00:18:24,020 Important at this stage. 267 00:18:24,200 --> 00:18:29,510 And here at the command prompt here, we will go back to here. 268 00:18:29,510 --> 00:18:36,380 Let's actually close this and we will go to the directory where you saved our Hello ESM file and your 269 00:18:36,380 --> 00:18:37,070 make file. 270 00:18:37,070 --> 00:18:40,880 In this case, we are in this directory so we won't go anywhere. 271 00:18:40,880 --> 00:18:48,110 So we will type make to assemble and build the program and then run the program by typing. 272 00:18:48,110 --> 00:18:48,470 Hello. 273 00:18:48,470 --> 00:18:51,950 So as you can see, we could Hello here and. 274 00:18:52,970 --> 00:18:58,250 If you see the message Hello world displayed in this screen here. 275 00:18:58,280 --> 00:18:58,940 I'm sorry. 276 00:18:59,240 --> 00:18:59,470 Hello? 277 00:18:59,480 --> 00:19:01,990 With two L and that's it. 278 00:19:02,000 --> 00:19:02,810 Our program works. 279 00:19:02,810 --> 00:19:08,630 So if you see the message Hello world displayed in front of the command prompt, then everything worked 280 00:19:08,630 --> 00:19:09,440 out fine. 281 00:19:09,470 --> 00:19:14,960 Otherwise you made something typing or other error and you need review your source code or mac file. 282 00:19:14,960 --> 00:19:20,000 So refill your cup of coffee and happy debugging.