This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Replacing branch-instruction with address assignment to PC

Hi,

in a thumb-assembly file (my toolchain is gcc 4.8), I want to replace a branch with address assignment to the program-counter.
So instead of:

b   lbl

I want to have something like:

pc = address(lbl)


This solution works. Here, I use the following code to store the address of a label in a register (here r12) and assign this to the pc.

ldr r12, =lbl   @ store the address of lbl in r12
mov pc, r12     @ store the address in pc (r15)

But when I, as shown below, use the stack to restore the value of r12 the execution crashes:

push {r12}          @ place holder for the address calculated later
push {r12}          @ save the r12 on stack
ldr r12, =lbl       @ store the address in r12
str r12, [sp, #4]   @ store the address in the slot reserved in line 1
push {r12}          @ restore r12
pop  {pc}           @ set pc to the address calculated in line 3 and stored on stack in line 4

If I, as you can see below, first store the address from the stack in the r12 and then move it to pc the code runs without any problem.

ldr r12, =lbl   @ store the address of lbl in r12
push {r12}      @ store the address on stack
pop  {r12}      @ load the address from the stack back in the r12
mov pc, r12     @ assign the address to the pc

 

any ideas what could be the reason why in pop {pc} (in the second code-segment) doesn't work?

PS. The code is being executed on a Cortex-M4F