I’ve got a function with signature double func(double,...) and I’m sure all the arguments I’m passing to func are type double. All arguments are stored in some a vector called arguments, I’m just wondering if there is way to write a generic code to pass any number of arguments specified in argumentVector to func?
something like :
for (int i=0;i<agumentVector.size();i++)
pushstack(argumentVector[i]);
res = call(func);
so far I’ve tried this code but it seems to have problems:
double* param = &argumentVector[0];
for(int i=0;i<argumentVector.size();i++)
{
_asm sub esp,8;
_asm fld param;
_asm fstp qword ptr [esp];
param++;
}
_asm call func;
_asm add esp,10h;
_asm fstp qword ptr res;
–just a crazy result I’m getting! this code runs without any problems :
double x[2] = {5,6};
double *param = x;
double res;
_asm sub esp,8;
_asm fld x;
_asm fstp qword ptr [esp];
_asm sub esp,8;
_asm fld x+8;
_asm fstp qword ptr [esp];
param++;
_asm call p;
_asm add esp,10h;
_asm fstp qword ptr res;
cout << res << "\n";
while in this one arguments are not passed properly!
double x[2] = {5,6};
double *param = x;
double res;
_asm sub esp,8;
_asm fld param;
_asm fstp qword ptr [esp];
_asm sub esp,8;
_asm fld param+8;
_asm fstp qword ptr [esp];
param++;
_asm call p;
_asm add esp,10h;
_asm fstp qword ptr res;
cout << res << "\n";
do you have any idea why? I though both x and param were simple arrays!
–edit2–
here is the complete assembly code so far VC generates
double x[2] = {5,6};
010E1A05 fld qword ptr [__real@4014000000000000 (10EB928h)]
010E1A0B fstp qword ptr [ebp-48h]
010E1A0E fld qword ptr [__real@4018000000000000 (10EB878h)]
010E1A14 fstp qword ptr [ebp-40h]
double *param = x;
010E1A17 lea eax,[ebp-48h]
010E1A1A mov dword ptr [ebp-54h],eax
double res;
p(param[0],param[1]);
010E1A1D mov esi,esp
010E1A1F mov eax,dword ptr [ebp-54h]
010E1A22 sub esp,8
010E1A25 fld qword ptr [eax+8]
010E1A28 fstp qword ptr [esp]
010E1A2B mov ecx,dword ptr [ebp-54h]
010E1A2E sub esp,8
010E1A31 fld qword ptr [ecx]
010E1A33 fstp qword ptr [esp]
010E1A36 call dword ptr [ebp-14h]
010E1A39 fstp st(0)
010E1A3B add esp,10h
010E1A3E cmp esi,esp
010E1A40 call @ILT+795(__RTC_CheckEsp) (10E1320h)
{
_asm sub esp,8;
010E1A45 sub esp,8
_asm fld param+8;
010E1A48 fld dword ptr [ebp-4Ch]
_asm fstp qword ptr [esp];
010E1A4B fstp qword ptr [esp]
_asm sub esp,8;
010E1A4E sub esp,8
_asm fld param;
010E1A51 fld dword ptr [ebp-54h]
_asm fstp qword ptr [esp];
010E1A54 fstp qword ptr [esp]
}
_asm call p;
010E1A57 call dword ptr [ebp-14h]
_asm add esp,10h;
010E1A5A add esp,10h
_asm fstp qword ptr res;
010E1A5D fstp qword ptr [ebp-64h]
cout << res << "\n";
010E1A60 push offset string "\n" (10EB834h)
010E1A65 mov esi,esp
010E1A67 sub esp,8
010E1A6A fld qword ptr [ebp-64h]
010E1A6D fstp qword ptr [esp]
010E1A70 mov ecx,dword ptr [__imp_std::cout (10EF3A8h)]
010E1A76 call dword ptr [__imp_std::basic_ostream<char,std::char_traits<char> >::operator<< (10EF3ACh)]
010E1A7C cmp esi,esp
010E1A7E call @ILT+795(__RTC_CheckEsp) (10E1320h)
010E1A83 push eax
010E1A84 call std::operator<<<std::char_traits<char> > (10E1294h)
010E1A89 add esp,8
return 0;
The following works in Visual Studio, but I think you can easily correct the inline assembler syntax so it fits your compiler’s requirements.