1 00:00:00,440 --> 00:00:05,330 Hello, my name is Stephan, and in this lecture we will embark on a journey into the realm of assembly 2 00:00:05,330 --> 00:00:11,270 programming, and our focus will be on understanding conditional jumps and branching. 3 00:00:11,330 --> 00:00:12,530 Crucial concepts. 4 00:00:13,530 --> 00:00:15,060 Low level programming. 5 00:00:15,060 --> 00:00:21,870 And to illustrate this and we will dissect the code that called the jumped ASM. 6 00:00:21,900 --> 00:00:27,690 Actually, we also develop it and I will explain this code line by line. 7 00:00:27,720 --> 00:00:31,650 So here, let's open our CSM project. 8 00:00:32,040 --> 00:00:35,010 The project here. 9 00:00:35,620 --> 00:00:37,780 Uh, we will start from zero. 10 00:00:38,130 --> 00:00:42,580 Uh, let's go to assembler here, create a new file. 11 00:00:42,580 --> 00:00:45,370 Let's jump here. 12 00:00:46,220 --> 00:00:47,420 And save it. 13 00:00:49,850 --> 00:00:52,220 Jump jumping, that is. 14 00:00:54,310 --> 00:01:01,210 And here, let's actually, uh, we will jump jumping that Assam at the comment here. 15 00:01:01,240 --> 00:01:03,280 Jumping at Assam. 16 00:01:03,400 --> 00:01:06,490 Exploring conditional jumps. 17 00:01:06,670 --> 00:01:08,620 Conditional jumps. 18 00:01:09,160 --> 00:01:13,330 And here we will create the extern printf. 19 00:01:13,420 --> 00:01:17,560 This is for declaring the external function. 20 00:01:17,640 --> 00:01:23,680 Uh, declare the external function printf. 21 00:01:24,250 --> 00:01:29,260 And here we will create a section data here with tabs. 22 00:01:29,260 --> 00:01:41,230 Here we will create a number one peak here 42 because this we are here defining a variable number one 23 00:01:41,980 --> 00:01:45,130 with the value of 42. 24 00:01:46,840 --> 00:01:47,380 42. 25 00:01:47,380 --> 00:01:50,110 And here we will also declare. 26 00:01:50,410 --> 00:01:52,620 Define a variable. 27 00:01:52,630 --> 00:01:59,170 Number two with the value of 41 here. 28 00:01:59,720 --> 00:02:03,230 So defining a variable. 29 00:02:03,230 --> 00:02:08,600 Number two with the path, the value. 30 00:02:10,020 --> 00:02:11,340 Value of. 31 00:02:12,340 --> 00:02:14,140 With the value of 41. 32 00:02:15,720 --> 00:02:22,200 And also we'll create the format string for comparison result one. 33 00:02:22,200 --> 00:02:28,560 And we will also write our code for format string for comparison result two here. 34 00:02:28,560 --> 00:02:29,460 So fmt. 35 00:02:30,930 --> 00:02:36,270 If empty one DB here a number. 36 00:02:38,850 --> 00:02:39,750 Number. 37 00:02:39,930 --> 00:02:43,560 Number one here and. 38 00:02:47,130 --> 00:02:48,210 Number two. 39 00:02:50,050 --> 00:03:00,550 So this will recreate your code like this because it's actually prints like equal or greater and equal, 40 00:03:00,580 --> 00:03:02,230 but it actually shows like this. 41 00:03:02,230 --> 00:03:05,470 So this is easier and more understandable to view here. 42 00:03:05,470 --> 00:03:09,580 But if we make this here and as you can see here, these are the separate characters. 43 00:03:09,760 --> 00:03:19,500 This shows this together, uh, to one as of one symbol shows that this is the operand, right? 44 00:03:19,510 --> 00:03:25,600 So logic, operand, operand and here ten and zero. 45 00:03:25,630 --> 00:03:26,920 So here we are. 46 00:03:26,920 --> 00:03:28,990 Format, string. 47 00:03:33,050 --> 00:03:36,230 Format string for comparison. 48 00:03:38,060 --> 00:03:40,580 The result one. 49 00:03:40,580 --> 00:03:43,940 And also we will create the form of two Again. 50 00:03:43,950 --> 00:03:51,410 DB here number one is less than number, one less. 51 00:03:53,070 --> 00:03:55,740 And, uh, number two. 52 00:03:55,740 --> 00:03:56,700 Number two. 53 00:03:56,730 --> 00:03:58,470 Here again, we. 54 00:04:02,920 --> 00:04:04,900 This is for formatting. 55 00:04:05,110 --> 00:04:06,940 Format String. 56 00:04:07,690 --> 00:04:09,610 For comparison. 57 00:04:09,610 --> 00:04:10,930 Result two. 58 00:04:11,350 --> 00:04:16,300 And here we start with the basics. 59 00:04:16,450 --> 00:04:23,500 We are declaring the printf function that will help us display messages later. 60 00:04:23,500 --> 00:04:27,100 And in this data section we define two variables. 61 00:04:27,400 --> 00:04:32,200 Number one and number two which and format strings. 62 00:04:32,210 --> 00:04:34,570 Also Fmt one. 63 00:04:34,960 --> 00:04:40,590 Fmt two to prepare for our output messages. 64 00:04:40,600 --> 00:04:49,750 And here we will also create the section text right now, section text Global Main. 65 00:04:49,750 --> 00:04:57,760 We are declaring this for the global entry point of the program main here and that's it. 66 00:04:57,760 --> 00:05:02,710 So now we will pass the beep here and after that. 67 00:05:05,700 --> 00:05:07,470 It will save this. 68 00:05:12,380 --> 00:05:14,180 Leaving the base pointer. 69 00:05:16,120 --> 00:05:18,400 Maybe the base pointer here. 70 00:05:18,550 --> 00:05:19,270 Move. 71 00:05:21,530 --> 00:05:21,820 Khabib. 72 00:05:21,890 --> 00:05:23,190 Actually, let's this. 73 00:05:32,520 --> 00:05:33,160 RB. 74 00:05:39,290 --> 00:05:41,060 And also RSP. 75 00:05:41,720 --> 00:05:43,310 Now with this. 76 00:05:47,280 --> 00:05:48,750 We are set. 77 00:05:49,020 --> 00:05:52,220 Set up the base pointer. 78 00:05:53,100 --> 00:06:01,320 And here, moving on this text section, we declare the global main function, which marks the entry 79 00:06:01,320 --> 00:06:03,780 point of our program. 80 00:06:04,050 --> 00:06:08,880 And we are also setting up the stack frame with the past rbpp. 81 00:06:09,690 --> 00:06:09,900 Uh. 82 00:06:11,190 --> 00:06:20,650 And move rwp rsp instructions ensuring that we have a proper execution environment. 83 00:06:20,670 --> 00:06:23,530 And now let's proceed with this. 84 00:06:23,580 --> 00:06:26,670 Here we will move the racks. 85 00:06:27,120 --> 00:06:27,870 Move. 86 00:06:29,700 --> 00:06:31,730 Tracks here. 87 00:06:31,830 --> 00:06:40,260 The number one here we are loading the value of number one into the register racks. 88 00:06:41,310 --> 00:06:44,460 But the value of. 89 00:06:45,540 --> 00:06:54,150 Of number one into register racks and move rb x rb x. 90 00:06:54,990 --> 00:06:57,510 We will write number two. 91 00:06:57,720 --> 00:07:06,030 Here we are loading the load, the value of number two into a register. 92 00:07:06,920 --> 00:07:08,770 RBCs again. 93 00:07:09,320 --> 00:07:18,520 And here we start loading by values of number one and number two into registers. 94 00:07:18,530 --> 00:07:22,520 The the racks here now holds value of. 95 00:07:23,650 --> 00:07:29,660 42, and RBCs contains the value of 41. 96 00:07:29,680 --> 00:07:30,100 Right. 97 00:07:30,100 --> 00:07:32,590 So these registers are. 98 00:07:33,280 --> 00:07:35,770 Where we will perform comparisons. 99 00:07:36,100 --> 00:07:42,150 Now here we will compare the values in Rax and Rex. 100 00:07:42,190 --> 00:07:45,460 In order to do that, we will use this instruction. 101 00:07:45,670 --> 00:07:46,450 So. 102 00:07:47,370 --> 00:07:49,910 Racks and RBCs. 103 00:07:50,100 --> 00:07:52,560 And after that, here. 104 00:07:55,480 --> 00:08:02,650 ING cmp for comparing the values in RACs and RBC's. 105 00:08:02,650 --> 00:08:05,080 And here we will use the. 106 00:08:08,510 --> 00:08:09,440 He here? 107 00:08:10,430 --> 00:08:11,810 As greater. 108 00:08:14,050 --> 00:08:15,460 Now with this. 109 00:08:15,460 --> 00:08:17,200 Here we are. 110 00:08:17,740 --> 00:08:18,850 Uh, jump. 111 00:08:19,380 --> 00:08:29,360 To greater if Rax is greater than or equal to rb x. 112 00:08:30,130 --> 00:08:32,410 Um, and next we use this. 113 00:08:32,410 --> 00:08:36,100 Um, here again, I will explain this again from zero. 114 00:08:36,280 --> 00:08:44,830 So this we are using this CPAp instruction to compare the values in racks and as I explained here. 115 00:08:44,830 --> 00:08:50,860 So the interesting part is here this instruction. 116 00:08:50,860 --> 00:08:58,720 So if racks is greater than or equal to RB x, we jump to a greater label. 117 00:08:58,720 --> 00:09:03,790 Otherwise we continue with the next instruction here. 118 00:09:04,710 --> 00:09:05,540 The chairs. 119 00:09:05,580 --> 00:09:09,210 We will load the format string for the second message. 120 00:09:09,210 --> 00:09:10,290 So move. 121 00:09:13,510 --> 00:09:16,000 Audi Fm2. 122 00:09:18,120 --> 00:09:22,050 This is for loading the format string. 123 00:09:23,640 --> 00:09:28,890 Praying for for the second message move. 124 00:09:31,740 --> 00:09:32,220 Zero. 125 00:09:34,430 --> 00:09:34,790 Zero. 126 00:09:34,790 --> 00:09:35,720 This is. 127 00:09:36,970 --> 00:09:40,270 This is for clearing racks here. 128 00:09:40,450 --> 00:09:45,010 No xmm registers in. 129 00:09:46,090 --> 00:09:48,310 All here. 130 00:09:49,420 --> 00:09:51,760 And we will call the print. 131 00:09:53,840 --> 00:09:54,470 Grand theft. 132 00:09:59,210 --> 00:10:01,560 This is this calls the printf. 133 00:10:05,480 --> 00:10:06,800 Number one. 134 00:10:07,070 --> 00:10:08,420 Number one. 135 00:10:08,720 --> 00:10:11,720 Less than number two. 136 00:10:12,690 --> 00:10:13,980 And GMP. 137 00:10:16,280 --> 00:10:18,170 BMP exit. 138 00:10:18,410 --> 00:10:21,350 So here this jump. 139 00:10:22,220 --> 00:10:24,220 To the table. 140 00:10:24,300 --> 00:10:30,020 So if the comparison condition didn't met here. 141 00:10:30,700 --> 00:10:32,260 Uh, wasn't or wasn't mad. 142 00:10:32,310 --> 00:10:42,160 Uh, we moved here, so we load the format, string Fmt into the RDA register and we clear rax since 143 00:10:42,190 --> 00:10:46,300 no error mem registers are involved here. 144 00:10:47,260 --> 00:10:50,140 Um, or registers involved here. 145 00:10:50,140 --> 00:10:55,060 And we then call the printf function to display the number. 146 00:10:55,090 --> 00:10:58,450 One is less than number two. 147 00:10:59,110 --> 00:11:09,400 Following that, we perform unconditional jump to the X label here and now we will use this greeter 148 00:11:09,400 --> 00:11:10,390 function here. 149 00:11:15,010 --> 00:11:16,370 And that's it. 150 00:11:16,390 --> 00:11:17,350 Move. 151 00:11:18,590 --> 00:11:21,720 R the i fmt one. 152 00:11:21,740 --> 00:11:22,780 Here we are. 153 00:11:22,790 --> 00:11:26,510 Load loading the format string. 154 00:11:27,620 --> 00:11:30,040 Spring for the first. 155 00:11:31,060 --> 00:11:34,780 Message and again, move racks. 156 00:11:35,590 --> 00:11:36,220 Zero. 157 00:11:36,550 --> 00:11:39,880 And here we are clearing clear the Or. 158 00:11:40,000 --> 00:11:40,230 Yeah. 159 00:11:40,240 --> 00:11:43,900 Clear the racks again and we will. 160 00:11:43,900 --> 00:11:44,560 We are. 161 00:11:44,590 --> 00:11:47,040 We will call the print. 162 00:11:47,050 --> 00:11:47,590 Print. 163 00:11:55,860 --> 00:11:56,600 Here we are. 164 00:11:56,610 --> 00:12:00,360 Call, call print printf to display. 165 00:12:00,930 --> 00:12:07,530 Display number one, greater than or equal to number two. 166 00:12:08,820 --> 00:12:12,930 So if the comparison condition didn't met we. 167 00:12:13,790 --> 00:12:23,270 When the racks greater than or equal to are accident meant we arrive at the greater label. 168 00:12:24,040 --> 00:12:31,510 Here we load the format string Fmt one into RDA register and clear Rax once more. 169 00:12:31,510 --> 00:12:39,940 So we then call the print function to display the message number one now greater than or equal to number 170 00:12:39,940 --> 00:12:40,540 two. 171 00:12:40,630 --> 00:12:43,510 And after that we will also. 172 00:12:44,990 --> 00:12:47,750 Create the exit label. 173 00:12:50,360 --> 00:12:51,590 Exit label. 174 00:12:51,680 --> 00:12:54,890 So here we will actually down here. 175 00:12:54,890 --> 00:12:56,840 So here we will move. 176 00:12:58,750 --> 00:12:59,380 RSP. 177 00:13:00,520 --> 00:13:01,990 RVP here. 178 00:13:02,170 --> 00:13:07,990 RVP Here we are restoring the stack pointer. 179 00:13:12,310 --> 00:13:15,280 Uh, pop up here. 180 00:13:15,370 --> 00:13:16,180 Here we are. 181 00:13:16,180 --> 00:13:20,530 Restore restoring the bass pointer again. 182 00:13:21,680 --> 00:13:22,580 Pointer. 183 00:13:22,790 --> 00:13:28,040 And we are now we're going to return from the main function. 184 00:13:28,890 --> 00:13:29,670 And. 185 00:13:30,600 --> 00:13:33,740 Brett here and we will not add anything here. 186 00:13:33,750 --> 00:13:35,580 Just go to command. 187 00:13:36,000 --> 00:13:38,910 Write that return from the. 188 00:13:40,430 --> 00:13:41,930 A main function. 189 00:13:41,930 --> 00:13:45,350 And finally, we reach the exit label. 190 00:13:46,160 --> 00:13:49,280 This is where we wrap up our program. 191 00:13:49,370 --> 00:13:56,180 We restore the stack pointer, RSP and base pointer rb. 192 00:13:56,480 --> 00:13:57,290 Oops, sorry. 193 00:13:57,290 --> 00:14:01,610 Not RB rb here to their original values. 194 00:14:01,610 --> 00:14:10,610 So we then use the pop rb p instruction to clean up the stack frame and read instruction. 195 00:14:11,120 --> 00:14:19,850 The read instruction gracefully exits the main function and in conclusion, our journey through does 196 00:14:19,850 --> 00:14:25,340 jumping that ACM has unveiled the mechanisms of conditional jumps and branching. 197 00:14:25,370 --> 00:14:32,150 So we have explored how assembly code can make decisions based on conditions influencing program flow. 198 00:14:33,070 --> 00:14:40,690 And these fundamental concepts lay the foundation for understanding more complex programming challenges 199 00:14:40,840 --> 00:14:43,100 and optimizing the code. 200 00:14:43,120 --> 00:14:49,720 So now when we click on this here, and as you can see here, we will get an error. 201 00:14:50,650 --> 00:15:01,570 You are getting this error because instruction we wrote the 64 bit assembly code, but here as default, 202 00:15:01,570 --> 00:15:06,970 the CSM comes with the 32 bit instruction. 203 00:15:07,880 --> 00:15:08,840 Or selected. 204 00:15:08,840 --> 00:15:13,370 And in order to fix that, we will go to settings here. 205 00:15:14,040 --> 00:15:14,550 Um. 206 00:15:15,520 --> 00:15:18,730 World here from settings world and here. 207 00:15:18,730 --> 00:15:22,090 As you can see, we have mode here click on 64. 208 00:15:22,970 --> 00:15:28,520 And assembler click on NSM because NSM best for this purposes. 209 00:15:28,730 --> 00:15:38,030 And as you can see here, when we click on 32 bit x86, we are getting this M 32 here. 210 00:15:38,030 --> 00:15:43,100 But when we click on 64 bit, as you can see it, it also changes here. 211 00:15:44,060 --> 00:15:52,210 It also changed l 32 two from 32 to x l 64. 212 00:15:52,220 --> 00:15:54,890 So now apply this here. 213 00:15:56,200 --> 00:15:57,220 And click okay. 214 00:15:57,250 --> 00:15:58,720 Now let's run our program again. 215 00:15:58,720 --> 00:16:04,120 And as you can see here, we'll start it and build successfully. 216 00:16:04,120 --> 00:16:12,820 And our output is number one, is greater than or equal to number two. 217 00:16:12,820 --> 00:16:14,200 And the program finished. 218 00:16:14,230 --> 00:16:17,710 Normally, execution time is 0.001. 219 00:16:17,710 --> 00:16:26,680 So we can also go to our folder where we compile this assembly here, the CD jump. 220 00:16:27,900 --> 00:16:28,380 LZ. 221 00:16:28,380 --> 00:16:30,300 And as you can see here, we have jumping. 222 00:16:30,960 --> 00:16:35,040 We can also create a separate Makefile and just compile it. 223 00:16:35,070 --> 00:16:37,110 But in this case we will not do that. 224 00:16:37,230 --> 00:16:42,420 And as you can see here, our code is works and when we. 225 00:16:44,040 --> 00:16:47,940 Here on test the program executing program finished. 226 00:16:47,970 --> 00:16:52,320 Normally the execution time is 0.0001. 227 00:16:53,250 --> 00:16:55,110 And this is our output. 228 00:16:55,110 --> 00:17:03,060 So remember, mastering this building blocks will empower you to delve deeper into the intricacies, 229 00:17:03,060 --> 00:17:11,540 intricacies, world of assembly programming and keep practicing, experimenting and pushing your boundaries. 230 00:17:11,550 --> 00:17:19,410 So the journey has just begun in this lecture here, and thank you for joining me in this enlightening 231 00:17:19,410 --> 00:17:20,460 exploration. 232 00:17:20,460 --> 00:17:28,530 Until next time, keep coding and keep expanding your programming, programming skills and happy learning 233 00:17:28,560 --> 00:17:30,180 awaiting you in next lecture.