Ophis/book/x836.html
2012-06-16 02:07:47 -07:00

215 lines
3.7 KiB
HTML

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/loose.dtd">
<HTML
><HEAD
><TITLE
>A quick digression on how subroutines work</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="Functionals"
HREF="c816.html"><LINK
REL="PREVIOUS"
TITLE="Functionals"
HREF="c816.html"><LINK
REL="NEXT"
TITLE="Dispatch-on-type and Data-Directed Assembler"
HREF="x855.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="c816.html"
ACCESSKEY="P"
>&#60;&#60;&#60; Previous</A
></TD
><TD
WIDTH="80%"
ALIGN="center"
VALIGN="bottom"
>Functionals</TD
><TD
WIDTH="10%"
ALIGN="right"
VALIGN="bottom"
><A
HREF="x855.html"
ACCESSKEY="N"
>Next &#62;&#62;&#62;</A
></TD
></TR
></TABLE
><HR
ALIGN="LEFT"
WIDTH="100%"></DIV
><DIV
CLASS="SECTION"
><H1
CLASS="SECTION"
><A
NAME="AEN836"
>A quick digression on how subroutines work</A
></H1
><P
> Ordinarily, subroutines are called with <TT
CLASS="LITERAL"
>JSR</TT
> and
finished with <TT
CLASS="LITERAL"
>RTS</TT
>. The <TT
CLASS="LITERAL"
>JSR</TT
>
instruction takes its own address, adds 2 to it, and pushes this
16-bit value on the stack, high byte first, then low byte (so that
the low byte will be popped off first).
</P
><P
> But wait, you may object. All <TT
CLASS="LITERAL"
>JSR</TT
> instructions
are three bytes long. This <SPAN
CLASS="QUOTE"
>"return address"</SPAN
> is in
the middle of the instruction. And you would be quite right;
the <TT
CLASS="LITERAL"
>RTS</TT
> instruction pops off the 16-bit
address, adds one to it, and <I
CLASS="EMPHASIS"
>then</I
> sets the
program counter to that value.
</P
><P
> So it <I
CLASS="EMPHASIS"
>is</I
> possible to set up
a <SPAN
CLASS="QUOTE"
>"<TT
CLASS="LITERAL"
>JSR</TT
> indirect"</SPAN
> kind of operation
by adding two to the indirect jump's address and then pushing that
value onto the stack before making the jump; however, you wouldn't
want to do this. It takes six bytes and trashes your accumulator,
and you can get the same functionality with half the space and
with no register corruption by simply defining the indirect jump
to be a one-instruction routine and <TT
CLASS="LITERAL"
>JSR</TT
>-ing to
it directly. As an added bonus, that way if you have multiple
indirect jumps through the same pointer, you don't need to
duplicate the jump instruction.
</P
><P
> Does this mean that abusing <TT
CLASS="LITERAL"
>JSR</TT
>
and <TT
CLASS="LITERAL"
>RTS</TT
> is a dead-end, though? Not at all...
</P
></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="c816.html"
ACCESSKEY="P"
>&#60;&#60;&#60; 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="x855.html"
ACCESSKEY="N"
>Next &#62;&#62;&#62;</A
></TD
></TR
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
>Functionals</TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="c816.html"
ACCESSKEY="U"
>Up</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
>Dispatch-on-type and Data-Directed Assembler</TD
></TR
></TABLE
></DIV
></BODY
></HTML
>