gcc inline assembly code in c c function call: I had just been to prove

I'm afraid I do not write it down, I do not remember when I was still in the future c nested assembly language, a process call c function assembly code.

Toss one afternoon, in the online view of the data, and then compilation of nested models to copy in c code to fix the problems I encountered, can finally find all in vain.

According to my initial understanding, the assembler to call a nested function of time in c c, when too many parameters c function assembly code can not write, it seems to have considerable restrictions on the number of parameters.

The following code, if I c function parameters increased to three, I would not write assembly code, and not by a simple assembly code to add a parameter to achieve.

More than the increase a parameter, the code is compiled, it will throw an error: 'asm' operand has impossible constraints | (operand constrained impossible 'asm')

I was completely abandoned, call it.

 

int abc2(int a, int b)
{
printf("\n");

printf("参数a(%d): %d\n", &a, a);
printf("参数b(%d): %d\n", &b, b);
//printf("参数c(%d): %d\n", &c, c);
//printf("参数d(%d): %d\n", &d, d);

return a > b ? a : b;
}

 

int main(int argc, char *argv[])
{
void *saved_my_esp, *target_addr;
unsigned int return_eax, return_edx;

void *func = abc2;

int a = 10;
int b = 20;
int c = 30;
int d = 40;

printf("&a:%d\n", &a);
printf("&b:%d\n", &b);
printf("&func:%d\n", func);


//参考: https://bbs.csdn.net/topics/360148698


__asm__ __volatile__ (

"movl %%esp, %0;"

"movl %3, %%eax;" //变量: d
"push %%eax;"

"movl %4, %%eax;" //变量: c
"push %%eax;"


"movl %5, %%eax;" //函数
"call *%%eax;"

"mov %%eax, %1;"
"mov %%edx, %2;"
"movl %0, %%eax;"

//"movl %%eax, %%esp;" /** 加这一句, 就出错 **/

: "+m" (saved_my_esp), "=m" (return_eax), "=m" (return_edx)

: "r"(b), "r"(a), "r"(func) // 关于 m, r 说明: https://www.cnblogs.com/Jezze/archive/2011/12/23/2299838.html

: "%eax", "%edx", "%esp"
);

printf("saved_my_esp:%d\n", saved_my_esp);
printf("return_eax:%d\n", return_eax);
printf("return_edx:%d\n", return_edx);

printf("\n");

//printf("return_value:'%s'\n", return_eax
//printf("return_value:%d\n", *(int *)return_eax);
//printf("return_value:%lf\n", *(float *)return_eax);
//printf("return_value:%d\n", *(BYTE *)return_eax);

printf("return_value:%d\n", return_eax);

printf("\n");

printf("------------------end-------------\n");

return 0;

}

 

Guess you like

Origin www.cnblogs.com/personnel/p/11414480.html