Hi, how can i set program counter from variable in C code?
void func(void) { uint32_t XADDR; XADDR = __current_pc(); ... // need set PC from XADDR // msr pc, XADDR not compiled }
Thanks in advance
You can't. And you shouldn't be trying to, either. Not from C code, anyway.
Whatever your actual problem is, destroying the compiler's control of the program counter like that is almost certainly not going to solve it.
But i can create it for call any address as function. May be create assembler function like
__B(uint32_t addr)
and call it for jump ?
You cant set PC directly. like PC = hhhh or so. But, you can modify the PC contents in C by using go to. You have to assign a label to the address (where you want to jump) and then go to that label. Alternatively, if you want only to call function(s), then function pointer can be used.
But in any case you are not allowed this in C - PC = hhhh
You might not be able to do it in C. You can do it from C. Keil manage it somehow with their RTX. Looks like they use the __asm keyword to take full control. You can look at the source code in \ARM\RL\RTX\SRC\CM\HAL_CM1.c for example.
You must be careful.
"how can i set program counter from variable in C code?"
C's closest approximation is a function pointer.
Of course you can. What fool said you couldn't.
I can't use LR because it contain return address for function. Now i can use only
PUSH {R0} POP {PC}
Simple way if __current_pc() can modified to __current_pc(uint32_t addr)
You said you wanted to set the PC, returning runs into a whole slew of issues about stack balance, and stuff the compiler expects to be happening.
You'd be far better of just calling with a function pointer and coming back so you can clean up, rather than hosing the stack and the system up.
So is it coming back to the point you change the PC, or to the address that called the function that messing up the PC and stack?
Don't do this in C, do it in assembler where you have some explicit control.
how can i set program counter from variable in C code?
Please state the company you work for so I can avoid their products
Erik
It probably wouldn't get enough market shares that you need to intentionally avoid it.
The ones who need to play interesting games with the PC outside of what a normal function call, a function pointer, loop constructs, if/case conditionals, a throw/catch or maybe long-jumps allows, are busy writing operating systems - and they don't need to ask a web forum how to do it.
Assembler is the tool of choice for people who wants to "own" the PC.
"Assembler is the tool of choice for people who wants to "own" the PC."
That's retarded sir. We new school. java is for peeps that does own the PC.
smackdown. we out.
I'm not really so convinced Java is a good way to own any program counter. It sounds more like a way to keep the machine busy performing garbage collect - which would imply that Java is the tool of choice for creating garbage.
You're talking out of your c#=t again.