In this video, we are going to discuss two important instructions, call and read once again to demonstrate
the use of call and read instructions.
I have created a simple program with the file name called a Red Dot S..
So let's navigate to our directly called call Dash Red.
Inside this, I have a file called Call Dash Red Dot.
See if you notice the program is basically making a function call to a function called func and it has
a seven arguments.
Once this function call is made, it basically executes the function definition.
After the execution is completed, it returns back to the next statement in the main function, which
is printf.
I am done and it executes it now like we did in the previous video.
Let's compile this and open it up in Jollibee and understand what kind of instructions are being executed
while this is being done.
So let's first combine this using GC Car Dash, Red Nash, National Call Dash right.
There it is.
Now we should have the binary car dash right now.
Let's open it up using GTP.
And I'm going to set up a breakpoint at the entry point of this program, which is main.
Now let's run the program and let's go through the disassembly I'm typing run.
We are currently inside the main function.
We are going to execute a bunch of instructions before making a call to the function named func.
Now, let's step aside until we reach this push instruction.
So I am typing asi asi hitting enter once again and there it is.
Now we are about to execute this push zero X to misconstruction which is at main plus 12.
Let's observe the next few instructions to be executed.
There is one push instruction, as you can see here, and there are a bunch of move instructions.
We can probably do our business businessmen to better view this d'arrigo that is one push instruction
and there are six more instructions which are moving some values.
I'm not counting this more instruction for now.
So we essentially have six more instructions and one push instruction.
Now let's examine what these values are zero six two zero two five eight and all this.
So I'm just opening up a calculator.
And I'm currently in hex mode, so I'm just typing to ABC and let's check what it is in decimal.
If you notice this, this is 700.
So let me type cat called Red Dot.
See if you notice 700 is the last argument.
How many arguments do we have here?
One, two, three, four, five, six, seven.
So the seventh argument is being pushed onto the stack.
Now let's take what we have after the push instruction.
Here it is, which is zero two five eight two five eight in Hex, should be 600 in decimal.
So let's once again check the program.
Six hundred is the sixth argument.
And if you notice, this sixth argument is being moved into the register online.
This is a sub register of online, since the value being moved is not large enough that online the register
is used instead of online.
So this is the sixth argument to the function.
Next, the value of zero x one of four is being moved into the register.
Added to this is a Register of the Register R8 once again, since the value being moved is not large
enough.
And does that register already is used instead of Orit?
This is the fifth argument to the function next.
The value zero x one nine zero is being moved into the register X..
This is the decimal 400 and once again X is the register of R6.
Since the value being moved is not large enough, X registry is used instead of R6.
This is the fourth argument to the function.
Next, the value zero one to see, which is decimal 300 is being moved into the register.
This is the register of RDX once again, since the value being moved is not large enough, that index
register is used instead of RDX.
This is the third argument to the function and similarly we have zero x C8 which is being moved into
airside.
This is a sample register of autosite and once again the value is not large enough.
So that IACI register is used instead of RSA.
And this is the second argument to the function, not the last one, the value zero x five.
They want to be a 6F zero zero five, which is a large decimal number eight being moved into the register
ideate since the value being moved is large enough this time the register idea is directly used instead
of a sub register.
So this is the first argument to the function.
If you notice the C program, here it is.
Now, these few instructions that we have just examined should give us an idea about how arguments are
handled when a function call is made in 1964, it processes the first six arguments of a function are
stored in the registers of the outside.
The are six, are eight and nine, respectively.
From the seventh argument, they will be pushed onto the stack from right to left.
Now, after all the arguments are handled, there is another important thing that we should notice when
we execute the instruction admen plus fifty nine, which is here, it will go ahead and execute the
definition of the function.
Once the execution completes, it has to return back and execute the print statement in the main function.
Then how does it know how to come back to the main function?
Let's knock down what we have on the top of the stack before executing call instruction.
Let's also note down the address of the next instruction after the call instruction.
So let's assign.
Let's step aside until we reach the call instruction,
if you notice, we are about to execute this instruction after executing the next move instruction,
but before executing this, let's take note of the address of the next instruction after the call instruction.
I'm is copying it here.
And I'm pasting it here and let's check what we have on the top of the stack.
If you notice, we currently have the argument that we pushed earlier, which is zero X to be C.
And I'm just pasting it here now, let's type aside.
Still, the top of the stack is the same.
Now let's execute this call instruction and let's see what happens to the top of the stack.
Have executed the instruction, and if you now observe the top of the stack, we have a different value
and just copying this and pasting it here once again in the notepad right after the address that we
have copied earlier, if you notice, the address that is pushed onto the stack is the same address
that we have gotten, which is right after the call instruction.
So what it means is when the instruction is executed, it has pushed the address of its next instruction
onto the stack.
So once the definition of the function is executed, it will return to this address by pumping this
value into the RPE register from the stack.
So let's test our theory by completely executing this function definition.
So let's do a single step.
I'm typing a sign multiple times.
All right, we are reaching the right instruction, which is the last instruction in the function funk,
so I'm just stepping aside once again as I once again and there you go.
We are.
And direct instruction.
And if you look at the next instruction after this read, it is going to be the instruction or the address
that we have just seen on the top of the stack earlier.
But how does the processor know that after executing this written instruction, it has to go to this
address?
When the direct instruction is executed, it is going to pop the value which is on the top of the stack
into the ORAC register, and then it will execute that.
If you notice the top of the stack at this point of time, it has the same address, which is the address
of the next instruction to the instruction.
And when we execute this read instruction, this address is going to be poured into the AP register.
So let's type aside and look at that.
AP register now contains the address of the instruction, which is right after our call instruction.
So that's how it functions work.
To give a recap in this lecture, we have seen how functions work and how registers and stack are used
to manage the arguments of the function.
And then we have also seen how the return address is placed on the stack.
So primarily we learned a few things in this lecture, how arguments are managed using registers and
stack when a function is called, how return value is placed on the stack when a function is called.
And we have also learned how the return address is poured into the AP register when a function is returning
from its execution.
We have also understood that read instruction always pops the value from the top of the stack into RHP
register to be able to return to the next instruction of the calling function.
That's all for this video.
See you in the next one.