WEBVTT

00:00.440 --> 00:05.330
Hello, my name is Stephan, and in this lecture we will embark on a journey into the realm of assembly

00:05.330 --> 00:11.270
programming, and our focus will be on understanding conditional jumps and branching.

00:11.330 --> 00:12.530
Crucial concepts.

00:13.530 --> 00:15.060
Low level programming.

00:15.060 --> 00:21.870
And to illustrate this and we will dissect the code that called the jumped ASM.

00:21.900 --> 00:27.690
Actually, we also develop it and I will explain this code line by line.

00:27.720 --> 00:31.650
So here, let's open our CSM project.

00:32.040 --> 00:35.010
The project here.

00:35.620 --> 00:37.780
Uh, we will start from zero.

00:38.130 --> 00:42.580
Uh, let's go to assembler here, create a new file.

00:42.580 --> 00:45.370
Let's jump here.

00:46.220 --> 00:47.420
And save it.

00:49.850 --> 00:52.220
Jump jumping, that is.

00:54.310 --> 01:01.210
And here, let's actually, uh, we will jump jumping that Assam at the comment here.

01:01.240 --> 01:03.270
Jumping at Assam.

01:03.400 --> 01:06.490
Exploring conditional jumps.

01:06.670 --> 01:08.620
Conditional jumps.

01:09.160 --> 01:13.330
And here we will create the extern printf.

01:13.420 --> 01:17.560
This is for declaring the external function.

01:17.650 --> 01:23.680
Uh, declare the external function printf.

01:24.250 --> 01:29.260
And here we will create a section data here with tabs.

01:29.260 --> 01:41.230
Here we will create a number one peak here 42 because this we are here defining a variable number one

01:41.980 --> 01:45.130
with the value of 42.

01:46.840 --> 01:47.380
42.

01:47.380 --> 01:50.110
And here we will also declare.

01:50.410 --> 01:52.620
Define a variable.

01:52.630 --> 01:59.170
Number two with the value of 41 here.

01:59.720 --> 02:03.230
So defining a variable.

02:03.230 --> 02:08.600
Number two with the path, the value.

02:10.020 --> 02:11.340
Value of.

02:12.340 --> 02:14.140
With the value of 41.

02:15.720 --> 02:22.200
And also we'll create the format string for comparison result one.

02:22.200 --> 02:28.560
And we will also write our code for format string for comparison result two here.

02:28.560 --> 02:29.460
So fmt.

02:30.930 --> 02:36.270
If empty one DB here a number.

02:38.850 --> 02:39.750
Number.

02:39.930 --> 02:43.560
Number one here and.

02:47.130 --> 02:48.210
Number two.

02:50.050 --> 03:00.550
So this will recreate your code like this because it's actually prints like equal or greater and equal,

03:00.580 --> 03:02.230
but it actually shows like this.

03:02.230 --> 03:05.470
So this is easier and more understandable to view here.

03:05.470 --> 03:09.580
But if we make this here and as you can see here, these are the separate characters.

03:09.760 --> 03:19.500
This shows this together, uh, to one as of one symbol shows that this is the operand, right?

03:19.510 --> 03:25.600
So logic, operand, operand and here ten and zero.

03:25.630 --> 03:26.920
So here we are.

03:26.920 --> 03:28.990
Format, string.

03:33.050 --> 03:36.230
Format string for comparison.

03:38.060 --> 03:40.580
The result one.

03:40.580 --> 03:43.940
And also we will create the form of two Again.

03:43.950 --> 03:51.410
DB here number one is less than number, one less.

03:53.070 --> 03:55.740
And, uh, number two.

03:55.740 --> 03:56.700
Number two.

03:56.730 --> 03:58.470
Here again, we.

04:02.920 --> 04:04.900
This is for formatting.

04:05.110 --> 04:06.940
Format String.

04:07.690 --> 04:09.610
For comparison.

04:09.610 --> 04:10.930
Result two.

04:11.350 --> 04:16.300
And here we start with the basics.

04:16.450 --> 04:23.500
We are declaring the printf function that will help us display messages later.

