How can I save & restore the program counter ?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kiarash
    New Member
    • Sep 2006
    • 3

    How can I save & restore the program counter ?

    In a called function,I want to save the "program counter" , when program reached to a specified location ,& then return from the function .
    In the next calling of this function , I want to restore "pc+1" to continue the process.

    How can I do this?
  • D_C
    Contributor
    • Jun 2006
    • 293

    #2
    Can you give an example of precisely what you want? It's not so clear to me. Besides, I'm not sure one can gain access to the program counter.

    C++ files may also be relative. That is suppose you have a function that prints the memory location of an instruction, then returns. If you run the program a second time, you may not get the same value.

    Comment

    • Banfa
      Recognized Expert Expert
      • Feb 2006
      • 9067

      #3
      To get access to processor registers you normally need to drop into assembly code.

      However the C/C++ standards do not define this, only some C/C++ compilers support it and all in propriatory ways.

      For instance in MSVC I think (IIRC) that the sytanx is

      Code:
      void fn(void
      {
          // Some C code here
      
          asm 
          {
              // we've dropped into assembler, assembly code here
          }
      
          // More C code here
      }
      However you'd better be really sure you know what you are doing it you are going to mess directly with the pc.

      Comment

      • kiarash
        New Member
        • Sep 2006
        • 3

        #4
        Thanks for your reply,

        I use IAR 4.4 for arm.
        In this program I have a timer interrupt. I have a specified routine to be done in this sub routine.But every part should be done in one time,then the program should wait until next interrupt service routine call to continue.

        I tried function pointer arrays & also switch-case routines.But these routines are not practical for me as they are very time consuming.

        If I can save PC, in the next interrupt call I can continue the routine very quickly.
        Isn't so?

        Comment

        • Banfa
          Recognized Expert Expert
          • Feb 2006
          • 9067

          #5
          Originally posted by kiarash
          If I can save PC, in the next interrupt call I can continue the routine very quickly.
          Isn't so?
          For the ARM platform I would definately expect you to have a way to write assembler instructions but it may require an assembler file (i.e. write 2 functions 1 to read the pc 1 to write it). You will need to check your toolset documentation.

          What you propose to do may work, on the other hand it may cause everything to fall over in a big heap.

          Comment

          • D_C
            Contributor
            • Jun 2006
            • 293

            #6
            In assembly, yes, this is do-able. Anytime an interrupt is called, it automatically pushes the registers and PC onto the stack. In order to pop the same information off of the stack, you should have an assembly command that "Returns from interrupt".

            From the limited assembly programming I have done, RETI was the return from interrupt command, that I don't think I ever needed.

            Comment

            Working...