gpt4 book ai didi

c - 将变量从汇编程序传递到 C

转载 作者:太空狗 更新时间:2023-10-29 15:07:57 26 4
gpt4 key购买 nike

#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;
}

关于c - 将变量从汇编程序传递到 C,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33807685/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com