WEBVTT

00:00.470 --> 00:07.040
Hello, my name is Stephan and welcome to this in-depth exploration of an assembly code example designed

00:07.040 --> 00:11.570
to calculate the sum of numbers from zero to a given value.

00:11.600 --> 00:18.320
This tutorial aims to guide you through the intricate workings of the code, breaking down each instruction

00:18.320 --> 00:25.530
to unveil the underlying mechanisms of register manipulation, loop constructs and interfacing with

00:25.530 --> 00:30.500
the external functions such as values printf here.

00:30.500 --> 00:37.040
So by the end of this lecture, you will have gained a profound understanding of these components,

00:37.160 --> 00:39.950
come together to create a functional program.

00:39.950 --> 00:43.850
And now let's delve into the specifics of the code.

00:43.940 --> 00:48.830
We will first declare the external function declaration.

00:48.830 --> 00:55.550
The External Directive announces the existence of the printf function, which resides externally, implying

00:56.270 --> 01:00.090
that its implementation will be linked later.

01:00.090 --> 01:03.930
So here we will write extern printf.

01:04.490 --> 01:10.550
And after that we will create a section data and this is the data section.

01:10.550 --> 01:15.140
And here we will define a 64 bit quad word.

01:15.170 --> 01:21.080
Here we will number number equal five.

01:21.110 --> 01:25.730
Here we are defining the 64 bit quad word, which is eight bytes.

01:26.990 --> 01:28.870
Uh, let's actually write it down here.

01:28.880 --> 01:30.320
Eight bytes.

01:33.620 --> 01:34.520
Quadword.

01:35.330 --> 01:45.020
We are defining the eight bytes Quadword variable named number with an initial value of five and we

01:45.020 --> 01:45.650
are here.

01:45.740 --> 01:47.380
Fmt db.

01:48.500 --> 01:50.030
We will add a string here.

01:50.030 --> 01:53.840
The sum from zero to.

01:55.650 --> 01:57.090
L d.

01:58.100 --> 01:58.940
Is.

02:01.130 --> 02:03.040
Is old.

02:03.410 --> 02:05.240
And here we will pass ten.

02:06.320 --> 02:06.790
Atlas.

02:07.870 --> 02:10.270
Here, ten and zero.

02:11.350 --> 02:20.130
Here we are defining a null terminated string format suitable for printf, and we have also developed

02:20.130 --> 02:23.230
the section BSS.

02:23.260 --> 02:27.610
This is the section section uninitialized data.

02:27.910 --> 02:34.990
Here currently empty and this section is used to reverse space for variables and this that will be initialized

02:34.990 --> 02:36.580
during program execution.

02:36.850 --> 02:39.970
We will also define the section text.

02:40.420 --> 02:42.460
Section Text.

02:43.090 --> 02:49.870
We will create a global main here and you can define main as the global entry point of the program.

02:50.260 --> 02:52.000
We will create the main.

02:52.000 --> 02:55.270
Now this is a function prologue here.

02:55.960 --> 02:57.760
Posh RVP.

02:59.110 --> 03:01.170
Here and move RVP.

03:02.900 --> 03:05.210
RWP rsp.

03:07.800 --> 03:08.610
Rtsp.

03:09.930 --> 03:20.070
And here in this line of code, we are preserving the value of base pointer rbpp by pushing it on to

03:20.070 --> 03:21.460
the stack.

03:21.480 --> 03:29.010
And here we are setting the base pointer rbpp to the current stack pointer RSP.

03:29.400 --> 03:33.660
And now we will go to initialization path phase here.

03:33.690 --> 03:37.050
So we will use move rc.

03:39.760 --> 03:40.600
Number.

03:41.290 --> 03:42.530
The number we defined here?

03:42.530 --> 03:43.100
Five.

03:43.780 --> 03:44.430
Here.

03:44.470 --> 03:45.280
Here we are.

03:45.390 --> 03:46.200
Load the.

03:46.480 --> 03:54.610
We are telling the assembly to load the value stored in the memory location number into the loop contour

