1 00:00:00,320 --> 00:00:06,320 Hello, my name is Typhoon and this is the last lecture of our section, and many arithmetic instructions 2 00:00:06,320 --> 00:00:10,040 are available, but we are going to show a selection of them. 3 00:00:10,040 --> 00:00:13,700 And the others are similar to what you learned here. 4 00:00:13,700 --> 00:00:21,770 So before we investigate arithmetic instructions and keep in mind that we use the printf with more than 5 00:00:21,770 --> 00:00:24,680 two arguments, so we need an additional register. 6 00:00:26,410 --> 00:00:27,310 The first one. 7 00:00:27,310 --> 00:00:31,240 The first argument goes into RTI. 8 00:00:31,660 --> 00:00:38,720 The second one into RSI and the third one into RDX. 9 00:00:38,770 --> 00:00:46,720 So this is how Printf expects us to provide the arguments in Linux and you will learn more about that 10 00:00:46,720 --> 00:00:53,770 later when we talk about the calling conventions, which you will learn that in next sections. 11 00:00:54,010 --> 00:00:57,160 And here there is a instructions here. 12 00:00:57,160 --> 00:01:03,640 The first instruction is Add, which can be used to add signed or unsigned integers. 13 00:01:03,730 --> 00:01:09,250 The second operand source is added to the first operand destination and the result is placed in the 14 00:01:09,250 --> 00:01:11,050 first operand destination here. 15 00:01:11,050 --> 00:01:14,800 And the destination operand can be a register or a memory location. 16 00:01:14,800 --> 00:01:19,870 So the source can be an immediate value, a register or a memory location. 17 00:01:19,870 --> 00:01:25,840 The source and destination cannot be a memory location in the same instruction, and when the resulting 18 00:01:25,840 --> 00:01:33,260 sum is too large to fit in the destination, the F flag is set for signed integers. 19 00:01:33,260 --> 00:01:37,280 So for unsigned integer, the o f flag is then set. 20 00:01:37,280 --> 00:01:38,000 So. 21 00:01:38,630 --> 00:01:48,530 When the result is zero, the z f flag is set to one and when the result is negative, the F flag is 22 00:01:48,530 --> 00:01:49,190 set. 23 00:01:52,210 --> 00:01:57,340 And the subtraction with sub is similar to the Add instruction. 24 00:01:58,380 --> 00:02:03,480 Uh, we can see it right here, adding and subtracting. 25 00:02:03,750 --> 00:02:05,670 So here the. 26 00:02:06,860 --> 00:02:14,900 To increment a register or a value in a memory location with one use the I and C instruction. 27 00:02:14,900 --> 00:02:22,700 Similarly, the a C can be used to decrement a register or value in a memory. 28 00:02:22,760 --> 00:02:24,110 Location with one. 29 00:02:24,110 --> 00:02:27,860 So the arithmetic shift instructions are a special breed. 30 00:02:27,860 --> 00:02:34,400 So here you will see that at the end of the code here somewhere here. 31 00:02:34,400 --> 00:02:38,990 So here if you shift left here. 32 00:02:38,990 --> 00:02:43,160 So the shift left is Sal is in fact multiplying. 33 00:02:43,190 --> 00:02:47,270 So if you shift left one position, you are multiplying by two. 34 00:02:47,300 --> 00:02:49,310 Every bit is shifted. 35 00:02:49,310 --> 00:02:53,690 One place to the left and zero is added to the right. 36 00:02:53,690 --> 00:03:02,720 So take the binary number one shift left one place and you obtain binary 10 or 2 in decimal representation. 37 00:03:02,720 --> 00:03:11,670 So you are getting from 0 to 2 and shift one shift left, one place again and you have a binary one 38 00:03:11,670 --> 00:03:15,180 zero 0 or 4 in decimal representation. 39 00:03:15,180 --> 00:03:19,140 So if you shift left two position, you multiply by four. 40 00:03:19,170 --> 00:03:22,560 But what if you want to multiply by six? 41 00:03:22,560 --> 00:03:31,230 So you shift left two times and then add two times the original source in that order. 42 00:03:31,350 --> 00:03:36,300 So shift right as our R here. 43 00:03:36,950 --> 00:03:41,180 Uh, is similar to a shift left, but it means dividing by two. 44 00:03:41,330 --> 00:03:47,920 So every bit is shifted one place to the right and an additional bit is added to the left. 45 00:03:47,930 --> 00:03:48,740 So. 46 00:03:49,790 --> 00:03:50,180 Here. 47 00:03:50,180 --> 00:03:51,620 There is a complication. 48 00:03:51,620 --> 00:03:58,910 However, if the original value was negative, the leftmost bit would be one, and if the shift instruction 49 00:03:58,910 --> 00:04:06,950 added two added a zero, the bit at the left, the value would become positive and the result would 50 00:04:06,950 --> 00:04:07,760 be wrong. 51 00:04:07,760 --> 00:04:16,460 So in the case of negative value as a R will add a one bit to the left and in the case of positive value, 52 00:04:16,490 --> 00:04:21,140 zero bits will be added to the left and this is called sign extension. 53 00:04:21,140 --> 00:04:28,460 So by the way, a quick way to see if a hexadecimal number is negative is to look at byte seven. 54 00:04:28,460 --> 00:04:33,620 This is the leftmost byte continuing from byte zero, which is the rightmost byte. 55 00:04:33,620 --> 00:04:34,970 So the number is negative. 56 00:04:34,970 --> 00:04:45,440 If byte zero starts with an eight, nine, A, B, C, D, E, or F, but you need to take into account 57 00:04:45,470 --> 00:04:48,380 all eight bytes. 58 00:04:48,380 --> 00:04:51,720 For example, the byte zero x. 59 00:04:52,290 --> 00:04:53,340 Uh, the device. 60 00:04:53,340 --> 00:05:00,930 The 12 is still a positive number because the leftmost byte, which is not shown here, is zero. 61 00:05:00,930 --> 00:05:03,450 And there's also no arithmetic shift instructions. 62 00:05:03,450 --> 00:05:06,120 They will be discussed in next lecture. 63 00:05:06,330 --> 00:05:14,990 And next here we multiply integers for multiplying the unsigned integers you can use mul here. 64 00:05:15,210 --> 00:05:21,390 Um, now we will hear, uh, mul for unsigned multiplication. 65 00:05:21,390 --> 00:05:24,420 So I think, uh, so the in the product shift. 66 00:05:24,420 --> 00:05:24,980 Right. 67 00:05:24,990 --> 00:05:26,730 Increment number. 68 00:05:27,780 --> 00:05:30,300 Total integer quotient and product. 69 00:05:30,300 --> 00:05:30,870 Yes. 70 00:05:35,860 --> 00:05:41,830 And here but we use the I'm u l. 71 00:05:41,860 --> 00:05:49,570 This is assigned multiple action which offers more flexibility so I can take one, 2 or 3 operands. 72 00:05:49,570 --> 00:05:54,640 And in our example we use one operand, as you can see here. 73 00:05:55,800 --> 00:05:58,500 And here. 74 00:06:00,510 --> 00:06:08,250 And the as I said in our example, we use this one operand and the operand following the I mul instruction. 75 00:06:08,300 --> 00:06:14,490 I mul instruction is multiplied with the value in rax right. 76 00:06:14,490 --> 00:06:20,400 So you may expect that the resulting product is stored in Rax, but that is not entirely correct. 77 00:06:20,400 --> 00:06:22,650 So let's illustrate an example here. 78 00:06:22,650 --> 00:06:28,110 So you can verify that when you multiply, for example, a two digit number with a three digit number, 79 00:06:28,140 --> 00:06:31,380 the product has 4 or 5 digits. 80 00:06:31,380 --> 00:06:40,560 So when you multiply 4 to 8 bit digit with 30 bit digit, you will obtain 77, 77 bit digit or 78 bit 81 00:06:40,560 --> 00:06:40,860 digit. 82 00:06:40,860 --> 00:06:44,730 And that value does not fit in 64 bit register. 83 00:06:44,730 --> 00:06:45,030 Right. 84 00:06:45,030 --> 00:06:52,950 So to cope with this, the instruction I moved here Am will store the lower 64 bits of the resulting 85 00:06:52,950 --> 00:06:58,530 product in Rax and upper 64 bits in RDX. 86 00:06:58,530 --> 00:07:00,520 And this can be very deceptive. 87 00:07:00,520 --> 00:07:03,070 Okay, so now let's experiment a little bit here. 88 00:07:03,070 --> 00:07:10,810 So go back to the source code here, modify the number one so that it contains here. 89 00:07:12,110 --> 00:07:13,070 Number one. 90 00:07:13,070 --> 00:07:14,300 One, two. 91 00:07:14,980 --> 00:07:16,210 Three, four. 92 00:07:16,210 --> 00:07:20,260 One, 234567890. 93 00:07:20,260 --> 00:07:20,890 And again. 94 00:07:20,890 --> 00:07:23,230 One, two, three, four, five, six, seven. 95 00:07:24,010 --> 00:07:34,870 That's it yeah seven and now and modify the number 2 to 100 and the product will just fit in racks so 96 00:07:34,870 --> 00:07:38,470 you can check the here you would. 97 00:07:39,870 --> 00:07:45,270 As you can see, we got an error, but missing node Unix like implies executable stack. 98 00:07:47,510 --> 00:07:50,780 Yeah, this is just a warning and not an error here. 99 00:07:50,780 --> 00:07:55,730 And as you can see here, we got very different result here. 100 00:07:57,710 --> 00:07:59,690 And if it started the budget. 101 00:08:03,620 --> 00:08:13,130 And in order to debug this, we need to we will put a break before the I mole instruction. 102 00:08:14,410 --> 00:08:16,000 The started here. 103 00:08:16,000 --> 00:08:25,330 And we will, uh, here we will put a break before the I instruction here, which is for the multiplying. 104 00:08:25,360 --> 00:08:26,110 That's it. 105 00:08:26,830 --> 00:08:27,730 Now. 106 00:08:29,060 --> 00:08:29,510 Let's go. 107 00:08:29,510 --> 00:08:32,810 And here we stopped it now. 108 00:08:33,600 --> 00:08:33,990 Ugh. 109 00:08:34,020 --> 00:08:34,710 Now. 110 00:08:36,030 --> 00:08:40,200 The result of the multiplication here will be. 111 00:08:43,720 --> 00:08:48,910 That's actually and that's the result of the multiple action here will be this number. 112 00:08:48,910 --> 00:08:54,520 And as you can see in racks after the instruction is executed. 113 00:08:54,610 --> 00:08:55,240 Right. 114 00:08:56,610 --> 00:08:58,440 Uh, the new modified. 115 00:08:58,640 --> 00:09:06,000 Now, uh, actually, let's actually modify number two into 10,000. 116 00:09:07,300 --> 00:09:07,650 Obsession. 117 00:09:07,700 --> 00:09:09,610 Let's stop the debugging. 118 00:09:11,210 --> 00:09:13,070 It still have that breakpoint here? 119 00:09:13,940 --> 00:09:14,990 One, two, three. 120 00:09:15,020 --> 00:09:17,420 Now it's in 10,000. 121 00:09:17,840 --> 00:09:18,920 The bugging is started. 122 00:09:18,920 --> 00:09:20,720 Let's go to email here. 123 00:09:20,840 --> 00:09:28,670 And with this, um, after, uh, of course, after setting the restart, after setting the 10,000 in 124 00:09:28,670 --> 00:09:31,970 number to stop the debugging and then start it again. 125 00:09:32,820 --> 00:09:40,320 And after look after the racks after executing moved here like this. 126 00:09:41,150 --> 00:09:43,040 And here. 127 00:09:43,100 --> 00:09:43,640 Um. 128 00:09:45,740 --> 00:09:52,240 So you can see that the product is a large negative value here, right? 129 00:09:52,250 --> 00:09:57,290 So this is because the most significant bit in Rax is a one. 130 00:09:57,290 --> 00:10:02,220 And CSM concludes that this must be a negative number. 131 00:10:02,240 --> 00:10:09,500 Also, Printf thinks that Rax contains a negative number because Rax contains a one bit in the leftmost 132 00:10:09,500 --> 00:10:12,430 position, so it's assumed to be negative. 133 00:10:12,440 --> 00:10:15,140 So be careful with printf.