简体   繁体   中英

Inline assembly code error

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

知道了,您忘了在addlcmpl操作数之间指定逗号cmpl )花了很长时间才注意到,也许太困了

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM