WEBVTT

00:00.320 --> 00:06.320
Hello, my name is Typhoon and this is the last lecture of our section, and many arithmetic instructions

00:06.320 --> 00:10.040
are available, but we are going to show a selection of them.

00:10.040 --> 00:13.700
And the others are similar to what you learned here.

00:13.700 --> 00:21.770
So before we investigate arithmetic instructions and keep in mind that we use the printf with more than

00:21.770 --> 00:24.680
two arguments, so we need an additional register.

00:26.410 --> 00:27.310
The first one.

00:27.310 --> 00:31.240
The first argument goes into RTI.

00:31.660 --> 00:38.720
The second one into RSI and the third one into RDX.

00:38.770 --> 00:46.720
So this is how Printf expects us to provide the arguments in Linux and you will learn more about that

00:46.720 --> 00:53.770
later when we talk about the calling conventions, which you will learn that in next sections.

00:54.010 --> 00:57.160
And here there is a instructions here.

00:57.160 --> 01:03.640
The first instruction is Add, which can be used to add signed or unsigned integers.

01:03.730 --> 01:09.250
The second operand source is added to the first operand destination and the result is placed in the

01:09.250 --> 01:11.050
first operand destination here.

01:11.050 --> 01:14.800
And the destination operand can be a register or a memory location.

01:14.800 --> 01:19.870
So the source can be an immediate value, a register or a memory location.

01:19.870 --> 01:25.840
The source and destination cannot be a memory location in the same instruction, and when the resulting

01:25.840 --> 01:33.260
sum is too large to fit in the destination, the F flag is set for signed integers.

01:33.260 --> 01:37.280
So for unsigned integer, the o f flag is then set.

01:37.280 --> 01:38.000
So.

01:38.630 --> 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

01:48.530 --> 01:49.190
set.

01:52.210 --> 01:57.340
And the subtraction with sub is similar to the Add instruction.

01:58.380 --> 02:03.480
Uh, we can see it right here, adding and subtracting.

02:03.750 --> 02:05.670
So here the.

02:06.860 --> 02:14.120
To increment a register or a value in a memory location with one use the I and C.

02:14.150 --> 02:14.900
Instructions.

02:14.900 --> 02:22.700
Similarly, the a C can be used to decrement a register or value in a memory.

02:22.760 --> 02:24.110
Location with one.

02:24.110 --> 02:27.860
So the arithmetic shift instructions are a special breed.

02:27.860 --> 02:34.400
So here you will see that at the end of the code here somewhere here.

02:34.400 --> 02:38.990
So here if you shift left here.

02:38.990 --> 02:43.160
So the shift left is Sal is in fact multiplying.

02:43.190 --> 02:47.270
So if you shift left one position, you are multiplying by two.

02:47.300 --> 02:49.310
Every bit is shifted.

02:49.310 --> 02:53.690
One place to the left and zero is added to the right.

02:53.690 --> 03:02.720
So take the binary number one shift left one place and you obtain binary 10 or 2 in decimal representation.

03:02.720 --> 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

03:11.670 --> 03:15.180
zero 0 or 4 in decimal representation.

03:15.180 --> 03:19.140
So if you shift left two position, you multiply by four.

03:19.170 --> 03:22.560
But what if you want to multiply by six?

03:22.560 --> 03:31.230
So you shift left two times and then add two times the original source in that order.

03:31.350 --> 03:34.290
So shift right as r.

03:34.290 --> 03:36.300
S a r here.

03:36.950 --> 03:41.180
Uh, is similar to a shift left, but it means dividing by two.

03:41.330 --> 03:47.920
So every bit is shifted one place to the right and an additional bit is added to the left.

03:47.930 --> 03:48.740
So.

03:49.790 --> 03:50.180
Here.

03:50.180 --> 03:51.620
There is a complication.

03:51.620 --> 03:58.910
However, if the original value was negative, the leftmost bit would be one, and if the shift instruction

03:58.910 --> 04:06.950
added two added a zero, the bit at the left, the value would become positive and the result would

04:06.950 --> 04:07.760
be wrong.

04:07.760 --> 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,

04:16.490 --> 04:21.140
zero bits will be added to the left and this is called sign extension.

04:21.140 --> 04:28.460
So by the way, a quick way to see if a hexadecimal number is negative is to look at byte seven.

04:28.460 --> 04:33.620
This is the leftmost byte continuing from byte zero, which is the rightmost byte.

04:33.620 --> 04:34.970
So the number is negative.

04:34.970 --> 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

04:45.470 --> 04:48.380
all eight bytes.

04:48.380 --> 04:51.720
For example, the byte zero x.

04:52.290 --> 04:53.340
Uh, the device.

04:53.340 --> 05:00.930
The 12 is still a positive number because the leftmost byte, which is not shown here, is zero.

05:00.930 --> 05:03.450
And there's also no arithmetic shift instructions.

05:03.450 --> 05:06.120
They will be discussed in next lecture.

05:06.330 --> 05:14.990
And next here we multiply integers for multiplying the unsigned integers you can use mul here.

