ARM Assembly Local Labels

后端 未结 2 358
梦谈多话
梦谈多话 2021-01-02 08:27

I\'m currently reading a tutorial on Raspberry Pi OS development and was wondering about the way local labels are used in this code snippet (GCC ARM Assembly):



        
2条回答
  •  無奈伤痛
    2021-01-02 09:23

    One of the main benefits of local labels is that since the same identifier can appear multiple times, they can be used in macros. Consider some hypothetical local label usage like this, though:

       .macro dothething rega regb ptr
       ldrex \regb, [\ptr]
       cmp \rega, \regb
       beq 1
    2: 
       ...
       strex \regb, \rega, [ptr]
       cmp \regb, #0
       bne 2
    1:
       .endm
    
    myfunction:
       dothething r0 r1 r2
       dothething r0 r1 r3
       bx lr
    

    That's actually allowed in armasm (albeit with slightly different syntax), where the behaviour in the absence of a specified direction is "search backwards, then forwards", but still under any reasonable default behaviour at least one of the jumps in the above code is going to end up targeting the wrong instance of a label. Explicitly calling out the direction with beq 1f and bne 2b in the macro resolves the ambiguity and generates the right jumps in both invocations of the macro.

    If you choose to use something that isn't a true local label, then not only do you potentially clutter up your symbol table with junk, but you also rob yourself of being able to use loops or conditional branching in macros since you'd generate non-unique symbols. My example might seem a bit contrived, but switch from assembler macros to inline asm blocks in C functions which get inlined all over your complex codebase, and things get a lot more real.

提交回复
热议问题