1 00:00:00,590 --> 00:00:01,730 Hello, my name is Typhoon. 2 00:00:01,730 --> 00:00:06,500 And in this lecture we will delve into the fascinating world of assembly language programming. 3 00:00:06,530 --> 00:00:11,930 In this lecture, we will explore the intricacies of some fundamental arithmetic operations within our 4 00:00:11,930 --> 00:00:12,680 program. 5 00:00:13,330 --> 00:00:22,090 Now get ready to witness the magic of incrementing decrementing shifting, multiplying and dividing. 6 00:00:22,120 --> 00:00:29,200 And throughout this lecture, we will dissect each of these operations step by step, unraveling the 7 00:00:29,200 --> 00:00:34,110 code line by line to understand how they work at their core. 8 00:00:34,120 --> 00:00:41,200 And we will be working with registers, memory and functions to manipulate numbers and display their 9 00:00:41,200 --> 00:00:42,010 results. 10 00:00:42,040 --> 00:00:48,400 The assembly language offers us a such a unique perspective into the inner workings of computers, low 11 00:00:48,400 --> 00:00:56,110 level operations, providing insights into how calculations are executed at the hardware level. 12 00:00:56,140 --> 00:01:02,950 Starting with incrementing, we will see how a single instruction can add one to a number and transform 13 00:01:02,950 --> 00:01:04,220 its value from there. 14 00:01:04,240 --> 00:01:12,190 We will move to Decrementing where we will learn how subtraction plays a role in reducing values. 15 00:01:12,950 --> 00:01:13,550 Shifting. 16 00:01:13,550 --> 00:01:19,970 Our next venture allows us to multiply and divide by powers of two throughout bit manipulation. 17 00:01:20,600 --> 00:01:26,930 And this demonstrates how computer architects use clever tricks to optimize certain operations. 18 00:01:26,930 --> 00:01:30,800 And our journey continues with multiplication and division. 19 00:01:30,800 --> 00:01:36,800 By diving into these operations, we will understand how computers perform these fundamental arithmetic 20 00:01:36,830 --> 00:01:42,620 tasks and how we can harness their power in assembler language. 21 00:01:42,620 --> 00:01:47,510 So fasten your belts as we embark on this enriching journey. 22 00:01:47,510 --> 00:01:54,710 But also by the end of this lecture you will have a solid grasp grasp of incrementing decrementing shifting, 23 00:01:54,710 --> 00:01:58,940 multiplying and dividing, working in assembly language. 24 00:01:59,210 --> 00:02:05,570 Now let's dive in to explore the intricate dance between software and hardware that makes these operations 25 00:02:05,570 --> 00:02:06,380 possible. 26 00:02:06,380 --> 00:02:14,060 But first we will write the incrementing incrementing side of our program. 27 00:02:14,060 --> 00:02:18,140 Here we will load the value of number one to rax. 28 00:02:18,140 --> 00:02:19,370 So move. 29 00:02:20,120 --> 00:02:21,410 Move rax. 30 00:02:23,600 --> 00:02:26,960 Uh, number one inside braces. 31 00:02:26,990 --> 00:02:31,700 Now we will use I and C racks. 32 00:02:32,210 --> 00:02:36,630 This we use this for incrementing rex by one. 33 00:02:36,650 --> 00:02:40,220 We will store the result in result I. 34 00:02:40,950 --> 00:02:41,640 Move. 35 00:02:42,980 --> 00:02:44,750 The result, I result. 36 00:02:46,660 --> 00:02:48,100 And racks. 37 00:02:48,490 --> 00:02:50,620 We will display the result here. 38 00:02:50,620 --> 00:02:53,050 So display the result. 39 00:02:54,690 --> 00:02:56,940 And after that we will move the. 40 00:02:58,670 --> 00:02:59,900 Fmt int. 41 00:02:59,930 --> 00:03:03,260 This is for a local address of format. 42 00:03:03,290 --> 00:03:10,040 Uh, we are loading the address of format string to die and now we will load the address of message 43 00:03:10,040 --> 00:03:10,880 to RSI. 44 00:03:11,240 --> 00:03:14,600 RSI The address of messages. 45 00:03:16,760 --> 00:03:19,130 I nci here. 46 00:03:19,160 --> 00:03:19,910 Number one. 47 00:03:20,120 --> 00:03:21,950 Number one, incremented. 48 00:03:23,160 --> 00:03:24,350 I nci. 49 00:03:24,640 --> 00:03:25,600 Move. 50 00:03:26,350 --> 00:03:27,140 Move! 51 00:03:27,150 --> 00:03:28,980 RDX. 52 00:03:29,940 --> 00:03:30,990 Result, I. 53 00:03:31,020 --> 00:03:35,970 Here we are loading the value of result i to RDX. 54 00:03:36,000 --> 00:03:40,290 Now we will clear the rax register which is returning the value. 55 00:03:41,050 --> 00:03:46,000 And now we will call the printf to display the formatted message. 56 00:03:46,030 --> 00:03:50,470 Now we will develop the decrementing here. 57 00:03:50,470 --> 00:03:55,240 So we will move load the value of number one to rax. 58 00:03:57,210 --> 00:03:57,990 Move. 59 00:04:02,910 --> 00:04:03,780 Racks. 60 00:04:04,730 --> 00:04:07,040 The number one. 61 00:04:08,070 --> 00:04:19,110 Uh, these here we in incrementing we use I and C and here we will use the c here rax this is for decrementing 62 00:04:19,110 --> 00:04:20,730 rax by one. 63 00:04:20,820 --> 00:04:30,060 We will use move here result i here rax we are storing the result in result i and now we will need to 64 00:04:30,060 --> 00:04:31,470 display the results. 65 00:04:31,470 --> 00:04:34,620 So display the result. 66 00:04:34,680 --> 00:04:43,620 Now we will use the load load address of format string to move the i fmt int. 67 00:04:45,830 --> 00:04:46,100 Right? 68 00:04:46,110 --> 00:04:46,730 Yeah. 69 00:04:47,090 --> 00:04:49,760 Fmt it mov rsi. 70 00:04:50,990 --> 00:04:52,850 Uh, Daisy here. 71 00:04:54,100 --> 00:04:55,780 There are string. 72 00:04:56,570 --> 00:04:57,500 Start. 73 00:04:58,860 --> 00:04:59,430 Let's see. 74 00:05:02,230 --> 00:05:02,530 E. 75 00:05:02,530 --> 00:05:03,040 A. 76 00:05:03,040 --> 00:05:03,880 S. 77 00:05:03,880 --> 00:05:06,070 C, d, a c. 78 00:05:06,460 --> 00:05:08,350 And now. 79 00:05:09,620 --> 00:05:12,880 We will use load the value of result I to RDX. 80 00:05:12,890 --> 00:05:15,260 Move RDX. 81 00:05:16,190 --> 00:05:28,050 Result I here and now we will move the racks zero to clear the racks register and we will call the printf. 82 00:05:28,730 --> 00:05:30,590 And now we will need to shift. 83 00:05:30,770 --> 00:05:33,530 Use the shift arithmetic left here. 84 00:05:33,830 --> 00:05:34,820 Shift. 85 00:05:35,240 --> 00:05:39,530 Left shift our metric. 86 00:05:42,050 --> 00:05:42,560 Are. 87 00:05:45,240 --> 00:05:47,190 Arithmetic left. 88 00:05:48,000 --> 00:05:53,010 So here what we're going to do is we will load the value of number one to Rax. 89 00:05:53,010 --> 00:06:00,030 So move rax number one cell rax. 90 00:06:00,910 --> 00:06:01,480 Two. 91 00:06:01,510 --> 00:06:04,300 I will explain all of this newly. 92 00:06:07,510 --> 00:06:08,020 Here. 93 00:06:08,020 --> 00:06:09,280 So move. 94 00:06:10,050 --> 00:06:21,480 Result I here and Rex here we shift Rex left by shifting the Rex left by two bits, which is me, which 95 00:06:21,480 --> 00:06:23,760 means multiplying by four. 96 00:06:23,790 --> 00:06:35,070 Here we are storing the result in result I here and now we will need to display display the result. 97 00:06:35,100 --> 00:06:36,090 Now we are. 98 00:06:36,240 --> 00:06:36,510 We are. 99 00:06:36,660 --> 00:06:40,640 We need to load the address of format string to r.d.e. 100 00:06:40,680 --> 00:06:42,060 So move the. 101 00:06:42,980 --> 00:06:46,820 Are the I fmt integer. 102 00:06:48,390 --> 00:06:50,160 Move the RSI. 103 00:06:54,540 --> 00:06:58,290 Sally, which we defined previously in data section. 104 00:07:00,570 --> 00:07:04,740 Number one shift left to which is multiplying by four. 105 00:07:05,590 --> 00:07:18,730 And move RDX here, which is, uh, for loading the value of result result i to RDX result I to RDX. 106 00:07:18,760 --> 00:07:25,420 Now we will move the racks so we will clear the racks register which is returning the value and we will 107 00:07:25,420 --> 00:07:28,000 lastly call the printf. 108 00:07:28,030 --> 00:07:29,140 Printf. 109 00:07:30,100 --> 00:07:33,060 And after that we will need to shift arithmetic. 110 00:07:33,070 --> 00:07:37,030 We will we will need to write shift arithmetic right here. 111 00:07:37,300 --> 00:07:39,580 So shift. 112 00:07:40,920 --> 00:07:43,770 Arithmetic right now. 113 00:07:43,770 --> 00:07:44,070 We will. 114 00:07:44,070 --> 00:07:44,520 Right. 115 00:07:44,550 --> 00:07:45,360 Move. 116 00:07:47,710 --> 00:07:48,310 Move. 117 00:07:49,900 --> 00:07:59,860 Tracks, number one, which is for loading, not bomber number one, and this is for loading the value 118 00:07:59,860 --> 00:08:02,140 of number one to racks. 119 00:08:02,140 --> 00:08:11,410 So s a R for shifting racks right by two bits, which is arithmetic shift, which is this. 120 00:08:11,410 --> 00:08:24,160 This here is divided by four here shift of SA racks and two and move. 121 00:08:25,920 --> 00:08:31,560 Result I here registered result in a result I now display. 122 00:08:31,590 --> 00:08:33,720 Display the result. 123 00:08:34,020 --> 00:08:34,890 Move. 124 00:08:35,880 --> 00:08:44,400 RTI Fmt which is load which is assembly code here loads the address of format string to RTI. 125 00:08:45,970 --> 00:08:50,980 MOV rsi sa i here. 126 00:08:51,010 --> 00:08:54,280 This is followed the address of message to RSI. 127 00:08:54,700 --> 00:09:05,050 Now we will load the value of result I to RDX, move the RDX to result I here and now we will move the 128 00:09:05,050 --> 00:09:11,980 rax we will clear the rax basically call printf again print printf. 129 00:09:12,940 --> 00:09:16,210 They're calling printf to display the formatted message. 130 00:09:16,210 --> 00:09:19,930 We just need to now write the shift arithmetic. 131 00:09:19,930 --> 00:09:21,610 Right with sign expression. 132 00:09:21,880 --> 00:09:26,410 Uh, and as well we will write displaying the result as well. 133 00:09:26,410 --> 00:09:31,330 And we will write, multiply, divide and we will end the program. 134 00:09:31,330 --> 00:09:34,600 And then in next lecture we will try to analyze it. 135 00:09:34,600 --> 00:09:39,640 So there's just 1 or 2 functionalities, uh, to be written in this lecture. 136 00:09:39,640 --> 00:09:42,010 And after that we are done. 137 00:09:42,460 --> 00:09:45,590 So what we're to we have already written the shift arithmetic. 138 00:09:45,590 --> 00:09:50,420 Right now we need to write a shift arithmetic, right, with sign extension. 139 00:09:52,860 --> 00:09:53,270 Here. 140 00:09:54,500 --> 00:09:55,220 Shift. 141 00:09:56,990 --> 00:09:58,310 Arithmetic. 142 00:09:58,310 --> 00:09:59,270 Right. 143 00:10:00,000 --> 00:10:02,510 With sign extension. 144 00:10:02,520 --> 00:10:03,660 This is for now. 145 00:10:03,660 --> 00:10:08,730 We will load the value of the move. 146 00:10:11,610 --> 00:10:17,070 Now we will move racks to negative number. 147 00:10:19,030 --> 00:10:22,690 Which is for loading the value of negative number to racks. 148 00:10:23,380 --> 00:10:25,450 Remember, this is a negative number for now. 149 00:10:25,960 --> 00:10:27,400 Now we will use a. 150 00:10:28,470 --> 00:10:37,680 As a ah, this is for shifting the racks by two bits with sign extension, which is this is also called 151 00:10:37,680 --> 00:10:39,480 the arithmetic shift. 152 00:10:39,600 --> 00:10:43,170 And after that we will store the result in result. 153 00:10:43,200 --> 00:10:44,570 I result. 154 00:10:44,700 --> 00:10:45,020 Yes. 155 00:10:45,030 --> 00:10:45,450 Result. 156 00:10:45,480 --> 00:10:48,420 I basically so result. 157 00:10:50,680 --> 00:10:55,600 Racks and now we will need to display the result here. 158 00:10:55,600 --> 00:11:00,640 So move or die fmt int. 159 00:11:02,150 --> 00:11:03,020 Which is followed. 160 00:11:03,350 --> 00:11:05,220 Address of format string two. 161 00:11:05,750 --> 00:11:08,450 Now we will move the RSI to. 162 00:11:08,490 --> 00:11:09,110 Sorry. 163 00:11:09,530 --> 00:11:10,490 This is for load. 164 00:11:10,490 --> 00:11:13,640 The address of address of message to RSI. 165 00:11:14,340 --> 00:11:18,380 Uh, remember, we have defined something like, uh, here. 166 00:11:18,410 --> 00:11:19,550 Number one shift. 167 00:11:19,550 --> 00:11:19,970 Right? 168 00:11:19,970 --> 00:11:23,690 Two dividing by four with sign expression. 169 00:11:24,730 --> 00:11:24,990 It. 170 00:11:25,140 --> 00:11:29,860 It will understand it better when we create the output for this run the program. 171 00:11:30,250 --> 00:11:31,690 And after that. 172 00:11:33,290 --> 00:11:34,370 Uh, we need to. 173 00:11:35,810 --> 00:11:39,980 Lord the value of result I to move. 174 00:11:41,050 --> 00:11:50,740 MOV rdx is result i here and move now we will move the rax which is zero. 175 00:11:50,740 --> 00:11:57,220 We will clear the rax register and we will call the printf as we always do. 176 00:11:57,400 --> 00:12:02,230 And now we will also multiply it, divide it and that's it. 177 00:12:02,230 --> 00:12:08,260 So multiplication will be much easier than division and you will see why. 178 00:12:08,650 --> 00:12:11,350 So multiply. 179 00:12:11,380 --> 00:12:12,640 So move. 180 00:12:13,540 --> 00:12:14,450 Racks. 181 00:12:16,050 --> 00:12:17,040 Number one. 182 00:12:17,040 --> 00:12:19,920 Here we are loading the value of number one to racks. 183 00:12:19,950 --> 00:12:23,580 We will email here, use email keyword. 184 00:12:24,570 --> 00:12:25,860 Number two. 185 00:12:26,070 --> 00:12:26,590 Here we are. 186 00:12:26,610 --> 00:12:32,190 Multiply we are telling the assembler to multiply rax with the value of number two, which is this is 187 00:12:32,190 --> 00:12:40,230 the 64 bit multiplication and we will use the move here to store the result in result. 188 00:12:40,260 --> 00:12:47,370 I in order to do that we will use the result I here and after that we will tell rax and now we will 189 00:12:47,370 --> 00:12:49,500 need to display the result here. 190 00:12:49,500 --> 00:12:56,340 Display the result mov rda you already know how to display the result here. 191 00:12:56,490 --> 00:12:59,400 Fmt int mov rsi. 192 00:13:01,980 --> 00:13:03,600 MOV RDX. 193 00:13:05,370 --> 00:13:09,790 Result II which is loading the value of result I to RDX. 194 00:13:09,810 --> 00:13:17,400 And now we will clear the register which is returning the value and we will call the print F. 195 00:13:17,670 --> 00:13:18,410 So that's it. 196 00:13:18,420 --> 00:13:22,140 Now we will write our division divide. 197 00:13:22,230 --> 00:13:25,920 Now we will call the move. 198 00:13:28,870 --> 00:13:29,740 Number one. 199 00:13:29,740 --> 00:13:34,960 Here we are loading the value of number one to racks. 200 00:13:34,960 --> 00:13:37,330 And now we will call the. 201 00:13:40,670 --> 00:13:50,120 MOV rdx zero clearing the rdx register, which is a reminder and i div here. 202 00:13:51,410 --> 00:13:52,340 He was. 203 00:13:53,360 --> 00:13:54,050 And. 204 00:13:55,760 --> 00:13:56,660 Award. 205 00:13:57,940 --> 00:13:59,350 Number two. 206 00:14:04,630 --> 00:14:13,090 And after that, with this here we are telling the assembler assembly to divide the racks by the value 207 00:14:13,090 --> 00:14:17,740 of number two, which is a 64 bit, uh, division. 208 00:14:17,740 --> 00:14:23,650 And this is the equation in racks reminder in RDX. 209 00:14:23,650 --> 00:14:26,140 And we will use the move here. 210 00:14:28,560 --> 00:14:29,340 Move. 211 00:14:29,370 --> 00:14:30,510 Result. 212 00:14:31,440 --> 00:14:32,220 Result. 213 00:14:32,250 --> 00:14:42,150 I Rex, this is the this is for storing the quotient quotient of in result I and now we will also modulo 214 00:14:42,480 --> 00:14:43,830 modulo here. 215 00:14:43,830 --> 00:14:49,710 This is for storing the reminder or restoring the reminder in modulo. 216 00:14:49,710 --> 00:14:51,300 Here are the x. 217 00:14:51,300 --> 00:14:54,540 Now we will need to display the quotient quotient. 218 00:14:54,750 --> 00:15:06,420 So display the quotient and now we will need to load the address of format string to move the the i 219 00:15:06,540 --> 00:15:10,590 fmt int mov rsi TV. 220 00:15:10,590 --> 00:15:15,750 This is for loading the address of message to RSI and move the. 221 00:15:18,800 --> 00:15:19,490 Now we are. 222 00:15:21,130 --> 00:15:23,770 RSI side, the X here. 223 00:15:24,010 --> 00:15:28,580 The result here is not the value of result to RDX. 224 00:15:28,750 --> 00:15:31,660 Now move the rax zero. 225 00:15:31,660 --> 00:15:34,450 Clear the rax register and call the. 226 00:15:35,620 --> 00:15:36,470 Printf. 227 00:15:37,480 --> 00:15:39,880 And now we need to display the reminder. 228 00:15:40,000 --> 00:15:42,790 And that's all display. 229 00:15:44,920 --> 00:15:46,150 Display the. 230 00:15:47,270 --> 00:15:48,410 Reminder. 231 00:15:48,620 --> 00:15:51,710 And here we will do the same logic here. 232 00:15:51,740 --> 00:15:57,260 Move by Fmt and move. 233 00:15:57,940 --> 00:16:03,550 RSI REM and mov rdx modulo. 234 00:16:06,410 --> 00:16:09,600 And after that, we will move the racks. 235 00:16:09,650 --> 00:16:10,340 Zero. 236 00:16:10,610 --> 00:16:13,480 And after that, we will call the print f. 237 00:16:14,970 --> 00:16:16,590 To display the formatted message. 238 00:16:16,590 --> 00:16:19,270 And now we need to write three instructions as well. 239 00:16:19,290 --> 00:16:25,050 Move rsp rbp for restoring the stack pointer. 240 00:16:25,080 --> 00:16:26,220 Pop Rbp. 241 00:16:28,140 --> 00:16:33,900 For restoring the bass pointer and we will return from the, uh, main function.