04:23.500 --> 04:27.100
And in this data section we define two variables.

04:27.400 --> 04:32.200
Number one and number two which and format strings.

04:32.210 --> 04:34.570
Also Fmt one.

04:34.960 --> 04:40.590
Fmt two to prepare for our output messages.

04:40.600 --> 04:49.750
And here we will also create the section text right now, section text Global Main.

04:49.750 --> 04:57.760
We are declaring this for the global entry point of the program main here and that's it.

04:57.760 --> 05:02.710
So now we will pass the beep here and after that.

05:05.700 --> 05:07.470
It will save this.

05:12.380 --> 05:14.180
Leaving the base pointer.

05:16.120 --> 05:18.400
Maybe the base pointer here.

05:18.550 --> 05:19.270
Move.

05:21.530 --> 05:21.810
Khabib.

05:21.890 --> 05:23.190
Actually, let's do this.

05:32.520 --> 05:33.160
RB.

05:39.290 --> 05:41.060
And also RSP.

05:41.720 --> 05:43.310
Now with this.

05:47.280 --> 05:48.750
We are set.

05:49.020 --> 05:52.220
Set up the base pointer.

05:53.100 --> 06:01.320
And here, moving on this text section, we declare the global main function, which marks the entry

06:01.320 --> 06:03.780
point of our program.

06:04.050 --> 06:08.160
And we are also setting up the stack frame with the path.

06:09.710 --> 06:09.920
Uh.

06:11.190 --> 06:20.650
And move rwp rsp instructions ensuring that we have a proper execution environment.

06:20.670 --> 06:23.530
And now let's proceed with this.

06:23.580 --> 06:26.670
Here we will move the racks.

06:27.120 --> 06:27.870
Move.

06:29.700 --> 06:31.730
Tracks here.

06:31.830 --> 06:40.260
The number one here we are loading the value of number one into the register racks.

06:41.310 --> 06:44.460
But the value of.

06:45.540 --> 06:54.150
Of number one into register racks and move rb x rb x.

06:54.990 --> 06:57.510
We will write number two.

06:57.720 --> 07:06.030
Here we are loading the load, the value of number two into a register.

07:06.920 --> 07:08.770
RBCs again.

07:09.320 --> 07:18.520
And here we start loading by values of number one and number two into registers.

07:18.530 --> 07:22.520
The the racks here now holds value of.

07:23.650 --> 07:29.660
42, and RBCs contains the value of 41.

07:29.680 --> 07:30.100
Right.

07:30.100 --> 07:32.590
So these registers are.

07:33.280 --> 07:35.770
Where we will perform comparisons.

07:36.100 --> 07:40.960
Now here we will compare the values in Rax and.

07:42.190 --> 07:45.460
In order to do that, we will use this instruction.

07:45.670 --> 07:46.450
So.

07:47.370 --> 07:49.910
Racks and RBCs.

07:50.100 --> 07:52.560
And after that, here.

07:55.480 --> 08:02.650
ING cmp for comparing the values in RACs and RBC's.

08:02.650 --> 08:05.080
And here we will use the.

08:08.510 --> 08:09.440
He here?

08:10.430 --> 08:11.810
As greater.

08:14.050 --> 08:15.460
Now with this.

08:15.460 --> 08:17.200
Here we are.

08:17.740 --> 08:18.850
Uh, jump.

08:19.380 --> 08:29.360
To greater if Rax is greater than or equal to rb x.

08:30.130 --> 08:32.410
Um, and next we use this.

08:32.410 --> 08:36.100
Um, here again, I will explain this again from zero.

08:36.280 --> 08:44.830
So this we are using this CPAp instruction to compare the values in racks and as I explained here.

08:44.830 --> 08:50.860
So the interesting part is here this instruction.

08:50.860 --> 08:58.720
So if racks is greater than or equal to RB x, we jump to a greater label.

08:58.720 --> 09:03.790
Otherwise we continue with the next instruction here.

09:04.710 --> 09:05.540
The chairs.

