The first performance improvements can be done without having to dig into pure Assembly:
Use unsigned char as a type whenever possible. int is mostly a waste. This is true for regular variables as well as for array indices. I.e. make sure that your arrays have mostly no more than 255 entries.
(I would suggest you to declare typedef unsigned character byte;
, so that you don't have to write this long name every time.)
When you access arrays, make sure that the index is a single value:
value = array[index];
generates good code, but value = array[index + 1];
generates shitty code. (Because the compiler doesn't know whether index + 1 maybe exceeds a single byte, so it treats it as an int.)
If you need to do this, it's better to do temp = index + 1;
and use this as an index.
(Always assuming that the array type and the index type is unsigned char. If you have
to use a large array with an int index, don't bother optimizing the access manually. The generated code is pretty much on par with manual assembly code here.)
If you access arrays via a pointer, it's always
shitty code generation. Because for pointer access, the pointer needs to be in the zeropage. So, the compiler always first copies your pointer into its own pointer before doing anything with it, even if your pointer is in the zeropage itself.
For pointer access, you should write some inline assembly functions and use them.
Don't use local variables. Use either global variables or local static variables. Same goes for function parameters.
Also you should read this:
https://shiru.untergrund.net/articles/p ... s_in_c.htm
And don't forget: With C you can do all kinds of neat macro tricks that aren't possible in other languages. For example, how do you do a function call with parameters, but without using actual parameters and using only global variables instead? Simple:
Code: Select all
void __fastcall__ MyFunction_(void);
#define MyFunction(parameter1, parameter2)\
MyFunctionParameter1 = parameter1;\
MyFunctionParameter2 = parameter2;\
Now you can call MyFunction(1, 5);
like with a regular parameterized function, but the code still works with global variables internally.