2

This may be a stupid question, but in my assembly code, during debugging, I have

pop{r2-r6,pc}

and I think it is giving me an hard fault exception. I understand what pop does, but I am unsure what the pc part means. I cannot find it explained anywhere on the internet and it is not a variable in my code anywhere.

I am using keil on an stm32 in c++

Dude
  • 261
  • 2
  • 5
  • 14
  • `pc` is just an alias for `r15` in your case, though more generally see Mike's answer. – OJFord Jul 16 '14 at 10:11
  • "pop pc" is what's popularly used to implement returning from functions (it pops the return address from the stack) – sehe Jul 16 '14 at 10:12

2 Answers2

5

pc or r15 is the program counter, the register which gives the address that the processor fetches instructions from. Changing it to another address makes the program execution jump to that address.

In this case, the address is read off the stack to return from a function call; the return address would have been pushed onto the stack (from the link register lr or r14) at the start of the function.

If that's causing a crash, then it's probably because the address on the stack has been corrupted. Perhaps you're writing outside the bounds of a local array, or overflowing the stack with too deep a function call level.

Mike Seymour
  • 249,747
  • 28
  • 448
  • 644
  • That helps. There is a very deep function call level, I will have to see if I can fix that somehow. – Dude Jul 16 '14 at 11:35
0

The PC register is the program counter, it holds the address of the next instruction to be executed on an ARM architecture (STM32 uses the ARM architecture).

The default in ARM assembly it to simply overwrite the PC register when a function is to return. What you are seeing with the pop statement is just a direct way to return, see here.

The rest of your question is neatly explained in Mike's post.

Stian Svedenborg
  • 1,797
  • 11
  • 27