This thread has been locked.
If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.
How can I change the stack return address to return to a common point.
I need to eit an interrupt and go to a fixed address.
many thanks
tom brown
Note that you have posted in the "Make the Switch to TI MCUs Success Stories" forum, but this is not a "success story" - is it?
Tom Brown1 said:How can I change the stack return address to return to a common point.
This is an extremely common novice question and is (almost) invariably not the way to proceed at all!!
Tom Brown1 said:I need to eit an interrupt and go to a fixed address.
Please state your actual goal; ie, what you're trying to achieve by this - it is almost certainly not a good way to -proceed!
If you're using Chrome then this is done automatically, provided you have the correct interrupt directives. Once the ISR finishes the previous PC is popped and returned to.
With assembly you only need to add the RETI instruction at the end. See the x2xx family guide for more information on how interrupts are handled.
Tony
Hi, this practice has to be discouraged than you are writing a task switcher on a preemptive OS.
Changing return address need to know exactly what was in previous routine so a better signal or semaphore or a simple global volatile variable suffice more better and cost of logic is less than changing return value.
Also program is more and more reliable and readable too.
When you switch from one executing routine to another post activation code never can be executed and this waste stack space never return usable, new activation code is executed so some new stack is wasted and also some status remain undetermined. After a short while program run out of stack space and your code crash in a bad way.
_____________________________________________
_________________________________________________
As from good programming rules DON'T DO THAT!!!!!!
_________________________________________________
You normally exit an ISR with the "RETI" instruction. This will pop the top of stack to SR and pop the next word to PC which brings it back to where it was before the interrupt. It sounds like you do not want that last action and want to go to somewhere else. Thus, thus instead of "RETI", you could do "POP SR" followed by "ADD #2,SP" and "BR #somewhere".
I do not see anything wrong in doing this. People normally do not do things like this because they normally do not have a need to do it. I would not be surprised at all if someone do have a need to do so under special situations. But I am very surprised that this someone can see the need and yet does not know how to do it. It is not a matter of style, trend, or fashion.
old_cow_yellow said:It sounds like you do not want that last action and want to go to somewhere else. Thus, thus instead of "RETI", you could do "POP SR" followed by "ADD #2,SP" and "BR #somewhere".
Hi OCY, see the short example and do some consideration about, using assembly change detail but cannot remove problem:
----------------------------------------
int noo(int x; int y)
{ // <---- prolog executed here
int a,b,c,d,e;
.... code ...
Interrupt is fired HERE <-------
// epilogue
}
int main(void)
{ // <---- prolog executed here
int a,b,c;
char str[80];
// some code here
a=noop(b,c);
// epilogue
}
------------------------------------------------------------------------------------------------------------------------------
Main prologue is executed, stack space and allocated to make space for all variables
Main code start execution...
noop called, so return address is on stack with parameters too
noop prolog is executed, stack space is allocated to make space for local variables and actual parameter preallocated by the call
-----------> interrupt is fired <--------------
return stack and status register are on stack but....
your code remove just them.. and all preallocated spaces? Two or three of these switch run stack out of space!!!!
No info on how many and what epilogue to be run to remove correct amount of stack space no way to determine how many return and parameter are on stack.
.....
In the case of assembly language some push register are necessary on top of routine so again undefined amount of stack space need to be freed by jump code.
change of compiler or version also affect amount of space allocated and position of variables.
I got a package that was modified in this way and was just crashing due to a different version number!!!!
IS THIS PRACTICE TO BE USED?????
Sorry but I discourage bad programming practices!!!
There is a common usage for this practice: when an OS forcefully kicks a running process. Then allocated memory, the complete stack and all ressources are discarded/freed and the program is terminated, returning to the OS command prompt or such.Roberto Romano said:IS THIS PRACTICE TO BE USED?????
However, on MSP, i tmakes no sense kicking a process. It is as useless as returning form main. If something goes wrong and an emergency exit has to be called, there are two ways to do it: either reset the MSP and try again, or to execute some specific code and then halt in an endless lopp or enter LPM for eternal sleep.
However, none of the two requires returning from an ISR to somewhere else than you were before the interrupt.
So you're completely right. The only use of this kind of programming that makes sense on an MSP (as far as I can think) is a task scheduler for multitasking. And indeed I use it for exactly this purpose (not an OS, jsut an isolated multitasking library). Every other problem I ever encountered never required it.
OCY: In most cases where this kind of technique is requested, people did not fully understand the purpose and scope of an ISR.
An ISR does its job unnoticed from the current operation (except for the time that passed). Typically, they want to do something in the ISR that does not belong there: changing the main program flow due to an event.
As you correctly noticed: those who have valid reasons to do such kind of manipulation shouldn't have to ask how it is done. And those who have to ask usually don't really need it. There's a reason why 'goto' doesn't work across function boundaries in the C language standard. The same applies to this kind of stack manipulation on assembly level.
**Attention** This is a public forum