Arm cortex m0 LDR instruction

98 Views Asked by At

What is the difference between these instructions in the ARM Cortex M0?

LDR r1, r2 
LDR r1, [r2]
MOV r1, r2

Is any of them wrong?

If none of them is wrong, why would I use the second one to load from memory when I can type it in the first expression?

And if the first one means load from register to register, what about the third?

Get confused with the second instruction, as if I use a register I mean the value on it.

Note: I had no simulator.

1

There are 1 best solutions below

0
cooperised On

The first form you give is wrong. All forms of LDR and STR (except for the label form of LDR) use square brackets around the expression used for calculation of the target address.

This is consistent across all the forms, which makes reading them easier once you're familiar with them. For example,

LDR r0, [r2, #4]

adds 4 to the contents of r2 and then loads into r0 from the calculated address. On the other hand

LDR r0, [r2], #4

uses the unmodified contents of r2 for the load address, and then adds 4 to r2 ("post-increment"). For completeness the final constant offset form is

LDR r0, [r2, #4]!

which does the same as the first example, but then also writes the calculated address back into r2 after the load ("pre-increment"). The exclamation mark is the "writeback character" and appears optionally in some other instructions too, like LDM and STM.

Finally, for register-to-register moves, MOV is the instruction you want.

The documentation on these instructions is pretty good (here for ARMv6M devices like the Cortex-M0). There's a quick reference card here for all Thumb and Thumb-2 instruction sets, which is very useful but only if you can read it, which probably requires a bit of knowledge about the instruction set first.