In my games (written directly in assembly language) I am using the index registers (IX and IY) very often, e.g. for controlling multiple enemies. It’s a very convenient way to do things and it produces nice readable code. But there have to be faster ways. One of the other ways to access lists of variables is using aligned indexing with HL. H is then used as a pointer to which enemy you want to adress and L points to the variable (e.g. x-coordinate). It is faster (in my pieces of code approximately 25 to 35%), but it has some serious drawbacks:
1) There have to be a 256 byte gap between the records
2) You cannot easily use HL anymore in the code (or you have to push/pop)
3) Code is less readable
Both methods use a form of indirect adressing. If there would be only one enemy, direct adressing could be used, which is much faster.
Now my idea is to copy all routines that are used with our enemies into RAM multiple times (each routine has its own copy for each enemy), and use direct adressing within each piece of code. This gives nice clean code, HL can be used normally, things don’t have to be aligned anymore. It is also possible to spawn enemy objects, allocate the RAM for its variables and code, and remove them later on; just like in object oriented programming. One obvious drawback is of course that it uses RAM, so this works best when there are not too many objects and not too many large routines to control the objects.
My estimate is that this way of programming is at least 1.5 to 2 times faster than aligned indexing with HL.
Would this be a good way to go forward? Does it maybe already exist and are there any examples of this? Did I miss some serious drawbacks of this method? What are your thoughts?
Regards, Micha