Touched
Resident ASMAGICIAN
- 625
- Posts
- 10
- Years
- Age 123
- Seen Feb 1, 2018
Since the only pokemon game I've ever hacked is R/S, I don't have any good example. In other GBA games, there are times where I need to push some registers (r0-r3) to insert a routine. I'm putting those just for insurance.
To check the availability of the stack, I'm checking all routines before and after this function. A function with SP modifying instruction can limit the space on the stack. If he calls the function inside a SP modifying function, there is a chance that pushing will overwrite important data.
Since the game was compiled, it follows a rather strict calling convention. This means that routines that are designed to be called as functions (i.e. they are called and then return) do not need push/pop r0-r3 because these are reserved for arguments and return values - a caller assumes the callee will alter these values. This is always the case for functions. However, you may need to push these registers if you are modify existing functions by hooking into a routine. However, since the question was about a routine that was called from a script, we know it is a function; the script handler and function code for callasm expect r0-r3 to be messed up, thus we need not push them. Another point I should make is that LR is not used therefore pushing it is not necessary.
I assume stack overflow (for the user stack at least) is incredibly unlikely unless you are implementing a recursive function that uses stack space or you are allocating an absurd amount of space on the stack; I highly doubt you could overflow the stack just by pushing once. Have you ever seen the stack overflow?