views:

143

answers:

1

When embedding assembly code into a C/C++ program, you can avoid clobbering registers by saving them with a push instruction (or specify clobber list of compiler supports it).

If you are including assembly inline and want to avoid the overhead of pushing and popping clobbered registers, is there a way of letting gcc choose registers for you (e.g. ones it knows have no useful info in them).

+7  A: 

Yes. You can specify that you want a particular variable (input or output) to be stored in a register, but you don't have to specify a register. See this document for a detailed explanation. Essentially, the inline assembly looks like this:

asm("your assembly instructions"
      : output1("=a"),  // I want output1 in the eax register
        output2("=r"),  // output2 can be in any general-purpose register
        output3("=q"),  // output3 can be in eax, ebx, ecx, or edx
        output4("=A")   // output4 can be in eax or edx
      : /* inputs */
      : /* clobbered registers */
   );
Adam Rosenfield
Thanks! I actually skimmed that file before posting but missed it. Now having taken a long time just to read those small sections I can see why! It's not pretty, but it works. Thanks again.
bugmenot77