Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting the PC value in ARM assembly

I have a Windows Mobile 6 ARMV4I project where I would like to get the value of the program counter.

The function is declared like this:

extern "C" unsigned __int32 GetPC();

My assembly code looks like this:

GetPC FUNCTION
    EXPORT GetPC
    ldr r0, [r15]   ; load the PC value in to r0
    mov pc, lr      ; return the value of r0
ENDFUNC

But, when I call the GetPC() function, I get the same number every time. So, I'm assuming my assembly isn't doing what I think it's doing. Can anybody point out what I may be doing wrong?

Thanks, PaulH

like image 732
PaulH Avatar asked Oct 23 '25 07:10

PaulH


2 Answers

PC is same every time, because ldr r0, [r15] has fixed address (PC is always same there). use R14 because it holds return address, that might be what you need. or use inline functions.

like image 109
Andrey Avatar answered Oct 25 '25 22:10

Andrey


If this is a true function, you should always get the same value in this code since you're just returning the current PC which is inside GetPC.

You probably want:

move r0, lr // put return address in r0
move pc, lr // return
like image 29
plinth Avatar answered Oct 26 '25 00:10

plinth



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!