returning to different address form interrupt with c routine

i used INTX0 for intering to the MENU of my program
and need to back to a address, labled in my main program. so i need to change the contain of the memory that SP is pointed at.
how can i do it in C . i chaned the SP to the address that point to 0x0000 and expected to be reset but it didnot.

Parents
  • If I understood, you are using an external interrupt to directly 'jump' to a function that process the user interface, and want to 'return to' a label in your main program body, is that what you want?

    A few comments on this:

    1) It is a really bad, bad idea to do this, in any language, but especially in C.

    While from a purely academic thought experience it would be possible to manipulate the return stack to do that, it requires much more than 'change the contents of [SP]'. The C runtime would be interrupted at any point, and would never return. Any non-pure reentrant code would probably cause a runtime corruption of non-atomic data accesses.

    Do you have any knowledge of the compiler entry/leave code for interrupt handlers? This is not standardized, and would require assembly code to proper hack the return stack. Furthermore, you would have to disable interrupts while processing the 'menu option', and only enable them at a while(1); loop or something like that.

    Another twisted-mind approach would be to use a call to an element of an array of function pointers that point to the menu option functions. That could be done entirely in C. The menu options would then be processed in the interrupt thread context, and the menu interrupt function would then return to the 'main' thread context, that might just be a while(1); loop. That is a silly way of using the interrupt, and is a total inversion of good programming techniques, but would ultimately work.

    2) A label in C has local scope, i.e., it is only visible at the current code block level. Labels are very suspicious in any C program, and are in the language only to save a indent level on an infinite loop, when you can place a goto label instead of a do{}while(1);. This is very, very ugly.

    3) As I said, this is simply the WRONG APPROACH.

    Interrupts are NOT MEANT to be used that way. If you want your external INT0 pin to cause a main menu entrance, you can just set a flag at the INT0 interrupt handler, and then test for this flag at the main thread of your program, which will invoke the menu function.

Reply
  • If I understood, you are using an external interrupt to directly 'jump' to a function that process the user interface, and want to 'return to' a label in your main program body, is that what you want?

    A few comments on this:

    1) It is a really bad, bad idea to do this, in any language, but especially in C.

    While from a purely academic thought experience it would be possible to manipulate the return stack to do that, it requires much more than 'change the contents of [SP]'. The C runtime would be interrupted at any point, and would never return. Any non-pure reentrant code would probably cause a runtime corruption of non-atomic data accesses.

    Do you have any knowledge of the compiler entry/leave code for interrupt handlers? This is not standardized, and would require assembly code to proper hack the return stack. Furthermore, you would have to disable interrupts while processing the 'menu option', and only enable them at a while(1); loop or something like that.

    Another twisted-mind approach would be to use a call to an element of an array of function pointers that point to the menu option functions. That could be done entirely in C. The menu options would then be processed in the interrupt thread context, and the menu interrupt function would then return to the 'main' thread context, that might just be a while(1); loop. That is a silly way of using the interrupt, and is a total inversion of good programming techniques, but would ultimately work.

    2) A label in C has local scope, i.e., it is only visible at the current code block level. Labels are very suspicious in any C program, and are in the language only to save a indent level on an infinite loop, when you can place a goto label instead of a do{}while(1);. This is very, very ugly.

    3) As I said, this is simply the WRONG APPROACH.

    Interrupts are NOT MEANT to be used that way. If you want your external INT0 pin to cause a main menu entrance, you can just set a flag at the INT0 interrupt handler, and then test for this flag at the main thread of your program, which will invoke the menu function.

Children
More questions in this forum