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: Select all
LDA lo
STA a
LDA hi
STA a+1
JSR my-routineCode: Select all
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.
...
RTSCode: Select all
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