cancel
Showing results for 
Search instead for 
Did you mean: 

General rule about the value of PC in cortex-M4 MCU (STM32f429)

NMaun
Associate

Hello, maybe it's not the right place to ask this type of questions, in case I apologize.

While trying to understand how ARM assembler works, I came across this problem and I'm currently struggling to find the answer.

Before explaining better my question what I want to do is to know the exact value of the instruction pointer of a certain instruction X, at runtime and without resorting to the debugger. I want to monitor the address of certain isntructions by sending it to an external tool, so I want to know, if I use the below code, which are the adjustment (if any) to the value of R1 in order to contain the address of the instruction X.

This is what I thought to acomplish this task:

MOV R1,PC
ADD/SUB R1, something ;if some adjustment is required
X ; instruciton I want to monitor
;send R1 to an external component

I cannot quite understand which is the rule about the value of PC, I found that is + 4 or +8 depending on if it is Thumb or ARM instruction but it seems that this rule is not always followed. Since I'm working on a cortex-M4 (STM32f429) only Thumb instruction are supported

I try to explain better with some example

If I analyze this code:

 80001d0:	b508      	push	{r3, lr}
 80001d2:	4b03      	ldr	r3, [pc, #12]	
 80001d4:	b11b      	cbz	r3, 80001de 
 80001d6:	4903      	ldr	r1, [pc, #12]	
 80001d8:	4803      	ldr	r0, [pc, #12]	
 80001da:	f3af 8000 	nop.w
 80001de:	bd08      	pop	{r3, pc}
 80001e0:	00000000 	.word	0x00000000
 80001e4:	2000000c 	.word	0x2000000c
 80001e8:	0800328c 	.word	0x0800328c

I found that in the first ldr pc is equal to the function pointer +2

in the second ldr also the value of pc is the function pointer +2, but in the last ldr pc is equal to the function pointer +4 (and is what that i'm expecting to appen in every case, if i've understood correctly).

Hope to been clear enough on which is my problem.

Thank you for your time, hope you can help me in some way

Best regards

1 REPLY 1

Thumb-2 has both 16-bit and 32-bit instructions, that's why you see either +2 or +4 bytes.