ARM Assembly Local Labels

后端 未结 2 343
梦谈多话
梦谈多话 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:19

    The important difference is that the numbered local labels can be reused without worry and that is why you need to specify the direction too. You can jump to preceding or following one, but not the ones beyond them.

    1: foo
    ...
    1: bar
    ...
    jmp 1b # jumps to bar
    ...
    jmp 1f # jumps to baz
    ...
    1: baz
    ...
    1: qux
    ...
    jmp 1b # jumps to qux
    

    As long as you only use them within a single block only, you can be sure they will work as intended and not conflict with anything else.

    0 讨论(0)
  • 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: <more instructions>
       ...
       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.

    0 讨论(0)
提交回复
热议问题