2

I have the a few questions regarding a piece of code I am trying to understand. I have read the manual here. But it does not seem to explain or answer all of the tricks being used here.

Here is the code:

#define SAMPLE_OFFSET1 0
#define SAMPLE_OFFSET2 100
#define SAMPLE_OFFSET1 1000
#define STRINGIFY(s) #s

struct data {
    int m1;
    int m2;
    const uint *m3;
    const uint *m4;
};

#define assembly_function(param1, ... param15) \
... body \
... body \
... body \
... body

void testFunction (data* c, uint *const sample) {
int bat, tmp;
    __asm__ volatile(
        assembly_function("%0", "%q0", "(%4)", "%1", "%w1",
                             "%2", "%q2", "%3", "%b3",
                             "%c6(%5)", "%c7(%5)",
                             STRINGIFY(SAMPLE_OFFSET1),
                             STRINGIFY(SAMPLE_OFFSET2),
                             STRINGIFY(SAMPLE_OFFSET3),
                             "%8")
        : "=&r"(bat), "=&r"(c->m1), "=&r"(c->m2), "=&q"(tmp)
        : "r"(sample), "r"(c),
          "i"(offsetof(data, m3)),
          "i"(offsetof(data, m4)),
          "1"(c->m1), "2"(c->m2)
        : "%rcx", "memory"
    );
}

I have wild guesses about the usage of some of the following constraints/options. But I feel its better to confirm from other fellows (or get link to some detailed manual).

  • %c in "%c6(%5)" and "%c7(%5)"
  • %q in "%q0" and "%q2",
  • %b in "%b3"
  • %w in "%w1"
  • parentheses in "(%4)"
  • "i" for two of the input parameters.
  • "1" and "2" for two of the input parameters.

Thanks for your help.

1
  • 3
    Stay away from tricks. Stick to plain and simple. Things can get tricky enough anyway. Commented Feb 18, 2015 at 19:12

1 Answer 1

1

%c forces output as a constant address, here it is used to get rid of the $ normally emitted in at&t syntax mode for immediates (that the i constraint implies).

The %b, %w, %l and %q are size override modifiers, they force the appropriate sized register (that is byte, word, long or quad) for the operand.

The parentheses are just part of your everyday at&t style effective address syntax.

i is an immediate integer operand (one with constant value).

"1" and "2" are matching constraints, that is place them in the same location as the indicated other operands.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for your comment. Regarding %c, would be it correct to understand that "%c6(%5)" == sum of the values of %5 and %6 and use this as an constant address in the memory from where we can only read but cannot write?
No, the address is constant not the value. It's a foo* const not a const foo*.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.