Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.programming > #2127 > unrolled thread

Implementing Local Labels in an Assembler

Started by"Charles Richmond" <numerist@aquaporin4.com>
First post2012-08-30 18:15 -0500
Last post2012-08-31 20:27 +0100
Articles 9 — 6 participants

Back to article view | Back to comp.programming


Contents

  Implementing Local Labels in an Assembler "Charles Richmond" <numerist@aquaporin4.com> - 2012-08-30 18:15 -0500
    Re: Implementing Local Labels in an Assembler Daniel Pitts <newsgroup.nospam@virtualinfinity.net> - 2012-08-30 16:35 -0700
    Re: Implementing Local Labels in an Assembler James Harris <james.harris.1@gmail.com> - 2012-08-31 00:07 -0700
    Re: Implementing Local Labels in an Assembler "BartC" <bc@freeuk.com> - 2012-08-31 11:29 +0100
      Re: Implementing Local Labels in an Assembler Nomen Nescio <nobody@dizum.com> - 2012-08-31 18:34 +0200
        Re: Implementing Local Labels in an Assembler "BartC" <bc@freeuk.com> - 2012-08-31 20:30 +0100
          Re: Implementing Local Labels in an Assembler Anonymous <nobody@remailer.paranoici.org> - 2012-09-02 09:10 +0000
      Re: Implementing Local Labels in an Assembler "Charles Richmond" <numerist@aquaporin4.com> - 2012-08-31 14:07 -0500
        Re: Implementing Local Labels in an Assembler "BartC" <bc@freeuk.com> - 2012-08-31 20:27 +0100

#2127 — Implementing Local Labels in an Assembler

From"Charles Richmond" <numerist@aquaporin4.com>
Date2012-08-30 18:15 -0500
SubjectImplementing Local Labels in an Assembler
Message-ID<k1os71$8om$1@dont-email.me>
Just as an exercise for myself, I am attempting to write an assembler for an 
old 8-bit microprocessor. The assembler will be written in C.

I am at a loss to figure out how to handle local labels.  My understanding 
is:  A local label in this assembly language is one that begins with a "$" 
(dollar sign).  Such labels only have the scope of between two non-local 
labels. So I can't just toss them into the symbol table with the other 
labels, unless there is some special processing here.

Can anyone suggest how I might handle local labels, or point me to some 
explanation on the internet of how to do this???


--
numerist at aquaporin4 dot com 

[toc] | [next] | [standalone]


#2128

FromDaniel Pitts <newsgroup.nospam@virtualinfinity.net>
Date2012-08-30 16:35 -0700
Message-ID<3NS%r.297$wr2.231@newsfe08.iad>
In reply to#2127
On 8/30/12 4:15 PM, Charles Richmond wrote:
> Just as an exercise for myself, I am attempting to write an assembler
> for an old 8-bit microprocessor. The assembler will be written in C.
>
> I am at a loss to figure out how to handle local labels.  My
> understanding is:  A local label in this assembly language is one that
> begins with a "$" (dollar sign).  Such labels only have the scope of
> between two non-local labels. So I can't just toss them into the symbol
> table with the other labels, unless there is some special processing here.
>
> Can anyone suggest how I might handle local labels, or point me to some
> explanation on the internet of how to do this???
First thought off the top of my head: Add them to the symbol tables, but 
remove all "$" labels then when you reach a non-local label.

Option two: Have a separate "local" symbol table that is checked first. 
  Clear it when appropriate.

[toc] | [prev] | [next] | [standalone]


#2129

FromJames Harris <james.harris.1@gmail.com>
Date2012-08-31 00:07 -0700
Message-ID<3864a1d0-7caf-4b13-8f31-1c37d35288b6@d7g2000vbv.googlegroups.com>
In reply to#2127
On Aug 31, 12:15 am, "Charles Richmond" <numer...@aquaporin4.com>
wrote:
> Just as an exercise for myself, I am attempting to write an assembler for an
> old 8-bit microprocessor. The assembler will be written in C.
>
> I am at a loss to figure out how to handle local labels.  My understanding
> is:  A local label in this assembly language is one that begins with a "$"
> (dollar sign).  Such labels only have the scope of between two non-local
> labels. So I can't just toss them into the symbol table with the other
> labels, unless there is some special processing here.

I take it the problem is you could have duplicate local labels? If so
why not prepend the preceding non-local label? So if you had

top:
  ...code...
$local:

store these labels in the symbol table as

  "top"
  "top-local"

Then you can just treat the local label like any other. (To separate
the two parts you need a character that cannot be part of a normal
label. I used a hyphen in the example.)

James

[toc] | [prev] | [next] | [standalone]


#2132

