[stella] A possible use for indexed indirect addressing on the VCS.

R Mundschau rmundschau at frontiernet.net
Mon Oct 9 20:58:16 CDT 2006


So I had a interesting thought this weekend that I wanted to share.   
I have been spending some time off and on trying to see if there is  
any unexploited use for indexed indirect (offset,X) addressing in the  
6502 micro.  It turns out that because the VCS has page 0 RAM  
shadowed in page 1 for the stack that there is a use.

If you wrote code like this

	NOP ; Some code
	JSR		MySubroutine
	.byte	parameter		;  A constant parameter passed to the subroutine.
							;  It Could be a z-page byte-sized absolute indexing pointer,  
or 2 byte word pointer to a variable
.returnpoint
	NOP ;  more code

..... ; In the subroutine we use indexed indirect addressing to fetch  
the parameter.

Mysubroutine
	TSX		; X = SP.
	LDA 	($00,X)	; fetch the parameter byte using the return address on  
the stack as the pointer to it.
	INC		$00,X	; Increment the return pointer to point past the  
parameter list to the address .returnpoint (Watch out for page  
boundaries!)
	; Repeat the load and increment pattern to handle more than 1 byte  
of parameters being passed.
	NOP	; do subroutine stuff.
	RTS	;  The INC instructions move the return pointer past the  
parameter list to the next instruction.

A key point here of course is that the parameter list could be any  
size.  You simply need to add the correct number of bytes to the  
return pointer on the stack.
So if you have a subroutine that you call many times using different  
parameters you may be able to save ROM space using this technique.   
It also kind of cool in that it gives a dynamic way to implement the  
mechanism of passing parameters to a function.  Code like this:

LDA	#param1
STA		temp1
LDA	#param2
STA		temp2
LDA	#param3
STA		temp3
JSR		MyRoutine

becomes

JSR		MyRoutine
paramlist:
.byte	param1
.byte	param2
.byte	param3
paramSize = * - paramlist

I don't have a convincing example in the real world, but it is a  
possibility to think about.   Such a technique would be a boon to a  
compiler writer.  Being able to write every function call as a JSR  
followed by a static parameter list of constants and pointers to  
variables.   It is truly unfortunate that the designers of the  
blessed 6502 did not think to have indexed indirect addressing be  
hardwired to use the page 1 where the stack is stored rather than  
zero page.  Its only that the VCS being so starved for memory happens  
to share the same RAM between page 1 and page 0, that makes this  
technique work.   Maybe the bBasic compiler could make use of this  
technique?

Cheers!
Rob 




More information about the Stella mailing list