Labels in GCC inline assembly Labels in GCC inline assembly c c

Labels in GCC inline assembly


A declaration of a local label is indeed a number followed by a colon. But a reference to a local label needs a suffix of f or b, depending on whether you want to look forwards or backwards - i.e. 1f refers to the next 1: label in the forwards direction.

So declaring the label as 1: is correct; but to reference it, you need to say jmp 1f (because you are jumping forwards in this case).


Well, this question isn't getting any younger, but there are two other interesting solutions.

1) This example uses %=. %= in an assembler template is replaced with a number that is "unique to each insn in the entire compilation. This is useful for making local labels that are referred to more than once in a given insn." Note that to use %=, you (apparently) must have at least one input (although you probably don't have to actually use it).

int a = 3;asm (    "test %0\n\t"    "jnz to_here%=\n\t"    "jz to_there%=\n\t"    "to_here%=:\n\t"    "to_there%=:"    ::"r" (a));

This outputs:

test %eaxjnz to_here14jz to_there14to_here14:to_there14:

Alternately, you can use the asm goto (Added in v4.5 I think). This actually lets you jump to c labels instead of just asm labels:

asm goto ("jmp %l0\n" : /* no output */ : /* no input */ : /* no clobber */ : gofurther);printf("Didn't jump\n");// c label:gofurther:printf("Jumped\n");