We are running a survey to help us improve the experience for all of our members. If you see the survey appear, please take the time to tell us about your experience if you can.
Hi, I am using STM32F4 Discovery board and Keil MDK-ARM version 4.53.0.0.I made some code which contains static variable.I have made two different .uvision projects in which project A works fine(static variable gets initialized to zero) whereas in project B this variable shows garbage value.The only difference in these two projects is startup file that is created automatically at the time of project creation.I want to know how this file differ for the same environment? The project scenarios are as given below:
The source code for both are same as follow. ********************************************* #include <stdio.h>; #define MAX 5
void sumIt(int);
void main() {
int i =0; int array[5]={1,2,3,4,5};
for(i = 0; i<MAX; ++i) sumIt(array[i]);
}
void sumIt(int num) {
static int sum = 0;
sum+=num;
********************************************* 1.Project/compiler settings and other environment settings are also same for both.
2.The only difference is in the start up file (That is loaded automatically by microvision while creating project)
PARAMETER ========= The difference in the start up file is as follow
Project A ============ IMPORT __main LDR R0, =SystemInit BLX R0 LDR R0, =__main BX R0 ENDP
Project B ============ IMPORT main LDR R0, =SystemInit BLX R0 LDR R0, =main BX R0 ENDP
3.As a result of this, change seen in the map file is as follow PARAMETER ========= MAP File
Project A ============ startup_stm32f4xx.o(.text) refers to __main.o(!!!main) for __main
Project B ============ startup_stm32f4xx.o(.text) refers to sample.o(.text) for main (sample.o is .object code of source file sample.c)
4.And as a result of these, variable gets initialized with values as follow
PARAMETER ========= variable value
Project A ============ sum = 0x00000000(initialized to zero)
Project B ============ sum=0x1C49D227(any non zero value)
Project B looks more like startup for an assembler program - something that do not make use of the C runtime library, and where there are no reason to follow any C requirement of having all global variables initialied before enter of main().
Not sure how project B was created, but it is not a suitable startup file for a C program.