Arrays: Difference between revisions

m
→‎Array Alignment: this was meant to be a subsection of z80 assembly
m (→‎Array Alignment: added a return to the first example so we don't try to execute the array as instructions and crash!)
m (→‎Array Alignment: this was meant to be a subsection of z80 assembly)
Line 8,547:
The main takeaway from all this is that arrays are handled the same as any other type of memory, and have no "special" syntax, apart from the boilerplate pointer arithmetic of <code>*array = *array + (desired_row_number*row_length*bytes_per_element) + (desired_column_number*bytes_per_element)</code>. This is the case for most assembly languages, even though the methods of offsetting a pointer may vary.
 
===Array Alignment===
One very important topic for arrays in Z80 Assembly is alignment. If you need fast access to the data in an array, you can more than triple your lookup speed by aligning the table ahead of time. If an array of 8-bit data is placed so that its base address ends in 00, you really only need to load half the pointer to the table into the "high register" (B, D, H, IXH, or IYH) and the desired index into the corresponding "low register." Using an <code>org</code> or <code>align</code> directive is the easiest way to accomplish this task. Aligned tables are very important in Z80 Assembly, more so than in other assembly languages, since Z80's index registers are, for a lack of a better term, underwhelming at best. Not only are they slow, you are limited to using constant offsets only (unless you use self-modifying code) and are also signed offsets meaning that you can't index an array longer than 128 bytes without doing additional pointer arithmetic. Having indexing limitations is not unusual for most CPUs but the Z80 is probably one of the worst at it. Thankfully, table alignment can solve nearly all those problems (assuming you have enough padding to spare.)
 
1,489

edits