09:05.580 --> 09:09.210
We will load the format string for the second message.

09:09.210 --> 09:10.290
So move.

09:13.510 --> 09:16.000
Audi Fm2.

09:18.120 --> 09:22.050
This is for loading the format string.

09:23.640 --> 09:28.890
Praying for for the second message move.

09:31.740 --> 09:32.220
Zero.

09:34.430 --> 09:34.790
Zero.

09:34.790 --> 09:35.720
This is.

09:36.970 --> 09:40.270
This is for clearing racks here.

09:40.450 --> 09:45.010
No xmm registers in.

09:46.090 --> 09:48.310
All here.

09:49.420 --> 09:51.760
And we will call the print.

09:53.840 --> 09:54.470
Grand theft.

09:59.210 --> 10:01.560
This is this calls the printf.

10:05.480 --> 10:06.800
Number one.

10:07.070 --> 10:08.420
Number one.

10:08.720 --> 10:11.720
Less than number two.

10:12.690 --> 10:13.980
And GMP.

10:16.280 --> 10:18.170
BMP exit.

10:18.410 --> 10:21.350
So here this jump.

10:22.220 --> 10:24.220
To the table.

10:24.300 --> 10:30.020
So if the comparison condition didn't met here.

10:30.700 --> 10:32.260
Uh, wasn't or wasn't mad.

10:32.310 --> 10:42.160
Uh, we moved here, so we load the format, string Fmt into the RDA register and we clear rax since

10:42.190 --> 10:46.300
no error mem registers are involved here.

10:47.260 --> 10:50.140
Um or XMM registers involved here.

10:50.140 --> 10:55.060
And we then call the printf function to display the number.

10:55.090 --> 10:58.450
One is less than number two.

10:59.110 --> 11:09.400
Following that, we perform unconditional jump to the x label here and now we will use this greeter

11:09.400 --> 11:10.390
function here.

11:15.010 --> 11:16.370
And that's it.

11:16.390 --> 11:17.350
Move.

11:18.590 --> 11:21.720
R the i fmt one.

11:21.740 --> 11:22.780
Here we are.

11:22.790 --> 11:26.510
Load loading the format string.

11:27.620 --> 11:30.040
Spring for the first.

11:31.060 --> 11:34.780
Message and again, move racks.

11:35.590 --> 11:36.220
Zero.

11:36.550 --> 11:39.880
And here we are clearing clear the Or.

11:40.000 --> 11:40.230
Yeah.

11:40.240 --> 11:43.900
Clear the racks again and we will.

11:43.900 --> 11:44.560
We are.

11:44.590 --> 11:47.040
We will call the print.

11:47.050 --> 11:47.590
Print.

11:55.860 --> 11:56.600
Here we are.

11:56.610 --> 12:00.360
Call call printf to display.

12:00.930 --> 12:07.530
Display number one, greater than or equal to number two.

12:08.820 --> 12:12.930
So if the comparison condition didn't met we.

12:13.790 --> 12:23.270
When the racks greater than or equal to are accident meant we arrive at the greater label.

12:24.040 --> 12:31.510
Here we load the format string Fmt one into RDA register and clear Rax once more.

12:31.510 --> 12:39.940
So we then call the print function to display the message number one now greater than or equal to number

12:39.940 --> 12:40.540
two.

12:40.630 --> 12:43.510
And after that we will also.

12:44.990 --> 12:47.750
Create the exit label.

12:50.360 --> 12:51.590
Exit label.

12:51.680 --> 12:54.890
So here we will actually down here.

12:54.890 --> 12:56.840
So here we will move.

12:58.750 --> 12:59.380
RSP.

13:00.520 --> 13:01.990
RVP here.

13:02.170 --> 13:07.990
RVP Here we are restoring the stack pointer.

13:12.310 --> 13:15.280
Uh, pop up here.

13:15.370 --> 13:16.180
Here we are.

13:16.180 --> 13:20.530
Restore restoring the bass pointer again.

13:21.680 --> 13:22.580
Pointer.

