I have the below inline assembly code:
int get_year(int a, int *b, char * c)
{
int ret, t1, t2;
asm (
\"addl %3, %[a] \\n\\
I can reproduce the problem if I compile your code with clang only when generating 64-bit code. When targeting 32-bit code there's no error. As Michael Petch said, this suggests the problem is the different sizes of the two operands.
It's not entirely clear what the best fix would be, as your asm statement doesn't make much sense. It's the equivalent of:
int get_year(int a, int *b, char *c) {
a += *b;
c[4] = 58;
return a;
}
There's no advantage to using an assembly statement to do what can be done more clearly and more efficiently using the C code above. So the best solution would be completely replace your code with the equivalent C code.
If you're just playing around with inline assembly, then equivalent inline assembly would be:
int get_year2(int a, int *b, char * c)
{
asm("addl %[b], %[a]"
: [a] "+r" (a)
: [b] "m" (*b)
: "cc");
asm("movb $58, %[c4]"
: [c4] "=rm" (c[4]));
return a;
}
I've used two asm statements because the two parts are unrelated. Keeping them separated provides for more opportunities for optimization. For example if you call this function but don't use the return value, the compiler can eliminate the first asm statement because its result isn't used and it has no side effects.
Instead of using a matching constraints, the "1" constraint that was giving you problems, I've used the "+" constraint modifier to mark the operand as both an input and an output. I find this works better. The constraint for the [b] operand should really be "rm" but unfortunately clang doesn't handle rm constraints well.
You probably noticed that I've used only two assembly statements where your example used four. The MOVL instruction isn't necessary, the compiler can handle moving the result in to the return value register, if necessary. Your last two assembly statements can be collapsed into one single statement that moves the constant directly into memory without clobbering a register. Speaking of which, your asm statement clobbers EFLAGS, the condition codes, so "cc" should be listed a clobbered, but as Peter Cordes notes it's not necessary with x86 targets but the compiler assumes they are anyways.