From"BartC" <bc@freeuk.com>
Date2012-08-31 11:29 +0100
Message-ID<k1q3pr$4gj$1@dont-email.me>
In reply to#2127

"Charles Richmond" <numerist@aquaporin4.com> wrote in message 
news:k1os71$8om$1@dont-email.me...
> Just as an exercise for myself, I am attempting to write an assembler for 
> an old 8-bit microprocessor. The assembler will be written in C.
>
> I am at a loss to figure out how to handle local labels.

> Can anyone suggest how I might handle local labels, or point me to some 
> explanation on the internet of how to do this???

I had a similar problem, with labels that were defined in the inline 
assembler of a HLL had file-scope, which meant each function needed a unique 
set of label names (which was a PITA).

So I used a scheme where local labels were prefixed with "#": #abc, #123, 
etc (with the extra advantage I could use numeric labels, so I didn't need 
to be so creative...).

The compiler then changed each "#" prefix to an identifier unique to the 
function; this could have been the function name, but I chose an ordinal 
value that incremented with each function, to keep things compact: so 
f33_abc, f33_123, etc.

> I am at a loss to figure out how to handle local labels.  My understanding 
> is:  A local label in this assembly language is one that begins with a "$" 
> (dollar sign).  Such labels only have the scope of between two non-local 
> labels. So I can't just toss them into the symbol table with the other 
> labels, unless there is some special processing here.
>

OK, so a label without the special prefix effectively partitions code into 
'functions', although I would be tempted to use a special designator for 
this, such as a 'function' directive; I assume you can customise the syntax 
of this assembler.

That last non-$ label (or the 'function' directive if it also has a name), 
is used to generate a unique prefix which can be added to the following 
$labels.

This would obviously be done both for $-labels which are actual labels, and 
those which are used as operands (jump instructions and such).

The main problem might be if you want to refer to those $-labels from 
outside that block; using a separate 'function' directive would have the 
advantage of allowing you to use ordinary labels for this purpose.

-- 
Bartc 

[toc] | [prev] | [next] | [standalone]


#2134

FromNomen Nescio <nobody@dizum.com>
Date2012-08-31 18:34 +0200
Message-ID<16a4802da50f44519c40a60aaa7d5b05@dizum.com>
In reply to#2132
What is the benefit of local labels? Do you really mean to allow duplicate
labels or do you just not want to export certain labels to the linker, or
something else?

If duplicate labels, you could make a rule that the closest reference
wins. I really don't like duplicate labels because I work on huge pieces of
code and these kinds of stupid assembly tricks make doing that harder than
it has to be. If you have a way you can clearly understand what is inside a
function and what isn't, you could generate a unique symbol table name by
function.local_label_name when the user codes local_label_name.

If not exporting the label to the linker, I should think you can do that
automagically, but then I don't know how you've decided to do your symbol
table management and elf header generation (or whatever you have to export
to the linker in your OS). In my case I simply don't export any labels
unless there's an assembler directive coded to do so and that includes
functions as well (all functions are local unless declared otherwise). I
find requiring directives is a lot more obvious and efficient than assuming
everything is exportable, and it cuts down dramatically on useless externs.

I've seen other assemblers that use various syntax to declare local labels
ddepending on whether there are forward or backward references to them. I
can't remember which, but check nasm and fasm. Doc and source is available
for both. The system whichever of them uses seems workable and the authors
are still around to ask questions.


"BartC" <bc@freeuk.com> wrote:

> 
> 
> "Charles Richmond" <numerist@aquaporin4.com> wrote in message 
> news:k1os71$8om$1@dont-email.me...
> > Just as an exercise for myself, I am attempting to write an assembler for 
> > an old 8-bit microprocessor. The assembler will be written in C.
> >
> > I am at a loss to figure out how to handle local labels.
> 
> > Can anyone suggest how I might handle local labels, or point me to some 
> > explanation on the internet of how to do this???
> 
> I had a similar problem, with labels that were defined in the inline 
> assembler of a HLL had file-scope, which meant each function needed a unique 
> set of label names (which was a PITA).
> 
> So I used a scheme where local labels were prefixed with "#": #abc, #123, 
> etc (with the extra advantage I could use numeric labels, so I didn't need 
> to be so creative...).
> 
> The compiler then changed each "#" prefix to an identifier unique to the 
> function; this could have been the function name, but I chose an ordinal 
> value that incremented with each function, to keep things compact: so 
> f33_abc, f33_123, etc.
> 
> > I am at a loss to figure out how to handle local labels.  My understanding 
> > is:  A local label in this assembly language is one that begins with a "$" 
> > (dollar sign).  Such labels only have the scope of between two non-local 
> > labels. So I can't just toss them into the symbol table with the other 
> > labels, unless there is some special processing here.
> >
> 
> OK, so a label without the special prefix effectively partitions code into 
> 'functions', although I would be tempted to use a special designator for 
> this, such as a 'function' directive; I assume you can customise the syntax 
> of this assembler.
> 
> That last non-$ label (or the 'function' directive if it also has a name), 
> is used to generate a unique prefix which can be added to the following 
> $labels.
> 
> This would obviously be done both for $-labels which are actual labels, and 
> those which are used as operands (jump instructions and such).
> 
> The main problem might be if you want to refer to those $-labels from 
> outside that block; using a separate 'function' directive would have the 
> advantage of allowing you to use ordinary labels for this purpose.
> 
> -- 
> Bartc 
> 





















