thefox wrote:This is wrong. TSX simply transfers the stack pointer to register X. You need to use LDA $0103,X etc to actually access the stack (like Quietust said).
Alright, now I understand. Even better. This means I can access these variables at will.
So, next try:
Code: Select all
FillSpecificMemoryWithValue:
; Parameters (put them to the stack in this order):
; 1. Length
; 2. Value
TSX ; Loads the current stack pointer.
LDA $0103, X ; Loads "value" into A.
LDX $0104, X ; Loads "length" into X.
Loop:
STA $1000, X ; Stores "value" at address $1000 + X.
DEX
BNE Loop ; If X isn't 0 yet, repeat, otherwise, leave function.
PLA ; Removes "value" from stack.
PLA ; Removes "length" from stack.
RTS
Code: Select all
LDA #$10 ; The parameter "length".
PSA ; "Length" is put into the stack.
LDA #$FF ; The parameter "value".
PSA ; "Value" is put into the stack.
JSR FillSpecificMemoryWithValue
Correct now?
If the stack starts at $0100, why do I have to get the most recent stack value with $010
3 + X? I assume that the first value in the stack might be the stack pointer itself, so that the actual stack variables start at $0101. But why $0103?
rainwarrior wrote:Yes, you can use the stack as mentioned, but I find having to move the stack pointer into X to retrieve them makes it a little inconvenient/inefficient. You don't really need to use the stack unless you have a recursive/re-entrant subroutine.
Yeah, I guess in a real game I will have to see for each function individually which version I'll take. But in the moment, I'll try the stack just to get it to know.
rainwarrior wrote:If I misunderstood the question and you're looking for a simplified syntax
No, you understood it correctly. I was just looking for the technique itself, not for syntax tricks.
But thank you all for the example anyway.