Hi
Want to know if there is a way to instruct the compiler to preserve more than the normal registers (R0-R3, R12, LR, PSR, and PC) during an interrupt.
Regards Viktor Bucher
Surely, the compiler has to preserve whatever registers it uses - and, if it doesn't use a register, there is no point in preserving it?!
Not exactly. I wrote an assembly function that is called from inside the interrupt. This function uses more registers than those that are preserved by the interrupt itself
but if YOU wrote the function in assembly, you must do the preserve and restore manually...
hence:
void __asm foo(void) { push... . . . pop... }
I know that and I did so. Those registers must only be preserved during an interrupt call and not when called from the main program. So this presents a difference. If I put that function in a library It would be a good idea not to preserve those registers and let the compiler do that when needed. That is why a asked the question.
But, surely, the function must always preserve whatever registers it modifies - however it gets called?!
Not all registers should be preserved during calls. At least with ARM tools.
A compiler always (at least if the processor allows it) defines a number of scratch registers that may be used without second thought.
But any function playing with other registers must take 100% responsibility for such use, and restore the expected state before returning or calling another function that hasn't been explicitly written to accept these exceptional register uses.
Those registers must only be preserved during an interrupt call and not when called from the main program.
Then write two versions of the function, one for calling from the ISR and one for calling from the main program. That avoids pesky reentrancy issues, too.
If you are using a Cortex M3, the PC, xPSR, r0-r3, r12 and LR are saved automatically BY THE PROCESSOR when the interrupt is generated.
If you use any other registers than these as scratch registers, you will need to save those your self. That is the same for non-interrupt functions (i.e. if you use any registers other than these you will need to save them) This means that on a Cortex-M3 processor you DO NOT need to distinguish between interrupt functions and non interrupt functions. The processor handles saving scratch registers used in the ARM calling convention you never need to know you are being called from an interrupt or a normal call.
Hi all
I agree with you people. I already know all the solution posted here. I just wanted to know if there is a command/pragma whatever may exist to inform the interrupt function it should preserve some other registers. Just that
Thank
I am speaking about Cortex-M3 here, if your processor is different please ignore.
No there is not because there is NEVER a need for this. When I say NEVER it is not that there are some cases that it may be useful, it is NEVER. You NEVER need and NEVER want this.
The Cortex-M3 was built with the ARM calling convention fully in mind. Any function that follows the ARM calling convention can be called from an interrupt or a normal function call. There is nothing more to do. The Cortex-M3 handles any differences between interrupt / normal call BEFORE the calls are made. There is no need to do anything different in an interrupt routine or a normal function call. If you are finding a need for this than you should reevaluate why you think you have this need, because you do not.
Robert,
I concur.
--Cpt. Vince Foster 2nd Cannon Place Fort Marcy Park, VA (P.S. my shortest post ever)
Actual content: 2 words;
Total word-count: 18.
/*------------------------------------------------------------------. ; Dear Andy, I comment my code with the same ratio of content ; ; to code ratio. This keeps the clearity to any other code-monkey ; ; who reads my work easy to understand. ; '------------------------------------------------------------------*/ word_count = (u8)49; return( word_count );
--Cpt. Vince Foster 2nd Cannon Place Fort Marcy Park, VA
View all questions in Keil forum