Advanced asm in gcc: 'asm operand has impossible limitations

This strcpy function is for copying the contents of src to dest, and it works just fine: display the two lines of Hello_src.

#include <stdio.h>

static inline char * strcpy(char * dest,const char *src)
{
    int d0, d1, d2;
    __asm__ __volatile__("1:\tlodsb\n\t"
                         "stosb\n\t"
                         "testb %%al,%%al\n\t"
                         "jne 1b"
                         : "=&S" (d0), "=&D" (d1), "=&a" (d2)
                         : "0"(src),"1"(dest)
                         : "memory");
    return dest;
}

int main(void) {
    char src_main[] = "Hello_src";
    char dest_main[] = "Hello_des";
    strcpy(dest_main, src_main);
    puts(src_main);
    puts(dest_main);
    return 0;
}
  • I tried to change the line : "0"(src),"1"(dest)to : "S"(src),"D"(dest)be an error: ‘asm’ operand has impossible constraints. I just do not understand. I thought the "0" / "1" here indicated the same restriction as the 0th / 1st output variable. limitation of the 0th output =&S, i.e. 1st exit restriction =&D. If I change 0 → S, 1 → D, there should be no errors. What is the reason for this?

  • " " (&)? "&" "", : "Hello_src". "clobbered" ?

+4
1

& , , . , - . -, 0/1 .

, . . clobber, , . , . , , asm:

extern int foo();
int bar()
{
    int x = foo();
    asm("nop" ::: "eax");
    return x;
}

:

call    foo
movl    %eax, %edx
nop
movl    %edx, %eax

, foo edx, , eax . eax, , . , , asm eax, : .

+1

All Articles