Local ARM assembly tags

I am currently reading a tutorial on Raspberry Pi OS development and wondered how local labels are used in this piece of code (GCC ARM Assembly):

... b 2f 1: stmia r4!, {r5-r8} 2: cmp r4, r9 blo 1b ... 

If you use 1: as a label, you must specify either f or b after the jump instruction so that the assembler knows in which direction the scope is pointing. As far as I know, you can also use this:

  ... b .2 .1: stmia r4!, {r5-r8} .2: cmp r4, r9 blo .1 ... 

I think this parameter is much less confusing (local labels are also marked with a dot in the x86 assembly) because there is no extra letter after linking to the label. I checked the resulting machine code, and it is the same. So my questions are:

  • Why are you using one option over another?

  • Why is it necessary to indicate the direction of the transition with f or b ?

+5
source share
2 answers

An important difference is that numbered local labels can be reused without problems, and therefore you need to also indicate the direction. You can go to the previous or next, but not to those that are behind 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 use them in only one block, you can be sure that they will work as intended and do not conflict with anything else.

+6
source

One of the main advantages of local labels is that since the same identifier can appear several times, they can be used in macros. Consider some hypothetical local labels like this:

  .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 

In fact, this is permitted in armasm (albeit with a slightly different syntax), where the behavior in the absence of the specified direction is a search back, then forward ", but with any reasonable default behavior, at least one of the jumps in the above code will be aimed to an incorrect label instance. Explicitly causing a direction with beq 1f and bne 2b in a macro, resolves the ambiguity and generates the correct jumps in both macro calls.

If you decide to use something that is not a true local label, then you will not only interfere with your character table with garbage, but you will also be robbed of the possibility of using loops or conditional branches in macros, since you generate unique characters. My example may seem a little far-fetched, but switch from assembler macros to asm built-in blocks in C functions that become embedded throughout your complex code base, and things get a lot more real .

+1
source

All Articles