Trying to practice drawing Stack Frame, so I need help here. I think the parameters/return is the one I need help on when it comes to the stack. Now here is an example of a snippet code, and aftewards, my attempted stack frame drawing. Therefore, any help on how to visually see this is appreciate.
#include <iostream>
#include "mymath.h"
usingnamespace std;
//not stack
int A(int);
int B(int);
int main(int argc, char* argv[]) {
int x;
double y;
char z;
x = 10;
y = 5.0;
z = 127;
int values[4] = {0, 1, 2, 3};
int number = x;
int ret = Abs(number);
// what happens to the stack going into and out of this function call?
// how about a more complicated method chain?
}
int A(int param) {
return B(param);
}
int B(int param) {
int local;
local = Pow(param, 2);
return local;
}
So I start from main function
Stack memory model. So after doing main, next would be Int A(int param)
not sure how to precisely do this.
| ? |55 return //
| ? |59 param // A(int)
| 10 |63 return absolute number
| 10 |67 number
| v[0] |71 Values[4] each holding 4 bytes.
| v[1] |75
| v[2] |79
| v[3] |83
| 127 |(Address 87) Z
| 5.0 |(Address 88) Y
| 10 |(Address 96) x
(Higher Address memory)
// what happens to the stack going into and out of this function call?
It is hard to actually tell. First, in x64 first four argument passed to function through registers and does not appears on stack at all. Second order of arguments and return value on stack depends on used calling convention. Third, actual use of stack depend on compioler optimisation. On high optimization level, your program will not use stack at all. In fact it will probably look like int main() {} after optimizing. In other cases, many variables values of which can be known at compile time can be replaced by constants. So it is impossible to tell which values will be on stack during and after function call without knowing at least calling convention used