I have a snippet that clears memory before initializing a game in NES 6502 assembly. When I leave the code inside the reset proc like so, it works:
.proc reset
SEI
CLD
LDX #0
ClearRAM:
STA $000,x
STA $100,x
STA $200,x
STA $300,x
STA $400,x
STA $500,x
STA $600,x
STA $700,x
INX
BNE ClearRAM
.endproc
Run Code Online (Sandbox Code Playgroud)
However, if I try to move this ClearRAM snippet inside a scoped proc:
.scope Memory
.proc clear
LDX #0
ClearRAM:
STA $000,x
STA $100,x
STA $200,x
STA $300,x
STA $400,x
STA $500,x
STA $600,x
STA $700,x
INX
BNE ClearRAM
RTS
.endproc
.endscope
Run Code Online (Sandbox Code Playgroud)
And then load it like so:
.proc reset
SEI
CLD
JSR Memory::clear
.endproc
Run Code Online (Sandbox Code Playgroud)
It stops working. It's like it loops forever in the ClearRAM loop.
Any idea what I am doing wrong?
Thank you!
Your code clears the first 2K of RAM memory. (I don't know if the NES has more than 2K of memory.)
Because the stack is always located in the first 1K of memory on systems using a 6502 CPU (to be more precise: in the range 100h...1FFh), your program also clears the stack.
The JSR saves the address where the program flow shall continue after the RTS to the stack and the RTS instruction reads that address from the stack.
If you erase the content of the stack, RTS will read some bad value from the stack and jump to that address.
如果A程序中的寄存器包含值12h,则该指令将从堆栈中RTS读取,并且程序将在地址(指令之后)继续执行,而不是在 后面的指令处继续执行。1212h1212hRTSJSR
| 归档时间: |
|
| 查看次数: |
79 次 |
| 最近记录: |