Labels in GCC inline assembly

后端 未结 2 1480
礼貌的吻别
礼貌的吻别 2020-11-27 03:02

In my ongoing experimentation with GCC inline assembly, I\'ve run into a new problem regarding labels and inlined code.

Consider the following simple jump:



        
相关标签:
2条回答
  • 2020-11-27 03:20

    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).

    0 讨论(0)
  • 2020-11-27 03:32

    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 %eax
    jnz to_here14
    jz to_there14
    to_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");
    
    0 讨论(0)
提交回复
热议问题