05:15.210 --> 05:21.390
Um, now we will hear, uh, mul for unsigned multiplication.

05:21.390 --> 05:24.420
So I think, uh, so the in the product shift.

05:24.420 --> 05:24.980
Right.

05:24.990 --> 05:26.730
Increment number.

05:27.780 --> 05:30.300
Total integer quotient and product.

05:30.300 --> 05:30.870
Yes.

05:35.860 --> 05:41.810
And here but we use the I'm u l.

05:41.860 --> 05:49.570
This is assigned multiple action which offers more flexibility so I can take one, 2 or 3 operands.

05:49.570 --> 05:54.640
And in our example we use one operand, as you can see here.

05:55.800 --> 05:58.500
And here.

06:00.510 --> 06:08.250
And the as I said in our example, we use this one operand and the operand following the I mul instruction.

06:08.300 --> 06:14.490
I mul instruction is multiplied with the value in rax right.

06:14.490 --> 06:20.400
So you may expect that the resulting product is stored in Rax, but that is not entirely correct.

06:20.400 --> 06:22.650
So let's illustrate an example here.

06:22.650 --> 06:28.110
So you can verify that when you multiply, for example, a two digit number with a three digit number,

06:28.140 --> 06:31.380
the product has 4 or 5 digits.

06:31.380 --> 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

06:40.560 --> 06:40.860
digit.

06:40.860 --> 06:44.730
And that value does not fit in 64 bit register.

06:44.730 --> 06:45.030
Right.

06:45.030 --> 06:52.950
So to cope with this, the instruction I moved here Am will store the lower 64 bits of the resulting

06:52.950 --> 06:58.530
product in Rax and upper 64 bits in RDX.

06:58.530 --> 07:00.520
And this can be very deceptive.

07:00.520 --> 07:03.070
Okay, so now let's experiment a little bit here.

07:03.070 --> 07:10.810
So go back to the source code here, modify the number one so that it contains here.

07:12.110 --> 07:13.070
Number one.

07:13.070 --> 07:14.300
One, two.

07:14.980 --> 07:16.210
Three, four.

07:16.210 --> 07:20.260
One, 234567890.

07:20.260 --> 07:20.890
And again.

07:20.890 --> 07:23.230
One, two, three, four, five, six, seven.

07:24.010 --> 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

07:34.870 --> 07:38.470
you can check the here you would.

07:39.870 --> 07:45.270
As you can see, we got an error, but missing node Unix like implies executable stack.

07:47.510 --> 07:50.780
Yeah, this is just a warning and not an error here.

07:50.780 --> 07:55.730
And as you can see here, we got very different result here.

07:57.710 --> 07:59.690
And if it started the budget.

08:03.620 --> 08:13.130
And in order to debug this, we need to we will put a break before the I mole instruction.

08:14.410 --> 08:16.000
The started here.

08:16.000 --> 08:25.330
And we will, uh, here we will put a break before the I instruction here, which is for the multiplying.

08:25.360 --> 08:26.110
That's it.

08:26.830 --> 08:27.730
Now.

08:29.060 --> 08:29.510
Let's go.

08:29.510 --> 08:32.810
And here we stopped it now.

08:33.600 --> 08:33.990
Ugh.

08:34.020 --> 08:34.710
Now.

08:36.030 --> 08:40.200
The result of the multiplication here will be.

08:43.720 --> 08:48.910
That's actually and that's the result of the multiple action here will be this number.

08:48.910 --> 08:54.520
And as you can see in racks after the instruction is executed.

08:54.610 --> 08:55.240
Right.

08:56.610 --> 08:58.440
Uh, the new modified.

08:58.640 --> 09:06.000
Now, uh, actually, let's actually modify number two into 10,000.

09:07.300 --> 09:07.640
Obsession.

09:07.700 --> 09:09.610
Let's stop the debugging.

09:11.210 --> 09:13.070
It still have that breakpoint here?

09:13.940 --> 09:14.990
One, two, three.

09:15.020 --> 09:17.420
Now it's in 10,000.

09:17.840 --> 09:18.920
The bugging is started.

09:18.920 --> 09:20.720
Let's go to email here.

09:20.840 --> 09:28.670
And with this, um, after, uh, of course, after setting the restart, after setting the 10,000 in

09:28.670 --> 09:31.970
number to stop the debugging and then start it again.

09:32.820 --> 09:40.320
And after look after the racks after executing moved here like this.

09:41.150 --> 09:43.040
And here.

09:43.100 --> 09:43.600
Um.

09:45.740 --> 09:52.240
So you can see that the product is a large negative value here, right?

09:52.250 --> 09:57.290
So this is because the most significant bit in Rax is a one.

09:57.290 --> 10:02.220
And CSM concludes that this must be a negative number.

10:02.240 --> 10:09.500
Also, Printf thinks that Rax contains a negative number because Rax contains a one bit in the leftmost

10:09.500 --> 10:12.430
position, so it's assumed to be negative.

10:12.440 --> 10:15.140
So be careful with printf.
