How to specify register constraints on the Intel x86_64 register r8 to r15 in GCC inline assembly?

前端 未结 2 1014
傲寒
傲寒 2020-12-11 00:38

Here\'s the list of register loading codes:

a eax
b ebx
c ecx
d edx
S esi
D ed

2条回答
  •  被撕碎了的回忆
    2020-12-11 01:36

    GCC doesn't provide such constraint for registers like r10, r8. However, you can make use of a feature called Local Register Variables. Please read the document carefully before using this feature, especially the warning paragraph.

    For example:

    static inline __attribute__((always_inline))
    long syscall4(long n, long a1, long a2, long a3, long a4) {
        long ret;
        register long r10 __asm__("r10") = a4;
        __asm__ __volatile__ (
            "syscall\n\t"
            : "=a"(ret)
            : "a"(n),
              "D"(a1),
              "S"(a2),
              "d"(a3),
              "r"(r10)
            : "memory",
              "rcx",
              "r11"
        );
        return ret;
    }
    

    See also musl's implementation of syscall stubs.

提交回复
热议问题