How is "+ & r" different from "+ r"?

The built-in assembler GCC recognizes declarators =rand =&r. This makes sense to me: =rallows the assembler to reuse the input register for output.

However, the GCC built-in assembler also recognizes +rand declarations +&r. This is not so important for me. After all, is not the difference between +rand the +&rdifference without difference? Isn't it enough just +rto tell the compiler to reserve a register for the single use of a single variable?

For example, what is wrong with the following GCC code?

#include <stdio.h>
int main()
{
    int a = 0;
    printf("Initially, a == %d.\n", a);
    /* The architecture is amd64/x86-64. */
    asm(
        "inc %[a]\n"
        : [a] "+r" (a)
        : : "cc"
    );
    printf("However, after incrementation, a == %d.\n", a);
    return 0;
}

, , (, ) +r , , , . , ?

8- 16- , .

+4
1

GCC , , . , , , clobber &, , , . , =, /, +. , :

int
foo() {
    int a = 1;
    asm("add %1, %0" : "+r" (a) : "r" (1));
    return a;
}

int
bar() {
    int a = 1;
    asm("add %1, %0\n\t"
        "add %1, %0"
        : "+r" (a) : "r" (1));
    return a;
}

, foo , , bar, . GCC :

_foo:
    movl    $1, %eax
/APP
    add %eax, %eax
/NO_APP
    ret

_bar:
    movl    $1, %eax
/APP
    add %eax, %eax
    add %eax, %eax
/NO_APP
    ret

GCC EAX . foo, , bar 4 3.

bar clobber:

int
baz() {
    int a = 1;
    asm("add %1, %0\n\t"
        "add %1, %0"
        : "+&r" (a) : "r" (1));
    return a;
}
_baz:
    movl    $1, %eax
    movl    %eax, %edx
/APP
    add %edx, %eax
    add %edx, %eax
/NO_APP
    ret

baz GCC , , / , .

+7

Source: https://habr.com/ru/post/1684525/


All Articles