Actual code generated:
Code: Select all
; array = arrayOfArray[index];
ldx #$00
lda _index
asl a
bcc L0085
inx
clc
L0085: adc _arrayOfArray
pha
txa
adc _arrayOfArray+1
tax
pla
;jsr ldaxi ; i've placed the subroutine inline here
ldy #1
sta ptr1
stx ptr1+1
lda (ptr1),y
tax
dey
lda (ptr1),y
rts
sta _array
stx _array+1
The
ldaxi function for reference.
That looks fairly efficient for what it does, but it's not able to do the final resolution of the pointer inline, it has to put the pointer in A:X and branch to that subroutine. (Jarhmander was missing the ASL by the way. CC65 does in fact know that index is 8-bit, but the implied *2 on the index promotes it to integer anyway.)
So in terms of efficiency, we've got maybe an extra JSR/RTS, PHA,PLA, TXA, TAX, TAX? About 25 extra cycles? The subroutine call itself accounts for half of it.
I kinda wonder how much efficiency CC65 would be able to gain with a special "7-bit index" type for accessing arrays of 16-bit data, that it could double without promoting to an int.