Manipulating the array

Now a routine to add (push) an element to the end of the array. Put this at the end of program ASQUR.

  • L: current length of array (initialized to 0 at the beginning of the program and updated by the routine itself)
  • r1: X-value of new square
  • r2: Y-value of new square
  • r3: X-speed of new square
  • r4: Y-speed of new square

None. (Of course, you could easily modify it to return a useful value, such as a pointer to the element added.)

:Lbl PE
:If L<177

Scary? Fine, I'll break it down.

Before it does anything, it checks if there are 177 elements in the list. This is because L1 can only hold 714 bytes of data, which is approximately 178*4 elements. If you're using any safe RAM location, make sure you change this limit accordingly. Having too many elements is called an overflow, which could mess up whatever data comes after L1 (in this case the variables A through [.

First look at the very inside of the mess of braces, at the line L+1→L*4+L1−4. The real action starts there. First, it increments L by one (you probably know why). Since that command returns the value of L, you can keep doing operations on it (multiplying by four in this case). Since each element in our example is four bytes long, L*4 gets the offset of the next element in the array. But there's a problem here: Since you incremented L already, this now points four bytes ahead of where you're supposed to be. We take care of this by adding only L1−4 to the total.

That gives us a pointer to where to store the first byte of the element, so r1→{L+1→L*4+L1−4} would store the first byte there.

Now here's the fun part: By storing to a variable location, the pointer you stored to is returned in HL. That means that you can keep on storing to the byte after it by simply adding one! That's why the line above is enclosed by r2→{ ... +1}: you just add one to get the next byte, then store to it. You can keep going like this for as long as you want; it's the most optimized way to store a mass of variable data in Axe!

Next up we'll actually add the pretty squares. Promise.

+ Tweet