03:54.640 --> 04:01.930
here, which in this case is rc x and we will move racks.

04:02.840 --> 04:03.440
Zero.

04:03.620 --> 04:12.170
We are initialize telling the assembly to initialize the accumulator racks to zero.

04:13.280 --> 04:16.610
And now we will create this loop here.

04:16.690 --> 04:17.480
B Loop.

04:17.900 --> 04:20.600
So this is the loop body.

04:20.630 --> 04:23.240
We will add like this.

04:23.360 --> 04:24.890
We will add Rex.

04:24.890 --> 04:25.280
Actually.

04:25.280 --> 04:25.760
Why?

04:26.780 --> 04:28.310
This here is like this.

04:32.000 --> 04:35.150
So bloop ad rex.

04:37.090 --> 04:40.960
Are racks and ask X.

04:41.920 --> 04:49.780
Here we are telling the assembler to add the value of the looping counter rc x to the accumulator rax

04:49.780 --> 04:50.470
rax.

04:50.620 --> 04:52.960
And now we will loop.

04:54.490 --> 04:55.540
Up here.

04:56.600 --> 05:04.520
Now we will decrement the loop counter arcs and jump back to bloop bloop.

05:04.520 --> 05:13.970
If RC X is not zero and here we can write our loop termination and here.

05:18.760 --> 05:20.870
Move the.

05:23.320 --> 05:26.080
Are the I fmtm here.

05:26.080 --> 05:31.330
Load the address of the format string into RDA or RDA.

05:34.390 --> 05:35.620
Or die here.

05:35.620 --> 05:36.010
Yeah.

05:36.040 --> 05:37.720
MOV RSI.

05:40.400 --> 05:41.210
Number.

05:41.990 --> 05:44.810
There are a lot of the value of number.

05:45.290 --> 05:55.390
This is the sum to be displayed into RSI and move X to racks.

05:55.400 --> 06:01.200
So load the computer to sum of value from racks into RDX.

06:01.250 --> 06:03.440
Move Racks.

06:05.000 --> 06:05.630
Zero.

06:05.750 --> 06:11.030
Clear racks in preparation for the printf call.

06:11.090 --> 06:13.810
Now we will also call the printf here.

06:13.820 --> 06:14.840
Call printf.

06:14.870 --> 06:18.860
We are invoking the printf function to display the formatted output.

06:18.860 --> 06:22.730
And now we will write our function epilogue here.

06:22.910 --> 06:24.650
MOV rsp.

06:26.230 --> 06:31.900
Rdrp E.r.v are telling the Assembly to restore the stack pointer rsp to its original value.

06:31.930 --> 06:39.820
Cleaning up the stack and we will pop the rbp restore the original value of the base pointer r p and

06:39.820 --> 06:42.640
after that we will return from the main function.

06:46.650 --> 06:47.190
This.

06:47.280 --> 06:55.590
The assembly code showcased here efficiently calculates the summation of numbers starting from zero

06:55.590 --> 06:57.960
up to a specified value.

06:57.990 --> 07:08.190
The key mechanism is the loop construct in which the RC X register serves as a loop counter with each

07:08.190 --> 07:09.300
iteration.

07:09.330 --> 07:21.330
RC x is automatically decremented and the the loop body adds its value to the accumulator racks or racks.

07:21.330 --> 07:30.570
And once the loop concludes, the accumulated sum is presented through the printf function, and a thought

07:31.200 --> 07:38.410
provoking experiment arises by setting number to a notably larger value such as 1 billion.

07:38.430 --> 07:44.160
By doing so, you can observe the program's execution time using the Linux Time command, which we will

07:44.160 --> 07:45.900
do that in the next lecture.

07:45.930 --> 07:49.990
Leading to valuable insights into its performance characteristics.

07:49.990 --> 07:57.130
And this enlightening tutorial empowers you to grasp the intricate interplay of registers, memory and

07:57.130 --> 07:59.530
control flow in assembly language programming.

07:59.620 --> 08:03.670
Fostering a deeper understanding of low level computer operation.
