Searching \ for 'Assembler Question (MPASM)' in subject line. ()
Make payments with PayPal - it's fast, free and secure! Help us get a faster server
FAQ page:
Search entire site for: 'Assembler Question (MPASM)'.

Truncated match.
PICList Thread
'Assembler Question (MPASM)'
1999\10\25@152205 by Martin SchŠfer

Hi Pic freaks,

I'm programming pic's for some years now. But there are always some
remaining mysteries. One of them:

What is the difference between

                               counter        equ        0x20
                               #define        counter      0x20

If I use counter in my assembler code like

   movwf    counter

there seems to be no difference.

Thanks for answering a such simple question :-)


1999\10\25@154421 by Don Hyde

The code
counter equ     0x20

sets an "assembler constant" to the hex value 20.  The conversion of the
symbols "0x20" to a binary value happens when the "equ" line is scanned by
the assembler, which you can tell because if you make a typo, the error
message occurs at that point.  Later when you reference "counter", the
assembler performs a value substitution at that point, inserting the binary
value just as it would insert the value of a label.

the code
#define counter 0x20

tells the assembler to save the text "0x20".  Later when the assembler sees
"counter", it performs a textual substitution.  If you made a typo in the
0x20, the error message would appear at the point where you referred to
"counter", and not on the line where you made the actual typo.  Since it's
text, you can put anything into a #define that you want
(for instance #define Serial_In PORTC,7 ), and it works or doesn't work
depending on the context where you later use it.  In this case,
       btfsc   Serial_In
would work, but
       goto    Serial_In
would get an error because a comma is illegal in the target of a goto.

> {Original Message removed}

1999\10\25@155950 by Martin SchŠfer

Thank you Don,

but if you want to name a register, which code version would you prefer
and why? Normally one uses the 'equ'-code. But makes this any sense?


Don Hyde schrieb:

{Quote hidden}

1999\10\25@155959 by Ben Stragnell


The EQU directive typically creates an entry in the assembler's symbol
table, and
assigns a value to it.

#define is a preprocessor directive, which is more of a text-substitution

In the example you give, there is no real difference. However, in some
and in C, #define can be used in a much more versatile manner, for instance,
creating macros.


{Original Message removed}

1999\10\25@161236 by Martin SchŠfer

Thank you Ben,

what you wrote seems to be what I was looking for. If the EQU directive creates
an entry in the symbol table and the #define not, than debugging is only
with the EQU directive.

Similar to the EQU directive is also the SET directive. Where can you use the
in an efficient way?


Ben Stragnell schrieb:

{Quote hidden}

1999\10\25@161929 by Don Hyde

I would probably prefer the equ wherever it will work, because if you make a
mistake (like I do a lot), then the error message will be more likely to
happen where the mistake is.  The #define is really more of a macro, and
whenever you use one, as with any macro, you should be aware that you may be
obscuring what is really happening, and making a place for an error to slide
in where you won't see it when you stare at the code trying to figure out
what's happening.

After 30 years of programming, I've become convinced that anything that
makes it easier to see what's happening in the code, is contributing to
reliable code.  Use equ's or whatever you have to name registers, because
it's easier to understand.  I use #define's to name individual bits because
that makes code easier to understand, and when I rearrange the I/O pins,
there's only one place to change.  But don't use a #define or a macro to
make 3 lines of code look like one just to make the code shorter unless it
REALLY DOES make it easier to understand.

> {Original Message removed}

1999\10\25@163218 by Don Hyde

Usually, an entry in the symbol creates what assembler writers have called
an "assembler constant".  Usually, if for some reason, it changes, that's a
bad thing.  Most assemblers are "two pass", the first pass counts the
instructions and figures out what addresses they will load at (making
"assembler constants" for all the labels as it goes).  In the second pass,
the code is spewed out to wherever.  If the address of a label is different
in the second pass, then something's royally fouled up.

So, if you try to change the value of something by having two equ's for it,
the assembler complains, often with the obscure "phase error" message.

Sometimes in a macro, you need something that acts more like a variable.  If
you notice, MPASM macros actually have looping directives, so you can
actually write little code-writing (or more often table-writing) snippets of
code.  That's what the SET directive is for.  Basically, it's just an equ
with the "isn't allowed to change" rule relaxed.

These can be very powerful, but are often like a siren, luring programmers
into creating totally incomprehensible whirlpools of code.

> {Original Message removed}

1999\10\26@014555 by w. v. ooijen / f. hanneman

picon face
> In the example you give, there is no real difference. However, in some
> assemblers,
> and in C, #define can be used in a much more versatile manner, for
> for
> creating macros.

In MPASM too you can do nifty things with macros, like replacing an
opcode with your own version. But the macro space is limited,
in practice the use is limited.


More... (looser matching)
- Last day of these posts
- In 1999 , 2000 only
- Today
- New search...