Difference between ldr and ldr.w

I recently had to debug a MachO binary and I came across the following instruction: -

ldr.w r4, [r1, r0, lsl #2]

My understanding is that it ldr r4, [r1, r0, lsl #2]

shifts r0 left two times, adds it to r1 and plays out the result.

How is ldr.w different?

+3


source to share


3 answers


.W

- an optional instruction width specifier. This does not affect the behavior of the instruction as such, it just ensures that a 32-bit instruction is generated. See infocenter.arm.com for details :



LDR (pc-relative) in Thumb-2 You can use the .W width specifier to force the LDR to generate a 32-bit instruction in Thumb-2 code. LDR.W always generates a 32-bit instruction, even if the target can be reached using a 16-bit LDR. For direct links, an LDR without .W will always generate a 16-bit instruction in Thumb code, even if it crashes for a target that can be achieved using a 32-bit LDR Thumb-2 instruction.

+5


source


From the ARM Assembler Manual :



You can use the .W width specifier to force the LDR to generate 32-bit instructions in Thumb-2 code. LDR.W always generates 32-bit instructions, even if the target can be achieved using 16-bit LDRs.

+1


source


There are ARM and thumb instructions. Some processors have thumb2 extensions for thumb instructions that are good, bad, or otherwise similar to ARM instructions. Sometimes you may want one and get another. Inside the thumb, you can of course stick to .w to mean the wider 32-bit thumb2 instruction instead of the 16-bit thumb instruction. Sometimes the syntax makes it obvious which one you want, since the thumb2 instructions are quite limited compared to ARM and thumb2.

Also note that there are different flavors of thumb2, dramatically different between ARMv6 and ARMv7. I think I have counted 30-40 ARMv6 thumb2 instructions and over 100 ARMv7 thumb2 instructions. (you will see this if you use cortex-m3 and get used to it, now try using cortex-m0).

+1


source







All Articles