WEBVTT

00:01.080 --> 00:08.820
In this video, we are going to discuss two important instructions, call and read once again to demonstrate

00:08.820 --> 00:11.040
the use of call and read instructions.

00:11.040 --> 00:16.760
I have created a simple program with the file name called a Red Dot S..

00:17.220 --> 00:20.550
So let's navigate to our directly called call Dash Red.

00:20.940 --> 00:25.470
Inside this, I have a file called Call Dash Red Dot.

00:25.470 --> 00:34.380
See if you notice the program is basically making a function call to a function called func and it has

00:34.380 --> 00:35.700
a seven arguments.

00:36.180 --> 00:40.590
Once this function call is made, it basically executes the function definition.

00:41.280 --> 00:47.970
After the execution is completed, it returns back to the next statement in the main function, which

00:47.970 --> 00:48.900
is printf.

00:48.900 --> 00:53.600
I am done and it executes it now like we did in the previous video.

00:53.820 --> 01:00.450
Let's compile this and open it up in Jollibee and understand what kind of instructions are being executed

01:00.690 --> 01:01.890
while this is being done.

01:02.520 --> 01:13.430
So let's first combine this using GC Car Dash, Red Nash, National Call Dash right.

01:14.970 --> 01:15.630
There it is.

01:16.500 --> 01:20.250
Now we should have the binary car dash right now.

01:20.250 --> 01:21.870
Let's open it up using GTP.

01:26.490 --> 01:31.120
And I'm going to set up a breakpoint at the entry point of this program, which is main.

01:33.270 --> 01:38.070
Now let's run the program and let's go through the disassembly I'm typing run.

01:39.450 --> 01:41.670
We are currently inside the main function.

01:41.700 --> 01:47.520
We are going to execute a bunch of instructions before making a call to the function named func.

01:48.390 --> 01:52.400
Now, let's step aside until we reach this push instruction.

01:52.830 --> 01:59.960
So I am typing asi asi hitting enter once again and there it is.

01:59.970 --> 02:06.570
Now we are about to execute this push zero X to misconstruction which is at main plus 12.

02:07.260 --> 02:10.550
Let's observe the next few instructions to be executed.

02:11.070 --> 02:16.920
There is one push instruction, as you can see here, and there are a bunch of move instructions.

02:17.280 --> 02:24.510
We can probably do our business businessmen to better view this d'arrigo that is one push instruction

02:25.050 --> 02:29.250
and there are six more instructions which are moving some values.

02:30.030 --> 02:32.430
I'm not counting this more instruction for now.

02:32.880 --> 02:37.730
So we essentially have six more instructions and one push instruction.

02:38.160 --> 02:45.510
Now let's examine what these values are zero six two zero two five eight and all this.

02:46.050 --> 02:48.000
So I'm just opening up a calculator.

02:50.790 --> 02:57.930
And I'm currently in hex mode, so I'm just typing to ABC and let's check what it is in decimal.

02:58.710 --> 03:00.530
If you notice this, this is 700.

03:01.140 --> 03:07.080
So let me type cat called Red Dot.

03:07.080 --> 03:12.630
See if you notice 700 is the last argument.

03:13.260 --> 03:15.040
How many arguments do we have here?

03:15.180 --> 03:18.570
One, two, three, four, five, six, seven.

03:19.170 --> 03:22.890
So the seventh argument is being pushed onto the stack.

03:23.550 --> 03:26.370
Now let's take what we have after the push instruction.

03:29.620 --> 03:40.000
Here it is, which is zero two five eight two five eight in Hex, should be 600 in decimal.

03:40.450 --> 03:42.460
So let's once again check the program.

03:44.110 --> 03:46.690
Six hundred is the sixth argument.

03:47.050 --> 03:52.450
And if you notice, this sixth argument is being moved into the register online.

03:53.350 --> 04:01.210
This is a sub register of online, since the value being moved is not large enough that online the register

04:01.220 --> 04:03.590
is used instead of online.

04:03.910 --> 04:06.260
So this is the sixth argument to the function.

04:06.850 --> 04:12.170
Next, the value of zero x one of four is being moved into the register.

04:12.220 --> 04:20.950
Added to this is a Register of the Register R8 once again, since the value being moved is not large

04:20.950 --> 04:21.310
enough.

04:21.550 --> 04:25.940
And does that register already is used instead of Orit?

04:26.620 --> 04:30.280
This is the fifth argument to the function next.

04:30.310 --> 04:35.920
The value zero x one nine zero is being moved into the register X..

04:36.220 --> 04:43.120
This is the decimal 400 and once again X is the register of R6.

04:43.690 --> 04:50.040
Since the value being moved is not large enough, X registry is used instead of R6.

04:50.410 --> 04:52.590
This is the fourth argument to the function.

04:53.140 --> 04:59.830
Next, the value zero one to see, which is decimal 300 is being moved into the register.

05:00.970 --> 05:07.490
This is the register of RDX once again, since the value being moved is not large enough, that index

05:07.520 --> 05:10.320
register is used instead of RDX.

05:10.630 --> 05:18.130
This is the third argument to the function and similarly we have zero x C8 which is being moved into

05:18.130 --> 05:18.750
airside.

05:19.330 --> 05:24.400
This is a sample register of autosite and once again the value is not large enough.

05:24.670 --> 05:28.330
So that IACI register is used instead of RSA.

05:28.630 --> 05:35.050
And this is the second argument to the function, not the last one, the value zero x five.

05:35.050 --> 05:45.160
They want to be a 6F zero zero five, which is a large decimal number eight being moved into the register

