Skip to main content
NGUYEN-VAN NHI
Associate II
March 18, 2023
Solved

How can I get the PC register value in STM32? Help me please.

  • March 18, 2023
  • 4 replies
  • 3686 views

..

    This topic has been closed for replies.
    Best answer by Tesla DeLorean

    R14 == LR

    R15 == PC

    calledfromwhere ; subroutine returns address from whence it came
     movs r0, lr
     bx lr
     
    Usage
     bl calledfromwhere ; assembler
     
     printf("%p\n", calledfromwhere() ); // C

    4 replies

    S.Ma
    Principal
    March 18, 2023

    In debug mode, place a breakpoint or stop the code, and view the disassembly window which will show you which address the PC value is. Or view the SFR core registers.... why do you need this?

    NGUYEN-VAN NHI
    Associate II
    March 18, 2023

    I want to read PC value to use it for program and not for observation purpose. Is there any way sir?

    Tesla DeLorean
    Guru
    March 18, 2023

    Explain better what it is you're attempting to do.

    I​n assembler you can move the value to other registers or do math directly with the PC. You could perhaps create a subroutine which moves LR to R0 and see where it was called from.

    P​erhaps learn MCU registers, instructions and assembler?

    Tips, Buy me a coffee, or three.. PayPal VenmoUp vote any posts that you find helpful, it shows what's working..
    NGUYEN-VAN NHI
    Associate II
    March 18, 2023

    Yes, I have some programs running on flash, I want to save some values from register r0 to r15 and the value of register pc, so that I can go back and continue doing my old work.

    Tesla DeLorean
    Tesla DeLoreanBest answer
    Guru
    March 18, 2023

    R14 == LR

    R15 == PC

    calledfromwhere ; subroutine returns address from whence it came
     movs r0, lr
     bx lr
     
    Usage
     bl calledfromwhere ; assembler
     
     printf("%p\n", calledfromwhere() ); // C

    Tips, Buy me a coffee, or three.. PayPal VenmoUp vote any posts that you find helpful, it shows what's working..
    Pavel A.
    Super User
    March 18, 2023

    GCC has intrinsic function __builtin_return_address() exactly for this