By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,963 Members | 2,490 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,963 IT Pros & Developers. It's quick & easy.

How can I save & restore the program counter ?

P: 3
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?
Sep 19 '06 #1
Share this Question
Share on Google+
5 Replies


100+
P: 293
D_C
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.
Sep 19 '06 #2

Banfa
Expert Mod 5K+
P: 8,916
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

Expand|Select|Wrap|Line Numbers
  1. void fn(void
  2. {
  3.     // Some C code here
  4.  
  5.     asm 
  6.     {
  7.         // we've dropped into assembler, assembly code here
  8.     }
  9.  
  10.     // More C code here
  11. }
  12.  
However you'd better be really sure you know what you are doing it you are going to mess directly with the pc.
Sep 20 '06 #3

P: 3
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?
Sep 20 '06 #4

Banfa
Expert Mod 5K+
P: 8,916
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.
Sep 20 '06 #5

100+
P: 293
D_C
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.
Sep 20 '06 #6

Post your reply

Sign in to post your reply or Sign up for a free account.