05:45.160 --> 05:53.170
ideate since the value being moved is large enough this time the register idea is directly used instead

05:53.170 --> 05:54.450
of a sub register.

05:54.850 --> 05:57.340
So this is the first argument to the function.

05:57.670 --> 06:01.480
If you notice the C program, here it is.

06:03.310 --> 06:10.270
Now, these few instructions that we have just examined should give us an idea about how arguments are

06:10.270 --> 06:19.510
handled when a function call is made in 1964, it processes the first six arguments of a function are

06:19.540 --> 06:23.200
stored in the registers of the outside.

06:23.910 --> 06:28.420
The are six, are eight and nine, respectively.

06:29.110 --> 06:34.010
From the seventh argument, they will be pushed onto the stack from right to left.

06:34.600 --> 06:41.080
Now, after all the arguments are handled, there is another important thing that we should notice when

06:41.080 --> 06:48.250
we execute the instruction admen plus fifty nine, which is here, it will go ahead and execute the

06:48.250 --> 06:49.510
definition of the function.

06:50.230 --> 06:56.020
Once the execution completes, it has to return back and execute the print statement in the main function.

06:56.660 --> 06:59.230
Then how does it know how to come back to the main function?

06:59.940 --> 07:05.100
Let's knock down what we have on the top of the stack before executing call instruction.

07:05.440 --> 07:09.970
Let's also note down the address of the next instruction after the call instruction.

07:10.480 --> 07:11.740
So let's assign.

07:14.560 --> 07:17.980
Let's step aside until we reach the call instruction,

07:23.320 --> 07:29.200
if you notice, we are about to execute this instruction after executing the next move instruction,

07:29.810 --> 07:36.580
but before executing this, let's take note of the address of the next instruction after the call instruction.

07:37.240 --> 07:38.320
I'm is copying it here.

07:42.660 --> 07:50.040
And I'm pasting it here and let's check what we have on the top of the stack.

07:51.200 --> 07:56.990
If you notice, we currently have the argument that we pushed earlier, which is zero X to be C.

07:59.790 --> 08:04.530
And I'm just pasting it here now, let's type aside.

08:06.140 --> 08:08.930
Still, the top of the stack is the same.

08:09.320 --> 08:14.570
Now let's execute this call instruction and let's see what happens to the top of the stack.

08:16.810 --> 08:23.700
Have executed the instruction, and if you now observe the top of the stack, we have a different value

08:24.180 --> 08:30.900
and just copying this and pasting it here once again in the notepad right after the address that we

08:30.900 --> 08:38.100
have copied earlier, if you notice, the address that is pushed onto the stack is the same address

08:38.100 --> 08:42.020
that we have gotten, which is right after the call instruction.

08:42.480 --> 08:50.760
So what it means is when the instruction is executed, it has pushed the address of its next instruction

08:50.760 --> 08:51.580
onto the stack.

08:52.290 --> 08:57.990
So once the definition of the function is executed, it will return to this address by pumping this

08:57.990 --> 09:00.820
value into the RPE register from the stack.

09:01.260 --> 09:05.260
So let's test our theory by completely executing this function definition.

09:05.550 --> 09:07.200
So let's do a single step.

09:09.570 --> 09:13.560
I'm typing a sign multiple times.

09:29.560 --> 09:34.960
All right, we are reaching the right instruction, which is the last instruction in the function funk,

09:35.320 --> 09:40.070
so I'm just stepping aside once again as I once again and there you go.

09:40.450 --> 09:40.900
We are.

09:40.900 --> 09:42.160
And direct instruction.

09:42.160 --> 09:48.940
And if you look at the next instruction after this read, it is going to be the instruction or the address

09:48.940 --> 09:51.840
that we have just seen on the top of the stack earlier.

09:52.630 --> 09:58.720
But how does the processor know that after executing this written instruction, it has to go to this

09:58.720 --> 09:59.260
address?

09:59.920 --> 10:06.580
When the direct instruction is executed, it is going to pop the value which is on the top of the stack

10:06.880 --> 10:10.930
into the ORAC register, and then it will execute that.

10:11.560 --> 10:17.920
If you notice the top of the stack at this point of time, it has the same address, which is the address

10:17.920 --> 10:20.340
of the next instruction to the instruction.

10:20.620 --> 10:27.030
And when we execute this read instruction, this address is going to be poured into the AP register.

10:27.490 --> 10:31.240
So let's type aside and look at that.

10:31.630 --> 10:38.640
AP register now contains the address of the instruction, which is right after our call instruction.

10:39.190 --> 10:40.900
So that's how it functions work.

10:41.380 --> 10:48.250
To give a recap in this lecture, we have seen how functions work and how registers and stack are used

10:48.250 --> 10:50.020
to manage the arguments of the function.

10:50.320 --> 10:54.820
And then we have also seen how the return address is placed on the stack.

10:55.360 --> 11:02.200
So primarily we learned a few things in this lecture, how arguments are managed using registers and

11:02.200 --> 11:07.690
stack when a function is called, how return value is placed on the stack when a function is called.

11:07.930 --> 11:15.010
And we have also learned how the return address is poured into the AP register when a function is returning

11:15.010 --> 11:16.140
from its execution.

11:16.870 --> 11:23.770
We have also understood that read instruction always pops the value from the top of the stack into RHP

11:23.770 --> 11:27.630
register to be able to return to the next instruction of the calling function.

11:28.300 --> 11:29.480
That's all for this video.

11:29.650 --> 11:30.760
See you in the next one.