[toc] | [prev] | [next] | [standalone]


#2140

From"BartC" <bc@freeuk.com>
Date2012-08-31 20:30 +0100
Message-ID<k1r3e0$gg4$1@dont-email.me>
In reply to#2134

"Nomen Nescio" <nobody@dizum.com> wrote in message 
news:16a4802da50f44519c40a60aaa7d5b05@dizum.com...
> What is the benefit of local labels? Do you really mean to allow duplicate
> labels or do you just not want to export certain labels to the linker, or
> something else?

Local labels are the equivalent of local variables in functions. Same 
benefit; you don't have to worry about name clashes anywhere else.

-- 
Bartc 

[toc] | [prev] | [next] | [standalone]


#2142

FromAnonymous <nobody@remailer.paranoici.org>
Date2012-09-02 09:10 +0000
Message-ID<034294eeb733066f53bec76d02f52769@remailer.paranoici.org>
In reply to#2140
"BartC" <bc@freeuk.com> wrote:

> 
> 
> "Nomen Nescio" <nobody@dizum.com> wrote in message 
> news:16a4802da50f44519c40a60aaa7d5b05@dizum.com...
> > What is the benefit of local labels? Do you really mean to allow duplicate
> > labels or do you just not want to export certain labels to the linker, or
> > something else?
> 
> Local labels are the equivalent of local variables in functions. Same 
> benefit; you don't have to worry about name clashes anywhere else.

Then use internal dot qualification.

> 
> -- 
> Bartc 
> 








[toc] | [prev] | [next] | [standalone]


#2137

From"Charles Richmond" <numerist@aquaporin4.com>
Date2012-08-31 14:07 -0500
Message-ID<k1r220$66d$1@dont-email.me>
In reply to#2132
"BartC" <bc@freeuk.com> wrote in message news:k1q3pr$4gj$1@dont-email.me...
>
>     [snip...]                         [snip...] 
> [snip...]
>
> OK, so a label without the special prefix effectively partitions code into 
> 'functions', although I would be tempted to use a special designator for 
> this, such as a 'function' directive; I assume you can customise the 
> syntax of this assembler.
>
> That last non-$ label (or the 'function' directive if it also has a name), 
> is used to generate a unique prefix which can be added to the following 
> $labels.
>
> This would obviously be done both for $-labels which are actual labels, 
> and those which are used as operands (jump instructions and such).
>
> The main problem might be if you want to refer to those $-labels from 
> outside that block; using a separate 'function' directive would have the 
> advantage of allowing you to use ordinary labels for this purpose.
>

The two regular assembly language labels *bounding* the instructions that 
contain one or more *local* labels... define the scope of those local 
labels.  These local labels can *not* be referenced outside of that scope.

However, the *same* local label can appear in multiple "scopes".  There's 
the problem...

--

numerist at aquaporin4 dot com

[toc] | [prev] | [next] | [standalone]


#2139

From"BartC" <bc@freeuk.com>
Date2012-08-31 20:27 +0100
Message-ID<k1r38n$f9n$1@dont-email.me>
In reply to#2137
"Charles Richmond" <numerist@aquaporin4.com> wrote in message 
news:k1r220$66d$1@dont-email.me...
> "BartC" <bc@freeuk.com> wrote in message 
> news:k1q3pr$4gj$1@dont-email.me...

>> That last non-$ label (or the 'function' directive if it also has a 
>> name), is used to generate a unique prefix which can be added to the 
>> following $labels.

> The two regular assembly language labels *bounding* the instructions that 
> contain one or more *local* labels... define the scope of those local 
> labels.  These local labels can *not* be referenced outside of that scope.
>
> However, the *same* local label can appear in multiple "scopes".  There's 
> the problem...

So each has to be renamed inside it's own scope. Or at least, that's one 
simple solution.

Exactly how you do that depends on how your assembler works. Here's one way:

o Set Z to 0
o When a regular label is defined, add 1 to Z
o When a $ label is defined or encountered, add Z (converted to text) as a 
prefix

-- 
Bartc 

[toc] | [prev] | [standalone]


Back to top | Article view | comp.programming


csiph-web