1
00:00:01,080 --> 00:00:08,820
In this video, we are going to discuss two important instructions, call and read once again to demonstrate

2
00:00:08,820 --> 00:00:11,040
the use of call and read instructions.

3
00:00:11,040 --> 00:00:16,760
I have created a simple program with the file name called a Red Dot S..

4
00:00:17,220 --> 00:00:20,550
So let's navigate to our directly called call Dash Red.

5
00:00:20,940 --> 00:00:25,470
Inside this, I have a file called Call Dash Red Dot.

6
00:00:25,470 --> 00:00:34,380
See if you notice the program is basically making a function call to a function called func and it has

7
00:00:34,380 --> 00:00:35,700
a seven arguments.

8
00:00:36,180 --> 00:00:40,590
Once this function call is made, it basically executes the function definition.

9
00:00:41,280 --> 00:00:47,970
After the execution is completed, it returns back to the next statement in the main function, which

10
00:00:47,970 --> 00:00:48,900
is printf.

11
00:00:48,900 --> 00:00:53,600
I am done and it executes it now like we did in the previous video.

12
00:00:53,820 --> 00:01:00,450
Let's compile this and open it up in Jollibee and understand what kind of instructions are being executed

13
00:01:00,690 --> 00:01:01,890
while this is being done.

14
00:01:02,520 --> 00:01:13,430
So let's first combine this using GC Car Dash, Red Nash, National Call Dash right.

15
00:01:14,970 --> 00:01:15,630
There it is.

16
00:01:16,500 --> 00:01:20,250
Now we should have the binary car dash right now.

17
00:01:20,250 --> 00:01:21,870
Let's open it up using GTP.

18
00:01:26,490 --> 00:01:31,120
And I'm going to set up a breakpoint at the entry point of this program, which is main.

19
00:01:33,270 --> 00:01:38,070
Now let's run the program and let's go through the disassembly I'm typing run.

20
00:01:39,450 --> 00:01:41,670
We are currently inside the main function.

21
00:01:41,700 --> 00:01:47,520
We are going to execute a bunch of instructions before making a call to the function named func.

22
00:01:48,390 --> 00:01:52,400
Now, let's step aside until we reach this push instruction.

23
00:01:52,830 --> 00:01:59,960
So I am typing asi asi hitting enter once again and there it is.

24
00:01:59,970 --> 00:02:06,570
Now we are about to execute this push zero X to misconstruction which is at main plus 12.

25
00:02:07,260 --> 00:02:10,550
Let's observe the next few instructions to be executed.

26
00:02:11,070 --> 00:02:16,920
There is one push instruction, as you can see here, and there are a bunch of move instructions.

27
00:02:17,280 --> 00:02:24,510
We can probably do our business businessmen to better view this d'arrigo that is one push instruction

28
00:02:25,050 --> 00:02:29,250
and there are six more instructions which are moving some values.

29
00:02:30,030 --> 00:02:32,430
I'm not counting this more instruction for now.

30
00:02:32,880 --> 00:02:37,730
So we essentially have six more instructions and one push instruction.

31
00:02:38,160 --> 00:02:45,510
Now let's examine what these values are zero six two zero two five eight and all this.

32
00:02:46,050 --> 00:02:48,000
So I'm just opening up a calculator.

33
00:02:50,790 --> 00: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.

34
00:02:58,710 --> 00:03:00,530
If you notice this, this is 700.

35
00:03:01,140 --> 00:03:07,080
So let me type cat called Red Dot.

36
00:03:07,080 --> 00:03:12,630
See if you notice 700 is the last argument.

37
00:03:13,260 --> 00:03:15,040
How many arguments do we have here?

38
00:03:15,180 --> 00:03:18,570
One, two, three, four, five, six, seven.

39
00:03:19,170 --> 00:03:22,890
So the seventh argument is being pushed onto the stack.

40
00:03:23,550 --> 00:03:26,370
Now let's take what we have after the push instruction.

41
00:03:29,620 --> 00:03:40,000
Here it is, which is zero two five eight two five eight in Hex, should be 600 in decimal.

42
00:03:40,450 --> 00:03:42,460
So let's once again check the program.

43
00:03:44,110 --> 00:03:46,690
Six hundred is the sixth argument.

