#include <stdio.h>
#include <stdlib.h>


int asm_sum(int x, int y)
{
        int ret = 0;

        __asm__ __volatile__(   "loop:\t\n"
                                "addl %0 %1\t\n"
                                "incl %1\t\n"
                                "cmpl %1 %2\t\n"
                                "jle loop"
                                :"=r"(ret)
                                :"r"(x), "r"(y)
                            );
        return ret;
}

int main()
{
        int x = 4;
        int y = 9;

        printf("asm_sum(%d, %d) return %d\n", x, y, asm_sum(x,y));


        return 0;
}

Above is a gcc inline assembly code, which I think is very simple, but when compile it I get the error

addup.c: Assembler messages:
addup.c:10: Error: junk `%eax' after register
addup.c:12: Error: junk `%edx' after register

anybody know what happen?????

EDIT:

Never forget the comma between operands... Also

cmp op1 op2 jle loop

would jump to loop only when op2 is less or equal than op1.

here is the code that working:

int asm_sum(int x, int y)
{
    int ret = 0;

    __asm__ __volatile__(   "loop:\t\n"
                            "   addl %1, %0\n\t"
                            "   inc %1\n\t"
                            "   cmp %2, %1\n\t"
                            "   jle loop"
                            :"=r"(ret)
                            :"r"(x), "r"(y)
                         );
    return ret;
}
有帮助吗?

解决方案

Got it, you forgot to specify commas between operands of addl and cmpl : )) took too long to notice that, too sleepy maybe

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top