OldGuru Posted March 1, 2002 Share Posted March 1, 2002 What is the limit of nested subroutines in the 6502 on the 5200? What do I mean? In some cases, I have my main code jumping to a subroutine, this subroutine calls another one and so on... The uP has to retain the return address of each JSR. I'd assume that it stores these addresses (and what else?) in the Stack (where is the stack located on the 5200?). How many memory location does each JSR take ? Now, if the Stack is located in the memory such that it overwirtes some program variables, the code execution will be corrupted... How does that work on the emulators? Where is the stack located? Is there a "different" memory location for the JSR return address than the stack? ...and, yes, there are quite a few questions here but an answer to any of them could help... Thank you, OldGuru. Quote Link to comment Share on other sites More sharing options...
DEBRO Posted March 1, 2002 Share Posted March 1, 2002 When you do a JSR the address-1 of the next operation is pushed on the stack. The stack is located in page one ($0100 - $01FF). Quote Link to comment Share on other sites More sharing options...
+Cafeman Posted March 1, 2002 Share Posted March 1, 2002 Wow, so you have 255 bytes on the stack? That's pretty much. Quote Link to comment Share on other sites More sharing options...
DanBoris Posted March 1, 2002 Share Posted March 1, 2002 So, assuming you don't use the stack for anything else, you can have 128 nested subroutines. This would be slightly less on a 5200 because of interrupts which also push stuff on the stack. Dan Quote Link to comment Share on other sites More sharing options...
OldGuru Posted March 1, 2002 Author Share Posted March 1, 2002 Ok, so to make things a bit more complicated: If there are JMP instructions in various routines, the address will not be restored, right? So, if instead of using RTS, I'd use JMP (back to a place I'd want) the stack can overflow... Is that right? Not that I do that in my code, I just try to understand the limits... The actual reason that I posted my questions is that after adding some more features to my game, the text area on the screen gets corrupted... That happend on the real console but not on VSS (0.7)... Still have no idea why... Thanks, OG Quote Link to comment Share on other sites More sharing options...
Nukey Shay Posted March 2, 2002 Share Posted March 2, 2002 That's why you would PLA it off the stack if you need to JMP out of a subroutine. But a more practical solution is just to test the conditions before entering a loop or subroutine. Quote Link to comment Share on other sites More sharing options...
Nukey Shay Posted March 2, 2002 Share Posted March 2, 2002 By the way... Most of my programming experience is from hacking into games and adding my own little routines to them (as in "combo this, cheatin' cpu!")...so I had used branching instructions much more often in place of JSR's or JMP's (in an attempt to keep my routines as relocateable as possible). You might want to try them for short interruptions to the routines. Also, 2 PLA instructions will remove only the last stored return address. If you wanted to kill off the previous return address but keep the current one intact, you would need to PLA TAX PLA TAY PLA PLA TYA PHA TXA PHA...pretty messy, isn't it? And you can seriously muck things up when manipulating the stack in this manner...so it's best to try to build the routines as straight as possible from the start rather than wedging stuff in later. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.