Skeetendo

’Cause all games were better on the GBC

You are not logged in.

  • Index
  • → Help/Question
  • → [pokecrystal] Best practices for a stable stack and registers?

#1 2017-05-09 01:24:50

The Most Curious Thing
Member
From: New England
Registered: 2017-05-03
Post 5/25

[pokecrystal] Best practices for a stable stack and registers?

I've been making changes to routines within pokecrystal, and frequently need to use a register/pair not already used by the routine—usually bc or de. But Crystal seems haphazard with its use of registers, and there's no guarantee that any given subroutine called by that routine will use the register pair safely (or won't use it at all).

Here's an illustration of my problem with a simple routine in anim_commands:

PlayBattleAnim: ; function that normally doesn't use bc

    push bc ; new
    ; do new stuff with bc!

    ld a, [rSVBK]
    push af

    ld a, 5
    ld [rSVBK], a
    
    push bc ; new
    call _PlayBattleAnim ; uses c without pushing bc!
    pop bc ; new

    pop af
    ld [rSVBK], a

    ; do more new stuff with bc!
    pop bc ; new
    ret

_PlayBattleAnim: ; cc0e4

    ld c, 6

    ; more code, including *more* subroutines

It seems like I need to push to the stack whenever there's any doubt whatsoever. But is that okay?—will I run into clock or RAM issues?

This all ties into another question: what's the (practical) difference between bc and de in pokecrystal? When should I favor one over the other if I have a choice? It seems like Game Freak liberally mixed and matched them but I'm sure there's some rhyme or reason.

Thanks all!

edit: I see now that at least predef functions preserve all registers but a, which is handy.

Last edited by The Most Curious Thing (2017-05-09 01:57:17)


Timeless Crystal ✿ (๑◔‿◔๑)

Offline

#2 2017-05-13 21:02:26

Miksy91
Member
Registered: 2010-10-16
Post 2,309/2,339

Re: [pokecrystal] Best practices for a stable stack and registers?

I don't think there is a practical difference in using bc or de. Use whichever you feel like really.

What comes to so called "stack overflow" issue, I wouldn't worry about it too much. I don't actually know for Crystal though, but for Gold and Silver, I have noticed that the stack never grows so high that it would lead even close to possible overflow errors. So I'd simply suggest pushing the registers onto stack when you feel like you need to (although there's no need to try to be too "sparing" about them), and when you test your hack, see how high the stack actually grows for you. This is easily noticeable if the stack is completely empty (00's for instance) at first (when you start playing the hack), and you'll for example see that after you have beta-tested your hack through, 50% of it has been filled some new values.

The "highest" ram address with a non-zero value thus indicates the location where stack pointer once pointed, and not a location that would generally be accessed. Mathematically you could think of this point as either the left or right end of a Gaussian, and thus stack growing much higher from there is a very unlikely situation.

Offline

#3 2017-05-14 02:40:17

The Most Curious Thing
Member
From: New England
Registered: 2017-05-03
Post 12/25

Re: [pokecrystal] Best practices for a stable stack and registers?

Thanks! :)


Timeless Crystal ✿ (๑◔‿◔๑)

Offline

  • Index
  • → Help/Question
  • → [pokecrystal] Best practices for a stable stack and registers?

Board footer

Powered by FluxBB