I am checking some terrible C source code; I haven't got any idea about how to maintain it or cooperate with it. But I found a very fundamental problem. It does NOT have a startup.asm; it has a startup.c using the powerful C extension "#pragma". So, the C runtime environment is setup by "#pragma section", "#pragma intvect", "#pragma asm". I quite worry about such a startup.c; so I contacted the FAE of our local distributor. The FAE is an experienced good engineer, but he told me that, this is not their standard way to setup C runtime environment; they definitely provided the startup.s from Day 1.
What will be the side-effect, when the C runtime environment is setup by the C extension "#pragma"?
"What will be the side-effect, when the C runtime environment is setup by the C extension "#pragma"?"
probably none.
as far as the mcu is concerned, it simply executes a series of commands sent to it. what generated those commands is irrelevant.
as to the wisdom of using .c start-up files, check the cmsis document and you will see that they plan to use .c start-up files later.
no, I wouldn't lose too much sleep over that.
CMSIS applies specifically to ARM Cortex-Mx, and one of the key goals of that architecture's design was to allow "all C" coding - in particular, witout requiring assembler startup code.
The 8051 is very different!
That's why I said early on that this can't be treated (just) as a generic topic - it does depend very much on the particular toolset!
the cmsis example is to show that there is nothing wrong with using .c start-up code.
and I am not sure why that wouldn't / couldn't be extended to cover the 8051.
Yes, the Cortex really are special.
No __irq keywords for interrupt handlers.
A fixed address for storage of information of the stack, so that the processor can initialize an initial stack on its own.
C is well qualified for low-level tasks.
One thing to care about when having strict C code to initialize a processor is that this init() function don't have access to the full C RTL until it has called a init function available with the C RTL - the call we normally see either directly before our main() is called, or the last call in a normal assembler startup file in case the C RTL init function performs the call to main().
Same thing with zeroing memory and assign of initialized data.
But having the init code in C or assembler is really irrelevant to this specific thread. In this case, we are talking about assembler initialization - but assembler instructions in a C file instead of in a separate assembler file.
This is bad modularization. And potentially, there may be part of the initialization in assembler and part in C, and lots of assumptions about compiler-specific requirements.
When talking about an all-C initialization for a Cortex processor, it would normally be the compiler vendor who supplies a complete system with the required helper functionality in the CRTL, and with a sample startup.c file containing the "normal" initialization steps.
'Yes, the Cortex really are special.'
like what?
"In this case, we are talking about assembler initialization - but assembler instructions in a C file instead of in a separate assembler file."
I am not sure if that (inclusion of assembler instructions in a C file) makes a big difference. end of the day, they produce the same result.
I do see a benefit to create a separate assembler file for those assembler instructions if you have tons of them -> essentially create a .asm start-up file.
but if the amount of assembler instructions is small, it probably makes more sense to include them in a .c file.
my point is that it is hard to make an absolute statement about whether it is right (or wrong) to embed assembler instructions in a .c file. it depends on many other factors.
"but if the amount of assembler instructions is small, it probably makes more sense to include them in a .c file."
That very much depends on used compiler.
Some compilers will remove support for source-level debugging if mixing assembler and C.
Some compilers will merge the compiler-generated assembler instructions with the inlined assembler instructions and then run everything through the optimizer, potentially resulting in an object file containing other instruction sequences than the programmer did write.
So in the general case, C and assembler shouldn't be mixed in the same file. Having assembler in a C file in the first place is outside of the C standard, so any such use should not be considered unless the specific compiler have specific documentation saying both how to do it and clearly documenting limitations or potential problems.
A compiler vendor normally ships sample startup files since there are a number of needs that must be fulfilled to make the C RTL work as expected and make the processor reach main() in a way fulfilling the requirements from the C standard. Not using the compiler-supplied startup file (with optional additions for supporting extra hardware) means that a developer have to take his chances about his own startup code being enough to fulfill all initialization requirements. Compiler vendors often do not write a document listing these requirements for the simple reason that they consider it enough to just ship a startup file.
View all questions in Keil forum