Searching \ for 'Macro- How to use?' in subject line. ()
Make payments with PayPal - it's fast, free and secure! Help us get a faster server
FAQ page: www.piclist.com/techref/index.htm?key=macro+how+use
Search entire site for: 'Macro- How to use?'.

Truncated match.
PICList Thread
'Macro- How to use?'
1998\06\17@155433 by

picon face
Hello

It has been suggested I write some macros to simplify my code. I haven't used
them before. The MPASM assemblers guide is limited. I have a couple of
questions. Please note I am a beginner

In my code I am struggling to keep table and references to the PCL within the
first 255 addresses. The guide states macros must be defined prior to their
use. If I define them at the beginning of my code will they occupy those
addresses?

Is it appropriate to define macros using an include file and simply make
reference to them at the beginning of the program?

Is there a straight forward tutorial that could help me understand how to
implement macros? I have all the most popular PIC books but they don't go into
macros.

Is the Parrallax assembler a compilation of macros?

1998\06\17@171723 by Martin Green

flavicon
face
It has been suggested I write some macros to simplify my code. I haven't used
them before. The MPASM assemblers guide is limited. I have a couple of
questions. Please note I am a beginner

In my code I am struggling to keep table and references to the PCL within the
first 255 addresses. The guide states macros must be defined prior to their
use. If I define them at the beginning of my code will they occupy those
addresses?

    Macros occupy NO code space. When they are invoked they generate code
    that DOES take up code space.

Is it appropriate to define macros using an include file and simply make
reference to them at the beginning of the program?

    Yes.

Is there a straight forward tutorial that could help me understand how to
implement macros? I have all the most popular PIC books but they don't go into
macros.

    Macros are fairly easy to use, get the MPASM reference from the Microchip
    web site. The chapter on macros is only a few pages long.

Is the Parrallax assembler a compilation of macros?

    Can't answer this one for sure, but I would guess not, although it does
    support macros.


    CIAO - Martin.

1998\06\17@172411 by Thomas McGahee

flavicon
face
See comments added to text.

----------
> From: <Nichole Petty> <spam_OUTPHXSYSTakeThisOuTspamAOL.COM>
> To: .....PICLISTKILLspamspam@spam@MITVMA.MIT.EDU
> Subject: Macro- How to use?
> Date: Wednesday, June 17, 1998 3:12 PM
>
> Hello
>
> It has been suggested I write some macros to simplify my code. I haven't used
> them before. The MPASM assemblers guide is limited. I have a couple of
> questions. Please note I am a beginner
>
> In my code I am struggling to keep table and references to the PCL within the
> first 255 addresses. The guide states macros must be defined prior to their
> use. If I define them at the beginning of my code will they occupy those
> addresses?
>

A macro *definition* uses no code space at all. When the macro is *referenced*
in the body of your assembler code, *then* the assembler will produce
actual code.

The assembler will decode the macro and its arguments and place the
required program code wherever the user has invoked the macro.

You can define a macro and then decide never to use it. Perfectly OK.

> Is it appropriate to define macros using an include file and simply make
> reference to them at the beginning of the program?
>

Perfectly OK. The only thing about include files is that unless you
have a copy of the include file you have no idea what the macro does.
Unless, of course, you specify that information in comments within the
program.

In many cases it is useful to include a comment that gives the user
the same sort of info that a C prototype would provide. In other
words, you don't have to specify HOW the macro works, but you say
in effect, "here is the name of a Macro I will be using, and
here are the parameters"  Personally, I always include a comment
that tells me the END result of what it does. For example:
;Macro MULT8_X_Y <X,Y,Z>  *** Multiply 8 bit X times 8 bit Y and put
;       result in 16 bit Z.

When preparing documentation, don't forget to include a listing of
all included files.

> Is there a straight forward tutorial that could help me understand how to
> implement macros? I have all the most popular PIC books but they don't go
into
> macros.

I am not personally aware of any. But working with macros is really
fairly stright-forward. What I suggest you do is experiment
writing simple macros. Then progress to more involved macros once
you have a good feel for the fundamentals. At the beginning keep
things simple. Assemble and see what the assembler produces. Go
from there.

>
> Is the Parrallax assembler a compilation of macros?

