Skip to main content
Nikolay
Associate II
May 27, 2021
Question

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

  • May 27, 2021
  • 5 replies
  • 2849 views

..

    This topic has been closed for replies.

    5 replies

    Cristian Gyorgy
    Associate II
    May 27, 2021

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

    Tesla DeLorean
    Guru
    May 27, 2021

    Call a function and return the return address...

    Tips, Buy me a coffee, or three.. PayPal VenmoUp vote any posts that you find helpful, it shows what's working..
    Nikolay
    NikolayAuthor
    Associate II
    May 28, 2021

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

    Best regards

    Nikolay

    Tesla DeLorean
    Guru
    May 29, 2021

    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 VenmoUp vote any posts that you find helpful, it shows what's working..
    MQi.1
    Senior II
    May 28, 2021

    Try it in STVD.

    Nikolay
    NikolayAuthor
    Associate II
    May 29, 2021

    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

    Tesla DeLorean
    Guru
    May 29, 2021

    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 VenmoUp vote any posts that you find helpful, it shows what's working..
    bluespirit
    Associate
    June 18, 2021

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