21

Here's the list of register loading codes:

a eax
b ebx
c ecx
d edx
S esi
D edi
I constant value (0 to 31)
q,r dynamically allocated register (see below)
g eax, ebx, ecx, edx or variable in memory
A eax and edx combined into a 64-bit integer (use long longs)

But this is register constraints for intel i386. My question is where I can find the register constraints of intel x86_64 system, like:

? %r10
? %r8
? %rdx

and so on.

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
Jin Chen
  • 632
  • 1
  • 4
  • 10

2 Answers2

18

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.

youfu
  • 1,547
  • 3
  • 15
  • 21
13

The machine specific constraints have a section in the gcc manual - the ugly details are found in config/i386/constraints.md.

Some constraints have different meanings for x86-64, e.g., q is %eax,%ebx,%ecx,%edx in 32-bit mode; in 64-bit mode, it's any general purpose integer register - and essentially the same as the r constraint. Specific registers names like a now refer to %rax, d to %rdx, etc.

There are, however, no special constraints or names for %r8 .. %r15. There's an excellent (x86-64 specific) tutorial on inline assembly and constraint use here.

Brett Hale
  • 21,653
  • 2
  • 61
  • 90
  • Thanks very much. I Google that for long time but get nothing. I even can't find something pointing out it doesn't exist. – Jin Chen Jun 18 '13 at 14:09
  • I have downvoted your answer because it does not actually answer OPs question, instead deferring the answer to an external resource. Stack Overflow answers must be self contained. Please extend your answer to encompass the relevant parts of the resource you linked. – fuz Nov 16 '20 at 19:57
  • 1
    You could at least mention that `register ... asm` local variables are the way to force `"r"` to pick the register you want, like the other answer shows. That's also what you do on some non-x86 ISAs where there typically aren't any specific-register constraints. – Peter Cordes Nov 17 '20 at 04:11
  • @BrettHale What Peter says. As long as your answer does not actually explain how to achieve what OP wants, my downvote stays. – fuz Nov 17 '20 at 10:11