cancel
Showing results for 
Search instead for 
Did you mean: 

Hi, How can I read program counter (PCL) in C? I use STM8S standard peripheral library Best regards Nikolay

Nikolay
Associate II
 
8 REPLIES 8
Cristian Gyorgy
Senior III

I think you can only access the PC register by it's address with the debug module, so not from within the application.

Call a function and return the return address...

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

Thanks DeLorean. How can I do that? Can you give a code example?

Best regards

Nikolay

MQi.1
Senior II

Try it in STVD.

Nikolay
Associate II

Hi MQi.1,

What do you mean? I use IAR Embedded Workbench for STM8 as IDE. I cannot switch to STVD. DeLoaren's answer looks great, but I don't know how manage to get the return address of the function.

Best regards

Nikolay

STM8 isn't my area

But ARM you have a subroutine like MOVS R0,LR ; BX LR, for x86 CALL $+x; POP EAX or POP EAX; JMP EAX type constructs.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

If you have a basic understanding of the processor registers, the call stack, C calling/parameter passing model, some assembler. If you understand how the call works and pushes the return address, you should be able load it.

Look at a disassembly listing of an average app you should be able to pull a couple of nuggets.

You should be able to do a relative call and pop the return address, or call a fixed function that can retrieve a value out of a memory you can access.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
bluespirit
Associate

You can use assemble index TRAP ; LDW X, SP ; LDW ($12, RAM), X ; the PC value is stored in RAM .