44
00:03:47,050 --> 00:03:52,450
And if you notice, this sixth argument is being moved into the register online.

45
00:03:53,350 --> 00:04:01,210
This is a sub register of online, since the value being moved is not large enough that online the register

46
00:04:01,220 --> 00:04:03,590
is used instead of online.

47
00:04:03,910 --> 00:04:06,260
So this is the sixth argument to the function.

48
00:04:06,850 --> 00:04:12,170
Next, the value of zero x one of four is being moved into the register.

49
00:04:12,220 --> 00:04:20,950
Added to this is a Register of the Register R8 once again, since the value being moved is not large

50
00:04:20,950 --> 00:04:21,310
enough.

51
00:04:21,550 --> 00:04:25,940
And does that register already is used instead of Orit?

52
00:04:26,620 --> 00:04:30,280
This is the fifth argument to the function next.

53
00:04:30,310 --> 00:04:35,920
The value zero x one nine zero is being moved into the register X..

54
00:04:36,220 --> 00:04:43,120
This is the decimal 400 and once again X is the register of R6.

55
00:04:43,690 --> 00:04:50,040
Since the value being moved is not large enough, X registry is used instead of R6.

56
00:04:50,410 --> 00:04:52,590
This is the fourth argument to the function.

57
00:04:53,140 --> 00:04:59,830
Next, the value zero one to see, which is decimal 300 is being moved into the register.

58
00:05:00,970 --> 00:05:07,490
This is the register of RDX once again, since the value being moved is not large enough, that index

59
00:05:07,520 --> 00:05:10,320
register is used instead of RDX.

60
00:05:10,630 --> 00:05:18,130
This is the third argument to the function and similarly we have zero x C8 which is being moved into

61
00:05:18,130 --> 00:05:18,750
airside.

62
00:05:19,330 --> 00:05:24,400
This is a sample register of autosite and once again the value is not large enough.

63
00:05:24,670 --> 00:05:28,330
So that IACI register is used instead of RSA.

64
00:05:28,630 --> 00:05:35,050
And this is the second argument to the function, not the last one, the value zero x five.

65
00:05:35,050 --> 00:05:45,160
They want to be a 6F zero zero five, which is a large decimal number eight being moved into the register

66
00:05:45,160 --> 00:05:53,170
ideate since the value being moved is large enough this time the register idea is directly used instead

67
00:05:53,170 --> 00:05:54,450
of a sub register.

68
00:05:54,850 --> 00:05:57,340
So this is the first argument to the function.

69
00:05:57,670 --> 00:06:01,480
If you notice the C program, here it is.

70
00:06:03,310 --> 00:06:10,270
Now, these few instructions that we have just examined should give us an idea about how arguments are

71
00:06:10,270 --> 00:06:19,510
handled when a function call is made in 1964, it processes the first six arguments of a function are

72
00:06:19,540 --> 00:06:23,200
stored in the registers of the outside.

73
00:06:23,910 --> 00:06:28,420
The are six, are eight and nine, respectively.

74
00:06:29,110 --> 00:06:34,010
From the seventh argument, they will be pushed onto the stack from right to left.

75
00:06:34,600 --> 00:06:41,080
Now, after all the arguments are handled, there is another important thing that we should notice when

76
00:06:41,080 --> 00:06:48,250
we execute the instruction admen plus fifty nine, which is here, it will go ahead and execute the

77
00:06:48,250 --> 00:06:49,510
definition of the function.

78
00:06:50,230 --> 00:06:56,020
Once the execution completes, it has to return back and execute the print statement in the main function.

79
00:06:56,660 --> 00:06:59,230
Then how does it know how to come back to the main function?

80
00:06:59,940 --> 00:07:05,100
Let's knock down what we have on the top of the stack before executing call instruction.

81
00:07:05,440 --> 00:07:09,970
Let's also note down the address of the next instruction after the call instruction.

82
00:07:10,480 --> 00:07:11,740
So let's assign.

83
00:07:14,560 --> 00:07:17,980
Let's step aside until we reach the call instruction,

84
00:07:23,320 --> 00:07:29,200
if you notice, we are about to execute this instruction after executing the next move instruction,

85
00:07:29,810 --> 00:07:36,580
but before executing this, let's take note of the address of the next instruction after the call instruction.

86
00:07:37,240 --> 00:07:38,320
I'm is copying it here.

