This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Regarding startup file

Hello, I written simple c program mention below by selecting the lpc2148 chip but not include its startup file in the project. when i run it will go in infinite loop. I also put the break point but i think its not coming to that execution line. Is it necessary to include startup file?
void main()
{ int a=2,b=3,c; c= a+b;
}

  • How do you think your code will startup if you don't include the startup file. Have you bothered to look at the Keil examples?

  • Here is a little known secret.

    Some processors (like 8051 and ARM chips) don't contain any logic to locate any main() function on startup. So the compiler manufacturer or the developer have to add a bit of work-around code to handle this processor limitation.

    The workaround code takes a bit of time and code space, but you really need it. In theory, a compiler vendor could create a C compiler that creates work-around code directly when it compiles the program, but that is seldom done. The reason is that different processors have requires different types of workarounds to manage to reach main(), so it's better to have the workarounds outside the compiler.

  • is a bug in the processor? any processors got bug fixed?

  • hi Per, thanks for reply. I have one basic question. I think when we start the processor it will jump to location 0x000000000. Can't the main function locate at that memory location without start up file.

  • I wouldn't see it so much a bug as a limitation. Another limitation is that many processors don't manage to initialize any global variables so you need to supply a startup file to zero global variables that isn't given an explicit initialization in the source code. And the startup code must also figure out a way to give specific start values for the global variables that needs that.

    In short, there are lots of functionality the startup files have to help with, to make sure that the processor can reach main() in a way that is compatible with the C language standard.

    Remember that a processor is normally not designed for a specific programming language. So there are different requirements when starting a C program or a Pascal program.

    The startup file is the workaround "glue" that helps making the processor compatible with a specific programming language. If you write programs in assembler instead of an high-level languaeg, then the processor can manage to run the program directly, without any helper code.

  • In theory, main() could have the code placed at address zero. But that would not be enough to make the processor compatible with the C language standard. Because the processor does not know that the C standard requires all global variables to be given initial values. And the processor does not know what requirements the C language program may have for a call stack. Note that some processors - see for example Cortex-M3 ARM chips - do know how to prepare a call stack before starting the user application.

  • but not include its startup file in the project.

    That makes less of a difference than you think. The linker will insist on having some startup file. If you refuse to put one in the project, it'll still pick one from the library. Go figure.

  • Why the great debate to a simple problem? Just add the startup file to your project and compile it and, guess what? your code will startup. There's no need to start debating whether various processors can run 'C' code with/without it, they can't. End of.

  • so how do you stop it gettnig the library?

  • Why the great debate to a simple problem? Just add the startup file to your project and compile it and, guess what? your code will startup. Why are you trying so hard to avoid using the startup file. It is required to setup vectors, stack, heap, and initialise data etc. In theory you could locate your main() at the processor's start address but then you would still end up having to do what the startup files does at the beginning of your main() function.

    Just use the startup file and stop wasting your time trying to avoid it!

  • can i write my own start up codes and librarys?

  • Why the great debate to a simple problem?

    I guess you don't like schools or knowledge. Why understand things when you can just follow a magic recipe? Problem is, you need to understand things to be able to take later steps and be able to deduce where these steps will lead you.

    In the end, a problem is only simple if you already know the answer - or have failed to see the real problem that needs to be solved.

  • Per, you guessed wrong!

    The question asked was "Is it necessary to include startup file?" and the simple answer is yes because of the need to set up stack, heap, vars etc. It didn't require you to show off your superior knowledge and confuse this beginner even more.

    Also this forum is meant to be for Keil/ARM related issues so the answer to the newbie's question is "yes, you need to use the startup file - go and read a decent 'C' book or visit a 'C' forum if you want to know why startup code is required for processors".

    This forum would be more useful if all the 'C' questions were blocked and the posts were limited to Keil/ARM issuess.

  • There is a description of the start up file in the manuals. Here's the one for the 8051.

    http://www.keil.com/support/man/docs/c251/c251_ap_startup.htm