[英]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);
}
我想通過result
變量將一些數據從匯編程序傳遞給main
。 這可能嗎? 我的匯編程序代碼導致Segmentation fault (core dumped)
。 我使用的是Ubuntu 15.10 x86_64,gcc 5.2.1。
更好的方法可能是:
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.