GBDK libraries documentation
PrevChapter 2. The Gameboy as a TargetNext

Using global variables

The local variables of a function are stored on the stack, requiring the compiler to calculate a variables absolute address every time its used. By declaring a variable as global it becomes statically allocated at an address in ram which in most cases makes the code more efficient.

Don't forget that global variables are still bad - but in this case its more efficient to use them.

Globals are especially good for large structures. The easiest way to access data on the stack is with the lda hl,x(sp) where x is a signed byte. If the size of the local variables is greater than 127 bytes (the upper limit of a signed byte) then significantly slower code is used.


PrevHomeNext
Avoiding PromotionUpOther