Indexed absolute addressing

From C64-Wiki
Jump to navigationJump to search

Indexed absolute addressing is an addressing mode in which the contents of either the X or Y index register is added to a given base address, to obtain the "target" address. This is useful in loops in which a number (256 or less) of bytes in memory is to be given similar "treatment". Here is an example in assembler, which uses a loop with the X index register to copy 50 bytes starting at label Source into 50 bytes beginning at label Target:

       LDX #0        Use X index register as "counter"
Loop:  LDA Source,X  Get a byte from Source + X
       STA Target,X  Store it at Target + X
       INX           Count up one byte
       CPX #50       Done all 50 yet?
       BNE Loop      If not, repeat from label Loop

In this example, the LDA and STA instructions are both using indexed addressing mode: The LDA fetches a byte from an address that is calculated as the value of label Source plus the contents of the X index register, and similarly the STA stores that same byte at an address that is the value of label Target plus the contents of the X index register. Since the X register acts as a counter in the loop, counting up from 0 thru 49, this reading and writing of bytes takes place on 50 consecutive bytes in memory. When X reaches 50 the BNE at the end no longer takes the branch back to label Loop, and the system "escapes" the loop.

Since an index register can only hold single-byte, unsigned integers in the range from 0 thru 255, the above routine can handle no more than 256 bytes. To process more bytes in a loop structure like this requires the use of indirect-indexed addressing.

The following 16 machine language instructions support indexed absolute addressing: ADC, AND, ASL, CMP, DEC, EOR, INC, LDA, LDX, LDY, LSR, ORA, ROL, ROR, SBC, STA.