So I've been having trouble implementing subroutines because of this strange problem. Supposedly its implied that the stack shouldn't be written to during subroutines, right? Since JSR writes the address of the next instruction to the stack and RTS reads that address to return, the stack shouldn't be written to, or should be trimmed so that the net effect is that the address written by JSR is at the top of the stack by the end of the subroutine. At least, this is what I'm assuming. Below are the instructions in the subroutine, and as you can see, several instructions push to the stack, but none pop off it except for RTS. After each instruction that changes the stack, I'll list an output of the stack at that point.
JSR cbaa abs
[200, 7]
LDA 00 imm
STA 04 zp
LDA ff imm
JMP f0a1 abs
STA 03 zp
LDA 0f imm
PHA
[15, 200, 7]
STA 0f zp
TXA
PHA
[255, 15, 200, 7]
TYA
PHA
[0, 255, 15, 200, 7]
LDA 00 zp
PHA
[0, 0, 255, 15, 200, 7]
LDA 05 zp
PHA
[0, 0, 0, 255, 15, 200, 7]
LDA 06 zp
PHA
[0, 0, 0, 0, 255, 15, 200, 7]
LDA 07 zp
PHA
[0, 0, 0, 0, 0, 255, 15, 200, 7]
LDA 08 zp
PHA
[0, 0, 0, 0, 0, 0, 255, 15, 200, 7]
LDA 09 zp
PHA
[0, 0, 0, 0, 0, 0, 0, 255, 15, 200, 7]
LDA 02 imm
STA 05 zp
LDA 0f zp
CMP 04 imm
BEQ 36 imm
LDA 0f imm
AND 03 zp
STA 07 zp
LDA 03 zp
LSR acc
LSR acc
LSR acc
LSR acc
STA 06 zp
TAX
LDA 00 imm
CLC
ADC 07 zp
DEX
BNE fb imm
STA 08 zp
LDA 0f zp
BNE 06 imm
ISC aea5 abs
CLC
ADC 01 imm
ASL acc
ASL acc
ASL acc
ASL acc
TAX
RTS
The initial address that was written to the stack is blocked by a bunch of values, and the program can't get to it. Can someone help?
JSR cbaa abs
[200, 7]
LDA 00 imm
STA 04 zp
LDA ff imm
JMP f0a1 abs
STA 03 zp
LDA 0f imm
PHA
[15, 200, 7]
STA 0f zp
TXA
PHA
[255, 15, 200, 7]
TYA
PHA
[0, 255, 15, 200, 7]
LDA 00 zp
PHA
[0, 0, 255, 15, 200, 7]
LDA 05 zp
PHA
[0, 0, 0, 255, 15, 200, 7]
LDA 06 zp
PHA
[0, 0, 0, 0, 255, 15, 200, 7]
LDA 07 zp
PHA
[0, 0, 0, 0, 0, 255, 15, 200, 7]
LDA 08 zp
PHA
[0, 0, 0, 0, 0, 0, 255, 15, 200, 7]
LDA 09 zp
PHA
[0, 0, 0, 0, 0, 0, 0, 255, 15, 200, 7]
LDA 02 imm
STA 05 zp
LDA 0f zp
CMP 04 imm
BEQ 36 imm
LDA 0f imm
AND 03 zp
STA 07 zp
LDA 03 zp
LSR acc
LSR acc
LSR acc
LSR acc
STA 06 zp
TAX
LDA 00 imm
CLC
ADC 07 zp
DEX
BNE fb imm
STA 08 zp
LDA 0f zp
BNE 06 imm
ISC aea5 abs
CLC
ADC 01 imm
ASL acc
ASL acc
ASL acc
ASL acc
TAX
RTS
The initial address that was written to the stack is blocked by a bunch of values, and the program can't get to it. Can someone help?