I am in a situation where I need to store a pointer to the next instruction in Thumb-2 assembly code. Thumb-2 instructions can be 16- and 32-bit, and are thus halfword-aligned. When I use PC
as the source operand in an add
, I actually read the word-aligned program counter plus 4 (that is, either 2 or 4 more than the current instruction). Therefore, I need to add either 0 or 2 to the current program counter to get the next instruction address.
Now, I could use .align
to get this instruction on a word boundary, in which case I can simply add 0. However, I would like to not use nop
s if not necessary.
Is something like the following possible?
.if alignment_of_next_instruction % 4 == 2
add r12,pc,#2
.else
add r12,pc,#0 @ just an example, mov would be better
.endif
str r12,[sp,#-4]!
The reason I cannot use nop
is that in some cases I don't need to get the address of the next instruction, but the instruction after that, or even one further. In those cases, an add
is necessary, but whether I need to add 10 or 12 (for example) depends on alignment.
If this isn't clear, here is what .align
would look like with my proposed syntax:
.if alignment_of_next_instruction % 4 == 2
nop
.endif
I cannot find anything in the ARM SDK reference guide, section Assembler expressions and operators (5.9), but perhaps I should be looking elsewhere.