Reputation: 95
I am trying to code the sum of natural numbers using inline x64 assembly in C but it isn't working.
#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:
}
}
I get the following error:
summer.c:4:1: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token
And also how can I define the n
variable? Is it better to do it in the inline assembly or in the C code?
Upvotes: 1
Views: 518
Reputation: 126777
The compiler is complaining because your function declaration doesn't make sense. unsigned
is a reserved keyword, call your function in some other way; also, you have to specify the type of the parameter.
int f(int n)
{
...
As for the assembly, the syntax depends from the toolchain you are using, depending from it you may have to use the hideous AT & T. syntax (or to specify some magic command to switch to Intel syntax and back).
(by the way, xor eax, eax
is more idiomatic than mov eax, 0
)
Upvotes: 1