Viitor_cc65/usr/share/doc/cc65/webdoc/cc65-8.html
kueller 223cc6685e Neue Version V963
git-svn-id: svn://svn.compuextreme.de/Viitor/V963/Viitor_cc65@5933 504e572c-2e33-0410-9681-be2bf7408885
2011-01-03 10:48:06 +00:00

59 lines
3.0 KiB
HTML

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<HTML>
<HEAD>
<META NAME="GENERATOR" CONTENT="LinuxDoc-Tools 0.9.20">
<TITLE>cc65 Users Guide: Register variables</TITLE>
<LINK HREF="cc65-9.html" REL=next>
<LINK HREF="cc65-7.html" REL=previous>
<LINK HREF="cc65.html#toc8" REL=contents>
</HEAD>
<BODY>
<A HREF="cc65-9.html">Next</A>
<A HREF="cc65-7.html">Previous</A>
<A HREF="cc65.html#toc8">Contents</A>
<HR>
<H2><A NAME="regvars"></A> <A NAME="s8">8.</A> <A HREF="cc65.html#toc8">Register variables</A></H2>
<P>The runtime for all supported platforms has 6 bytes of zero page space
available for register variables (this could be increased, but I think it's a
good value). So you can declare register variables up to a total size of 6 per
function. The compiler will allocate register space on a "first come, first
served" base and convert any <CODE>register</CODE> declarations that exceed the
available register space silently to <CODE>auto</CODE>. Parameters can also be
declared as <CODE>register</CODE>, this will in fact give slightly shorter code than
using a register variable.</P>
<P>Since a function must save the current values of the registers on entry and
restore them on exit, there is an overhead associated with register variables,
and this overhead is quite high (about 20 bytes per variable). This means that
just declaring anything as <CODE>register</CODE> is not a good idea.</P>
<P>The best use for register variables are pointers, especially those that point
to structures. The magic number here is about 3 uses of a struct field: If the
function contains this number or even more, the generated code will be usually
shorter and faster when using a register variable for the struct pointer. The
reason for this is that the register variable can in many cases be used as a
pointer directly. Having a pointer in an auto variable means that this pointer
must first be copied into a zero page location, before it can be dereferenced.</P>
<P>Second best use for register variables are counters. However, there is not
much difference in the code generated for counters, so you will need at least
100 operations on this variable (for example in a loop) to make it worth the
trouble. The only savings you get here are by the use of a zero page variable
instead of one on the stack or in the data segment.</P>
<P>Register variables must be explicitly enabled by using <CODE>-Or</CODE> or <CODE>-r</CODE> on
the command line. Register variables are only accepted on function top level,
register variables declared in interior blocks are silently converted to
<CODE>auto</CODE>. With register variables disabled, all variables declared as
<CODE>register</CODE> are actually auto variables.</P>
<P>Please take care when using register variables: While they are helpful and can
lead to a tremendous speedup when used correctly, improper usage will cause
bloated code and a slowdown.</P>
<HR>
<A HREF="cc65-9.html">Next</A>
<A HREF="cc65-7.html">Previous</A>
<A HREF="cc65.html#toc8">Contents</A>
</BODY>
</HTML>