mirror of
https://github.com/michaelcmartin/Ophis.git
synced 2024-12-30 10:30:47 +00:00
289 lines
4.8 KiB
HTML
289 lines
4.8 KiB
HTML
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/loose.dtd">
|
|
<HTML
|
|
><HEAD
|
|
><TITLE
|
|
>Macros</TITLE
|
|
><META
|
|
NAME="GENERATOR"
|
|
CONTENT="Modular DocBook HTML Stylesheet Version 1.79"><LINK
|
|
REL="HOME"
|
|
TITLE="Programming with Ophis"
|
|
HREF="book1.html"><LINK
|
|
REL="UP"
|
|
TITLE="Headers, Libraries, and Macros"
|
|
HREF="c236.html"><LINK
|
|
REL="PREVIOUS"
|
|
TITLE="Headers, Libraries, and Macros"
|
|
HREF="c236.html"><LINK
|
|
REL="NEXT"
|
|
TITLE="Example code"
|
|
HREF="x287.html"></HEAD
|
|
><BODY
|
|
CLASS="SECTION"
|
|
BGCOLOR="#FFFFFF"
|
|
TEXT="#000000"
|
|
LINK="#0000FF"
|
|
VLINK="#840084"
|
|
ALINK="#0000FF"
|
|
><DIV
|
|
CLASS="NAVHEADER"
|
|
><TABLE
|
|
SUMMARY="Header navigation table"
|
|
WIDTH="100%"
|
|
BORDER="0"
|
|
CELLPADDING="0"
|
|
CELLSPACING="0"
|
|
><TR
|
|
><TH
|
|
COLSPAN="3"
|
|
ALIGN="center"
|
|
>Programming with Ophis</TH
|
|
></TR
|
|
><TR
|
|
><TD
|
|
WIDTH="10%"
|
|
ALIGN="left"
|
|
VALIGN="bottom"
|
|
><A
|
|
HREF="c236.html"
|
|
ACCESSKEY="P"
|
|
><<< Previous</A
|
|
></TD
|
|
><TD
|
|
WIDTH="80%"
|
|
ALIGN="center"
|
|
VALIGN="bottom"
|
|
>Headers, Libraries, and Macros</TD
|
|
><TD
|
|
WIDTH="10%"
|
|
ALIGN="right"
|
|
VALIGN="bottom"
|
|
><A
|
|
HREF="x287.html"
|
|
ACCESSKEY="N"
|
|
>Next >>></A
|
|
></TD
|
|
></TR
|
|
></TABLE
|
|
><HR
|
|
ALIGN="LEFT"
|
|
WIDTH="100%"></DIV
|
|
><DIV
|
|
CLASS="SECTION"
|
|
><H1
|
|
CLASS="SECTION"
|
|
><A
|
|
NAME="AEN257"
|
|
>Macros</A
|
|
></H1
|
|
><P
|
|
> A macro is a way of expressing a lot of code or data with a
|
|
simple shorthand. It's also usually configurable. Traditional
|
|
macro systems such as C's <TT
|
|
CLASS="LITERAL"
|
|
>#define</TT
|
|
> mechanic
|
|
use <I
|
|
CLASS="EMPHASIS"
|
|
>textual replacement</I
|
|
>: a macro is
|
|
expanded before any evaluation or even parsing occurs.
|
|
</P
|
|
><P
|
|
> In contrast, Ophis's macro system uses a <I
|
|
CLASS="EMPHASIS"
|
|
>call by
|
|
value</I
|
|
> approach where the arguments to macros are
|
|
evaluated to bytes or words before being inserted into the macro
|
|
body. This produces effects much closer to those of a
|
|
traditional function call. A more detailed discussion of the
|
|
tradeoffs may be found in <A
|
|
HREF="a505.html"
|
|
>the Appendix called <I
|
|
>Ophis Command Reference</I
|
|
></A
|
|
>.
|
|
</P
|
|
><DIV
|
|
CLASS="SECTION"
|
|
><H2
|
|
CLASS="SECTION"
|
|
><A
|
|
NAME="AEN265"
|
|
>Macro definitions</A
|
|
></H2
|
|
><P
|
|
> A macro definition is a set of statements between
|
|
a <TT
|
|
CLASS="LITERAL"
|
|
>.macro</TT
|
|
> statement and
|
|
a <TT
|
|
CLASS="LITERAL"
|
|
>.macend</TT
|
|
> statement.
|
|
The <TT
|
|
CLASS="LITERAL"
|
|
>.macro</TT
|
|
> statement also names the macro
|
|
being defined.
|
|
</P
|
|
><P
|
|
> No global or anonymous labels may be defined inside a macro:
|
|
temporary labels only persist in the macro expansion itself.
|
|
(Each macro body has its own scope.)
|
|
</P
|
|
><P
|
|
> Arguments to macros are referred to by number: the first is
|
|
<TT
|
|
CLASS="LITERAL"
|
|
>_1</TT
|
|
>, the second <TT
|
|
CLASS="LITERAL"
|
|
>_2</TT
|
|
>, and so on.
|
|
</P
|
|
><P
|
|
> Here's a macro that encapsulates the printing routine in our
|
|
<SPAN
|
|
CLASS="QUOTE"
|
|
>"Hello World"</SPAN
|
|
> program, with an argument being the
|
|
address of the string to print:
|
|
</P
|
|
><TABLE
|
|
BORDER="0"
|
|
BGCOLOR="#E0E0E0"
|
|
WIDTH="100%"
|
|
><TR
|
|
><TD
|
|
><PRE
|
|
CLASS="PROGRAMLISTING"
|
|
>.macro print
|
|
ldx #0
|
|
_loop: lda _1, x
|
|
beq _done
|
|
jsr chrout
|
|
inx
|
|
bne _loop
|
|
_done:
|
|
.macend</PRE
|
|
></TD
|
|
></TR
|
|
></TABLE
|
|
></DIV
|
|
><DIV
|
|
CLASS="SECTION"
|
|
><H2
|
|
CLASS="SECTION"
|
|
><A
|
|
NAME="AEN278"
|
|
>Macro invocations</A
|
|
></H2
|
|
><P
|
|
> Macros may be invoked in two ways: one that looks like a
|
|
directive, and one that looks like an instruction.
|
|
</P
|
|
><P
|
|
> The most common way to invoke a macro is to backquote the name
|
|
of the macro. It is also possible to use
|
|
the <TT
|
|
CLASS="LITERAL"
|
|
>.invoke</TT
|
|
> command. These commands look
|
|
like this:
|
|
</P
|
|
><TABLE
|
|
BORDER="0"
|
|
BGCOLOR="#E0E0E0"
|
|
WIDTH="100%"
|
|
><TR
|
|
><TD
|
|
><PRE
|
|
CLASS="PROGRAMLISTING"
|
|
>`print msg
|
|
.invoke print msg</PRE
|
|
></TD
|
|
></TR
|
|
></TABLE
|
|
><P
|
|
> Arguments are passed to the macro as a comma-separated list.
|
|
They must all be expressions that evaluate to byte or word
|
|
values—a mechanism similar to <TT
|
|
CLASS="LITERAL"
|
|
>.alias</TT
|
|
>
|
|
is used to assign their values to the <TT
|
|
CLASS="LITERAL"
|
|
>_n</TT
|
|
>
|
|
names.
|
|
</P
|
|
></DIV
|
|
></DIV
|
|
><DIV
|
|
CLASS="NAVFOOTER"
|
|
><HR
|
|
ALIGN="LEFT"
|
|
WIDTH="100%"><TABLE
|
|
SUMMARY="Footer navigation table"
|
|
WIDTH="100%"
|
|
BORDER="0"
|
|
CELLPADDING="0"
|
|
CELLSPACING="0"
|
|
><TR
|
|
><TD
|
|
WIDTH="33%"
|
|
ALIGN="left"
|
|
VALIGN="top"
|
|
><A
|
|
HREF="c236.html"
|
|
ACCESSKEY="P"
|
|
><<< Previous</A
|
|
></TD
|
|
><TD
|
|
WIDTH="34%"
|
|
ALIGN="center"
|
|
VALIGN="top"
|
|
><A
|
|
HREF="book1.html"
|
|
ACCESSKEY="H"
|
|
>Home</A
|
|
></TD
|
|
><TD
|
|
WIDTH="33%"
|
|
ALIGN="right"
|
|
VALIGN="top"
|
|
><A
|
|
HREF="x287.html"
|
|
ACCESSKEY="N"
|
|
>Next >>></A
|
|
></TD
|
|
></TR
|
|
><TR
|
|
><TD
|
|
WIDTH="33%"
|
|
ALIGN="left"
|
|
VALIGN="top"
|
|
>Headers, Libraries, and Macros</TD
|
|
><TD
|
|
WIDTH="34%"
|
|
ALIGN="center"
|
|
VALIGN="top"
|
|
><A
|
|
HREF="c236.html"
|
|
ACCESSKEY="U"
|
|
>Up</A
|
|
></TD
|
|
><TD
|
|
WIDTH="33%"
|
|
ALIGN="right"
|
|
VALIGN="top"
|
|
>Example code</TD
|
|
></TR
|
|
></TABLE
|
|
></DIV
|
|
></BODY
|
|
></HTML
|
|
> |