I am trying to understand how a function call and returning from function works at machine level. From Microsoft’s documentation and many other resources define returning from a function like this:
A return statement ends the execution of a function, and returns control to the calling function. Execution resumes in the calling function at the point immediately following the call. A return statement can return a value to the calling function.
But I could find none that gave a detailed explanation on the low level working of return statements and function calling.I watched a Youtube video (it was the only one which i could find that discussed this at machine level). It was for c but I think the execution should be similar in c++.
Let say I have two test codes as follows (Assuming no optimization is being done by the compiler):
- This one should have common execution in both c and c++
int square(int i){
return i*i;
}
int main(){
int num = square(7);
return 0;
}
- C does not have classes. So this code is for c++ only. Assuming a class called MyObj is already defined and is big enough that it can neither be stored in a single register nor in multiple registers.
MyObj func(MyObj obj_recieved){
//...
//Does some operations on obj_recieved
//...
return obj_recieved;
}
int main(){
MyObj obj1;
MyObj obj2 = func(obj1);
return 0;
}
How will the execution look at machine level of the above two codes?
From what I could learn through the video and searching over the internet. I think for the first code it should go like this:
-
An integer variable num is created on stack.
-
The function square() is executed with 7 as its argument and for that following steps will occur.
- A pointer is saved on the stack for the return address. In this case the return address will be the address of num variable.
- A stack frame is created on the stack for the square() functions and the argument 7 gets copied and stored in this stack frame.
- Stack pointer now moves to the function square().
- An instruction for multiply command is executed and 7 gets multiplied with 7
- The result of multiply instruction which will be 49 is to be returned and this gets stored in a register ( but this can vary with CPUs).
- The stack frame created for square() gets cleaned up.
- The stack pointer now moves back to the main() function and to the return address which is address of num
-
The returned value is stored in num variable.
-
A return statement is encountered which returns 0, so 0 gets stored in a register.
-
The stack for main() gets cleaned up and program execution finishes. The OS should handle the rest including the returned value 0 inside the register.
Is this the correct way the CPU does the execution for code 1 for both c and c++?
For 2nd code I got stuck on sub steps of point 2 that I have written in the execution for code 1.
When stack pointer reaches obj2, CPU will do the same thing as it did for code 1. obj1 will be copied and stored in obj_recieved inside the stack frame of func(). At the end of execution a copy of this obj_recieved should be returned but where will it store the copy of obj_recieved?
Divyansh Singh is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.