[英]Passing variable from assembler to C
#include <stdio.h>
int main(){
__asm__ (
"result: \n\t"
".long 0 \n\t"
"rdtsc \n\t"
"movl %eax, %ecx\n\t"
"rdtsc \n\t"
"subl %ecx, %eax\n\t"
"movl %eax, result\n\t"
);
extern int result;
printf("%d\n", result);
}
I would like to pass some data from assembler to main
via the result
variable. 我想通过result
变量将一些数据从汇编程序传递给main
。 Is this possible? 这可能吗? My assembler code causes a Segmentation fault (core dumped)
. 我的汇编程序代码导致Segmentation fault (core dumped)
。 I am using Ubuntu 15.10 x86_64, gcc 5.2.1. 我使用的是Ubuntu 15.10 x86_64,gcc 5.2.1。
A better approach could be: 更好的方法可能是:
int main (void)
{
unsigned before, after;
__asm__
(
"rdtsc\n\t"
"movl %%eax, %0\n\t"
"rdtsc\n\t"
: "=rm" (before), "=a" (after)
: /* no inputs */
: "edx"
);
/* TODO: check for after < before in case you were unlucky
* to hit a wraparound */
printf("%u\n", after - before);
return 0;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.