What's the best way to define local labels in an assembler macro?

Erik Christiansen dvalin@internode.on.net
Mon Apr 30 19:49:00 GMT 2012


Please keep this on-list.

On 30.04.12 13:53, R. Diez wrote:
> > The second "1:" demonstrates label re-use, and forward/backward
> > referencing.
> 
> Thanks for your answer.
> 
> What happens if MACRO_B defines itself a "1:" label? It will collide
> with that of calling MACRO_A, won't it?

You appear to have neither tried the given examples, nor read "info as".
Please do both, as advised. Then try the posted alternative to your
example, to easily answer that question.

> > Since \@ merely adds numbers, taken from the count of macro
> > invocations, to the label name, it's just another way of making the
> > label unique.
> 
> What happens if several nested macro invocations use \@ ? At which
> points will this variable be incremented then? I mean, say MACRO_A
> defines a label based on \@, then invokes MACRO_B and MACRO_C, then it
> tries to reference that \@ label, will the macro invocations count
> have changed in the meantime?

See "info as". It is also immediately clear once you expend the effort
of trying it out, even without prior reading. Learning by doing
surpasses all other methods. (Not least if unfamiliarity with English is
hampering comprehension of the documentation and advice already given.)

Try invoking:

     unique_labels_x3 alpha        ; Labels .LL0001 - 3, lbl_0 - 2, &
     unique_labels_x3 beta         ; ulx_alpha, ulx_beta, ulx_gamma
     unique_labels_x3 gamma        ; are generated.

Have a look not only at the labels generated, but also under the heading
"DEFINED SYMBOLS" in the .lst file your assembly generates. Then try
nesting macros.

Incidentally, there's a typo in the unique_labels_x3 demo macro.
It should be "jmp ulx_\dest", for full didactic benefit.
(And jumps to the other labels could be added, to show the assembler
using them all.)

Happy experimenting!

Erik

-- 
Documentation is like sex: when it is good, it is very, very good;
and when it is bad, it is better than nothing.
                                                   - Dick Brandon



More information about the Binutils mailing list