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

220 lines
3.7 KiB
HTML
Raw Blame History

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/loose.dtd">
<HTML
><HEAD
><TITLE
>Functionals</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="To HLL and Back"
HREF="p481.html"><LINK
REL="PREVIOUS"
TITLE="Conclusion"
HREF="x813.html"><LINK
REL="NEXT"
TITLE="A quick digression on how subroutines work"
HREF="x836.html"></HEAD
><BODY
CLASS="CHAPTER"
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="x813.html"
ACCESSKEY="P"
>&#60;&#60;&#60; Previous</A
></TD
><TD
WIDTH="80%"
ALIGN="center"
VALIGN="bottom"
></TD
><TD
WIDTH="10%"
ALIGN="right"
VALIGN="bottom"
><A
HREF="x836.html"
ACCESSKEY="N"
>Next &#62;&#62;&#62;</A
></TD
></TR
></TABLE
><HR
ALIGN="LEFT"
WIDTH="100%"></DIV
><DIV
CLASS="CHAPTER"
><H1
><A
NAME="AEN816"
></A
>Functionals</H1
><P
> This essay deals with indirect calls. These are the core of an
enormous number of high level languages: LISP's closures, C's
function pointers, C++ and Java's virtual method calls, and some
implementations of the <TT
CLASS="LITERAL"
>switch</TT
> statement.</P
><P
> These techniques vary in complexity, and most will not be
appropriate for large-scale assembler projects. Of them, however,
the Data-Directed approach is the most likely to lead to organized
and maintainable code.</P
><DIV
CLASS="SECTION"
><H1
CLASS="SECTION"
><A
NAME="AEN821"
>Function Pointers</A
></H1
><P
> Because assembly language is totally untyped, function pointers
are the same as any other sixteen-bit integer. This makes
representing them really quite easy; most assemblers should permit
routines to be declared simply by naming the routine as
a <TT
CLASS="LITERAL"
>.word</TT
> directly.
</P
><P
> To actually invoke these methods, copy them to some sixteen-bit
location (say, <TT
CLASS="LITERAL"
>target</TT
>) and then invoking the
method is a simple matter of the using an indirect jump:
the <TT
CLASS="LITERAL"
>JMP<EFBFBD>(target)</TT
> instruction.
</P
><P
> There's really only one subtlety here, and it's that the indirect
jump is an indirect <I
CLASS="EMPHASIS"
>jump</I
>, not an
indirect <I
CLASS="EMPHASIS"
>function call</I
>. Thus, if some
function <TT
CLASS="LITERAL"
>A</TT
> makes in indirect jump to some
routine, when that routine returns, it returns to whoever
called <TT
CLASS="LITERAL"
>A</TT
>, not <TT
CLASS="LITERAL"
>A</TT
>
itself.
</P
><P
> There are several ways of dealing with this, but only one correct
way, which is to structure your procedures so that any call
to <TT
CLASS="LITERAL"
>JMP<EFBFBD>(xxxx)</TT
> occurs at the very
end.
</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="x813.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="x836.html"
ACCESSKEY="N"
>Next &#62;&#62;&#62;</A
></TD
></TR
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
>Conclusion</TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="p481.html"
ACCESSKEY="U"
>Up</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
>A quick digression on how subroutines work</TD
></TR
></TABLE
></DIV
></BODY
></HTML
>