Programming with Ophis | ||
---|---|---|
<<< Previous | Pointers and Indirection | Next >>> |
This essay has concerned itself almost exclusively with the Indirect Indexed—or (Indirect), Y—mode. What about Indexed Indirect—(Indirect, X)? This is a much less useful mode than the Y register's version. While the Y register indirection lets you implement pointers and arrays in full generality, the X register is useful for pretty much only one application: lookup tables for single byte values.
Even coming up with a motivating example for this is difficult, but here goes. Suppose you have multiple, widely disparate sections of memory that you're watching for signals. The following routine takes a resource index in the accumulator and returns the status byte for the corresponding resource.
; This data is sitting on the zero page somewhere resource_status_table: .word resource0_status, resource1_status, .word resource2_status, resource3_status, ; etc. etc. etc. ; This is the actual program code .text getstatus: clc ; Multiply argument by 2 before putting it in X, so that it asl ; produces a value that's properly word-indexed tax lda (resource_status_table, x) rts |
Why having a routine such as this is better than just having the calling routine access resourceN_status itself as an absolute memory load is left as an exercise for the reader. That aside, this code fragment does serve as a reminder that when indexing an array of anything other than bytes, you must multiply your index by the size of the objects you want to index. C does this automatically—assembler does not. Stay sharp.
<<< Previous | Home | Next >>> |
Pointer arithmetic | Up | Comparison with the other indexed forms |