Forum : 8051
Original Post
Post Information | Post |
---|---|
December 9, 2008 - 1:50pm
|
Hi, I just wonder if I declare a global variable above/outside any function like this:... unsigned char myVar=0x33; void myFunction(void){ ...is myVar then initialized to 0x33? If I scan through the generated listfile I don't see anything like it. Thanks in advance, Henk |
Yes, your variable will always be initialized if you declare it this way. The initialization is done by the startup code, which performs the global variables initialization before jumping to main().
The only uncertain case is when you declare a global variable without initializing it, such as:
In such a case, depending on how the compiler directive INITSTATICVAR is set in your project, the "myVar" variable can either be initialized to 0 or not initialized at all (i.e. contains random junk upon main() entry)
Regards,
Bruno
Hi,
thanks,
but if I init it like:
unsigned char myVar=0x33;
I suspect to find some '33' or '51' value in the corresponding list file but I don't see any?
As I'm updating a aplication made by someone else and as I'm fairly unexperienced to this compiler I found that the startup file is changed at two locations:
old : TALEN EQU 0FFh ; typical length is 256 bytes.
new: TALEN EQU 07Fh ; length is 128 bytes.
old : ITIO EQU 001h ; make non-zero to permit I/O initialization
new : ITIO EQU 000h ;
But I don't think these two do something with variable initialisation, right?
Henk
Hi
The way you initialize your variable is the good one.
The variable is correctly initialized.
I do not understand the reason of this question.
Can you let us know what you expect to see and for what reason you need to see it.
Here is what I can say without knowing what you attempt to do:
If you need to verify that it is correctly done you can run the simulator and have and search in the xdata view the symbol "myvar"
You will see that it has the value 33.
If you want to see the code that is generated you can search in your .m51 file the symbol "?C_INITSEGSTART" and to which address it correspond. Then you can search in the "disassembly view" or the "code view" this address and see the corresponding code.
Matloub