An alternative to the parameter passing described at NesDev. The main difference is that the target routine is responsible for calling the parameter-extracting code. (I'm guessing this is a well used technique- I would be interested to know where it was first used and what the most efficient implementation looks like.)
This method seems particularly appropriate if you wish to call a routine that operates on a structure at a fixed memory location, but will work for any constant word parameters.
Load/store method of passing a word,
Code:
LDA lo
STA a
LDA hi
STA a+1
JSR my-routine
Callsite method,
Code:
JSR my-routine
DW my-parameter
;;execution flow resumes here
...
MY-ROUTINE:
;Dereference the parameter at the call-site
JSR deref-w
...
X and A now contain the lo and hi word of
my-parameter respectively.
...
RTS
Here is the implementation of the dereferencing sub-routine. Other tricks are of course possible, e.g.
https://wiki.nesdev.com/w/index.php/650 ... ent_system Code:
DEREF-W 0B28 BA TSX
0B29 E8 INX ;Points to return address of this function
0B2A E8 INX ;Now skip to return address of grandparent
0B2B E8 INX
;Store the grandparent return address
0B2C BD0001 LDA $0100,X
0B2F 8519 STA $19 ;TMP
0B31 BD0101 LDA $0101,X
0B34 851A STA $1A ;TMP + 1
;Now we have the address of the parameter (-1)
;Add two to it so we can skip it when parent returns
0B36 18 CLC
0B37 A902 LDA #$02
0B39 7D0001 ADC $0100,X
0B3C 9D0001 STA $0100,X
0B3F A900 LDA #$00
0B41 7D0101 ADC $0101,X
0B44 9D0101 STA $0101,X
0B47 A001 LDY #$01 ;Offset against -1 for return convention
;Dereference the word at the parameter address
0B49 B119 LDA ($19),Y
0B4B AA TAX
0B4C C8 INY
0B4D B119 LDA ($19),Y
0B4F 60 RTS
A nice extension would be a entry point that allows for only a single byte- I will add this at some point but haven't found it necessary yet for the game I am writing, see
https://github.com/djangojames/exploratory/blob/master/blog.md