In a certain sense, every assembler is a compilation of macros.
They just don't call them macros. Whether or
not the Parallax assembler was specifically designed that way or not, I
do not know. What I do know is that you can write macros to implement
any of the Parallax language you want to emulate in MPASM. There are a
few gotchas, of course. For example, you cannot use as a macro name any
already existing directive/instruction. For obvious reasons :)
But you can easily add the Parallax style pseudo-8051 instructions.
There is a certain danger in using macros this way, and that is that
the assembler code does not immediately reveal HOW the instruction
is being implemented, and so you might think an instruction is a
single byte instruction, when in reality it is composed of several
single byte instructions. As has already been pointed out on this PIC
list this week, doing a BTFSS may not do what you expect, if the
following "instruction" is not single byte, but actually a macro that
is two or more bytes long. Gotcha!

On a slightly different note, it is possible to write an assembler
style program that gets almost all its info from a special "include"
file. The contents of the file determine the target processor. Some
companies produce such universal assemblers. They are quite useful
because the programmer deals with the same basic product no matter
what processor he is writing for at the moment. Every assembler
has its own personal quirks. It is nice if the number of quirks
you have to remember are few in number, and always the SAME quirks!

Hope this helps.

1998\06\17@185238 by Calvin

flavicon
face
Macro definitions will not use PIC space. The macro instructions are
inserted in place of the macro call wherever it is found by the assembler.
So the space used will be the same as if you inserted the code inline
yourself.

Calvin

{Original Message removed}

1998\06\18@032921 by Dr. Imre Bartfai
flavicon
face
What is the Parallax assembler you suppose to support macros?

I use SPASM 4.7 and it does NOT support macros.

Regards,
Imre


On Wed, 17 Jun 1998, Martin Green wrote:

{Quote hidden}

1998\06\18@032928 by Dr. Imre Bartfai

flavicon
face
On Wed, 17 Jun 1998, <Nichole Petty> wrote:

> Hello
>
> It has been suggested I write some macros to simplify my code. I haven't used
> them before. The MPASM assemblers guide is limited. I have a couple of
> questions. Please note I am a beginner
>
> In my code I am struggling to keep table and references to the PCL within the
> first 255 addresses. The guide states macros must be defined prior to their
> use. If I define them at the beginning of my code will they occupy those
> addresses?
Nope. Macro definitions don't occupy code space.
>
> Is it appropriate to define macros using an include file and simply make
> reference to them at the beginning of the program?
Yeah. You can even set up your macro library.
>
> Is there a straight forward tutorial that could help me understand how to
> implement macros? I have all the most popular PIC books but they don't go into
> macros.
There is the text 'usrguide.txt' for MPASM, maybe packaged with obsolete
version.
>
> Is the Parrallax assembler a compilation of macros?
>
>
Nope. However, you can see it so, but there are ladder macros. The
Parallax assembler - I like it! - is not capable to cope with macros.

Regards
Imre

1998\06\18@110218 by paulb

flavicon
face
Hello Jon.

> It has been suggested I write some macros to simplify my code.

 Who suggested that (I wonder!)?

> I haven't used them before.  The MPASM assembler guide is limited.

 Typical!

> Please note I am a beginner

 I can't believe that from seeing the code you write!

> In my code I am struggling to keep table and references to the PCL
> within the first 255 addresses.

 I have already made some suggestions on that....

>  The guide states macros must be defined prior to their use.  If I
> define them at the beginning of my code will they occupy those
> addresses?

 A macro is an interesting concept; otherwise called a "template".  In
other words, it sets the pattern of something.  When you call it, the
values you supply in the call fill the "holes" (local variables) in the
template and you "punch out" a piece of code with this pattern.

 You only make code (and use code space) when you call the macro.
Conversely, *each* time you do so, you make more code (but not
necessarily the *same* code, this is the beauty of it) and use more code
space.  As has been pointed out, if you use a macro library either
in-line or as an include file, the unused macros contribute *nothing* to
your code size.

 Compare this with subroutines; the subroutine is compiled into code
once only (therefore can only ever do the same operation though it may
have alternate branches) whether or not you call it.  It is not
therefore a good idea to use libraries of these unless an "intelligent"
linker is used to boot out the unwanted ones.  (Most "C" systems work
something like this.)  However, even if you call it a hundred times in
your code, it still only appears once and occupies a single instance in
code space.

