Next Previous Contents

14. Use register variables with care

Register variables may give faster and shorter code, but they do also have an overhead. Register variables are actually zero page locations, so using them saves roughly one cycle per access. Since the old values have to be saved and restored, there is an overhead of about 70 cycles per 2 byte variable. It is easy to see, that - apart from the additional code that is needed to save and restore the values - you need to make heavy use of a variable to justify the overhead.

As a general rule: Use register variables only for pointers that are dereferenced several times in your function, or for heavily used induction variables in a loop (with several 100 accesses).

When declaring register variables, try to keep them together, because this will allow the compiler to save and restore the old values in one chunk, and not in several.

And remember: Register variables must be enabled with -r or -Or.


Next Previous Contents