This is the mail archive of the binutils@sourceware.org mailing list for the binutils project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

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


On 30.04.12 20:49, R. Diez wrote:
> By the way, I did take the time to do some more tests, and your first
> recommendation already proved wrong.

No, the example you have posted (quoted below) is NOT the example
suggested, which was in reality:

   .macro MACRO_A
   1:

   MACRO_B whatever

   jump 1b

   1:                      // A "jump 1f" in MACRO_B allows exit.
   .endm

Please _try_ it. (Optionally with any "2:" to "9:" label in MACRO_B, if
fiddling with examples absolutely must be done before they have been
tried at all.)

If you find your own construction:

> .macro MACRO_B
> 
> Â 1:ÂÂÂ l.nop 2
> ÂÂÂÂÂÂÂ l.j 1b
> 
> .endm
> 
> .macro MACRO_A
> 
> Â 1:Â l.nop 1
> ÂÂÂÂÂ MACRO_B
> ÂÂÂÂÂ l.j 1b
> 
> .endm

to be "wrong", then feel free to stop doing it. Here the problem is
still failure to read "info as", which is enormously more informative
than the inadequate manpage. Perhaps the easiest way to grab a copy is:

$ sudo apt-get install binutils-doc

Since grubbing about in tree-structured documentation is a pain, just
hit "/label_". That search takes you directly to an enlightening
example, under the heading of "Local Labels".

Armed with understanding from the "info" documentation, we see that 1b
refers to the nearest "1:" backwards. To reach past that, to a more
distant local label, change "1:" and "1b" in MACRO_B to "2:" and "2b".
(Or conversely, make the more distant local label "2:" or "9:".)

And, as explained in the additional help in my code comment above, the
example can be extended to also demonstrate the 1b/1f working:

   .macro MACRO_B
      l.j 1f
   .macro

But as soon as you get around to reading the available documentation,
the penny drops, and it's all dead simple, innit?  ;-)

Using just this one form of local labels, it is quite practical to make
up a small suite of macros which implement IF-ENDIF constructs in
assembler, with the jumps auto-adjusting when an ELSE is interposed.
It is only necessary to refrain from using the same labels in any
intervening code. But the same labels are used in any number of IF
constructs. There is no risk of conflict, if used correctly.

Erik

-- 
The ultimate barrier is one's viewpoint.
      - Terry Pratchett, "The Dark Side of the Sun"


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]