13:22.790 --> 13:28.040
And we are now we're going to return from the main function.

13:28.890 --> 13:29.670
And.

13:30.600 --> 13:33.740
Brett here and we will not add anything here.

13:33.750 --> 13:35.580
Just go to command.

13:36.000 --> 13:38.910
Write that return from the.

13:40.430 --> 13:41.930
A main function.

13:41.930 --> 13:45.350
And finally, we reach the exit label.

13:46.160 --> 13:49.280
This is where we wrap up our program.

13:49.370 --> 13:56.180
We restore the stack pointer, RSP and base pointer rb.

13:56.480 --> 13:57.290
Oops, sorry.

13:57.290 --> 14:01.610
Not RB rb here to their original values.

14:01.610 --> 14:10.610
So we then use the pop rb p instruction to clean up the stack frame and read instruction.

14:11.120 --> 14:19.850
The read instruction gracefully exits the main function and in conclusion, our journey through does

14:19.850 --> 14:25.340
jumping that ACM has unveiled the mechanisms of conditional jumps and branching.

14:25.370 --> 14:32.150
So we have explored how assembly code can make decisions based on conditions influencing program flow.

14:33.070 --> 14:40.690
And these fundamental concepts lay the foundation for understanding more complex programming challenges

14:40.840 --> 14:43.100
and optimizing the code.

14:43.120 --> 14:49.720
So now when we click on this here, and as you can see here, we will get an error.

14:50.650 --> 15:01.570
You are getting this error because instruction we wrote the 64 bit assembly code, but here as default,

15:01.570 --> 15:06.970
the CSM comes with the 32 bit instruction.

15:07.880 --> 15:08.840
Or selected.

15:08.840 --> 15:13.370
And in order to fix that, we will go to settings here.

15:14.040 --> 15:14.550
Um.

15:15.520 --> 15:18.730
World here from settings world and here.

15:18.730 --> 15:22.090
As you can see, we have mode here click on 64.

15:22.970 --> 15:28.520
And assembler click on NSM because NSM best for this purposes.

15:28.730 --> 15:38.030
And as you can see here, when we click on 32 bit x86, we are getting this M 32 here.

15:38.030 --> 15:43.100
But when we click on 64 bit, as you can see it, it also changes here.

15:44.060 --> 15:52.210
It also changed l 32 two from 32 to x l 64.

15:52.220 --> 15:54.890
So now apply this here.

15:56.200 --> 15:57.220
And click okay.

15:57.250 --> 15:58.720
Now let's run our program again.

15:58.720 --> 16:04.120
And as you can see here, we'll start it and build successfully.

16:04.120 --> 16:12.820
And our output is number one, is greater than or equal to number two.

16:12.820 --> 16:14.200
And the program finished.

16:14.230 --> 16:17.710
Normally, execution time is 0.001.

16:17.710 --> 16:26.680
So we can also go to our folder where we compile this assembly here, the CD jump.

16:27.900 --> 16:28.380
LZ.

16:28.380 --> 16:30.300
And as you can see here, we have jumping.

16:30.960 --> 16:35.040
We can also create a separate Makefile and just compile it.

16:35.070 --> 16:37.110
But in this case we will not do that.

16:37.230 --> 16:42.420
And as you can see here, our code is works and when we.

16:44.040 --> 16:47.940
Here on test the program executing program finished.

16:47.970 --> 16:52.320
Normally the execution time is 0.0001.

16:53.250 --> 16:55.110
And this is our output.

16:55.110 --> 17:03.060
So remember, mastering this building blocks will empower you to delve deeper into the intricacies,

17:03.060 --> 17:11.540
intricacies, world of assembly programming and keep practicing, experimenting and pushing your boundaries.

17:11.550 --> 17:19.410
So the journey has just begun in this lecture here, and thank you for joining me in this enlightening

17:19.410 --> 17:20.460
exploration.

17:20.460 --> 17:28.530
Until next time, keep coding and keep expanding your programming, programming skills and happy learning

17:28.560 --> 17:30.180
awaiting you in next lecture.