87
00:07:42,660 --> 00:07:50,040
And I'm pasting it here and let's check what we have on the top of the stack.

88
00:07:51,200 --> 00:07:56,990
If you notice, we currently have the argument that we pushed earlier, which is zero X to be C.

89
00:07:59,790 --> 00:08:04,530
And I'm just pasting it here now, let's type aside.

90
00:08:06,140 --> 00:08:08,930
Still, the top of the stack is the same.

91
00:08:09,320 --> 00:08:14,570
Now let's execute this call instruction and let's see what happens to the top of the stack.

92
00:08:16,810 --> 00:08:23,700
Have executed the instruction, and if you now observe the top of the stack, we have a different value

93
00:08:24,180 --> 00:08:30,900
and just copying this and pasting it here once again in the notepad right after the address that we

94
00:08:30,900 --> 00:08:38,100
have copied earlier, if you notice, the address that is pushed onto the stack is the same address

95
00:08:38,100 --> 00:08:42,020
that we have gotten, which is right after the call instruction.

96
00:08:42,480 --> 00:08:50,760
So what it means is when the instruction is executed, it has pushed the address of its next instruction

97
00:08:50,760 --> 00:08:51,580
onto the stack.

98
00:08:52,290 --> 00:08:57,990
So once the definition of the function is executed, it will return to this address by pumping this

99
00:08:57,990 --> 00:09:00,820
value into the RPE register from the stack.

100
00:09:01,260 --> 00:09:05,260
So let's test our theory by completely executing this function definition.

101
00:09:05,550 --> 00:09:07,200
So let's do a single step.

102
00:09:09,570 --> 00:09:13,560
I'm typing a sign multiple times.

103
00:09:29,560 --> 00:09:34,960
All right, we are reaching the right instruction, which is the last instruction in the function funk,

104
00:09:35,320 --> 00:09:40,070
so I'm just stepping aside once again as I once again and there you go.

105
00:09:40,450 --> 00:09:40,900
We are.

106
00:09:40,900 --> 00:09:42,160
And direct instruction.

107
00:09:42,160 --> 00:09:48,940
And if you look at the next instruction after this read, it is going to be the instruction or the address

108
00:09:48,940 --> 00:09:51,840
that we have just seen on the top of the stack earlier.

109
00:09:52,630 --> 00:09:58,720
But how does the processor know that after executing this written instruction, it has to go to this

110
00:09:58,720 --> 00:09:59,260
address?

111
00:09:59,920 --> 00:10:06,580
When the direct instruction is executed, it is going to pop the value which is on the top of the stack

112
00:10:06,880 --> 00:10:10,930
into the ORAC register, and then it will execute that.

113
00:10:11,560 --> 00: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

114
00:10:17,920 --> 00:10:20,340
of the next instruction to the instruction.

115
00:10:20,620 --> 00:10:27,030
And when we execute this read instruction, this address is going to be poured into the AP register.

116
00:10:27,490 --> 00:10:31,240
So let's type aside and look at that.

117
00:10:31,630 --> 00:10:38,640
AP register now contains the address of the instruction, which is right after our call instruction.

118
00:10:39,190 --> 00:10:40,900
So that's how it functions work.

119
00:10:41,380 --> 00:10:48,250
To give a recap in this lecture, we have seen how functions work and how registers and stack are used

120
00:10:48,250 --> 00:10:50,020
to manage the arguments of the function.

121
00:10:50,320 --> 00:10:54,820
And then we have also seen how the return address is placed on the stack.

122
00:10:55,360 --> 00:11:02,200
So primarily we learned a few things in this lecture, how arguments are managed using registers and

123
00:11:02,200 --> 00:11:07,690
stack when a function is called, how return value is placed on the stack when a function is called.

124
00:11:07,930 --> 00:11:15,010
And we have also learned how the return address is poured into the AP register when a function is returning

125
00:11:15,010 --> 00:11:16,140
from its execution.

126
00:11:16,870 --> 00:11:23,770
We have also understood that read instruction always pops the value from the top of the stack into RHP

127
00:11:23,770 --> 00:11:27,630
register to be able to return to the next instruction of the calling function.

128
00:11:28,300 --> 00:11:29,480
That's all for this video.

129
00:11:29,650 --> 00:11:30,760
See you in the next one.