> Is it appropriate to define macros using an include file and simply
> make reference to them at the beginning of the program?

 This is one of the *principal* uses of include files.  The other,
"defines" are very much the same; in fact a macro is in effect little
more than a multi-line "define" with a parameter substitution mechanism.

> Is there a straight forward tutorial that could help me understand how
> to implement macros? I have all the most popular PIC books but they
> don't go into macros.

 Can't say.  See if you can figure how the macros I sent you fit.

 There are two main reasons to use macros.  One is to avoid mistakes!
The lesser is to use smaller, more concise source code.

 Where you have a similar sequence of instructions again and again in
the code, you can write a macro to summarise on one line that sequence,
factoring out the variables.  If for example, the sequence

       movlw   <literal>
       movwf   <variable>

.. appears frequently, and in PIC code it certainly *does*, you define a
macro such as:

movl2   macro   var, lit
       movlw   lit
       movwf   var
       endm

.. where "movl2" is the macro name (*not* in this case a label for an
address in code space) and "var" and "lit" are local variables which in
the macro invocation will be set to the (string) values following
"movl2" wherever it appears in code as a pseudo-op.  The code snippet
(in this case the next two lines) with these substitutions made will
then be assembled into code space as if it had been in-line in the first
place.  "endm" means the end of the code to be substituted.

 So, if you have thus defined "movl2" previously in the code, the
sequence:
       MOVL2   PULSIN_H,3

.. will be seen by the assembler as:
       movlw   3
       movwf   PULSIN_H

.. and assembled accordingly.  Note that this is my choice of a mnemonic
and Parallax uses something different.  I chose it so that it reads
"Move Literal TO <register>", but also the "2" reminds that it takes two
program words and cycles; I favour a naming convention for similar two-
words macros.

 In this case, the advantages are that since the operation only really
affects one register (even though it leaves the literal value in W),
the connection of the literal and the register is made on the same line.
There is no need to look at the code and determine "chicken and egg"
especially if a cascade of tehse loads occur together; in a series, each
operation links source and target on the one line.

 While the obvious use of macros is to code a long sequence into a
short mnemonic and make the source less cluttered, the readability
benefits may also be worth defining a set of operations used only *once*
as macros, so that the series of macro calls subsequently forms a self-
documenting summary of program flow (FORTH-syyle).  This makes MACRO
very much a "high-level" language with implementation-hiding.

 ****************

 *How* does a macro assembler do it?  Well, it maintains a "stack" of
macro invocations (loosely; "calls") and an area into which macros are
compiled as they are read from the source.  The stack contains pointers
to the current reading position for each macro within the macro storage
space, plus the value of each of the substituted parameters.  Macros are
by *no* means unique to assemblers, there are generic macro processors,
text processors (with features still uncommon in WYSIWYG word
processors) etc.

 As again has already been mentioned, a good macro processor with a
mathematical expression evaluator can be used to make an assembler or
disassembler for virtually *any* processor by using include files.  Most
macro assemblers for one CPU type can be used to compile for another,
even with a quite different architecture and language, so you can for
example, use MPASM to program a 68HC11 (I think - I haven't tested it)!

> Is the Parallax assembler a compilation of macros?

 Again, as others have said - probably!  That certainly is *one* way it
could have been built.

 Apologies if this is too elementary for some - I will re-work it into
a FAQ on my home page - sometime!

 Cheers,
       Paul B.

1998\06\26@055707 by Larry G. Nelson Sr.

flavicon
face
At 03:12 PM 6/17/98 EDT, you wrote:
>Hello
>
>It has been suggested I write some macros to simplify my code. I haven't used
>them before. The MPASM assemblers guide is limited. I have a couple of
>questions. Please note I am a beginner
>
>In my code I am struggling to keep table and references to the PCL within the
>first 255 addresses. The guide states macros must be defined prior to their
>use. If I define them at the beginning of my code will they occupy those
>addresses?

no They use space only when used but unlike subroutines use space every
time they are used.

>
>Is it appropriate to define macros using an include file and simply make
>reference to them at the beginning of the program?

This is acceptable to do.


Larry G. Nelson Sr.
L.NelsonspamKILLspamieee.org
http://www.ultranet.com/~nr

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