gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

Coding > All globals are 32bits?!

#11808 - regularkid - Sun Oct 19, 2003 11:23 pm

So the compiler allocates 4 bytes (32 bits) for all global variables!! Anyone know why it does this? Is there a way to turn this off....maybe a compiler setting? I was hoping there was a way other than using a structure with all the variables inside it.

On a side question: In the memory map, there are two sections for data in IWRAM: (.bss and .data). Why the need for two different sections? I can't see a difference between the data that goes in each one. Any ideas?

Thanks!
_________________
- RegularKid

#11809 - DekuTree64 - Sun Oct 19, 2003 11:39 pm

.bss is uninitialized data, so anything in there will just be zero-filled by the Crt0 when starting up. In the .data section, it stores all the initial values in ROM and copies them to IWRAM when starting up.
As for variable sizes, I don't think there's any way of talking the compiler out of it. It does seem to work if you make an asm file and declare them yourself though, like
Code:

@vars.s
.data
.align 2
.global var2bytes
var1:
.hword 0
.global var1byte
var1byte:
.byte 0

And declare them as externs in your .c/.h files. Just be sure you keep things aligned to their own size, like don't put a single byte var and then a word right after it. I have only started to experiment with this since yesterday though, so only trust your own tests as to wether it's really ok.
_________________
___________
The best optimization is to do nothing at all.
Therefore a fully optimized program doesn't exist.
-Deku

#11810 - Burton Radons - Sun Oct 19, 2003 11:48 pm

.bss stands for "block started by symbol", but its meaning has nothing to do with its usage, which is a block which has no initialiser (interpreted as meaning "should be initialised to zero" nowadays). Because there are no contents to it, there is no point to storing it in the file. This was back in the bad old days when linkers had to be written such that they could swap in and out necessary memory manually because there wasn't enough to link a whole program otherwise; nowadays it's just a complication.

It looks like this bug only shows up if the globals are uninitialised; just initialising them to a value appears to kick them into being linked properly.

#11811 - regularkid - Mon Oct 20, 2003 12:19 am

Thanks for the explanations!

Hey, you're right, the bug only shows itself in the .bss section. So, if I iniitlaize all my variables that are less than 32bits, it should all be fixed.
_________________
- RegularKid

#11812 - tepples - Mon Oct 20, 2003 1:52 am

DekuTree64 wrote:
As for variable sizes, I don't think there's any way of talking the compiler out of it.

I save space by putting global data into structs. For example: make a type struct Player containing all the variables related to the player and an instance struct Player p; thereof. Useful side effect: after this encapsulation, you can more easily handle multiple players.

Burton Radons wrote:
It looks like this bug only shows up if the globals are uninitialised; just initialising them to a value appears to kick them into being linked properly.

However, initializing your global variables will also make them take up space in the ROM.
_________________
-- Where is he?
-- Who?
-- You know, the human.
-- I think he moved to Tilwick.

#11819 - Burton Radons - Mon Oct 20, 2003 11:59 am

tepples wrote:
However, initializing your global variables will also make them take up space in the ROM.


Which is more vital: ROM space or IWRAM?

#11822 - tepples - Mon Oct 20, 2003 3:21 pm

IWRAM, and that's why I use the struct method. The struct method also makes a few overlay tricks easier, further saving IWRAM.
_________________
-- Where is he?
-- Who?
-- You know, the human.
-- I think he moved to Tilwick.

#11836 - Gordon - Tue Oct 21, 2003 4:16 am

Where can I find out information of all section names such .bss, .data, ctor, ...stab, .custdata , .rodata ?
what is ctor stand for?
what is .rodata stand for? ..
thx

#11839 - tepples - Tue Oct 21, 2003 5:10 am

Most common program sections:
.text: program code
.rodata: read-only data
.data: pre-initialized data, copied to RAM
.bss: uninitialized data

C++ functions:
ctor: short for C++ constructor
dtor: short for C++ destructor
_________________
-- Where is he?
-- Who?
-- You know, the human.
-- I think he moved to Tilwick.

#11840 - Gordon - Tue Oct 21, 2003 6:05 am

Where did you find out all the information?