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 lblI 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
There appears to be a typo in your code.
Best regards
Simon.
Right, that's a typo. Thanks!
You can of course just change your first example to "LDR PC,=lbl", and avoid having to preserve and restore R12.
Note however, that "LDR PC,=lbl" is largely equivalent to "B lbl", and I suspect that what your really want is "LDR PC,lbl" which branches to the address stored at "lbl".
Thanks, but I need to assign the value to the PC using the stack.