gpt4 book ai didi

c - C 中的 x64 内联汇编

转载 作者:太空宇宙 更新时间:2023-11-04 03:46:56 25 4
gpt4 key购买 nike

我正在尝试编码 the sum of natural numbers在 C 中使用内联 x64 程序集,但它不起作用。

#include <stdio.h>

int unsigned(n)
{
__asm__
{
mov ecx, n;
mov eax, 0;
cmp ecx, 0;
je ende;
label:
add eax, ecx;
loop label;

ende:

}
}

我收到以下错误:

summer.c:4:1: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token

还有如何定义 n 变量?是在内联汇编中还是在 C 代码中更好?

最佳答案

编译器报错是因为你的函数声明没有意义。 unsigned 是保留关键字,以其他方式调用您的函数;另外,您必须指定参数的类型。

int f(int n) 
{
...

至于程序集,语法取决于您使用的工具链,根据它您可能不得不使用可怕的 AT & T. 语法(或指定一些魔法命令来切换到 Intel 语法并返回)。

(顺便说一下,xor eax, eaxmov eax, 0 更符合习惯)

关于c - C 中的 x64 内联汇编,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23552665/

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